-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.c
414 lines (366 loc) · 11.4 KB
/
decoder.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
/*------------------------------------------------------------------------
* Copyright 2007-2009 (c) Jeff Brown <spadix@users.sourceforge.net>
*
* This file is part of the ZBar Bar Code Reader.
*
* The ZBar Bar Code Reader is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* The ZBar Bar Code Reader is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser Public License
* along with the ZBar Bar Code Reader; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* http://sourceforge.net/projects/zbar
*------------------------------------------------------------------------*/
#include <config.h>
#include <stdlib.h> /* malloc, calloc, free */
#include <stdio.h> /* snprintf */
#include <string.h> /* memset, strlen */
#include <zbar.h>
#include "decoder.h"
//#include "mini_stdint.h"
#include "vmsys.h"
#include "vmio.h"
#define malloc vm_malloc
#define free vm_free
#define calloc(x,y) vm_calloc(x*y)
#define realloc vm_realloc
#define assert(x) if(!(x)){vm_exit_app();while(1){};}
#if defined(DEBUG_DECODER) || defined(DEBUG_EAN) || \
defined(DEBUG_CODE39) || defined(DEBUG_I25) || \
defined(DEBUG_CODE128) || defined(DEBUG_QR_FINDER) || \
(defined(DEBUG_PDF417) && (DEBUG_PDF417 >= 4))
# define DEBUG_LEVEL 1
#endif
#include "debug.h"
zbar_decoder_t *zbar_decoder_create ()
{
zbar_decoder_t *dcode = calloc(1, sizeof(zbar_decoder_t));
dcode->buf_alloc = BUFFER_MIN;
dcode->buf = malloc(dcode->buf_alloc);
/* initialize default configs */
#ifdef ENABLE_EAN
dcode->ean.enable = 1;
dcode->ean.ean13_config = ((1 << ZBAR_CFG_ENABLE) |
(1 << ZBAR_CFG_EMIT_CHECK));
dcode->ean.ean8_config = ((1 << ZBAR_CFG_ENABLE) |
(1 << ZBAR_CFG_EMIT_CHECK));
dcode->ean.upca_config = 1 << ZBAR_CFG_EMIT_CHECK;
dcode->ean.upce_config = 1 << ZBAR_CFG_EMIT_CHECK;
dcode->ean.isbn10_config = 1 << ZBAR_CFG_EMIT_CHECK;
dcode->ean.isbn13_config = 1 << ZBAR_CFG_EMIT_CHECK;
#endif
#ifdef ENABLE_I25
dcode->i25.config = 1 << ZBAR_CFG_ENABLE;
CFG(dcode->i25, ZBAR_CFG_MIN_LEN) = 6;
#endif
#ifdef ENABLE_CODE39
dcode->code39.config = 1 << ZBAR_CFG_ENABLE;
CFG(dcode->code39, ZBAR_CFG_MIN_LEN) = 1;
#endif
#ifdef ENABLE_CODE128
dcode->code128.config = 1 << ZBAR_CFG_ENABLE;
#endif
#ifdef ENABLE_PDF417
dcode->pdf417.config = 1 << ZBAR_CFG_ENABLE;
#endif
#ifdef ENABLE_QRCODE
dcode->qrf.config = 1 << ZBAR_CFG_ENABLE;
#endif
zbar_decoder_reset(dcode);
return(dcode);
}
void zbar_decoder_destroy (zbar_decoder_t *dcode)
{
if(dcode->buf)
free(dcode->buf);
free(dcode);
}
void zbar_decoder_reset (zbar_decoder_t *dcode)
{
memset(dcode, 0, (long)&dcode->buf_alloc - (long)dcode);
#ifdef ENABLE_EAN
ean_reset(&dcode->ean);
#endif
#ifdef ENABLE_I25
i25_reset(&dcode->i25);
#endif
#ifdef ENABLE_CODE39
code39_reset(&dcode->code39);
#endif
#ifdef ENABLE_CODE128
code128_reset(&dcode->code128);
#endif
#ifdef ENABLE_PDF417
pdf417_reset(&dcode->pdf417);
#endif
#ifdef ENABLE_QRCODE
qr_finder_reset(&dcode->qrf);
#endif
}
void zbar_decoder_new_scan (zbar_decoder_t *dcode)
{
/* soft reset decoder */
memset(dcode->w, 0, sizeof(dcode->w));
dcode->lock = 0;
dcode->idx = 0;
#ifdef ENABLE_EAN
ean_new_scan(&dcode->ean);
#endif
#ifdef ENABLE_I25
i25_reset(&dcode->i25);
#endif
#ifdef ENABLE_CODE39
code39_reset(&dcode->code39);
#endif
#ifdef ENABLE_CODE128
code128_reset(&dcode->code128);
#endif
#ifdef ENABLE_PDF417
pdf417_reset(&dcode->pdf417);
#endif
#ifdef ENABLE_QRCODE
qr_finder_reset(&dcode->qrf);
#endif
}
zbar_color_t zbar_decoder_get_color (const zbar_decoder_t *dcode)
{
return(get_color(dcode));
}
const char *zbar_decoder_get_data (const zbar_decoder_t *dcode)
{
return((char*)dcode->buf);
}
unsigned int zbar_decoder_get_data_length (const zbar_decoder_t *dcode)
{
return(dcode->buflen);
}
zbar_decoder_handler_t *
zbar_decoder_set_handler (zbar_decoder_t *dcode,
zbar_decoder_handler_t handler)
{
zbar_decoder_handler_t *result = dcode->handler;
dcode->handler = handler;
return(result);
}
void zbar_decoder_set_userdata (zbar_decoder_t *dcode,
void *userdata)
{
dcode->userdata = userdata;
}
void *zbar_decoder_get_userdata (const zbar_decoder_t *dcode)
{
return(dcode->userdata);
}
zbar_symbol_type_t zbar_decoder_get_type (const zbar_decoder_t *dcode)
{
return(dcode->type);
}
zbar_symbol_type_t zbar_decode_width (zbar_decoder_t *dcode,
unsigned w)
{
zbar_symbol_type_t sym = 0;
dcode->w[dcode->idx & (DECODE_WINDOW - 1)] = w;
dprintf(1, " decode[%x]: w=%d (%g)\n", dcode->idx, w, (w / 32.));
/* each decoder processes width stream in parallel */
sym = dcode->type = ZBAR_NONE;
#ifdef ENABLE_EAN
if((dcode->ean.enable) &&
(sym = _zbar_decode_ean(dcode)))
dcode->type = sym;
#endif
#ifdef ENABLE_CODE39
if(TEST_CFG(dcode->code39.config, ZBAR_CFG_ENABLE) &&
(sym = _zbar_decode_code39(dcode)) > ZBAR_PARTIAL)
dcode->type = sym;
#endif
#ifdef ENABLE_CODE128
if(TEST_CFG(dcode->code128.config, ZBAR_CFG_ENABLE) &&
(sym = _zbar_decode_code128(dcode)) > ZBAR_PARTIAL)
dcode->type = sym;
#endif
#ifdef ENABLE_I25
if(TEST_CFG(dcode->i25.config, ZBAR_CFG_ENABLE) &&
(sym = _zbar_decode_i25(dcode)) > ZBAR_PARTIAL)
dcode->type = sym;
#endif
#ifdef ENABLE_PDF417
if(TEST_CFG(dcode->pdf417.config, ZBAR_CFG_ENABLE) &&
(sym = _zbar_decode_pdf417(dcode)) > ZBAR_PARTIAL)
dcode->type = sym;
#endif
#ifdef ENABLE_QRCODE
if(TEST_CFG(dcode->qrf.config, ZBAR_CFG_ENABLE) &&
(sym = _zbar_find_qr(dcode)) > ZBAR_PARTIAL)
dcode->type = sym;
#endif
dcode->idx++;
if(dcode->type) {
if(dcode->handler)
dcode->handler(dcode);
if(dcode->lock && dcode->type > ZBAR_PARTIAL)
dcode->lock = 0;
}
return(dcode->type);
}
static int decoder_set_config_bool (zbar_decoder_t *dcode,
zbar_symbol_type_t sym,
zbar_config_t cfg,
int val)
{
unsigned *config = NULL;
switch(sym) {
#ifdef ENABLE_EAN
case ZBAR_EAN13:
config = &dcode->ean.ean13_config;
break;
case ZBAR_EAN8:
config = &dcode->ean.ean8_config;
break;
case ZBAR_UPCA:
config = &dcode->ean.upca_config;
break;
case ZBAR_UPCE:
config = &dcode->ean.upce_config;
break;
case ZBAR_ISBN10:
config = &dcode->ean.isbn10_config;
break;
case ZBAR_ISBN13:
config = &dcode->ean.isbn13_config;
break;
#endif
#ifdef ENABLE_I25
case ZBAR_I25:
config = &dcode->i25.config;
break;
#endif
#ifdef ENABLE_CODE39
case ZBAR_CODE39:
config = &dcode->code39.config;
break;
#endif
#ifdef ENABLE_CODE128
case ZBAR_CODE128:
config = &dcode->code128.config;
break;
#endif
#ifdef ENABLE_PDF417
case ZBAR_PDF417:
config = &dcode->pdf417.config;
break;
#endif
#ifdef ENABLE_QRCODE
case ZBAR_QRCODE:
config = &dcode->qrf.config;
break;
#endif
/* FIXME handle addons */
default:
return(1);
}
if(!config || cfg >= ZBAR_CFG_NUM)
return(1);
if(!val)
*config &= ~(1 << cfg);
else if(val == 1)
*config |= (1 << cfg);
else
return(1);
#ifdef ENABLE_EAN
dcode->ean.enable = TEST_CFG(dcode->ean.ean13_config |
dcode->ean.ean8_config |
dcode->ean.upca_config |
dcode->ean.upce_config |
dcode->ean.isbn10_config |
dcode->ean.isbn13_config,
ZBAR_CFG_ENABLE);
#endif
return(0);
}
static int decoder_set_config_int (zbar_decoder_t *dcode,
zbar_symbol_type_t sym,
zbar_config_t cfg,
int val)
{
switch(sym) {
#ifdef ENABLE_I25
case ZBAR_I25:
CFG(dcode->i25, cfg) = val;
break;
#endif
#ifdef ENABLE_CODE39
case ZBAR_CODE39:
CFG(dcode->code39, cfg) = val;
break;
#endif
#ifdef ENABLE_CODE128
case ZBAR_CODE128:
CFG(dcode->code128, cfg) = val;
break;
#endif
#ifdef ENABLE_PDF417
case ZBAR_PDF417:
CFG(dcode->pdf417, cfg) = val;
break;
#endif
default:
return(1);
}
return(0);
}
int zbar_decoder_set_config (zbar_decoder_t *dcode,
zbar_symbol_type_t sym,
zbar_config_t cfg,
int val)
{
if(sym == ZBAR_NONE) {
zbar_decoder_set_config(dcode, ZBAR_EAN13, cfg, val);
zbar_decoder_set_config(dcode, ZBAR_EAN8, cfg, val);
zbar_decoder_set_config(dcode, ZBAR_UPCA, cfg, val);
zbar_decoder_set_config(dcode, ZBAR_UPCE, cfg, val);
zbar_decoder_set_config(dcode, ZBAR_ISBN10, cfg, val);
zbar_decoder_set_config(dcode, ZBAR_ISBN13, cfg, val);
zbar_decoder_set_config(dcode, ZBAR_I25, cfg, val);
zbar_decoder_set_config(dcode, ZBAR_CODE39, cfg, val);
zbar_decoder_set_config(dcode, ZBAR_CODE128, cfg, val);
zbar_decoder_set_config(dcode, ZBAR_PDF417, cfg, val);
zbar_decoder_set_config(dcode, ZBAR_QRCODE, cfg, val);
return(0);
}
if(cfg >= 0 && cfg < ZBAR_CFG_NUM)
return(decoder_set_config_bool(dcode, sym, cfg, val));
else if(cfg >= ZBAR_CFG_MIN_LEN && cfg <= ZBAR_CFG_MAX_LEN)
return(decoder_set_config_int(dcode, sym, cfg, val));
else
return(1);
}
static char *decoder_dump = NULL;
static unsigned decoder_dumplen = 0;
const char *_zbar_decoder_buf_dump (unsigned char *buf,
unsigned int buflen)
{
int i;
char *p = NULL;
int dumplen = (buflen * 3) + 12;
if(!decoder_dump || dumplen > decoder_dumplen) {
if(decoder_dump)
free(decoder_dump);
decoder_dump = malloc(dumplen);
decoder_dumplen = dumplen;
}
p = decoder_dump +
sprintf(decoder_dump, "buf[%04x]=",
(buflen > 0xffff) ? 0xffff : buflen);
for(i = 0; i < buflen; i++)
p += sprintf(p, "%s%02x", (i) ? " " : "", buf[i]);
return(decoder_dump);
}