xml.c (2154B)
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 is_allowed_metachar (char c) 34 { 35 return (c >= 'a' && c <= 'z') || 36 (c >= 'A' && c <= 'Z') || 37 (c >= '0' && c <= '9') || 38 c == '-' || 39 c == '.' || 40 c == ':'; 41 } 42 43 44 bool 45 TALER_is_valid_subject_metadata_string (const char *src) 46 { 47 unsigned int len = 0; 48 if (NULL == src) 49 return true; 50 51 while (*src) 52 { 53 if (! is_allowed_metachar (*src++)) 54 return false; 55 if (++len > 40) 56 return false; 57 } 58 return true; 59 } 60 61 62 char * 63 TALER_escape_xml (const char *str) 64 { 65 struct GNUNET_Buffer out = { 0 }; 66 const char *p = str; 67 68 while (*p) 69 { 70 const char *esc = NULL; 71 72 switch (*p) 73 { 74 case '&': 75 esc = "&"; 76 break; 77 case '<': 78 esc = "<"; 79 break; 80 case '>': 81 esc = ">"; 82 break; 83 case '"': 84 esc = """; 85 break; 86 case '\'': 87 esc = "'"; 88 break; 89 } 90 if (NULL != esc) 91 GNUNET_buffer_write_str (&out, 92 esc); 93 else 94 GNUNET_buffer_write (&out, 95 p, 96 1); 97 p++; 98 } 99 return GNUNET_buffer_reap_str (&out); 100 }