qrencode.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /*
  2. * qrencode - QR Code encoder
  3. *
  4. * Copyright (C) 2006-2014 Kentaro Fukuchi <[email protected]>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #if HAVE_CONFIG_H
  21. # include "config.h"
  22. #endif
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. #include "qrencode.h"
  28. #include "qrspec.h"
  29. #include "mqrspec.h"
  30. #include "bitstream.h"
  31. #include "qrinput.h"
  32. #include "rsecc.h"
  33. #include "split.h"
  34. #include "mask.h"
  35. #include "mmask.h"
  36. /******************************************************************************
  37. * Raw code
  38. *****************************************************************************/
  39. typedef struct {
  40. int dataLength;
  41. unsigned char *data;
  42. int eccLength;
  43. unsigned char *ecc;
  44. } RSblock;
  45. typedef struct {
  46. int version;
  47. int dataLength;
  48. int eccLength;
  49. unsigned char *datacode;
  50. unsigned char *ecccode;
  51. int b1;
  52. int blocks;
  53. RSblock *rsblock;
  54. int count;
  55. } QRRawCode;
  56. static void RSblock_initBlock(RSblock *block, int dl, unsigned char *data, int el, unsigned char *ecc)
  57. {
  58. block->dataLength = dl;
  59. block->data = data;
  60. block->eccLength = el;
  61. block->ecc = ecc;
  62. RSECC_encode(dl, el, data, ecc);
  63. }
  64. static int RSblock_init(RSblock *blocks, int spec[5], unsigned char *data, unsigned char *ecc)
  65. {
  66. int i;
  67. RSblock *block;
  68. unsigned char *dp, *ep;
  69. int el, dl;
  70. dl = QRspec_rsDataCodes1(spec);
  71. el = QRspec_rsEccCodes1(spec);
  72. block = blocks;
  73. dp = data;
  74. ep = ecc;
  75. for(i=0; i<QRspec_rsBlockNum1(spec); i++) {
  76. RSblock_initBlock(block, dl, dp, el, ep);
  77. dp += dl;
  78. ep += el;
  79. block++;
  80. }
  81. if(QRspec_rsBlockNum2(spec) == 0) return 0;
  82. dl = QRspec_rsDataCodes2(spec);
  83. el = QRspec_rsEccCodes2(spec);
  84. for(i=0; i<QRspec_rsBlockNum2(spec); i++) {
  85. RSblock_initBlock(block, dl, dp, el, ep);
  86. dp += dl;
  87. ep += el;
  88. block++;
  89. }
  90. return 0;
  91. }
  92. __STATIC void QRraw_free(QRRawCode *raw);
  93. __STATIC QRRawCode *QRraw_new(QRinput *input)
  94. {
  95. QRRawCode *raw;
  96. int spec[5], ret;
  97. raw = (QRRawCode *)malloc(sizeof(QRRawCode));
  98. if(raw == NULL) return NULL;
  99. raw->datacode = QRinput_getByteStream(input);
  100. if(raw->datacode == NULL) {
  101. free(raw);
  102. return NULL;
  103. }
  104. QRspec_getEccSpec(input->version, input->level, spec);
  105. raw->version = input->version;
  106. raw->b1 = QRspec_rsBlockNum1(spec);
  107. raw->dataLength = QRspec_rsDataLength(spec);
  108. raw->eccLength = QRspec_rsEccLength(spec);
  109. raw->ecccode = (unsigned char *)malloc(raw->eccLength);
  110. if(raw->ecccode == NULL) {
  111. free(raw->datacode);
  112. free(raw);
  113. return NULL;
  114. }
  115. raw->blocks = QRspec_rsBlockNum(spec);
  116. raw->rsblock = (RSblock *)calloc(raw->blocks, sizeof(RSblock));
  117. if(raw->rsblock == NULL) {
  118. QRraw_free(raw);
  119. return NULL;
  120. }
  121. ret = RSblock_init(raw->rsblock, spec, raw->datacode, raw->ecccode);
  122. if(ret < 0) {
  123. QRraw_free(raw);
  124. return NULL;
  125. }
  126. raw->count = 0;
  127. return raw;
  128. }
  129. /**
  130. * Return a code (byte).
  131. * This function can be called iteratively.
  132. * @param raw raw code.
  133. * @return code
  134. */
  135. __STATIC unsigned char QRraw_getCode(QRRawCode *raw)
  136. {
  137. int col, row;
  138. unsigned char ret;
  139. if(raw->count < raw->dataLength) {
  140. row = raw->count % raw->blocks;
  141. col = raw->count / raw->blocks;
  142. if(col >= raw->rsblock[0].dataLength) {
  143. row += raw->b1;
  144. }
  145. ret = raw->rsblock[row].data[col];
  146. } else if(raw->count < raw->dataLength + raw->eccLength) {
  147. row = (raw->count - raw->dataLength) % raw->blocks;
  148. col = (raw->count - raw->dataLength) / raw->blocks;
  149. ret = raw->rsblock[row].ecc[col];
  150. } else {
  151. return 0;
  152. }
  153. raw->count++;
  154. return ret;
  155. }
  156. __STATIC void QRraw_free(QRRawCode *raw)
  157. {
  158. if(raw != NULL) {
  159. free(raw->datacode);
  160. free(raw->ecccode);
  161. free(raw->rsblock);
  162. free(raw);
  163. }
  164. }
  165. /******************************************************************************
  166. * Raw code for Micro QR Code
  167. *****************************************************************************/
  168. typedef struct {
  169. int version;
  170. int dataLength;
  171. int eccLength;
  172. unsigned char *datacode;
  173. unsigned char *ecccode;
  174. RSblock *rsblock;
  175. int oddbits;
  176. int count;
  177. } MQRRawCode;
  178. __STATIC void MQRraw_free(MQRRawCode *raw);
  179. __STATIC MQRRawCode *MQRraw_new(QRinput *input)
  180. {
  181. MQRRawCode *raw;
  182. raw = (MQRRawCode *)malloc(sizeof(MQRRawCode));
  183. if(raw == NULL) return NULL;
  184. raw->version = input->version;
  185. raw->dataLength = MQRspec_getDataLength(input->version, input->level);
  186. raw->eccLength = MQRspec_getECCLength(input->version, input->level);
  187. raw->oddbits = raw->dataLength * 8 - MQRspec_getDataLengthBit(input->version, input->level);
  188. raw->datacode = QRinput_getByteStream(input);
  189. if(raw->datacode == NULL) {
  190. free(raw);
  191. return NULL;
  192. }
  193. raw->ecccode = (unsigned char *)malloc(raw->eccLength);
  194. if(raw->ecccode == NULL) {
  195. free(raw->datacode);
  196. free(raw);
  197. return NULL;
  198. }
  199. raw->rsblock = (RSblock *)calloc(1, sizeof(RSblock));
  200. if(raw->rsblock == NULL) {
  201. MQRraw_free(raw);
  202. return NULL;
  203. }
  204. RSblock_initBlock(raw->rsblock, raw->dataLength, raw->datacode, raw->eccLength, raw->ecccode);
  205. raw->count = 0;
  206. return raw;
  207. }
  208. /**
  209. * Return a code (byte).
  210. * This function can be called iteratively.
  211. * @param raw raw code.
  212. * @return code
  213. */
  214. __STATIC unsigned char MQRraw_getCode(MQRRawCode *raw)
  215. {
  216. unsigned char ret;
  217. if(raw->count < raw->dataLength) {
  218. ret = raw->datacode[raw->count];
  219. } else if(raw->count < raw->dataLength + raw->eccLength) {
  220. ret = raw->ecccode[raw->count - raw->dataLength];
  221. } else {
  222. return 0;
  223. }
  224. raw->count++;
  225. return ret;
  226. }
  227. __STATIC void MQRraw_free(MQRRawCode *raw)
  228. {
  229. if(raw != NULL) {
  230. free(raw->datacode);
  231. free(raw->ecccode);
  232. free(raw->rsblock);
  233. free(raw);
  234. }
  235. }
  236. /******************************************************************************
  237. * Frame filling
  238. *****************************************************************************/
  239. typedef struct {
  240. int width;
  241. unsigned char *frame;
  242. int x, y;
  243. int dir;
  244. int bit;
  245. int mqr;
  246. } FrameFiller;
  247. static void FrameFiller_set(FrameFiller *filler, int width, unsigned char *frame, int mqr)
  248. {
  249. filler->width = width;
  250. filler->frame = frame;
  251. filler->x = width - 1;
  252. filler->y = width - 1;
  253. filler->dir = -1;
  254. filler->bit = -1;
  255. filler->mqr = mqr;
  256. }
  257. static unsigned char *FrameFiller_next(FrameFiller *filler)
  258. {
  259. unsigned char *p;
  260. int x, y, w;
  261. if(filler->bit == -1) {
  262. filler->bit = 0;
  263. return filler->frame + filler->y * filler->width + filler->x;
  264. }
  265. x = filler->x;
  266. y = filler->y;
  267. p = filler->frame;
  268. w = filler->width;
  269. if(filler->bit == 0) {
  270. x--;
  271. filler->bit++;
  272. } else {
  273. x++;
  274. y += filler->dir;
  275. filler->bit--;
  276. }
  277. if(filler->dir < 0) {
  278. if(y < 0) {
  279. y = 0;
  280. x -= 2;
  281. filler->dir = 1;
  282. if(!filler->mqr && x == 6) {
  283. x--;
  284. y = 9;
  285. }
  286. }
  287. } else {
  288. if(y == w) {
  289. y = w - 1;
  290. x -= 2;
  291. filler->dir = -1;
  292. if(!filler->mqr && x == 6) {
  293. x--;
  294. y -= 8;
  295. }
  296. }
  297. }
  298. if(x < 0 || y < 0) return NULL;
  299. filler->x = x;
  300. filler->y = y;
  301. if(p[y * w + x] & 0x80) {
  302. // This tail recursion could be optimized.
  303. return FrameFiller_next(filler);
  304. }
  305. return &p[y * w + x];
  306. }
  307. #ifdef WITH_TESTS
  308. extern unsigned char *FrameFiller_test(int version)
  309. {
  310. int width;
  311. unsigned char *frame, *p;
  312. int i, length;
  313. FrameFiller filler;
  314. width = QRspec_getWidth(version);
  315. frame = QRspec_newFrame(version);
  316. if(frame == NULL) return NULL;
  317. FrameFiller_set(&filler, width, frame, 0);
  318. length = QRspec_getDataLength(version, QR_ECLEVEL_L) * 8
  319. + QRspec_getECCLength(version, QR_ECLEVEL_L) * 8
  320. + QRspec_getRemainder(version);
  321. for(i=0; i<length; i++) {
  322. p = FrameFiller_next(&filler);
  323. if(p == NULL) {
  324. free(frame);
  325. return NULL;
  326. }
  327. *p = (unsigned char)(i & 0x7f) | 0x80;
  328. }
  329. return frame;
  330. }
  331. extern unsigned char *FrameFiller_testMQR(int version)
  332. {
  333. int width;
  334. unsigned char *frame, *p;
  335. int i, length;
  336. FrameFiller filler;
  337. width = MQRspec_getWidth(version);
  338. frame = MQRspec_newFrame(version);
  339. if(frame == NULL) return NULL;
  340. FrameFiller_set(&filler, width, frame, 1);
  341. length = MQRspec_getDataLengthBit(version, QR_ECLEVEL_L)
  342. + MQRspec_getECCLength(version, QR_ECLEVEL_L) * 8;
  343. for(i=0; i<length; i++) {
  344. p = FrameFiller_next(&filler);
  345. if(p == NULL) {
  346. fprintf(stderr, "Frame filler run over the frame!\n");
  347. return frame;
  348. }
  349. *p = (unsigned char)(i & 0x7f) | 0x80;
  350. }
  351. return frame;
  352. }
  353. #endif
  354. /******************************************************************************
  355. * QR-code encoding
  356. *****************************************************************************/
  357. __STATIC QRcode *QRcode_new(int version, int width, unsigned char *data)
  358. {
  359. QRcode *qrcode;
  360. qrcode = (QRcode *)malloc(sizeof(QRcode));
  361. if(qrcode == NULL) return NULL;
  362. qrcode->version = version;
  363. qrcode->width = width;
  364. qrcode->data = data;
  365. return qrcode;
  366. }
  367. void QRcode_free(QRcode *qrcode)
  368. {
  369. if(qrcode != NULL) {
  370. free(qrcode->data);
  371. free(qrcode);
  372. }
  373. }
  374. __STATIC QRcode *QRcode_encodeMask(QRinput *input, int mask)
  375. {
  376. int width, version;
  377. QRRawCode *raw;
  378. unsigned char *frame, *masked, *p, code, bit;
  379. int i, j;
  380. QRcode *qrcode = NULL;
  381. FrameFiller filler;
  382. if(input->mqr) {
  383. errno = EINVAL;
  384. return NULL;
  385. }
  386. if(input->version < 0 || input->version > QRSPEC_VERSION_MAX) {
  387. errno = EINVAL;
  388. return NULL;
  389. }
  390. if(input->level > QR_ECLEVEL_H) {
  391. errno = EINVAL;
  392. return NULL;
  393. }
  394. raw = QRraw_new(input);
  395. if(raw == NULL) return NULL;
  396. version = raw->version;
  397. width = QRspec_getWidth(version);
  398. frame = QRspec_newFrame(version);
  399. if(frame == NULL) {
  400. QRraw_free(raw);
  401. return NULL;
  402. }
  403. FrameFiller_set(&filler, width, frame, 0);
  404. /* inteleaved data and ecc codes */
  405. for(i=0; i<raw->dataLength + raw->eccLength; i++) {
  406. code = QRraw_getCode(raw);
  407. bit = 0x80;
  408. for(j=0; j<8; j++) {
  409. p = FrameFiller_next(&filler);
  410. if(p == NULL) goto EXIT;
  411. *p = 0x02 | ((bit & code) != 0);
  412. bit = bit >> 1;
  413. }
  414. }
  415. QRraw_free(raw);
  416. raw = NULL;
  417. /* remainder bits */
  418. j = QRspec_getRemainder(version);
  419. for(i=0; i<j; i++) {
  420. p = FrameFiller_next(&filler);
  421. if(p == NULL) goto EXIT;
  422. *p = 0x02;
  423. }
  424. /* masking */
  425. if(mask == -2) { // just for debug purpose
  426. masked = (unsigned char *)malloc(width * width);
  427. memcpy(masked, frame, width * width);
  428. } else if(mask < 0) {
  429. masked = Mask_mask(width, frame, input->level);
  430. } else {
  431. masked = Mask_makeMask(width, frame, mask, input->level);
  432. }
  433. if(masked == NULL) {
  434. goto EXIT;
  435. }
  436. qrcode = QRcode_new(version, width, masked);
  437. if(qrcode == NULL) {
  438. free(masked);
  439. }
  440. EXIT:
  441. QRraw_free(raw);
  442. free(frame);
  443. return qrcode;
  444. }
  445. __STATIC QRcode *QRcode_encodeMaskMQR(QRinput *input, int mask)
  446. {
  447. int width, version;
  448. MQRRawCode *raw;
  449. unsigned char *frame, *masked, *p, code, bit;
  450. int i, j;
  451. QRcode *qrcode = NULL;
  452. FrameFiller filler;
  453. if(!input->mqr) {
  454. errno = EINVAL;
  455. return NULL;
  456. }
  457. if(input->version <= 0 || input->version > MQRSPEC_VERSION_MAX) {
  458. errno = EINVAL;
  459. return NULL;
  460. }
  461. if(input->level > QR_ECLEVEL_Q) {
  462. errno = EINVAL;
  463. return NULL;
  464. }
  465. raw = MQRraw_new(input);
  466. if(raw == NULL) return NULL;
  467. version = raw->version;
  468. width = MQRspec_getWidth(version);
  469. frame = MQRspec_newFrame(version);
  470. if(frame == NULL) {
  471. MQRraw_free(raw);
  472. return NULL;
  473. }
  474. FrameFiller_set(&filler, width, frame, 1);
  475. /* inteleaved data and ecc codes */
  476. for(i=0; i<raw->dataLength + raw->eccLength; i++) {
  477. code = MQRraw_getCode(raw);
  478. if(raw->oddbits && i == raw->dataLength - 1) {
  479. bit = 1 << (raw->oddbits - 1);
  480. for(j=0; j<raw->oddbits; j++) {
  481. p = FrameFiller_next(&filler);
  482. if(p == NULL) goto EXIT;
  483. *p = 0x02 | ((bit & code) != 0);
  484. bit = bit >> 1;
  485. }
  486. } else {
  487. bit = 0x80;
  488. for(j=0; j<8; j++) {
  489. p = FrameFiller_next(&filler);
  490. if(p == NULL) goto EXIT;
  491. *p = 0x02 | ((bit & code) != 0);
  492. bit = bit >> 1;
  493. }
  494. }
  495. }
  496. MQRraw_free(raw);
  497. raw = NULL;
  498. /* masking */
  499. if(mask < 0) {
  500. masked = MMask_mask(version, frame, input->level);
  501. } else {
  502. masked = MMask_makeMask(version, frame, mask, input->level);
  503. }
  504. if(masked == NULL) {
  505. goto EXIT;
  506. }
  507. qrcode = QRcode_new(version, width, masked);
  508. EXIT:
  509. MQRraw_free(raw);
  510. free(frame);
  511. return qrcode;
  512. }
  513. QRcode *QRcode_encodeInput(QRinput *input)
  514. {
  515. if(input->mqr) {
  516. return QRcode_encodeMaskMQR(input, -1);
  517. } else {
  518. return QRcode_encodeMask(input, -1);
  519. }
  520. }
  521. static QRcode *QRcode_encodeStringReal(const char *string, int version, QRecLevel level, int mqr, QRencodeMode hint, int casesensitive)
  522. {
  523. QRinput *input;
  524. QRcode *code;
  525. int ret;
  526. if(string == NULL) {
  527. errno = EINVAL;
  528. return NULL;
  529. }
  530. if(hint != QR_MODE_8 && hint != QR_MODE_KANJI) {
  531. errno = EINVAL;
  532. return NULL;
  533. }
  534. if(mqr) {
  535. input = QRinput_newMQR(version, level);
  536. } else {
  537. input = QRinput_new2(version, level);
  538. }
  539. if(input == NULL) return NULL;
  540. ret = Split_splitStringToQRinput(string, input, hint, casesensitive);
  541. if(ret < 0) {
  542. QRinput_free(input);
  543. return NULL;
  544. }
  545. code = QRcode_encodeInput(input);
  546. QRinput_free(input);
  547. return code;
  548. }
  549. QRcode *QRcode_encodeString(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
  550. {
  551. return QRcode_encodeStringReal(string, version, level, 0, hint, casesensitive);
  552. }
  553. QRcode *QRcode_encodeStringMQR(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
  554. {
  555. return QRcode_encodeStringReal(string, version, level, 1, hint, casesensitive);
  556. }
  557. static QRcode *QRcode_encodeDataReal(const unsigned char *data, int length, int version, QRecLevel level, int mqr)
  558. {
  559. QRinput *input;
  560. QRcode *code;
  561. int ret;
  562. if(data == NULL || length == 0) {
  563. errno = EINVAL;
  564. return NULL;
  565. }
  566. if(mqr) {
  567. input = QRinput_newMQR(version, level);
  568. } else {
  569. input = QRinput_new2(version, level);
  570. }
  571. if(input == NULL) return NULL;
  572. ret = QRinput_append(input, QR_MODE_8, length, data);
  573. if(ret < 0) {
  574. QRinput_free(input);
  575. return NULL;
  576. }
  577. code = QRcode_encodeInput(input);
  578. QRinput_free(input);
  579. return code;
  580. }
  581. QRcode *QRcode_encodeData(int size, const unsigned char *data, int version, QRecLevel level)
  582. {
  583. return QRcode_encodeDataReal(data, size, version, level, 0);
  584. }
  585. QRcode *QRcode_encodeString8bit(const char *string, int version, QRecLevel level)
  586. {
  587. if(string == NULL) {
  588. errno = EINVAL;
  589. return NULL;
  590. }
  591. return QRcode_encodeDataReal((unsigned char *)string, strlen(string), version, level, 0);
  592. }
  593. QRcode *QRcode_encodeDataMQR(int size, const unsigned char *data, int version, QRecLevel level)
  594. {
  595. return QRcode_encodeDataReal(data, size, version, level, 1);
  596. }
  597. QRcode *QRcode_encodeString8bitMQR(const char *string, int version, QRecLevel level)
  598. {
  599. if(string == NULL) {
  600. errno = EINVAL;
  601. return NULL;
  602. }
  603. return QRcode_encodeDataReal((unsigned char *)string, strlen(string), version, level, 1);
  604. }
  605. /******************************************************************************
  606. * Structured QR-code encoding
  607. *****************************************************************************/
  608. static QRcode_List *QRcode_List_newEntry(void)
  609. {
  610. QRcode_List *entry;
  611. entry = (QRcode_List *)malloc(sizeof(QRcode_List));
  612. if(entry == NULL) return NULL;
  613. entry->next = NULL;
  614. entry->code = NULL;
  615. return entry;
  616. }
  617. static void QRcode_List_freeEntry(QRcode_List *entry)
  618. {
  619. if(entry != NULL) {
  620. QRcode_free(entry->code);
  621. free(entry);
  622. }
  623. }
  624. void QRcode_List_free(QRcode_List *qrlist)
  625. {
  626. QRcode_List *list = qrlist, *next;
  627. while(list != NULL) {
  628. next = list->next;
  629. QRcode_List_freeEntry(list);
  630. list = next;
  631. }
  632. }
  633. int QRcode_List_size(QRcode_List *qrlist)
  634. {
  635. QRcode_List *list = qrlist;
  636. int size = 0;
  637. while(list != NULL) {
  638. size++;
  639. list = list->next;
  640. }
  641. return size;
  642. }
  643. #if 0
  644. static unsigned char QRcode_parity(const char *str, int size)
  645. {
  646. unsigned char parity = 0;
  647. int i;
  648. for(i=0; i<size; i++) {
  649. parity ^= str[i];
  650. }
  651. return parity;
  652. }
  653. #endif
  654. QRcode_List *QRcode_encodeInputStructured(QRinput_Struct *s)
  655. {
  656. QRcode_List *head = NULL;
  657. QRcode_List *tail = NULL;
  658. QRcode_List *entry;
  659. QRinput_InputList *list = s->head;
  660. while(list != NULL) {
  661. if(head == NULL) {
  662. entry = QRcode_List_newEntry();
  663. if(entry == NULL) goto ABORT;
  664. head = entry;
  665. tail = head;
  666. } else {
  667. entry = QRcode_List_newEntry();
  668. if(entry == NULL) goto ABORT;
  669. tail->next = entry;
  670. tail = tail->next;
  671. }
  672. tail->code = QRcode_encodeInput(list->input);
  673. if(tail->code == NULL) {
  674. goto ABORT;
  675. }
  676. list = list->next;
  677. }
  678. return head;
  679. ABORT:
  680. QRcode_List_free(head);
  681. return NULL;
  682. }
  683. static QRcode_List *QRcode_encodeInputToStructured(QRinput *input)
  684. {
  685. QRinput_Struct *s;
  686. QRcode_List *codes;
  687. s = QRinput_splitQRinputToStruct(input);
  688. if(s == NULL) return NULL;
  689. codes = QRcode_encodeInputStructured(s);
  690. QRinput_Struct_free(s);
  691. return codes;
  692. }
  693. static QRcode_List *QRcode_encodeDataStructuredReal(
  694. int size, const unsigned char *data,
  695. int version, QRecLevel level,
  696. int eightbit, QRencodeMode hint, int casesensitive)
  697. {
  698. QRinput *input;
  699. QRcode_List *codes;
  700. int ret;
  701. if(version <= 0) {
  702. errno = EINVAL;
  703. return NULL;
  704. }
  705. if(!eightbit && (hint != QR_MODE_8 && hint != QR_MODE_KANJI)) {
  706. errno = EINVAL;
  707. return NULL;
  708. }
  709. input = QRinput_new2(version, level);
  710. if(input == NULL) return NULL;
  711. if(eightbit) {
  712. ret = QRinput_append(input, QR_MODE_8, size, data);
  713. } else {
  714. ret = Split_splitStringToQRinput((char *)data, input, hint, casesensitive);
  715. }
  716. if(ret < 0) {
  717. QRinput_free(input);
  718. return NULL;
  719. }
  720. codes = QRcode_encodeInputToStructured(input);
  721. QRinput_free(input);
  722. return codes;
  723. }
  724. QRcode_List *QRcode_encodeDataStructured(int size, const unsigned char *data, int version, QRecLevel level) {
  725. return QRcode_encodeDataStructuredReal(size, data, version, level, 1, QR_MODE_NUL, 0);
  726. }
  727. QRcode_List *QRcode_encodeString8bitStructured(const char *string, int version, QRecLevel level) {
  728. if(string == NULL) {
  729. errno = EINVAL;
  730. return NULL;
  731. }
  732. return QRcode_encodeDataStructured(strlen(string), (unsigned char *)string, version, level);
  733. }
  734. QRcode_List *QRcode_encodeStringStructured(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
  735. {
  736. if(string == NULL) {
  737. errno = EINVAL;
  738. return NULL;
  739. }
  740. return QRcode_encodeDataStructuredReal(strlen(string), (unsigned char *)string, version, level, 0, hint, casesensitive);
  741. }
  742. /******************************************************************************
  743. * System utilities
  744. *****************************************************************************/
  745. void QRcode_APIVersion(int *major_version, int *minor_version, int *micro_version)
  746. {
  747. if(major_version != NULL) {
  748. *major_version = MAJOR_VERSION;
  749. }
  750. if(minor_version != NULL) {
  751. *minor_version = MINOR_VERSION;
  752. }
  753. if(micro_version != NULL) {
  754. *micro_version = MICRO_VERSION;
  755. }
  756. }
  757. char *QRcode_APIVersionString(void)
  758. {
  759. return VERSION;
  760. }