aaaaaa123456789

Hexadecimal dumping library

Aug 23rd, 2014
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. static void hexdump_small_dump(FILE *, void *, unsigned, unsigned);
  5.  
  6. void hex_dump (FILE * stream, void * data, unsigned length, unsigned initial_offset, int show_header) {
  7.   if (!(stream && data && length)) return;
  8.   if (show_header) fprintf(stream,
  9.     "          00 01 02 03  04 05 06 07 - 08 09 0A 0B  0C 0D 0E 0F  0123456789ABCDEF\n"
  10.     "--------- -- -- -- --  -- -- -- --   -- -- -- --  -- -- -- --  ----------------\n");
  11.   if ((length + (initial_offset % 16)) <= 16) {
  12.     hexdump_small_dump(stream, data, length, initial_offset);
  13.     return;
  14.   }
  15.   unsigned char * dp = data;
  16.   if (initial_offset % 16) {
  17.     unsigned char initseg = 16 - (initial_offset % 16);
  18.     hexdump_small_dump(stream, data, initseg, initial_offset);
  19.     dp += initseg;
  20.     initial_offset += initseg;
  21.     length -= initseg;
  22.   }
  23.   char line[82];
  24.   char buf[10];
  25.   unsigned char pos;
  26.   while (length >= 16) {
  27.     strcpy(line,
  28.       /* 0    5   10   15   20   25   30   35   40   45   50   55   60   65   70   75   80 */
  29.         "********: ?? ?? ?? ??  ?? ?? ?? ?? - ?? ?? ?? ??  ?? ?? ?? ??  ................\n");
  30.     sprintf(buf, "%08X", initial_offset);
  31.     memcpy(line, buf, 8);
  32.     for (pos = 0; pos < 16; pos ++) {
  33.       sprintf(buf, "%02hhX", dp[pos]);
  34.       memcpy(line + 10 + 3 * pos + (pos / 4) + (pos / 8), buf, 2);
  35.       if ((dp[pos] >= 32) && (dp[pos] <= 126)) line[63 + pos] = dp[pos];
  36.     }
  37.     fprintf(stream, "%s", line);
  38.     dp += 16;
  39.     initial_offset += 16;
  40.     length -= 16;
  41.   }
  42.   if (length) hexdump_small_dump(stream, dp, length, initial_offset);
  43. }
  44.  
  45. static void hexdump_small_dump (FILE * stream, void * data, unsigned length, unsigned initial_offset) {
  46.   char line[81];
  47.   char buf[10];
  48.   unsigned char start, pos, end;
  49.   memset(line, ' ', 79);
  50.   line[79] = '\n';
  51.   line[80] = 0;
  52.   line[8] = ':';
  53.   start = initial_offset % 16;
  54.   initial_offset &= ~15;
  55.   end = start + length;
  56.   sprintf(buf, "%08X", initial_offset);
  57.   memcpy(line, buf, 8);
  58.   if ((start < 8) && (end > 8)) line[35] = '-';
  59.   for (pos = start; pos < end; pos ++) {
  60.     sprintf(buf, "%02hhX", (pos - start)[(unsigned char *) data]);
  61.     memcpy(line + 10 + 3 * pos + (pos / 4) + (pos / 8), buf, 2);
  62.     line[63 + pos] = (((pos - start)[(unsigned char *) data] >= 32) && ((pos - start)[(unsigned char *) data] <= 126)) ?
  63.                      (pos - start)[(unsigned char *) data] : '.';
  64.   }
  65.   fprintf(stream, "%s", line);
  66. }
Add Comment
Please, Sign In to add comment