Coverage Report

Created: 2026-01-10 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxml2/catalog.c
Line
Count
Source
1
/**
2
 * catalog.c: set of generic Catalog related routines
3
 *
4
 * Reference:  SGML Open Technical Resolution TR9401:1997.
5
 *             http://www.jclark.com/sp/catalog.htm
6
 *
7
 *             XML Catalogs Working Draft 06 August 2001
8
 *             http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
9
 *
10
 * See Copyright for the status of this software.
11
 *
12
 * Author: Daniel Veillard
13
 */
14
15
#define IN_LIBXML
16
#include "libxml.h"
17
18
#ifdef LIBXML_CATALOG_ENABLED
19
#include <stdio.h>
20
#include <stdlib.h>
21
#include <string.h>
22
23
#include <fcntl.h>
24
#include <sys/stat.h>
25
26
#ifdef _WIN32
27
  #include <io.h>
28
#else
29
  #include <unistd.h>
30
#endif
31
32
#include <libxml/xmlmemory.h>
33
#include <libxml/hash.h>
34
#include <libxml/uri.h>
35
#include <libxml/parserInternals.h>
36
#include <libxml/catalog.h>
37
#include <libxml/xmlerror.h>
38
#include <libxml/threads.h>
39
40
#include "private/cata.h"
41
#include "private/buf.h"
42
#include "private/error.h"
43
#include "private/memory.h"
44
#include "private/threads.h"
45
46
0
#define MAX_DELEGATE  50
47
0
#define MAX_CATAL_DEPTH 50
48
49
#ifdef _WIN32
50
# define PATH_SEPARATOR ';'
51
#else
52
0
# define PATH_SEPARATOR ':'
53
#endif
54
55
0
#define XML_URN_PUBID "urn:publicid:"
56
0
#define XML_CATAL_BREAK ((xmlChar *) -1)
57
#ifndef XML_XML_DEFAULT_CATALOG
58
2
#define XML_XML_DEFAULT_CATALOG "file://" XML_SYSCONFDIR "/xml/catalog"
59
#endif
60
61
#ifdef LIBXML_SGML_CATALOG_ENABLED
62
#ifndef XML_SGML_DEFAULT_CATALOG
63
#define XML_SGML_DEFAULT_CATALOG "file://" XML_SYSCONFDIR "/sgml/catalog"
64
#endif
65
#endif
66
67
static xmlChar *xmlCatalogNormalizePublic(const xmlChar *pubID);
68
static int xmlExpandCatalog(xmlCatalogPtr catal, const char *filename, int depth);
69
70
/************************************************************************
71
 *                  *
72
 *      Types, all private        *
73
 *                  *
74
 ************************************************************************/
75
76
typedef enum {
77
    XML_CATA_REMOVED = -1,
78
    XML_CATA_NONE = 0,
79
    XML_CATA_CATALOG,
80
    XML_CATA_BROKEN_CATALOG,
81
    XML_CATA_NEXT_CATALOG,
82
    XML_CATA_GROUP,
83
    XML_CATA_PUBLIC,
84
    XML_CATA_SYSTEM,
85
    XML_CATA_REWRITE_SYSTEM,
86
    XML_CATA_DELEGATE_PUBLIC,
87
    XML_CATA_DELEGATE_SYSTEM,
88
    XML_CATA_URI,
89
    XML_CATA_REWRITE_URI,
90
    XML_CATA_DELEGATE_URI
91
#ifdef LIBXML_SGML_CATALOG_ENABLED
92
    ,
93
    SGML_CATA_SYSTEM,
94
    SGML_CATA_PUBLIC,
95
    SGML_CATA_ENTITY,
96
    SGML_CATA_PENTITY,
97
    SGML_CATA_DOCTYPE,
98
    SGML_CATA_LINKTYPE,
99
    SGML_CATA_NOTATION,
100
    SGML_CATA_DELEGATE,
101
    SGML_CATA_BASE,
102
    SGML_CATA_CATALOG,
103
    SGML_CATA_DOCUMENT,
104
    SGML_CATA_SGMLDECL
105
#endif
106
} xmlCatalogEntryType;
107
108
typedef struct _xmlCatalogEntry xmlCatalogEntry;
109
typedef xmlCatalogEntry *xmlCatalogEntryPtr;
110
struct _xmlCatalogEntry {
111
    struct _xmlCatalogEntry *next;
112
    struct _xmlCatalogEntry *parent;
113
    struct _xmlCatalogEntry *children;
114
    xmlCatalogEntryType type;
115
    xmlChar *name;
116
    xmlChar *value;
117
    xmlChar *URL;  /* The expanded URL using the base */
118
    xmlCatalogPrefer prefer;
119
    int dealloc;
120
    int depth;
121
    struct _xmlCatalogEntry *group;
122
};
123
124
typedef enum {
125
    XML_XML_CATALOG_TYPE = 1
126
#ifdef LIBXML_SGML_CATALOG_ENABLED
127
    ,
128
    XML_SGML_CATALOG_TYPE
129
#endif
130
} xmlCatalogType;
131
132
#ifdef LIBXML_SGML_CATALOG_ENABLED
133
2
#define XML_MAX_SGML_CATA_DEPTH 10
134
#endif
135
struct _xmlCatalog {
136
    xmlCatalogType type;  /* either XML or SGML */
137
138
#ifdef LIBXML_SGML_CATALOG_ENABLED
139
    /*
140
     * SGML Catalogs are stored as a simple hash table of catalog entries
141
     * Catalog stack to check against overflows when building the
142
     * SGML catalog
143
     */
144
    char *catalTab[XML_MAX_SGML_CATA_DEPTH];  /* stack of catals */
145
    int          catalNr; /* Number of current catal streams */
146
    int          catalMax;  /* Max number of catal streams */
147
    xmlHashTablePtr sgml;
148
#endif
149
150
    /*
151
     * XML Catalogs are stored as a tree of Catalog entries
152
     */
153
    xmlCatalogPrefer prefer;
154
    xmlCatalogEntryPtr xml;
155
};
156
157
/************************************************************************
158
 *                  *
159
 *      Global variables        *
160
 *                  *
161
 ************************************************************************/
162
163
/*
164
 * Those are preferences
165
 */
166
static int xmlDebugCatalogs = 0;   /* used for debugging */
167
static xmlCatalogAllow xmlCatalogDefaultAllow = XML_CATA_ALLOW_ALL;
168
static xmlCatalogPrefer xmlCatalogDefaultPrefer = XML_CATA_PREFER_PUBLIC;
169
170
/*
171
 * Hash table containing all the trees of XML catalogs parsed by
172
 * the application.
173
 */
174
static xmlHashTablePtr xmlCatalogXMLFiles = NULL;
175
176
/*
177
 * The default catalog in use by the application
178
 */
179
static xmlCatalogPtr xmlDefaultCatalog = NULL;
180
181
/*
182
 * A mutex for modifying the shared global catalog(s)
183
 * xmlDefaultCatalog tree.
184
 * It also protects xmlCatalogXMLFiles
185
 * The core of this readers/writer scheme is in #xmlFetchXMLCatalogFile
186
 */
187
static xmlRMutex xmlCatalogMutex;
188
189
/*
190
 * Whether the default system catalog was initialized.
191
 */
192
static int xmlCatalogInitialized = 0;
193
194
/************************************************************************
195
 *                  *
196
 *      Catalog error handlers        *
197
 *                  *
198
 ************************************************************************/
199
200
/**
201
 * Handle an out of memory condition
202
 */
203
static void
204
xmlCatalogErrMemory(void)
205
0
{
206
0
    xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_CATALOG, NULL);
207
0
}
208
209
/**
210
 * Handle a catalog error
211
 *
212
 * @param catal  the Catalog entry
213
 * @param node  the context node
214
 * @param error  the error code
215
 * @param msg  the error message
216
 * @param str1  error string 1
217
 * @param str2  error string 2
218
 * @param str3  error string 3
219
 */
220
static void LIBXML_ATTR_FORMAT(4,0)
221
xmlCatalogErr(xmlCatalogEntryPtr catal, xmlNodePtr node, int error,
222
               const char *msg, const xmlChar *str1, const xmlChar *str2,
223
         const xmlChar *str3)
224
0
{
225
0
    int res;
226
227
0
    res = xmlRaiseError(NULL, NULL, NULL, catal, node,
228
0
                        XML_FROM_CATALOG, error, XML_ERR_ERROR, NULL, 0,
229
0
                        (const char *) str1, (const char *) str2,
230
0
                        (const char *) str3, 0, 0,
231
0
                        msg, str1, str2, str3);
232
0
    if (res < 0)
233
0
        xmlCatalogErrMemory();
234
0
}
235
236
static void
237
0
xmlCatalogPrintDebug(const char *fmt, ...) {
238
0
    va_list ap;
239
240
0
    va_start(ap, fmt);
241
0
    xmlVPrintErrorMessage(fmt, ap);
242
0
    va_end(ap);
243
0
}
244
245
/************************************************************************
246
 *                  *
247
 *      Allocation and Freeing        *
248
 *                  *
249
 ************************************************************************/
250
251
/**
252
 * create a new Catalog entry, this type is shared both by XML and
253
 * SGML catalogs, but the acceptable types values differs.
254
 *
255
 * @param type  type of entry
256
 * @param name  name of the entry
257
 * @param value  value of the entry
258
 * @param URL  URL of the entry
259
 * @param prefer  the PUBLIC vs. SYSTEM current preference value
260
 * @param group  for members of a group, the group entry
261
 * @returns the xmlCatalogEntry or NULL in case of error
262
 */
263
static xmlCatalogEntryPtr
264
xmlNewCatalogEntry(xmlCatalogEntryType type, const xmlChar *name,
265
     const xmlChar *value, const xmlChar *URL, xmlCatalogPrefer prefer,
266
2
     xmlCatalogEntryPtr group) {
267
2
    xmlCatalogEntryPtr ret;
268
2
    xmlChar *normid = NULL;
269
270
2
    ret = (xmlCatalogEntryPtr) xmlMalloc(sizeof(xmlCatalogEntry));
271
2
    if (ret == NULL) {
272
0
        xmlCatalogErrMemory();
273
0
  return(NULL);
274
0
    }
275
2
    ret->next = NULL;
276
2
    ret->parent = NULL;
277
2
    ret->children = NULL;
278
2
    ret->type = type;
279
2
    if (type == XML_CATA_PUBLIC || type == XML_CATA_DELEGATE_PUBLIC) {
280
0
        normid = xmlCatalogNormalizePublic(name);
281
0
        if (normid != NULL)
282
0
            name = (*normid != 0 ? normid : NULL);
283
0
    }
284
2
    if (name != NULL)
285
0
  ret->name = xmlStrdup(name);
286
2
    else
287
2
  ret->name = NULL;
288
2
    if (normid != NULL)
289
0
        xmlFree(normid);
290
2
    if (value != NULL)
291
0
  ret->value = xmlStrdup(value);
292
2
    else
293
2
  ret->value = NULL;
294
2
    if (URL == NULL)
295
0
  URL = value;
296
2
    if (URL != NULL)
297
2
  ret->URL = xmlStrdup(URL);
298
0
    else
299
0
  ret->URL = NULL;
300
2
    ret->prefer = prefer;
301
2
    ret->dealloc = 0;
302
2
    ret->depth = 0;
303
2
    ret->group = group;
304
2
    return(ret);
305
2
}
306
307
static void
308
xmlFreeCatalogEntryList(xmlCatalogEntryPtr ret);
309
310
/**
311
 * Free the memory allocated to a Catalog entry
312
 *
313
 * @param payload  a Catalog entry
314
 * @param name  unused
315
 */
316
static void
317
0
xmlFreeCatalogEntry(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
318
0
    xmlCatalogEntryPtr ret = (xmlCatalogEntryPtr) payload;
319
0
    if (ret == NULL)
320
0
  return;
321
    /*
322
     * Entries stored in the file hash must be deallocated
323
     * only by the file hash cleaner !
324
     */
325
0
    if (ret->dealloc == 1)
326
0
  return;
327
328
0
    if (xmlDebugCatalogs) {
329
0
  if (ret->name != NULL)
330
0
      xmlCatalogPrintDebug(
331
0
        "Free catalog entry %s\n", ret->name);
332
0
  else if (ret->value != NULL)
333
0
      xmlCatalogPrintDebug(
334
0
        "Free catalog entry %s\n", ret->value);
335
0
  else
336
0
      xmlCatalogPrintDebug(
337
0
        "Free catalog entry\n");
338
0
    }
339
340
0
    if (ret->name != NULL)
341
0
  xmlFree(ret->name);
342
0
    if (ret->value != NULL)
343
0
  xmlFree(ret->value);
344
0
    if (ret->URL != NULL)
345
0
  xmlFree(ret->URL);
346
0
    xmlFree(ret);
347
0
}
348
349
/**
350
 * Free the memory allocated to a full chained list of Catalog entries
351
 *
352
 * @param ret  a Catalog entry list
353
 */
354
static void
355
0
xmlFreeCatalogEntryList(xmlCatalogEntryPtr ret) {
356
0
    xmlCatalogEntryPtr next;
357
358
0
    while (ret != NULL) {
359
0
  next = ret->next;
360
0
  xmlFreeCatalogEntry(ret, NULL);
361
0
  ret = next;
362
0
    }
363
0
}
364
365
/**
366
 * Free the memory allocated to list of Catalog entries from the
367
 * catalog file hash.
368
 *
369
 * @param payload  a Catalog entry list
370
 * @param name  unused
371
 */
372
static void
373
xmlFreeCatalogHashEntryList(void *payload,
374
0
                            const xmlChar *name ATTRIBUTE_UNUSED) {
375
0
    xmlCatalogEntryPtr catal = (xmlCatalogEntryPtr) payload;
376
0
    xmlCatalogEntryPtr children, next;
377
378
0
    if (catal == NULL)
379
0
  return;
380
381
0
    children = catal->children;
382
0
    while (children != NULL) {
383
0
  next = children->next;
384
0
  children->dealloc = 0;
385
0
  children->children = NULL;
386
0
  xmlFreeCatalogEntry(children, NULL);
387
0
  children = next;
388
0
    }
389
0
    catal->dealloc = 0;
390
0
    xmlFreeCatalogEntry(catal, NULL);
391
0
}
392
393
/**
394
 * create a new Catalog, this type is shared both by XML and
395
 * SGML catalogs, but the acceptable types values differs.
396
 *
397
 * @param type  type of catalog
398
 * @param prefer  the PUBLIC vs. SYSTEM current preference value
399
 * @returns the xmlCatalog or NULL in case of error
400
 */
401
static xmlCatalogPtr
402
2
xmlCreateNewCatalog(xmlCatalogType type, xmlCatalogPrefer prefer) {
403
2
    xmlCatalogPtr ret;
404
405
2
    ret = (xmlCatalogPtr) xmlMalloc(sizeof(xmlCatalog));
406
2
    if (ret == NULL) {
407
0
        xmlCatalogErrMemory();
408
0
  return(NULL);
409
0
    }
410
2
    memset(ret, 0, sizeof(xmlCatalog));
411
2
    ret->type = type;
412
2
    ret->prefer = prefer;
413
2
#ifdef LIBXML_SGML_CATALOG_ENABLED
414
2
    ret->catalNr = 0;
415
2
    ret->catalMax = XML_MAX_SGML_CATA_DEPTH;
416
2
    if (ret->type == XML_SGML_CATALOG_TYPE)
417
0
  ret->sgml = xmlHashCreate(10);
418
2
#endif
419
2
    return(ret);
420
2
}
421
422
/**
423
 * Free the memory allocated to a Catalog
424
 *
425
 * @deprecated Internal function, don't use
426
 *
427
 * @param catal  a Catalog
428
 */
429
void
430
0
xmlFreeCatalog(xmlCatalog *catal) {
431
0
    if (catal == NULL)
432
0
  return;
433
0
    if (catal->xml != NULL)
434
0
  xmlFreeCatalogEntryList(catal->xml);
435
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
436
0
    if (catal->sgml != NULL)
437
0
  xmlHashFree(catal->sgml, xmlFreeCatalogEntry);
438
0
#endif
439
0
    xmlFree(catal);
440
0
}
441
442
/************************************************************************
443
 *                  *
444
 *      Serializing Catalogs        *
445
 *                  *
446
 ************************************************************************/
447
448
#ifdef LIBXML_OUTPUT_ENABLED
449
#ifdef LIBXML_SGML_CATALOG_ENABLED
450
/**
451
 * Serialize an SGML Catalog entry
452
 *
453
 * @param payload  the catalog entry
454
 * @param data  the file.
455
 * @param name  unused
456
 */
457
static void
458
xmlCatalogDumpEntry(void *payload, void *data,
459
0
                    const xmlChar *name ATTRIBUTE_UNUSED) {
460
0
    xmlCatalogEntryPtr entry = (xmlCatalogEntryPtr) payload;
461
0
    FILE *out = (FILE *) data;
462
0
    if ((entry == NULL) || (out == NULL))
463
0
  return;
464
0
    switch (entry->type) {
465
0
  case SGML_CATA_ENTITY:
466
0
      fprintf(out, "ENTITY "); break;
467
0
  case SGML_CATA_PENTITY:
468
0
      fprintf(out, "ENTITY %%"); break;
469
0
  case SGML_CATA_DOCTYPE:
470
0
      fprintf(out, "DOCTYPE "); break;
471
0
  case SGML_CATA_LINKTYPE:
472
0
      fprintf(out, "LINKTYPE "); break;
473
0
  case SGML_CATA_NOTATION:
474
0
      fprintf(out, "NOTATION "); break;
475
0
  case SGML_CATA_PUBLIC:
476
0
      fprintf(out, "PUBLIC "); break;
477
0
  case SGML_CATA_SYSTEM:
478
0
      fprintf(out, "SYSTEM "); break;
479
0
  case SGML_CATA_DELEGATE:
480
0
      fprintf(out, "DELEGATE "); break;
481
0
  case SGML_CATA_BASE:
482
0
      fprintf(out, "BASE "); break;
483
0
  case SGML_CATA_CATALOG:
484
0
      fprintf(out, "CATALOG "); break;
485
0
  case SGML_CATA_DOCUMENT:
486
0
      fprintf(out, "DOCUMENT "); break;
487
0
  case SGML_CATA_SGMLDECL:
488
0
      fprintf(out, "SGMLDECL "); break;
489
0
  default:
490
0
      return;
491
0
    }
492
0
    switch (entry->type) {
493
0
  case SGML_CATA_ENTITY:
494
0
  case SGML_CATA_PENTITY:
495
0
  case SGML_CATA_DOCTYPE:
496
0
  case SGML_CATA_LINKTYPE:
497
0
  case SGML_CATA_NOTATION:
498
0
      fprintf(out, "%s", (const char *) entry->name); break;
499
0
  case SGML_CATA_PUBLIC:
500
0
  case SGML_CATA_SYSTEM:
501
0
  case SGML_CATA_SGMLDECL:
502
0
  case SGML_CATA_DOCUMENT:
503
0
  case SGML_CATA_CATALOG:
504
0
  case SGML_CATA_BASE:
505
0
  case SGML_CATA_DELEGATE:
506
0
      fprintf(out, "\"%s\"", entry->name); break;
507
0
  default:
508
0
      break;
509
0
    }
510
0
    switch (entry->type) {
511
0
  case SGML_CATA_ENTITY:
512
0
  case SGML_CATA_PENTITY:
513
0
  case SGML_CATA_DOCTYPE:
514
0
  case SGML_CATA_LINKTYPE:
515
0
  case SGML_CATA_NOTATION:
516
0
  case SGML_CATA_PUBLIC:
517
0
  case SGML_CATA_SYSTEM:
518
0
  case SGML_CATA_DELEGATE:
519
0
      fprintf(out, " \"%s\"", entry->value); break;
520
0
  default:
521
0
      break;
522
0
    }
523
0
    fprintf(out, "\n");
524
0
}
525
#endif /* LIBXML_SGML_CATALOG_ENABLED */
526
527
/**
528
 * Serializes a Catalog entry, called by xmlDumpXMLCatalog and recursively
529
 * for group entries
530
 *
531
 * @param catal  top catalog entry
532
 * @param catalog  pointer to the xml tree
533
 * @param doc  the containing document
534
 * @param ns  the current namespace
535
 * @param cgroup  group node for group members
536
 */
