-
Notifications
You must be signed in to change notification settings - Fork 1
/
decoder.c
324 lines (286 loc) · 8.82 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
/*------------------------------------------------------------------------
* Copyright 2007-2008 (c) Jeff Brown <spadix@users.sourceforge.net>
*
* This file is part of the Zebra Barcode Library.
*
* The Zebra Barcode Library 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 Zebra Barcode Library 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 Zebra Barcode Library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* http://sourceforge.net/projects/zebra
*------------------------------------------------------------------------*/
#include "config.h"
#include <stdlib.h> /* malloc, free */
#include <stdio.h> /* snprintf */
#include <string.h> /* memset, strlen */
#include "zebra.h"
#include "decoder.h"
#if defined(DEBUG_DECODER) || defined(DEBUG_EAN) || defined(DEBUG_CODE128) || \
defined(DEBUG_CODE39) || defined(DEBUG_I25) || \
(defined(DEBUG_PDF417) && (DEBUG_PDF417 >= 4))
# define DEBUG_LEVEL 1
#endif
#include "debug.h"
zebra_decoder_t *zebra_decoder_create ()
{
zebra_decoder_t *dcode = malloc(sizeof(zebra_decoder_t));
dcode->buflen = BUFFER_MIN;
dcode->buf = malloc(dcode->buflen);
dcode->handler = dcode->userdata = NULL;
/* initialize default configs */
#ifdef ENABLE_EAN
dcode->ean.enable = 1;
dcode->ean.ean13_config = ((1 << ZEBRA_CFG_ENABLE) |
(1 << ZEBRA_CFG_EMIT_CHECK));
dcode->ean.ean8_config = ((1 << ZEBRA_CFG_ENABLE) |
(1 << ZEBRA_CFG_EMIT_CHECK));
dcode->ean.upca_config = 1 << ZEBRA_CFG_EMIT_CHECK;
dcode->ean.upce_config = 1 << ZEBRA_CFG_EMIT_CHECK;
dcode->ean.isbn10_config = 1 << ZEBRA_CFG_EMIT_CHECK;
dcode->ean.isbn13_config = 1 << ZEBRA_CFG_EMIT_CHECK;
#endif
#ifdef ENABLE_I25
dcode->i25.config = 1 << ZEBRA_CFG_ENABLE;
#endif
#ifdef ENABLE_CODE39
dcode->code39.config = 1 << ZEBRA_CFG_ENABLE;
#endif
#ifdef ENABLE_CODE128
dcode->code128.config = 1 << ZEBRA_CFG_ENABLE;
#endif
#ifdef ENABLE_PDF417
dcode->pdf417.config = 1 << ZEBRA_CFG_ENABLE;
#endif
zebra_decoder_reset(dcode);
return(dcode);
}
void zebra_decoder_destroy (zebra_decoder_t *dcode)
{
if(dcode->buf)
free(dcode->buf);
free(dcode);
}
void zebra_decoder_reset (zebra_decoder_t *dcode)
{
memset(dcode, 0, (long)&dcode->buf - (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
}
void zebra_decoder_new_scan (zebra_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
}
zebra_color_t zebra_decoder_get_color (const zebra_decoder_t *dcode)
{
return(get_color(dcode));
}
const char *zebra_decoder_get_data (const zebra_decoder_t *dcode)
{
return((char*)dcode->buf);
}
zebra_decoder_handler_t *
zebra_decoder_set_handler (zebra_decoder_t *dcode,
zebra_decoder_handler_t handler)
{
zebra_decoder_handler_t *result = dcode->handler;
dcode->handler = handler;
return(result);
}
void zebra_decoder_set_userdata (zebra_decoder_t *dcode,
void *userdata)
{
dcode->userdata = userdata;
}
void *zebra_decoder_get_userdata (const zebra_decoder_t *dcode)
{
return(dcode->userdata);
}
zebra_symbol_type_t zebra_decoder_get_type (const zebra_decoder_t *dcode)
{
return(dcode->type);
}
zebra_symbol_type_t zebra_decode_width (zebra_decoder_t *dcode,
unsigned w)
{
zebra_symbol_type_t sym;
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 = ZEBRA_NONE;
#ifdef ENABLE_EAN
if((dcode->ean.enable) &&
(sym = _zebra_decode_ean(dcode)))
dcode->type = sym;
#endif
#ifdef ENABLE_CODE39
if(TEST_CFG(dcode->code39.config, ZEBRA_CFG_ENABLE) &&
(sym = _zebra_decode_code39(dcode)) > ZEBRA_PARTIAL)
dcode->type = sym;
#endif
#ifdef ENABLE_CODE128
if(TEST_CFG(dcode->code128.config, ZEBRA_CFG_ENABLE) &&
(sym = _zebra_decode_code128(dcode)) > ZEBRA_PARTIAL)
dcode->type = sym;
#endif
#ifdef ENABLE_I25
if(TEST_CFG(dcode->i25.config, ZEBRA_CFG_ENABLE) &&
(sym = _zebra_decode_i25(dcode)) > ZEBRA_PARTIAL)
dcode->type = sym;
#endif
#ifdef ENABLE_PDF417
if(TEST_CFG(dcode->pdf417.config, ZEBRA_CFG_ENABLE) &&
(sym = _zebra_decode_pdf417(dcode)) > ZEBRA_PARTIAL)
dcode->type = sym;
#endif
dcode->idx++;
if(dcode->type) {
if(dcode->handler)
dcode->handler(dcode);
if(dcode->lock && dcode->type > ZEBRA_PARTIAL)
dcode->lock = 0;
}
return(dcode->type);
}
int zebra_decoder_set_config (zebra_decoder_t *dcode,
zebra_symbol_type_t sym,
zebra_config_t cfg,
int val)
{
unsigned *config = NULL;
switch(sym) {
case ZEBRA_NONE:
zebra_decoder_set_config(dcode, ZEBRA_EAN13, cfg, val);
zebra_decoder_set_config(dcode, ZEBRA_EAN8, cfg, val);
zebra_decoder_set_config(dcode, ZEBRA_UPCA, cfg, val);
zebra_decoder_set_config(dcode, ZEBRA_UPCE, cfg, val);
zebra_decoder_set_config(dcode, ZEBRA_ISBN10, cfg, val);
zebra_decoder_set_config(dcode, ZEBRA_ISBN13, cfg, val);
zebra_decoder_set_config(dcode, ZEBRA_I25, cfg, val);
zebra_decoder_set_config(dcode, ZEBRA_CODE39, cfg, val);
zebra_decoder_set_config(dcode, ZEBRA_CODE128, cfg, val);
zebra_decoder_set_config(dcode, ZEBRA_PDF417, cfg, val);
return(0);
#ifdef ENABLE_EAN
case ZEBRA_EAN13:
config = &dcode->ean.ean13_config;
break;
case ZEBRA_EAN8:
config = &dcode->ean.ean8_config;
break;
case ZEBRA_UPCA:
config = &dcode->ean.upca_config;
break;
case ZEBRA_UPCE:
config = &dcode->ean.upce_config;
break;
case ZEBRA_ISBN10:
config = &dcode->ean.isbn10_config;
break;
case ZEBRA_ISBN13:
config = &dcode->ean.isbn13_config;
break;
#endif
#ifdef ENABLE_I25
case ZEBRA_I25:
config = &dcode->i25.config;
break;
#endif
#ifdef ENABLE_CODE39
case ZEBRA_CODE39:
config = &dcode->code39.config;
break;
#endif
#ifdef ENABLE_CODE128
case ZEBRA_CODE128:
config = &dcode->code128.config;
break;
#endif
#ifdef ENABLE_PDF417
case ZEBRA_PDF417:
config = &dcode->pdf417.config;
break;
#endif
/* FIXME handle addons */
default:
return(1);
}
if(!config || cfg >= ZEBRA_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,
ZEBRA_CFG_ENABLE);
#endif
return(0);
}
static char *decoder_dump = NULL;
const char *_zebra_decoder_buf_dump (unsigned char *buf,
unsigned int buflen)
{
char *p;
int i;
int dumplen = (buflen * 3) + 12;
if(!decoder_dump || dumplen > strlen(decoder_dump)) {
if(decoder_dump)
free(decoder_dump);
decoder_dump = malloc(dumplen);
}
p = decoder_dump +
snprintf(decoder_dump, 12, "buf[%04x]=",
(buflen > 0xffff) ? 0xffff : buflen);
for(i = 0; i < buflen; i++)
p += snprintf(p, 4, "%s%02x", (i) ? " " : "", buf[i]);
return(decoder_dump);
}