This commit is contained in:
Johannes Theiner 2018-11-28 12:04:53 +01:00
commit a4c6623cb9
1 changed files with 25 additions and 13 deletions

View File

@ -2,27 +2,39 @@
#include <stdio.h> #include <stdio.h>
#include <memory.h> #include <memory.h>
#include <malloc.h> #include <malloc.h>
#include <stdint.h>
void memdump(unsigned char *string) { void memdump(unsigned char *string) {
printf("ADDR\t\t00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 0123456789ABCDEF\n"); printf("ADDR\t\t00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 0123456789ABCDEF\n");
int columns = 16; int columns = 16;
int rows = 4; int rows = 4;
char array[columns * rows]; char symbols[columns * rows];
if((long) string % 16 != 0) {
string = string - ((long) string % 16);
}
for(int i = 0; i < rows; i++) { for(int i = 0; i < rows; i++) {
printf("%p\t", (void *) &string[i * columns]); printf("%p\t", (void *) &string[i * columns]);//print address
if((long) string % 16 != 0) {
printf("oh nein, kein mod 16");
}
for (int j = i * columns; j < (i + 1) * columns; j++) { for (int j = i * columns; j < (i + 1) * columns; j++) {
printf("%02x ", string[j]); printf("%02x ", string[j]);//print char number
if(string[j] < 0x20 || string[j] > 0x7e) array[j] = '.'; if(string[j] < 0x20 || string[j] > 0x7e)//check for printable characters
else array[j] = string[j]; symbols[j] = '.';
else symbols[j] = string[j];
} }
printf(" "); printf(" ");
//print characters
for (int j = i * columns; j < (i + 1) * columns; j++) { for (int j = i * columns; j < (i + 1) * columns; j++) {
printf("%c", array[j]); printf("%c", symbols[j]);
} }
printf("\n"); printf("\n");
} }
@ -32,10 +44,11 @@ void memdump(unsigned char *string) {
int memreplace(char *string, char cin, char cout, char **caddr) { int memreplace(char *string, char cin, char cout, char **caddr) {
int found = 0; int found = 0;
for (int i = 0; i < strlen(string); ++i) { for (int i = 0; i < strlen(string); ++i) {
//if char is to be replaced
if(string[i] == cin) { if(string[i] == cin) {
string[i] = cout;//replace char
found++; found++;
string[i] = cout; *caddr = &string[i];//setting last found address
*caddr = &string[i];
} }
} }
return found; return found;
@ -47,13 +60,12 @@ int main(int argc, char **argv) {
return -1; return -1;
} }
memdump((unsigned char *) argv[1]); memdump((unsigned char *) argv[0]);
printf("\tLaenge der Zeichenkette (inkl \\0): %i Byte(s)\n", (int) strlen(argv[1]) +1);
printf("\tLaenge der Zeichenkette (inkl \\0): %i Byte(s)\n", (int) strlen(argv[1]));
printf("\tErsetzen: '%c' mit '%c'\n", *argv[2], *argv[3]); printf("\tErsetzen: '%c' mit '%c'\n", *argv[2], *argv[3]);
unsigned char *copy = malloc(4 * 16 * sizeof(char)); unsigned char *copy = malloc(4 * 16 * sizeof(char));//allocate memory for all chars
strcpy((char *) copy, argv[1]); strcpy((char *) copy, argv[1]);
char *lastAddress = NULL; char *lastAddress = NULL;