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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
*
* GimpXmlParser
* Copyright (C) 2003 Sven Neumann <sven@gimp.org>
*
* This program 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <gio/gio.h>
#include "config-types.h"
#include "gimpxmlparser.h"
struct _GimpXmlParser
{
GMarkupParseContext *context;
};
static gboolean parse_encoding (const gchar *text,
gint text_len,
gchar **encodind);
/**
* gimp_xml_parser_new:
* @markup_parser: a #GMarkupParser
* @user_data: user data to pass to #GMarkupParser functions
*
* GimpXmlParser is a thin wrapper around GMarkupParser. This function
* creates one for you and sets up a GMarkupParseContext.
*
* Returns: a new #GimpXmlParser
**/
GimpXmlParser *
gimp_xml_parser_new (const GMarkupParser *markup_parser,
gpointer user_data)
{
GimpXmlParser *parser;
g_return_val_if_fail (markup_parser != NULL, NULL);
parser = g_slice_new (GimpXmlParser);
parser->context = g_markup_parse_context_new (markup_parser,
0, user_data, NULL);
return parser;
}
/**
* gimp_xml_parser_parse_file:
* @parser: a #GimpXmlParser
* @filename: name of a file to parse
* @error: return location for possible errors
*
* This function creates a GIOChannel for @filename and calls
* gimp_xml_parser_parse_io_channel() for you.
*
* Returns: %TRUE on success, %FALSE otherwise
**/
gboolean
gimp_xml_parser_parse_file (GimpXmlParser *parser,
const gchar *filename,
GError **error)
{
GIOChannel *io;
gboolean success;
g_return_val_if_fail (parser != NULL, FALSE);
g_return_val_if_fail (filename != NULL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
io = g_io_channel_new_file (filename, "r", error);
if (!io)
return FALSE;
success = gimp_xml_parser_parse_io_channel (parser, io, error);
g_io_channel_unref (io);
return success;
}
/**
* gimp_xml_parser_parse_gfile:
* @parser: a #GimpXmlParser
* @file: the #GFile to parse
* @error: return location for possible errors
*
* This function creates a GIOChannel for @file and calls
* gimp_xml_parser_parse_io_channel() for you.
*
* Returns: %TRUE on success, %FALSE otherwise
**/
gboolean
gimp_xml_parser_parse_gfile (GimpXmlParser *parser,
GFile *file,
GError **error)
{
gchar *path;
gboolean success;
g_return_val_if_fail (parser != NULL, FALSE);
g_return_val_if_fail (G_IS_FILE (file), FALSE);
path = g_file_get_path (file);
success = gimp_xml_parser_parse_file (parser, path, error);
g_free (path);
return success;
}
/**
* gimp_xml_parser_parse_fd:
* @parser: a #GimpXmlParser
* @fd: a file descriptor
* @error: return location for possible errors
*
* This function creates a GIOChannel for @fd and calls
* gimp_xml_parser_parse_io_channel() for you.
*
* Returns: %TRUE on success, %FALSE otherwise
**/
gboolean
gimp_xml_parser_parse_fd (GimpXmlParser *parser,
gint fd,
GError **error)
{
GIOChannel *io;
gboolean success;
g_return_val_if_fail (parser != NULL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
#ifdef G_OS_WIN32
io = g_io_channel_win32_new_fd (fd);
#else
io = g_io_channel_unix_new (fd);
#endif
success = gimp_xml_parser_parse_io_channel (parser, io, error);
g_io_channel_unref (io);
return success;
}
/**
* gimp_xml_parser_parse_io_channel:
* @parser: a #GimpXmlParser
* @io: a #GIOChannel
* @error: return location for possible errors
*
* Makes @parser read from the specified @io channel. This function
* returns when the GIOChannel becomes empty (end of file) or an
* error occurs, either reading from @io or parsing the read data.
*
* This function tries to determine the character encoding from the
* XML header and converts the content to UTF-8 for you. For this
* feature to work, the XML header with the encoding attribute must be
* contained in the first 4096 bytes read. Otherwise UTF-8 encoding
* will be assumed and parsing may break later if this assumption
* was wrong.
*
* Returns: %TRUE on success, %FALSE otherwise
**/
gboolean
gimp_xml_parser_parse_io_channel (GimpXmlParser *parser,
GIOChannel *io,
GError **error)
{
GIOStatus status;
gchar buffer[4096];
gsize len = 0;
gsize bytes;
const gchar *io_encoding;
gchar *encoding = NULL;
g_return_val_if_fail (parser != NULL, FALSE);
g_return_val_if_fail (io != NULL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
io_encoding = g_io_channel_get_encoding (io);
if (g_strcmp0 (io_encoding, "UTF-8"))
{
g_warning ("gimp_xml_parser_parse_io_channel():\n"
"The encoding has already been set on this GIOChannel!");
return FALSE;
}
/* try to determine the encoding */
g_io_channel_set_encoding (io, NULL, NULL);
while (len < sizeof (buffer))
{
status = g_io_channel_read_chars (io, buffer + len, 1, &bytes, error);
len += bytes;
if (status == G_IO_STATUS_ERROR)
return FALSE;
if (status == G_IO_STATUS_EOF)
break;
if (parse_encoding (buffer, len, &encoding))
break;
}
if (encoding)
{
if (! g_io_channel_set_encoding (io, encoding, error))
return FALSE;
g_free (encoding);
}
else
{
g_io_channel_set_encoding (io, "UTF-8", NULL);
}
while (TRUE)
{
if (!g_markup_parse_context_parse (parser->context, buffer, len, error))
return FALSE;
status = g_io_channel_read_chars (io,
buffer, sizeof (buffer), &len, error);
switch (status)
{
case G_IO_STATUS_ERROR:
return FALSE;
case G_IO_STATUS_EOF:
return g_markup_parse_context_end_parse (parser->context, error);
case G_IO_STATUS_NORMAL:
case G_IO_STATUS_AGAIN:
break;
}
}
}
/**
* gimp_xml_parser_parse_buffer:
* @parser: a #GimpXmlParser
* @buffer: a string buffer
* @len: the number of byes in @buffer or -1 if @buffer is nul-terminated
* @error: return location for possible errors
*
* This function uses the given @parser to parse the XML in @buffer.
*
* Returns: %TRUE on success, %FALSE otherwise
**/
gboolean
gimp_xml_parser_parse_buffer (GimpXmlParser *parser,
const gchar *buffer,
gssize len,
GError **error)
{
gchar *encoding = NULL;
gchar *conv = NULL;
gboolean success;
g_return_val_if_fail (parser != NULL, FALSE);
g_return_val_if_fail (buffer != NULL || len == 0, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (len < 0)
len = strlen (buffer);
if (parse_encoding (buffer, len, &encoding) && encoding)
{
if (g_ascii_strcasecmp (encoding, "UTF-8") &&
g_ascii_strcasecmp (encoding, "UTF8"))
{
gsize written;
conv = g_convert (buffer, len,
"UTF-8", encoding, NULL, &written, error);
if (! conv)
{
g_free (encoding);
return FALSE;
}
len = written;
}
g_free (encoding);
}
success = g_markup_parse_context_parse (parser->context,
conv ? conv : buffer, len, error);
if (conv)
g_free (conv);
return success;
}
/**
* gimp_xml_parser_free:
* @parser: a #GimpXmlParser
*
* Frees the resources allocated for @parser. You must not access
* @parser after calling this function.
**/
void
gimp_xml_parser_free (GimpXmlParser *parser)
{
g_return_if_fail (parser != NULL);
g_markup_parse_context_free (parser->context);
g_slice_free (GimpXmlParser, parser);
}
/* Try to determine encoding from XML header. This function returns
FALSE when it doesn't have enough text to parse. It returns TRUE
and sets encoding when the XML header has been parsed.
*/
static gboolean
parse_encoding (const gchar *text,
gint text_len,
gchar **encoding)
{
const gchar *start;
const gchar *end;
gint i;
g_return_val_if_fail (text, FALSE);
if (text_len < 20)
return FALSE;
start = g_strstr_len (text, text_len, "<?xml");
if (!start)
return FALSE;
end = g_strstr_len (start, text_len - (start - text), "?>");
if (!end)
return FALSE;
*encoding = NULL;
text_len = end - start;
if (text_len < 12)
return TRUE;
start = g_strstr_len (start + 1, text_len - 1, "encoding");
if (!start)
return TRUE;
start += 8;
while (start < end && *start == ' ')
start++;
if (*start != '=')
return TRUE;
start++;
while (start < end && *start == ' ')
start++;
if (*start != '\"' && *start != '\'')
return TRUE;
text_len = end - start;
if (text_len < 1)
return TRUE;
for (i = 1; i < text_len; i++)
if (start[i] == start[0])
break;
if (i == text_len || i < 3)
return TRUE;
*encoding = g_strndup (start + 1, i - 1);
return TRUE;
}
|