-
Notifications
You must be signed in to change notification settings - Fork 7
/
qr.c
748 lines (635 loc) · 24.1 KB
/
qr.c
1
2
3
4
5
6
7
8
9
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
// SPDX-License-Identifier: CC0-1.0
/*
* QR Code is a registered trademark of DENSO WAVE INCORPORATED in Japan
* and other countries.
*
*/
#include <getopt.h>
#include <locale.h>
#include <math.h>
#include <qrencode.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* STDIN read buffer chunk size */
#define STDIN_CHUNKSIZE 64
/* Single-module blocks (large size) */
#define BLOCK_0 " "
#define BLOCK_1 "██"
/* Single-module blocks (large size, compact mode) */
#define BLOCK_0_C " "
#define BLOCK_1_C "█"
/* Double-module blocks (default size, normal mode) */
/*
Module bit order: [ bottom | top ]
*/
#define DBL_BLOCK_00 " "
#define DBL_BLOCK_01 "▀"
#define DBL_BLOCK_10 "▄"
#define DBL_BLOCK_11 "█"
/* Quad-module blocks (default size, compact mode) */
/*
Module bit order: [ bottom_right | top_right | bottom_left | top_left ]
*/
#define QUAD_BLOCK_0000 " "
#define QUAD_BLOCK_0001 "▘"
#define QUAD_BLOCK_0010 "▖"
#define QUAD_BLOCK_0011 "▌"
#define QUAD_BLOCK_0100 "▝"
#define QUAD_BLOCK_1000 "▗"
#define QUAD_BLOCK_1001 "▚"
#define QUAD_BLOCK_1010 "▄"
#define QUAD_BLOCK_0101 "▀"
#define QUAD_BLOCK_O110 "▞"
#define QUAD_BLOCK_0111 "▛"
#define QUAD_BLOCK_1011 "▙"
#define QUAD_BLOCK_1100 "▐"
#define QUAD_BLOCK_1101 "▜"
#define QUAD_BLOCK_1110 "▟"
#define QUAD_BLOCK_1111 "█"
/* ANSI terminal colors */
#define FG_WH "\x1b[37m"
#define FG_DF "\x1b[39m"
#define BG_BK "\x1b[40m"
#define BG_DF "\x1b[49m"
#define BGBK_FGWH BG_BK FG_WH
#define BGDF_FGDF BG_DF FG_DF
/* Newline character(s) */
#define EOL "\n"
typedef unsigned char bool;
#define true 1
#define false 0
/* Use binary code to represent modules when referring to blocks */
#define B_0 0
#define B_1 1
#define B_00 B_0
#define B_01 B_1
#define B_10 2
#define B_11 3
#define B_0000 B_0
#define B_0001 B_1
#define B_0010 B_10
#define B_0011 B_11
#define B_0100 4
#define B_0101 5
#define B_0110 6
#define B_0111 7
#define B_1000 8
#define B_1001 9
#define B_1010 10
#define B_1011 11
#define B_1100 12
#define B_1101 13
#define B_1110 14
#define B_1111 15
typedef struct {
char encode_mode;
int version;
char ec_level;
bool large;
bool compact;
short border;
bool invert;
bool plain;
} Options;
/* Unicode BOM */
const char *utf8_bom = "\xEF\xBB\xBF";
/* Help message */
const char *help_msg =
"Usage: qr [OPTIONS] STRING" EOL
" or: cat FILE | qr [OPTIONS]" EOL
EOL
"Options:" EOL
" -m QR mode [na8k] (n = number, a = alphabet, 8 = 8-bit, "
"k = Kanji)" EOL
" -v QR version [1-40]" EOL
" -e QR EC level [lmqh] or [1-4]" EOL
" -l use two characters per block" EOL
" -c compact mode" EOL
" -b border width [1-4] (the default is 1)" EOL
" -i invert colors" EOL
" -p force colorless output" EOL
" -h print help info and exit" EOL
" -V print version info and exit" EOL
;
void print_help_msg(void)
{
printf("%s" EOL, help_msg);
}
static inline bool str_has_utf8_bom(const char *string)
{
return (strcmp(string, utf8_bom) == 0);
}
char *qr_data_to_text(const QRcode *code, const char border_width,
const bool invert_colors, const bool paint,
const bool large_size, const bool compact_mode)
{
int ih = 0; // Horizontal index counter
int iv = 0; // Vertical index counter
const unsigned char *data = code->data;
if (data == NULL) {
return NULL;
}
const int resolution = code->width;
const int l = resolution + border_width * 2;
char *text;
if (large_size) {
/*******************************************************************/
/* One module per block (large size and large size + compact mode) */
/*******************************************************************/
const char *blocks[2] = {
// 0
(invert_colors) ?
(compact_mode) ? BLOCK_0_C : BLOCK_0 :
(compact_mode) ? BLOCK_1_C : BLOCK_1,
// 1
(invert_colors) ?
(compact_mode) ? BLOCK_1_C : BLOCK_1 :
(compact_mode) ? BLOCK_0_C : BLOCK_0,
};
const char modules_per_block_v = 1;
const char modules_per_block_h = 1;
const int byte_len =
(strlen((compact_mode) ? BLOCK_1_C : BLOCK_1) * l + strlen(EOL)) * l +
((paint) ? strlen(BGBK_FGWH) + strlen(BGDF_FGDF) : 0) * l;
text = malloc(byte_len + 1);
/* Move cursor to the beginning of *text */
text[0] = '\0';
/* Top border */
for (iv = 0; iv < border_width; iv += modules_per_block_v) {
/* Set palette */
if (paint) {
strcat(text, BGBK_FGWH);
}
/* Append top border blocks */
for (ih = 0; ih < l; ih += modules_per_block_h) {
strcat(text, blocks[B_0]);
}
/* Reset palette */
if (paint) {
strcat(text, BGDF_FGDF);
}
/* Put newline */
strcat(text, EOL);
}
/* Left border, data, right border */
for (iv = 0; iv < resolution; iv += modules_per_block_v) {
/* Set palette */
if (paint) {
strcat(text, BGBK_FGWH);
}
/* Append left border blocks */
for (ih = 0; ih < border_width; ih += modules_per_block_h) {
strcat(text, blocks[B_0]);
}
/* Append data blocks */
for (ih = 0; ih < resolution; ih++) {
strcat(text, blocks[data[iv * resolution + ih] & B_1]);
}
/* Append right border blocks */
for (ih = 0; ih < border_width; ih += modules_per_block_h) {
strcat(text, blocks[B_0]);
}
/* Reset palette */
if (paint) {
strcat(text, BGDF_FGDF);
}
/* Put newline */
strcat(text, EOL);
}
/* Bottom border */
for (iv = 0; iv < border_width; iv += modules_per_block_v) {
/* Set palette */
if (paint) {
strcat(text, BGBK_FGWH);
}
/* Append bottom border blocks */
for (ih = 0; ih < l; ih += modules_per_block_h) {
strcat(text, blocks[B_0]);
}
/* Reset palette */
if (paint) {
strcat(text, BGDF_FGDF);
}
/* Put newline */
strcat(text, EOL);
}
} else {
/****************************************************************/
/* Two or four modules per block (normal mode and compact mode) */
/****************************************************************/
if (compact_mode) {
/*****************************************/
/* Four modules per block (compact mode) */
/*****************************************/
const char *blocks[16] = {
(invert_colors) ? QUAD_BLOCK_0000 : QUAD_BLOCK_1111,
(invert_colors) ? QUAD_BLOCK_0001 : QUAD_BLOCK_1110,
(invert_colors) ? QUAD_BLOCK_0010 : QUAD_BLOCK_1101,
(invert_colors) ? QUAD_BLOCK_0011 : QUAD_BLOCK_1100,
(invert_colors) ? QUAD_BLOCK_0100 : QUAD_BLOCK_1011,
(invert_colors) ? QUAD_BLOCK_0101 : QUAD_BLOCK_1010,
(invert_colors) ? QUAD_BLOCK_O110 : QUAD_BLOCK_1001,
(invert_colors) ? QUAD_BLOCK_0111 : QUAD_BLOCK_1000,
(invert_colors) ? QUAD_BLOCK_1000 : QUAD_BLOCK_0111,
(invert_colors) ? QUAD_BLOCK_1001 : QUAD_BLOCK_O110,
(invert_colors) ? QUAD_BLOCK_1010 : QUAD_BLOCK_0101,
(invert_colors) ? QUAD_BLOCK_1011 : QUAD_BLOCK_0100,
(invert_colors) ? QUAD_BLOCK_1100 : QUAD_BLOCK_0011,
(invert_colors) ? QUAD_BLOCK_1101 : QUAD_BLOCK_0010,
(invert_colors) ? QUAD_BLOCK_1110 : QUAD_BLOCK_0001,
(invert_colors) ? QUAD_BLOCK_1111 : QUAD_BLOCK_0000,
};
const char modules_per_block_v = 2;
const char modules_per_block_h = 2;
const char border_leftover_v = (border_width % modules_per_block_v);
const char border_leftover_h = (border_width % modules_per_block_h);
const int byte_len =
(strlen(QUAD_BLOCK_1111) * l + strlen(EOL)) * l +
((paint) ? strlen(BGBK_FGWH) + strlen(BGDF_FGDF) : 0) * l;
text = malloc(byte_len + 1);
/* Move cursor to the beginning of *text */
text[0] = '\0';
/* Top border */
for (iv = 0; iv < border_width - border_leftover_v; iv += modules_per_block_v) {
/* Set palette */
if (paint) {
strcat(text, BGBK_FGWH);
}
/* Append top border quad-blocks */
for (ih = 0; ih < l - border_leftover_h; ih += modules_per_block_h) {
strcat(text, blocks[B_0000]);
}
/* Trailing quad-module blocks for right border */
if (border_leftover_h % modules_per_block_h != 0) {
/* Avoid coloring rightmost (transparent) quad-module line */
if (paint && !invert_colors) {
strcat(text, BG_DF);
}
/* Append quad-module block */
strcat(text, blocks[(invert_colors) ? B_0000 : B_1100]);
}
/* Reset palette */
if (paint) {
strcat(text, BGDF_FGDF);
}
/* Put newline */
strcat(text, EOL);
}
/* Left border, data, right border */
for (iv = -border_leftover_v; iv < resolution; iv += modules_per_block_v) {
/* Set palette */
if (paint) {
strcat(text, BGBK_FGWH);
}
/* Append left border quad-module blocks */
for (ih = 0; ih < border_width - border_leftover_h; ih += modules_per_block_h) {
strcat(text, blocks[B_0000]);
}
/* Append data quad-module blocks */
bool is_top_half_border_iter = (iv == -1) ? true : false;
bool is_last_data_row_iter = (iv == resolution - 1) ? true : false;
for (ih = -border_leftover_h; ih < resolution; ih += modules_per_block_h) {
bool is_left_half_border_iter = (ih == -1) ? true : false;
bool is_last_data_col_iter = (ih == resolution - 1) ? true : false;
unsigned char block_mask = B_0000;
if (is_top_half_border_iter) {
if (is_left_half_border_iter) {
// Able to determine bottom-right module
if (data[iv * resolution + ih + resolution + 1] & 1) {
block_mask += B_1000;
}
} else {
// Can determine both of bottom modules
if (data[iv * resolution + ih + resolution] & 1) {
block_mask += B_0010;
}
if (!is_last_data_col_iter && data[iv * resolution + ih + resolution + 1] & 1) {
block_mask += B_1000;
}
}
} else {
if (is_left_half_border_iter) {
// Can determine both of right modules
if (data[iv * resolution + ih + 1] & 1) {
block_mask += B_0100;
}
if (!is_last_data_row_iter && data[iv * resolution + ih + resolution + 1] & 1) {
block_mask += B_1000;
}
} else {
// Can determine all modules
if (data[iv * resolution + ih] & 1) {
block_mask += B_0001;
}
if (!is_last_data_col_iter && data[iv * resolution + ih + 1] & 1) {
block_mask += B_0100;
}
if (!is_last_data_row_iter) {
if (data[iv * resolution + ih + resolution] & 1) {
block_mask += B_0010;
}
if (!is_last_data_col_iter && data[iv * resolution + ih + resolution + 1] & 1) {
block_mask += B_1000;
}
}
}
}
strcat(text, blocks[block_mask]);
}
/* Append right border quad-module blocks */
for (ih = 0; ih < border_width - border_leftover_h; ih += modules_per_block_h) {
strcat(text, blocks[B_0000]);
}
/* Trailing quad-module blocks for right border */
if (border_leftover_h % modules_per_block_h != 0) {
/* Avoid coloring rightmost (transparent) quad-module line */
if (paint && !invert_colors) {
strcat(text, BG_DF);
}
/* Append quad-module block */
strcat(text, blocks[(invert_colors) ? B_0000 : B_1100]);
}
/* Reset palette */
if (paint) {
strcat(text, BGDF_FGDF);
}
/* Put newline */
strcat(text, EOL);
}
/* Bottom border */
for (iv = modules_per_block_v; iv < border_width; iv += modules_per_block_v) {
/* Set palette */
if (paint) {
strcat(text, BGBK_FGWH);
}
/* Append quad-module blocks */
for (ih = 0; ih < l - border_leftover_h; ih += modules_per_block_h) {
strcat(text, blocks[B_0000]);
}
/* Trailing quad-module blocks for right border */
if (border_leftover_h % modules_per_block_h != 0) {
/* Avoid coloring rightmost (transparent) quad-module line */
if (paint && !invert_colors) {
strcat(text, BG_DF);
}
/* Append quad-module block */
strcat(text, blocks[(invert_colors) ? B_0000 : B_1100]);
}
/* Reset palette */
if (paint) {
strcat(text, BGDF_FGDF);
}
/* Put newline */
strcat(text, EOL);
}
/* Trailing quad-module blocks for bottom border */
if (border_leftover_v == 0 || border_leftover_v % modules_per_block_v != 0) {
/* Set palette */
if (paint) {
strcat(text, BGBK_FGWH);
/* Avoid coloring last (transparent) quad-module line */
if (!invert_colors) {
strcat(text, BG_DF);
}
}
/* Append quad-module blocks */
for (ih = 0; ih < l - border_leftover_h; ih += modules_per_block_h) {
strcat(text, blocks[(invert_colors) ? B_0000 : B_1010]);
}
/* Trailing quad-module blocks for right border */
if (border_leftover_h % modules_per_block_h != 0) {
/* Avoid coloring rightmost (transparent) quad-module line */
if (paint && !invert_colors) {
strcat(text, BG_DF);
}
/* Append quad-module block */
strcat(text, blocks[(invert_colors) ? B_0000 : B_1110]);
}
/* Reset palette */
if (paint) {
strcat(text, BGDF_FGDF);
}
/* Put newline */
strcat(text, EOL);
}
} else {
/***************************************/
/* Two modules per block (normal mode) */
/***************************************/
const char *blocks[4] = {
(invert_colors) ? DBL_BLOCK_00 : DBL_BLOCK_11,
(invert_colors) ? DBL_BLOCK_10 : DBL_BLOCK_01,
(invert_colors) ? DBL_BLOCK_01 : DBL_BLOCK_10,
(invert_colors) ? DBL_BLOCK_11 : DBL_BLOCK_00,
};
const char modules_per_block_v = 2;
const char modules_per_block_h = 1;
const char border_leftover_v = (border_width % modules_per_block_v);
const int byte_len =
(strlen(DBL_BLOCK_11) * l + strlen(EOL)) * l +
((paint) ? strlen(BGBK_FGWH) + strlen(BGDF_FGDF) : 0) * l;
text = malloc(byte_len + 1);
/* Move cursor to the beginning of *text */
text[0] = '\0';
/* Top border */
for (iv = 0; iv < border_width - border_leftover_v; iv += modules_per_block_v) {
/* Set palette */
if (paint) {
strcat(text, BGBK_FGWH);
}
/* Append top border double-module blocks */
for (ih = 0; ih < l; ih++) {
strcat(text, blocks[B_00]);
}
/* Reset palette */
if (paint) {
strcat(text, BGDF_FGDF);
}
/* Put newline */
strcat(text, EOL);
}
/* Left border, data, right border */
for (iv = -border_leftover_v; iv < resolution; iv += modules_per_block_v) {
/* Set palette */
if (paint) {
strcat(text, BGBK_FGWH);
}
/* Append left border double-module blocks */
for (ih = 0; ih < border_width; ih += modules_per_block_h) {
strcat(text, blocks[B_00]);
}
/* Append data double-module blocks */
bool is_top_half_border_iter = (iv == -1) ? true : false;
bool is_last_data_row_iter = (iv == resolution - 1) ? true : false;
for (ih = 0; ih < resolution; ih += modules_per_block_h) {
unsigned char block_mask = B_00;
if (is_top_half_border_iter) {
if (data[iv * resolution + ih + resolution] & 1) {
block_mask += B_01;
}
} else {
if (data[iv * resolution + ih] & 1) {
block_mask += B_10;
}
if (!is_last_data_row_iter && data[iv * resolution + ih + resolution] & 1) {
block_mask += B_01;
}
}
strcat(text, blocks[block_mask]);
}
/* Append right border double-module blocks */
for (ih = 0; ih < border_width; ih += modules_per_block_h) {
strcat(text, blocks[B_00]);
}
/* Reset palette */
if (paint) {
strcat(text, BGDF_FGDF);
}
/* Put newline */
strcat(text, EOL);
}
/* Bottom border */
for (iv = modules_per_block_v; iv < border_width; iv += modules_per_block_v) {
/* Set palette */
if (paint) {
strcat(text, BGBK_FGWH);
}
/* Append double-module blocks */
for (ih = 0; ih < l; ih += modules_per_block_h) {
strcat(text, blocks[B_00]);
}
/* Reset palette */
if (paint) {
strcat(text, BGDF_FGDF);
}
/* Put newline */
strcat(text, EOL);
}
/* Trailing double-module blocks for bottom border */
{
/* Set palette */
if (paint) {
strcat(text, BGBK_FGWH);
/* Avoid coloring last (transparent) double-line */
if (!invert_colors) {
strcat(text, BG_DF);
}
}
/* Append double-module blocks */
for (ih = 0; ih < l; ih += modules_per_block_h) {
strcat(text, blocks[(invert_colors) ? B_00 : B_01]);
}
/* Reset palette */
if (paint) {
strcat(text, BGDF_FGDF);
}
/* Put newline */
strcat(text, EOL);
}
}
}
return text;
}
QRencodeMode get_qr_encode_mode(const char encode_mode)
{
switch (encode_mode) {
case 'n':
case 'N':
return QR_MODE_NUM;
case 'a':
case 'A':
return QR_MODE_AN;
case '8':
return QR_MODE_8;
case 'k':
case 'K':
return QR_MODE_KANJI;
default:
return QR_MODE_NUL;
}
}
QRecLevel get_qr_ec_level(const char ec_level)
{
switch (ec_level) {
case '1':
case 'l':
case 'L':
return QR_ECLEVEL_L;
case '2':
case 'm':
case 'M':
return QR_ECLEVEL_M;
case '3':
case 'q':
case 'Q':
return QR_ECLEVEL_Q;
case '4':
case 'h':
case 'H':
return QR_ECLEVEL_H;
default:
return -1;
}
}
char * getQR(char * str)
{
int ret = 0;
int c = 0;
/* Default options */
Options options = {
.encode_mode = '8',
.version = 0,
.ec_level = '1',
.large = false,
.compact = false,
.border = 1,
.invert = false,
.plain = false
};
/* Generate and output QR code */
/*******************************/
QRcode *qr;
/* Ensure QR Code contains UTF-8 BOM */
if (str_has_utf8_bom(str)) {
qr = QRcode_encodeString(str, options.version,
get_qr_ec_level(options.ec_level),
get_qr_encode_mode(options.encode_mode), true);
} else {
char str_utf8[strlen(utf8_bom) + strlen(str) + 1];
memset(str_utf8, '\0', sizeof(str_utf8));
strncpy(str_utf8, utf8_bom, sizeof(str_utf8));
strncat(str_utf8, str, sizeof(str_utf8));
str_utf8[strlen(utf8_bom) + strlen(str)] = '\0';
qr = QRcode_encodeString(str_utf8, options.version,
get_qr_ec_level(options.ec_level),
get_qr_encode_mode(options.encode_mode), true);
}
/* Bail out if unable to successfully execute QRcode_encodeString() */
if (qr == NULL) {
ret = 1;
goto exit;
}
/* Enforce colorless output mode for non-terminal environments */
if (!isatty(STDOUT_FILENO)) {
options.plain = true;
}
/* Convert QR code data into text */
char *qr_code_text = qr_data_to_text(qr, options.border, options.invert,
!options.plain, options.large,
options.compact);
/* Output QR code as text */
if (qr_code_text) {
return qr_code_text;
} else {
ret = 1;
}
/* Clean up */
if (qr != NULL) {
QRcode_free(qr);
}
free(qr_code_text);
exit:
return NULL;
}