537
static void xmlDumpXMLCatalogNode(xmlCatalogEntryPtr catal, xmlNodePtr catalog,
538
0
        xmlDocPtr doc, xmlNsPtr ns, xmlCatalogEntryPtr cgroup) {
539
0
    xmlNodePtr node;
540
0
    xmlCatalogEntryPtr cur;
541
    /*
542
     * add all the catalog entries
543
     */
544
0
    cur = catal;
545
0
    while (cur != NULL) {
546
0
        if (cur->group == cgroup) {
547
0
      switch (cur->type) {
548
0
          case XML_CATA_REMOVED:
549
0
        break;
550
0
          case XML_CATA_BROKEN_CATALOG:
551
0
          case XML_CATA_CATALOG:
552
0
        if (cur == catal) {
553
0
      cur = cur->children;
554
0
            continue;
555
0
        }
556
0
        break;
557
0
    case XML_CATA_NEXT_CATALOG:
558
0
        node = xmlNewDocNode(doc, ns, BAD_CAST "nextCatalog", NULL);
559
0
        xmlSetProp(node, BAD_CAST "catalog", cur->value);
560
0
        xmlAddChild(catalog, node);
561
0
                    break;
562
0
    case XML_CATA_NONE:
563
0
        break;
564
0
    case XML_CATA_GROUP:
565
0
        node = xmlNewDocNode(doc, ns, BAD_CAST "group", NULL);
566
0
        xmlSetProp(node, BAD_CAST "id", cur->name);
567
0
        if (cur->value != NULL) {
568
0
            xmlNsPtr xns;
569
0
      xns = xmlSearchNsByHref(doc, node, XML_XML_NAMESPACE);
570
0
      if (xns != NULL)
571
0
          xmlSetNsProp(node, xns, BAD_CAST "base",
572
0
           cur->value);
573
0
        }
574
0
        switch (cur->prefer) {
575
0
      case XML_CATA_PREFER_NONE:
576
0
                break;
577
0
      case XML_CATA_PREFER_PUBLIC:
578
0
                xmlSetProp(node, BAD_CAST "prefer", BAD_CAST "public");
579
0
          break;
580
0
      case XML_CATA_PREFER_SYSTEM:
581
0
                xmlSetProp(node, BAD_CAST "prefer", BAD_CAST "system");
582
0
          break;
583
0
        }
584
0
        xmlDumpXMLCatalogNode(cur->next, node, doc, ns, cur);
585
0
        xmlAddChild(catalog, node);
586
0
              break;
587
0
    case XML_CATA_PUBLIC:
588
0
        node = xmlNewDocNode(doc, ns, BAD_CAST "public", NULL);
589
0
        xmlSetProp(node, BAD_CAST "publicId", cur->name);
590
0
        xmlSetProp(node, BAD_CAST "uri", cur->value);
591
0
        xmlAddChild(catalog, node);
592
0
        break;
593
0
    case XML_CATA_SYSTEM:
594
0
        node = xmlNewDocNode(doc, ns, BAD_CAST "system", NULL);
595
0
        xmlSetProp(node, BAD_CAST "systemId", cur->name);
596
0
        xmlSetProp(node, BAD_CAST "uri", cur->value);
597
0
        xmlAddChild(catalog, node);
598
0
        break;
599
0
    case XML_CATA_REWRITE_SYSTEM:
600
0
        node = xmlNewDocNode(doc, ns, BAD_CAST "rewriteSystem", NULL);
601
0
        xmlSetProp(node, BAD_CAST "systemIdStartString", cur->name);
602
0
        xmlSetProp(node, BAD_CAST "rewritePrefix", cur->value);
603
0
        xmlAddChild(catalog, node);
604
0
        break;
605
0
    case XML_CATA_DELEGATE_PUBLIC:
606
0
        node = xmlNewDocNode(doc, ns, BAD_CAST "delegatePublic", NULL);
607
0
        xmlSetProp(node, BAD_CAST "publicIdStartString", cur->name);
608
0
        xmlSetProp(node, BAD_CAST "catalog", cur->value);
609
0
        xmlAddChild(catalog, node);
610
0
        break;
611
0
    case XML_CATA_DELEGATE_SYSTEM:
612
0
        node = xmlNewDocNode(doc, ns, BAD_CAST "delegateSystem", NULL);
613
0
        xmlSetProp(node, BAD_CAST "systemIdStartString", cur->name);
614
0
        xmlSetProp(node, BAD_CAST "catalog", cur->value);
615
0
        xmlAddChild(catalog, node);
616
0
        break;
617
0
    case XML_CATA_URI:
618
0
        node = xmlNewDocNode(doc, ns, BAD_CAST "uri", NULL);
619
0
        xmlSetProp(node, BAD_CAST "name", cur->name);
620
0
        xmlSetProp(node, BAD_CAST "uri", cur->value);
621
0
        xmlAddChild(catalog, node);
622
0
        break;
623
0
    case XML_CATA_REWRITE_URI:
624
0
        node = xmlNewDocNode(doc, ns, BAD_CAST "rewriteURI", NULL);
625
0
        xmlSetProp(node, BAD_CAST "uriStartString", cur->name);
626
0
        xmlSetProp(node, BAD_CAST "rewritePrefix", cur->value);
627
0
        xmlAddChild(catalog, node);
628
0
        break;
629
0
    case XML_CATA_DELEGATE_URI:
630
0
        node = xmlNewDocNode(doc, ns, BAD_CAST "delegateURI", NULL);
631
0
        xmlSetProp(node, BAD_CAST "uriStartString", cur->name);
632
0
        xmlSetProp(node, BAD_CAST "catalog", cur->value);
633
0
        xmlAddChild(catalog, node);
634
0
        break;
635
0
                default:
636
0
        break;
637
0
      }
638
0
        }
639
0
  cur = cur->next;
640
0
    }
641
0
}
642
643
static int
644
0
xmlDumpXMLCatalog(FILE *out, xmlCatalogEntryPtr catal) {
645
0
    int ret;
646
0
    xmlDocPtr doc;
647
0
    xmlNsPtr ns;
648
0
    xmlDtdPtr dtd;
649
0
    xmlNodePtr catalog;
650
0
    xmlOutputBufferPtr buf;
651
652
    /*
653
     * Rebuild a catalog
654
     */
655
0
    doc = xmlNewDoc(NULL);
656
0
    if (doc == NULL)
657
0
  return(-1);
658
0
    dtd = xmlNewDtd(doc, BAD_CAST "catalog",
659
0
         BAD_CAST "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN",
660
0
BAD_CAST "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd");
661
662
0
    xmlAddChild((xmlNodePtr) doc, (xmlNodePtr) dtd);
663
664
0
    ns = xmlNewNs(NULL, XML_CATALOGS_NAMESPACE, NULL);
665
0
    if (ns == NULL) {
666
0
  xmlFreeDoc(doc);
667
0
  return(-1);
668
0
    }
669
0
    catalog = xmlNewDocNode(doc, ns, BAD_CAST "catalog", NULL);
670
0
    if (catalog == NULL) {
671
0
  xmlFreeNs(ns);
672
0
  xmlFreeDoc(doc);
673
0
  return(-1);
674
0
    }
675
0
    catalog->nsDef = ns;
676
0
    xmlAddChild((xmlNodePtr) doc, catalog);
677
678
0
    xmlDumpXMLCatalogNode(catal, catalog, doc, ns, NULL);
679
680
    /*
681
     * reserialize it
682
     */
683
0
    buf = xmlOutputBufferCreateFile(out, NULL);
684
0
    if (buf == NULL) {
685
0
  xmlFreeDoc(doc);
686
0
  return(-1);
687
0
    }
688
0
    ret = xmlSaveFormatFileTo(buf, doc, NULL, 1);
689
690
    /*
691
     * Free it
692
     */
693
0
    xmlFreeDoc(doc);
694
695
0
    return(ret);
696
0
}
697
#endif /* LIBXML_OUTPUT_ENABLED */
698
699
/************************************************************************
700
 *                  *
701
 *      Converting SGML Catalogs to XML     *
702
 *                  *
703
 ************************************************************************/
704
705
#ifdef LIBXML_SGML_CATALOG_ENABLED
706
707
/**
708
 * Convert one entry from the catalog
709
 *
710
 * @param payload  the entry
711
 * @param data  pointer to the catalog being converted
712
 * @param name  unused
713
 */
714
static void
715
xmlCatalogConvertEntry(void *payload, void *data,
716
0
                       const xmlChar *name ATTRIBUTE_UNUSED) {
717
0
    xmlCatalogEntryPtr entry = (xmlCatalogEntryPtr) payload;
718
0
    xmlCatalogPtr catal = (xmlCatalogPtr) data;
719
0
    if ((entry == NULL) || (catal == NULL) || (catal->sgml == NULL) ||
720
0
  (catal->xml == NULL))
721
0
  return;
722
0
    switch (entry->type) {
723
0
  case SGML_CATA_ENTITY:
724
0
      entry->type = XML_CATA_PUBLIC;
725
0
      break;
726
0
  case SGML_CATA_PENTITY:
727
0
      entry->type = XML_CATA_PUBLIC;
728
0
      break;
729
0
  case SGML_CATA_DOCTYPE:
730
0
      entry->type = XML_CATA_PUBLIC;
731
0
      break;
732
0
  case SGML_CATA_LINKTYPE:
733
0
      entry->type = XML_CATA_PUBLIC;
734
0
      break;
735
0
  case SGML_CATA_NOTATION:
736
0
      entry->type = XML_CATA_PUBLIC;
737
0
      break;
738
0
  case SGML_CATA_PUBLIC:
739
0
      entry->type = XML_CATA_PUBLIC;
740
0
      break;
741
0
  case SGML_CATA_SYSTEM:
742
0
      entry->type = XML_CATA_SYSTEM;
743
0
      break;
744
0
  case SGML_CATA_DELEGATE:
745
0
      entry->type = XML_CATA_DELEGATE_PUBLIC;
746
0
      break;
747
0
  case SGML_CATA_CATALOG:
748
0
      entry->type = XML_CATA_CATALOG;
749
0
      break;
750
0
  default:
751
0
      xmlHashRemoveEntry(catal->sgml, entry->name, xmlFreeCatalogEntry);
752
0
      return;
753
0
    }
754
    /*
755
     * Conversion successful, remove from the SGML catalog
756
     * and add it to the default XML one
757
     */
758
0
    xmlHashRemoveEntry(catal->sgml, entry->name, NULL);
759
0
    entry->parent = catal->xml;
760
0
    entry->next = NULL;
761
0
    if (catal->xml->children == NULL)
762
0
  catal->xml->children = entry;
763
0
    else {
764
0
  xmlCatalogEntryPtr prev;
765
766
0
  prev = catal->xml->children;
767
0
  while (prev->next != NULL)
768
0
      prev = prev->next;
769
0
  prev->next = entry;
770
0
    }
771
0
}
772
773
/**
774
 * Convert all the SGML catalog entries as XML ones
775
 *
776
 * @deprecated Internal function, don't use
777
 *
778
 * @param catal  the catalog
779
 * @returns the number of entries converted if successful, -1 otherwise
780
 */
781
int
782
0
xmlConvertSGMLCatalog(xmlCatalog *catal) {
783
784
0
    if ((catal == NULL) || (catal->type != XML_SGML_CATALOG_TYPE))
785
0
  return(-1);
786
787
0
    if (xmlDebugCatalogs) {
788
0
  xmlCatalogPrintDebug(
789
0
    "Converting SGML catalog to XML\n");
790
0
    }
791
0
    xmlHashScan(catal->sgml, xmlCatalogConvertEntry, &catal);
792
0
    return(0);
793
0
}
794
795
#endif /* LIBXML_SGML_CATALOG_ENABLED */
796
797
/************************************************************************
798
 *                  *
799
 *      Helper function         *
800
 *                  *
801
 ************************************************************************/
802
803
/**
804
 * Expand the URN into the equivalent Public Identifier
805
 *
806
 * @param urn  an "urn:publicid:" to unwrap
807
 * @returns the new identifier or NULL, the string must be deallocated
808
 *         by the caller.
809
 */
810
static xmlChar *
811
0
xmlCatalogUnWrapURN(const xmlChar *urn) {
812
0
    xmlChar result[2000];
813
0
    unsigned int i = 0;
814
815
0
    if (xmlStrncmp(urn, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1))
816
0
  return(NULL);
817
0
    urn += sizeof(XML_URN_PUBID) - 1;
818
819
0
    while (*urn != 0) {
820
0
  if (i > sizeof(result) - 4)
821
0
      break;
822
0
  if (*urn == '+') {
823
0
      result[i++] = ' ';
824
0
      urn++;
825
0
  } else if (*urn == ':') {
826
0
      result[i++] = '/';
827
0
      result[i++] = '/';
828
0
      urn++;
829
0
  } else if (*urn == ';') {
830
0
      result[i++] = ':';
831
0
      result[i++] = ':';
832
0
      urn++;
833
0
  } else if (*urn == '%') {
834
0
      if ((urn[1] == '2') && (urn[2] == 'B'))
835
0
    result[i++] = '+';
836
0
      else if ((urn[1] == '3') && (urn[2] == 'A'))
837
0
    result[i++] = ':';
838
0
      else if ((urn[1] == '2') && (urn[2] == 'F'))
839
0
    result[i++] = '/';
840
0
      else if ((urn[1] == '3') && (urn[2] == 'B'))
841
0
    result[i++] = ';';
842
0
      else if ((urn[1] == '2') && (urn[2] == '7'))
843
0
    result[i++] = '\'';
844
0
      else if ((urn[1] == '3') && (urn[2] == 'F'))
845
0
    result[i++] = '?';
846
0
      else if ((urn[1] == '2') && (urn[2] == '3'))
847
0
    result[i++] = '#';
848
0
      else if ((urn[1] == '2') && (urn[2] == '5'))
849
0
    result[i++] = '%';
850
0
      else {
851
0
    result[i++] = *urn;
852
0
    urn++;
853
0
    continue;
854
0
      }
855
0
      urn += 3;
856
0
  } else {
857
0
      result[i++] = *urn;
858
0
      urn++;
859
0
  }
860
0
    }
861
0
    result[i] = 0;
862
863
0
    return(xmlStrdup(result));
864
0
}
865
866
/**
867
 * parse an XML file and build a tree. It's like #xmlParseFile
868
 * except it bypass all catalog lookups.
869
 *
870
 * @deprecated Internal function, don't use
871
 *
872
 * @param filename  the filename
873
 * @returns the resulting document tree or NULL in case of error
874
 */
875
876
xmlDoc *
877
0
xmlParseCatalogFile(const char *filename) {
878
0
    xmlDocPtr ret;
879
0
    xmlParserCtxtPtr ctxt;
880
0
    xmlParserInputPtr inputStream;
881
0
    xmlParserInputBufferPtr buf;
882
883
0
    ctxt = xmlNewParserCtxt();
884
0
    if (ctxt == NULL) {
885
0
        xmlCatalogErrMemory();
886
0
  return(NULL);
887
0
    }
888
889
0
    buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
890
0
    if (buf == NULL) {
891
0
  xmlFreeParserCtxt(ctxt);
892
0
  return(NULL);
893
0
    }
894
895
0
    inputStream = xmlNewInputStream(ctxt);
896
0
    if (inputStream == NULL) {
897
0
  xmlFreeParserInputBuffer(buf);
898
0
  xmlFreeParserCtxt(ctxt);
899
0
  return(NULL);
900
0
    }
901
902
0
    inputStream->filename = (char *) xmlCanonicPath((const xmlChar *)filename);
903
0
    inputStream->buf = buf;
904
0
    xmlBufResetInput(buf->buffer, inputStream);
905
906
0
    if (xmlCtxtPushInput(ctxt, inputStream) < 0) {
907
0
        xmlFreeInputStream(inputStream);
908
0
        xmlFreeParserCtxt(ctxt);
909
0
        return(NULL);
910
0
    }
911
912
0
    ctxt->valid = 0;
913
0
    ctxt->validate = 0;
914
0
    ctxt->loadsubset = 0;
915
0
    ctxt->pedantic = 0;
916
0
    ctxt->dictNames = 1;
917
918
0
    xmlParseDocument(ctxt);
919
920
0
    if (ctxt->wellFormed)
921
0
  ret = ctxt->myDoc;
922
0
    else {
923
0
        ret = NULL;
924
0
        xmlFreeDoc(ctxt->myDoc);
925
0
        ctxt->myDoc = NULL;
926
0
    }
927
0
    xmlFreeParserCtxt(ctxt);
928
929
0
    return(ret);
930
0
}
931
932
/**
933
 * Load a file content into memory.
934
 *
935
 * @param filename  a file path
936
 * @returns a pointer to the 0 terminated string or NULL in case of error
937
 */
938
static xmlChar *
939
xmlLoadFileContent(const char *filename)
940
0
{
941
0
    int fd;
942
0
    int len;
943
0
    long size;
944
945
0
    struct stat info;
946
0
    xmlChar *content;
947
948
0
    if (filename == NULL)
949
0
        return (NULL);
950
951
0
    if (stat(filename, &info) < 0)
952
0
        return (NULL);
953
954
0
    fd = open(filename, O_RDONLY);
955
0
    if (fd  < 0)
956
0
    {
957
0
        return (NULL);
958
0
    }
959
0
    size = info.st_size;
960
0
    content = xmlMalloc(size + 10);
961
0
    if (content == NULL) {
962
0
        xmlCatalogErrMemory();
963
0
  close(fd);
964
0
        return (NULL);
965
0
    }
966
0
    len = read(fd, content, size);
967
0
    close(fd);
968
0
    if (len < 0) {
969
0
        xmlFree(content);
970
0
        return (NULL);
971
0
    }
972
0
    content[len] = 0;
973
974
0
    return(content);
975
0
}
976
977
/**
978
 *  Normalizes the Public Identifier
979
 *
980
 * Implements 6.2. Public Identifier Normalization
981
 * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
982
 *
983
 * @param pubID  the public ID string
984
 * @returns the new string or NULL, the string must be deallocated
985
 *         by the caller.
986
 */
