a3: memdump teilweise fertig

This commit is contained in:
Johannes Theiner 2018-11-02 08:44:34 +01:00
parent 0fc98cc661
commit ffe9cb19b9
2 changed files with 65 additions and 6 deletions

View File

@ -1,30 +1,84 @@
#include "a3.h"
#include <stdlib.h>
#include <stdio.h>
#include <mem.h>
#include <assert.h>
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);
}

View File

@ -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);
#endif