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>
36 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
38 # include <sys/mman.h>
52 static void MD5Init(struct MD5Context *ctx);
53 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len);
54 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
55 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]);
57 #define CACHEBASE_LEN (1 + 32 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX))
60 FcCacheIsMmapSafe (int fd)
63 MMAP_NOT_INITIALIZED = 0,
68 static void *static_status;
70 status = (intptr_t) fc_atomic_ptr_get (&static_status);
72 if (status == MMAP_NOT_INITIALIZED)
74 const char *env = getenv ("FONTCONFIG_USE_MMAP");
76 if (env && FcNameBool ((const FcChar8 *) env, &use))
77 status = use ? MMAP_USE : MMAP_DONT_USE;
79 status = MMAP_CHECK_FS;
80 (void) fc_atomic_ptr_cmpexch (&static_status, NULL, (void *) status);
83 if (status == MMAP_CHECK_FS)
84 return FcIsFsMmapSafe (fd);
86 return status == MMAP_USE;
90 static const char bin2hex[] = { '0', '1', '2', '3',
96 FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN])
98 unsigned char hash[16];
101 struct MD5Context ctx;
104 MD5Update (&ctx, (const unsigned char *)dir, strlen ((const char *) dir));
106 MD5Final (hash, &ctx);
109 hex_hash = cache_base + 1;
110 for (cnt = 0; cnt < 16; ++cnt)
112 hex_hash[2*cnt ] = bin2hex[hash[cnt] >> 4];
113 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
116 strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX);
122 FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
124 FcChar8 *cache_hashed = NULL;
125 FcChar8 cache_base[CACHEBASE_LEN];
129 FcDirCacheBasename (dir, cache_base);
131 list = FcStrListCreate (config->cacheDirs);
135 while ((cache_dir = FcStrListNext (list)))
137 cache_hashed = FcStrPlus (cache_dir, cache_base);
140 (void) unlink ((char *) cache_hashed);
141 FcStrFree (cache_hashed);
143 FcStrListDone (list);
144 /* return FcFalse if something went wrong */
151 FcDirCacheOpenFile (const FcChar8 *cache_file, struct stat *file_stat)
156 if (FcStat (cache_file, file_stat) < 0)
159 fd = FcOpen((char *) cache_file, O_RDONLY | O_BINARY);
163 if (fstat (fd, file_stat) < 0)
173 * Look for a cache file for the specified dir. Attempt
174 * to use each one we find, stopping when the callback
178 FcDirCacheProcess (FcConfig *config, const FcChar8 *dir,
179 FcBool (*callback) (int fd, struct stat *fd_stat,
180 struct stat *dir_stat, void *closure),
181 void *closure, FcChar8 **cache_file_ret)
184 FcChar8 cache_base[CACHEBASE_LEN];
187 struct stat file_stat, dir_stat;
188 FcBool ret = FcFalse;
190 if (FcStatChecksum (dir, &dir_stat) < 0)
193 FcDirCacheBasename (dir, cache_base);
195 list = FcStrListCreate (config->cacheDirs);
199 while ((cache_dir = FcStrListNext (list)))
201 FcChar8 *cache_hashed = FcStrPlus (cache_dir, cache_base);
204 fd = FcDirCacheOpenFile (cache_hashed, &file_stat);
206 ret = (*callback) (fd, &file_stat, &dir_stat, closure);
211 *cache_file_ret = cache_hashed;
213 FcStrFree (cache_hashed);
217 FcStrFree (cache_hashed);
219 FcStrListDone (list);
224 #define FC_CACHE_MIN_MMAP 1024
227 * Skip list element, make sure the 'next' pointer is the last thing
228 * in the structure, it will be allocated large enough to hold all
229 * of the necessary pointers
232 typedef struct _FcCacheSkip FcCacheSkip;
234 struct _FcCacheSkip {
241 FcCacheSkip *next[1];
245 * The head of the skip list; pointers for every possible level
246 * in the skip list, plus the largest level in the list
249 #define FC_CACHE_MAX_LEVEL 16
251 /* Protected by cache_lock below */
252 static FcCacheSkip *fcCacheChains[FC_CACHE_MAX_LEVEL];
253 static int fcCacheMaxLevel;
262 static struct random_data fcrandbuf;
263 static char statebuf[256];
264 static FcBool initialized = FcFalse;
266 if (initialized != FcTrue)
268 initstate_r(time(NULL), statebuf, 256, &fcrandbuf);
269 initialized = FcTrue;
272 random_r(&fcrandbuf, &result);
274 static char statebuf[256];
276 static FcBool initialized = FcFalse;
278 if (initialized != FcTrue)
280 state = initstate(time(NULL), statebuf, 256);
281 initialized = FcTrue;
284 state = setstate(statebuf);
292 static unsigned int seed = time(NULL);
294 result = rand_r(&seed);
296 static FcBool initialized = FcFalse;
298 if (initialized != FcTrue)
301 initialized = FcTrue;
305 # error no random number generator function available.
312 static FcMutex *cache_lock;
319 lock = fc_atomic_ptr_get (&cache_lock);
321 lock = (FcMutex *) malloc (sizeof (FcMutex));
323 if (!fc_atomic_ptr_cmpexch (&cache_lock, NULL, lock)) {
324 FcMutexFinish (lock);
329 /* Initialize random state */
339 FcMutexUnlock (cache_lock);
346 lock = fc_atomic_ptr_get (&cache_lock);
347 if (lock && fc_atomic_ptr_cmpexch (&cache_lock, lock, NULL)) {
348 FcMutexFinish (lock);
356 * Generate a random level number, distributed
357 * so that each level is 1/4 as likely as the one before
359 * Note that level numbers run 1 <= level <= MAX_LEVEL
364 /* tricky bit -- each bit is '1' 75% of the time */
365 long int bits = FcRandom () | FcRandom ();
368 while (++level < FC_CACHE_MAX_LEVEL)
378 * Insert cache into the list
381 FcCacheInsert (FcCache *cache, struct stat *cache_stat)
383 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
384 FcCacheSkip *s, **next;
390 * Find links along each chain
392 next = fcCacheChains;
393 for (i = fcCacheMaxLevel; --i >= 0; )
395 for (; (s = next[i]); next = s->next)
396 if (s->cache > cache)
398 update[i] = &next[i];
402 * Create new list element
404 level = random_level ();
405 if (level > fcCacheMaxLevel)
407 level = fcCacheMaxLevel + 1;
408 update[fcCacheMaxLevel] = &fcCacheChains[fcCacheMaxLevel];
409 fcCacheMaxLevel = level;
412 s = malloc (sizeof (FcCacheSkip) + (level - 1) * sizeof (FcCacheSkip *));
417 s->size = cache->size;
418 FcRefInit (&s->ref, 1);
421 s->cache_dev = cache_stat->st_dev;
422 s->cache_ino = cache_stat->st_ino;
423 s->cache_mtime = cache_stat->st_mtime;
433 * Insert into all fcCacheChains
435 for (i = 0; i < level; i++)
437 s->next[i] = *update[i];
446 FcCacheFindByAddrUnlocked (void *object)
449 FcCacheSkip **next = fcCacheChains;
456 * Walk chain pointers one level at a time
458 for (i = fcCacheMaxLevel; --i >= 0;)
459 while (next[i] && (char *) object >= ((char *) next[i]->cache + next[i]->size))
460 next = next[i]->next;
465 if (s && (char *) object < ((char *) s->cache + s->size))
471 FcCacheFindByAddr (void *object)
475 ret = FcCacheFindByAddrUnlocked (object);
481 FcCacheRemoveUnlocked (FcCache *cache)
483 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
484 FcCacheSkip *s, **next;
488 * Find links along each chain
490 next = fcCacheChains;
491 for (i = fcCacheMaxLevel; --i >= 0; )
493 for (; (s = next[i]); next = s->next)
494 if (s->cache >= cache)
496 update[i] = &next[i];
499 for (i = 0; i < fcCacheMaxLevel && *update[i] == s; i++)
500 *update[i] = s->next[i];
501 while (fcCacheMaxLevel > 0 && fcCacheChains[fcCacheMaxLevel - 1] == NULL)
507 FcCacheFindByStat (struct stat *cache_stat)
512 for (s = fcCacheChains[0]; s; s = s->next[0])
513 if (s->cache_dev == cache_stat->st_dev &&
514 s->cache_ino == cache_stat->st_ino &&
515 s->cache_mtime == cache_stat->st_mtime)
526 FcDirCacheDisposeUnlocked (FcCache *cache)
528 FcCacheRemoveUnlocked (cache);
530 switch (cache->magic) {
531 case FC_CACHE_MAGIC_ALLOC:
534 case FC_CACHE_MAGIC_MMAP:
535 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
536 munmap (cache, cache->size);
537 #elif defined(_WIN32)
538 UnmapViewOfFile (cache);
545 FcCacheObjectReference (void *object)
547 FcCacheSkip *skip = FcCacheFindByAddr (object);
550 FcRefInc (&skip->ref);
554 FcCacheObjectDereference (void *object)
559 skip = FcCacheFindByAddrUnlocked (object);
562 if (FcRefDec (&skip->ref) == 1)
563 FcDirCacheDisposeUnlocked (skip->cache);
573 for (i = 0; i < FC_CACHE_MAX_LEVEL; i++)
574 assert (fcCacheChains[i] == NULL);
575 assert (fcCacheMaxLevel == 0);
581 FcCacheTimeValid (FcCache *cache, struct stat *dir_stat)
583 struct stat dir_static;
587 if (FcStatChecksum (FcCacheDir (cache), &dir_static) < 0)
589 dir_stat = &dir_static;
591 if (FcDebug () & FC_DBG_CACHE)
592 printf ("FcCacheTimeValid dir \"%s\" cache checksum %d dir checksum %d\n",
593 FcCacheDir (cache), cache->checksum, (int) dir_stat->st_mtime);
594 return cache->checksum == (int) dir_stat->st_mtime;
598 * Map a cache file into memory
601 FcDirCacheMapFd (int fd, struct stat *fd_stat, struct stat *dir_stat)
604 FcBool allocated = FcFalse;
606 if (fd_stat->st_size < (int) sizeof (FcCache))
608 cache = FcCacheFindByStat (fd_stat);
611 if (FcCacheTimeValid (cache, dir_stat))
613 FcDirCacheUnload (cache);
618 * Large cache files are mmap'ed, smaller cache files are read. This
619 * balances the system cost of mmap against per-process memory usage.
621 if (FcCacheIsMmapSafe (fd) && fd_stat->st_size >= FC_CACHE_MIN_MMAP)
623 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
624 cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0);
625 #ifdef HAVE_POSIX_FADVISE
626 posix_fadvise (fd, 0, fd_stat->st_size, POSIX_FADV_WILLNEED);
628 if (cache == MAP_FAILED)
630 #elif defined(_WIN32)
635 hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL,
636 PAGE_READONLY, 0, 0, NULL);
637 if (hFileMap != NULL)
639 cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0,
641 CloseHandle (hFileMap);
648 cache = malloc (fd_stat->st_size);
652 if (read (fd, cache, fd_stat->st_size) != fd_stat->st_size)
659 if (cache->magic != FC_CACHE_MAGIC_MMAP ||
660 cache->version < FC_CACHE_CONTENT_VERSION ||
661 cache->size != (intptr_t) fd_stat->st_size ||
662 !FcCacheTimeValid (cache, dir_stat) ||
663 !FcCacheInsert (cache, fd_stat))
669 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
670 munmap (cache, fd_stat->st_size);
671 #elif defined(_WIN32)
672 UnmapViewOfFile (cache);
678 /* Mark allocated caches so they're freed rather than unmapped */
680 cache->magic = FC_CACHE_MAGIC_ALLOC;
686 FcDirCacheReference (FcCache *cache, int nref)
688 FcCacheSkip *skip = FcCacheFindByAddr (cache);
691 FcRefAdd (&skip->ref, nref);
695 FcDirCacheUnload (FcCache *cache)
697 FcCacheObjectDereference (cache);
701 FcDirCacheMapHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
703 FcCache *cache = FcDirCacheMapFd (fd, fd_stat, dir_stat);
707 *((FcCache **) closure) = cache;
712 FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file)
714 FcCache *cache = NULL;
716 if (!FcDirCacheProcess (config, dir,
724 FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat)
728 struct stat my_file_stat;
731 file_stat = &my_file_stat;
732 fd = FcDirCacheOpenFile (cache_file, file_stat);
735 cache = FcDirCacheMapFd (fd, file_stat, NULL);
741 * Validate a cache file by reading the header and checking
742 * the magic number and the size field
745 FcDirCacheValidateHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure FC_UNUSED)
750 if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache))
752 else if (c.magic != FC_CACHE_MAGIC_MMAP)
754 else if (c.version < FC_CACHE_CONTENT_VERSION)
756 else if (fd_stat->st_size != c.size)
758 else if (c.checksum != (int) dir_stat->st_mtime)
764 FcDirCacheValidConfig (const FcChar8 *dir, FcConfig *config)
766 return FcDirCacheProcess (config, dir,
767 FcDirCacheValidateHelper,
772 FcDirCacheValid (const FcChar8 *dir)
776 config = FcConfigGetCurrent ();
780 return FcDirCacheValidConfig (dir, config);
784 * Build a cache structure from the given contents
787 FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcStrSet *dirs)
789 FcSerialize *serialize = FcSerializeCreate ();
792 FcChar8 *dir_serialize;
793 intptr_t *dirs_serialize;
794 FcFontSet *set_serialize;
799 * Space for cache structure
801 FcSerializeReserve (serialize, sizeof (FcCache));
805 if (!FcStrSerializeAlloc (serialize, dir))
810 FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *));
811 for (i = 0; i < dirs->num; i++)
812 if (!FcStrSerializeAlloc (serialize, dirs->strs[i]))
818 if (!FcFontSetSerializeAlloc (serialize, set))
821 /* Serialize layout complete. Now allocate space and fill it */
822 cache = malloc (serialize->size);
825 /* shut up valgrind */
826 memset (cache, 0, serialize->size);
828 serialize->linear = cache;
830 cache->magic = FC_CACHE_MAGIC_ALLOC;
831 cache->version = FC_CACHE_CONTENT_VERSION;
832 cache->size = serialize->size;
833 cache->checksum = (int) dir_stat->st_mtime;
836 * Serialize directory name
838 dir_serialize = FcStrSerialize (serialize, dir);
841 cache->dir = FcPtrToOffset (cache, dir_serialize);
846 dirs_serialize = FcSerializePtr (serialize, dirs);
849 cache->dirs = FcPtrToOffset (cache, dirs_serialize);
850 cache->dirs_count = dirs->num;
851 for (i = 0; i < dirs->num; i++)
853 FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]);
856 dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize);
862 set_serialize = FcFontSetSerialize (serialize, set);
865 cache->set = FcPtrToOffset (cache, set_serialize);
867 FcSerializeDestroy (serialize);
869 FcCacheInsert (cache, NULL);
876 FcSerializeDestroy (serialize);
883 #define mkdir(path,mode) _mkdir(path)
887 FcMakeDirectory (const FcChar8 *dir)
892 if (strlen ((char *) dir) == 0)
895 parent = FcStrDirname (dir);
898 if (access ((char *) parent, F_OK) == 0)
899 ret = mkdir ((char *) dir, 0755) == 0 && chmod ((char *) dir, 0755) == 0;
900 else if (access ((char *) parent, F_OK) == -1)
901 ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0755) == 0) && chmod ((char *) dir, 0755) == 0;
908 /* write serialized state to the cache file */
910 FcDirCacheWrite (FcCache *cache, FcConfig *config)
912 FcChar8 *dir = FcCacheDir (cache);
913 FcChar8 cache_base[CACHEBASE_LEN];
914 FcChar8 *cache_hashed;
918 FcChar8 *cache_dir = NULL;
921 struct stat cache_stat;
926 * Write it to the first directory in the list which is writable
929 list = FcStrListCreate (config->cacheDirs);
932 while ((test_dir = FcStrListNext (list))) {
933 if (access ((char *) test_dir, W_OK) == 0)
935 cache_dir = test_dir;
941 * If the directory doesn't exist, try to create it
943 if (access ((char *) test_dir, F_OK) == -1) {
944 if (FcMakeDirectory (test_dir))
946 cache_dir = test_dir;
947 /* Create CACHEDIR.TAG */
948 FcDirCacheCreateTagFile (cache_dir);
953 * Otherwise, try making it writable
955 else if (chmod ((char *) test_dir, 0755) == 0)
957 cache_dir = test_dir;
958 /* Try to create CACHEDIR.TAG too */
959 FcDirCacheCreateTagFile (cache_dir);
964 FcStrListDone (list);
968 FcDirCacheBasename (dir, cache_base);
969 cache_hashed = FcStrPlus (cache_dir, cache_base);
973 if (FcDebug () & FC_DBG_CACHE)
974 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
977 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
981 if (!FcAtomicLock (atomic))
984 fd = FcOpen((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
988 /* Temporarily switch magic to MMAP while writing to file */
989 magic = cache->magic;
990 if (magic != FC_CACHE_MAGIC_MMAP)
991 cache->magic = FC_CACHE_MAGIC_MMAP;
994 * Write cache contents to file
996 written = write (fd, cache, cache->size);
998 /* Switch magic back */
999 if (magic != FC_CACHE_MAGIC_MMAP)
1000 cache->magic = magic;
1002 if (written != cache->size)
1004 perror ("write cache");
1009 if (!FcAtomicReplaceOrig(atomic))
1012 /* If the file is small, update the cache chain entry such that the
1013 * new cache file is not read again. If it's large, we don't do that
1014 * such that we reload it, using mmap, which is shared across processes.
1016 if (cache->size < FC_CACHE_MIN_MMAP && FcStat (cache_hashed, &cache_stat))
1019 if ((skip = FcCacheFindByAddrUnlocked (cache)))
1021 skip->cache_dev = cache_stat.st_dev;
1022 skip->cache_ino = cache_stat.st_ino;
1023 skip->cache_mtime = cache_stat.st_mtime;
1028 FcStrFree (cache_hashed);
1029 FcAtomicUnlock (atomic);
1030 FcAtomicDestroy (atomic);
1036 FcAtomicUnlock (atomic);
1038 FcAtomicDestroy (atomic);
1040 FcStrFree (cache_hashed);
1045 FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose)
1050 FcBool ret = FcTrue;
1053 struct stat target_stat;
1055 dir_base = FcStrPlus (cache_dir, (FcChar8 *) FC_DIR_SEPARATOR_S);
1058 fprintf (stderr, "Fontconfig error: %s: out of memory\n", cache_dir);
1061 if (access ((char *) cache_dir, W_OK) != 0)
1063 if (verbose || FcDebug () & FC_DBG_CACHE)
1064 printf ("%s: not cleaning %s cache directory\n", cache_dir,
1065 access ((char *) cache_dir, F_OK) == 0 ? "unwritable" : "non-existent");
1068 if (verbose || FcDebug () & FC_DBG_CACHE)
1069 printf ("%s: cleaning cache directory\n", cache_dir);
1070 d = opendir ((char *) cache_dir);
1073 perror ((char *) cache_dir);
1077 while ((ent = readdir (d)))
1080 const FcChar8 *target_dir;
1082 if (ent->d_name[0] == '.')
1084 /* skip cache files for different architectures and */
1085 /* files which are not cache files at all */
1086 if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) ||
1087 strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX))
1090 file_name = FcStrPlus (dir_base, (FcChar8 *) ent->d_name);
1093 fprintf (stderr, "Fontconfig error: %s: allocation failure\n", cache_dir);
1098 cache = FcDirCacheLoadFile (file_name, NULL);
1101 if (verbose || FcDebug () & FC_DBG_CACHE)
1102 printf ("%s: invalid cache file: %s\n", cache_dir, ent->d_name);
1107 target_dir = FcCacheDir (cache);
1108 if (stat ((char *) target_dir, &target_stat) < 0)
1110 if (verbose || FcDebug () & FC_DBG_CACHE)
1111 printf ("%s: %s: missing directory: %s \n",
1112 cache_dir, ent->d_name, target_dir);
1115 FcDirCacheUnload (cache);
1119 if (unlink ((char *) file_name) < 0)
1121 perror ((char *) file_name);
1125 FcStrFree (file_name);
1130 FcStrFree (dir_base);
1136 * Hokey little macro trick to permit the definitions of C functions
1137 * with the same name as CPP macros
1139 #define args1(x) (x)
1140 #define args2(x,y) (x,y)
1143 FcCacheDir args1(const FcCache *c)
1145 return FcCacheDir (c);
1149 FcCacheCopySet args1(const FcCache *c)
1151 FcFontSet *old = FcCacheSet (c);
1152 FcFontSet *new = FcFontSetCreate ();
1157 for (i = 0; i < old->nfont; i++)
1159 FcPattern *font = FcFontSetFont (old, i);
1161 FcPatternReference (font);
1162 if (!FcFontSetAdd (new, font))
1164 FcFontSetDestroy (new);
1172 FcCacheSubdir args2(const FcCache *c, int i)
1174 return FcCacheSubdir (c, i);
1178 FcCacheNumSubdir args1(const FcCache *c)
1180 return c->dirs_count;
1184 FcCacheNumFont args1(const FcCache *c)
1186 return FcCacheSet(c)->nfont;
1190 * This code implements the MD5 message-digest algorithm.
1191 * The algorithm is due to Ron Rivest. This code was
1192 * written by Colin Plumb in 1993, no copyright is claimed.
1193 * This code is in the public domain; do with it what you wish.
1195 * Equivalent code is available from RSA Data Security, Inc.
1196 * This code has been tested against that, and is equivalent,
1197 * except that you don't need to include two pages of legalese
1200 * To compute the message digest of a chunk of bytes, declare an
1201 * MD5Context structure, pass it to MD5Init, call MD5Update as
1202 * needed on buffers full of bytes, and then call MD5Final, which
1203 * will fill a supplied 16-byte array with the digest.
1207 #define byteReverse(buf, len) /* Nothing */
1210 * Note: this code is harmless on little-endian machines.
1212 void byteReverse(unsigned char *buf, unsigned longs)
1216 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1217 ((unsigned) buf[1] << 8 | buf[0]);
1218 *(FcChar32 *) buf = t;
1225 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1226 * initialization constants.
1228 static void MD5Init(struct MD5Context *ctx)
1230 ctx->buf[0] = 0x67452301;
1231 ctx->buf[1] = 0xefcdab89;
1232 ctx->buf[2] = 0x98badcfe;
1233 ctx->buf[3] = 0x10325476;
1240 * Update context to reflect the concatenation of another buffer full
1243 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len)
1247 /* Update bitcount */
1250 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1251 ctx->bits[1]++; /* Carry from low to high */
1252 ctx->bits[1] += len >> 29;
1254 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1256 /* Handle any leading odd-sized chunks */
1259 unsigned char *p = (unsigned char *) ctx->in + t;
1263 memcpy(p, buf, len);
1267 byteReverse(ctx->in, 16);
1268 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1272 /* Process data in 64-byte chunks */
1275 memcpy(ctx->in, buf, 64);
1276 byteReverse(ctx->in, 16);
1277 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1282 /* Handle any remaining bytes of data. */
1284 memcpy(ctx->in, buf, len);
1288 * Final wrapup - pad to 64-byte boundary with the bit pattern
1289 * 1 0* (64-bit count of bits processed, MSB-first)
1291 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1296 /* Compute number of bytes mod 64 */
1297 count = (ctx->bits[0] >> 3) & 0x3F;
1299 /* Set the first char of padding to 0x80. This is safe since there is
1300 always at least one byte free */
1301 p = ctx->in + count;
1304 /* Bytes of padding needed to make 64 bytes */
1305 count = 64 - 1 - count;
1307 /* Pad out to 56 mod 64 */
1309 /* Two lots of padding: Pad the first block to 64 bytes */
1310 memset(p, 0, count);
1311 byteReverse(ctx->in, 16);
1312 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1314 /* Now fill the next block with 56 bytes */
1315 memset(ctx->in, 0, 56);
1317 /* Pad block to 56 bytes */
1318 memset(p, 0, count - 8);
1320 byteReverse(ctx->in, 14);
1322 /* Append length in bits and transform */
1323 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1324 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1326 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1327 byteReverse((unsigned char *) ctx->buf, 4);
1328 memcpy(digest, ctx->buf, 16);
1329 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
1333 /* The four core functions - F1 is optimized somewhat */
1335 /* #define F1(x, y, z) (x & y | ~x & z) */
1336 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1337 #define F2(x, y, z) F1(z, x, y)
1338 #define F3(x, y, z) (x ^ y ^ z)
1339 #define F4(x, y, z) (y ^ (x | ~z))
1341 /* This is the central step in the MD5 algorithm. */
1342 #define MD5STEP(f, w, x, y, z, data, s) \
1343 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1346 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1347 * reflect the addition of 16 longwords of new data. MD5Update blocks
1348 * the data and converts bytes into longwords for this routine.
1350 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1352 register FcChar32 a, b, c, d;
1359 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1360 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1361 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1362 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1363 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1364 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1365 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1366 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1367 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1368 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1369 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1370 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1371 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1372 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1373 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1374 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1376 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1377 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1378 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1379 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1380 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1381 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1382 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1383 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1384 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1385 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1386 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1387 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1388 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1389 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1390 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1391 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1393 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1394 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1395 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1396 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1397 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1398 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1399 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1400 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1401 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1402 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1403 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1404 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1405 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1406 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1407 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1408 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1410 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1411 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1412 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1413 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1414 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1415 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1416 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1417 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1418 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1419 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1420 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1421 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1422 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1423 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1424 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1425 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1434 FcDirCacheCreateTagFile (const FcChar8 *cache_dir)
1440 static const FcChar8 cache_tag_contents[] =
1441 "Signature: 8a477f597d28d172789f06886806bc55\n"
1442 "# This file is a cache directory tag created by fontconfig.\n"
1443 "# For information about cache directory tags, see:\n"
1444 "# http://www.brynosaurus.com/cachedir/\n";
1445 static size_t cache_tag_contents_size = sizeof (cache_tag_contents) - 1;
1446 FcBool ret = FcFalse;
1451 if (access ((char *) cache_dir, W_OK) == 0)
1453 /* Create CACHEDIR.TAG */
1454 cache_tag = FcStrPlus (cache_dir, (const FcChar8 *) FC_DIR_SEPARATOR_S "CACHEDIR.TAG");
1457 atomic = FcAtomicCreate ((FcChar8 *)cache_tag);
1460 if (!FcAtomicLock (atomic))
1462 fd = FcOpen((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0644);
1465 fp = fdopen(fd, "wb");
1469 fwrite(cache_tag_contents, cache_tag_contents_size, sizeof (FcChar8), fp);
1472 if (!FcAtomicReplaceOrig(atomic))
1477 FcAtomicUnlock (atomic);
1479 FcAtomicDestroy (atomic);
1481 FcStrFree (cache_tag);
1484 if (FcDebug () & FC_DBG_CACHE)
1487 printf ("Created CACHEDIR.TAG at %s\n", cache_dir);
1489 printf ("Unable to create CACHEDIR.TAG at %s\n", cache_dir);
1496 FcCacheCreateTagFile (const FcConfig *config)
1498 FcChar8 *cache_dir = NULL;
1501 list = FcConfigGetCacheDirs (config);
1505 while ((cache_dir = FcStrListNext (list)))
1507 if (FcDirCacheCreateTagFile (cache_dir))
1510 FcStrListDone (list);
1514 #include "fcaliastail.h"