medical.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* medical.c - Handles 1 track and 2 track pharmacode and Codabar */
  2. /*
  3. libzint - the open source barcode library
  4. Copyright (C) 2008-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. INTERNAL int code39(struct zint_symbol *symbol, unsigned char source[], int length);
  32. static const char CALCIUM[] = "0123456789-$:/.+ABCD";
  33. #define CALCIUM_INNER_F (IS_NUM_F | IS_MNS_F | IS_CLI_F | IS_PLS_F) /* CALCIUM_INNER "0123456789-$:/.+" */
  34. /* Codabar table checked against EN 798:1995 */
  35. static const char CodaTable[20][8] = {
  36. {'1','1','1','1','1','2','2','1'}, {'1','1','1','1','2','2','1','1'}, {'1','1','1','2','1','1','2','1'},
  37. {'2','2','1','1','1','1','1','1'}, {'1','1','2','1','1','2','1','1'}, {'2','1','1','1','1','2','1','1'},
  38. {'1','2','1','1','1','1','2','1'}, {'1','2','1','1','2','1','1','1'}, {'1','2','2','1','1','1','1','1'},
  39. {'2','1','1','2','1','1','1','1'}, {'1','1','1','2','2','1','1','1'}, {'1','1','2','2','1','1','1','1'},
  40. {'2','1','1','1','2','1','2','1'}, {'2','1','2','1','1','1','2','1'}, {'2','1','2','1','2','1','1','1'},
  41. {'1','1','2','1','2','1','2','1'}, {'1','1','2','2','1','2','1','1'}, {'1','2','1','2','1','1','2','1'},
  42. {'1','1','1','2','1','2','2','1'}, {'1','1','1','2','2','2','1','1'}
  43. };
  44. INTERNAL int pharma(struct zint_symbol *symbol, unsigned char source[], int length) {
  45. /* "Pharmacode can represent only a single integer from 3 to 131070. Unlike other
  46. commonly used one-dimensional barcode schemes, pharmacode does not store the data in a
  47. form corresponding to the human-readable digits; the number is encoded in binary, rather
  48. than decimal. Pharmacode is read from right to left: with n as the bar position starting
  49. at 0 on the right, each narrow bar adds 2^n to the value and each wide bar adds 2(2^n).
  50. The minimum barcode is 2 bars and the maximum 16, so the smallest number that could
  51. be encoded is 3 (2 narrow bars) and the biggest is 131070 (16 wide bars)."
  52. - http://en.wikipedia.org/wiki/Pharmacode */
  53. /* This code uses the One Track Pharamacode calculating algorithm as recommended by
  54. the specification at http://www.laetus.com/laetus.php?request=file&id=69
  55. (http://www.gomaro.ch/ftproot/Laetus_PHARMA-CODE.pdf) */
  56. int i;
  57. int tester;
  58. int counter, error_number = 0, h;
  59. char inter[18] = {0}; /* 131070 -> 17 bits */
  60. char *in = inter;
  61. char dest[64]; /* 17 * 2 + 1 */
  62. char *d = dest;
  63. if (length > 6) {
  64. return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 350, "Input length %d too long (maximum 6)", length);
  65. }
  66. if ((i = not_sane(NEON_F, source, length))) {
  67. return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 351,
  68. "Invalid character at position %d in input (digits only)", i);
  69. }
  70. tester = to_int(source, length);
  71. if ((tester < 3) || (tester > 131070)) {
  72. return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 352, "Input value '%d' out of range (3 to 131070)", tester);
  73. }
  74. do {
  75. if (!(tester & 1)) {
  76. *in++ = 'W';
  77. tester = (tester - 2) / 2;
  78. } else {
  79. *in++ = 'N';
  80. tester = (tester - 1) / 2;
  81. }
  82. } while (tester != 0);
  83. h = in - inter;
  84. for (counter = h - 1; counter >= 0; counter--) {
  85. *d++ = inter[counter] == 'W' ? '3' : '1';
  86. *d++ = '2';
  87. }
  88. *--d = '\0'; /* Chop off final bar */
  89. expand(symbol, dest, d - dest);
  90. if (symbol->output_options & COMPLIANT_HEIGHT) {
  91. /* Laetus Pharmacode Guide 1.2 Standard one-track height 8mm / 0.5mm (X) */
  92. error_number = set_height(symbol, 16.0f, 0.0f, 0.0f, 0 /*no_errtxt*/);
  93. } else {
  94. (void) set_height(symbol, 0.0f, 50.0f, 0.0f, 1 /*no_errtxt*/);
  95. }
  96. return error_number;
  97. }
  98. static int pharma_two_calc(int tester, char *d) {
  99. /* This code uses the Two Track Pharamacode defined in the document at
  100. http://www.laetus.com/laetus.php?request=file&id=69 and using a modified
  101. algorithm from the One Track system. This standard accepts integet values
  102. from 4 to 64570080. */
  103. int counter, h;
  104. char inter[17];
  105. char *in = inter;
  106. do {
  107. switch (tester % 3) {
  108. case 0:
  109. *in++ = '3';
  110. tester = (tester - 3) / 3;
  111. break;
  112. case 1:
  113. *in++ = '1';
  114. tester = (tester - 1) / 3;
  115. break;
  116. case 2:
  117. *in++ = '2';
  118. tester = (tester - 2) / 3;
  119. break;
  120. }
  121. } while (tester != 0);
  122. h = in - inter;
  123. for (counter = h - 1; counter >= 0; counter--) {
  124. *d++ = inter[counter];
  125. }
  126. *d = '\0';
  127. return h;
  128. }
  129. INTERNAL int pharma_two(struct zint_symbol *symbol, unsigned char source[], int length) {
  130. /* Draws the patterns for two track pharmacode */
  131. int i;
  132. int tester;
  133. char height_pattern[200];
  134. unsigned int loopey, h;
  135. int writer;
  136. int error_number = 0;
  137. if (length > 8) {
  138. return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 354, "Input length %d too long (maximum 8)", length);
  139. }
  140. if ((i = not_sane(NEON_F, source, length))) {
  141. return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 355,
  142. "Invalid character at position %d in input (digits only)", i);
  143. }
  144. tester = to_int(source, length);
  145. if ((tester < 4) || (tester > 64570080)) {
  146. return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 353, "Input value '%d' out of range (4 to 64570080)", tester);
  147. }
  148. h = pharma_two_calc(tester, height_pattern);
  149. writer = 0;
  150. for (loopey = 0; loopey < h; loopey++) {
  151. if ((height_pattern[loopey] == '2') || (height_pattern[loopey] == '3')) {
  152. set_module(symbol, 0, writer);
  153. }
  154. if ((height_pattern[loopey] == '1') || (height_pattern[loopey] == '3')) {
  155. set_module(symbol, 1, writer);
  156. }
  157. writer += 2;
  158. }
  159. symbol->rows = 2;
  160. symbol->width = writer - 1;
  161. if (symbol->output_options & COMPLIANT_HEIGHT) {
  162. /* Laetus Pharmacode Guide 1.4
  163. Two-track height min 8mm / 2mm (X max) = 4X (2X per row), standard 8mm / 1mm = 8X,
  164. max 12mm / 0.8mm (X min) = 15X */
  165. error_number = set_height(symbol, 2.0f, 8.0f, 15.0f, 0 /*no_errtxt*/);
  166. } else {
  167. (void) set_height(symbol, 0.0f, 10.0f, 0.0f, 1 /*no_errtxt*/);
  168. }
  169. return error_number;
  170. }
  171. /* The Codabar system consisting of simple substitution */
  172. INTERNAL int codabar(struct zint_symbol *symbol, unsigned char source[], int length) {
  173. int i, error_number = 0;
  174. int posns[103];
  175. char dest[833]; /* (103 + 1) * 8 + 1 == 833 */
  176. char *d = dest;
  177. int add_checksum, count = 0, checksum = 0;
  178. int d_chars = 0;
  179. if (length > 103) { /* No stack smashing please (103 + 1) * 11 = 1144 */
  180. return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 356, "Input length %d too long (maximum 103)", length);
  181. }
  182. /* BS EN 798:1995 4.2 "'Codabar' symbols shall consist of ... b) start character;
  183. c) one or more symbol characters representing data ... d) stop character ..." */
  184. if (length < 3) {
  185. return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 362, "Input length %d too short (minimum 3)", length);
  186. }
  187. to_upper(source, length);
  188. /* Codabar must begin and end with the characters A, B, C or D */
  189. if ((source[0] != 'A') && (source[0] != 'B') && (source[0] != 'C')
  190. && (source[0] != 'D')) {
  191. return errtxt(ZINT_ERROR_INVALID_DATA, symbol, 358, "Does not begin with \"A\", \"B\", \"C\" or \"D\"");
  192. }
  193. if ((source[length - 1] != 'A') && (source[length - 1] != 'B') &&
  194. (source[length - 1] != 'C') && (source[length - 1] != 'D')) {
  195. return errtxt(ZINT_ERROR_INVALID_DATA, symbol, 359, "Does not end with \"A\", \"B\", \"C\" or \"D\"");
  196. }
  197. if ((i = not_sane_lookup(CALCIUM, sizeof(CALCIUM) - 1, source, length, posns))) {
  198. return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 357,
  199. "Invalid character at position %1$d in input (\"%2$s\" only)", i, CALCIUM);
  200. }
  201. /* And must not use A, B, C or D otherwise (BS EN 798:1995 4.3.2) */
  202. if ((i = not_sane(CALCIUM_INNER_F, source + 1, length - 2))) {
  203. return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 363,
  204. "Invalid character at position %d in input (cannot contain \"A\", \"B\", \"C\" or \"D\")", i);
  205. }
  206. /* Add check character: 1 don't show to HRT, 2 do show to HRT
  207. (unfortunately to maintain back-compatibility, this is reverse of C25) */
  208. add_checksum = symbol->option_2 == 1 || symbol->option_2 == 2;
  209. for (i = 0; i < length; i++, d += 8) {
  210. if (add_checksum) {
  211. /* BS EN 798:1995 A.3 suggests using ISO 7064 algorithm but leaves it application defined.
  212. Following BWIPP and TEC-IT, use this simple mod-16 algorithm (not in ISO 7064) */
  213. count += posns[i];
  214. if (i + 1 == length) {
  215. checksum = count % 16;
  216. if (checksum) {
  217. checksum = 16 - checksum;
  218. }
  219. if (symbol->debug & ZINT_DEBUG_PRINT) {
  220. printf("Codabar: %s, count %d, checksum %d (%c)\n", source, count, checksum, CALCIUM[checksum]);
  221. }
  222. memcpy(d, CodaTable[checksum], 8);
  223. d += 8;
  224. }
  225. }
  226. memcpy(d, CodaTable[posns[i]], 8);
  227. if (source[i] == '/' || source[i] == ':' || source[i] == '.' || source[i] == '+') { /* Wide data characters */
  228. d_chars++;
  229. }
  230. }
  231. expand(symbol, dest, d - dest);
  232. if (symbol->output_options & COMPLIANT_HEIGHT) {
  233. /* BS EN 798:1995 4.4.1 (d) max of 5mm / 0.43mm (X max) ~ 11.628 or 15% of width where (taking N =
  234. narrow/wide ratio as 2 and I = X) width = ((2 * N + 5) * C + (N – 1) * (D + 2)) * X + I * (C – 1) + 2Q
  235. = ((4 + 5) * C + (D + 2) + C - 1 + 2 * 10) * X = (10 * C + D + 21) * X
  236. Length (C) includes start/stop chars */
  237. const float min_height_min = 11.6279068f; /* 5.0 / 0.43 */
  238. float min_height = stripf((10.0f * ((add_checksum ? length + 1 : length) + 2.0f) + d_chars + 21.0f) * 0.15f);
  239. if (min_height < min_height_min) {
  240. min_height = min_height_min;
  241. }
  242. /* Using 50 as default as none recommended */
  243. error_number = set_height(symbol, min_height, min_height > 50.0f ? min_height : 50.0f, 0.0f, 0 /*no_errtxt*/);
  244. } else {
  245. (void) set_height(symbol, 0.0f, 50.0f, 0.0f, 1 /*no_errtxt*/);
  246. }
  247. ustrcpy(symbol->text, source);
  248. if (symbol->option_2 == 2) {
  249. symbol->text[length - 1] = CALCIUM[checksum]; /* Place before final A/B/C/D character (BS EN 798:1995 A.3) */
  250. symbol->text[length] = source[length - 1];
  251. symbol->text[length + 1] = '\0';
  252. }
  253. return error_number;
  254. }
  255. /* Italian Pharmacode */
  256. INTERNAL int code32(struct zint_symbol *symbol, unsigned char source[], int length) {
  257. static const char TABELLA[] = "0123456789BCDFGHJKLMNPQRSTUVWXYZ";
  258. int i, zeroes, error_number = 0, checksum, checkpart, checkdigit;
  259. char localstr[10], risultante[7];
  260. unsigned int pharmacode, devisor;
  261. int codeword[6];
  262. /* Validate the input */
  263. if (length > 8) {
  264. return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 360, "Input length %d too long (maximum 8)", length);
  265. }
  266. if ((i = not_sane(NEON_F, source, length))) {
  267. return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 361,
  268. "Invalid character at position %d in input (digits only)", i);
  269. }
  270. /* Add leading zeros as required */
  271. zeroes = 8 - length;
  272. memset(localstr, '0', zeroes);
  273. ustrcpy(localstr + zeroes, source);
  274. /* Calculate the check digit */
  275. checksum = 0;
  276. for (i = 0; i < 4; i++) {
  277. checkpart = ctoi(localstr[i * 2]);
  278. checksum += checkpart;
  279. checkpart = 2 * (ctoi(localstr[(i * 2) + 1]));
  280. if (checkpart >= 10) {
  281. checksum += (checkpart - 10) + 1;
  282. } else {
  283. checksum += checkpart;
  284. }
  285. }
  286. /* Add check digit to data string */
  287. checkdigit = checksum % 10;
  288. localstr[8] = itoc(checkdigit);
  289. localstr[9] = '\0';
  290. /* Convert string into an integer value */
  291. pharmacode = atoi(localstr);
  292. /* Convert from decimal to base-32 */
  293. devisor = 33554432;
  294. for (i = 5; i >= 0; i--) {
  295. unsigned int remainder;
  296. codeword[i] = pharmacode / devisor;
  297. remainder = pharmacode % devisor;
  298. pharmacode = remainder;
  299. devisor /= 32;
  300. }
  301. /* Look up values in 'Tabella di conversione' */
  302. for (i = 5; i >= 0; i--) {
  303. risultante[5 - i] = TABELLA[codeword[i]];
  304. }
  305. risultante[6] = '\0';
  306. /* Plot the barcode using Code 39 */
  307. error_number = code39(symbol, (unsigned char *) risultante, 6);
  308. if (error_number != 0) { /* Should never happen */
  309. return error_number; /* Not reached */
  310. }
  311. if (symbol->output_options & COMPLIANT_HEIGHT) {
  312. /* Allegato A Caratteristiche tecniche del bollino farmaceutico
  313. (https://www.gazzettaufficiale.it/do/atto/serie_generale/caricaPdf?cdimg=14A0566800100010110001
  314. &dgu=2014-07-18&art.dataPubblicazioneGazzetta=2014-07-18&art.codiceRedazionale=14A05668&art.num=1
  315. &art.tiposerie=SG)
  316. X given as 0.250mm; height (and quiet zones) left to ISO/IEC 16388:2007 (Code 39)
  317. So min height 5mm = 5mm / 0.25mm = 20 > 15% of width, i.e. (10 * 8 + 19) * 0.15 = 14.85 */
  318. error_number = set_height(symbol, 20.0f, 20.0f, 0.0f, 0 /*no_errtxt*/); /* Use as default also */
  319. } else {
  320. (void) set_height(symbol, 0.0f, 50.0f, 0.0f, 1 /*no_errtxt*/);
  321. }
  322. /* Override the normal text output with the Pharmacode number */
  323. ustrcpy(symbol->text, "A");
  324. ustrcat(symbol->text, localstr);
  325. return error_number;
  326. }
  327. /* vim: set ts=4 sw=4 et : */