-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbitstreamdecoder.c
More file actions
496 lines (456 loc) · 16.7 KB
/
bitstreamdecoder.c
File metadata and controls
496 lines (456 loc) · 16.7 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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#include <stdlib.h>
#include "big5.h"
#include "bitstream.h"
#include "bitstreamdecoder.h"
#include "bytebuffer.h"
#include "eci.h"
#include "euc_kr.h"
#include "gb18030.h"
#include "logs.h"
#include "shiftjis.h"
// Here are the possible mode values
#define TERMINATOR 0
#define NUMERIC 1
#define ALPHANUMERIC 2
#define STRUCTURED_APPEND 3
#define BYTE 4
#define FNC1_FIRST 5
#define ECI 7
#define KANJI 8
#define FNC1_SECOND 9
#define ALPHANUMERIC_CHARS "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"
int decode_byte_segment(struct bitstream* stream, unsigned int count, EciMode eci_mode, struct bytebuffer* buffer) {
if (8 * count > remaining_bits(stream)) {
return DECODING_ERROR;
}
if (eci_mode == GB18030) {
return decode_gb18030_segment(stream, count, buffer);
}
if (eci_mode == Big5) {
return decode_big5_segment(stream, count, buffer);
}
if (eci_mode == EUC_KR) {
return decode_euc_kr_segment(stream, count, buffer);
}
int res;
for (unsigned int i = 0 ; i < count ; i++) {
u_int8_t value = read_bits(stream, 8);
if (eci_mode == UTF8) {
// If we have utf8, we can copy the raw bytes as is
if (MEMORY_ERROR == write_byte(buffer, value)) {
return MEMORY_ERROR;
}
} if (eci_mode == UnicodeBigUnmarked) {
// For UTF16 Big Endian, we need 2 bytes
if (remaining_bits(stream) < 8) {
return DECODING_ERROR;
}
// If we have utf8, we can copy the raw bytes as is
if (MEMORY_ERROR == write_byte(buffer, value)) {
return MEMORY_ERROR;
}
value = read_bits(stream, 8);
if (MEMORY_ERROR == write_byte(buffer, value)) {
return MEMORY_ERROR;
}
} else if (eci_mode == SJIS) {
if (value <= 0x7F) {
// This is a one byte ascii value
if (MEMORY_ERROR == write_byte(buffer, value)) {
return MEMORY_ERROR;
}
} else {
// We have a 2-byte value
if (remaining_bits(stream) < 8) {
return DECODING_ERROR;
}
u_int8_t value2 = read_bits(stream, 8);
u_int32_t unicode = from_SJIS((value << 8) | value2);
if (SUCCESS != (res = write_unicode_as_utf8(buffer, unicode))) {
return res;
}
}
} else {
// If we have a supported one byte charset, let's write
// the utf8 representation of the character
u_int32_t unicode;
switch(eci_mode) {
case ISO8859_1: unicode = from_iso8859_1(value); break;
case ISO8859_2: unicode = from_iso8859_2(value); break;
case ISO8859_3: unicode = from_iso8859_3(value); break;
case ISO8859_4: unicode = from_iso8859_4(value); break;
case ISO8859_5: unicode = from_iso8859_5(value); break;
case ISO8859_6: unicode = from_iso8859_6(value); break;
case ISO8859_7: unicode = from_iso8859_7(value); break;
case ISO8859_8: unicode = from_iso8859_8(value); break;
case ISO8859_9: unicode = from_iso8859_9(value); break;
case ISO8859_10: unicode = from_iso8859_10(value); break;
case ISO8859_11: unicode = from_iso8859_11(value); break;
case ISO8859_13: unicode = from_iso8859_13(value); break;
case ISO8859_14: unicode = from_iso8859_14(value); break;
case ISO8859_15: unicode = from_iso8859_15(value); break;
case ISO8859_16: unicode = from_iso8859_16(value); break;
case Cp437: unicode = from_Cp437(value); break;
case Cp1250: unicode = from_Cp1250(value); break;
case Cp1251: unicode = from_Cp1251(value); break;
case Cp1252: unicode = from_Cp1252(value); break;
case Cp1256: unicode = from_Cp1256(value); break;
case ASCII: unicode = from_ascii(value); break;
default: return DECODING_ERROR;
}
if (SUCCESS != (res = write_unicode_as_utf8(buffer, unicode))) {
return res;
}
}
}
return SUCCESS;
}
void decode_percents_in_FNC1_mode(struct bytebuffer* buffer, unsigned int start) {
unsigned int end_pos = buffer->n_bytes;
unsigned int read_pos = start;
unsigned int write_pos = start;
while (read_pos < end_pos) {
u_int8_t v = buffer->bytes[read_pos++];
if (v != '%') {
buffer->bytes[write_pos++] = v;
} else {
// We need to decode a '%'
if (read_pos == end_pos) {
// '%' at the end of the buffer; let's consider it a single '%'
buffer->bytes[write_pos++] = 0x1D;
} else {
v = buffer->bytes[read_pos++];
if (v == '%') {
// We have a '%%' sequence
buffer->bytes[write_pos++] = '%';
} else {
// We have '%' followed by something else;
buffer->bytes[write_pos++] = 0x1D;
buffer->bytes[write_pos++] = v;
}
}
}
}
// When we are done, the write position is the new buffer size
buffer->n_bytes = write_pos;
}
/**
* Alphanumeric characters are encoded as 11-bit pairs plus maybe a
* single 6-bit value at the end. Each value is converted into a character
* with the ALPHANUMERIC_CHARS lookup table.
*
* Decodes count alphanumeric characters from the given stream
* and adds the corresponding data as utf8 in the given buffer.
*
* @return SUCCESS on success
* DECODING_ERROR on failure
* MEMORY_ERROR on memory allocation error
*/
static int decode_alphanumeric_segment(struct bitstream* stream, unsigned int count, int fnc1_mode, struct bytebuffer* buffer) {
int start = buffer->n_bytes;
while (count > 1) {
if (remaining_bits(stream) < 11) {
return DECODING_ERROR;
}
u_int32_t value = read_bits(stream, 11);
if ((value / 45) >= 45) {
return DECODING_ERROR;
}
u_int8_t first = ALPHANUMERIC_CHARS[value / 45];
u_int8_t second = ALPHANUMERIC_CHARS[value % 45];
if (MEMORY_ERROR == write_byte(buffer, first)
|| MEMORY_ERROR == write_byte(buffer, second)) {
return MEMORY_ERROR;
}
count -= 2;
}
if (count == 1) {
// There is a single character at the end of the segment
if (remaining_bits(stream) < 6) {
return DECODING_ERROR;
}
u_int32_t value = read_bits(stream, 6);
if (value >= 45) {
return DECODING_ERROR;
}
if (MEMORY_ERROR == write_byte(buffer, ALPHANUMERIC_CHARS[value])) {
return MEMORY_ERROR;
}
}
if (fnc1_mode) {
// If we are in FNC1 mode, there is more to do
decode_percents_in_FNC1_mode(buffer, start);
}
return SUCCESS;
}
/**
* Numeric characters are encoded as 10-bit triplets plus maybe a
* 7-bit pair or a 4-bit single value at the end.
* converted into a characterwith the ALPHANUMERIC_CHARS lookup table.
*
* Decodes count numeric characters from the given stream
* and adds the corresponding data as utf8 in the given buffer.
*
* @return SUCCESS on success
* DECODING_ERROR on failure
* MEMORY_ERROR on memory allocation error
*/
static int decode_numeric_segment(struct bitstream* stream, unsigned int count, struct bytebuffer* buffer) {
while (count >= 3) {
if (remaining_bits(stream) < 10) {
return DECODING_ERROR;
}
u_int32_t value = read_bits(stream, 10);
if (value >= 1000) {
return DECODING_ERROR;
}
u_int8_t first = '0' + (value / 100);
u_int8_t second = '0' + ((value / 10) % 10);
u_int8_t third = '0' + (value % 10);
if (MEMORY_ERROR == write_byte(buffer, first)
|| MEMORY_ERROR == write_byte(buffer, second)
|| MEMORY_ERROR == write_byte(buffer, third)) {
return MEMORY_ERROR;
}
count -= 3;
}
if (count == 1) {
// There is a single character at the end of the segment
if (remaining_bits(stream) < 4) {
return DECODING_ERROR;
}
u_int32_t value = read_bits(stream, 4);
if (value >= 10) {
return DECODING_ERROR;
}
if (MEMORY_ERROR == write_byte(buffer, '0' + value)) {
return MEMORY_ERROR;
}
} else if (count == 2) {
// There are 2 characters at the end
if (remaining_bits(stream) < 7) {
return DECODING_ERROR;
}
u_int32_t value = read_bits(stream, 7);
if (value >= 100) {
return DECODING_ERROR;
}
u_int8_t first = '0' + (value / 10);
u_int8_t second = '0' + (value % 10);
if (MEMORY_ERROR == write_byte(buffer, first)
|| MEMORY_ERROR == write_byte(buffer, second)) {
return MEMORY_ERROR;
}
}
return SUCCESS;
}
/**
* Kanji characters are 2-bytes Shift JIS X 0208 values that are either
* in the range from 0x8140 to 0x9FFC or in the range 0xE040 to 0xEBBF.
*
* Each Kanji character WXYZ is encoded as a 13-bit value as
* follows:
*
* 1) Subtract 0x8140 or 0xC140 to obtain a new value ABCD.
* The value to subtract depend on the range the value belongs to.
* 2) Use (AB * 0xC0 + CD) as a 13-bit value
*
* Decodes count Kanji characters from the given stream
* and adds the corresponding data as utf8 in the given buffer.
*
* @return SUCCESS on success
* DECODING_ERROR on failure
* MEMORY_ERROR on memory allocation error
*/
static int decode_kanji_segment(struct bitstream* stream, unsigned int count, struct bytebuffer* buffer) {
if (count * 13 > remaining_bits(stream)) {
return DECODING_ERROR;
}
while (count > 0) {
// Get the raw 13-bit value
u_int32_t value = read_bits(stream, 13);
// Unpack it as a 2-byte value
value = ((value / 0xC0) << 8) | (value % 0xC0);
// Adjust the value to its original range
if (value < 0x1F00) {
value += 0x8140;
} else {
value += 0xC140;
}
count--;
uint32_t unicode = from_SJIS(value);
int res = write_unicode_as_utf8(buffer, unicode);
if (res != SUCCESS) {
return res;
}
}
return SUCCESS;
}
/**
* The QR code specification says that the number of bits to be read
* for a segment is not the same for all modes/versions so this functions
* tells how many bits should be read for a given combination.
*/
static unsigned int get_character_count_bit(u_int8_t mode, unsigned int version) {
switch(mode) {
case NUMERIC: {
if (version <= 9) {
return 10;
} else if (version <= 26) {
return 12;
} else {
return 14;
}
}
case ALPHANUMERIC: {
if (version <= 9) {
return 9;
} else if (version <= 26) {
return 11;
} else {
return 13;
}
}
case BYTE: {
if (version <= 9) {
return 8;
} else if (version <= 26) {
return 16;
} else {
return 16;
}
}
case KANJI: {
if (version <= 9) {
return 8;
} else if (version <= 26) {
return 10;
} else {
return 12;
}
}
default: return -1;
}
}
int decode_bitstream(struct bitstream* stream, unsigned int version, u_int8_t* *decoded) {
if (version < 1 || version > 40) {
return DECODING_ERROR;
}
struct bytebuffer* buffer = new_bytebuffer();
if (buffer == NULL) {
return MEMORY_ERROR;
}
gory("\nDecoding bytes...\n");
u_int8_t mode;
int fnc1_mode = 0;
EciMode eci_mode = ISO8859_1;
do {
if (remaining_bits(stream) < 4) {
mode = TERMINATOR;
} else {
mode = read_bits(stream, 4);
}
switch(mode) {
case TERMINATOR: break;
case FNC1_FIRST:
case FNC1_SECOND: {
// These modes act as a flag to turn on a special behaviour
// when decoding alphanumeric segments. Once on, the flag
// cannot be turned off
fnc1_mode = 1;
break;
}
case STRUCTURED_APPEND: {
// The structured append mode is meant to indicate that
// a QR code is part of a series of QR codes that should be
// put together to reassemble the full original message.
// We will ignore this feature, so let's just skip the 16
// bits describing the symbol sequence and the parity data
if (remaining_bits(stream) < 16) {
free_bytebuffer(buffer);
return DECODING_ERROR;
}
read_bits(stream, 16);
break;
}
case ECI: {
// This mode indicates that we need to read an ECI value
// representing the new charset encoding to be used from now on
int n = read_eci_designator(stream);
if (n != DECODING_ERROR) {
int new_eci_mode = get_eci_mode(n);
if (new_eci_mode != DECODING_ERROR) {
eci_mode = new_eci_mode;
break;
}
}
free_bytebuffer(buffer);
return DECODING_ERROR;
}
case NUMERIC:
case ALPHANUMERIC:
case BYTE:
case KANJI: {
// Let's handle the non-special modes. They have in common
// to expose the number of characters to read after the mode
// in a way that depends on the mode/version combination
u_int32_t count = read_bits(stream, get_character_count_bit(mode, version));
switch(mode) {
case NUMERIC: {
gory("Decoding numeric segment representing %d digits\n", count);
int res = decode_numeric_segment(stream, count, buffer);
if (res != SUCCESS) {
free_bytebuffer(buffer);
return res;
}
break;
}
case ALPHANUMERIC: {
gory("Decoding alphanumeric segment representing %d characters\n", count);
int res = decode_alphanumeric_segment(stream, count, fnc1_mode, buffer);
if (res != SUCCESS) {
free_bytebuffer(buffer);
return res;
}
break;
}
case BYTE: {
gory("Decoding binary segment of %d bytes with ECI encoding %s\n", count, get_eci_name(eci_mode));
int res = decode_byte_segment(stream, count, eci_mode, buffer);
if (res != SUCCESS) {
free_bytebuffer(buffer);
return res;
}
break;
}
case KANJI: {
gory("Decoding Kanji segment representing %d characters\n", count);
int res = decode_kanji_segment(stream, count, buffer);
if (res != SUCCESS) {
free_bytebuffer(buffer);
return res;
}
break;
}
}
break;
}
default: {
// Unknown mode
free_bytebuffer(buffer);
return DECODING_ERROR;
}
}
} while (mode != TERMINATOR);
unsigned int n = buffer->n_bytes;
// Let's turn the buffer into a null-terminated string
if (MEMORY_ERROR == write_byte(buffer, 0)) {
free_bytebuffer(buffer);
return MEMORY_ERROR;
}
// Let's steal the byte array from the byte buffer
*decoded = buffer->bytes;
buffer->bytes = NULL;
free_bytebuffer(buffer);
return n;
}