general_field.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* general_field.c - Handles general field compaction (GS1 DataBar and composites) */
  2. /*
  3. libzint - the open source barcode library
  4. Copyright (C) 2019-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 "common.h"
  30. #include "general_field.h"
  31. static const char alphanum_puncs[] = "*,-./";
  32. static const char isoiec_puncs[] = "!\"%&'()*+,-./:;<=>?_ "; /* Note contains space, not in cset82 */
  33. #define IS_ISOIEC_F (IS_LWR_F | IS_C82_F | IS_AST_F | IS_PLS_F | IS_MNS_F | IS_SPC_F)
  34. /* Returns type of char at `i`. FNC1 counted as NUMERIC. Returns 0 if invalid char */
  35. static int general_field_type(const char *general_field, const int i) {
  36. if (general_field[i] == '\x1D' || z_isdigit(general_field[i])) {
  37. return NUMERIC;
  38. }
  39. if (z_isupper(general_field[i]) || posn(alphanum_puncs, general_field[i]) != -1) {
  40. return ALPHANUMERIC;
  41. }
  42. if (!not_sane(IS_ISOIEC_F, (const unsigned char *) general_field + i, 1)) {
  43. return ISOIEC;
  44. }
  45. return 0;
  46. }
  47. /* Returns true if next (including `i`) `num` chars of type `type`, or if given (non-zero), `type2` */
  48. static int general_field_next(const char *general_field, int i, const int general_field_len, int num, const int type,
  49. const int type2) {
  50. if (i + num > general_field_len) {
  51. return 0;
  52. }
  53. for (; i < general_field_len && num; i++, num--) {
  54. int type_i = general_field_type(general_field, i);
  55. if ((type_i != type && !type2) || (type_i != type && type_i != type2)) {
  56. return 0;
  57. }
  58. }
  59. return num == 0;
  60. }
  61. /* Returns true if next (including `i`) `num` up to `max_num` chars of type `type` and occur at end */
  62. static int general_field_next_terminate(const char *general_field, int i, const int general_field_len, int num,
  63. const int max_num, const int type) {
  64. if (i + max_num < general_field_len) {
  65. return 0;
  66. }
  67. for (; i < general_field_len; i++, num--) {
  68. if (general_field_type(general_field, i) != type) {
  69. return 0;
  70. }
  71. }
  72. return i == general_field_len && num <= 0;
  73. }
  74. /* Returns true if none of the next (including `i`) `num` chars (or end occurs) of type `type` */
  75. static int general_field_next_none(const char *general_field, int i, const int general_field_len, int num,
  76. const int type) {
  77. for (; i < general_field_len && num; i++, num--) {
  78. if (general_field_type(general_field, i) == type) {
  79. return 0;
  80. }
  81. }
  82. return num == 0 || i == general_field_len;
  83. }
  84. /* Attempts to apply encoding rules from sections 7.2.5.5.1 to 7.2.5.5.3
  85. * of ISO/IEC 24724:2011 (same as sections 5.4.1 to 5.4.3 of ISO/IEC 24723:2010) */
  86. INTERNAL int general_field_encode(const char *general_field, const int general_field_len, int *p_mode,
  87. char *p_last_digit, char binary_string[], int *p_bp) {
  88. int i, d1, d2;
  89. int mode = *p_mode;
  90. char last_digit = '\0'; /* Set to odd remaining digit at end if any */
  91. int bp = *p_bp;
  92. for (i = 0; i < general_field_len; ) {
  93. int type = general_field_type(general_field, i);
  94. if (!type) {
  95. return 0;
  96. }
  97. switch (mode) {
  98. case NUMERIC:
  99. if (i < general_field_len - 1) { /* If at least 2 characters remain */
  100. if (type != NUMERIC || general_field_type(general_field, i + 1) != NUMERIC) {
  101. /* 7.2.5.5.1/5.4.1 a) */
  102. bp = bin_append_posn(0, 4, binary_string, bp); /* Alphanumeric latch "0000" */
  103. mode = ALPHANUMERIC;
  104. } else {
  105. d1 = general_field[i] == '\x1D' ? 10 : ctoi(general_field[i]);
  106. d2 = general_field[i + 1] == '\x1D' ? 10 : ctoi(general_field[i + 1]);
  107. bp = bin_append_posn((11 * d1) + d2 + 8, 7, binary_string, bp);
  108. i += 2;
  109. }
  110. } else { /* If 1 character remains */
  111. if (type != NUMERIC) {
  112. /* 7.2.5.5.1/5.4.1 b) */
  113. bp = bin_append_posn(0, 4, binary_string, bp); /* Alphanumeric latch "0000" */
  114. mode = ALPHANUMERIC;
  115. } else {
  116. /* Ending with single digit.
  117. * 7.2.5.5.1 c) and 5.4.1 c) dealt with separately outside this procedure */
  118. last_digit = general_field[i];
  119. i++;
  120. }
  121. }
  122. break;
  123. case ALPHANUMERIC:
  124. if (general_field[i] == '\x1D') {
  125. /* 7.2.5.5.2/5.4.2 a) */
  126. bp = bin_append_posn(15, 5, binary_string, bp); /* "01111" */
  127. mode = NUMERIC;
  128. i++;
  129. } else if (type == ISOIEC) {
  130. /* 7.2.5.5.2/5.4.2 b) */
  131. bp = bin_append_posn(4, 5, binary_string, bp); /* ISO/IEC 646 latch "00100" */
  132. mode = ISOIEC;
  133. } else if (general_field_next(general_field, i, general_field_len, 6, NUMERIC, 0)) {
  134. /* 7.2.5.5.2/5.4.2 c) */
  135. bp = bin_append_posn(0, 3, binary_string, bp); /* Numeric latch "000" */
  136. mode = NUMERIC;
  137. } else if (general_field_next_terminate(general_field, i, general_field_len, 4,
  138. 5 /*Can limit to 5 max due to above*/, NUMERIC)) {
  139. /* 7.2.5.5.2/5.4.2 d) */
  140. bp = bin_append_posn(0, 3, binary_string, bp); /* Numeric latch "000" */
  141. mode = NUMERIC;
  142. } else if (z_isdigit(general_field[i])) {
  143. bp = bin_append_posn(general_field[i] - 43, 5, binary_string, bp);
  144. i++;
  145. } else if (z_isupper(general_field[i])) {
  146. bp = bin_append_posn(general_field[i] - 33, 6, binary_string, bp);
  147. i++;
  148. } else {
  149. bp = bin_append_posn(posn(alphanum_puncs, general_field[i]) + 58, 6, binary_string, bp);
  150. i++;
  151. }
  152. break;
  153. case ISOIEC:
  154. if (general_field[i] == '\x1D') {
  155. /* 7.2.5.5.3/5.4.3 a) */
  156. bp = bin_append_posn(15, 5, binary_string, bp); /* "01111" */
  157. mode = NUMERIC;
  158. i++;
  159. } else {
  160. int next_10_not_isoiec = general_field_next_none(general_field, i, general_field_len, 10, ISOIEC);
  161. if (next_10_not_isoiec && general_field_next(general_field, i, general_field_len, 4,
  162. NUMERIC, 0)) {
  163. /* 7.2.5.5.3/5.4.3 b) */
  164. bp = bin_append_posn(0, 3, binary_string, bp); /* Numeric latch "000" */
  165. mode = NUMERIC;
  166. } else if (next_10_not_isoiec && general_field_next(general_field, i, general_field_len, 5,
  167. ALPHANUMERIC, NUMERIC)) {
  168. /* 7.2.5.5.3/5.4.3 c) */
  169. /* Note this rule can produce longer bitstreams if most of the alphanumerics are numeric */
  170. bp = bin_append_posn(4, 5, binary_string, bp); /* Alphanumeric latch "00100" */
  171. mode = ALPHANUMERIC;
  172. } else if (z_isdigit(general_field[i])) {
  173. bp = bin_append_posn(general_field[i] - 43, 5, binary_string, bp);
  174. i++;
  175. } else if (z_isupper(general_field[i])) {
  176. bp = bin_append_posn(general_field[i] - 1, 7, binary_string, bp);
  177. i++;
  178. } else if (z_islower(general_field[i])) {
  179. bp = bin_append_posn(general_field[i] - 7, 7, binary_string, bp);
  180. i++;
  181. } else {
  182. bp = bin_append_posn(posn(isoiec_puncs, general_field[i]) + 232, 8, binary_string, bp);
  183. i++;
  184. }
  185. }
  186. break;
  187. }
  188. }
  189. *p_mode = mode;
  190. *p_last_digit = last_digit;
  191. *p_bp = bp;
  192. return 1;
  193. }
  194. /* vim: set ts=4 sw=4 et : */