]> git.wh0rd.org - fontconfig.git/blob - src/fccache.c
Store font directory mtime in cache file.
[fontconfig.git] / src / fccache.c
1 /*
2 * Copyright © 2000 Keith Packard
3 * Copyright © 2005 Patrick Lam
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of Keith Packard not be used in
10 * advertising or publicity pertaining to distribution of the software without
11 * specific, written prior permission. Keith Packard makes no
12 * representations about the suitability of this software for any purpose. It
13 * is provided "as is" without express or implied warranty.
14 *
15 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21 * PERFORMANCE OF THIS SOFTWARE.
22 */
23
24 #include "fcint.h"
25 #include "../fc-arch/fcarch.h"
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <dirent.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <assert.h>
32 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
33 # include <unistd.h>
34 # include <sys/mman.h>
35 #elif defined(_WIN32)
36 # include <windows.h>
37 #endif
38
39 #ifndef O_BINARY
40 #define O_BINARY 0
41 #endif
42
43 struct MD5Context {
44 FcChar32 buf[4];
45 FcChar32 bits[2];
46 unsigned char in[64];
47 };
48
49 static void MD5Init(struct MD5Context *ctx);
50 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len);
51 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
52 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]);
53
54 #define CACHEBASE_LEN (1 + 32 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX))
55
56 static const char bin2hex[] = { '0', '1', '2', '3',
57 '4', '5', '6', '7',
58 '8', '9', 'a', 'b',
59 'c', 'd', 'e', 'f' };
60
61 static FcChar8 *
62 FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN])
63 {
64 unsigned char hash[16];
65 FcChar8 *hex_hash;
66 int cnt;
67 struct MD5Context ctx;
68
69 MD5Init (&ctx);
70 MD5Update (&ctx, (const unsigned char *)dir, strlen ((const char *) dir));
71
72 MD5Final (hash, &ctx);
73
74 cache_base[0] = '/';
75 hex_hash = cache_base + 1;
76 for (cnt = 0; cnt < 16; ++cnt)
77 {
78 hex_hash[2*cnt ] = bin2hex[hash[cnt] >> 4];
79 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
80 }
81 hex_hash[2*cnt] = 0;
82 strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX);
83
84 return cache_base;
85 }
86
87 FcBool
88 FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
89 {
90 FcChar8 *cache_hashed = NULL;
91 FcChar8 cache_base[CACHEBASE_LEN];
92 FcStrList *list;
93 FcChar8 *cache_dir;
94
95 FcDirCacheBasename (dir, cache_base);
96
97 list = FcStrListCreate (config->cacheDirs);
98 if (!list)
99 return FcFalse;
100
101 while ((cache_dir = FcStrListNext (list)))
102 {
103 cache_hashed = FcStrPlus (cache_dir, cache_base);
104 if (!cache_hashed)
105 break;
106 (void) unlink ((char *) cache_hashed);
107 FcStrFree (cache_hashed);
108 }
109 FcStrListDone (list);
110 /* return FcFalse if something went wrong */
111 if (cache_dir)
112 return FcFalse;
113 return FcTrue;
114 }
115
116 static int
117 FcDirCacheOpenFile (const FcChar8 *cache_file, struct stat *file_stat)
118 {
119 int fd;
120
121 fd = open((char *) cache_file, O_RDONLY | O_BINARY);
122 if (fd < 0)
123 return fd;
124 if (fstat (fd, file_stat) < 0)
125 {
126 close (fd);
127 return -1;
128 }
129 return fd;
130 }
131
132 /*
133 * Look for a cache file for the specified dir. Attempt
134 * to use each one we find, stopping when the callback
135 * indicates success
136 */
137 static FcBool
138 FcDirCacheProcess (FcConfig *config, const FcChar8 *dir,
139 FcBool (*callback) (int fd, struct stat *fd_stat,
140 struct stat *dir_stat, void *closure),
141 void *closure, FcChar8 **cache_file_ret)
142 {
143 int fd = -1;
144 FcChar8 cache_base[CACHEBASE_LEN];
145 FcStrList *list;
146 FcChar8 *cache_dir;
147 struct stat file_stat, dir_stat;
148 FcBool ret = FcFalse;
149
150 if (stat ((char *) dir, &dir_stat) < 0)
151 return FcFalse;
152
153 FcDirCacheBasename (dir, cache_base);
154
155 list = FcStrListCreate (config->cacheDirs);
156 if (!list)
157 return FcFalse;
158
159 while ((cache_dir = FcStrListNext (list)))
160 {
161 FcChar8 *cache_hashed = FcStrPlus (cache_dir, cache_base);
162 if (!cache_hashed)
163 break;
164 fd = FcDirCacheOpenFile (cache_hashed, &file_stat);
165 if (fd >= 0) {
166 ret = (*callback) (fd, &file_stat, &dir_stat, closure);
167 close (fd);
168 if (ret)
169 {
170 if (cache_file_ret)
171 *cache_file_ret = cache_hashed;
172 else
173 FcStrFree (cache_hashed);
174 break;
175 }
176 }
177 FcStrFree (cache_hashed);
178 }
179 FcStrListDone (list);
180
181 return ret;
182 }
183
184 #define FC_CACHE_MIN_MMAP 1024
185
186 /*
187 * Skip list element, make sure the 'next' pointer is the last thing
188 * in the structure, it will be allocated large enough to hold all
189 * of the necessary pointers
190 */
191
192 typedef struct _FcCacheSkip FcCacheSkip;
193
194 struct _FcCacheSkip {
195 FcCache *cache;
196 int ref;
197 intptr_t size;
198 dev_t cache_dev;
199 ino_t cache_ino;
200 time_t cache_mtime;
201 FcCacheSkip *next[1];
202 };
203
204 /*
205 * The head of the skip list; pointers for every possible level
206 * in the skip list, plus the largest level in the list
207 */
208
209 #define FC_CACHE_MAX_LEVEL 16
210
211 static FcCacheSkip *fcCacheChains[FC_CACHE_MAX_LEVEL];
212 static int fcCacheMaxLevel;
213
214 #if HAVE_RANDOM
215 # define FcRandom() random()
216 #else
217 # if HAVE_LRAND48
218 # define FcRandom() lrand48()
219 # else
220 # if HAVE_RAND
221 # define FcRandom() rand()
222 # endif
223 # endif
224 #endif
225 /*
226 * Generate a random level number, distributed
227 * so that each level is 1/4 as likely as the one before
228 *
229 * Note that level numbers run 1 <= level <= MAX_LEVEL
230 */
231 static int
232 random_level (void)
233 {
234 /* tricky bit -- each bit is '1' 75% of the time */
235 long int bits = FcRandom () | FcRandom ();
236 int level = 0;
237
238 while (++level < FC_CACHE_MAX_LEVEL)
239 {
240 if (bits & 1)
241 break;
242 bits >>= 1;
243 }
244 return level;
245 }
246
247 /*
248 * Insert cache into the list
249 */
250 static FcBool
251 FcCacheInsert (FcCache *cache, struct stat *cache_stat)
252 {
253 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
254 FcCacheSkip *s, **next;
255 int i, level;
256
257 /*
258 * Find links along each chain
259 */
260 next = fcCacheChains;
261 for (i = fcCacheMaxLevel; --i >= 0; )
262 {
263 for (; (s = next[i]); next = s->next)
264 if (s->cache > cache)
265 break;
266 update[i] = &next[i];
267 }
268
269 /*
270 * Create new list element
271 */
272 level = random_level ();
273 if (level > fcCacheMaxLevel)
274 {
275 level = fcCacheMaxLevel + 1;
276 update[fcCacheMaxLevel] = &fcCacheChains[fcCacheMaxLevel];
277 fcCacheMaxLevel = level;
278 }
279
280 s = malloc (sizeof (FcCacheSkip) + (level - 1) * sizeof (FcCacheSkip *));
281 if (!s)
282 return FcFalse;
283
284 s->cache = cache;
285 s->size = cache->size;
286 s->ref = 1;
287 if (cache_stat)
288 {
289 s->cache_dev = cache_stat->st_dev;
290 s->cache_ino = cache_stat->st_ino;
291 s->cache_mtime = cache_stat->st_mtime;
292 }
293 else
294 {
295 s->cache_dev = 0;
296 s->cache_ino = 0;
297 s->cache_mtime = 0;
298 }
299
300 /*
301 * Insert into all fcCacheChains
302 */
303 for (i = 0; i < level; i++)
304 {
305 s->next[i] = *update[i];
306 *update[i] = s;
307 }
308 return FcTrue;
309 }
310
311 static FcCacheSkip *
312 FcCacheFindByAddr (void *object)
313 {
314 int i;
315 FcCacheSkip **next = fcCacheChains;
316 FcCacheSkip *s;
317
318 /*
319 * Walk chain pointers one level at a time
320 */
321 for (i = fcCacheMaxLevel; --i >= 0;)
322 while (next[i] && (char *) object >= ((char *) next[i]->cache + next[i]->size))
323 next = next[i]->next;
324 /*
325 * Here we are
326 */
327 s = next[0];
328 if (s && (char *) object < ((char *) s->cache + s->size))
329 return s;
330 return NULL;
331 }
332
333 static void
334 FcCacheRemove (FcCache *cache)
335 {
336 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
337 FcCacheSkip *s, **next;
338 int i;
339
340 /*
341 * Find links along each chain
342 */
343 next = fcCacheChains;
344 for (i = fcCacheMaxLevel; --i >= 0; )
345 {
346 for (; (s = next[i]); next = s->next)
347 if (s->cache >= cache)
348 break;
349 update[i] = &next[i];
350 }
351 s = next[0];
352 for (i = 0; i < fcCacheMaxLevel && *update[i] == s; i++)
353 *update[i] = s->next[i];
354 while (fcCacheMaxLevel > 0 && fcCacheChains[fcCacheMaxLevel - 1] == NULL)
355 fcCacheMaxLevel--;
356 free (s);
357 }
358
359 static FcCache *
360 FcCacheFindByStat (struct stat *cache_stat)
361 {
362 FcCacheSkip *s;
363
364 for (s = fcCacheChains[0]; s; s = s->next[0])
365 if (s->cache_dev == cache_stat->st_dev &&
366 s->cache_ino == cache_stat->st_ino &&
367 s->cache_mtime == cache_stat->st_mtime)
368 {
369 s->ref++;
370 return s->cache;
371 }
372 return NULL;
373 }
374
375 static void
376 FcDirCacheDispose (FcCache *cache)
377 {
378 switch (cache->magic) {
379 case FC_CACHE_MAGIC_ALLOC:
380 free (cache);
381 break;
382 case FC_CACHE_MAGIC_MMAP:
383 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
384 munmap (cache, cache->size);
385 #elif defined(_WIN32)
386 UnmapViewOfFile (cache);
387 #endif
388 break;
389 }
390 FcCacheRemove (cache);
391 }
392
393 void
394 FcCacheObjectReference (void *object)
395 {
396 FcCacheSkip *skip = FcCacheFindByAddr (object);
397
398 if (skip)
399 skip->ref++;
400 }
401
402 void
403 FcCacheObjectDereference (void *object)
404 {
405 FcCacheSkip *skip = FcCacheFindByAddr (object);
406
407 if (skip)
408 {
409 skip->ref--;
410 if (skip->ref <= 0)
411 FcDirCacheDispose (skip->cache);
412 }
413 }
414
415 void
416 FcCacheFini (void)
417 {
418 int i;
419
420 for (i = 0; i < FC_CACHE_MAX_LEVEL; i++)
421 assert (fcCacheChains[i] == NULL);
422 assert (fcCacheMaxLevel == 0);
423 }
424
425 static FcBool
426 FcCacheTimeValid (FcCache *cache, struct stat *dir_stat)
427 {
428 struct stat dir_static;
429
430 if (!dir_stat)
431 {
432 if (stat ((const char *) FcCacheDir (cache), &dir_static) < 0)
433 return FcFalse;
434 dir_stat = &dir_static;
435 }
436 if (FcDebug () & FC_DBG_CACHE)
437 printf ("FcCacheTimeValid dir \"%s\" cache time %d dir time %d\n",
438 FcCacheDir (cache), cache->mtime, (int) dir_stat->st_mtime);
439 return cache->mtime == (int) dir_stat->st_mtime;
440 }
441
442 /*
443 * Map a cache file into memory
444 */
445 static FcCache *
446 FcDirCacheMapFd (int fd, struct stat *fd_stat, struct stat *dir_stat)
447 {
448 FcCache *cache;
449 FcBool allocated = FcFalse;
450
451 if (fd_stat->st_size < sizeof (FcCache))
452 return NULL;
453 cache = FcCacheFindByStat (fd_stat);
454 if (cache)
455 return cache;
456 /*
457 * For small cache files, just read them into memory
458 */
459 if (fd_stat->st_size >= FC_CACHE_MIN_MMAP)
460 {
461 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
462 cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0);
463 #elif defined(_WIN32)
464 {
465 HANDLE hFileMap;
466
467 cache = NULL;
468 hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL,
469 PAGE_READONLY, 0, 0, NULL);
470 if (hFileMap != NULL)
471 {
472 cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0,
473 fd_stat->st_size);
474 CloseHandle (hFileMap);
475 }
476 }
477 #endif
478 }
479 if (!cache)
480 {
481 cache = malloc (fd_stat->st_size);
482 if (!cache)
483 return NULL;
484
485 if (read (fd, cache, fd_stat->st_size) != fd_stat->st_size)
486 {
487 free (cache);
488 return NULL;
489 }
490 allocated = FcTrue;
491 }
492 if (cache->magic != FC_CACHE_MAGIC_MMAP ||
493 cache->version < FC_CACHE_CONTENT_VERSION ||
494 cache->size != fd_stat->st_size ||
495 !FcCacheInsert (cache, fd_stat) ||
496 !FcCacheTimeValid (cache, dir_stat))
497 {
498 if (allocated)
499 free (cache);
500 else
501 {
502 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
503 munmap (cache, fd_stat->st_size);
504 #elif defined(_WIN32)
505 UnmapViewOfFile (cache);
506 #endif
507 }
508 return NULL;
509 }
510
511 /* Mark allocated caches so they're freed rather than unmapped */
512 if (allocated)
513 cache->magic = FC_CACHE_MAGIC_ALLOC;
514
515 return cache;
516 }
517
518 void
519 FcDirCacheReference (FcCache *cache, int nref)
520 {
521 FcCacheSkip *skip = FcCacheFindByAddr (cache);
522
523 if (skip)
524 skip->ref += nref;
525 }
526
527 void
528 FcDirCacheUnload (FcCache *cache)
529 {
530 FcCacheObjectDereference (cache);
531 }
532
533 static FcBool
534 FcDirCacheMapHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
535 {
536 FcCache *cache = FcDirCacheMapFd (fd, fd_stat, dir_stat);
537
538 if (!cache)
539 return FcFalse;
540 *((FcCache **) closure) = cache;
541 return FcTrue;
542 }
543
544 FcCache *
545 FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file)
546 {
547 FcCache *cache = NULL;
548
549 if (!FcDirCacheProcess (config, dir,
550 FcDirCacheMapHelper,
551 &cache, cache_file))
552 return NULL;
553 return cache;
554 }
555
556 FcCache *
557 FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat)
558 {
559 int fd;
560 FcCache *cache;
561
562 fd = FcDirCacheOpenFile (cache_file, file_stat);
563 if (fd < 0)
564 return NULL;
565 cache = FcDirCacheMapFd (fd, file_stat, NULL);
566 close (fd);
567 return cache;
568 }
569
570 /*
571 * Validate a cache file by reading the header and checking
572 * the magic number and the size field
573 */
574 static FcBool
575 FcDirCacheValidateHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
576 {
577 FcBool ret = FcTrue;
578 FcCache c;
579
580 if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache))
581 ret = FcFalse;
582 else if (c.magic != FC_CACHE_MAGIC_MMAP)
583 ret = FcFalse;
584 else if (c.version < FC_CACHE_CONTENT_VERSION)
585 ret = FcFalse;
586 else if (fd_stat->st_size != c.size)
587 ret = FcFalse;
588 else if (c.mtime != (int) dir_stat->st_mtime)
589 ret = FcFalse;
590 return ret;
591 }
592
593 static FcBool
594 FcDirCacheValidConfig (const FcChar8 *dir, FcConfig *config)
595 {
596 return FcDirCacheProcess (config, dir,
597 FcDirCacheValidateHelper,
598 NULL, NULL);
599 }
600
601 FcBool
602 FcDirCacheValid (const FcChar8 *dir)
603 {
604 FcConfig *config;
605
606 config = FcConfigGetCurrent ();
607 if (!config)
608 return FcFalse;
609
610 return FcDirCacheValidConfig (dir, config);
611 }
612
613 /*
614 * Build a cache structure from the given contents
615 */
616 FcCache *
617 FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcStrSet *dirs)
618 {
619 FcSerialize *serialize = FcSerializeCreate ();
620 FcCache *cache;
621 int i;
622 intptr_t cache_offset;
623 intptr_t dirs_offset;
624 FcChar8 *dir_serialize;
625 intptr_t *dirs_serialize;
626 FcFontSet *set_serialize;
627
628 if (!serialize)
629 return NULL;
630 /*
631 * Space for cache structure
632 */
633 cache_offset = FcSerializeReserve (serialize, sizeof (FcCache));
634 /*
635 * Directory name
636 */
637 if (!FcStrSerializeAlloc (serialize, dir))
638 goto bail1;
639 /*
640 * Subdirs
641 */
642 dirs_offset = FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *));
643 for (i = 0; i < dirs->num; i++)
644 if (!FcStrSerializeAlloc (serialize, dirs->strs[i]))
645 goto bail1;
646
647 /*
648 * Patterns
649 */
650 if (!FcFontSetSerializeAlloc (serialize, set))
651 goto bail1;
652
653 /* Serialize layout complete. Now allocate space and fill it */
654 cache = malloc (serialize->size);
655 if (!cache)
656 goto bail1;
657 /* shut up valgrind */
658 memset (cache, 0, serialize->size);
659
660 serialize->linear = cache;
661
662 cache->magic = FC_CACHE_MAGIC_ALLOC;
663 cache->version = FC_CACHE_CONTENT_VERSION;
664 cache->size = serialize->size;
665 cache->mtime = (int) dir_stat->st_mtime;
666
667 /*
668 * Serialize directory name
669 */
670 dir_serialize = FcStrSerialize (serialize, dir);
671 if (!dir_serialize)
672 goto bail2;
673 cache->dir = FcPtrToOffset (cache, dir_serialize);
674
675 /*
676 * Serialize sub dirs
677 */
678 dirs_serialize = FcSerializePtr (serialize, dirs);
679 if (!dirs_serialize)
680 goto bail2;
681 cache->dirs = FcPtrToOffset (cache, dirs_serialize);
682 cache->dirs_count = dirs->num;
683 for (i = 0; i < dirs->num; i++)
684 {
685 FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]);
686 if (!d_serialize)
687 goto bail2;
688 dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize);
689 }
690
691 /*
692 * Serialize font set
693 */
694 set_serialize = FcFontSetSerialize (serialize, set);
695 if (!set_serialize)
696 goto bail2;
697 cache->set = FcPtrToOffset (cache, set_serialize);
698
699 FcSerializeDestroy (serialize);
700
701 FcCacheInsert (cache, NULL);
702
703 return cache;
704
705 bail2:
706 free (cache);
707 bail1:
708 FcSerializeDestroy (serialize);
709 return NULL;
710 }
711
712
713 #ifdef _WIN32
714 #define mkdir(path,mode) _mkdir(path)
715 #endif
716
717 static FcBool
718 FcMakeDirectory (const FcChar8 *dir)
719 {
720 FcChar8 *parent;
721 FcBool ret;
722
723 if (strlen ((char *) dir) == 0)
724 return FcFalse;
725
726 parent = FcStrDirname (dir);
727 if (!parent)
728 return FcFalse;
729 if (access ((char *) parent, W_OK|X_OK) == 0)
730 ret = mkdir ((char *) dir, 0777) == 0;
731 else if (access ((char *) parent, F_OK) == -1)
732 ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0777) == 0);
733 else
734 ret = FcFalse;
735 FcStrFree (parent);
736 return ret;
737 }
738
739 /* write serialized state to the cache file */
740 FcBool
741 FcDirCacheWrite (FcCache *cache, FcConfig *config)
742 {
743 FcChar8 *dir = FcCacheDir (cache);
744 FcChar8 cache_base[CACHEBASE_LEN];
745 FcChar8 *cache_hashed;
746 int fd;
747 FcAtomic *atomic;
748 FcStrList *list;
749 FcChar8 *cache_dir = NULL;
750 FcChar8 *test_dir;
751 int magic;
752 int written;
753
754 /*
755 * Write it to the first directory in the list which is writable
756 */
757
758 list = FcStrListCreate (config->cacheDirs);
759 if (!list)
760 return FcFalse;
761 while ((test_dir = FcStrListNext (list))) {
762 if (access ((char *) test_dir, W_OK|X_OK) == 0)
763 {
764 cache_dir = test_dir;
765 break;
766 }
767 else
768 {
769 /*
770 * If the directory doesn't exist, try to create it
771 */
772 if (access ((char *) test_dir, F_OK) == -1) {
773 if (FcMakeDirectory (test_dir))
774 {
775 cache_dir = test_dir;
776 break;
777 }
778 }
779 }
780 }
781 FcStrListDone (list);
782 if (!cache_dir)
783 return FcFalse;
784
785 FcDirCacheBasename (dir, cache_base);
786 cache_hashed = FcStrPlus (cache_dir, cache_base);
787 if (!cache_hashed)
788 return FcFalse;
789
790 if (FcDebug () & FC_DBG_CACHE)
791 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
792 dir, cache_hashed);
793
794 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
795 if (!atomic)
796 goto bail1;
797
798 if (!FcAtomicLock (atomic))
799 goto bail3;
800
801 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
802 if (fd == -1)
803 goto bail4;
804
805 /* Temporarily switch magic to MMAP while writing to file */
806 magic = cache->magic;
807 if (magic != FC_CACHE_MAGIC_MMAP)
808 cache->magic = FC_CACHE_MAGIC_MMAP;
809
810 /*
811 * Write cache contents to file
812 */
813 written = write (fd, cache, cache->size);
814
815 /* Switch magic back */
816 if (magic != FC_CACHE_MAGIC_MMAP)
817 cache->magic = magic;
818
819 if (written != cache->size)
820 {
821 perror ("write cache");
822 goto bail5;
823 }
824
825 close(fd);
826 if (!FcAtomicReplaceOrig(atomic))
827 goto bail4;
828 FcStrFree (cache_hashed);
829 FcAtomicUnlock (atomic);
830 FcAtomicDestroy (atomic);
831 return FcTrue;
832
833 bail5:
834 close (fd);
835 bail4:
836 FcAtomicUnlock (atomic);
837 bail3:
838 FcAtomicDestroy (atomic);
839 bail1:
840 FcStrFree (cache_hashed);
841 return FcFalse;
842 }
843
844 /*
845 * Hokey little macro trick to permit the definitions of C functions
846 * with the same name as CPP macros
847 */
848 #define args1(x) (x)
849 #define args2(x,y) (x,y)
850
851 const FcChar8 *
852 FcCacheDir args1(const FcCache *c)
853 {
854 return FcCacheDir (c);
855 }
856
857 FcFontSet *
858 FcCacheCopySet args1(const FcCache *c)
859 {
860 FcFontSet *old = FcCacheSet (c);
861 FcFontSet *new = FcFontSetCreate ();
862 int i;
863
864 if (!new)
865 return NULL;
866 for (i = 0; i < old->nfont; i++)
867 {
868 FcPattern *font = FcFontSetFont (old, i);
869
870 FcPatternReference (font);
871 if (!FcFontSetAdd (new, font))
872 {
873 FcFontSetDestroy (new);
874 return NULL;
875 }
876 }
877 return new;
878 }
879
880 const FcChar8 *
881 FcCacheSubdir args2(const FcCache *c, int i)
882 {
883 return FcCacheSubdir (c, i);
884 }
885
886 int
887 FcCacheNumSubdir args1(const FcCache *c)
888 {
889 return c->dirs_count;
890 }
891
892 int
893 FcCacheNumFont args1(const FcCache *c)
894 {
895 return FcCacheSet(c)->nfont;
896 }
897
898 /*
899 * This code implements the MD5 message-digest algorithm.
900 * The algorithm is due to Ron Rivest. This code was
901 * written by Colin Plumb in 1993, no copyright is claimed.
902 * This code is in the public domain; do with it what you wish.
903 *
904 * Equivalent code is available from RSA Data Security, Inc.
905 * This code has been tested against that, and is equivalent,
906 * except that you don't need to include two pages of legalese
907 * with every copy.
908 *
909 * To compute the message digest of a chunk of bytes, declare an
910 * MD5Context structure, pass it to MD5Init, call MD5Update as
911 * needed on buffers full of bytes, and then call MD5Final, which
912 * will fill a supplied 16-byte array with the digest.
913 */
914
915 #ifndef HIGHFIRST
916 #define byteReverse(buf, len) /* Nothing */
917 #else
918 /*
919 * Note: this code is harmless on little-endian machines.
920 */
921 void byteReverse(unsigned char *buf, unsigned longs)
922 {
923 FcChar32 t;
924 do {
925 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
926 ((unsigned) buf[1] << 8 | buf[0]);
927 *(FcChar32 *) buf = t;
928 buf += 4;
929 } while (--longs);
930 }
931 #endif
932
933 /*
934 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
935 * initialization constants.
936 */
937 static void MD5Init(struct MD5Context *ctx)
938 {
939 ctx->buf[0] = 0x67452301;
940 ctx->buf[1] = 0xefcdab89;
941 ctx->buf[2] = 0x98badcfe;
942 ctx->buf[3] = 0x10325476;
943
944 ctx->bits[0] = 0;
945 ctx->bits[1] = 0;
946 }
947
948 /*
949 * Update context to reflect the concatenation of another buffer full
950 * of bytes.
951 */
952 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len)
953 {
954 FcChar32 t;
955
956 /* Update bitcount */
957
958 t = ctx->bits[0];
959 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
960 ctx->bits[1]++; /* Carry from low to high */
961 ctx->bits[1] += len >> 29;
962
963 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
964
965 /* Handle any leading odd-sized chunks */
966
967 if (t) {
968 unsigned char *p = (unsigned char *) ctx->in + t;
969
970 t = 64 - t;
971 if (len < t) {
972 memcpy(p, buf, len);
973 return;
974 }
975 memcpy(p, buf, t);
976 byteReverse(ctx->in, 16);
977 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
978 buf += t;
979 len -= t;
980 }
981 /* Process data in 64-byte chunks */
982
983 while (len >= 64) {
984 memcpy(ctx->in, buf, 64);
985 byteReverse(ctx->in, 16);
986 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
987 buf += 64;
988 len -= 64;
989 }
990
991 /* Handle any remaining bytes of data. */
992
993 memcpy(ctx->in, buf, len);
994 }
995
996 /*
997 * Final wrapup - pad to 64-byte boundary with the bit pattern
998 * 1 0* (64-bit count of bits processed, MSB-first)
999 */
1000 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1001 {
1002 unsigned count;
1003 unsigned char *p;
1004
1005 /* Compute number of bytes mod 64 */
1006 count = (ctx->bits[0] >> 3) & 0x3F;
1007
1008 /* Set the first char of padding to 0x80. This is safe since there is
1009 always at least one byte free */
1010 p = ctx->in + count;
1011 *p++ = 0x80;
1012
1013 /* Bytes of padding needed to make 64 bytes */
1014 count = 64 - 1 - count;
1015
1016 /* Pad out to 56 mod 64 */
1017 if (count < 8) {
1018 /* Two lots of padding: Pad the first block to 64 bytes */
1019 memset(p, 0, count);
1020 byteReverse(ctx->in, 16);
1021 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1022
1023 /* Now fill the next block with 56 bytes */
1024 memset(ctx->in, 0, 56);
1025 } else {
1026 /* Pad block to 56 bytes */
1027 memset(p, 0, count - 8);
1028 }
1029 byteReverse(ctx->in, 14);
1030
1031 /* Append length in bits and transform */
1032 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1033 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1034
1035 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1036 byteReverse((unsigned char *) ctx->buf, 4);
1037 memcpy(digest, ctx->buf, 16);
1038 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
1039 }
1040
1041
1042 /* The four core functions - F1 is optimized somewhat */
1043
1044 /* #define F1(x, y, z) (x & y | ~x & z) */
1045 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1046 #define F2(x, y, z) F1(z, x, y)
1047 #define F3(x, y, z) (x ^ y ^ z)
1048 #define F4(x, y, z) (y ^ (x | ~z))
1049
1050 /* This is the central step in the MD5 algorithm. */
1051 #define MD5STEP(f, w, x, y, z, data, s) \
1052 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1053
1054 /*
1055 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1056 * reflect the addition of 16 longwords of new data. MD5Update blocks
1057 * the data and converts bytes into longwords for this routine.
1058 */
1059 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1060 {
1061 register FcChar32 a, b, c, d;
1062
1063 a = buf[0];
1064 b = buf[1];
1065 c = buf[2];
1066 d = buf[3];
1067
1068 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1069 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1070 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1071 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1072 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1073 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1074 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1075 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1076 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1077 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1078 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1079 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1080 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1081 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1082 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1083 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1084
1085 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1086 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1087 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1088 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1089 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1090 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1091 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1092 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1093 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1094 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1095 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1096 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1097 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1098 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1099 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1100 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1101
1102 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1103 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1104 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1105 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1106 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1107 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1108 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1109 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1110 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1111 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1112 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1113 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1114 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1115 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1116 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1117 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1118
1119 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1120 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1121 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1122 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1123 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1124 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1125 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1126 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1127 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1128 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1129 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1130 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1131 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1132 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1133 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1134 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1135
1136 buf[0] += a;
1137 buf[1] += b;
1138 buf[2] += c;
1139 buf[3] += d;
1140 }
1141 #define __fccache__
1142 #include "fcaliastail.h"
1143 #undef __fccache__