1 // Copyright (c) 2013, LKC Technologies, Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer. Redistributions in binary
9 // form must reproduce the above copyright notice, this list of conditions and
10 // the following disclaimer in the documentation and/or other materials
11 // provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
12 // HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
13 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
14 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
15 // COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
16 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
17 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
18 // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
19 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
21 // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
23 #include "../../../lvgl.h"
24 #if LV_USE_BARCODE
25
26 #include "code128.h"
27 #include <string.h>
28
29 #define CODE128_MALLOC lv_malloc
30 #define CODE128_REALLOC lv_realloc
31 #define CODE128_FREE lv_free
32 #define CODE128_MEMSET lv_memset
33 #define CODE128_STRLEN lv_strlen
34 #define CODE128_ASSERT LV_ASSERT
35
36 #define CODE128_QUIET_ZONE_LEN 10
37 #define CODE128_CHAR_LEN 11
38 #define CODE128_STOP_CODE_LEN 13
39
40 #define CODE128_START_CODE_A 103
41 #define CODE128_START_CODE_B 104
42 #define CODE128_START_CODE_C 105
43
44 #define CODE128_MODE_A 'a'
45 #define CODE128_MODE_B 'b'
46 #define CODE128_MODE_C 'c'
47
48 #define CODE128_MIN_ENCODE_LEN (CODE128_QUIET_ZONE_LEN * 2 + CODE128_CHAR_LEN * 2 + CODE128_STOP_CODE_LEN)
49
50 static const int code128_pattern[] = {
51 // value: pattern, bar/space widths
52 1740, // 0: 11011001100, 212222
53 1644, // 1: 11001101100, 222122
54 1638, // 2: 11001100110, 222221
55 1176, // 3: 10010011000, 121223
56 1164, // 4: 10010001100, 121322
57 1100, // 5: 10001001100, 131222
58 1224, // 6: 10011001000, 122213
59 1220, // 7: 10011000100, 122312
60 1124, // 8: 10001100100, 132212
61 1608, // 9: 11001001000, 221213
62 1604, // 10: 11001000100, 221312
63 1572, // 11: 11000100100, 231212
64 1436, // 12: 10110011100, 112232
65 1244, // 13: 10011011100, 122132
66 1230, // 14: 10011001110, 122231
67 1484, // 15: 10111001100, 113222
68 1260, // 16: 10011101100, 123122
69 1254, // 17: 10011100110, 123221
70 1650, // 18: 11001110010, 223211
71 1628, // 19: 11001011100, 221132
72 1614, // 20: 11001001110, 221231
73 1764, // 21: 11011100100, 213212
74 1652, // 22: 11001110100, 223112
75 1902, // 23: 11101101110, 312131
76 1868, // 24: 11101001100, 311222
77 1836, // 25: 11100101100, 321122
78 1830, // 26: 11100100110, 321221
79 1892, // 27: 11101100100, 312212
80 1844, // 28: 11100110100, 322112
81 1842, // 29: 11100110010, 322211
82 1752, // 30: 11011011000, 212123
83 1734, // 31: 11011000110, 212321
84 1590, // 32: 11000110110, 232121
85 1304, // 33: 10100011000, 111323
86 1112, // 34: 10001011000, 131123
87 1094, // 35: 10001000110, 131321
88 1416, // 36: 10110001000, 112313
89 1128, // 37: 10001101000, 132113
90 1122, // 38: 10001100010, 132311
91 1672, // 39: 11010001000, 211313
92 1576, // 40: 11000101000, 231113
93 1570, // 41: 11000100010, 231311
94 1464, // 42: 10110111000, 112133
95 1422, // 43: 10110001110
96 1134, // 44: 10001101110
97 1496, // 45: 10111011000, 113123
98 1478, // 46: 10111000110, 113321
99 1142, // 47: 10001110110, 133121
100 1910, // 48: 11101110110, 313121
101 1678, // 49: 11010001110, 211331
102 1582, // 50: 11000101110, 231131
103 1768, // 51: 11011101000, 213113
104 1762, // 52: 11011100010, 213311
105 1774, // 53: 11011101110, 213131
106 1880, // 54: 11101011000, 311123
107 1862, // 55: 11101000110, 311321
108 1814, // 56: 11100010110, 331121
109 1896, // 57: 11101101000, 312113
110 1890, // 58: 11101100010, 312311
111 1818, // 59: 11100011010, 332111
112 1914, // 60: 11101111010, 314111
113 1602, // 61: 11001000010, 221411
114 1930, // 62: 11110001010, 431111
115 1328, // 63: 10100110000, 111224
116 1292, // 64: 10100001100, 111422
117 1200, // 65: 10010110000, 121124
118 1158, // 66: 10010000110, 121421
119 1068, // 67: 10000101100, 141122
120 1062, // 68: 10000100110, 141221
121 1424, // 69: 10110010000, 112214
122 1412, // 70: 10110000100, 112412
123 1232, // 71: 10011010000, 122114
124 1218, // 72: 10011000010, 122411
125 1076, // 73: 10000110100, 142112
126 1074, // 74: 10000110010, 142211
127 1554, // 75: 11000010010, 241211
128 1616, // 76: 11001010000, 221114
129 1978, // 77: 11110111010, 413111
130 1556, // 78: 11000010100, 241112
131 1146, // 79: 10001111010, 134111
132 1340, // 80: 10100111100, 111242
133 1212, // 81: 10010111100, 121142
134 1182, // 82: 10010011110, 121241
135 1508, // 83: 10111100100, 114212
136 1268, // 84: 10011110100, 124112
137 1266, // 85: 10011110010, 124211
138 1956, // 86: 11110100100, 411212
139 1940, // 87: 11110010100, 421112
140 1938, // 88: 11110010010, 421211
141 1758, // 89: 11011011110, 212141
142 1782, // 90: 11011110110, 214121
143 1974, // 91: 11110110110, 412121
144 1400, // 92: 10101111000, 111143
145 1310, // 93: 10100011110, 111341
146 1118, // 94: 10001011110, 131141
147 1512, // 95: 10111101000, 114113
148 1506, // 96: 10111100010, 114311
149 1960, // 97: 11110101000, 411113
150 1954, // 98: 11110100010, 411311
151 1502, // 99: 10111011110, 113141
152 1518, // 100: 10111101110, 114131
153 1886, // 101: 11101011110, 311141
154 1966, // 102: 11110101110, 411131
155 1668, // 103: 11010000100, 211412
156 1680, // 104: 11010010000, 211214
157 1692 // 105: 11010011100, 211232
158 };
159
160 static const int code128_stop_pattern = 6379; // 1100011101011, 2331112
161
162 struct code128_step {
163 int prev_ix; // Index of previous step, if any
164 const char * next_input; // Remaining input
165 unsigned short len; // The length of the pattern so far (includes this step)
166 char mode; // State for the current encoding
167 signed char code; // What code should be written for this step
168 };
169
170 struct code128_state {
171 struct code128_step * steps;
172 int allocated_steps;
173 int current_ix;
174 int todo_ix;
175 int best_ix;
176
177 size_t maxlength;
178 };
179
code128_estimate_len(const char * s)180 size_t code128_estimate_len(const char * s)
181 {
182 return CODE128_QUIET_ZONE_LEN
183 + CODE128_CHAR_LEN // start code
184 + CODE128_CHAR_LEN * (CODE128_STRLEN(s) * 11 / 10) // contents + 10% padding
185 + CODE128_CHAR_LEN // checksum
186 + CODE128_STOP_CODE_LEN
187 + CODE128_QUIET_ZONE_LEN;
188 }
189
code128_append_pattern(int pattern,int pattern_length,char * out)190 static void code128_append_pattern(int pattern, int pattern_length, char * out)
191 {
192 // All patterns have their first bit set by design
193 CODE128_ASSERT(pattern & (1 << (pattern_length - 1)));
194
195 int i;
196 for(i = pattern_length - 1; i >= 0; i--) {
197 // cast avoids warning: implicit conversion from 'int' to 'char' changes value from 255 to -1 [-Wconstant-conversion]
198 *out++ = (unsigned char)((pattern & (1 << i)) ? 255 : 0);
199 }
200 }
201
code128_append_code(int code,char * out)202 static int code128_append_code(int code, char * out)
203 {
204 CODE128_ASSERT(code >= 0 && code < (int)(sizeof(code128_pattern) / sizeof(code128_pattern[0])));
205 code128_append_pattern(code128_pattern[code], CODE128_CHAR_LEN, out);
206 return CODE128_CHAR_LEN;
207 }
208
code128_append_stop_code(char * out)209 static int code128_append_stop_code(char * out)
210 {
211 code128_append_pattern(code128_stop_pattern, CODE128_STOP_CODE_LEN, out);
212 return CODE128_STOP_CODE_LEN;
213 }
214
code128_switch_code(char from_mode,char to_mode)215 static signed char code128_switch_code(char from_mode, char to_mode)
216 {
217 switch(from_mode) {
218 case CODE128_MODE_A:
219 switch(to_mode) {
220 case CODE128_MODE_B:
221 return 100;
222 case CODE128_MODE_C:
223 return 99;
224 }
225 break;
226
227 case CODE128_MODE_B:
228 switch(to_mode) {
229 case CODE128_MODE_A:
230 return 101;
231 case CODE128_MODE_C:
232 return 99;
233 }
234 break;
235
236 case CODE128_MODE_C:
237 switch(to_mode) {
238 case CODE128_MODE_B:
239 return 100;
240 case CODE128_MODE_A:
241 return 101;
242 }
243 break;
244 default:
245 break;
246 }
247
248 CODE128_ASSERT(0); // Invalid mode switch
249 return -1;
250 }
251
code128a_ascii_to_code(signed char value)252 static signed char code128a_ascii_to_code(signed char value)
253 {
254 if(value >= ' ' && value <= '_')
255 return (signed char)(value - ' ');
256 else if(value >= 0 && value < ' ')
257 return (signed char)(value + 64);
258 else if(value == (signed char)CODE128_FNC1)
259 return 102;
260 else if(value == (signed char)CODE128_FNC2)
261 return 97;
262 else if(value == (signed char)CODE128_FNC3)
263 return 96;
264 else if(value == (signed char)CODE128_FNC4)
265 return 101;
266 else
267 return -1;
268 }
269
code128b_ascii_to_code(signed char value)270 static signed char code128b_ascii_to_code(signed char value)
271 {
272 if(value >= ' ') // value <= 127 is implied
273 return (signed char)(value - ' ');
274 else if(value == (signed char)CODE128_FNC1)
275 return 102;
276 else if(value == (signed char)CODE128_FNC2)
277 return 97;
278 else if(value == (signed char)CODE128_FNC3)
279 return 96;
280 else if(value == (signed char)CODE128_FNC4)
281 return 100;
282 else
283 return -1;
284 }
285
code128c_ascii_to_code(const char * values)286 static signed char code128c_ascii_to_code(const char * values)
287 {
288 if(values[0] == CODE128_FNC1)
289 return 102;
290
291 if(values[0] >= '0' && values[0] <= '9' &&
292 values[1] >= '0' && values[1] <= '9') {
293 char code = 10 * (values[0] - '0') + (values[1] - '0');
294 return code;
295 }
296
297 return -1;
298 }
299
code128_do_a_step(struct code128_step * base,int prev_ix,int ix)300 static int code128_do_a_step(struct code128_step * base, int prev_ix, int ix)
301 {
302 struct code128_step * previous_step = &base[prev_ix];
303 struct code128_step * step = &base[ix];
304
305 char value = *previous_step->next_input;
306 // NOTE: Currently we can't encode NULL
307 if(value == 0)
308 return 0;
309
310 step->code = code128a_ascii_to_code(value);
311 if(step->code < 0)
312 return 0;
313
314 step->prev_ix = prev_ix;
315 step->next_input = previous_step->next_input + 1;
316 step->mode = CODE128_MODE_A;
317 step->len = previous_step->len + CODE128_CHAR_LEN;
318 if(step->mode != previous_step->mode)
319 step->len += CODE128_CHAR_LEN; // Need to switch modes
320
321 return 1;
322 }
323
code128_do_b_step(struct code128_step * base,int prev_ix,int ix)324 static int code128_do_b_step(struct code128_step * base, int prev_ix, int ix)
325 {
326 struct code128_step * previous_step = &base[prev_ix];
327 struct code128_step * step = &base[ix];
328
329 char value = *previous_step->next_input;
330 // NOTE: Currently we can't encode NULL
331 if(value == 0)
332 return 0;
333
334 step->code = code128b_ascii_to_code(value);
335 if(step->code < 0)
336 return 0;
337
338 step->prev_ix = prev_ix;
339 step->next_input = previous_step->next_input + 1;
340 step->mode = CODE128_MODE_B;
341 step->len = previous_step->len + CODE128_CHAR_LEN;
342 if(step->mode != previous_step->mode)
343 step->len += CODE128_CHAR_LEN; // Need to switch modes
344
345 return 1;
346 }
347
code128_do_c_step(struct code128_step * base,int prev_ix,int ix)348 static int code128_do_c_step(struct code128_step * base, int prev_ix, int ix)
349 {
350 struct code128_step * previous_step = &base[prev_ix];
351 struct code128_step * step = &base[ix];
352
353 char value = *previous_step->next_input;
354 // NOTE: Currently we can't encode NULL
355 if(value == 0)
356 return 0;
357
358 step->code = code128c_ascii_to_code(previous_step->next_input);
359 if(step->code < 0)
360 return 0;
361
362 step->prev_ix = prev_ix;
363 step->next_input = previous_step->next_input + 1;
364
365 // Mode C consumes 2 characters for codes 0-99
366 if(step->code < 100)
367 step->next_input++;
368
369 step->mode = CODE128_MODE_C;
370 step->len = previous_step->len + CODE128_CHAR_LEN;
371 if(step->mode != previous_step->mode)
372 step->len += CODE128_CHAR_LEN; // Need to switch modes
373
374 return 1;
375 }
376
code128_alloc_step(struct code128_state * state)377 static struct code128_step * code128_alloc_step(struct code128_state * state)
378 {
379 if(state->todo_ix >= state->allocated_steps) {
380 state->allocated_steps += 1024;
381 state->steps = (struct code128_step *) CODE128_REALLOC(state->steps,
382 state->allocated_steps * sizeof(struct code128_step));
383 }
384
385 struct code128_step * step = &state->steps[state->todo_ix];
386
387 CODE128_MEMSET(step, 0, sizeof(*step));
388 return step;
389 }
390
code128_do_step(struct code128_state * state)391 static void code128_do_step(struct code128_state * state)
392 {
393 struct code128_step * step = &state->steps[state->current_ix];
394 if(*step->next_input == 0) {
395 // Done, so see if we have a new shortest encoding.
396 if((step->len < state->maxlength) ||
397 (state->best_ix < 0 && step->len == state->maxlength)) {
398 state->best_ix = state->current_ix;
399
400 // Update maxlength to avoid considering anything longer
401 state->maxlength = step->len;
402 }
403 return;
404 }
405
406 // Don't try if we're already at or beyond the max acceptable
407 // length;
408 if(step->len >= state->maxlength)
409 return;
410 char mode = step->mode;
411
412 code128_alloc_step(state);
413 int mode_c_worked = 0;
414
415 // Always try mode C
416 if(code128_do_c_step(state->steps, state->current_ix, state->todo_ix)) {
417 state->todo_ix++;
418 code128_alloc_step(state);
419 mode_c_worked = 1;
420 }
421
422 if(mode == CODE128_MODE_A) {
423 // If A works, stick with A. There's no advantage to switching
424 // to B proactively if A still works.
425 if(code128_do_a_step(state->steps, state->current_ix, state->todo_ix) ||
426 code128_do_b_step(state->steps, state->current_ix, state->todo_ix))
427 state->todo_ix++;
428 }
429 else if(mode == CODE128_MODE_B) {
430 // The same logic applies here. There's no advantage to switching
431 // proactively to A if B still works.
432 if(code128_do_b_step(state->steps, state->current_ix, state->todo_ix) ||
433 code128_do_a_step(state->steps, state->current_ix, state->todo_ix))
434 state->todo_ix++;
435 }
436 else if(!mode_c_worked) {
437 // In mode C. If mode C worked and we're in mode C, trying anything
438 // else is pointless since the mode C encoding will be shorter and
439 // there won't be any mode switches.
440
441 // If we're leaving mode C, though, try both in case one ends up
442 // better than the other.
443 if(code128_do_a_step(state->steps, state->current_ix, state->todo_ix)) {
444 state->todo_ix++;
445 code128_alloc_step(state);
446 }
447 if(code128_do_b_step(state->steps, state->current_ix, state->todo_ix))
448 state->todo_ix++;
449 }
450 }
451
code128_encode_raw(const char * s,char * out,size_t maxlength)452 size_t code128_encode_raw(const char * s, char * out, size_t maxlength)
453 {
454 struct code128_state state;
455
456 const size_t overhead = CODE128_QUIET_ZONE_LEN
457 + CODE128_CHAR_LEN // checksum
458 + CODE128_STOP_CODE_LEN
459 + CODE128_QUIET_ZONE_LEN;
460 if(maxlength < overhead + CODE128_CHAR_LEN + CODE128_CHAR_LEN) {
461 // Need space to encode the start character and one additional
462 // character.
463 return 0;
464 }
465
466 state.allocated_steps = 256;
467 state.steps = (struct code128_step *) CODE128_MALLOC(state.allocated_steps * sizeof(struct code128_step));
468 state.current_ix = 0;
469 state.todo_ix = 0;
470 state.maxlength = maxlength - overhead;
471 state.best_ix = -1;
472
473 // Initialize the first 3 steps for the 3 encoding routes (A, B, C)
474 state.steps[0].prev_ix = -1;
475 state.steps[0].next_input = s;
476 state.steps[0].len = CODE128_CHAR_LEN;
477 state.steps[0].mode = CODE128_MODE_C;
478 state.steps[0].code = CODE128_START_CODE_C;
479
480 state.steps[1].prev_ix = -1;
481 state.steps[1].next_input = s;
482 state.steps[1].len = CODE128_CHAR_LEN;
483 state.steps[1].mode = CODE128_MODE_A;
484 state.steps[1].code = CODE128_START_CODE_A;
485
486 state.steps[2].prev_ix = -1;
487 state.steps[2].next_input = s;
488 state.steps[2].len = CODE128_CHAR_LEN;
489 state.steps[2].mode = CODE128_MODE_B;
490 state.steps[2].code = CODE128_START_CODE_B;
491
492 state.todo_ix = 3;
493
494 // Keep going until no more work
495 do {
496 code128_do_step(&state);
497 state.current_ix++;
498 } while(state.current_ix != state.todo_ix);
499
500 // If no best_step, then fail.
501 if(state.best_ix < 0) {
502 CODE128_FREE(state.steps);
503 return 0;
504 }
505
506 // Determine the list of codes
507 size_t num_codes = state.maxlength / CODE128_CHAR_LEN;
508 char * codes = CODE128_MALLOC(num_codes);
509 CODE128_ASSERT(codes);
510
511 struct code128_step * step = &state.steps[state.best_ix];
512 size_t i;
513 for(i = num_codes - 1; i > 0; --i) {
514 struct code128_step * prev_step = &state.steps[step->prev_ix];
515 codes[i] = step->code;
516 if(step->mode != prev_step->mode) {
517 --i;
518 codes[i] = code128_switch_code(prev_step->mode, step->mode);
519 }
520 step = prev_step;
521 }
522 codes[0] = step->code;
523
524 // Encode everything up to the checksum
525 size_t actual_length = state.maxlength + overhead;
526 CODE128_MEMSET(out, 0, CODE128_QUIET_ZONE_LEN);
527 out += CODE128_QUIET_ZONE_LEN;
528 for(i = 0; i < num_codes; i++)
529 out += code128_append_code(codes[i], out);
530
531 // Compute the checksum
532 int sum = codes[0];
533 for(i = 1; i < num_codes; i++)
534 sum += (int)(codes[i] * i);
535 out += code128_append_code(sum % 103, out);
536
537 // Finalize the code.
538 out += code128_append_stop_code(out);
539 CODE128_MEMSET(out, 0, CODE128_QUIET_ZONE_LEN);
540
541 CODE128_FREE(codes);
542 CODE128_FREE(state.steps);
543 return actual_length;
544 }
545
546 /**
547 * @brief Encode the GS1 string
548 *
549 * This converts [FNC1] sequences to raw FNC1 characters and
550 * removes spaces before encoding the barcodes.
551 *
552 * @return the length of barcode data in bytes
553 */
code128_encode_gs1(const char * s,char * out,size_t maxlength)554 size_t code128_encode_gs1(const char * s, char * out, size_t maxlength)
555 {
556 size_t raw_size = CODE128_STRLEN(s) + 1;
557 char * raw = CODE128_MALLOC(raw_size);
558 CODE128_ASSERT(raw);
559 if(!raw) {
560 return 0;
561 }
562
563 char * p = raw;
564 for(; *s != '\0'; s++) {
565 if(strncmp(s, "[FNC1]", 6) == 0) {
566 *p++ = CODE128_FNC1;
567 s += 5;
568 }
569 else if(*s != ' ') {
570 *p++ = *s;
571 }
572 }
573 *p = '\0';
574
575 size_t length = code128_encode_raw(raw, out, maxlength);
576
577 CODE128_FREE(raw);
578
579 return length;
580 }
581
582 #endif /*LV_USE_BARCODE*/
583