summaryrefslogtreecommitdiff
path: root/pango/css/gtkcssdataurl.c
blob: 53b1ed93bdb4e8821860a62bc59b416f026e758a (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* GStreamer data:// uri source element
 * Copyright (C) 2009 Igalia S.L
 * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/*<private>
 * # Data URLs
 *
 * These function allow encoding and decoding of data: URLs, see
 * [RFC 2397](http://tools.ietf.org/html/rfc2397) for more information.
 */

#include "config.h"

#include "gtkcssdataurlprivate.h"

#include <string.h>

#define _(x) x

/*<private>
 * gtk_css_data_url_parse:
 * @url: the URL to parse
 * @out_mimetype: (out nullable optional): Return location to set the contained
 *   mime type to. If no mime type was specified, this value is set to %NULL.
 * @error: error location
 *
 * Decodes a data URL according to RFC2397 and returns the decoded data.
 *
 * Returns: a new `GBytes` with the decoded data
 */
GBytes *
gtk_css_data_url_parse (const char  *url,
                        char       **out_mimetype,
                        GError     **error)
{
  char *mimetype = NULL;
  const char *parameters_start;
  const char *data_start;
  GBytes *bytes;
  gboolean base64 = FALSE;
  char *charset = NULL;
  gpointer bdata;
  gsize bsize;

  /* url must be an URI as defined in RFC 2397
   * data:[<mediatype>][;base64],<data>
   */
  if (g_ascii_strncasecmp ("data:", url, 5) != 0)
    {
      g_set_error (error,
                   G_IO_ERROR,
                   G_IO_ERROR_INVALID_FILENAME,
                   _("Not a data: URL"));
      return NULL;
    }

  url += 5;

  parameters_start = strchr (url, ';');
  data_start = strchr (url, ',');
  if (data_start == NULL)
    {
      g_set_error (error,
                   G_IO_ERROR,
                   G_IO_ERROR_INVALID_FILENAME,
                   _("Malformed data: URL"));
      return NULL;
    }
  if (parameters_start > data_start)
    parameters_start = NULL;

  if (data_start != url && parameters_start != url)
    {
      mimetype = g_strndup (url,
                            (parameters_start ? parameters_start
                                              : data_start) - url);
    }
  else
    {
      mimetype = NULL;
    }

  if (parameters_start != NULL)
    {
      char *parameters_str;
      char **parameters;
      guint i;

      parameters_str = g_strndup (parameters_start + 1, data_start - parameters_start - 1);
      parameters = g_strsplit (parameters_str, ";", -1);

      for (i = 0; parameters[i] != NULL; i++)
        {
          if (g_ascii_strcasecmp ("base64", parameters[i]) == 0)
            {
              base64 = TRUE;
            }
          else if (g_ascii_strncasecmp ("charset=", parameters[i], 8) == 0)
            {
              g_free (charset);
              charset = g_strdup (parameters[i] + 8);
            }
        }
    g_free (parameters_str);
    g_strfreev (parameters);
  }

  /* Skip comma */
  data_start += 1;
  if (base64)
    {
      bdata = g_base64_decode (data_start, &bsize);
    }
  else
    {
      /* URI encoded, i.e. "percent" encoding */
      /* XXX: This doesn't allow nul bytes */
      bdata = g_uri_unescape_string (data_start, NULL);
      if (bdata == NULL)
        {
          g_set_error (error,
                       G_IO_ERROR,
                       G_IO_ERROR_INVALID_FILENAME,
                       _("Could not unescape string"));
          g_free (mimetype);
          return NULL;
        }
      bsize = strlen (bdata);
    }

  /* Convert to UTF8 */
  if ((mimetype == NULL || g_ascii_strcasecmp ("text/plain", mimetype) == 0) &&
      charset && g_ascii_strcasecmp ("US-ASCII", charset) != 0
      && g_ascii_strcasecmp ("UTF-8", charset) != 0)
    {
      gsize read;
      gsize written;
      gpointer data;

      data = g_convert_with_fallback (bdata, bsize,
                                      "UTF-8", charset, 
                                      (char *) "*",
                                      &read, &written, NULL);
      g_free (bdata);

      bdata = data;
      bsize = written;
    }
  bytes = g_bytes_new_take (bdata, bsize);

  g_free (charset);
  if (out_mimetype)
    *out_mimetype = mimetype;
  else
    g_free (mimetype);

  return bytes;
}