diff --git a/src/c/a3.c b/src/c/a3.c index a61f18f..d4420c8 100644 --- a/src/c/a3.c +++ b/src/c/a3.c @@ -1,30 +1,84 @@ #include "a3.h" #include #include +#include +#include void memdump(unsigned char *string) { + int i; + unsigned char *pc = string; + int length = strlen((const char *) string); + unsigned char buff[length]; + for (i = 0; i < length; i++) { + + if ((i % 16) == 0) { + if (i != 0) + printf(" %s\n", buff); + printf(" 0x%04x ", i); + } + + printf(" %02x", pc[i]); + + if ((pc[i] < 0x20) || (pc[i] > 0x7e)) { + buff[i % 16] = '.'; + } else { + buff[i % 16] = pc[i]; + } + + buff[(i % 16) + 1] = '\0'; + } + while ((i % 16) != 0) { + printf(" "); + i++; + } + + printf(" %s\n", buff); } int memreplace(char *string, char cin, char cout, char **caddr) { - + return 42; } int main(int argc, char **argv) { - if(argc <= 3) { - printf("Not enough arguments"); + if(argc != 4) { + printf("Not enough arguments or to many arguments"); return -1; } + size_t size = 1; + for (int i = 1; i < argc; i++) { + size += strlen(argv[i]) + 1; + } + char *str = malloc(size); + if (!str) { + fprintf(stderr, "No memory\n"); + return 2; + } - memdump((unsigned char*)argv[1]); + size = 0; + for (int i = 1; i < argc; i++) { + + size += sprintf(str + size, "%s ", argv[i]); + + assert(str[size] == '\0'); + } + + str[--size] = '\0'; + + printf("%s\n", str); + + + memdump((unsigned char*)str); char **lastAddress; int count = memreplace(argv[1], *argv[2], *argv[3], 0); - printf("Laenge der Zeichenkette (inkl \\0): %i Bytes\n", sizeof(argv[1])); + printf("Laenge der Zeichenkette (inkl \\0): %i Bytes\n", 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]); + + free(str); } \ No newline at end of file diff --git a/src/c/a3.h b/src/c/a3.h index 5752e0e..776856f 100644 --- a/src/c/a3.h +++ b/src/c/a3.h @@ -1,2 +1,7 @@ +#ifndef HnP_a3 +#define HnP_a3 + void mempdump(unsigned char *string); -int memreplace(char *string, char cin, char cout, char **caddr); \ No newline at end of file +int memreplace(char *string, char cin, char cout, char **caddr); + +#endif \ No newline at end of file