EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
utils_hex.cpp
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Zhur
24 */
25 
26 #include "eve-core.h"
27 
28 #include "log/logsys.h"
29 #include "utils/utils_string.h"
30 
32 
33 void build_hex_line( const uint8* buffer, size_t length, size_t offset, char* ret, unsigned int padding )
34 {
35  ret += snprintf( ret, length, "%0*zX:", padding, offset );
36 
37  char printable[17];
38 
39  for( size_t i = 0; i < 16; i++ )
40  {
41  if( i == 8 )
42  {
43  ret += snprintf( ret, length, " -" );
44  }
45 
46  if( ( i + offset ) < length )
47  {
48  uint8 c = *(const uint8*)( buffer + offset + i );
49 
50  ret += snprintf( ret, length, " %02X", c );
51  printable[i] = ( IsPrintable( c ) ? (char)c : '.' );
52  }
53  else
54  {
55  ret += snprintf( ret, length, " " );
56  printable[i] = 0;
57  }
58  }
59 
60  snprintf( ret, length, " | %.16s", printable );
61 }
62 
63 void pfxHexDump( const char* pfx, FILE* into, const uint8* data, size_t length )
64 {
65  char buffer[80];
66 
67  for( uint32 offset = 0; offset < length; offset += 16 )
68  {
69  build_hex_line( data, length, offset, buffer, 4 );
70 
71  fprintf( into, "%s%s\n", pfx, buffer );
72  }
73 }
74 
75 void pfxHexDump( const char* pfx, LogType type, const uint8* data, size_t length )
76 {
77  char buffer[80];
78 
79  for( uint32 offset = 0; offset < length; offset += 16 )
80  {
81  build_hex_line( data, length, offset, buffer, 4 );
82 
83  _log( type, "%s%s", pfx, buffer );
84  }
85 }
86 
87 void pfxHexDumpPreview( const char* pfx, FILE* into, const uint8* data, size_t length )
88 {
89  char buffer[80];
90 
91  if( length > HEX_DUMP_PREVIEW_LIMIT )
92  {
93  pfxHexDump( pfx, into, data, HEX_DUMP_PREVIEW_LIMIT - 32 );
94  fprintf( into, "%s ... truncated ...\n", pfx );
95 
96  build_hex_line( data, length, length - 16, buffer, 4 );
97  fprintf( into, "%s%s\n", pfx, buffer );
98  }
99  else
100  pfxHexDump( pfx, into, data, length );
101 }
102 
103 void pfxHexDumpPreview( const char* pfx, LogType type, const uint8* data, size_t length )
104 {
105  char buffer[80];
106 
107  if( length > HEX_DUMP_PREVIEW_LIMIT )
108  {
109  pfxHexDump( pfx, type, data, HEX_DUMP_PREVIEW_LIMIT - 32 );
110  _log( type, "%s ... truncated ...", pfx );
111 
112  build_hex_line( data, length, length - 16, buffer, 4 );
113  _log( type, "%s%s", pfx, buffer );
114  }
115  else
116  pfxHexDump( pfx, type, data, length );
117 }
unsigned __int8 uint8
Definition: eve-compat.h:46
#define _log(type, fmt,...)
Definition: logsys.h:124
const uint32 HEX_DUMP_PREVIEW_LIMIT
Definition: utils_hex.cpp:31
void build_hex_line(const uint8 *buffer, size_t length, size_t offset, char *ret, unsigned int padding)
Build a printable line suitable for hex dump.
Definition: utils_hex.cpp:33
void pfxHexDump(const char *pfx, FILE *into, const uint8 *data, size_t length)
Definition: utils_hex.cpp:63
bool IsPrintable(const PyString *str)
Checks whether string is printable.
Definition: EVEUtils.cpp:31
#define snprintf
Definition: eve-compat.h:184
unsigned __int32 uint32
Definition: eve-compat.h:50
LogType
Definition: logsys.h:59
void pfxHexDumpPreview(const char *pfx, FILE *into, const uint8 *data, size_t length)
Definition: utils_hex.cpp:87