qrencode  4.1.1
About: libqrencode is a fast and compact library and command-line utility for encoding data in a QR Code symbol.
  Fossies Dox: qrencode-4.1.1.tar.bz2  ("unofficial" and yet experimental doxygen-generated source code documentation)  

qrinput.c
Go to the documentation of this file.
1/*
2 * qrencode - QR Code encoder
3 *
4 * Input data chunk class
5 * Copyright (C) 2006-2017 Kentaro Fukuchi <kentaro@fukuchi.org>
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
22#if HAVE_CONFIG_H
23# include "config.h"
24#endif
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
29
30#include "qrencode.h"
31#include "qrspec.h"
32#include "mqrspec.h"
33#include "bitstream.h"
34#include "qrinput.h"
35
36/******************************************************************************
37 * Utilities
38 *****************************************************************************/
40{
41 return (mode >= QR_MODE_NUM && mode <= QR_MODE_KANJI);
42}
43
44/******************************************************************************
45 * Entry of input data
46 *****************************************************************************/
47
48static QRinput_List *QRinput_List_newEntry(QRencodeMode mode, int size, const unsigned char *data)
49{
50 QRinput_List *entry;
51
52 if(QRinput_check(mode, size, data)) {
53 errno = EINVAL;
54 return NULL;
55 }
56
57 entry = (QRinput_List *)malloc(sizeof(QRinput_List));
58 if(entry == NULL) return NULL;
59
60 entry->mode = mode;
61 entry->size = size;
62 entry->data = NULL;
63 if(size > 0) {
64 entry->data = (unsigned char *)malloc((size_t)size);
65 if(entry->data == NULL) {
66 free(entry);
67 return NULL;
68 }
69 memcpy(entry->data, data, (size_t)size);
70 }
71 entry->bstream = NULL;
72 entry->next = NULL;
73
74 return entry;
75}
76
78{
79 if(entry != NULL) {
80 free(entry->data);
81 BitStream_free(entry->bstream);
82 free(entry);
83 }
84}
85
87{
88 QRinput_List *n;
89
90 n = (QRinput_List *)malloc(sizeof(QRinput_List));
91 if(n == NULL) return NULL;
92
93 n->mode = entry->mode;
94 n->size = entry->size;
95 n->data = (unsigned char *)malloc((size_t)n->size);
96 if(n->data == NULL) {
97 free(n);
98 return NULL;
99 }
100 memcpy(n->data, entry->data, (size_t)entry->size);
101 n->bstream = NULL;
102 n->next = NULL;
103
104 return n;
105}
106
107/******************************************************************************
108 * Input Data
109 *****************************************************************************/
110
112{
113 return QRinput_new2(0, QR_ECLEVEL_L);
114}
115
117{
118 QRinput *input;
119
120 if(version < 0 || version > QRSPEC_VERSION_MAX || level < 0 || level > QR_ECLEVEL_H) {
121 errno = EINVAL;
122 return NULL;
123 }
124
125 input = (QRinput *)malloc(sizeof(QRinput));
126 if(input == NULL) return NULL;
127
128 input->head = NULL;
129 input->tail = NULL;
130 input->version = version;
131 input->level = level;
132 input->mqr = 0;
133 input->fnc1 = 0;
134
135 return input;
136}
137
139{
140 QRinput *input;
141
142 if(version <= 0 || version > MQRSPEC_VERSION_MAX) goto INVALID;
143 if((MQRspec_getECCLength(version, level) == 0)) goto INVALID;
144
145 input = QRinput_new2(version, level);
146 if(input == NULL) return NULL;
147
148 input->mqr = 1;
149
150 return input;
151
152INVALID:
153 errno = EINVAL;
154 return NULL;
155}
156
158{
159 return input->version;
160}
161
163{
164 if(input->mqr || version < 0 || version > QRSPEC_VERSION_MAX) {
165 errno = EINVAL;
166 return -1;
167 }
168
169 input->version = version;
170
171 return 0;
172}
173
175{
176 return input->level;
177}
178
180{
181 if(input->mqr || level > QR_ECLEVEL_H) {
182 errno = EINVAL;
183 return -1;
184 }
185
186 input->level = level;
187
188 return 0;
189}
190
192{
193 if(input->mqr) {
194 if(version <= 0 || version > MQRSPEC_VERSION_MAX) goto INVALID;
195 if((MQRspec_getECCLength(version, level) == 0)) goto INVALID;
196 } else {
197 if(version < 0 || version > QRSPEC_VERSION_MAX) goto INVALID;
198 if(level > QR_ECLEVEL_H) goto INVALID;
199 }
200
201 input->version = version;
202 input->level = level;
203
204 return 0;
205
206INVALID:
207 errno = EINVAL;
208 return -1;
209}
210
211static void QRinput_appendEntry(QRinput *input, QRinput_List *entry)
212{
213 if(input->tail == NULL) {
214 input->head = entry;
215 input->tail = entry;
216 } else {
217 input->tail->next = entry;
218 input->tail = entry;
219 }
220 entry->next = NULL;
221}
222
223int QRinput_append(QRinput *input, QRencodeMode mode, int size, const unsigned char *data)
224{
225 QRinput_List *entry;
226
227 entry = QRinput_List_newEntry(mode, size, data);
228 if(entry == NULL) {
229 return -1;
230 }
231
232 QRinput_appendEntry(input, entry);
233
234 return 0;
235}
236
237/**
238 * Insert a structured-append header to the head of the input data.
239 * @param input input data.
240 * @param size number of structured symbols.
241 * @param number index number of the symbol. (1 <= number <= size)
242 * @param parity parity among input data. (NOTE: each symbol of a set of structured symbols has the same parity data)
243 * @retval 0 success.
244 * @retval -1 error occurred and errno is set to indeicate the error. See Execptions for the details.
245 * @throw EINVAL invalid parameter.
246 * @throw ENOMEM unable to allocate memory.
247 */
248STATIC_IN_RELEASE int QRinput_insertStructuredAppendHeader(QRinput *input, int size, int number, unsigned char parity)
249{
250 QRinput_List *entry;
251 unsigned char buf[3];
252
254 errno = EINVAL;
255 return -1;
256 }
257 if(number <= 0 || number > size) {
258 errno = EINVAL;
259 return -1;
260 }
261
262 buf[0] = (unsigned char)size;
263 buf[1] = (unsigned char)number;
264 buf[2] = parity;
266 if(entry == NULL) {
267 return -1;
268 }
269
270 entry->next = input->head;
271 input->head = entry;
272
273 return 0;
274}
275
276int QRinput_appendECIheader(QRinput *input, unsigned int ecinum)
277{
278 unsigned char data[4];
279
280 if(ecinum > 999999) {
281 errno = EINVAL;
282 return -1;
283 }
284
285 /* We manually create byte array of ecinum because
286 (unsigned char *)&ecinum may cause bus error on some architectures, */
287 data[0] = ecinum & 0xff;
288 data[1] = (ecinum >> 8) & 0xff;
289 data[2] = (ecinum >> 16) & 0xff;
290 data[3] = (ecinum >> 24) & 0xff;
291 return QRinput_append(input, QR_MODE_ECI, 4, data);
292}
293
295{
296 QRinput_List *list, *next;
297
298 if(input != NULL) {
299 list = input->head;
300 while(list != NULL) {
301 next = list->next;
303 list = next;
304 }
305 free(input);
306 }
307}
308
309static unsigned char QRinput_calcParity(QRinput *input)
310{
311 unsigned char parity = 0;
312 QRinput_List *list;
313 int i;
314
315 list = input->head;
316 while(list != NULL) {
317 if(list->mode != QR_MODE_STRUCTURE) {
318 for(i = list->size-1; i >= 0; i--) {
319 parity ^= list->data[i];
320 }
321 }
322 list = list->next;
323 }
324
325 return parity;
326}
327
329{
330 QRinput *n;
331 QRinput_List *list, *e;
332
333 if(input->mqr) {
334 n = QRinput_newMQR(input->version, input->level);
335 } else {
336 n = QRinput_new2(input->version, input->level);
337 }
338 if(n == NULL) return NULL;
339
340 list = input->head;
341 while(list != NULL) {
342 e = QRinput_List_dup(list);
343 if(e == NULL) {
344 QRinput_free(n);
345 return NULL;
346 }
348 list = list->next;
349 }
350
351 return n;
352}
353
354/******************************************************************************
355 * Numeric data
356 *****************************************************************************/
357
358/**
359 * Check the input data.
360 * @param size
361 * @param data
362 * @return result
363 */
364static int QRinput_checkModeNum(int size, const char *data)
365{
366 int i;
367
368 for(i = 0; i < size; i++) {
369 if(data[i] < '0' || data[i] > '9')
370 return -1;
371 }
372
373 return 0;
374}
375
376/**
377 * Estimate the length of the encoded bit stream of numeric data.
378 * @param size
379 * @return number of bits
380 */
382{
383 int w;
384 int bits;
385
386 w = size / 3;
387 bits = w * 10;
388 switch(size - w * 3) {
389 case 1:
390 bits += 4;
391 break;
392 case 2:
393 bits += 7;
394 break;
395 default:
396 break;
397 }
398
399 return bits;
400}
401
402/**
403 * Convert the number data and append to a bit stream.
404 * @param entry
405 * @param mqr
406 * @retval 0 success
407 * @retval -1 an error occurred and errno is set to indeicate the error.
408 * See Execptions for the details.
409 * @throw ENOMEM unable to allocate memory.
410 */
411static int QRinput_encodeModeNum(QRinput_List *entry, BitStream *bstream, int version, int mqr)
412{
413 int words, i, ret;
414 unsigned int val;
415
416 if(mqr) {
417 if(version > 1) {
418 ret = BitStream_appendNum(bstream, (size_t)(version - 1), MQRSPEC_MODEID_NUM);
419 if(ret < 0) return -1;
420 }
421 ret = BitStream_appendNum(bstream, (size_t)MQRspec_lengthIndicator(QR_MODE_NUM, version), (unsigned int)entry->size);
422 if(ret < 0) return -1;
423 } else {
424 ret = BitStream_appendNum(bstream, 4, QRSPEC_MODEID_NUM);
425 if(ret < 0) return -1;
426
427 ret = BitStream_appendNum(bstream, (size_t)QRspec_lengthIndicator(QR_MODE_NUM, version), (unsigned int)entry->size);
428 if(ret < 0) return -1;
429 }
430
431 words = entry->size / 3;
432 for(i = 0; i < words; i++) {
433 val = (unsigned int)(entry->data[i*3 ] - '0') * 100;
434 val += (unsigned int)(entry->data[i*3+1] - '0') * 10;
435 val += (unsigned int)(entry->data[i*3+2] - '0');
436
437 ret = BitStream_appendNum(bstream, 10, val);
438 if(ret < 0) return -1;
439 }
440
441 if(entry->size - words * 3 == 1) {
442 val = (unsigned int)(entry->data[words*3] - '0');
443 ret = BitStream_appendNum(bstream, 4, val);
444 if(ret < 0) return -1;
445 } else if(entry->size - words * 3 == 2) {
446 val = (unsigned int)(entry->data[words*3 ] - '0') * 10;
447 val += (unsigned int)(entry->data[words*3+1] - '0');
448 ret = BitStream_appendNum(bstream, 7, val);
449 if(ret < 0) return -1;
450 }
451
452 return 0;
453}
454
455/******************************************************************************
456 * Alphabet-numeric data
457 *****************************************************************************/
458
459const signed char QRinput_anTable[128] = {
460 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
461 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
462 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43,
463 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1,
464 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
465 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
466 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
467 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
468};
469
470/**
471 * Check the input data.
472 * @param size
473 * @param data
474 * @return result
475 */
476static int QRinput_checkModeAn(int size, const char *data)
477{
478 int i;
479
480 for(i = 0; i < size; i++) {
481 if(QRinput_lookAnTable(data[i]) < 0)
482 return -1;
483 }
484
485 return 0;
486}
487
488/**
489 * Estimate the length of the encoded bit stream of alphabet-numeric data.
490 * @param size
491 * @return number of bits
492 */
494{
495 int w;
496 int bits;
497
498 w = size / 2;
499 bits = w * 11;
500 if(size & 1) {
501 bits += 6;
502 }
503
504 return bits;
505}
506
507/**
508 * Convert the alphabet-numeric data and append to a bit stream.
509 * @param entry
510 * @param mqr
511 * @retval 0 success
512 * @retval -1 an error occurred and errno is set to indeicate the error.
513 * See Execptions for the details.
514 * @throw ENOMEM unable to allocate memory.
515 * @throw EINVAL invalid version.
516 */
517static int QRinput_encodeModeAn(QRinput_List *entry, BitStream *bstream, int version, int mqr)
518{
519 int words, i, ret;
520 unsigned int val;
521
522 if(mqr) {
523 if(version < 2) {
524 errno = ERANGE;
525 return -1;
526 }
527 ret = BitStream_appendNum(bstream, (size_t)(version - 1), MQRSPEC_MODEID_AN);
528 if(ret < 0) return -1;
529 ret = BitStream_appendNum(bstream, (size_t)MQRspec_lengthIndicator(QR_MODE_AN, version), (unsigned int)entry->size);
530 if(ret < 0) return -1;
531 } else {
532 ret = BitStream_appendNum(bstream, 4, QRSPEC_MODEID_AN);
533 if(ret < 0) return -1;
534 ret = BitStream_appendNum(bstream, (size_t)QRspec_lengthIndicator(QR_MODE_AN, version), (unsigned int)entry->size);
535 if(ret < 0) return -1;
536 }
537
538 words = entry->size / 2;
539 for(i = 0; i < words; i++) {
540 val = (unsigned int)QRinput_lookAnTable(entry->data[i*2 ]) * 45;
541 val += (unsigned int)QRinput_lookAnTable(entry->data[i*2+1]);
542
543 ret = BitStream_appendNum(bstream, 11, val);
544 if(ret < 0) return -1;
545 }
546
547 if(entry->size & 1) {
548 val = (unsigned int)QRinput_lookAnTable(entry->data[words * 2]);
549
550 ret = BitStream_appendNum(bstream, 6, val);
551 if(ret < 0) return -1;
552 }
553
554 return 0;
555}
556
557/******************************************************************************
558 * 8 bit data
559 *****************************************************************************/
560
561/**
562 * Estimate the length of the encoded bit stream of 8 bit data.
563 * @param size
564 * @return number of bits
565 */
567{
568 return size * 8;
569}
570
571/**
572 * Convert the 8bits data and append to a bit stream.
573 * @param entry
574 * @param mqr
575 * @retval 0 success
576 * @retval -1 an error occurred and errno is set to indeicate the error.
577 * See Execptions for the details.
578 * @throw ENOMEM unable to allocate memory.
579 */
580static int QRinput_encodeMode8(QRinput_List *entry, BitStream *bstream, int version, int mqr)
581{
582 int ret;
583
584 if(mqr) {
585 if(version < 3) {
586 errno = ERANGE;
587 return -1;
588 }
589 ret = BitStream_appendNum(bstream, (size_t)(version - 1), MQRSPEC_MODEID_8);
590 if(ret < 0) return -1;
591 ret = BitStream_appendNum(bstream, (size_t)MQRspec_lengthIndicator(QR_MODE_8, version), (unsigned int)entry->size);
592 if(ret < 0) return -1;
593 } else {
594 ret = BitStream_appendNum(bstream, 4, QRSPEC_MODEID_8);
595 if(ret < 0) return -1;
596 ret = BitStream_appendNum(bstream, (size_t)QRspec_lengthIndicator(QR_MODE_8, version), (unsigned int)entry->size);
597 if(ret < 0) return -1;
598 }
599
600 ret = BitStream_appendBytes(bstream, (size_t)entry->size, entry->data);
601 if(ret < 0) return -1;
602
603 return 0;
604}
605
606
607/******************************************************************************
608 * Kanji data
609 *****************************************************************************/
610
611/**
612 * Estimate the length of the encoded bit stream of kanji data.
613 * @param size
614 * @return number of bits
615 */
617{
618 return (size / 2) * 13;
619}
620
621/**
622 * Check the input data.
623 * @param size
624 * @param data
625 * @return result
626 */
627static int QRinput_checkModeKanji(int size, const unsigned char *data)
628{
629 int i;
630 unsigned int val;
631
632 if(size & 1)
633 return -1;
634
635 for(i = 0; i < size; i+=2) {
636 val = ((unsigned int)data[i] << 8) | data[i+1];
637 if(val < 0x8140 || (val > 0x9ffc && val < 0xe040) || val > 0xebbf) {
638 return -1;
639 }
640 }
641
642 return 0;
643}
644
645/**
646 * Convert the kanji data and append to a bit stream.
647 * @param entry
648 * @param mqr
649 * @retval 0 success
650 * @retval -1 an error occurred and errno is set to indeicate the error.
651 * See Execptions for the details.
652 * @throw ENOMEM unable to allocate memory.
653 * @throw EINVAL invalid version.
654 */
655static int QRinput_encodeModeKanji(QRinput_List *entry, BitStream *bstream, int version, int mqr)
656{
657 int ret, i;
658 unsigned int val, h;
659
660 if(mqr) {
661 if(version < 2) {
662 errno = ERANGE;
663 return -1;
664 }
665 ret = BitStream_appendNum(bstream, (size_t)(version - 1), MQRSPEC_MODEID_KANJI);
666 if(ret < 0) return -1;
667 ret = BitStream_appendNum(bstream, (size_t)MQRspec_lengthIndicator(QR_MODE_KANJI, version), (unsigned int)entry->size/2);
668 if(ret < 0) return -1;
669 } else {
670 ret = BitStream_appendNum(bstream, 4, QRSPEC_MODEID_KANJI);
671 if(ret < 0) return -1;
672 ret = BitStream_appendNum(bstream, (size_t)QRspec_lengthIndicator(QR_MODE_KANJI, version), (unsigned int)entry->size/2);
673 if(ret < 0) return -1;
674 }
675
676 for(i = 0; i < entry->size; i+=2) {
677 val = ((unsigned int)entry->data[i] << 8) | entry->data[i+1];
678 if(val <= 0x9ffc) {
679 val -= 0x8140;
680 } else {
681 val -= 0xc140;
682 }
683 h = (val >> 8) * 0xc0;
684 val = (val & 0xff) + h;
685
686 ret = BitStream_appendNum(bstream, 13, val);
687 if(ret < 0) return -1;
688 }
689
690 return 0;
691}
692
693/******************************************************************************
694 * Structured Symbol
695 *****************************************************************************/
696
697/**
698 * Convert a structure symbol code and append to a bit stream.
699 * @param entry
700 * @param mqr
701 * @retval 0 success
702 * @retval -1 an error occurred and errno is set to indeicate the error.
703 * See Execptions for the details.
704 * @throw ENOMEM unable to allocate memory.
705 * @throw EINVAL invalid entry.
706 */
707static int QRinput_encodeModeStructure(QRinput_List *entry, BitStream *bstream, int mqr)
708{
709 int ret;
710
711 if(mqr) {
712 errno = EINVAL;
713 return -1;
714 }
715
717 if(ret < 0) return -1;
718 ret = BitStream_appendNum(bstream, 4, entry->data[1] - 1U);
719 if(ret < 0) return -1;
720 ret = BitStream_appendNum(bstream, 4, entry->data[0] - 1U);
721 if(ret < 0) return -1;
722 ret = BitStream_appendNum(bstream, 8, entry->data[2]);
723 if(ret < 0) return -1;
724
725 return 0;
726}
727
728/******************************************************************************
729 * FNC1
730 *****************************************************************************/
731
733{
734 if(size != 1) return -1;
735
736 /* No data check required. */
737
738 return 0;
739}
740
742{
743 int ret;
744
746 if(ret < 0) return -1;
747
748 ret = BitStream_appendBytes(bstream, 1, entry->data);
749 if(ret < 0) return -1;
750
751 return 0;
752}
753
754/******************************************************************************
755 * ECI header
756 *****************************************************************************/
757static unsigned int QRinput_decodeECIfromByteArray(unsigned char *data)
758{
759 int i;
760 unsigned int ecinum;
761
762 ecinum = 0;
763 for(i = 0; i < 4; i++) {
764 ecinum = ecinum << 8;
765 ecinum |= data[3-i];
766 }
767
768 return ecinum;
769}
770
771static int QRinput_estimateBitsModeECI(unsigned char *data)
772{
773 unsigned int ecinum;
774
775 ecinum = QRinput_decodeECIfromByteArray(data);
776
777 /* See Table 4 of JISX 0510:2004 pp.17. */
778 if(ecinum < 128) {
779 return MODE_INDICATOR_SIZE + 8;
780 } else if(ecinum < 16384) {
781 return MODE_INDICATOR_SIZE + 16;
782 } else {
783 return MODE_INDICATOR_SIZE + 24;
784 }
785}
786
787static int QRinput_encodeModeECI(QRinput_List *entry, BitStream *bstream)
788{
789 int ret, words;
790 unsigned int ecinum, code;
791
792 ecinum = QRinput_decodeECIfromByteArray(entry->data);
793
794 /* See Table 4 of JISX 0510:2004 pp.17. */
795 if(ecinum < 128) {
796 words = 1;
797 code = ecinum;
798 } else if(ecinum < 16384) {
799 words = 2;
800 code = 0x8000 + ecinum;
801 } else {
802 words = 3;
803 code = 0xc0000 + ecinum;
804 }
805
806 ret = BitStream_appendNum(bstream, 4, QRSPEC_MODEID_ECI);
807 if(ret < 0) return -1;
808
809 ret = BitStream_appendNum(bstream, (size_t)words * 8, code);
810 if(ret < 0) return -1;
811
812 return 0;
813}
814
815/******************************************************************************
816 * Validation
817 *****************************************************************************/
818
819int QRinput_check(QRencodeMode mode, int size, const unsigned char *data)
820{
821 if((mode == QR_MODE_FNC1FIRST && size < 0) || size <= 0) return -1;
822
823 switch(mode) {
824 case QR_MODE_NUM:
825 return QRinput_checkModeNum(size, (const char *)data);
826 case QR_MODE_AN:
827 return QRinput_checkModeAn(size, (const char *)data);
828 case QR_MODE_KANJI:
829 return QRinput_checkModeKanji(size, data);
830 case QR_MODE_8:
831 return 0;
833 return 0;
834 case QR_MODE_ECI:
835 return 0;
837 return 0;
840 case QR_MODE_NUL:
841 break;
842 }
843
844 return -1;
845}
846
847/******************************************************************************
848 * Estimation of the bit length
849 *****************************************************************************/
850
851/**
852 * Estimate the length of the encoded bit stream on the current version.
853 * @param entry
854 * @param version version of the symbol
855 * @param mqr
856 * @return number of bits
857 */
859{
860 int bits = 0;
861 int l, m;
862 int num;
863
864 if(version == 0) version = 1;
865
866 switch(entry->mode) {
867 case QR_MODE_NUM:
868 bits = QRinput_estimateBitsModeNum(entry->size);
869 break;
870 case QR_MODE_AN:
871 bits = QRinput_estimateBitsModeAn(entry->size);
872 break;
873 case QR_MODE_8:
874 bits = QRinput_estimateBitsMode8(entry->size);
875 break;
876 case QR_MODE_KANJI:
877 bits = QRinput_estimateBitsModeKanji(entry->size);
878 break;
881 case QR_MODE_ECI:
882 bits = QRinput_estimateBitsModeECI(entry->data);
883 break;
885 return MODE_INDICATOR_SIZE;
887 return MODE_INDICATOR_SIZE + 8;
888 default:
889 return 0;
890 }
891
892 if(mqr) {
894 m = version - 1;
895 bits += l + m;
896 } else {
898 m = 1 << l;
899 if(entry->mode == QR_MODE_KANJI) {
900 num = (entry->size/2 + m - 1) / m;
901 } else {
902 num = (entry->size + m - 1) / m;
903 }
904
905 bits += num * (MODE_INDICATOR_SIZE + l);
906 }
907
908 return bits;
909}
910
911/**
912 * Estimate the length of the encoded bit stream of the data.
913 * @param input input data
914 * @param version version of the symbol
915 * @return number of bits
916 */
917STATIC_IN_RELEASE int QRinput_estimateBitStreamSize(QRinput *input, int version)
918{
919 QRinput_List *list;
920 int bits = 0;
921
922 list = input->head;
923 while(list != NULL) {
924 bits += QRinput_estimateBitStreamSizeOfEntry(list, version, input->mqr);
925 list = list->next;
926 }
927
928 return bits;
929}
930
931/**
932 * Estimate the required version number of the symbol.
933 * @param input input data
934 * @return required version number or -1 for failure.
935 */
936STATIC_IN_RELEASE int QRinput_estimateVersion(QRinput *input)
937{
938 int bits;
939 int version, prev;
940
941 version = 0;
942 do {
943 prev = version;
944 bits = QRinput_estimateBitStreamSize(input, prev);
945 version = QRspec_getMinimumVersion((bits + 7) / 8, input->level);
946 if(prev == 0 && version > 1) {
947 version--;
948 }
949 } while (version > prev);
950
951 return version;
952}
953
954/**
955 * Return required length in bytes for specified mode, version and bits.
956 * @param mode
957 * @param version
958 * @param bits
959 * @return required length of code words in bytes.
960 */
961STATIC_IN_RELEASE int QRinput_lengthOfCode(QRencodeMode mode, int version, int bits)
962{
963 int payload, size, chunks, remain, maxsize;
964
965 payload = bits - 4 - QRspec_lengthIndicator(mode, version);
966 switch(mode) {
967 case QR_MODE_NUM:
968 chunks = payload / 10;
969 remain = payload - chunks * 10;
970 size = chunks * 3;
971 if(remain >= 7) {
972 size += 2;
973 } else if(remain >= 4) {
974 size += 1;
975 }
976 break;
977 case QR_MODE_AN:
978 chunks = payload / 11;
979 remain = payload - chunks * 11;
980 size = chunks * 2;
981 if(remain >= 6) size++;
982 break;
983 case QR_MODE_8:
984 size = payload / 8;
985 break;
986 case QR_MODE_KANJI:
987 size = (payload / 13) * 2;
988 break;
990 size = payload / 8;
991 break;
992 default:
993 size = 0;
994 break;
995 }
996 maxsize = QRspec_maximumWords(mode, version);
997 if(size < 0) size = 0;
998 if(maxsize > 0 && size > maxsize) size = maxsize;
999
1000 return size;
1001}
1002
1003/******************************************************************************
1004 * Data conversion
1005 *****************************************************************************/
1006
1007/**
1008 * Convert the input data in the data chunk and append to a bit stream.
1009 * @param entry
1010 * @param bstream
1011 * @return number of bits (>0) or -1 for failure.
1012 */
1013static int QRinput_encodeBitStream(QRinput_List *entry, BitStream *bstream, int version, int mqr)
1014{
1015 int words, ret;
1016 QRinput_List *st1 = NULL, *st2 = NULL;
1017 int prevsize;
1018
1019 prevsize = (int)BitStream_size(bstream);
1020
1021 if(mqr) {
1022 words = MQRspec_maximumWords(entry->mode, version);
1023 } else {
1024 words = QRspec_maximumWords(entry->mode, version);
1025 }
1026 if(words != 0 && entry->size > words) {
1027 st1 = QRinput_List_newEntry(entry->mode, words, entry->data);
1028 if(st1 == NULL) goto ABORT;
1029 st2 = QRinput_List_newEntry(entry->mode, entry->size - words, &entry->data[words]);
1030 if(st2 == NULL) goto ABORT;
1031
1032 ret = QRinput_encodeBitStream(st1, bstream, version, mqr);
1033 if(ret < 0) goto ABORT;
1034 ret = QRinput_encodeBitStream(st2, bstream, version, mqr);
1035 if(ret < 0) goto ABORT;
1036
1039 } else {
1040 ret = 0;
1041 switch(entry->mode) {
1042 case QR_MODE_NUM:
1043 ret = QRinput_encodeModeNum(entry, bstream, version, mqr);
1044 break;
1045 case QR_MODE_AN:
1046 ret = QRinput_encodeModeAn(entry, bstream, version, mqr);
1047 break;
1048 case QR_MODE_8:
1049 ret = QRinput_encodeMode8(entry, bstream, version, mqr);
1050 break;
1051 case QR_MODE_KANJI:
1052 ret = QRinput_encodeModeKanji(entry, bstream, version, mqr);
1053 break;
1054 case QR_MODE_STRUCTURE:
1055 ret = QRinput_encodeModeStructure(entry, bstream, mqr);
1056 break;
1057 case QR_MODE_ECI:
1058 ret = QRinput_encodeModeECI(entry, bstream);
1059 break;
1060 case QR_MODE_FNC1SECOND:
1061 ret = QRinput_encodeModeFNC1Second(entry, bstream);
1062 break;
1063 default:
1064 break;
1065 }
1066 if(ret < 0) return -1;
1067 }
1068
1069 return (int)BitStream_size(bstream) - prevsize;
1070ABORT:
1073 return -1;
1074}
1075
1076/**
1077 * Convert the input data to a bit stream.
1078 * @param input input data.
1079 * @retval 0 success
1080 * @retval -1 an error occurred and errno is set to indeicate the error.
1081 * See Execptions for the details.
1082 * @throw ENOMEM unable to allocate memory.
1083 */
1084static int QRinput_createBitStream(QRinput *input, BitStream *bstream)
1085{
1086 QRinput_List *list;
1087 int bits, total = 0;
1088
1089 list = input->head;
1090 while(list != NULL) {
1091 bits = QRinput_encodeBitStream(list, bstream, input->version, input->mqr);
1092 if(bits < 0) return -1;
1093 total += bits;
1094 list = list->next;
1095 }
1096
1097 return total;
1098}
1099
1100/**
1101 * Convert the input data to a bit stream.
1102 * When the version number is given and that is not sufficient, it is increased
1103 * automatically.
1104 * @param input input data.
1105 * @param bstream where the converted data is stored.
1106 * @retval 0 success
1107 * @retval -1 an error occurred and errno is set to indeicate the error.
1108 * See Execptions for the details.
1109 * @throw ENOMEM unable to allocate memory.
1110 * @throw ERANGE input data is too large.
1111 */
1112static int QRinput_convertData(QRinput *input, BitStream *bstream)
1113{
1114 int bits;
1115 int ver;
1116
1117 ver = QRinput_estimateVersion(input);
1118 if(ver > QRinput_getVersion(input)) {
1119 QRinput_setVersion(input, ver);
1120 }
1121
1122 for(;;) {
1123 BitStream_reset(bstream);
1124 bits = QRinput_createBitStream(input, bstream);
1125 if(bits < 0) return -1;
1126 ver = QRspec_getMinimumVersion((bits + 7) / 8, input->level);
1127 if(ver > QRinput_getVersion(input)) {
1128 QRinput_setVersion(input, ver);
1129 } else {
1130 break;
1131 }
1132 }
1133
1134 return 0;
1135}
1136
1137/**
1138 * Append padding bits for the input data.
1139 * @param bstream Bitstream to be appended.
1140 * @param input input data.
1141 * @retval 0 success
1142 * @retval -1 an error occurred and errno is set to indeicate the error.
1143 * See Execptions for the details.
1144 * @throw ERANGE input data is too large.
1145 * @throw ENOMEM unable to allocate memory.
1146 */
1147static int QRinput_appendPaddingBit(BitStream *bstream, QRinput *input)
1148{
1149 int bits, maxbits, words, maxwords, i, ret;
1150 int padlen;
1151
1152 bits = (int)BitStream_size(bstream);
1153 maxwords = QRspec_getDataLength(input->version, input->level);
1154 maxbits = maxwords * 8;
1155
1156 if(maxbits < bits) {
1157 errno = ERANGE;
1158 return -1;
1159 }
1160 if(maxbits == bits) {
1161 return 0;
1162 }
1163
1164 if(maxbits - bits <= 4) {
1165 return (int)BitStream_appendNum(bstream, (size_t)(maxbits - bits), 0);
1166 }
1167
1168 words = (bits + 4 + 7) / 8;
1169
1170 ret = (int)BitStream_appendNum(bstream, (size_t)(words * 8 - bits), 0);
1171 if(ret < 0) return ret;
1172
1173 padlen = maxwords - words;
1174 if(padlen > 0) {
1175 for(i = 0; i < padlen; i++) {
1176 ret = (int)BitStream_appendNum(bstream, 8, (i&1)?0x11:0xec);
1177 if(ret < 0) {
1178 return ret;
1179 }
1180 }
1181 }
1182
1183 return 0;
1184}
1185
1186/**
1187 * Append padding bits for the input data - Micro QR Code version.
1188 * @param bstream Bitstream to be appended.
1189 * @param input input data.
1190 * @retval 0 success
1191 * @retval -1 an error occurred and errno is set to indeicate the error.
1192 * See Execptions for the details.
1193 * @throw ERANGE input data is too large.
1194 * @throw ENOMEM unable to allocate memory.
1195 */
1197{
1198 int bits, maxbits, words, maxwords, i, ret, termbits;
1199 int padlen;
1200
1201 bits = (int)BitStream_size(bstream);
1202 maxbits = MQRspec_getDataLengthBit(input->version, input->level);
1203 maxwords = maxbits / 8;
1204
1205 if(maxbits < bits) {
1206 errno = ERANGE;
1207 return -1;
1208 }
1209 if(maxbits == bits) {
1210 return 0;
1211 }
1212
1213 termbits = input->version * 2 + 1;
1214
1215 if(maxbits - bits <= termbits) {
1216 return (int)BitStream_appendNum(bstream, (size_t)(maxbits - bits), 0);
1217 }
1218
1219 bits += termbits;
1220
1221 words = (bits + 7) / 8;
1222 if(maxbits - words * 8 > 0) {
1223 termbits += words * 8 - bits;
1224 if(words == maxwords) termbits += maxbits - words * 8;
1225 } else {
1226 termbits += words * 8 - bits;
1227 }
1228 ret = (int)BitStream_appendNum(bstream, (size_t)termbits, 0);
1229 if(ret < 0) return ret;
1230
1231 padlen = maxwords - words;
1232 if(padlen > 0) {
1233 for(i = 0; i < padlen; i++) {
1234 ret = (int)BitStream_appendNum(bstream, 8, (i&1)?0x11:0xec);
1235 if(ret < 0) return ret;
1236 }
1237 termbits = maxbits - maxwords * 8;
1238 if(termbits > 0) {
1239 ret = (int)BitStream_appendNum(bstream, (size_t)termbits, 0);
1240 if(ret < 0) return ret;
1241 }
1242 }
1243
1244 return 0;
1245}
1246
1248{
1249 QRinput_List *entry = NULL;
1250
1251 if(input->fnc1 == 1) {
1252 entry = QRinput_List_newEntry(QR_MODE_FNC1FIRST, 0, NULL);
1253 } else if(input->fnc1 == 2) {
1254 entry = QRinput_List_newEntry(QR_MODE_FNC1SECOND, 1, &(input->appid));
1255 }
1256 if(entry == NULL) {
1257 return -1;
1258 }
1259
1260 if(input->head->mode != QR_MODE_STRUCTURE && input->head->mode != QR_MODE_ECI) {
1261 entry->next = input->head;
1262 input->head = entry;
1263 } else {
1264 entry->next = input->head->next;
1265 input->head->next = entry;
1266 }
1267
1268 return 0;
1269}
1270
1271/**
1272 * Merge all bit streams in the input data.
1273 * @param input input data.
1274 * @return merged bit stream
1275 */
1276
1277STATIC_IN_RELEASE int QRinput_mergeBitStream(QRinput *input, BitStream *bstream)
1278{
1279 if(input->mqr) {
1280 if(QRinput_createBitStream(input, bstream) < 0) {
1281 return -1;
1282 }
1283 } else {
1284 if(input->fnc1) {
1285 if(QRinput_insertFNC1Header(input) < 0) {
1286 return -1;
1287 }
1288 }
1289 if(QRinput_convertData(input, bstream) < 0) {
1290 return -1;
1291 }
1292 }
1293
1294 return 0;
1295}
1296
1297/**
1298 * Merge all bit streams in the input data and append padding bits
1299 * @param input input data.
1300 * @return padded merged bit stream
1301 */
1302
1303STATIC_IN_RELEASE int QRinput_getBitStream(QRinput *input, BitStream *bstream)
1304{
1305 int ret;
1306
1307 ret = QRinput_mergeBitStream(input, bstream);
1308 if(ret < 0) return -1;
1309
1310 if(input->mqr) {
1311 ret = QRinput_appendPaddingBitMQR(bstream, input);
1312 } else {
1313 ret = QRinput_appendPaddingBit(bstream, input);
1314 }
1315 if(ret < 0) return -1;
1316
1317 return 0;
1318}
1319
1320/**
1321 * Pack all bit streams padding bits into a byte array.
1322 * @param input input data.
1323 * @return padded merged byte stream
1324 */
1325
1326unsigned char *QRinput_getByteStream(QRinput *input)
1327{
1328 BitStream *bstream;
1329 unsigned char *array;
1330 int ret;
1331
1332 bstream = BitStream_new();
1333 if(bstream == NULL) {
1334 return NULL;
1335 }
1336
1337 ret = QRinput_getBitStream(input, bstream);
1338 if(ret < 0) {
1339 BitStream_free(bstream);
1340 return NULL;
1341 }
1342 array = BitStream_toByte(bstream);
1343 BitStream_free(bstream);
1344
1345 return array;
1346}
1347
1348/******************************************************************************
1349 * Structured input data
1350 *****************************************************************************/
1351
1353{
1354 QRinput_InputList *entry;
1355
1356 entry = (QRinput_InputList *)malloc(sizeof(QRinput_InputList));
1357 if(entry == NULL) return NULL;
1358
1359 entry->input = input;
1360 entry->next = NULL;
1361
1362 return entry;
1363}
1364
1366{
1367 if(entry != NULL) {
1368 QRinput_free(entry->input);
1369 free(entry);
1370 }
1371}
1372
1374{
1375 QRinput_Struct *s;
1376
1377 s = (QRinput_Struct *)malloc(sizeof(QRinput_Struct));
1378 if(s == NULL) return NULL;
1379
1380 s->size = 0;
1381 s->parity = -1;
1382 s->head = NULL;
1383 s->tail = NULL;
1384
1385 return s;
1386}
1387
1388void QRinput_Struct_setParity(QRinput_Struct *s, unsigned char parity)
1389{
1390 s->parity = (int)parity;
1391}
1392
1394{
1396
1397 if(input->mqr) {
1398 errno = EINVAL;
1399 return -1;
1400 }
1401
1402 e = QRinput_InputList_newEntry(input);
1403 if(e == NULL) return -1;
1404
1405 s->size++;
1406 if(s->tail == NULL) {
1407 s->head = e;
1408 s->tail = e;
1409 } else {
1410 s->tail->next = e;
1411 s->tail = e;
1412 }
1413
1414 return s->size;
1415}
1416
1418{
1419 QRinput_InputList *list, *next;
1420
1421 if(s != NULL) {
1422 list = s->head;
1423 while(list != NULL) {
1424 next = list->next;
1426 list = next;
1427 }
1428 free(s);
1429 }
1430}
1431
1433{
1434 QRinput_InputList *list;
1435 unsigned char parity = 0;
1436
1437 list = s->head;
1438 while(list != NULL) {
1439 parity ^= QRinput_calcParity(list->input);
1440 list = list->next;
1441 }
1442
1443 QRinput_Struct_setParity(s, parity);
1444
1445 return parity;
1446}
1447
1448static int QRinput_List_shrinkEntry(QRinput_List *entry, int bytes)
1449{
1450 unsigned char *data;
1451
1452 data = (unsigned char *)malloc((size_t)bytes);
1453 if(data == NULL) return -1;
1454
1455 memcpy(data, entry->data, (size_t)bytes);
1456 free(entry->data);
1457 entry->data = data;
1458 entry->size = bytes;
1459
1460 return 0;
1461}
1462
1463STATIC_IN_RELEASE int QRinput_splitEntry(QRinput_List *entry, int bytes)
1464{
1465 QRinput_List *e;
1466 int ret;
1467
1468 e = QRinput_List_newEntry(entry->mode, entry->size - bytes, entry->data + bytes);
1469 if(e == NULL) {
1470 return -1;
1471 }
1472
1473 ret = QRinput_List_shrinkEntry(entry, bytes);
1474 if(ret < 0) {
1476 return -1;
1477 }
1478
1479 e->next = entry->next;
1480 entry->next = e;
1481
1482 return 0;
1483}
1484
1486{
1487 QRinput *p = NULL;
1488 QRinput_Struct *s = NULL;
1489 int bits, maxbits, nextbits, bytes, ret;
1490 QRinput_List *list, *next, *prev;
1491 BitStream *bstream = NULL;
1492
1493 if(input->mqr) {
1494 errno = EINVAL;
1495 return NULL;
1496 }
1497
1498 s = QRinput_Struct_new();
1499 if(s == NULL) return NULL;
1500
1501 input = QRinput_dup(input);
1502 if(input == NULL) {
1504 return NULL;
1505 }
1506
1508 maxbits = QRspec_getDataLength(input->version, input->level) * 8 - STRUCTURE_HEADER_SIZE;
1509
1510 if(maxbits <= 0) goto ABORT;
1511
1512 bstream = BitStream_new();
1513 if(bstream == NULL) goto ABORT;
1514
1515 bits = 0;
1516 list = input->head;
1517 prev = NULL;
1518 while(list != NULL) {
1519 nextbits = QRinput_estimateBitStreamSizeOfEntry(list, input->version, input->mqr);
1520 if(bits + nextbits <= maxbits) {
1521 BitStream_reset(bstream);
1522 ret = QRinput_encodeBitStream(list, bstream, input->version, input->mqr);
1523 if(ret < 0) goto ABORT;
1524 bits += ret;
1525 prev = list;
1526 list = list->next;
1527 } else {
1528 bytes = QRinput_lengthOfCode(list->mode, input->version, maxbits - bits);
1529 p = QRinput_new2(input->version, input->level);
1530 if(p == NULL) goto ABORT;
1531 if(bytes > 0) {
1532 /* Splits this entry into 2 entries. */
1533 ret = QRinput_splitEntry(list, bytes);
1534 if(ret < 0) {
1535 QRinput_free(p);
1536 goto ABORT;
1537 }
1538 /* First half is the tail of the current input. */
1539 next = list->next;
1540 list->next = NULL;
1541 /* Second half is the head of the next input, p.*/
1542 p->head = next;
1543 /* Renew QRinput.tail. */
1544 p->tail = input->tail;
1545 input->tail = list;
1546 /* Point to the next entry. */
1547 prev = list;
1548 list = next;
1549 } else {
1550 /* Current entry will go to the next input. */
1551 prev->next = NULL;
1552 p->head = list;
1553 p->tail = input->tail;
1554 input->tail = prev;
1555 }
1556 ret = QRinput_Struct_appendInput(s, input);
1557 if(ret < 0) {
1558 QRinput_free(p);
1559 goto ABORT;
1560 }
1561 input = p;
1562 bits = 0;
1563 }
1564 }
1565 ret = QRinput_Struct_appendInput(s, input);
1566 if(ret < 0) goto ABORT;
1567 if(s->size > MAX_STRUCTURED_SYMBOLS) {
1568 errno = ERANGE;
1570 BitStream_free(bstream);
1571 return NULL;
1572 }
1574 if(ret < 0) {
1576 BitStream_free(bstream);
1577 return NULL;
1578 }
1579
1580 BitStream_free(bstream);
1581 return s;
1582
1583ABORT:
1584 BitStream_free(bstream);
1585 QRinput_free(input);
1587 return NULL;
1588}
1589
1591{
1592 int i;
1593 QRinput_InputList *list;
1594
1595 if(s->size == 1) {
1596 return 0;
1597 }
1598
1599 if(s->parity < 0) {
1601 }
1602 i = 1;
1603 list = s->head;
1604 while(list != NULL) {
1606 return -1;
1607 i++;
1608 list = list->next;
1609 }
1610
1611 return 0;
1612}
1613
1614/******************************************************************************
1615 * Extended encoding mode (FNC1 and ECI)
1616 *****************************************************************************/
1617
1619{
1620 if(input->mqr) {
1621 errno = EINVAL;
1622 return -1;
1623 }
1624 input->fnc1 = 1;
1625
1626 return 0;
1627}
1628
1629int QRinput_setFNC1Second(QRinput *input, unsigned char appid)
1630{
1631 if(input->mqr) {
1632 errno = EINVAL;
1633 return -1;
1634 }
1635 input->fnc1 = 2;
1636 input->appid = appid;
1637
1638 return 0;
1639}
void BitStream_free(BitStream *bstream)
Definition: bitstream.c:225
BitStream * BitStream_new(void)
Definition: bitstream.c:33
unsigned char * BitStream_toByte(BitStream *bstream)
Definition: bitstream.c:184
int BitStream_appendNum(BitStream *bstream, size_t bits, unsigned int num)
Definition: bitstream.c:152
int BitStream_appendBytes(BitStream *bstream, size_t size, unsigned char *data)
Definition: bitstream.c:168
#define BitStream_reset(__bstream__)
Definition: bitstream.h:39
#define BitStream_size(__bstream__)
Definition: bitstream.h:38
int MQRspec_getECCLength(int version, QRecLevel level)
Return maximum error correction code length (bytes) for the version.
Definition: mqrspec.c:75
int MQRspec_maximumWords(QRencodeMode mode, int version)
Return the maximum length for the mode and version.
Definition: mqrspec.c:104
int MQRspec_lengthIndicator(QRencodeMode mode, int version)
Length indicator.
Definition: mqrspec.c:99
int MQRspec_getDataLengthBit(int version, QRecLevel level)
Return maximum data code length (bits) for the version.
Definition: mqrspec.c:59
#define MQRSPEC_MODEID_KANJI
Definition: mqrspec.h:148
#define MQRSPEC_MODEID_8
Definition: mqrspec.h:147
#define MQRSPEC_MODEID_AN
Definition: mqrspec.h:146
#define MQRSPEC_MODEID_NUM
Mode indicator.
Definition: mqrspec.h:145
static int size
Definition: qrenc.c:41
static QRecLevel level
Definition: qrenc.c:50
static int version
Definition: qrenc.c:40
QRecLevel
Level of error correction.
Definition: qrencode.h:124
@ QR_ECLEVEL_H
highest
Definition: qrencode.h:128
@ QR_ECLEVEL_L
lowest
Definition: qrencode.h:125
QRencodeMode
Encoding mode.
Definition: qrencode.h:109
@ QR_MODE_KANJI
Kanji (shift-jis) mode.
Definition: qrencode.h:114
@ QR_MODE_STRUCTURE
Internal use only.
Definition: qrencode.h:115
@ QR_MODE_FNC1FIRST
FNC1, first position.
Definition: qrencode.h:117
@ QR_MODE_ECI
ECI mode.
Definition: qrencode.h:116
@ QR_MODE_FNC1SECOND
FNC1, second position.
Definition: qrencode.h:118
@ QR_MODE_AN
Alphabet-numeric mode.
Definition: qrencode.h:112
@ QR_MODE_8
8-bit data mode
Definition: qrencode.h:113
@ QR_MODE_NUL
Terminator (NUL character). Internal use only.
Definition: qrencode.h:110
@ QR_MODE_NUM
Numeric mode.
Definition: qrencode.h:111
#define MQRSPEC_VERSION_MAX
Maximum version (size) of QR-code symbol.
Definition: qrencode.h:139
#define QRSPEC_VERSION_MAX
Maximum version (size) of QR-code symbol.
Definition: qrencode.h:134
static int QRinput_encodeModeFNC1Second(QRinput_List *entry, BitStream *bstream)
Definition: qrinput.c:741
int QRinput_estimateBitsModeNum(int size)
Estimate the length of the encoded bit stream of numeric data.
Definition: qrinput.c:381
STATIC_IN_RELEASE int QRinput_getBitStream(QRinput *input, BitStream *bstream)
Merge all bit streams in the input data and append padding bits.
Definition: qrinput.c:1303
static int QRinput_encodeModeECI(QRinput_List *entry, BitStream *bstream)
Definition: qrinput.c:787
static int QRinput_convertData(QRinput *input, BitStream *bstream)
Convert the input data to a bit stream.
Definition: qrinput.c:1112
STATIC_IN_RELEASE int QRinput_insertStructuredAppendHeader(QRinput *input, int size, int number, unsigned char parity)
Insert a structured-append header to the head of the input data.
Definition: qrinput.c:248
int QRinput_estimateBitsModeKanji(int size)
Kanji data.
Definition: qrinput.c:616
QRecLevel QRinput_getErrorCorrectionLevel(QRinput *input)
Get current error correction level.
Definition: qrinput.c:174
static int QRinput_estimateBitsModeECI(unsigned char *data)
Definition: qrinput.c:771
STATIC_IN_RELEASE int QRinput_lengthOfCode(QRencodeMode mode, int version, int bits)
Return required length in bytes for specified mode, version and bits.
Definition: qrinput.c:961
const signed char QRinput_anTable[128]
Alphabet-numeric data.
Definition: qrinput.c:459
int QRinput_getVersion(QRinput *input)
Get current version.
Definition: qrinput.c:157
QRinput * QRinput_new(void)
Input Data.
Definition: qrinput.c:111
int QRinput_isSplittableMode(QRencodeMode mode)
Utilities.
Definition: qrinput.c:39
static int QRinput_estimateBitStreamSizeOfEntry(QRinput_List *entry, int version, int mqr)
Estimation of the bit length.
Definition: qrinput.c:858
int QRinput_setVersion(QRinput *input, int version)
Set version of the QR code that is to be encoded.
Definition: qrinput.c:162
int QRinput_Struct_appendInput(QRinput_Struct *s, QRinput *input)
Append a QRinput object to the set.
Definition: qrinput.c:1393
void QRinput_free(QRinput *input)
Free the input object.
Definition: qrinput.c:294
static int QRinput_insertFNC1Header(QRinput *input)
Definition: qrinput.c:1247
QRinput_Struct * QRinput_Struct_new(void)
Instantiate a set of input data object.
Definition: qrinput.c:1373
void QRinput_Struct_setParity(QRinput_Struct *s, unsigned char parity)
Set parity of structured symbols.
Definition: qrinput.c:1388
int QRinput_check(QRencodeMode mode, int size, const unsigned char *data)
Validation.
Definition: qrinput.c:819
int QRinput_setFNC1Second(QRinput *input, unsigned char appid)
Set FNC1-2nd position flag and application identifier.
Definition: qrinput.c:1629
static void QRinput_appendEntry(QRinput *input, QRinput_List *entry)
Definition: qrinput.c:211
int QRinput_append(QRinput *input, QRencodeMode mode, int size, const unsigned char *data)
Append data to an input object.
Definition: qrinput.c:223
static int QRinput_encodeMode8(QRinput_List *entry, BitStream *bstream, int version, int mqr)
Convert the 8bits data and append to a bit stream.
Definition: qrinput.c:580
static int QRinput_appendPaddingBit(BitStream *bstream, QRinput *input)
Append padding bits for the input data.
Definition: qrinput.c:1147
static int QRinput_encodeModeAn(QRinput_List *entry, BitStream *bstream, int version, int mqr)
Convert the alphabet-numeric data and append to a bit stream.
Definition: qrinput.c:517
int QRinput_setVersionAndErrorCorrectionLevel(QRinput *input, int version, QRecLevel level)
Set version and error correction level of the QR code at once.
Definition: qrinput.c:191
static int QRinput_encodeModeStructure(QRinput_List *entry, BitStream *bstream, int mqr)
Structured Symbol.
Definition: qrinput.c:707
QRinput_Struct * QRinput_splitQRinputToStruct(QRinput *input)
Split a QRinput to QRinput_Struct.
Definition: qrinput.c:1485
unsigned char * QRinput_getByteStream(QRinput *input)
Pack all bit streams padding bits into a byte array.
Definition: qrinput.c:1326
STATIC_IN_RELEASE int QRinput_estimateBitStreamSize(QRinput *input, int version)
Estimate the length of the encoded bit stream of the data.
Definition: qrinput.c:917
STATIC_IN_RELEASE int QRinput_splitEntry(QRinput_List *entry, int bytes)
Definition: qrinput.c:1463
static QRinput_List * QRinput_List_dup(QRinput_List *entry)
Definition: qrinput.c:86
STATIC_IN_RELEASE int QRinput_mergeBitStream(QRinput *input, BitStream *bstream)
Merge all bit streams in the input data.
Definition: qrinput.c:1277
static int QRinput_checkModeNum(int size, const char *data)
Numeric data.
Definition: qrinput.c:364
int QRinput_Struct_insertStructuredAppendHeaders(QRinput_Struct *s)
Insert structured-append headers to the input structure.
Definition: qrinput.c:1590
static unsigned char QRinput_calcParity(QRinput *input)
Definition: qrinput.c:309
static int QRinput_encodeBitStream(QRinput_List *entry, BitStream *bstream, int version, int mqr)
Data conversion.
Definition: qrinput.c:1013
static int QRinput_createBitStream(QRinput *input, BitStream *bstream)
Convert the input data to a bit stream.
Definition: qrinput.c:1084
static int QRinput_encodeModeKanji(QRinput_List *entry, BitStream *bstream, int version, int mqr)
Convert the kanji data and append to a bit stream.
Definition: qrinput.c:655
static unsigned int QRinput_decodeECIfromByteArray(unsigned char *data)
ECI header.
Definition: qrinput.c:757
static int QRinput_checkModeAn(int size, const char *data)
Check the input data.
Definition: qrinput.c:476
static void QRinput_List_freeEntry(QRinput_List *entry)
Definition: qrinput.c:77
static int QRinput_List_shrinkEntry(QRinput_List *entry, int bytes)
Definition: qrinput.c:1448
int QRinput_appendECIheader(QRinput *input, unsigned int ecinum)
Append ECI header.
Definition: qrinput.c:276
int QRinput_setFNC1First(QRinput *input)
Extended encoding mode (FNC1 and ECI)
Definition: qrinput.c:1618
static void QRinput_InputList_freeEntry(QRinput_InputList *entry)
Definition: qrinput.c:1365
static QRinput_List * QRinput_List_newEntry(QRencodeMode mode, int size, const unsigned char *data)
Entry of input data.
Definition: qrinput.c:48
int QRinput_estimateBitsMode8(int size)
8 bit data
Definition: qrinput.c:566
QRinput * QRinput_new2(int version, QRecLevel level)
Instantiate an input data object.
Definition: qrinput.c:116
STATIC_IN_RELEASE int QRinput_estimateVersion(QRinput *input)
Estimate the required version number of the symbol.
Definition: qrinput.c:936
int QRinput_estimateBitsModeAn(int size)
Estimate the length of the encoded bit stream of alphabet-numeric data.
Definition: qrinput.c:493
QRinput * QRinput_dup(QRinput *input)
Definition: qrinput.c:328
static unsigned char QRinput_Struct_calcParity(QRinput_Struct *s)
Definition: qrinput.c:1432
static int QRinput_encodeModeNum(QRinput_List *entry, BitStream *bstream, int version, int mqr)
Convert the number data and append to a bit stream.
Definition: qrinput.c:411
QRinput * QRinput_newMQR(int version, QRecLevel level)
Instantiate an input data object.
Definition: qrinput.c:138
static int QRinput_appendPaddingBitMQR(BitStream *bstream, QRinput *input)
Append padding bits for the input data - Micro QR Code version.
Definition: qrinput.c:1196
void QRinput_Struct_free(QRinput_Struct *s)
Free all of QRinput in the set.
Definition: qrinput.c:1417
static int QRinput_checkModeFNC1Second(int size)
FNC1.
Definition: qrinput.c:732
static QRinput_InputList * QRinput_InputList_newEntry(QRinput *input)
Structured input data.
Definition: qrinput.c:1352
int QRinput_setErrorCorrectionLevel(QRinput *input, QRecLevel level)
Set error correction level of the QR code that is to be encoded.
Definition: qrinput.c:179
static int QRinput_checkModeKanji(int size, const unsigned char *data)
Check the input data.
Definition: qrinput.c:627
#define MAX_STRUCTURED_SYMBOLS
Maximum number of symbols in a set of structured-appended symbols.
Definition: qrinput.h:112
#define STRUCTURE_HEADER_SIZE
Length of a segment of structured-append header.
Definition: qrinput.h:107
#define QRinput_lookAnTable(__c__)
Look up the alphabet-numeric convesion table (see JIS X0510:2004, pp.19).
Definition: qrinput.h:95
#define MODE_INDICATOR_SIZE
Length of a standard mode indicator in bits.
Definition: qrinput.h:102
int QRspec_getMinimumVersion(int size, QRecLevel level)
Return a version number that satisfies the input code length.
Definition: qrspec.c:108
int QRspec_maximumWords(QRencodeMode mode, int version)
Return the maximum length for the mode and version.
Definition: qrspec.c:158
int QRspec_lengthIndicator(QRencodeMode mode, int version)
Length indicator.
Definition: qrspec.c:142
int QRspec_getDataLength(int version, QRecLevel level)
Return maximum data code length (bytes) for the version.
Definition: qrspec.c:98
#define QRSPEC_MODEID_NUM
Definition: qrspec.h:165
#define QRSPEC_MODEID_ECI
Mode indicator.
Definition: qrspec.h:164
#define QRSPEC_MODEID_8
Definition: qrspec.h:167
#define QRSPEC_MODEID_AN
Definition: qrspec.h:166
#define QRSPEC_MODEID_STRUCTURE
Definition: qrspec.h:171
#define QRSPEC_MODEID_FNC1SECOND
Definition: qrspec.h:170
#define QRSPEC_MODEID_KANJI
Definition: qrspec.h:168
QRinput_InputList * next
Definition: qrinput.h:63
QRinput * input
Definition: qrinput.h:62
int size
Size of data chunk (byte).
Definition: qrinput.h:37
QRinput_List * next
Definition: qrinput.h:40
QRencodeMode mode
Definition: qrinput.h:36
unsigned char * data
Data chunk.
Definition: qrinput.h:38
BitStream * bstream
Definition: qrinput.h:39
QRinput_InputList * head
Definition: qrinput.h:69
QRinput_InputList * tail
Definition: qrinput.h:70
int size
number of structured symbols
Definition: qrinput.h:67
Input Data.
Definition: qrinput.h:46
int fnc1
Definition: qrinput.h:52
int mqr
Definition: qrinput.h:51
QRinput_List * head
Definition: qrinput.h:49
int version
Definition: qrinput.h:47
QRecLevel level
Definition: qrinput.h:48
QRinput_List * tail
Definition: qrinput.h:50
unsigned char appid
Definition: qrinput.h:53