]> git.wh0rd.org - fontconfig.git/blame - src/fccache.c
Fix matching bug when multiple elements match; don't use the sum of all
[fontconfig.git] / src / fccache.c
CommitLineData
24330d27 1/*
4bd4418a 2 * $RCSId: xc/lib/fontconfig/src/fccache.c,v 1.12 2002/08/22 07:36:44 keithp Exp $
24330d27 3 *
46b51147 4 * Copyright © 2000 Keith Packard
03a212e5 5 * Copyright © 2005 Patrick Lam
24330d27
KP
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of Keith Packard not be used in
12 * advertising or publicity pertaining to distribution of the software without
13 * specific, written prior permission. Keith Packard makes no
14 * representations about the suitability of this software for any purpose. It
15 * is provided "as is" without express or implied warranty.
16 *
17 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
23 * PERFORMANCE OF THIS SOFTWARE.
24 */
25
212c9f43 26#include <fcntl.h>
4262e0b3 27#include <dirent.h>
ea44e218 28#include <string.h>
212c9f43
PL
29#include <sys/mman.h>
30#include <sys/utsname.h>
fd77c154
PL
31#include <sys/types.h>
32#include <unistd.h>
24330d27 33#include "fcint.h"
7fd7221e 34#include <unistd.h>
24330d27 35
2dbe7597 36#define ENDIAN_TEST 0x12345678
7fd7221e 37#define MACHINE_SIGNATURE_SIZE 9 + 5*20 + 1
2dbe7597 38
ea44e218
PL
39static int
40FcDirCacheOpen (char * cache_file);
41
42static char *
43FcDirCacheHashName (char * cache_file, int collisions);
44
eb0cf671 45static off_t
c60ec7cc 46FcCacheSkipToArch (int fd, const char * arch);
1b7be377 47
eb0cf671 48static FcBool
1d879de2 49FcCacheCopyOld (int fd, int fd_orig, off_t start);
1b7be377 50
eb0cf671
PL
51static void *
52FcDirCacheProduce (FcFontSet *set, FcCache * metadata);
1b7be377
PL
53
54static FcBool
e77c1718 55FcDirCacheConsume (int fd, const char * dir, FcFontSet *set);
1b7be377 56
2eb84374 57FcBool
2304e38f 58FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir);
1b7be377 59
eb0cf671
PL
60static int
61FcCacheNextOffset(off_t w);
1b7be377 62
eb0cf671
PL
63static char *
64FcCacheMachineSignature (void);
1b7be377
PL
65
66static FcBool
eb0cf671 67FcCacheHaveBank (int bank);
1b7be377 68
e77c1718
PL
69static void
70FcCacheAddBankDir (int bank, const char * dir);
71
ea44e218
PL
72struct MD5Context {
73 FcChar32 buf[4];
74 FcChar32 bits[2];
75 unsigned char in[64];
76};
77
78static void MD5Init(struct MD5Context *ctx);
79static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len);
80static void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
81static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]);
82
eb0cf671 83#define FC_DBG_CACHE_REF 1024
1b7be377 84
8245771d
PL
85static char *
86FcCacheReadString (int fd, char *dest, int len)
24330d27 87{
212c9f43 88 FcChar8 c;
ccb3e93b 89 FcBool escape;
ccb3e93b
KP
90 int size;
91 int i;
24330d27 92
24330d27 93 if (len == 0)
03a212e5 94 return 0;
24330d27 95
ccb3e93b
KP
96 size = len;
97 i = 0;
24330d27 98 escape = FcFalse;
212c9f43 99 while (read (fd, &c, 1) == 1)
24330d27
KP
100 {
101 if (!escape)
102 {
103 switch (c) {
104 case '"':
ccb3e93b
KP
105 c = '\0';
106 break;
24330d27
KP
107 case '\\':
108 escape = FcTrue;
109 continue;
110 }
111 }
ccb3e93b
KP
112 if (i == size)
113 {
212c9f43
PL
114 dest[i++] = 0;
115 return dest;
ccb3e93b 116 }
212c9f43 117 dest[i++] = c;
ccb3e93b 118 if (c == '\0')
212c9f43 119 return dest;
24330d27
KP
120 escape = FcFalse;
121 }
ccb3e93b 122 return 0;
24330d27
KP
123}
124
ea44e218
PL
125static void
126FcCacheSkipString (int fd)
127{
128 FcChar8 c;
129 FcBool escape;
130
131 escape = FcFalse;
132 while (read (fd, &c, 1) == 1)
133 {
134 if (!escape)
135 {
136 switch (c) {
137 case '"':
138 c = '\0';
139 break;
140 case '\\':
141 escape = FcTrue;
142 continue;
143 }
144 }
145 if (c == '\0')
146 return;
147 escape = FcFalse;
148 }
149 return;
150}
151
24330d27 152static FcBool
8245771d 153FcCacheWriteString (int fd, const char *chars)
327a7fd4 154{
212c9f43 155 if (write (fd, chars, strlen(chars)+1) != strlen(chars)+1)
327a7fd4 156 return FcFalse;
327a7fd4
KP
157 return FcTrue;
158}
159
eb0cf671
PL
160static void
161FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
1b7be377 162{
5e678e94
PL
163 FcMemFree (FC_MEM_STRING, strlen (d->name)+1);
164 free (d->name);
165 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir));
166 free (d);
1b7be377
PL
167}
168
eb0cf671
PL
169FcGlobalCache *
170FcGlobalCacheCreate (void)
1b7be377 171{
eb0cf671 172 FcGlobalCache *cache;
1b7be377 173
eb0cf671
PL
174 cache = malloc (sizeof (FcGlobalCache));
175 if (!cache)
176 return 0;
177 FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCache));
178 cache->dirs = 0;
179 cache->updated = FcFalse;
180 cache->fd = -1;
181 return cache;
1b7be377
PL
182}
183
eb0cf671
PL
184void
185FcGlobalCacheDestroy (FcGlobalCache *cache)
327a7fd4 186{
eb0cf671 187 FcGlobalCacheDir *d, *next;
327a7fd4 188
eb0cf671 189 for (d = cache->dirs; d; d = next)
327a7fd4 190 {
eb0cf671
PL
191 next = d->next;
192 FcGlobalCacheDirDestroy (d);
327a7fd4 193 }
eb0cf671
PL
194 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache));
195 free (cache);
327a7fd4
KP
196}
197
198void
eb0cf671 199FcGlobalCacheLoad (FcGlobalCache *cache,
03a212e5 200 FcStrSet *staleDirs,
2b25f00c
PL
201 const FcChar8 *cache_file,
202 FcConfig *config)
327a7fd4 203{
ea44e218 204 char name_buf[FC_MAX_FILE_LEN];
eb0cf671 205 FcGlobalCacheDir *d, *next;
2b25f00c 206 FcFileTime config_time = FcConfigModifiedTime (config);
eb0cf671
PL
207 char * current_arch_machine_name;
208 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
209 off_t current_arch_start;
327a7fd4 210
03a212e5
PL
211 struct stat cache_stat, dir_stat;
212
213 if (stat ((char *) cache_file, &cache_stat) < 0)
214 return;
215
eb0cf671
PL
216 cache->fd = open ((char *) cache_file, O_RDONLY);
217 if (cache->fd == -1)
218 return;
327a7fd4 219
eb0cf671 220 cache->updated = FcFalse;
327a7fd4 221
c60ec7cc
PL
222 FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
223 if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) != 0)
224 return;
225
eb0cf671
PL
226 current_arch_machine_name = FcCacheMachineSignature ();
227 current_arch_start = FcCacheSkipToArch(cache->fd,
c60ec7cc 228 current_arch_machine_name);
eb0cf671 229 if (current_arch_start < 0)
e58b50e8 230 goto bail_and_destroy;
327a7fd4 231
eb0cf671 232 lseek (cache->fd, current_arch_start, SEEK_SET);
03a212e5
PL
233 FcCacheReadString (cache->fd, candidate_arch_machine_name,
234 sizeof (candidate_arch_machine_name));
235 if (strlen(candidate_arch_machine_name) == 0)
e58b50e8 236 goto bail_and_destroy;
1b7be377 237
eb0cf671 238 while (1)
1b7be377 239 {
8cb4c56d
PL
240 off_t targ;
241
eb0cf671
PL
242 FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
243 if (!strlen(name_buf))
1b7be377 244 break;
1b7be377 245
9090cb9e
PL
246 /* Directory must be older than the global cache file; also
247 cache must be newer than the config file. */
03a212e5 248 if (stat ((char *) name_buf, &dir_stat) < 0 ||
2b25f00c 249 dir_stat.st_mtime > cache_stat.st_mtime ||
9090cb9e 250 (config_time.set && cache_stat.st_mtime < config_time.time))
03a212e5
PL
251 {
252 FcCache md;
253
8245771d 254 FcStrSetAdd (staleDirs, FcStrCopy ((FcChar8 *)name_buf));
03a212e5
PL
255 read (cache->fd, &md, sizeof (FcCache));
256 lseek (cache->fd, FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count, SEEK_SET);
257 continue;
258 }
259
eb0cf671
PL
260 d = malloc (sizeof (FcGlobalCacheDir));
261 if (!d)
262 goto bail1;
1b7be377 263
eb0cf671
PL
264 d->next = cache->dirs;
265 cache->dirs = d;
1b7be377 266
8245771d 267 d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
eb0cf671
PL
268 d->ent = 0;
269 d->offset = lseek (cache->fd, 0, SEEK_CUR);
8cb4c56d
PL
270 if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
271 goto bail1;
272 targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
273 if (lseek (cache->fd, targ, SEEK_SET) != targ)
274 goto bail1;
1b7be377 275 }
eb0cf671 276 return;
1b7be377 277
eb0cf671
PL
278 bail1:
279 for (d = cache->dirs; d; d = next)
1b7be377 280 {
eb0cf671
PL
281 next = d->next;
282 free (d);
1b7be377 283 }
eb0cf671 284 cache->dirs = 0;
e58b50e8
PL
285
286 close (cache->fd);
287 cache->fd = -1;
288 return;
289
290 bail_and_destroy:
eb0cf671
PL
291 close (cache->fd);
292 cache->fd = -1;
e58b50e8
PL
293
294 if (stat ((char *) cache_file, &cache_stat) == 0)
295 unlink ((char *)cache_file);
296
eb0cf671 297 return;
e58b50e8 298
1b7be377
PL
299}
300
1b7be377 301FcBool
8245771d 302FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config)
1b7be377 303{
eb0cf671 304 FcGlobalCacheDir *d;
03a212e5 305 FcBool ret = FcFalse;
1b7be377 306
eb0cf671 307 if (cache->fd == -1)
1b7be377 308 return FcFalse;
1b7be377 309
eb0cf671 310 for (d = cache->dirs; d; d = d->next)
1b7be377 311 {
03a212e5 312 if (strncmp (d->name, dir, strlen(dir)) == 0)
1b7be377 313 {
eb0cf671 314 lseek (cache->fd, d->offset, SEEK_SET);
e77c1718 315 if (!FcDirCacheConsume (cache->fd, dir, set))
eb0cf671 316 return FcFalse;
03a212e5
PL
317 if (strcmp (d->name, dir) == 0)
318 ret = FcTrue;
1b7be377 319 }
1b7be377 320 }
1b7be377 321
03a212e5 322 return ret;
1b7be377
PL
323}
324
eb0cf671
PL
325FcBool
326FcGlobalCacheUpdate (FcGlobalCache *cache,
8245771d 327 const char *name,
eb0cf671 328 FcFontSet *set)
1b7be377 329{
eb0cf671 330 FcGlobalCacheDir * d;
1b7be377 331
eb0cf671 332 for (d = cache->dirs; d; d = d->next)
1b7be377 333 {
eb0cf671 334 if (strcmp(d->name, name) == 0)
1b7be377 335 break;
24330d27 336 }
24330d27 337
eb0cf671 338 if (!d)
24330d27 339 {
eb0cf671
PL
340 d = malloc (sizeof (FcGlobalCacheDir));
341 if (!d)
342 return FcFalse;
343 d->next = cache->dirs;
344 cache->dirs = d;
24330d27 345 }
24330d27 346
eb0cf671 347 cache->updated = FcTrue;
24330d27 348
8245771d 349 d->name = (char *)FcStrCopy ((FcChar8 *)name);
eb0cf671
PL
350 d->ent = FcDirCacheProduce (set, &d->metadata);
351 d->offset = 0;
352 return FcTrue;
24330d27
KP
353}
354
355FcBool
327a7fd4
KP
356FcGlobalCacheSave (FcGlobalCache *cache,
357 const FcChar8 *cache_file)
24330d27 358{
1d879de2 359 int fd, fd_orig;
327a7fd4 360 FcGlobalCacheDir *dir;
327a7fd4 361 FcAtomic *atomic;
eb0cf671
PL
362 off_t current_arch_start = 0, truncate_to;
363 char * current_arch_machine_name, * header;
24330d27 364
eb0cf671 365 if (!cache->updated)
24330d27
KP
366 return FcTrue;
367
daeed6e0 368#if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
a391da8f
KP
369 /* Set-UID programs can't safely update the cache */
370 if (getuid () != geteuid ())
371 return FcFalse;
daeed6e0 372#endif
a391da8f 373
134f6011
KP
374 atomic = FcAtomicCreate (cache_file);
375 if (!atomic)
1d879de2
PL
376 return FcFalse;
377
134f6011 378 if (!FcAtomicLock (atomic))
24330d27 379 goto bail1;
eb0cf671
PL
380 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT,
381 S_IRUSR | S_IWUSR);
382 if (fd == -1)
24330d27
KP
383 goto bail2;
384
1d879de2
PL
385 fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY);
386
eb0cf671 387 current_arch_machine_name = FcCacheMachineSignature ();
1d879de2
PL
388 if (fd_orig == -1)
389 current_arch_start = 0;
390 else
391 current_arch_start = FcCacheSkipToArch (fd_orig,
c60ec7cc 392 current_arch_machine_name);
1d879de2 393
eb0cf671 394 if (current_arch_start < 0)
649cc361 395 current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
eb0cf671 396
1d879de2
PL
397 if (!FcCacheCopyOld(fd, fd_orig, current_arch_start))
398 goto bail3;
399
400 close (fd_orig);
401 fd_orig = -1;
eb0cf671
PL
402
403 current_arch_start = lseek(fd, 0, SEEK_CUR);
404 if (ftruncate (fd, current_arch_start) == -1)
1d879de2 405 goto bail3;
eb0cf671 406
03a212e5
PL
407 header = malloc (10 + strlen (current_arch_machine_name));
408 if (!header)
1d879de2 409 goto bail3;
03a212e5 410
8cb4c56d 411 truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11;
eb0cf671 412 for (dir = cache->dirs; dir; dir = dir->next)
24330d27 413 {
eb0cf671
PL
414 truncate_to += strlen(dir->name) + 1;
415 truncate_to += sizeof (FcCache);
1c5b6345 416 truncate_to = FcCacheNextOffset (truncate_to);
eb0cf671
PL
417 truncate_to += dir->metadata.count;
418 }
419 truncate_to -= current_arch_start;
eb0cf671 420
c60ec7cc 421 FcCacheWriteString (fd, FC_GLOBAL_MAGIC_COOKIE);
8cb4c56d
PL
422 sprintf (header, "%8x ", (int)truncate_to);
423 strcat (header, current_arch_machine_name);
424 if (!FcCacheWriteString (fd, header))
1d879de2 425 goto bail4;
8cb4c56d 426
eb0cf671
PL
427 for (dir = cache->dirs; dir; dir = dir->next)
428 {
c60ec7cc 429 if (dir->name)
5e678e94
PL
430 {
431 FcCacheWriteString (fd, dir->name);
432 write (fd, &dir->metadata, sizeof(FcCache));
f6ee3db5 433 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET);
5e678e94
PL
434 write (fd, dir->ent, dir->metadata.count);
435 free (dir->ent);
436 }
24330d27 437 }
eb0cf671 438 FcCacheWriteString (fd, "");
24330d27 439
eb0cf671 440 if (close (fd) == -1)
1d879de2 441 goto bail25;
24330d27 442
134f6011 443 if (!FcAtomicReplaceOrig (atomic))
1d879de2 444 goto bail25;
24330d27 445
134f6011
KP
446 FcAtomicUnlock (atomic);
447 FcAtomicDestroy (atomic);
448
24330d27
KP
449 cache->updated = FcFalse;
450 return FcTrue;
451
1d879de2
PL
452 bail4:
453 free (header);
454 bail3:
455 if (fd_orig != -1)
456 close (fd_orig);
457
458 close (fd);
459 bail25:
134f6011 460 FcAtomicDeleteNew (atomic);
1d879de2 461 bail2:
134f6011 462 FcAtomicUnlock (atomic);
1d879de2 463 bail1:
134f6011 464 FcAtomicDestroy (atomic);
24330d27
KP
465 return FcFalse;
466}
467
212c9f43 468/*
eb0cf671
PL
469 * Find the next presumably-mmapable offset after the supplied file
470 * position.
212c9f43 471 */
4262e0b3
PL
472static int
473FcCacheNextOffset(off_t w)
179c3995 474{
7fd7221e
PL
475 static long pagesize = -1;
476 if (pagesize == -1)
477 pagesize = sysconf(_SC_PAGESIZE);
478 if (w % pagesize == 0)
212c9f43
PL
479 return w;
480 else
7fd7221e 481 return ((w / pagesize)+1)*pagesize;
179c3995
KP
482}
483
212c9f43
PL
484/* return the address of the segment for the provided arch,
485 * or -1 if arch not found */
486static off_t
c60ec7cc 487FcCacheSkipToArch (int fd, const char * arch)
212c9f43 488{
2dbe7597
PL
489 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
490 char * candidate_arch;
212c9f43
PL
491 off_t current_arch_start = 0;
492
ea44e218 493 lseek (fd, 0, SEEK_SET);
c60ec7cc 494 FcCacheSkipString (fd);
ea44e218
PL
495 current_arch_start = lseek (fd, 0, SEEK_CUR);
496
212c9f43
PL
497 /* skip arches that are not the current arch */
498 while (1)
499 {
500 long bs;
501
8cb4c56d
PL
502 if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
503 return -1;
504
eb0cf671 505 if (FcCacheReadString (fd, candidate_arch_machine_name_count,
2dbe7597 506 sizeof (candidate_arch_machine_name_count)) == 0)
5e678e94 507 return -1;
2dbe7597
PL
508 if (!strlen(candidate_arch_machine_name_count))
509 return -1;
510 bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16);
8cb4c56d 511
13cdf607 512 // count = 0 should probably be distinguished from the !bs condition
8cb4c56d
PL
513 if (!bs || bs < strlen (candidate_arch_machine_name_count))
514 return -1;
515
2dbe7597 516 candidate_arch++; /* skip leading space */
212c9f43 517
2dbe7597 518 if (strcmp (candidate_arch, arch)==0)
5e678e94 519 return current_arch_start;
212c9f43
PL
520 current_arch_start += bs;
521 }
522
5e678e94 523 return -1;
212c9f43
PL
524}
525
526/* Cuts out the segment at the file pointer (moves everything else
527 * down to cover it), and leaves the file pointer at the end of the
528 * file. */
212c9f43 529static FcBool
1d879de2 530FcCacheCopyOld (int fd, int fd_orig, off_t start)
212c9f43 531{
eb0cf671 532 char * buf = malloc (8192);
2dbe7597 533 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
4262e0b3 534 long bs;
212c9f43 535 int c, bytes_skipped;
1d879de2 536 off_t loc;
212c9f43
PL
537
538 if (!buf)
539 return FcFalse;
540
1d879de2
PL
541 loc = 0;
542 lseek (fd, 0, SEEK_SET); lseek (fd_orig, 0, SEEK_SET);
543 do
544 {
545 int b = 8192;
546 if (loc + b > start)
547 b = start - loc;
548
549 if ((c = read (fd_orig, buf, b)) <= 0)
550 break;
551 if (write (fd, buf, c) < 0)
552 goto bail;
553
554 loc += c;
555 }
556 while (c > 0);
557
212c9f43 558 lseek (fd, start, SEEK_SET);
eb0cf671 559 if (FcCacheReadString (fd, candidate_arch_machine_name,
212c9f43
PL
560 sizeof (candidate_arch_machine_name)) == 0)
561 goto done;
2dbe7597 562 if (!strlen(candidate_arch_machine_name))
212c9f43
PL
563 goto done;
564
2dbe7597 565 bs = strtol(candidate_arch_machine_name, 0, 16);
212c9f43
PL
566 if (bs == 0)
567 goto done;
568
569 bytes_skipped = 0;
570 do
571 {
572 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
eb0cf671 573 if ((c = read (fd, buf, 8192)) <= 0)
212c9f43
PL
574 break;
575 lseek (fd, start+bytes_skipped, SEEK_SET);
576 if (write (fd, buf, c) < 0)
577 goto bail;
578 bytes_skipped += c;
579 }
580 while (c > 0);
581 lseek (fd, start+bytes_skipped, SEEK_SET);
582
583 done:
584 free (buf);
585 return FcTrue;
586
587 bail:
588 free (buf);
589 return FcFalse;
590}
591
55c8fa4f 592/* Does not check that the cache has the appropriate arch section. */
ec760b17
PL
593/* Also, this can be fooled if the original location has a stale
594 * cache, and the hashed location has an up-to-date cache. Oh well,
595 * sucks to be you in that case! */
1b7be377
PL
596FcBool
597FcDirCacheValid (const FcChar8 *dir)
598{
ea44e218 599 FcChar8 *cache_file;
1b7be377 600 struct stat file_stat, dir_stat;
ea44e218 601 int fd;
1b7be377
PL
602
603 if (stat ((char *) dir, &dir_stat) < 0)
1b7be377 604 return FcFalse;
6bf23804 605
ea44e218
PL
606 cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
607 if (!cache_file)
608 return FcFalse;
609
610 fd = FcDirCacheOpen ((char *)cache_file);
611
612 if (fd < 0)
613 goto bail;
614 if (fstat (fd, &file_stat) < 0)
615 goto bail1;
616
617 close (fd);
55c8fa4f 618 FcStrFree (cache_file);
ea44e218 619
55c8fa4f
PL
620 /*
621 * If the directory has been modified more recently than
622 * the cache file, the cache is not valid
623 */
2b25f00c 624 if (dir_stat.st_mtime > file_stat.st_mtime)
55c8fa4f 625 return FcFalse;
2b25f00c 626
55c8fa4f 627 return FcTrue;
ea44e218
PL
628
629 bail1:
630 close (fd);
631 bail:
632 FcStrFree (cache_file);
633 return FcFalse;
55c8fa4f
PL
634}
635
636/* Assumes that the cache file in 'dir' exists.
637 * Checks that the cache has the appropriate arch section. */
638FcBool
639FcDirCacheHasCurrentArch (const FcChar8 *dir)
640{
ea44e218 641 char *cache_file;
55c8fa4f
PL
642 int fd;
643 off_t current_arch_start;
644 char *current_arch_machine_name;
645
ea44e218
PL
646 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
647 if (!cache_file)
648 return FcFalse;
6bf23804 649
ea44e218
PL
650 fd = FcDirCacheOpen (cache_file);
651 if (fd < 0)
652 goto bail;
653
654 current_arch_machine_name = FcCacheMachineSignature();
c60ec7cc 655 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
6bf23804
PL
656 close (fd);
657
658 if (current_arch_start < 0)
659 return FcFalse;
55c8fa4f
PL
660
661 return FcTrue;
ea44e218
PL
662
663 bail:
664 free (cache_file);
665 return FcFalse;
55c8fa4f 666}
6bf23804 667
55c8fa4f
PL
668FcBool
669FcDirCacheUnlink (const FcChar8 *dir)
670{
3bfae75d
PL
671 char *cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
672 char *cache_hashed;
673 int fd, collisions;
15d7bd0a 674 struct stat cache_stat;
3bfae75d 675 char name_buf[FC_MAX_FILE_LEN];
55c8fa4f 676
3bfae75d 677 /* First remove normal cache file. */
15d7bd0a
PL
678 if (stat ((char *) cache_file, &cache_stat) == 0 &&
679 unlink ((char *)cache_file) != 0)
3bfae75d
PL
680 goto bail;
681
682 /* Next remove any applicable hashed files. */
683 fd = -1; collisions = 0;
684 do
1178b569 685 {
3bfae75d
PL
686 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
687 if (!cache_hashed)
688 goto bail;
689
690 if (fd > 0)
691 close (fd);
692 fd = open(cache_hashed, O_RDONLY);
693 if (fd == -1)
694 {
695 FcStrFree ((FcChar8 *)cache_file);
696 return FcTrue;
697 }
698
699 FcCacheReadString (fd, name_buf, sizeof (name_buf));
700 if (!strlen(name_buf))
701 goto bail;
702 } while (strcmp (name_buf, cache_file) != 0);
703
704 FcStrFree ((FcChar8 *)cache_file);
705 close (fd);
706
707 if (stat ((char *) cache_hashed, &cache_stat) == 0 &&
708 unlink ((char *)cache_hashed) != 0)
709 {
710 FcStrFree ((FcChar8 *)cache_hashed);
711 goto bail;
1178b569 712 }
55c8fa4f 713
3bfae75d 714 FcStrFree ((FcChar8 *)cache_hashed);
1b7be377 715 return FcTrue;
3bfae75d
PL
716
717 bail:
718 FcStrFree ((FcChar8 *)cache_file);
719 return FcFalse;
1b7be377
PL
720}
721
4262e0b3 722static int
1b7be377
PL
723FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
724 FcStrList *list, FcFontSet * set)
4262e0b3 725{
4262e0b3
PL
726 int ret = 0;
727 FcChar8 *dir;
728 FcChar8 *file, *base;
729 FcStrSet *subdirs;
730 FcStrList *sublist;
731 struct stat statb;
732
733 /*
03a212e5 734 * Read in the results from 'list'.
4262e0b3
PL
735 */
736 while ((dir = FcStrListNext (list)))
737 {
9fad72ab
PL
738 if (!FcConfigAcceptFilename (config, dir))
739 continue;
740
4262e0b3
PL
741 /* freed below */
742 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
743 if (!file)
744 return FcFalse;
745
746 strcpy ((char *) file, (char *) dir);
747 strcat ((char *) file, "/");
748 base = file + strlen ((char *) file);
749
750 subdirs = FcStrSetCreate ();
751 if (!subdirs)
752 {
753 fprintf (stderr, "Can't create directory set\n");
754 ret++;
755 free (file);
756 continue;
757 }
758
759 if (access ((char *) dir, X_OK) < 0)
760 {
761 switch (errno) {
762 case ENOENT:
763 case ENOTDIR:
764 case EACCES:
765 break;
766 default:
767 fprintf (stderr, "\"%s\": ", dir);
768 perror ("");
769 ret++;
770 }
771 FcStrSetDestroy (subdirs);
772 free (file);
773 continue;
774 }
775 if (stat ((char *) dir, &statb) == -1)
776 {
777 fprintf (stderr, "\"%s\": ", dir);
778 perror ("");
779 FcStrSetDestroy (subdirs);
780 ret++;
781 free (file);
782 continue;
783 }
784 if (!S_ISDIR (statb.st_mode))
785 {
786 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
787 FcStrSetDestroy (subdirs);
788 free (file);
789 continue;
790 }
2304e38f 791 if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir))
4262e0b3 792 {
1b7be377 793 if (FcDebug () & FC_DBG_FONTSET)
03a212e5 794 printf ("cache scan dir %s\n", dir);
2304e38f 795
1b7be377
PL
796 FcDirScanConfig (set, subdirs, cache,
797 config->blanks, dir, FcFalse, config);
4262e0b3
PL
798 }
799 sublist = FcStrListCreate (subdirs);
800 FcStrSetDestroy (subdirs);
801 if (!sublist)
802 {
803 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
804 ret++;
805 free (file);
806 continue;
807 }
1b7be377 808 ret += FcCacheReadDirs (config, cache, sublist, set);
4262e0b3
PL
809 free (file);
810 }
811 FcStrListDone (list);
812 return ret;
813}
814
815FcFontSet *
1b7be377 816FcCacheRead (FcConfig *config, FcGlobalCache * cache)
4262e0b3
PL
817{
818 FcFontSet * s = FcFontSetCreate();
819 if (!s)
820 return 0;
821
1b7be377 822 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
4262e0b3
PL
823 goto bail;
824
825 return s;
826
827 bail:
828 FcFontSetDestroy (s);
829 return 0;
830}
831
ea44e218
PL
832static const char bin2hex[] = { '0', '1', '2', '3',
833 '4', '5', '6', '7',
834 '8', '9', 'a', 'b',
835 'c', 'd', 'e', 'f' };
836
837static char *
838FcDirCacheHashName (char * cache_file, int collisions)
839{
840 unsigned char hash[16], hex_hash[33];
841 char *cache_hashed;
842 unsigned char uscore = '_';
843 int cnt, i;
844 FcChar8 *tmp;
845 struct MD5Context ctx;
846
847 MD5Init (&ctx);
848 MD5Update (&ctx, (unsigned char *)cache_file, strlen (cache_file));
849
850 for (i = 0; i < collisions; i++)
851 MD5Update (&ctx, &uscore, 1);
852
853 MD5Final (hash, &ctx);
854
855 for (cnt = 0; cnt < 16; ++cnt)
856 {
857 hex_hash[2*cnt] = bin2hex[hash[cnt] >> 4];
858 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
859 }
860 hex_hash[32] = 0;
861
862 tmp = FcStrPlus ((FcChar8 *)hex_hash, (FcChar8 *)FC_CACHE_SUFFIX);
863 if (!tmp)
864 return 0;
865
83b67390 866 cache_hashed = (char *)FcStrPlus ((FcChar8 *)PKGCACHEDIR"/", tmp);
ea44e218
PL
867 free (tmp);
868
869 return cache_hashed;
870}
871
872/* Opens the hashed name for cache_file.
873 * This would fail in the unlikely event of a collision and subsequent
874 * removal of the file which originally caused the collision. */
875static int
876FcDirCacheOpen (char *cache_file)
877{
878 int fd = -1, collisions = 0;
879 char *cache_hashed;
880 char name_buf[FC_MAX_FILE_LEN];
881
ec760b17
PL
882 fd = open(cache_file, O_RDONLY);
883 if (fd != -1)
884 return fd;
885
ea44e218
PL
886 do
887 {
888 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
889 if (!cache_hashed)
890 return -1;
891
892 if (fd > 0)
893 close (fd);
894 fd = open(cache_hashed, O_RDONLY);
3bfae75d
PL
895 FcStrFree ((FcChar8 *)cache_hashed);
896
ea44e218
PL
897 if (fd == -1)
898 return -1;
899 FcCacheReadString (fd, name_buf, sizeof (name_buf));
900 if (!strlen(name_buf))
901 goto bail;
902 } while (strcmp (name_buf, cache_file) != 0);
903 return fd;
904
905 bail:
906 close (fd);
907 return -1;
908}
909
212c9f43 910/* read serialized state from the cache file */
2eb84374 911FcBool
2304e38f 912FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir)
212c9f43 913{
ea44e218
PL
914 char *cache_file;
915 int fd;
916 char *current_arch_machine_name;
917 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
918 off_t current_arch_start = 0;
919 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
212c9f43 920
ea44e218 921 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
4262e0b3 922 if (!cache_file)
ea44e218 923 return FcFalse;
212c9f43 924
ea44e218
PL
925 fd = FcDirCacheOpen (cache_file);
926 if (fd < 0)
927 goto bail;
212c9f43 928
ea44e218
PL
929 current_arch_machine_name = FcCacheMachineSignature();
930 current_arch_start = FcCacheSkipToArch(fd,
c60ec7cc 931 current_arch_machine_name);
212c9f43 932 if (current_arch_start < 0)
4262e0b3 933 goto bail1;
212c9f43
PL
934
935 lseek (fd, current_arch_start, SEEK_SET);
eb0cf671 936 if (FcCacheReadString (fd, candidate_arch_machine_name,
212c9f43 937 sizeof (candidate_arch_machine_name)) == 0)
4262e0b3 938 goto bail1;
2304e38f
PL
939
940 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
8245771d 941 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
2304e38f 942
e77c1718 943 if (!FcDirCacheConsume (fd, (const char *)dir, set))
eb0cf671
PL
944 goto bail1;
945
946 close(fd);
947 free (cache_file);
948 return FcTrue;
949
950 bail1:
2304e38f 951 close (fd);
eb0cf671
PL
952 bail:
953 free (cache_file);
954 return FcFalse;
955}
956
957static FcBool
e77c1718 958FcDirCacheConsume (int fd, const char * dir, FcFontSet *set)
eb0cf671
PL
959{
960 FcCache metadata;
961 void * current_dir_block;
fd77c154 962 off_t pos;
212c9f43 963
212c9f43
PL
964 read(fd, &metadata, sizeof(FcCache));
965 if (metadata.magic != FC_CACHE_MAGIC)
eb0cf671 966 return FcFalse;
212c9f43 967
2dbe7597 968 if (!metadata.count)
05a98eaf
PL
969 {
970 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
971 lseek (fd, pos, SEEK_SET);
eb0cf671 972 return FcTrue;
05a98eaf 973 }
2dbe7597 974
fd77c154 975 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
2dbe7597
PL
976 current_dir_block = mmap (0, metadata.count,
977 PROT_READ, MAP_SHARED, fd, pos);
c60ec7cc 978 lseek (fd, pos+metadata.count, SEEK_SET);
2dbe7597 979 if (current_dir_block == MAP_FAILED)
eb0cf671 980 return FcFalse;
eb0cf671 981
e77c1718
PL
982 FcCacheAddBankDir (metadata.bank, dir);
983
b8948e85
PL
984 if (!FcFontSetUnserialize (&metadata, set, current_dir_block))
985 return FcFalse;
986
212c9f43 987 return FcTrue;
eb0cf671
PL
988}
989
990static void *
991FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
992{
993 void * current_dir_block, * final_dir_block;
8245771d 994 static unsigned int rand_state = 0;
8e351527 995 int bank, needed_bytes_no_align;
eb0cf671
PL
996
997 if (!rand_state)
998 rand_state = time(0L);
999 bank = rand_r(&rand_state);
1000
1001 while (FcCacheHaveBank(bank))
1002 bank = rand_r(&rand_state);
1003
1004 memset (metadata, 0, sizeof(FcCache));
1005 FcFontSetNewBank();
8e351527
PL
1006 needed_bytes_no_align = FcFontSetNeededBytes (set);
1007 metadata->count = needed_bytes_no_align +
7fd7221e 1008 FcFontSetNeededBytesAlign ();
eb0cf671
PL
1009 metadata->magic = FC_CACHE_MAGIC;
1010 metadata->bank = bank;
1011
8e351527
PL
1012 if (!needed_bytes_no_align) /* not a failure, no fonts to write */
1013 {
1014 /* no, you don't really need to write any bytes at all. */
1015 metadata->count = 0;
eb0cf671 1016 return 0;
8e351527 1017 }
eb0cf671
PL
1018
1019 current_dir_block = malloc (metadata->count);
1020 if (!current_dir_block)
1021 goto bail;
8e351527
PL
1022 // shut up valgrind
1023 memset (current_dir_block, 0, metadata->count);
eb0cf671
PL
1024 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
1025
8e351527 1026 if ((void *)((char *)current_dir_block+metadata->count) < final_dir_block)
eb0cf671
PL
1027 goto bail;
1028
1029 if (!FcFontSetSerialize (bank, set))
1030 goto bail;
1031
1032 return current_dir_block;
212c9f43 1033
4262e0b3 1034 bail:
eb0cf671
PL
1035 free (current_dir_block);
1036 return 0;
212c9f43
PL
1037}
1038
1039/* write serialized state to the cache file */
1040FcBool
2304e38f 1041FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
212c9f43 1042{
ea44e218 1043 char *cache_file;
3bfae75d 1044 char *cache_hashed;
1d879de2
PL
1045 int fd, fd_orig, i, dirs_count;
1046 FcAtomic *atomic;
1047 FcCache metadata;
1048 off_t current_arch_start = 0, truncate_to;
ea44e218
PL
1049 char name_buf[FC_MAX_FILE_LEN];
1050 int collisions;
8245771d 1051
1d879de2 1052 char *current_arch_machine_name, * header;
ea44e218 1053 void *current_dir_block = 0;
212c9f43 1054
ea44e218 1055 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
4262e0b3
PL
1056 if (!cache_file)
1057 goto bail;
1058
ea44e218
PL
1059 /* Ensure that we're not trampling a cache for some other dir. */
1060 /* This is slightly different from FcDirCacheOpen, since it
1061 * needs the filename, not the file descriptor. */
1062 fd = -1; collisions = 0;
1063 do
1064 {
3bfae75d
PL
1065 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
1066 if (!cache_hashed)
ea44e218
PL
1067 goto bail0;
1068
1069 if (fd > 0)
1070 close (fd);
3bfae75d 1071 fd = open(cache_hashed, O_RDONLY);
ea44e218
PL
1072 if (fd == -1)
1073 break;
1074 FcCacheReadString (fd, name_buf, sizeof (name_buf));
1075 close (fd);
1076
1077 if (!strlen(name_buf))
1078 break;
1079
1080 if (strcmp (name_buf, cache_file) != 0)
1081 continue;
1082 } while (0);
1083
eb0cf671 1084 current_dir_block = FcDirCacheProduce (set, &metadata);
212c9f43 1085
a9698bed 1086 if (metadata.count && !current_dir_block)
ea44e218 1087 goto bail1;
212c9f43 1088
4262e0b3
PL
1089 if (FcDebug () & FC_DBG_CACHE)
1090 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
1091
3bfae75d 1092 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
1d879de2 1093 if (!atomic)
ea44e218 1094 goto bail1;
212c9f43 1095
1d879de2 1096 if (!FcAtomicLock (atomic))
ec760b17
PL
1097 {
1098 /* Now try rewriting the original version of the file. */
1099 FcAtomicDestroy (atomic);
1d879de2 1100
3bfae75d 1101 atomic = FcAtomicCreate ((FcChar8 *)cache_file);
ec760b17
PL
1102 fd_orig = open (cache_file, O_RDONLY);
1103 if (fd_orig == -1)
1104 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1105
1106 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1107 if (fd == -1)
1108 goto bail2;
1109 }
1110
1111 /* In all cases, try opening the real location of the cache file first. */
1112 /* (even if that's not atomic.) */
1113 fd_orig = open (cache_file, O_RDONLY);
1114 if (fd_orig == -1)
1115 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1d879de2
PL
1116
1117 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1118 if (fd == -1)
ec760b17 1119 goto bail3;
ea44e218
PL
1120
1121 FcCacheWriteString (fd, cache_file);
1d879de2 1122
eb0cf671 1123 current_arch_machine_name = FcCacheMachineSignature ();
1d879de2
PL
1124 current_arch_start = 0;
1125
1126 if (fd_orig != -1)
1127 current_arch_start =
c60ec7cc 1128 FcCacheSkipToArch(fd_orig, current_arch_machine_name);
1d879de2 1129
212c9f43 1130 if (current_arch_start < 0)
649cc361 1131 current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
212c9f43 1132
1d879de2 1133 if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
ea44e218 1134 goto bail4;
1d879de2
PL
1135
1136 if (fd_orig != -1)
1137 close (fd_orig);
212c9f43
PL
1138
1139 current_arch_start = lseek(fd, 0, SEEK_CUR);
1140 if (ftruncate (fd, current_arch_start) == -1)
ea44e218 1141 goto bail4;
212c9f43 1142
6aee8c6f
PL
1143 /* allocate space for subdir names in this block */
1144 dirs_count = 0;
1145 for (i = 0; i < dirs->size; i++)
1146 dirs_count += strlen((char *)dirs->strs[i]) + 1;
1147 dirs_count ++;
1148
212c9f43 1149 /* now write the address of the next offset */
6aee8c6f 1150 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start;
2dbe7597 1151 header = malloc (10 + strlen (current_arch_machine_name));
eb0cf671 1152 if (!header)
ea44e218 1153 goto bail4;
2dbe7597
PL
1154 sprintf (header, "%8x ", (int)truncate_to);
1155 strcat (header, current_arch_machine_name);
eb0cf671 1156 if (!FcCacheWriteString (fd, header))
ea44e218 1157 goto bail5;
212c9f43 1158
2304e38f 1159 for (i = 0; i < dirs->size; i++)
8245771d 1160 FcCacheWriteString (fd, (char *)dirs->strs[i]);
2304e38f
PL
1161 FcCacheWriteString (fd, "");
1162
4262e0b3 1163 write (fd, &metadata, sizeof(FcCache));
a9698bed
PL
1164 if (metadata.count)
1165 {
1166 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
1167 write (fd, current_dir_block, metadata.count);
1168 free (current_dir_block);
1169 }
4262e0b3 1170
2dbe7597 1171 /* this actually serves to pad out the cache file, if needed */
212c9f43 1172 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
ea44e218 1173 goto bail5;
212c9f43
PL
1174
1175 close(fd);
1d879de2 1176 if (!FcAtomicReplaceOrig(atomic))
ea44e218 1177 goto bail5;
3bfae75d 1178 FcStrFree ((FcChar8 *)cache_hashed);
1d879de2
PL
1179 FcAtomicUnlock (atomic);
1180 FcAtomicDestroy (atomic);
212c9f43
PL
1181 return FcTrue;
1182
ea44e218 1183 bail5:
0230c9f8 1184 free (header);
ea44e218 1185 bail4:
1d879de2 1186 close (fd);
ea44e218 1187 bail3:
1d879de2 1188 FcAtomicUnlock (atomic);
ea44e218 1189 bail2:
1d879de2 1190 FcAtomicDestroy (atomic);
ea44e218 1191 bail1:
3bfae75d 1192 FcStrFree ((FcChar8 *)cache_hashed);
eb0cf671 1193 bail0:
8245771d 1194 unlink ((char *)cache_file);
4262e0b3 1195 free (cache_file);
1d879de2
PL
1196 if (current_dir_block)
1197 free (current_dir_block);
1198 bail:
212c9f43
PL
1199 return FcFalse;
1200}
1201
2dbe7597 1202static char *
eb0cf671 1203FcCacheMachineSignature ()
2dbe7597
PL
1204{
1205 static char buf[MACHINE_SIGNATURE_SIZE];
7fd7221e 1206 int32_t magic = ENDIAN_TEST;
2dbe7597
PL
1207 char * m = (char *)&magic;
1208
1209 sprintf (buf, "%2x%2x%2x%2x "
1210 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
7fd7221e 1211 "%4x %4x %4x %4x %4x %4x %4x %4x\n",
2dbe7597 1212 m[0], m[1], m[2], m[3],
cd310911
PL
1213 (unsigned int)sizeof (char),
1214 (unsigned int)sizeof (char *),
1215 (unsigned int)sizeof (int),
1216 (unsigned int)sizeof (FcPattern),
1217 (unsigned int)sizeof (FcPatternEltPtr),
1218 (unsigned int)sizeof (struct _FcPatternElt *),
1219 (unsigned int)sizeof (FcPatternElt),
1220 (unsigned int)sizeof (FcObjectPtr),
1221 (unsigned int)sizeof (FcValueListPtr),
1222 (unsigned int)sizeof (FcValue),
1223 (unsigned int)sizeof (FcValueBinding),
1224 (unsigned int)sizeof (struct _FcValueList *),
1225 (unsigned int)sizeof (FcCharSet),
1226 (unsigned int)sizeof (FcCharLeaf **),
1227 (unsigned int)sizeof (FcChar16 *),
1228 (unsigned int)sizeof (FcChar16),
1229 (unsigned int)sizeof (FcCharLeaf),
1230 (unsigned int)sizeof (FcChar32),
7fd7221e
PL
1231 (unsigned int)sizeof (FcCache),
1232 (unsigned int)sysconf(_SC_PAGESIZE));
2dbe7597
PL
1233
1234 return buf;
1235}
1236
4262e0b3 1237static int banks_ptr = 0, banks_alloc = 0;
b8948e85 1238int * _fcBankId = 0, * _fcBankIdx = 0;
e77c1718 1239static const char ** bankDirs = 0;
4262e0b3 1240
eb0cf671 1241static FcBool
4262e0b3
PL
1242FcCacheHaveBank (int bank)
1243{
1244 int i;
1245
1246 if (bank < FC_BANK_FIRST)
1247 return FcTrue;
1248
1249 for (i = 0; i < banks_ptr; i++)
b8948e85 1250 if (_fcBankId[i] == bank)
4262e0b3
PL
1251 return FcTrue;
1252
1253 return FcFalse;
1254}
1255
1256int
b8948e85 1257FcCacheBankToIndexMTF (int bank)
4262e0b3 1258{
751932dd 1259 int i, j;
4262e0b3
PL
1260
1261 for (i = 0; i < banks_ptr; i++)
b8948e85 1262 if (_fcBankId[_fcBankIdx[i]] == bank)
751932dd 1263 {
b8948e85 1264 int t = _fcBankIdx[i];
751932dd
PL
1265
1266 for (j = i; j > 0; j--)
b8948e85
PL
1267 _fcBankIdx[j] = _fcBankIdx[j-1];
1268 _fcBankIdx[0] = t;
751932dd
PL
1269 return t;
1270 }
4262e0b3 1271
2dbe7597 1272 if (banks_ptr >= banks_alloc)
4262e0b3 1273 {
751932dd 1274 int * b, * bidx;
e77c1718
PL
1275 const char ** bds;
1276
b8948e85 1277 b = realloc (_fcBankId, (banks_alloc + 4) * sizeof(int));
4262e0b3
PL
1278 if (!b)
1279 return -1;
b8948e85 1280 _fcBankId = b;
751932dd 1281
b8948e85 1282 bidx = realloc (_fcBankIdx, (banks_alloc + 4) * sizeof(int));
751932dd
PL
1283 if (!bidx)
1284 return -1;
b8948e85 1285 _fcBankIdx = bidx;
751932dd 1286
e77c1718
PL
1287 bds = realloc (bankDirs, (banks_alloc + 4) * sizeof (char *));
1288 if (!bds)
1289 return -1;
1290 bankDirs = bds;
1291
4262e0b3
PL
1292 banks_alloc += 4;
1293 }
1294
1295 i = banks_ptr++;
b8948e85
PL
1296 _fcBankId[i] = bank;
1297 _fcBankIdx[i] = i;
4262e0b3
PL
1298 return i;
1299}
e77c1718
PL
1300
1301static void
1302FcCacheAddBankDir (int bank, const char * dir)
1303{
b8948e85 1304 int bi = FcCacheBankToIndexMTF (bank);
e77c1718
PL
1305
1306 if (bi < 0)
1307 return;
1308
1309 bankDirs[bi] = (const char *)FcStrCopy ((FcChar8 *)dir);
1310}
1311
1312const char *
1313FcCacheFindBankDir (int bank)
1314{
1315 int bi = FcCacheBankToIndex (bank);
1316 return bankDirs[bi];
1317}
1318
ea44e218
PL
1319/*
1320 * This code implements the MD5 message-digest algorithm.
1321 * The algorithm is due to Ron Rivest. This code was
1322 * written by Colin Plumb in 1993, no copyright is claimed.
1323 * This code is in the public domain; do with it what you wish.
1324 *
1325 * Equivalent code is available from RSA Data Security, Inc.
1326 * This code has been tested against that, and is equivalent,
1327 * except that you don't need to include two pages of legalese
1328 * with every copy.
1329 *
1330 * To compute the message digest of a chunk of bytes, declare an
1331 * MD5Context structure, pass it to MD5Init, call MD5Update as
1332 * needed on buffers full of bytes, and then call MD5Final, which
1333 * will fill a supplied 16-byte array with the digest.
1334 */
1335
1336#ifndef HIGHFIRST
1337#define byteReverse(buf, len) /* Nothing */
1338#else
1339/*
1340 * Note: this code is harmless on little-endian machines.
1341 */
1342void byteReverse(unsigned char *buf, unsigned longs)
1343{
1344 FcChar32 t;
1345 do {
1346 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1347 ((unsigned) buf[1] << 8 | buf[0]);
1348 *(FcChar32 *) buf = t;
1349 buf += 4;
1350 } while (--longs);
1351}
1352#endif
1353
1354/*
1355 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1356 * initialization constants.
1357 */
1358static void MD5Init(struct MD5Context *ctx)
1359{
1360 ctx->buf[0] = 0x67452301;
1361 ctx->buf[1] = 0xefcdab89;
1362 ctx->buf[2] = 0x98badcfe;
1363 ctx->buf[3] = 0x10325476;
1364
1365 ctx->bits[0] = 0;
1366 ctx->bits[1] = 0;
1367}
1368
1369/*
1370 * Update context to reflect the concatenation of another buffer full
1371 * of bytes.
1372 */
1373static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len)
1374{
1375 FcChar32 t;
1376
1377 /* Update bitcount */
1378
1379 t = ctx->bits[0];
1380 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1381 ctx->bits[1]++; /* Carry from low to high */
1382 ctx->bits[1] += len >> 29;
1383
1384 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1385
1386 /* Handle any leading odd-sized chunks */
1387
1388 if (t) {
1389 unsigned char *p = (unsigned char *) ctx->in + t;
1390
1391 t = 64 - t;
1392 if (len < t) {
1393 memcpy(p, buf, len);
1394 return;
1395 }
1396 memcpy(p, buf, t);
1397 byteReverse(ctx->in, 16);
1398 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1399 buf += t;
1400 len -= t;
1401 }
1402 /* Process data in 64-byte chunks */
1403
1404 while (len >= 64) {
1405 memcpy(ctx->in, buf, 64);
1406 byteReverse(ctx->in, 16);
1407 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1408 buf += 64;
1409 len -= 64;
1410 }
1411
1412 /* Handle any remaining bytes of data. */
1413
1414 memcpy(ctx->in, buf, len);
1415}
1416
1417/*
1418 * Final wrapup - pad to 64-byte boundary with the bit pattern
1419 * 1 0* (64-bit count of bits processed, MSB-first)
1420 */
1421static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1422{
1423 unsigned count;
1424 unsigned char *p;
1425
1426 /* Compute number of bytes mod 64 */
1427 count = (ctx->bits[0] >> 3) & 0x3F;
1428
1429 /* Set the first char of padding to 0x80. This is safe since there is
1430 always at least one byte free */
1431 p = ctx->in + count;
1432 *p++ = 0x80;
1433
1434 /* Bytes of padding needed to make 64 bytes */
1435 count = 64 - 1 - count;
1436
1437 /* Pad out to 56 mod 64 */
1438 if (count < 8) {
1439 /* Two lots of padding: Pad the first block to 64 bytes */
1440 memset(p, 0, count);
1441 byteReverse(ctx->in, 16);
1442 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1443
1444 /* Now fill the next block with 56 bytes */
1445 memset(ctx->in, 0, 56);
1446 } else {
1447 /* Pad block to 56 bytes */
1448 memset(p, 0, count - 8);
1449 }
1450 byteReverse(ctx->in, 14);
1451
1452 /* Append length in bits and transform */
1453 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1454 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1455
1456 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1457 byteReverse((unsigned char *) ctx->buf, 4);
1458 memcpy(digest, ctx->buf, 16);
1459 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
1460}
1461
1462
1463/* The four core functions - F1 is optimized somewhat */
1464
1465/* #define F1(x, y, z) (x & y | ~x & z) */
1466#define F1(x, y, z) (z ^ (x & (y ^ z)))
1467#define F2(x, y, z) F1(z, x, y)
1468#define F3(x, y, z) (x ^ y ^ z)
1469#define F4(x, y, z) (y ^ (x | ~z))
1470
1471/* This is the central step in the MD5 algorithm. */
1472#define MD5STEP(f, w, x, y, z, data, s) \
1473 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1474
1475/*
1476 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1477 * reflect the addition of 16 longwords of new data. MD5Update blocks
1478 * the data and converts bytes into longwords for this routine.
1479 */
1480static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1481{
1482 register FcChar32 a, b, c, d;
1483
1484 a = buf[0];
1485 b = buf[1];
1486 c = buf[2];
1487 d = buf[3];
1488
1489 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1490 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1491 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1492 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1493 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1494 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1495 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1496 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1497 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1498 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1499 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1500 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1501 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1502 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1503 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1504 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1505
1506 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1507 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1508 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1509 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1510 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1511 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1512 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1513 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1514 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1515 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1516 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1517 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1518 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1519 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1520 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1521 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1522
1523 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1524 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1525 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1526 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1527 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1528 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1529 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1530 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1531 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1532 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1533 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1534 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1535 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1536 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1537 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1538 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1539
1540 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1541 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1542 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1543 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1544 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1545 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1546 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1547 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1548 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1549 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1550 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1551 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1552 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1553 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1554 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1555 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1556
1557 buf[0] += a;
1558 buf[1] += b;
1559 buf[2] += c;
1560 buf[3] += d;
1561}