Coverage Report

Created: 2026-02-26 06:55

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
0
#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
0
#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
0
     xmlCatalogEntryPtr group) {
323
0
    xmlCatalogEntryPtr ret;
324
0
    xmlChar *normid = NULL;
325
326
0
    ret = (xmlCatalogEntryPtr) xmlMalloc(sizeof(xmlCatalogEntry));
327
0
    if (ret == NULL) {
328
0
        xmlCatalogErrMemory();
329
0
  return(NULL);
330
0
    }
331
0
    ret->next = NULL;
332
0
    ret->parent = NULL;
333
0
    ret->children = NULL;
334
0
    ret->type = type;
335
0
    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
0
    if (name != NULL)
341
0
  ret->name = xmlStrdup(name);
342
0
    else
343
0
  ret->name = NULL;
344
0
    if (normid != NULL)
345
0
        xmlFree(normid);
346
0
    if (value != NULL)
347
0
  ret->value = xmlStrdup(value);
348
0
    else
349
0
  ret->value = NULL;
350
0
    if (URL == NULL)
351
0
  URL = value;
352
0
    if (URL != NULL)
353
0
  ret->URL = xmlStrdup(URL);
354
0
    else
355
0
  ret->URL = NULL;
356
0
    ret->prefer = prefer;
357
0
    ret->dealloc = 0;
358
0
    ret->depth = 0;
359
0
    ret->group = group;
360
0
    return(ret);
361
0
}
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
0
xmlCreateNewCatalog(xmlCatalogType type, xmlCatalogPrefer prefer) {
459
0
    xmlCatalogPtr ret;
460
461
0
    ret = (xmlCatalogPtr) xmlMalloc(sizeof(xmlCatalog));
462
0
    if (ret == NULL) {
463
0
        xmlCatalogErrMemory();
464
0
  return(NULL);
465
0
    }
466
0
    memset(ret, 0, sizeof(xmlCatalog));
467
0
    ret->type = type;
468
0
    ret->prefer = prefer;
469
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
470
0
    ret->catalNr = 0;
471
0
    ret->catalMax = XML_MAX_SGML_CATA_DEPTH;
472
0
    if (ret->type == XML_SGML_CATALOG_TYPE)
473
0
  ret->sgml = xmlHashCreate(10);
474
0
#endif
475
0
    return(ret);
476
0
}
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
2556
0
        filename = xmlBuildURI(sysid, base);
2557
0
        if (filename != NULL) {
2558
0
      xmlExpandCatalog(catal, (const char *)filename, depth);
2559
0
      xmlFree(filename);
2560
0
        }
2561
0
    }
2562
0
      }
2563
      /*
2564
       * drop anything else we won't handle it
2565
       */
2566
0
      if (name != NULL)
2567
0
    xmlFree(name);
2568
0
      if (sysid != NULL)
2569
0
    xmlFree(sysid);
2570
0
  }
2571
0
    }
2572
0
    if (base != NULL)
2573
0
  xmlFree(base);
2574
0
    if (cur == NULL)
2575
0
  return(-1);
2576
0
    return(0);
2577
0
}
2578
2579
/************************************************************************
2580
 *                  *
2581
 *      SGML Catalog handling       *
2582
 *                  *
2583
 ************************************************************************/
2584
2585
/**
2586
 * Try to lookup the catalog local reference associated to a public ID
2587
 *
2588
 * @param catal  an SGML catalog hash
2589
 * @param pubID  the public ID string
2590
 * @returns the local resource if found or NULL otherwise.
2591
 */
2592
static const xmlChar *
2593
0
xmlCatalogGetSGMLPublic(xmlHashTablePtr catal, const xmlChar *pubID) {
2594
0
    xmlCatalogEntryPtr entry;
2595
0
    xmlChar *normid;
2596
2597
0
    if (catal == NULL)
2598
0
  return(NULL);
2599
2600
0
    normid = xmlCatalogNormalizePublic(pubID);
2601
0
    if (normid != NULL)
2602
0
        pubID = (*normid != 0 ? normid : NULL);
2603
2604
0
    entry = (xmlCatalogEntryPtr) xmlHashLookup(catal, pubID);
2605
0
    if (entry == NULL) {
2606
0
  if (normid != NULL)
2607
0
      xmlFree(normid);
2608
0
  return(NULL);
2609
0
    }
2610
0
    if (entry->type == SGML_CATA_PUBLIC) {
2611
0
  if (normid != NULL)
2612
0
      xmlFree(normid);
2613
0
  return(entry->URL);
2614
0
    }
2615
0
    if (normid != NULL)
2616
0
        xmlFree(normid);
2617
0
    return(NULL);
2618
0
}
2619
2620
/**
2621
 * Try to lookup the catalog local reference for a system ID
2622
 *
2623
 * @param catal  an SGML catalog hash
2624
 * @param sysID  the system ID string
2625
 * @returns the local resource if found or NULL otherwise.
2626
 */
2627
static const xmlChar *
2628
0
xmlCatalogGetSGMLSystem(xmlHashTablePtr catal, const xmlChar *sysID) {
2629
0
    xmlCatalogEntryPtr entry;
2630
2631
0
    if (catal == NULL)
2632
0
  return(NULL);
2633
2634
0
    entry = (xmlCatalogEntryPtr) xmlHashLookup(catal, sysID);
2635
0
    if (entry == NULL)
2636
0
  return(NULL);
2637
0
    if (entry->type == SGML_CATA_SYSTEM)
2638
0
  return(entry->URL);
2639
0
    return(NULL);
2640
0
}
2641
2642
/**
2643
 * Do a complete resolution lookup of an External Identifier
2644
 *
2645
 * @param catal  the SGML catalog
2646
 * @param pubID  the public ID string
2647
 * @param sysID  the system ID string
2648
 * @returns the URI of the resource or NULL if not found
2649
 */
2650
static const xmlChar *
2651
xmlCatalogSGMLResolve(xmlCatalogPtr catal, const xmlChar *pubID,
2652
0
                const xmlChar *sysID) {
2653
0
    const xmlChar *ret = NULL;
2654
2655
0
    if (catal->sgml == NULL)
2656
0
  return(NULL);
2657
2658
0
    if (pubID != NULL)
2659
0
  ret = xmlCatalogGetSGMLPublic(catal->sgml, pubID);
2660
0
    if (ret != NULL)
2661
0
  return(ret);
2662
0
    if (sysID != NULL)
2663
0
  ret = xmlCatalogGetSGMLSystem(catal->sgml, sysID);
2664
0
    if (ret != NULL)
2665
0
  return(ret);
2666
0
    return(NULL);
2667
0
}
2668
2669
#endif /* LIBXML_SGML_CATALOG_ENABLED */
2670
2671
/************************************************************************
2672
 *                  *
2673
 *      Specific Public interfaces      *
2674
 *                  *
2675
 ************************************************************************/
