1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#define DEFAULT_DELIMITER "$"
void compile_c_code(char * s) {
printf("%.*s\n", (int) strlen(s), s);
}
void compile_byte_array(char * s) {
printf("write(OUT, \"");
uint64_t slen = strlen(s);
for (uint64_t i = 0; i < slen; ++i) {
printf("\\x%02x", s[i]);
}
printf("\", %lu);\n", strlen(s));
}
char *read_file_to_string(const char *filename) {
FILE *file = fopen(filename, "rb");
if (!file) {
perror("Error opening file");
return NULL;
}
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
rewind(file);
char *buffer = (char *)malloc(file_size + 1);
if (!buffer) {
perror("Memory allocation failed");
fclose(file);
return NULL;
}
fread(buffer, 1, file_size, file);
buffer[file_size] = '\0';
fclose(file);
return buffer;
}
#define CHARSET_SIZE 128 // Covering standard ASCII characters
int print_good_delimiters(const char * s) {
int seen[CHARSET_SIZE] = {0}; // Array to track seen characters
size_t len = strlen(s);
for (size_t i = 0; i < len; i++) {
if (s[i] >= 0 && (int)s[i] < CHARSET_SIZE) {
seen[(int)s[i]] = 1;
}
}
// Define the characters to check: A-Za-z0-9 and common symbols
char *valid_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?`~";
int flag = 0;
printf("Good options for delimiters:\n");
for (int i = 0; valid_chars[i] != '\0'; i++) {
if (!seen[(unsigned char)valid_chars[i]]) {
putchar(valid_chars[i]);
flag++;
}
}
if (!flag) {
printf(" :( none found, compromises shall be made");
}
putchar('\n');
return 0;
}
int main(int argc, char *argv[])
{
if (argc < 2) {
fprintf(stderr, "Usage: %s <template> [delim|-d] [> OUTPUT]\n", argv[0]);
return 1;
}
const char *filepath = argv[1];
char *template = read_file_to_string(filepath);
if (!template) return 1;
if (argc > 2 && 0 == strcmp(argv[2], "-d")) {
print_good_delimiters(template);
exit(0);
}
char delim[1024] = DEFAULT_DELIMITER;
if (argc > 2) {
strcpy(delim, argv[2]);
}
int c_code_mode = 1;
for (int i = 0; delim[i] != '\0'; i++) {
if (template[i] != delim[i]) {
c_code_mode = 0;
break;
}
}
char *token = strtok(template, delim);
while (token) {
if (c_code_mode) {
compile_c_code(token);
} else {
compile_byte_array(token);
}
c_code_mode = !c_code_mode;
token = strtok(NULL, delim);
}
free(template);
return 0;
}
|