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