Coverage Report

Created: 2024-09-06 07:53

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