auspost.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* auspost.c - Handles Australia Post 4-State Barcode */
  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. static const char GDSET[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz #";
  30. #define GDSET_F (IS_NUM_F | IS_UPR_F | IS_LWR_F | IS_SPC_F | IS_HSH_F)
  31. static const char AusNTable[10][2] = {
  32. {'0','0'}, {'0','1'}, {'0','2'}, {'1','0'}, {'1','1'}, {'1','2'}, {'2','0'}, {'2','1'}, {'2','2'}, {'3','0'}
  33. };
  34. static const char AusCTable[64][3] = {
  35. {'2','2','2'}, {'3','0','0'}, {'3','0','1'}, {'3','0','2'}, {'3','1','0'}, {'3','1','1'},
  36. {'3','1','2'}, {'3','2','0'}, {'3','2','1'}, {'3','2','2'}, {'0','0','0'}, {'0','0','1'},
  37. {'0','0','2'}, {'0','1','0'}, {'0','1','1'}, {'0','1','2'}, {'0','2','0'}, {'0','2','1'},
  38. {'0','2','2'}, {'1','0','0'}, {'1','0','1'}, {'1','0','2'}, {'1','1','0'}, {'1','1','1'},
  39. {'1','1','2'}, {'1','2','0'}, {'1','2','1'}, {'1','2','2'}, {'2','0','0'}, {'2','0','1'},
  40. {'2','0','2'}, {'2','1','0'}, {'2','1','1'}, {'2','1','2'}, {'2','2','0'}, {'2','2','1'},
  41. {'0','2','3'}, {'0','3','0'}, {'0','3','1'}, {'0','3','2'}, {'0','3','3'}, {'1','0','3'},
  42. {'1','1','3'}, {'1','2','3'}, {'1','3','0'}, {'1','3','1'}, {'1','3','2'}, {'1','3','3'},
  43. {'2','0','3'}, {'2','1','3'}, {'2','2','3'}, {'2','3','0'}, {'2','3','1'}, {'2','3','2'},
  44. {'2','3','3'}, {'3','0','3'}, {'3','1','3'}, {'3','2','3'}, {'3','3','0'}, {'3','3','1'},
  45. {'3','3','2'}, {'3','3','3'}, {'0','0','3'}, {'0','1','3'}
  46. };
  47. static const char AusBarTable[64][3] = {
  48. {'0','0','0'}, {'0','0','1'}, {'0','0','2'}, {'0','0','3'}, {'0','1','0'}, {'0','1','1'},
  49. {'0','1','2'}, {'0','1','3'}, {'0','2','0'}, {'0','2','1'}, {'0','2','2'}, {'0','2','3'},
  50. {'0','3','0'}, {'0','3','1'}, {'0','3','2'}, {'0','3','3'}, {'1','0','0'}, {'1','0','1'},
  51. {'1','0','2'}, {'1','0','3'}, {'1','1','0'}, {'1','1','1'}, {'1','1','2'}, {'1','1','3'},
  52. {'1','2','0'}, {'1','2','1'}, {'1','2','2'}, {'1','2','3'}, {'1','3','0'}, {'1','3','1'},
  53. {'1','3','2'}, {'1','3','3'}, {'2','0','0'}, {'2','0','1'}, {'2','0','2'}, {'2','0','3'},
  54. {'2','1','0'}, {'2','1','1'}, {'2','1','2'}, {'2','1','3'}, {'2','2','0'}, {'2','2','1'},
  55. {'2','2','2'}, {'2','2','3'}, {'2','3','0'}, {'2','3','1'}, {'2','3','2'}, {'2','3','3'},
  56. {'3','0','0'}, {'3','0','1'}, {'3','0','2'}, {'3','0','3'}, {'3','1','0'}, {'3','1','1'},
  57. {'3','1','2'}, {'3','1','3'}, {'3','2','0'}, {'3','2','1'}, {'3','2','2'}, {'3','2','3'},
  58. {'3','3','0'}, {'3','3','1'}, {'3','3','2'}, {'3','3','3'}
  59. };
  60. #include <stdio.h>
  61. #include "common.h"
  62. #include "reedsol.h"
  63. static char aus_convert_pattern(char data, int shift) {
  64. return (data - '0') << shift;
  65. }
  66. /* Adds Reed-Solomon error correction to auspost */
  67. static char *aus_rs_error(char data_pattern[], char *d) {
  68. int reader, length, triple_writer = 0;
  69. unsigned char triple[31];
  70. unsigned char result[5];
  71. rs_t rs;
  72. for (reader = 2, length = d - data_pattern; reader < length; reader += 3, triple_writer++) {
  73. triple[triple_writer] = aus_convert_pattern(data_pattern[reader], 4)
  74. + aus_convert_pattern(data_pattern[reader + 1], 2)
  75. + aus_convert_pattern(data_pattern[reader + 2], 0);
  76. }
  77. rs_init_gf(&rs, 0x43);
  78. rs_init_code(&rs, 4, 1);
  79. rs_encode(&rs, triple_writer, triple, result);
  80. for (reader = 0; reader < 4; reader++, d += 3) {
  81. memcpy(d, AusBarTable[result[reader]], 3);
  82. }
  83. return d;
  84. }
  85. INTERNAL int daft_set_height(struct zint_symbol *symbol, const float min_height, const float max_height);
  86. /* Handles Australia Posts's 4 State Codes */
  87. INTERNAL int auspost(struct zint_symbol *symbol, unsigned char source[], int length) {
  88. /* Customer Standard Barcode, Barcode 2 or Barcode 3 system determined automatically
  89. (i.e. the FCC doesn't need to be specified by the user) dependent
  90. on the length of the input string */
  91. /* The contents of data_pattern conform to the following standard:
  92. 0 = Tracker, Ascender and Descender
  93. 1 = Tracker and Ascender
  94. 2 = Tracker and Descender
  95. 3 = Tracker only */
  96. int i;
  97. int error_number;
  98. int writer;
  99. int loopey, reader;
  100. int h;
  101. char data_pattern[200];
  102. char *d = data_pattern;
  103. char fcc[3] = {0}, dpid[10];
  104. char localstr[30];
  105. /* Do all of the length checking first to avoid stack smashing */
  106. if (symbol->symbology == BARCODE_AUSPOST) {
  107. if (length != 8 && length != 13 && length != 16 && length != 18 && length != 23) {
  108. return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 401, "Input length %d wrong (8, 13, 16, 18 or 23 only)",
  109. length);
  110. }
  111. } else if (length > 8) {
  112. return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 403, "Input length %d too long (maximum 8)", length);
  113. }
  114. /* Check input immediately to catch nuls */
  115. if ((i = not_sane(GDSET_F, source, length))) {
  116. return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 404,
  117. "Invalid character at position %d in input (alphanumerics, space and \"#\" only)", i);
  118. }
  119. localstr[0] = '\0';
  120. if (symbol->symbology == BARCODE_AUSPOST) {
  121. /* Format control code (FCC) */
  122. switch (length) {
  123. case 8:
  124. strcpy(fcc, "11");
  125. break;
  126. case 13:
  127. strcpy(fcc, "59");
  128. break;
  129. case 16:
  130. strcpy(fcc, "59");
  131. if ((i = not_sane(NEON_F, source, length))) {
  132. return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 402,
  133. "Invalid character at position %d in input (digits only for FCC 59 length 16)",
  134. i);
  135. }
  136. break;
  137. case 18:
  138. strcpy(fcc, "62");
  139. break;
  140. case 23:
  141. strcpy(fcc, "62");
  142. if ((i = not_sane(NEON_F, source, length))) {
  143. return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 406,
  144. "Invalid character at position %d in input (digits only for FCC 62 length 23)",
  145. i);
  146. }
  147. break;
  148. }
  149. } else {
  150. int zeroes;
  151. switch (symbol->symbology) {
  152. case BARCODE_AUSREPLY: strcpy(fcc, "45");
  153. break;
  154. case BARCODE_AUSROUTE: strcpy(fcc, "87");
  155. break;
  156. case BARCODE_AUSREDIRECT: strcpy(fcc, "92");
  157. break;
  158. }
  159. /* Add leading zeros as required */
  160. zeroes = 8 - length;
  161. memset(localstr, '0', zeroes);
  162. localstr[zeroes] = '\0';
  163. }
  164. if (symbol->debug & ZINT_DEBUG_PRINT) {
  165. printf("AUSPOST FCC: %s\n", fcc);
  166. }
  167. ustrncat(localstr, source, length);
  168. h = (int) strlen(localstr);
  169. /* Verify that the first 8 characters are numbers */
  170. memcpy(dpid, localstr, 8);
  171. dpid[8] = '\0';
  172. if ((i = not_sane(NEON_F, (unsigned char *) dpid, 8))) {
  173. return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 405,
  174. "Invalid character at position %d in DPID (first 8 characters) (digits only)", i);
  175. }
  176. /* Start character */
  177. memcpy(d, "13", 2);
  178. d += 2;
  179. /* Encode the FCC */
  180. for (reader = 0; reader < 2; reader++, d += 2) {
  181. memcpy(d, AusNTable[fcc[reader] - '0'], 2);
  182. }
  183. /* Delivery Point Identifier (DPID) */
  184. for (reader = 0; reader < 8; reader++, d += 2) {
  185. memcpy(d, AusNTable[dpid[reader] - '0'], 2);
  186. }
  187. /* Customer Information */
  188. if (h > 8) {
  189. if ((h == 13) || (h == 18)) {
  190. for (reader = 8; reader < h; reader++, d += 3) {
  191. memcpy(d, AusCTable[posn(GDSET, localstr[reader])], 3);
  192. }
  193. } else if ((h == 16) || (h == 23)) {
  194. for (reader = 8; reader < h; reader++, d += 2) {
  195. memcpy(d, AusNTable[localstr[reader] - '0'], 2);
  196. }
  197. }
  198. }
  199. /* Filler bar */
  200. h = d - data_pattern;
  201. switch (h) {
  202. case 22:
  203. case 37:
  204. case 52:
  205. *d++ = '3';
  206. break;
  207. default:
  208. break;
  209. }
  210. /* Reed Solomon error correction */
  211. d = aus_rs_error(data_pattern, d);
  212. /* Stop character */
  213. memcpy(d, "13", 2);
  214. d += 2;
  215. /* Turn the symbol into a bar pattern ready for plotting */
  216. writer = 0;
  217. h = d - data_pattern;
  218. for (loopey = 0; loopey < h; loopey++) {
  219. if ((data_pattern[loopey] == '1') || (data_pattern[loopey] == '0')) {
  220. set_module(symbol, 0, writer);
  221. }
  222. set_module(symbol, 1, writer);
  223. if ((data_pattern[loopey] == '2') || (data_pattern[loopey] == '0')) {
  224. set_module(symbol, 2, writer);
  225. }
  226. writer += 2;
  227. }
  228. if (symbol->output_options & COMPLIANT_HEIGHT) {
  229. /* Australia Post Customer Barcoding Technical Specifications (Revised Aug 2012) Dimensions, placement and
  230. printing p.12
  231. (https://auspost.com.au/content/dam/auspost_corp/media/documents/
  232. customer-barcode-technical-specifications-aug2012.pdf)
  233. X 0.5mm (average of 0.4mm - 0.6mm), min height 4.2mm / 0.6mm (X max) = 7, max 5.6mm / 0.4mm (X min) = 14
  234. Tracker 1.3mm (average of 1mm - 1.6mm)
  235. Ascender/Descender 3.15mm (average of 2.6mm - 3.7mm) less T = 1.85mm
  236. */
  237. symbol->row_height[0] = 3.7f; /* 1.85f / 0.5f */
  238. symbol->row_height[1] = 2.6f; /* 1.3f / 0.5f */
  239. error_number = daft_set_height(symbol, 7.0f, 14.0f); /* Note using max X for minimum and min X for maximum */
  240. } else {
  241. symbol->row_height[0] = 3.0f;
  242. symbol->row_height[1] = 2.0f;
  243. error_number = daft_set_height(symbol, 0.0f, 0.0f);
  244. }
  245. symbol->rows = 3;
  246. symbol->width = writer - 1;
  247. return error_number;
  248. }
  249. /* vim: set ts=4 sw=4 et : */