Hexdump.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. public static readonly char[] HexDigits = { '0', '1', '2', '3', '4',
  30. '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  31. private static bool IsIsoControl(char c)
  32. {
  33. return (c >= '\u0000' && c <= '\u001F') || (c >= '\u007F' && c <= '\u009F');
  34. }
  35. /// <summary>
  36. /// Generate "hexdump" output of the buffer at src like the following:
  37. /// <p><blockquote><pre>
  38. /// 00000: 04 d2 29 00 00 01 00 00 00 00 00 01 20 45 47 46 |..).........
  39. /// </summary>
  40. /// <remarks>
  41. /// Generate "hexdump" output of the buffer at src like the following:
  42. /// <p><blockquote><pre>
  43. /// 00000: 04 d2 29 00 00 01 00 00 00 00 00 01 20 45 47 46 |..)......... EGF|
  44. /// 00010: 43 45 46 45 45 43 41 43 41 43 41 43 41 43 41 43 |CEFEECACACACACAC|
  45. /// 00020: 41 43 41 43 41 43 41 43 41 43 41 41 44 00 00 20 |ACACACACACAAD.. |
  46. /// 00030: 00 01 c0 0c 00 20 00 01 00 00 00 00 00 06 20 00 |..... ........ .|
  47. /// 00040: ac 22 22 e1 |."". |
  48. /// </blockquote></pre>
  49. /// </remarks>
  50. public static void ToHexdump(TextWriter ps, byte[] src, int srcIndex, int length)
  51. {
  52. if (length == 0)
  53. {
  54. return;
  55. }
  56. int s = length % 16;
  57. int r = (s == 0) ? length / 16 : length / 16 + 1;
  58. char[] c = new char[r * (74 + NlLength)];
  59. char[] d = new char[16];
  60. int i;
  61. int si = 0;
  62. int ci = 0;
  63. do
  64. {
  65. ToHexChars(si, c, ci, 5);
  66. ci += 5;
  67. c[ci++] = ':';
  68. do
  69. {
  70. if (si == length)
  71. {
  72. int n = 16 - s;
  73. Array.Copy(SpaceChars, 0, c, ci, n * 3);
  74. ci += n * 3;
  75. Array.Copy(SpaceChars, 0, d, s, n);
  76. break;
  77. }
  78. c[ci++] = ' ';
  79. i = src[srcIndex + si] & 0xFF;
  80. ToHexChars(i, c, ci, 2);
  81. ci += 2;
  82. if (i < 0 || IsIsoControl((char)i))
  83. {
  84. d[si % 16] = '.';
  85. }
  86. else
  87. {
  88. d[si % 16] = (char)i;
  89. }
  90. }
  91. while ((++si % 16) != 0);
  92. c[ci++] = ' ';
  93. c[ci++] = ' ';
  94. c[ci++] = '|';
  95. Array.Copy(d, 0, c, ci, 16);
  96. ci += 16;
  97. c[ci++] = '|';
  98. //Sharpen.Runtime.GetCharsForString(NL, 0, NL_LENGTH, c, ci);
  99. c = Nl.ToCharArray(0, NlLength);
  100. ci += NlLength;
  101. }
  102. while (si < length);
  103. ps.WriteLine(c);
  104. }
  105. /// <summary>
  106. /// This is an alternative to the <code>java.lang.Integer.toHexString</cod>
  107. /// method.
  108. /// </summary>
  109. /// <remarks>
  110. /// This is an alternative to the <code>java.lang.Integer.toHexString</cod>
  111. /// method. It is an efficient relative that also will pad the left side so
  112. /// that the result is <code>size</code> digits.
  113. /// </remarks>
  114. public static string ToHexString(int val, int size)
  115. {
  116. char[] c = new char[size];
  117. ToHexChars(val, c, 0, size);
  118. return new string(c);
  119. }
  120. public static string ToHexString(long val, int size)
  121. {
  122. char[] c = new char[size];
  123. ToHexChars(val, c, 0, size);
  124. return new string(c);
  125. }
  126. public static string ToHexString(byte[] src, int srcIndex, int size)
  127. {
  128. char[] c = new char[size];
  129. size = (size % 2 == 0) ? size / 2 : size / 2 + 1;
  130. for (int i = 0, j = 0; i < size; i++)
  131. {
  132. c[j++] = HexDigits[(src[i] >> 4) & 0x0F];
  133. if (j == c.Length)
  134. {
  135. break;
  136. }
  137. c[j++] = HexDigits[src[i] & 0x0F];
  138. }
  139. return new string(c);
  140. }
  141. /// <summary>
  142. /// This is the same as
  143. /// <see cref="ToHexString(int, int)">ToHexString(int, int)</see>
  144. /// but provides a more practical form when trying to avoid
  145. /// <see cref="string">string</see>
  146. /// concatenation and
  147. /// <see cref="System.Text.StringBuilder">System.Text.StringBuilder</see>
  148. /// .
  149. /// </summary>
  150. public static void ToHexChars(int val, char[] dst, int dstIndex, int size)
  151. {
  152. while (size > 0)
  153. {
  154. int i = dstIndex + size - 1;
  155. if (i < dst.Length)
  156. {
  157. dst[i] = HexDigits[val & 0x000F];
  158. }
  159. if (val != 0)
  160. {
  161. val = (int)(((uint)val) >> 4);
  162. }
  163. size--;
  164. }
  165. }
  166. public static void ToHexChars(long val, char[] dst, int dstIndex, int size)
  167. {
  168. while (size > 0)
  169. {
  170. dst[dstIndex + size - 1] = HexDigits[(int)(val & 0x000FL)];
  171. if (val != 0)
  172. {
  173. val = (long)(((ulong)val) >> 4);
  174. }
  175. size--;
  176. }
  177. }
  178. }
  179. }