f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering/***
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering This file is part of systemd.
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering Copyright 2012 Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering systemd is free software; you can redistribute it and/or modify it
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering under the terms of the GNU Lesser General Public License as published by
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering (at your option) any later version.
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering systemd is distributed in the hope that it will be useful, but
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering Lesser General Public License for more details.
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering You should have received a copy of the GNU Lesser General Public License
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering***/
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering#include <assert.h>
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering#include <errno.h>
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering#include <qrencode.h>
cf0fbc49e67b55f8d346fc94de28c90113505297Thomas Hindoe Paaboel Andersen#include <stdbool.h>
cf0fbc49e67b55f8d346fc94de28c90113505297Thomas Hindoe Paaboel Andersen#include <stdio.h>
cf0fbc49e67b55f8d346fc94de28c90113505297Thomas Hindoe Paaboel Andersen#include <stdlib.h>
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering#include "journal-qrcode.h"
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering#define WHITE_ON_BLACK "\033[40;37;1m"
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering#define NORMAL "\033[0m"
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poetteringstatic void print_border(FILE *output, unsigned width) {
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering unsigned x, y;
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering /* Four rows of border */
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering for (y = 0; y < 4; y += 2) {
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering fputs(WHITE_ON_BLACK, output);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering for (x = 0; x < 4 + width + 4; x++)
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering fputs("\342\226\210", output);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering fputs(NORMAL "\n", output);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering }
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering}
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
92221ed7cbf0323919fc2ae379be2322a3e20558Lennart Poetteringint print_qr_code(
92221ed7cbf0323919fc2ae379be2322a3e20558Lennart Poettering FILE *output,
92221ed7cbf0323919fc2ae379be2322a3e20558Lennart Poettering const void *seed,
92221ed7cbf0323919fc2ae379be2322a3e20558Lennart Poettering size_t seed_size,
92221ed7cbf0323919fc2ae379be2322a3e20558Lennart Poettering uint64_t start,
92221ed7cbf0323919fc2ae379be2322a3e20558Lennart Poettering uint64_t interval,
92221ed7cbf0323919fc2ae379be2322a3e20558Lennart Poettering const char *hn,
92221ed7cbf0323919fc2ae379be2322a3e20558Lennart Poettering sd_id128_t machine) {
92221ed7cbf0323919fc2ae379be2322a3e20558Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering FILE *f;
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering char *url = NULL;
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering size_t url_size = 0, i;
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering QRcode* qr;
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering unsigned x, y;
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering assert(seed);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering assert(seed_size > 0);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering f = open_memstream(&url, &url_size);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering if (!f)
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering return -ENOMEM;
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering fputs("fss://", f);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering for (i = 0; i < seed_size; i++) {
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering if (i > 0 && i % 3 == 0)
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering fputc('-', f);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering fprintf(f, "%02x", ((uint8_t*) seed)[i]);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering }
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
507f22bd0172bff5e5d98145b1419bd472a2c57fZbigniew Jędrzejewski-Szmek fprintf(f, "/%"PRIx64"-%"PRIx64"?machine=" SD_ID128_FORMAT_STR,
507f22bd0172bff5e5d98145b1419bd472a2c57fZbigniew Jędrzejewski-Szmek start,
507f22bd0172bff5e5d98145b1419bd472a2c57fZbigniew Jędrzejewski-Szmek interval,
92221ed7cbf0323919fc2ae379be2322a3e20558Lennart Poettering SD_ID128_FORMAT_VAL(machine));
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering if (hn)
92221ed7cbf0323919fc2ae379be2322a3e20558Lennart Poettering fprintf(f, ";hostname=%s", hn);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering if (ferror(f)) {
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering fclose(f);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering free(url);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering return -ENOMEM;
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering }
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering fclose(f);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering qr = QRcode_encodeString(url, 0, QR_ECLEVEL_L, QR_MODE_8, 1);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering free(url);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering if (!qr)
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering return -ENOMEM;
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering print_border(output, qr->width);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering for (y = 0; y < (unsigned) qr->width; y += 2) {
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering const uint8_t *row1, *row2;
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering row1 = qr->data + qr->width * y;
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering row2 = row1 + qr->width;
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering fputs(WHITE_ON_BLACK, output);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering for (x = 0; x < 4; x++)
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering fputs("\342\226\210", output);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering for (x = 0; x < (unsigned) qr->width; x ++) {
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering bool a, b;
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering a = row1[x] & 1;
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering b = (y+1) < (unsigned) qr->width ? (row2[x] & 1) : false;
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering if (a && b)
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering fputc(' ', output);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering else if (a)
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering fputs("\342\226\204", output);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering else if (b)
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering fputs("\342\226\200", output);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering else
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering fputs("\342\226\210", output);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering }
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering for (x = 0; x < 4; x++)
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering fputs("\342\226\210", output);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering fputs(NORMAL "\n", output);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering }
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering print_border(output, qr->width);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering QRcode_free(qr);
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering return 0;
f6a971bc0bf1252e9614919ccca0d53db5fc53d9Lennart Poettering}