home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / dsniff / decode_hex.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-16  |  1.2 KB  |  61 lines

  1. /*
  2.   decode_hex.c
  3.  
  4.   Hex dump, for debugging.
  5.   
  6.   Copyright (c) 2000 Dug Song <dugsong@monkey.org>
  7.   
  8.   $Id: decode_hex.c,v 1.1 2000/05/16 17:31:14 dugsong Exp $
  9. */
  10.  
  11. #include <sys/types.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15. #include "decode.h"
  16.  
  17. /* adapted from OpenBSD tcpdump: dump the buffer in emacs-hexl format */
  18.  
  19. int
  20. decode_hex(u_char *buf, int len)
  21. {
  22.     u_int i, j, jm;
  23.     int c;
  24.  
  25.     Buf[0] = '\0';
  26.     
  27.     for (i = 0; i < len; i += 0x10) {
  28.             snprintf(Buf + strlen(Buf), sizeof(Buf) - strlen(Buf),
  29.              "  %04x: ", (u_int)(i));
  30.         jm = len - i;
  31.         jm = jm > 16 ? 16 : jm;
  32.         
  33.         for (j = 0; j < jm; j++) {
  34.             if ((j % 2) == 1)
  35.                 snprintf(Buf + strlen(Buf),
  36.                      sizeof(Buf) - strlen(Buf),
  37.                      "%02x ", (u_int)buf[i+j]);
  38.             else
  39.                 snprintf(Buf + strlen(Buf),
  40.                      sizeof(Buf) - strlen(Buf),
  41.                      "%02x", (u_int)buf[i+j]);
  42.         }
  43.         for (; j < 16; j++) {
  44.             if ((j % 2) == 1)
  45.                 strlcat(Buf, "   ", sizeof(Buf));
  46.             else
  47.                 strlcat(Buf, "  ", sizeof(Buf));
  48.         }
  49.         strlcat(Buf, " ", sizeof(Buf));
  50.         
  51.         for (j = 0; j < jm; j++) {
  52.             c = buf[i+j];
  53.             c = isprint(c) ? c : '.';
  54.             snprintf(Buf + strlen(Buf), sizeof(Buf) - strlen(Buf),
  55.                  "%c", c);
  56.         }
  57.         strlcat(Buf, "\n", sizeof(Buf));
  58.     }
  59.     return (strlen(Buf));
  60. }
  61.