Debian Codegraph @

src/shared/qrcode-util.c

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