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;
453 * Walk chain pointers one level at a time
455 for (i = fcCacheMaxLevel; --i >= 0;)
456 while (next[i] && (char *) object >= ((char *) next[i]->cache + next[i]->size))
457 next = next[i]->next;
462 if (s && (char *) object < ((char *) s->cache + s->size))
468 FcCacheFindByAddr (void *object)
472 ret = FcCacheFindByAddrUnlocked (object);
478 FcCacheRemoveUnlocked (FcCache *cache)
480 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
481 FcCacheSkip *s, **next;
485 * Find links along each chain
487 next = fcCacheChains;
488 for (i = fcCacheMaxLevel; --i >= 0; )
490 for (; (s = next[i]); next = s->next)
491 if (s->cache >= cache)
493 update[i] = &next[i];
496 for (i = 0; i < fcCacheMaxLevel && *update[i] == s; i++)
497 *update[i] = s->next[i];
498 while (fcCacheMaxLevel > 0 && fcCacheChains[fcCacheMaxLevel - 1] == NULL)
504 FcCacheFindByStat (struct stat *cache_stat)
509 for (s = fcCacheChains[0]; s; s = s->next[0])
510 if (s->cache_dev == cache_stat->st_dev &&
511 s->cache_ino == cache_stat->st_ino &&
512 s->cache_mtime == cache_stat->st_mtime)
523 FcDirCacheDisposeUnlocked (FcCache *cache)
525 FcCacheRemoveUnlocked (cache);
527 switch (cache->magic) {
528 case FC_CACHE_MAGIC_ALLOC:
531 case FC_CACHE_MAGIC_MMAP:
532 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
533 munmap (cache, cache->size);
534 #elif defined(_WIN32)
535 UnmapViewOfFile (cache);
542 FcCacheObjectReference (void *object)
544 FcCacheSkip *skip = FcCacheFindByAddr (object);
547 FcRefInc (&skip->ref);
551 FcCacheObjectDereference (void *object)
556 skip = FcCacheFindByAddrUnlocked (object);
559 if (FcRefDec (&skip->ref) <= 1)
560 FcDirCacheDisposeUnlocked (skip->cache);
570 for (i = 0; i < FC_CACHE_MAX_LEVEL; i++)
571 assert (fcCacheChains[i] == NULL);
572 assert (fcCacheMaxLevel == 0);
578 FcCacheTimeValid (FcCache *cache, struct stat *dir_stat)
580 struct stat dir_static;
584 if (FcStatChecksum (FcCacheDir (cache), &dir_static) < 0)
586 dir_stat = &dir_static;
588 if (FcDebug () & FC_DBG_CACHE)
589 printf ("FcCacheTimeValid dir \"%s\" cache checksum %d dir checksum %d\n",
590 FcCacheDir (cache), cache->checksum, (int) dir_stat->st_mtime);
591 return cache->checksum == (int) dir_stat->st_mtime;
595 * Map a cache file into memory
598 FcDirCacheMapFd (int fd, struct stat *fd_stat, struct stat *dir_stat)
601 FcBool allocated = FcFalse;
603 if (fd_stat->st_size < (int) sizeof (FcCache))
605 cache = FcCacheFindByStat (fd_stat);
608 if (FcCacheTimeValid (cache, dir_stat))
610 FcDirCacheUnload (cache);
615 * Large cache files are mmap'ed, smaller cache files are read. This
616 * balances the system cost of mmap against per-process memory usage.
618 if (FcCacheIsMmapSafe (fd) && fd_stat->st_size >= FC_CACHE_MIN_MMAP)
620 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
621 cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0);
622 #ifdef HAVE_POSIX_FADVISE
623 posix_fadvise (fd, 0, fd_stat->st_size, POSIX_FADV_WILLNEED);
625 if (cache == MAP_FAILED)
627 #elif defined(_WIN32)
632 hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL,
633 PAGE_READONLY, 0, 0, NULL);
634 if (hFileMap != NULL)
636 cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0,
638 CloseHandle (hFileMap);
645 cache = malloc (fd_stat->st_size);
649 if (read (fd, cache, fd_stat->st_size) != fd_stat->st_size)
656 if (cache->magic != FC_CACHE_MAGIC_MMAP ||
657 cache->version < FC_CACHE_CONTENT_VERSION ||
658 cache->size != (intptr_t) fd_stat->st_size ||
659 !FcCacheTimeValid (cache, dir_stat) ||
660 !FcCacheInsert (cache, fd_stat))
666 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
667 munmap (cache, fd_stat->st_size);
668 #elif defined(_WIN32)
669 UnmapViewOfFile (cache);
675 /* Mark allocated caches so they're freed rather than unmapped */
677 cache->magic = FC_CACHE_MAGIC_ALLOC;
683 FcDirCacheReference (FcCache *cache, int nref)
685 FcCacheSkip *skip = FcCacheFindByAddr (cache);
688 FcRefAdd (&skip->ref, nref);
692 FcDirCacheUnload (FcCache *cache)
694 FcCacheObjectDereference (cache);
698 FcDirCacheMapHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
700 FcCache *cache = FcDirCacheMapFd (fd, fd_stat, dir_stat);
704 *((FcCache **) closure) = cache;
709 FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file)
711 FcCache *cache = NULL;
713 if (!FcDirCacheProcess (config, dir,
721 FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat)
725 struct stat my_file_stat;
728 file_stat = &my_file_stat;
729 fd = FcDirCacheOpenFile (cache_file, file_stat);
732 cache = FcDirCacheMapFd (fd, file_stat, NULL);
738 * Validate a cache file by reading the header and checking
739 * the magic number and the size field
742 FcDirCacheValidateHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure FC_UNUSED)
747 if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache))
749 else if (c.magic != FC_CACHE_MAGIC_MMAP)
751 else if (c.version < FC_CACHE_CONTENT_VERSION)
753 else if (fd_stat->st_size != c.size)
755 else if (c.checksum != (int) dir_stat->st_mtime)
761 FcDirCacheValidConfig (const FcChar8 *dir, FcConfig *config)
763 return FcDirCacheProcess (config, dir,
764 FcDirCacheValidateHelper,
769 FcDirCacheValid (const FcChar8 *dir)
773 config = FcConfigGetCurrent ();
777 return FcDirCacheValidConfig (dir, config);
781 * Build a cache structure from the given contents
784 FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcStrSet *dirs)
786 FcSerialize *serialize = FcSerializeCreate ();
789 FcChar8 *dir_serialize;
790 intptr_t *dirs_serialize;
791 FcFontSet *set_serialize;
796 * Space for cache structure
798 FcSerializeReserve (serialize, sizeof (FcCache));
802 if (!FcStrSerializeAlloc (serialize, dir))
807 FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *));
808 for (i = 0; i < dirs->num; i++)
809 if (!FcStrSerializeAlloc (serialize, dirs->strs[i]))
815 if (!FcFontSetSerializeAlloc (serialize, set))
818 /* Serialize layout complete. Now allocate space and fill it */
819 cache = malloc (serialize->size);
822 /* shut up valgrind */
823 memset (cache, 0, serialize->size);
825 serialize->linear = cache;
827 cache->magic = FC_CACHE_MAGIC_ALLOC;
828 cache->version = FC_CACHE_CONTENT_VERSION;
829 cache->size = serialize->size;
830 cache->checksum = (int) dir_stat->st_mtime;
833 * Serialize directory name
835 dir_serialize = FcStrSerialize (serialize, dir);
838 cache->dir = FcPtrToOffset (cache, dir_serialize);
843 dirs_serialize = FcSerializePtr (serialize, dirs);
846 cache->dirs = FcPtrToOffset (cache, dirs_serialize);
847 cache->dirs_count = dirs->num;
848 for (i = 0; i < dirs->num; i++)
850 FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]);
853 dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize);
859 set_serialize = FcFontSetSerialize (serialize, set);
862 cache->set = FcPtrToOffset (cache, set_serialize);
864 FcSerializeDestroy (serialize);
866 FcCacheInsert (cache, NULL);
873 FcSerializeDestroy (serialize);
880 #define mkdir(path,mode) _mkdir(path)
884 FcMakeDirectory (const FcChar8 *dir)
889 if (strlen ((char *) dir) == 0)
892 parent = FcStrDirname (dir);
895 if (access ((char *) parent, F_OK) == 0)
896 ret = mkdir ((char *) dir, 0755) == 0 && chmod ((char *) dir, 0755) == 0;
897 else if (access ((char *) parent, F_OK) == -1)
898 ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0755) == 0) && chmod ((char *) dir, 0755) == 0;
905 /* write serialized state to the cache file */
907 FcDirCacheWrite (FcCache *cache, FcConfig *config)
909 FcChar8 *dir = FcCacheDir (cache);
910 FcChar8 cache_base[CACHEBASE_LEN];
911 FcChar8 *cache_hashed;
915 FcChar8 *cache_dir = NULL;
918 struct stat cache_stat;
923 * Write it to the first directory in the list which is writable
926 list = FcStrListCreate (config->cacheDirs);
929 while ((test_dir = FcStrListNext (list))) {
930 if (access ((char *) test_dir, W_OK) == 0)
932 cache_dir = test_dir;
938 * If the directory doesn't exist, try to create it
940 if (access ((char *) test_dir, F_OK) == -1) {
941 if (FcMakeDirectory (test_dir))
943 cache_dir = test_dir;
944 /* Create CACHEDIR.TAG */
945 FcDirCacheCreateTagFile (cache_dir);
950 * Otherwise, try making it writable
952 else if (chmod ((char *) test_dir, 0755) == 0)
954 cache_dir = test_dir;
955 /* Try to create CACHEDIR.TAG too */
956 FcDirCacheCreateTagFile (cache_dir);
961 FcStrListDone (list);
965 FcDirCacheBasename (dir, cache_base);
966 cache_hashed = FcStrPlus (cache_dir, cache_base);
970 if (FcDebug () & FC_DBG_CACHE)
971 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
974 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
978 if (!FcAtomicLock (atomic))
981 fd = FcOpen((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
985 /* Temporarily switch magic to MMAP while writing to file */
986 magic = cache->magic;
987 if (magic != FC_CACHE_MAGIC_MMAP)
988 cache->magic = FC_CACHE_MAGIC_MMAP;
991 * Write cache contents to file
993 written = write (fd, cache, cache->size);
995 /* Switch magic back */
996 if (magic != FC_CACHE_MAGIC_MMAP)
997 cache->magic = magic;
999 if (written != cache->size)
1001 perror ("write cache");
1006 if (!FcAtomicReplaceOrig(atomic))
1009 /* If the file is small, update the cache chain entry such that the
1010 * new cache file is not read again. If it's large, we don't do that
1011 * such that we reload it, using mmap, which is shared across processes.
1013 if (cache->size < FC_CACHE_MIN_MMAP && FcStat (cache_hashed, &cache_stat))
1016 if ((skip = FcCacheFindByAddrUnlocked (cache)))
1018 skip->cache_dev = cache_stat.st_dev;
1019 skip->cache_ino = cache_stat.st_ino;
1020 skip->cache_mtime = cache_stat.st_mtime;
1025 FcStrFree (cache_hashed);
1026 FcAtomicUnlock (atomic);
1027 FcAtomicDestroy (atomic);
1033 FcAtomicUnlock (atomic);
1035 FcAtomicDestroy (atomic);
1037 FcStrFree (cache_hashed);
1042 FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose)
1047 FcBool ret = FcTrue;
1050 struct stat target_stat;
1052 dir_base = FcStrPlus (cache_dir, (FcChar8 *) FC_DIR_SEPARATOR_S);
1055 fprintf (stderr, "Fontconfig error: %s: out of memory\n", cache_dir);
1058 if (access ((char *) cache_dir, W_OK) != 0)
1060 if (verbose || FcDebug () & FC_DBG_CACHE)
1061 printf ("%s: not cleaning %s cache directory\n", cache_dir,
1062 access ((char *) cache_dir, F_OK) == 0 ? "unwritable" : "non-existent");
1065 if (verbose || FcDebug () & FC_DBG_CACHE)
1066 printf ("%s: cleaning cache directory\n", cache_dir);
1067 d = opendir ((char *) cache_dir);
1070 perror ((char *) cache_dir);
1074 while ((ent = readdir (d)))
1077 const FcChar8 *target_dir;
1079 if (ent->d_name[0] == '.')
1081 /* skip cache files for different architectures and */
1082 /* files which are not cache files at all */
1083 if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) ||
1084 strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX))
1087 file_name = FcStrPlus (dir_base, (FcChar8 *) ent->d_name);
1090 fprintf (stderr, "Fontconfig error: %s: allocation failure\n", cache_dir);
1095 cache = FcDirCacheLoadFile (file_name, NULL);
1098 if (verbose || FcDebug () & FC_DBG_CACHE)
1099 printf ("%s: invalid cache file: %s\n", cache_dir, ent->d_name);
1104 target_dir = FcCacheDir (cache);
1105 if (stat ((char *) target_dir, &target_stat) < 0)
1107 if (verbose || FcDebug () & FC_DBG_CACHE)
1108 printf ("%s: %s: missing directory: %s \n",
1109 cache_dir, ent->d_name, target_dir);
1115 if (unlink ((char *) file_name) < 0)
1117 perror ((char *) file_name);
1121 FcDirCacheUnload (cache);
1122 FcStrFree (file_name);
1127 FcStrFree (dir_base);
1133 * Hokey little macro trick to permit the definitions of C functions
1134 * with the same name as CPP macros
1136 #define args1(x) (x)
1137 #define args2(x,y) (x,y)
1140 FcCacheDir args1(const FcCache *c)
1142 return FcCacheDir (c);
1146 FcCacheCopySet args1(const FcCache *c)
1148 FcFontSet *old = FcCacheSet (c);
1149 FcFontSet *new = FcFontSetCreate ();
1154 for (i = 0; i < old->nfont; i++)
1156 FcPattern *font = FcFontSetFont (old, i);
1158 FcPatternReference (font);
1159 if (!FcFontSetAdd (new, font))
1161 FcFontSetDestroy (new);
1169 FcCacheSubdir args2(const FcCache *c, int i)
1171 return FcCacheSubdir (c, i);
1175 FcCacheNumSubdir args1(const FcCache *c)
1177 return c->dirs_count;
1181 FcCacheNumFont args1(const FcCache *c)
1183 return FcCacheSet(c)->nfont;
1187 * This code implements the MD5 message-digest algorithm.
1188 * The algorithm is due to Ron Rivest. This code was
1189 * written by Colin Plumb in 1993, no copyright is claimed.
1190 * This code is in the public domain; do with it what you wish.
1192 * Equivalent code is available from RSA Data Security, Inc.
1193 * This code has been tested against that, and is equivalent,
1194 * except that you don't need to include two pages of legalese
1197 * To compute the message digest of a chunk of bytes, declare an
1198 * MD5Context structure, pass it to MD5Init, call MD5Update as
1199 * needed on buffers full of bytes, and then call MD5Final, which
1200 * will fill a supplied 16-byte array with the digest.
1204 #define byteReverse(buf, len) /* Nothing */
1207 * Note: this code is harmless on little-endian machines.
1209 void byteReverse(unsigned char *buf, unsigned longs)
1213 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1214 ((unsigned) buf[1] << 8 | buf[0]);
1215 *(FcChar32 *) buf = t;
1222 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1223 * initialization constants.
1225 static void MD5Init(struct MD5Context *ctx)
1227 ctx->buf[0] = 0x67452301;
1228 ctx->buf[1] = 0xefcdab89;
1229 ctx->buf[2] = 0x98badcfe;
1230 ctx->buf[3] = 0x10325476;
1237 * Update context to reflect the concatenation of another buffer full
1240 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len)
1244 /* Update bitcount */
1247 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1248 ctx->bits[1]++; /* Carry from low to high */
1249 ctx->bits[1] += len >> 29;
1251 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1253 /* Handle any leading odd-sized chunks */
1256 unsigned char *p = (unsigned char *) ctx->in + t;
1260 memcpy(p, buf, len);
1264 byteReverse(ctx->in, 16);
1265 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1269 /* Process data in 64-byte chunks */
1272 memcpy(ctx->in, buf, 64);
1273 byteReverse(ctx->in, 16);
1274 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1279 /* Handle any remaining bytes of data. */
1281 memcpy(ctx->in, buf, len);
1285 * Final wrapup - pad to 64-byte boundary with the bit pattern
1286 * 1 0* (64-bit count of bits processed, MSB-first)
1288 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1293 /* Compute number of bytes mod 64 */
1294 count = (ctx->bits[0] >> 3) & 0x3F;
1296 /* Set the first char of padding to 0x80. This is safe since there is
1297 always at least one byte free */
1298 p = ctx->in + count;
1301 /* Bytes of padding needed to make 64 bytes */
1302 count = 64 - 1 - count;
1304 /* Pad out to 56 mod 64 */
1306 /* Two lots of padding: Pad the first block to 64 bytes */
1307 memset(p, 0, count);
1308 byteReverse(ctx->in, 16);
1309 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1311 /* Now fill the next block with 56 bytes */
1312 memset(ctx->in, 0, 56);
1314 /* Pad block to 56 bytes */
1315 memset(p, 0, count - 8);
1317 byteReverse(ctx->in, 14);
1319 /* Append length in bits and transform */
1320 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1321 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1323 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1324 byteReverse((unsigned char *) ctx->buf, 4);
1325 memcpy(digest, ctx->buf, 16);
1326 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
1330 /* The four core functions - F1 is optimized somewhat */
1332 /* #define F1(x, y, z) (x & y | ~x & z) */
1333 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1334 #define F2(x, y, z) F1(z, x, y)
1335 #define F3(x, y, z) (x ^ y ^ z)
1336 #define F4(x, y, z) (y ^ (x | ~z))
1338 /* This is the central step in the MD5 algorithm. */
1339 #define MD5STEP(f, w, x, y, z, data, s) \
1340 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1343 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1344 * reflect the addition of 16 longwords of new data. MD5Update blocks
1345 * the data and converts bytes into longwords for this routine.
1347 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1349 register FcChar32 a, b, c, d;
1356 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1357 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1358 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1359 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1360 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1361 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1362 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1363 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1364 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1365 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1366 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1367 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1368 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1369 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1370 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1371 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1373 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1374 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1375 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1376 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1377 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1378 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1379 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1380 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1381 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1382 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1383 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1384 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1385 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1386 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1387 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1388 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1390 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1391 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1392 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1393 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1394 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1395 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1396 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1397 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1398 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1399 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1400 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1401 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1402 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1403 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1404 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1405 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1407 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1408 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1409 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1410 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1411 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1412 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1413 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1414 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1415 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1416 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1417 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1418 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1419 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1420 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1421 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1422 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1431 FcDirCacheCreateTagFile (const FcChar8 *cache_dir)
1437 static const FcChar8 cache_tag_contents[] =
1438 "Signature: 8a477f597d28d172789f06886806bc55\n"
1439 "# This file is a cache directory tag created by fontconfig.\n"
1440 "# For information about cache directory tags, see:\n"
1441 "# http://www.brynosaurus.com/cachedir/\n";
1442 static size_t cache_tag_contents_size = sizeof (cache_tag_contents) - 1;
1443 FcBool ret = FcFalse;
1448 if (access ((char *) cache_dir, W_OK) == 0)
1450 /* Create CACHEDIR.TAG */
1451 cache_tag = FcStrPlus (cache_dir, (const FcChar8 *) FC_DIR_SEPARATOR_S "CACHEDIR.TAG");
1454 atomic = FcAtomicCreate ((FcChar8 *)cache_tag);
1457 if (!FcAtomicLock (atomic))
1459 fd = FcOpen((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0644);
1462 fp = fdopen(fd, "wb");
1466 fwrite(cache_tag_contents, cache_tag_contents_size, sizeof (FcChar8), fp);
1469 if (!FcAtomicReplaceOrig(atomic))
1474 FcAtomicUnlock (atomic);
1476 FcAtomicDestroy (atomic);
1478 FcStrFree (cache_tag);
1481 if (FcDebug () & FC_DBG_CACHE)
1484 printf ("Created CACHEDIR.TAG at %s\n", cache_dir);
1486 printf ("Unable to create CACHEDIR.TAG at %s\n", cache_dir);
1493 FcCacheCreateTagFile (const FcConfig *config)
1495 FcChar8 *cache_dir = NULL;
1498 list = FcConfigGetCacheDirs (config);
1502 while ((cache_dir = FcStrListNext (list)))
1504 if (FcDirCacheCreateTagFile (cache_dir))
1507 FcStrListDone (list);
1511 #include "fcaliastail.h"