1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
/*
This file is part of TALER
Copyright (C) 2020 Taler Systems SA
TALER is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
* @file merchant/backend/taler-merchant-httpd_qr.c
* @brief logic to create QR codes in HTML
* @author Christian Grothoff
*/
#include "platform.h"
#include <gnunet/gnunet_util_lib.h>
#include <qrencode.h>
/**
* Create the HTML for a QR code for a URI.
*
* @param uri input string to encode
* @return NULL on error, encoded URI otherwise
*/
char *
TMH_create_qrcode (const char *uri)
{
QRinput *qri;
QRcode *qrc;
struct GNUNET_Buffer buf = { 0 };
qri = QRinput_new2 (0,
QR_ECLEVEL_M);
if (NULL == qri)
{
GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
"QRinput_new2");
return NULL;
}
/* first try encoding as uppercase-only alpha-numerical
QR code (much smaller encoding); if that fails, also
try using binary encoding */
if ( (0 !=
QRinput_append (qri,
QR_MODE_AN,
strlen (uri),
(unsigned char *) uri)) &&
(0 !=
QRinput_append (qri,
QR_MODE_8,
strlen (uri),
(unsigned char *) uri)) )
{
GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
"QRinput_append");
QRinput_free (qri);
return NULL;
}
qrc = QRcode_encodeInput (qri);
if (NULL == qrc)
{
GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
"QRcode_encodeInput");
QRinput_free (qri);
return NULL;
}
QRinput_free (qri);
/* FIXME-Dold: generate <img> with inline SVG instead of <pre> here! */
GNUNET_buffer_write_str (&buf,
"<p class=\"qrtext\"><br>\n<br>\n<br>\n<br>\n");
for (unsigned int y = 0; y<qrc->width; y++)
{
GNUNET_buffer_write_str (&buf,
" ");
for (unsigned int x = 0; x<qrc->width; x++)
{
unsigned int off = x + y * qrc->width;
GNUNET_buffer_write_fstr (&buf,
"%s",
(0 != (qrc->data[off] & 1))
? "██"
: " ");
}
GNUNET_buffer_write_str (&buf,
" <br>");
}
GNUNET_buffer_write_str (&buf,
"<br>\n<br>\n<br>\n<br>\n</p>");
QRcode_free (qrc);
return GNUNET_buffer_reap_str (&buf);
}
|