| 1 |
/* $Id: xml_escape.c,v 1.11 2004/11/21 23:40:40 mgrouch Exp $ */ |
| 2 |
|
| 3 |
/* |
| 4 |
|
| 5 |
XMLStarlet: Command Line Toolkit to query/edit/check/transform XML documents |
| 6 |
|
| 7 |
Copyright (c) 2002-2004 Mikhail Grushinskiy. All Rights Reserved. |
| 8 |
|
| 9 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 |
of this software and associated documentation files (the "Software"), to deal |
| 11 |
in the Software without restriction, including without limitation the rights |
| 12 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 |
copies of the Software, and to permit persons to whom the Software is |
| 14 |
furnished to do so, subject to the following conditions: |
| 15 |
|
| 16 |
The above copyright notice and this permission notice shall be included in |
| 17 |
all copies or substantial portions of the Software. |
| 18 |
|
| 19 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 |
THE SOFTWARE. |
| 26 |
|
| 27 |
*/ |
| 28 |
|
| 29 |
#include <config.h> |
| 30 |
|
| 31 |
#include <string.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <ctype.h> |
| 35 |
|
| 36 |
#include <libxml/xmlmemory.h> |
| 37 |
#include <libxml/debugXML.h> |
| 38 |
#include <libxml/xmlIO.h> |
| 39 |
#include <libxml/parserInternals.h> |
| 40 |
|
| 41 |
#include "xmlstar.h" |
| 42 |
#include "escape.h" |
| 43 |
|
| 44 |
#define INSZ 4*1024 |
| 45 |
|
| 46 |
/** |
| 47 |
* Print small help for command line options |
| 48 |
*/ |
| 49 |
void |
| 50 |
escUsage(int argc, char **argv, int escape, exit_status status) |
| 51 |
{ |
| 52 |
extern void fprint_escape_usage(FILE* o, const char* argv0); |
| 53 |
extern void fprint_unescape_usage(FILE* o, const char* argv0); |
| 54 |
extern const char more_info[]; |
| 55 |
FILE *o = (status == EXIT_SUCCESS)? stdout : stderr; |
| 56 |
if (escape) fprint_escape_usage(o, argv[0]); |
| 57 |
else fprint_unescape_usage(o, argv[0]); |
| 58 |
fprintf(o, "%s", more_info); |
| 59 |
exit(status); |
| 60 |
} |
| 61 |
|
| 62 |
/* "apos" or "quot" are biggest, add 1 for leading "&" */ |
| 63 |
enum { MAX_ENTITY_NAME = 1+4 }; |
| 64 |
|
| 65 |
/* return 1 if entity was recognized and value output, 0 otherwise */ |
| 66 |
static int |
| 67 |
put_entity_value(const char* entname, FILE* out) |
| 68 |
{ |
| 69 |
if (entname[1] == '#') { |
| 70 |
char* endptr; |
| 71 |
const char* entnul = entname + strlen(entname); |
| 72 |
int num = (entname[2] == 'x')? |
| 73 |
strtol(&entname[3], &endptr, 16): |
| 74 |
strtol(&entname[2], &endptr, 10); |
| 75 |
if (endptr == entnul) { |
| 76 |
putc(num, out); |
| 77 |
return 1; |
| 78 |
} |
| 79 |
} else { |
| 80 |
xmlEntityPtr entity = xmlGetPredefinedEntity((xmlChar*) &entname[1]); |
| 81 |
if (entity) { |
| 82 |
fputs((char*) (entity->content), out); |
| 83 |
return 1; |
| 84 |
} |
| 85 |
} |
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
/* |
| 90 |
* Macro used to grow the current buffer. |
| 91 |
*/ |
| 92 |
#define grow_BufferReentrant() { \ |
| 93 |
buffer_size *= 2; \ |
| 94 |
buffer = (xmlChar *) \ |
| 95 |
xmlRealloc(buffer, buffer_size * sizeof(xmlChar)); \ |
| 96 |
if (buffer == NULL) { \ |
| •97 |
fprintf(stderr, "%s", "growing buffer error"); \ |
| 98 |
abort(); \ |
| 99 |
} \ |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* xml_C11NNormalizeString: |
| 104 |
* @input: the input string |
| 105 |
* @mode: the normalization mode (attribute, comment, PI or text) |
| 106 |
* |
| 107 |
* Converts a string to a canonical (normalized) format. The code is stolen |
| 108 |
* from xmlEncodeEntitiesReentrant(). Added normalization of \x09, \x0a, \x0A |
| 109 |
* and the @mode parameter |
| 110 |
* |
| 111 |
* Returns a normalized string (caller is responsible for calling xmlFree()) |
| 112 |
* or NULL if an error occurs |
| 113 |
*/ |
| 114 |
xmlChar * |
| 115 |
xml_C11NNormalizeString(const xmlChar * input, |
| 116 |
xml_C14NNormalizationMode mode) |
| 117 |
{ |
| 118 |
const xmlChar *cur = input; |
| 119 |
xmlChar *buffer = NULL; |
| 120 |
xmlChar *out = NULL; |
| 121 |
int buffer_size = 0; |
| 122 |
|
| 123 |
if (input == NULL) |
| 124 |
return (NULL); |
| 125 |
|
| 126 |
/* |
| 127 |
* allocate an translation buffer. |
| 128 |
*/ |
| 129 |
buffer_size = 1000; |
| 130 |
buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar)); |
| 131 |
if (buffer == NULL) { |
| •132 |
fprintf(stderr, "%s", "allocating buffer error"); |
| 133 |
abort(); |
| 134 |
} |
| 135 |
out = buffer; |
| 136 |
|
| 137 |
while (*cur != '\0') { |
| 138 |
if ((out - buffer) > (buffer_size - 10)) { |
| 139 |
int indx = out - buffer; |
| 140 |
|
| 141 |
grow_BufferReentrant(); |
| 142 |
out = &buffer[indx]; |
| 143 |
} |
| 144 |
|
| 145 |
if ((*cur == '<') && ((mode == XML_C14N_NORMALIZE_ATTR) || |
| 146 |
(mode == XML_C14N_NORMALIZE_TEXT))) { |
| 147 |
*out++ = '&'; |
| 148 |
*out++ = 'l'; |
| 149 |
*out++ = 't'; |
| 150 |
*out++ = ';'; |
| 151 |
} else if ((*cur == '>') && (mode == XML_C14N_NORMALIZE_TEXT)) { |
| 152 |
*out++ = '&'; |
| 153 |
*out++ = 'g'; |
| 154 |
*out++ = 't'; |
| 155 |
*out++ = ';'; |
| 156 |
} else if ((*cur == '&') && ((mode == XML_C14N_NORMALIZE_ATTR) || |
| 157 |
(mode == XML_C14N_NORMALIZE_TEXT))) { |
| 158 |
*out++ = '&'; |
| 159 |
*out++ = 'a'; |
| 160 |
*out++ = 'm'; |
| 161 |
*out++ = 'p'; |
| 162 |
*out++ = ';'; |
| 163 |
} else if ((*cur == '"') && (mode == XML_C14N_NORMALIZE_ATTR)) { |
| 164 |
*out++ = '&'; |
| 165 |
*out++ = 'q'; |
| 166 |
*out++ = 'u'; |
| 167 |
*out++ = 'o'; |
| 168 |
*out++ = 't'; |
| 169 |
*out++ = ';'; |
| 170 |
} else if ((*cur == '\x09') && (mode == XML_C14N_NORMALIZE_ATTR)) { |
| 171 |
*out++ = '&'; |
| 172 |
*out++ = '#'; |
| 173 |
*out++ = 'x'; |
| 174 |
*out++ = '9'; |
| 175 |
*out++ = ';'; |
| 176 |
} else if ((*cur == '\x0A') && (mode == XML_C14N_NORMALIZE_ATTR)) { |
| 177 |
*out++ = '&'; |
| 178 |
*out++ = '#'; |
| 179 |
*out++ = 'x'; |
| 180 |
*out++ = 'A'; |
| 181 |
*out++ = ';'; |
| 182 |
} else if ((*cur == '\x0D') && ((mode == XML_C14N_NORMALIZE_ATTR) || |
| 183 |
(mode == XML_C14N_NORMALIZE_TEXT) || |
| 184 |
(mode == XML_C14N_NORMALIZE_COMMENT) || |
| 185 |
(mode == XML_C14N_NORMALIZE_PI))) { |
| 186 |
*out++ = '&'; |
| 187 |
*out++ = '#'; |
| 188 |
*out++ = 'x'; |
| 189 |
*out++ = 'D'; |
| 190 |
*out++ = ';'; |
| 191 |
} else { |
| 192 |
/* |
| 193 |
* Works because on UTF-8, all extended sequences cannot |
| 194 |
* result in bytes in the ASCII range. |
| 195 |
*/ |
| 196 |
*out++ = *cur; |
| 197 |
} |
| 198 |
cur++; |
| 199 |
} |
| 200 |
*out++ = 0; |
| 201 |
return (buffer); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* write unescaped version of @str to @out, if @str appears to end in the middle |
| 206 |
* of entity, return the entity. |
| 207 |
*/ |
| 208 |
static const char * |
| 209 |
xml_unescape(const char* str, FILE* out) |
| 210 |
{ |
| 211 |
static char entity[MAX_ENTITY_NAME+1]; /* +1 for NUL terminator */ |
| 212 |
int i; |
| 213 |
|
| 214 |
for (i = 0; str[i]; i++) { |
| 215 |
if (str[i] == '&') { |
| 216 |
int entity_len; |
| 217 |
int semicolon_off = i+1; |
| 218 |
for (;;) { |
| 219 |
if (str[semicolon_off] == ';' || |
| 220 |
str[semicolon_off] == '\0' || |
| 221 |
isspace(str[semicolon_off])) break; |
| 222 |
semicolon_off++; |
| 223 |
} |
| 224 |
entity_len = semicolon_off - i; |
| •225 |
if (entity_len <= MAX_ENTITY_NAME) { |
| 226 |
memcpy(entity, &str[i], entity_len); |
| 227 |
entity[entity_len] = '\0'; |
| 228 |
if (str[semicolon_off] == ';') { |
| 229 |
if (put_entity_value(entity, out)) { |
| 230 |
i = semicolon_off; |
| 231 |
continue; |
| 232 |
} |
| 233 |
if (!globalOptions.quiet) |
| 234 |
fprintf(stderr, "unrecognized entity: %s\n", entity); |
| 235 |
} else if (str[semicolon_off] == '\0') { |
| 236 |
return entity; |
| 237 |
} else { |
| 238 |
if (!globalOptions.quiet) |
| 239 |
fprintf(stderr, "unterminated entity name: %.*s\n", entity_len, &str[i]); |
| 240 |
} |
| 241 |
} else { |
| 242 |
if (!globalOptions.quiet) |
| 243 |
fprintf(stderr, "entity name too long: %.*s\n", entity_len, &str[i]); |
| 244 |
} |
| 245 |
} |
| 246 |
putc(str[i], out); |
| 247 |
} |
| 248 |
return NULL; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* This is the main function for 'escape/unescape' options |
| 253 |
*/ |
| 254 |
int |
| 255 |
escMain(int argc, char **argv, int escape) |
| 256 |
{ |
| 257 |
int ret = 0; |
| 258 |
int readStdIn = 0; |
| 259 |
|
| 260 |
char* inp = NULL; |
| 261 |
xmlChar* outBuf = NULL; |
| 262 |
|
| 263 |
if (argc < 2) escUsage(argc, argv, escape, EXIT_BAD_ARGS); |
| 264 |
|
| 265 |
inp = argv[2]; |
| 266 |
|
| 267 |
if (argc > 2) |
| 268 |
{ |
| 269 |
if (!strcmp(argv[2], "--help") || !strcmp(argv[2], "-h") || |
| 270 |
!strcmp(argv[2], "-?") || !strcmp(argv[2], "-Z")) |
| 271 |
escUsage(argc, argv, escape, EXIT_SUCCESS); |
| 272 |
if (!strcmp(argv[2], "-")) readStdIn = 1; |
| 273 |
} |
| 274 |
else |
| 275 |
{ |
| 276 |
readStdIn = 1; |
| 277 |
} |
| 278 |
|
| 279 |
if (readStdIn) |
| 280 |
{ |
| 281 |
static char line[INSZ]; |
| 282 |
int offset = 0; |
| 283 |
|
| 284 |
while (!feof(stdin)) |
| 285 |
{ |
| 286 |
if (fgets(line + offset, INSZ - offset, stdin)) |
| 287 |
{ |
| 288 |
if (escape) |
| 289 |
{ |
| 290 |
outBuf = xmlEncodeEntitiesReentrant(NULL, (xmlChar*) line); |
| 291 |
if (outBuf) |
| 292 |
{ |
| 293 |
fprintf(stdout, "%s", outBuf); |
| 294 |
xmlFree(outBuf); |
| 295 |
} |
| 296 |
} |
| 297 |
else |
| 298 |
{ |
| 299 |
const char* partial_entity = xml_unescape(line, stdout); |
| 300 |
if (partial_entity) { |
| 301 |
offset = strlen(partial_entity); |
| 302 |
memcpy(line, partial_entity, offset); |
| 303 |
} else { |
| 304 |
offset = 0; |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
if (offset) { |
| 310 |
fprintf(stdout, "%.*s", offset, line); |
| 311 |
if (!globalOptions.quiet) |
| 312 |
fprintf(stderr, "partial entity: %.*s\n", offset, line); |
| 313 |
} |
| 314 |
return ret; |
| 315 |
} |
| 316 |
|
| 317 |
if (escape) |
| 318 |
{ |
| 319 |
outBuf = xmlEncodeEntitiesReentrant(NULL, (xmlChar*) inp); |
| 320 |
if (outBuf) |
| 321 |
{ |
| 322 |
fprintf(stdout, "%s\n", outBuf); |
| 323 |
xmlFree(outBuf); |
| 324 |
} |
| 325 |
} |
| 326 |
else |
| 327 |
{ |
| 328 |
const char* partial_entity = xml_unescape(inp, stdout); |
| 329 |
if (partial_entity) |
| 330 |
{ |
| 331 |
fprintf(stdout, "%s\n", partial_entity); |
| 332 |
if (!globalOptions.quiet) |
| 333 |
fprintf(stderr, "partial entity: %s\n", partial_entity); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
return ret; |
| 338 |
} |