code39.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*------------------------------------------------------------------------
  2. * Copyright 2008-2009 (c) Jeff Brown <spadix@users.sourceforge.net>
  3. *
  4. * This file is part of the ZBar Bar Code Reader.
  5. *
  6. * The ZBar Bar Code Reader is free software; you can redistribute it
  7. * and/or modify it under the terms of the GNU Lesser Public License as
  8. * published by the Free Software Foundation; either version 2.1 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * The ZBar Bar Code Reader is distributed in the hope that it will be
  12. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser Public License
  17. * along with the ZBar Bar Code Reader; if not, write to the Free
  18. * Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  19. * Boston, MA 02110-1301 USA
  20. *
  21. * http://sourceforge.net/projects/zbar
  22. *------------------------------------------------------------------------*/
  23. #include <config.h>
  24. #include <string.h> /* memmove */
  25. #include <zbar.h>
  26. #include "decoder.h"
  27. #ifdef DEBUG_CODE39
  28. # define DEBUG_LEVEL (DEBUG_CODE39)
  29. #endif
  30. #include "debug.h"
  31. #define NUM_CHARS (0x2c)
  32. static const unsigned char code39_hi[32] = {
  33. 0x80 | 0x00, /* 2 next */
  34. 0x40 | 0x02, /* 4 */
  35. 0x80 | 0x06, /* 2 next */
  36. 0xc0 | 0x08, /* 2 skip */
  37. 0x40 | 0x0a, /* 4 */
  38. 0x80 | 0x0e, /* 2 next */
  39. 0xc0 | 0x10, /* 2 skip */
  40. 0x00 | 0x12, /* direct */
  41. 0x80 | 0x13, /* 2 next */
  42. 0xc0 | 0x15, /* 2 skip */
  43. 0x80 | 0x17, /* 2 next */
  44. 0xff,
  45. 0xc0 | 0x19, /* 2 skip */
  46. 0x00 | 0x1b, /* direct */
  47. 0xff,
  48. 0xff,
  49. 0x40 | 0x1c, /* 4 */
  50. 0x80 | 0x20, /* 2 next */
  51. 0xc0 | 0x22, /* 2 skip */
  52. 0x00 | 0x24, /* direct */
  53. 0x80 | 0x25, /* 2 next */
  54. 0xff,
  55. 0x00 | 0x27, /* direct */
  56. 0xff,
  57. 0xc0 | 0x28, /* 2 skip */
  58. 0x00 | 0x2a, /* direct */
  59. 0xff,
  60. 0xff,
  61. 0x00 | 0x2b, /* direct */
  62. 0xff,
  63. 0xff,
  64. 0xff,
  65. };
  66. typedef struct char39_s {
  67. unsigned char chk, rev, fwd;
  68. } char39_t;
  69. static const char39_t code39_encodings[NUM_CHARS] = {
  70. { 0x07, 0x1a, 0x20 }, /* 00 */
  71. { 0x0d, 0x10, 0x03 }, /* 01 */
  72. { 0x13, 0x17, 0x22 }, /* 02 */
  73. { 0x16, 0x1d, 0x23 }, /* 03 */
  74. { 0x19, 0x0d, 0x05 }, /* 04 */
  75. { 0x1c, 0x13, 0x06 }, /* 05 */
  76. { 0x25, 0x07, 0x0c }, /* 06 */
  77. { 0x2a, 0x2a, 0x27 }, /* 07 */
  78. { 0x31, 0x04, 0x0e }, /* 08 */
  79. { 0x34, 0x00, 0x0f }, /* 09 */
  80. { 0x43, 0x15, 0x25 }, /* 0a */
  81. { 0x46, 0x1c, 0x26 }, /* 0b */
  82. { 0x49, 0x0b, 0x08 }, /* 0c */
  83. { 0x4c, 0x12, 0x09 }, /* 0d */
  84. { 0x52, 0x19, 0x2b }, /* 0e */
  85. { 0x58, 0x0f, 0x00 }, /* 0f */
  86. { 0x61, 0x02, 0x11 }, /* 10 */
  87. { 0x64, 0x09, 0x12 }, /* 11 */
  88. { 0x70, 0x06, 0x13 }, /* 12 */
  89. { 0x85, 0x24, 0x16 }, /* 13 */
  90. { 0x8a, 0x29, 0x28 }, /* 14 */
  91. { 0x91, 0x21, 0x18 }, /* 15 */
  92. { 0x94, 0x2b, 0x19 }, /* 16 */
  93. { 0xa2, 0x28, 0x29 }, /* 17 */
  94. { 0xa8, 0x27, 0x2a }, /* 18 */
  95. { 0xc1, 0x1f, 0x1b }, /* 19 */
  96. { 0xc4, 0x26, 0x1c }, /* 1a */
  97. { 0xd0, 0x23, 0x1d }, /* 1b */
  98. { 0x03, 0x14, 0x1e }, /* 1c */
  99. { 0x06, 0x1b, 0x1f }, /* 1d */
  100. { 0x09, 0x0a, 0x01 }, /* 1e */
  101. { 0x0c, 0x11, 0x02 }, /* 1f */
  102. { 0x12, 0x18, 0x21 }, /* 20 */
  103. { 0x18, 0x0e, 0x04 }, /* 21 */
  104. { 0x21, 0x01, 0x0a }, /* 22 */
  105. { 0x24, 0x08, 0x0b }, /* 23 */
  106. { 0x30, 0x05, 0x0d }, /* 24 */
  107. { 0x42, 0x16, 0x24 }, /* 25 */
  108. { 0x48, 0x0c, 0x07 }, /* 26 */
  109. { 0x60, 0x03, 0x10 }, /* 27 */
  110. { 0x81, 0x1e, 0x14 }, /* 28 */
  111. { 0x84, 0x25, 0x15 }, /* 29 */
  112. { 0x90, 0x22, 0x17 }, /* 2a */
  113. { 0xc0, 0x20, 0x1a }, /* 2b */
  114. };
  115. static const unsigned char code39_characters[NUM_CHARS] =
  116. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*";
  117. static inline unsigned char code39_decode1 (unsigned char enc,
  118. unsigned e,
  119. unsigned s)
  120. {
  121. unsigned char E = decode_e(e, s, 36);
  122. if(E > 7)
  123. return(0xff);
  124. enc <<= 1;
  125. if(E > 2) {
  126. enc |= 1;
  127. dprintf(2, "1");
  128. }
  129. else
  130. dprintf(2, "0");
  131. return(enc);
  132. }
  133. static inline signed char code39_decode9 (zbar_decoder_t *dcode)
  134. {
  135. code39_decoder_t *dcode39 = &dcode->code39;
  136. dprintf(2, " s=%d ", dcode39->s9);
  137. if(dcode39->s9 < 9)
  138. return(-1);
  139. /* threshold bar width ratios */
  140. unsigned char i, enc = 0;
  141. for(i = 0; i < 5; i++) {
  142. enc = code39_decode1(enc, get_width(dcode, i), dcode39->s9);
  143. if(enc == 0xff)
  144. return(-1);
  145. }
  146. zassert(enc < 0x20, -1, " enc=%x s9=%x\n", enc, dcode39->s9);
  147. /* lookup first 5 encoded widths for coarse decode */
  148. unsigned char idx = code39_hi[enc];
  149. if(idx == 0xff)
  150. return(-1);
  151. /* encode remaining widths (NB first encoded width is lost) */
  152. for(; i < 9; i++) {
  153. enc = code39_decode1(enc, get_width(dcode, i), dcode39->s9);
  154. if(enc == 0xff)
  155. return(-1);
  156. }
  157. if((idx & 0xc0) == 0x80)
  158. idx = (idx & 0x3f) + ((enc >> 3) & 1);
  159. else if((idx & 0xc0) == 0xc0)
  160. idx = (idx & 0x3f) + ((enc >> 2) & 1);
  161. else if(idx & 0xc0)
  162. idx = (idx & 0x3f) + ((enc >> 2) & 3);
  163. zassert(idx < 0x2c, -1, " idx=%x enc=%x s9=%x\n", idx, enc, dcode39->s9);
  164. const char39_t *c = &code39_encodings[idx];
  165. dprintf(2, " i=%02x chk=%02x c=%02x/%02x", idx, c->chk, c->fwd, c->rev);
  166. if(enc != c->chk)
  167. return(-1);
  168. dcode39->width = dcode39->s9;
  169. return((dcode39->direction) ? c->rev : c->fwd);
  170. }
  171. static inline signed char code39_decode_start (zbar_decoder_t *dcode)
  172. {
  173. code39_decoder_t *dcode39 = &dcode->code39;
  174. signed char c = code39_decode9(dcode);
  175. if(c == 0x19)
  176. dcode39->direction ^= 1;
  177. else if(c != 0x2b) {
  178. dprintf(2, "\n");
  179. return(ZBAR_NONE);
  180. }
  181. /* check leading quiet zone - spec is 10x */
  182. unsigned quiet = get_width(dcode, 9);
  183. if(quiet && quiet < dcode39->s9 / 2) {
  184. dprintf(2, " [invalid quiet]\n");
  185. return(ZBAR_NONE);
  186. }
  187. dcode39->element = 9;
  188. dcode39->character = 0;
  189. dprintf(1, " dir=%x [valid start]\n", dcode39->direction);
  190. return(ZBAR_PARTIAL);
  191. }
  192. static inline void code39_postprocess (zbar_decoder_t *dcode)
  193. {
  194. code39_decoder_t *dcode39 = &dcode->code39;
  195. int i;
  196. if(dcode39->direction) {
  197. /* reverse buffer */
  198. dprintf(2, " (rev)");
  199. for(i = 0; i < dcode39->character / 2; i++) {
  200. unsigned j = dcode39->character - 1 - i;
  201. char code = dcode->buf[i];
  202. dcode->buf[i] = dcode->buf[j];
  203. dcode->buf[j] = code;
  204. }
  205. }
  206. for(i = 0; i < dcode39->character; i++)
  207. dcode->buf[i] = ((dcode->buf[i] < 0x2b)
  208. ? code39_characters[(unsigned)dcode->buf[i]]
  209. : '?');
  210. dcode->buflen = i;
  211. dcode->buf[i] = '\0';
  212. }
  213. zbar_symbol_type_t _zbar_decode_code39 (zbar_decoder_t *dcode)
  214. {
  215. code39_decoder_t *dcode39 = &dcode->code39;
  216. /* update latest character width */
  217. dcode39->s9 -= get_width(dcode, 9);
  218. dcode39->s9 += get_width(dcode, 0);
  219. if(dcode39->character < 0) {
  220. if(get_color(dcode) != ZBAR_BAR)
  221. return(ZBAR_NONE);
  222. dprintf(2, " code39:");
  223. return(code39_decode_start(dcode));
  224. }
  225. if(++dcode39->element < 9)
  226. return(ZBAR_NONE);
  227. dprintf(2, " code39[%c%02d+%x]",
  228. (dcode39->direction) ? '<' : '>',
  229. dcode39->character, dcode39->element);
  230. if(dcode39->element == 10) {
  231. unsigned space = get_width(dcode, 0);
  232. if(dcode39->character &&
  233. dcode->buf[dcode39->character - 1] == 0x2b) { /* STOP */
  234. /* trim STOP character */
  235. dcode39->character--;
  236. zbar_symbol_type_t sym = ZBAR_CODE39;
  237. /* trailing quiet zone check */
  238. if(space && space < dcode39->width / 2) {
  239. dprintf(2, " [invalid qz]\n");
  240. sym = ZBAR_NONE;
  241. }
  242. else if(dcode39->character < CFG(*dcode39, ZBAR_CFG_MIN_LEN) ||
  243. (CFG(*dcode39, ZBAR_CFG_MAX_LEN) > 0 &&
  244. dcode39->character > CFG(*dcode39, ZBAR_CFG_MAX_LEN))) {
  245. dprintf(2, " [invalid len]\n");
  246. sym = ZBAR_NONE;
  247. }
  248. else {
  249. /* FIXME checksum (needs config enable) */
  250. code39_postprocess(dcode);
  251. dprintf(2, " [valid end]\n");
  252. }
  253. dcode39->character = -1;
  254. if(!sym)
  255. dcode->lock = 0;
  256. return(sym);
  257. }
  258. if(space > dcode39->width / 2) {
  259. /* inter-character space check failure */
  260. dcode->lock = 0;
  261. dcode39->character = -1;
  262. dprintf(2, " ics>%d [invalid ics]", dcode39->width);
  263. }
  264. dcode39->element = 0;
  265. dprintf(2, "\n");
  266. return(ZBAR_NONE);
  267. }
  268. signed char c = code39_decode9(dcode);
  269. dprintf(2, " c=%d", c);
  270. /* lock shared resources */
  271. if(!dcode39->character && get_lock(dcode, ZBAR_CODE39)) {
  272. dcode39->character = -1;
  273. dprintf(1, " [locked %d]\n", dcode->lock);
  274. return(ZBAR_PARTIAL);
  275. }
  276. if(c < 0 ||
  277. ((dcode39->character >= BUFFER_MIN) &&
  278. size_buf(dcode, dcode39->character + 1))) {
  279. dprintf(1, (c < 0) ? " [aborted]\n" : " [overflow]\n");
  280. dcode->lock = 0;
  281. dcode39->character = -1;
  282. return(ZBAR_NONE);
  283. }
  284. else {
  285. zassert(c < 0x2c, ZBAR_NONE, "c=%02x s9=%x\n", c, dcode39->s9);
  286. dprintf(2, "\n");
  287. }
  288. dcode->buf[dcode39->character++] = c;
  289. return(ZBAR_NONE);
  290. }