]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/qrcode-util.c
5e61e84a9e2b3f3debda12520c7c6503cfd83e8d
[thirdparty/systemd.git] / src / shared / qrcode-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "qrcode-util.h"
4
5 #if HAVE_QRENCODE
6 #include <qrencode.h>
7 #include <stdio.h>
8
9 #include "ansi-color.h"
10 #include "dlfcn-util.h"
11 #include "locale-util.h"
12 #include "log.h"
13 #include "strv.h"
14 #include "terminal-util.h"
15
16 #define ANSI_WHITE_ON_BLACK "\033[40;37;1m"
17 #define UNICODE_FULL_BLOCK UTF8("█")
18 #define UNICODE_LOWER_HALF_BLOCK UTF8("▄")
19 #define UNICODE_UPPER_HALF_BLOCK UTF8("▀")
20
21 static void *qrcode_dl = NULL;
22
23 static DLSYM_PROTOTYPE(QRcode_encodeString) = NULL;
24 static DLSYM_PROTOTYPE(QRcode_free) = NULL;
25
26 int dlopen_qrencode(void) {
27 int r;
28
29 ELF_NOTE_DLOPEN("qrencode",
30 "Support for generating QR codes",
31 ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED,
32 "libqrencode.so.4", "libqrencode.so.3");
33
34 FOREACH_STRING(s, "libqrencode.so.4", "libqrencode.so.3") {
35 r = dlopen_many_sym_or_warn(
36 &qrcode_dl, s, LOG_DEBUG,
37 DLSYM_ARG(QRcode_encodeString),
38 DLSYM_ARG(QRcode_free));
39 if (r >= 0)
40 break;
41 }
42
43 return r;
44 }
45
46 static void print_border(FILE *output, unsigned width, unsigned row, unsigned column) {
47 assert(output);
48 assert(width);
49
50 if (row != UINT_MAX && column != UINT_MAX) {
51 int r, fd;
52
53 fd = fileno(output);
54 if (fd < 0)
55 return (void)log_debug_errno(errno, "Failed to get file descriptor from the file stream: %m");
56
57 r = terminal_set_cursor_position(fd, row, column);
58 if (r < 0)
59 log_warning_errno(r, "Failed to move terminal cursor position, ignoring: %m");
60
61 /* Four rows of border */
62 for (unsigned y = 0; y < 4; y += 2) {
63 fputs(ANSI_WHITE_ON_BLACK, output);
64
65 for (unsigned x = 0; x < 4 + width + 4; x++)
66 fputs(UNICODE_FULL_BLOCK, output);
67
68 fputs(ANSI_NORMAL "\n", output);
69 r = terminal_set_cursor_position(fd, row + 1, column);
70 if (r < 0)
71 log_warning_errno(r, "Failed to move terminal cursor position, ignoring: %m");
72 }
73 } else {
74 /* Four rows of border */
75 for (unsigned y = 0; y < 4; y += 2) {
76 fputs(ANSI_WHITE_ON_BLACK, output);
77
78 for (unsigned x = 0; x < 4 + width + 4; x++)
79 fputs(UNICODE_FULL_BLOCK, output);
80
81 fputs(ANSI_NORMAL "\n", output);
82 }
83 }
84 }
85
86 static void write_qrcode(FILE *output, QRcode *qr, unsigned row, unsigned column) {
87 assert(qr);
88
89 if (!output)
90 output = stdout;
91
92 print_border(output, qr->width, row, column);
93
94 if (row != UINT_MAX && column != UINT_MAX) {
95 /* After printing two rows of top border, we need to move the cursor down two rows before starting to print the actual QR code */
96 int r, fd, move_down = 2;
97 fd = fileno(output);
98 if (fd < 0)
99 return (void)log_debug_errno(errno, "Failed to get file descriptor from the file stream: %m");
100
101 r = terminal_set_cursor_position(fd, row + move_down, column);
102 if (r < 0)
103 log_warning_errno(r, "Failed to move terminal cursor position, ignoring: %m");
104
105 for (unsigned y = 0; y < (unsigned) qr->width; y += 2) {
106 const uint8_t *row1 = qr->data + qr->width * y;
107 const uint8_t *row2 = row1 + qr->width;
108
109 fputs(ANSI_WHITE_ON_BLACK, output);
110
111 for (unsigned x = 0; x < 4; x++)
112 fputs(UNICODE_FULL_BLOCK, output);
113
114 for (unsigned x = 0; x < (unsigned) qr->width; x++) {
115 bool a, b;
116
117 a = row1[x] & 1;
118 b = (y+1) < (unsigned) qr->width ? (row2[x] & 1) : false;
119
120 if (a && b)
121 fputc(' ', output);
122 else if (a)
123 fputs(UNICODE_LOWER_HALF_BLOCK, output);
124 else if (b)
125 fputs(UNICODE_UPPER_HALF_BLOCK, output);
126 else
127 fputs(UNICODE_FULL_BLOCK, output);
128 }
129
130 for (unsigned x = 0; x < 4; x++)
131 fputs(UNICODE_FULL_BLOCK, output);
132 r = terminal_set_cursor_position(fd, row + move_down, column);
133 if (r < 0)
134 log_warning_errno(r, "Failed to move terminal cursor position, ignoring: %m");
135 move_down += 1;
136 fputs(ANSI_NORMAL "\n", output);
137 }
138
139 print_border(output, qr->width, row + move_down, column);
140 } else {
141
142 for (unsigned y = 0; y < (unsigned) qr->width; y += 2) {
143 const uint8_t *row1 = qr->data + qr->width * y;
144 const uint8_t *row2 = row1 + qr->width;
145
146 fputs(ANSI_WHITE_ON_BLACK, output);
147 for (unsigned x = 0; x < 4; x++)
148 fputs(UNICODE_FULL_BLOCK, output);
149
150 for (unsigned x = 0; x < (unsigned) qr->width; x++) {
151 bool a, b;
152
153 a = row1[x] & 1;
154 b = (y+1) < (unsigned) qr->width ? (row2[x] & 1) : false;
155
156 if (a && b)
157 fputc(' ', output);
158 else if (a)
159 fputs(UNICODE_LOWER_HALF_BLOCK, output);
160 else if (b)
161 fputs(UNICODE_UPPER_HALF_BLOCK, output);
162 else
163 fputs(UNICODE_FULL_BLOCK, output);
164 }
165
166 for (unsigned x = 0; x < 4; x++)
167 fputs(UNICODE_FULL_BLOCK, output);
168 fputs(ANSI_NORMAL "\n", output);
169 }
170
171 print_border(output, qr->width, row, column);
172 }
173
174 fflush(output);
175 }
176
177 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(QRcode*, sym_QRcode_free, NULL);
178
179 int print_qrcode_full(
180 FILE *out,
181 const char *header,
182 const char *string,
183 unsigned row,
184 unsigned column,
185 unsigned tty_width,
186 unsigned tty_height,
187 bool check_tty) {
188
189 int r;
190
191 /* If this is not a UTF-8 system or ANSI colors aren't supported/disabled don't print any QR
192 * codes */
193 if (!is_locale_utf8())
194 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Not an UTF-8 system, cannot print qrcode");
195 if (check_tty && !colors_enabled())
196 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Colors are disabled, cannot print qrcode");
197
198 r = dlopen_qrencode();
199 if (r < 0)
200 return r;
201
202 _cleanup_(sym_QRcode_freep) QRcode *qr =
203 sym_QRcode_encodeString(string, 0, QR_ECLEVEL_L, QR_MODE_8, 1);
204 if (!qr)
205 return log_oom_debug();
206
207 if (row != UINT_MAX && column != UINT_MAX) {
208 unsigned qr_code_width, qr_code_height;
209
210 int fd = fileno(out);
211 if (fd < 0)
212 return log_debug_errno(errno, "Failed to get file descriptor from the file stream: %m");
213
214 qr_code_width = qr_code_height = qr->width + 8;
215 if (column + qr_code_width > tty_width)
216 column = tty_width - qr_code_width;
217
218 /* Terminal characters are twice as high as they are wide so it's qr_code_height / 2,
219 * our QR code prints an extra new line, so we have -1 as well */
220 if (row + qr_code_height > tty_height)
221 row = tty_height - (qr_code_height / 2 ) - 1;
222
223 if (header) {
224 r = terminal_set_cursor_position(fd, row - 2, tty_width - qr_code_width - 2);
225 if (r < 0)
226 log_warning_errno(r, "Failed to move terminal cursor position, ignoring: %m");
227
228 fprintf(out, "%s:\n\n", header);
229 }
230 } else
231 if (header)
232 fprintf(out, "\n%s:\n\n", header);
233
234 write_qrcode(out, qr, row, column);
235 fputc('\n', out);
236
237 return 0;
238 }
239 #endif