-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathean13.c
284 lines (255 loc) · 5.72 KB
/
ean13.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
/**
* @file ean13.c
* @brief ean13 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 "ean13.h"
//https://en.wikipedia.org/wiki/International_Article_Number
#if 0
#define DEBUG
#endif
//add left/right blank for barcode
#define EAN13_APPEND_BLANK
//add left blank to display human readable string(first digit)
#ifdef EAN13_APPEND_BLANK
#define EAN13_BLANK_LEN 8
#endif
#define EAN13_INPUT_LEN 12
#define EAN13_PATTERN_LEN 7
//start/stop
#define EAN13_MARKER_INDEX 0
#define EAN13_MARKER_PATTERN_LEN 3
//center
#define EAN13_CENTER_MARKER_INDEX 1
#define EAN13_CENTER_PATTERN_LEN 5
static const s32 ean13_marker_pattern[] = {
0x05, //start/stop
0x0A //center
};
//left-hand odd parity encoding
static const s32 ean13_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 ean13_left_even_pattern[] = {
0x27, //0
0x33, //1
0x1B, //2
0x21, //3
0x1D, //4
0x39, //5
0x05, //6
0x11, //7
0x09, //8
0x17 //9
};
//right-hand parity encoding
static const s32 ean13_right_pattern[] = {
0x72, //0
0x66, //1
0x6C, //2
0x42, //3
0x5C, //4
0x4E, //5
0x50, //6
0x44, //7
0x48, //8
0x74 //9
};
//1:odd 0:even
static const s32 ean13_left_parity_table[] = {
0x3F, //OOOOOO
0x34, //OOEOEE
0x32, //OOEEOE
0x31, //OOEEEO
0x2C, //OEOOEE
0x26, //OEEOOE
0x23, //OEEEOO
0x2A, //OEOEOE
0x29, //OEOEEO
0x25 //OEEOEO
};
#ifdef EAN13_APPEND_BLANK
static s32 ean13_append_blank(s8 *out) {
memset(out, 0, EAN13_BLANK_LEN);
return EAN13_BLANK_LEN;
}
#endif
static s32 ean13_append_marker(s32 index, s8 *out) {
s32 i;
s32 marker = ean13_marker_pattern[index];
s32 marker_len = index ? EAN13_CENTER_PATTERN_LEN : EAN13_MARKER_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;
}
static s32 ean13_append_data(const s8 *input, s8 *out, s32 *checksum) {
s32 i,j;
s32 sum = 0;
s32 weight = 0;
s32 index = 0;
s32 append_len = 0;
s32 total_len = 0;
s32 pattern = 0;
s32 first_index = *input - '0';
//append left-hand
for(i=0; i<((EAN13_INPUT_LEN>>1) + 1); i++) {
index = *(input+i) - '0';
if (index < 0 || index > 9) {
printf("%s %d input err\n",__func__,__LINE__);
goto err;
}
#ifdef DEBUG
printf("%c index:%d->", *(input+i), index);
#endif
weight = ((i%2) == 0) ? 1 : 3;
sum += index * weight;
//skip first digit
if (i > 0) {
pattern = (ean13_left_parity_table[first_index] & (1 << (6-i))) ? ean13_left_odd_pattern[index] : ean13_left_even_pattern[index];
for (j = EAN13_PATTERN_LEN; j > 0; j--) {
#ifdef DEBUG
printf("%d",(pattern & (1 << (j-1))) ? 1 : 0);
#endif
*out++ = (pattern & (1 << (j-1))) ? 1 : 0;
}
total_len += EAN13_PATTERN_LEN;
}
#ifdef DEBUG
printf("\n");
#endif
}
//append center marker
#ifdef DEBUG
printf("center->");
#endif
append_len = ean13_append_marker(EAN13_CENTER_MARKER_INDEX, out);
total_len += append_len;
out += append_len;
//append right-hand
for(; i<EAN13_INPUT_LEN; i++) {
index = *(input+i) - '0';
if (index < 0 || index > 9) {
printf("%s %d input err\n",__func__,__LINE__);
goto err;
}
#ifdef DEBUG
printf("%c index:%d->", *(input+i), index);
#endif
pattern = ean13_right_pattern[index];
weight = ((i%2) == 0) ? 1 : 3;
sum += index * weight;
for (j = EAN13_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
total_len += EAN13_PATTERN_LEN;
}
//append checksum(modulo 10)
sum = (sum % 10 != 0) ? (10 - (sum % 10)) : 0;
*checksum = sum;
#ifdef DEBUG
printf("check:%d->",sum);
#endif
pattern = ean13_right_pattern[sum];
for (j = EAN13_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
total_len += EAN13_PATTERN_LEN;
return total_len;
err:
return -1;
}
//len = start + (data-1) + checksum + stop
s32 ean13_max_len(const s8 *input) {
s32 len = 0;
if (input != NULL) {
len = EAN13_INPUT_LEN * EAN13_PATTERN_LEN + (EAN13_MARKER_PATTERN_LEN<<1) + EAN13_CENTER_PATTERN_LEN;
#ifdef EAN13_APPEND_BLANK
len += (EAN13_BLANK_LEN << 1);
#endif
}
return len;
}
s32 ean13_encode(const s8 *input, s8 *output, s32 *checksum) {
s32 barcode_len = 0;
s32 input_len = 0;
s32 append_len = 0;
if (input == NULL || output == NULL) {
printf("%s %d err\n",__func__,__LINE__);
goto end;
}
input_len = strlen(input);
if (input_len != EAN13_INPUT_LEN) {
printf("%s %d input err\n",__func__,__LINE__);
goto end;
}
#ifdef EAN13_APPEND_BLANK
//append left blank
append_len = ean13_append_blank(output);
output += append_len;
barcode_len += append_len;
#endif
#ifdef DEBUG
printf("start->");
#endif
//append start code
append_len = ean13_append_marker(EAN13_MARKER_INDEX, output);
output += append_len;
barcode_len += append_len;
//append data code and checksum
append_len = ean13_append_data(input, 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 = ean13_append_marker(EAN13_MARKER_INDEX, output);
output += append_len;
barcode_len += append_len;
#ifdef EAN13_APPEND_BLANK
//append right blank
append_len = ean13_append_blank(output);
output += append_len;
barcode_len += append_len;
#endif
end:
return barcode_len;
}