987
static xmlChar *
988
xmlCatalogNormalizePublic(const xmlChar *pubID)
989
0
{
990
0
    int ok = 1;
991
0
    int white;
992
0
    const xmlChar *p;
993
0
    xmlChar *ret;
994
0
    xmlChar *q;
995
996
0
    if (pubID == NULL)
997
0
        return(NULL);
998
999
0
    white = 1;
1000
0
    for (p = pubID;*p != 0 && ok;p++) {
1001
0
        if (!xmlIsBlank_ch(*p))
1002
0
            white = 0;
1003
0
        else if (*p == 0x20 && !white)
1004
0
            white = 1;
1005
0
        else
1006
0
            ok = 0;
1007
0
    }
1008
0
    if (ok && !white) /* is normalized */
1009
0
        return(NULL);
1010
1011
0
    ret = xmlStrdup(pubID);
1012
0
    q = ret;
1013
0
    white = 0;
1014
0
    for (p = pubID;*p != 0;p++) {
1015
0
        if (xmlIsBlank_ch(*p)) {
1016
0
            if (q != ret)
1017
0
                white = 1;
1018
0
        } else {
1019
0
            if (white) {
1020
0
                *(q++) = 0x20;
1021
0
                white = 0;
1022
0
            }
1023
0
            *(q++) = *p;
1024
0
        }
1025
0
    }
1026
0
    *q = 0;
1027
0
    return(ret);
1028
0
}
1029
1030
/************************************************************************
1031
 *                  *
1032
 *      The XML Catalog parser        *
1033
 *                  *
1034
 ************************************************************************/
1035
1036
static xmlCatalogEntryPtr
1037
xmlParseXMLCatalogFile(xmlCatalogPrefer prefer, const xmlChar *filename);
1038
static void
1039
xmlParseXMLCatalogNodeList(xmlNodePtr cur, xmlCatalogPrefer prefer,
1040
                     xmlCatalogEntryPtr parent, xmlCatalogEntryPtr cgroup);
1041
static xmlChar *
1042
xmlCatalogListXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID,
1043
                const xmlChar *sysID);
1044
static xmlChar *
1045
xmlCatalogListXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI);
1046
1047
1048
/**
1049
 * lookup the internal type associated to an XML catalog entry name
1050
 *
1051
 * @param name  the name
1052
 * @returns the type associated with that name
1053
 */
1054
static xmlCatalogEntryType
1055
0
xmlGetXMLCatalogEntryType(const xmlChar *name) {
1056
0
    xmlCatalogEntryType type = XML_CATA_NONE;
1057
0
    if (xmlStrEqual(name, (const xmlChar *) "system"))
1058
0
  type = XML_CATA_SYSTEM;
1059
0
    else if (xmlStrEqual(name, (const xmlChar *) "public"))
1060
0
  type = XML_CATA_PUBLIC;
1061
0
    else if (xmlStrEqual(name, (const xmlChar *) "rewriteSystem"))
1062
0
  type = XML_CATA_REWRITE_SYSTEM;
1063
0
    else if (xmlStrEqual(name, (const xmlChar *) "delegatePublic"))
1064
0
  type = XML_CATA_DELEGATE_PUBLIC;
1065
0
    else if (xmlStrEqual(name, (const xmlChar *) "delegateSystem"))
1066
0
  type = XML_CATA_DELEGATE_SYSTEM;
1067
0
    else if (xmlStrEqual(name, (const xmlChar *) "uri"))
1068
0
  type = XML_CATA_URI;
1069
0
    else if (xmlStrEqual(name, (const xmlChar *) "rewriteURI"))
1070
0
  type = XML_CATA_REWRITE_URI;
1071
0
    else if (xmlStrEqual(name, (const xmlChar *) "delegateURI"))
1072
0
  type = XML_CATA_DELEGATE_URI;
1073
0
    else if (xmlStrEqual(name, (const xmlChar *) "nextCatalog"))
1074
0
  type = XML_CATA_NEXT_CATALOG;
1075
0
    else if (xmlStrEqual(name, (const xmlChar *) "catalog"))
1076
0
  type = XML_CATA_CATALOG;
1077
0
    return(type);
1078
0
}
1079
1080
/**
1081
 * Finishes the examination of an XML tree node of a catalog and build
1082
 * a Catalog entry from it.
1083
 *
1084
 * @param cur  the XML node
1085
 * @param type  the type of Catalog entry
1086
 * @param name  the name of the node
1087
 * @param attrName  the attribute holding the value
1088
 * @param uriAttrName  the attribute holding the URI-Reference
1089
 * @param prefer  the PUBLIC vs. SYSTEM current preference value
1090
 * @param cgroup  the group which includes this node
1091
 * @returns the new Catalog entry node or NULL in case of error.
1092
 */
1093
static xmlCatalogEntryPtr
1094
xmlParseXMLCatalogOneNode(xmlNodePtr cur, xmlCatalogEntryType type,
1095
        const xmlChar *name, const xmlChar *attrName,
1096
        const xmlChar *uriAttrName, xmlCatalogPrefer prefer,
1097
0
        xmlCatalogEntryPtr cgroup) {
1098
0
    int ok = 1;
1099
0
    xmlChar *uriValue;
1100
0
    xmlChar *nameValue = NULL;
1101
0
    xmlChar *base = NULL;
1102
0
    xmlChar *URL = NULL;
1103
0
    xmlCatalogEntryPtr ret = NULL;
1104
1105
0
    if (attrName != NULL) {
1106
0
  nameValue = xmlGetProp(cur, attrName);
1107
0
  if (nameValue == NULL) {
1108
0
      xmlCatalogErr(ret, cur, XML_CATALOG_MISSING_ATTR,
1109
0
        "%s entry lacks '%s'\n", name, attrName, NULL);
1110
0
      ok = 0;
1111
0
  }
1112
0
    }
1113
0
    uriValue = xmlGetProp(cur, uriAttrName);
1114
0
    if (uriValue == NULL) {
1115
0
  xmlCatalogErr(ret, cur, XML_CATALOG_MISSING_ATTR,
1116
0
    "%s entry lacks '%s'\n", name, uriAttrName, NULL);
1117
0
  ok = 0;
1118
0
    }
1119
0
    if (!ok) {
1120
0
  if (nameValue != NULL)
1121
0
      xmlFree(nameValue);
1122
0
  if (uriValue != NULL)
1123
0
      xmlFree(uriValue);
1124
0
  return(NULL);
1125
0
    }
1126
1127
0
    base = xmlNodeGetBase(cur->doc, cur);
1128
0
    URL = xmlBuildURI(uriValue, base);
1129
0
    if (URL != NULL) {
1130
0
  if (xmlDebugCatalogs > 1) {
1131
0
      if (nameValue != NULL)
1132
0
    xmlCatalogPrintDebug(
1133
0
      "Found %s: '%s' '%s'\n", name, nameValue, URL);
1134
0
      else
1135
0
    xmlCatalogPrintDebug(
1136
0
      "Found %s: '%s'\n", name, URL);
1137
0
  }
1138
0
  ret = xmlNewCatalogEntry(type, nameValue, uriValue, URL, prefer, cgroup);
1139
0
    } else {
1140
0
  xmlCatalogErr(ret, cur, XML_CATALOG_ENTRY_BROKEN,
1141
0
    "%s entry '%s' broken ?: %s\n", name, uriAttrName, uriValue);
1142
0
    }
1143
0
    if (nameValue != NULL)
1144
0
  xmlFree(nameValue);
1145
0
    if (uriValue != NULL)
1146
0
  xmlFree(uriValue);
1147
0
    if (base != NULL)
1148
0
  xmlFree(base);
1149
0
    if (URL != NULL)
1150
0
  xmlFree(URL);
1151
0
    return(ret);
1152
0
}
1153
1154
/**
1155
 * Examines an XML tree node of a catalog and build
1156
 * a Catalog entry from it adding it to its parent. The examination can
1157
 * be recursive.
1158
 *
1159
 * @param cur  the XML node
1160
 * @param prefer  the PUBLIC vs. SYSTEM current preference value
1161
 * @param parent  the parent Catalog entry
1162
 * @param cgroup  the group which includes this node
1163
 */
1164
static void
1165
xmlParseXMLCatalogNode(xmlNodePtr cur, xmlCatalogPrefer prefer,
1166
                 xmlCatalogEntryPtr parent, xmlCatalogEntryPtr cgroup)
1167
0
{
1168
0
    xmlChar *base = NULL;
1169
0
    xmlCatalogEntryPtr entry = NULL;
1170
1171
0
    if (cur == NULL)
1172
0
        return;
1173
0
    if (xmlStrEqual(cur->name, BAD_CAST "group")) {
1174
0
        xmlChar *prop;
1175
0
  xmlCatalogPrefer pref = XML_CATA_PREFER_NONE;
1176
1177
0
        prop = xmlGetProp(cur, BAD_CAST "prefer");
1178
0
        if (prop != NULL) {
1179
0
            if (xmlStrEqual(prop, BAD_CAST "system")) {
1180
0
                prefer = XML_CATA_PREFER_SYSTEM;
1181
0
            } else if (xmlStrEqual(prop, BAD_CAST "public")) {
1182
0
                prefer = XML_CATA_PREFER_PUBLIC;
1183
0
            } else {
1184
0
    xmlCatalogErr(parent, cur, XML_CATALOG_PREFER_VALUE,
1185
0
                              "Invalid value for prefer: '%s'\n",
1186
0
            prop, NULL, NULL);
1187
0
            }
1188
0
            xmlFree(prop);
1189
0
      pref = prefer;
1190
0
        }
1191
0
  prop = xmlGetProp(cur, BAD_CAST "id");
1192
0
  base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
1193
0
  entry = xmlNewCatalogEntry(XML_CATA_GROUP, prop, base, NULL, pref, cgroup);
1194
0
  xmlFree(prop);
1195
0
    } else if (xmlStrEqual(cur->name, BAD_CAST "public")) {
1196
0
  entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_PUBLIC,
1197
0
    BAD_CAST "public", BAD_CAST "publicId", BAD_CAST "uri", prefer, cgroup);
1198
0
    } else if (xmlStrEqual(cur->name, BAD_CAST "system")) {
1199
0
  entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_SYSTEM,
1200
0
    BAD_CAST "system", BAD_CAST "systemId", BAD_CAST "uri", prefer, cgroup);
1201
0
    } else if (xmlStrEqual(cur->name, BAD_CAST "rewriteSystem")) {
1202
0
  entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_REWRITE_SYSTEM,
1203
0
    BAD_CAST "rewriteSystem", BAD_CAST "systemIdStartString",
1204
0
    BAD_CAST "rewritePrefix", prefer, cgroup);
1205
0
    } else if (xmlStrEqual(cur->name, BAD_CAST "delegatePublic")) {
1206
0
  entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_DELEGATE_PUBLIC,
1207
0
    BAD_CAST "delegatePublic", BAD_CAST "publicIdStartString",
1208
0
    BAD_CAST "catalog", prefer, cgroup);
1209
0
    } else if (xmlStrEqual(cur->name, BAD_CAST "delegateSystem")) {
1210
0
  entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_DELEGATE_SYSTEM,
1211
0
    BAD_CAST "delegateSystem", BAD_CAST "systemIdStartString",
1212
0
    BAD_CAST "catalog", prefer, cgroup);
1213
0
    } else if (xmlStrEqual(cur->name, BAD_CAST "uri")) {
1214
0
  entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_URI,
1215
0
    BAD_CAST "uri", BAD_CAST "name",
1216
0
    BAD_CAST "uri", prefer, cgroup);
1217
0
    } else if (xmlStrEqual(cur->name, BAD_CAST "rewriteURI")) {
1218
0
  entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_REWRITE_URI,
1219
0
    BAD_CAST "rewriteURI", BAD_CAST "uriStartString",
1220
0
    BAD_CAST "rewritePrefix", prefer, cgroup);
1221
0
    } else if (xmlStrEqual(cur->name, BAD_CAST "delegateURI")) {
1222
0
  entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_DELEGATE_URI,
1223
0
    BAD_CAST "delegateURI", BAD_CAST "uriStartString",
1224
0
    BAD_CAST "catalog", prefer, cgroup);
1225
0
    } else if (xmlStrEqual(cur->name, BAD_CAST "nextCatalog")) {
1226
0
  xmlCatalogEntryPtr prev = parent->children;
1227
1228
0
  entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_NEXT_CATALOG,
1229
0
    BAD_CAST "nextCatalog", NULL,
1230
0
    BAD_CAST "catalog", prefer, cgroup);
1231
  /* Avoid duplication of nextCatalog */
1232
0
  while (prev != NULL) {
1233
0
      if ((prev->type == XML_CATA_NEXT_CATALOG) &&
1234
0
    (xmlStrEqual (prev->URL, entry->URL)) &&
1235
0
    (xmlStrEqual (prev->value, entry->value)) &&
1236
0
    (prev->prefer == entry->prefer) &&
1237
0
    (prev->group == entry->group)) {
1238
0
        if (xmlDebugCatalogs)
1239
0
      xmlCatalogPrintDebug(
1240
0
          "Ignoring repeated nextCatalog %s\n", entry->URL);
1241
0
        xmlFreeCatalogEntry(entry, NULL);
1242
0
        entry = NULL;
1243
0
        break;
1244
0
      }
1245
0
      prev = prev->next;
1246
0
  }
1247
0
    }
1248
0
    if (entry != NULL) {
1249
0
        if (parent != NULL) {
1250
0
      entry->parent = parent;
1251
0
      if (parent->children == NULL)
1252
0
    parent->children = entry;
1253
0
      else {
1254
0
    xmlCatalogEntryPtr prev;
1255
1256
0
    prev = parent->children;
1257
0
    while (prev->next != NULL)
1258
0
        prev = prev->next;
1259
0
    prev->next = entry;
1260
0
      }
1261
0
  }
1262
0
  if (entry->type == XML_CATA_GROUP) {
1263
      /*
1264
       * Recurse to propagate prefer to the subtree
1265
       * (xml:base handling is automated)
1266
       */
1267
0
            xmlParseXMLCatalogNodeList(cur->children, prefer, parent, entry);
1268
0
  }
1269
0
    }
1270
0
    if (base != NULL)
1271
0
  xmlFree(base);
1272
0
}
1273
1274
/**
1275
 * Examines a list of XML sibling nodes of a catalog and build
1276
 * a list of Catalog entry from it adding it to the parent.
1277
 * The examination will recurse to examine node subtrees.
1278
 *
1279
 * @param cur  the XML node list of siblings
1280
 * @param prefer  the PUBLIC vs. SYSTEM current preference value
1281
 * @param parent  the parent Catalog entry
1282
 * @param cgroup  the group which includes this list
1283
 */
1284
static void
1285
xmlParseXMLCatalogNodeList(xmlNodePtr cur, xmlCatalogPrefer prefer,
1286
0
                     xmlCatalogEntryPtr parent, xmlCatalogEntryPtr cgroup) {
1287
0
    while (cur != NULL) {
1288
0
  if ((cur->ns != NULL) && (cur->ns->href != NULL) &&
1289
0
      (xmlStrEqual(cur->ns->href, XML_CATALOGS_NAMESPACE))) {
1290
0
      xmlParseXMLCatalogNode(cur, prefer, parent, cgroup);
1291
0
  }
1292
0
  cur = cur->next;
1293
0
    }
1294
    /* TODO: sort the list according to REWRITE lengths and prefer value */
1295
0
}
1296
1297
/**
1298
 * Parses the catalog file to extract the XML tree and then analyze the
1299
 * tree to build a list of Catalog entries corresponding to this catalog
1300
 *
1301
 * @param prefer  the PUBLIC vs. SYSTEM current preference value
1302
 * @param filename  the filename for the catalog
1303
 * @returns the resulting Catalog entries list
1304
 */
1305
static xmlCatalogEntryPtr
1306
0
xmlParseXMLCatalogFile(xmlCatalogPrefer prefer, const xmlChar *filename) {
1307
0
    xmlDocPtr doc;
1308
0
    xmlNodePtr cur;
1309
0
    xmlChar *prop;
1310
0
    xmlCatalogEntryPtr parent = NULL;
1311
1312
0
    if (filename == NULL)
1313
0
        return(NULL);
1314
1315
0
    doc = xmlParseCatalogFile((const char *) filename);
1316
0
    if (doc == NULL) {
1317
0
  if (xmlDebugCatalogs)
1318
0
      xmlCatalogPrintDebug(
1319
0
        "Failed to parse catalog %s\n", filename);
1320
0
  return(NULL);
1321
0
    }
1322
1323
0
    if (xmlDebugCatalogs)
1324
0
  xmlCatalogPrintDebug(
1325
0
    "Parsing catalog %s\n", filename);
1326
1327
0
    cur = xmlDocGetRootElement(doc);
1328
0
    if ((cur != NULL) && (xmlStrEqual(cur->name, BAD_CAST "catalog")) &&
1329
0
  (cur->ns != NULL) && (cur->ns->href != NULL) &&
1330
0
  (xmlStrEqual(cur->ns->href, XML_CATALOGS_NAMESPACE))) {
1331
1332
0
  parent = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
1333
0
            (const xmlChar *)filename, NULL, prefer, NULL);
1334
0
        if (parent == NULL) {
1335
0
      xmlFreeDoc(doc);
1336
0
      return(NULL);
1337
0
  }
1338
1339
0
  prop = xmlGetProp(cur, BAD_CAST "prefer");
1340
0
  if (prop != NULL) {
1341
0
      if (xmlStrEqual(prop, BAD_CAST "system")) {
1342
0
    prefer = XML_CATA_PREFER_SYSTEM;
1343
0
      } else if (xmlStrEqual(prop, BAD_CAST "public")) {
1344
0
    prefer = XML_CATA_PREFER_PUBLIC;
1345
0
      } else {
1346
0
    xmlCatalogErr(NULL, cur, XML_CATALOG_PREFER_VALUE,
1347
0
            "Invalid value for prefer: '%s'\n",
1348
0
            prop, NULL, NULL);
1349
0
      }
1350
0
      xmlFree(prop);
1351
0
  }
1352
0
  cur = cur->children;
1353
0
  xmlParseXMLCatalogNodeList(cur, prefer, parent, NULL);
1354
0
    } else {
1355
0
  xmlCatalogErr(NULL, (xmlNodePtr) doc, XML_CATALOG_NOT_CATALOG,
1356
0
          "File %s is not an XML Catalog\n",
1357
0
          filename, NULL, NULL);
1358
0
  xmlFreeDoc(doc);
1359
0
  return(NULL);
1360
0
    }
1361
0
    xmlFreeDoc(doc);
1362
0
    return(parent);
