-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmain.c
135 lines (111 loc) · 3.92 KB
/
main.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
/***********************************************************
* Base64 example app *
* @author Ahmed Elzoughby *
* @date July 23, 2017 *
* Purpose: Demonstration of Base64 library *
* Usage: base64 (encode | decode) <source> [<destination>] *
***********************************************************/
#include <stdio.h>
#include <stdbool.h>
#include "base64.h"
void usage();
void welcome();
void do_and_print(char *opt, char *src);
void do_and_save(char *opt, char *src, char *dest);
bool file_exists(char* path);
void args_error();
int main(int argc, char* argv[]) {
if (argc == 1)
welcome();
else if (argc == 3)
do_and_print(argv[1], argv[2]);
else if (argc == 4)
do_and_save(argv[1], argv[2], argv[3]);
else
args_error();
}
void welcome() {
printf("===============================================================\n");
printf("%s", base64_decode("SGF2ZSB0byBkZWFsIHdpdGggQmFzZTY0IGZvcm1hdD8gV2VsY29tZSE="));
printf("\n===============================================================\n\n");
usage();
printf("\n===============================================================\n");
printf("%s", base64_decode( base64_encode("www.linkedin.com/in/elzoughby")));
printf("\n===============================================================\n");
}
void usage() {
printf("Usage: base64 (encode | decode) <source> [<destination>] \n\n"
"Options: \n\t"
"encode \t\t\t Convert ASCII string into Base64 format \n\t"
"decode \t\t\t Convert Base64 format into ASCII string \n"
"Arguments: \n\t"
"source \t\t\t String or path/to/file to be converted \n\t"
"destination \t path/to/converted/file \n"
);
}
void do_and_print(char *opt, char *src) {
if(file_exists(src)) {
FILE* file = fopen(src, "r");
char* line = NULL;
size_t len = 0;
if(file == NULL) exit(1);
if(strcmp("encode", opt) == 0)
while(getline(&line, &len, file) != -1)
printf("%s\n", base64_encode(line));
else if(strcmp("decode", opt) == 0)
while(getline(&line, &len, file) != -1)
printf("%s\n", base64_decode(line));
else
args_error();
fclose(file);
if(line) free(line);
} else {
if(strcmp("encode", opt) == 0)
printf("%s", base64_encode(src));
else if(strcmp("decode", opt) == 0)
printf("%s", base64_decode(src));
else
args_error();
}
}
void do_and_save(char *opt, char *src, char *dest) {
FILE* dest_file = fopen(dest, "w");
if(dest_file == NULL) exit(1);
if(file_exists(src)) {
FILE* src_file = fopen(src, "r");
char* line = NULL;
size_t len = 0;
if(src_file == NULL) exit(1);
if(strcmp("encode", opt) == 0)
while (getline(&line, &len, src_file) != -1)
fprintf(dest_file, "%s\n" ,base64_encode(line));
else if(strcmp("decode", opt) == 0)
while (getline(&line, &len, src_file) != -1)
fprintf(dest_file, "%s\n", base64_decode(line));
else
args_error();
fclose(src_file);
if(line) free(line);
} else {
if(strcmp("encode", opt) == 0)
fprintf(dest_file, "%s", base64_encode(src));
else if(strcmp("decode", opt) == 0)
fprintf(dest_file , "%s", base64_decode(src));
else
args_error();
}
fclose(dest_file);
}
bool file_exists(char* path) {
bool result = false;
FILE* file;
if(file = fopen(path, "r")) {
fclose(file);
result = true;
}
return result;
}
void args_error() {
printf("Wrong Parameters! \n\n");
usage();
}