forked from GNOME/librsvg
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rsvg-io.c
335 lines (271 loc) · 9.01 KB
/
rsvg-io.c
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2000 Eazel, Inc.
Copyright (C) 2002, 2003, 2004, 2005 Dom Lachowicz <cinamod@hotmail.com>
Copyright (C) 2003, 2004, 2005 Caleb Moore <c.moore@student.unsw.edu.au>
Copyright © 2011, 2012 Christian Persch
This program 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 program 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 program; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include "rsvg-io.h"
#include "rsvg-private.h"
#include <string.h>
/* Copied from soup-request-data.c (LGPL2+):
* Copyright (C) 2009, 2010 Red Hat, Inc.
* Copyright (C) 2010 Igalia, S.L.
* and from soup-uri.c:
* Copyright 1999-2003 Ximian, Inc.
*/
#define XDIGIT(c) ((c) <= '9' ? (c) - '0' : ((c) & 0x4F) - 'A' + 10)
#define HEXCHAR(s) ((XDIGIT (s[1]) << 4) + XDIGIT (s[2]))
static char *
uri_decoded_copy (const char *part,
gsize length)
{
unsigned char *s, *d;
char *decoded = g_strndup (part, length);
s = d = (unsigned char *)decoded;
do {
if (*s == '%') {
if (!g_ascii_isxdigit (s[1]) ||
!g_ascii_isxdigit (s[2])) {
*d++ = *s;
continue;
}
*d++ = HEXCHAR (s);
s += 2;
} else {
*d++ = *s;
}
} while (*s++);
return decoded;
}
#define BASE64_INDICATOR ";base64"
#define BASE64_INDICATOR_LEN (sizeof (";base64") - 1)
static char *
rsvg_decode_data_uri (const char *uri,
char **out_mime_type,
gsize *out_len,
GError **error)
{
const char *comma, *start, *end;
char *mime_type;
char *data;
gsize data_len;
gboolean base64 = FALSE;
g_assert (out_len != NULL);
g_assert (strncmp (uri, "data:", 5) == 0);
mime_type = NULL;
start = uri + 5;
comma = strchr (start, ',');
if (comma && comma != start) {
/* Deal with MIME type / params */
if (comma > start + BASE64_INDICATOR_LEN &&
!g_ascii_strncasecmp (comma - BASE64_INDICATOR_LEN, BASE64_INDICATOR, BASE64_INDICATOR_LEN)) {
end = comma - BASE64_INDICATOR_LEN;
base64 = TRUE;
} else {
end = comma;
}
if (end != start) {
mime_type = uri_decoded_copy (start, end - start);
}
}
if (comma)
start = comma + 1;
if (*start) {
data = uri_decoded_copy (start, strlen (start));
if (base64)
data = (char *) g_base64_decode_inplace (data, &data_len);
else
data_len = strlen (data);
} else {
data = NULL;
data_len = 0;
}
if (out_mime_type)
*out_mime_type = mime_type;
else
g_free (mime_type);
*out_len = data_len;
return data;
}
static char *
rsvg_acquire_file_data (const char *uri,
char **out_mime_type,
gsize *out_len,
GCancellable *cancellable,
GError **error)
{
GFile *file;
gchar *path, *data;
gsize len;
char *content_type;
rsvg_return_val_if_fail (uri != NULL, NULL, error);
g_assert (out_len != NULL);
file = g_file_new_for_uri (uri);
path = g_file_get_path (file);
if (path == NULL) {
g_object_unref (file);
return NULL;
}
if (!g_file_get_contents (path, &data, &len, error)) {
g_free (path);
g_object_unref (file);
return NULL;
}
if (out_mime_type &&
(content_type = g_content_type_guess (path, (guchar *) data, len, NULL))) {
*out_mime_type = g_content_type_get_mime_type (content_type);
g_free (content_type);
}
g_free (path);
g_object_unref (file);
*out_len = len;
return data;
}
static GInputStream *
rsvg_acquire_gvfs_stream (const char *uri,
const char *base_uri,
char **out_mime_type,
GCancellable *cancellable,
GError **error)
{
GFile *base, *file;
GFileInputStream *stream;
GError *err = NULL;
file = g_file_new_for_uri (uri);
stream = g_file_read (file, cancellable, &err);
g_object_unref (file);
if (stream == NULL &&
g_error_matches (err, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) {
g_clear_error (&err);
base = g_file_new_for_uri (base_uri);
file = g_file_resolve_relative_path (base, uri);
g_object_unref (base);
stream = g_file_read (file, cancellable, &err);
g_object_unref (file);
}
if (stream == NULL) {
g_propagate_error (error, err);
return NULL;
}
if (out_mime_type) {
GFileInfo *file_info;
const char *content_type;
file_info = g_file_input_stream_query_info (stream,
G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
cancellable,
NULL /* error */);
if (file_info &&
(content_type = g_file_info_get_content_type (file_info)))
*out_mime_type = g_content_type_get_mime_type (content_type);
else
*out_mime_type = NULL;
if (file_info)
g_object_unref (file_info);
}
return G_INPUT_STREAM (stream);
}
static char *
rsvg_acquire_gvfs_data (const char *uri,
const char *base_uri,
char **out_mime_type,
gsize *out_len,
GCancellable *cancellable,
GError **error)
{
GFile *base, *file;
GError *err;
char *data;
gsize len;
char *content_type;
gboolean res;
file = g_file_new_for_uri (uri);
err = NULL;
data = NULL;
if (!(res = g_file_load_contents (file, cancellable, &data, &len, NULL, &err)) &&
g_error_matches (err, G_IO_ERROR, G_IO_ERROR_NOT_FOUND) &&
base_uri != NULL) {
g_clear_error (&err);
g_object_unref (file);
base = g_file_new_for_uri (base_uri);
file = g_file_resolve_relative_path (base, uri);
g_object_unref (base);
res = g_file_load_contents (file, cancellable, &data, &len, NULL, &err);
}
g_object_unref (file);
if (err) {
g_propagate_error (error, err);
return NULL;
}
if (out_mime_type &&
(content_type = g_content_type_guess (uri, (guchar *) data, len, NULL))) {
*out_mime_type = g_content_type_get_mime_type (content_type);
g_free (content_type);
}
*out_len = len;
return data;
}
char *
_rsvg_io_acquire_data (const char *href,
const char *base_uri,
char **mime_type,
gsize *len,
GCancellable *cancellable,
GError **error)
{
char *data;
gsize llen;
if (!(href && *href)) {
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Invalid URI");
return NULL;
}
if (!len)
len = &llen;
if (strncmp (href, "data:", 5) == 0)
return rsvg_decode_data_uri (href, mime_type, len, error);
if ((data = rsvg_acquire_file_data (href, mime_type, len, cancellable, NULL)))
return data;
if ((data = rsvg_acquire_gvfs_data (href, base_uri, mime_type, len, cancellable, error)))
return data;
return NULL;
}
GInputStream *
_rsvg_io_acquire_stream (const char *href,
const char *base_uri,
char **mime_type,
GCancellable *cancellable,
GError **error)
{
GInputStream *stream;
char *data;
gsize len;
if (!(href && *href)) {
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Invalid URI");
return NULL;
}
if (strncmp (href, "data:", 5) == 0) {
if (!(data = rsvg_decode_data_uri (href, mime_type, &len, error)))
return NULL;
return g_memory_input_stream_new_from_data (data, len, (GDestroyNotify) g_free);
}
if ((data = rsvg_acquire_file_data (href, mime_type, &len, cancellable, NULL)))
return g_memory_input_stream_new_from_data (data, len, (GDestroyNotify) g_free);
if ((stream = rsvg_acquire_gvfs_stream (href, base_uri, mime_type, cancellable, error)))
return stream;
return NULL;
}