1363
0
}
1364
1365
/**
1366
 * Fetch and parse the subcatalog referenced by an entry
1367
 *
1368
 * @param catal  an existing but incomplete catalog entry
1369
 * @returns 0 in case of success, -1 otherwise
1370
 */
1371
static int
1372
0
xmlFetchXMLCatalogFile(xmlCatalogEntryPtr catal) {
1373
0
    xmlCatalogEntryPtr doc;
1374
1375
0
    if (catal == NULL)
1376
0
  return(-1);
1377
0
    if (catal->URL == NULL)
1378
0
  return(-1);
1379
1380
    /*
1381
     * lock the whole catalog for modification
1382
     */
1383
0
    xmlRMutexLock(&xmlCatalogMutex);
1384
0
    if (catal->children != NULL) {
1385
  /* Okay someone else did it in the meantime */
1386
0
  xmlRMutexUnlock(&xmlCatalogMutex);
1387
0
  return(0);
1388
0
    }
1389
1390
0
    if (xmlCatalogXMLFiles != NULL) {
1391
0
  doc = (xmlCatalogEntryPtr)
1392
0
      xmlHashLookup(xmlCatalogXMLFiles, catal->URL);
1393
0
  if (doc != NULL) {
1394
0
      if (xmlDebugCatalogs)
1395
0
    xmlCatalogPrintDebug(
1396
0
        "Found %s in file hash\n", catal->URL);
1397
1398
0
      if (catal->type == XML_CATA_CATALOG)
1399
0
    catal->children = doc->children;
1400
0
      else
1401
0
    catal->children = doc;
1402
0
      catal->dealloc = 0;
1403
0
      xmlRMutexUnlock(&xmlCatalogMutex);
1404
0
      return(0);
1405
0
  }
1406
0
  if (xmlDebugCatalogs)
1407
0
      xmlCatalogPrintDebug(
1408
0
    "%s not found in file hash\n", catal->URL);
1409
0
    }
1410
1411
    /*
1412
     * Fetch and parse. Note that xmlParseXMLCatalogFile does not
1413
     * use the existing catalog, there is no recursion allowed at
1414
     * that level.
1415
     */
1416
0
    doc = xmlParseXMLCatalogFile(catal->prefer, catal->URL);
1417
0
    if (doc == NULL) {
1418
0
  catal->type = XML_CATA_BROKEN_CATALOG;
1419
0
  xmlRMutexUnlock(&xmlCatalogMutex);
1420
0
  return(-1);
1421
0
    }
1422
1423
0
    if (catal->type == XML_CATA_CATALOG)
1424
0
  catal->children = doc->children;
1425
0
    else
1426
0
  catal->children = doc;
1427
1428
0
    doc->dealloc = 1;
1429
1430
0
    if (xmlCatalogXMLFiles == NULL)
1431
0
  xmlCatalogXMLFiles = xmlHashCreate(10);
1432
0
    if (xmlCatalogXMLFiles != NULL) {
1433
0
  if (xmlDebugCatalogs)
1434
0
      xmlCatalogPrintDebug(
1435
0
    "%s added to file hash\n", catal->URL);
1436
0
  xmlHashAddEntry(xmlCatalogXMLFiles, catal->URL, doc);
1437
0
    }
1438
0
    xmlRMutexUnlock(&xmlCatalogMutex);
1439
0
    return(0);
1440
0
}
1441
1442
/************************************************************************
1443
 *                  *
1444
 *      XML Catalog handling        *
1445
 *                  *
1446
 ************************************************************************/
1447
1448
/**
1449
 * Add an entry in the XML catalog, it may overwrite existing but
1450
 * different entries.
1451
 *
1452
 * @param catal  top of an XML catalog
1453
 * @param type  the type of record to add to the catalog
1454
 * @param orig  the system, public or prefix to match (or NULL)
1455
 * @param replace  the replacement value for the match
1456
 * @returns 0 if successful, -1 otherwise
1457
 */
1458
static int
1459
xmlAddXMLCatalog(xmlCatalogEntryPtr catal, const xmlChar *type,
1460
0
        const xmlChar *orig, const xmlChar *replace) {
1461
0
    xmlCatalogEntryPtr cur;
1462
0
    xmlCatalogEntryType typ;
1463
0
    int doregister = 0;
1464
1465
0
    if ((catal == NULL) ||
1466
0
  ((catal->type != XML_CATA_CATALOG) &&
1467
0
   (catal->type != XML_CATA_BROKEN_CATALOG)))
1468
0
  return(-1);
1469
0
    if (catal->children == NULL) {
1470
0
  xmlFetchXMLCatalogFile(catal);
1471
0
    }
1472
0
    if (catal->children == NULL)
1473
0
  doregister = 1;
1474
1475
0
    typ = xmlGetXMLCatalogEntryType(type);
1476
0
    if (typ == XML_CATA_NONE) {
1477
0
  if (xmlDebugCatalogs)
1478
0
      xmlCatalogPrintDebug(
1479
0
        "Failed to add unknown element %s to catalog\n", type);
1480
0
  return(-1);
1481
0
    }
1482
1483
0
    cur = catal->children;
1484
    /*
1485
     * Might be a simple "update in place"
1486
     */
1487
0
    if (cur != NULL) {
1488
0
  while (cur != NULL) {
1489
0
      if ((orig != NULL) && (cur->type == typ) &&
1490
0
    (xmlStrEqual(orig, cur->name))) {
1491
0
    if (xmlDebugCatalogs)
1492
0
        xmlCatalogPrintDebug(
1493
0
          "Updating element %s to catalog\n", type);
1494
0
    if (cur->value != NULL)
1495
0
        xmlFree(cur->value);
1496
0
    if (cur->URL != NULL)
1497
0
        xmlFree(cur->URL);
1498
0
    cur->value = xmlStrdup(replace);
1499
0
    cur->URL = xmlStrdup(replace);
1500
0
    return(0);
1501
0
      }
1502
0
      if (cur->next == NULL)
1503
0
    break;
1504
0
      cur = cur->next;
1505
0
  }
1506
0
    }
1507
0
    if (xmlDebugCatalogs)
1508
0
  xmlCatalogPrintDebug(
1509
0
    "Adding element %s to catalog\n", type);
1510
0
    if (cur == NULL)
1511
0
  catal->children = xmlNewCatalogEntry(typ, orig, replace,
1512
0
                                 NULL, catal->prefer, NULL);
1513
0
    else
1514
0
  cur->next = xmlNewCatalogEntry(typ, orig, replace,
1515
0
                           NULL, catal->prefer, NULL);
1516
0
    if (doregister) {
1517
0
        catal->type = XML_CATA_CATALOG;
1518
0
  cur = (xmlCatalogEntryPtr)xmlHashLookup(xmlCatalogXMLFiles, catal->URL);
1519
0
  if (cur != NULL)
1520
0
      cur->children = catal->children;
1521
0
    }
1522
1523
0
    return(0);
1524
0
}
1525
1526
/**
1527
 * Remove entries in the XML catalog where the value or the URI
1528
 * is equal to `value`
1529
 *
1530
 * @param catal  top of an XML catalog
1531
 * @param value  the value to remove from the catalog
1532
 * @returns the number of entries removed if successful, -1 otherwise
1533
 */
1534
static int
1535
0
xmlDelXMLCatalog(xmlCatalogEntryPtr catal, const xmlChar *value) {
1536
0
    xmlCatalogEntryPtr cur;
1537
0
    int ret = 0;
1538
1539
0
    if ((catal == NULL) ||
1540
0
  ((catal->type != XML_CATA_CATALOG) &&
1541
0
   (catal->type != XML_CATA_BROKEN_CATALOG)))
1542
0
  return(-1);
1543
0
    if (value == NULL)
1544
0
  return(-1);
1545
0
    if (catal->children == NULL) {
1546
0
  xmlFetchXMLCatalogFile(catal);
1547
0
    }
1548
1549
    /*
1550
     * Scan the children
1551
     */
1552
0
    cur = catal->children;
1553
0
    while (cur != NULL) {
1554
0
  if (((cur->name != NULL) && (xmlStrEqual(value, cur->name))) ||
1555
0
      (xmlStrEqual(value, cur->value))) {
1556
0
      if (xmlDebugCatalogs) {
1557
0
    if (cur->name != NULL)
1558
0
        xmlCatalogPrintDebug(
1559
0
          "Removing element %s from catalog\n", cur->name);
1560
0
    else
1561
0
        xmlCatalogPrintDebug(
1562
0
          "Removing element %s from catalog\n", cur->value);
1563
0
      }
1564
0
      cur->type = XML_CATA_REMOVED;
1565
0
  }
1566
0
  cur = cur->next;
1567
0
    }
1568
0
    return(ret);
1569
0
}
1570
1571
/**
1572
 * Do a complete resolution lookup of an External Identifier for a
1573
 * list of catalog entries.
1574
 *
1575
 * Implements (or tries to) 7.1. External Identifier Resolution
1576
 * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
1577
 *
1578
 * @param catal  a catalog list
1579
 * @param pubID  the public ID string
1580
 * @param sysID  the system ID string
1581
 * @returns the URI of the resource or NULL if not found
1582
 */
1583
static xmlChar *
1584
xmlCatalogXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID,
1585
0
                const xmlChar *sysID) {
1586
0
    xmlChar *ret = NULL;
1587
0
    xmlCatalogEntryPtr cur;
1588
0
    int haveDelegate = 0;
1589
0
    int haveNext = 0;
1590
1591
    /*
1592
     * protection against loops
1593
     */
1594
0
    if (catal->depth > MAX_CATAL_DEPTH) {
1595
0
  xmlCatalogErr(catal, NULL, XML_CATALOG_RECURSION,
1596
0
          "Detected recursion in catalog %s\n",
1597
0
          catal->name, NULL, NULL);
1598
0
  return(NULL);
1599
0
    }
1600
0
    catal->depth++;
1601
1602
    /*
1603
     * First tries steps 2/ 3/ 4/ if a system ID is provided.
1604
     */
1605
0
    if (sysID != NULL) {
1606
0
  xmlCatalogEntryPtr rewrite = NULL;
1607
0
  int lenrewrite = 0, len;
1608
0
  cur = catal;
1609
0
  haveDelegate = 0;
1610
0
  while (cur != NULL) {
1611
0
      switch (cur->type) {
1612
0
    case XML_CATA_SYSTEM:
1613
0
        if (xmlStrEqual(sysID, cur->name)) {
1614
0
      if (xmlDebugCatalogs)
1615
0
          xmlCatalogPrintDebug(
1616
0
            "Found system match %s, using %s\n",
1617
0
                    cur->name, cur->URL);
1618
0
      catal->depth--;
1619
0
      return(xmlStrdup(cur->URL));
1620
0
        }
1621
0
        break;
1622
0
    case XML_CATA_REWRITE_SYSTEM:
1623
0
        len = xmlStrlen(cur->name);
1624
0
        if ((len > lenrewrite) &&
1625
0
      (!xmlStrncmp(sysID, cur->name, len))) {
1626
0
      lenrewrite = len;
1627
0
      rewrite = cur;
1628
0
        }
1629
0
        break;
1630
0
    case XML_CATA_DELEGATE_SYSTEM:
1631
0
        if (!xmlStrncmp(sysID, cur->name, xmlStrlen(cur->name)))
1632
0
      haveDelegate++;
1633
0
        break;
1634
0
    case XML_CATA_NEXT_CATALOG:
1635
0
        haveNext++;
1636
0
        break;
1637
0
    default:
1638
0
        break;
1639
0
      }
1640
0
      cur = cur->next;
1641
0
  }
1642
0
  if (rewrite != NULL) {
1643
0
      if (xmlDebugCatalogs)
1644
0
    xmlCatalogPrintDebug(
1645
0
      "Using rewriting rule %s\n", rewrite->name);
1646
0
      ret = xmlStrdup(rewrite->URL);
1647
0
      if (ret != NULL)
1648
0
    ret = xmlStrcat(ret, &sysID[lenrewrite]);
1649
0
      catal->depth--;
1650
0
      return(ret);
1651
0
  }
1652
0
  if (haveDelegate) {
1653
0
      const xmlChar *delegates[MAX_DELEGATE];
1654
0
      int nbList = 0, i;
1655
1656
      /*
1657
       * Assume the entries have been sorted by decreasing substring
1658
       * matches when the list was produced.
1659
       */
1660
0
      cur = catal;
1661
0
      while (cur != NULL) {
1662
0
    if ((cur->type == XML_CATA_DELEGATE_SYSTEM) &&
1663
0
        (!xmlStrncmp(sysID, cur->name, xmlStrlen(cur->name)))) {
1664
0
        for (i = 0;i < nbList;i++)
1665
0
      if (xmlStrEqual(cur->URL, delegates[i]))
1666
0
          break;
1667
0
        if (i < nbList) {
1668
0
      cur = cur->next;
1669
0
      continue;
1670
0
        }
1671
0
        if (nbList < MAX_DELEGATE)
1672
0
      delegates[nbList++] = cur->URL;
1673
1674
0
        if (cur->children == NULL) {
1675
0
      xmlFetchXMLCatalogFile(cur);
1676
0
        }
1677
0
        if (cur->children != NULL) {
1678
0
      if (xmlDebugCatalogs)
1679
0
          xmlCatalogPrintDebug(
1680
0
            "Trying system delegate %s\n", cur->URL);
1681
0
      ret = xmlCatalogListXMLResolve(
1682
0
        cur->children, NULL, sysID);
1683
0
      if (ret != NULL) {
1684
0
          catal->depth--;
1685
0
          return(ret);
1686
0
      }
1687
0
        }
1688
0
    }
1689
0
    cur = cur->next;
1690
0
      }
1691
      /*
1692
       * Apply the cut algorithm explained in 4/
1693
       */
1694
0
      catal->depth--;
1695
0
      return(XML_CATAL_BREAK);
1696
0
  }
1697
0
    }
1698
    /*
1699
     * Then tries 5/ 6/ if a public ID is provided
1700
     */
1701
0
    if (pubID != NULL) {
1702
0
  cur = catal;
1703
0
  haveDelegate = 0;
1704
0
  while (cur != NULL) {
1705
0
      switch (cur->type) {
1706
0
    case XML_CATA_PUBLIC:
1707
0
        if (xmlStrEqual(pubID, cur->name)) {
1708
0
      if (xmlDebugCatalogs)
1709
0
          xmlCatalogPrintDebug(
1710
0
            "Found public match %s\n", cur->name);
1711
0
      catal->depth--;
1712
0
      return(xmlStrdup(cur->URL));
1713
0
        }
1714
0
        break;
1715
0
    case XML_CATA_DELEGATE_PUBLIC:
1716
0
        if (!xmlStrncmp(pubID, cur->name, xmlStrlen(cur->name)) &&
1717
0
      (cur->prefer == XML_CATA_PREFER_PUBLIC))
1718
0
      haveDelegate++;
1719
0
        break;
1720
0
    case XML_CATA_NEXT_CATALOG:
1721
0
        if (sysID == NULL)
1722
0
      haveNext++;
1723
0
        break;
1724
0
    default:
1725
0
        break;
1726
0
      }
1727
0
      cur = cur->next;
1728
0
  }
1729
0
  if (haveDelegate) {
1730
0
      const xmlChar *delegates[MAX_DELEGATE];
1731
0
      int nbList = 0, i;
1732
1733
      /*
1734
       * Assume the entries have been sorted by decreasing substring
1735
       * matches when the list was produced.
1736
       */
1737
0
      cur = catal;
1738
0
      while (cur != NULL) {
1739
0
    if ((cur->type == XML_CATA_DELEGATE_PUBLIC) &&
1740
0
        (cur->prefer == XML_CATA_PREFER_PUBLIC) &&
1741
0
        (!xmlStrncmp(pubID, cur->name, xmlStrlen(cur->name)))) {
1742
1743
0
        for (i = 0;i < nbList;i++)
1744
0
      if (xmlStrEqual(cur->URL, delegates[i]))
1745
0
          break;
1746
0
        if (i < nbList) {
1747
0
      cur = cur->next;
1748
0
      continue;
1749
0
        }
1750
0
        if (nbList < MAX_DELEGATE)
1751
0
      delegates[nbList++] = cur->URL;
1752
1753
0
        if (cur->children == NULL) {
1754
0
      xmlFetchXMLCatalogFile(cur);
1755
0
        }
1756
0
        if (cur->children != NULL) {
1757
0
      if (xmlDebugCatalogs)
1758
0
          xmlCatalogPrintDebug(
1759
0
            "Trying public delegate %s\n", cur->URL);
1760
0
      ret = xmlCatalogListXMLResolve(
1761
0
        cur->children, pubID, NULL);
1762
0
      if (ret != NULL) {
1763
0
          catal->depth--;
1764
0
          return(ret);
1765
0
      }
1766
0
        }
1767
0
    }
1768
0
    cur = cur->next;
1769
0
      }
1770
      /*
1771
       * Apply the cut algorithm explained in 4/
1772
       */
1773
0
      catal->depth--;
1774
0
      return(XML_CATAL_BREAK);
1775
0
  }
1776
0
    }
1777
0
    if (haveNext) {
1778
0
  cur = catal;
1779
0
  while (cur != NULL) {
1780
0
      if (cur->type == XML_CATA_NEXT_CATALOG) {
1781
0
    if (cur->children == NULL) {
1782
0
        xmlFetchXMLCatalogFile(cur);
1783
0
    }
1784
0
    if (cur->children != NULL) {
1785
0
        ret = xmlCatalogListXMLResolve(cur->children, pubID, sysID);
1786
0
        if (ret != NULL) {
1787
0
      catal->depth--;
1788
0
      return(ret);
1789
0
        } else if (catal->depth > MAX_CATAL_DEPTH) {
1790
0
            return(NULL);
1791
0
        }
1792
0
    }
1793
0
      }
1794
0
      cur = cur->next;
1795
0
  }
1796
0
    }
1797
1798
0
    catal->depth--;
1799
0
    return(NULL);
1800
0
}
1801
1802
/**
1803
 * Do a complete resolution lookup of an External Identifier for a
1804
 * list of catalog entries.
1805
 *
1806
 * Implements (or tries to) 7.2.2. URI Resolution
1807
 * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
1808
 *
1809
 * @param catal  a catalog list
1810
 * @param URI  the URI
1811
 * @returns the URI of the resource or NULL if not found
1812
 */
