a3: memdump teilweise fertig
This commit is contained in:
parent
0fc98cc661
commit
ffe9cb19b9
64
src/c/a3.c
64
src/c/a3.c
|
@ -1,30 +1,84 @@
|
||||||
#include "a3.h"
|
#include "a3.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <mem.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
void memdump(unsigned char *string) {
|
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) {
|
int memreplace(char *string, char cin, char cout, char **caddr) {
|
||||||
|
return 42;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
if(argc <= 3) {
|
if(argc != 4) {
|
||||||
printf("Not enough arguments");
|
printf("Not enough arguments or to many arguments");
|
||||||
return -1;
|
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;
|
char **lastAddress;
|
||||||
int count = memreplace(argv[1], *argv[2], *argv[3], 0);
|
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("Ersetzen: '%c' mit '%c'\n", *argv[2], *argv[3]);
|
||||||
printf("Suchzeichen wurde %i mal gefunden und ersetzt\n", count);
|
printf("Suchzeichen wurde %i mal gefunden und ersetzt\n", count);
|
||||||
printf("zuletzt an Addresse 0x%lx \n", (unsigned long) (&lastAddress));
|
printf("zuletzt an Addresse 0x%lx \n", (unsigned long) (&lastAddress));
|
||||||
memdump((unsigned char*)argv[1]);
|
memdump((unsigned char*)argv[1]);
|
||||||
|
|
||||||
|
free(str);
|
||||||
}
|
}
|
|
@ -1,2 +1,7 @@
|
||||||
|
#ifndef HnP_a3
|
||||||
|
#define HnP_a3
|
||||||
|
|
||||||
void mempdump(unsigned char *string);
|
void mempdump(unsigned char *string);
|
||||||
int memreplace(char *string, char cin, char cout, char **caddr);
|
int memreplace(char *string, char cin, char cout, char **caddr);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue