]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journal-qrcode.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / journal / journal-qrcode.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2012 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <qrencode.h>
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "journal-qrcode.h"
28 #include "macro.h"
29
30 #define WHITE_ON_BLACK "\033[40;37;1m"
31 #define NORMAL "\033[0m"
32
33 static void print_border(FILE *output, unsigned width) {
34 unsigned x, y;
35
36 /* Four rows of border */
37 for (y = 0; y < 4; y += 2) {
38 fputs(WHITE_ON_BLACK, output);
39
40 for (x = 0; x < 4 + width + 4; x++)
41 fputs("\342\226\210", output);
42
43 fputs(NORMAL "\n", output);
44 }
45 }
46
47 int print_qr_code(
48 FILE *output,
49 const void *seed,
50 size_t seed_size,
51 uint64_t start,
52 uint64_t interval,
53 const char *hn,
54 sd_id128_t machine) {
55
56 FILE *f;
57 char *url = NULL;
58 size_t url_size = 0, i;
59 QRcode* qr;
60 unsigned x, y;
61
62 assert(seed);
63 assert(seed_size > 0);
64
65 f = open_memstream(&url, &url_size);
66 if (!f)
67 return -ENOMEM;
68
69 fputs_unlocked("fss://", f);
70
71 for (i = 0; i < seed_size; i++) {
72 if (i > 0 && i % 3 == 0)
73 fputc_unlocked('-', f);
74 fprintf(f, "%02x", ((uint8_t*) seed)[i]);
75 }
76
77 fprintf(f, "/%"PRIx64"-%"PRIx64"?machine=" SD_ID128_FORMAT_STR,
78 start,
79 interval,
80 SD_ID128_FORMAT_VAL(machine));
81
82 if (hn)
83 fprintf(f, ";hostname=%s", hn);
84
85 if (ferror(f)) {
86 fclose(f);
87 free(url);
88 return -ENOMEM;
89 }
90
91 fclose(f);
92
93 qr = QRcode_encodeString(url, 0, QR_ECLEVEL_L, QR_MODE_8, 1);
94 free(url);
95
96 if (!qr)
97 return -ENOMEM;
98
99 print_border(output, qr->width);
100
101 for (y = 0; y < (unsigned) qr->width; y += 2) {
102 const uint8_t *row1, *row2;
103
104 row1 = qr->data + qr->width * y;
105 row2 = row1 + qr->width;
106
107 fputs(WHITE_ON_BLACK, output);
108 for (x = 0; x < 4; x++)
109 fputs("\342\226\210", output);
110
111 for (x = 0; x < (unsigned) qr->width; x ++) {
112 bool a, b;
113
114 a = row1[x] & 1;
115 b = (y+1) < (unsigned) qr->width ? (row2[x] & 1) : false;
116
117 if (a && b)
118 fputc(' ', output);
119 else if (a)
120 fputs("\342\226\204", output);
121 else if (b)
122 fputs("\342\226\200", output);
123 else
124 fputs("\342\226\210", output);
125 }
126
127 for (x = 0; x < 4; x++)
128 fputs("\342\226\210", output);
129 fputs(NORMAL "\n", output);
130 }
131
132 print_border(output, qr->width);
133
134 QRcode_free(qr);
135 return 0;
136 }