1813
static xmlChar *
1814
0
xmlCatalogXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI) {
1815
0
    xmlChar *ret = NULL;
1816
0
    xmlCatalogEntryPtr cur;
1817
0
    int haveDelegate = 0;
1818
0
    int haveNext = 0;
1819
0
    xmlCatalogEntryPtr rewrite = NULL;
1820
0
    int lenrewrite = 0, len;
1821
1822
0
    if (catal == NULL)
1823
0
  return(NULL);
1824
1825
0
    if (URI == NULL)
1826
0
  return(NULL);
1827
1828
0
    if (catal->depth > MAX_CATAL_DEPTH) {
1829
0
  xmlCatalogErr(catal, NULL, XML_CATALOG_RECURSION,
1830
0
          "Detected recursion in catalog %s\n",
1831
0
          catal->name, NULL, NULL);
1832
0
  return(NULL);
1833
0
    }
1834
1835
    /*
1836
     * First tries steps 2/ 3/ 4/ if a system ID is provided.
1837
     */
1838
0
    cur = catal;
1839
0
    haveDelegate = 0;
1840
0
    while (cur != NULL) {
1841
0
  switch (cur->type) {
1842
0
      case XML_CATA_URI:
1843
0
    if (xmlStrEqual(URI, cur->name)) {
1844
0
        if (xmlDebugCatalogs)
1845
0
      xmlCatalogPrintDebug(
1846
0
        "Found URI match %s\n", cur->name);
1847
0
        return(xmlStrdup(cur->URL));
1848
0
    }
1849
0
    break;
1850
0
      case XML_CATA_REWRITE_URI:
1851
0
    len = xmlStrlen(cur->name);
1852
0
    if ((len > lenrewrite) &&
1853
0
        (!xmlStrncmp(URI, cur->name, len))) {
1854
0
        lenrewrite = len;
1855
0
        rewrite = cur;
1856
0
    }
1857
0
    break;
1858
0
      case XML_CATA_DELEGATE_URI:
1859
0
    if (!xmlStrncmp(URI, cur->name, xmlStrlen(cur->name)))
1860
0
        haveDelegate++;
1861
0
    break;
1862
0
      case XML_CATA_NEXT_CATALOG:
1863
0
    haveNext++;
1864
0
    break;
1865
0
      default:
1866
0
    break;
1867
0
  }
1868
0
  cur = cur->next;
1869
0
    }
1870
0
    if (rewrite != NULL) {
1871
0
  if (xmlDebugCatalogs)
1872
0
      xmlCatalogPrintDebug(
1873
0
        "Using rewriting rule %s\n", rewrite->name);
1874
0
  ret = xmlStrdup(rewrite->URL);
1875
0
  if (ret != NULL)
1876
0
      ret = xmlStrcat(ret, &URI[lenrewrite]);
1877
0
  return(ret);
1878
0
    }
1879
0
    if (haveDelegate) {
1880
0
  const xmlChar *delegates[MAX_DELEGATE];
1881
0
  int nbList = 0, i;
1882
1883
  /*
1884
   * Assume the entries have been sorted by decreasing substring
1885
   * matches when the list was produced.
1886
   */
1887
0
  cur = catal;
1888
0
  while (cur != NULL) {
1889
0
      if (((cur->type == XML_CATA_DELEGATE_SYSTEM) ||
1890
0
           (cur->type == XML_CATA_DELEGATE_URI)) &&
1891
0
    (!xmlStrncmp(URI, cur->name, xmlStrlen(cur->name)))) {
1892
0
    for (i = 0;i < nbList;i++)
1893
0
        if (xmlStrEqual(cur->URL, delegates[i]))
1894
0
      break;
1895
0
    if (i < nbList) {
1896
0
        cur = cur->next;
1897
0
        continue;
1898
0
    }
1899
0
    if (nbList < MAX_DELEGATE)
1900
0
        delegates[nbList++] = cur->URL;
1901
1902
0
    if (cur->children == NULL) {
1903
0
        xmlFetchXMLCatalogFile(cur);
1904
0
    }
1905
0
    if (cur->children != NULL) {
1906
0
        if (xmlDebugCatalogs)
1907
0
      xmlCatalogPrintDebug(
1908
0
        "Trying URI delegate %s\n", cur->URL);
1909
0
        ret = xmlCatalogListXMLResolveURI(
1910
0
          cur->children, URI);
1911
0
        if (ret != NULL)
1912
0
      return(ret);
1913
0
    }
1914
0
      }
1915
0
      cur = cur->next;
1916
0
  }
1917
  /*
1918
   * Apply the cut algorithm explained in 4/
1919
   */
1920
0
  return(XML_CATAL_BREAK);
1921
0
    }
1922
0
    if (haveNext) {
1923
0
  cur = catal;
1924
0
  while (cur != NULL) {
1925
0
      if (cur->type == XML_CATA_NEXT_CATALOG) {
1926
0
    if (cur->children == NULL) {
1927
0
        xmlFetchXMLCatalogFile(cur);
1928
0
    }
1929
0
    if (cur->children != NULL) {
1930
0
        ret = xmlCatalogListXMLResolveURI(cur->children, URI);
1931
0
        if (ret != NULL)
1932
0
      return(ret);
1933
0
    }
1934
0
      }
1935
0
      cur = cur->next;
1936
0
  }
1937
0
    }
1938
1939
0
    return(NULL);
1940
0
}
1941
1942
/**
1943
 * Do a complete resolution lookup of an External Identifier for a
1944
 * list of catalogs
1945
 *
1946
 * Implements (or tries to) 7.1. External Identifier Resolution
1947
 * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
1948
 *
1949
 * @param catal  a catalog list
1950
 * @param pubID  the public ID string
1951
 * @param sysID  the system ID string
1952
 * @returns the URI of the resource or NULL if not found
1953
 */
1954
static xmlChar *
1955
xmlCatalogListXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID,
1956
0
                const xmlChar *sysID) {
1957
0
    xmlChar *ret = NULL;
1958
0
    xmlChar *urnID = NULL;
1959
0
    xmlChar *normid;
1960
1961
0
    if (catal == NULL)
1962
0
        return(NULL);
1963
0
    if ((pubID == NULL) && (sysID == NULL))
1964
0
  return(NULL);
1965
1966
0
    normid = xmlCatalogNormalizePublic(pubID);
1967
0
    if (normid != NULL)
1968
0
        pubID = (*normid != 0 ? normid : NULL);
1969
1970
0
    if (!xmlStrncmp(pubID, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1)) {
1971
0
  urnID = xmlCatalogUnWrapURN(pubID);
1972
0
  if (xmlDebugCatalogs) {
1973
0
      if (urnID == NULL)
1974
0
    xmlCatalogPrintDebug(
1975
0
      "Public URN ID %s expanded to NULL\n", pubID);
1976
0
      else
1977
0
    xmlCatalogPrintDebug(
1978
0
      "Public URN ID expanded to %s\n", urnID);
1979
0
  }
1980
0
  ret = xmlCatalogListXMLResolve(catal, urnID, sysID);
1981
0
  if (urnID != NULL)
1982
0
      xmlFree(urnID);
1983
0
  if (normid != NULL)
1984
0
      xmlFree(normid);
1985
0
  return(ret);
1986
0
    }
1987
0
    if (!xmlStrncmp(sysID, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1)) {
1988
0
  urnID = xmlCatalogUnWrapURN(sysID);
1989
0
  if (xmlDebugCatalogs) {
1990
0
      if (urnID == NULL)
1991
0
    xmlCatalogPrintDebug(
1992
0
      "System URN ID %s expanded to NULL\n", sysID);
1993
0
      else
1994
0
    xmlCatalogPrintDebug(
1995
0
      "System URN ID expanded to %s\n", urnID);
1996
0
  }
1997
0
  if (pubID == NULL)
1998
0
      ret = xmlCatalogListXMLResolve(catal, urnID, NULL);
1999
0
  else if (xmlStrEqual(pubID, urnID))
2000
0
      ret = xmlCatalogListXMLResolve(catal, pubID, NULL);
2001
0
  else {
2002
0
      ret = xmlCatalogListXMLResolve(catal, pubID, urnID);
2003
0
  }
2004
0
  if (urnID != NULL)
2005
0
      xmlFree(urnID);
2006
0
  if (normid != NULL)
2007
0
      xmlFree(normid);
2008
0
  return(ret);
2009
0
    }
2010
0
    while (catal != NULL) {
2011
0
  if (catal->type == XML_CATA_CATALOG) {
2012
0
      if (catal->children == NULL) {
2013
0
    xmlFetchXMLCatalogFile(catal);
2014
0
      }
2015
0
      if (catal->children != NULL) {
2016
0
    ret = xmlCatalogXMLResolve(catal->children, pubID, sysID);
2017
0
    if (ret != NULL) {
2018
0
        break;
2019
0
                } else if (catal->children->depth > MAX_CATAL_DEPTH) {
2020
0
              ret = NULL;
2021
0
        break;
2022
0
          }
2023
0
      }
2024
0
  }
2025
0
  catal = catal->next;
2026
0
    }
2027
0
    if (normid != NULL)
2028
0
  xmlFree(normid);
2029
0
    return(ret);
2030
0
}
2031
2032
/**
2033
 * Do a complete resolution lookup of an URI for a list of catalogs
2034
 *
2035
 * Implements (or tries to) 7.2. URI Resolution
2036
 * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
2037
 *
2038
 * @param catal  a catalog list
2039
 * @param URI  the URI
2040
 * @returns the URI of the resource or NULL if not found
2041
 */
2042
static xmlChar *
2043
0
xmlCatalogListXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI) {
2044
0
    xmlChar *ret = NULL;
2045
0
    xmlChar *urnID = NULL;
2046
0
    xmlCatalogEntryPtr cur = NULL;
2047
2048
0
    if (catal == NULL)
2049
0
        return(NULL);
2050
0
    if (URI == NULL)
2051
0
  return(NULL);
2052
2053
0
    if (catal->depth > MAX_CATAL_DEPTH) {
2054
0
  xmlCatalogErr(catal, NULL, XML_CATALOG_RECURSION,
2055
0
          "Detected recursion in catalog %s\n",
2056
0
          catal->name, NULL, NULL);
2057
0
  return(NULL);
2058
0
    }
2059
0
    catal->depth++;
2060
2061
0
    if (!xmlStrncmp(URI, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1)) {
2062
0
  urnID = xmlCatalogUnWrapURN(URI);
2063
0
  if (xmlDebugCatalogs) {
2064
0
      if (urnID == NULL)
2065
0
    xmlCatalogPrintDebug(
2066
0
      "URN ID %s expanded to NULL\n", URI);
2067
0
      else
2068
0
    xmlCatalogPrintDebug(
2069
0
      "URN ID expanded to %s\n", urnID);
2070
0
  }
2071
0
  ret = xmlCatalogListXMLResolve(catal, urnID, NULL);
2072
0
  if (urnID != NULL)
2073
0
      xmlFree(urnID);
2074
0
  catal->depth--;
2075
0
  return(ret);
2076
0
    }
2077
0
    cur = catal;
2078
0
    while (cur != NULL) {
2079
0
  if (cur->type == XML_CATA_CATALOG) {
2080
0
      if (cur->children == NULL) {
2081
0
    xmlFetchXMLCatalogFile(cur);
2082
0
      }
2083
0
      if (cur->children != NULL) {
2084
0
    ret = xmlCatalogXMLResolveURI(cur->children, URI);
2085
0
    if (ret != NULL) {
2086
0
        catal->depth--;
2087
0
        return(ret);
2088
0
    }
2089
0
      }
2090
0
  }
2091
0
  cur = cur->next;
2092
0
    }
2093
2094
0
    catal->depth--;
2095
0
    return(ret);
2096
0
}
2097
2098
#ifdef LIBXML_SGML_CATALOG_ENABLED
2099
2100
/************************************************************************
2101
 *                  *
2102
 *      The SGML Catalog parser       *
2103
 *                  *
2104
 ************************************************************************/
2105
2106
2107
0
#define RAW *cur
2108
0
#define NEXT cur++;
2109
0
#define SKIP(x) cur += x;
2110
2111
0
#define SKIP_BLANKS while (IS_BLANK_CH(*cur)) NEXT;
2112
2113
/**
2114
 * Skip a comment in an SGML catalog
2115
 *
2116
 * @param cur  the current character
2117
 * @returns new current character
2118
 */
2119
static const xmlChar *
2120
0
xmlParseSGMLCatalogComment(const xmlChar *cur) {
2121
0
    if ((cur[0] != '-') || (cur[1] != '-'))
2122
0
  return(cur);
2123
0
    SKIP(2);
2124
0
    while ((cur[0] != 0) && ((cur[0] != '-') || ((cur[1] != '-'))))
2125
0
  NEXT;
2126
0
    if (cur[0] == 0) {
2127
0
  return(NULL);
2128
0
    }
2129
0
    return(cur + 2);
2130
0
}
2131
2132
/**
2133
 * Parse an SGML catalog ID
2134
 *
2135
 * @param cur  the current character
2136
 * @param id  the return location
2137
 * @returns new current character and store the value in `id`
2138
 */
2139
static const xmlChar *
2140
0
xmlParseSGMLCatalogPubid(const xmlChar *cur, xmlChar **id) {
2141
0
    xmlChar *buf = NULL;
2142
0
    int len = 0;
2143
0
    int size = 50;
2144
0
    xmlChar stop;
2145
2146
0
    *id = NULL;
2147
2148
0
    if (RAW == '"') {
2149
0
        NEXT;
2150
0
  stop = '"';
2151
0
    } else if (RAW == '\'') {
2152
0
        NEXT;
2153
0
  stop = '\'';
2154
0
    } else {
2155
0
  stop = ' ';
2156
0
    }
2157
0
    buf = xmlMalloc(size);
2158
0
    if (buf == NULL) {
2159
0
        xmlCatalogErrMemory();
2160
0
  return(NULL);
2161
0
    }
2162
0
    while (IS_PUBIDCHAR_CH(*cur) || (*cur == '?')) {
2163
0
  if ((*cur == stop) && (stop != ' '))
2164
0
      break;
2165
0
  if ((stop == ' ') && (IS_BLANK_CH(*cur)))
2166
0
      break;
2167
0
  if (len + 1 >= size) {
2168
0
            xmlChar *tmp;
2169
0
            int newSize;
2170
2171
0
            newSize = xmlGrowCapacity(size, 1, 1, XML_MAX_ITEMS);
2172
0
            if (newSize < 0) {
2173
0
    xmlCatalogErrMemory();
2174
0
    xmlFree(buf);
2175
0
    return(NULL);
2176
0
            }
2177
0
      tmp = xmlRealloc(buf, newSize);
2178
0
      if (tmp == NULL) {
2179
0
    xmlCatalogErrMemory();
2180
0
    xmlFree(buf);
2181
0
    return(NULL);
2182
0
      }
2183
0
      buf = tmp;
2184
0
            size = newSize;
2185
0
  }
2186
0
  buf[len++] = *cur;
2187
0
  NEXT;
2188
0
    }
2189
0
    buf[len] = 0;
2190
0
    if (stop == ' ') {
2191
0
  if (!IS_BLANK_CH(*cur)) {
2192
0
      xmlFree(buf);
2193
0
      return(NULL);
2194
0
  }
2195
0
    } else {
2196
0
  if (*cur != stop) {
2197
0
      xmlFree(buf);
2198
0
      return(NULL);
2199
0
  }
2200
0
  NEXT;
2201
0
    }
2202
0
    *id = buf;
2203
0
    return(cur);
2204
0
}
2205
2206
/**
2207
 * Parse an SGML catalog name
2208
 *
2209
 * @param cur  the current character
2210
 * @param name  the return location
2211
 * @returns new current character and store the value in `name`
2212
 */
2213
static const xmlChar *
2214
0
xmlParseSGMLCatalogName(const xmlChar *cur, xmlChar **name) {
2215
0
    xmlChar buf[XML_MAX_NAMELEN + 5];
2216
0
    int len = 0;
2217
0
    int c;
2218
2219
0
    *name = NULL;
2220
2221
    /*
2222
     * Handler for more complex cases
2223
     */
2224
0
    c = *cur;
2225
0
    if ((!IS_LETTER(c) && (c != '_') && (c != ':'))) {
2226
0
  return(NULL);
2227
0
    }
2228
2229
0
    while (((IS_LETTER(c)) || (IS_DIGIT(c)) ||
2230
0
            (c == '.') || (c == '-') ||
2231
0
      (c == '_') || (c == ':'))) {
2232
0
  buf[len++] = c;
2233
0
  cur++;
2234
0
  c = *cur;
2235
0
  if (len >= XML_MAX_NAMELEN)
2236
0
      return(NULL);
2237
0
    }
2238
0
    *name = xmlStrndup(buf, len);
2239
0
    return(cur);
2240
0
}
2241
2242
/**
2243
 * Get the Catalog entry type for a given SGML Catalog name
2244
 *
2245
 * @param name  the entry name
2246
 * @returns Catalog entry type
2247
 */
2248
static xmlCatalogEntryType
2249
0
xmlGetSGMLCatalogEntryType(const xmlChar *name) {
2250
0
    xmlCatalogEntryType type = XML_CATA_NONE;
2251
0
    if (xmlStrEqual(name, (const xmlChar *) "SYSTEM"))
2252
0
  type = SGML_CATA_SYSTEM;
2253
0
    else if (xmlStrEqual(name, (const xmlChar *) "PUBLIC"))
2254
0
  type = SGML_CATA_PUBLIC;
2255
0
    else if (xmlStrEqual(name, (const xmlChar *) "DELEGATE"))
2256
0
  type = SGML_CATA_DELEGATE;
2257
0
    else if (xmlStrEqual(name, (const xmlChar *) "ENTITY"))
2258
0
  type = SGML_CATA_ENTITY;
2259
0
    else if (xmlStrEqual(name, (const xmlChar *) "DOCTYPE"))
2260
0
  type = SGML_CATA_DOCTYPE;
2261
0
    else if (xmlStrEqual(name, (const xmlChar *) "LINKTYPE"))
2262
0
  type = SGML_CATA_LINKTYPE;
2263
0
    else if (xmlStrEqual(name, (const xmlChar *) "NOTATION"))
2264
0
  type = SGML_CATA_NOTATION;
2265
0
    else if (xmlStrEqual(name, (const xmlChar *) "SGMLDECL"))
2266
0
  type = SGML_CATA_SGMLDECL;
2267
0
    else if (xmlStrEqual(name, (const xmlChar *) "DOCUMENT"))
2268
0
  type = SGML_CATA_DOCUMENT;
2269
0
    else if (xmlStrEqual(name, (const xmlChar *) "CATALOG"))
2270
0
  type = SGML_CATA_CATALOG;
2271
0
    else if (xmlStrEqual(name, (const xmlChar *) "BASE"))
2272
0
  type = SGML_CATA_BASE;
2273
0
    return(type);
2274
0
}
2275
2276
/**
2277
 * Parse an SGML catalog content and fill up the `catal` hash table with
2278
 * the new entries found.
2279
 *
2280
 * @param catal  the SGML Catalog
2281
 * @param value  the content of the SGML Catalog serialization
2282
 * @param file  the filepath for the catalog
2283
 * @param super  should this be handled as a Super Catalog in which case
2284
 *          parsing is not recursive
2285
 * @param depth  the current depth of the catalog
2286
 * @returns 0 in case of success, -1 in case of error.
2287
 */
