qrencode.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*
  2. * qrencode - QR Code encoder
  3. *
  4. * Copyright (C) 2006-2010 Kentaro Fukuchi <fukuchi@megaui.net>
  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. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. //#include "config.h"
  25. #include "qrencode.h"
  26. #include "qrspec.h"
  27. #include "bitstream.h"
  28. #include "qrinput.h"
  29. #include "rscode.h"
  30. #include "split.h"
  31. #include "mask.h"
  32. /******************************************************************************
  33. * Raw code
  34. *****************************************************************************/
  35. typedef struct {
  36. int dataLength;
  37. unsigned char *data;
  38. int eccLength;
  39. unsigned char *ecc;
  40. } RSblock;
  41. typedef struct {
  42. int version;
  43. unsigned char *datacode;
  44. unsigned char *ecccode;
  45. int blocks;
  46. RSblock *rsblock;
  47. int count;
  48. int dataLength;
  49. int eccLength;
  50. int b1;
  51. } QRRawCode;
  52. static void RSblock_initBlock(RSblock *block, int dl, unsigned char *data, int el, unsigned char *ecc, RS *rs)
  53. {
  54. block->dataLength = dl;
  55. block->data = data;
  56. block->eccLength = el;
  57. block->ecc = ecc;
  58. encode_rs_char(rs, data, ecc);
  59. }
  60. static int RSblock_init(RSblock *blocks, int spec[5], unsigned char *data, unsigned char *ecc)
  61. {
  62. int i;
  63. RSblock *block;
  64. unsigned char *dp, *ep;
  65. RS *rs;
  66. int el, dl;
  67. dl = QRspec_rsDataCodes1(spec);
  68. el = QRspec_rsEccCodes1(spec);
  69. rs = init_rs(8, 0x11d, 0, 1, el, 255 - dl - el);
  70. if(rs == NULL) return -1;
  71. block = blocks;
  72. dp = data;
  73. ep = ecc;
  74. for(i=0; i<QRspec_rsBlockNum1(spec); i++) {
  75. RSblock_initBlock(block, dl, dp, el, ep, rs);
  76. dp += dl;
  77. ep += el;
  78. block++;
  79. }
  80. if(QRspec_rsBlockNum2(spec) == 0) return 0;
  81. dl = QRspec_rsDataCodes2(spec);
  82. el = QRspec_rsEccCodes2(spec);
  83. rs = init_rs(8, 0x11d, 0, 1, el, 255 - dl - el);
  84. if(rs == NULL) return -1;
  85. for(i=0; i<QRspec_rsBlockNum2(spec); i++) {
  86. RSblock_initBlock(block, dl, dp, el, ep, rs);
  87. dp += dl;
  88. ep += el;
  89. block++;
  90. }
  91. return 0;
  92. }
  93. //__STATIC
  94. static void QRraw_free(QRRawCode *raw);
  95. //__STATIC
  96. static QRRawCode *QRraw_new(QRinput *input)
  97. {
  98. QRRawCode *raw;
  99. int spec[5], ret;
  100. raw = (QRRawCode *)malloc(sizeof(QRRawCode));
  101. if(raw == NULL) return NULL;
  102. raw->datacode = QRinput_getByteStream(input);
  103. if(raw->datacode == NULL) {
  104. free(raw);
  105. return NULL;
  106. }
  107. QRspec_getEccSpec(input->version, input->level, spec);
  108. raw->version = input->version;
  109. raw->b1 = QRspec_rsBlockNum1(spec);
  110. raw->dataLength = QRspec_rsDataLength(spec);
  111. raw->eccLength = QRspec_rsEccLength(spec);
  112. raw->ecccode = (unsigned char *)malloc(raw->eccLength);
  113. if(raw->ecccode == NULL) {
  114. free(raw->datacode);
  115. free(raw);
  116. return NULL;
  117. }
  118. raw->blocks = QRspec_rsBlockNum(spec);
  119. raw->rsblock = (RSblock *)calloc(sizeof(RSblock), raw->blocks);
  120. if(raw->rsblock == NULL) {
  121. QRraw_free(raw);
  122. return NULL;
  123. }
  124. ret = RSblock_init(raw->rsblock, spec, raw->datacode, raw->ecccode);
  125. if(ret < 0) {
  126. QRraw_free(raw);
  127. return NULL;
  128. }
  129. raw->count = 0;
  130. return raw;
  131. }
  132. /**
  133. * Return a code (byte).
  134. * This function can be called iteratively.
  135. * @param raw raw code.
  136. * @return code
  137. */
  138. //__STATIC
  139. static unsigned char QRraw_getCode(QRRawCode *raw)
  140. {
  141. int col, row;
  142. unsigned char ret;
  143. if(raw->count < raw->dataLength) {
  144. row = raw->count % raw->blocks;
  145. col = raw->count / raw->blocks;
  146. if(col >= raw->rsblock[0].dataLength) {
  147. row += raw->b1;
  148. }
  149. ret = raw->rsblock[row].data[col];
  150. } else if(raw->count < raw->dataLength + raw->eccLength) {
  151. row = (raw->count - raw->dataLength) % raw->blocks;
  152. col = (raw->count - raw->dataLength) / raw->blocks;
  153. ret = raw->rsblock[row].ecc[col];
  154. } else {
  155. return 0;
  156. }
  157. raw->count++;
  158. return ret;
  159. }
  160. //__STATIC
  161. static void QRraw_free(QRRawCode *raw)
  162. {
  163. if(raw != NULL) {
  164. free(raw->datacode);
  165. free(raw->ecccode);
  166. if(raw->rsblock != NULL) {
  167. free(raw->rsblock);
  168. }
  169. free(raw);
  170. }
  171. }
  172. /******************************************************************************
  173. * Frame filling
  174. *****************************************************************************/
  175. typedef struct {
  176. int width;
  177. unsigned char *frame;
  178. int x, y;
  179. int dir;
  180. int bit;
  181. } FrameFiller;
  182. static FrameFiller *FrameFiller_new(int width, unsigned char *frame)
  183. {
  184. FrameFiller *filler;
  185. filler = (FrameFiller *)malloc(sizeof(FrameFiller));
  186. if(filler == NULL) return NULL;
  187. filler->width = width;
  188. filler->frame = frame;
  189. filler->x = width - 1;
  190. filler->y = width - 1;
  191. filler->dir = -1;
  192. filler->bit = -1;
  193. return filler;
  194. }
  195. static unsigned char *FrameFiller_next(FrameFiller *filler)
  196. {
  197. unsigned char *p;
  198. int x, y, w;
  199. if(filler->bit == -1) {
  200. filler->bit = 0;
  201. return filler->frame + filler->y * filler->width + filler->x;
  202. }
  203. x = filler->x;
  204. y = filler->y;
  205. p = filler->frame;
  206. w = filler->width;
  207. if(filler->bit == 0) {
  208. x--;
  209. filler->bit++;
  210. } else {
  211. x++;
  212. y += filler->dir;
  213. filler->bit--;
  214. }
  215. if(filler->dir < 0) {
  216. if(y < 0) {
  217. y = 0;
  218. x -= 2;
  219. filler->dir = 1;
  220. if(x == 6) {
  221. x--;
  222. y = 9;
  223. }
  224. }
  225. } else {
  226. if(y == w) {
  227. y = w - 1;
  228. x -= 2;
  229. filler->dir = -1;
  230. if(x == 6) {
  231. x--;
  232. y -= 8;
  233. }
  234. }
  235. }
  236. if(x < 0 || y < 0) return NULL;
  237. filler->x = x;
  238. filler->y = y;
  239. if(p[y * w + x] & 0x80) {
  240. // This tail recursion could be optimized.
  241. return FrameFiller_next(filler);
  242. }
  243. return &p[y * w + x];
  244. }
  245. #if 0
  246. unsigned char *FrameFiller_fillerTest(int version)
  247. {
  248. int width, length;
  249. unsigned char *frame, *p;
  250. FrameFiller *filler;
  251. int i, j;
  252. unsigned char cl = 1;
  253. unsigned char ch = 0;
  254. width = QRspec_getWidth(version);
  255. frame = QRspec_newFrame(version);
  256. filler = FrameFiller_new(width, frame);
  257. length = QRspec_getDataLength(version, QR_ECLEVEL_L)
  258. + QRspec_getECCLength(version, QR_ECLEVEL_L);
  259. for(i=0; i<length; i++) {
  260. for(j=0; j<8; j++) {
  261. p = FrameFiller_next(filler);
  262. *p = ch | cl;
  263. cl++;
  264. if(cl == 9) {
  265. cl = 1;
  266. ch += 0x10;
  267. }
  268. }
  269. }
  270. length = QRspec_getRemainder(version);
  271. for(i=0; i<length; i++) {
  272. p = FrameFiller_next(filler);
  273. *p = 0xa0;
  274. }
  275. p = FrameFiller_next(filler);
  276. free(filler);
  277. if(p != NULL) {
  278. return NULL;
  279. }
  280. return frame;
  281. }
  282. #endif
  283. /******************************************************************************
  284. * QR-code encoding
  285. *****************************************************************************/
  286. static QRcode *QRcode_new(int version, int width, unsigned char *data)
  287. {
  288. QRcode *qrcode;
  289. qrcode = (QRcode *)malloc(sizeof(QRcode));
  290. if(qrcode == NULL) return NULL;
  291. qrcode->version = version;
  292. qrcode->width = width;
  293. qrcode->data = data;
  294. return qrcode;
  295. }
  296. void QRcode_free(QRcode *qrcode)
  297. {
  298. if(qrcode != NULL) {
  299. free(qrcode->data);
  300. free(qrcode);
  301. }
  302. }
  303. //__STATIC
  304. static QRcode *QRcode_encodeMask(QRinput *input, int mask)
  305. {
  306. int width, version;
  307. QRRawCode *raw;
  308. unsigned char *frame, *masked, *p, code, bit;
  309. FrameFiller *filler;
  310. int i, j;
  311. QRcode *qrcode;
  312. if(input->version < 0 || input->version > QRSPEC_VERSION_MAX) {
  313. errno = EINVAL;
  314. return NULL;
  315. }
  316. if(input->level > QR_ECLEVEL_H) {
  317. errno = EINVAL;
  318. return NULL;
  319. }
  320. raw = QRraw_new(input);
  321. if(raw == NULL) return NULL;
  322. version = raw->version;
  323. width = QRspec_getWidth(version);
  324. frame = QRspec_newFrame(version);
  325. if(frame == NULL) {
  326. QRraw_free(raw);
  327. return NULL;
  328. }
  329. filler = FrameFiller_new(width, frame);
  330. if(filler == NULL) {
  331. QRraw_free(raw);
  332. free(frame);
  333. return NULL;
  334. }
  335. /* inteleaved data and ecc codes */
  336. for(i=0; i<raw->dataLength + raw->eccLength; i++) {
  337. code = QRraw_getCode(raw);
  338. bit = 0x80;
  339. for(j=0; j<8; j++) {
  340. p = FrameFiller_next(filler);
  341. *p = 0x02 | ((bit & code) != 0);
  342. bit = bit >> 1;
  343. }
  344. }
  345. QRraw_free(raw);
  346. /* remainder bits */
  347. j = QRspec_getRemainder(version);
  348. for(i=0; i<j; i++) {
  349. p = FrameFiller_next(filler);
  350. *p = 0x02;
  351. }
  352. free(filler);
  353. /* masking */
  354. if(mask < 0) {
  355. masked = Mask_mask(width, frame, input->level);
  356. } else {
  357. masked = Mask_makeMask(width, frame, mask, input->level);
  358. }
  359. if(masked == NULL) {
  360. free(frame);
  361. return NULL;
  362. }
  363. qrcode = QRcode_new(version, width, masked);
  364. free(frame);
  365. return qrcode;
  366. }
  367. QRcode *QRcode_encodeInput(QRinput *input)
  368. {
  369. return QRcode_encodeMask(input, -1);
  370. }
  371. QRcode *QRcode_encodeString8bit(const char *string, int version, QRecLevel level)
  372. {
  373. QRinput *input;
  374. QRcode *code;
  375. int ret;
  376. if(string == NULL) {
  377. errno = EINVAL;
  378. return NULL;
  379. }
  380. input = QRinput_new2(version, level);
  381. if(input == NULL) return NULL;
  382. ret = QRinput_append(input, QR_MODE_8, strlen(string), (unsigned char *)string);
  383. if(ret < 0) {
  384. QRinput_free(input);
  385. return NULL;
  386. }
  387. code = QRcode_encodeInput(input);
  388. QRinput_free(input);
  389. return code;
  390. }
  391. QRcode *QRcode_encodeString(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
  392. {
  393. QRinput *input;
  394. QRcode *code;
  395. int ret;
  396. if(hint != QR_MODE_8 && hint != QR_MODE_KANJI) {
  397. errno = EINVAL;
  398. return NULL;
  399. }
  400. input = QRinput_new2(version, level);
  401. if(input == NULL) return NULL;
  402. ret = Split_splitStringToQRinput(string, input, hint, casesensitive);
  403. if(ret < 0) {
  404. QRinput_free(input);
  405. return NULL;
  406. }
  407. code = QRcode_encodeInput(input);
  408. QRinput_free(input);
  409. return code;
  410. }
  411. /******************************************************************************
  412. * Structured QR-code encoding
  413. *****************************************************************************/
  414. static QRcode_List *QRcode_List_newEntry(void)
  415. {
  416. QRcode_List *entry;
  417. entry = (QRcode_List *)malloc(sizeof(QRcode_List));
  418. if(entry == NULL) return NULL;
  419. entry->next = NULL;
  420. entry->code = NULL;
  421. return entry;
  422. }
  423. static void QRcode_List_freeEntry(QRcode_List *entry)
  424. {
  425. if(entry != NULL) {
  426. QRcode_free(entry->code);
  427. free(entry);
  428. }
  429. }
  430. void QRcode_List_free(QRcode_List *qrlist)
  431. {
  432. QRcode_List *list = qrlist, *next;
  433. while(list != NULL) {
  434. next = list->next;
  435. QRcode_List_freeEntry(list);
  436. list = next;
  437. }
  438. }
  439. int QRcode_List_size(QRcode_List *qrlist)
  440. {
  441. QRcode_List *list = qrlist;
  442. int size = 0;
  443. while(list != NULL) {
  444. size++;
  445. list = list->next;
  446. }
  447. return size;
  448. }
  449. #if 0
  450. static unsigned char QRcode_parity(const char *str, int size)
  451. {
  452. unsigned char parity = 0;
  453. int i;
  454. for(i=0; i<size; i++) {
  455. parity ^= str[i];
  456. }
  457. return parity;
  458. }
  459. #endif
  460. QRcode_List *QRcode_encodeInputStructured(QRinput_Struct *s)
  461. {
  462. QRcode_List *head = NULL;
  463. QRcode_List *tail = NULL;
  464. QRcode_List *entry;
  465. QRinput_InputList *list = s->head;
  466. while(list != NULL) {
  467. if(head == NULL) {
  468. entry = QRcode_List_newEntry();
  469. if(entry == NULL) goto ABORT;
  470. head = entry;
  471. tail = head;
  472. } else {
  473. entry = QRcode_List_newEntry();
  474. if(entry == NULL) goto ABORT;
  475. tail->next = entry;
  476. tail = tail->next;
  477. }
  478. tail->code = QRcode_encodeInput(list->input);
  479. if(tail->code == NULL) {
  480. goto ABORT;
  481. }
  482. list = list->next;
  483. }
  484. return head;
  485. ABORT:
  486. QRcode_List_free(head);
  487. return NULL;
  488. }
  489. static QRcode_List *QRcode_encodeInputToStructured(QRinput *input)
  490. {
  491. QRinput_Struct *s;
  492. QRcode_List *codes;
  493. s = QRinput_splitQRinputToStruct(input);
  494. if(s == NULL) return NULL;
  495. codes = QRcode_encodeInputStructured(s);
  496. QRinput_Struct_free(s);
  497. return codes;
  498. }
  499. QRcode_List *QRcode_encodeString8bitStructured(const char *string, int version, QRecLevel level)
  500. {
  501. QRinput *input;
  502. QRcode_List *codes;
  503. int ret;
  504. if(version <= 0) {
  505. errno = EINVAL;
  506. return NULL;
  507. }
  508. input = QRinput_new2(version, level);
  509. if(input == NULL) return NULL;
  510. ret = QRinput_append(input, QR_MODE_8, strlen(string), (unsigned char *)string);
  511. if(ret < 0) {
  512. QRinput_free(input);
  513. return NULL;
  514. }
  515. codes = QRcode_encodeInputToStructured(input);
  516. QRinput_free(input);
  517. return codes;
  518. }
  519. QRcode_List *QRcode_encodeStringStructured(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
  520. {
  521. QRinput *input;
  522. QRcode_List *codes;
  523. int ret;
  524. if(version <= 0) {
  525. errno = EINVAL;
  526. return NULL;
  527. }
  528. if(hint != QR_MODE_8 && hint != QR_MODE_KANJI) {
  529. errno = EINVAL;
  530. return NULL;
  531. }
  532. input = QRinput_new2(version, level);
  533. if(input == NULL) return NULL;
  534. ret = Split_splitStringToQRinput(string, input, hint, casesensitive);
  535. if(ret < 0) {
  536. QRinput_free(input);
  537. return NULL;
  538. }
  539. codes = QRcode_encodeInputToStructured(input);
  540. QRinput_free(input);
  541. return codes;
  542. }