Debian Codegraph @

src/common/hexdump.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1998 Mark Baysinger ([email protected])
3 * Copyright (C) 1998,1999 Ross Combs ([email protected])
4 * Copyright (C) 2004 Donny Redmond ([email protected])
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 "common/hexdump.h"
22
23 #include "common/packet.h"
24 #include "common/eventlog.h"
25 #include "common/setup_after.h"
26
27 namespace pvpgn
28 {
29
30 extern void hexdump(std::FILE * stream, void const * data, unsigned int len)
31 {
32 unsigned int i;
33 char dst[100];
34 unsigned char * datac;
35
36 if (!data) {
37 eventlog(eventlog_level_error, __FUNCTION__, "got NULL data");
38 return;
39 }
40
41 if (!stream) {
42 eventlog(eventlog_level_error, __FUNCTION__, "got NULL stream");
43 return;
44 }
45
46 for (i = 0, datac = (unsigned char*)data; i < len; i += 16, datac += 16)
47 {
48 hexdump_string(datac, (len - i < 16) ? (len - i) : 16, dst, i);
49 std::fprintf(stream, "%s\n", dst);
50 std::fflush(stream);
51 }
52 }
53
54 extern void hexdump_string(unsigned char * data, unsigned int datalen, char * dst, unsigned int counter)
55 {
56 unsigned int c;
57 int tlen = 0;
58 unsigned char *datatmp;
59
60 datatmp = data;
61 tlen += std::sprintf((dst + tlen), "%04X: ", counter);
62
63 for (c = 0; c < 8; c++) /* left half of hex dump */
64 if (c < datalen)
65 tlen += std::sprintf((dst + tlen), "%02X ", *(datatmp++));
66 else
67 tlen += std::sprintf((dst + tlen), " "); /* pad if short line */
68
69 tlen += std::sprintf((dst + tlen), " ");
70
71 for (c = 8; c < 16; c++) /* right half of hex dump */
72 if (c < datalen)
73 tlen += std::sprintf((dst + tlen), "%02X ", *(datatmp++));
74 else
75 tlen += std::sprintf((dst + tlen), " "); /* pad if short line */
76
77 tlen += std::sprintf((dst + tlen), " ");
78
79 for (c = 0, datatmp = data; c < 16; c++, datatmp++) /* ASCII dump */
80 if (c < datalen) {
81 if (*datatmp >= 32 && *datatmp < 127)
82 tlen += std::sprintf((dst + tlen), "%c", *datatmp);
83 else
84 tlen += std::sprintf((dst + tlen), "."); /* put this for non-printables */
85 }
86
87 }
88
89 }