-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.c
103 lines (76 loc) · 2.59 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "include/encoding.h"
#define INPUT_OUTPUT_RATIO 4
#define READ_BUFFER 10485760
#define OUTPUT_BUFFER READ_BUFFER * INPUT_OUTPUT_RATIO
#define STDIN 0
#define STDOUT 1
int is_decoder(int argc, char **argv) {
//Even with one argument we've still got two characters (char + NUL)
if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'd')
return 1;
return 0;
}
int main(int argc, char **argv) {
ssize_t bytes_read = 0, total_bytes_read = 0, num_bytes_write = 0;
unsigned char *bytes_in, *bytes_out;
int ret;
int decoding = is_decoder(argc, argv);
alloc_decode_lookup_tbl();
bytes_in = malloc(READ_BUFFER * sizeof(*bytes_in));
bytes_out = malloc(OUTPUT_BUFFER * sizeof(*bytes_out));
if (!bytes_in || !bytes_out) {
fprintf(stderr, "- Could not allocate memory for input/output\n");
ret = -1;
goto out;
}
unsigned char *buffer_pos = bytes_in;
while ( (bytes_read = read(STDIN, buffer_pos, READ_BUFFER - total_bytes_read)) ) {
if (bytes_read < 0) {
fprintf(stderr, "- read() error\n");
ret = -1;
goto out;
}
total_bytes_read += bytes_read;
if (decoding) {
//If decoding we need a multiple of four bytes.
if (!total_bytes_read & 4) {
fprintf(stderr, "%ld\n", total_bytes_read);
//Move our buffer along
buffer_pos += bytes_read;
continue;
}
num_bytes_write = ws_decode(bytes_in, bytes_out, total_bytes_read);
if (!num_bytes_write) {
fprintf(stderr, "- Decoding error, likely invalid byte\n");
ret = -1;
goto out;
}
} else {
num_bytes_write = ws_encode(bytes_in, bytes_out, total_bytes_read);
}
//Write out to stdout
if ( write(STDOUT, bytes_out, num_bytes_write) < 0 ) {
fprintf(stderr, "- write() error\n");
ret = -1;
}
//Reset buffer position and total bytes read.
buffer_pos = bytes_in;
total_bytes_read = 0;
}
//If we got there and total_bytes_read isn't zero, it means that our input wasn't
//a multiple of 4 bytes and our decoding failed.
if (total_bytes_read != 0) {
fprintf(stderr, "- Error: decoding and input not a multiple of 4 bytes\n");
ret = -1;
goto out;
}
ret = 0;
out:
free(bytes_in);
free(bytes_out);
return ret;
}