code49.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* code49.c - Handles Code 49 */
  2. /*
  3. libzint - the open source barcode library
  4. Copyright (C) 2009-2024 Robin Stuart <rstuart114@gmail.com>
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. 3. Neither the name of the project nor the names of its contributors
  14. may be used to endorse or promote products derived from this software
  15. without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  20. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. SUCH DAMAGE.
  27. */
  28. /* SPDX-License-Identifier: BSD-3-Clause */
  29. #include <stdio.h>
  30. #include "common.h"
  31. #include "code49.h"
  32. static const char C49_INSET[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%!&*";
  33. /* "!" represents Shift 1 and "&" represents Shift 2, "*" represents FNC1 */
  34. INTERNAL int code49(struct zint_symbol *symbol, unsigned char source[], int length) {
  35. int i, j, rows, M, x_count, y_count, z_count, posn_val, local_value;
  36. char intermediate[170] = "";
  37. char *d = intermediate;
  38. int codewords[170], codeword_count;
  39. int c_grid[8][8]; /* Refers to table 3 */
  40. int w_grid[8][4]; /* Refets to table 2 */
  41. int pad_count = 0;
  42. char pattern[80];
  43. int bp = 0; /* Initialize to suppress gcc -Wmaybe-uninitialized warning */
  44. int gs1;
  45. int h;
  46. int error_number = 0;
  47. if (length > 81) {
  48. return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 430, "Input length %d too long (maximum 81)", length);
  49. }
  50. if ((symbol->input_mode & 0x07) == GS1_MODE) {
  51. gs1 = 1;
  52. *d++ = '*'; /* FNC1 */
  53. } else {
  54. gs1 = 0;
  55. }
  56. for (i = 0; i < length; i++) {
  57. if (source[i] > 127) {
  58. return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 431,
  59. "Invalid character at position %d in input, extended ASCII not allowed", i + 1);
  60. }
  61. if (gs1 && source[i] == '\x1D') {
  62. *d++ = '*'; /* FNC1 */
  63. } else {
  64. const char *const entry = c49_table7[source[i]];
  65. memcpy(d, entry, 2);
  66. d += entry[1] ? 2 : 1;
  67. }
  68. }
  69. codeword_count = 0;
  70. i = 0;
  71. h = d - intermediate;
  72. do {
  73. if (z_isdigit(intermediate[i])) {
  74. /* Numeric data */
  75. for (j = 0; z_isdigit(intermediate[i + j]); j++);
  76. if (j >= 5) {
  77. /* Use Numeric Encodation Method */
  78. int block_count, c;
  79. int block_remain;
  80. int block_value;
  81. codewords[codeword_count] = 48; /* Numeric Shift */
  82. codeword_count++;
  83. block_count = j / 5;
  84. block_remain = j % 5;
  85. for (c = 0; c < block_count; c++) {
  86. if ((c == block_count - 1) && (block_remain == 2)) {
  87. /* Rule (d) */
  88. block_value = 100000;
  89. block_value += ctoi(intermediate[i]) * 1000;
  90. block_value += ctoi(intermediate[i + 1]) * 100;
  91. block_value += ctoi(intermediate[i + 2]) * 10;
  92. block_value += ctoi(intermediate[i + 3]);
  93. codewords[codeword_count] = block_value / (48 * 48);
  94. block_value = block_value - (48 * 48) * codewords[codeword_count];
  95. codeword_count++;
  96. codewords[codeword_count] = block_value / 48;
  97. block_value = block_value - 48 * codewords[codeword_count];
  98. codeword_count++;
  99. codewords[codeword_count] = block_value;
  100. codeword_count++;
  101. i += 4;
  102. block_value = ctoi(intermediate[i]) * 100;
  103. block_value += ctoi(intermediate[i + 1]) * 10;
  104. block_value += ctoi(intermediate[i + 2]);
  105. codewords[codeword_count] = block_value / 48;
  106. block_value = block_value - 48 * codewords[codeword_count];
  107. codeword_count++;
  108. codewords[codeword_count] = block_value;
  109. codeword_count++;
  110. i += 3;
  111. } else {
  112. block_value = ctoi(intermediate[i]) * 10000;
  113. block_value += ctoi(intermediate[i + 1]) * 1000;
  114. block_value += ctoi(intermediate[i + 2]) * 100;
  115. block_value += ctoi(intermediate[i + 3]) * 10;
  116. block_value += ctoi(intermediate[i + 4]);
  117. codewords[codeword_count] = block_value / (48 * 48);
  118. block_value = block_value - (48 * 48) * codewords[codeword_count];
  119. codeword_count++;
  120. codewords[codeword_count] = block_value / 48;
  121. block_value = block_value - 48 * codewords[codeword_count];
  122. codeword_count++;
  123. codewords[codeword_count] = block_value;
  124. codeword_count++;
  125. i += 5;
  126. }
  127. }
  128. switch (block_remain) {
  129. case 1:
  130. /* Rule (a) */
  131. codewords[codeword_count] = posn(C49_INSET, intermediate[i]);
  132. codeword_count++;
  133. i++;
  134. break;
  135. case 3:
  136. /* Rule (b) */
  137. block_value = ctoi(intermediate[i]) * 100;
  138. block_value += ctoi(intermediate[i + 1]) * 10;
  139. block_value += ctoi(intermediate[i + 2]);
  140. codewords[codeword_count] = block_value / 48;
  141. block_value = block_value - 48 * codewords[codeword_count];
  142. codeword_count++;
  143. codewords[codeword_count] = block_value;
  144. codeword_count++;
  145. i += 3;
  146. break;
  147. case 4:
  148. /* Rule (c) */
  149. block_value = 100000;
  150. block_value += ctoi(intermediate[i]) * 1000;
  151. block_value += ctoi(intermediate[i + 1]) * 100;
  152. block_value += ctoi(intermediate[i + 2]) * 10;
  153. block_value += ctoi(intermediate[i + 3]);
  154. codewords[codeword_count] = block_value / (48 * 48);
  155. block_value = block_value - (48 * 48) * codewords[codeword_count];
  156. codeword_count++;
  157. codewords[codeword_count] = block_value / 48;
  158. block_value = block_value - 48 * codewords[codeword_count];
  159. codeword_count++;
  160. codewords[codeword_count] = block_value;
  161. codeword_count++;
  162. i += 4;
  163. break;
  164. }
  165. if (i < h) {
  166. /* There is more to add */
  167. codewords[codeword_count] = 48; /* Numeric Shift */
  168. codeword_count++;
  169. }
  170. } else {
  171. codewords[codeword_count] = posn(C49_INSET, intermediate[i]);
  172. codeword_count++;
  173. i++;
  174. }
  175. } else {
  176. codewords[codeword_count] = posn(C49_INSET, intermediate[i]);
  177. codeword_count++;
  178. i++;
  179. }
  180. } while (i < h);
  181. switch (codewords[0]) {
  182. /* Set starting mode value */
  183. case 48: M = 2;
  184. break;
  185. case 43: M = 4;
  186. break;
  187. case 44: M = 5;
  188. break;
  189. default: M = 0;
  190. break;
  191. }
  192. if (M != 0) {
  193. codeword_count--;
  194. for (i = 0; i < codeword_count; i++) {
  195. codewords[i] = codewords[i + 1];
  196. }
  197. }
  198. if (codeword_count > 49) {
  199. return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 432, "Input too long, requires %d codewords (maximum 49)",
  200. codeword_count);
  201. }
  202. /* Place codewords in code character array (c grid) */
  203. rows = 0;
  204. do {
  205. for (i = 0; i < 7; i++) {
  206. if (((rows * 7) + i) < codeword_count) {
  207. c_grid[rows][i] = codewords[(rows * 7) + i];
  208. } else {
  209. c_grid[rows][i] = 48; /* Pad */
  210. pad_count++;
  211. }
  212. }
  213. rows++;
  214. } while ((rows * 7) < codeword_count);
  215. if ((((rows <= 6) && (pad_count < 5))) || (rows > 6) || (rows == 1)) {
  216. /* Add a row */
  217. for (i = 0; i < 7; i++) {
  218. c_grid[rows][i] = 48; /* Pad */
  219. }
  220. rows++;
  221. }
  222. if (symbol->option_1 >= 2 && symbol->option_1 <= 8) { /* Minimum no. of rows */
  223. if (symbol->option_1 > rows) {
  224. for (j = symbol->option_1 - rows; j > 0; j--) {
  225. for (i = 0; i < 7; i++) {
  226. c_grid[rows][i] = 48; /* Pad */
  227. }
  228. rows++;
  229. }
  230. }
  231. } else if (symbol->option_1 >= 1) {
  232. strcpy(symbol->errtxt, "433: Minimum number of rows out of range (2 to 8)");
  233. return ZINT_ERROR_INVALID_OPTION;
  234. }
  235. /* Add row count and mode character */
  236. c_grid[rows - 1][6] = (7 * (rows - 2)) + M;
  237. /* Add row check character */
  238. for (i = 0; i < rows - 1; i++) {
  239. int row_sum = 0;
  240. for (j = 0; j < 7; j++) {
  241. row_sum += c_grid[i][j];
  242. }
  243. c_grid[i][7] = row_sum % 49;
  244. }
  245. /* Calculate Symbol Check Characters */
  246. posn_val = 0;
  247. x_count = c_grid[rows - 1][6] * 20;
  248. y_count = c_grid[rows - 1][6] * 16;
  249. z_count = c_grid[rows - 1][6] * 38;
  250. for (i = 0; i < rows - 1; i++) {
  251. for (j = 0; j < 4; j++) {
  252. local_value = (c_grid[i][2 * j] * 49) + c_grid[i][(2 * j) + 1];
  253. x_count += c49_x_weight[posn_val] * local_value;
  254. y_count += c49_y_weight[posn_val] * local_value;
  255. z_count += c49_z_weight[posn_val] * local_value;
  256. posn_val++;
  257. }
  258. }
  259. if (rows > 6) {
  260. /* Add Z Symbol Check */
  261. c_grid[rows - 1][0] = (z_count % 2401) / 49;
  262. c_grid[rows - 1][1] = (z_count % 2401) % 49;
  263. }
  264. local_value = (c_grid[rows - 1][0] * 49) + c_grid[rows - 1][1];
  265. x_count += c49_x_weight[posn_val] * local_value;
  266. y_count += c49_y_weight[posn_val] * local_value;
  267. posn_val++;
  268. /* Add Y Symbol Check */
  269. c_grid[rows - 1][2] = (y_count % 2401) / 49;
  270. c_grid[rows - 1][3] = (y_count % 2401) % 49;
  271. local_value = (c_grid[rows - 1][2] * 49) + c_grid[rows - 1][3];
  272. x_count += c49_x_weight[posn_val] * local_value;
  273. /* Add X Symbol Check */
  274. c_grid[rows - 1][4] = (x_count % 2401) / 49;
  275. c_grid[rows - 1][5] = (x_count % 2401) % 49;
  276. /* Add last row check character */
  277. j = 0;
  278. for (i = 0; i < 7; i++) {
  279. j += c_grid[rows - 1][i];
  280. }
  281. c_grid[rows - 1][7] = j % 49;
  282. if (symbol->debug & ZINT_DEBUG_PRINT) {
  283. fputs("Codewords:\n", stdout);
  284. for (i = 0; i < rows; i++) {
  285. for (j = 0; j < 8; j++) {
  286. printf(" %2d", c_grid[i][j]);
  287. }
  288. fputc('\n', stdout);
  289. }
  290. }
  291. #ifdef ZINT_TEST
  292. if (symbol->debug & ZINT_DEBUG_TEST) {
  293. debug_test_codeword_dump_int(symbol, (int *)c_grid, rows * 8);
  294. }
  295. #endif
  296. /* Transfer data to symbol character array (w grid) */
  297. for (i = 0; i < rows; i++) {
  298. for (j = 0; j < 4; j++) {
  299. w_grid[i][j] = (c_grid[i][2 * j] * 49) + c_grid[i][(2 * j) + 1];
  300. }
  301. }
  302. for (i = 0; i < rows; i++) {
  303. bp = 0;
  304. bp = bin_append_posn(2, 2, pattern, bp); /* Start character "10" */
  305. for (j = 0; j < 4; j++) {
  306. if (i != (rows - 1)) {
  307. if (c49_table4[i][j] == 'E') {
  308. /* Even Parity */
  309. bp = bin_append_posn(c49_even_bitpattern[w_grid[i][j]], 16, pattern, bp);
  310. } else {
  311. /* Odd Parity */
  312. bp = bin_append_posn(c49_odd_bitpattern[w_grid[i][j]], 16, pattern, bp);
  313. }
  314. } else {
  315. /* Last row uses all even parity */
  316. bp = bin_append_posn(c49_even_bitpattern[w_grid[i][j]], 16, pattern, bp);
  317. }
  318. }
  319. bp = bin_append_posn(15, 4, pattern, bp); /* Stop character "1111" */
  320. /* Expand into symbol */
  321. for (j = 0; j < bp; j++) {
  322. if (pattern[j] == '1') {
  323. set_module(symbol, i, j);
  324. }
  325. }
  326. }
  327. symbol->rows = rows;
  328. symbol->width = bp;
  329. if (symbol->output_options & COMPLIANT_HEIGHT) {
  330. /* ANSI/AIM BC6-2000 Section 2.6 minimum 8X; use 10X as default
  331. Formula 2 H = ((h + g)r + g)X = rows * row_height + (rows - 1) * separator as borders not included
  332. in symbol->height (added on) */
  333. const int separator = symbol->option_3 >= 1 && symbol->option_3 <= 4 ? symbol->option_3 : 1;
  334. const float min_row_height = stripf((8.0f * rows + separator * (rows - 1)) / rows);
  335. const float default_height = 10.0f * rows + separator * (rows - 1);
  336. error_number = set_height(symbol, min_row_height, default_height, 0.0f, 0 /*no_errtxt*/);
  337. } else {
  338. (void) set_height(symbol, 0.0f, 10.0f * rows, 0.0f, 1 /*no_errtxt*/);
  339. }
  340. symbol->output_options |= BARCODE_BIND;
  341. if (symbol->border_width == 0) { /* Allow override if non-zero */
  342. symbol->border_width = 1; /* ANSI/AIM BC6-2000 Section 2.1 (note change from previous default 2) */
  343. }
  344. return error_number;
  345. }
  346. /* vim: set ts=4 sw=4 et : */