2676
2677
#ifdef LIBXML_SGML_CATALOG_ENABLED
2678
/**
2679
 * Load an SGML super catalog. It won't expand CATALOG or DELEGATE
2680
 * references. This is only needed for manipulating SGML Super Catalogs
2681
 * like adding and removing CATALOG or DELEGATE entries.
2682
 *
2683
 * @deprecated Internal function, don't use
2684
 *
2685
 * @param filename  a file path
2686
 * @returns the catalog parsed or NULL in case of error
2687
 */
2688
xmlCatalog *
2689
xmlLoadSGMLSuperCatalog(const char *filename)
2690
0
{
2691
0
    xmlChar *content;
2692
0
    xmlCatalogPtr catal;
2693
0
    int ret;
2694
2695
0
    content = xmlLoadFileContent(filename);
2696
0
    if (content == NULL)
2697
0
        return(NULL);
2698
2699
0
    catal = xmlCreateNewCatalog(XML_SGML_CATALOG_TYPE, xmlCatalogDefaultPrefer);
2700
0
    if (catal == NULL) {
2701
0
  xmlFree(content);
2702
0
  return(NULL);
2703
0
    }
2704
2705
0
    ret = xmlParseSGMLCatalog(catal, content, filename, 1, 0);
2706
0
    xmlFree(content);
2707
0
    if (ret < 0) {
2708
0
  xmlFreeCatalog(catal);
2709
0
  return(NULL);
2710
0
    }
2711
0
    return (catal);
2712
0
}
2713
#endif /* LIBXML_SGML_CATALOG_ENABLED */
2714
2715
/**
2716
 * Load the catalog and build the associated data structures.
2717
 * This can be either an XML Catalog or an SGML Catalog
2718
 * It will recurse in SGML CATALOG entries. On the other hand XML
2719
 * Catalogs are not handled recursively.
2720
 *
2721
 * @deprecated Internal function, don't use
2722
 *
2723
 * @param filename  a file path
2724
 * @returns the catalog parsed or NULL in case of error
2725
 */
2726
xmlCatalog *
2727
xmlLoadACatalog(const char *filename)
2728
0
{
2729
0
    xmlChar *content;
2730
0
    xmlChar *first;
2731
0
    xmlCatalogPtr catal;
2732
2733
0
    content = xmlLoadFileContent(filename);
2734
0
    if (content == NULL)
2735
0
        return(NULL);
2736
2737
2738
0
    first = content;
2739
2740
0
    while ((*first != 0) && (*first != '-') && (*first != '<') &&
2741
0
     (!(((*first >= 'A') && (*first <= 'Z')) ||
2742
0
        ((*first >= 'a') && (*first <= 'z')))))
2743
0
  first++;
2744
2745
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
2746
0
    if (*first != '<') {
2747
0
        int ret;
2748
2749
0
  catal = xmlCreateNewCatalog(XML_SGML_CATALOG_TYPE, xmlCatalogDefaultPrefer);
2750
0
  if (catal == NULL) {
2751
0
      xmlFree(content);
2752
0
      return(NULL);
2753
0
  }
2754
0
        ret = xmlParseSGMLCatalog(catal, content, filename, 0, 0);
2755
0
  if (ret < 0) {
2756
0
      xmlFreeCatalog(catal);
2757
0
      xmlFree(content);
2758
0
      return(NULL);
2759
0
  }
2760
0
    } else
2761
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
2762
0
    {
2763
0
  catal = xmlCreateNewCatalog(XML_XML_CATALOG_TYPE, xmlCatalogDefaultPrefer);
2764
0
  if (catal == NULL) {
2765
0
      xmlFree(content);
2766
0
      return(NULL);
2767
0
  }
2768
0
        catal->xml = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
2769
0
           NULL, BAD_CAST filename, xmlCatalogDefaultPrefer, NULL);
2770
0
    }
2771
0
    xmlFree(content);
2772
0
    return (catal);
2773
0
}
2774
2775
/**
2776
 * Load the catalog and expand the existing catal structure.
2777
 * This can be either an XML Catalog or an SGML Catalog
2778
 *
2779
 * @param catal  a catalog
2780
 * @param filename  a file path
2781
 * @param depth  the current depth of the catalog
2782
 * @returns 0 in case of success, -1 in case of error
2783
 */
2784
static int
2785
xmlExpandCatalog(xmlCatalogPtr catal, const char *filename, int depth)
2786
0
{
2787
0
    if ((catal == NULL) || (filename == NULL))
2788
0
  return(-1);
2789
2790
    /* Check recursion depth */
2791
0
    if (depth > MAX_CATAL_DEPTH) {
2792
0
  return(-1);
2793
0
    }
2794
2795
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
2796
0
    if (catal->type == XML_SGML_CATALOG_TYPE) {
2797
0
  xmlChar *content;
2798
0
        int ret;
2799
2800
0
  content = xmlLoadFileContent(filename);
2801
0
  if (content == NULL)
2802
0
      return(-1);
2803
2804
0
        ret = xmlParseSGMLCatalog(catal, content, filename, 0, depth + 1);
2805
0
  if (ret < 0) {
2806
0
      xmlFree(content);
2807
0
      return(-1);
2808
0
  }
2809
0
  xmlFree(content);
2810
0
    } else
2811
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
2812
0
    {
2813
0
  xmlCatalogEntryPtr tmp, cur;
2814
0
  tmp = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
2815
0
           NULL, BAD_CAST filename, xmlCatalogDefaultPrefer, NULL);
2816
2817
0
  cur = catal->xml;
2818
0
  if (cur == NULL) {
2819
0
      catal->xml = tmp;
2820
0
  } else {
2821
0
      while (cur->next != NULL) cur = cur->next;
2822
0
      cur->next = tmp;
2823
0
  }
2824
0
    }
2825
0
    return (0);
2826
0
}
2827
2828
/**
2829
 * Try to lookup the catalog resource for a system ID
2830
 *
2831
 * @deprecated Internal function, don't use
2832
 *
2833
 * @param catal  a Catalog
2834
 * @param sysID  the system ID string
2835
 * @returns the resource if found or NULL otherwise, the value returned
2836
 *      must be freed by the caller.
2837
 */
