qrencode.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. /*
  2. * qrencode - QR Code encoder
  3. *
  4. * Copyright (C) 2006-2017 Kentaro Fukuchi <kentaro@fukuchi.org>
  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. int eccLength;
  42. unsigned char *data;
  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((size_t)dl, (size_t)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_IN_RELEASE void QRraw_free(QRRawCode *raw);
  93. STATIC_IN_RELEASE 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((size_t)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((size_t)(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_IN_RELEASE 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_IN_RELEASE 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_IN_RELEASE void MQRraw_free(MQRRawCode *raw);
  179. STATIC_IN_RELEASE 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((size_t)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_IN_RELEASE 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_IN_RELEASE 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 if(y == w) {
  288. y = w - 1;
  289. x -= 2;
  290. filler->dir = -1;
  291. if(!filler->mqr && x == 6) {
  292. x--;
  293. y -= 8;
  294. }
  295. }
  296. if(x < 0 || y < 0) return NULL;
  297. filler->x = x;
  298. filler->y = y;
  299. if(p[y * w + x] & 0x80) {
  300. // This tail recursion could be optimized.
  301. return FrameFiller_next(filler);
  302. }
  303. return &p[y * w + x];
  304. }
  305. #ifdef WITH_TESTS
  306. unsigned char *FrameFiller_test(int version)
  307. {
  308. int width;
  309. unsigned char *frame, *p;
  310. int i, length;
  311. FrameFiller filler;
  312. width = QRspec_getWidth(version);
  313. frame = QRspec_newFrame(version);
  314. if(frame == NULL) return NULL;
  315. FrameFiller_set(&filler, width, frame, 0);
  316. length = QRspec_getDataLength(version, QR_ECLEVEL_L) * 8
  317. + QRspec_getECCLength(version, QR_ECLEVEL_L) * 8
  318. + QRspec_getRemainder(version);
  319. for(i = 0; i < length; i++) {
  320. p = FrameFiller_next(&filler);
  321. if(p == NULL) {
  322. free(frame);
  323. return NULL;
  324. }
  325. *p = (unsigned char)(i & 0x7f) | 0x80;
  326. }
  327. return frame;
  328. }
  329. unsigned char *FrameFiller_testMQR(int version)
  330. {
  331. int width;
  332. unsigned char *frame, *p;
  333. int i, length;
  334. FrameFiller filler;
  335. width = MQRspec_getWidth(version);
  336. frame = MQRspec_newFrame(version);
  337. if(frame == NULL) return NULL;
  338. FrameFiller_set(&filler, width, frame, 1);
  339. length = MQRspec_getDataLengthBit(version, QR_ECLEVEL_L)
  340. + MQRspec_getECCLength(version, QR_ECLEVEL_L) * 8;
  341. for(i = 0; i < length; i++) {
  342. p = FrameFiller_next(&filler);
  343. if(p == NULL) {
  344. fprintf(stderr, "Frame filler run over the frame!\n");
  345. return frame;
  346. }
  347. *p = (unsigned char)(i & 0x7f) | 0x80;
  348. }
  349. return frame;
  350. }
  351. #endif
  352. /******************************************************************************
  353. * QR-code encoding
  354. *****************************************************************************/
  355. STATIC_IN_RELEASE QRcode *QRcode_new(int version, int width, unsigned char *data)
  356. {
  357. QRcode *qrcode;
  358. qrcode = (QRcode *)malloc(sizeof(QRcode));
  359. if(qrcode == NULL) return NULL;
  360. qrcode->version = version;
  361. qrcode->width = width;
  362. qrcode->data = data;
  363. return qrcode;
  364. }
  365. void QRcode_free(QRcode *qrcode)
  366. {
  367. if(qrcode != NULL) {
  368. free(qrcode->data);
  369. free(qrcode);
  370. }
  371. }
  372. STATIC_IN_RELEASE QRcode *QRcode_encodeMask(QRinput *input, int mask)
  373. {
  374. int width, version;
  375. QRRawCode *raw;
  376. unsigned char *frame, *masked, *p, code, bit;
  377. int i, j;
  378. QRcode *qrcode = NULL;
  379. FrameFiller filler;
  380. if(input->mqr) {
  381. errno = EINVAL;
  382. return NULL;
  383. }
  384. if(input->version < 0 || input->version > QRSPEC_VERSION_MAX) {
  385. errno = EINVAL;
  386. return NULL;
  387. }
  388. if(input->level > QR_ECLEVEL_H) {
  389. errno = EINVAL;
  390. return NULL;
  391. }
  392. raw = QRraw_new(input);
  393. if(raw == NULL) return NULL;
  394. version = raw->version;
  395. width = QRspec_getWidth(version);
  396. frame = QRspec_newFrame(version);
  397. if(frame == NULL) {
  398. QRraw_free(raw);
  399. return NULL;
  400. }
  401. FrameFiller_set(&filler, width, frame, 0);
  402. /* interleaved data and ecc codes */
  403. for(i = 0; i < raw->dataLength; i++) {
  404. code = QRraw_getCode(raw);
  405. bit = 0x80;
  406. for(j = 0; j < 8; j++) {
  407. p = FrameFiller_next(&filler);
  408. if(p == NULL) goto EXIT;
  409. *p = ((bit & code) != 0);
  410. bit = bit >> 1;
  411. }
  412. }
  413. for(i = 0; i < raw->eccLength; i++) {
  414. code = QRraw_getCode(raw);
  415. bit = 0x80;
  416. for(j = 0; j < 8; j++) {
  417. p = FrameFiller_next(&filler);
  418. if(p == NULL) goto EXIT;
  419. *p = 0x02 | ((bit & code) != 0);
  420. bit = bit >> 1;
  421. }
  422. }
  423. QRraw_free(raw);
  424. raw = NULL;
  425. /* remainder bits */
  426. j = QRspec_getRemainder(version);
  427. for(i = 0; i < j; i++) {
  428. p = FrameFiller_next(&filler);
  429. if(p == NULL) goto EXIT;
  430. *p = 0x02;
  431. }
  432. /* masking */
  433. if(mask == -2) { // just for debug purpose
  434. masked = (unsigned char *)malloc((size_t)(width * width));
  435. memcpy(masked, frame, (size_t)(width * width));
  436. } else if(mask < 0) {
  437. masked = Mask_mask(width, frame, input->level);
  438. } else {
  439. masked = Mask_makeMask(width, frame, mask, input->level);
  440. }
  441. if(masked == NULL) {
  442. goto EXIT;
  443. }
  444. qrcode = QRcode_new(version, width, masked);
  445. if(qrcode == NULL) {
  446. free(masked);
  447. }
  448. EXIT:
  449. QRraw_free(raw);
  450. free(frame);
  451. return qrcode;
  452. }
  453. STATIC_IN_RELEASE QRcode *QRcode_encodeMaskMQR(QRinput *input, int mask)
  454. {
  455. int width, version;
  456. MQRRawCode *raw;
  457. unsigned char *frame, *masked, *p, code, bit;
  458. int i, j, length;
  459. QRcode *qrcode = NULL;
  460. FrameFiller filler;
  461. if(!input->mqr) {
  462. errno = EINVAL;
  463. return NULL;
  464. }
  465. if(input->version <= 0 || input->version > MQRSPEC_VERSION_MAX) {
  466. errno = EINVAL;
  467. return NULL;
  468. }
  469. if(input->level > QR_ECLEVEL_Q) {
  470. errno = EINVAL;
  471. return NULL;
  472. }
  473. raw = MQRraw_new(input);
  474. if(raw == NULL) return NULL;
  475. version = raw->version;
  476. width = MQRspec_getWidth(version);
  477. frame = MQRspec_newFrame(version);
  478. if(frame == NULL) {
  479. MQRraw_free(raw);
  480. return NULL;
  481. }
  482. FrameFiller_set(&filler, width, frame, 1);
  483. /* interleaved data and ecc codes */
  484. for(i = 0; i < raw->dataLength; i++) {
  485. code = MQRraw_getCode(raw);
  486. bit = 0x80;
  487. if(raw->oddbits && i == raw->dataLength - 1) {
  488. length = raw->oddbits;
  489. } else {
  490. length = 8;
  491. }
  492. for(j = 0; j < length; j++) {
  493. p = FrameFiller_next(&filler);
  494. if(p == NULL) goto EXIT;
  495. *p = ((bit & code) != 0);
  496. bit = bit >> 1;
  497. }
  498. }
  499. for(i = 0; i < raw->eccLength; i++) {
  500. code = MQRraw_getCode(raw);
  501. bit = 0x80;
  502. length = 8;
  503. for(j = 0; j < length; j++) {
  504. p = FrameFiller_next(&filler);
  505. if(p == NULL) goto EXIT;
  506. *p = 0x02 | ((bit & code) != 0);
  507. bit = bit >> 1;
  508. }
  509. }
  510. MQRraw_free(raw);
  511. raw = NULL;
  512. /* masking */
  513. if(mask == -2) { // just for debug purpose
  514. masked = (unsigned char *)malloc((size_t)(width * width));
  515. memcpy(masked, frame, (size_t)(width * width));
  516. } else if(mask < 0) {
  517. masked = MMask_mask(version, frame, input->level);
  518. } else {
  519. masked = MMask_makeMask(version, frame, mask, input->level);
  520. }
  521. if(masked == NULL) {
  522. goto EXIT;
  523. }
  524. qrcode = QRcode_new(version, width, masked);
  525. if(qrcode == NULL) {
  526. free(masked);
  527. }
  528. EXIT:
  529. MQRraw_free(raw);
  530. free(frame);
  531. return qrcode;
  532. }
  533. QRcode *QRcode_encodeInput(QRinput *input)
  534. {
  535. if(input->mqr) {
  536. return QRcode_encodeMaskMQR(input, -1);
  537. } else {
  538. return QRcode_encodeMask(input, -1);
  539. }
  540. }
  541. static QRcode *QRcode_encodeStringReal(const char *string, int version, QRecLevel level, int mqr, QRencodeMode hint, int casesensitive)
  542. {
  543. QRinput *input;
  544. QRcode *code;
  545. int ret;
  546. if(string == NULL) {
  547. errno = EINVAL;
  548. return NULL;
  549. }
  550. if(hint != QR_MODE_8 && hint != QR_MODE_KANJI) {
  551. errno = EINVAL;
  552. return NULL;
  553. }
  554. if(mqr) {
  555. input = QRinput_newMQR(version, level);
  556. } else {
  557. input = QRinput_new2(version, level);
  558. }
  559. if(input == NULL) return NULL;
  560. ret = Split_splitStringToQRinput(string, input, hint, casesensitive);
  561. if(ret < 0) {
  562. QRinput_free(input);
  563. return NULL;
  564. }
  565. code = QRcode_encodeInput(input);
  566. QRinput_free(input);
  567. return code;
  568. }
  569. QRcode *QRcode_encodeString(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
  570. {
  571. return QRcode_encodeStringReal(string, version, level, 0, hint, casesensitive);
  572. }
  573. QRcode *QRcode_encodeStringMQR(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
  574. {
  575. return QRcode_encodeStringReal(string, version, level, 1, hint, casesensitive);
  576. }
  577. static QRcode *QRcode_encodeDataReal(const unsigned char *data, int length, int version, QRecLevel level, int mqr)
  578. {
  579. QRinput *input;
  580. QRcode *code;
  581. int ret;
  582. if(data == NULL || length == 0) {
  583. errno = EINVAL;
  584. return NULL;
  585. }
  586. if(mqr) {
  587. input = QRinput_newMQR(version, level);
  588. } else {
  589. input = QRinput_new2(version, level);
  590. }
  591. if(input == NULL) return NULL;
  592. ret = QRinput_append(input, QR_MODE_8, length, data);
  593. if(ret < 0) {
  594. QRinput_free(input);
  595. return NULL;
  596. }
  597. code = QRcode_encodeInput(input);
  598. QRinput_free(input);
  599. return code;
  600. }
  601. QRcode *QRcode_encodeData(int size, const unsigned char *data, int version, QRecLevel level)
  602. {
  603. return QRcode_encodeDataReal(data, size, version, level, 0);
  604. }
  605. QRcode *QRcode_encodeString8bit(const char *string, int version, QRecLevel level)
  606. {
  607. if(string == NULL) {
  608. errno = EINVAL;
  609. return NULL;
  610. }
  611. return QRcode_encodeDataReal((unsigned char *)string, (int)strlen(string), version, level, 0);
  612. }
  613. QRcode *QRcode_encodeDataMQR(int size, const unsigned char *data, int version, QRecLevel level)
  614. {
  615. return QRcode_encodeDataReal(data, size, version, level, 1);
  616. }
  617. QRcode *QRcode_encodeString8bitMQR(const char *string, int version, QRecLevel level)
  618. {
  619. if(string == NULL) {
  620. errno = EINVAL;
  621. return NULL;
  622. }
  623. return QRcode_encodeDataReal((unsigned char *)string, (int)strlen(string), version, level, 1);
  624. }
  625. /******************************************************************************
  626. * Structured QR-code encoding
  627. *****************************************************************************/
  628. static QRcode_List *QRcode_List_newEntry(void)
  629. {
  630. QRcode_List *entry;
  631. entry = (QRcode_List *)malloc(sizeof(QRcode_List));
  632. if(entry == NULL) return NULL;
  633. entry->next = NULL;
  634. entry->code = NULL;
  635. return entry;
  636. }
  637. static void QRcode_List_freeEntry(QRcode_List *entry)
  638. {
  639. if(entry != NULL) {
  640. QRcode_free(entry->code);
  641. free(entry);
  642. }
  643. }
  644. void QRcode_List_free(QRcode_List *qrlist)
  645. {
  646. QRcode_List *list = qrlist, *next;
  647. while(list != NULL) {
  648. next = list->next;
  649. QRcode_List_freeEntry(list);
  650. list = next;
  651. }
  652. }
  653. int QRcode_List_size(QRcode_List *qrlist)
  654. {
  655. QRcode_List *list = qrlist;
  656. int size = 0;
  657. while(list != NULL) {
  658. size++;
  659. list = list->next;
  660. }
  661. return size;
  662. }
  663. #if 0
  664. static unsigned char QRcode_parity(const char *str, int size)
  665. {
  666. unsigned char parity = 0;
  667. int i;
  668. for(i = 0; i < size; i++) {
  669. parity ^= str[i];
  670. }
  671. return parity;
  672. }
  673. #endif
  674. QRcode_List *QRcode_encodeInputStructured(QRinput_Struct *s)
  675. {
  676. QRcode_List *head = NULL;
  677. QRcode_List *tail = NULL;
  678. QRcode_List *entry;
  679. QRinput_InputList *list = s->head;
  680. while(list != NULL) {
  681. if(head == NULL) {
  682. entry = QRcode_List_newEntry();
  683. if(entry == NULL) goto ABORT;
  684. head = entry;
  685. tail = head;
  686. } else {
  687. entry = QRcode_List_newEntry();
  688. if(entry == NULL) goto ABORT;
  689. tail->next = entry;
  690. tail = tail->next;
  691. }
  692. tail->code = QRcode_encodeInput(list->input);
  693. if(tail->code == NULL) {
  694. goto ABORT;
  695. }
  696. list = list->next;
  697. }
  698. return head;
  699. ABORT:
  700. QRcode_List_free(head);
  701. return NULL;
  702. }
  703. static QRcode_List *QRcode_encodeInputToStructured(QRinput *input)
  704. {
  705. QRinput_Struct *s;
  706. QRcode_List *codes;
  707. s = QRinput_splitQRinputToStruct(input);
  708. if(s == NULL) return NULL;
  709. codes = QRcode_encodeInputStructured(s);
  710. QRinput_Struct_free(s);
  711. return codes;
  712. }
  713. static QRcode_List *QRcode_encodeDataStructuredReal(
  714. int size, const unsigned char *data,
  715. int version, QRecLevel level,
  716. int eightbit, QRencodeMode hint, int casesensitive)
  717. {
  718. QRinput *input;
  719. QRcode_List *codes;
  720. int ret;
  721. if(version <= 0) {
  722. errno = EINVAL;
  723. return NULL;
  724. }
  725. if(!eightbit && (hint != QR_MODE_8 && hint != QR_MODE_KANJI)) {
  726. errno = EINVAL;
  727. return NULL;
  728. }
  729. input = QRinput_new2(version, level);
  730. if(input == NULL) return NULL;
  731. if(eightbit) {
  732. ret = QRinput_append(input, QR_MODE_8, size, data);
  733. } else {
  734. ret = Split_splitStringToQRinput((char *)data, input, hint, casesensitive);
  735. }
  736. if(ret < 0) {
  737. QRinput_free(input);
  738. return NULL;
  739. }
  740. codes = QRcode_encodeInputToStructured(input);
  741. QRinput_free(input);
  742. return codes;
  743. }
  744. QRcode_List *QRcode_encodeDataStructured(int size, const unsigned char *data, int version, QRecLevel level) {
  745. return QRcode_encodeDataStructuredReal(size, data, version, level, 1, QR_MODE_NUL, 0);
  746. }
  747. QRcode_List *QRcode_encodeString8bitStructured(const char *string, int version, QRecLevel level) {
  748. if(string == NULL) {
  749. errno = EINVAL;
  750. return NULL;
  751. }
  752. return QRcode_encodeDataStructured((int)strlen(string), (unsigned char *)string, version, level);
  753. }
  754. QRcode_List *QRcode_encodeStringStructured(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
  755. {
  756. if(string == NULL) {
  757. errno = EINVAL;
  758. return NULL;
  759. }
  760. return QRcode_encodeDataStructuredReal((int)strlen(string), (unsigned char *)string, version, level, 0, hint, casesensitive);
  761. }
  762. /******************************************************************************
  763. * System utilities
  764. *****************************************************************************/
  765. void QRcode_APIVersion(int *major_version, int *minor_version, int *micro_version)
  766. {
  767. if(major_version != NULL) {
  768. *major_version = MAJOR_VERSION;
  769. }
  770. if(minor_version != NULL) {
  771. *minor_version = MINOR_VERSION;
  772. }
  773. if(micro_version != NULL) {
  774. *micro_version = MICRO_VERSION;
  775. }
  776. }
  777. char *QRcode_APIVersionString(void)
  778. {
  779. return VERSION;
  780. }
  781. void QRcode_clearCache(void)
  782. {
  783. return;
  784. }