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