-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrecode57.c
More file actions
405 lines (345 loc) · 11.4 KB
/
recode57.c
File metadata and controls
405 lines (345 loc) · 11.4 KB
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
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
enum encoding {
UTF8, UTF16BE, UTF16LE, UTF32BE, UTF32LE,
};
/*
* Devuelve el encoding correspondiente al argumento.
*/
static enum encoding str_to_encoding(const char *enc) {
if (strcmp(enc, "UTF-8") == 0)
return UTF8;
else if (strcmp(enc, "UTF-16BE") == 0)
return UTF16BE;
else if (strcmp(enc, "UTF-16LE") == 0)
return UTF16LE;
else if (strcmp(enc, "UTF-32BE") == 0)
return UTF32BE;
else if (strcmp(enc, "UTF-32LE") == 0)
return UTF32LE;
else
return -1;
}
/*
* Devuelve el encoding correspondiente al byte order mark (BOM).
*/
static enum encoding bom_to_encoding(uint8_t *bom) {
if(bom[0] == 0xFE && bom[1] == 0xFF) {
return UTF16BE;
}
if(bom[0] == 0x00 && bom[1] == 0x00 && bom[2] == 0xFE && bom[3] == 0xFF){
return UTF32BE;
}
if(bom[0] == 0xFF && bom[1] == 0xFE && bom[2] == 0x00 && bom[3] == 0x00){
return UTF32LE;
}
if(bom[0] == 0xFF && bom[1] == 0xFE){
return UTF16LE;
}
// Si no es BOM válida, se asume UTF-8.
return UTF8;
}
void utf8_to_ucs4(uint8_t* buf, int* b, uint32_t* codepoint){
if((buf[(*b)] & 0x80) == 0){
(*codepoint) = buf[(*b)++];
}else if((buf[(*b)] & 0x20) == 0){
(*codepoint) = buf[(*b)++] & 0x1F;
(*codepoint) = ((*codepoint) << 6) | (buf[(*b)++] & 0x3F);
}else if((buf[(*b)] & 0x10) == 0){
(*codepoint) = buf[(*b)++] & 0xF;
(*codepoint) = ((*codepoint) << 6) | (buf[(*b)++] & 0x3F);
(*codepoint) = ((*codepoint) << 6) | (buf[(*b)++] & 0x3F);
}else{
(*codepoint) = buf[(*b)++] & 0x7;
(*codepoint) = ((*codepoint) << 6) | (buf[(*b)++] & 0x3F);
(*codepoint) = ((*codepoint) << 6) | (buf[(*b)++] & 0x3F);
(*codepoint) = ((*codepoint) << 6) | (buf[(*b)++] & 0x3F);
}
}
void utf16le_to_ucs4(uint8_t* buf, int* b, uint32_t* codepoint){
if(((buf[(*b)+1] & 0xD8) == 0xD8) && ((buf[(*b)+3] & 0xDC) == 0xDC)){
uint32_t high_surrogate = buf[(*b)+1];
high_surrogate = ((high_surrogate << 8) | buf[(*b)]) - 0xD800; (*b) += 2;
uint32_t low_surrogate = buf[(*b)+1];
low_surrogate = ((low_surrogate << 8) | buf[(*b)]) - 0xDC00; (*b) += 2;
(*codepoint) = ((high_surrogate << 10) | low_surrogate) + 0x10000;
}else{
(*codepoint) = buf[(*b)+1];
(*codepoint) = ((*codepoint) << 8) | buf[(*b)];
(*b) += 2;
}
}
void utf16be_to_ucs4(uint8_t* buf, int* b, uint32_t* codepoint){
if(((buf[(*b)] & 0xD8) == 0xD8) && ((buf[(*b)+2] & 0xDC) == 0xDC)){
uint32_t high_surrogate = buf[(*b)++];
high_surrogate = ((high_surrogate << 8) | buf[(*b)++]) - 0xD800;
uint32_t low_surrogate = buf[(*b)++];
low_surrogate = ((low_surrogate << 8) | buf[(*b)++]) - 0xDC00;
(*codepoint) = ((high_surrogate << 10) | low_surrogate) + 0x10000;
}else{
(*codepoint) = buf[(*b)++];
(*codepoint) = ((*codepoint) << 8) | buf[(*b)++];
}
}
/*
* Devuelve verdadero si hay un codepoint codificado en buf.
*/
static bool has_codepoint(enum encoding enc, uint8_t *buf, int n) {
switch (enc) {
case UTF32BE:
case UTF32LE:
return n >= 4;
case UTF16BE:
return (n >= 4 ||
(n >= 2 && (buf[0] & 0xD8) == 0));
case UTF16LE:
return (n >= 4 ||
(n >= 2 && (buf[1] & 0xD8) == 0));
case UTF8:
return (n >= 4 ||
(n >= 3 && buf[0] <= 0xEF) ||
(n >= 2 && buf[0] <= 0xDF) ||
(n >= 1 && buf[0] <= 0x7F));
default:
return false;
}
}
/*
* Transforma una codificación a UCS-4.
*
* Argumentos:
*
* - enc: el encoding original.
* - buf: los bytes originales.
* - nbytes: número de bytes disponibles en buf.
* - destbuf: resultado de la conversión.
*
* Devuelve: el número de CODEPOINTS obtenidos (número
* de elementos escritos en destbuf).
*
* Actualiza: nbytes contiene el número de bytes que no se
* pudieron consumir (secuencia o surrgate incompleto).
*
* Se debe garantiza que "destbuf" puede albergar al menos nbytes
* elementos (caso enc=UTF-8, buf=ASCII).
*/
int orig_to_ucs4(enum encoding enc, uint8_t *buf, int* nbytes, uint32_t *destbuf) {
int cant_cp = 0, b = 0;
while(has_codepoint(enc, &buf[b], *nbytes) && b < (*nbytes)){
uint32_t codepoint = 0;
switch(enc){
case UTF32BE:
codepoint = codepoint | buf[b++];
codepoint = (codepoint << 8) | buf[b++];
codepoint = (codepoint << 8) | buf[b++];
codepoint = (codepoint << 8) | buf[b++];
break;
case UTF32LE:
codepoint = codepoint | buf[b+3];
codepoint = ((codepoint << 8) | buf[b+2]);
codepoint = ((codepoint << 8) | buf[b+1]);
codepoint = ((codepoint << 8) | buf[b]);
b += 4;
break;
case UTF16LE:
utf16le_to_ucs4(buf, &b, &codepoint);
break;
case UTF16BE:
utf16be_to_ucs4(buf, &b, &codepoint);
break;
case UTF8:
utf8_to_ucs4(buf, &b, &codepoint);
break;
}
destbuf[cant_cp++] = codepoint;
}
(*nbytes) = (*nbytes) - b;
return cant_cp;
}
void ucs4_to_utf8(uint32_t input, uint8_t* outbuf, int* b){
uint32_t input_be = input;
if((input_be <= 0x7F)){
outbuf[(*b)++] = input;
}else if(input_be <= 0x7FF){
outbuf[(*b)++] = 0xC0 | (input >> 6);
outbuf[(*b)++] = 0x80 | (input & 0x3F);
}else if(input_be <= 0xFFFF){
/*
Not
shown */
}else{
/*
Not
shown
*/
}
}
void ucs4_to_utf16le(uint32_t input, uint8_t* outbuf, int* b){
if((input > 0xFFFF) == 0){
outbuf[(*b)++] = input & 0xFF;
outbuf[(*b)++] = (input >> 8);
}else{
uint32_t aux = input;
aux = aux - 0x10000;
uint32_t high_surrogate = ((aux >> 10) + 0xD800);
outbuf[(*b)++] = high_surrogate & 0xFF;
outbuf[(*b)++] = high_surrogate >> 8;
uint32_t low_surrogate = (aux & 0x3FF) + 0xDC00;
outbuf[(*b)++] = low_surrogate & 0xFF;
outbuf[(*b)++] = low_surrogate >> 8;
}
}
void ucs4_to_utf16be(uint32_t input, uint8_t* outbuf, int* b){
if((input > 0xFFFF) == 0){
outbuf[(*b)++] = (input >> 8);
outbuf[(*b)++] = (input & 0xFF);
}else{
uint32_t aux = input;
aux = aux - 0x10000;
uint32_t high_surrogate = ((aux >> 10) + 0xD800);
outbuf[(*b)++] = high_surrogate >> 8;
outbuf[(*b)++] = high_surrogate & 0xFF;
uint32_t low_surrogate = (aux & 0x3FF) + 0xDC00;
outbuf[(*b)++] = low_surrogate >> 8;
outbuf[(*b)++] = low_surrogate & 0xFF;
}
}
/*
* Transforma UCS-4 a la codificación deseada.
*
* Argumentos:
*
* - enc: el encoding destino.
* - input: los codepoints a codificar.
* - npoints: el número de codepoints en input.
* - output: resultado de la conversión.
*
* Devuelve: el número de BYTES obtenidos (número
* de elementos escritos en destbuf).
*
* Se debe garantiza que "destbuf" puede albergar al menos npoints*4
* elementos, o bytes (caso enc=UTF-32).
*/
int ucs4_to_dest(enum encoding enc, uint32_t *input, int npoints, uint8_t *outbuf) {
int b = 0;
for (int i = 0; i < npoints; i++) {
switch (enc) {
case UTF32BE:
outbuf[b++] = input[i] >> 24;
outbuf[b++] = (input[i] >> 16) & 0xFF;
outbuf[b++] = (input[i] >> 8) & 0xFF;
outbuf[b++] = input[i] & 0xFF;
break;
case UTF32LE:
outbuf[b++] = input[i] & 0xFF;
outbuf[b++] = (input[i] >> 8) & 0xFF;
outbuf[b++] = (input[i] >> 16) & 0xFF;
outbuf[b++] = input[i] >> 24;
break;
case UTF16LE:
ucs4_to_utf16le(input[i], outbuf, &b);
break;
case UTF16BE:
ucs4_to_utf16be(input[i], outbuf, &b);
break;
case UTF8:
ucs4_to_utf8(input[i], outbuf, &b);
break;
}
}
return b;
}
void add_first_dest_bom(enum encoding dest_enc, uint8_t* outbuf, int *outbytes){
uint32_t outbuf_aux[(*outbytes)];
memcpy(outbuf_aux, outbuf, *outbytes);
switch(dest_enc){
case UTF8:
break;
case UTF16LE:
outbuf[0] = 0xFF;
outbuf[1] = 0xFE;
memcpy(outbuf+2, outbuf_aux, *outbytes);
(*outbytes) += 2;
break;
case UTF16BE:
outbuf[0] = 0xFE;
outbuf[1] = 0xFF;
memcpy(outbuf+2, outbuf_aux, *outbytes);
(*outbytes) += 2;
break;
case UTF32LE:
outbuf[0] = 0xFF;
outbuf[1] = 0xFE;
outbuf[2] = 0x00;
outbuf[3] = 0x00;
memcpy(outbuf+4, outbuf_aux, *outbytes);
(*outbytes) += 4;
break;
case UTF32BE:
outbuf[0] = 0x00;
outbuf[1] = 0x00;
outbuf[2] = 0xFE;
outbuf[3] = 0xFF;
memcpy(outbuf+4, outbuf_aux, *outbytes);
(*outbytes) += 4;
break;
}
}
int main(int argc, char *argv[]) {
enum encoding orig_enc, dest_enc;
if(argc != 2) {
fprintf(stderr, "Uso: ./recode57 <encoding>\n");
return 1;
}
if((dest_enc = str_to_encoding(argv[1])) > 4) {
fprintf(stderr, "Encoding no válido: %s\n", argv[1]);
return 1;
}
// Detectar codificación origen con byte order mark.
uint8_t bom[4];
if(read(STDIN_FILENO, bom, 4) < 0){
perror("Error");
return -1;
}
orig_enc = bom_to_encoding(bom);
// En cada iteración, leer hasta 1024 bytes, convertirlos a UCS-4
// (equivalente a UTF32-LE con enteros nativos) y convertirlo por
// salida estándar.
uint8_t inbuf[1024];
uint8_t outbuf[1024*4];
uint32_t ucs4[1024];
ssize_t inbytes;
int npoints, outbytes, prevbytes = 0;
// Si orig_enc no fue UTF-32, quedaron 2 o 4 bytes en "bom" que
// deben ser prefijados en inbuf.
if (orig_enc == UTF8) {
memcpy(inbuf, bom, 4);
prevbytes = 4;
} else if (orig_enc == UTF16BE || orig_enc == UTF16LE) {
memcpy(inbuf, bom + 2, 2);
prevbytes = 2;
}
bool first_dest_bom = true;
while((inbytes = read(STDIN_FILENO, inbuf + prevbytes, sizeof(inbuf) - prevbytes)) > 0) {
prevbytes += inbytes;
npoints = orig_to_ucs4(orig_enc, inbuf, &prevbytes, ucs4);
outbytes = ucs4_to_dest(dest_enc, ucs4, npoints, outbuf);
if(first_dest_bom){
add_first_dest_bom(dest_enc, outbuf, &outbytes);
first_dest_bom = false;
}
ssize_t written_bytes = 0;
do{
written_bytes += write(STDOUT_FILENO, outbuf + written_bytes, outbytes - written_bytes);
}while(written_bytes < outbytes);
if (prevbytes > 0) {
uint8_t inbuf_aux[1024];
memcpy(inbuf_aux, inbuf+(inbytes-prevbytes), prevbytes);
memcpy(inbuf, inbuf_aux, prevbytes);
}
}
return 0;
}