2838
xmlChar *
2839
0
xmlACatalogResolveSystem(xmlCatalog *catal, const xmlChar *sysID) {
2840
0
    xmlChar *ret = NULL;
2841
2842
0
    if ((sysID == NULL) || (catal == NULL))
2843
0
  return(NULL);
2844
2845
0
    if (xmlDebugCatalogs)
2846
0
  xmlCatalogPrintDebug(
2847
0
    "Resolve sysID %s\n", sysID);
2848
2849
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
2850
0
    if (catal->type == XML_SGML_CATALOG_TYPE) {
2851
0
  const xmlChar *sgml;
2852
2853
0
  sgml = xmlCatalogGetSGMLSystem(catal->sgml, sysID);
2854
0
  if (sgml != NULL)
2855
0
      ret = xmlStrdup(sgml);
2856
0
    } else
2857
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
2858
0
    {
2859
0
        xmlResetCatalogResolveCache();
2860
0
        ret = xmlCatalogListXMLResolve(catal->xml, NULL, sysID);
2861
0
        xmlResetCatalogResolveCache();
2862
0
  if (ret == XML_CATAL_BREAK)
2863
0
      ret = NULL;
2864
0
    }
2865
0
    return(ret);
2866
0
}
2867
2868
/**
2869
 * Try to lookup the catalog local reference associated to a public ID in that catalog
2870
 *
2871
 * @deprecated Internal function, don't use
2872
 *
2873
 * @param catal  a Catalog
2874
 * @param pubID  the public ID string
2875
 * @returns the local resource if found or NULL otherwise, the value returned
2876
 *      must be freed by the caller.
2877
 */
2878
xmlChar *
2879
0
xmlACatalogResolvePublic(xmlCatalog *catal, const xmlChar *pubID) {
2880
0
    xmlChar *ret = NULL;
2881
2882
0
    if ((pubID == NULL) || (catal == NULL))
2883
0
  return(NULL);
2884
2885
0
    if (xmlDebugCatalogs)
2886
0
  xmlCatalogPrintDebug(
2887
0
    "Resolve pubID %s\n", pubID);
2888
2889
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
2890
0
    if (catal->type == XML_SGML_CATALOG_TYPE) {
2891
0
  const xmlChar *sgml;
2892
2893
0
  sgml = xmlCatalogGetSGMLPublic(catal->sgml, pubID);
2894
0
  if (sgml != NULL)
2895
0
      ret = xmlStrdup(sgml);
2896
0
    } else
2897
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
2898
0
    {
2899
0
        xmlResetCatalogResolveCache();
2900
0
        ret = xmlCatalogListXMLResolve(catal->xml, pubID, NULL);
2901
0
        xmlResetCatalogResolveCache();
2902
0
  if (ret == XML_CATAL_BREAK)
2903
0
      ret = NULL;
2904
0
    }
2905
0
    return(ret);
2906
0
}
2907
2908
/**
2909
 * Do a complete resolution lookup of an External Identifier
2910
 *
2911
 * @deprecated Internal function, don't use
2912
 *
2913
 * @param catal  a Catalog
2914
 * @param pubID  the public ID string
2915
 * @param sysID  the system ID string
2916
 * @returns the URI of the resource or NULL if not found, it must be freed
2917
 *      by the caller.
2918
 */
2919
xmlChar *
2920
xmlACatalogResolve(xmlCatalog *catal, const xmlChar * pubID,
2921
                   const xmlChar * sysID)
2922
0
{
2923
0
    xmlChar *ret = NULL;
2924
2925
0
    if (((pubID == NULL) && (sysID == NULL)) || (catal == NULL))
2926
0
        return (NULL);
2927
2928
0
    if (xmlDebugCatalogs) {
2929
0
         if ((pubID != NULL) && (sysID != NULL)) {
2930
0
             xmlCatalogPrintDebug(
2931
0
                             "Resolve: pubID %s sysID %s\n", pubID, sysID);
2932
0
         } else if (pubID != NULL) {
2933
0
             xmlCatalogPrintDebug(
2934
0
                             "Resolve: pubID %s\n", pubID);
2935
0
         } else {
2936
0
             xmlCatalogPrintDebug(
2937
0
                             "Resolve: sysID %s\n", sysID);
2938
0
         }
2939
0
    }
2940
2941
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
2942
0
    if (catal->type == XML_SGML_CATALOG_TYPE) {
2943
0
        const xmlChar *sgml;
2944
2945
0
        sgml = xmlCatalogSGMLResolve(catal, pubID, sysID);
2946
0
        if (sgml != NULL)
2947
0
            ret = xmlStrdup(sgml);
2948
0
    } else
2949
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
2950
0
    {
2951
0
        xmlResetCatalogResolveCache();
2952
0
        ret = xmlCatalogListXMLResolve(catal->xml, pubID, sysID);
2953
0
        xmlResetCatalogResolveCache();
2954
2955
0
  if (ret == XML_CATAL_BREAK)
2956
0
      ret = NULL;
2957
0
    }
2958
0
    return (ret);
2959
0
}
2960
2961
/**
2962
 * Do a complete resolution lookup of an URI
2963
 *
2964
 * @deprecated Internal function, don't use
2965
 *
2966
 * @param catal  a Catalog
2967
 * @param URI  the URI
2968
 * @returns the URI of the resource or NULL if not found, it must be freed
2969
 *      by the caller.
2970
 */
2971
xmlChar *
2972
0
xmlACatalogResolveURI(xmlCatalog *catal, const xmlChar *URI) {
2973
0
    xmlChar *ret = NULL;
2974
2975
0
    if ((URI == NULL) || (catal == NULL))
2976
0
  return(NULL);
2977
2978
0
    if (xmlDebugCatalogs)
2979
0
  xmlCatalogPrintDebug(
2980
0
    "Resolve URI %s\n", URI);
2981
2982
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
2983
0
    if (catal->type == XML_SGML_CATALOG_TYPE) {
2984
0
  const xmlChar *sgml;
2985
2986
0
  sgml = xmlCatalogSGMLResolve(catal, NULL, URI);
2987
0
  if (sgml != NULL)
2988
0
            ret = xmlStrdup(sgml);
2989
0
    } else
2990
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
2991
0
    {
2992
0
  ret = xmlCatalogListXMLResolveURI(catal->xml, URI);
2993
0
  if (ret == XML_CATAL_BREAK)
2994
0
      ret = NULL;
2995
0
    }
2996
0
    return(ret);
2997
0
}
2998
2999
#ifdef LIBXML_OUTPUT_ENABLED
3000
/**
3001
 * Dump the given catalog to the given file.
3002
 *
3003
 * @deprecated Internal function, don't use
3004
 *
3005
 * @param catal  a Catalog
3006
 * @param out  the file.
3007
 */
