src/qrinput.c

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