home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.pdx.edu / 2014.02.ftp.ee.pdx.edu.tar / ftp.ee.pdx.edu / pub / users / Harry / Blitz / version-1-0 / BlitzSrc / hexdump.c < prev    next >
Text File  |  2006-09-25  |  2KB  |  120 lines

  1. /**********
  2.  
  3.    hexdump  -  Harry Porter
  4.  
  5.    This program reads a file from the standard input containing
  6.    arbitrary hex data and outputs it to the standard output
  7.    in a format suitable for human examination.  Unlike various
  8.    versions of 'od', it prints the bytes in exactly the order read.
  9.  
  10. **********/
  11.  
  12. #define EOF -1
  13.  
  14. int row [16];
  15. int size;
  16.  
  17. main () {
  18.    int addr;
  19.  
  20.    /* Each execution of this loop prints a single output line. */
  21.    addr = 0;
  22.    readline ();
  23.    while (size > 0) {
  24.      putlong (addr);
  25.      printf (":  ");
  26.      printline ();
  27.      addr = addr + 16;
  28.      readline ();
  29.    }
  30.  
  31. }
  32.  
  33.  
  34.  
  35. /* readline - This routine reads in the next 16 bytes from the file and
  36.               places them in the array named 'row', setting 'size' to
  37.               be the number of bytes read in.  Size will be less than
  38.               16 if EOF was encountered, and may possibly be 0.  */
  39. readline () {
  40. int c;
  41.   size = 0;
  42.   c = getchar ();
  43.   while (c != EOF) {
  44.     row [size] = c;
  45.     size = size + 1;
  46.     if (size >= 16) break;
  47.       c = getchar ();
  48.   }
  49. }
  50.  
  51.  
  52.  
  53. /* putlong - This routine is passed an integer, which it displays as 8
  54.              hex digits.  */
  55. putlong (i) {
  56.   putbyt ((i>>24) & 0x000000ff);
  57.   putbyt ((i>>16) & 0x000000ff);
  58.   putbyt ((i>>8) & 0x000000ff);
  59.   putbyt ((i>>0) & 0x000000ff);
  60. }
  61.  
  62.  
  63.  
  64. /* printline - This routine prints the current 'row'.  */
  65. printline () {
  66.   int i, c;
  67.   if (size > 0) {
  68.     i = 0;
  69.     while (i<16) {
  70.       if (i < size) {
  71.         putbyt (row[i]);
  72.       } else {
  73.         printf ("  ");
  74.       }
  75.       i++;
  76.       if ((i%2) == 0) {
  77.         putchar (' ');
  78.       }
  79.       if ((i%4) == 0) {
  80.         putchar (' ');
  81.       }
  82.     }
  83.     printf ("  ");
  84.     for (i=0; i<size; i++) {
  85.       c = row[i];
  86.       if ((c>=' ') && (c <= '~')) {
  87.         putchar (c);
  88.       } else {
  89.         putchar ('.');
  90.       }
  91.     }
  92.     printf ("\n");
  93.   }
  94. }
  95.  
  96.  
  97.  
  98. /* putbyt - This routine is passed a byte (i.e., an integer < 256) which
  99.           it displays as 2 hex characters.  If passed a number out of that
  100.           range, it outputs nothing.  */
  101. putbyt (c)
  102.    int c;
  103. {
  104.   int i;
  105.   if ((c >= 0) && (c <= 255)) {
  106.     i = (c & 0x000000f0) >> 4;
  107.     if (i < 10) {
  108.       putchar ('0' + i);
  109.     } else {
  110.       putchar ('A' + i - 10);
  111.     }
  112.     i = (c & 0x0000000f) >> 0;
  113.     if (i < 10) {
  114.       putchar ('0' + i);
  115.     } else {
  116.       putchar ('A' + i - 10);
  117.     }
  118.   }
  119. }
  120.