1a45ae5f8SJohn Marino /* Shared helper routines for manipulating XML. 2a45ae5f8SJohn Marino 3*ef5ccd6cSJohn Marino Copyright (C) 2006-2013 Free Software Foundation, Inc. 4a45ae5f8SJohn Marino 5a45ae5f8SJohn Marino This file is part of GDB. 6a45ae5f8SJohn Marino 7a45ae5f8SJohn Marino This program is free software; you can redistribute it and/or modify 8a45ae5f8SJohn Marino it under the terms of the GNU General Public License as published by 9a45ae5f8SJohn Marino the Free Software Foundation; either version 3 of the License, or 10a45ae5f8SJohn Marino (at your option) any later version. 11a45ae5f8SJohn Marino 12a45ae5f8SJohn Marino This program is distributed in the hope that it will be useful, 13a45ae5f8SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of 14a45ae5f8SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15a45ae5f8SJohn Marino GNU General Public License for more details. 16a45ae5f8SJohn Marino 17a45ae5f8SJohn Marino You should have received a copy of the GNU General Public License 18a45ae5f8SJohn Marino along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19a45ae5f8SJohn Marino 20a45ae5f8SJohn Marino #ifdef GDBSERVER 21a45ae5f8SJohn Marino #include "server.h" 22a45ae5f8SJohn Marino #else 23a45ae5f8SJohn Marino #include "defs.h" 24a45ae5f8SJohn Marino #endif 25a45ae5f8SJohn Marino 26a45ae5f8SJohn Marino #include "xml-utils.h" 27a45ae5f8SJohn Marino 28a45ae5f8SJohn Marino #include <string.h> 29a45ae5f8SJohn Marino 30a45ae5f8SJohn Marino /* Return a malloc allocated string with special characters from TEXT 31a45ae5f8SJohn Marino replaced by entity references. */ 32a45ae5f8SJohn Marino 33a45ae5f8SJohn Marino char * 34a45ae5f8SJohn Marino xml_escape_text (const char *text) 35a45ae5f8SJohn Marino { 36a45ae5f8SJohn Marino char *result; 37a45ae5f8SJohn Marino int i, special; 38a45ae5f8SJohn Marino 39a45ae5f8SJohn Marino /* Compute the length of the result. */ 40a45ae5f8SJohn Marino for (i = 0, special = 0; text[i] != '\0'; i++) 41a45ae5f8SJohn Marino switch (text[i]) 42a45ae5f8SJohn Marino { 43a45ae5f8SJohn Marino case '\'': 44a45ae5f8SJohn Marino case '\"': 45a45ae5f8SJohn Marino special += 5; 46a45ae5f8SJohn Marino break; 47a45ae5f8SJohn Marino case '&': 48a45ae5f8SJohn Marino special += 4; 49a45ae5f8SJohn Marino break; 50a45ae5f8SJohn Marino case '<': 51a45ae5f8SJohn Marino case '>': 52a45ae5f8SJohn Marino special += 3; 53a45ae5f8SJohn Marino break; 54a45ae5f8SJohn Marino default: 55a45ae5f8SJohn Marino break; 56a45ae5f8SJohn Marino } 57a45ae5f8SJohn Marino 58a45ae5f8SJohn Marino /* Expand the result. */ 59a45ae5f8SJohn Marino result = xmalloc (i + special + 1); 60a45ae5f8SJohn Marino for (i = 0, special = 0; text[i] != '\0'; i++) 61a45ae5f8SJohn Marino switch (text[i]) 62a45ae5f8SJohn Marino { 63a45ae5f8SJohn Marino case '\'': 64a45ae5f8SJohn Marino strcpy (result + i + special, "'"); 65a45ae5f8SJohn Marino special += 5; 66a45ae5f8SJohn Marino break; 67a45ae5f8SJohn Marino case '\"': 68a45ae5f8SJohn Marino strcpy (result + i + special, """); 69a45ae5f8SJohn Marino special += 5; 70a45ae5f8SJohn Marino break; 71a45ae5f8SJohn Marino case '&': 72a45ae5f8SJohn Marino strcpy (result + i + special, "&"); 73a45ae5f8SJohn Marino special += 4; 74a45ae5f8SJohn Marino break; 75a45ae5f8SJohn Marino case '<': 76a45ae5f8SJohn Marino strcpy (result + i + special, "<"); 77a45ae5f8SJohn Marino special += 3; 78a45ae5f8SJohn Marino break; 79a45ae5f8SJohn Marino case '>': 80a45ae5f8SJohn Marino strcpy (result + i + special, ">"); 81a45ae5f8SJohn Marino special += 3; 82a45ae5f8SJohn Marino break; 83a45ae5f8SJohn Marino default: 84a45ae5f8SJohn Marino result[i + special] = text[i]; 85a45ae5f8SJohn Marino break; 86a45ae5f8SJohn Marino } 87a45ae5f8SJohn Marino result[i + special] = '\0'; 88a45ae5f8SJohn Marino 89a45ae5f8SJohn Marino return result; 90a45ae5f8SJohn Marino } 91