2 * qrencode - QR Code encoder
4 * Input data chunk class
5 * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
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.
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.
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
30 #include "bitstream.h"
33 /******************************************************************************
35 *****************************************************************************/
37 static QRinput_List *QRinput_List_newEntry(QRencodeMode mode, int size, const unsigned char *data)
41 if(QRinput_check(mode, size, data)) {
46 entry = (QRinput_List *)malloc(sizeof(QRinput_List));
47 if(entry == NULL) return NULL;
51 entry->data = (unsigned char *)malloc(size);
52 if(entry->data == NULL) {
56 memcpy(entry->data, data, size);
57 entry->bstream = NULL;
63 static void QRinput_List_freeEntry(QRinput_List *entry)
67 BitStream_free(entry->bstream);
72 static QRinput_List *QRinput_List_dup(QRinput_List *entry)
76 n = (QRinput_List *)malloc(sizeof(QRinput_List));
77 if(n == NULL) return NULL;
79 n->mode = entry->mode;
80 n->size = entry->size;
81 n->data = (unsigned char *)malloc(n->size);
86 memcpy(n->data, entry->data, entry->size);
93 /******************************************************************************
95 *****************************************************************************/
97 QRinput *QRinput_new(void)
99 return QRinput_new2(0, QR_ECLEVEL_L);
102 QRinput *QRinput_new2(int version, QRecLevel level)
106 if(version < 0 || version > QRSPEC_VERSION_MAX || level > QR_ECLEVEL_H) {
111 input = (QRinput *)malloc(sizeof(QRinput));
112 if(input == NULL) return NULL;
116 input->version = version;
117 input->level = level;
122 int QRinput_getVersion(QRinput *input)
124 return input->version;
127 int QRinput_setVersion(QRinput *input, int version)
129 if(version < 0 || version > QRSPEC_VERSION_MAX) {
134 input->version = version;
139 QRecLevel QRinput_getErrorCorrectionLevel(QRinput *input)
144 int QRinput_setErrorCorrectionLevel(QRinput *input, QRecLevel level)
146 if(level > QR_ECLEVEL_H) {
151 input->level = level;
156 static void QRinput_appendEntry(QRinput *input, QRinput_List *entry)
158 if(input->tail == NULL) {
162 input->tail->next = entry;
168 int QRinput_append(QRinput *input, QRencodeMode mode, int size, const unsigned char *data)
172 entry = QRinput_List_newEntry(mode, size, data);
177 QRinput_appendEntry(input, entry);
183 * Insert a structured-append header to the head of the input data.
184 * @param input input data.
185 * @param size number of structured symbols.
186 * @param index index number of the symbol. (1 <= index <= size)
187 * @param parity parity among input data. (NOTE: each symbol of a set of structured symbols has the same parity data)
189 * @retval -1 error occurred and errno is set to indeicate the error. See Execptions for the details.
190 * @throw EINVAL invalid parameter.
191 * @throw ENOMEM unable to allocate memory.
193 __STATIC int QRinput_insertStructuredAppendHeader(QRinput *input, int size, int index, unsigned char parity)
196 unsigned char buf[3];
198 if(size > MAX_STRUCTURED_SYMBOLS) {
202 if(index <= 0 || index > MAX_STRUCTURED_SYMBOLS) {
207 buf[0] = (unsigned char)size;
208 buf[1] = (unsigned char)index;
210 entry = QRinput_List_newEntry(QR_MODE_STRUCTURE, 3, buf);
215 entry->next = input->head;
221 void QRinput_free(QRinput *input)
223 QRinput_List *list, *next;
227 while(list != NULL) {
229 QRinput_List_freeEntry(list);
236 static unsigned char QRinput_calcParity(QRinput *input)
238 unsigned char parity = 0;
243 while(list != NULL) {
244 if(list->mode != QR_MODE_STRUCTURE) {
245 for(i=list->size-1; i>=0; i--) {
246 parity ^= list->data[i];
255 QRinput *QRinput_dup(QRinput *input)
258 QRinput_List *list, *e;
260 n = QRinput_new2(input->version, input->level);
261 if(n == NULL) return NULL;
264 while(list != NULL) {
265 e = QRinput_List_dup(list);
270 QRinput_appendEntry(n, e);
277 /******************************************************************************
279 *****************************************************************************/
282 * Check the input data.
287 static int QRinput_checkModeNum(int size, const char *data)
291 for(i=0; i<size; i++) {
292 if(data[i] < '0' || data[i] > '9')
300 * Estimates the length of the encoded bit stream of numeric data.
302 * @return number of bits
304 int QRinput_estimateBitsModeNum(int size)
311 switch(size - w * 3) {
326 * Convert the number data to a bit stream.
329 * @retval -1 an error occurred and errno is set to indeicate the error.
330 * See Execptions for the details.
331 * @throw ENOMEM unable to allocate memory.
333 static int QRinput_encodeModeNum(QRinput_List *entry, int version)
338 words = entry->size / 3;
339 entry->bstream = BitStream_new();
340 if(entry->bstream == NULL) return -1;
343 ret = BitStream_appendNum(entry->bstream, 4, val);
344 if(ret < 0) goto ABORT;
347 ret = BitStream_appendNum(entry->bstream, QRspec_lengthIndicator(QR_MODE_NUM, version), val);
348 if(ret < 0) goto ABORT;
350 for(i=0; i<words; i++) {
351 val = (entry->data[i*3 ] - '0') * 100;
352 val += (entry->data[i*3+1] - '0') * 10;
353 val += (entry->data[i*3+2] - '0');
355 ret = BitStream_appendNum(entry->bstream, 10, val);
356 if(ret < 0) goto ABORT;
359 if(entry->size - words * 3 == 1) {
360 val = entry->data[words*3] - '0';
361 ret = BitStream_appendNum(entry->bstream, 4, val);
362 if(ret < 0) goto ABORT;
363 } else if(entry->size - words * 3 == 2) {
364 val = (entry->data[words*3 ] - '0') * 10;
365 val += (entry->data[words*3+1] - '0');
366 BitStream_appendNum(entry->bstream, 7, val);
367 if(ret < 0) goto ABORT;
372 BitStream_free(entry->bstream);
373 entry->bstream = NULL;
377 /******************************************************************************
378 * Alphabet-numeric data
379 *****************************************************************************/
381 const signed char QRinput_anTable[128] = {
382 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
383 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
384 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43,
385 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1,
386 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
387 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
388 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
389 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
393 * Check the input data.
398 static int QRinput_checkModeAn(int size, const char *data)
402 for(i=0; i<size; i++) {
403 if(QRinput_lookAnTable(data[i]) < 0)
411 * Estimates the length of the encoded bit stream of alphabet-numeric data.
413 * @return number of bits
415 int QRinput_estimateBitsModeAn(int size)
430 * Convert the alphabet-numeric data to a bit stream.
433 * @retval -1 an error occurred and errno is set to indeicate the error.
434 * See Execptions for the details.
435 * @throw ENOMEM unable to allocate memory.
437 static int QRinput_encodeModeAn(QRinput_List *entry, int version)
442 words = entry->size / 2;
443 entry->bstream = BitStream_new();
444 if(entry->bstream == NULL) return -1;
447 ret = BitStream_appendNum(entry->bstream, 4, val);
448 if(ret < 0) goto ABORT;
451 ret = BitStream_appendNum(entry->bstream, QRspec_lengthIndicator(QR_MODE_AN, version), val);
452 if(ret < 0) goto ABORT;
454 for(i=0; i<words; i++) {
455 val = (unsigned int)QRinput_lookAnTable(entry->data[i*2 ]) * 45;
456 val += (unsigned int)QRinput_lookAnTable(entry->data[i*2+1]);
458 ret = BitStream_appendNum(entry->bstream, 11, val);
459 if(ret < 0) goto ABORT;
462 if(entry->size & 1) {
463 val = (unsigned int)QRinput_lookAnTable(entry->data[words * 2]);
465 ret = BitStream_appendNum(entry->bstream, 6, val);
466 if(ret < 0) goto ABORT;
471 BitStream_free(entry->bstream);
472 entry->bstream = NULL;
476 /******************************************************************************
478 *****************************************************************************/
481 * Estimates the length of the encoded bit stream of 8 bit data.
483 * @return number of bits
485 int QRinput_estimateBitsMode8(int size)
491 * Convert the 8bits data to a bit stream.
494 * @retval -1 an error occurred and errno is set to indeicate the error.
495 * See Execptions for the details.
496 * @throw ENOMEM unable to allocate memory.
498 static int QRinput_encodeMode8(QRinput_List *entry, int version)
503 entry->bstream = BitStream_new();
504 if(entry->bstream == NULL) return -1;
507 ret = BitStream_appendNum(entry->bstream, 4, val);
508 if(ret < 0) goto ABORT;
511 ret = BitStream_appendNum(entry->bstream, QRspec_lengthIndicator(QR_MODE_8, version), val);
512 if(ret < 0) goto ABORT;
514 for(i=0; i<entry->size; i++) {
515 ret = BitStream_appendNum(entry->bstream, 8, entry->data[i]);
516 if(ret < 0) goto ABORT;
521 BitStream_free(entry->bstream);
522 entry->bstream = NULL;
527 /******************************************************************************
529 *****************************************************************************/
532 * Estimates the length of the encoded bit stream of kanji data.
534 * @return number of bits
536 int QRinput_estimateBitsModeKanji(int size)
538 return (size / 2) * 13;
542 * Check the input data.
547 static int QRinput_checkModeKanji(int size, const unsigned char *data)
555 for(i=0; i<size; i+=2) {
556 val = ((unsigned int)data[i] << 8) | data[i+1];
557 if(val < 0x8140 || (val > 0x9ffc && val < 0xe040) || val > 0xebbf) {
566 * Convert the kanji data to a bit stream.
569 * @retval -1 an error occurred and errno is set to indeicate the error.
570 * See Execptions for the details.
571 * @throw ENOMEM unable to allocate memory.
573 static int QRinput_encodeModeKanji(QRinput_List *entry, int version)
578 entry->bstream = BitStream_new();
579 if(entry->bstream == NULL) return -1;
582 ret = BitStream_appendNum(entry->bstream, 4, val);
583 if(ret < 0) goto ABORT;
585 val = entry->size / 2;
586 ret = BitStream_appendNum(entry->bstream, QRspec_lengthIndicator(QR_MODE_KANJI, version), val);
587 if(ret < 0) goto ABORT;
589 for(i=0; i<entry->size; i+=2) {
590 val = ((unsigned int)entry->data[i] << 8) | entry->data[i+1];
596 h = (val >> 8) * 0xc0;
597 val = (val & 0xff) + h;
599 ret = BitStream_appendNum(entry->bstream, 13, val);
600 if(ret < 0) goto ABORT;
605 BitStream_free(entry->bstream);
606 entry->bstream = NULL;
610 /******************************************************************************
612 *****************************************************************************/
615 * Convert a structure symbol code to a bit stream.
618 * @retval -1 an error occurred and errno is set to indeicate the error.
619 * See Execptions for the details.
620 * @throw ENOMEM unable to allocate memory.
622 static int QRinput_encodeModeStructure(QRinput_List *entry)
626 entry->bstream = BitStream_new();
627 if(entry->bstream == NULL) return -1;
629 ret = BitStream_appendNum(entry->bstream, 4, 0x03);
630 if(ret < 0) goto ABORT;
631 ret = BitStream_appendNum(entry->bstream, 4, entry->data[1] - 1);
632 if(ret < 0) goto ABORT;
633 ret = BitStream_appendNum(entry->bstream, 4, entry->data[0] - 1);
634 if(ret < 0) goto ABORT;
635 ret = BitStream_appendNum(entry->bstream, 8, entry->data[2]);
636 if(ret < 0) goto ABORT;
640 BitStream_free(entry->bstream);
641 entry->bstream = NULL;
645 /******************************************************************************
647 *****************************************************************************/
649 int QRinput_check(QRencodeMode mode, int size, const unsigned char *data)
651 if(size <= 0) return -1;
655 return QRinput_checkModeNum(size, (const char *)data);
658 return QRinput_checkModeAn(size, (const char *)data);
661 return QRinput_checkModeKanji(size, data);
666 case QR_MODE_STRUCTURE:
676 /******************************************************************************
677 * Estimation of the bit length
678 *****************************************************************************/
681 * Estimates the length of the encoded bit stream on the current version.
683 * @param version version of the symbol
684 * @return number of bits
686 static int QRinput_estimateBitStreamSizeOfEntry(QRinput_List *entry, int version)
692 if(version == 0) version = 1;
694 switch(entry->mode) {
696 bits = QRinput_estimateBitsModeNum(entry->size);
699 bits = QRinput_estimateBitsModeAn(entry->size);
702 bits = QRinput_estimateBitsMode8(entry->size);
705 bits = QRinput_estimateBitsModeKanji(entry->size);
707 case QR_MODE_STRUCTURE:
708 return STRUCTURE_HEADER_BITS;
713 l = QRspec_lengthIndicator(entry->mode, version);
715 num = (entry->size + m - 1) / m;
717 bits += num * (4 + l); // mode indicator (4bits) + length indicator
723 * Estimates the length of the encoded bit stream of the data.
724 * @param input input data
725 * @param version version of the symbol
726 * @return number of bits
728 __STATIC int QRinput_estimateBitStreamSize(QRinput *input, int version)
734 while(list != NULL) {
735 bits += QRinput_estimateBitStreamSizeOfEntry(list, version);
743 * Estimates the required version number of the symbol.
744 * @param input input data
745 * @return required version number
747 static int QRinput_estimateVersion(QRinput *input)
755 bits = QRinput_estimateBitStreamSize(input, prev);
756 version = QRspec_getMinimumVersion((bits + 7) / 8, input->level);
760 } while (version > prev);
766 * Returns required length in bytes for specified mode, version and bits.
770 * @return required length of code words in bytes.
772 __STATIC int QRinput_lengthOfCode(QRencodeMode mode, int version, int bits)
774 int payload, size, chunks, remain, maxsize;
776 payload = bits - 4 - QRspec_lengthIndicator(mode, version);
779 chunks = payload / 10;
780 remain = payload - chunks * 10;
784 } else if(remain >= 4) {
789 chunks = payload / 11;
790 remain = payload - chunks * 11;
792 if(remain >= 6) size++;
798 size = (payload / 13) * 2;
800 case QR_MODE_STRUCTURE:
807 maxsize = QRspec_maximumWords(mode, version);
808 if(size < 0) size = 0;
809 if(size > maxsize) size = maxsize;
814 /******************************************************************************
816 *****************************************************************************/
819 * Convert the input data in the data chunk to a bit stream.
821 * @return number of bits (>0) or -1 for failure.
823 static int QRinput_encodeBitStream(QRinput_List *entry, int version)
826 QRinput_List *st1 = NULL, *st2 = NULL;
828 if(entry->bstream != NULL) {
829 BitStream_free(entry->bstream);
830 entry->bstream = NULL;
833 words = QRspec_maximumWords(entry->mode, version);
834 if(entry->size > words) {
835 st1 = QRinput_List_newEntry(entry->mode, words, entry->data);
836 if(st1 == NULL) goto ABORT;
837 st2 = QRinput_List_newEntry(entry->mode, entry->size - words, &entry->data[words]);
838 if(st2 == NULL) goto ABORT;
840 ret = QRinput_encodeBitStream(st1, version);
841 if(ret < 0) goto ABORT;
842 ret = QRinput_encodeBitStream(st2, version);
843 if(ret < 0) goto ABORT;
844 entry->bstream = BitStream_new();
845 if(entry->bstream == NULL) goto ABORT;
846 ret = BitStream_append(entry->bstream, st1->bstream);
847 if(ret < 0) goto ABORT;
848 ret = BitStream_append(entry->bstream, st2->bstream);
849 if(ret < 0) goto ABORT;
850 QRinput_List_freeEntry(st1);
851 QRinput_List_freeEntry(st2);
854 switch(entry->mode) {
856 ret = QRinput_encodeModeNum(entry, version);
859 ret = QRinput_encodeModeAn(entry, version);
862 ret = QRinput_encodeMode8(entry, version);
865 ret = QRinput_encodeModeKanji(entry, version);
867 case QR_MODE_STRUCTURE:
868 ret = QRinput_encodeModeStructure(entry);
873 if(ret < 0) return -1;
876 return BitStream_size(entry->bstream);
878 QRinput_List_freeEntry(st1);
879 QRinput_List_freeEntry(st2);
884 * Convert the input data to a bit stream.
885 * @param input input data.
887 * @retval -1 an error occurred and errno is set to indeicate the error.
888 * See Execptions for the details.
889 * @throw ENOMEM unable to allocate memory.
891 static int QRinput_createBitStream(QRinput *input)
897 while(list != NULL) {
898 bits = QRinput_encodeBitStream(list, input->version);
899 if(bits < 0) return -1;
908 * Convert the input data to a bit stream.
909 * When the version number is given and that is not sufficient, it is increased
911 * @param input input data.
913 * @retval -1 an error occurred and errno is set to indeicate the error.
914 * See Execptions for the details.
915 * @throw ENOMEM unable to allocate memory.
916 * @throw EINVAL input is too large.
918 static int QRinput_convertData(QRinput *input)
923 ver = QRinput_estimateVersion(input);
924 if(ver > QRinput_getVersion(input)) {
925 QRinput_setVersion(input, ver);
929 bits = QRinput_createBitStream(input);
930 if(bits < 0) return -1;
931 ver = QRspec_getMinimumVersion((bits + 7) / 8, input->level);
935 } else if(ver > QRinput_getVersion(input)) {
936 QRinput_setVersion(input, ver);
946 * Append padding bits for the input data.
947 * @param bstream Bitstream to be appended.
948 * @param input input data.
950 * @retval -1 an error occurred and errno is set to indeicate the error.
951 * See Execptions for the details.
952 * @throw ENOMEM unable to allocate memory.
954 static int QRinput_appendPaddingBit(BitStream *bstream, QRinput *input)
956 int bits, maxbits, words, maxwords, i, ret;
957 BitStream *padding = NULL;
958 unsigned char *padbuf;
961 bits = BitStream_size(bstream);
962 maxwords = QRspec_getDataLength(input->version, input->level);
963 maxbits = maxwords * 8;
965 if(maxbits == bits) {
969 if(maxbits - bits < 5) {
970 ret = BitStream_appendNum(bstream, maxbits - bits, 0);
975 words = (bits + 7) / 8;
977 padding = BitStream_new();
978 if(padding == NULL) return -1;
979 ret = BitStream_appendNum(padding, words * 8 - bits + 4, 0);
980 if(ret < 0) goto DONE;
982 padlen = maxwords - words;
984 padbuf = (unsigned char *)malloc(padlen);
989 for(i=0; i<padlen; i++) {
990 padbuf[i] = (i&1)?0x11:0xec;
992 ret = BitStream_appendBytes(padding, padlen, padbuf);
999 ret = BitStream_append(bstream, padding);
1002 BitStream_free(padding);
1007 * Merge all bit streams in the input data.
1008 * @param input input data.
1009 * @return merged bit stream
1012 __STATIC BitStream *QRinput_mergeBitStream(QRinput *input)
1018 if(QRinput_convertData(input) < 0) {
1022 bstream = BitStream_new();
1023 if(bstream == NULL) return NULL;
1026 while(list != NULL) {
1027 ret = BitStream_append(bstream, list->bstream);
1029 BitStream_free(bstream);
1039 * Merge all bit streams in the input data and append padding bits
1040 * @param input input data.
1041 * @return padded merged bit stream
1044 __STATIC BitStream *QRinput_getBitStream(QRinput *input)
1049 bstream = QRinput_mergeBitStream(input);
1050 if(bstream == NULL) {
1053 ret = QRinput_appendPaddingBit(bstream, input);
1055 BitStream_free(bstream);
1063 * Pack all bit streams padding bits into a byte array.
1064 * @param input input data.
1065 * @return padded merged byte stream
1068 unsigned char *QRinput_getByteStream(QRinput *input)
1071 unsigned char *array;
1073 bstream = QRinput_getBitStream(input);
1074 if(bstream == NULL) {
1077 array = BitStream_toByte(bstream);
1078 BitStream_free(bstream);
1083 /******************************************************************************
1084 * Structured input data
1085 *****************************************************************************/
1087 static QRinput_InputList *QRinput_InputList_newEntry(QRinput *input)
1089 QRinput_InputList *entry;
1091 entry = (QRinput_InputList *)malloc(sizeof(QRinput_InputList));
1092 if(entry == NULL) return NULL;
1094 entry->input = input;
1100 static void QRinput_InputList_freeEntry(QRinput_InputList *entry)
1103 QRinput_free(entry->input);
1108 QRinput_Struct *QRinput_Struct_new(void)
1112 s = (QRinput_Struct *)malloc(sizeof(QRinput_Struct));
1113 if(s == NULL) return NULL;
1123 void QRinput_Struct_setParity(QRinput_Struct *s, unsigned char parity)
1125 s->parity = (int)parity;
1128 int QRinput_Struct_appendInput(QRinput_Struct *s, QRinput *input)
1130 QRinput_InputList *e;
1132 e = QRinput_InputList_newEntry(input);
1133 if(e == NULL) return -1;
1136 if(s->tail == NULL) {
1147 void QRinput_Struct_free(QRinput_Struct *s)
1149 QRinput_InputList *list, *next;
1153 while(list != NULL) {
1155 QRinput_InputList_freeEntry(list);
1162 static unsigned char QRinput_Struct_calcParity(QRinput_Struct *s)
1164 QRinput_InputList *list;
1165 unsigned char parity = 0;
1168 while(list != NULL) {
1169 parity ^= QRinput_calcParity(list->input);
1173 QRinput_Struct_setParity(s, parity);
1178 static int QRinput_List_shrinkEntry(QRinput_List *entry, int bytes)
1180 unsigned char *data;
1182 data = (unsigned char *)malloc(bytes);
1183 if(data == NULL) return -1;
1185 memcpy(data, entry->data, bytes);
1188 entry->size = bytes;
1193 __STATIC int QRinput_splitEntry(QRinput_List *entry, int bytes)
1198 e = QRinput_List_newEntry(entry->mode, entry->size - bytes, entry->data + bytes);
1203 ret = QRinput_List_shrinkEntry(entry, bytes);
1205 QRinput_List_freeEntry(e);
1209 e->next = entry->next;
1215 QRinput_Struct *QRinput_splitQRinputToStruct(QRinput *input)
1219 int bits, maxbits, nextbits, bytes, ret;
1220 QRinput_List *list, *next, *prev;
1222 s = QRinput_Struct_new();
1223 if(s == NULL) return NULL;
1225 input = QRinput_dup(input);
1227 QRinput_Struct_free(s);
1231 QRinput_Struct_setParity(s, QRinput_calcParity(input));
1232 maxbits = QRspec_getDataLength(input->version, input->level) * 8 - STRUCTURE_HEADER_BITS;
1235 QRinput_Struct_free(s);
1236 QRinput_free(input);
1243 while(list != NULL) {
1244 nextbits = QRinput_estimateBitStreamSizeOfEntry(list, input->version);
1245 if(bits + nextbits <= maxbits) {
1246 ret = QRinput_encodeBitStream(list, input->version);
1247 if(ret < 0) goto ABORT;
1252 bytes = QRinput_lengthOfCode(list->mode, input->version, maxbits - bits);
1254 /* Splits this entry into 2 entries. */
1255 ret = QRinput_splitEntry(list, bytes);
1256 if(ret < 0) goto ABORT;
1257 /* First half is the tail of the current input. */
1260 /* Second half is the head of the next input, p.*/
1261 p = QRinput_new2(input->version, input->level);
1262 if(p == NULL) goto ABORT;
1264 /* Renew QRinput.tail. */
1265 p->tail = input->tail;
1267 /* Point to the next entry. */
1271 /* Current entry will go to the next input. */
1273 p = QRinput_new2(input->version, input->level);
1274 if(p == NULL) goto ABORT;
1276 p->tail = input->tail;
1279 ret = QRinput_Struct_appendInput(s, input);
1280 if(ret < 0) goto ABORT;
1285 QRinput_Struct_appendInput(s, input);
1286 if(s->size > MAX_STRUCTURED_SYMBOLS) {
1287 QRinput_Struct_free(s);
1291 ret = QRinput_Struct_insertStructuredAppendHeaders(s);
1293 QRinput_Struct_free(s);
1300 QRinput_free(input);
1301 QRinput_Struct_free(s);
1305 int QRinput_Struct_insertStructuredAppendHeaders(QRinput_Struct *s)
1308 QRinput_InputList *list;
1311 QRinput_Struct_calcParity(s);
1315 while(list != NULL) {
1321 while(list != NULL) {
1322 if(QRinput_insertStructuredAppendHeader(list->input, num, i, s->parity))