3008
void
3009
0
xmlACatalogDump(xmlCatalog *catal, FILE *out) {
3010
0
    if ((out == NULL) || (catal == NULL))
3011
0
  return;
3012
3013
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
3014
0
    if (catal->type == XML_SGML_CATALOG_TYPE) {
3015
0
  xmlHashScan(catal->sgml, xmlCatalogDumpEntry, out);
3016
0
    } else
3017
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
3018
0
    {
3019
0
  xmlDumpXMLCatalog(out, catal->xml);
3020
0
    }
3021
0
}
3022
#endif /* LIBXML_OUTPUT_ENABLED */
3023
3024
/**
3025
 * Add an entry in the catalog, it may overwrite existing but
3026
 * different entries.
3027
 *
3028
 * @deprecated Internal function, don't use
3029
 *
3030
 * @param catal  a Catalog
3031
 * @param type  the type of record to add to the catalog
3032
 * @param orig  the system, public or prefix to match
3033
 * @param replace  the replacement value for the match
3034
 * @returns 0 if successful, -1 otherwise
3035
 */
3036
int
3037
xmlACatalogAdd(xmlCatalog *catal, const xmlChar * type,
3038
              const xmlChar * orig, const xmlChar * replace)
3039
0
{
3040
0
    int res = -1;
3041
3042
0
    if (catal == NULL)
3043
0
  return(-1);
3044
3045
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
3046
0
    if (catal->type == XML_SGML_CATALOG_TYPE) {
3047
0
        xmlCatalogEntryType cattype;
3048
3049
0
        cattype = xmlGetSGMLCatalogEntryType(type);
3050
0
        if (cattype != XML_CATA_NONE) {
3051
0
            xmlCatalogEntryPtr entry;
3052
3053
0
            entry = xmlNewCatalogEntry(cattype, orig, replace, NULL,
3054
0
                                       XML_CATA_PREFER_NONE, NULL);
3055
0
      if (catal->sgml == NULL)
3056
0
    catal->sgml = xmlHashCreate(10);
3057
0
            res = xmlHashAddEntry(catal->sgml, orig, entry);
3058
0
            if (res < 0)
3059
0
                xmlFreeCatalogEntry(entry, NULL);
3060
0
        }
3061
0
    } else
3062
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
3063
0
    {
3064
0
        res = xmlAddXMLCatalog(catal->xml, type, orig, replace);
3065
0
    }
3066
0
    return (res);
3067
0
}
3068
3069
/**
3070
 * Remove an entry from the catalog
3071
 *
3072
 * @deprecated Internal function, don't use
3073
 *
3074
 * @param catal  a Catalog
3075
 * @param value  the value to remove
3076
 * @returns the number of entries removed if successful, -1 otherwise
3077
 */
3078
int
3079
0
xmlACatalogRemove(xmlCatalog *catal, const xmlChar *value) {
3080
0
    int res = -1;
3081
3082
0
    if ((catal == NULL) || (value == NULL))
3083
0
  return(-1);
3084
3085
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
3086
0
    if (catal->type == XML_SGML_CATALOG_TYPE) {
3087
0
  res = xmlHashRemoveEntry(catal->sgml, value, xmlFreeCatalogEntry);
3088
0
  if (res == 0)
3089
0
      res = 1;
3090
0
    } else
3091
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
3092
0
    {
3093
0
  res = xmlDelXMLCatalog(catal->xml, value);
3094
0
    }
3095
0
    return(res);
3096
0
}
3097
3098
/**
3099
 * create a new Catalog.
3100
 *
3101
 * @deprecated Internal function, don't use
3102
 *
3103
 * @param sgml  should this create an SGML catalog
3104
 * @returns the xmlCatalog or NULL in case of error
3105
 */
3106
xmlCatalog *
3107
0
xmlNewCatalog(int sgml) {
3108
0
    xmlCatalogPtr catal = NULL;
3109
3110
0
    (void) sgml;
3111
3112
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
3113
0
    if (sgml) {
3114
0
  catal = xmlCreateNewCatalog(XML_SGML_CATALOG_TYPE,
3115
0
                        xmlCatalogDefaultPrefer);
3116
0
        if ((catal != NULL) && (catal->sgml == NULL))
3117
0
      catal->sgml = xmlHashCreate(10);
3118
0
    } else
3119
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
3120
0
    {
3121
0
  catal = xmlCreateNewCatalog(XML_XML_CATALOG_TYPE,
3122
0
                        xmlCatalogDefaultPrefer);
3123
0
    }
3124
0
    return(catal);
3125
0
}
3126
3127
/**
3128
 * Check is a catalog is empty
3129
 *
3130
 * @deprecated Internal function, don't use
3131
 *
3132
 * @param catal  should this create an SGML catalog
3133
 * @returns 1 if the catalog is empty, 0 if not, amd -1 in case of error.
3134
 */
3135
int
3136
0
xmlCatalogIsEmpty(xmlCatalog *catal) {
3137
0
    if (catal == NULL)
3138
0
  return(-1);
3139
3140
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
3141
0
    if (catal->type == XML_SGML_CATALOG_TYPE) {
3142
0
  int res;
3143
3144
0
  if (catal->sgml == NULL)
3145
0
      return(1);
3146
0
  res = xmlHashSize(catal->sgml);
3147
0
  if (res == 0)
3148
0
      return(1);
3149
0
  if (res < 0)
3150
0
      return(-1);
3151
0
    } else
3152
0
#endif /* LIBXML_SGML_CATALOG_ENABLED */
3153
0
    {
3154
0
  if (catal->xml == NULL)
3155
0
      return(1);
3156
0
  if ((catal->xml->type != XML_CATA_CATALOG) &&
3157
0
      (catal->xml->type != XML_CATA_BROKEN_CATALOG))
3158
0
      return(-1);
3159
0
  if (catal->xml->children == NULL)
3160
0
      return(1);
3161
0
        return(0);
3162
0
    }
3163
0
    return(0);
3164
0
}
3165
3166
/************************************************************************
3167
 *                  *
3168
 *   Public interfaces manipulating the global shared default catalog *
3169
 *                  *
3170
 ************************************************************************/
3171
3172
/**
3173
 * Do the catalog initialization only of global data, doesn't try to load
3174
 * any catalog actually.
3175
 */
3176
void
3177
1
xmlInitCatalogInternal(void) {
3178
1
    if (getenv("XML_DEBUG_CATALOG"))
3179
0
  xmlDebugCatalogs = 1;
3180
1
    xmlInitRMutex(&xmlCatalogMutex);
3181
1
}
3182
3183
/**
3184
 * Load the default system catalog.
3185
 */
