Coverage Report

Created: 2025-08-26 06:41

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