2288
static int
2289
xmlParseSGMLCatalog(xmlCatalogPtr catal, const xmlChar *value,
2290
0
              const char *file, int super, int depth) {
2291
0
    const xmlChar *cur = value;
2292
0
    xmlChar *base = NULL;
2293
0
    int res;
2294
2295
0
    if ((cur == NULL) || (file == NULL))
2296
0
        return(-1);
2297
2298
    /* Check recursion depth */
2299
0
    if (depth > MAX_CATAL_DEPTH) {
2300
0
        return(-1);
2301
0
    }
2302
2303
0
    base = xmlStrdup((const xmlChar *) file);
2304
2305
0
    while ((cur != NULL) && (cur[0] != 0)) {
2306
0
  SKIP_BLANKS;
2307
0
  if (cur[0] == 0)
2308
0
      break;
2309
0
  if ((cur[0] == '-') && (cur[1] == '-')) {
2310
0
      cur = xmlParseSGMLCatalogComment(cur);
2311
0
      if (cur == NULL) {
2312
    /* error */
2313
0
    break;
2314
0
      }
2315
0
  } else {
2316
0
      xmlChar *sysid = NULL;
2317
0
      xmlChar *name = NULL;
2318
0
      xmlCatalogEntryType type = XML_CATA_NONE;
2319
2320
0
      cur = xmlParseSGMLCatalogName(cur, &name);
2321
0
      if (cur == NULL || name == NULL) {
2322
    /* error */
2323
0
    break;
2324
0
      }
2325
0
      if (!IS_BLANK_CH(*cur)) {
2326
    /* error */
2327
0
    xmlFree(name);
2328
0
    break;
2329
0
      }
2330
0
      SKIP_BLANKS;
2331
0
      if (xmlStrEqual(name, (const xmlChar *) "SYSTEM"))
2332
0
                type = SGML_CATA_SYSTEM;
2333
0
      else if (xmlStrEqual(name, (const xmlChar *) "PUBLIC"))
2334
0
                type = SGML_CATA_PUBLIC;
2335
0
      else if (xmlStrEqual(name, (const xmlChar *) "DELEGATE"))
2336
0
                type = SGML_CATA_DELEGATE;
2337
0
      else if (xmlStrEqual(name, (const xmlChar *) "ENTITY"))
2338
0
                type = SGML_CATA_ENTITY;
2339
0
      else if (xmlStrEqual(name, (const xmlChar *) "DOCTYPE"))
2340
0
                type = SGML_CATA_DOCTYPE;
2341
0
      else if (xmlStrEqual(name, (const xmlChar *) "LINKTYPE"))
2342
0
                type = SGML_CATA_LINKTYPE;
2343
0
      else if (xmlStrEqual(name, (const xmlChar *) "NOTATION"))
2344
0
                type = SGML_CATA_NOTATION;
2345
0
      else if (xmlStrEqual(name, (const xmlChar *) "SGMLDECL"))
2346
0
                type = SGML_CATA_SGMLDECL;
2347
0
      else if (xmlStrEqual(name, (const xmlChar *) "DOCUMENT"))
2348
0
                type = SGML_CATA_DOCUMENT;
2349
0
      else if (xmlStrEqual(name, (const xmlChar *) "CATALOG"))
2350
0
                type = SGML_CATA_CATALOG;
2351
0
      else if (xmlStrEqual(name, (const xmlChar *) "BASE"))
2352
0
                type = SGML_CATA_BASE;
2353
0
      else if (xmlStrEqual(name, (const xmlChar *) "OVERRIDE")) {
2354
0
    xmlFree(name);
2355
0
    cur = xmlParseSGMLCatalogName(cur, &name);
2356
0
    if (name == NULL) {
2357
        /* error */
2358
0
        break;
2359
0
    }
2360
0
    xmlFree(name);
2361
0
    continue;
2362
0
      }
2363
0
      xmlFree(name);
2364
0
      name = NULL;
2365
2366
0
      switch(type) {
2367
0
    case SGML_CATA_ENTITY:
2368
0
        if (*cur == '%')
2369
0
      type = SGML_CATA_PENTITY;
2370
                    /* Falls through. */
2371
0
    case SGML_CATA_PENTITY:
2372
0
    case SGML_CATA_DOCTYPE:
2373
0
    case SGML_CATA_LINKTYPE:
2374
0
    case SGML_CATA_NOTATION:
2375
0
        cur = xmlParseSGMLCatalogName(cur, &name);
2376
0
        if (cur == NULL) {
2377
      /* error */
2378
0
      break;
2379
0
        }
2380
0
        if (!IS_BLANK_CH(*cur)) {
2381
      /* error */
2382
0
      break;
2383
0
        }
2384
0
        SKIP_BLANKS;
2385
0
        cur = xmlParseSGMLCatalogPubid(cur, &sysid);
2386
0
        if (cur == NULL) {
2387
      /* error */
2388
0
      break;
2389
0
        }
2390
0
        break;
2391
0
    case SGML_CATA_PUBLIC:
2392
0
    case SGML_CATA_SYSTEM:
2393
0
    case SGML_CATA_DELEGATE:
2394
0
        cur = xmlParseSGMLCatalogPubid(cur, &name);
2395
0
        if (cur == NULL) {
2396
      /* error */
2397
0
      break;
2398
0
        }
2399
0
        if (type != SGML_CATA_SYSTEM) {
2400
0
            xmlChar *normid;
2401
2402
0
            normid = xmlCatalogNormalizePublic(name);
2403
0
            if (normid != NULL) {
2404
0
                if (name != NULL)
2405
0
                    xmlFree(name);
2406
0
                if (*normid != 0)
2407
0
                    name = normid;
2408
0
                else {
2409
0
                    xmlFree(normid);
2410
0
                    name = NULL;
2411
0
                }
2412
0
            }
2413
0
        }
2414
0
        if (!IS_BLANK_CH(*cur)) {
2415
      /* error */
2416
0
      break;
2417
0
        }
2418
0
        SKIP_BLANKS;
2419
0
        cur = xmlParseSGMLCatalogPubid(cur, &sysid);
2420
0
        if (cur == NULL) {
2421
      /* error */
2422
0
      break;
2423
0
        }
2424
0
        break;
2425
0
    case SGML_CATA_BASE:
2426
0
    case SGML_CATA_CATALOG:
2427
0
    case SGML_CATA_DOCUMENT:
2428
0
    case SGML_CATA_SGMLDECL:
2429
0
        cur = xmlParseSGMLCatalogPubid(cur, &sysid);
2430
0
        if (cur == NULL) {
2431
      /* error */
2432
0
      break;
2433
0
        }
2434
0
        break;
2435
0
    default:
2436
0
        break;
2437
0
      }
2438
0
      if (cur == NULL) {
2439
0
    if (name != NULL)
2440
0
        xmlFree(name);
2441
0
    if (sysid != NULL)
2442
0
        xmlFree(sysid);
2443
0
    break;
2444
0
      } else if (type == SGML_CATA_BASE) {
2445
0
    if (base != NULL)
2446
0
        xmlFree(base);
2447
0
    base = xmlStrdup(sysid);
2448
0
      } else if ((type == SGML_CATA_PUBLIC) ||
2449
0
           (type == SGML_CATA_SYSTEM)) {
2450
0
    xmlChar *filename;
2451
2452
0
    filename = xmlBuildURI(sysid, base);
2453
0
    if (filename != NULL) {
2454
0
        xmlCatalogEntryPtr entry;
2455
2456
0
        entry = xmlNewCatalogEntry(type, name, filename,
2457
0
                             NULL, XML_CATA_PREFER_NONE, NULL);
2458
0
        res = xmlHashAddEntry(catal->sgml, name, entry);
2459
0
        if (res < 0) {
2460
0
      xmlFreeCatalogEntry(entry, NULL);
2461
0
        }
2462
0
        xmlFree(filename);
2463
0
    }
2464
2465
0
      } else if (type == SGML_CATA_CATALOG) {
2466
0
    if (super) {
2467
0
        xmlCatalogEntryPtr entry;
2468
2469
0
        entry = xmlNewCatalogEntry(type, sysid, NULL, NULL,
2470
0
                             XML_CATA_PREFER_NONE, NULL);
2471
0
        res = xmlHashAddEntry(catal->sgml, sysid, entry);
2472
0
        if (res < 0) {
2473
0
      xmlFreeCatalogEntry(entry, NULL);
2474
0
        }
2475
0
    } else {
2476
0
        xmlChar *filename;
2477
2478
0
        filename = xmlBuildURI(sysid, base);
2479
0
        if (filename != NULL) {
2480
0
      xmlExpandCatalog(catal, (const char *)filename, depth);
2481
0
      xmlFree(filename);
2482
0
        }
2483
0
    }
2484
0
      }
2485
      /*
2486
       * drop anything else we won't handle it
2487
       */
2488
0
      if (name != NULL)
2489
0
    xmlFree(name);
2490
0
      if (sysid != NULL)
2491
0
    xmlFree(sysid);
2492
0
  }
2493
0
    }
2494
0
    if (base != NULL)
2495
0
  xmlFree(base);
2496
0
    if (cur == NULL)
2497
0
  return(-1);
2498
0
    return(0);
2499
0
}
2500
2501
/************************************************************************
2502
 *                  *
2503
 *      SGML Catalog handling       *
2504
 *                  *
2505
 ************************************************************************/
2506
2507
/**
2508
 * Try to lookup the catalog local reference associated to a public ID
2509
 *
2510
 * @param catal  an SGML catalog hash
2511
 * @param pubID  the public ID string
2512
 * @returns the local resource if found or NULL otherwise.
2513
 */
2514
static const xmlChar *
2515
0
xmlCatalogGetSGMLPublic(xmlHashTablePtr catal, const xmlChar *pubID) {
2516
0
    xmlCatalogEntryPtr entry;
2517
0
    xmlChar *normid;
2518
2519
0
    if (catal == NULL)
2520
0
  return(NULL);
2521
2522
0
    normid = xmlCatalogNormalizePublic(pubID);
2523
0
    if (normid != NULL)
2524
0
        pubID = (*normid != 0 ? normid : NULL);
2525
2526
0
    entry = (xmlCatalogEntryPtr) xmlHashLookup(catal, pubID);
2527
0
    if (entry == NULL) {
2528
0
  if (normid != NULL)
2529
0
      xmlFree(normid);
2530
0
  return(NULL);
2531
0
    }
2532
0
    if (entry->type == SGML_CATA_PUBLIC) {
2533
0
  if (normid != NULL)
2534
0
      xmlFree(normid);
2535
0
  return(entry->URL);
2536
0
    }
2537
0
    if (normid != NULL)
2538
0
        xmlFree(normid);
2539
0
    return(NULL);
2540
0
}
2541
2542
/**
2543
 * Try to lookup the catalog local reference for a system ID
2544
 *
2545
 * @param catal  an SGML catalog hash
2546
 * @param sysID  the system ID string
2547
 * @returns the local resource if found or NULL otherwise.
2548
 */
2549
static const xmlChar *
2550
0
xmlCatalogGetSGMLSystem(xmlHashTablePtr catal, const xmlChar *sysID) {
2551
0
    xmlCatalogEntryPtr entry;
2552
2553
0
    if (catal == NULL)
2554
0
  return(NULL);
2555
2556
0
    entry = (xmlCatalogEntryPtr) xmlHashLookup(catal, sysID);
2557
0
    if (entry == NULL)
2558
0
  return(NULL);
2559
0
    if (entry->type == SGML_CATA_SYSTEM)
2560
0
  return(entry->URL);
2561
0
    return(NULL);
2562
0
}
2563
2564
/**
2565
 * Do a complete resolution lookup of an External Identifier
2566
 *
2567
 * @param catal  the SGML catalog
2568
 * @param pubID  the public ID string
2569
 * @param sysID  the system ID string
2570
 * @returns the URI of the resource or NULL if not found
2571
 */
2572
static const xmlChar *
2573
xmlCatalogSGMLResolve(xmlCatalogPtr catal, const xmlChar *pubID,
2574
0
                const xmlChar *sysID) {
2575
0
    const xmlChar *ret = NULL;
2576
2577
0
    if (catal->sgml == NULL)
2578
0
  return(NULL);
2579
2580
0
    if (pubID != NULL)
2581
0
  ret = xmlCatalogGetSGMLPublic(catal->sgml, pubID);
2582
0
    if (ret != NULL)
2583
0
  return(ret);
2584
0
    if (sysID != NULL)
2585
0
  ret = xmlCatalogGetSGMLSystem(catal->sgml, sysID);
2586
0
    if (ret != NULL)
2587
0
  return(ret);
2588
0
    return(NULL);
2589
0
}
2590
2591
#endif /* LIBXML_SGML_CATALOG_ENABLED */
2592
2593
/************************************************************************
2594
 *                  *
2595
 *      Specific Public interfaces      *
2596
 *                  *
2597
 ************************************************************************/
2598
2599
#ifdef LIBXML_SGML_CATALOG_ENABLED
2600
/**
2601
 * Load an SGML super catalog. It won't expand CATALOG or DELEGATE
2602
 * references. This is only needed for manipulating SGML Super Catalogs
2603
 * like adding and removing CATALOG or DELEGATE entries.
2604
 *
2605
 * @deprecated Internal function, don't use
2606
 *
2607
 * @param filename  a file path
2608
 * @returns the catalog parsed or NULL in case of error
2609
 */
2610
xmlCatalog *
2611
xmlLoadSGMLSuperCatalog(const char *filename)
2612
0
{
2613
0
    xmlChar *content;
2614
0
    xmlCatalogPtr catal;
2615
0
    int ret;
2616
2617
0
    content = xmlLoadFileContent(filename);
2618
0
    if (content == NULL)
2619
0
        return(NULL);
2620
2621
0
    catal = xmlCreateNewCatalog(XML_SGML_CATALOG_TYPE, xmlCatalogDefaultPrefer);
2622
0
    if (catal == NULL) {
2623
0
  xmlFree(content);
2624
0
  return(NULL);
2625
0
    }
2626
2627
0
    ret = xmlParseSGMLCatalog(catal, content, filename, 1, 0);
2628
0
    xmlFree(content);
2629
0
    if (ret < 0) {
2630
0
  xmlFreeCatalog(catal);
2631
0
  return(NULL);
2632
0
    }
2633
0
    return (catal);
2634
0
}
2635
#endif /* LIBXML_SGML_CATALOG_ENABLED */
2636
2637
/**
2638
 * Load the catalog and build the associated data structures.
2639
 * This can be either an XML Catalog or an SGML Catalog
2640
 * It will recurse in SGML CATALOG entries. On the other hand XML
2641
 * Catalogs are not handled recursively.
2642
 *
2643
 * @deprecated Internal function, don't use
2644
 *
2645
 * @param filename  a file path
2646
 * @returns the catalog parsed or NULL in case of error
2647
 */
2648
xmlCatalog *
2649
xmlLoadACatalog(const char *filename)
2650
0
{
2651
0
    xmlChar *content;
2652
0
    xmlChar *first;
2653
0
    xmlCatalogPtr catal;
2654
2655
0
    content = xmlLoadFileContent(filename);
2656
0
    if (content == NULL)
2657
0
        return(NULL);
2658
2659
2660
0
    first = content;
2661
2662
0
    while ((*first != 0) && (*first != '-') && (*first != '<') &&
2663
0
     (!(((*first >= 'A') && (*first <= 'Z')) ||
2664
0
        ((*first >= 'a') && (*first <= 'z')))))
2665
0
  first++;
2666
2667
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
2668
0
    if (*first != '<') {
2669
0
        int ret;
2670
2671
0
  catal = xmlCreateNewCatalog(XML_SGML_CATALOG_TYPE, xmlCatalogDefaultPrefer);
2672
0
  if (catal == NULL) {
2673
0
      xmlFree(content);
2674
0
      return(NULL);
2675
0
  }
2676
0
        ret = xmlParseSGMLCatalog(catal, content, filename, 0, 0);
2677
0
  if (ret < 0) {
2678
0
      xmlFreeCatalog(catal);
2679
0
      xmlFree(content);
2680
0
      return(NULL);
2681
0
  }
2682
0
    } else
2683
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
2684
0
    {
2685
0
  catal = xmlCreateNewCatalog(XML_XML_CATALOG_TYPE, xmlCatalogDefaultPrefer);
2686
0
  if (catal == NULL) {
2687
0
      xmlFree(content);
2688
0
      return(NULL);
2689
0
  }
2690
0
        catal->xml = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
2691
0
           NULL, BAD_CAST filename, xmlCatalogDefaultPrefer, NULL);
2692
0
    }
2693
0
    xmlFree(content);
2694
0
    return (catal);
2695
0
}
2696
2697
/**
2698
 * Load the catalog and expand the existing catal structure.
2699
 * This can be either an XML Catalog or an SGML Catalog
2700
 *
2701
 * @param catal  a catalog
2702
 * @param filename  a file path
2703
 * @param depth  the current depth of the catalog
2704
 * @returns 0 in case of success, -1 in case of error
2705
 */
2706
static int
2707
xmlExpandCatalog(xmlCatalogPtr catal, const char *filename, int depth)
2708
0
{
2709
0
    if ((catal == NULL) || (filename == NULL))
2710
0
  return(-1);
2711
2712
    /* Check recursion depth */
2713
0
    if (depth > MAX_CATAL_DEPTH) {
2714
0
  return(-1);
2715
0
    }
2716
2717
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
2718
0
    if (catal->type == XML_SGML_CATALOG_TYPE) {
2719
0
  xmlChar *content;
2720
0
        int ret;
2721
2722
0
  content = xmlLoadFileContent(filename);
2723
0
  if (content == NULL)
2724
0
      return(-1);
2725
2726
0
        ret = xmlParseSGMLCatalog(catal, content, filename, 0, depth + 1);
2727
0
  if (ret < 0) {
2728
0
      xmlFree(content);
2729
0
      return(-1);
2730
0
  }
2731
0
  xmlFree(content);
2732
0
    } else
2733
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
2734
0
    {
2735
0
  xmlCatalogEntryPtr tmp, cur;
2736
0
  tmp = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
2737
0
           NULL, BAD_CAST filename, xmlCatalogDefaultPrefer, NULL);
2738
2739
0
  cur = catal->xml;
2740
0
  if (cur == NULL) {
2741
0
      catal->xml = tmp;
2742
0
  } else {
2743
0
      while (cur->next != NULL) cur = cur->next;
2744
0
      cur->next = tmp;
2745
0
  }
2746
0
    }
2747
0
    return (0);
2748
0
}
2749
2750
/**
2751
 * Try to lookup the catalog resource for a system ID
2752
 *
2753
 * @deprecated Internal function, don't use
2754
 *
2755
 * @param catal  a Catalog
2756
 * @param sysID  the system ID string
2757
 * @returns the resource if found or NULL otherwise, the value returned
2758
 *      must be freed by the caller.
2759
 */
