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 static FcCacheSkip *fcCacheChains[FC_CACHE_MAX_LEVEL];
252 static int fcCacheMaxLevel;
261 static struct random_data fcrandbuf;
262 static char statebuf[256];
263 static FcBool initialized = FcFalse;
265 if (initialized != FcTrue)
267 initstate_r(time(NULL), statebuf, 256, &fcrandbuf);
268 initialized = FcTrue;
271 random_r(&fcrandbuf, &result);
273 static char statebuf[256];
275 static FcBool initialized = FcFalse;
277 if (initialized != FcTrue)
279 state = initstate(time(NULL), statebuf, 256);
280 initialized = FcTrue;
283 state = setstate(statebuf);
291 static unsigned int seed = time(NULL);
293 result = rand_r(&seed);
295 static FcBool initialized = FcFalse;
297 if (initialized != FcTrue)
300 initialized = FcTrue;
304 # error no random number generator function available.
311 static FcMutex *cache_lock;
318 lock = fc_atomic_ptr_get (&cache_lock);
320 lock = (FcMutex *) malloc (sizeof (FcMutex));
322 if (!fc_atomic_ptr_cmpexch (&cache_lock, NULL, lock)) {
323 FcMutexFinish (lock);
333 FcMutexUnlock (cache_lock);
340 lock = fc_atomic_ptr_get (&cache_lock);
341 if (lock && fc_atomic_ptr_cmpexch (&cache_lock, lock, NULL)) {
342 FcMutexFinish (lock);
350 * Generate a random level number, distributed
351 * so that each level is 1/4 as likely as the one before
353 * Note that level numbers run 1 <= level <= MAX_LEVEL
358 /* tricky bit -- each bit is '1' 75% of the time */
359 long int bits = FcRandom () | FcRandom ();
362 while (++level < FC_CACHE_MAX_LEVEL)
372 * Insert cache into the list
375 FcCacheInsert (FcCache *cache, struct stat *cache_stat)
377 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
378 FcCacheSkip *s, **next;
382 * Find links along each chain
384 next = fcCacheChains;
385 for (i = fcCacheMaxLevel; --i >= 0; )
387 for (; (s = next[i]); next = s->next)
388 if (s->cache > cache)
390 update[i] = &next[i];
394 * Create new list element
396 level = random_level ();
397 if (level > fcCacheMaxLevel)
399 level = fcCacheMaxLevel + 1;
400 update[fcCacheMaxLevel] = &fcCacheChains[fcCacheMaxLevel];
401 fcCacheMaxLevel = level;
404 s = malloc (sizeof (FcCacheSkip) + (level - 1) * sizeof (FcCacheSkip *));
409 s->size = cache->size;
410 FcRefInit (&s->ref, 1);
413 s->cache_dev = cache_stat->st_dev;
414 s->cache_ino = cache_stat->st_ino;
415 s->cache_mtime = cache_stat->st_mtime;
425 * Insert into all fcCacheChains
427 for (i = 0; i < level; i++)
429 s->next[i] = *update[i];
436 FcCacheFindByAddr (void *object)
439 FcCacheSkip **next = fcCacheChains;
443 * Walk chain pointers one level at a time
445 for (i = fcCacheMaxLevel; --i >= 0;)
446 while (next[i] && (char *) object >= ((char *) next[i]->cache + next[i]->size))
447 next = next[i]->next;
452 if (s && (char *) object < ((char *) s->cache + s->size))
458 FcCacheRemove (FcCache *cache)
460 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
461 FcCacheSkip *s, **next;
465 * Find links along each chain
467 next = fcCacheChains;
468 for (i = fcCacheMaxLevel; --i >= 0; )
470 for (; (s = next[i]); next = s->next)
471 if (s->cache >= cache)
473 update[i] = &next[i];
476 for (i = 0; i < fcCacheMaxLevel && *update[i] == s; i++)
477 *update[i] = s->next[i];
478 while (fcCacheMaxLevel > 0 && fcCacheChains[fcCacheMaxLevel - 1] == NULL)
484 FcCacheFindByStat (struct stat *cache_stat)
488 for (s = fcCacheChains[0]; s; s = s->next[0])
489 if (s->cache_dev == cache_stat->st_dev &&
490 s->cache_ino == cache_stat->st_ino &&
491 s->cache_mtime == cache_stat->st_mtime)
500 FcDirCacheDispose (FcCache *cache)
502 switch (cache->magic) {
503 case FC_CACHE_MAGIC_ALLOC:
506 case FC_CACHE_MAGIC_MMAP:
507 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
508 munmap (cache, cache->size);
509 #elif defined(_WIN32)
510 UnmapViewOfFile (cache);
514 FcCacheRemove (cache);
518 FcCacheObjectReference (void *object)
520 FcCacheSkip *skip = FcCacheFindByAddr (object);
523 FcRefInc (&skip->ref);
527 FcCacheObjectDereference (void *object)
529 FcCacheSkip *skip = FcCacheFindByAddr (object);
533 if (FcRefDec (&skip->ref) <= 1)
534 FcDirCacheDispose (skip->cache);
543 for (i = 0; i < FC_CACHE_MAX_LEVEL; i++)
544 assert (fcCacheChains[i] == NULL);
545 assert (fcCacheMaxLevel == 0);
551 FcCacheTimeValid (FcCache *cache, struct stat *dir_stat)
553 struct stat dir_static;
557 if (FcStatChecksum (FcCacheDir (cache), &dir_static) < 0)
559 dir_stat = &dir_static;
561 if (FcDebug () & FC_DBG_CACHE)
562 printf ("FcCacheTimeValid dir \"%s\" cache checksum %d dir checksum %d\n",
563 FcCacheDir (cache), cache->checksum, (int) dir_stat->st_mtime);
564 return cache->checksum == (int) dir_stat->st_mtime;
568 * Map a cache file into memory
571 FcDirCacheMapFd (int fd, struct stat *fd_stat, struct stat *dir_stat)
574 FcBool allocated = FcFalse;
576 if (fd_stat->st_size < (int) sizeof (FcCache))
578 cache = FcCacheFindByStat (fd_stat);
581 if (FcCacheTimeValid (cache, dir_stat))
583 FcDirCacheUnload (cache);
588 * Large cache files are mmap'ed, smaller cache files are read. This
589 * balances the system cost of mmap against per-process memory usage.
591 if (FcCacheIsMmapSafe (fd) && fd_stat->st_size >= FC_CACHE_MIN_MMAP)
593 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
594 cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0);
595 #ifdef HAVE_POSIX_FADVISE
596 posix_fadvise (fd, 0, fd_stat->st_size, POSIX_FADV_WILLNEED);
598 if (cache == MAP_FAILED)
600 #elif defined(_WIN32)
605 hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL,
606 PAGE_READONLY, 0, 0, NULL);
607 if (hFileMap != NULL)
609 cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0,
611 CloseHandle (hFileMap);
618 cache = malloc (fd_stat->st_size);
622 if (read (fd, cache, fd_stat->st_size) != fd_stat->st_size)
629 if (cache->magic != FC_CACHE_MAGIC_MMAP ||
630 cache->version < FC_CACHE_CONTENT_VERSION ||
631 cache->size != (intptr_t) fd_stat->st_size ||
632 !FcCacheTimeValid (cache, dir_stat) ||
633 !FcCacheInsert (cache, fd_stat))
639 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
640 munmap (cache, fd_stat->st_size);
641 #elif defined(_WIN32)
642 UnmapViewOfFile (cache);
648 /* Mark allocated caches so they're freed rather than unmapped */
650 cache->magic = FC_CACHE_MAGIC_ALLOC;
656 FcDirCacheReference (FcCache *cache, int nref)
658 FcCacheSkip *skip = FcCacheFindByAddr (cache);
661 FcRefAdd (&skip->ref, nref);
665 FcDirCacheUnload (FcCache *cache)
667 FcCacheObjectDereference (cache);
671 FcDirCacheMapHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
673 FcCache *cache = FcDirCacheMapFd (fd, fd_stat, dir_stat);
677 *((FcCache **) closure) = cache;
682 FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file)
684 FcCache *cache = NULL;
686 if (!FcDirCacheProcess (config, dir,
694 FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat)
698 struct stat my_file_stat;
701 file_stat = &my_file_stat;
702 fd = FcDirCacheOpenFile (cache_file, file_stat);
705 cache = FcDirCacheMapFd (fd, file_stat, NULL);
711 * Validate a cache file by reading the header and checking
712 * the magic number and the size field
715 FcDirCacheValidateHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure FC_UNUSED)
720 if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache))
722 else if (c.magic != FC_CACHE_MAGIC_MMAP)
724 else if (c.version < FC_CACHE_CONTENT_VERSION)
726 else if (fd_stat->st_size != c.size)
728 else if (c.checksum != (int) dir_stat->st_mtime)
734 FcDirCacheValidConfig (const FcChar8 *dir, FcConfig *config)
736 return FcDirCacheProcess (config, dir,
737 FcDirCacheValidateHelper,
742 FcDirCacheValid (const FcChar8 *dir)
746 config = FcConfigGetCurrent ();
750 return FcDirCacheValidConfig (dir, config);
754 * Build a cache structure from the given contents
757 FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcStrSet *dirs)
759 FcSerialize *serialize = FcSerializeCreate ();
762 FcChar8 *dir_serialize;
763 intptr_t *dirs_serialize;
764 FcFontSet *set_serialize;
769 * Space for cache structure
771 FcSerializeReserve (serialize, sizeof (FcCache));
775 if (!FcStrSerializeAlloc (serialize, dir))
780 FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *));
781 for (i = 0; i < dirs->num; i++)
782 if (!FcStrSerializeAlloc (serialize, dirs->strs[i]))
788 if (!FcFontSetSerializeAlloc (serialize, set))
791 /* Serialize layout complete. Now allocate space and fill it */
792 cache = malloc (serialize->size);
795 /* shut up valgrind */
796 memset (cache, 0, serialize->size);
798 serialize->linear = cache;
800 cache->magic = FC_CACHE_MAGIC_ALLOC;
801 cache->version = FC_CACHE_CONTENT_VERSION;
802 cache->size = serialize->size;
803 cache->checksum = (int) dir_stat->st_mtime;
806 * Serialize directory name
808 dir_serialize = FcStrSerialize (serialize, dir);
811 cache->dir = FcPtrToOffset (cache, dir_serialize);
816 dirs_serialize = FcSerializePtr (serialize, dirs);
819 cache->dirs = FcPtrToOffset (cache, dirs_serialize);
820 cache->dirs_count = dirs->num;
821 for (i = 0; i < dirs->num; i++)
823 FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]);
826 dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize);
832 set_serialize = FcFontSetSerialize (serialize, set);
835 cache->set = FcPtrToOffset (cache, set_serialize);
837 FcSerializeDestroy (serialize);
839 FcCacheInsert (cache, NULL);
846 FcSerializeDestroy (serialize);
852 #define mkdir(path,mode) _mkdir(path)
856 FcMakeDirectory (const FcChar8 *dir)
861 if (strlen ((char *) dir) == 0)
864 parent = FcStrDirname (dir);
867 if (access ((char *) parent, F_OK) == 0)
868 ret = mkdir ((char *) dir, 0755) == 0 && chmod ((char *) dir, 0755) == 0;
869 else if (access ((char *) parent, F_OK) == -1)
870 ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0755) == 0) && chmod ((char *) dir, 0755) == 0;
877 /* write serialized state to the cache file */
879 FcDirCacheWrite (FcCache *cache, FcConfig *config)
881 FcChar8 *dir = FcCacheDir (cache);
882 FcChar8 cache_base[CACHEBASE_LEN];
883 FcChar8 *cache_hashed;
887 FcChar8 *cache_dir = NULL;
890 struct stat cache_stat;
895 * Write it to the first directory in the list which is writable
898 list = FcStrListCreate (config->cacheDirs);
901 while ((test_dir = FcStrListNext (list))) {
902 if (access ((char *) test_dir, W_OK) == 0)
904 cache_dir = test_dir;
910 * If the directory doesn't exist, try to create it
912 if (access ((char *) test_dir, F_OK) == -1) {
913 if (FcMakeDirectory (test_dir))
915 cache_dir = test_dir;
916 /* Create CACHEDIR.TAG */
917 FcDirCacheCreateTagFile (cache_dir);
922 * Otherwise, try making it writable
924 else if (chmod ((char *) test_dir, 0755) == 0)
926 cache_dir = test_dir;
927 /* Try to create CACHEDIR.TAG too */
928 FcDirCacheCreateTagFile (cache_dir);
933 FcStrListDone (list);
937 FcDirCacheBasename (dir, cache_base);
938 cache_hashed = FcStrPlus (cache_dir, cache_base);
942 if (FcDebug () & FC_DBG_CACHE)
943 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
946 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
950 if (!FcAtomicLock (atomic))
953 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
957 /* Temporarily switch magic to MMAP while writing to file */
958 magic = cache->magic;
959 if (magic != FC_CACHE_MAGIC_MMAP)
960 cache->magic = FC_CACHE_MAGIC_MMAP;
963 * Write cache contents to file
965 written = write (fd, cache, cache->size);
967 /* Switch magic back */
968 if (magic != FC_CACHE_MAGIC_MMAP)
969 cache->magic = magic;
971 if (written != cache->size)
973 perror ("write cache");
978 if (!FcAtomicReplaceOrig(atomic))
981 /* If the file is small, update the cache chain entry such that the
982 * new cache file is not read again. If it's large, we don't do that
983 * such that we reload it, using mmap, which is shared across processes.
985 if (cache->size < FC_CACHE_MIN_MMAP &&
986 (skip = FcCacheFindByAddr (cache)) &&
987 FcStat (cache_hashed, &cache_stat))
989 skip->cache_dev = cache_stat.st_dev;
990 skip->cache_ino = cache_stat.st_ino;
991 skip->cache_mtime = cache_stat.st_mtime;
994 FcStrFree (cache_hashed);
995 FcAtomicUnlock (atomic);
996 FcAtomicDestroy (atomic);
1002 FcAtomicUnlock (atomic);
1004 FcAtomicDestroy (atomic);
1006 FcStrFree (cache_hashed);
1011 FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose)
1016 FcBool ret = FcTrue;
1019 struct stat target_stat;
1021 dir_base = FcStrPlus (cache_dir, (FcChar8 *) FC_DIR_SEPARATOR_S);
1024 fprintf (stderr, "Fontconfig error: %s: out of memory\n", cache_dir);
1027 if (access ((char *) cache_dir, W_OK) != 0)
1029 if (verbose || FcDebug () & FC_DBG_CACHE)
1030 printf ("%s: not cleaning %s cache directory\n", cache_dir,
1031 access ((char *) cache_dir, F_OK) == 0 ? "unwritable" : "non-existent");
1034 if (verbose || FcDebug () & FC_DBG_CACHE)
1035 printf ("%s: cleaning cache directory\n", cache_dir);
1036 d = opendir ((char *) cache_dir);
1039 perror ((char *) cache_dir);
1043 while ((ent = readdir (d)))
1046 const FcChar8 *target_dir;
1048 if (ent->d_name[0] == '.')
1050 /* skip cache files for different architectures and */
1051 /* files which are not cache files at all */
1052 if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) ||
1053 strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX))
1056 file_name = FcStrPlus (dir_base, (FcChar8 *) ent->d_name);
1059 fprintf (stderr, "Fontconfig error: %s: allocation failure\n", cache_dir);
1064 cache = FcDirCacheLoadFile (file_name, NULL);
1067 if (verbose || FcDebug () & FC_DBG_CACHE)
1068 printf ("%s: invalid cache file: %s\n", cache_dir, ent->d_name);
1073 target_dir = FcCacheDir (cache);
1074 if (stat ((char *) target_dir, &target_stat) < 0)
1076 if (verbose || FcDebug () & FC_DBG_CACHE)
1077 printf ("%s: %s: missing directory: %s \n",
1078 cache_dir, ent->d_name, target_dir);
1084 if (unlink ((char *) file_name) < 0)
1086 perror ((char *) file_name);
1090 FcDirCacheUnload (cache);
1091 FcStrFree (file_name);
1096 FcStrFree (dir_base);
1102 * Hokey little macro trick to permit the definitions of C functions
1103 * with the same name as CPP macros
1105 #define args1(x) (x)
1106 #define args2(x,y) (x,y)
1109 FcCacheDir args1(const FcCache *c)
1111 return FcCacheDir (c);
1115 FcCacheCopySet args1(const FcCache *c)
1117 FcFontSet *old = FcCacheSet (c);
1118 FcFontSet *new = FcFontSetCreate ();
1123 for (i = 0; i < old->nfont; i++)
1125 FcPattern *font = FcFontSetFont (old, i);
1127 FcPatternReference (font);
1128 if (!FcFontSetAdd (new, font))
1130 FcFontSetDestroy (new);
1138 FcCacheSubdir args2(const FcCache *c, int i)
1140 return FcCacheSubdir (c, i);
1144 FcCacheNumSubdir args1(const FcCache *c)
1146 return c->dirs_count;
1150 FcCacheNumFont args1(const FcCache *c)
1152 return FcCacheSet(c)->nfont;
1156 * This code implements the MD5 message-digest algorithm.
1157 * The algorithm is due to Ron Rivest. This code was
1158 * written by Colin Plumb in 1993, no copyright is claimed.
1159 * This code is in the public domain; do with it what you wish.
1161 * Equivalent code is available from RSA Data Security, Inc.
1162 * This code has been tested against that, and is equivalent,
1163 * except that you don't need to include two pages of legalese
1166 * To compute the message digest of a chunk of bytes, declare an
1167 * MD5Context structure, pass it to MD5Init, call MD5Update as
1168 * needed on buffers full of bytes, and then call MD5Final, which
1169 * will fill a supplied 16-byte array with the digest.
1173 #define byteReverse(buf, len) /* Nothing */
1176 * Note: this code is harmless on little-endian machines.
1178 void byteReverse(unsigned char *buf, unsigned longs)
1182 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1183 ((unsigned) buf[1] << 8 | buf[0]);
1184 *(FcChar32 *) buf = t;
1191 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1192 * initialization constants.
1194 static void MD5Init(struct MD5Context *ctx)
1196 ctx->buf[0] = 0x67452301;
1197 ctx->buf[1] = 0xefcdab89;
1198 ctx->buf[2] = 0x98badcfe;
1199 ctx->buf[3] = 0x10325476;
1206 * Update context to reflect the concatenation of another buffer full
1209 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len)
1213 /* Update bitcount */
1216 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1217 ctx->bits[1]++; /* Carry from low to high */
1218 ctx->bits[1] += len >> 29;
1220 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1222 /* Handle any leading odd-sized chunks */
1225 unsigned char *p = (unsigned char *) ctx->in + t;
1229 memcpy(p, buf, len);
1233 byteReverse(ctx->in, 16);
1234 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1238 /* Process data in 64-byte chunks */
1241 memcpy(ctx->in, buf, 64);
1242 byteReverse(ctx->in, 16);
1243 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1248 /* Handle any remaining bytes of data. */
1250 memcpy(ctx->in, buf, len);
1254 * Final wrapup - pad to 64-byte boundary with the bit pattern
1255 * 1 0* (64-bit count of bits processed, MSB-first)
1257 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1262 /* Compute number of bytes mod 64 */
1263 count = (ctx->bits[0] >> 3) & 0x3F;
1265 /* Set the first char of padding to 0x80. This is safe since there is
1266 always at least one byte free */
1267 p = ctx->in + count;
1270 /* Bytes of padding needed to make 64 bytes */
1271 count = 64 - 1 - count;
1273 /* Pad out to 56 mod 64 */
1275 /* Two lots of padding: Pad the first block to 64 bytes */
1276 memset(p, 0, count);
1277 byteReverse(ctx->in, 16);
1278 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1280 /* Now fill the next block with 56 bytes */
1281 memset(ctx->in, 0, 56);
1283 /* Pad block to 56 bytes */
1284 memset(p, 0, count - 8);
1286 byteReverse(ctx->in, 14);
1288 /* Append length in bits and transform */
1289 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1290 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1292 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1293 byteReverse((unsigned char *) ctx->buf, 4);
1294 memcpy(digest, ctx->buf, 16);
1295 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
1299 /* The four core functions - F1 is optimized somewhat */
1301 /* #define F1(x, y, z) (x & y | ~x & z) */
1302 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1303 #define F2(x, y, z) F1(z, x, y)
1304 #define F3(x, y, z) (x ^ y ^ z)
1305 #define F4(x, y, z) (y ^ (x | ~z))
1307 /* This is the central step in the MD5 algorithm. */
1308 #define MD5STEP(f, w, x, y, z, data, s) \
1309 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1312 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1313 * reflect the addition of 16 longwords of new data. MD5Update blocks
1314 * the data and converts bytes into longwords for this routine.
1316 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1318 register FcChar32 a, b, c, d;
1325 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1326 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1327 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1328 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1329 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1330 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1331 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1332 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1333 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1334 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1335 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1336 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1337 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1338 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1339 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1340 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1342 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1343 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1344 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1345 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1346 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1347 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1348 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1349 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1350 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1351 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1352 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1353 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1354 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1355 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1356 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1357 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1359 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1360 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1361 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1362 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1363 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1364 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1365 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1366 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1367 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1368 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1369 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1370 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1371 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1372 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1373 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1374 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1376 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1377 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1378 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1379 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1380 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1381 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1382 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1383 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1384 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1385 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1386 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1387 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1388 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1389 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1390 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1391 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1400 FcDirCacheCreateTagFile (const FcChar8 *cache_dir)
1406 static const FcChar8 cache_tag_contents[] =
1407 "Signature: 8a477f597d28d172789f06886806bc55\n"
1408 "# This file is a cache directory tag created by fontconfig.\n"
1409 "# For information about cache directory tags, see:\n"
1410 "# http://www.brynosaurus.com/cachedir/\n";
1411 static size_t cache_tag_contents_size = sizeof (cache_tag_contents) - 1;
1412 FcBool ret = FcFalse;
1417 if (access ((char *) cache_dir, W_OK) == 0)
1419 /* Create CACHEDIR.TAG */
1420 cache_tag = FcStrPlus (cache_dir, (const FcChar8 *) FC_DIR_SEPARATOR_S "CACHEDIR.TAG");
1423 atomic = FcAtomicCreate ((FcChar8 *)cache_tag);
1426 if (!FcAtomicLock (atomic))
1428 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0644);
1431 fp = fdopen(fd, "wb");
1435 fwrite(cache_tag_contents, cache_tag_contents_size, sizeof (FcChar8), fp);
1438 if (!FcAtomicReplaceOrig(atomic))
1443 FcAtomicUnlock (atomic);
1445 FcAtomicDestroy (atomic);
1447 FcStrFree (cache_tag);
1450 if (FcDebug () & FC_DBG_CACHE)
1453 printf ("Created CACHEDIR.TAG at %s\n", cache_dir);
1455 printf ("Unable to create CACHEDIR.TAG at %s\n", cache_dir);
1462 FcCacheCreateTagFile (const FcConfig *config)
1464 FcChar8 *cache_dir = NULL;
1467 list = FcConfigGetCacheDirs (config);
1471 while ((cache_dir = FcStrListNext (list)))
1473 if (FcDirCacheCreateTagFile (cache_dir))
1476 FcStrListDone (list);
1480 #include "fcaliastail.h"