plessey.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /* plessey.c - Handles Plessey and MSI Plessey */
  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. #define SSET_F (IS_NUM_F | IS_UHX_F) /* SSET "0123456789ABCDEF" */
  32. static const char PlessTable[16][8] = {
  33. {'1','3','1','3','1','3','1','3'}, {'3','1','1','3','1','3','1','3'}, {'1','3','3','1','1','3','1','3'},
  34. {'3','1','3','1','1','3','1','3'}, {'1','3','1','3','3','1','1','3'}, {'3','1','1','3','3','1','1','3'},
  35. {'1','3','3','1','3','1','1','3'}, {'3','1','3','1','3','1','1','3'}, {'1','3','1','3','1','3','3','1'},
  36. {'3','1','1','3','1','3','3','1'}, {'1','3','3','1','1','3','3','1'}, {'3','1','3','1','1','3','3','1'},
  37. {'1','3','1','3','3','1','3','1'}, {'3','1','1','3','3','1','3','1'}, {'1','3','3','1','3','1','3','1'},
  38. {'3','1','3','1','3','1','3','1'}
  39. };
  40. static const char MSITable[10][8] = {
  41. {'1','2','1','2','1','2','1','2'}, {'1','2','1','2','1','2','2','1'}, {'1','2','1','2','2','1','1','2'},
  42. {'1','2','1','2','2','1','2','1'}, {'1','2','2','1','1','2','1','2'}, {'1','2','2','1','1','2','2','1'},
  43. {'1','2','2','1','2','1','1','2'}, {'1','2','2','1','2','1','2','1'}, {'2','1','1','2','1','2','1','2'},
  44. {'2','1','1','2','1','2','2','1'}
  45. };
  46. /* Not MSI/Plessey but the older Plessey standard */
  47. INTERNAL int plessey(struct zint_symbol *symbol, unsigned char source[], int length) {
  48. int i;
  49. unsigned char checkptr[67 * 4 + 8] = {0};
  50. static const char grid[9] = {1, 1, 1, 1, 0, 1, 0, 0, 1};
  51. char dest[570]; /* 8 + 67 * 8 + 2 * 8 + 9 + 1 = 570 */
  52. char *d = dest;
  53. int error_number = 0;
  54. if (length > 67) { /* 16 + 67 * 16 + 4 * 8 + 19 = 1139 */
  55. return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 370, "Input length %d too long (maximum 67)", length);
  56. }
  57. if ((i = not_sane(SSET_F, source, length))) {
  58. return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 371,
  59. "Invalid character at position %d in input (digits and \"ABCDEF\" only)", i);
  60. }
  61. /* Start character */
  62. memcpy(d, "31311331", 8);
  63. d += 8;
  64. /* Data area */
  65. for (i = 0; i < length; i++, d += 8) {
  66. unsigned int check = source[i] - '0' - (source[i] >> 6) * 7;
  67. memcpy(d, PlessTable[check], 8);
  68. checkptr[4 * i] = check & 1;
  69. checkptr[4 * i + 1] = (check >> 1) & 1;
  70. checkptr[4 * i + 2] = (check >> 2) & 1;
  71. checkptr[4 * i + 3] = (check >> 3) & 1;
  72. }
  73. /* CRC check digit code adapted from code by Leonid A. Broukhis
  74. used in GNU Barcode */
  75. for (i = 0; i < (4 * length); i++) {
  76. if (checkptr[i]) {
  77. int j;
  78. for (j = 0; j < 9; j++)
  79. checkptr[i + j] ^= grid[j];
  80. }
  81. }
  82. for (i = 0; i < 8; i++) {
  83. switch (checkptr[length * 4 + i]) {
  84. case 0: memcpy(d, "13", 2);
  85. d += 2;
  86. break;
  87. case 1: memcpy(d, "31", 2);
  88. d += 2;
  89. break;
  90. }
  91. }
  92. /* Stop character */
  93. memcpy(d, "331311313", 9);
  94. d += 9;
  95. expand(symbol, dest, d - dest);
  96. /* TODO: Find documentation on BARCODE_PLESSEY dimensions/height */
  97. symbol->text[0] = '\0';
  98. ustrncat(symbol->text, source, length);
  99. return error_number;
  100. }
  101. /* Modulo 10 check digit - Luhn algorithm
  102. See https://en.wikipedia.org/wiki/Luhn_algorithm */
  103. static char msi_check_digit_mod10(const unsigned char source[], const int length) {
  104. static const char vals[2][10] = {
  105. { 0, 2, 4, 6, 8, 1, 3, 5, 7, 9 }, /* Doubled and digits summed */
  106. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, /* Single */
  107. };
  108. int i, x = 0, undoubled = 0;
  109. for (i = length - 1; i >= 0; i--) {
  110. /* Note overflow impossible for max length 92 * max weight 9 * max val 15 == 12420 */
  111. x += vals[undoubled][ctoi(source[i])];
  112. undoubled = !undoubled;
  113. }
  114. return itoc((10 - x % 10) % 10);
  115. }
  116. /* Modulo 11 check digit - IBM weight system wrap = 7, NCR system wrap = 9
  117. See https://en.wikipedia.org/wiki/MSI_Barcode */
  118. static char msi_check_digit_mod11(const unsigned char source[], const int length, const int wrap) {
  119. int i, x = 0, weight = 2;
  120. for (i = length - 1; i >= 0; i--) {
  121. /* Note overflow impossible for max length 92 * max weight 9 * max val 15 == 12420 */
  122. x += weight * ctoi(source[i]);
  123. weight++;
  124. if (weight > wrap) {
  125. weight = 2;
  126. }
  127. }
  128. return itoc((11 - x % 11) % 11); /* Will return ':' for 10 */
  129. }
  130. /* Plain MSI Plessey - does not calculate any check character */
  131. static char *msi_plessey_nomod(struct zint_symbol *symbol, const unsigned char source[], const int length,
  132. char *d) {
  133. int i;
  134. for (i = 0; i < length; i++, d += 8) {
  135. memcpy(d, MSITable[source[i] - '0'], 8);
  136. }
  137. symbol->text[0] = '\0';
  138. ustrncat(symbol->text, source, length);
  139. return d;
  140. }
  141. /* MSI Plessey with Modulo 10 check digit */
  142. static char *msi_plessey_mod10(struct zint_symbol *symbol, const unsigned char source[], const int length,
  143. const int no_checktext, char *d) {
  144. int i;
  145. char check_digit;
  146. /* draw data section */
  147. for (i = 0; i < length; i++, d += 8) {
  148. memcpy(d, MSITable[source[i] - '0'], 8);
  149. }
  150. /* calculate check digit */
  151. check_digit = msi_check_digit_mod10(source, length);
  152. /* draw check digit */
  153. memcpy(d, MSITable[check_digit - '0'], 8);
  154. d += 8;
  155. symbol->text[0] = '\0';
  156. ustrncat(symbol->text, source, length);
  157. if (!no_checktext) {
  158. symbol->text[length] = check_digit;
  159. symbol->text[length + 1] = '\0';
  160. }
  161. return d;
  162. }
  163. /* MSI Plessey with two Modulo 10 check digits */
  164. static char *msi_plessey_mod1010(struct zint_symbol *symbol, const unsigned char source[], const int length,
  165. const int no_checktext, char *d) {
  166. int i;
  167. unsigned char temp[92 + 2 + 1];
  168. /* Append check digits */
  169. temp[0] = '\0';
  170. ustrncat(temp, source, length);
  171. temp[length] = msi_check_digit_mod10(source, length);
  172. temp[length + 1] = msi_check_digit_mod10(temp, length + 1);
  173. temp[length + 2] = '\0';
  174. /* draw data section */
  175. for (i = 0; i < length + 2; i++, d += 8) {
  176. memcpy(d, MSITable[temp[i] - '0'], 8);
  177. }
  178. if (no_checktext) {
  179. symbol->text[0] = '\0';
  180. ustrncat(symbol->text, source, length);
  181. } else {
  182. ustrcpy(symbol->text, temp);
  183. }
  184. return d;
  185. }
  186. /* MSI Plessey with Modulo 11 check digit */
  187. static char *msi_plessey_mod11(struct zint_symbol *symbol, const unsigned char source[], const int length,
  188. const int no_checktext, const int wrap, char *d) {
  189. /* Uses the IBM weight system if wrap = 7, and the NCR system if wrap = 9 */
  190. int i;
  191. char check_digit;
  192. /* draw data section */
  193. for (i = 0; i < length; i++, d += 8) {
  194. memcpy(d, MSITable[source[i] - '0'], 8);
  195. }
  196. /* Append check digit */
  197. check_digit = msi_check_digit_mod11(source, length, wrap);
  198. if (check_digit == ':') {
  199. memcpy(d, MSITable[1], 8);
  200. d += 8;
  201. memcpy(d, MSITable[0], 8);
  202. d += 8;
  203. } else {
  204. memcpy(d, MSITable[check_digit - '0'], 8);
  205. d += 8;
  206. }
  207. symbol->text[0] = '\0';
  208. ustrncat(symbol->text, source, length);
  209. if (!no_checktext) {
  210. if (check_digit == ':') {
  211. ustrcat(symbol->text, "10");
  212. } else {
  213. symbol->text[length] = check_digit;
  214. symbol->text[length + 1] = '\0';
  215. }
  216. }
  217. return d;
  218. }
  219. /* MSI Plessey with Modulo 11 check digit and Modulo 10 check digit */
  220. static char *msi_plessey_mod1110(struct zint_symbol *symbol, const unsigned char source[], const int length,
  221. const int no_checktext, const int wrap, char *d) {
  222. /* Uses the IBM weight system if wrap = 7, and the NCR system if wrap = 9 */
  223. int i;
  224. char check_digit;
  225. unsigned char temp[92 + 3 + 1];
  226. int temp_len = length;
  227. temp[0] = '\0';
  228. ustrncat(temp, source, length);
  229. /* Append first (mod 11) digit */
  230. check_digit = msi_check_digit_mod11(source, length, wrap);
  231. if (check_digit == ':') {
  232. temp[temp_len++] = '1';
  233. temp[temp_len++] = '0';
  234. } else {
  235. temp[temp_len++] = check_digit;
  236. }
  237. /* Append second (mod 10) check digit */
  238. temp[temp_len] = msi_check_digit_mod10(temp, temp_len);
  239. temp[++temp_len] = '\0';
  240. /* draw data section */
  241. for (i = 0; i < temp_len; i++, d += 8) {
  242. memcpy(d, MSITable[temp[i] - '0'], 8);
  243. }
  244. if (no_checktext) {
  245. symbol->text[0] = '\0';
  246. ustrncat(symbol->text, source, length);
  247. } else {
  248. ustrcpy(symbol->text, temp);
  249. }
  250. return d;
  251. }
  252. INTERNAL int msi_plessey(struct zint_symbol *symbol, unsigned char source[], int length) {
  253. int error_number = 0;
  254. int i;
  255. char dest[766]; /* 2 + 92 * 8 + 3 * 8 + 3 + 1 = 766 */
  256. char *d = dest;
  257. int check_option = symbol->option_2;
  258. int no_checktext = 0;
  259. if (length > 92) { /* 3 (Start) + 92 * 12 + 3 * 12 + 4 (Stop) = 1147 */
  260. return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 372, "Input length %d too long (maximum 92)", length);
  261. }
  262. if ((i = not_sane(NEON_F, source, length))) {
  263. return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 377,
  264. "Invalid character at position %d in input (digits only)", i);
  265. }
  266. if (check_option >= 11 && check_option <= 16) { /* +10 means don't print check digits in HRT */
  267. check_option -= 10;
  268. no_checktext = 1;
  269. }
  270. if ((check_option < 0) || (check_option > 6)) {
  271. check_option = 0;
  272. }
  273. /* Start character */
  274. memcpy(d, "21", 2);
  275. d += 2;
  276. switch (check_option) {
  277. case 0: d = msi_plessey_nomod(symbol, source, length, d);
  278. break;
  279. case 1: d = msi_plessey_mod10(symbol, source, length, no_checktext, d);
  280. break;
  281. case 2: d = msi_plessey_mod1010(symbol, source, length, no_checktext, d);
  282. break;
  283. case 3: d = msi_plessey_mod11(symbol, source, length, no_checktext, 7 /*IBM wrap*/, d);
  284. break;
  285. case 4: d = msi_plessey_mod1110(symbol, source, length, no_checktext, 7 /*IBM wrap*/, d);
  286. break;
  287. case 5: d = msi_plessey_mod11(symbol, source, length, no_checktext, 9 /*NCR wrap*/, d);
  288. break;
  289. case 6: d = msi_plessey_mod1110(symbol, source, length, no_checktext, 9 /*NCR wrap*/, d);
  290. break;
  291. }
  292. /* Stop character */
  293. memcpy(d, "121", 3);
  294. d += 3;
  295. expand(symbol, dest, d - dest);
  296. /* TODO: Find documentation on BARCODE_MSI_PLESSEY dimensions/height */
  297. return error_number;
  298. }
  299. /* vim: set ts=4 sw=4 et : */