forked from dani4/ZBarWin64
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_decode.c
273 lines (238 loc) · 8.2 KB
/
test_decode.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
/*------------------------------------------------------------------------
* Copyright 2007 (c) Jeff Brown <[email protected]>
*
* 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 <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <zebra.h>
zebra_decoder_t *decoder;
static void symbol_handler (zebra_decoder_t *decoder)
{
zebra_symbol_type_t sym = zebra_decoder_get_type(decoder);
if(sym <= ZEBRA_PARTIAL)
return;
printf("%s%s: %s\n",
zebra_get_symbol_name(sym),
zebra_get_addon_name(sym),
zebra_decoder_get_data(decoder));
}
static void encode_junk (int n)
{
printf("encode random junk...\n");
int i;
for(i = 0; i < n; i++)
zebra_decode_width(decoder, 10. * (rand() / (RAND_MAX + 1.)));
}
#define FWD 1
#define REV 0
static void encode (unsigned int units,
int fwd)
{
printf(" raw=%x%c\n", units, (fwd) ? '<' : '>');
if(!fwd)
while(units && !(units >> 0x1c))
units <<= 4;
while(units) {
unsigned char w = (fwd) ? units & 0xf : units >> 0x1c;
zebra_decode_width(decoder, w);
if(fwd)
units >>= 4;
else
units <<= 4;
}
}
/*------------------------------------------------------------*/
/* Code 128 encoding */
typedef enum code128_char_e {
FNC3 = 0x60,
FNC2 = 0x61,
SHIFT = 0x62,
CODE_C = 0x63,
CODE_B = 0x64,
CODE_A = 0x65,
FNC1 = 0x66,
START_A = 0x67,
START_B = 0x68,
START_C = 0x69,
STOP = 0x6a,
} code128_char_t;
static const unsigned int code128[107] = {
0x212222, 0x222122, 0x222221, 0x121223, /* 00 */
0x121322, 0x131222, 0x122213, 0x122312,
0x132212, 0x221213, 0x221312, 0x231212, /* 08 */
0x112232, 0x122132, 0x122231, 0x113222,
0x123122, 0x123221, 0x223211, 0x221132, /* 10 */
0x221231, 0x213212, 0x223112, 0x312131,
0x311222, 0x321122, 0x321221, 0x312212, /* 18 */
0x322112, 0x322211, 0x212123, 0x212321,
0x232121, 0x111323, 0x131123, 0x131321, /* 20 */
0x112313, 0x132113, 0x132311, 0x211313,
0x231113, 0x231311, 0x112133, 0x112331, /* 28 */
0x132131, 0x113123, 0x113321, 0x133121,
0x313121, 0x211331, 0x231131, 0x213113, /* 30 */
0x213311, 0x213131, 0x311123, 0x311321,
0x331121, 0x312113, 0x312311, 0x332111, /* 38 */
0x314111, 0x221411, 0x431111, 0x111224,
0x111422, 0x121124, 0x121421, 0x141122, /* 40 */
0x141221, 0x112214, 0x112412, 0x122114,
0x122411, 0x142112, 0x142211, 0x241211, /* 48 */
0x221114, 0x413111, 0x241112, 0x134111,
0x111242, 0x121142, 0x121241, 0x114212, /* 50 */
0x124112, 0x124211, 0x411212, 0x421112,
0x421211, 0x212141, 0x214121, 0x412121, /* 58 */
0x111143, 0x111341, 0x131141, 0x114113,
0x114311, 0x411113, 0x411311, 0x113141, /* 60 */
0x114131, 0x311141, 0x411131,
0x211412, 0x211214, 0x211232, /* 67 */
0x2331112a, /* STOP (6a) */
};
static void encode_code128b (unsigned char *data)
{
printf("------------------------------------------------------------\n"
"encode CODE-128(B): %s\n"
" encode START_B: %02x", data, START_B);
encode(code128[START_B], 0);
int i, chk = START_B;
for(i = 0; data[i]; i++) {
printf(" encode '%c': %02x", data[i], data[i] - 0x20);
encode(code128[data[i] - 0x20], 0);
chk += (i + 1) * (data[i] - 0x20);
}
chk %= 103;
printf(" encode checksum: %02x", chk);
encode(code128[chk], 0);
printf(" encode STOP: %02x", STOP);
encode(code128[STOP], 0);
printf("------------------------------------------------------------\n");
}
static void encode_code128c (unsigned char *data)
{
printf("------------------------------------------------------------\n"
"encode CODE-128(C): %s\n"
" encode START_C: %02x", data, START_C);
encode(code128[START_C], 0);
int i, chk = START_C;
for(i = 0; data[i]; i += 2) {
assert(data[i] >= '0');
assert(data[i + 1] >= '0');
unsigned char c = (data[i] - '0') * 10 + (data[i + 1] - '0');
printf(" encode '%c%c': %02d", data[i], data[i + 1], c);
encode(code128[c], 0);
chk += (i / 2 + 1) * c;
}
chk %= 103;
printf(" encode checksum: %02x", chk);
encode(code128[chk], 0);
printf(" encode STOP: %02x", STOP);
encode(code128[STOP], 0);
printf("------------------------------------------------------------\n");
}
/*------------------------------------------------------------*/
/* EAN/UPC encoding */
static const unsigned int ean_digits[10] = {
0x1123, 0x1222, 0x2212, 0x1141, 0x2311,
0x1321, 0x4111, 0x2131, 0x3121, 0x2113,
};
static const unsigned int ean_guard[] = {
0, 0,
0x11, /* [2] add-on delineator */
0x1117, /* [3] normal guard bars */
0x2117, /* [4] add-on guard bars */
0x11111, /* [5] center guard bars */
0x111111 /* [6] "special" guard bars */
};
static const unsigned char ean_parity_encode[] = {
0x3f, /* AAAAAA = 0 */
0x34, /* AABABB = 1 */
0x32, /* AABBAB = 2 */
0x31, /* AABBBA = 3 */
0x2c, /* ABAABB = 4 */
0x26, /* ABBAAB = 5 */
0x23, /* ABBBAA = 6 */
0x2a, /* ABABAB = 7 */
0x29, /* ABABBA = 8 */
0x25, /* ABBABA = 9 */
};
static void encode_ean13 (unsigned char *data)
{
int i;
unsigned char par = ean_parity_encode[data[0]];
printf("------------------------------------------------------------\n"
"encode EAN-13: %s (%02x)\n"
" encode start guard:",
data, par);
encode(ean_guard[3], FWD);
for(i = 1; i < 7; i++, par <<= 1) {
printf(" encode %x%c:", (par >> 5) & 1, data[i]);
encode(ean_digits[data[i] - '0'], REV ^ ((par >> 5) & 1));
}
printf(" encode center guard:");
encode(ean_guard[5], FWD);
for(; i < 13; i++) {
printf(" encode %x%c:", 0, data[i]);
encode(ean_digits[data[i] - '0'], FWD);
}
printf(" encode end guard:");
encode(ean_guard[3], REV);
printf("------------------------------------------------------------\n");
}
/*------------------------------------------------------------*/
/* main test flow */
int main (int argc, char **argv)
{
int i;
int rnd_size = 10; /* should be even */
srand(0xbabeface);
/* FIXME TBD:
* - random module width (!= 1.0)
* - simulate scan speed variance
* - simulate dark "swelling" and light "blooming"
* - inject parity errors
*/
decoder = zebra_decoder_create();
zebra_decoder_set_handler(decoder, symbol_handler);
encode_junk(rnd_size);
unsigned char data[32] = { 0 };
int chk = 0;
for(i = 0; i < 12; i++) {
unsigned char c = (rand() >> 16) % 10;
data[i] = c + '0';
chk += (i & 1) ? c * 3 : c;
}
data[i] = chk % 10;
if(data[i])
data[i] = 10 - data[i];
data[i] += '0';
data[++i] = 0;
encode_ean13(data);
encode_junk(rnd_size);
data[--i] = 0;
encode_code128c(data);
encode_junk(rnd_size);
for(i = 0; i < 10; i++)
data[i] = (rand() >> 16) % 0x5f + 0x20;
data[i] = 0;
encode_code128b(data);
encode_junk(rnd_size);
zebra_decoder_destroy(decoder);
return(0);
}