]> git.sur5r.net Git - glabels/blob - qrencode-3.1.0/qrencode.c
Organized master branch to be top-level directory for glabels, instead of
[glabels] / qrencode-3.1.0 / qrencode.c
1 /*
2  * qrencode - QR Code encoder
3  *
4  * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25
26 #include "config.h"
27 #include "qrencode.h"
28 #include "qrspec.h"
29 #include "bitstream.h"
30 #include "qrinput.h"
31 #include "rscode.h"
32 #include "split.h"
33 #include "mask.h"
34
35 /******************************************************************************
36  * Raw code
37  *****************************************************************************/
38
39 typedef struct {
40         int dataLength;
41         unsigned char *data;
42         int eccLength;
43         unsigned char *ecc;
44 } RSblock;
45
46 typedef struct {
47         int version;
48         unsigned char *datacode;
49         unsigned char *ecccode;
50         int blocks;
51         RSblock *rsblock;
52         int count;
53         int dataLength;
54         int eccLength;
55         int b1;
56 } QRRawCode;
57
58 static void RSblock_initBlock(RSblock *block, int dl, unsigned char *data, int el, unsigned char *ecc, RS *rs)
59 {
60         block->dataLength = dl;
61         block->data = data;
62         block->eccLength = el;
63         block->ecc = ecc;
64
65         encode_rs_char(rs, data, ecc);
66 }
67
68 static int RSblock_init(RSblock *blocks, int spec[5], unsigned char *data, unsigned char *ecc)
69 {
70         int i;
71         RSblock *block;
72         unsigned char *dp, *ep;
73         RS *rs;
74         int el, dl;
75
76         dl = QRspec_rsDataCodes1(spec);
77         el = QRspec_rsEccCodes1(spec);
78         rs = init_rs(8, 0x11d, 0, 1, el, 255 - dl - el);
79         if(rs == NULL) return -1;
80
81         block = blocks;
82         dp = data;
83         ep = ecc;
84         for(i=0; i<QRspec_rsBlockNum1(spec); i++) {
85                 RSblock_initBlock(block, dl, dp, el, ep, rs);
86                 dp += dl;
87                 ep += el;
88                 block++;
89         }
90
91         if(QRspec_rsBlockNum2(spec) == 0) return 0;
92
93         dl = QRspec_rsDataCodes2(spec);
94         el = QRspec_rsEccCodes2(spec);
95         rs = init_rs(8, 0x11d, 0, 1, el, 255 - dl - el);
96         if(rs == NULL) return -1;
97         for(i=0; i<QRspec_rsBlockNum2(spec); i++) {
98                 RSblock_initBlock(block, dl, dp, el, ep, rs);
99                 dp += dl;
100                 ep += el;
101                 block++;
102         }
103
104         return 0;
105 }
106
107 __STATIC void QRraw_free(QRRawCode *raw);
108 __STATIC QRRawCode *QRraw_new(QRinput *input)
109 {
110         QRRawCode *raw;
111         int spec[5], ret;
112
113         raw = (QRRawCode *)malloc(sizeof(QRRawCode));
114         if(raw == NULL) return NULL;
115
116         raw->datacode = QRinput_getByteStream(input);
117         if(raw->datacode == NULL) {
118                 free(raw);
119                 return NULL;
120         }
121
122         QRspec_getEccSpec(input->version, input->level, spec);
123
124         raw->version = input->version;
125         raw->b1 = QRspec_rsBlockNum1(spec);
126         raw->dataLength = QRspec_rsDataLength(spec);
127         raw->eccLength = QRspec_rsEccLength(spec);
128         raw->ecccode = (unsigned char *)malloc(raw->eccLength);
129         if(raw->ecccode == NULL) {
130                 free(raw->datacode);
131                 free(raw);
132                 return NULL;
133         }
134
135         raw->blocks = QRspec_rsBlockNum(spec);
136         raw->rsblock = (RSblock *)calloc(sizeof(RSblock), raw->blocks);
137         if(raw->rsblock == NULL) {
138                 QRraw_free(raw);
139                 return NULL;
140         }
141         ret = RSblock_init(raw->rsblock, spec, raw->datacode, raw->ecccode);
142         if(ret < 0) {
143                 QRraw_free(raw);
144                 return NULL;
145         }
146
147         raw->count = 0;
148
149         return raw;
150 }
151
152 /**
153  * Return a code (byte).
154  * This function can be called iteratively.
155  * @param raw raw code.
156  * @return code
157  */
158 __STATIC unsigned char QRraw_getCode(QRRawCode *raw)
159 {
160         int col, row;
161         unsigned char ret;
162
163         if(raw->count < raw->dataLength) {
164                 row = raw->count % raw->blocks;
165                 col = raw->count / raw->blocks;
166                 if(col >= raw->rsblock[row].dataLength) {
167                         row += raw->b1;
168                 }
169                 ret = raw->rsblock[row].data[col];
170         } else if(raw->count < raw->dataLength + raw->eccLength) {
171                 row = (raw->count - raw->dataLength) % raw->blocks;
172                 col = (raw->count - raw->dataLength) / raw->blocks;
173                 ret = raw->rsblock[row].ecc[col];
174         } else {
175                 return 0;
176         }
177         raw->count++;
178         return ret;
179 }
180
181 __STATIC void QRraw_free(QRRawCode *raw)
182 {
183         if(raw != NULL) {
184                 free(raw->datacode);
185                 free(raw->ecccode);
186                 if(raw->rsblock != NULL) {
187                         free(raw->rsblock);
188                 }
189                 free(raw);
190         }
191 }
192
193 /******************************************************************************
194  * Frame filling
195  *****************************************************************************/
196
197 typedef struct {
198         int width;
199         unsigned char *frame;
200         int x, y;
201         int dir;
202         int bit;
203 } FrameFiller;
204
205 static FrameFiller *FrameFiller_new(int width, unsigned char *frame)
206 {
207         FrameFiller *filler;
208
209         filler = (FrameFiller *)malloc(sizeof(FrameFiller));
210         if(filler == NULL) return NULL;
211         filler->width = width;
212         filler->frame = frame;
213         filler->x = width - 1;
214         filler->y = width - 1;
215         filler->dir = -1;
216         filler->bit = -1;
217
218         return filler;
219 }
220
221 static unsigned char *FrameFiller_next(FrameFiller *filler)
222 {
223         unsigned char *p;
224         int x, y, w;
225
226         if(filler->bit == -1) {
227                 filler->bit = 0;
228                 return filler->frame + filler->y * filler->width + filler->x;
229         }
230
231         x = filler->x;
232         y = filler->y;
233         p = filler->frame;
234         w = filler->width;
235
236         if(filler->bit == 0) {
237                 x--;
238                 filler->bit++;
239         } else {
240                 x++;
241                 y += filler->dir;
242                 filler->bit--;
243         }
244
245         if(filler->dir < 0) {
246                 if(y < 0) {
247                         y = 0;
248                         x -= 2;
249                         filler->dir = 1;
250                         if(x == 6) {
251                                 x--;
252                                 y = 9;
253                         }
254                 }
255         } else {
256                 if(y == w) {
257                         y = w - 1;
258                         x -= 2;
259                         filler->dir = -1;
260                         if(x == 6) {
261                                 x--;
262                                 y -= 8;
263                         }
264                 }
265         }
266         if(x < 0 || y < 0) return NULL;
267
268         filler->x = x;
269         filler->y = y;
270
271         if(p[y * w + x] & 0x80) {
272                 // This tail recursion could be optimized.
273                 return FrameFiller_next(filler);
274         }
275         return &p[y * w + x];
276 }
277
278 #if 0
279 unsigned char *FrameFiller_fillerTest(int version)
280 {
281         int width, length;
282         unsigned char *frame, *p;
283         FrameFiller *filler;
284         int i, j;
285         unsigned char cl = 1;
286         unsigned char ch = 0;
287
288         width = QRspec_getWidth(version);
289         frame = QRspec_newFrame(version);
290         filler = FrameFiller_new(width, frame);
291         length = QRspec_getDataLength(version, QR_ECLEVEL_L)
292                         + QRspec_getECCLength(version, QR_ECLEVEL_L);
293
294         for(i=0; i<length; i++) {
295                 for(j=0; j<8; j++) {
296                         p = FrameFiller_next(filler);
297                         *p = ch | cl;
298                         cl++;
299                         if(cl == 9) {
300                                 cl = 1;
301                                 ch += 0x10;
302                         }
303                 }
304         }
305         length = QRspec_getRemainder(version);
306         for(i=0; i<length; i++) {
307                 p = FrameFiller_next(filler);
308                 *p = 0xa0;
309         }
310         p = FrameFiller_next(filler);
311         free(filler);
312         if(p != NULL) {
313                 return NULL;
314         }
315
316         return frame;
317 }
318 #endif
319
320
321 /******************************************************************************
322  * QR-code encoding
323  *****************************************************************************/
324
325 static QRcode *QRcode_new(int version, int width, unsigned char *data)
326 {
327         QRcode *qrcode;
328
329         qrcode = (QRcode *)malloc(sizeof(QRcode));
330         if(qrcode == NULL) return NULL;
331
332         qrcode->version = version;
333         qrcode->width = width;
334         qrcode->data = data;
335
336         return qrcode;
337 }
338
339 void QRcode_free(QRcode *qrcode)
340 {
341         if(qrcode != NULL) {
342                 free(qrcode->data);
343                 free(qrcode);
344         }
345 }
346
347 __STATIC QRcode *QRcode_encodeMask(QRinput *input, int mask)
348 {
349         int width, version;
350         QRRawCode *raw;
351         unsigned char *frame, *masked, *p, code, bit;
352         FrameFiller *filler;
353         int i, j;
354         QRcode *qrcode;
355
356         if(input->version < 0 || input->version > QRSPEC_VERSION_MAX) {
357                 errno = EINVAL;
358                 return NULL;
359         }
360         if(input->level > QR_ECLEVEL_H) {
361                 errno = EINVAL;
362                 return NULL;
363         }
364
365         raw = QRraw_new(input);
366         if(raw == NULL) return NULL;
367
368         version = raw->version;
369         width = QRspec_getWidth(version);
370         frame = QRspec_newFrame(version);
371         if(frame == NULL) {
372                 QRraw_free(raw);
373                 return NULL;
374         }
375         filler = FrameFiller_new(width, frame);
376         if(filler == NULL) {
377                 QRraw_free(raw);
378                 free(frame);
379                 return NULL;
380         }
381
382         /* inteleaved data and ecc codes */
383         for(i=0; i<raw->dataLength + raw->eccLength; i++) {
384                 code = QRraw_getCode(raw);
385                 bit = 0x80;
386                 for(j=0; j<8; j++) {
387                         p = FrameFiller_next(filler);
388                         *p = 0x02 | ((bit & code) != 0);
389                         bit = bit >> 1;
390                 }
391         }
392         QRraw_free(raw);
393         /* remainder bits */
394         j = QRspec_getRemainder(version);
395         for(i=0; i<j; i++) {
396                 p = FrameFiller_next(filler);
397                 *p = 0x02;
398         }
399         free(filler);
400         /* masking */
401         if(mask < 0) {
402                 masked = Mask_mask(width, frame, input->level);
403         } else {
404                 masked = Mask_makeMask(width, frame, mask, input->level);
405         }
406         if(masked == NULL) {
407                 free(frame);
408                 return NULL;
409         }
410         qrcode = QRcode_new(version, width, masked);
411
412         free(frame);
413
414         return qrcode;
415 }
416
417 QRcode *QRcode_encodeInput(QRinput *input)
418 {
419         return QRcode_encodeMask(input, -1);
420 }
421
422 QRcode *QRcode_encodeString8bit(const char *string, int version, QRecLevel level)
423 {
424         QRinput *input;
425         QRcode *code;
426         int ret;
427
428         if(string == NULL) {
429                 errno = EINVAL;
430                 return NULL;
431         }
432
433         input = QRinput_new2(version, level);
434         if(input == NULL) return NULL;
435
436         ret = QRinput_append(input, QR_MODE_8, strlen(string), (unsigned char *)string);
437         if(ret < 0) {
438                 QRinput_free(input);
439                 return NULL;
440         }
441         code = QRcode_encodeInput(input);
442         QRinput_free(input);
443
444         return code;
445 }
446
447 QRcode *QRcode_encodeString(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
448 {
449         QRinput *input;
450         QRcode *code;
451         int ret;
452
453         if(hint != QR_MODE_8 && hint != QR_MODE_KANJI) {
454                 errno = EINVAL;
455                 return NULL;
456         }
457
458         input = QRinput_new2(version, level);
459         if(input == NULL) return NULL;
460
461         ret = Split_splitStringToQRinput(string, input, hint, casesensitive);
462         if(ret < 0) {
463                 QRinput_free(input);
464                 return NULL;
465         }
466
467         code = QRcode_encodeInput(input);
468         QRinput_free(input);
469
470         return code;
471 }
472
473 /******************************************************************************
474  * Structured QR-code encoding
475  *****************************************************************************/
476
477 static QRcode_List *QRcode_List_newEntry(void)
478 {
479         QRcode_List *entry;
480
481         entry = (QRcode_List *)malloc(sizeof(QRcode_List));
482         if(entry == NULL) return NULL;
483
484         entry->next = NULL;
485         entry->code = NULL;
486
487         return entry;
488 }
489
490 static void QRcode_List_freeEntry(QRcode_List *entry)
491 {
492         if(entry != NULL) {
493                 QRcode_free(entry->code);
494                 free(entry);
495         }
496 }
497
498 void QRcode_List_free(QRcode_List *qrlist)
499 {
500         QRcode_List *list = qrlist, *next;
501
502         while(list != NULL) {
503                 next = list->next;
504                 QRcode_List_freeEntry(list);
505                 list = next;
506         }
507 }
508
509 int QRcode_List_size(QRcode_List *qrlist)
510 {
511         QRcode_List *list = qrlist;
512         int size = 0;
513
514         while(list != NULL) {
515                 size++;
516                 list = list->next;
517         }
518
519         return size;
520 }
521
522 #if 0
523 static unsigned char QRcode_parity(const char *str, int size)
524 {
525         unsigned char parity = 0;
526         int i;
527
528         for(i=0; i<size; i++) {
529                 parity ^= str[i];
530         }
531
532         return parity;
533 }
534 #endif
535
536 QRcode_List *QRcode_encodeInputStructured(QRinput_Struct *s)
537 {
538         QRcode_List *head = NULL;
539         QRcode_List *tail = NULL;
540         QRcode_List *entry;
541         QRinput_InputList *list = s->head;
542
543         while(list != NULL) {
544                 if(head == NULL) {
545                         entry = QRcode_List_newEntry();
546                         if(entry == NULL) goto ABORT;
547                         head = entry;
548                         tail = head;
549                 } else {
550                         entry = QRcode_List_newEntry();
551                         if(entry == NULL) goto ABORT;
552                         tail->next = entry;
553                         tail = tail->next;
554                 }
555                 tail->code = QRcode_encodeInput(list->input);
556                 if(tail->code == NULL) {
557                         goto ABORT;
558                 }
559                 list = list->next;
560         }
561
562         return head;
563 ABORT:
564         QRcode_List_free(head);
565         return NULL;
566 }
567
568 static QRcode_List *QRcode_encodeInputToStructured(QRinput *input)
569 {
570         QRinput_Struct *s;
571         QRcode_List *codes;
572
573         s = QRinput_splitQRinputToStruct(input);
574         if(s == NULL) return NULL;
575
576         codes = QRcode_encodeInputStructured(s);
577         QRinput_Struct_free(s);
578
579         return codes;
580 }
581
582 QRcode_List *QRcode_encodeString8bitStructured(const char *string, int version, QRecLevel level)
583 {
584         QRinput *input;
585         QRcode_List *codes;
586         int ret;
587
588         if(version <= 0) {
589                 errno = EINVAL;
590                 return NULL;
591         }
592
593         input = QRinput_new2(version, level);
594         if(input == NULL) return NULL;
595
596         ret = QRinput_append(input, QR_MODE_8, strlen(string), (unsigned char *)string);
597         if(ret < 0) {
598                 QRinput_free(input);
599                 return NULL;
600         }
601         codes = QRcode_encodeInputToStructured(input);
602         QRinput_free(input);
603
604         return codes;
605 }
606
607 QRcode_List *QRcode_encodeStringStructured(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
608 {
609         QRinput *input;
610         QRcode_List *codes;
611         int ret;
612
613         if(version <= 0) {
614                 errno = EINVAL;
615                 return NULL;
616         }
617         if(hint != QR_MODE_8 && hint != QR_MODE_KANJI) {
618                 errno = EINVAL;
619                 return NULL;
620         }
621
622         input = QRinput_new2(version, level);
623         if(input == NULL) return NULL;
624
625         ret = Split_splitStringToQRinput(string, input, hint, casesensitive);
626         if(ret < 0) {
627                 QRinput_free(input);
628                 return NULL;
629         }
630         codes = QRcode_encodeInputToStructured(input);
631         QRinput_free(input);
632
633         return codes;
634 }