From 27127d5d4c5c7da4fb84731ba0d44331b5658885 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Fri, 2 Nov 2018 22:03:51 +0100 Subject: [PATCH] =?UTF-8?q?a3:=20memdump=20fast=20funktionst=C3=BCchtig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/c/a3.c | 56 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/src/c/a3.c b/src/c/a3.c index 5ce1db0..de514bc 100644 --- a/src/c/a3.c +++ b/src/c/a3.c @@ -5,35 +5,38 @@ #include void memdump(unsigned char *string) { - int i; - unsigned char buffer[64]; - printf("ADDR 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 0123456789ABCDEF\n"); - for(i = 0; i <= 64; i++) { - string++; - if((i % 16) == 0) { - if(i != 0) printf(" %s\n", string); - printf("0x%04x ", *string); - buffer[i] = string[i]; + + int lineLength = 16; + char array[lineLength * 4]; + + + for(int i = 0; i <= 3; i++) { + //FIXME: address seems wrong + printf("0x%02x ", string[i]); + for (int j = i * lineLength; j < (i + 1) * lineLength; j++) { + printf("%02x ", string[j]); + if(string[j] < 0x20 || string[j] > 0x7e) array[j] = '.'; + else array[j] = string[j]; } - printf(" %02x", string[i]); - /*if ((string[i] < 0x20) || (string[i] > 0x7e)) - buffer[i] = '.'; - else - buffer[i] = string[i];*/ + printf(" "); - buffer[(i % 16) + 1] = '\0'; + for (int j = i * lineLength; j < (i + 1) * lineLength; j++) { + printf("%c", array[j]); + + } + + printf("\n"); } - while ((i % 16) != 0) { - printf(" "); - i++; - } + + } int memreplace(char *string, char cin, char cout, char **caddr) { + //memctl return 42; } @@ -43,10 +46,17 @@ int main(int argc, char **argv) { return -1; } + size_t size = 4 * 16; + unsigned char *buffer = malloc(size); + if(buffer == NULL) return -1; - memdump((unsigned char*)*argv); - char **lastAddress; - int count = memreplace(argv[1], *argv[2], *argv[3], 0); + + + + memdump(buffer); + + char **lastAddress = NULL; + int count = memreplace(argv[1], *argv[2], *argv[3], lastAddress); printf("Laenge der Zeichenkette (inkl \\0): %i Bytes\n", (int) strlen(argv[1])); printf("Ersetzen: '%c' mit '%c'\n", *argv[2], *argv[3]); @@ -54,7 +64,7 @@ int main(int argc, char **argv) { printf("zuletzt an Addresse 0x%lx \n", (unsigned long) (&lastAddress)); memdump((unsigned char*)argv[1]); - free(argv); + free(buffer); return 0; } \ No newline at end of file