-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
431 lines (404 loc) · 16.1 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include <stdio.h>
#include <string.h>
union bytes {
unsigned int codePoint;
unsigned char part[4];
};
void swapUChar(unsigned char *a, unsigned char *b) {
*a ^= *b;
*b ^= *a;
*a ^= *b;
}
void change2ByteUtfEndian(union bytes *a) {
swapUChar(&a->part[0], &a->part[1]);
}
void change4ByteUtfEndian(union bytes *a) {
swapUChar(&a->part[1], &a->part[2]);
swapUChar(&a->part[0], &a->part[3]);
}
void change4ByteUtf16Endian(union bytes *a) {
swapUChar(&a->part[1], &a->part[0]);
swapUChar(&a->part[2], &a->part[3]);
}
int printError(const int r, const char *str) {
printf("Error: %s", str);
return r;
}
int between(const unsigned char c, const unsigned char a, const unsigned char b) {
if (c < a || c > b)
return 0;
return 1;
}
void wrongUtf8toErrorCodePoint(union bytes *point) {
point->codePoint = 0xDC00 + point->part[0];
}
void ErrorCodePointToWrongUtf8(union bytes *point) {
point->codePoint = point->part[0];
}
union bytes utf8toCodePoint(FILE *input) {
union bytes point;
point.codePoint = 0;
fread(&point.part, sizeof(unsigned char), 1, input);
if (point.part[0] < 0x80) {
//корректная одно-битная последовательность
return point;
} else if (between(point.part[0], 0xC2, 0xDF)) {//двух-битная последовательность
fread(&point.part[1], sizeof(unsigned char), 1, input);
if (!feof(input)) {
if (between(point.part[1], 0x80, 0xBF)) {
//корректная двух-битная последовательность
point.codePoint = ((point.part[0] - 0xC0) << 6) + (point.part[1] - 0x80);
return point;
} else {//незаконченная двух-битная последовательность
fseek(input, -1, SEEK_CUR);
}
} //else незаконченная двух-битная последовательность
} else if (between(point.part[0], 0xE0, 0xEF)) {//трёх-битная последовательность
fread(&point.part[1], sizeof(unsigned char), 1, input);
if (!feof(input)) {
if (between(point.part[1], 0x80, 0xBF)) {
if (point.part[0] == 0xE0 && point.part[1] < 0xA0 || //не минимальная трех-битная последовательность
point.part[0] == 0xED && point.part[1] >= 0xA0) { //суррогатная пара
fseek(input, -1, SEEK_CUR);
} else {
fread(&point.part[2], sizeof(unsigned char), 1, input);
if (feof(input)) {//незаконченная трех-битная последовательность
fseek(input, -1, SEEK_CUR);
} else if (between(point.part[2], 0x80, 0xBF)) {
//корректная трех-битная последовательность
point.codePoint = ((point.part[0] - 0xE0) << 12) +
((point.part[1] - 0x80) << 6) + (point.part[2] - 0x80);
return point;
} else {//незаконченная трех-битная последовательность
fseek(input, -2, SEEK_CUR);
}
}
} else {//незаконченная трех-битная последовательность
fseek(input, -1, SEEK_CUR);
}
} //else незаконченная трех-битная последовательность
} else if (between(point.part[0], 0xF0, 0xF4)) {//четырех-битная последовательность
fread(&point.part[1], sizeof(unsigned char), 1, input);
if (!feof(input)) {
if (between(point.part[1], 0x80, 0xBF)) {
if (point.part[0] == 0xF0 && point.part[1] < 0x90 || //не минимальная четырех-битная последовательность
point.part[0] == 0xF4 && point.part[1] >= 0x90) { //Выход за границы Unicode U+110000..U+13FFFF
fseek(input, -1, SEEK_CUR);
} else {
fread(&point.part[2], sizeof(unsigned char), 1, input);
if (!feof(input)) {
if (between(point.part[2], 0x80, 0xBF)) {
fread(&point.part[3], sizeof(unsigned char), 1, input);
if (!feof(input)) {
if (between(point.part[3], 0x80, 0xBF)) {
//корректная четырех-битная последовательность
point.codePoint = ((point.part[0] - 0xF0) << 18) + ((point.part[1] - 0x80) << 12)
+ ((point.part[2] - 0x80) << 6) + (point.part[3] - 0x80);
return point;
} else {//незаконченная четырех-битная последовательность
fseek(input, -3, SEEK_CUR);
}
} else {//незаконченная четырех-битная последовательность
fseek(input, -2, SEEK_CUR);
}
} else {//незаконченная четырех-битная последовательность
fseek(input, -2, SEEK_CUR);
}
} else {//незаконченная четырех-битная последовательность
fseek(input, -1, SEEK_CUR);
}
}
} else {//незаконченная четырех-битная последовательность
fseek(input, -1, SEEK_CUR);
}
} //else незаконченная четырех-битная последовательность
} //else неначатая , слишком длинная, не минимальная двух-битная, Выход за границы Unicode ≥ U+140000
fseek(input, 0, SEEK_CUR);
wrongUtf8toErrorCodePoint(&point);
return point;
}
void utf16LELongToCP(union bytes *point) {
point->codePoint = ((((point->part[1] - 0xD8) << 8) + point->part[0]) << 10) +
((point->part[3] - 0xDC) << 8) + point->part[2] + 0x10000;
}
union bytes utf16LEtoCodePoint(FILE *input) {
union bytes point;
point.codePoint = 0;
fread(&point.part, sizeof(unsigned char), 2, input);
if (!feof(input) && between(point.part[1], 0xD8, 0xDB) &&
fread(&point.part[2], sizeof(unsigned char), 2, input)) {
if (between(point.part[3], 0xDC, 0xDF)) {
utf16LELongToCP(&point);
} else {
point.part[2] = point.part[3] = 0;
fseek(input, -2, SEEK_CUR);
}
}
return point;
}
union bytes utf16BEtoCodePoint(FILE *input) {
union bytes point;
point.codePoint = 0;
fread(&point.part, sizeof(unsigned char), 2, input);
if (!feof(input) && between(point.part[0], 0xD8, 0xDB) &&
fread(&point.part[2], sizeof(unsigned char), 2, input)) {
if (between(point.part[2], 0xDC, 0xDF)) {
change4ByteUtf16Endian(&point);
utf16LELongToCP(&point);
return point;
}
point.part[2] = point.part[3] = 0;
fseek(input, -2, SEEK_CUR);
}
change2ByteUtfEndian(&point);
return point;
}
union bytes utf32LEtoCodePoint(FILE *input) {
union bytes point;
point.codePoint = 0;
fread(&point.part, sizeof(unsigned char), 4, input);
return point;
}
union bytes utf32BEtoCodePoint(FILE *input) {
union bytes point = utf32LEtoCodePoint(input);
change4ByteUtfEndian(&point);
return point;
}
void writeCodePointToUtf8(FILE *output, union bytes point) {
if (point.codePoint < 0x80) {
fwrite(&point.part, sizeof(unsigned char), 1, output);
} else if (point.codePoint < 0x800) {
point.part[3] = 0xC0 + (point.part[1] << 2) + (point.part[0] >> 6);
point.part[2] = 0x80 + (point.part[0] & 0x3F);
change4ByteUtfEndian(&point);
fwrite(&point.part, sizeof(unsigned char), 2, output);
} else if (point.codePoint < 0x10000) {
if (point.part[1] == 0xDC && between(point.part[0], 0x80, 0xFF)) {
ErrorCodePointToWrongUtf8(&point);
fwrite(&point.part, sizeof(unsigned char), 1, output);
return;
}
point.part[3] = 0xE0 + (point.part[1] >> 4);
point.part[2] = 0x80 + ((point.part[1] & 0x0F) << 2) + (point.part[0] >> 6);
point.part[1] = 0x80 + (point.part[0] & 0x3F);
change4ByteUtfEndian(&point);
fwrite(&point.part, sizeof(unsigned char), 3, output);
} else if (point.codePoint < 0x200000) {
point.part[3] = 0xF0 + (point.part[2] >> 2);
point.part[2] = 0x80 + ((point.part[2] & 0x03) << 4) + (point.part[1] >> 4);
point.part[1] = 0x80 + ((point.part[1] & 0x0F) << 2) + (point.part[0] >> 6);
point.part[0] = 0x80 + (point.part[0] & 0x3F);
change4ByteUtfEndian(&point);
fwrite(&point.part, sizeof(unsigned char), 4, output);
}
}
void CPLongToUtf16(union bytes *point) {
point->codePoint -= 0x10000;
point->codePoint = ((point->codePoint & 0x3FF | 0xDC00) << 16) + ((point->codePoint >> 10 & 0x3FF) | 0xD800);
}
void writeCodePointToUtf16BE(FILE *output, union bytes point) {
if (point.codePoint > 0xFFFF) {
CPLongToUtf16(&point);
change4ByteUtf16Endian(&point);
fwrite(&point.part, sizeof(unsigned char), 4, output);
} else {
change2ByteUtfEndian(&point);
fwrite(&point.part, sizeof(unsigned char), 2, output);
}
}
void writeCodePointToUtf16LE(FILE *output, union bytes point) {
if (point.codePoint > 0xFFFF) {
CPLongToUtf16(&point);
fwrite(&point.part, sizeof(unsigned char), 4, output);
} else {
fwrite(&point.part, sizeof(unsigned char), 2, output);
}
}
void writeCodePointToUtf32LE(FILE *output, union bytes point) {
fwrite(&point.part, sizeof(unsigned char), 4, output);
}
void writeCodePointToUtf32BE(FILE *output, union bytes point) {
change4ByteUtfEndian(&point);
writeCodePointToUtf32LE(output, point);
}
int main(int argc, char **argv) {
//checking if number of arguments is correct
if (argc != 4) {
return printError(1, "Incorrect input arguments.\n"
"This program expect launching with 3 arguments: *name* input output mode\n"
"Where:\n"
" input - name of input file\n"
" output - name of output file\n"
" mode - encoding of output file \n");
}
//checking output mode
unsigned int outputMode;
if (strlen(argv[3]) != 1 || argv[3][0] < '0' || argv[3][0] > '5') {
return printError(1, "Incorrect output mode argument.");
} else {
outputMode = argv[3][0] - '0';
}
//opening input file
FILE *input = fopen(argv[1], "rb");
if (input == NULL) {
return printError(1, "Error occurred while opening input file.");
}
//reading bom and determining the encoding of file
unsigned int inputMode;
union bytes (*inputFunction)(FILE *);
union bytes BOM;
BOM.codePoint = 0;
fread(&BOM.part[0], sizeof(unsigned char), 2, input);
if (!feof(input)) {
switch (BOM.codePoint) {
case 0xBBEF:
//utf8bom
fread(&BOM.part[2], sizeof(unsigned char), 1, input);
if (!feof(input)) {
if (BOM.codePoint == 0xBFBBEF) {
inputFunction = utf8toCodePoint;
inputMode = 1;
break;
}
}
//utf8
inputFunction = utf8toCodePoint;
inputMode = 0;
fseek(input, 0, SEEK_SET);
break;
case 0xFEFF:
fread(&BOM.part[2], sizeof(unsigned char), 2, input);
if (!feof(input)) {
if (BOM.codePoint == 0x0000FEFF) {
//utf32le or 16le
inputFunction = utf32LEtoCodePoint;
inputMode = 4;
break;
}
fseek(input, -2, SEEK_CUR);
}
//utf16le
inputFunction = utf16LEtoCodePoint;
inputMode = 2;
break;
case 0xFFFE:
//utf16be
inputFunction = utf16BEtoCodePoint;
inputMode = 3;
break;
case 0x0000:
fread(&BOM.part[2], sizeof(unsigned char), 2, input);
if (!feof(input)) {
if (BOM.codePoint == 0xFFFE0000) {
//utf32be
inputFunction = utf32BEtoCodePoint;
inputMode = 5;
break;
}
}
default:
//utf8
inputFunction = utf8toCodePoint;
inputMode = 0;
fseek(input, 0, SEEK_SET);
break;
}
} else {
//utf8
inputFunction = utf8toCodePoint;
inputMode = 0;
fseek(input, 0, SEEK_SET);
}
//setting suitable output function
void (*outputFunction)(FILE *, union bytes);
switch (outputMode) {
case 0:
case 1:
outputFunction = writeCodePointToUtf8;
break;
case 2:
outputFunction = writeCodePointToUtf16LE;
break;
case 3:
outputFunction = writeCodePointToUtf16BE;
break;
case 4:
outputFunction = writeCodePointToUtf32LE;
break;
case 5:
outputFunction = writeCodePointToUtf32BE;
break;
}
//opening output file
FILE *output = fopen(argv[2], "wb");
if (output == NULL) {
if (fclose(input)) {
return printError(1, "Error occurred while opening output file and closing input.");
}
return printError(1, "Error occurred while opening output file.");
}
//writing bom if needed
if (outputMode != 0) {
if (inputMode > 0) {
fseek(input, 0, SEEK_SET);
} else {
BOM.codePoint = 0xFEFF;
outputFunction(output, BOM);
}
}
// decoding/encoding
if (inputMode == 4) {
//writing in utf32le and checking if it's not utf16le
union bytes tmp;
while (!feof(input)) {
tmp = inputFunction(input);
if (tmp.codePoint > 0x10FFFF) {
inputFunction = utf16LEtoCodePoint;
inputMode = 2;
break;
}
if (!feof(input))
outputFunction(output, tmp);
}
if (inputMode == 2) {
fseek(output, 0, SEEK_SET);
if (outputMode == 0) {
fseek(input, 2, SEEK_SET);
} else {
fseek(input, 0, SEEK_SET);
}
}
}
if (inputMode == outputMode || inputMode + outputMode == 1) {
unsigned char tmp;
while (1) {
fread(&tmp, sizeof(unsigned char), 1, input);
if (feof(input)) {
break;
}
fwrite(&tmp, sizeof(unsigned char), 1, output);
}
} else {
union bytes tmp;
while (1) {
tmp.codePoint = 0;
tmp = inputFunction(input);
if (feof(input)) {
break;
}
outputFunction(output, tmp);
}
}
//closing output file
if (fclose(output)) {
if (fclose(input)) {
return printError(2, "Error occurred while closing input and output files.");
}
return printError(2, "Error occurred while closing output file.");
}
//closing input file
if (fclose(input)) {
return printError(2, "Error occurred while closing input file.");
}
return 0;
}