1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Lennart Poettering
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.
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.
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/>.
30 #include "journal-qrcode.h"
32 #define WHITE_ON_BLACK "\033[40;37;1m"
33 #define NORMAL "\033[0m"
35 static void print_border(FILE *output, unsigned width) {
38 /* Four rows of border */
39 for (y = 0; y < 4; y += 2) {
40 fputs(WHITE_ON_BLACK, output);
42 for (x = 0; x < 4 + width + 4; x++)
43 fputs("\342\226\210", output);
45 fputs(NORMAL "\n", output);
49 int print_qr_code(FILE *output, const void *seed, size_t seed_size, uint64_t start, uint64_t interval, const char *hn, sd_id128_t mahcine) {
52 size_t url_size = 0, i;
57 assert(seed_size > 0);
59 f = open_memstream(&url, &url_size);
65 for (i = 0; i < seed_size; i++) {
66 if (i > 0 && i % 3 == 0)
68 fprintf(f, "%02x", ((uint8_t*) seed)[i]);
71 fprintf(f, "/%llx-%llx\n", (unsigned long long) start, (unsigned long long) interval);
74 fprintf(f, "?hostname=%s", hn);
84 qr = QRcode_encodeString(url, 0, QR_ECLEVEL_L, QR_MODE_8, 1);
90 print_border(output, qr->width);
92 for (y = 0; y < (unsigned) qr->width; y += 2) {
93 const uint8_t *row1, *row2;
95 row1 = qr->data + qr->width * y;
96 row2 = row1 + qr->width;
98 fputs(WHITE_ON_BLACK, output);
99 for (x = 0; x < 4; x++)
100 fputs("\342\226\210", output);
102 for (x = 0; x < (unsigned) qr->width; x ++) {
106 b = (y+1) < (unsigned) qr->width ? (row2[x] & 1) : false;
111 fputs("\342\226\204", output);
113 fputs("\342\226\200", output);
115 fputs("\342\226\210", output);
118 for (x = 0; x < 4; x++)
119 fputs("\342\226\210", output);
120 fputs(NORMAL "\n", output);
123 print_border(output, qr->width);