2760
xmlChar *
2761
0
xmlACatalogResolveSystem(xmlCatalog *catal, const xmlChar *sysID) {
2762
0
    xmlChar *ret = NULL;
2763
2764
0
    if ((sysID == NULL) || (catal == NULL))
2765
0
  return(NULL);
2766
2767
0
    if (xmlDebugCatalogs)
2768
0
  xmlCatalogPrintDebug(
2769
0
    "Resolve sysID %s\n", sysID);
2770
2771
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
2772
0
    if (catal->type == XML_SGML_CATALOG_TYPE) {
2773
0
  const xmlChar *sgml;
2774
2775
0
  sgml = xmlCatalogGetSGMLSystem(catal->sgml, sysID);
2776
0
  if (sgml != NULL)
2777
0
      ret = xmlStrdup(sgml);
2778
0
    } else
2779
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
2780
0
    {
2781
0
  ret = xmlCatalogListXMLResolve(catal->xml, NULL, sysID);
2782
0
  if (ret == XML_CATAL_BREAK)
2783
0
      ret = NULL;
2784
0
    }
2785
0
    return(ret);
2786
0
}
2787
2788
/**
2789
 * Try to lookup the catalog local reference associated to a public ID in that catalog
2790
 *
2791
 * @deprecated Internal function, don't use
2792
 *
2793
 * @param catal  a Catalog
2794
 * @param pubID  the public ID string
2795
 * @returns the local resource if found or NULL otherwise, the value returned
2796
 *      must be freed by the caller.
2797
 */
2798
xmlChar *
2799
0
xmlACatalogResolvePublic(xmlCatalog *catal, const xmlChar *pubID) {
2800
0
    xmlChar *ret = NULL;
2801
2802
0
    if ((pubID == NULL) || (catal == NULL))
2803
0
  return(NULL);
2804
2805
0
    if (xmlDebugCatalogs)
2806
0
  xmlCatalogPrintDebug(
2807
0
    "Resolve pubID %s\n", pubID);
2808
2809
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
2810
0
    if (catal->type == XML_SGML_CATALOG_TYPE) {
2811
0
  const xmlChar *sgml;
2812
2813
0
  sgml = xmlCatalogGetSGMLPublic(catal->sgml, pubID);
2814
0
  if (sgml != NULL)
2815
0
      ret = xmlStrdup(sgml);
2816
0
    } else
2817
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
2818
0
    {
2819
0
  ret = xmlCatalogListXMLResolve(catal->xml, pubID, NULL);
2820
0
  if (ret == XML_CATAL_BREAK)
2821
0
      ret = NULL;
2822
0
    }
2823
0
    return(ret);
2824
0
}
2825
2826
/**
2827
 * Do a complete resolution lookup of an External Identifier
2828
 *
2829
 * @deprecated Internal function, don't use
2830
 *
2831
 * @param catal  a Catalog
2832
 * @param pubID  the public ID string
2833
 * @param sysID  the system ID string
2834
 * @returns the URI of the resource or NULL if not found, it must be freed
2835
 *      by the caller.
2836
 */
2837
xmlChar *
2838
xmlACatalogResolve(xmlCatalog *catal, const xmlChar * pubID,
2839
                   const xmlChar * sysID)
2840
0
{
2841
0
    xmlChar *ret = NULL;
2842
2843
0
    if (((pubID == NULL) && (sysID == NULL)) || (catal == NULL))
2844
0
        return (NULL);
2845
2846
0
    if (xmlDebugCatalogs) {
2847
0
         if ((pubID != NULL) && (sysID != NULL)) {
2848
0
             xmlCatalogPrintDebug(
2849
0
                             "Resolve: pubID %s sysID %s\n", pubID, sysID);
2850
0
         } else if (pubID != NULL) {
2851
0
             xmlCatalogPrintDebug(
2852
0
                             "Resolve: pubID %s\n", pubID);
2853
0
         } else {
2854
0
             xmlCatalogPrintDebug(
2855
0
                             "Resolve: sysID %s\n", sysID);
2856
0
         }
2857
0
    }
2858
2859
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
2860
0
    if (catal->type == XML_SGML_CATALOG_TYPE) {
2861
0
        const xmlChar *sgml;
2862
2863
0
        sgml = xmlCatalogSGMLResolve(catal, pubID, sysID);
2864
0
        if (sgml != NULL)
2865
0
            ret = xmlStrdup(sgml);
2866
0
    } else
2867
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
2868
0
    {
2869
0
        ret = xmlCatalogListXMLResolve(catal->xml, pubID, sysID);
2870
0
  if (ret == XML_CATAL_BREAK)
2871
0
      ret = NULL;
2872
0
    }
2873
0
    return (ret);
2874
0
}
2875
2876
/**
2877
 * Do a complete resolution lookup of an URI
2878
 *
2879
 * @deprecated Internal function, don't use
2880
 *
2881
 * @param catal  a Catalog
2882
 * @param URI  the URI
2883
 * @returns the URI of the resource or NULL if not found, it must be freed
2884
 *      by the caller.
2885
 */
2886
xmlChar *
2887
0
xmlACatalogResolveURI(xmlCatalog *catal, const xmlChar *URI) {
2888
0
    xmlChar *ret = NULL;
2889
2890
0
    if ((URI == NULL) || (catal == NULL))
2891
0
  return(NULL);
2892
2893
0
    if (xmlDebugCatalogs)
2894
0
  xmlCatalogPrintDebug(
2895
0
    "Resolve URI %s\n", URI);
2896
2897
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
2898
0
    if (catal->type == XML_SGML_CATALOG_TYPE) {
2899
0
  const xmlChar *sgml;
2900
2901
0
  sgml = xmlCatalogSGMLResolve(catal, NULL, URI);
2902
0
  if (sgml != NULL)
2903
0
            ret = xmlStrdup(sgml);
2904
0
    } else
2905
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
2906
0
    {
2907
0
  ret = xmlCatalogListXMLResolveURI(catal->xml, URI);
2908
0
  if (ret == XML_CATAL_BREAK)
2909
0
      ret = NULL;
2910
0
    }
2911
0
    return(ret);
2912
0
}
2913
2914
#ifdef LIBXML_OUTPUT_ENABLED
2915
/**
2916
 * Dump the given catalog to the given file.
2917
 *
2918
 * @deprecated Internal function, don't use
2919
 *
2920
 * @param catal  a Catalog
2921
 * @param out  the file.
2922
 */
2923
void
2924
0
xmlACatalogDump(xmlCatalog *catal, FILE *out) {
2925
0
    if ((out == NULL) || (catal == NULL))
2926
0
  return;
2927
2928
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
2929
0
    if (catal->type == XML_SGML_CATALOG_TYPE) {
2930
0
  xmlHashScan(catal->sgml, xmlCatalogDumpEntry, out);
2931
0
    } else
2932
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
2933
0
    {
2934
0
  xmlDumpXMLCatalog(out, catal->xml);
2935
0
    }
2936
0
}
2937
#endif /* LIBXML_OUTPUT_ENABLED */
2938
2939
/**
2940
 * Add an entry in the catalog, it may overwrite existing but
2941
 * different entries.
2942
 *
2943
 * @deprecated Internal function, don't use
2944
 *
2945
 * @param catal  a Catalog
2946
 * @param type  the type of record to add to the catalog
2947
 * @param orig  the system, public or prefix to match
2948
 * @param replace  the replacement value for the match
2949
 * @returns 0 if successful, -1 otherwise
2950
 */
2951
int
2952
xmlACatalogAdd(xmlCatalog *catal, const xmlChar * type,
2953
              const xmlChar * orig, const xmlChar * replace)
2954
0
{
2955
0
    int res = -1;
2956
2957
0
    if (catal == NULL)
2958
0
  return(-1);
2959
2960
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
2961
0
    if (catal->type == XML_SGML_CATALOG_TYPE) {
2962
0
        xmlCatalogEntryType cattype;
2963
2964
0
        cattype = xmlGetSGMLCatalogEntryType(type);
2965
0
        if (cattype != XML_CATA_NONE) {
2966
0
            xmlCatalogEntryPtr entry;
2967
2968
0
            entry = xmlNewCatalogEntry(cattype, orig, replace, NULL,
2969
0
                                       XML_CATA_PREFER_NONE, NULL);
2970
0
      if (catal->sgml == NULL)
2971
0
    catal->sgml = xmlHashCreate(10);
2972
0
            res = xmlHashAddEntry(catal->sgml, orig, entry);
2973
0
            if (res < 0)
2974
0
                xmlFreeCatalogEntry(entry, NULL);
2975
0
        }
2976
0
    } else
2977
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
2978
0
    {
2979
0
        res = xmlAddXMLCatalog(catal->xml, type, orig, replace);
2980
0
    }
2981
0
    return (res);
2982
0
}
2983
2984
/**
2985
 * Remove an entry from the catalog
2986
 *
2987
 * @deprecated Internal function, don't use
2988
 *
2989
 * @param catal  a Catalog
2990
 * @param value  the value to remove
2991
 * @returns the number of entries removed if successful, -1 otherwise
2992
 */
2993
int
2994
0
xmlACatalogRemove(xmlCatalog *catal, const xmlChar *value) {
2995
0
    int res = -1;
2996
2997
0
    if ((catal == NULL) || (value == NULL))
2998
0
  return(-1);
2999
3000
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
3001
0
    if (catal->type == XML_SGML_CATALOG_TYPE) {
3002
0
  res = xmlHashRemoveEntry(catal->sgml, value, xmlFreeCatalogEntry);
3003
0
  if (res == 0)
3004
0
      res = 1;
3005
0
    } else
3006
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
3007
0
    {
3008
0
  res = xmlDelXMLCatalog(catal->xml, value);
3009
0
    }
3010
0
    return(res);
3011
0
}
3012
3013
/**
3014
 * create a new Catalog.
3015
 *
3016
 * @deprecated Internal function, don't use
3017
 *
3018
 * @param sgml  should this create an SGML catalog
3019
 * @returns the xmlCatalog or NULL in case of error
3020
 */
3021
xmlCatalog *
3022
0
xmlNewCatalog(int sgml) {
3023
0
    xmlCatalogPtr catal = NULL;
3024
3025
0
    (void) sgml;
3026
3027
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
3028
0
    if (sgml) {
3029
0
  catal = xmlCreateNewCatalog(XML_SGML_CATALOG_TYPE,
3030
0
                        xmlCatalogDefaultPrefer);
3031
0
        if ((catal != NULL) && (catal->sgml == NULL))
3032
0
      catal->sgml = xmlHashCreate(10);
3033
0
    } else
3034
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
3035
0
    {
3036
0
  catal = xmlCreateNewCatalog(XML_XML_CATALOG_TYPE,
3037
0
                        xmlCatalogDefaultPrefer);
3038
0
    }
3039
0
    return(catal);
3040
0
}
3041
3042
/**
3043
 * Check is a catalog is empty
3044
 *
3045
 * @deprecated Internal function, don't use
3046
 *
3047
 * @param catal  should this create an SGML catalog
3048
 * @returns 1 if the catalog is empty, 0 if not, amd -1 in case of error.
3049
 */
3050
int
3051
0
xmlCatalogIsEmpty(xmlCatalog *catal) {
3052
0
    if (catal == NULL)
3053
0
  return(-1);
3054
3055
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
3056
0
    if (catal->type == XML_SGML_CATALOG_TYPE) {
3057
0
  int res;
3058
3059
0
  if (catal->sgml == NULL)
3060
0
      return(1);
3061
0
  res = xmlHashSize(catal->sgml);
3062
0
  if (res == 0)
3063
0
      return(1);
3064
0
  if (res < 0)
3065
0
      return(-1);
3066
0
    } else
3067
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
3068
0
    {
3069
0
  if (catal->xml == NULL)
3070
0
      return(1);
3071
0
  if ((catal->xml->type != XML_CATA_CATALOG) &&
3072
0
      (catal->xml->type != XML_CATA_BROKEN_CATALOG))
3073
0
      return(-1);
3074
0
  if (catal->xml->children == NULL)
3075
0
      return(1);
3076
0
        return(0);
3077
0
    }
3078
0
    return(0);
3079
0
}
3080
3081
/************************************************************************
3082
 *                  *
3083
 *   Public interfaces manipulating the global shared default catalog *
3084
 *                  *
3085
 ************************************************************************/
3086
3087
/**
3088
 * Do the catalog initialization only of global data, doesn't try to load
3089
 * any catalog actually.
3090
 */
3091
void
3092
2
xmlInitCatalogInternal(void) {
3093
2
    if (getenv("XML_DEBUG_CATALOG"))
3094
0
  xmlDebugCatalogs = 1;
3095
2
    xmlInitRMutex(&xmlCatalogMutex);
3096
2
}
3097
3098
/**
3099
 * Load the default system catalog.
3100
 */
3101
void
3102
2
xmlInitializeCatalog(void) {
3103
2
    if (xmlCatalogInitialized != 0)
3104
0
  return;
3105
3106
2
    xmlInitParser();
3107
3108
2
    xmlRMutexLock(&xmlCatalogMutex);
3109
3110
2
    if (xmlDefaultCatalog == NULL) {
3111
2
  const char *catalogs;
3112
2
  char *path;
3113
2
  const char *cur, *paths;
3114
2
  xmlCatalogPtr catal;
3115
2
  xmlCatalogEntryPtr *nextent;
3116
3117
2
  catalogs = (const char *) getenv("XML_CATALOG_FILES");
3118
2
  if (catalogs == NULL)
3119
2
      catalogs = XML_XML_DEFAULT_CATALOG;
3120
3121
2
  catal = xmlCreateNewCatalog(XML_XML_CATALOG_TYPE,
3122
2
    xmlCatalogDefaultPrefer);
3123
2
  if (catal != NULL) {
3124
      /* the XML_CATALOG_FILES envvar is allowed to contain a
3125
         space-separated list of entries. */
3126
2
      cur = catalogs;
3127
2
      nextent = &catal->xml;
3128
4
      while (*cur != '\0') {
3129
2
    while (xmlIsBlank_ch(*cur))
3130
0
        cur++;
3131
2
    if (*cur != 0) {
3132
2
        paths = cur;
3133
68
        while ((*cur != 0) && (!xmlIsBlank_ch(*cur)))
3134
66
      cur++;
3135
2
        path = (char *) xmlStrndup((const xmlChar *)paths, cur - paths);
3136
2
        if (path != NULL) {
3137
2
      *nextent = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
3138
2
        NULL, BAD_CAST path, xmlCatalogDefaultPrefer, NULL);
3139
2
      if (*nextent != NULL)
3140
2
          nextent = &((*nextent)->next);
3141
2
      xmlFree(path);
3142
2
        }
3143
2
    }
3144
2
      }
3145
2
      xmlDefaultCatalog = catal;
3146
2
  }
3147
2
    }
3148
3149
2
    xmlRMutexUnlock(&xmlCatalogMutex);
3150
3151
2
    xmlCatalogInitialized = 1;
3152
2
}
3153
3154
3155
/**
3156
 * Load the catalog and makes its definitions effective for the default
3157
 * external entity loader. It will recurse in SGML CATALOG entries.
3158
 * this function is not thread safe, catalog initialization should
3159
 * preferably be done once at startup
3160
 *
3161
 * @param filename  a file path
3162
 * @returns 0 in case of success -1 in case of error
3163
 */
3164
int
3165
xmlLoadCatalog(const char *filename)
3166
0
{
3167
0
    int ret;
3168
0
    xmlCatalogPtr catal;
3169
3170
0
    xmlInitParser();
3171
3172
0
    xmlRMutexLock(&xmlCatalogMutex);
3173
3174
0
    if (xmlDefaultCatalog == NULL) {
3175
0
  catal = xmlLoadACatalog(filename);
3176
0
  if (catal == NULL) {
3177
0
      xmlRMutexUnlock(&xmlCatalogMutex);
3178
0
      return(-1);
3179
0
  }
3180
3181
0
  xmlDefaultCatalog = catal;
3182
0
  xmlRMutexUnlock(&xmlCatalogMutex);
3183
0
        xmlCatalogInitialized = 1;
3184
0
  return(0);
3185
0
    }
3186
3187
0
    ret = xmlExpandCatalog(xmlDefaultCatalog, filename, 0);
3188
0
    xmlRMutexUnlock(&xmlCatalogMutex);
3189
0
    return(ret);
3190
0
}
3191
3192
/**
3193
 * Load the catalogs and makes their definitions effective for the default
3194
 * external entity loader.
3195
 * this function is not thread safe, catalog initialization should
3196
 * preferably be done once at startup
3197
 *
3198
 * @param pathss  a list of directories separated by a colon or a space.
3199
 */
3200
void
3201
0
xmlLoadCatalogs(const char *pathss) {
3202
0
    const char *cur;
3203
0
    const char *paths;
3204
0
    xmlChar *path;
3205
#ifdef _WIN32
3206
    int i, iLen;
3207
#endif
3208
3209
0
    if (pathss == NULL)
3210
0
  return;
3211
3212
0
    cur = pathss;
3213
0
    while (*cur != 0) {
3214
0
  while (xmlIsBlank_ch(*cur)) cur++;
3215
0
  if (*cur != 0) {
3216
0
      paths = cur;
3217
0
      while ((*cur != 0) && (*cur != PATH_SEPARATOR) && (!xmlIsBlank_ch(*cur)))
3218
0
    cur++;
3219
0
      path = xmlStrndup((const xmlChar *)paths, cur - paths);
3220
0
      if (path != NULL) {
3221
#ifdef _WIN32
3222
        iLen = strlen((const char*)path);
3223
        for(i = 0; i < iLen; i++) {
3224
            if(path[i] == '\\') {
3225
                path[i] = '/';
3226
            }
3227
        }
3228
#endif
3229
0
    xmlLoadCatalog((const char *) path);
3230
0
    xmlFree(path);
3231
0
      }
3232
0
  }
3233
0
  while (*cur == PATH_SEPARATOR)
3234
0
      cur++;
3235
0
    }
3236
0
}
3237
3238
/**
3239
 * Free up all the memory associated with catalogs
3240
 */
3241
void
3242
0
xmlCatalogCleanup(void) {
3243
0
    xmlRMutexLock(&xmlCatalogMutex);
3244
0
    if (xmlDebugCatalogs)
3245
0
  xmlCatalogPrintDebug(
3246
0
    "Catalogs cleanup\n");
3247
0
    if (xmlCatalogXMLFiles != NULL)
3248
0
  xmlHashFree(xmlCatalogXMLFiles, xmlFreeCatalogHashEntryList);
3249
0
    xmlCatalogXMLFiles = NULL;
3250
0
    if (xmlDefaultCatalog != NULL)
3251
0
  xmlFreeCatalog(xmlDefaultCatalog);
3252
0
    xmlDefaultCatalog = NULL;
3253
0
    xmlDebugCatalogs = 0;
3254
0
    xmlCatalogInitialized = 0;
3255
0
    xmlRMutexUnlock(&xmlCatalogMutex);
3256
0
}
3257
3258
/**
3259
 * Free global data.
3260
 */
3261
void
3262
0
xmlCleanupCatalogInternal(void) {
3263
0
    xmlCleanupRMutex(&xmlCatalogMutex);
3264
0
}
3265
3266
/**
3267
 * Try to lookup the catalog resource for a system ID
3268
 *
3269
 * @param sysID  the system ID string
3270
 * @returns the resource if found or NULL otherwise, the value returned
3271
 *      must be freed by the caller.
3272
 */
