a3: weiterer Fortschritt

This commit is contained in:
Johannes Theiner 2018-11-02 12:59:31 +01:00
parent ffe9cb19b9
commit ed1dde77f3

View File

@ -1,39 +1,32 @@
#include "a3.h"
#include <stdlib.h>
#include <stdio.h>
#include <mem.h>
#include <assert.h>
#include <memory.h>
void memdump(unsigned char *string) {
int i;
unsigned char *pc = string;
int length = strlen((const char *) string);
unsigned char buff[length];
unsigned char buffer[16];
for (i = 0; i < length; i++) {
if ((i % 16) == 0) {
if (i != 0)
printf(" %s\n", buff);
printf(" 0x%04x ", i);
printf("ADDR 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 0123456789ABCDEF\n");
for(i = 0; i <= 32; i++) {
if((i % 16) == 0) {
if(i != 0) printf(" %s\n", string++);
printf("0x%04x ", *string);
}
printf(" %02x", pc[i]);
if ((pc[i] < 0x20) || (pc[i] > 0x7e)) {
buff[i % 16] = '.';
printf(" %02x", string[i]);
if ((string[i] < 0x20) || (string[i] > 0x7e)) {
buffer[i % 16] = '.';
} else {
buff[i % 16] = pc[i];
buffer[i % 16] = string[i];
}
buff[(i % 16) + 1] = '\0';
buffer[(i % 16) + 1] = '\0';
}
while ((i % 16) != 0) {
printf(" ");
i++;
}
printf(" %s\n", buff);
printf(" %s\n", buffer);
}
@ -47,38 +40,16 @@ int main(int argc, char **argv) {
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;
}
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);
memdump((unsigned char*)argv);
char **lastAddress;
int count = memreplace(argv[1], *argv[2], *argv[3], 0);
printf("Laenge der Zeichenkette (inkl \\0): %i Bytes\n", strlen(argv[1]));
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]);
free(str);
free(argv);
}