1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
|
#include "catalog.h"
#include <stdlib.h>
#include <stdio.h>
#include "system.h"
#include "catutil.h"
#include "string.h"
#include "string16.h"
static char *res_ext(Catalog catalog, char *file,
const char *public, const char *system, Prefer prefer);
static char *res_uri(Catalog catalog, char *file, const char *uri);
static int entry_compare(const void *a, const void *b);
/* used internally to indicate failure */
static char *fail = "fail";
char *ResolveExternalIdentifier(Catalog catalog,
const char *public, const char *system,
Prefer prefer)
{
int i;
char *temp;
if(catalog_debug)
fprintf(stderr,
"resolving external identifier <%s> <%s> with prefer=%s\n",
public ? public : "(null)", system ? system : "(null)",
PreferName[prefer]);
/* Normalize and unwrap */
if(IsPublicidUrn(public))
{
if(!(temp = UnwrapPublicidUrn(public)) ||
!(public = NormalizePublic8(temp)))
return 0;
Free(temp);
}
else
if(public && !(public = NormalizePublic8(public)))
return 0;
if(IsPublicidUrn(system))
{
if(!(temp = UnwrapPublicidUrn(system)) ||
/* NB normalize as public because that's what it will end up as! */
!(system = NormalizePublic8(temp)))
return 0;
Free(temp);
if(public)
{
if(strcmp(public, system) == 0)
{
Free((void *)system);
system = 0;
}
else
{
Fprintf(Stderr, "Unwrapped publicid-urn system id %s does not match public id %s, discarding\n",
system, public);
Free((void *)system);
system = 0;
}
}
else
{
public = system;
system = 0;
}
}
else
if(system && !(system = NormalizeSystem8(system)))
return 0;
if(catalog_debug)
fprintf(stderr,
"after normalizing and unwrapping: <%s> <%s>\n",
public ? public : "(null)", system ? system : "(null)");
/* Search the catalogs in the path */
for(i=0; i<VectorCount(catalog->path); i )
{
char *result =
res_ext(catalog, catalog->path[i], public, system, prefer);
if(result == fail)
return 0;
if(result)
return result;
}
return 0;
}
static char *res_ext(Catalog catalog, char *file,
const char *public, const char *system, Prefer prefer)
{
int i;
int len, best_length, prefix_length, nmatches;
char *best_value = 0, *result = 0; /* init to please gcc */
CatalogEntryFile cef;
CatalogEntry entry;
Vector(CatalogEntry, delegate_list);
Prefer p;
if(catalog_debug)
fprintf(stderr, "looking for <%s> <%s> in %s\n",
public ? public : "(null)",
system ? system : "(null)",
file);
if(!(cef = GetCatalogEntryFile(catalog, file)))
return fail;
if(!system)
goto try_public; /* a gratuitous goto */
/* Try for a matching system entry */
if(catalog_debug)
fprintf(stderr, "trying %d system entries\n",
VectorCount(cef->systemEntries));
for(i=0; i<VectorCount(cef->systemEntries); i )
{
entry = cef->systemEntries[i];
if(strcmp(system, entry->match) == 0)
{
if(catalog_debug)
fprintf(stderr, "matched %s, returning %s\n",
entry->match, entry->value);
return strdup8(entry->value);
}
}
/* Try for a matching rewriteSystem entry */
if(catalog_debug)
fprintf(stderr, "trying %d rewriteSystem entries\n",
VectorCount(cef->rewriteSystemEntries));
best_length = 0;
for(i=0; i<VectorCount(cef->rewriteSystemEntries); i )
{
entry = cef->rewriteSystemEntries[i];
len = strlen(entry->match);
if(len > best_length &&
strncmp(system, entry->match, len) == 0)
{
best_length = len;
best_value = entry->value;
}
}
if(best_length > 0)
{
prefix_length = strlen(best_value);
if(!(result = Malloc(prefix_length strlen(system best_length) 1)))
return fail;
strcpy(result, best_value);
strcpy(result prefix_length, system best_length);
if(catalog_debug)
fprintf(stderr, "best match %s (%d), returning %s\n",
best_value, prefix_length, result);
return result;
}
/* Try for a matching delegateSystem entry */
if(catalog_debug)
fprintf(stderr, "trying %d delegateSystem entries\n",
VectorCount(cef->delegateSystemEntries));
VectorInit(delegate_list);
for(i=0; i<VectorCount(cef->delegateSystemEntries); i )
{
entry = cef->delegateSystemEntries[i];
len = strlen(entry->match);
if(strncmp(system, entry->match, len) == 0)
if(!VectorPush(delegate_list, entry))
return fail;
}
if((nmatches = VectorCount(delegate_list)) > 0)
{
qsort((void *)delegate_list, nmatches, sizeof(delegate_list[0]),
entry_compare);
if(catalog_debug)
{
fprintf(stderr, "%d matches:\n", nmatches);
for(i=0; i<nmatches; i )
fprintf(stderr, " %s -> %s\n",
delegate_list[i]->match, delegate_list[i]->value);
}
for(i=0; i<nmatches; i )
{
result =
res_ext(catalog, delegate_list[i]->value, 0, system, prefer);
if(result)
break;
}
Free(delegate_list);
return result;
}
try_public:
if(!public)
goto try_more_files;
/* Try for a matching public entry */
if(catalog_debug)
fprintf(stderr, "trying %d public entries\n",
VectorCount(cef->publicEntries));
for(i=0; i<VectorCount(cef->publicEntries); i )
{
entry = cef->publicEntries[i];
p = entry->prefer == PR_unspecified ? prefer : entry->prefer;
if(system && p != PR_public)
continue;
if(strcmp(public, entry->match) == 0)
{
if(catalog_debug)
fprintf(stderr, "matched %s, returning %s\n",
entry->match, entry->value);
return strdup8(entry->value);
}
}
/* Try for a matching delegatePublic entry */
if(catalog_debug)
fprintf(stderr, "trying %d delegatePublic entries\n",
VectorCount(cef->delegatePublicEntries));
VectorInit(delegate_list);
for(i=0; i<VectorCount(cef->delegatePublicEntries); i )
{
entry = cef->delegatePublicEntries[i];
p = entry->prefer == PR_unspecified ? prefer : entry->prefer;
if(system && p != PR_public)
continue;
len = strlen(entry->match);
if(strncmp(public, entry->match, len) == 0)
if(!VectorPush(delegate_list, entry))
return fail;
}
if((nmatches = VectorCount(delegate_list)) > 0)
{
qsort((void *)delegate_list, nmatches, sizeof(delegate_list[0]),
entry_compare);
if(catalog_debug)
{
fprintf(stderr, "%d matches:\n", nmatches);
for(i=0; i<nmatches; i )
fprintf(stderr, " %s -> %s\n",
delegate_list[i]->match, delegate_list[i]->value);
}
for(i=0; i<nmatches; i )
{
result =
res_ext(catalog, delegate_list[i]->value, public, 0, prefer);
if(result)
break;
}
Free(delegate_list);
return result;
}
try_more_files:
/* Try any catalog files specified in nextCatalog entries */
if(catalog_debug)
fprintf(stderr, "trying %d nextCatalog entries\n",
VectorCount(cef->nextCatalogEntries));
result = 0;
for(i=0; i<VectorCount(cef->nextCatalogEntries); i )
{
result = res_ext(catalog, cef->nextCatalogEntries[i], public, system, prefer);
if(result)
break;
}
if(result)
return result;
return 0;
}
char *ResolveURI(Catalog catalog, const char *uri)
{
int i, is_publicid_urn;
char *temp;
if(catalog_debug)
fprintf(stderr, "resolving uri <%s>\n", uri);
/* Normalize and unwrap */
is_publicid_urn = IsPublicidUrn(uri);
if(is_publicid_urn)
{
if(!(temp = UnwrapPublicidUrn(uri)) ||
!(uri = NormalizePublic8(temp)))
return 0;
Free(temp);
}
else
uri = NormalizeSystem8(uri);
if(catalog_debug)
fprintf(stderr, "after normalizing and unwrapping: <%s>\n", uri);
/* Search the catalogs in the path */
for(i=0; i<VectorCount(catalog->path); i )
{
char *result;
if(is_publicid_urn)
result = res_ext(catalog, catalog->path[i], uri, 0, PR_public);
else
result = res_uri(catalog, catalog->path[i], uri);
if(result == fail)
return 0;
if(result)
return result;
}
return 0;
}
static char *res_uri(Catalog catalog, char *file, const char *uri)
{
int i;
int len, best_length, prefix_length, nmatches;
char *best_value = 0, *result = 0; /* init to please gcc */
CatalogEntryFile cef;
CatalogEntry entry;
Vector(CatalogEntry, delegate_list);
if(catalog_debug)
fprintf(stderr, "looking for <%s> in %s\n", uri, file);
if(!(cef = GetCatalogEntryFile(catalog, file)))
return fail;
/* Try for a matching uri entry */
if(catalog_debug)
fprintf(stderr, "trying %d uri entries\n",
VectorCount(cef->uriEntries));
for(i=0; i<VectorCount(cef->uriEntries); i )
{
entry = cef->uriEntries[i];
if(strcmp(uri, entry->match) == 0)
{
if(catalog_debug)
fprintf(stderr, "matched %s, returning %s\n",
entry->match, entry->value);
return entry->value;
}
}
/* Try for a matching rewriteURI entry */
if(catalog_debug)
fprintf(stderr, "trying %d rewriteURI entries\n",
VectorCount(cef->rewriteURIEntries));
best_length = 0;
for(i=0; i<VectorCount(cef->rewriteURIEntries); i )
{
entry = cef->rewriteURIEntries[i];
len = strlen(entry->match);
if(len > best_length &&
strncmp(uri, entry->match, len) == 0)
{
best_length = len;
best_value = entry->value;
}
}
if(best_length > 0)
{
prefix_length = strlen(best_value);
if(!(result = Malloc(prefix_length strlen(uri best_length) 1)))
return fail;
strcpy(result, best_value);
strcpy(result prefix_length, uri best_length);
if(catalog_debug)
fprintf(stderr, "best match %s (%d), returning %s\n",
best_value, prefix_length, result);
return result;
}
/* Try for a matching delegateURI entry */
if(catalog_debug)
fprintf(stderr, "trying %d delegateURI entries\n",
VectorCount(cef->delegateURIEntries));
VectorInit(delegate_list);
for(i=0; i<VectorCount(cef->delegateURIEntries); i )
{
entry = cef->delegateURIEntries[i];
len = strlen(entry->match);
if(strncmp(uri, entry->match, len) == 0)
if(!VectorPush(delegate_list, entry))
return fail;
}
if((nmatches = VectorCount(delegate_list)) > 0)
{
qsort((void *)delegate_list, nmatches, sizeof(delegate_list[0]),
entry_compare);
if(catalog_debug)
{
fprintf(stderr, "%d matches:\n", nmatches);
for(i=0; i<nmatches; i )
fprintf(stderr, " %s -> %s\n",
delegate_list[i]->match, delegate_list[i]->value);
}
for(i=0; i<nmatches; i )
{
result = res_uri(catalog, delegate_list[i]->value, uri);
if(result)
break;
}
Free(delegate_list);
return result;
}
/* Try any catalog files specified in nextCatalog entries */
if(catalog_debug)
fprintf(stderr, "trying %d nextCatalog entries\n",
VectorCount(cef->nextCatalogEntries));
result = 0;
for(i=0; i<VectorCount(cef->nextCatalogEntries); i )
{
result = res_uri(catalog, cef->nextCatalogEntries[i], uri);
if(result)
break;
}
if(result)
return result;
return 0;
}
static int entry_compare(const void *a, const void *b)
{
/* longest first, i.e. long < short */
return strlen((*(CatalogEntry *)b)->match) -
strlen((*(CatalogEntry *)a)->match);
}
|