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