]> git.wh0rd.org - fontconfig.git/blame - src/fccache.c
Fix double-free which occurs because FcValueCanonicalize doesn't create an
[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
ea44e218 46FcCacheSkipToArch (int fd, const char * arch, FcBool global);
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
eb0cf671
PL
222 current_arch_machine_name = FcCacheMachineSignature ();
223 current_arch_start = FcCacheSkipToArch(cache->fd,
ea44e218 224 current_arch_machine_name, FcTrue);
eb0cf671 225 if (current_arch_start < 0)
e58b50e8 226 goto bail_and_destroy;
327a7fd4 227
eb0cf671 228 lseek (cache->fd, current_arch_start, SEEK_SET);
03a212e5
PL
229 FcCacheReadString (cache->fd, candidate_arch_machine_name,
230 sizeof (candidate_arch_machine_name));
231 if (strlen(candidate_arch_machine_name) == 0)
e58b50e8 232 goto bail_and_destroy;
1b7be377 233
eb0cf671 234 while (1)
1b7be377 235 {
8cb4c56d
PL
236 off_t targ;
237
eb0cf671
PL
238 FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
239 if (!strlen(name_buf))
1b7be377 240 break;
1b7be377 241
9090cb9e
PL
242 /* Directory must be older than the global cache file; also
243 cache must be newer than the config file. */
03a212e5 244 if (stat ((char *) name_buf, &dir_stat) < 0 ||
2b25f00c 245 dir_stat.st_mtime > cache_stat.st_mtime ||
9090cb9e 246 (config_time.set && cache_stat.st_mtime < config_time.time))
03a212e5
PL
247 {
248 FcCache md;
249
8245771d 250 FcStrSetAdd (staleDirs, FcStrCopy ((FcChar8 *)name_buf));
03a212e5
PL
251 read (cache->fd, &md, sizeof (FcCache));
252 lseek (cache->fd, FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count, SEEK_SET);
253 continue;
254 }
255
eb0cf671
PL
256 d = malloc (sizeof (FcGlobalCacheDir));
257 if (!d)
258 goto bail1;
1b7be377 259
eb0cf671
PL
260 d->next = cache->dirs;
261 cache->dirs = d;
1b7be377 262
8245771d 263 d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
eb0cf671
PL
264 d->ent = 0;
265 d->offset = lseek (cache->fd, 0, SEEK_CUR);
8cb4c56d
PL
266 if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
267 goto bail1;
268 targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
269 if (lseek (cache->fd, targ, SEEK_SET) != targ)
270 goto bail1;
1b7be377 271 }
eb0cf671 272 return;
1b7be377 273
eb0cf671
PL
274 bail1:
275 for (d = cache->dirs; d; d = next)
1b7be377 276 {
eb0cf671
PL
277 next = d->next;
278 free (d);
1b7be377 279 }
eb0cf671 280 cache->dirs = 0;
e58b50e8
PL
281
282 close (cache->fd);
283 cache->fd = -1;
284 return;
285
286 bail_and_destroy:
eb0cf671
PL
287 close (cache->fd);
288 cache->fd = -1;
e58b50e8
PL
289
290 if (stat ((char *) cache_file, &cache_stat) == 0)
291 unlink ((char *)cache_file);
292
eb0cf671 293 return;
e58b50e8 294
1b7be377
PL
295}
296
1b7be377 297FcBool
8245771d 298FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config)
1b7be377 299{
eb0cf671 300 FcGlobalCacheDir *d;
03a212e5 301 FcBool ret = FcFalse;
1b7be377 302
eb0cf671 303 if (cache->fd == -1)
1b7be377 304 return FcFalse;
1b7be377 305
eb0cf671 306 for (d = cache->dirs; d; d = d->next)
1b7be377 307 {
03a212e5 308 if (strncmp (d->name, dir, strlen(dir)) == 0)
1b7be377 309 {
eb0cf671 310 lseek (cache->fd, d->offset, SEEK_SET);
e77c1718 311 if (!FcDirCacheConsume (cache->fd, dir, set))
eb0cf671 312 return FcFalse;
03a212e5
PL
313 if (strcmp (d->name, dir) == 0)
314 ret = FcTrue;
1b7be377 315 }
1b7be377 316 }
1b7be377 317
03a212e5 318 return ret;
1b7be377
PL
319}
320
eb0cf671
PL
321FcBool
322FcGlobalCacheUpdate (FcGlobalCache *cache,
8245771d 323 const char *name,
eb0cf671 324 FcFontSet *set)
1b7be377 325{
eb0cf671 326 FcGlobalCacheDir * d;
1b7be377 327
eb0cf671
PL
328 if (!set->nfont)
329 return FcTrue;
1b7be377 330
eb0cf671 331 for (d = cache->dirs; d; d = d->next)
1b7be377 332 {
eb0cf671 333 if (strcmp(d->name, name) == 0)
1b7be377 334 break;
24330d27 335 }
24330d27 336
eb0cf671 337 if (!d)
24330d27 338 {
eb0cf671
PL
339 d = malloc (sizeof (FcGlobalCacheDir));
340 if (!d)
341 return FcFalse;
342 d->next = cache->dirs;
343 cache->dirs = d;
24330d27 344 }
24330d27 345
eb0cf671 346 cache->updated = FcTrue;
24330d27 347
8245771d 348 d->name = (char *)FcStrCopy ((FcChar8 *)name);
eb0cf671
PL
349 d->ent = FcDirCacheProduce (set, &d->metadata);
350 d->offset = 0;
351 return FcTrue;
24330d27
KP
352}
353
354FcBool
327a7fd4
KP
355FcGlobalCacheSave (FcGlobalCache *cache,
356 const FcChar8 *cache_file)
24330d27 357{
1d879de2 358 int fd, fd_orig;
327a7fd4 359 FcGlobalCacheDir *dir;
327a7fd4 360 FcAtomic *atomic;
eb0cf671
PL
361 off_t current_arch_start = 0, truncate_to;
362 char * current_arch_machine_name, * header;
24330d27 363
eb0cf671 364 if (!cache->updated)
24330d27
KP
365 return FcTrue;
366
daeed6e0 367#if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
a391da8f
KP
368 /* Set-UID programs can't safely update the cache */
369 if (getuid () != geteuid ())
370 return FcFalse;
daeed6e0 371#endif
a391da8f 372
134f6011
KP
373 atomic = FcAtomicCreate (cache_file);
374 if (!atomic)
1d879de2
PL
375 return FcFalse;
376
134f6011 377 if (!FcAtomicLock (atomic))
24330d27 378 goto bail1;
eb0cf671
PL
379 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT,
380 S_IRUSR | S_IWUSR);
381 if (fd == -1)
24330d27
KP
382 goto bail2;
383
1d879de2
PL
384 fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY);
385
eb0cf671 386 current_arch_machine_name = FcCacheMachineSignature ();
1d879de2
PL
387 if (fd_orig == -1)
388 current_arch_start = 0;
389 else
390 current_arch_start = FcCacheSkipToArch (fd_orig,
ea44e218
PL
391 current_arch_machine_name,
392 FcTrue);
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
8cb4c56d
PL
421 sprintf (header, "%8x ", (int)truncate_to);
422 strcat (header, current_arch_machine_name);
423 if (!FcCacheWriteString (fd, header))
1d879de2 424 goto bail4;
8cb4c56d 425
eb0cf671
PL
426 for (dir = cache->dirs; dir; dir = dir->next)
427 {
5e678e94
PL
428 if (dir->ent)
429 {
430 FcCacheWriteString (fd, dir->name);
431 write (fd, &dir->metadata, sizeof(FcCache));
f6ee3db5 432 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET);
5e678e94
PL
433 write (fd, dir->ent, dir->metadata.count);
434 free (dir->ent);
435 }
24330d27 436 }
eb0cf671 437 FcCacheWriteString (fd, "");
24330d27 438
eb0cf671 439 if (close (fd) == -1)
1d879de2 440 goto bail25;
24330d27 441
134f6011 442 if (!FcAtomicReplaceOrig (atomic))
1d879de2 443 goto bail25;
24330d27 444
134f6011
KP
445 FcAtomicUnlock (atomic);
446 FcAtomicDestroy (atomic);
447
24330d27
KP
448 cache->updated = FcFalse;
449 return FcTrue;
450
1d879de2
PL
451 bail4:
452 free (header);
453 bail3:
454 if (fd_orig != -1)
455 close (fd_orig);
456
457 close (fd);
458 bail25:
134f6011 459 FcAtomicDeleteNew (atomic);
1d879de2 460 bail2:
134f6011 461 FcAtomicUnlock (atomic);
1d879de2 462 bail1:
134f6011 463 FcAtomicDestroy (atomic);
24330d27
KP
464 return FcFalse;
465}
466
212c9f43 467/*
eb0cf671
PL
468 * Find the next presumably-mmapable offset after the supplied file
469 * position.
212c9f43 470 */
4262e0b3
PL
471static int
472FcCacheNextOffset(off_t w)
179c3995 473{
7fd7221e
PL
474 static long pagesize = -1;
475 if (pagesize == -1)
476 pagesize = sysconf(_SC_PAGESIZE);
477 if (w % pagesize == 0)
212c9f43
PL
478 return w;
479 else
7fd7221e 480 return ((w / pagesize)+1)*pagesize;
179c3995
KP
481}
482
212c9f43
PL
483/* return the address of the segment for the provided arch,
484 * or -1 if arch not found */
485static off_t
ea44e218 486FcCacheSkipToArch (int fd, const char * arch, FcBool global)
212c9f43 487{
2dbe7597
PL
488 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
489 char * candidate_arch;
212c9f43
PL
490 off_t current_arch_start = 0;
491
ea44e218
PL
492 lseek (fd, 0, SEEK_SET);
493 if (!global)
494 FcCacheSkipString (fd);
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();
655 current_arch_start = FcCacheSkipToArch(fd,
656 current_arch_machine_name, FcFalse);
6bf23804
PL
657 close (fd);
658
659 if (current_arch_start < 0)
660 return FcFalse;
55c8fa4f
PL
661
662 return FcTrue;
ea44e218
PL
663
664 bail:
665 free (cache_file);
666 return FcFalse;
55c8fa4f 667}
6bf23804 668
55c8fa4f
PL
669FcBool
670FcDirCacheUnlink (const FcChar8 *dir)
671{
3bfae75d
PL
672 char *cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
673 char *cache_hashed;
674 int fd, collisions;
15d7bd0a 675 struct stat cache_stat;
3bfae75d 676 char name_buf[FC_MAX_FILE_LEN];
55c8fa4f 677
3bfae75d 678 /* First remove normal cache file. */
15d7bd0a
PL
679 if (stat ((char *) cache_file, &cache_stat) == 0 &&
680 unlink ((char *)cache_file) != 0)
3bfae75d
PL
681 goto bail;
682
683 /* Next remove any applicable hashed files. */
684 fd = -1; collisions = 0;
685 do
1178b569 686 {
3bfae75d
PL
687 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
688 if (!cache_hashed)
689 goto bail;
690
691 if (fd > 0)
692 close (fd);
693 fd = open(cache_hashed, O_RDONLY);
694 if (fd == -1)
695 {
696 FcStrFree ((FcChar8 *)cache_file);
697 return FcTrue;
698 }
699
700 FcCacheReadString (fd, name_buf, sizeof (name_buf));
701 if (!strlen(name_buf))
702 goto bail;
703 } while (strcmp (name_buf, cache_file) != 0);
704
705 FcStrFree ((FcChar8 *)cache_file);
706 close (fd);
707
708 if (stat ((char *) cache_hashed, &cache_stat) == 0 &&
709 unlink ((char *)cache_hashed) != 0)
710 {
711 FcStrFree ((FcChar8 *)cache_hashed);
712 goto bail;
1178b569 713 }
55c8fa4f 714
3bfae75d 715 FcStrFree ((FcChar8 *)cache_hashed);
1b7be377 716 return FcTrue;
3bfae75d
PL
717
718 bail:
719 FcStrFree ((FcChar8 *)cache_file);
720 return FcFalse;
1b7be377
PL
721}
722
4262e0b3 723static int
1b7be377
PL
724FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
725 FcStrList *list, FcFontSet * set)
4262e0b3 726{
4262e0b3
PL
727 int ret = 0;
728 FcChar8 *dir;
729 FcChar8 *file, *base;
730 FcStrSet *subdirs;
731 FcStrList *sublist;
732 struct stat statb;
733
734 /*
03a212e5 735 * Read in the results from 'list'.
4262e0b3
PL
736 */
737 while ((dir = FcStrListNext (list)))
738 {
9fad72ab
PL
739 if (!FcConfigAcceptFilename (config, dir))
740 continue;
741
4262e0b3
PL
742 /* freed below */
743 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
744 if (!file)
745 return FcFalse;
746
747 strcpy ((char *) file, (char *) dir);
748 strcat ((char *) file, "/");
749 base = file + strlen ((char *) file);
750
751 subdirs = FcStrSetCreate ();
752 if (!subdirs)
753 {
754 fprintf (stderr, "Can't create directory set\n");
755 ret++;
756 free (file);
757 continue;
758 }
759
760 if (access ((char *) dir, X_OK) < 0)
761 {
762 switch (errno) {
763 case ENOENT:
764 case ENOTDIR:
765 case EACCES:
766 break;
767 default:
768 fprintf (stderr, "\"%s\": ", dir);
769 perror ("");
770 ret++;
771 }
772 FcStrSetDestroy (subdirs);
773 free (file);
774 continue;
775 }
776 if (stat ((char *) dir, &statb) == -1)
777 {
778 fprintf (stderr, "\"%s\": ", dir);
779 perror ("");
780 FcStrSetDestroy (subdirs);
781 ret++;
782 free (file);
783 continue;
784 }
785 if (!S_ISDIR (statb.st_mode))
786 {
787 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
788 FcStrSetDestroy (subdirs);
789 free (file);
790 continue;
791 }
2304e38f 792 if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir))
4262e0b3 793 {
1b7be377 794 if (FcDebug () & FC_DBG_FONTSET)
03a212e5 795 printf ("cache scan dir %s\n", dir);
2304e38f 796
1b7be377
PL
797 FcDirScanConfig (set, subdirs, cache,
798 config->blanks, dir, FcFalse, config);
4262e0b3
PL
799 }
800 sublist = FcStrListCreate (subdirs);
801 FcStrSetDestroy (subdirs);
802 if (!sublist)
803 {
804 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
805 ret++;
806 free (file);
807 continue;
808 }
1b7be377 809 ret += FcCacheReadDirs (config, cache, sublist, set);
4262e0b3
PL
810 free (file);
811 }
812 FcStrListDone (list);
813 return ret;
814}
815
816FcFontSet *
1b7be377 817FcCacheRead (FcConfig *config, FcGlobalCache * cache)
4262e0b3
PL
818{
819 FcFontSet * s = FcFontSetCreate();
820 if (!s)
821 return 0;
822
1b7be377 823 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
4262e0b3
PL
824 goto bail;
825
826 return s;
827
828 bail:
829 FcFontSetDestroy (s);
830 return 0;
831}
832
ea44e218
PL
833static const char bin2hex[] = { '0', '1', '2', '3',
834 '4', '5', '6', '7',
835 '8', '9', 'a', 'b',
836 'c', 'd', 'e', 'f' };
837
838static char *
839FcDirCacheHashName (char * cache_file, int collisions)
840{
841 unsigned char hash[16], hex_hash[33];
842 char *cache_hashed;
843 unsigned char uscore = '_';
844 int cnt, i;
845 FcChar8 *tmp;
846 struct MD5Context ctx;
847
848 MD5Init (&ctx);
849 MD5Update (&ctx, (unsigned char *)cache_file, strlen (cache_file));
850
851 for (i = 0; i < collisions; i++)
852 MD5Update (&ctx, &uscore, 1);
853
854 MD5Final (hash, &ctx);
855
856 for (cnt = 0; cnt < 16; ++cnt)
857 {
858 hex_hash[2*cnt] = bin2hex[hash[cnt] >> 4];
859 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
860 }
861 hex_hash[32] = 0;
862
863 tmp = FcStrPlus ((FcChar8 *)hex_hash, (FcChar8 *)FC_CACHE_SUFFIX);
864 if (!tmp)
865 return 0;
866
83b67390 867 cache_hashed = (char *)FcStrPlus ((FcChar8 *)PKGCACHEDIR"/", tmp);
ea44e218
PL
868 free (tmp);
869
870 return cache_hashed;
871}
872
873/* Opens the hashed name for cache_file.
874 * This would fail in the unlikely event of a collision and subsequent
875 * removal of the file which originally caused the collision. */
876static int
877FcDirCacheOpen (char *cache_file)
878{
879 int fd = -1, collisions = 0;
880 char *cache_hashed;
881 char name_buf[FC_MAX_FILE_LEN];
882
ec760b17
PL
883 fd = open(cache_file, O_RDONLY);
884 if (fd != -1)
885 return fd;
886
ea44e218
PL
887 do
888 {
889 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
890 if (!cache_hashed)
891 return -1;
892
893 if (fd > 0)
894 close (fd);
895 fd = open(cache_hashed, O_RDONLY);
3bfae75d
PL
896 FcStrFree ((FcChar8 *)cache_hashed);
897
ea44e218
PL
898 if (fd == -1)
899 return -1;
900 FcCacheReadString (fd, name_buf, sizeof (name_buf));
901 if (!strlen(name_buf))
902 goto bail;
903 } while (strcmp (name_buf, cache_file) != 0);
904 return fd;
905
906 bail:
907 close (fd);
908 return -1;
909}
910
212c9f43 911/* read serialized state from the cache file */
2eb84374 912FcBool
2304e38f 913FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir)
212c9f43 914{
ea44e218
PL
915 char *cache_file;
916 int fd;
917 char *current_arch_machine_name;
918 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
919 off_t current_arch_start = 0;
920 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
212c9f43 921
ea44e218 922 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
4262e0b3 923 if (!cache_file)
ea44e218 924 return FcFalse;
212c9f43 925
ea44e218
PL
926 fd = FcDirCacheOpen (cache_file);
927 if (fd < 0)
928 goto bail;
212c9f43 929
ea44e218
PL
930 current_arch_machine_name = FcCacheMachineSignature();
931 current_arch_start = FcCacheSkipToArch(fd,
932 current_arch_machine_name,
933 FcFalse);
212c9f43 934 if (current_arch_start < 0)
4262e0b3 935 goto bail1;
212c9f43
PL
936
937 lseek (fd, current_arch_start, SEEK_SET);
eb0cf671 938 if (FcCacheReadString (fd, candidate_arch_machine_name,
212c9f43 939 sizeof (candidate_arch_machine_name)) == 0)
4262e0b3 940 goto bail1;
2304e38f
PL
941
942 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
8245771d 943 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
2304e38f 944
e77c1718 945 if (!FcDirCacheConsume (fd, (const char *)dir, set))
eb0cf671
PL
946 goto bail1;
947
948 close(fd);
949 free (cache_file);
950 return FcTrue;
951
952 bail1:
2304e38f 953 close (fd);
eb0cf671
PL
954 bail:
955 free (cache_file);
956 return FcFalse;
957}
958
959static FcBool
e77c1718 960FcDirCacheConsume (int fd, const char * dir, FcFontSet *set)
eb0cf671
PL
961{
962 FcCache metadata;
963 void * current_dir_block;
fd77c154 964 off_t pos;
212c9f43 965
212c9f43
PL
966 read(fd, &metadata, sizeof(FcCache));
967 if (metadata.magic != FC_CACHE_MAGIC)
eb0cf671 968 return FcFalse;
212c9f43 969
2dbe7597 970 if (!metadata.count)
eb0cf671 971 return FcTrue;
2dbe7597 972
fd77c154 973 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
2dbe7597
PL
974 current_dir_block = mmap (0, metadata.count,
975 PROT_READ, MAP_SHARED, fd, pos);
976 if (current_dir_block == MAP_FAILED)
eb0cf671 977 return FcFalse;
eb0cf671 978
e77c1718
PL
979 FcCacheAddBankDir (metadata.bank, dir);
980
b8948e85
PL
981 if (!FcFontSetUnserialize (&metadata, set, current_dir_block))
982 return FcFalse;
983
212c9f43 984 return FcTrue;
eb0cf671
PL
985}
986
987static void *
988FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
989{
990 void * current_dir_block, * final_dir_block;
8245771d 991 static unsigned int rand_state = 0;
8e351527 992 int bank, needed_bytes_no_align;
eb0cf671
PL
993
994 if (!rand_state)
995 rand_state = time(0L);
996 bank = rand_r(&rand_state);
997
998 while (FcCacheHaveBank(bank))
999 bank = rand_r(&rand_state);
1000
1001 memset (metadata, 0, sizeof(FcCache));
1002 FcFontSetNewBank();
8e351527
PL
1003 needed_bytes_no_align = FcFontSetNeededBytes (set);
1004 metadata->count = needed_bytes_no_align +
7fd7221e 1005 FcFontSetNeededBytesAlign ();
eb0cf671
PL
1006 metadata->magic = FC_CACHE_MAGIC;
1007 metadata->bank = bank;
1008
8e351527
PL
1009 if (!needed_bytes_no_align) /* not a failure, no fonts to write */
1010 {
1011 /* no, you don't really need to write any bytes at all. */
1012 metadata->count = 0;
eb0cf671 1013 return 0;
8e351527 1014 }
eb0cf671
PL
1015
1016 current_dir_block = malloc (metadata->count);
1017 if (!current_dir_block)
1018 goto bail;
8e351527
PL
1019 // shut up valgrind
1020 memset (current_dir_block, 0, metadata->count);
eb0cf671
PL
1021 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
1022
8e351527 1023 if ((void *)((char *)current_dir_block+metadata->count) < final_dir_block)
eb0cf671
PL
1024 goto bail;
1025
1026 if (!FcFontSetSerialize (bank, set))
1027 goto bail;
1028
1029 return current_dir_block;
212c9f43 1030
4262e0b3 1031 bail:
eb0cf671
PL
1032 free (current_dir_block);
1033 return 0;
212c9f43
PL
1034}
1035
1036/* write serialized state to the cache file */
1037FcBool
2304e38f 1038FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
212c9f43 1039{
ea44e218 1040 char *cache_file;
3bfae75d 1041 char *cache_hashed;
1d879de2
PL
1042 int fd, fd_orig, i, dirs_count;
1043 FcAtomic *atomic;
1044 FcCache metadata;
1045 off_t current_arch_start = 0, truncate_to;
ea44e218
PL
1046 char name_buf[FC_MAX_FILE_LEN];
1047 int collisions;
8245771d 1048
1d879de2 1049 char *current_arch_machine_name, * header;
ea44e218 1050 void *current_dir_block = 0;
212c9f43 1051
ea44e218 1052 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
4262e0b3
PL
1053 if (!cache_file)
1054 goto bail;
1055
ea44e218
PL
1056 /* Ensure that we're not trampling a cache for some other dir. */
1057 /* This is slightly different from FcDirCacheOpen, since it
1058 * needs the filename, not the file descriptor. */
1059 fd = -1; collisions = 0;
1060 do
1061 {
3bfae75d
PL
1062 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
1063 if (!cache_hashed)
ea44e218
PL
1064 goto bail0;
1065
1066 if (fd > 0)
1067 close (fd);
3bfae75d 1068 fd = open(cache_hashed, O_RDONLY);
ea44e218
PL
1069 if (fd == -1)
1070 break;
1071 FcCacheReadString (fd, name_buf, sizeof (name_buf));
1072 close (fd);
1073
1074 if (!strlen(name_buf))
1075 break;
1076
1077 if (strcmp (name_buf, cache_file) != 0)
1078 continue;
1079 } while (0);
1080
eb0cf671 1081 current_dir_block = FcDirCacheProduce (set, &metadata);
212c9f43 1082
a9698bed 1083 if (metadata.count && !current_dir_block)
ea44e218 1084 goto bail1;
212c9f43 1085
4262e0b3
PL
1086 if (FcDebug () & FC_DBG_CACHE)
1087 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
1088
3bfae75d 1089 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
1d879de2 1090 if (!atomic)
ea44e218 1091 goto bail1;
212c9f43 1092
1d879de2 1093 if (!FcAtomicLock (atomic))
ec760b17
PL
1094 {
1095 /* Now try rewriting the original version of the file. */
1096 FcAtomicDestroy (atomic);
1d879de2 1097
3bfae75d 1098 atomic = FcAtomicCreate ((FcChar8 *)cache_file);
ec760b17
PL
1099 fd_orig = open (cache_file, O_RDONLY);
1100 if (fd_orig == -1)
1101 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1102
1103 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1104 if (fd == -1)
1105 goto bail2;
1106 }
1107
1108 /* In all cases, try opening the real location of the cache file first. */
1109 /* (even if that's not atomic.) */
1110 fd_orig = open (cache_file, O_RDONLY);
1111 if (fd_orig == -1)
1112 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1d879de2
PL
1113
1114 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1115 if (fd == -1)
ec760b17 1116 goto bail3;
ea44e218
PL
1117
1118 FcCacheWriteString (fd, cache_file);
1d879de2 1119
eb0cf671 1120 current_arch_machine_name = FcCacheMachineSignature ();
1d879de2
PL
1121 current_arch_start = 0;
1122
1123 if (fd_orig != -1)
1124 current_arch_start =
ea44e218 1125 FcCacheSkipToArch(fd_orig, current_arch_machine_name, FcFalse);
1d879de2 1126
212c9f43 1127 if (current_arch_start < 0)
649cc361 1128 current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
212c9f43 1129
1d879de2 1130 if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
ea44e218 1131 goto bail4;
1d879de2
PL
1132
1133 if (fd_orig != -1)
1134 close (fd_orig);
212c9f43
PL
1135
1136 current_arch_start = lseek(fd, 0, SEEK_CUR);
1137 if (ftruncate (fd, current_arch_start) == -1)
ea44e218 1138 goto bail4;
212c9f43 1139
6aee8c6f
PL
1140 /* allocate space for subdir names in this block */
1141 dirs_count = 0;
1142 for (i = 0; i < dirs->size; i++)
1143 dirs_count += strlen((char *)dirs->strs[i]) + 1;
1144 dirs_count ++;
1145
212c9f43 1146 /* now write the address of the next offset */
6aee8c6f 1147 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start;
2dbe7597 1148 header = malloc (10 + strlen (current_arch_machine_name));
eb0cf671 1149 if (!header)
ea44e218 1150 goto bail4;
2dbe7597
PL
1151 sprintf (header, "%8x ", (int)truncate_to);
1152 strcat (header, current_arch_machine_name);
eb0cf671 1153 if (!FcCacheWriteString (fd, header))
ea44e218 1154 goto bail5;
212c9f43 1155
2304e38f 1156 for (i = 0; i < dirs->size; i++)
8245771d 1157 FcCacheWriteString (fd, (char *)dirs->strs[i]);
2304e38f
PL
1158 FcCacheWriteString (fd, "");
1159
4262e0b3 1160 write (fd, &metadata, sizeof(FcCache));
a9698bed
PL
1161 if (metadata.count)
1162 {
1163 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
1164 write (fd, current_dir_block, metadata.count);
1165 free (current_dir_block);
1166 }
4262e0b3 1167
2dbe7597 1168 /* this actually serves to pad out the cache file, if needed */
212c9f43 1169 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
ea44e218 1170 goto bail5;
212c9f43
PL
1171
1172 close(fd);
1d879de2 1173 if (!FcAtomicReplaceOrig(atomic))
ea44e218 1174 goto bail5;
3bfae75d 1175 FcStrFree ((FcChar8 *)cache_hashed);
1d879de2
PL
1176 FcAtomicUnlock (atomic);
1177 FcAtomicDestroy (atomic);
212c9f43
PL
1178 return FcTrue;
1179
ea44e218 1180 bail5:
0230c9f8 1181 free (header);
ea44e218 1182 bail4:
1d879de2 1183 close (fd);
ea44e218 1184 bail3:
1d879de2 1185 FcAtomicUnlock (atomic);
ea44e218 1186 bail2:
1d879de2 1187 FcAtomicDestroy (atomic);
ea44e218 1188 bail1:
3bfae75d 1189 FcStrFree ((FcChar8 *)cache_hashed);
eb0cf671 1190 bail0:
8245771d 1191 unlink ((char *)cache_file);
4262e0b3 1192 free (cache_file);
1d879de2
PL
1193 if (current_dir_block)
1194 free (current_dir_block);
1195 bail:
212c9f43
PL
1196 return FcFalse;
1197}
1198
2dbe7597 1199static char *
eb0cf671 1200FcCacheMachineSignature ()
2dbe7597
PL
1201{
1202 static char buf[MACHINE_SIGNATURE_SIZE];
7fd7221e 1203 int32_t magic = ENDIAN_TEST;
2dbe7597
PL
1204 char * m = (char *)&magic;
1205
1206 sprintf (buf, "%2x%2x%2x%2x "
1207 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
7fd7221e 1208 "%4x %4x %4x %4x %4x %4x %4x %4x\n",
2dbe7597 1209 m[0], m[1], m[2], m[3],
cd310911
PL
1210 (unsigned int)sizeof (char),
1211 (unsigned int)sizeof (char *),
1212 (unsigned int)sizeof (int),
1213 (unsigned int)sizeof (FcPattern),
1214 (unsigned int)sizeof (FcPatternEltPtr),
1215 (unsigned int)sizeof (struct _FcPatternElt *),
1216 (unsigned int)sizeof (FcPatternElt),
1217 (unsigned int)sizeof (FcObjectPtr),
1218 (unsigned int)sizeof (FcValueListPtr),
1219 (unsigned int)sizeof (FcValue),
1220 (unsigned int)sizeof (FcValueBinding),
1221 (unsigned int)sizeof (struct _FcValueList *),
1222 (unsigned int)sizeof (FcCharSet),
1223 (unsigned int)sizeof (FcCharLeaf **),
1224 (unsigned int)sizeof (FcChar16 *),
1225 (unsigned int)sizeof (FcChar16),
1226 (unsigned int)sizeof (FcCharLeaf),
1227 (unsigned int)sizeof (FcChar32),
7fd7221e
PL
1228 (unsigned int)sizeof (FcCache),
1229 (unsigned int)sysconf(_SC_PAGESIZE));
2dbe7597
PL
1230
1231 return buf;
1232}
1233
4262e0b3 1234static int banks_ptr = 0, banks_alloc = 0;
b8948e85 1235int * _fcBankId = 0, * _fcBankIdx = 0;
e77c1718 1236static const char ** bankDirs = 0;
4262e0b3 1237
eb0cf671 1238static FcBool
4262e0b3
PL
1239FcCacheHaveBank (int bank)
1240{
1241 int i;
1242
1243 if (bank < FC_BANK_FIRST)
1244 return FcTrue;
1245
1246 for (i = 0; i < banks_ptr; i++)
b8948e85 1247 if (_fcBankId[i] == bank)
4262e0b3
PL
1248 return FcTrue;
1249
1250 return FcFalse;
1251}
1252
1253int
b8948e85 1254FcCacheBankToIndexMTF (int bank)
4262e0b3 1255{
751932dd 1256 int i, j;
4262e0b3
PL
1257
1258 for (i = 0; i < banks_ptr; i++)
b8948e85 1259 if (_fcBankId[_fcBankIdx[i]] == bank)
751932dd 1260 {
b8948e85 1261 int t = _fcBankIdx[i];
751932dd
PL
1262
1263 for (j = i; j > 0; j--)
b8948e85
PL
1264 _fcBankIdx[j] = _fcBankIdx[j-1];
1265 _fcBankIdx[0] = t;
751932dd
PL
1266 return t;
1267 }
4262e0b3 1268
2dbe7597 1269 if (banks_ptr >= banks_alloc)
4262e0b3 1270 {
751932dd 1271 int * b, * bidx;
e77c1718
PL
1272 const char ** bds;
1273
b8948e85 1274 b = realloc (_fcBankId, (banks_alloc + 4) * sizeof(int));
4262e0b3
PL
1275 if (!b)
1276 return -1;
b8948e85 1277 _fcBankId = b;
751932dd 1278
b8948e85 1279 bidx = realloc (_fcBankIdx, (banks_alloc + 4) * sizeof(int));
751932dd
PL
1280 if (!bidx)
1281 return -1;
b8948e85 1282 _fcBankIdx = bidx;
751932dd 1283
e77c1718
PL
1284 bds = realloc (bankDirs, (banks_alloc + 4) * sizeof (char *));
1285 if (!bds)
1286 return -1;
1287 bankDirs = bds;
1288
4262e0b3
PL
1289 banks_alloc += 4;
1290 }
1291
1292 i = banks_ptr++;
b8948e85
PL
1293 _fcBankId[i] = bank;
1294 _fcBankIdx[i] = i;
4262e0b3
PL
1295 return i;
1296}
e77c1718
PL
1297
1298static void
1299FcCacheAddBankDir (int bank, const char * dir)
1300{
b8948e85 1301 int bi = FcCacheBankToIndexMTF (bank);
e77c1718
PL
1302
1303 if (bi < 0)
1304 return;
1305
1306 bankDirs[bi] = (const char *)FcStrCopy ((FcChar8 *)dir);
1307}
1308
1309const char *
1310FcCacheFindBankDir (int bank)
1311{
1312 int bi = FcCacheBankToIndex (bank);
1313 return bankDirs[bi];
1314}
1315
ea44e218
PL
1316/*
1317 * This code implements the MD5 message-digest algorithm.
1318 * The algorithm is due to Ron Rivest. This code was
1319 * written by Colin Plumb in 1993, no copyright is claimed.
1320 * This code is in the public domain; do with it what you wish.
1321 *
1322 * Equivalent code is available from RSA Data Security, Inc.
1323 * This code has been tested against that, and is equivalent,
1324 * except that you don't need to include two pages of legalese
1325 * with every copy.
1326 *
1327 * To compute the message digest of a chunk of bytes, declare an
1328 * MD5Context structure, pass it to MD5Init, call MD5Update as
1329 * needed on buffers full of bytes, and then call MD5Final, which
1330 * will fill a supplied 16-byte array with the digest.
1331 */
1332
1333#ifndef HIGHFIRST
1334#define byteReverse(buf, len) /* Nothing */
1335#else
1336/*
1337 * Note: this code is harmless on little-endian machines.
1338 */
1339void byteReverse(unsigned char *buf, unsigned longs)
1340{
1341 FcChar32 t;
1342 do {
1343 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1344 ((unsigned) buf[1] << 8 | buf[0]);
1345 *(FcChar32 *) buf = t;
1346 buf += 4;
1347 } while (--longs);
1348}
1349#endif
1350
1351/*
1352 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1353 * initialization constants.
1354 */
1355static void MD5Init(struct MD5Context *ctx)
1356{
1357 ctx->buf[0] = 0x67452301;
1358 ctx->buf[1] = 0xefcdab89;
1359 ctx->buf[2] = 0x98badcfe;
1360 ctx->buf[3] = 0x10325476;
1361
1362 ctx->bits[0] = 0;
1363 ctx->bits[1] = 0;
1364}
1365
1366/*
1367 * Update context to reflect the concatenation of another buffer full
1368 * of bytes.
1369 */
1370static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len)
1371{
1372 FcChar32 t;
1373
1374 /* Update bitcount */
1375
1376 t = ctx->bits[0];
1377 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1378 ctx->bits[1]++; /* Carry from low to high */
1379 ctx->bits[1] += len >> 29;
1380
1381 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1382
1383 /* Handle any leading odd-sized chunks */
1384
1385 if (t) {
1386 unsigned char *p = (unsigned char *) ctx->in + t;
1387
1388 t = 64 - t;
1389 if (len < t) {
1390 memcpy(p, buf, len);
1391 return;
1392 }
1393 memcpy(p, buf, t);
1394 byteReverse(ctx->in, 16);
1395 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1396 buf += t;
1397 len -= t;
1398 }
1399 /* Process data in 64-byte chunks */
1400
1401 while (len >= 64) {
1402 memcpy(ctx->in, buf, 64);
1403 byteReverse(ctx->in, 16);
1404 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1405 buf += 64;
1406 len -= 64;
1407 }
1408
1409 /* Handle any remaining bytes of data. */
1410
1411 memcpy(ctx->in, buf, len);
1412}
1413
1414/*
1415 * Final wrapup - pad to 64-byte boundary with the bit pattern
1416 * 1 0* (64-bit count of bits processed, MSB-first)
1417 */
1418static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1419{
1420 unsigned count;
1421 unsigned char *p;
1422
1423 /* Compute number of bytes mod 64 */
1424 count = (ctx->bits[0] >> 3) & 0x3F;
1425
1426 /* Set the first char of padding to 0x80. This is safe since there is
1427 always at least one byte free */
1428 p = ctx->in + count;
1429 *p++ = 0x80;
1430
1431 /* Bytes of padding needed to make 64 bytes */
1432 count = 64 - 1 - count;
1433
1434 /* Pad out to 56 mod 64 */
1435 if (count < 8) {
1436 /* Two lots of padding: Pad the first block to 64 bytes */
1437 memset(p, 0, count);
1438 byteReverse(ctx->in, 16);
1439 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1440
1441 /* Now fill the next block with 56 bytes */
1442 memset(ctx->in, 0, 56);
1443 } else {
1444 /* Pad block to 56 bytes */
1445 memset(p, 0, count - 8);
1446 }
1447 byteReverse(ctx->in, 14);
1448
1449 /* Append length in bits and transform */
1450 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1451 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1452
1453 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1454 byteReverse((unsigned char *) ctx->buf, 4);
1455 memcpy(digest, ctx->buf, 16);
1456 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
1457}
1458
1459
1460/* The four core functions - F1 is optimized somewhat */
1461
1462/* #define F1(x, y, z) (x & y | ~x & z) */
1463#define F1(x, y, z) (z ^ (x & (y ^ z)))
1464#define F2(x, y, z) F1(z, x, y)
1465#define F3(x, y, z) (x ^ y ^ z)
1466#define F4(x, y, z) (y ^ (x | ~z))
1467
1468/* This is the central step in the MD5 algorithm. */
1469#define MD5STEP(f, w, x, y, z, data, s) \
1470 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1471
1472/*
1473 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1474 * reflect the addition of 16 longwords of new data. MD5Update blocks
1475 * the data and converts bytes into longwords for this routine.
1476 */
1477static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1478{
1479 register FcChar32 a, b, c, d;
1480
1481 a = buf[0];
1482 b = buf[1];
1483 c = buf[2];
1484 d = buf[3];
1485
1486 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1487 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1488 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1489 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1490 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1491 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1492 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1493 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1494 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1495 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1496 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1497 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1498 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1499 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1500 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1501 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1502
1503 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1504 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1505 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1506 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1507 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1508 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1509 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1510 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1511 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1512 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1513 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1514 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1515 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1516 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1517 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1518 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1519
1520 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1521 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1522 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1523 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1524 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1525 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1526 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1527 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1528 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1529 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1530 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1531 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1532 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1533 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1534 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1535 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1536
1537 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1538 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1539 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1540 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1541 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1542 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1543 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1544 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1545 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1546 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1547 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1548 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1549 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1550 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1551 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1552 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1553
1554 buf[0] += a;
1555 buf[1] += b;
1556 buf[2] += c;
1557 buf[3] += d;
1558}