-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupce.c
449 lines (411 loc) · 9.06 KB
/
upce.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
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
/**
* @file upce.c
* @brief upce encoder
* @author Hansen.Z(hansen@pay-device.com)
* @version 1.0
* @date 2016-09-03
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "upce.h"
//https://en.wikipedia.org/wiki/International_Article_Number
#if 0
#define DEBUG
#endif
//add left/right blank for barcode
#define UPCE_APPEND_BLANK
//add left blank to display human readable string(first digit)
#ifdef UPCE_APPEND_BLANK
#define UPCE_BLANK_LEN 8
#endif
#define UPCA_INPUT_LEN 11
#define UPCE_INPUT_LEN 6
#define UPCE_PATTERN_LEN 7
//start
#define UPCE_START_INDEX 0
#define UPCE_START_PATTERN_LEN 3
//center and stop
#define UPCE_STOP_INDEX 1
#define UPCE_STOP_PATTERN_LEN 6
static const s32 upce_marker_pattern[] = {
0x05, //101 start
0x15 //010101 center and stop
};
//left-hand odd parity encoding
static const s32 upce_left_odd_pattern[] = {
0x0D, //0
0x19, //1
0x13, //2
0x3D, //3
0x23, //4
0x31, //5
0x2F, //6
0x3B, //7
0x37, //8
0x0B //9
};
//left-hand even parity encoding
static const s32 upce_left_even_pattern[] = {
0x27, //0
0x33, //1
0x1B, //2
0x21, //3
0x1D, //4
0x39, //5
0x05, //6
0x11, //7
0x09, //8
0x17 //9
};
//1:odd 0:even
static const s32 upce_system_0_parity_table[] = {
0x07, //EEEOOO
0x0B, //EEOEOO
0x0D, //EEOOEO
0x0E, //EEOOOE
0x13, //EOEEOO
0x19, //EOOEEO
0x1C, //EOOOEE
0x15, //EOEOEO
0x16, //EOEOOE
0x1A //EOOEOE
};
//1:odd 0:even
static const s32 upce_system_1_parity_table[] = {
0x38, //OOOEEE
0x34, //OOEOEE
0x32, //OOEEOE
0x31, //OOEEEO
0x2C, //OEOOEE
0x26, //OEEOOE
0x23, //OEEEOO
0x2A, //OEOEOE
0x29, //OEOEEO
0x25, //OEEOEO
};
#ifdef UPCE_APPEND_BLANK
static s32 upce_append_blank(s8 *out) {
memset(out, 0, UPCE_BLANK_LEN);
return UPCE_BLANK_LEN;
}
#endif
static s32 upce_append_marker(s32 index, s8 *out) {
s32 i;
s32 marker = upce_marker_pattern[index];
s32 marker_len = index ? UPCE_STOP_PATTERN_LEN : UPCE_START_PATTERN_LEN;
for (i = marker_len; i > 0; i--) {
#ifdef DEBUG
printf("%d",(marker & (1 << (i-1))) ? 1 : 0);
#endif
*out++ = (marker & (1 << (i-1))) ? 1 : 0;
}
#ifdef DEBUG
printf("\n");
#endif
return marker_len;
}
//input must be UPC-E 6 digits
static s32 upce_append_data(s8 start_code, const s8 *input, s8 *out, const s32 checksum) {
s32 i,j;
s32 index;
s32 pattern;
s32 parity = (start_code == '0') ? upce_system_0_parity_table[checksum] : upce_system_1_parity_table[checksum];
#ifdef DEBUG
printf("parity:");
for (j = UPCE_INPUT_LEN; j > 0; j--) {
printf("%c",(parity & (1 << (j-1))) ? 'O' : 'E');
}
printf("\n");
#endif
for(i=0; i<UPCE_INPUT_LEN; i++) {
index = *(input + i) - '0';
pattern = (parity & (1 << (UPCE_INPUT_LEN-i-1))) ? upce_left_odd_pattern[index] : upce_left_even_pattern[index];
#ifdef DEBUG
printf("%d->", index);
#endif
for (j = UPCE_PATTERN_LEN; j > 0; j--) {
#ifdef DEBUG
printf("%d",(pattern & (1 << (j-1))) ? 1 : 0);
#endif
*out++ = (pattern & (1 << (j-1))) ? 1 : 0;
}
#ifdef DEBUG
printf("\n");
#endif
}
return UPCE_INPUT_LEN * UPCE_PATTERN_LEN;
}
//convert UPC-A 11 digits to UPC-E 6 digits
static s32 upce_convert_from_upca(const s8 *from, s8 *to) {
s32 i;
s8 ends[3] = {0};
s8 product_code[5] = {0};
//get last three digits
for(i=5; i>2; i--) {
ends[i-3] = *(from + i);
}
//get product code
for(i=6; i>11; i++) {
product_code[i] = *(from + i);
}
//refer to:http://www.barcodeisland.com/upce.phtml#Conversion
if (ends[2] == '0' && ends[1] == '0' && ends[0] < '3') {
//check product code
if (atoi(product_code) > 999) {
printf("%s %d err:can't converted to UPC-E\n",__func__,__LINE__);
goto err;
}
//take first 2 digits
*to++ = *(from + 1);
*to++ = *(from + 2);
//take latest 3 digits
*to++ = *(from + 8);
*to++ = *(from + 9);
*to++ = *(from + 10);
//take end
*to = ends[0];
} else if (ends[2] == '0' && ends[1] == '0' && ends[0] > '2') {
//check product code
if (atoi(product_code) > 99) {
printf("%s %d err:can't converted to UPC-E\n",__func__,__LINE__);
goto err;
}
//take first 3 digits
*to++ = *(from + 1);
*to++ = *(from + 2);
*to++ = *(from + 3);
//take latest 2 digits
*to++ = *(from + 9);
*to++ = *(from + 10);
//end is '3'
*to = '3';
} else if (ends[2] == '0') {
//check product code
if (atoi(product_code) > 9) {
printf("%s %d err:can't converted to UPC-E\n",__func__,__LINE__);
goto err;
}
//take first 4 digits
for(i=1; i<5; i++) {
*to++ = *(from + i);
}
//take end digits
*to++ = *(from + 10);
//end is '4'
*to = '4';
} else {
//check product code
if (atoi(product_code) > 9 || atoi(product_code) < 5) {
printf("%s %d err:can't converted to UPC-E\n",__func__,__LINE__);
goto err;
}
//take manufacturer code
for(i=1; i<6; i++) {
*to++ = *(from + i);
}
//take end digits
*to++ = *(from + 10);
}
return 0;
err:
return -1;
}
//convert UPC-E 6 digits to UPC-A 11 digits
static s32 upce_convert_to_upca(const s8 *from, s8 *to) {
s8 end = *(from + UPCE_INPUT_LEN - 1);
s32 i;
//always start with '0'
*to++ = '0';
switch(end) {
case '0':
case '1':
case '2':
//take first 2 digits
*to++ = *from;
*to++ = *(from + 1);
//take the last ditit
*to++ = end;
//take four 0 digits
for(i=0; i<4; i++) {
*to++ = '0';
}
//take 3 through 5
for(i=2; i<5; i++) {
*to++ = *(from + i);
}
break;
case '3':
//take first 3 digits
for(i=0; i<3; i++) {
*to++ = *(from + i);
}
//take five 0 digits
for(i=0; i<5; i++) {
*to++ = '0';
}
//take 4 through 5
*to++ = *(from + 3);
*to++ = *(from + 4);
break;
case '4':
//take first 4 digits
for(i=0; i<4; i++) {
*to++ = *(from + i);
}
//take five 0 digits
for(i=0; i<5; i++) {
*to++ = '0';
}
//take 5th digit
*to++ = *(from + 4);
break;
case '5':
case '6':
case '7':
case '8':
case '9':
//take first 5 digits
for(i=0; i<5; i++) {
*to++ = *(from + i);
}
//take four 0 digits
for(i=0; i<4; i++) {
*to++ = '0';
}
//take last digit
*to = end;
break;
}
return 0;
}
static s32 upce_checksum(const s8 *input, s32 len) {
s32 i;
s32 sum = 0;
s32 weight = 0;
s32 index = 0;
s8 str[UPCA_INPUT_LEN] = {0};
if (len == UPCE_INPUT_LEN) {
//convert UPC-E to UPC-A 11 digits then compute checksum
upce_convert_to_upca(input, str);
} else {
memcpy(str, input, len);
}
for(i=0; i<UPCA_INPUT_LEN; i++) {
index = str[i] - '0';
if (index < 0 || index > 9) {
printf("%s %d input err\n",__func__,__LINE__);
goto err;
}
weight = ((i%2) == 0) ? 3 : 1;
sum += index * weight;
}
sum = (sum % 10 != 0) ? (10 - (sum % 10)) : 0;
#ifdef DEBUG
if (len == UPCE_INPUT_LEN) {
printf("UPCE->UPCA: ");
for(i=0;i<UPCA_INPUT_LEN;i++) {
printf("%c",str[i]);
}
printf("%d",sum);
printf("\n");
}
#endif
return sum;
err:
return -1;
}
//len = start + data + stop
s32 upce_max_len(const s8 *input) {
s32 len = 0;
if (input != NULL) {
len = UPCE_INPUT_LEN * UPCE_PATTERN_LEN + UPCE_START_PATTERN_LEN + UPCE_STOP_PATTERN_LEN;
#ifdef UPCE_APPEND_BLANK
len += (UPCE_BLANK_LEN << 1);
#endif
}
return len;
}
//input: 11 digits(must start with 0 or 1 to be converted UPC-E), 6 digits
s32 upce_encode(const s8 *input, s8 *output, s32 *checksum) {
s32 barcode_len = 0;
s32 input_len = 0;
s32 append_len = 0;
s8 str[UPCE_INPUT_LEN] = {0};
s8 start_code = '0';
if (input == NULL || output == NULL) {
printf("%s %d err\n",__func__,__LINE__);
goto end;
}
input_len = strlen(input);
if (input_len != UPCE_INPUT_LEN && input_len != UPCA_INPUT_LEN) {
printf("%s %d input err\n",__func__,__LINE__);
goto end;
}
if (input_len == UPCA_INPUT_LEN) {
//check start for upc-a
if (*input != '0' && *input != '1') {
printf("%s %d input err\n",__func__,__LINE__);
goto end;
}
//convert upc-a to upc-e
if (upce_convert_from_upca(input, str) < 0) {
printf("%s %d input err\n",__func__,__LINE__);
goto end;
}
start_code = *input;
#ifdef DEBUG
printf("UPCA->UPCE:");
#endif
} else {
#ifdef DEBUG
printf("UPCE:");
#endif
memcpy(str, input, UPCE_INPUT_LEN);
}
#ifdef DEBUG
int i;
for(i=0;i<UPCE_INPUT_LEN;i++) {
printf("%c",str[i]);
}
printf("\n");
#endif
#ifdef UPCE_APPEND_BLANK
//append left blank
append_len = upce_append_blank(output);
output += append_len;
barcode_len += append_len;
#endif
#ifdef DEBUG
printf("start->");
#endif
//append start code
append_len = upce_append_marker(UPCE_START_INDEX, output);
output += append_len;
barcode_len += append_len;
//compute check digit by input string
*checksum = upce_checksum(input, input_len);
//append data code and checksum
append_len = upce_append_data(start_code, str, output, *checksum);
if (append_len < 0) {
printf("%s %d end\n",__func__,__LINE__);
goto end;
}
output += append_len;
barcode_len += append_len;
#ifdef DEBUG
printf("stop->");
#endif
//append stop code
append_len = upce_append_marker(UPCE_STOP_INDEX, output);
output += append_len;
barcode_len += append_len;
#ifdef UPCE_APPEND_BLANK
//append right blank
append_len = upce_append_blank(output);
output += append_len;
barcode_len += append_len;
#endif
end:
return barcode_len;
}