1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
/* Copyright (C) 2001-2023 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied,
modified or distributed except as expressly authorized under the terms
of the license contained in the file LICENSE in this distribution.
Refer to licensing information at http://www.artifex.com or contact
Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
CA 94129, USA, for further information.
*/
/* Simple XML document object model on top of Expat. */
#include "ghostxps.h"
#include <expat.h>
#define XMLBUFLEN 4096
#define NS_XPS "http://schemas.microsoft.com/xps/2005/06"
#define NS_MC "http://schemas.openxmlformats.org/markup-compatibility/2006"
#define NS_OXPS "http://schemas.openxps.org/oxps/v1.0"
typedef struct xps_parser_s xps_parser_t;
struct xps_parser_s
{
xps_context_t *ctx;
xps_item_t *root;
xps_item_t *head;
const char *error;
char *base; /* base of relative URIs */
};
struct xps_item_s
{
char *name;
char **atts;
xps_item_t *up;
xps_item_t *down;
xps_item_t *tail;
xps_item_t *next;
};
static const char *
skip_namespace(const char *s)
{
const char *p = strchr(s, ' ');
if (p)
return p + 1;
return s;
}
static void
on_open_tag(void *zp, const char *ns_name, const char **atts)
{
xps_parser_t *parser = zp;
xps_context_t *ctx = parser->ctx;
xps_item_t *item;
xps_item_t *tail;
int namelen;
int attslen;
int textlen;
const char *name;
char *p;
int i;
if (parser->error)
return;
/* check namespace */
name = NULL;
p = strstr(ns_name, NS_XPS);
if (p == ns_name)
{
name = strchr(ns_name, ' ') + 1;
}
p = strstr(ns_name, NS_MC);
if (p == ns_name)
{
name = strchr(ns_name, ' ') + 1;
}
p = strstr(ns_name, NS_OXPS);
if (p == ns_name)
{
name = strchr(ns_name, ' ') + 1;
}
if (!name)
{
dmprintf1(ctx->memory, "unknown namespace: %s\n", ns_name);
name = ns_name;
}
/* count size to alloc */
namelen = strlen(name) + 1; /* zero terminated */
attslen = sizeof(char*); /* with space for sentinel */
textlen = 0;
for (i = 0; atts[i]; i++)
{
attslen += sizeof(char*);
if ((i & 1) == 0)
textlen += strlen(skip_namespace(atts[i])) + 1;
else
textlen += strlen(atts[i]) + 1;
}
item = xps_alloc(ctx, sizeof(xps_item_t) + attslen + namelen + textlen);
if (!item) {
parser->error = "out of memory";
gs_throw(gs_error_VMerror, "out of memory.\n");
return;
}
/* copy strings to new memory */
item->atts = (char**) (((char*)item) + sizeof(xps_item_t));
item->name = ((char*)item) + sizeof(xps_item_t) + attslen;
p = ((char*)item) + sizeof(xps_item_t) + attslen + namelen;
strcpy(item->name, name);
for (i = 0; atts[i]; i++)
{
item->atts[i] = p;
if ((i & 1) == 0)
strcpy(item->atts[i], skip_namespace(atts[i]));
else
strcpy(item->atts[i], atts[i]);
p += strlen(p) + 1;
}
item->atts[i] = 0;
/* link item into tree */
item->up = parser->head;
item->down = NULL;
item->next = NULL;
if (!parser->head)
{
parser->root = item;
parser->head = item;
return;
}
if (!parser->head->down)
{
parser->head->down = item;
parser->head->tail = item;
parser->head = item;
return;
}
tail = parser->head->tail;
tail->next = item;
parser->head->tail = item;
parser->head = item;
}
static void
on_close_tag(void *zp, const char *name)
{
xps_parser_t *parser = zp;
if (parser->error)
return;
if (parser->head)
parser->head = parser->head->up;
}
static inline int
is_xml_space(int c)
{
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
}
static void
on_text(void *zp, char *buf, int len)
{
xps_parser_t *parser = zp;
xps_context_t *ctx = parser->ctx;
const char *atts[3];
int i;
if (parser->error)
return;
for (i = 0; i < len; i++)
{
if (!is_xml_space(buf[i]))
{
char *tmp = xps_alloc(ctx, len + 1);
if (!tmp)
{
parser->error = "out of memory";
gs_throw(gs_error_VMerror, "out of memory.\n");
return;
}
atts[0] = "";
atts[1] = tmp;
atts[2] = NULL;
memcpy(tmp, buf, len);
tmp[len] = 0;
on_open_tag(zp, "", atts);
on_close_tag(zp, "");
xps_free(ctx, tmp);
return;
}
}
}
xps_item_t *
xps_parse_xml(xps_context_t *ctx, byte *buf, int len)
{
xps_parser_t parser;
XML_Parser xp;
int code;
parser.ctx = ctx;
parser.root = NULL;
parser.head = NULL;
parser.error = NULL;
xp = XML_ParserCreateNS(NULL, ' ');
if (!xp)
{
gs_throw(-1, "xml error: could not create expat parser");
return NULL;
}
XML_SetUserData(xp, &parser);
XML_SetParamEntityParsing(xp, XML_PARAM_ENTITY_PARSING_NEVER);
XML_SetStartElementHandler(xp, (XML_StartElementHandler)on_open_tag);
XML_SetEndElementHandler(xp, (XML_EndElementHandler)on_close_tag);
XML_SetCharacterDataHandler(xp, (XML_CharacterDataHandler)on_text);
code = XML_Parse(xp, (char*)buf, len, 1);
if (code == 0 || parser.error != NULL)
{
if (parser.root)
xps_free_item(ctx, parser.root);
if (XML_ErrorString(XML_GetErrorCode(xp)) != 0)
emprintf1(parser.ctx->memory, "XML_Error: %s\n", XML_ErrorString(XML_GetErrorCode(xp)));
XML_ParserFree(xp);
gs_throw1(-1, "parser error: %s", parser.error);
return NULL;
}
XML_ParserFree(xp);
return parser.root;
}
xps_item_t *
xps_next(xps_item_t *item)
{
return item->next;
}
xps_item_t *
xps_down(xps_item_t *item)
{
return item->down;
}
char *
xps_tag(xps_item_t *item)
{
return item->name;
}
char *
xps_att(xps_item_t *item, const char *att)
{
int i;
for (i = 0; item->atts[i]; i += 2)
if (!strcmp(item->atts[i], att))
return item->atts[i + 1];
return NULL;
}
void
xps_detach_and_free_remainder(xps_context_t *ctx, xps_item_t *root, xps_item_t *item)
{
if (item->up)
item->up->down = NULL;
xps_free_item(ctx, item->next);
item->next = NULL;
xps_free_item(ctx, root);
}
void
xps_free_item(xps_context_t *ctx, xps_item_t *item)
{
xps_item_t *next;
while (item)
{
next = item->next;
if (item->down)
xps_free_item(ctx, item->down);
xps_free(ctx, item);
item = next;
}
}
static void indent(int n)
{
while (n--)
dlprintf(" ");
}
static void
xps_debug_item_imp(xps_item_t *item, int level, int loop)
{
int i;
while (item)
{
indent(level);
if (strlen(item->name) == 0)
dlprintf1("%s\n", item->atts[1]);
else
{
dlprintf1("<%s", item->name);
for (i = 0; item->atts[i]; i += 2)
dlprintf2(" %s=\"%s\"", item->atts[i], item->atts[i+1]);
if (item->down)
{
dlprintf(">\n");
xps_debug_item_imp(item->down, level + 1, 1);
indent(level);
dlprintf1("</%s>\n", item->name);
}
else
dlprintf(" />\n");
}
item = item->next;
if (!loop)
return;
}
}
void
xps_debug_item(xps_item_t *item, int level)
{
xps_debug_item_imp(item, level, 0);
}
|