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