/
main.c
132 lines (114 loc) · 3.07 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
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include "converter/ascii_64_converter.h"
#include "handlers/file_handler.h"
#define HELP_LENGTH 297
#define VERSION 1.0
#define ENCODE_BLOCK_LENGTH 3
#define DECODE_BLOCK_LENGTH 4
#define READ_ENCODE_LENGTH 4
#define RESULT_ENCODE_LENGTH 5
#define READ_DECODE_LENGTH 5
#define RESULT_DECODE_LENGTH 4
void show_version() {
printf("v%f\n", VERSION);
}
void show_help() {
FILE *fp = fopen("help.txt", "r");
if (!fp) return;
size_t size = fsize(fp);
char buffer[HELP_LENGTH + 1];
fread(buffer, sizeof(char), size, fp);
buffer[HELP_LENGTH] = '\0';
puts(buffer);
fclose(fp);
}
void encode_ascii_to_64(char* input_file, char* output_file) {
char arr[READ_ENCODE_LENGTH], processed[RESULT_ENCODE_LENGTH];
arr[READ_ENCODE_LENGTH - 1] = '\0';
processed[RESULT_ENCODE_LENGTH - 1] = '\0';
FILE* input;
FILE* output;
if (!input_file && !output_file) {
input = stdin;
output = stdout;
} else {
input = fopen(input_file, "r");
output = fopen(output_file, "w");
}
if (!input || !output) return;
size_t read_size;
for (int i = 0; !feof(input); i += READ_ENCODE_LENGTH - 1) {
read_size = get_encode_chars(input, arr);
if (read_size == 3 && feof(input)) break;
_encode_ascii_to_64(arr, read_size, processed);
if (read_size != 0) write_code(output, processed);
}
fclose(input);
fclose(output);
}
void decode_64_to_ascii(char* input_file, char* output_file) {
char arr[READ_DECODE_LENGTH], processed[RESULT_DECODE_LENGTH];
arr[READ_DECODE_LENGTH - 1] = '\0';
processed[RESULT_DECODE_LENGTH - 1] = '\0';
FILE* input = fopen(input_file, "r");
FILE* output = fopen(output_file, "w");
if (!input || !output) return;
size_t write_size;
for (int i = 0; i < fsize(input); i += READ_DECODE_LENGTH - 1) {
get_decode_chars(input, arr);
write_size = _decode_64_to_ascii(arr, processed);
write_decode(output, write_size, processed);
}
fclose(input);
fclose(output);
}
int main (int argc, char *argv[]) {
bool help, version, output, input, decode;
help = version = output = input = decode = false;
char *input_file, *output_file;
input_file = output_file = NULL;
int flag = 0;
struct option opts[] = {
{"version", no_argument, 0, 'V'},
{"help", no_argument, 0, 'h'},
{"output", required_argument, 0, 'o'},
{"input", required_argument, 0, 'i'},
{"decode", no_argument, 0, 'd'}
};
while ((flag = getopt_long(argc, argv, "Vho:i:d", opts, NULL)) != -1) {
switch (flag) {
case 'V' :
version = true;
break;
case 'h' :
help = true;
break;
case 'o' :
output_file = optarg;
output = true;
break;
case 'i' :
input_file = optarg;
input = true;
break;
case 'd' :
decode = true;
}
}
if (help) {
show_help();
} else if (version) {
show_version();
} else if (input && output && decode) {
decode_64_to_ascii(input_file, output_file);
} else if (input && output) {
encode_ascii_to_64(input_file, output_file);
} else if (!feof(stdin)) {
encode_ascii_to_64(NULL, NULL);
}
return EXIT_SUCCESS;
}