Hexdump.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. using System;
  18. using System.IO;
  19. namespace SharpCifs.Util
  20. {
  21. public class Hexdump
  22. {
  23. private static readonly string Nl = @"\r\n"; //Runtime.GetProperty("line.separator");
  24. private static readonly int NlLength = Nl.Length;
  25. private static readonly char[] SpaceChars =
  26. {
  27. ' ', ' ', ' ', ' ', ' '
  28. , ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
  29. , ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
  30. , ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
  31. };
  32. public static readonly char[] HexDigits
  33. = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  34. private static bool IsIsoControl(char c)
  35. {
  36. return (c >= '\u0000' && c <= '\u001F') || (c >= '\u007F' && c <= '\u009F');
  37. }
  38. /// <summary>
  39. /// Generate "hexdump" output of the buffer at src like the following:
  40. /// <p><blockquote><pre>
  41. /// 00000: 04 d2 29 00 00 01 00 00 00 00 00 01 20 45 47 46 |..).........
  42. /// </summary>
  43. /// <remarks>
  44. /// Generate "hexdump" output of the buffer at src like the following:
  45. /// <p><blockquote><pre>
  46. /// 00000: 04 d2 29 00 00 01 00 00 00 00 00 01 20 45 47 46 |..)......... EGF|
  47. /// 00010: 43 45 46 45 45 43 41 43 41 43 41 43 41 43 41 43 |CEFEECACACACACAC|
  48. /// 00020: 41 43 41 43 41 43 41 43 41 43 41 41 44 00 00 20 |ACACACACACAAD.. |
  49. /// 00030: 00 01 c0 0c 00 20 00 01 00 00 00 00 00 06 20 00 |..... ........ .|
  50. /// 00040: ac 22 22 e1 |."". |
  51. /// </blockquote></pre>
  52. /// </remarks>
  53. public static void ToHexdump(TextWriter ps, byte[] src, int srcIndex, int length)
  54. {
  55. if (length == 0)
  56. {
  57. return;
  58. }
  59. int s = length % 16;
  60. int r = (s == 0) ? length / 16 : length / 16 + 1;
  61. char[] c = new char[r * (74 + NlLength)];
  62. char[] d = new char[16];
  63. int i;
  64. int si = 0;
  65. int ci = 0;
  66. do
  67. {
  68. ToHexChars(si, c, ci, 5);
  69. ci += 5;
  70. c[ci++] = ':';
  71. do
  72. {
  73. if (si == length)
  74. {
  75. int n = 16 - s;
  76. Array.Copy(SpaceChars, 0, c, ci, n * 3);
  77. ci += n * 3;
  78. Array.Copy(SpaceChars, 0, d, s, n);
  79. break;
  80. }
  81. c[ci++] = ' ';
  82. i = src[srcIndex + si] & 0xFF;
  83. ToHexChars(i, c, ci, 2);
  84. ci += 2;
  85. if (i < 0 || IsIsoControl((char)i))
  86. {
  87. d[si % 16] = '.';
  88. }
  89. else
  90. {
  91. d[si % 16] = (char)i;
  92. }
  93. }
  94. while ((++si % 16) != 0);
  95. c[ci++] = ' ';
  96. c[ci++] = ' ';
  97. c[ci++] = '|';
  98. Array.Copy(d, 0, c, ci, 16);
  99. ci += 16;
  100. c[ci++] = '|';
  101. //Sharpen.Runtime.GetCharsForString(NL, 0, NL_LENGTH, c, ci);
  102. c = Nl.ToCharArray(0, NlLength);
  103. ci += NlLength;
  104. }
  105. while (si < length);
  106. ps.WriteLine(c);
  107. }
  108. /// <summary>
  109. /// This is an alternative to the <code>java.lang.Integer.toHexString</cod>
  110. /// method.
  111. /// </summary>
  112. /// <remarks>
  113. /// This is an alternative to the <code>java.lang.Integer.toHexString</cod>
  114. /// method. It is an efficient relative that also will pad the left side so
  115. /// that the result is <code>size</code> digits.
  116. /// </remarks>
  117. public static string ToHexString(int val, int size)
  118. {
  119. char[] c = new char[size];
  120. ToHexChars(val, c, 0, size);
  121. return new string(c);
  122. }
  123. public static string ToHexString(long val, int size)
  124. {
  125. char[] c = new char[size];
  126. ToHexChars(val, c, 0, size);
  127. return new string(c);
  128. }
  129. public static string ToHexString(byte[] src, int srcIndex, int size)
  130. {
  131. char[] c = new char[size];
  132. size = (size % 2 == 0) ? size / 2 : size / 2 + 1;
  133. for (int i = 0, j = 0; i < size; i++)
  134. {
  135. c[j++] = HexDigits[(src[i] >> 4) & 0x0F];
  136. if (j == c.Length)
  137. {
  138. break;
  139. }
  140. c[j++] = HexDigits[src[i] & 0x0F];
  141. }
  142. return new string(c);
  143. }
  144. /// <summary>
  145. /// This is the same as
  146. /// <see cref="ToHexString(int, int)">ToHexString(int, int)</see>
  147. /// but provides a more practical form when trying to avoid
  148. /// <see cref="string">string</see>
  149. /// concatenation and
  150. /// <see cref="System.Text.StringBuilder">System.Text.StringBuilder</see>
  151. /// .
  152. /// </summary>
  153. public static void ToHexChars(int val, char[] dst, int dstIndex, int size)
  154. {
  155. while (size > 0)
  156. {
  157. int i = dstIndex + size - 1;
  158. if (i < dst.Length)
  159. {
  160. dst[i] = HexDigits[val & 0x000F];
  161. }
  162. if (val != 0)
  163. {
  164. val = (int)(((uint)val) >> 4);
  165. }
  166. size--;
  167. }
  168. }
  169. public static void ToHexChars(long val, char[] dst, int dstIndex, int size)
  170. {
  171. while (size > 0)
  172. {
  173. dst[dstIndex + size - 1] = HexDigits[(int)(val & 0x000FL)];
  174. if (val != 0)
  175. {
  176. val = (long)(((ulong)val) >> 4);
  177. }
  178. size--;
  179. }
  180. }
  181. }
  182. }