]>
Commit | Line | Data |
---|---|---|
f6a971bc LP |
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 <stdio.h> | |
24 | #include <errno.h> | |
25 | #include <stdlib.h> | |
26 | #include <stdbool.h> | |
27 | ||
28 | #include <qrencode.h> | |
29 | ||
30 | #include "journal-qrcode.h" | |
31 | ||
32 | #define WHITE_ON_BLACK "\033[40;37;1m" | |
33 | #define NORMAL "\033[0m" | |
34 | ||
35 | static void print_border(FILE *output, unsigned width) { | |
36 | unsigned x, y; | |
37 | ||
38 | /* Four rows of border */ | |
39 | for (y = 0; y < 4; y += 2) { | |
40 | fputs(WHITE_ON_BLACK, output); | |
41 | ||
42 | for (x = 0; x < 4 + width + 4; x++) | |
43 | fputs("\342\226\210", output); | |
44 | ||
45 | fputs(NORMAL "\n", output); | |
46 | } | |
47 | } | |
48 | ||
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) { | |
50 | FILE *f; | |
51 | char *url = NULL; | |
52 | size_t url_size = 0, i; | |
53 | QRcode* qr; | |
54 | unsigned x, y; | |
55 | ||
56 | assert(seed); | |
57 | assert(seed_size > 0); | |
58 | ||
59 | f = open_memstream(&url, &url_size); | |
60 | if (!f) | |
61 | return -ENOMEM; | |
62 | ||
63 | fputs("fss://", f); | |
64 | ||
65 | for (i = 0; i < seed_size; i++) { | |
66 | if (i > 0 && i % 3 == 0) | |
67 | fputc('-', f); | |
68 | fprintf(f, "%02x", ((uint8_t*) seed)[i]); | |
69 | } | |
70 | ||
71 | fprintf(f, "/%llx-%llx\n", (unsigned long long) start, (unsigned long long) interval); | |
72 | ||
73 | if (hn) | |
74 | fprintf(f, "?hostname=%s", hn); | |
75 | ||
76 | if (ferror(f)) { | |
77 | fclose(f); | |
78 | free(url); | |
79 | return -ENOMEM; | |
80 | } | |
81 | ||
82 | fclose(f); | |
83 | ||
84 | qr = QRcode_encodeString(url, 0, QR_ECLEVEL_L, QR_MODE_8, 1); | |
85 | free(url); | |
86 | ||
87 | if (!qr) | |
88 | return -ENOMEM; | |
89 | ||
90 | print_border(output, qr->width); | |
91 | ||
92 | for (y = 0; y < (unsigned) qr->width; y += 2) { | |
93 | const uint8_t *row1, *row2; | |
94 | ||
95 | row1 = qr->data + qr->width * y; | |
96 | row2 = row1 + qr->width; | |
97 | ||
98 | fputs(WHITE_ON_BLACK, output); | |
99 | for (x = 0; x < 4; x++) | |
100 | fputs("\342\226\210", output); | |
101 | ||
102 | for (x = 0; x < (unsigned) qr->width; x ++) { | |
103 | bool a, b; | |
104 | ||
105 | a = row1[x] & 1; | |
106 | b = (y+1) < (unsigned) qr->width ? (row2[x] & 1) : false; | |
107 | ||
108 | if (a && b) | |
109 | fputc(' ', output); | |
110 | else if (a) | |
111 | fputs("\342\226\204", output); | |
112 | else if (b) | |
113 | fputs("\342\226\200", output); | |
114 | else | |
115 | fputs("\342\226\210", output); | |
116 | } | |
117 | ||
118 | for (x = 0; x < 4; x++) | |
119 | fputs("\342\226\210", output); | |
120 | fputs(NORMAL "\n", output); | |
121 | } | |
122 | ||
123 | print_border(output, qr->width); | |
124 | ||
125 | QRcode_free(qr); | |
126 | return 0; | |
127 | } |