Branch data Line data Source code
1 : : /********************************************************************\
2 : : * gnc-xml-helper.h -- api for xml helpers *
3 : : * *
4 : : * Copyright (C) 2001 James LewisMoss <dres@debian.org> *
5 : : * *
6 : : * This program is free software; you can redistribute it and/or *
7 : : * modify it under the terms of the GNU General Public License as *
8 : : * published by the Free Software Foundation; either version 2 of *
9 : : * the License, or (at your option) any later version. *
10 : : * *
11 : : * This program is distributed in the hope that it will be useful, *
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 : : * GNU General Public License for more details. *
15 : : * *
16 : : * You should have received a copy of the GNU General Public License*
17 : : * along with this program; if not, contact: *
18 : : * *
19 : : * Free Software Foundation Voice: +1-617-542-5942 *
20 : : * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21 : : * Boston, MA 02110-1301, USA gnu@gnu.org *
22 : : * *
23 : : \********************************************************************/
24 : : #include <glib.h>
25 : :
26 : : #include "gnc-xml-helper.h"
27 : :
28 : : xmlChar*
29 : 138024 : checked_char_cast (gchar* val)
30 : : {
31 : 138024 : const int length = -1; /* Assumes val is null-terminated */
32 : : gchar* end;
33 : 138024 : if (val == NULL) return NULL;
34 : : /* Replace any invalid UTF-8 characters with a sequence of '?' */
35 : 138024 : while (!g_utf8_validate (val, length, (const gchar**) (&end)))
36 : 0 : *end = '?';
37 : : /* Replace any invalid (for XML) control characters (everything < 0x20
38 : : * except \n, \t, and \r) with '?'. Technically we should replace
39 : : * these with a numeric entity, but that will blow up the libxml
40 : : * functions that expect raw text. It seems unlikely that anyone
41 : : * would use intentionally use one of these characters anyway.
42 : : */
43 : :
44 : 1380614 : for (end = val; *end; ++end)
45 : 1242590 : if (*end > 0 && *end < 0x20 && *end != 0x09 &&
46 : 63927 : *end != 0x0a && *end != 0x0d)
47 : 1 : *end = '?';
48 : 138024 : return (xmlChar*) (val);
49 : : }
|