xml.c (2126B)
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/taler_util.h" 22 23 24 /** 25 * We allow [a-zA-Z0-9-.:] in extra_wire_subject_metadata. 26 * Test @a c for it. 27 * 28 * @param c character to test 29 * @return true if OK 30 */ 31 static inline bool 32 is_allowed_metachar (char c) 33 { 34 return (c >= 'a' && c <= 'z') || 35 (c >= 'A' && c <= 'Z') || 36 (c >= '0' && c <= '9') || 37 c == '-' || 38 c == '.' || 39 c == ':'; 40 } 41 42 43 bool 44 TALER_is_valid_subject_metadata_string (const char *src) 45 { 46 unsigned int len = 0; 47 if (NULL == src) 48 return true; 49 50 while (*src) 51 { 52 if (! is_allowed_metachar (*src++)) 53 return false; 54 if (++len > 40) 55 return false; 56 } 57 return true; 58 } 59 60 61 char * 62 TALER_escape_xml (const char *str) 63 { 64 struct GNUNET_Buffer out = { 0 }; 65 const char *p = str; 66 67 while (*p) 68 { 69 const char *esc = NULL; 70 71 switch (*p) 72 { 73 case '&': 74 esc = "&"; 75 break; 76 case '<': 77 esc = "<"; 78 break; 79 case '>': 80 esc = ">"; 81 break; 82 case '"': 83 esc = """; 84 break; 85 case '\'': 86 esc = "'"; 87 break; 88 } 89 if (NULL != esc) 90 GNUNET_buffer_write_str (&out, 91 esc); 92 else 93 GNUNET_buffer_write (&out, 94 p, 95 1); 96 p++; 97 } 98 return GNUNET_buffer_reap_str (&out); 99 }