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