forked from mjg59/tpmtotp
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathqrenc.c
142 lines (118 loc) · 2.8 KB
/
qrenc.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
/*
* qrenc - Output an ASCII QR encode for the input
*
* Copyright 2016 Trammell Hudson <[email protected]>
* Copyright 2015 Matthew Garrett <[email protected]>
* Copyright 2015 Andreas Fuchs, Fraunhofer SIT
*
* Portions derived from sealfile.c by J. Kravitz and Copyright (C) 2004 IBM
* Corporation
*
* Portions derived from qrenc.c Copyright (C) 2006-2012 Kentaro Fukuchi
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
#include <qrencode.h>
static int unicode_output = 1;
static const int margin = 1;
static const char * utf8(int cp)
{
static unsigned char buf[4];
buf[0] = 0xE0 | ((cp >> 12) & 0x0F);
buf[1] = 0x80 | ((cp >> 6) & 0x3F);
buf[2] = 0x80 | ((cp >> 0) & 0x3F);
buf[3] = '\0';
return (const char*) buf;
}
static const char * block(int cp)
{
if (unicode_output)
{
if (cp == 0)
return utf8(0x2588);
if (cp == 1)
return utf8(0x2580);
if (cp == 2)
return utf8(0x2584);
if (cp == 3)
return " ";
} else {
// code page whatever
if (cp == 0) return "\xDB";
if (cp == 1) return "\xDC";
if (cp == 2) return "\xDF";
if (cp == 3) return " ";
}
return "--?";
}
static int
writeANSI(
const QRcode * const qrcode,
FILE * const fp
)
{
/* raw data */
const unsigned char * const p = qrcode->data;
for(int y=0 ; y < margin ; y++)
{
for(int x=0; x<qrcode->width + 4 * margin; x++)
fputs(block(0), fp);
fputs("\n", fp);
}
for(int y=0; y < qrcode->width; y += 2)
{
const unsigned char * const row0 = p + (y+0)*qrcode->width;
const unsigned char * const row1 = p + (y+1)*qrcode->width;
for(int x=0; x < margin*2; x++ )
fputs(block(0), fp);
for(int x=0; x < qrcode->width; x++)
{
int r0 = row0[x] & 0x1;
int r1 = y < qrcode->width-1 ? row1[x] & 0x1 : 0;
fputs(block(r0 << 1 | r1 << 0), fp);
}
for(int x=0; x<margin*2; x++ )
fputs(block(0), fp);
fputs("\n", fp);
}
/* bottom margin; only do a half block on the last one */
for(int y=0 ; y < margin ; y++)
{
for(int x=0; x<qrcode->width + 4 * margin; x++)
fputs(block(1), fp);
fputs("\n", fp);
}
return 0;
}
int main(int argc, char *argv[])
{
const char * qr_string = "";
if (argc > 1)
qr_string = argv[1];
if (strlen(qr_string) == 0)
{
fprintf(stderr, "%s: empty strings are not valid\n", argv[0]);
return -1;
}
QRcode * qrcode = QRcode_encodeString(
qr_string,
0,
QR_ECLEVEL_L,
QR_MODE_8,
1
);
writeANSI(qrcode, stdout);
return 0;
}