summaryrefslogtreecommitdiff
path: root/src/backend/taler-merchant-httpd_qr.c
blob: ddb73167b4e0f8eff825c3d550ca2de07356e9a0 (plain)
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);
  GNUNET_buffer_write_fstr (&buf,
                           "<svg width='100mm' height='100mm' viewBox='0 0 %u %u' "
                           "version='1.1' xmlns='http://www.w3.org/2000/svg' "
                           "style='shape-rendering: crispedges;'>\n",
                           qrc->width,
                           qrc->width);
  for (unsigned int y = 0; y<qrc->width; y++)
  {
    for (unsigned int x = 0; x<qrc->width; x++)
    {
      unsigned int off = x + y * qrc->width;
      if (0 == (qrc->data[off] & 1))
        continue;
      GNUNET_buffer_write_fstr (&buf,
                                " <rect x=%u y=%u width=1 height=1 />\n",
                                x,
                                y);
    }
  }
  GNUNET_buffer_write_str (&buf,
                           "</svg>");
  QRcode_free (qrc);
  return GNUNET_buffer_reap_str (&buf);
}