3186
void
3187
0
xmlInitializeCatalog(void) {
3188
0
    if (xmlCatalogInitialized != 0)
3189
0
  return;
3190
3191
0
    xmlInitParser();
3192
3193
0
    xmlRMutexLock(&xmlCatalogMutex);
3194
3195
0
    if (xmlDefaultCatalog == NULL) {
3196
0
  const char *catalogs;
3197
0
  char *path;
3198
0
  const char *cur, *paths;
3199
0
  xmlCatalogPtr catal;
3200
0
  xmlCatalogEntryPtr *nextent;
3201
3202
0
  catalogs = (const char *) getenv("XML_CATALOG_FILES");
3203
0
  if (catalogs == NULL)
3204
0
      catalogs = XML_XML_DEFAULT_CATALOG;
3205
3206
0
  catal = xmlCreateNewCatalog(XML_XML_CATALOG_TYPE,
3207
0
    xmlCatalogDefaultPrefer);
3208
0
  if (catal != NULL) {
3209
      /* the XML_CATALOG_FILES envvar is allowed to contain a
3210
         space-separated list of entries. */
3211
0
      cur = catalogs;
3212
0
      nextent = &catal->xml;
3213
0
      while (*cur != '\0') {
3214
0
    while (xmlIsBlank_ch(*cur))
3215
0
        cur++;
3216
0
    if (*cur != 0) {
3217
0
        paths = cur;
3218
0
        while ((*cur != 0) && (!xmlIsBlank_ch(*cur)))
3219
0
      cur++;
3220
0
        path = (char *) xmlStrndup((const xmlChar *)paths, cur - paths);
3221
0
        if (path != NULL) {
3222
0
      *nextent = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
3223
0
        NULL, BAD_CAST path, xmlCatalogDefaultPrefer, NULL);
3224
0
      if (*nextent != NULL)
3225
0
          nextent = &((*nextent)->next);
3226
0
      xmlFree(path);
3227
0
        }
3228
0
    }
3229
0
      }
3230
0
      xmlDefaultCatalog = catal;
3231
0
  }
3232
0
    }
3233
3234
0
    xmlRMutexUnlock(&xmlCatalogMutex);
3235
3236
0
    xmlCatalogInitialized = 1;
3237
0
}
3238
3239
3240
/**
3241
 * Load the catalog and makes its definitions effective for the default
3242
 * external entity loader. It will recurse in SGML CATALOG entries.
3243
 * this function is not thread safe, catalog initialization should
3244
 * preferably be done once at startup
3245
 *
3246
 * @param filename  a file path
3247
 * @returns 0 in case of success -1 in case of error
3248
 */
3249
int
3250
xmlLoadCatalog(const char *filename)
3251
0
{
3252
0
    int ret;
3253
0
    xmlCatalogPtr catal;
3254
3255
0
    xmlInitParser();
3256
3257
0
    xmlRMutexLock(&xmlCatalogMutex);
3258
3259
0
    if (xmlDefaultCatalog == NULL) {
3260
0
  catal = xmlLoadACatalog(filename);
3261
0
  if (catal == NULL) {
3262
0
      xmlRMutexUnlock(&xmlCatalogMutex);
3263
0
      return(-1);
3264
0
  }
3265
3266
0
  xmlDefaultCatalog = catal;
3267
0
  xmlRMutexUnlock(&xmlCatalogMutex);
3268
0
        xmlCatalogInitialized = 1;
3269
0
  return(0);
3270
0
    }
3271
3272
0
    ret = xmlExpandCatalog(xmlDefaultCatalog, filename, 0);
3273
0
    xmlRMutexUnlock(&xmlCatalogMutex);
3274
0
    return(ret);
3275
0
}
3276
3277
/**
3278
 * Load the catalogs and makes their definitions effective for the default
3279
 * external entity loader.
3280
 * this function is not thread safe, catalog initialization should
3281
 * preferably be done once at startup
3282
 *
3283
 * @param pathss  a list of directories separated by a colon or a space.
3284
 */
3285
void
3286
0
xmlLoadCatalogs(const char *pathss) {
3287
0
    const char *cur;
3288
0
    const char *paths;
3289
0
    xmlChar *path;
3290
#ifdef LIBXML2_WINPATH_ENABLED
3291
    int i, iLen;
3292
#endif
3293
3294
0
    if (pathss == NULL)
3295
0
  return;
3296
3297
0
    cur = pathss;
3298
0
    while (*cur != 0) {
3299
0
  while (xmlIsBlank_ch(*cur)) cur++;
3300
0
  if (*cur != 0) {
3301
0
      paths = cur;
3302
0
      while ((*cur != 0) && (*cur != PATH_SEPARATOR) && (!xmlIsBlank_ch(*cur)))
3303
0
    cur++;
3304
0
      path = xmlStrndup((const xmlChar *)paths, cur - paths);
3305
0
      if (path != NULL) {
3306
#ifdef LIBXML2_WINPATH_ENABLED
3307
        iLen = strlen((const char*)path);
3308
        for(i = 0; i < iLen; i++) {
3309
            if(path[i] == '\\') {
3310
                path[i] = '/';
3311
            }
3312
        }
3313
#endif
3314
0
    xmlLoadCatalog((const char *) path);
3315
0
    xmlFree(path);
3316
0
      }
3317
0
  }
3318
0
  while (*cur == PATH_SEPARATOR)
3319
0
      cur++;
3320
0
    }
3321
0
}
3322
3323
/**
3324
 * Free up all the memory associated with catalogs
3325
 */
3326
void
3327
0
xmlCatalogCleanup(void) {
3328
0
    xmlRMutexLock(&xmlCatalogMutex);
3329
0
    if (xmlDebugCatalogs)
3330
0
  xmlCatalogPrintDebug(
3331
0
    "Catalogs cleanup\n");
3332
0
    if (xmlCatalogXMLFiles != NULL)
3333
0
  xmlHashFree(xmlCatalogXMLFiles, xmlFreeCatalogHashEntryList);
3334
0
    xmlCatalogXMLFiles = NULL;
3335
0
    if (xmlDefaultCatalog != NULL)
3336
0
  xmlFreeCatalog(xmlDefaultCatalog);
3337
0
    xmlDefaultCatalog = NULL;
3338
0
    xmlDebugCatalogs = 0;
3339
0
    xmlCatalogInitialized = 0;
3340
0
    xmlRMutexUnlock(&xmlCatalogMutex);
3341
0
}
3342
3343
/**
3344
 * Free global data.
3345
 */
3346
void
3347
0
xmlCleanupCatalogInternal(void) {
3348
0
    xmlCleanupRMutex(&xmlCatalogMutex);
3349
0
}
3350
3351
/**
3352
 * Try to lookup the catalog resource for a system ID
3353
 *
3354
 * @param sysID  the system ID string
3355
 * @returns the resource if found or NULL otherwise, the value returned
3356
 *      must be freed by the caller.
3357
 */
