1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /** @file 3 * @brief XML quoting routines 4 *//* 5 * Authors: 6 * see git history 7 * Krzysztof Kosiński <tweenk.pl@gmail.com> 8 * 9 * Copyright (C) 2015 Authors 10 * Released under GNU GPL v2+, read the file 'COPYING' for more information. 11 */ 12 13 #include "xml/quote.h" 14 #include <cstring> 15 #include <glib.h> 16 17 /// Returns the length of the string after quoting the characters <code>"&<></code>. xml_quoted_strlen(char const * val)18size_t xml_quoted_strlen(char const *val) 19 { 20 if (!val) return 0; 21 size_t len = 0; 22 23 for (char const *valp = val; *valp; ++valp) { 24 switch (*valp) { 25 case '"': 26 len += 6; // " 27 break; 28 case '&': 29 len += 5; // & 30 break; 31 case '<': 32 case '>': 33 len += 4; // < or > 34 break; 35 default: 36 ++len; 37 break; 38 } 39 } 40 return len; 41 } 42 xml_quote_strdup(char const * src)43char *xml_quote_strdup(char const *src) 44 { 45 size_t len = xml_quoted_strlen(src); 46 char *result = static_cast<char*>(g_malloc(len + 1)); 47 char *resp = result; 48 49 for (char const *srcp = src; *srcp; ++srcp) { 50 switch(*srcp) { 51 case '"': 52 strcpy(resp, """); 53 resp += 6; 54 break; 55 case '&': 56 strcpy(resp, "&"); 57 resp += 5; 58 break; 59 case '<': 60 strcpy(resp, "<"); 61 resp += 4; 62 break; 63 case '>': 64 strcpy(resp, ">"); 65 resp += 4; 66 break; 67 default: 68 *resp++ = *srcp; 69 break; 70 } 71 } 72 *resp = 0; 73 return result; 74 } 75 76 // quote: ", &, <, > 77 78 79 /* 80 Local Variables: 81 mode:c++ 82 c-file-style:"stroustrup" 83 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) 84 indent-tabs-mode:nil 85 fill-column:99 86 End: 87 */ 88 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : 89