Coverage Report

Created: 2026-03-06 07:01

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