summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2025-09-25 15:26:57 +0200
committerCarlos Maiolino <[email protected]>2025-09-25 15:26:57 +0200
commit29e2e93f3f32efa47e95f95260431167e228fe7a (patch)
tree4961bbea472b0b98f29a5a82565c92aa3586d7a9
parent4d1c070b813f3b3b64bd8bf96723756041840ac0 (diff)
chap7: add small programs made in chap7
Signed-off-by: Carlos Maiolino <[email protected]>
-rwxr-xr-xC/HF/chap7/ecatbin0 -> 12944 bytes
-rw-r--r--C/HF/chap7/ecat.c4
-rw-r--r--C/HF/chap7/encrypt.c9
-rw-r--r--C/HF/chap7/encrypt.h3
-rw-r--r--C/HF/chap7/res.txt1
-rw-r--r--C/HF/chap7/xor.c10
-rw-r--r--C/HF/chap7/xor.h1
7 files changed, 26 insertions, 2 deletions
diff --git a/C/HF/chap7/ecat b/C/HF/chap7/ecat
new file mode 100755
index 0000000..eb19bfd
--- /dev/null
+++ b/C/HF/chap7/ecat
Binary files differ
diff --git a/C/HF/chap7/ecat.c b/C/HF/chap7/ecat.c
index ec034e4..8ee5e6a 100644
--- a/C/HF/chap7/ecat.c
+++ b/C/HF/chap7/ecat.c
@@ -7,13 +7,15 @@ int main(int argc, char **argv)
FILE *fd;
char buf[80];
+ encrypt_arr_t fn_arr = {xor16, xor42, xor64};
if (argc != 2)
return 1;
+ printf("Arr size: %ld\n", sizeof(fn_arr) / sizeof(*fn_arr));
fd = fopen(argv[1], "r");
while (fgets(buf, 80, fd)) {
- encrypt(buf, xor64);
+ encrypt_arr(buf, fn_arr, 3);
printf("%s", buf);
}
diff --git a/C/HF/chap7/encrypt.c b/C/HF/chap7/encrypt.c
index 1b5ce25..6c3eccd 100644
--- a/C/HF/chap7/encrypt.c
+++ b/C/HF/chap7/encrypt.c
@@ -1,6 +1,13 @@
#include "encrypt.h"
-typedef void (*encrypt_fn_t)(char *message);
void encrypt(char *message, encrypt_fn_t encrypt_fn) {
encrypt_fn(message);
}
+
+void encrypt_arr(char *message, encrypt_arr_t fn_arr, int size)
+{
+ int i;
+
+ for (i = 0; i < size; i++)
+ encrypt(message, fn_arr[i]);
+}
diff --git a/C/HF/chap7/encrypt.h b/C/HF/chap7/encrypt.h
index 4e130e9..e70d8f6 100644
--- a/C/HF/chap7/encrypt.h
+++ b/C/HF/chap7/encrypt.h
@@ -2,6 +2,9 @@
#define ENCRYPT_H
typedef void (*encrypt_fn_t)(char *message);
+typedef void (*encrypt_arr_t[])(char *message);
+
void encrypt(char *message, encrypt_fn_t);
+void encrypt_arr(char *message, encrypt_arr_t fn_arr, int size);
#endif /* ENCRYPT_H */
diff --git a/C/HF/chap7/res.txt b/C/HF/chap7/res.txt
new file mode 100644
index 0000000..fbc8a19
--- /dev/null
+++ b/C/HF/chap7/res.txt
@@ -0,0 +1 @@
+(54;6>5p
diff --git a/C/HF/chap7/xor.c b/C/HF/chap7/xor.c
index 0efa3a4..ae5f0b2 100644
--- a/C/HF/chap7/xor.c
+++ b/C/HF/chap7/xor.c
@@ -1,6 +1,15 @@
#include <stdio.h>
#include "xor.h"
+void xor16(char *message)
+{
+ printf("Orig: %s\n", message);
+ while (*message) {
+ *message = *message ^ 16;
+ message++;
+ }
+}
+
void xor42(char *message)
{
printf("Orig: %s\n", message);
@@ -18,3 +27,4 @@ void xor64(char *message)
message++;
}
}
+
diff --git a/C/HF/chap7/xor.h b/C/HF/chap7/xor.h
index 6f3de42..8dd8e17 100644
--- a/C/HF/chap7/xor.h
+++ b/C/HF/chap7/xor.h
@@ -1,6 +1,7 @@
#ifndef XOR_H
#define XOR_H
+void xor16(char *m);
void xor42(char *m);
void xor64(char *m);