3358
xmlChar *
3359
0
xmlCatalogResolveSystem(const xmlChar *sysID) {
3360
0
    xmlChar *ret;
3361
3362
0
    if (!xmlCatalogInitialized)
3363
0
  xmlInitializeCatalog();
3364
3365
0
    ret = xmlACatalogResolveSystem(xmlDefaultCatalog, sysID);
3366
0
    return(ret);
3367
0
}
3368
3369
/**
3370
 * Try to lookup the catalog reference associated to a public ID
3371
 *
3372
 * @param pubID  the public ID string
3373
 * @returns the resource if found or NULL otherwise, the value returned
3374
 *      must be freed by the caller.
3375
 */
3376
xmlChar *
3377
0
xmlCatalogResolvePublic(const xmlChar *pubID) {
3378
0
    xmlChar *ret;
3379
3380
0
    if (!xmlCatalogInitialized)
3381
0
  xmlInitializeCatalog();
3382
3383
0
    ret = xmlACatalogResolvePublic(xmlDefaultCatalog, pubID);
3384
0
    return(ret);
3385
0
}
3386
3387
/**
3388
 * Do a complete resolution lookup of an External Identifier
3389
 *
3390
 * @param pubID  the public ID string
3391
 * @param sysID  the system ID string
3392
 * @returns the URI of the resource or NULL if not found, it must be freed
3393
 *      by the caller.
3394
 */
3395
xmlChar *
3396
0
xmlCatalogResolve(const xmlChar *pubID, const xmlChar *sysID) {
3397
0
    xmlChar *ret;
3398
3399
0
    if (!xmlCatalogInitialized)
3400
0
  xmlInitializeCatalog();
3401
3402
0
    ret = xmlACatalogResolve(xmlDefaultCatalog, pubID, sysID);
3403
0
    return(ret);
3404
0
}
3405
3406
/**
3407
 * Do a complete resolution lookup of an URI
3408
 *
3409
 * @param URI  the URI
3410
 * @returns the URI of the resource or NULL if not found, it must be freed
3411
 *      by the caller.
3412
 */
3413
xmlChar *
3414
0
xmlCatalogResolveURI(const xmlChar *URI) {
3415
0
    xmlChar *ret;
3416
3417
0
    if (!xmlCatalogInitialized)
3418
0
  xmlInitializeCatalog();
3419
3420
0
    ret = xmlACatalogResolveURI(xmlDefaultCatalog, URI);
3421
0
    return(ret);
3422
0
}
3423
3424
#ifdef LIBXML_OUTPUT_ENABLED
3425
/**
3426
 * Dump all the global catalog content to the given file.
3427
 *
3428
 * @param out  the file.
3429
 */
3430
void
3431
0
xmlCatalogDump(FILE *out) {
3432
0
    if (out == NULL)
3433
0
  return;
3434
3435
0
    if (!xmlCatalogInitialized)
3436
0
  xmlInitializeCatalog();
3437
3438
0
    xmlACatalogDump(xmlDefaultCatalog, out);
3439
0
}
3440
3441
/**
3442
 * Dump all the global catalog content as a xmlDoc
3443
 * This function is just for testing/debugging purposes
3444
 *
3445
 * @returns  The catalog as xmlDoc or NULL if failed, it must be freed by the caller.
3446
 */
3447
xmlDocPtr
3448
0
xmlCatalogDumpDoc(void) {
3449
0
    if (!xmlCatalogInitialized)
3450
0
        xmlInitializeCatalog();
3451
3452
0
    return xmlDumpXMLCatalogToDoc(xmlDefaultCatalog->xml);
3453
0
}
3454
#endif /* LIBXML_OUTPUT_ENABLED */
3455
3456
/**
3457
 * Add an entry in the catalog, it may overwrite existing but
3458
 * different entries.
3459
 * If called before any other catalog routine, allows to override the
3460
 * default shared catalog put in place by #xmlInitializeCatalog;
3461
 *
3462
 * @param type  the type of record to add to the catalog
3463
 * @param orig  the system, public or prefix to match
3464
 * @param replace  the replacement value for the match
3465
 * @returns 0 if successful, -1 otherwise
3466
 */
3467
int
3468
0
xmlCatalogAdd(const xmlChar *type, const xmlChar *orig, const xmlChar *replace) {
3469
0
    int res = -1;
3470
3471
0
    xmlInitParser();
3472
3473
0
    xmlRMutexLock(&xmlCatalogMutex);
3474
    /*
3475
     * Specific case where one want to override the default catalog
3476
     * put in place by xmlInitializeCatalog();
3477
     */
3478
0
    if ((xmlDefaultCatalog == NULL) &&
3479
0
  (xmlStrEqual(type, BAD_CAST "catalog"))) {
3480
0
  xmlDefaultCatalog = xmlCreateNewCatalog(XML_XML_CATALOG_TYPE,
3481
0
                              xmlCatalogDefaultPrefer);
3482
0
  if (xmlDefaultCatalog != NULL) {
3483
0
     xmlDefaultCatalog->xml = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
3484
0
            orig, NULL,  xmlCatalogDefaultPrefer, NULL);
3485
0
  }
3486
0
  xmlRMutexUnlock(&xmlCatalogMutex);
3487
0
        xmlCatalogInitialized = 1;
3488
0
  return(0);
3489
0
    }
3490
3491
0
    res = xmlACatalogAdd(xmlDefaultCatalog, type, orig, replace);
3492
0
    xmlRMutexUnlock(&xmlCatalogMutex);
3493
0
    return(res);
3494
0
}
3495
3496
/**
3497
 * Remove an entry from the catalog
3498
 *
3499
 * @param value  the value to remove
3500
 * @returns the number of entries removed if successful, -1 otherwise
3501
 */
3502
int
3503
0
xmlCatalogRemove(const xmlChar *value) {
3504
0
    int res;
3505
3506
0
    if (!xmlCatalogInitialized)
3507
0
  xmlInitializeCatalog();
3508
3509
0
    xmlRMutexLock(&xmlCatalogMutex);
3510
0
    res = xmlACatalogRemove(xmlDefaultCatalog, value);
3511
0
    xmlRMutexUnlock(&xmlCatalogMutex);
3512
0
    return(res);
3513
0
}
3514
3515
#ifdef LIBXML_SGML_CATALOG_ENABLED
3516
/**
3517
 * Convert all the SGML catalog entries as XML ones
3518
 *
3519
 * @returns the number of entries converted if successful, -1 otherwise
3520
 */
3521
int
3522
0
xmlCatalogConvert(void) {
3523
0
    int res = -1;
3524
3525
0
    if (!xmlCatalogInitialized)
3526
0
  xmlInitializeCatalog();
3527
3528
0
    xmlRMutexLock(&xmlCatalogMutex);
3529
0
    res = xmlConvertSGMLCatalog(xmlDefaultCatalog);
3530
0
    xmlRMutexUnlock(&xmlCatalogMutex);
3531
0
    return(res);
3532
0
}
3533
#endif /* LIBXML_SGML_CATALOG_ENABLED */
3534
3535
/************************************************************************
3536
 *                  *
3537
 *  Public interface manipulating the common preferences    *
3538
 *                  *
3539
 ************************************************************************/
