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 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 = open((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);
879 #define mkdir(path,mode) _mkdir(path)
883 FcMakeDirectory (const FcChar8 *dir)
888 if (strlen ((char *) dir) == 0)
891 parent = FcStrDirname (dir);
894 if (access ((char *) parent, F_OK) == 0)
895 ret = mkdir ((char *) dir, 0755) == 0 && chmod ((char *) dir, 0755) == 0;
896 else if (access ((char *) parent, F_OK) == -1)
897 ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0755) == 0) && chmod ((char *) dir, 0755) == 0;
904 /* write serialized state to the cache file */
906 FcDirCacheWrite (FcCache *cache, FcConfig *config)
908 FcChar8 *dir = FcCacheDir (cache);
909 FcChar8 cache_base[CACHEBASE_LEN];
910 FcChar8 *cache_hashed;
914 FcChar8 *cache_dir = NULL;
917 struct stat cache_stat;
922 * Write it to the first directory in the list which is writable
925 list = FcStrListCreate (config->cacheDirs);
928 while ((test_dir = FcStrListNext (list))) {
929 if (access ((char *) test_dir, W_OK) == 0)
931 cache_dir = test_dir;
937 * If the directory doesn't exist, try to create it
939 if (access ((char *) test_dir, F_OK) == -1) {
940 if (FcMakeDirectory (test_dir))
942 cache_dir = test_dir;
943 /* Create CACHEDIR.TAG */
944 FcDirCacheCreateTagFile (cache_dir);
949 * Otherwise, try making it writable
951 else if (chmod ((char *) test_dir, 0755) == 0)
953 cache_dir = test_dir;
954 /* Try to create CACHEDIR.TAG too */
955 FcDirCacheCreateTagFile (cache_dir);
960 FcStrListDone (list);
964 FcDirCacheBasename (dir, cache_base);
965 cache_hashed = FcStrPlus (cache_dir, cache_base);
969 if (FcDebug () & FC_DBG_CACHE)
970 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
973 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
977 if (!FcAtomicLock (atomic))
980 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
984 /* Temporarily switch magic to MMAP while writing to file */
985 magic = cache->magic;
986 if (magic != FC_CACHE_MAGIC_MMAP)
987 cache->magic = FC_CACHE_MAGIC_MMAP;
990 * Write cache contents to file
992 written = write (fd, cache, cache->size);
994 /* Switch magic back */
995 if (magic != FC_CACHE_MAGIC_MMAP)
996 cache->magic = magic;
998 if (written != cache->size)
1000 perror ("write cache");
1005 if (!FcAtomicReplaceOrig(atomic))
1008 /* If the file is small, update the cache chain entry such that the
1009 * new cache file is not read again. If it's large, we don't do that
1010 * such that we reload it, using mmap, which is shared across processes.
1012 if (cache->size < FC_CACHE_MIN_MMAP && FcStat (cache_hashed, &cache_stat))
1015 if ((skip = FcCacheFindByAddrUnlocked (cache)))
1017 skip->cache_dev = cache_stat.st_dev;
1018 skip->cache_ino = cache_stat.st_ino;
1019 skip->cache_mtime = cache_stat.st_mtime;
1024 FcStrFree (cache_hashed);
1025 FcAtomicUnlock (atomic);
1026 FcAtomicDestroy (atomic);
1032 FcAtomicUnlock (atomic);
1034 FcAtomicDestroy (atomic);
1036 FcStrFree (cache_hashed);
1041 FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose)
1046 FcBool ret = FcTrue;
1049 struct stat target_stat;
1051 dir_base = FcStrPlus (cache_dir, (FcChar8 *) FC_DIR_SEPARATOR_S);
1054 fprintf (stderr, "Fontconfig error: %s: out of memory\n", cache_dir);
1057 if (access ((char *) cache_dir, W_OK) != 0)
1059 if (verbose || FcDebug () & FC_DBG_CACHE)
1060 printf ("%s: not cleaning %s cache directory\n", cache_dir,
1061 access ((char *) cache_dir, F_OK) == 0 ? "unwritable" : "non-existent");
1064 if (verbose || FcDebug () & FC_DBG_CACHE)
1065 printf ("%s: cleaning cache directory\n", cache_dir);
1066 d = opendir ((char *) cache_dir);
1069 perror ((char *) cache_dir);
1073 while ((ent = readdir (d)))
1076 const FcChar8 *target_dir;
1078 if (ent->d_name[0] == '.')
1080 /* skip cache files for different architectures and */
1081 /* files which are not cache files at all */
1082 if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) ||
1083 strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX))
1086 file_name = FcStrPlus (dir_base, (FcChar8 *) ent->d_name);
1089 fprintf (stderr, "Fontconfig error: %s: allocation failure\n", cache_dir);
1094 cache = FcDirCacheLoadFile (file_name, NULL);
1097 if (verbose || FcDebug () & FC_DBG_CACHE)
1098 printf ("%s: invalid cache file: %s\n", cache_dir, ent->d_name);
1103 target_dir = FcCacheDir (cache);
1104 if (stat ((char *) target_dir, &target_stat) < 0)
1106 if (verbose || FcDebug () & FC_DBG_CACHE)
1107 printf ("%s: %s: missing directory: %s \n",
1108 cache_dir, ent->d_name, target_dir);
1114 if (unlink ((char *) file_name) < 0)
1116 perror ((char *) file_name);
1120 FcDirCacheUnload (cache);
1121 FcStrFree (file_name);
1126 FcStrFree (dir_base);
1132 * Hokey little macro trick to permit the definitions of C functions
1133 * with the same name as CPP macros
1135 #define args1(x) (x)
1136 #define args2(x,y) (x,y)
1139 FcCacheDir args1(const FcCache *c)
1141 return FcCacheDir (c);
1145 FcCacheCopySet args1(const FcCache *c)
1147 FcFontSet *old = FcCacheSet (c);
1148 FcFontSet *new = FcFontSetCreate ();
1153 for (i = 0; i < old->nfont; i++)
1155 FcPattern *font = FcFontSetFont (old, i);
1157 FcPatternReference (font);
1158 if (!FcFontSetAdd (new, font))
1160 FcFontSetDestroy (new);
1168 FcCacheSubdir args2(const FcCache *c, int i)
1170 return FcCacheSubdir (c, i);
1174 FcCacheNumSubdir args1(const FcCache *c)
1176 return c->dirs_count;
1180 FcCacheNumFont args1(const FcCache *c)
1182 return FcCacheSet(c)->nfont;
1186 * This code implements the MD5 message-digest algorithm.
1187 * The algorithm is due to Ron Rivest. This code was
1188 * written by Colin Plumb in 1993, no copyright is claimed.
1189 * This code is in the public domain; do with it what you wish.
1191 * Equivalent code is available from RSA Data Security, Inc.
1192 * This code has been tested against that, and is equivalent,
1193 * except that you don't need to include two pages of legalese
1196 * To compute the message digest of a chunk of bytes, declare an
1197 * MD5Context structure, pass it to MD5Init, call MD5Update as
1198 * needed on buffers full of bytes, and then call MD5Final, which
1199 * will fill a supplied 16-byte array with the digest.
1203 #define byteReverse(buf, len) /* Nothing */
1206 * Note: this code is harmless on little-endian machines.
1208 void byteReverse(unsigned char *buf, unsigned longs)
1212 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1213 ((unsigned) buf[1] << 8 | buf[0]);
1214 *(FcChar32 *) buf = t;
1221 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1222 * initialization constants.
1224 static void MD5Init(struct MD5Context *ctx)
1226 ctx->buf[0] = 0x67452301;
1227 ctx->buf[1] = 0xefcdab89;
1228 ctx->buf[2] = 0x98badcfe;
1229 ctx->buf[3] = 0x10325476;
1236 * Update context to reflect the concatenation of another buffer full
1239 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len)
1243 /* Update bitcount */
1246 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1247 ctx->bits[1]++; /* Carry from low to high */
1248 ctx->bits[1] += len >> 29;
1250 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1252 /* Handle any leading odd-sized chunks */
1255 unsigned char *p = (unsigned char *) ctx->in + t;
1259 memcpy(p, buf, len);
1263 byteReverse(ctx->in, 16);
1264 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1268 /* Process data in 64-byte chunks */
1271 memcpy(ctx->in, buf, 64);
1272 byteReverse(ctx->in, 16);
1273 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1278 /* Handle any remaining bytes of data. */
1280 memcpy(ctx->in, buf, len);
1284 * Final wrapup - pad to 64-byte boundary with the bit pattern
1285 * 1 0* (64-bit count of bits processed, MSB-first)
1287 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1292 /* Compute number of bytes mod 64 */
1293 count = (ctx->bits[0] >> 3) & 0x3F;
1295 /* Set the first char of padding to 0x80. This is safe since there is
1296 always at least one byte free */
1297 p = ctx->in + count;
1300 /* Bytes of padding needed to make 64 bytes */
1301 count = 64 - 1 - count;
1303 /* Pad out to 56 mod 64 */
1305 /* Two lots of padding: Pad the first block to 64 bytes */
1306 memset(p, 0, count);
1307 byteReverse(ctx->in, 16);
1308 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1310 /* Now fill the next block with 56 bytes */
1311 memset(ctx->in, 0, 56);
1313 /* Pad block to 56 bytes */
1314 memset(p, 0, count - 8);
1316 byteReverse(ctx->in, 14);
1318 /* Append length in bits and transform */
1319 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1320 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1322 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1323 byteReverse((unsigned char *) ctx->buf, 4);
1324 memcpy(digest, ctx->buf, 16);
1325 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
1329 /* The four core functions - F1 is optimized somewhat */
1331 /* #define F1(x, y, z) (x & y | ~x & z) */
1332 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1333 #define F2(x, y, z) F1(z, x, y)
1334 #define F3(x, y, z) (x ^ y ^ z)
1335 #define F4(x, y, z) (y ^ (x | ~z))
1337 /* This is the central step in the MD5 algorithm. */
1338 #define MD5STEP(f, w, x, y, z, data, s) \
1339 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1342 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1343 * reflect the addition of 16 longwords of new data. MD5Update blocks
1344 * the data and converts bytes into longwords for this routine.
1346 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1348 register FcChar32 a, b, c, d;
1355 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1356 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1357 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1358 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1359 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1360 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1361 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1362 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1363 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1364 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1365 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1366 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1367 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1368 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1369 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1370 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1372 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1373 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1374 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1375 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1376 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1377 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1378 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1379 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1380 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1381 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1382 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1383 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1384 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1385 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1386 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1387 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1389 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1390 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1391 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1392 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1393 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1394 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1395 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1396 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1397 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1398 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1399 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1400 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1401 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1402 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1403 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1404 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1406 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1407 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1408 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1409 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1410 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1411 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1412 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1413 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1414 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1415 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1416 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1417 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1418 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1419 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1420 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1421 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1430 FcDirCacheCreateTagFile (const FcChar8 *cache_dir)
1436 static const FcChar8 cache_tag_contents[] =
1437 "Signature: 8a477f597d28d172789f06886806bc55\n"
1438 "# This file is a cache directory tag created by fontconfig.\n"
1439 "# For information about cache directory tags, see:\n"
1440 "# http://www.brynosaurus.com/cachedir/\n";
1441 static size_t cache_tag_contents_size = sizeof (cache_tag_contents) - 1;
1442 FcBool ret = FcFalse;
1447 if (access ((char *) cache_dir, W_OK) == 0)
1449 /* Create CACHEDIR.TAG */
1450 cache_tag = FcStrPlus (cache_dir, (const FcChar8 *) FC_DIR_SEPARATOR_S "CACHEDIR.TAG");
1453 atomic = FcAtomicCreate ((FcChar8 *)cache_tag);
1456 if (!FcAtomicLock (atomic))
1458 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0644);
1461 fp = fdopen(fd, "wb");
1465 fwrite(cache_tag_contents, cache_tag_contents_size, sizeof (FcChar8), fp);
1468 if (!FcAtomicReplaceOrig(atomic))
1473 FcAtomicUnlock (atomic);
1475 FcAtomicDestroy (atomic);
1477 FcStrFree (cache_tag);
1480 if (FcDebug () & FC_DBG_CACHE)
1483 printf ("Created CACHEDIR.TAG at %s\n", cache_dir);
1485 printf ("Unable to create CACHEDIR.TAG at %s\n", cache_dir);
1492 FcCacheCreateTagFile (const FcConfig *config)
1494 FcChar8 *cache_dir = NULL;
1497 list = FcConfigGetCacheDirs (config);
1501 while ((cache_dir = FcStrListNext (list)))
1503 if (FcDirCacheCreateTagFile (cache_dir))
1506 FcStrListDone (list);
1510 #include "fcaliastail.h"