Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2026 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU General Public License as published by the Free Software
7 : Foundation; either version 3, or (at your option) any later version.
8 :
9 : TALER is distributed in the hope that it will be useful, but WITHOUT ANY
10 : WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 : A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 :
13 : You should have received a copy of the GNU General Public License along with
14 : TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
15 : */
16 : /**
17 : * @file xml.c
18 : * @brief Common utility functions for XML handling
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/platform.h"
22 : #include "taler/taler_util.h"
23 :
24 :
25 : /**
26 : * We allow [a-zA-Z0-9-.:] in extra_wire_subject_metadata.
27 : * Test @a c for it.
28 : *
29 : * @param c character to test
30 : * @return true if OK
31 : */
32 : static inline bool
33 0 : is_allowed_metachar (char c)
34 : {
35 0 : return (c >= 'a' && c <= 'z') ||
36 0 : (c >= 'A' && c <= 'Z') ||
37 0 : (c >= '0' && c <= '9') ||
38 0 : c == '-' ||
39 0 : c == '.' ||
40 : c == ':';
41 : }
42 :
43 :
44 : bool
45 98 : TALER_is_valid_subject_metadata_string (const char *src)
46 : {
47 98 : unsigned int len = 0;
48 98 : if (NULL == src)
49 98 : return true;
50 :
51 0 : while (*src)
52 : {
53 0 : if (! is_allowed_metachar (*src++))
54 0 : return false;
55 0 : if (++len > 40)
56 0 : return false;
57 : }
58 0 : return true;
59 : }
60 :
61 :
62 : char *
63 0 : TALER_escape_xml (const char *str)
64 : {
65 0 : struct GNUNET_Buffer out = { 0 };
66 0 : const char *p = str;
67 :
68 0 : while (*p)
69 : {
70 0 : const char *esc = NULL;
71 :
72 0 : switch (*p)
73 : {
74 0 : case '&':
75 0 : esc = "&";
76 0 : break;
77 0 : case '<':
78 0 : esc = "<";
79 0 : break;
80 0 : case '>':
81 0 : esc = ">";
82 0 : break;
83 0 : case '"':
84 0 : esc = """;
85 0 : break;
86 0 : case '\'':
87 0 : esc = "'";
88 0 : break;
89 : }
90 0 : if (NULL != esc)
91 0 : GNUNET_buffer_write_str (&out,
92 : esc);
93 : else
94 0 : GNUNET_buffer_write (&out,
95 : p,
96 : 1);
97 0 : p++;
98 : }
99 0 : return GNUNET_buffer_reap_str (&out);
100 : }
|