1 /*
2  * Copyright (C) 1998  Mark Baysinger (mbaysing@ucsd.edu)
3  * Copyright (C) 1998,1999  Ross Combs (rocombs@cs.nmsu.edu)
4  * Copyright (C) 2004  Donny Redmond (dredmond@linuxmail.org)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  */
20 #include "common/setup_before.h"
21 #include <stdio.h>
22 #ifdef HAVE_STDDEF_H
23 # include <stddef.h>
24 #else
25 # ifndef NULL
26 #  define NULL ((void *)0)
27 # endif
28 #endif
29 #include <string.h>
30 #include "common/packet.h"
31 #include "common/hexdump.h"
32 #include "common/eventlog.h"
33 #include "common/setup_after.h"
34 
hexdump(FILE * stream,void const * data,unsigned int len)35 extern void hexdump(FILE * stream, void const * data, unsigned int len)
36 {
37     unsigned int i;
38     char dst[100];
39     unsigned char * datac;
40 
41     if (!data) {
42 	eventlog(eventlog_level_error, __FUNCTION__, "got NULL data");
43         return;
44     }
45 
46     if (!stream) {
47 	eventlog(eventlog_level_error, __FUNCTION__, "got NULL stream");
48 	return;
49     }
50 
51     for (i = 0, datac = (char*)data ; i < len; i += 16, datac += 16)
52     {
53 	hexdump_string(datac, (len - i < 16) ? (len - i) : 16, dst, i);
54 	fprintf(stream, "%s\n", dst);
55 	fflush(stream);
56     }
57 }
58 
hexdump_string(unsigned char * data,unsigned int datalen,char * dst,unsigned int counter)59 extern void hexdump_string(unsigned char * data, unsigned int datalen, char * dst, unsigned int counter)
60 {
61     unsigned int c;
62     int tlen = 0;
63     unsigned char *datatmp;
64 
65     datatmp = data;
66     tlen += sprintf((dst+tlen), "%04X:   ", counter);
67 
68     for (c=0; c<8; c++) /* left half of hex dump */
69         if (c<datalen)
70     	    tlen += sprintf((dst+tlen), "%02X ", *(datatmp++));
71 	else
72 	    tlen += sprintf((dst+tlen), "   "); /* pad if short line */
73 
74     tlen += sprintf((dst+tlen),"  ");
75 
76     for (c=8; c<16; c++) /* right half of hex dump */
77         if (c<datalen)
78 	    tlen += sprintf((dst+tlen), "%02X ", *(datatmp++));
79 	else
80 	    tlen += sprintf((dst+tlen),"   "); /* pad if short line */
81 
82     tlen += sprintf((dst+tlen),"   ");
83 
84     for (c=0, datatmp = data; c<16; c++, datatmp++) /* ASCII dump */
85         if (c<datalen) {
86 	    if (*datatmp >=32 && *datatmp<127)
87 		tlen += sprintf((dst+tlen), "%c", *datatmp);
88 	    else
89 		tlen += sprintf((dst+tlen), "."); /* put this for non-printables */
90 	}
91 
92 }
93