3273
xmlChar *
3274
0
xmlCatalogResolveSystem(const xmlChar *sysID) {
3275
0
    xmlChar *ret;
3276
3277
0
    if (!xmlCatalogInitialized)
3278
0
  xmlInitializeCatalog();
3279
3280
0
    ret = xmlACatalogResolveSystem(xmlDefaultCatalog, sysID);
3281
0
    return(ret);
3282
0
}
3283
3284
/**
3285
 * Try to lookup the catalog reference associated to a public ID
3286
 *
3287
 * @param pubID  the public ID string
3288
 * @returns the resource if found or NULL otherwise, the value returned
3289
 *      must be freed by the caller.
3290
 */
3291
xmlChar *
3292
0
xmlCatalogResolvePublic(const xmlChar *pubID) {
3293
0
    xmlChar *ret;
3294
3295
0
    if (!xmlCatalogInitialized)
3296
0
  xmlInitializeCatalog();
3297
3298
0
    ret = xmlACatalogResolvePublic(xmlDefaultCatalog, pubID);
3299
0
    return(ret);
3300
0
}
3301
3302
/**
3303
 * Do a complete resolution lookup of an External Identifier
3304
 *
3305
 * @param pubID  the public ID string
3306
 * @param sysID  the system ID string
3307
 * @returns the URI of the resource or NULL if not found, it must be freed
3308
 *      by the caller.
3309
 */
3310
xmlChar *
3311
0
xmlCatalogResolve(const xmlChar *pubID, const xmlChar *sysID) {
3312
0
    xmlChar *ret;
3313
3314
0
    if (!xmlCatalogInitialized)
3315
0
  xmlInitializeCatalog();
3316
3317
0
    ret = xmlACatalogResolve(xmlDefaultCatalog, pubID, sysID);
3318
0
    return(ret);
3319
0
}
3320
3321
/**
3322
 * Do a complete resolution lookup of an URI
3323
 *
3324
 * @param URI  the URI
3325
 * @returns the URI of the resource or NULL if not found, it must be freed
3326
 *      by the caller.
3327
 */
3328
xmlChar *
3329
0
xmlCatalogResolveURI(const xmlChar *URI) {
3330
0
    xmlChar *ret;
3331
3332
0
    if (!xmlCatalogInitialized)
3333
0
  xmlInitializeCatalog();
3334
3335
0
    ret = xmlACatalogResolveURI(xmlDefaultCatalog, URI);
3336
0
    return(ret);
3337
0
}
3338
3339
#ifdef LIBXML_OUTPUT_ENABLED
3340
/**
3341
 * Dump all the global catalog content to the given file.
3342
 *
3343
 * @param out  the file.
3344
 */
3345
void
3346
0
xmlCatalogDump(FILE *out) {
3347
0
    if (out == NULL)
3348
0
  return;
3349
3350
0
    if (!xmlCatalogInitialized)
3351
0
  xmlInitializeCatalog();
3352
3353
0
    xmlACatalogDump(xmlDefaultCatalog, out);
3354
0
}
3355
#endif /* LIBXML_OUTPUT_ENABLED */
3356
3357
/**
3358
 * Add an entry in the catalog, it may overwrite existing but
3359
 * different entries.
3360
 * If called before any other catalog routine, allows to override the
3361
 * default shared catalog put in place by #xmlInitializeCatalog;
3362
 *
3363
 * @param type  the type of record to add to the catalog
3364
 * @param orig  the system, public or prefix to match
3365
 * @param replace  the replacement value for the match
3366
 * @returns 0 if successful, -1 otherwise
3367
 */
3368
int
3369
0
xmlCatalogAdd(const xmlChar *type, const xmlChar *orig, const xmlChar *replace) {
3370
0
    int res = -1;
3371
3372
0
    xmlInitParser();
3373
3374
0
    xmlRMutexLock(&xmlCatalogMutex);
3375
    /*
3376
     * Specific case where one want to override the default catalog
3377
     * put in place by xmlInitializeCatalog();
3378
     */
3379
0
    if ((xmlDefaultCatalog == NULL) &&
3380
0
  (xmlStrEqual(type, BAD_CAST "catalog"))) {
3381
0
  xmlDefaultCatalog = xmlCreateNewCatalog(XML_XML_CATALOG_TYPE,
3382
0
                              xmlCatalogDefaultPrefer);
3383
0
  if (xmlDefaultCatalog != NULL) {
3384
0
     xmlDefaultCatalog->xml = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
3385
0
            orig, NULL,  xmlCatalogDefaultPrefer, NULL);
3386
0
  }
3387
0
  xmlRMutexUnlock(&xmlCatalogMutex);
3388
0
        xmlCatalogInitialized = 1;
3389
0
  return(0);
3390
0
    }
3391
3392
0
    res = xmlACatalogAdd(xmlDefaultCatalog, type, orig, replace);
3393
0
    xmlRMutexUnlock(&xmlCatalogMutex);
3394
0
    return(res);
3395
0
}
3396
3397
/**
3398
 * Remove an entry from the catalog
3399
 *
3400
 * @param value  the value to remove
3401
 * @returns the number of entries removed if successful, -1 otherwise
3402
 */
3403
int
3404
0
xmlCatalogRemove(const xmlChar *value) {
3405
0
    int res;
3406
3407
0
    if (!xmlCatalogInitialized)
3408
0
  xmlInitializeCatalog();
3409
3410
0
    xmlRMutexLock(&xmlCatalogMutex);
3411
0
    res = xmlACatalogRemove(xmlDefaultCatalog, value);
3412
0
    xmlRMutexUnlock(&xmlCatalogMutex);
3413
0
    return(res);
3414
0
}
3415
3416
#ifdef LIBXML_SGML_CATALOG_ENABLED
3417
/**
3418
 * Convert all the SGML catalog entries as XML ones
3419
 *
3420
 * @returns the number of entries converted if successful, -1 otherwise
3421
 */
3422
int
3423
0
xmlCatalogConvert(void) {
3424
0
    int res = -1;
3425
3426
0
    if (!xmlCatalogInitialized)
3427
0
  xmlInitializeCatalog();
3428
3429
0
    xmlRMutexLock(&xmlCatalogMutex);
3430
0
    res = xmlConvertSGMLCatalog(xmlDefaultCatalog);
3431
0
    xmlRMutexUnlock(&xmlCatalogMutex);
3432
0
    return(res);
3433
0
}
3434
#endif /* LIBXML_SGML_CATALOG_ENABLED */
3435
3436
/************************************************************************
3437
 *                  *
3438
 *  Public interface manipulating the common preferences    *
3439
 *                  *
3440
 ************************************************************************/
3441
3442
/**
3443
 * Used to get the user preference w.r.t. to what catalogs should
3444
 * be accepted
3445
 *
3446
 * @deprecated Use XML_PARSE_NO_SYS_CATALOG and
3447
 * XML_PARSE_CATALOG_PI.
3448
 *
3449
 * @returns the current xmlCatalogAllow value
3450
 */
3451
xmlCatalogAllow
3452
284k
xmlCatalogGetDefaults(void) {
3453
284k
    return(xmlCatalogDefaultAllow);
3454
284k
}
3455
3456
/**
3457
 * Used to set the user preference w.r.t. to what catalogs should
3458
 * be accepted
3459
 *
3460
 * @deprecated Use XML_PARSE_NO_SYS_CATALOG and
3461
 * XML_PARSE_CATALOG_PI.
3462
 *
3463
 * @param allow  what catalogs should be accepted
3464
 */
3465
void
3466
2
xmlCatalogSetDefaults(xmlCatalogAllow allow) {
3467
2
    if (xmlDebugCatalogs) {
3468
0
  switch (allow) {
3469
0
      case XML_CATA_ALLOW_NONE:
3470
0
    xmlCatalogPrintDebug(
3471
0
      "Disabling catalog usage\n");
3472
0
    break;
3473
0
      case XML_CATA_ALLOW_GLOBAL:
3474
0
    xmlCatalogPrintDebug(
3475
0
      "Allowing only global catalogs\n");
3476
0
    break;
3477
0
      case XML_CATA_ALLOW_DOCUMENT:
3478
0
    xmlCatalogPrintDebug(
3479
0
      "Allowing only catalogs from the document\n");
3480
0
    break;
3481
0
      case XML_CATA_ALLOW_ALL:
3482
0
    xmlCatalogPrintDebug(
3483
0
      "Allowing all catalogs\n");
3484
0
    break;
3485
0
  }
3486
0
    }
3487
2
    xmlCatalogDefaultAllow = allow;
3488
2
}
3489
3490
/**
3491
 * Allows to set the preference between public and system for deletion
3492
 * in XML Catalog resolution. C.f. section 4.1.1 of the spec
3493
 * Values accepted are XML_CATA_PREFER_PUBLIC or XML_CATA_PREFER_SYSTEM
3494
 *
3495
 * @deprecated This setting is global and not thread-safe.
3496
 *
3497
 * @param prefer  the default preference for delegation
3498
 * @returns the previous value of the default preference for delegation
3499
 */
3500
xmlCatalogPrefer
3501
0
xmlCatalogSetDefaultPrefer(xmlCatalogPrefer prefer) {
3502
0
    xmlCatalogPrefer ret = xmlCatalogDefaultPrefer;
3503
3504
0
    if (prefer == XML_CATA_PREFER_NONE)
3505
0
  return(ret);
3506
3507
0
    if (xmlDebugCatalogs) {
3508
0
  switch (prefer) {
3509
0
      case XML_CATA_PREFER_PUBLIC:
3510
0
    xmlCatalogPrintDebug(
3511
0
      "Setting catalog preference to PUBLIC\n");
3512
0
    break;
3513
0
      case XML_CATA_PREFER_SYSTEM:
3514
0
    xmlCatalogPrintDebug(
3515
0
      "Setting catalog preference to SYSTEM\n");
3516
0
    break;
3517
0
      default:
3518
0
    return(ret);
3519
0
  }
3520
0
    }
3521
0
    xmlCatalogDefaultPrefer = prefer;
3522
0
    return(ret);
3523
0
}
3524
3525
/**
3526
 * Used to set the debug level for catalog operation, 0 disable
3527
 * debugging, 1 enable it
3528
 *
3529
 * @param level  the debug level of catalogs required
3530
 * @returns the previous value of the catalog debugging level
3531
 */
3532
int
3533
0
xmlCatalogSetDebug(int level) {
3534
0
    int ret = xmlDebugCatalogs;
3535
3536
0
    if (level <= 0)
3537
0
        xmlDebugCatalogs = 0;
3538
0
    else
3539
0
  xmlDebugCatalogs = level;
3540
0
    return(ret);
3541
0
}
3542
3543
/************************************************************************
3544
 *                  *
3545
 *   Minimal interfaces used for per-document catalogs by the parser  *
3546
 *                  *
3547
 ************************************************************************/
3548
3549
/**
3550
 * Free up the memory associated to the catalog list
3551
 *
3552
 * @param catalogs  a document's list of catalogs
3553
 */
3554
void
3555
0
xmlCatalogFreeLocal(void *catalogs) {
3556
0
    xmlCatalogEntryPtr catal;
3557
3558
0
    catal = (xmlCatalogEntryPtr) catalogs;
3559
0
    if (catal != NULL)
3560
0
  xmlFreeCatalogEntryList(catal);
3561
0
}
3562
3563
3564
/**
3565
 * Add the new entry to the catalog list
3566
 *
3567
 * @param catalogs  a document's list of catalogs
3568
 * @param URL  the URL to a new local catalog
3569
 * @returns the updated list
3570
 */
3571
void *
3572
0
xmlCatalogAddLocal(void *catalogs, const xmlChar *URL) {
3573
0
    xmlCatalogEntryPtr catal, add;
3574
3575
0
    xmlInitParser();
3576
3577
0
    if (URL == NULL)
3578
0
  return(catalogs);
3579
3580
0
    if (xmlDebugCatalogs)
3581
0
  xmlCatalogPrintDebug(
3582
0
    "Adding document catalog %s\n", URL);
3583
3584
0
    add = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL, URL, NULL,
3585
0
                       xmlCatalogDefaultPrefer, NULL);
3586
0
    if (add == NULL)
3587
0
  return(catalogs);
3588
3589
0
    catal = (xmlCatalogEntryPtr) catalogs;
3590
0
    if (catal == NULL)
3591
0
  return((void *) add);
3592
3593
0
    while (catal->next != NULL)
3594
0
  catal = catal->next;
3595
0
    catal->next = add;
3596
0
    return(catalogs);
3597
0
}
3598
3599
/**
3600
 * Do a complete resolution lookup of an External Identifier using a
3601
 * document's private catalog list
3602
 *
3603
 * @param catalogs  a document's list of catalogs
3604
 * @param pubID  the public ID string
3605
 * @param sysID  the system ID string
3606
 * @returns the URI of the resource or NULL if not found, it must be freed
3607
 *      by the caller.
3608
 */
3609
xmlChar *
3610
xmlCatalogLocalResolve(void *catalogs, const xmlChar *pubID,
3611
0
                 const xmlChar *sysID) {
3612
0
    xmlCatalogEntryPtr catal;
3613
0
    xmlChar *ret;
3614
3615
0
    if ((pubID == NULL) && (sysID == NULL))
3616
0
  return(NULL);
3617
3618
0
    if (xmlDebugCatalogs) {
3619
0
        if ((pubID != NULL) && (sysID != NULL)) {
3620
0
            xmlCatalogPrintDebug(
3621
0
                            "Local Resolve: pubID %s sysID %s\n", pubID, sysID);
3622
0
        } else if (pubID != NULL) {
3623
0
            xmlCatalogPrintDebug(
3624
0
                            "Local Resolve: pubID %s\n", pubID);
3625
0
        } else {
3626
0
            xmlCatalogPrintDebug(
3627
0
                            "Local Resolve: sysID %s\n", sysID);
3628
0
        }
3629
0
    }
3630
3631
0
    catal = (xmlCatalogEntryPtr) catalogs;
3632
0
    if (catal == NULL)
3633
0
  return(NULL);
3634
0
    ret = xmlCatalogListXMLResolve(catal, pubID, sysID);
3635
0
    if ((ret != NULL) && (ret != XML_CATAL_BREAK))
3636
0
  return(ret);
3637
0
    return(NULL);
3638
0
}
3639
3640
/**
3641
 * Do a complete resolution lookup of an URI using a
3642
 * document's private catalog list
3643
 *
3644
 * @param catalogs  a document's list of catalogs
3645
 * @param URI  the URI
3646
 * @returns the URI of the resource or NULL if not found, it must be freed
3647
 *      by the caller.
3648
 */
3649
xmlChar *
3650
0
xmlCatalogLocalResolveURI(void *catalogs, const xmlChar *URI) {
3651
0
    xmlCatalogEntryPtr catal;
3652
0
    xmlChar *ret;
3653
3654
0
    if (URI == NULL)
3655
0
  return(NULL);
3656
3657
0
    if (xmlDebugCatalogs)
3658
0
  xmlCatalogPrintDebug(
3659
0
    "Resolve URI %s\n", URI);
3660
3661
0
    catal = (xmlCatalogEntryPtr) catalogs;
3662
0
    if (catal == NULL)
3663
0
  return(NULL);
3664
0
    ret = xmlCatalogListXMLResolveURI(catal, URI);
3665
0
    if ((ret != NULL) && (ret != XML_CATAL_BREAK))
3666
0
  return(ret);
3667
0
    return(NULL);
3668
0
}
3669
3670
/************************************************************************
3671
 *                  *
3672
 *      Deprecated interfaces       *
3673
 *                  *
3674
 ************************************************************************/
3675
/**
3676
 * Try to lookup the catalog reference associated to a system ID
3677
 *
3678
 * @deprecated use #xmlCatalogResolveSystem
3679
 *
3680
 * @param sysID  the system ID string
3681
 * @returns the resource if found or NULL otherwise.
3682
 */
3683
const xmlChar *
3684
0
xmlCatalogGetSystem(const xmlChar *sysID) {
3685
0
    xmlChar *ret;
3686
0
    static xmlChar result[1000];
3687
0
    static int msg = 0;
3688
3689
0
    if (!xmlCatalogInitialized)
3690
0
  xmlInitializeCatalog();
3691
3692
0
    if (msg == 0) {
3693
0
  xmlPrintErrorMessage(
3694
0
    "Use of deprecated xmlCatalogGetSystem() call\n");
3695
0
  msg++;
3696
0
    }
3697
3698
0
    if (sysID == NULL)
3699
0
  return(NULL);
3700
3701
    /*
3702
     * Check first the XML catalogs
3703
     */
3704
0
    if (xmlDefaultCatalog != NULL) {
3705
0
  ret = xmlCatalogListXMLResolve(xmlDefaultCatalog->xml, NULL, sysID);
3706
0
  if ((ret != NULL) && (ret != XML_CATAL_BREAK)) {
3707
0
      snprintf((char *) result, sizeof(result) - 1, "%s", (char *) ret);
3708
0
      result[sizeof(result) - 1] = 0;
3709
0
      return(result);
3710
0
  }
3711
0
    }
3712
3713
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
3714
0
    if (xmlDefaultCatalog != NULL)
3715
0
  return(xmlCatalogGetSGMLSystem(xmlDefaultCatalog->sgml, sysID));
3716
0
#endif
3717
0
    return(NULL);
3718
0
}
3719
3720
/**
3721
 * Try to lookup the catalog reference associated to a public ID
3722
 *
3723
 * @deprecated use #xmlCatalogResolvePublic
3724
 *
3725
 * @param pubID  the public ID string
3726
 * @returns the resource if found or NULL otherwise.
3727
 */
3728
const xmlChar *
3729
0
xmlCatalogGetPublic(const xmlChar *pubID) {
3730
0
    xmlChar *ret;
3731
0
    static xmlChar result[1000];
3732
0
    static int msg = 0;
3733
3734
0
    if (!xmlCatalogInitialized)
3735
0
  xmlInitializeCatalog();
3736
3737
0
    if (msg == 0) {
3738
0
  xmlPrintErrorMessage(
3739
0
    "Use of deprecated xmlCatalogGetPublic() call\n");
3740
0
  msg++;
3741
0
    }
3742
3743
0
    if (pubID == NULL)
3744
0
  return(NULL);
3745
3746
    /*
3747
     * Check first the XML catalogs
3748
     */
3749
0
    if (xmlDefaultCatalog != NULL) {
3750
0
  ret = xmlCatalogListXMLResolve(xmlDefaultCatalog->xml, pubID, NULL);
3751
0
  if ((ret != NULL) && (ret != XML_CATAL_BREAK)) {
3752
0
      snprintf((char *) result, sizeof(result) - 1, "%s", (char *) ret);
3753
0
      result[sizeof(result) - 1] = 0;
3754
0
      return(result);
3755
0
  }
3756
0
    }
3757
3758
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
3759
0
    if (xmlDefaultCatalog != NULL)
3760
0
  return(xmlCatalogGetSGMLPublic(xmlDefaultCatalog->sgml, pubID));
3761
0
#endif
3762
0
    return(NULL);
3763
0
}
3764
3765
#endif /* LIBXML_CATALOG_ENABLED */