2 * Copyright © 2000 Keith Packard
3 * Copyright © 2005 Patrick Lam
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 the author(s) not be used in
10 * advertising or publicity pertaining to distribution of the software without
11 * specific, written prior permission. The authors make no
12 * representations about the suitability of this software for any purpose. It
13 * is provided "as is" without express or implied warranty.
15 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE AUTHOR(S) 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.
33 #include <sys/types.h>
35 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
37 # include <sys/mman.h>
51 static void MD5Init(struct MD5Context *ctx);
52 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len);
53 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
54 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]);
56 #define CACHEBASE_LEN (1 + 32 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX))
59 FcCacheIsMmapSafe (int fd)
62 MMAP_NOT_INITIALIZED = 0,
67 static void *static_status;
69 status = (intptr_t) fc_atomic_ptr_get (&static_status);
71 if (status == MMAP_NOT_INITIALIZED)
73 const char *env = getenv ("FONTCONFIG_USE_MMAP");
75 if (env && FcNameBool ((const FcChar8 *) env, &use))
76 status = use ? MMAP_USE : MMAP_DONT_USE;
78 status = MMAP_CHECK_FS;
79 (void) fc_atomic_ptr_cmpexch (&static_status, NULL, (void *) status);
82 if (status == MMAP_CHECK_FS)
83 return FcIsFsMmapSafe (fd);
85 return status == MMAP_USE;
89 static const char bin2hex[] = { '0', '1', '2', '3',
95 FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN])
97 unsigned char hash[16];
100 struct MD5Context ctx;
103 MD5Update (&ctx, (const unsigned char *)dir, strlen ((const char *) dir));
105 MD5Final (hash, &ctx);
108 hex_hash = cache_base + 1;
109 for (cnt = 0; cnt < 16; ++cnt)
111 hex_hash[2*cnt ] = bin2hex[hash[cnt] >> 4];
112 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
115 strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX);
121 FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
123 FcChar8 *cache_hashed = NULL;
124 FcChar8 cache_base[CACHEBASE_LEN];
127 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
129 FcDirCacheBasename (dir, cache_base);
131 list = FcStrListCreate (config->cacheDirs);
135 while ((cache_dir = FcStrListNext (list)))
138 cache_hashed = FcStrBuildFilename (sysroot, cache_dir, cache_base, NULL);
140 cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL);
143 (void) unlink ((char *) cache_hashed);
144 FcStrFree (cache_hashed);
146 FcStrListDone (list);
147 /* return FcFalse if something went wrong */
154 FcDirCacheOpenFile (const FcChar8 *cache_file, struct stat *file_stat)
159 if (FcStat (cache_file, file_stat) < 0)
162 fd = FcOpen((char *) cache_file, O_RDONLY | O_BINARY);
166 if (fstat (fd, file_stat) < 0)
176 * Look for a cache file for the specified dir. Attempt
177 * to use each one we find, stopping when the callback
181 FcDirCacheProcess (FcConfig *config, const FcChar8 *dir,
182 FcBool (*callback) (int fd, struct stat *fd_stat,
183 struct stat *dir_stat, void *closure),
184 void *closure, FcChar8 **cache_file_ret)
187 FcChar8 cache_base[CACHEBASE_LEN];
190 struct stat file_stat, dir_stat;
191 FcBool ret = FcFalse;
193 if (FcStatChecksum (dir, &dir_stat) < 0)
196 FcDirCacheBasename (dir, cache_base);
198 list = FcStrListCreate (config->cacheDirs);
202 while ((cache_dir = FcStrListNext (list)))
204 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
205 FcChar8 *cache_hashed;
208 cache_hashed = FcStrBuildFilename (sysroot, cache_dir, cache_base, NULL);
210 cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL);
213 fd = FcDirCacheOpenFile (cache_hashed, &file_stat);
215 ret = (*callback) (fd, &file_stat, &dir_stat, closure);
220 *cache_file_ret = cache_hashed;
222 FcStrFree (cache_hashed);
226 FcStrFree (cache_hashed);
228 FcStrListDone (list);
233 #define FC_CACHE_MIN_MMAP 1024
236 * Skip list element, make sure the 'next' pointer is the last thing
237 * in the structure, it will be allocated large enough to hold all
238 * of the necessary pointers
241 typedef struct _FcCacheSkip FcCacheSkip;
243 struct _FcCacheSkip {
250 FcCacheSkip *next[1];
254 * The head of the skip list; pointers for every possible level
255 * in the skip list, plus the largest level in the list
258 #define FC_CACHE_MAX_LEVEL 16
260 /* Protected by cache_lock below */
261 static FcCacheSkip *fcCacheChains[FC_CACHE_MAX_LEVEL];
262 static int fcCacheMaxLevel;
265 static FcMutex *cache_lock;
272 lock = fc_atomic_ptr_get (&cache_lock);
274 lock = (FcMutex *) malloc (sizeof (FcMutex));
276 if (!fc_atomic_ptr_cmpexch (&cache_lock, NULL, lock)) {
277 FcMutexFinish (lock);
282 /* Initialize random state */
292 FcMutexUnlock (cache_lock);
299 lock = fc_atomic_ptr_get (&cache_lock);
300 if (lock && fc_atomic_ptr_cmpexch (&cache_lock, lock, NULL)) {
301 FcMutexFinish (lock);
309 * Generate a random level number, distributed
310 * so that each level is 1/4 as likely as the one before
312 * Note that level numbers run 1 <= level <= MAX_LEVEL
317 /* tricky bit -- each bit is '1' 75% of the time */
318 long int bits = FcRandom () | FcRandom ();
321 while (++level < FC_CACHE_MAX_LEVEL)
331 * Insert cache into the list
334 FcCacheInsert (FcCache *cache, struct stat *cache_stat)
336 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
337 FcCacheSkip *s, **next;
343 * Find links along each chain
345 next = fcCacheChains;
346 for (i = fcCacheMaxLevel; --i >= 0; )
348 for (; (s = next[i]); next = s->next)
349 if (s->cache > cache)
351 update[i] = &next[i];
355 * Create new list element
357 level = random_level ();
358 if (level > fcCacheMaxLevel)
360 level = fcCacheMaxLevel + 1;
361 update[fcCacheMaxLevel] = &fcCacheChains[fcCacheMaxLevel];
362 fcCacheMaxLevel = level;
365 s = malloc (sizeof (FcCacheSkip) + (level - 1) * sizeof (FcCacheSkip *));
370 s->size = cache->size;
371 FcRefInit (&s->ref, 1);
374 s->cache_dev = cache_stat->st_dev;
375 s->cache_ino = cache_stat->st_ino;
376 s->cache_mtime = cache_stat->st_mtime;
386 * Insert into all fcCacheChains
388 for (i = 0; i < level; i++)
390 s->next[i] = *update[i];
399 FcCacheFindByAddrUnlocked (void *object)
402 FcCacheSkip **next = fcCacheChains;
409 * Walk chain pointers one level at a time
411 for (i = fcCacheMaxLevel; --i >= 0;)
412 while (next[i] && (char *) object >= ((char *) next[i]->cache + next[i]->size))
413 next = next[i]->next;
418 if (s && (char *) object < ((char *) s->cache + s->size))
424 FcCacheFindByAddr (void *object)
428 ret = FcCacheFindByAddrUnlocked (object);
434 FcCacheRemoveUnlocked (FcCache *cache)
436 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
437 FcCacheSkip *s, **next;
441 * Find links along each chain
443 next = fcCacheChains;
444 for (i = fcCacheMaxLevel; --i >= 0; )
446 for (; (s = next[i]); next = s->next)
447 if (s->cache >= cache)
449 update[i] = &next[i];
452 for (i = 0; i < fcCacheMaxLevel && *update[i] == s; i++)
453 *update[i] = s->next[i];
454 while (fcCacheMaxLevel > 0 && fcCacheChains[fcCacheMaxLevel - 1] == NULL)
460 FcCacheFindByStat (struct stat *cache_stat)
465 for (s = fcCacheChains[0]; s; s = s->next[0])
466 if (s->cache_dev == cache_stat->st_dev &&
467 s->cache_ino == cache_stat->st_ino &&
468 s->cache_mtime == cache_stat->st_mtime)
479 FcDirCacheDisposeUnlocked (FcCache *cache)
481 FcCacheRemoveUnlocked (cache);
483 switch (cache->magic) {
484 case FC_CACHE_MAGIC_ALLOC:
487 case FC_CACHE_MAGIC_MMAP:
488 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
489 munmap (cache, cache->size);
490 #elif defined(_WIN32)
491 UnmapViewOfFile (cache);
498 FcCacheObjectReference (void *object)
500 FcCacheSkip *skip = FcCacheFindByAddr (object);
503 FcRefInc (&skip->ref);
507 FcCacheObjectDereference (void *object)
512 skip = FcCacheFindByAddrUnlocked (object);
515 if (FcRefDec (&skip->ref) == 1)
516 FcDirCacheDisposeUnlocked (skip->cache);
526 for (i = 0; i < FC_CACHE_MAX_LEVEL; i++)
527 assert (fcCacheChains[i] == NULL);
528 assert (fcCacheMaxLevel == 0);
534 FcCacheTimeValid (FcCache *cache, struct stat *dir_stat)
536 struct stat dir_static;
540 if (FcStatChecksum (FcCacheDir (cache), &dir_static) < 0)
542 dir_stat = &dir_static;
544 if (FcDebug () & FC_DBG_CACHE)
545 printf ("FcCacheTimeValid dir \"%s\" cache checksum %d dir checksum %d\n",
546 FcCacheDir (cache), cache->checksum, (int) dir_stat->st_mtime);
547 return cache->checksum == (int) dir_stat->st_mtime;
551 * Map a cache file into memory
554 FcDirCacheMapFd (int fd, struct stat *fd_stat, struct stat *dir_stat)
557 FcBool allocated = FcFalse;
559 if (fd_stat->st_size < (int) sizeof (FcCache))
561 cache = FcCacheFindByStat (fd_stat);
564 if (FcCacheTimeValid (cache, dir_stat))
566 FcDirCacheUnload (cache);
571 * Large cache files are mmap'ed, smaller cache files are read. This
572 * balances the system cost of mmap against per-process memory usage.
574 if (FcCacheIsMmapSafe (fd) && fd_stat->st_size >= FC_CACHE_MIN_MMAP)
576 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
577 cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0);
578 #ifdef HAVE_POSIX_FADVISE
579 posix_fadvise (fd, 0, fd_stat->st_size, POSIX_FADV_WILLNEED);
581 if (cache == MAP_FAILED)
583 #elif defined(_WIN32)
588 hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL,
589 PAGE_READONLY, 0, 0, NULL);
590 if (hFileMap != NULL)
592 cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0,
594 CloseHandle (hFileMap);
601 cache = malloc (fd_stat->st_size);
605 if (read (fd, cache, fd_stat->st_size) != fd_stat->st_size)
612 if (cache->magic != FC_CACHE_MAGIC_MMAP ||
613 cache->version < FC_CACHE_CONTENT_VERSION ||
614 cache->size != (intptr_t) fd_stat->st_size ||
615 !FcCacheTimeValid (cache, dir_stat) ||
616 !FcCacheInsert (cache, fd_stat))
622 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
623 munmap (cache, fd_stat->st_size);
624 #elif defined(_WIN32)
625 UnmapViewOfFile (cache);
631 /* Mark allocated caches so they're freed rather than unmapped */
633 cache->magic = FC_CACHE_MAGIC_ALLOC;
639 FcDirCacheReference (FcCache *cache, int nref)
641 FcCacheSkip *skip = FcCacheFindByAddr (cache);
644 FcRefAdd (&skip->ref, nref);
648 FcDirCacheUnload (FcCache *cache)
650 FcCacheObjectDereference (cache);
654 FcDirCacheMapHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
656 FcCache *cache = FcDirCacheMapFd (fd, fd_stat, dir_stat);
660 *((FcCache **) closure) = cache;
665 FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file)
667 FcCache *cache = NULL;
669 if (!FcDirCacheProcess (config, dir,
677 FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat)
681 struct stat my_file_stat;
684 file_stat = &my_file_stat;
685 fd = FcDirCacheOpenFile (cache_file, file_stat);
688 cache = FcDirCacheMapFd (fd, file_stat, NULL);
694 * Validate a cache file by reading the header and checking
695 * the magic number and the size field
698 FcDirCacheValidateHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure FC_UNUSED)
703 if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache))
705 else if (c.magic != FC_CACHE_MAGIC_MMAP)
707 else if (c.version < FC_CACHE_CONTENT_VERSION)
709 else if (fd_stat->st_size != c.size)
711 else if (c.checksum != (int) dir_stat->st_mtime)
717 FcDirCacheValidConfig (const FcChar8 *dir, FcConfig *config)
719 return FcDirCacheProcess (config, dir,
720 FcDirCacheValidateHelper,
725 FcDirCacheValid (const FcChar8 *dir)
729 config = FcConfigGetCurrent ();
733 return FcDirCacheValidConfig (dir, config);
737 * Build a cache structure from the given contents
740 FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcStrSet *dirs)
742 FcSerialize *serialize = FcSerializeCreate ();
745 FcChar8 *dir_serialize;
746 intptr_t *dirs_serialize;
747 FcFontSet *set_serialize;
752 * Space for cache structure
754 FcSerializeReserve (serialize, sizeof (FcCache));
758 if (!FcStrSerializeAlloc (serialize, dir))
763 FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *));
764 for (i = 0; i < dirs->num; i++)
765 if (!FcStrSerializeAlloc (serialize, dirs->strs[i]))
771 if (!FcFontSetSerializeAlloc (serialize, set))
774 /* Serialize layout complete. Now allocate space and fill it */
775 cache = malloc (serialize->size);
778 /* shut up valgrind */
779 memset (cache, 0, serialize->size);
781 serialize->linear = cache;
783 cache->magic = FC_CACHE_MAGIC_ALLOC;
784 cache->version = FC_CACHE_CONTENT_VERSION;
785 cache->size = serialize->size;
786 cache->checksum = (int) dir_stat->st_mtime;
789 * Serialize directory name
791 dir_serialize = FcStrSerialize (serialize, dir);
794 cache->dir = FcPtrToOffset (cache, dir_serialize);
799 dirs_serialize = FcSerializePtr (serialize, dirs);
802 cache->dirs = FcPtrToOffset (cache, dirs_serialize);
803 cache->dirs_count = dirs->num;
804 for (i = 0; i < dirs->num; i++)
806 FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]);
809 dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize);
815 set_serialize = FcFontSetSerialize (serialize, set);
818 cache->set = FcPtrToOffset (cache, set_serialize);
820 FcSerializeDestroy (serialize);
822 FcCacheInsert (cache, NULL);
829 FcSerializeDestroy (serialize);
836 #define mkdir(path,mode) _mkdir(path)
840 FcMakeDirectory (const FcChar8 *dir)
845 if (strlen ((char *) dir) == 0)
848 parent = FcStrDirname (dir);
851 if (access ((char *) parent, F_OK) == 0)
852 ret = mkdir ((char *) dir, 0755) == 0 && chmod ((char *) dir, 0755) == 0;
853 else if (access ((char *) parent, F_OK) == -1)
854 ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0755) == 0) && chmod ((char *) dir, 0755) == 0;
861 /* write serialized state to the cache file */
863 FcDirCacheWrite (FcCache *cache, FcConfig *config)
865 FcChar8 *dir = FcCacheDir (cache);
866 FcChar8 cache_base[CACHEBASE_LEN];
867 FcChar8 *cache_hashed;
871 FcChar8 *cache_dir = NULL;
872 FcChar8 *test_dir, *d = NULL;
874 struct stat cache_stat;
877 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
880 * Write it to the first directory in the list which is writable
883 list = FcStrListCreate (config->cacheDirs);
886 while ((test_dir = FcStrListNext (list)))
891 d = FcStrBuildFilename (sysroot, test_dir, NULL);
893 d = FcStrCopyFilename (test_dir);
895 if (access ((char *) d, W_OK) == 0)
897 cache_dir = FcStrCopyFilename (d);
903 * If the directory doesn't exist, try to create it
905 if (access ((char *) d, F_OK) == -1) {
906 if (FcMakeDirectory (d))
908 cache_dir = FcStrCopyFilename (d);
909 /* Create CACHEDIR.TAG */
910 FcDirCacheCreateTagFile (d);
915 * Otherwise, try making it writable
917 else if (chmod ((char *) d, 0755) == 0)
919 cache_dir = FcStrCopyFilename (d);
920 /* Try to create CACHEDIR.TAG too */
921 FcDirCacheCreateTagFile (d);
928 FcStrListDone (list);
932 FcDirCacheBasename (dir, cache_base);
933 cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL);
936 FcStrFree (cache_dir);
938 if (FcDebug () & FC_DBG_CACHE)
939 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
942 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
946 if (!FcAtomicLock (atomic))
949 fd = FcOpen((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
953 /* Temporarily switch magic to MMAP while writing to file */
954 magic = cache->magic;
955 if (magic != FC_CACHE_MAGIC_MMAP)
956 cache->magic = FC_CACHE_MAGIC_MMAP;
959 * Write cache contents to file
961 written = write (fd, cache, cache->size);
963 /* Switch magic back */
964 if (magic != FC_CACHE_MAGIC_MMAP)
965 cache->magic = magic;
967 if (written != cache->size)
969 perror ("write cache");
974 if (!FcAtomicReplaceOrig(atomic))
977 /* If the file is small, update the cache chain entry such that the
978 * new cache file is not read again. If it's large, we don't do that
979 * such that we reload it, using mmap, which is shared across processes.
981 if (cache->size < FC_CACHE_MIN_MMAP && FcStat (cache_hashed, &cache_stat))
984 if ((skip = FcCacheFindByAddrUnlocked (cache)))
986 skip->cache_dev = cache_stat.st_dev;
987 skip->cache_ino = cache_stat.st_ino;
988 skip->cache_mtime = cache_stat.st_mtime;
993 FcStrFree (cache_hashed);
994 FcAtomicUnlock (atomic);
995 FcAtomicDestroy (atomic);
1001 FcAtomicUnlock (atomic);
1003 FcAtomicDestroy (atomic);
1005 FcStrFree (cache_hashed);
1010 FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose)
1015 FcBool ret = FcTrue;
1018 struct stat target_stat;
1019 const FcChar8 *sysroot;
1021 /* FIXME: this API needs to support non-current FcConfig */
1022 sysroot = FcConfigGetSysRoot (NULL);
1024 dir = FcStrBuildFilename (sysroot, cache_dir, NULL);
1026 dir = FcStrCopyFilename (cache_dir);
1029 fprintf (stderr, "Fontconfig error: %s: out of memory\n", cache_dir);
1032 if (access ((char *) dir, W_OK) != 0)
1034 if (verbose || FcDebug () & FC_DBG_CACHE)
1035 printf ("%s: not cleaning %s cache directory\n", dir,
1036 access ((char *) dir, F_OK) == 0 ? "unwritable" : "non-existent");
1039 if (verbose || FcDebug () & FC_DBG_CACHE)
1040 printf ("%s: cleaning cache directory\n", dir);
1041 d = opendir ((char *) dir);
1044 perror ((char *) dir);
1048 while ((ent = readdir (d)))
1051 const FcChar8 *target_dir;
1053 if (ent->d_name[0] == '.')
1055 /* skip cache files for different architectures and */
1056 /* files which are not cache files at all */
1057 if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) ||
1058 strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX))
1061 file_name = FcStrBuildFilename (dir, (FcChar8 *)ent->d_name, NULL);
1064 fprintf (stderr, "Fontconfig error: %s: allocation failure\n", dir);
1069 cache = FcDirCacheLoadFile (file_name, NULL);
1072 if (verbose || FcDebug () & FC_DBG_CACHE)
1073 printf ("%s: invalid cache file: %s\n", dir, ent->d_name);
1078 target_dir = FcCacheDir (cache);
1079 if (stat ((char *) target_dir, &target_stat) < 0)
1081 if (verbose || FcDebug () & FC_DBG_CACHE)
1082 printf ("%s: %s: missing directory: %s \n",
1083 dir, ent->d_name, target_dir);
1086 FcDirCacheUnload (cache);
1090 if (unlink ((char *) file_name) < 0)
1092 perror ((char *) file_name);
1096 FcStrFree (file_name);
1107 * Hokey little macro trick to permit the definitions of C functions
1108 * with the same name as CPP macros
1110 #define args1(x) (x)
1111 #define args2(x,y) (x,y)
1114 FcCacheDir args1(const FcCache *c)
1116 return FcCacheDir (c);
1120 FcCacheCopySet args1(const FcCache *c)
1122 FcFontSet *old = FcCacheSet (c);
1123 FcFontSet *new = FcFontSetCreate ();
1128 for (i = 0; i < old->nfont; i++)
1130 FcPattern *font = FcFontSetFont (old, i);
1132 FcPatternReference (font);
1133 if (!FcFontSetAdd (new, font))
1135 FcFontSetDestroy (new);
1143 FcCacheSubdir args2(const FcCache *c, int i)
1145 return FcCacheSubdir (c, i);
1149 FcCacheNumSubdir args1(const FcCache *c)
1151 return c->dirs_count;
1155 FcCacheNumFont args1(const FcCache *c)
1157 return FcCacheSet(c)->nfont;
1161 * This code implements the MD5 message-digest algorithm.
1162 * The algorithm is due to Ron Rivest. This code was
1163 * written by Colin Plumb in 1993, no copyright is claimed.
1164 * This code is in the public domain; do with it what you wish.
1166 * Equivalent code is available from RSA Data Security, Inc.
1167 * This code has been tested against that, and is equivalent,
1168 * except that you don't need to include two pages of legalese
1171 * To compute the message digest of a chunk of bytes, declare an
1172 * MD5Context structure, pass it to MD5Init, call MD5Update as
1173 * needed on buffers full of bytes, and then call MD5Final, which
1174 * will fill a supplied 16-byte array with the digest.
1178 #define byteReverse(buf, len) /* Nothing */
1181 * Note: this code is harmless on little-endian machines.
1183 void byteReverse(unsigned char *buf, unsigned longs)
1187 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1188 ((unsigned) buf[1] << 8 | buf[0]);
1189 *(FcChar32 *) buf = t;
1196 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1197 * initialization constants.
1199 static void MD5Init(struct MD5Context *ctx)
1201 ctx->buf[0] = 0x67452301;
1202 ctx->buf[1] = 0xefcdab89;
1203 ctx->buf[2] = 0x98badcfe;
1204 ctx->buf[3] = 0x10325476;
1211 * Update context to reflect the concatenation of another buffer full
1214 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len)
1218 /* Update bitcount */
1221 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1222 ctx->bits[1]++; /* Carry from low to high */
1223 ctx->bits[1] += len >> 29;
1225 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1227 /* Handle any leading odd-sized chunks */
1230 unsigned char *p = (unsigned char *) ctx->in + t;
1234 memcpy(p, buf, len);
1238 byteReverse(ctx->in, 16);
1239 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1243 /* Process data in 64-byte chunks */
1246 memcpy(ctx->in, buf, 64);
1247 byteReverse(ctx->in, 16);
1248 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1253 /* Handle any remaining bytes of data. */
1255 memcpy(ctx->in, buf, len);
1259 * Final wrapup - pad to 64-byte boundary with the bit pattern
1260 * 1 0* (64-bit count of bits processed, MSB-first)
1262 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1267 /* Compute number of bytes mod 64 */
1268 count = (ctx->bits[0] >> 3) & 0x3F;
1270 /* Set the first char of padding to 0x80. This is safe since there is
1271 always at least one byte free */
1272 p = ctx->in + count;
1275 /* Bytes of padding needed to make 64 bytes */
1276 count = 64 - 1 - count;
1278 /* Pad out to 56 mod 64 */
1280 /* Two lots of padding: Pad the first block to 64 bytes */
1281 memset(p, 0, count);
1282 byteReverse(ctx->in, 16);
1283 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1285 /* Now fill the next block with 56 bytes */
1286 memset(ctx->in, 0, 56);
1288 /* Pad block to 56 bytes */
1289 memset(p, 0, count - 8);
1291 byteReverse(ctx->in, 14);
1293 /* Append length in bits and transform */
1294 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1295 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1297 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1298 byteReverse((unsigned char *) ctx->buf, 4);
1299 memcpy(digest, ctx->buf, 16);
1300 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
1304 /* The four core functions - F1 is optimized somewhat */
1306 /* #define F1(x, y, z) (x & y | ~x & z) */
1307 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1308 #define F2(x, y, z) F1(z, x, y)
1309 #define F3(x, y, z) (x ^ y ^ z)
1310 #define F4(x, y, z) (y ^ (x | ~z))
1312 /* This is the central step in the MD5 algorithm. */
1313 #define MD5STEP(f, w, x, y, z, data, s) \
1314 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1317 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1318 * reflect the addition of 16 longwords of new data. MD5Update blocks
1319 * the data and converts bytes into longwords for this routine.
1321 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1323 register FcChar32 a, b, c, d;
1330 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1331 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1332 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1333 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1334 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1335 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1336 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1337 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1338 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1339 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1340 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1341 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1342 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1343 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1344 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1345 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1347 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1348 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1349 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1350 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1351 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1352 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1353 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1354 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1355 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1356 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1357 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1358 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1359 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1360 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1361 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1362 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1364 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1365 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1366 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1367 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1368 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1369 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1370 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1371 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1372 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1373 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1374 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1375 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1376 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1377 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1378 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1379 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1381 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1382 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1383 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1384 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1385 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1386 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1387 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1388 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1389 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1390 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1391 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1392 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1393 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1394 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1395 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1396 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1405 FcDirCacheCreateTagFile (const FcChar8 *cache_dir)
1411 static const FcChar8 cache_tag_contents[] =
1412 "Signature: 8a477f597d28d172789f06886806bc55\n"
1413 "# This file is a cache directory tag created by fontconfig.\n"
1414 "# For information about cache directory tags, see:\n"
1415 "# http://www.brynosaurus.com/cachedir/\n";
1416 static size_t cache_tag_contents_size = sizeof (cache_tag_contents) - 1;
1417 FcBool ret = FcFalse;
1422 if (access ((char *) cache_dir, W_OK) == 0)
1424 /* Create CACHEDIR.TAG */
1425 cache_tag = FcStrBuildFilename (cache_dir, "CACHEDIR.TAG", NULL);
1428 atomic = FcAtomicCreate ((FcChar8 *)cache_tag);
1431 if (!FcAtomicLock (atomic))
1433 fd = FcOpen((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0644);
1436 fp = fdopen(fd, "wb");
1440 fwrite(cache_tag_contents, cache_tag_contents_size, sizeof (FcChar8), fp);
1443 if (!FcAtomicReplaceOrig(atomic))
1448 FcAtomicUnlock (atomic);
1450 FcAtomicDestroy (atomic);
1452 FcStrFree (cache_tag);
1455 if (FcDebug () & FC_DBG_CACHE)
1458 printf ("Created CACHEDIR.TAG at %s\n", cache_dir);
1460 printf ("Unable to create CACHEDIR.TAG at %s\n", cache_dir);
1467 FcCacheCreateTagFile (const FcConfig *config)
1469 FcChar8 *cache_dir = NULL, *d = NULL;
1471 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
1473 list = FcConfigGetCacheDirs (config);
1477 while ((cache_dir = FcStrListNext (list)))
1482 d = FcStrBuildFilename (sysroot, cache_dir, NULL);
1484 d = FcStrCopyFilename (cache_dir);
1485 if (FcDirCacheCreateTagFile (d))
1490 FcStrListDone (list);
1494 #include "fcaliastail.h"