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

  1. /*
  2.   hex.c
  3.   
  4.   Copyright (c) 2000 Dug Song <dugsong@monkey.org>
  5.   
  6.   $Id: hex.c,v 1.1 2000/05/16 17:31:15 dugsong Exp $
  7. */
  8.  
  9. #include <sys/types.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. #include "hex.h"
  14.  
  15. int
  16. hex_decode(char *src, u_char *dst, int dstsize)
  17. {
  18.     char *p, *pe;
  19.     u_char *q, *qe, ch, cl;
  20.     
  21.     pe = src + strlen(src);
  22.     qe = dst + dstsize;
  23.     
  24.     for (p = src, q = dst; p < pe && q < qe && isxdigit((int)*p); p += 2) {
  25.         ch = tolower(p[0]);
  26.         cl = tolower(p[1]);
  27.         
  28.         if ((ch >= '0') && (ch <= '9')) ch -= '0';
  29.         else if ((ch >= 'a') && (ch <= 'f')) ch -= 'a' - 10;
  30.         else return (-1);
  31.         
  32.         if ((cl >= '0') && (cl <= '9')) cl -= '0';
  33.         else if ((cl >= 'a') && (cl <= 'f')) cl -= 'a' - 10;
  34.         else return (-1);
  35.         
  36.         *q++ = (ch << 4) | cl;
  37.     }
  38.     return (q - dst);
  39. }
  40.  
  41. /* adapted from OpenBSD tcpdump: dump the buffer in emacs-hexl format */
  42. void
  43. hex_print(const u_char *buf, int len, int offset)
  44. {
  45.     u_int i, j, jm;
  46.     int c;
  47.     
  48.     printf("\n");
  49.     for (i = 0; i < len; i += 0x10) {
  50.         printf("  %04x: ", (u_int)(i + offset));
  51.         jm = len - i;
  52.         jm = jm > 16 ? 16 : jm;
  53.         
  54.         for (j = 0; j < jm; j++) {
  55.             if ((j % 2) == 1) printf("%02x ", (u_int) buf[i+j]);
  56.             else printf("%02x", (u_int) buf[i+j]);
  57.         }
  58.         for (; j < 16; j++) {
  59.             if ((j % 2) == 1) printf("   ");
  60.             else printf("  ");
  61.         }
  62.         printf(" ");
  63.         
  64.         for (j = 0; j < jm; j++) {
  65.             c = buf[i+j];
  66.             c = isprint(c) ? c : '.';
  67.             printf("%c", c);
  68.         }
  69.         printf("\n");
  70.     }
  71. }
  72.  
  73.