3540
3541
/**
3542
 * Used to get the user preference w.r.t. to what catalogs should
3543
 * be accepted
3544
 *
3545
 * @deprecated Use XML_PARSE_NO_SYS_CATALOG and
3546
 * XML_PARSE_CATALOG_PI.
3547
 *
3548
 * @returns the current xmlCatalogAllow value
3549
 */
3550
xmlCatalogAllow
3551
3.60k
xmlCatalogGetDefaults(void) {
3552
3.60k
    return(xmlCatalogDefaultAllow);
3553
3.60k
}
3554
3555
/**
3556
 * Used to set the user preference w.r.t. to what catalogs should
3557
 * be accepted
3558
 *
3559
 * @deprecated Use XML_PARSE_NO_SYS_CATALOG and
3560
 * XML_PARSE_CATALOG_PI.
3561
 *
3562
 * @param allow  what catalogs should be accepted
3563
 */
3564
void
3565
0
xmlCatalogSetDefaults(xmlCatalogAllow allow) {
3566
0
    if (xmlDebugCatalogs) {
3567
0
  switch (allow) {
3568
0
      case XML_CATA_ALLOW_NONE:
3569
0
    xmlCatalogPrintDebug(
3570
0
      "Disabling catalog usage\n");
3571
0
    break;
3572
0
      case XML_CATA_ALLOW_GLOBAL:
3573
0
    xmlCatalogPrintDebug(
3574
0
      "Allowing only global catalogs\n");
3575
0
    break;
3576
0
      case XML_CATA_ALLOW_DOCUMENT:
3577
0
    xmlCatalogPrintDebug(
3578
0
      "Allowing only catalogs from the document\n");
3579
0
    break;
3580
0
      case XML_CATA_ALLOW_ALL:
3581
0
    xmlCatalogPrintDebug(
3582
0
      "Allowing all catalogs\n");
3583
0
    break;
3584
0
  }
3585
0
    }
3586
0
    xmlCatalogDefaultAllow = allow;
3587
0
}
3588
3589
/**
3590
 * Allows to set the preference between public and system for deletion
3591
 * in XML Catalog resolution. C.f. section 4.1.1 of the spec
3592
 * Values accepted are XML_CATA_PREFER_PUBLIC or XML_CATA_PREFER_SYSTEM
3593
 *
3594
 * @deprecated This setting is global and not thread-safe.
3595
 *
3596
 * @param prefer  the default preference for delegation
3597
 * @returns the previous value of the default preference for delegation
3598
 */
3599
xmlCatalogPrefer
3600
0
xmlCatalogSetDefaultPrefer(xmlCatalogPrefer prefer) {
3601
0
    xmlCatalogPrefer ret = xmlCatalogDefaultPrefer;
3602
3603
0
    if (prefer == XML_CATA_PREFER_NONE)
3604
0
  return(ret);
3605
3606
0
    if (xmlDebugCatalogs) {
3607
0
  switch (prefer) {
3608
0
      case XML_CATA_PREFER_PUBLIC:
3609
0
    xmlCatalogPrintDebug(
3610
0
      "Setting catalog preference to PUBLIC\n");
3611
0
    break;
3612
0
      case XML_CATA_PREFER_SYSTEM:
3613
0
    xmlCatalogPrintDebug(
3614
0
      "Setting catalog preference to SYSTEM\n");
3615
0
    break;
3616
0
      default:
3617
0
    return(ret);
3618
0
  }
3619
0
    }
3620
0
    xmlCatalogDefaultPrefer = prefer;
3621
0
    return(ret);
3622
0
}
3623
3624
/**
3625
 * Used to set the debug level for catalog operation, 0 disable
3626
 * debugging, 1 enable it
3627
 *
3628
 * @param level  the debug level of catalogs required
3629
 * @returns the previous value of the catalog debugging level
3630
 */
3631
int
3632
0
xmlCatalogSetDebug(int level) {
3633
0
    int ret = xmlDebugCatalogs;
3634
3635
0
    if (level <= 0)
3636
0
        xmlDebugCatalogs = 0;
3637
0
    else
3638
0
  xmlDebugCatalogs = level;
3639
0
    return(ret);
3640
0
}
3641
3642
/************************************************************************
3643
 *                  *
3644
 *   Minimal interfaces used for per-document catalogs by the parser  *
3645
 *                  *
3646
 ************************************************************************/
3647
3648
/**
3649
 * Free up the memory associated to the catalog list
3650
 *
3651
 * @param catalogs  a document's list of catalogs
3652
 */
3653
void
3654
0
xmlCatalogFreeLocal(void *catalogs) {
3655
0
    xmlCatalogEntryPtr catal;
3656
3657
0
    catal = (xmlCatalogEntryPtr) catalogs;
3658
0
    if (catal != NULL)
3659
0
  xmlFreeCatalogEntryList(catal);
3660
0
}
3661
3662
3663
/**
3664
 * Add the new entry to the catalog list
3665
 *
3666
 * @param catalogs  a document's list of catalogs
3667
 * @param URL  the URL to a new local catalog
3668
 * @returns the updated list
3669
 */
3670
void *
3671
0
xmlCatalogAddLocal(void *catalogs, const xmlChar *URL) {
3672
0
    xmlCatalogEntryPtr catal, add;
3673
3674
0
    xmlInitParser();
3675
3676
0
    if (URL == NULL)
3677
0
  return(catalogs);
3678
3679
0
    if (xmlDebugCatalogs)
3680
0
  xmlCatalogPrintDebug(
3681
0
    "Adding document catalog %s\n", URL);
3682
3683
0
    add = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL, URL, NULL,
3684
0
                       xmlCatalogDefaultPrefer, NULL);
3685
0
    if (add == NULL)
3686
0
  return(catalogs);
3687
3688
0
    catal = (xmlCatalogEntryPtr) catalogs;
3689
0
    if (catal == NULL)
3690
0
  return((void *) add);
3691
3692
0
    while (catal->next != NULL)
3693
0
  catal = catal->next;
3694
0
    catal->next = add;
3695
0
    return(catalogs);
3696
0
}
3697
3698
/**
3699
 * Do a complete resolution lookup of an External Identifier using a
3700
 * document's private catalog list
3701
 *
3702
 * @param catalogs  a document's list of catalogs
3703
 * @param pubID  the public ID string
3704
 * @param sysID  the system ID string
3705
 * @returns the URI of the resource or NULL if not found, it must be freed
3706
 *      by the caller.
3707
 */
