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 * Generate a random level number, distributed
312 * so that each level is 1/4 as likely as the one before
314 * Note that level numbers run 1 <= level <= MAX_LEVEL
319 /* tricky bit -- each bit is '1' 75% of the time */
320 long int bits = FcRandom () | FcRandom ();
323 while (++level < FC_CACHE_MAX_LEVEL)
333 * Insert cache into the list
336 FcCacheInsert (FcCache *cache, struct stat *cache_stat)
338 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
339 FcCacheSkip *s, **next;
343 * Find links along each chain
345 next = fcCacheChains;
346 for (i = fcCacheMaxLevel; --i >= 0; )
348 for (; (s = next[i]); next = s->next)
349 if (s->cache > cache)
351 update[i] = &next[i];
355 * Create new list element
357 level = random_level ();
358 if (level > fcCacheMaxLevel)
360 level = fcCacheMaxLevel + 1;
361 update[fcCacheMaxLevel] = &fcCacheChains[fcCacheMaxLevel];
362 fcCacheMaxLevel = level;
365 s = malloc (sizeof (FcCacheSkip) + (level - 1) * sizeof (FcCacheSkip *));
370 s->size = cache->size;
374 s->cache_dev = cache_stat->st_dev;
375 s->cache_ino = cache_stat->st_ino;
376 s->cache_mtime = cache_stat->st_mtime;
386 * Insert into all fcCacheChains
388 for (i = 0; i < level; i++)
390 s->next[i] = *update[i];
397 FcCacheFindByAddr (void *object)
400 FcCacheSkip **next = fcCacheChains;
404 * Walk chain pointers one level at a time
406 for (i = fcCacheMaxLevel; --i >= 0;)
407 while (next[i] && (char *) object >= ((char *) next[i]->cache + next[i]->size))
408 next = next[i]->next;
413 if (s && (char *) object < ((char *) s->cache + s->size))
419 FcCacheRemove (FcCache *cache)
421 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
422 FcCacheSkip *s, **next;
426 * Find links along each chain
428 next = fcCacheChains;
429 for (i = fcCacheMaxLevel; --i >= 0; )
431 for (; (s = next[i]); next = s->next)
432 if (s->cache >= cache)
434 update[i] = &next[i];
437 for (i = 0; i < fcCacheMaxLevel && *update[i] == s; i++)
438 *update[i] = s->next[i];
439 while (fcCacheMaxLevel > 0 && fcCacheChains[fcCacheMaxLevel - 1] == NULL)
445 FcCacheFindByStat (struct stat *cache_stat)
449 for (s = fcCacheChains[0]; s; s = s->next[0])
450 if (s->cache_dev == cache_stat->st_dev &&
451 s->cache_ino == cache_stat->st_ino &&
452 s->cache_mtime == cache_stat->st_mtime)
461 FcDirCacheDispose (FcCache *cache)
463 switch (cache->magic) {
464 case FC_CACHE_MAGIC_ALLOC:
467 case FC_CACHE_MAGIC_MMAP:
468 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
469 munmap (cache, cache->size);
470 #elif defined(_WIN32)
471 UnmapViewOfFile (cache);
475 FcCacheRemove (cache);
479 FcCacheObjectReference (void *object)
481 FcCacheSkip *skip = FcCacheFindByAddr (object);
488 FcCacheObjectDereference (void *object)
490 FcCacheSkip *skip = FcCacheFindByAddr (object);
496 FcDirCacheDispose (skip->cache);
505 for (i = 0; i < FC_CACHE_MAX_LEVEL; i++)
506 assert (fcCacheChains[i] == NULL);
507 assert (fcCacheMaxLevel == 0);
511 FcCacheTimeValid (FcCache *cache, struct stat *dir_stat)
513 struct stat dir_static;
517 if (FcStatChecksum (FcCacheDir (cache), &dir_static) < 0)
519 dir_stat = &dir_static;
521 if (FcDebug () & FC_DBG_CACHE)
522 printf ("FcCacheTimeValid dir \"%s\" cache checksum %d dir checksum %d\n",
523 FcCacheDir (cache), cache->checksum, (int) dir_stat->st_mtime);
524 return cache->checksum == (int) dir_stat->st_mtime;
528 * Map a cache file into memory
531 FcDirCacheMapFd (int fd, struct stat *fd_stat, struct stat *dir_stat)
534 FcBool allocated = FcFalse;
536 if (fd_stat->st_size < (int) sizeof (FcCache))
538 cache = FcCacheFindByStat (fd_stat);
541 if (FcCacheTimeValid (cache, dir_stat))
543 FcDirCacheUnload (cache);
548 * Large cache files are mmap'ed, smaller cache files are read. This
549 * balances the system cost of mmap against per-process memory usage.
551 if (FcCacheIsMmapSafe (fd) && fd_stat->st_size >= FC_CACHE_MIN_MMAP)
553 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
554 cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0);
555 #ifdef HAVE_POSIX_FADVISE
556 posix_fadvise (fd, 0, fd_stat->st_size, POSIX_FADV_WILLNEED);
558 if (cache == MAP_FAILED)
560 #elif defined(_WIN32)
565 hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL,
566 PAGE_READONLY, 0, 0, NULL);
567 if (hFileMap != NULL)
569 cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0,
571 CloseHandle (hFileMap);
578 cache = malloc (fd_stat->st_size);
582 if (read (fd, cache, fd_stat->st_size) != fd_stat->st_size)
589 if (cache->magic != FC_CACHE_MAGIC_MMAP ||
590 cache->version < FC_CACHE_CONTENT_VERSION ||
591 cache->size != (intptr_t) fd_stat->st_size ||
592 !FcCacheTimeValid (cache, dir_stat) ||
593 !FcCacheInsert (cache, fd_stat))
599 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
600 munmap (cache, fd_stat->st_size);
601 #elif defined(_WIN32)
602 UnmapViewOfFile (cache);
608 /* Mark allocated caches so they're freed rather than unmapped */
610 cache->magic = FC_CACHE_MAGIC_ALLOC;
616 FcDirCacheReference (FcCache *cache, int nref)
618 FcCacheSkip *skip = FcCacheFindByAddr (cache);
625 FcDirCacheUnload (FcCache *cache)
627 FcCacheObjectDereference (cache);
631 FcDirCacheMapHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
633 FcCache *cache = FcDirCacheMapFd (fd, fd_stat, dir_stat);
637 *((FcCache **) closure) = cache;
642 FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file)
644 FcCache *cache = NULL;
646 if (!FcDirCacheProcess (config, dir,
654 FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat)
658 struct stat my_file_stat;
661 file_stat = &my_file_stat;
662 fd = FcDirCacheOpenFile (cache_file, file_stat);
665 cache = FcDirCacheMapFd (fd, file_stat, NULL);
671 * Validate a cache file by reading the header and checking
672 * the magic number and the size field
675 FcDirCacheValidateHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure FC_UNUSED)
680 if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache))
682 else if (c.magic != FC_CACHE_MAGIC_MMAP)
684 else if (c.version < FC_CACHE_CONTENT_VERSION)
686 else if (fd_stat->st_size != c.size)
688 else if (c.checksum != (int) dir_stat->st_mtime)
694 FcDirCacheValidConfig (const FcChar8 *dir, FcConfig *config)
696 return FcDirCacheProcess (config, dir,
697 FcDirCacheValidateHelper,
702 FcDirCacheValid (const FcChar8 *dir)
706 config = FcConfigGetCurrent ();
710 return FcDirCacheValidConfig (dir, config);
714 * Build a cache structure from the given contents
717 FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcStrSet *dirs)
719 FcSerialize *serialize = FcSerializeCreate ();
722 FcChar8 *dir_serialize;
723 intptr_t *dirs_serialize;
724 FcFontSet *set_serialize;
729 * Space for cache structure
731 FcSerializeReserve (serialize, sizeof (FcCache));
735 if (!FcStrSerializeAlloc (serialize, dir))
740 FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *));
741 for (i = 0; i < dirs->num; i++)
742 if (!FcStrSerializeAlloc (serialize, dirs->strs[i]))
748 if (!FcFontSetSerializeAlloc (serialize, set))
751 /* Serialize layout complete. Now allocate space and fill it */
752 cache = malloc (serialize->size);
755 /* shut up valgrind */
756 memset (cache, 0, serialize->size);
758 serialize->linear = cache;
760 cache->magic = FC_CACHE_MAGIC_ALLOC;
761 cache->version = FC_CACHE_CONTENT_VERSION;
762 cache->size = serialize->size;
763 cache->checksum = (int) dir_stat->st_mtime;
766 * Serialize directory name
768 dir_serialize = FcStrSerialize (serialize, dir);
771 cache->dir = FcPtrToOffset (cache, dir_serialize);
776 dirs_serialize = FcSerializePtr (serialize, dirs);
779 cache->dirs = FcPtrToOffset (cache, dirs_serialize);
780 cache->dirs_count = dirs->num;
781 for (i = 0; i < dirs->num; i++)
783 FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]);
786 dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize);
792 set_serialize = FcFontSetSerialize (serialize, set);
795 cache->set = FcPtrToOffset (cache, set_serialize);
797 FcSerializeDestroy (serialize);
799 FcCacheInsert (cache, NULL);
806 FcSerializeDestroy (serialize);
812 #define mkdir(path,mode) _mkdir(path)
816 FcMakeDirectory (const FcChar8 *dir)
821 if (strlen ((char *) dir) == 0)
824 parent = FcStrDirname (dir);
827 if (access ((char *) parent, F_OK) == 0)
828 ret = mkdir ((char *) dir, 0755) == 0 && chmod ((char *) dir, 0755) == 0;
829 else if (access ((char *) parent, F_OK) == -1)
830 ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0755) == 0) && chmod ((char *) dir, 0755) == 0;
837 /* write serialized state to the cache file */
839 FcDirCacheWrite (FcCache *cache, FcConfig *config)
841 FcChar8 *dir = FcCacheDir (cache);
842 FcChar8 cache_base[CACHEBASE_LEN];
843 FcChar8 *cache_hashed;
847 FcChar8 *cache_dir = NULL;
850 struct stat cache_stat;
855 * Write it to the first directory in the list which is writable
858 list = FcStrListCreate (config->cacheDirs);
861 while ((test_dir = FcStrListNext (list))) {
862 if (access ((char *) test_dir, W_OK) == 0)
864 cache_dir = test_dir;
870 * If the directory doesn't exist, try to create it
872 if (access ((char *) test_dir, F_OK) == -1) {
873 if (FcMakeDirectory (test_dir))
875 cache_dir = test_dir;
876 /* Create CACHEDIR.TAG */
877 FcDirCacheCreateTagFile (cache_dir);
882 * Otherwise, try making it writable
884 else if (chmod ((char *) test_dir, 0755) == 0)
886 cache_dir = test_dir;
887 /* Try to create CACHEDIR.TAG too */
888 FcDirCacheCreateTagFile (cache_dir);
893 FcStrListDone (list);
897 FcDirCacheBasename (dir, cache_base);
898 cache_hashed = FcStrPlus (cache_dir, cache_base);
902 if (FcDebug () & FC_DBG_CACHE)
903 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
906 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
910 if (!FcAtomicLock (atomic))
913 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
917 /* Temporarily switch magic to MMAP while writing to file */
918 magic = cache->magic;
919 if (magic != FC_CACHE_MAGIC_MMAP)
920 cache->magic = FC_CACHE_MAGIC_MMAP;
923 * Write cache contents to file
925 written = write (fd, cache, cache->size);
927 /* Switch magic back */
928 if (magic != FC_CACHE_MAGIC_MMAP)
929 cache->magic = magic;
931 if (written != cache->size)
933 perror ("write cache");
938 if (!FcAtomicReplaceOrig(atomic))
941 /* If the file is small, update the cache chain entry such that the
942 * new cache file is not read again. If it's large, we don't do that
943 * such that we reload it, using mmap, which is shared across processes.
945 if (cache->size < FC_CACHE_MIN_MMAP &&
946 (skip = FcCacheFindByAddr (cache)) &&
947 FcStat (cache_hashed, &cache_stat))
949 skip->cache_dev = cache_stat.st_dev;
950 skip->cache_ino = cache_stat.st_ino;
951 skip->cache_mtime = cache_stat.st_mtime;
954 FcStrFree (cache_hashed);
955 FcAtomicUnlock (atomic);
956 FcAtomicDestroy (atomic);
962 FcAtomicUnlock (atomic);
964 FcAtomicDestroy (atomic);
966 FcStrFree (cache_hashed);
971 FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose)
979 struct stat target_stat;
981 dir_base = FcStrPlus (cache_dir, (FcChar8 *) FC_DIR_SEPARATOR_S);
984 fprintf (stderr, "Fontconfig error: %s: out of memory\n", cache_dir);
987 if (access ((char *) cache_dir, W_OK) != 0)
989 if (verbose || FcDebug () & FC_DBG_CACHE)
990 printf ("%s: not cleaning %s cache directory\n", cache_dir,
991 access ((char *) cache_dir, F_OK) == 0 ? "unwritable" : "non-existent");
994 if (verbose || FcDebug () & FC_DBG_CACHE)
995 printf ("%s: cleaning cache directory\n", cache_dir);
996 d = opendir ((char *) cache_dir);
999 perror ((char *) cache_dir);
1003 while ((ent = readdir (d)))
1006 const FcChar8 *target_dir;
1008 if (ent->d_name[0] == '.')
1010 /* skip cache files for different architectures and */
1011 /* files which are not cache files at all */
1012 if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) ||
1013 strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX))
1016 file_name = FcStrPlus (dir_base, (FcChar8 *) ent->d_name);
1019 fprintf (stderr, "Fontconfig error: %s: allocation failure\n", cache_dir);
1024 cache = FcDirCacheLoadFile (file_name, NULL);
1027 if (verbose || FcDebug () & FC_DBG_CACHE)
1028 printf ("%s: invalid cache file: %s\n", cache_dir, ent->d_name);
1033 target_dir = FcCacheDir (cache);
1034 if (stat ((char *) target_dir, &target_stat) < 0)
1036 if (verbose || FcDebug () & FC_DBG_CACHE)
1037 printf ("%s: %s: missing directory: %s \n",
1038 cache_dir, ent->d_name, target_dir);
1044 if (unlink ((char *) file_name) < 0)
1046 perror ((char *) file_name);
1050 FcDirCacheUnload (cache);
1051 FcStrFree (file_name);
1056 FcStrFree (dir_base);
1062 * Hokey little macro trick to permit the definitions of C functions
1063 * with the same name as CPP macros
1065 #define args1(x) (x)
1066 #define args2(x,y) (x,y)
1069 FcCacheDir args1(const FcCache *c)
1071 return FcCacheDir (c);
1075 FcCacheCopySet args1(const FcCache *c)
1077 FcFontSet *old = FcCacheSet (c);
1078 FcFontSet *new = FcFontSetCreate ();
1083 for (i = 0; i < old->nfont; i++)
1085 FcPattern *font = FcFontSetFont (old, i);
1087 FcPatternReference (font);
1088 if (!FcFontSetAdd (new, font))
1090 FcFontSetDestroy (new);
1098 FcCacheSubdir args2(const FcCache *c, int i)
1100 return FcCacheSubdir (c, i);
1104 FcCacheNumSubdir args1(const FcCache *c)
1106 return c->dirs_count;
1110 FcCacheNumFont args1(const FcCache *c)
1112 return FcCacheSet(c)->nfont;
1116 * This code implements the MD5 message-digest algorithm.
1117 * The algorithm is due to Ron Rivest. This code was
1118 * written by Colin Plumb in 1993, no copyright is claimed.
1119 * This code is in the public domain; do with it what you wish.
1121 * Equivalent code is available from RSA Data Security, Inc.
1122 * This code has been tested against that, and is equivalent,
1123 * except that you don't need to include two pages of legalese
1126 * To compute the message digest of a chunk of bytes, declare an
1127 * MD5Context structure, pass it to MD5Init, call MD5Update as
1128 * needed on buffers full of bytes, and then call MD5Final, which
1129 * will fill a supplied 16-byte array with the digest.
1133 #define byteReverse(buf, len) /* Nothing */
1136 * Note: this code is harmless on little-endian machines.
1138 void byteReverse(unsigned char *buf, unsigned longs)
1142 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1143 ((unsigned) buf[1] << 8 | buf[0]);
1144 *(FcChar32 *) buf = t;
1151 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1152 * initialization constants.
1154 static void MD5Init(struct MD5Context *ctx)
1156 ctx->buf[0] = 0x67452301;
1157 ctx->buf[1] = 0xefcdab89;
1158 ctx->buf[2] = 0x98badcfe;
1159 ctx->buf[3] = 0x10325476;
1166 * Update context to reflect the concatenation of another buffer full
1169 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len)
1173 /* Update bitcount */
1176 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1177 ctx->bits[1]++; /* Carry from low to high */
1178 ctx->bits[1] += len >> 29;
1180 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1182 /* Handle any leading odd-sized chunks */
1185 unsigned char *p = (unsigned char *) ctx->in + t;
1189 memcpy(p, buf, len);
1193 byteReverse(ctx->in, 16);
1194 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1198 /* Process data in 64-byte chunks */
1201 memcpy(ctx->in, buf, 64);
1202 byteReverse(ctx->in, 16);
1203 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1208 /* Handle any remaining bytes of data. */
1210 memcpy(ctx->in, buf, len);
1214 * Final wrapup - pad to 64-byte boundary with the bit pattern
1215 * 1 0* (64-bit count of bits processed, MSB-first)
1217 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1222 /* Compute number of bytes mod 64 */
1223 count = (ctx->bits[0] >> 3) & 0x3F;
1225 /* Set the first char of padding to 0x80. This is safe since there is
1226 always at least one byte free */
1227 p = ctx->in + count;
1230 /* Bytes of padding needed to make 64 bytes */
1231 count = 64 - 1 - count;
1233 /* Pad out to 56 mod 64 */
1235 /* Two lots of padding: Pad the first block to 64 bytes */
1236 memset(p, 0, count);
1237 byteReverse(ctx->in, 16);
1238 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1240 /* Now fill the next block with 56 bytes */
1241 memset(ctx->in, 0, 56);
1243 /* Pad block to 56 bytes */
1244 memset(p, 0, count - 8);
1246 byteReverse(ctx->in, 14);
1248 /* Append length in bits and transform */
1249 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1250 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1252 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1253 byteReverse((unsigned char *) ctx->buf, 4);
1254 memcpy(digest, ctx->buf, 16);
1255 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
1259 /* The four core functions - F1 is optimized somewhat */
1261 /* #define F1(x, y, z) (x & y | ~x & z) */
1262 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1263 #define F2(x, y, z) F1(z, x, y)
1264 #define F3(x, y, z) (x ^ y ^ z)
1265 #define F4(x, y, z) (y ^ (x | ~z))
1267 /* This is the central step in the MD5 algorithm. */
1268 #define MD5STEP(f, w, x, y, z, data, s) \
1269 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1272 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1273 * reflect the addition of 16 longwords of new data. MD5Update blocks
1274 * the data and converts bytes into longwords for this routine.
1276 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1278 register FcChar32 a, b, c, d;
1285 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1286 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1287 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1288 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1289 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1290 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1291 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1292 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1293 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1294 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1295 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1296 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1297 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1298 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1299 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1300 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1302 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1303 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1304 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1305 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1306 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1307 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1308 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1309 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1310 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1311 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1312 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1313 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1314 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1315 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1316 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1317 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1319 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1320 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1321 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1322 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1323 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1324 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1325 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1326 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1327 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1328 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1329 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1330 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1331 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1332 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1333 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1334 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1336 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1337 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1338 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1339 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1340 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1341 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1342 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1343 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1344 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1345 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1346 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1347 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1348 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1349 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1350 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1351 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1360 FcDirCacheCreateTagFile (const FcChar8 *cache_dir)
1366 static const FcChar8 cache_tag_contents[] =
1367 "Signature: 8a477f597d28d172789f06886806bc55\n"
1368 "# This file is a cache directory tag created by fontconfig.\n"
1369 "# For information about cache directory tags, see:\n"
1370 "# http://www.brynosaurus.com/cachedir/\n";
1371 static size_t cache_tag_contents_size = sizeof (cache_tag_contents) - 1;
1372 FcBool ret = FcFalse;
1377 if (access ((char *) cache_dir, W_OK) == 0)
1379 /* Create CACHEDIR.TAG */
1380 cache_tag = FcStrPlus (cache_dir, (const FcChar8 *) FC_DIR_SEPARATOR_S "CACHEDIR.TAG");
1383 atomic = FcAtomicCreate ((FcChar8 *)cache_tag);
1386 if (!FcAtomicLock (atomic))
1388 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0644);
1391 fp = fdopen(fd, "wb");
1395 fwrite(cache_tag_contents, cache_tag_contents_size, sizeof (FcChar8), fp);
1398 if (!FcAtomicReplaceOrig(atomic))
1403 FcAtomicUnlock (atomic);
1405 FcAtomicDestroy (atomic);
1407 FcStrFree (cache_tag);
1410 if (FcDebug () & FC_DBG_CACHE)
1413 printf ("Created CACHEDIR.TAG at %s\n", cache_dir);
1415 printf ("Unable to create CACHEDIR.TAG at %s\n", cache_dir);
1422 FcCacheCreateTagFile (const FcConfig *config)
1424 FcChar8 *cache_dir = NULL;
1427 list = FcConfigGetCacheDirs (config);
1431 while ((cache_dir = FcStrListNext (list)))
1433 if (FcDirCacheCreateTagFile (cache_dir))
1436 FcStrListDone (list);
1440 #include "fcaliastail.h"