summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorai4rei <ai4rei@54d463be-8e91-2dee-dedb-b68131a5f0ec>2011-03-07 09:51:33 +0000
committerai4rei <ai4rei@54d463be-8e91-2dee-dedb-b68131a5f0ec>2011-03-07 09:51:33 +0000
commit281445fc155a78e1e40124989678d99639094e8f (patch)
tree4eaa91e849f451ca5dff6db19575808161767d61 /src/common
parent11c9fddde1192be90bd83118af9fc95f93fd5064 (diff)
downloadhercules-281445fc155a78e1e40124989678d99639094e8f.tar.gz
hercules-281445fc155a78e1e40124989678d99639094e8f.tar.bz2
hercules-281445fc155a78e1e40124989678d99639094e8f.tar.xz
hercules-281445fc155a78e1e40124989678d99639094e8f.zip
* Cleaned up packet dumping code.
- Replaced utils function 'dump' with 'WriteDump' (files) and 'ShowDump' (console), and used those to replace inlined code in clif (related r10947). - Fixed clif_parse_debug not printing anything, when it is used with a variable length packet. - Added ability to dump invalid packets through define DUMP_INVALID_PACKET (clif.c). - Removed code to dump all incoming packets, as that can be achieved with the DUMP_UNKNOWN_PACKET code as well when needed (from r1009, related r10947). git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@14734 54d463be-8e91-2dee-dedb-b68131a5f0ec
Diffstat (limited to 'src/common')
-rw-r--r--src/common/utils.c70
-rw-r--r--src/common/utils.h3
2 files changed, 50 insertions, 23 deletions
diff --git a/src/common/utils.c b/src/common/utils.c
index 945e3e337..f1813ea41 100644
--- a/src/common/utils.c
+++ b/src/common/utils.c
@@ -5,6 +5,7 @@
#include "../common/mmo.h"
#include "../common/malloc.h"
#include "../common/showmsg.h"
+#include "socket.h"
#include "utils.h"
#include <stdio.h>
@@ -25,39 +26,64 @@
#include <sys/stat.h>
#endif
-// generate a hex dump of the first 'length' bytes of 'buffer'
-void dump(FILE* fp, const unsigned char* buffer, int length)
+
+/// Dumps given buffer into file pointed to by a handle.
+void WriteDump(FILE* fp, const void* buffer, size_t length)
{
- int i, j;
+ size_t i;
+ char hex[48+1], ascii[16+1];
- fprintf(fp, " Hex ASCII\n");
- fprintf(fp, " ----------------------------------------------- ----------------");
+ fprintf(fp, "--- 00-01-02-03-04-05-06-07-08-09-0A-0B-0C-0D-0E-0F 0123456789ABCDEF\n");
+ ascii[16] = 0;
- for (i = 0; i < length; i += 16)
+ for( i = 0; i < length; i++ )
{
- fprintf(fp, "\n%p ", &buffer[i]);
- for (j = i; j < i + 16; ++j)
+ char c = RBUFB(buffer,i);
+
+ ascii[i%16] = ISCNTRL(c) ? '.' : c;
+ sprintf(hex+(i%16)*3, "%02X ", RBUFB(buffer,i));
+
+ if( (i%16) == 15 )
{
- if (j < length)
- fprintf(fp, "%02hX ", buffer[j]);
- else
- fprintf(fp, " ");
+ fprintf(fp, "%03X %s %s\n", i/16, hex, ascii);
}
+ }
- fprintf(fp, " | ");
+ if( (i%16) != 0 )
+ {
+ ascii[i%16] = 0;
+ fprintf(fp, "%03X %-48s %-16s\n", i/16, hex, ascii);
+ }
+}
- for (j = i; j < i + 16; ++j)
+
+/// Dumps given buffer on the console.
+void ShowDump(const void* buffer, size_t length)
+{
+ size_t i;
+ char hex[48+1], ascii[16+1];
+
+ ShowDebug("--- 00-01-02-03-04-05-06-07-08-09-0A-0B-0C-0D-0E-0F 0123456789ABCDEF\n");
+ ascii[16] = 0;
+
+ for( i = 0; i < length; i++ )
+ {
+ char c = RBUFB(buffer,i);
+
+ ascii[i%16] = ISCNTRL(c) ? '.' : c;
+ sprintf(hex+(i%16)*3, "%02X ", RBUFB(buffer,i));
+
+ if( (i%16) == 15 )
{
- if (j < length) {
- if (buffer[j] > 31 && buffer[j] < 127)
- fprintf(fp, "%c", buffer[j]);
- else
- fprintf(fp, ".");
- } else
- fprintf(fp, " ");
+ ShowDebug("%03X %s %s\n", i/16, hex, ascii);
}
}
- fprintf(fp, "\n");
+
+ if( (i%16) != 0 )
+ {
+ ascii[i%16] = 0;
+ ShowDebug("%03X %-48s %-16s\n", i/16, hex, ascii);
+ }
}
diff --git a/src/common/utils.h b/src/common/utils.h
index 5cf3ff3cf..2fe078615 100644
--- a/src/common/utils.h
+++ b/src/common/utils.h
@@ -11,7 +11,8 @@
#include <stdio.h> // FILE*
// generate a hex dump of the first 'length' bytes of 'buffer'
-void dump(FILE* fp, const unsigned char* buffer, int length);
+void WriteDump(FILE* fp, const void* buffer, size_t length);
+void ShowDump(const void* buffer, size_t length);
void findfile(const char *p, const char *pat, void (func)(const char*));
bool exists(const char* filename);