-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrot13.c
217 lines (190 loc) · 4.86 KB
/
rot13.c
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// rot13 string encoder/decoder tool
// written by Mike Hedlund 11/12/2018
// last updated 11/13/2018
// github repo: https://github.com/CausticKirbyZ/rot13
// _ _ _____
// _ __ ___ | |_/ |___ /
// | '__/ _ \| __| | |_ \
// | | | (_) | |_| |___) |
// |_| \___/ \__|_|____/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int bool;
enum { false, true };
void encoder(char *mes);
void decoder(char *mes);
void encoder_chst(char *mes);
void decoder_chst(char *mes);
void readfile(char *filename);
void printhelp();
bool exclude(char c);
int strpos(char c);
// global variables
char * exclusions = "";
char * charset = "entire ascii table";
char * buffer;
int rot = 13;
int main(int argc, char ** argv)
{
if (argc <= 2)
{
printhelp();
return 0;
}
bool encode=true,decode=false,file=false,chst=false;
char *filename;
for(int i=0; i < argc; i++)
{
if (strcmp(argv[i],"-o") == 0)
rot = atoi(argv[i+1]);
else if(strcmp(argv[i],"-e") == 0)
{
encode = true;
decode = false;
continue;
}
else if (strcmp(argv[i],"-d") == 0)
{
decode = true;
encode = false;
continue;
}
else if (strcmp(argv[i],"-f") == 0)
{
filename = argv[i+1];
file = true;
}
else if (strcmp(argv[i],"-ex") == 0)
{
exclusions = argv[i+1];
}
else if (strcmp(argv[i],"-ch") == 0)
{
chst = true;
charset = argv[i+1];
}
else if(strcmp(argv[i],"--help") == 0)
{
printhelp();
return 0;
}
else
continue;
}
printf("charset: %s\n", charset);
//main logic
if(file)
readfile(filename);
else
buffer = argv[argc-1];
if(encode)
(chst) ? encoder_chst(buffer) : encoder(buffer);
else if(decode)
(chst) ? decoder_chst(buffer) : decoder(buffer);
else
printf("wrong input");
printf("%s\n",buffer);
return 0;
}
// encodes global buffer
void encoder(char *mes)
{
for(int i = 0; i < strlen(mes); i++)
{
if(exclude(mes[i]))
continue;
else
mes[i] = (mes[i] + rot) % 127;
}
}
// decodes global buffer
void decoder(char *mes)
{
for(int i = 0; i < strlen(mes); i++)
{
if(exclude(mes[i]))
continue;
else
mes[i] = (mes[i] - rot + 127) % 127;
}
}
// encodes global buffer
void encoder_chst(char *mes)
{
for(int i = 0; i < strlen(mes); i++)
{
if(exclude(mes[i]) || strpos(mes[i]) == -1)
continue;
else if (strpos(mes[i]) == -1)
continue;
else
mes[i] = charset[(strpos(mes[i]) + rot) % strlen(charset)];
}
}
void decoder_chst(char *mes)
{
for(int i = 0; i < strlen(mes); i++)
{
if( exclude(mes[i]) || strpos(mes[i]) == -1)
continue;
else
mes[i] = charset[
(
strpos(mes[i]) - rot + strlen(charset)
) % strlen(charset) ];
}
}
// reads contents of file into global buffer variable
void readfile(char *filename)
{
long length;
FILE *f = fopen(filename, "r");
if(f)
{
fseek(f, 0, SEEK_END);
length = ftell(f);
fseek(f, 0, SEEK_SET);
buffer = malloc(length);
if(buffer)
{
fread(buffer, 1, length, f);
}
fclose (f);
}
}
// returns true if char is in exclusion list
bool exclude(char c)
{
for(int i = 0; i < strlen(exclusions); i++)
if(c == exclusions[i])
return true;
return false;
}
int strpos(char c)
{
int i = (int)(strchr(charset, c) - charset);
return (i > -1 && i < 128) ? i : -1 ;
}
// prints help
void printhelp()
{
printf("\x1B[34m _ _ _____\n _ __ ___ | |_/ |___ /\n| \'__/ _ \\| __| | |_ \\\n| | | (_) | |_| |___) |\n|_| \\___/ \\__|_|____/\n");
printf("\x1B[32mhttps://github.com/CausticKirbyZ/rot13\n\n\n");
printf("\x1B[37mrot13 (-d | -e) [-o <charoffset>][-ex <exclusion list][-ch <charset>] ( (-f <filename>) | <message> )\n\n"
" Usage:\n"
"--------------------------------------\n\n"
" ./rot13 -e message\n"
" ./rot13 -e -o 7 -f file1.txt\n"
" ./rot13 -d -o 3 -ch abcd -f file2.txt\n"
" ./rot13 -e -o 5 -ex , -f file3.csv\n\n\n"
" Options:\n"
"--------------------------------------\n\n"
" -d decode\n"
" -e encode\n"
" -o offset\n"
" -f file input\n"
" -ex exclusion list\n"
" -ch character set, default is entire ascii table\n"
" -h, --help help me\n\n");
}