split.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * qrencode - QR Code encoder
  3. *
  4. * Input data splitter.
  5. * Copyright (C) 2006-2017 Kentaro Fukuchi <kentaro@fukuchi.org>
  6. *
  7. * The following data / specifications are taken from
  8. * "Two dimensional symbol -- QR-code -- Basic Specification" (JIS X0510:2004)
  9. * or
  10. * "Automatic identification and data capture techniques --
  11. * QR Code 2005 bar code symbology specification" (ISO/IEC 18004:2006)
  12. *
  13. * This library is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2.1 of the License, or any later version.
  17. *
  18. * This library is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with this library; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. #if HAVE_CONFIG_H
  28. # include "config.h"
  29. #endif
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. #include "qrencode.h"
  34. #include "qrinput.h"
  35. #include "qrspec.h"
  36. #include "split.h"
  37. #define isdigit(__c__) ((unsigned char)((signed char)(__c__) - '0') < 10)
  38. #define isalnum(__c__) (QRinput_lookAnTable(__c__) >= 0)
  39. #if !HAVE_STRDUP
  40. #undef strdup
  41. char *strdup(const char *s)
  42. {
  43. size_t len = strlen(s) + 1;
  44. void *newstring = malloc(len);
  45. if(newstring == NULL) return NULL;
  46. return (char *)memcpy(newstring, s, len);
  47. }
  48. #endif
  49. static QRencodeMode Split_identifyMode(const char *string, QRencodeMode hint)
  50. {
  51. unsigned char c, d;
  52. unsigned int word;
  53. c = (unsigned char)string[0];
  54. if(c == '\0') return QR_MODE_NUL;
  55. if(isdigit(c)) {
  56. return QR_MODE_NUM;
  57. } else if(isalnum(c)) {
  58. return QR_MODE_AN;
  59. } else if(hint == QR_MODE_KANJI) {
  60. d = (unsigned char)string[1];
  61. if(d != '\0') {
  62. word = ((unsigned int)c << 8) | d;
  63. if((word >= 0x8140 && word <= 0x9ffc) || (word >= 0xe040 && word <= 0xebbf)) {
  64. return QR_MODE_KANJI;
  65. }
  66. }
  67. }
  68. return QR_MODE_8;
  69. }
  70. static int Split_eatAn(const char *string, QRinput *input, QRencodeMode hint);
  71. static int Split_eat8(const char *string, QRinput *input, QRencodeMode hint);
  72. static int Split_eatNum(const char *string, QRinput *input,QRencodeMode hint)
  73. {
  74. const char *p;
  75. int ret;
  76. int run;
  77. int dif;
  78. int ln;
  79. QRencodeMode mode;
  80. ln = QRspec_lengthIndicator(QR_MODE_NUM, input->version);
  81. p = string;
  82. while(isdigit(*p)) {
  83. p++;
  84. }
  85. run = (int)(p - string);
  86. mode = Split_identifyMode(p, hint);
  87. if(mode == QR_MODE_8) {
  88. dif = QRinput_estimateBitsModeNum(run) + 4 + ln
  89. + QRinput_estimateBitsMode8(1) /* + 4 + l8 */
  90. - QRinput_estimateBitsMode8(run + 1) /* - 4 - l8 */;
  91. if(dif > 0) {
  92. return Split_eat8(string, input, hint);
  93. }
  94. }
  95. if(mode == QR_MODE_AN) {
  96. dif = QRinput_estimateBitsModeNum(run) + 4 + ln
  97. + QRinput_estimateBitsModeAn(1) /* + 4 + la */
  98. - QRinput_estimateBitsModeAn(run + 1) /* - 4 - la */;
  99. if(dif > 0) {
  100. return Split_eatAn(string, input, hint);
  101. }
  102. }
  103. ret = QRinput_append(input, QR_MODE_NUM, run, (unsigned char *)string);
  104. if(ret < 0) return -1;
  105. return run;
  106. }
  107. static int Split_eatAn(const char *string, QRinput *input, QRencodeMode hint)
  108. {
  109. const char *p, *q;
  110. int ret;
  111. int run;
  112. int dif;
  113. int la, ln;
  114. la = QRspec_lengthIndicator(QR_MODE_AN, input->version);
  115. ln = QRspec_lengthIndicator(QR_MODE_NUM, input->version);
  116. p = string;
  117. while(isalnum(*p)) {
  118. if(isdigit(*p)) {
  119. q = p;
  120. while(isdigit(*q)) {
  121. q++;
  122. }
  123. dif = QRinput_estimateBitsModeAn((int)(p - string)) /* + 4 + la */
  124. + QRinput_estimateBitsModeNum((int)(q - p)) + 4 + ln
  125. + (isalnum(*q)?(4 + ln):0)
  126. - QRinput_estimateBitsModeAn((int)(q - string)) /* - 4 - la */;
  127. if(dif < 0) {
  128. break;
  129. }
  130. p = q;
  131. } else {
  132. p++;
  133. }
  134. }
  135. run = (int)(p - string);
  136. if(*p && !isalnum(*p)) {
  137. dif = QRinput_estimateBitsModeAn(run) + 4 + la
  138. + QRinput_estimateBitsMode8(1) /* + 4 + l8 */
  139. - QRinput_estimateBitsMode8(run + 1) /* - 4 - l8 */;
  140. if(dif > 0) {
  141. return Split_eat8(string, input, hint);
  142. }
  143. }
  144. ret = QRinput_append(input, QR_MODE_AN, run, (unsigned char *)string);
  145. if(ret < 0) return -1;
  146. return run;
  147. }
  148. static int Split_eatKanji(const char *string, QRinput *input, QRencodeMode hint)
  149. {
  150. const char *p;
  151. int ret;
  152. int run;
  153. p = string;
  154. while(Split_identifyMode(p, hint) == QR_MODE_KANJI) {
  155. p += 2;
  156. }
  157. run = (int)(p - string);
  158. ret = QRinput_append(input, QR_MODE_KANJI, run, (unsigned char *)string);
  159. if(ret < 0) return -1;
  160. return run;
  161. }
  162. static int Split_eat8(const char *string, QRinput *input, QRencodeMode hint)
  163. {
  164. const char *p, *q;
  165. QRencodeMode mode;
  166. int ret;
  167. int run;
  168. int dif;
  169. int la, ln, l8;
  170. int swcost;
  171. la = QRspec_lengthIndicator(QR_MODE_AN, input->version);
  172. ln = QRspec_lengthIndicator(QR_MODE_NUM, input->version);
  173. l8 = QRspec_lengthIndicator(QR_MODE_8, input->version);
  174. p = string + 1;
  175. while(*p != '\0') {
  176. mode = Split_identifyMode(p, hint);
  177. if(mode == QR_MODE_KANJI) {
  178. break;
  179. }
  180. if(mode == QR_MODE_NUM) {
  181. q = p;
  182. while(isdigit(*q)) {
  183. q++;
  184. }
  185. if(Split_identifyMode(q, hint) == QR_MODE_8) {
  186. swcost = 4 + l8;
  187. } else {
  188. swcost = 0;
  189. }
  190. dif = QRinput_estimateBitsMode8((int)(p - string)) /* + 4 + l8 */
  191. + QRinput_estimateBitsModeNum((int)(q - p)) + 4 + ln
  192. + swcost
  193. - QRinput_estimateBitsMode8((int)(q - string)) /* - 4 - l8 */;
  194. if(dif < 0) {
  195. break;
  196. }
  197. p = q;
  198. } else if(mode == QR_MODE_AN) {
  199. q = p;
  200. while(isalnum(*q)) {
  201. q++;
  202. }
  203. if(Split_identifyMode(q, hint) == QR_MODE_8) {
  204. swcost = 4 + l8;
  205. } else {
  206. swcost = 0;
  207. }
  208. dif = QRinput_estimateBitsMode8((int)(p - string)) /* + 4 + l8 */
  209. + QRinput_estimateBitsModeAn((int)(q - p)) + 4 + la
  210. + swcost
  211. - QRinput_estimateBitsMode8((int)(q - string)) /* - 4 - l8 */;
  212. if(dif < 0) {
  213. break;
  214. }
  215. p = q;
  216. } else {
  217. p++;
  218. }
  219. }
  220. run = (int)(p - string);
  221. ret = QRinput_append(input, QR_MODE_8, run, (unsigned char *)string);
  222. if(ret < 0) return -1;
  223. return run;
  224. }
  225. static int Split_splitString(const char *string, QRinput *input,
  226. QRencodeMode hint)
  227. {
  228. int length;
  229. QRencodeMode mode;
  230. if(*string == '\0') return 0;
  231. mode = Split_identifyMode(string, hint);
  232. if(mode == QR_MODE_NUM) {
  233. length = Split_eatNum(string, input, hint);
  234. } else if(mode == QR_MODE_AN) {
  235. length = Split_eatAn(string, input, hint);
  236. } else if(mode == QR_MODE_KANJI && hint == QR_MODE_KANJI) {
  237. length = Split_eatKanji(string, input, hint);
  238. } else {
  239. length = Split_eat8(string, input, hint);
  240. }
  241. if(length == 0) return 0;
  242. if(length < 0) return -1;
  243. return Split_splitString(&string[length], input, hint);
  244. }
  245. static char *dupAndToUpper(const char *str, QRencodeMode hint)
  246. {
  247. char *newstr, *p;
  248. QRencodeMode mode;
  249. newstr = strdup(str);
  250. if(newstr == NULL) return NULL;
  251. p = newstr;
  252. while(*p != '\0') {
  253. mode = Split_identifyMode(p, hint);
  254. if(mode == QR_MODE_KANJI) {
  255. p += 2;
  256. } else {
  257. if (*p >= 'a' && *p <= 'z') {
  258. *p = (char)((int)*p - 32);
  259. }
  260. p++;
  261. }
  262. }
  263. return newstr;
  264. }
  265. int Split_splitStringToQRinput(const char *string, QRinput *input,
  266. QRencodeMode hint, int casesensitive)
  267. {
  268. char *newstr;
  269. int ret;
  270. if(string == NULL || *string == '\0') {
  271. errno = EINVAL;
  272. return -1;
  273. }
  274. if(!casesensitive) {
  275. newstr = dupAndToUpper(string, hint);
  276. if(newstr == NULL) return -1;
  277. ret = Split_splitString(newstr, input, hint);
  278. free(newstr);
  279. } else {
  280. ret = Split_splitString(string, input, hint);
  281. }
  282. return ret;
  283. }