-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathean8.c
253 lines (225 loc) · 5.07 KB
/
ean8.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
/**
* @file ean8.c
* @brief ean8 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 "ean8.h"
//https://en.wikipedia.org/wiki/International_Article_Number
#if 0
#define DEBUG
#endif
//add left/right blank for barcode
#define EAN8_APPEND_BLANK
//add left blank to display human readable string(first digit)
#ifdef EAN8_APPEND_BLANK
#define EAN8_BLANK_LEN 8
#endif
#define EAN8_INPUT_LEN 7
#define EAN8_PATTERN_LEN 7
//start/stop
#define EAN8_MARKER_INDEX 0
#define EAN8_MARKER_PATTERN_LEN 3
//center
#define EAN8_CENTER_MARKER_INDEX 1
#define EAN8_CENTER_PATTERN_LEN 5
static const s32 ean8_marker_pattern[] = {
0x05, //start/stop
0x0A //center
};
//left-hand encoding
static const s32 ean8_left_pattern[] = {
0x0D, //0
0x19, //1
0x13, //2
0x3D, //3
0x23, //4
0x31, //5
0x2F, //6
0x3B, //7
0x37, //8
0x0B //9
};
//right-hand parity encoding
static const s32 ean8_right_pattern[] = {
0x72, //0
0x66, //1
0x6C, //2
0x42, //3
0x5C, //4
0x4E, //5
0x50, //6
0x44, //7
0x48, //8
0x74 //9
};
#ifdef EAN8_APPEND_BLANK
static s32 ean8_append_blank(s8 *out) {
memset(out, 0, EAN8_BLANK_LEN);
return EAN8_BLANK_LEN;
}
#endif
static s32 ean8_append_marker(s32 index, s8 *out) {
s32 i;
s32 marker = ean8_marker_pattern[index];
s32 marker_len = index ? EAN8_CENTER_PATTERN_LEN : EAN8_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 ean8_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;
//append left-hand
for(i=0; i<((EAN8_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) ? 3 : 1;
sum += index * weight;
pattern = ean8_left_pattern[index];
for (j = EAN8_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 += EAN8_PATTERN_LEN;
#ifdef DEBUG
printf("\n");
#endif
}
//append center marker
#ifdef DEBUG
printf("center->");
#endif
append_len = ean8_append_marker(EAN8_CENTER_MARKER_INDEX, out);
total_len += append_len;
out += append_len;
//append right-hand
for(; i<EAN8_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 = ean8_right_pattern[index];
weight = ((i%2) == 0) ? 3 : 1;
sum += index * weight;
for (j = EAN8_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 += EAN8_PATTERN_LEN;
}
//append checksum(modulo 10)
sum = (sum % 10 != 0) ? (10 - (sum % 10)) : 0;
*checksum = sum;
#ifdef DEBUG
printf("check:%d->",sum);
#endif
pattern = ean8_right_pattern[sum];
for (j = EAN8_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 += EAN8_PATTERN_LEN;
return total_len;
err:
return -1;
}
//len = start + (data-1) + checksum + stop
s32 ean8_max_len(const s8 *input) {
s32 len = 0;
if (input != NULL) {
len = (EAN8_INPUT_LEN + 1) * EAN8_PATTERN_LEN + (EAN8_MARKER_PATTERN_LEN<<1) + EAN8_CENTER_PATTERN_LEN;
#ifdef EAN8_APPEND_BLANK
len += (EAN8_BLANK_LEN << 1);
#endif
}
return len;
}
s32 ean8_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 != EAN8_INPUT_LEN) {
printf("%s %d input err\n",__func__,__LINE__);
goto end;
}
#ifdef EAN8_APPEND_BLANK
//append left blank
append_len = ean8_append_blank(output);
output += append_len;
barcode_len += append_len;
#endif
#ifdef DEBUG
printf("start->");
#endif
//append start code
append_len = ean8_append_marker(EAN8_MARKER_INDEX, output);
output += append_len;
barcode_len += append_len;
//append data code and checksum
append_len = ean8_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 = ean8_append_marker(EAN8_MARKER_INDEX, output);
output += append_len;
barcode_len += append_len;
#ifdef EAN8_APPEND_BLANK
//append right blank
append_len = ean8_append_blank(output);
output += append_len;
barcode_len += append_len;
#endif
end:
return barcode_len;
}