| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335 |
- /*
- * qrencode - QR Code encoder
- *
- * Input data chunk class
- * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- //#include "config.h"
- #include "qrencode.h"
- #include "qrspec.h"
- #include "bitstream.h"
- #include "qrinput.h"
- /******************************************************************************
- * Entry of input data
- *****************************************************************************/
- static QRinput_List *QRinput_List_newEntry(QRencodeMode mode, int size, const unsigned char *data)
- {
- QRinput_List *entry;
- if(QRinput_check(mode, size, data)) {
- errno = EINVAL;
- return NULL;
- }
- entry = (QRinput_List *)malloc(sizeof(QRinput_List));
- if(entry == NULL) return NULL;
- entry->mode = mode;
- entry->size = size;
- entry->data = (unsigned char *)malloc(size);
- if(entry->data == NULL) {
- free(entry);
- return NULL;
- }
- memcpy(entry->data, data, size);
- entry->bstream = NULL;
- entry->next = NULL;
- return entry;
- }
- static void QRinput_List_freeEntry(QRinput_List *entry)
- {
- if(entry != NULL) {
- free(entry->data);
- BitStream_free(entry->bstream);
- free(entry);
- }
- }
- static QRinput_List *QRinput_List_dup(QRinput_List *entry)
- {
- QRinput_List *n;
- n = (QRinput_List *)malloc(sizeof(QRinput_List));
- if(n == NULL) return NULL;
- n->mode = entry->mode;
- n->size = entry->size;
- n->data = (unsigned char *)malloc(n->size);
- if(n->data == NULL) {
- free(n);
- return NULL;
- }
- memcpy(n->data, entry->data, entry->size);
- n->bstream = NULL;
- n->next = NULL;
- return n;
- }
- /******************************************************************************
- * Input Data
- *****************************************************************************/
- QRinput *QRinput_new(void)
- {
- return QRinput_new2(0, QR_ECLEVEL_L);
- }
- QRinput *QRinput_new2(int version, QRecLevel level)
- {
- QRinput *input;
- if(version < 0 || version > QRSPEC_VERSION_MAX || level > QR_ECLEVEL_H) {
- errno = EINVAL;
- return NULL;
- }
- input = (QRinput *)malloc(sizeof(QRinput));
- if(input == NULL) return NULL;
- input->head = NULL;
- input->tail = NULL;
- input->version = version;
- input->level = level;
- return input;
- }
- int QRinput_getVersion(QRinput *input)
- {
- return input->version;
- }
- int QRinput_setVersion(QRinput *input, int version)
- {
- if(version < 0 || version > QRSPEC_VERSION_MAX) {
- errno = EINVAL;
- return -1;
- }
- input->version = version;
- return 0;
- }
- QRecLevel QRinput_getErrorCorrectionLevel(QRinput *input)
- {
- return input->level;
- }
- int QRinput_setErrorCorrectionLevel(QRinput *input, QRecLevel level)
- {
- if(level > QR_ECLEVEL_H) {
- errno = EINVAL;
- return -1;
- }
- input->level = level;
- return 0;
- }
- static void QRinput_appendEntry(QRinput *input, QRinput_List *entry)
- {
- if(input->tail == NULL) {
- input->head = entry;
- input->tail = entry;
- } else {
- input->tail->next = entry;
- input->tail = entry;
- }
- entry->next = NULL;
- }
- int QRinput_append(QRinput *input, QRencodeMode mode, int size, const unsigned char *data)
- {
- QRinput_List *entry;
- entry = QRinput_List_newEntry(mode, size, data);
- if(entry == NULL) {
- return -1;
- }
- QRinput_appendEntry(input, entry);
- return 0;
- }
- /**
- * Insert a structured-append header to the head of the input data.
- * @param input input data.
- * @param size number of structured symbols.
- * @param index index number of the symbol. (1 <= index <= size)
- * @param parity parity among input data. (NOTE: each symbol of a set of structured symbols has the same parity data)
- * @retval 0 success.
- * @retval -1 error occurred and errno is set to indeicate the error. See Execptions for the details.
- * @throw EINVAL invalid parameter.
- * @throw ENOMEM unable to allocate memory.
- */
- //__STATIC
- static int QRinput_insertStructuredAppendHeader(QRinput *input, int size, int index, unsigned char parity)
- {
- QRinput_List *entry;
- unsigned char buf[3];
- if(size > MAX_STRUCTURED_SYMBOLS) {
- errno = EINVAL;
- return -1;
- }
- if(index <= 0 || index > MAX_STRUCTURED_SYMBOLS) {
- errno = EINVAL;
- return -1;
- }
- buf[0] = (unsigned char)size;
- buf[1] = (unsigned char)index;
- buf[2] = parity;
- entry = QRinput_List_newEntry(QR_MODE_STRUCTURE, 3, buf);
- if(entry == NULL) {
- return -1;
- }
- entry->next = input->head;
- input->head = entry;
- return 0;
- }
- void QRinput_free(QRinput *input)
- {
- QRinput_List *list, *next;
- if(input != NULL) {
- list = input->head;
- while(list != NULL) {
- next = list->next;
- QRinput_List_freeEntry(list);
- list = next;
- }
- free(input);
- }
- }
- static unsigned char QRinput_calcParity(QRinput *input)
- {
- unsigned char parity = 0;
- QRinput_List *list;
- int i;
- list = input->head;
- while(list != NULL) {
- if(list->mode != QR_MODE_STRUCTURE) {
- for(i=list->size-1; i>=0; i--) {
- parity ^= list->data[i];
- }
- }
- list = list->next;
- }
- return parity;
- }
- QRinput *QRinput_dup(QRinput *input)
- {
- QRinput *n;
- QRinput_List *list, *e;
- n = QRinput_new2(input->version, input->level);
- if(n == NULL) return NULL;
- list = input->head;
- while(list != NULL) {
- e = QRinput_List_dup(list);
- if(e == NULL) {
- QRinput_free(n);
- return NULL;
- }
- QRinput_appendEntry(n, e);
- list = list->next;
- }
- return n;
- }
- /******************************************************************************
- * Numeric data
- *****************************************************************************/
- /**
- * Check the input data.
- * @param size
- * @param data
- * @return result
- */
- static int QRinput_checkModeNum(int size, const char *data)
- {
- int i;
- for(i=0; i<size; i++) {
- if(data[i] < '0' || data[i] > '9')
- return -1;
- }
- return 0;
- }
- /**
- * Estimates the length of the encoded bit stream of numeric data.
- * @param size
- * @return number of bits
- */
- int QRinput_estimateBitsModeNum(int size)
- {
- int w;
- int bits;
- w = size / 3;
- bits = w * 10;
- switch(size - w * 3) {
- case 1:
- bits += 4;
- break;
- case 2:
- bits += 7;
- break;
- default:
- break;
- }
- return bits;
- }
- /**
- * Convert the number data to a bit stream.
- * @param entry
- * @retval 0 success
- * @retval -1 an error occurred and errno is set to indeicate the error.
- * See Execptions for the details.
- * @throw ENOMEM unable to allocate memory.
- */
- static int QRinput_encodeModeNum(QRinput_List *entry, int version)
- {
- int words, i, ret;
- unsigned int val;
- words = entry->size / 3;
- entry->bstream = BitStream_new();
- if(entry->bstream == NULL) return -1;
- val = 0x1;
- ret = BitStream_appendNum(entry->bstream, 4, val);
- if(ret < 0) goto ABORT;
-
- val = entry->size;
- ret = BitStream_appendNum(entry->bstream, QRspec_lengthIndicator(QR_MODE_NUM, version), val);
- if(ret < 0) goto ABORT;
- for(i=0; i<words; i++) {
- val = (entry->data[i*3 ] - '0') * 100;
- val += (entry->data[i*3+1] - '0') * 10;
- val += (entry->data[i*3+2] - '0');
- ret = BitStream_appendNum(entry->bstream, 10, val);
- if(ret < 0) goto ABORT;
- }
- if(entry->size - words * 3 == 1) {
- val = entry->data[words*3] - '0';
- ret = BitStream_appendNum(entry->bstream, 4, val);
- if(ret < 0) goto ABORT;
- } else if(entry->size - words * 3 == 2) {
- val = (entry->data[words*3 ] - '0') * 10;
- val += (entry->data[words*3+1] - '0');
- BitStream_appendNum(entry->bstream, 7, val);
- if(ret < 0) goto ABORT;
- }
- return 0;
- ABORT:
- BitStream_free(entry->bstream);
- entry->bstream = NULL;
- return -1;
- }
- /******************************************************************************
- * Alphabet-numeric data
- *****************************************************************************/
- const signed char QRinput_anTable[128] = {
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43,
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1,
- -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
- 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
- };
- /**
- * Check the input data.
- * @param size
- * @param data
- * @return result
- */
- static int QRinput_checkModeAn(int size, const char *data)
- {
- int i;
- for(i=0; i<size; i++) {
- if(QRinput_lookAnTable(data[i]) < 0)
- return -1;
- }
- return 0;
- }
- /**
- * Estimates the length of the encoded bit stream of alphabet-numeric data.
- * @param size
- * @return number of bits
- */
- int QRinput_estimateBitsModeAn(int size)
- {
- int w;
- int bits;
- w = size / 2;
- bits = w * 11;
- if(size & 1) {
- bits += 6;
- }
- return bits;
- }
- /**
- * Convert the alphabet-numeric data to a bit stream.
- * @param entry
- * @retval 0 success
- * @retval -1 an error occurred and errno is set to indeicate the error.
- * See Execptions for the details.
- * @throw ENOMEM unable to allocate memory.
- */
- static int QRinput_encodeModeAn(QRinput_List *entry, int version)
- {
- int words, i, ret;
- unsigned int val;
- words = entry->size / 2;
- entry->bstream = BitStream_new();
- if(entry->bstream == NULL) return -1;
- val = 0x2;
- ret = BitStream_appendNum(entry->bstream, 4, val);
- if(ret < 0) goto ABORT;
-
- val = entry->size;
- ret = BitStream_appendNum(entry->bstream, QRspec_lengthIndicator(QR_MODE_AN, version), val);
- if(ret < 0) goto ABORT;
- for(i=0; i<words; i++) {
- val = (unsigned int)QRinput_lookAnTable(entry->data[i*2 ]) * 45;
- val += (unsigned int)QRinput_lookAnTable(entry->data[i*2+1]);
- ret = BitStream_appendNum(entry->bstream, 11, val);
- if(ret < 0) goto ABORT;
- }
- if(entry->size & 1) {
- val = (unsigned int)QRinput_lookAnTable(entry->data[words * 2]);
- ret = BitStream_appendNum(entry->bstream, 6, val);
- if(ret < 0) goto ABORT;
- }
- return 0;
- ABORT:
- BitStream_free(entry->bstream);
- entry->bstream = NULL;
- return -1;
- }
- /******************************************************************************
- * 8 bit data
- *****************************************************************************/
- /**
- * Estimates the length of the encoded bit stream of 8 bit data.
- * @param size
- * @return number of bits
- */
- int QRinput_estimateBitsMode8(int size)
- {
- return size * 8;
- }
- /**
- * Convert the 8bits data to a bit stream.
- * @param entry
- * @retval 0 success
- * @retval -1 an error occurred and errno is set to indeicate the error.
- * See Execptions for the details.
- * @throw ENOMEM unable to allocate memory.
- */
- static int QRinput_encodeMode8(QRinput_List *entry, int version)
- {
- int ret, i;
- unsigned int val;
- entry->bstream = BitStream_new();
- if(entry->bstream == NULL) return -1;
- val = 0x4;
- ret = BitStream_appendNum(entry->bstream, 4, val);
- if(ret < 0) goto ABORT;
-
- val = entry->size;
- ret = BitStream_appendNum(entry->bstream, QRspec_lengthIndicator(QR_MODE_8, version), val);
- if(ret < 0) goto ABORT;
- for(i=0; i<entry->size; i++) {
- ret = BitStream_appendNum(entry->bstream, 8, entry->data[i]);
- if(ret < 0) goto ABORT;
- }
- return 0;
- ABORT:
- BitStream_free(entry->bstream);
- entry->bstream = NULL;
- return -1;
- }
- /******************************************************************************
- * Kanji data
- *****************************************************************************/
- /**
- * Estimates the length of the encoded bit stream of kanji data.
- * @param size
- * @return number of bits
- */
- int QRinput_estimateBitsModeKanji(int size)
- {
- return (size / 2) * 13;
- }
- /**
- * Check the input data.
- * @param size
- * @param data
- * @return result
- */
- static int QRinput_checkModeKanji(int size, const unsigned char *data)
- {
- int i;
- unsigned int val;
- if(size & 1)
- return -1;
- for(i=0; i<size; i+=2) {
- val = ((unsigned int)data[i] << 8) | data[i+1];
- if(val < 0x8140 || (val > 0x9ffc && val < 0xe040) || val > 0xebbf) {
- return -1;
- }
- }
- return 0;
- }
- /**
- * Convert the kanji data to a bit stream.
- * @param entry
- * @retval 0 success
- * @retval -1 an error occurred and errno is set to indeicate the error.
- * See Execptions for the details.
- * @throw ENOMEM unable to allocate memory.
- */
- static int QRinput_encodeModeKanji(QRinput_List *entry, int version)
- {
- int ret, i;
- unsigned int val, h;
- entry->bstream = BitStream_new();
- if(entry->bstream == NULL) return -1;
- val = 0x8;
- ret = BitStream_appendNum(entry->bstream, 4, val);
- if(ret < 0) goto ABORT;
-
- val = entry->size / 2;
- ret = BitStream_appendNum(entry->bstream, QRspec_lengthIndicator(QR_MODE_KANJI, version), val);
- if(ret < 0) goto ABORT;
- for(i=0; i<entry->size; i+=2) {
- val = ((unsigned int)entry->data[i] << 8) | entry->data[i+1];
- if(val <= 0x9ffc) {
- val -= 0x8140;
- } else {
- val -= 0xc140;
- }
- h = (val >> 8) * 0xc0;
- val = (val & 0xff) + h;
- ret = BitStream_appendNum(entry->bstream, 13, val);
- if(ret < 0) goto ABORT;
- }
- return 0;
- ABORT:
- BitStream_free(entry->bstream);
- entry->bstream = NULL;
- return -1;
- }
- /******************************************************************************
- * Structured Symbol
- *****************************************************************************/
- /**
- * Convert a structure symbol code to a bit stream.
- * @param entry
- * @retval 0 success
- * @retval -1 an error occurred and errno is set to indeicate the error.
- * See Execptions for the details.
- * @throw ENOMEM unable to allocate memory.
- */
- static int QRinput_encodeModeStructure(QRinput_List *entry)
- {
- int ret;
- entry->bstream = BitStream_new();
- if(entry->bstream == NULL) return -1;
- ret = BitStream_appendNum(entry->bstream, 4, 0x03);
- if(ret < 0) goto ABORT;
- ret = BitStream_appendNum(entry->bstream, 4, entry->data[1] - 1);
- if(ret < 0) goto ABORT;
- ret = BitStream_appendNum(entry->bstream, 4, entry->data[0] - 1);
- if(ret < 0) goto ABORT;
- ret = BitStream_appendNum(entry->bstream, 8, entry->data[2]);
- if(ret < 0) goto ABORT;
- return 0;
- ABORT:
- BitStream_free(entry->bstream);
- entry->bstream = NULL;
- return -1;
- }
- /******************************************************************************
- * Validation
- *****************************************************************************/
- int QRinput_check(QRencodeMode mode, int size, const unsigned char *data)
- {
- if(size <= 0) return -1;
- switch(mode) {
- case QR_MODE_NUM:
- return QRinput_checkModeNum(size, (const char *)data);
- break;
- case QR_MODE_AN:
- return QRinput_checkModeAn(size, (const char *)data);
- break;
- case QR_MODE_KANJI:
- return QRinput_checkModeKanji(size, data);
- break;
- case QR_MODE_8:
- return 0;
- break;
- case QR_MODE_STRUCTURE:
- return 0;
- break;
- default:
- break;
- }
- return -1;
- }
- /******************************************************************************
- * Estimation of the bit length
- *****************************************************************************/
- /**
- * Estimates the length of the encoded bit stream on the current version.
- * @param entry
- * @param version version of the symbol
- * @return number of bits
- */
- static int QRinput_estimateBitStreamSizeOfEntry(QRinput_List *entry, int version)
- {
- int bits = 0;
- int l, m;
- int num;
- if(version == 0) version = 1;
- switch(entry->mode) {
- case QR_MODE_NUM:
- bits = QRinput_estimateBitsModeNum(entry->size);
- break;
- case QR_MODE_AN:
- bits = QRinput_estimateBitsModeAn(entry->size);
- break;
- case QR_MODE_8:
- bits = QRinput_estimateBitsMode8(entry->size);
- break;
- case QR_MODE_KANJI:
- bits = QRinput_estimateBitsModeKanji(entry->size);
- break;
- case QR_MODE_STRUCTURE:
- return STRUCTURE_HEADER_BITS;
- default:
- return 0;
- }
- l = QRspec_lengthIndicator(entry->mode, version);
- m = 1 << l;
- num = (entry->size + m - 1) / m;
- bits += num * (4 + l); // mode indicator (4bits) + length indicator
- return bits;
- }
- /**
- * Estimates the length of the encoded bit stream of the data.
- * @param input input data
- * @param version version of the symbol
- * @return number of bits
- */
- //__STATIC
- static int QRinput_estimateBitStreamSize(QRinput *input, int version)
- {
- QRinput_List *list;
- int bits = 0;
- list = input->head;
- while(list != NULL) {
- bits += QRinput_estimateBitStreamSizeOfEntry(list, version);
- list = list->next;
- }
- return bits;
- }
- /**
- * Estimates the required version number of the symbol.
- * @param input input data
- * @return required version number
- */
- static int QRinput_estimateVersion(QRinput *input)
- {
- int bits;
- int version, prev;
- version = 0;
- do {
- prev = version;
- bits = QRinput_estimateBitStreamSize(input, prev);
- version = QRspec_getMinimumVersion((bits + 7) / 8, input->level);
- if (version < 0) {
- return -1;
- }
- } while (version > prev);
- return version;
- }
- /**
- * Returns required length in bytes for specified mode, version and bits.
- * @param mode
- * @param version
- * @param bits
- * @return required length of code words in bytes.
- */
- //__STATIC
- static int QRinput_lengthOfCode(QRencodeMode mode, int version, int bits)
- {
- int payload, size, chunks, remain, maxsize;
- payload = bits - 4 - QRspec_lengthIndicator(mode, version);
- switch(mode) {
- case QR_MODE_NUM:
- chunks = payload / 10;
- remain = payload - chunks * 10;
- size = chunks * 3;
- if(remain >= 7) {
- size += 2;
- } else if(remain >= 4) {
- size += 1;
- }
- break;
- case QR_MODE_AN:
- chunks = payload / 11;
- remain = payload - chunks * 11;
- size = chunks * 2;
- if(remain >= 6) size++;
- break;
- case QR_MODE_8:
- size = payload / 8;
- break;
- case QR_MODE_KANJI:
- size = (payload / 13) * 2;
- break;
- case QR_MODE_STRUCTURE:
- size = payload / 8;
- break;
- default:
- size = 0;
- break;
- }
- maxsize = QRspec_maximumWords(mode, version);
- if(size < 0) size = 0;
- if(size > maxsize) size = maxsize;
- return size;
- }
- /******************************************************************************
- * Data conversion
- *****************************************************************************/
- /**
- * Convert the input data in the data chunk to a bit stream.
- * @param entry
- * @return number of bits (>0) or -1 for failure.
- */
- static int QRinput_encodeBitStream(QRinput_List *entry, int version)
- {
- int words, ret;
- QRinput_List *st1 = NULL, *st2 = NULL;
- if(entry->bstream != NULL) {
- BitStream_free(entry->bstream);
- entry->bstream = NULL;
- }
- words = QRspec_maximumWords(entry->mode, version);
- if(entry->size > words) {
- st1 = QRinput_List_newEntry(entry->mode, words, entry->data);
- if(st1 == NULL) goto ABORT;
- st2 = QRinput_List_newEntry(entry->mode, entry->size - words, &entry->data[words]);
- if(st2 == NULL) goto ABORT;
- ret = QRinput_encodeBitStream(st1, version);
- if(ret < 0) goto ABORT;
- ret = QRinput_encodeBitStream(st2, version);
- if(ret < 0) goto ABORT;
- entry->bstream = BitStream_new();
- if(entry->bstream == NULL) goto ABORT;
- ret = BitStream_append(entry->bstream, st1->bstream);
- if(ret < 0) goto ABORT;
- ret = BitStream_append(entry->bstream, st2->bstream);
- if(ret < 0) goto ABORT;
- QRinput_List_freeEntry(st1);
- QRinput_List_freeEntry(st2);
- } else {
- ret = 0;
- switch(entry->mode) {
- case QR_MODE_NUM:
- ret = QRinput_encodeModeNum(entry, version);
- break;
- case QR_MODE_AN:
- ret = QRinput_encodeModeAn(entry, version);
- break;
- case QR_MODE_8:
- ret = QRinput_encodeMode8(entry, version);
- break;
- case QR_MODE_KANJI:
- ret = QRinput_encodeModeKanji(entry, version);
- break;
- case QR_MODE_STRUCTURE:
- ret = QRinput_encodeModeStructure(entry);
- break;
- default:
- break;
- }
- if(ret < 0) return -1;
- }
- return BitStream_size(entry->bstream);
- ABORT:
- QRinput_List_freeEntry(st1);
- QRinput_List_freeEntry(st2);
- return -1;
- }
- /**
- * Convert the input data to a bit stream.
- * @param input input data.
- * @retval 0 success
- * @retval -1 an error occurred and errno is set to indeicate the error.
- * See Execptions for the details.
- * @throw ENOMEM unable to allocate memory.
- */
- static int QRinput_createBitStream(QRinput *input)
- {
- QRinput_List *list;
- int bits, total = 0;
- list = input->head;
- while(list != NULL) {
- bits = QRinput_encodeBitStream(list, input->version);
- if(bits < 0) return -1;
- total += bits;
- list = list->next;
- }
- return total;
- }
- /**
- * Convert the input data to a bit stream.
- * When the version number is given and that is not sufficient, it is increased
- * automatically.
- * @param input input data.
- * @retval 0 success
- * @retval -1 an error occurred and errno is set to indeicate the error.
- * See Execptions for the details.
- * @throw ENOMEM unable to allocate memory.
- * @throw EINVAL input is too large.
- */
- static int QRinput_convertData(QRinput *input)
- {
- int bits;
- int ver;
- ver = QRinput_estimateVersion(input);
- if(ver > QRinput_getVersion(input)) {
- QRinput_setVersion(input, ver);
- }
- for(;;) {
- bits = QRinput_createBitStream(input);
- if(bits < 0) return -1;
- ver = QRspec_getMinimumVersion((bits + 7) / 8, input->level);
- if(ver < 0) {
- errno = EINVAL;
- return -1;
- } else if(ver > QRinput_getVersion(input)) {
- QRinput_setVersion(input, ver);
- } else {
- break;
- }
- }
- return 0;
- }
- /**
- * Append padding bits for the input data.
- * @param bstream Bitstream to be appended.
- * @param input input data.
- * @retval 0 success
- * @retval -1 an error occurred and errno is set to indeicate the error.
- * See Execptions for the details.
- * @throw ENOMEM unable to allocate memory.
- */
- static int QRinput_appendPaddingBit(BitStream *bstream, QRinput *input)
- {
- int bits, maxbits, words, maxwords, i, ret;
- BitStream *padding = NULL;
- unsigned char *padbuf;
- int padlen;
- bits = BitStream_size(bstream);
- maxwords = QRspec_getDataLength(input->version, input->level);
- maxbits = maxwords * 8;
- if(maxbits == bits) {
- return 0;
- }
- if(maxbits - bits < 5) {
- ret = BitStream_appendNum(bstream, maxbits - bits, 0);
- goto DONE;
- }
- bits += 4;
- words = (bits + 7) / 8;
- padding = BitStream_new();
- if(padding == NULL) return -1;
- ret = BitStream_appendNum(padding, words * 8 - bits + 4, 0);
- if(ret < 0) goto DONE;
- padlen = maxwords - words;
- if(padlen > 0) {
- padbuf = (unsigned char *)malloc(padlen);
- if(padbuf == NULL) {
- ret = -1;
- goto DONE;
- }
- for(i=0; i<padlen; i++) {
- padbuf[i] = (i&1)?0x11:0xec;
- }
- ret = BitStream_appendBytes(padding, padlen, padbuf);
- free(padbuf);
- if(ret < 0) {
- goto DONE;
- }
- }
- ret = BitStream_append(bstream, padding);
- DONE:
- BitStream_free(padding);
- return ret;
- }
- /**
- * Merge all bit streams in the input data.
- * @param input input data.
- * @return merged bit stream
- */
- //__STATIC
- static BitStream *QRinput_mergeBitStream(QRinput *input)
- {
- BitStream *bstream;
- QRinput_List *list;
- int ret;
- if(QRinput_convertData(input) < 0) {
- return NULL;
- }
- bstream = BitStream_new();
- if(bstream == NULL) return NULL;
- list = input->head;
- while(list != NULL) {
- ret = BitStream_append(bstream, list->bstream);
- if(ret < 0) {
- BitStream_free(bstream);
- return NULL;
- }
- list = list->next;
- }
- return bstream;
- }
- /**
- * Merge all bit streams in the input data and append padding bits
- * @param input input data.
- * @return padded merged bit stream
- */
- //__STATIC
- static BitStream *QRinput_getBitStream(QRinput *input)
- {
- BitStream *bstream;
- int ret;
- bstream = QRinput_mergeBitStream(input);
- if(bstream == NULL) {
- return NULL;
- }
- ret = QRinput_appendPaddingBit(bstream, input);
- if(ret < 0) {
- BitStream_free(bstream);
- return NULL;
- }
- return bstream;
- }
- /**
- * Pack all bit streams padding bits into a byte array.
- * @param input input data.
- * @return padded merged byte stream
- */
- unsigned char *QRinput_getByteStream(QRinput *input)
- {
- BitStream *bstream;
- unsigned char *array;
- bstream = QRinput_getBitStream(input);
- if(bstream == NULL) {
- return NULL;
- }
- array = BitStream_toByte(bstream);
- BitStream_free(bstream);
- return array;
- }
- /******************************************************************************
- * Structured input data
- *****************************************************************************/
- static QRinput_InputList *QRinput_InputList_newEntry(QRinput *input)
- {
- QRinput_InputList *entry;
- entry = (QRinput_InputList *)malloc(sizeof(QRinput_InputList));
- if(entry == NULL) return NULL;
- entry->input = input;
- entry->next = NULL;
- return entry;
- }
- static void QRinput_InputList_freeEntry(QRinput_InputList *entry)
- {
- if(entry != NULL) {
- QRinput_free(entry->input);
- free(entry);
- }
- }
- QRinput_Struct *QRinput_Struct_new(void)
- {
- QRinput_Struct *s;
- s = (QRinput_Struct *)malloc(sizeof(QRinput_Struct));
- if(s == NULL) return NULL;
- s->size = 0;
- s->parity = -1;
- s->head = NULL;
- s->tail = NULL;
- return s;
- }
- void QRinput_Struct_setParity(QRinput_Struct *s, unsigned char parity)
- {
- s->parity = (int)parity;
- }
- int QRinput_Struct_appendInput(QRinput_Struct *s, QRinput *input)
- {
- QRinput_InputList *e;
- e = QRinput_InputList_newEntry(input);
- if(e == NULL) return -1;
- s->size++;
- if(s->tail == NULL) {
- s->head = e;
- s->tail = e;
- } else {
- s->tail->next = e;
- s->tail = e;
- }
- return s->size;
- }
- void QRinput_Struct_free(QRinput_Struct *s)
- {
- QRinput_InputList *list, *next;
-
- if(s != NULL) {
- list = s->head;
- while(list != NULL) {
- next = list->next;
- QRinput_InputList_freeEntry(list);
- list = next;
- }
- free(s);
- }
- }
- static unsigned char QRinput_Struct_calcParity(QRinput_Struct *s)
- {
- QRinput_InputList *list;
- unsigned char parity = 0;
- list = s->head;
- while(list != NULL) {
- parity ^= QRinput_calcParity(list->input);
- list = list->next;
- }
- QRinput_Struct_setParity(s, parity);
- return parity;
- }
- static int QRinput_List_shrinkEntry(QRinput_List *entry, int bytes)
- {
- unsigned char *data;
- data = (unsigned char *)malloc(bytes);
- if(data == NULL) return -1;
- memcpy(data, entry->data, bytes);
- free(entry->data);
- entry->data = data;
- entry->size = bytes;
- return 0;
- }
- //__STATIC
- static int QRinput_splitEntry(QRinput_List *entry, int bytes)
- {
- QRinput_List *e;
- int ret;
- e = QRinput_List_newEntry(entry->mode, entry->size - bytes, entry->data + bytes);
- if(e == NULL) {
- return -1;
- }
- ret = QRinput_List_shrinkEntry(entry, bytes);
- if(ret < 0) {
- QRinput_List_freeEntry(e);
- return -1;
- }
- e->next = entry->next;
- entry->next = e;
- return 0;
- }
- QRinput_Struct *QRinput_splitQRinputToStruct(QRinput *input)
- {
- QRinput *p;
- QRinput_Struct *s;
- int bits, maxbits, nextbits, bytes, ret;
- QRinput_List *list, *next, *prev;
- s = QRinput_Struct_new();
- if(s == NULL) return NULL;
- input = QRinput_dup(input);
- if(input == NULL) {
- QRinput_Struct_free(s);
- return NULL;
- }
- QRinput_Struct_setParity(s, QRinput_calcParity(input));
- maxbits = QRspec_getDataLength(input->version, input->level) * 8 - STRUCTURE_HEADER_BITS;
- if(maxbits <= 0) {
- QRinput_Struct_free(s);
- QRinput_free(input);
- return NULL;
- }
- bits = 0;
- list = input->head;
- prev = NULL;
- while(list != NULL) {
- nextbits = QRinput_estimateBitStreamSizeOfEntry(list, input->version);
- if(bits + nextbits <= maxbits) {
- ret = QRinput_encodeBitStream(list, input->version);
- if(ret < 0) goto ABORT;
- bits += ret;
- prev = list;
- list = list->next;
- } else {
- bytes = QRinput_lengthOfCode(list->mode, input->version, maxbits - bits);
- if(bytes > 0) {
- /* Splits this entry into 2 entries. */
- ret = QRinput_splitEntry(list, bytes);
- if(ret < 0) goto ABORT;
- /* First half is the tail of the current input. */
- next = list->next;
- list->next = NULL;
- /* Second half is the head of the next input, p.*/
- p = QRinput_new2(input->version, input->level);
- if(p == NULL) goto ABORT;
- p->head = next;
- /* Renew QRinput.tail. */
- p->tail = input->tail;
- input->tail = list;
- /* Point to the next entry. */
- prev = list;
- list = next;
- } else {
- /* Current entry will go to the next input. */
- prev->next = NULL;
- p = QRinput_new2(input->version, input->level);
- if(p == NULL) goto ABORT;
- p->head = list;
- p->tail = input->tail;
- input->tail = prev;
- }
- ret = QRinput_Struct_appendInput(s, input);
- if(ret < 0) goto ABORT;
- input = p;
- bits = 0;
- }
- }
- QRinput_Struct_appendInput(s, input);
- if(s->size > MAX_STRUCTURED_SYMBOLS) {
- QRinput_Struct_free(s);
- errno = ERANGE;
- return NULL;
- }
- ret = QRinput_Struct_insertStructuredAppendHeaders(s);
- if(ret < 0) {
- QRinput_Struct_free(s);
- return NULL;
- }
- return s;
- ABORT:
- QRinput_free(input);
- QRinput_Struct_free(s);
- return NULL;
- }
- int QRinput_Struct_insertStructuredAppendHeaders(QRinput_Struct *s)
- {
- int num, i;
- QRinput_InputList *list;
- if(s->parity < 0) {
- QRinput_Struct_calcParity(s);
- }
- num = 0;
- list = s->head;
- while(list != NULL) {
- num++;
- list = list->next;
- }
- i = 1;
- list = s->head;
- while(list != NULL) {
- if(QRinput_insertStructuredAppendHeader(list->input, num, i, s->parity))
- return -1;
- i++;
- list = list->next;
- }
- return 0;
- }
|