From ebd099bef6ec15052c25db4429c530fe49e28184 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Thu, 8 Nov 2018 10:16:07 +0100 Subject: [PATCH] a3: memdump funktioniert --- src/c/a3.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/c/a3.c b/src/c/a3.c index de514bc..1f43a2b 100644 --- a/src/c/a3.c +++ b/src/c/a3.c @@ -7,21 +7,21 @@ void memdump(unsigned char *string) { printf("ADDR 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 0123456789ABCDEF\n"); - int lineLength = 16; - char array[lineLength * 4]; + int columns = 16; + int rows = 4; + char array[columns * rows]; - for(int i = 0; i <= 3; i++) { - //FIXME: address seems wrong + for(int i = 0; i < rows; i++) { printf("0x%02x ", string[i]); - for (int j = i * lineLength; j < (i + 1) * lineLength; j++) { + for (int j = i * columns; j < (i + 1) * columns; j++) { printf("%02x ", string[j]); if(string[j] < 0x20 || string[j] > 0x7e) array[j] = '.'; else array[j] = string[j]; } printf(" "); - for (int j = i * lineLength; j < (i + 1) * lineLength; j++) { + for (int j = i * columns; j < (i + 1) * columns; j++) { printf("%c", array[j]); } @@ -51,20 +51,25 @@ int main(int argc, char **argv) { if(buffer == NULL) return -1; + memdump((unsigned char *) argv[1]); - memdump(buffer); + printf("Laenge der Zeichenkette (inkl \\0): %i Bytes\n", (int) strlen(argv[1])); + printf("Ersetzen: '%c' mit '%c'\n", *argv[2], *argv[3]); + + unsigned char *copy = malloc(4 * 16 * sizeof(char)); + + strcpy((char *) copy, argv[1]); + 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]); printf("Suchzeichen wurde %i mal gefunden und ersetzt\n", count); printf("zuletzt an Addresse 0x%lx \n", (unsigned long) (&lastAddress)); - memdump((unsigned char*)argv[1]); + memdump(copy); - free(buffer); + free(copy); return 0; } \ No newline at end of file