3708
xmlChar *
3709
xmlCatalogLocalResolve(void *catalogs, const xmlChar *pubID,
3710
0
                 const xmlChar *sysID) {
3711
0
    xmlCatalogEntryPtr catal;
3712
0
    xmlChar *ret;
3713
3714
0
    if ((pubID == NULL) && (sysID == NULL))
3715
0
  return(NULL);
3716
3717
0
    if (xmlDebugCatalogs) {
3718
0
        if ((pubID != NULL) && (sysID != NULL)) {
3719
0
            xmlCatalogPrintDebug(
3720
0
                            "Local Resolve: pubID %s sysID %s\n", pubID, sysID);
3721
0
        } else if (pubID != NULL) {
3722
0
            xmlCatalogPrintDebug(
3723
0
                            "Local Resolve: pubID %s\n", pubID);
3724
0
        } else {
3725
0
            xmlCatalogPrintDebug(
3726
0
                            "Local Resolve: sysID %s\n", sysID);
3727
0
        }
3728
0
    }
3729
3730
0
    catal = (xmlCatalogEntryPtr) catalogs;
3731
0
    if (catal == NULL)
3732
0
  return(NULL);
3733
0
    xmlResetCatalogResolveCache();
3734
0
    ret = xmlCatalogListXMLResolve(catal, pubID, sysID);
3735
0
    xmlResetCatalogResolveCache();
3736
0
    if ((ret != NULL) && (ret != XML_CATAL_BREAK))
3737
0
  return(ret);
3738
0
    return(NULL);
3739
0
}
3740
3741
/**
3742
 * Do a complete resolution lookup of an URI using a
3743
 * document's private catalog list
3744
 *
3745
 * @param catalogs  a document's list of catalogs
3746
 * @param URI  the URI
3747
 * @returns the URI of the resource or NULL if not found, it must be freed
3748
 *      by the caller.
3749
 */
3750
xmlChar *
3751
0
xmlCatalogLocalResolveURI(void *catalogs, const xmlChar *URI) {
3752
0
    xmlCatalogEntryPtr catal;
3753
0
    xmlChar *ret;
3754
3755
0
    if (URI == NULL)
3756
0
  return(NULL);
3757
3758
0
    if (xmlDebugCatalogs)
3759
0
  xmlCatalogPrintDebug(
3760
0
    "Resolve URI %s\n", URI);
3761
3762
0
    catal = (xmlCatalogEntryPtr) catalogs;
3763
0
    if (catal == NULL)
3764
0
  return(NULL);
3765
0
    ret = xmlCatalogListXMLResolveURI(catal, URI);
3766
0
    if ((ret != NULL) && (ret != XML_CATAL_BREAK))
3767
0
  return(ret);
3768
0
    return(NULL);
3769
0
}
3770
3771
/************************************************************************
3772
 *                  *
3773
 *      Deprecated interfaces       *
3774
 *                  *
3775
 ************************************************************************/
3776
/**
3777
 * Try to lookup the catalog reference associated to a system ID
3778
 *
3779
 * @deprecated use #xmlCatalogResolveSystem
3780
 *
3781
 * @param sysID  the system ID string
3782
 * @returns the resource if found or NULL otherwise.
3783
 */
3784
const xmlChar *
3785
0
xmlCatalogGetSystem(const xmlChar *sysID) {
3786
0
    xmlChar *ret;
3787
0
    static xmlChar result[1000];
3788
0
    static int msg = 0;
3789
3790
0
    if (!xmlCatalogInitialized)
3791
0
  xmlInitializeCatalog();
3792
3793
0
    if (msg == 0) {
3794
0
  xmlPrintErrorMessage(
3795
0
    "Use of deprecated xmlCatalogGetSystem() call\n");
3796
0
  msg++;
3797
0
    }
3798
3799
0
    if (sysID == NULL)
3800
0
  return(NULL);
3801
3802
    /*
3803
     * Check first the XML catalogs
3804
     */
3805
0
    if (xmlDefaultCatalog != NULL) {
3806
0
        xmlResetCatalogResolveCache();
3807
0
        ret = xmlCatalogListXMLResolve(xmlDefaultCatalog->xml, NULL, sysID);
3808
0
        xmlResetCatalogResolveCache();
3809
0
  if ((ret != NULL) && (ret != XML_CATAL_BREAK)) {
3810
0
      snprintf((char *) result, sizeof(result) - 1, "%s", (char *) ret);
3811
0
      result[sizeof(result) - 1] = 0;
3812
0
      return(result);
3813
0
  }
3814
0
    }
3815
3816
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
3817
0
    if (xmlDefaultCatalog != NULL)
3818
0
  return(xmlCatalogGetSGMLSystem(xmlDefaultCatalog->sgml, sysID));
3819
0
#endif
3820
0
    return(NULL);
3821
0
}
3822
3823
/**
3824
 * Try to lookup the catalog reference associated to a public ID
3825
 *
3826
 * @deprecated use #xmlCatalogResolvePublic
3827
 *
3828
 * @param pubID  the public ID string
3829
 * @returns the resource if found or NULL otherwise.
3830
 */
3831
const xmlChar *
3832
0
xmlCatalogGetPublic(const xmlChar *pubID) {
3833
0
    xmlChar *ret;
3834
0
    static xmlChar result[1000];
3835
0
    static int msg = 0;
3836
3837
0
    if (!xmlCatalogInitialized)
3838
0
  xmlInitializeCatalog();
3839
3840
0
    if (msg == 0) {
3841
0
  xmlPrintErrorMessage(
3842
0
    "Use of deprecated xmlCatalogGetPublic() call\n");
3843
0
  msg++;
3844
0
    }
3845
3846
0
    if (pubID == NULL)
3847
0
  return(NULL);
3848
3849
    /*
3850
     * Check first the XML catalogs
3851
     */
3852
0
    if (xmlDefaultCatalog != NULL) {
3853
0
        xmlResetCatalogResolveCache();
3854
0
        ret = xmlCatalogListXMLResolve(xmlDefaultCatalog->xml, pubID, NULL);
3855
0
        xmlResetCatalogResolveCache();
3856
0
  if ((ret != NULL) && (ret != XML_CATAL_BREAK)) {
3857
0
      snprintf((char *) result, sizeof(result) - 1, "%s", (char *) ret);
3858
0
      result[sizeof(result) - 1] = 0;
3859
0
      return(result);
3860
0
  }
3861
0
    }
3862
3863
0
#ifdef LIBXML_SGML_CATALOG_ENABLED
3864
0
    if (xmlDefaultCatalog != NULL)
3865
0
  return(xmlCatalogGetSGMLPublic(xmlDefaultCatalog->sgml, pubID));
3866
0
#endif
3867
0
    return(NULL);
3868
0
}
3869
3870
#endif /* LIBXML_CATALOG_ENABLED */