qrinput.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  1. /*
  2. * qrencode - QR Code encoder
  3. *
  4. * Input data chunk class
  5. * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. //#include "config.h"
  26. #include "qrencode.h"
  27. #include "qrspec.h"
  28. #include "bitstream.h"
  29. #include "qrinput.h"
  30. /******************************************************************************
  31. * Entry of input data
  32. *****************************************************************************/
  33. static QRinput_List *QRinput_List_newEntry(QRencodeMode mode, int size, const unsigned char *data)
  34. {
  35. QRinput_List *entry;
  36. if(QRinput_check(mode, size, data)) {
  37. errno = EINVAL;
  38. return NULL;
  39. }
  40. entry = (QRinput_List *)malloc(sizeof(QRinput_List));
  41. if(entry == NULL) return NULL;
  42. entry->mode = mode;
  43. entry->size = size;
  44. entry->data = (unsigned char *)malloc(size);
  45. if(entry->data == NULL) {
  46. free(entry);
  47. return NULL;
  48. }
  49. memcpy(entry->data, data, size);
  50. entry->bstream = NULL;
  51. entry->next = NULL;
  52. return entry;
  53. }
  54. static void QRinput_List_freeEntry(QRinput_List *entry)
  55. {
  56. if(entry != NULL) {
  57. free(entry->data);
  58. BitStream_free(entry->bstream);
  59. free(entry);
  60. }
  61. }
  62. static QRinput_List *QRinput_List_dup(QRinput_List *entry)
  63. {
  64. QRinput_List *n;
  65. n = (QRinput_List *)malloc(sizeof(QRinput_List));
  66. if(n == NULL) return NULL;
  67. n->mode = entry->mode;
  68. n->size = entry->size;
  69. n->data = (unsigned char *)malloc(n->size);
  70. if(n->data == NULL) {
  71. free(n);
  72. return NULL;
  73. }
  74. memcpy(n->data, entry->data, entry->size);
  75. n->bstream = NULL;
  76. n->next = NULL;
  77. return n;
  78. }
  79. /******************************************************************************
  80. * Input Data
  81. *****************************************************************************/
  82. QRinput *QRinput_new(void)
  83. {
  84. return QRinput_new2(0, QR_ECLEVEL_L);
  85. }
  86. QRinput *QRinput_new2(int version, QRecLevel level)
  87. {
  88. QRinput *input;
  89. if(version < 0 || version > QRSPEC_VERSION_MAX || level > QR_ECLEVEL_H) {
  90. errno = EINVAL;
  91. return NULL;
  92. }
  93. input = (QRinput *)malloc(sizeof(QRinput));
  94. if(input == NULL) return NULL;
  95. input->head = NULL;
  96. input->tail = NULL;
  97. input->version = version;
  98. input->level = level;
  99. return input;
  100. }
  101. int QRinput_getVersion(QRinput *input)
  102. {
  103. return input->version;
  104. }
  105. int QRinput_setVersion(QRinput *input, int version)
  106. {
  107. if(version < 0 || version > QRSPEC_VERSION_MAX) {
  108. errno = EINVAL;
  109. return -1;
  110. }
  111. input->version = version;
  112. return 0;
  113. }
  114. QRecLevel QRinput_getErrorCorrectionLevel(QRinput *input)
  115. {
  116. return input->level;
  117. }
  118. int QRinput_setErrorCorrectionLevel(QRinput *input, QRecLevel level)
  119. {
  120. if(level > QR_ECLEVEL_H) {
  121. errno = EINVAL;
  122. return -1;
  123. }
  124. input->level = level;
  125. return 0;
  126. }
  127. static void QRinput_appendEntry(QRinput *input, QRinput_List *entry)
  128. {
  129. if(input->tail == NULL) {
  130. input->head = entry;
  131. input->tail = entry;
  132. } else {
  133. input->tail->next = entry;
  134. input->tail = entry;
  135. }
  136. entry->next = NULL;
  137. }
  138. int QRinput_append(QRinput *input, QRencodeMode mode, int size, const unsigned char *data)
  139. {
  140. QRinput_List *entry;
  141. entry = QRinput_List_newEntry(mode, size, data);
  142. if(entry == NULL) {
  143. return -1;
  144. }
  145. QRinput_appendEntry(input, entry);
  146. return 0;
  147. }
  148. /**
  149. * Insert a structured-append header to the head of the input data.
  150. * @param input input data.
  151. * @param size number of structured symbols.
  152. * @param index index number of the symbol. (1 <= index <= size)
  153. * @param parity parity among input data. (NOTE: each symbol of a set of structured symbols has the same parity data)
  154. * @retval 0 success.
  155. * @retval -1 error occurred and errno is set to indeicate the error. See Execptions for the details.
  156. * @throw EINVAL invalid parameter.
  157. * @throw ENOMEM unable to allocate memory.
  158. */
  159. //__STATIC
  160. static int QRinput_insertStructuredAppendHeader(QRinput *input, int size, int index, unsigned char parity)
  161. {
  162. QRinput_List *entry;
  163. unsigned char buf[3];
  164. if(size > MAX_STRUCTURED_SYMBOLS) {
  165. errno = EINVAL;
  166. return -1;
  167. }
  168. if(index <= 0 || index > MAX_STRUCTURED_SYMBOLS) {
  169. errno = EINVAL;
  170. return -1;
  171. }
  172. buf[0] = (unsigned char)size;
  173. buf[1] = (unsigned char)index;
  174. buf[2] = parity;
  175. entry = QRinput_List_newEntry(QR_MODE_STRUCTURE, 3, buf);
  176. if(entry == NULL) {
  177. return -1;
  178. }
  179. entry->next = input->head;
  180. input->head = entry;
  181. return 0;
  182. }
  183. void QRinput_free(QRinput *input)
  184. {
  185. QRinput_List *list, *next;
  186. if(input != NULL) {
  187. list = input->head;
  188. while(list != NULL) {
  189. next = list->next;
  190. QRinput_List_freeEntry(list);
  191. list = next;
  192. }
  193. free(input);
  194. }
  195. }
  196. static unsigned char QRinput_calcParity(QRinput *input)
  197. {
  198. unsigned char parity = 0;
  199. QRinput_List *list;
  200. int i;
  201. list = input->head;
  202. while(list != NULL) {
  203. if(list->mode != QR_MODE_STRUCTURE) {
  204. for(i=list->size-1; i>=0; i--) {
  205. parity ^= list->data[i];
  206. }
  207. }
  208. list = list->next;
  209. }
  210. return parity;
  211. }
  212. QRinput *QRinput_dup(QRinput *input)
  213. {
  214. QRinput *n;
  215. QRinput_List *list, *e;
  216. n = QRinput_new2(input->version, input->level);
  217. if(n == NULL) return NULL;
  218. list = input->head;
  219. while(list != NULL) {
  220. e = QRinput_List_dup(list);
  221. if(e == NULL) {
  222. QRinput_free(n);
  223. return NULL;
  224. }
  225. QRinput_appendEntry(n, e);
  226. list = list->next;
  227. }
  228. return n;
  229. }
  230. /******************************************************************************
  231. * Numeric data
  232. *****************************************************************************/
  233. /**
  234. * Check the input data.
  235. * @param size
  236. * @param data
  237. * @return result
  238. */
  239. static int QRinput_checkModeNum(int size, const char *data)
  240. {
  241. int i;
  242. for(i=0; i<size; i++) {
  243. if(data[i] < '0' || data[i] > '9')
  244. return -1;
  245. }
  246. return 0;
  247. }
  248. /**
  249. * Estimates the length of the encoded bit stream of numeric data.
  250. * @param size
  251. * @return number of bits
  252. */
  253. int QRinput_estimateBitsModeNum(int size)
  254. {
  255. int w;
  256. int bits;
  257. w = size / 3;
  258. bits = w * 10;
  259. switch(size - w * 3) {
  260. case 1:
  261. bits += 4;
  262. break;
  263. case 2:
  264. bits += 7;
  265. break;
  266. default:
  267. break;
  268. }
  269. return bits;
  270. }
  271. /**
  272. * Convert the number data to a bit stream.
  273. * @param entry
  274. * @retval 0 success
  275. * @retval -1 an error occurred and errno is set to indeicate the error.
  276. * See Execptions for the details.
  277. * @throw ENOMEM unable to allocate memory.
  278. */
  279. static int QRinput_encodeModeNum(QRinput_List *entry, int version)
  280. {
  281. int words, i, ret;
  282. unsigned int val;
  283. words = entry->size / 3;
  284. entry->bstream = BitStream_new();
  285. if(entry->bstream == NULL) return -1;
  286. val = 0x1;
  287. ret = BitStream_appendNum(entry->bstream, 4, val);
  288. if(ret < 0) goto ABORT;
  289. val = entry->size;
  290. ret = BitStream_appendNum(entry->bstream, QRspec_lengthIndicator(QR_MODE_NUM, version), val);
  291. if(ret < 0) goto ABORT;
  292. for(i=0; i<words; i++) {
  293. val = (entry->data[i*3 ] - '0') * 100;
  294. val += (entry->data[i*3+1] - '0') * 10;
  295. val += (entry->data[i*3+2] - '0');
  296. ret = BitStream_appendNum(entry->bstream, 10, val);
  297. if(ret < 0) goto ABORT;
  298. }
  299. if(entry->size - words * 3 == 1) {
  300. val = entry->data[words*3] - '0';
  301. ret = BitStream_appendNum(entry->bstream, 4, val);
  302. if(ret < 0) goto ABORT;
  303. } else if(entry->size - words * 3 == 2) {
  304. val = (entry->data[words*3 ] - '0') * 10;
  305. val += (entry->data[words*3+1] - '0');
  306. BitStream_appendNum(entry->bstream, 7, val);
  307. if(ret < 0) goto ABORT;
  308. }
  309. return 0;
  310. ABORT:
  311. BitStream_free(entry->bstream);
  312. entry->bstream = NULL;
  313. return -1;
  314. }
  315. /******************************************************************************
  316. * Alphabet-numeric data
  317. *****************************************************************************/
  318. const signed char QRinput_anTable[128] = {
  319. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  320. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  321. 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43,
  322. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1,
  323. -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  324. 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
  325. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  326. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  327. };
  328. /**
  329. * Check the input data.
  330. * @param size
  331. * @param data
  332. * @return result
  333. */
  334. static int QRinput_checkModeAn(int size, const char *data)
  335. {
  336. int i;
  337. for(i=0; i<size; i++) {
  338. if(QRinput_lookAnTable(data[i]) < 0)
  339. return -1;
  340. }
  341. return 0;
  342. }
  343. /**
  344. * Estimates the length of the encoded bit stream of alphabet-numeric data.
  345. * @param size
  346. * @return number of bits
  347. */
  348. int QRinput_estimateBitsModeAn(int size)
  349. {
  350. int w;
  351. int bits;
  352. w = size / 2;
  353. bits = w * 11;
  354. if(size & 1) {
  355. bits += 6;
  356. }
  357. return bits;
  358. }
  359. /**
  360. * Convert the alphabet-numeric data to a bit stream.
  361. * @param entry
  362. * @retval 0 success
  363. * @retval -1 an error occurred and errno is set to indeicate the error.
  364. * See Execptions for the details.
  365. * @throw ENOMEM unable to allocate memory.
  366. */
  367. static int QRinput_encodeModeAn(QRinput_List *entry, int version)
  368. {
  369. int words, i, ret;
  370. unsigned int val;
  371. words = entry->size / 2;
  372. entry->bstream = BitStream_new();
  373. if(entry->bstream == NULL) return -1;
  374. val = 0x2;
  375. ret = BitStream_appendNum(entry->bstream, 4, val);
  376. if(ret < 0) goto ABORT;
  377. val = entry->size;
  378. ret = BitStream_appendNum(entry->bstream, QRspec_lengthIndicator(QR_MODE_AN, version), val);
  379. if(ret < 0) goto ABORT;
  380. for(i=0; i<words; i++) {
  381. val = (unsigned int)QRinput_lookAnTable(entry->data[i*2 ]) * 45;
  382. val += (unsigned int)QRinput_lookAnTable(entry->data[i*2+1]);
  383. ret = BitStream_appendNum(entry->bstream, 11, val);
  384. if(ret < 0) goto ABORT;
  385. }
  386. if(entry->size & 1) {
  387. val = (unsigned int)QRinput_lookAnTable(entry->data[words * 2]);
  388. ret = BitStream_appendNum(entry->bstream, 6, val);
  389. if(ret < 0) goto ABORT;
  390. }
  391. return 0;
  392. ABORT:
  393. BitStream_free(entry->bstream);
  394. entry->bstream = NULL;
  395. return -1;
  396. }
  397. /******************************************************************************
  398. * 8 bit data
  399. *****************************************************************************/
  400. /**
  401. * Estimates the length of the encoded bit stream of 8 bit data.
  402. * @param size
  403. * @return number of bits
  404. */
  405. int QRinput_estimateBitsMode8(int size)
  406. {
  407. return size * 8;
  408. }
  409. /**
  410. * Convert the 8bits data to a bit stream.
  411. * @param entry
  412. * @retval 0 success
  413. * @retval -1 an error occurred and errno is set to indeicate the error.
  414. * See Execptions for the details.
  415. * @throw ENOMEM unable to allocate memory.
  416. */
  417. static int QRinput_encodeMode8(QRinput_List *entry, int version)
  418. {
  419. int ret, i;
  420. unsigned int val;
  421. entry->bstream = BitStream_new();
  422. if(entry->bstream == NULL) return -1;
  423. val = 0x4;
  424. ret = BitStream_appendNum(entry->bstream, 4, val);
  425. if(ret < 0) goto ABORT;
  426. val = entry->size;
  427. ret = BitStream_appendNum(entry->bstream, QRspec_lengthIndicator(QR_MODE_8, version), val);
  428. if(ret < 0) goto ABORT;
  429. for(i=0; i<entry->size; i++) {
  430. ret = BitStream_appendNum(entry->bstream, 8, entry->data[i]);
  431. if(ret < 0) goto ABORT;
  432. }
  433. return 0;
  434. ABORT:
  435. BitStream_free(entry->bstream);
  436. entry->bstream = NULL;
  437. return -1;
  438. }
  439. /******************************************************************************
  440. * Kanji data
  441. *****************************************************************************/
  442. /**
  443. * Estimates the length of the encoded bit stream of kanji data.
  444. * @param size
  445. * @return number of bits
  446. */
  447. int QRinput_estimateBitsModeKanji(int size)
  448. {
  449. return (size / 2) * 13;
  450. }
  451. /**
  452. * Check the input data.
  453. * @param size
  454. * @param data
  455. * @return result
  456. */
  457. static int QRinput_checkModeKanji(int size, const unsigned char *data)
  458. {
  459. int i;
  460. unsigned int val;
  461. if(size & 1)
  462. return -1;
  463. for(i=0; i<size; i+=2) {
  464. val = ((unsigned int)data[i] << 8) | data[i+1];
  465. if(val < 0x8140 || (val > 0x9ffc && val < 0xe040) || val > 0xebbf) {
  466. return -1;
  467. }
  468. }
  469. return 0;
  470. }
  471. /**
  472. * Convert the kanji data to a bit stream.
  473. * @param entry
  474. * @retval 0 success
  475. * @retval -1 an error occurred and errno is set to indeicate the error.
  476. * See Execptions for the details.
  477. * @throw ENOMEM unable to allocate memory.
  478. */
  479. static int QRinput_encodeModeKanji(QRinput_List *entry, int version)
  480. {
  481. int ret, i;
  482. unsigned int val, h;
  483. entry->bstream = BitStream_new();
  484. if(entry->bstream == NULL) return -1;
  485. val = 0x8;
  486. ret = BitStream_appendNum(entry->bstream, 4, val);
  487. if(ret < 0) goto ABORT;
  488. val = entry->size / 2;
  489. ret = BitStream_appendNum(entry->bstream, QRspec_lengthIndicator(QR_MODE_KANJI, version), val);
  490. if(ret < 0) goto ABORT;
  491. for(i=0; i<entry->size; i+=2) {
  492. val = ((unsigned int)entry->data[i] << 8) | entry->data[i+1];
  493. if(val <= 0x9ffc) {
  494. val -= 0x8140;
  495. } else {
  496. val -= 0xc140;
  497. }
  498. h = (val >> 8) * 0xc0;
  499. val = (val & 0xff) + h;
  500. ret = BitStream_appendNum(entry->bstream, 13, val);
  501. if(ret < 0) goto ABORT;
  502. }
  503. return 0;
  504. ABORT:
  505. BitStream_free(entry->bstream);
  506. entry->bstream = NULL;
  507. return -1;
  508. }
  509. /******************************************************************************
  510. * Structured Symbol
  511. *****************************************************************************/
  512. /**
  513. * Convert a structure symbol code to a bit stream.
  514. * @param entry
  515. * @retval 0 success
  516. * @retval -1 an error occurred and errno is set to indeicate the error.
  517. * See Execptions for the details.
  518. * @throw ENOMEM unable to allocate memory.
  519. */
  520. static int QRinput_encodeModeStructure(QRinput_List *entry)
  521. {
  522. int ret;
  523. entry->bstream = BitStream_new();
  524. if(entry->bstream == NULL) return -1;
  525. ret = BitStream_appendNum(entry->bstream, 4, 0x03);
  526. if(ret < 0) goto ABORT;
  527. ret = BitStream_appendNum(entry->bstream, 4, entry->data[1] - 1);
  528. if(ret < 0) goto ABORT;
  529. ret = BitStream_appendNum(entry->bstream, 4, entry->data[0] - 1);
  530. if(ret < 0) goto ABORT;
  531. ret = BitStream_appendNum(entry->bstream, 8, entry->data[2]);
  532. if(ret < 0) goto ABORT;
  533. return 0;
  534. ABORT:
  535. BitStream_free(entry->bstream);
  536. entry->bstream = NULL;
  537. return -1;
  538. }
  539. /******************************************************************************
  540. * Validation
  541. *****************************************************************************/
  542. int QRinput_check(QRencodeMode mode, int size, const unsigned char *data)
  543. {
  544. if(size <= 0) return -1;
  545. switch(mode) {
  546. case QR_MODE_NUM:
  547. return QRinput_checkModeNum(size, (const char *)data);
  548. break;
  549. case QR_MODE_AN:
  550. return QRinput_checkModeAn(size, (const char *)data);
  551. break;
  552. case QR_MODE_KANJI:
  553. return QRinput_checkModeKanji(size, data);
  554. break;
  555. case QR_MODE_8:
  556. return 0;
  557. break;
  558. case QR_MODE_STRUCTURE:
  559. return 0;
  560. break;
  561. default:
  562. break;
  563. }
  564. return -1;
  565. }
  566. /******************************************************************************
  567. * Estimation of the bit length
  568. *****************************************************************************/
  569. /**
  570. * Estimates the length of the encoded bit stream on the current version.
  571. * @param entry
  572. * @param version version of the symbol
  573. * @return number of bits
  574. */
  575. static int QRinput_estimateBitStreamSizeOfEntry(QRinput_List *entry, int version)
  576. {
  577. int bits = 0;
  578. int l, m;
  579. int num;
  580. if(version == 0) version = 1;
  581. switch(entry->mode) {
  582. case QR_MODE_NUM:
  583. bits = QRinput_estimateBitsModeNum(entry->size);
  584. break;
  585. case QR_MODE_AN:
  586. bits = QRinput_estimateBitsModeAn(entry->size);
  587. break;
  588. case QR_MODE_8:
  589. bits = QRinput_estimateBitsMode8(entry->size);
  590. break;
  591. case QR_MODE_KANJI:
  592. bits = QRinput_estimateBitsModeKanji(entry->size);
  593. break;
  594. case QR_MODE_STRUCTURE:
  595. return STRUCTURE_HEADER_BITS;
  596. default:
  597. return 0;
  598. }
  599. l = QRspec_lengthIndicator(entry->mode, version);
  600. m = 1 << l;
  601. num = (entry->size + m - 1) / m;
  602. bits += num * (4 + l); // mode indicator (4bits) + length indicator
  603. return bits;
  604. }
  605. /**
  606. * Estimates the length of the encoded bit stream of the data.
  607. * @param input input data
  608. * @param version version of the symbol
  609. * @return number of bits
  610. */
  611. //__STATIC
  612. static int QRinput_estimateBitStreamSize(QRinput *input, int version)
  613. {
  614. QRinput_List *list;
  615. int bits = 0;
  616. list = input->head;
  617. while(list != NULL) {
  618. bits += QRinput_estimateBitStreamSizeOfEntry(list, version);
  619. list = list->next;
  620. }
  621. return bits;
  622. }
  623. /**
  624. * Estimates the required version number of the symbol.
  625. * @param input input data
  626. * @return required version number
  627. */
  628. static int QRinput_estimateVersion(QRinput *input)
  629. {
  630. int bits;
  631. int version, prev;
  632. version = 0;
  633. do {
  634. prev = version;
  635. bits = QRinput_estimateBitStreamSize(input, prev);
  636. version = QRspec_getMinimumVersion((bits + 7) / 8, input->level);
  637. if (version < 0) {
  638. return -1;
  639. }
  640. } while (version > prev);
  641. return version;
  642. }
  643. /**
  644. * Returns required length in bytes for specified mode, version and bits.
  645. * @param mode
  646. * @param version
  647. * @param bits
  648. * @return required length of code words in bytes.
  649. */
  650. //__STATIC
  651. static int QRinput_lengthOfCode(QRencodeMode mode, int version, int bits)
  652. {
  653. int payload, size, chunks, remain, maxsize;
  654. payload = bits - 4 - QRspec_lengthIndicator(mode, version);
  655. switch(mode) {
  656. case QR_MODE_NUM:
  657. chunks = payload / 10;
  658. remain = payload - chunks * 10;
  659. size = chunks * 3;
  660. if(remain >= 7) {
  661. size += 2;
  662. } else if(remain >= 4) {
  663. size += 1;
  664. }
  665. break;
  666. case QR_MODE_AN:
  667. chunks = payload / 11;
  668. remain = payload - chunks * 11;
  669. size = chunks * 2;
  670. if(remain >= 6) size++;
  671. break;
  672. case QR_MODE_8:
  673. size = payload / 8;
  674. break;
  675. case QR_MODE_KANJI:
  676. size = (payload / 13) * 2;
  677. break;
  678. case QR_MODE_STRUCTURE:
  679. size = payload / 8;
  680. break;
  681. default:
  682. size = 0;
  683. break;
  684. }
  685. maxsize = QRspec_maximumWords(mode, version);
  686. if(size < 0) size = 0;
  687. if(size > maxsize) size = maxsize;
  688. return size;
  689. }
  690. /******************************************************************************
  691. * Data conversion
  692. *****************************************************************************/
  693. /**
  694. * Convert the input data in the data chunk to a bit stream.
  695. * @param entry
  696. * @return number of bits (>0) or -1 for failure.
  697. */
  698. static int QRinput_encodeBitStream(QRinput_List *entry, int version)
  699. {
  700. int words, ret;
  701. QRinput_List *st1 = NULL, *st2 = NULL;
  702. if(entry->bstream != NULL) {
  703. BitStream_free(entry->bstream);
  704. entry->bstream = NULL;
  705. }
  706. words = QRspec_maximumWords(entry->mode, version);
  707. if(entry->size > words) {
  708. st1 = QRinput_List_newEntry(entry->mode, words, entry->data);
  709. if(st1 == NULL) goto ABORT;
  710. st2 = QRinput_List_newEntry(entry->mode, entry->size - words, &entry->data[words]);
  711. if(st2 == NULL) goto ABORT;
  712. ret = QRinput_encodeBitStream(st1, version);
  713. if(ret < 0) goto ABORT;
  714. ret = QRinput_encodeBitStream(st2, version);
  715. if(ret < 0) goto ABORT;
  716. entry->bstream = BitStream_new();
  717. if(entry->bstream == NULL) goto ABORT;
  718. ret = BitStream_append(entry->bstream, st1->bstream);
  719. if(ret < 0) goto ABORT;
  720. ret = BitStream_append(entry->bstream, st2->bstream);
  721. if(ret < 0) goto ABORT;
  722. QRinput_List_freeEntry(st1);
  723. QRinput_List_freeEntry(st2);
  724. } else {
  725. ret = 0;
  726. switch(entry->mode) {
  727. case QR_MODE_NUM:
  728. ret = QRinput_encodeModeNum(entry, version);
  729. break;
  730. case QR_MODE_AN:
  731. ret = QRinput_encodeModeAn(entry, version);
  732. break;
  733. case QR_MODE_8:
  734. ret = QRinput_encodeMode8(entry, version);
  735. break;
  736. case QR_MODE_KANJI:
  737. ret = QRinput_encodeModeKanji(entry, version);
  738. break;
  739. case QR_MODE_STRUCTURE:
  740. ret = QRinput_encodeModeStructure(entry);
  741. break;
  742. default:
  743. break;
  744. }
  745. if(ret < 0) return -1;
  746. }
  747. return BitStream_size(entry->bstream);
  748. ABORT:
  749. QRinput_List_freeEntry(st1);
  750. QRinput_List_freeEntry(st2);
  751. return -1;
  752. }
  753. /**
  754. * Convert the input data to a bit stream.
  755. * @param input input data.
  756. * @retval 0 success
  757. * @retval -1 an error occurred and errno is set to indeicate the error.
  758. * See Execptions for the details.
  759. * @throw ENOMEM unable to allocate memory.
  760. */
  761. static int QRinput_createBitStream(QRinput *input)
  762. {
  763. QRinput_List *list;
  764. int bits, total = 0;
  765. list = input->head;
  766. while(list != NULL) {
  767. bits = QRinput_encodeBitStream(list, input->version);
  768. if(bits < 0) return -1;
  769. total += bits;
  770. list = list->next;
  771. }
  772. return total;
  773. }
  774. /**
  775. * Convert the input data to a bit stream.
  776. * When the version number is given and that is not sufficient, it is increased
  777. * automatically.
  778. * @param input input data.
  779. * @retval 0 success
  780. * @retval -1 an error occurred and errno is set to indeicate the error.
  781. * See Execptions for the details.
  782. * @throw ENOMEM unable to allocate memory.
  783. * @throw EINVAL input is too large.
  784. */
  785. static int QRinput_convertData(QRinput *input)
  786. {
  787. int bits;
  788. int ver;
  789. ver = QRinput_estimateVersion(input);
  790. if(ver > QRinput_getVersion(input)) {
  791. QRinput_setVersion(input, ver);
  792. }
  793. for(;;) {
  794. bits = QRinput_createBitStream(input);
  795. if(bits < 0) return -1;
  796. ver = QRspec_getMinimumVersion((bits + 7) / 8, input->level);
  797. if(ver < 0) {
  798. errno = EINVAL;
  799. return -1;
  800. } else if(ver > QRinput_getVersion(input)) {
  801. QRinput_setVersion(input, ver);
  802. } else {
  803. break;
  804. }
  805. }
  806. return 0;
  807. }
  808. /**
  809. * Append padding bits for the input data.
  810. * @param bstream Bitstream to be appended.
  811. * @param input input data.
  812. * @retval 0 success
  813. * @retval -1 an error occurred and errno is set to indeicate the error.
  814. * See Execptions for the details.
  815. * @throw ENOMEM unable to allocate memory.
  816. */
  817. static int QRinput_appendPaddingBit(BitStream *bstream, QRinput *input)
  818. {
  819. int bits, maxbits, words, maxwords, i, ret;
  820. BitStream *padding = NULL;
  821. unsigned char *padbuf;
  822. int padlen;
  823. bits = BitStream_size(bstream);
  824. maxwords = QRspec_getDataLength(input->version, input->level);
  825. maxbits = maxwords * 8;
  826. if(maxbits == bits) {
  827. return 0;
  828. }
  829. if(maxbits - bits < 5) {
  830. ret = BitStream_appendNum(bstream, maxbits - bits, 0);
  831. goto DONE;
  832. }
  833. bits += 4;
  834. words = (bits + 7) / 8;
  835. padding = BitStream_new();
  836. if(padding == NULL) return -1;
  837. ret = BitStream_appendNum(padding, words * 8 - bits + 4, 0);
  838. if(ret < 0) goto DONE;
  839. padlen = maxwords - words;
  840. if(padlen > 0) {
  841. padbuf = (unsigned char *)malloc(padlen);
  842. if(padbuf == NULL) {
  843. ret = -1;
  844. goto DONE;
  845. }
  846. for(i=0; i<padlen; i++) {
  847. padbuf[i] = (i&1)?0x11:0xec;
  848. }
  849. ret = BitStream_appendBytes(padding, padlen, padbuf);
  850. free(padbuf);
  851. if(ret < 0) {
  852. goto DONE;
  853. }
  854. }
  855. ret = BitStream_append(bstream, padding);
  856. DONE:
  857. BitStream_free(padding);
  858. return ret;
  859. }
  860. /**
  861. * Merge all bit streams in the input data.
  862. * @param input input data.
  863. * @return merged bit stream
  864. */
  865. //__STATIC
  866. static BitStream *QRinput_mergeBitStream(QRinput *input)
  867. {
  868. BitStream *bstream;
  869. QRinput_List *list;
  870. int ret;
  871. if(QRinput_convertData(input) < 0) {
  872. return NULL;
  873. }
  874. bstream = BitStream_new();
  875. if(bstream == NULL) return NULL;
  876. list = input->head;
  877. while(list != NULL) {
  878. ret = BitStream_append(bstream, list->bstream);
  879. if(ret < 0) {
  880. BitStream_free(bstream);
  881. return NULL;
  882. }
  883. list = list->next;
  884. }
  885. return bstream;
  886. }
  887. /**
  888. * Merge all bit streams in the input data and append padding bits
  889. * @param input input data.
  890. * @return padded merged bit stream
  891. */
  892. //__STATIC
  893. static BitStream *QRinput_getBitStream(QRinput *input)
  894. {
  895. BitStream *bstream;
  896. int ret;
  897. bstream = QRinput_mergeBitStream(input);
  898. if(bstream == NULL) {
  899. return NULL;
  900. }
  901. ret = QRinput_appendPaddingBit(bstream, input);
  902. if(ret < 0) {
  903. BitStream_free(bstream);
  904. return NULL;
  905. }
  906. return bstream;
  907. }
  908. /**
  909. * Pack all bit streams padding bits into a byte array.
  910. * @param input input data.
  911. * @return padded merged byte stream
  912. */
  913. unsigned char *QRinput_getByteStream(QRinput *input)
  914. {
  915. BitStream *bstream;
  916. unsigned char *array;
  917. bstream = QRinput_getBitStream(input);
  918. if(bstream == NULL) {
  919. return NULL;
  920. }
  921. array = BitStream_toByte(bstream);
  922. BitStream_free(bstream);
  923. return array;
  924. }
  925. /******************************************************************************
  926. * Structured input data
  927. *****************************************************************************/
  928. static QRinput_InputList *QRinput_InputList_newEntry(QRinput *input)
  929. {
  930. QRinput_InputList *entry;
  931. entry = (QRinput_InputList *)malloc(sizeof(QRinput_InputList));
  932. if(entry == NULL) return NULL;
  933. entry->input = input;
  934. entry->next = NULL;
  935. return entry;
  936. }
  937. static void QRinput_InputList_freeEntry(QRinput_InputList *entry)
  938. {
  939. if(entry != NULL) {
  940. QRinput_free(entry->input);
  941. free(entry);
  942. }
  943. }
  944. QRinput_Struct *QRinput_Struct_new(void)
  945. {
  946. QRinput_Struct *s;
  947. s = (QRinput_Struct *)malloc(sizeof(QRinput_Struct));
  948. if(s == NULL) return NULL;
  949. s->size = 0;
  950. s->parity = -1;
  951. s->head = NULL;
  952. s->tail = NULL;
  953. return s;
  954. }
  955. void QRinput_Struct_setParity(QRinput_Struct *s, unsigned char parity)
  956. {
  957. s->parity = (int)parity;
  958. }
  959. int QRinput_Struct_appendInput(QRinput_Struct *s, QRinput *input)
  960. {
  961. QRinput_InputList *e;
  962. e = QRinput_InputList_newEntry(input);
  963. if(e == NULL) return -1;
  964. s->size++;
  965. if(s->tail == NULL) {
  966. s->head = e;
  967. s->tail = e;
  968. } else {
  969. s->tail->next = e;
  970. s->tail = e;
  971. }
  972. return s->size;
  973. }
  974. void QRinput_Struct_free(QRinput_Struct *s)
  975. {
  976. QRinput_InputList *list, *next;
  977. if(s != NULL) {
  978. list = s->head;
  979. while(list != NULL) {
  980. next = list->next;
  981. QRinput_InputList_freeEntry(list);
  982. list = next;
  983. }
  984. free(s);
  985. }
  986. }
  987. static unsigned char QRinput_Struct_calcParity(QRinput_Struct *s)
  988. {
  989. QRinput_InputList *list;
  990. unsigned char parity = 0;
  991. list = s->head;
  992. while(list != NULL) {
  993. parity ^= QRinput_calcParity(list->input);
  994. list = list->next;
  995. }
  996. QRinput_Struct_setParity(s, parity);
  997. return parity;
  998. }
  999. static int QRinput_List_shrinkEntry(QRinput_List *entry, int bytes)
  1000. {
  1001. unsigned char *data;
  1002. data = (unsigned char *)malloc(bytes);
  1003. if(data == NULL) return -1;
  1004. memcpy(data, entry->data, bytes);
  1005. free(entry->data);
  1006. entry->data = data;
  1007. entry->size = bytes;
  1008. return 0;
  1009. }
  1010. //__STATIC
  1011. static int QRinput_splitEntry(QRinput_List *entry, int bytes)
  1012. {
  1013. QRinput_List *e;
  1014. int ret;
  1015. e = QRinput_List_newEntry(entry->mode, entry->size - bytes, entry->data + bytes);
  1016. if(e == NULL) {
  1017. return -1;
  1018. }
  1019. ret = QRinput_List_shrinkEntry(entry, bytes);
  1020. if(ret < 0) {
  1021. QRinput_List_freeEntry(e);
  1022. return -1;
  1023. }
  1024. e->next = entry->next;
  1025. entry->next = e;
  1026. return 0;
  1027. }
  1028. QRinput_Struct *QRinput_splitQRinputToStruct(QRinput *input)
  1029. {
  1030. QRinput *p;
  1031. QRinput_Struct *s;
  1032. int bits, maxbits, nextbits, bytes, ret;
  1033. QRinput_List *list, *next, *prev;
  1034. s = QRinput_Struct_new();
  1035. if(s == NULL) return NULL;
  1036. input = QRinput_dup(input);
  1037. if(input == NULL) {
  1038. QRinput_Struct_free(s);
  1039. return NULL;
  1040. }
  1041. QRinput_Struct_setParity(s, QRinput_calcParity(input));
  1042. maxbits = QRspec_getDataLength(input->version, input->level) * 8 - STRUCTURE_HEADER_BITS;
  1043. if(maxbits <= 0) {
  1044. QRinput_Struct_free(s);
  1045. QRinput_free(input);
  1046. return NULL;
  1047. }
  1048. bits = 0;
  1049. list = input->head;
  1050. prev = NULL;
  1051. while(list != NULL) {
  1052. nextbits = QRinput_estimateBitStreamSizeOfEntry(list, input->version);
  1053. if(bits + nextbits <= maxbits) {
  1054. ret = QRinput_encodeBitStream(list, input->version);
  1055. if(ret < 0) goto ABORT;
  1056. bits += ret;
  1057. prev = list;
  1058. list = list->next;
  1059. } else {
  1060. bytes = QRinput_lengthOfCode(list->mode, input->version, maxbits - bits);
  1061. if(bytes > 0) {
  1062. /* Splits this entry into 2 entries. */
  1063. ret = QRinput_splitEntry(list, bytes);
  1064. if(ret < 0) goto ABORT;
  1065. /* First half is the tail of the current input. */
  1066. next = list->next;
  1067. list->next = NULL;
  1068. /* Second half is the head of the next input, p.*/
  1069. p = QRinput_new2(input->version, input->level);
  1070. if(p == NULL) goto ABORT;
  1071. p->head = next;
  1072. /* Renew QRinput.tail. */
  1073. p->tail = input->tail;
  1074. input->tail = list;
  1075. /* Point to the next entry. */
  1076. prev = list;
  1077. list = next;
  1078. } else {
  1079. /* Current entry will go to the next input. */
  1080. prev->next = NULL;
  1081. p = QRinput_new2(input->version, input->level);
  1082. if(p == NULL) goto ABORT;
  1083. p->head = list;
  1084. p->tail = input->tail;
  1085. input->tail = prev;
  1086. }
  1087. ret = QRinput_Struct_appendInput(s, input);
  1088. if(ret < 0) goto ABORT;
  1089. input = p;
  1090. bits = 0;
  1091. }
  1092. }
  1093. QRinput_Struct_appendInput(s, input);
  1094. if(s->size > MAX_STRUCTURED_SYMBOLS) {
  1095. QRinput_Struct_free(s);
  1096. errno = ERANGE;
  1097. return NULL;
  1098. }
  1099. ret = QRinput_Struct_insertStructuredAppendHeaders(s);
  1100. if(ret < 0) {
  1101. QRinput_Struct_free(s);
  1102. return NULL;
  1103. }
  1104. return s;
  1105. ABORT:
  1106. QRinput_free(input);
  1107. QRinput_Struct_free(s);
  1108. return NULL;
  1109. }
  1110. int QRinput_Struct_insertStructuredAppendHeaders(QRinput_Struct *s)
  1111. {
  1112. int num, i;
  1113. QRinput_InputList *list;
  1114. if(s->parity < 0) {
  1115. QRinput_Struct_calcParity(s);
  1116. }
  1117. num = 0;
  1118. list = s->head;
  1119. while(list != NULL) {
  1120. num++;
  1121. list = list->next;
  1122. }
  1123. i = 1;
  1124. list = s->head;
  1125. while(list != NULL) {
  1126. if(QRinput_insertStructuredAppendHeader(list->input, num, i, s->parity))
  1127. return -1;
  1128. i++;
  1129. list = list->next;
  1130. }
  1131. return 0;
  1132. }