-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
310 lines (257 loc) · 9.99 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// CBOR splitter/joiner. Text and bytestrings goes to one stream, everything else the other.
// Designed and implemented by Vitaly "_Vi" Shukela in 2017.
// License is MIT + Apache 2
#ifndef NO_FDOPEN
#define _POSIX_SOURCE
#endif
#include <stdio.h>
#include <string.h>
#include "cborsplit.h"
uint8_t buf_cbor[65536];
uint8_t buf_misc[65536];
uint8_t buf_text[65536];
int main(int argc, char* argv[]) {
if (argc != 5) {
fprintf(stderr, "Usage:\n");
fprintf(stderr, " cborsplit {-s|--split} in out1 out2\n");
fprintf(stderr, " cborsplit {-m|--merge} in1 in2 out\n");
fprintf(stderr, " '-' instead of in/out means stdin/stdout\n");
#ifndef NO_FDOPEN
fprintf(stderr, " '-' instead of out1/out2/in1/in2 means fd 3/fd 4/fd 3/fd 4\n");
#endif
return 1;
}
if (!strcmp(argv[1], "-s") || !strcmp(argv[1], "--split")) {
FILE* f_cbor;
FILE* f_misc;
FILE* f_text;
if (!strcmp(argv[2], "-")) {
f_cbor=stdin;
} else {
f_cbor=fopen(argv[2], "rb");
if (!f_cbor) { perror("fopen"); return 2; }
}
if (!strcmp(argv[3], "-")) {
#ifndef NO_FDOPEN
f_misc = fdopen(3, "wb");
if (!f_misc) { perror("fdopen"); return 1; }
#else
{ fprintf(stderr, "fdopen support not compiled in\n"); return 1; }
#endif
} else {
f_misc=fopen(argv[3], "wb");
if (!f_misc) { perror("fopen"); return 2; }
}
if (!strcmp(argv[4], "-")) {
#ifndef NO_FDOPEN
f_text = fdopen(4, "wb");
if (!f_text) { perror("fdopen"); return 1; }
#else
{ fprintf(stderr, "fdopen support not compiled in\n"); return 1; }
#endif
} else {
f_text=fopen(argv[4], "wb");
if (!f_text) { perror("fopen"); return 2; }
}
struct cbsp_splitter s;
memset(&s, 0, sizeof(s));
s.cbor.buffer = buf_cbor;
s.cbor.len = 0;
s.misc.buffer = buf_misc;
s.misc.capacity = sizeof buf_misc;
s.text.buffer = buf_text;
s.text.capacity = sizeof buf_text;
int reading_finished = 0;
int force_write = 0;
uint64_t bytes_processed = 0;
for(;;) {
if (s.cbor.len < 12 && !reading_finished) {
memmove(buf_cbor, s.cbor.buffer, s.cbor.len);
size_t ret = fread(
buf_cbor+s.cbor.len,
1,
sizeof buf_cbor - s.cbor.len,
f_cbor);
if (ret < sizeof buf_cbor - s.cbor.len) {
reading_finished = 1;
}
s.cbor.buffer = buf_cbor;
s.cbor.len += ret;
}
if (s.misc.capacity < 12 || force_write) {
size_t ret = fwrite(buf_misc, 1, s.misc.buffer - buf_misc, f_misc);
if (ret < s.misc.buffer - buf_misc) {
perror("fwrite short write");
return 3;
}
s.misc.capacity += ret;
s.misc.buffer -= ret;
}
if (s.text.capacity < 64 || force_write) {
size_t ret = fwrite(buf_text, 1, s.text.buffer - buf_text, f_text);
if (ret < s.text.buffer - buf_text) {
perror("fwrite short write");
return 3;
}
s.text.capacity += ret;
s.text.buffer -= ret;
}
const uint8_t *saved_buf = s.cbor.buffer;
#ifdef DEBUG
fprintf(stderr, "Processing at offset %llu\n", bytes_processed);
#endif
enum cbsp_step_result res = cbsp_splitter_step(&s);
bytes_processed += s.cbor.buffer - saved_buf;
switch (res) {
case CBSP_INVAL:
// shoud not happen
return 5;
case CBSP_DATAERR:
fprintf(stderr, "Error in data at pos %llu\n", (long long unsigned)bytes_processed);
return 4;
case CBSP_OK:
// OK
if (reading_finished && s.cbor.len == 0) {
force_write = 1;
}
break;
case CBSP_MOREDATA:
if (reading_finished && s.cbor.len > 0) {
fprintf(stderr, "Trimmed data\n");
return 4;
}
break;
case CBSP_MOREDATA_TEXT:
// should not happen
return 5;
}
if (force_write && s.text.buffer == buf_text && s.misc.buffer == buf_misc) {
break;
}
} // for(;;)
} else
if (!strcmp(argv[1], "-m") || !strcmp(argv[1], "--merge") || !strcmp(argv[1], "-j")) {
FILE* f_misc;
FILE* f_text;
FILE* f_cbor;
if (!strcmp(argv[2], "-")) {
#ifndef NO_FDOPEN
f_misc = fdopen(3, "rb");
if (!f_misc) { perror("fdopen"); return 1; }
#else
{ fprintf(stderr, "fdopen support not compiled in\n"); return 1; }
#endif
} else {
f_misc=fopen(argv[2], "rb");
if (!f_misc) { perror("fopen"); return 2; }
}
if (!strcmp(argv[3], "-")) {
#ifndef NO_FDOPEN
f_text = fdopen(4, "rb");
if (!f_text) { perror("fdopen"); return 1; }
#else
{ fprintf(stderr, "fdopen support not compiled in\n"); return 1; }
#endif
} else {
f_text=fopen(argv[3], "rb");
if (!f_text) { perror("fopen"); return 2; }
}
if (!strcmp(argv[4], "-")) {
f_cbor=stdout;
} else {
f_cbor=fopen(argv[4], "wb");
if (!f_cbor) { perror("fopen"); return 2; }
}
struct cbsp_merger s;
memset(&s, 0, sizeof(s));
s.misc.buffer = buf_misc;
s.misc.len = 0;
s.text.buffer = buf_text;
s.text.len = 0;
s.cbor.buffer = buf_cbor;
s.cbor.capacity = sizeof buf_cbor;
int reading_finished_misc = 0;
int reading_finished_text = 0;
int force_write = 0;
uint64_t bytes_processed = 0;
for(;;) {
if (s.misc.len < 12 && !reading_finished_misc) {
memmove(buf_misc, s.misc.buffer, s.misc.len);
size_t ret = fread(
buf_misc+s.misc.len,
1,
sizeof buf_misc - s.misc.len,
f_misc);
if (ret < sizeof buf_misc - s.misc.len) {
reading_finished_misc = 1;
}
s.misc.buffer = buf_misc;
s.misc.len += ret;
}
if (s.text.len < 64 && !reading_finished_text) {
memmove(buf_text, s.text.buffer, s.text.len);
size_t ret = fread(
buf_text+s.text.len,
1,
sizeof buf_text - s.text.len,
f_text);
if (ret < sizeof buf_text - s.text.len) {
reading_finished_text = 1;
}
s.text.buffer = buf_text;
s.text.len += ret;
}
if (s.cbor.capacity < 64 || force_write) {
size_t ret = fwrite(buf_cbor, 1, s.cbor.buffer - buf_cbor, f_cbor);
if (ret < s.cbor.buffer - buf_cbor) {
perror("fwrite short write");
return 3;
}
s.cbor.capacity += ret;
s.cbor.buffer -= ret;
}
const uint8_t *saved_buf = s.misc.buffer;
#ifdef DEBUG
fprintf(stderr, "Processing at offset %llu\n", bytes_processed);
fprintf(stderr, "Currently there are %d bytes in misc, %d bytes in text and %d available\n",
(int)s.misc.len, (int)s.text.len, (int)s.cbor.capacity
);
#endif
enum cbsp_step_result res = cbsp_merger_step(&s);
bytes_processed += s.misc.buffer - saved_buf;
switch (res) {
case CBSP_INVAL:
// shoud not happen
return 5;
case CBSP_DATAERR:
fprintf(stderr, "Error in data at pos %llu in first input\n", (long long unsigned)bytes_processed);
return 4;
case CBSP_OK:
// OK
if (reading_finished_misc && s.misc.len == 0) {
force_write = 1;
}
break;
case CBSP_MOREDATA:
if (reading_finished_misc && s.misc.len > 0) {
fprintf(stderr, "Trimmed misc data\n");
return 4;
}
break;
case CBSP_MOREDATA_TEXT:
if (reading_finished_text) {
fprintf(stderr, "Trimmed text data\n");
return 4;
}
break;
}
if (force_write && s.cbor.buffer == buf_cbor) {
break;
}
}//for(;;)
} else {
fprintf(stderr, "First argument should be --split (-s) or --merge (-m)\n");
return 1;
}
return 0;
}