-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.c
210 lines (182 loc) · 4.43 KB
/
main.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
#include "QR_Encode.h"
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH) /* 0664 */
#define WHITE "\x1b[47m\x1b[30m"
#define BLACK "\x1b[40m\x1b[37m"
#define RESET "\x1b[0m"
static int margin = 4;
/* Because getprogname(3) is not portable yet. */
const char* progname;
static void usage(void);
static void white(void);
static void black(void);
static void nl(void);
static void top_bottom_margin(int width);
static void left_right_margin(void);
static void ansi_qr(const unsigned char* data, int width);
static void usage(void)
{
fprintf(stderr, "%s [-l level] [-v version] [-p mask] [-o file] "
"[-m margin] <input string>\n",
progname);
fprintf(stderr, "level, version and mask defaults to 3, 0 and auto respectively\n");
exit(2);
}
static void printError(int error)
{
switch (error) {
case QR_E_EmptyInput:
dprintf(STDOUT_FILENO, "empty input\n");
break;
case QR_E_VersionNotFit:
dprintf(STDOUT_FILENO, "Required version does not fit into output buffer\n");
break;
case QR_E_InvalidVersion:
dprintf(STDOUT_FILENO, "Input data can't fit in QR code\n");
break;
default:
dprintf(STDOUT_FILENO, "Unknown error\n");
break;
}
}
int main(int argc, char* argv[])
{
progname = argv[0];
QR_Level level = QR_LEVEL_H;
int version = 0;
QR_MaskPattern mask = QR_MaskAuto;
char* fname = NULL;
int fd = -1;
int ch;
while ((ch = getopt(argc, argv, "l:v:o:m:p:")) != -1) {
switch (ch) {
case 'l':
level = atoi(optarg);
break;
case 'v':
version = atoi(optarg);
break;
case 'o':
fname = optarg;
break;
case 'm':
margin = atoi(optarg);
break;
case 'p':
mask = atoi(optarg);
break;
default:
usage();
/* NOTREACHED */
}
}
argc -= optind;
argv += optind;
if (argc != 1) {
usage();
/* NOTREACHED */
}
errno = EINVAL;
if (level < QR_LEVEL_L || level > QR_LEVEL_H) {
err(1, "level");
}
if (version < 0 || version > 40) {
err(1, "version");
}
if (mask < QR_MaskAuto || mask > QR_Mask8) {
err(1, "mask");
}
errno = 0;
unsigned char encoded[MAX_BITDATA];
int width = EncodeData(level, version, mask, argv[0], 0, encoded);
if (width <= 0) {
printError(width);
exit(EXIT_FAILURE);
}
int size = ((width * width) / 8) + (((width * width) % 8) ? 1 : 0);
printf("QR Code width: %d\n", width);
if (fname != NULL && *fname != '\0') {
fd = open(fname, O_WRONLY | O_CREAT | O_TRUNC, MODE);
if (fd == -1) {
err(1, "open");
}
printf("writing file (%d bytes)\n", size);
if (write(fd, encoded, size) != size) {
err(1, "write");
}
close(fd);
}
ansi_qr(encoded, width);
return 0;
}
enum { was_reset,
was_white,
was_black } static last_color
= was_reset;
static void white(void)
{
if (last_color != was_white) {
printf(WHITE);
last_color = was_white;
}
printf(" ");
}
static void black(void)
{
if (last_color != was_black) {
printf(BLACK);
last_color = was_black;
}
printf(" ");
}
static void nl(void)
{
if (last_color != was_reset) {
printf(RESET);
last_color = was_reset;
}
printf("\n");
}
static void top_bottom_margin(int width)
{
int i, j;
for (i = 0; i < margin; i++) {
for (j = 0; j < width + (margin * 2); j++) {
white();
}
nl();
}
}
static void left_right_margin(void)
{
int i;
for (i = 0; i < margin; i++) {
white();
}
}
static void ansi_qr(const unsigned char* data, int width)
{
int i, j;
top_bottom_margin(width);
for (i = 0; i < width; i++) {
left_right_margin();
for (j = 0; j < width; j++) {
long byte = (i * width + j) / 8;
long bit = (i * width + j) % 8;
if (data[byte] & (0x80 >> bit)) {
black();
} else {
white();
}
}
left_right_margin();
nl();
}
top_bottom_margin(width);
}