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;
371 FcRefInit (&s->ref, 1);
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);
484 FcRefInc (&skip->ref);
488 FcCacheObjectDereference (void *object)
490 FcCacheSkip *skip = FcCacheFindByAddr (object);
494 if (FcRefDec (&skip->ref) <= 1)
495 FcDirCacheDispose (skip->cache);
504 for (i = 0; i < FC_CACHE_MAX_LEVEL; i++)
505 assert (fcCacheChains[i] == NULL);
506 assert (fcCacheMaxLevel == 0);
510 FcCacheTimeValid (FcCache *cache, struct stat *dir_stat)
512 struct stat dir_static;
516 if (FcStatChecksum (FcCacheDir (cache), &dir_static) < 0)
518 dir_stat = &dir_static;
520 if (FcDebug () & FC_DBG_CACHE)
521 printf ("FcCacheTimeValid dir \"%s\" cache checksum %d dir checksum %d\n",
522 FcCacheDir (cache), cache->checksum, (int) dir_stat->st_mtime);
523 return cache->checksum == (int) dir_stat->st_mtime;
527 * Map a cache file into memory
530 FcDirCacheMapFd (int fd, struct stat *fd_stat, struct stat *dir_stat)
533 FcBool allocated = FcFalse;
535 if (fd_stat->st_size < (int) sizeof (FcCache))
537 cache = FcCacheFindByStat (fd_stat);
540 if (FcCacheTimeValid (cache, dir_stat))
542 FcDirCacheUnload (cache);
547 * Large cache files are mmap'ed, smaller cache files are read. This
548 * balances the system cost of mmap against per-process memory usage.
550 if (FcCacheIsMmapSafe (fd) && fd_stat->st_size >= FC_CACHE_MIN_MMAP)
552 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
553 cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0);
554 #ifdef HAVE_POSIX_FADVISE
555 posix_fadvise (fd, 0, fd_stat->st_size, POSIX_FADV_WILLNEED);
557 if (cache == MAP_FAILED)
559 #elif defined(_WIN32)
564 hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL,
565 PAGE_READONLY, 0, 0, NULL);
566 if (hFileMap != NULL)
568 cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0,
570 CloseHandle (hFileMap);
577 cache = malloc (fd_stat->st_size);
581 if (read (fd, cache, fd_stat->st_size) != fd_stat->st_size)
588 if (cache->magic != FC_CACHE_MAGIC_MMAP ||
589 cache->version < FC_CACHE_CONTENT_VERSION ||
590 cache->size != (intptr_t) fd_stat->st_size ||
591 !FcCacheTimeValid (cache, dir_stat) ||
592 !FcCacheInsert (cache, fd_stat))
598 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
599 munmap (cache, fd_stat->st_size);
600 #elif defined(_WIN32)
601 UnmapViewOfFile (cache);
607 /* Mark allocated caches so they're freed rather than unmapped */
609 cache->magic = FC_CACHE_MAGIC_ALLOC;
615 FcDirCacheReference (FcCache *cache, int nref)
617 FcCacheSkip *skip = FcCacheFindByAddr (cache);
620 FcRefAdd (&skip->ref, nref);
624 FcDirCacheUnload (FcCache *cache)
626 FcCacheObjectDereference (cache);
630 FcDirCacheMapHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
632 FcCache *cache = FcDirCacheMapFd (fd, fd_stat, dir_stat);
636 *((FcCache **) closure) = cache;
641 FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file)
643 FcCache *cache = NULL;
645 if (!FcDirCacheProcess (config, dir,
653 FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat)
657 struct stat my_file_stat;
660 file_stat = &my_file_stat;
661 fd = FcDirCacheOpenFile (cache_file, file_stat);
664 cache = FcDirCacheMapFd (fd, file_stat, NULL);
670 * Validate a cache file by reading the header and checking
671 * the magic number and the size field
674 FcDirCacheValidateHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure FC_UNUSED)
679 if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache))
681 else if (c.magic != FC_CACHE_MAGIC_MMAP)
683 else if (c.version < FC_CACHE_CONTENT_VERSION)
685 else if (fd_stat->st_size != c.size)
687 else if (c.checksum != (int) dir_stat->st_mtime)
693 FcDirCacheValidConfig (const FcChar8 *dir, FcConfig *config)
695 return FcDirCacheProcess (config, dir,
696 FcDirCacheValidateHelper,
701 FcDirCacheValid (const FcChar8 *dir)
705 config = FcConfigGetCurrent ();
709 return FcDirCacheValidConfig (dir, config);
713 * Build a cache structure from the given contents
716 FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcStrSet *dirs)
718 FcSerialize *serialize = FcSerializeCreate ();
721 FcChar8 *dir_serialize;
722 intptr_t *dirs_serialize;
723 FcFontSet *set_serialize;
728 * Space for cache structure
730 FcSerializeReserve (serialize, sizeof (FcCache));
734 if (!FcStrSerializeAlloc (serialize, dir))
739 FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *));
740 for (i = 0; i < dirs->num; i++)
741 if (!FcStrSerializeAlloc (serialize, dirs->strs[i]))
747 if (!FcFontSetSerializeAlloc (serialize, set))
750 /* Serialize layout complete. Now allocate space and fill it */
751 cache = malloc (serialize->size);
754 /* shut up valgrind */
755 memset (cache, 0, serialize->size);
757 serialize->linear = cache;
759 cache->magic = FC_CACHE_MAGIC_ALLOC;
760 cache->version = FC_CACHE_CONTENT_VERSION;
761 cache->size = serialize->size;
762 cache->checksum = (int) dir_stat->st_mtime;
765 * Serialize directory name
767 dir_serialize = FcStrSerialize (serialize, dir);
770 cache->dir = FcPtrToOffset (cache, dir_serialize);
775 dirs_serialize = FcSerializePtr (serialize, dirs);
778 cache->dirs = FcPtrToOffset (cache, dirs_serialize);
779 cache->dirs_count = dirs->num;
780 for (i = 0; i < dirs->num; i++)
782 FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]);
785 dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize);
791 set_serialize = FcFontSetSerialize (serialize, set);
794 cache->set = FcPtrToOffset (cache, set_serialize);
796 FcSerializeDestroy (serialize);
798 FcCacheInsert (cache, NULL);
805 FcSerializeDestroy (serialize);
811 #define mkdir(path,mode) _mkdir(path)
815 FcMakeDirectory (const FcChar8 *dir)
820 if (strlen ((char *) dir) == 0)
823 parent = FcStrDirname (dir);
826 if (access ((char *) parent, F_OK) == 0)
827 ret = mkdir ((char *) dir, 0755) == 0 && chmod ((char *) dir, 0755) == 0;
828 else if (access ((char *) parent, F_OK) == -1)
829 ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0755) == 0) && chmod ((char *) dir, 0755) == 0;
836 /* write serialized state to the cache file */
838 FcDirCacheWrite (FcCache *cache, FcConfig *config)
840 FcChar8 *dir = FcCacheDir (cache);
841 FcChar8 cache_base[CACHEBASE_LEN];
842 FcChar8 *cache_hashed;
846 FcChar8 *cache_dir = NULL;
849 struct stat cache_stat;
854 * Write it to the first directory in the list which is writable
857 list = FcStrListCreate (config->cacheDirs);
860 while ((test_dir = FcStrListNext (list))) {
861 if (access ((char *) test_dir, W_OK) == 0)
863 cache_dir = test_dir;
869 * If the directory doesn't exist, try to create it
871 if (access ((char *) test_dir, F_OK) == -1) {
872 if (FcMakeDirectory (test_dir))
874 cache_dir = test_dir;
875 /* Create CACHEDIR.TAG */
876 FcDirCacheCreateTagFile (cache_dir);
881 * Otherwise, try making it writable
883 else if (chmod ((char *) test_dir, 0755) == 0)
885 cache_dir = test_dir;
886 /* Try to create CACHEDIR.TAG too */
887 FcDirCacheCreateTagFile (cache_dir);
892 FcStrListDone (list);
896 FcDirCacheBasename (dir, cache_base);
897 cache_hashed = FcStrPlus (cache_dir, cache_base);
901 if (FcDebug () & FC_DBG_CACHE)
902 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
905 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
909 if (!FcAtomicLock (atomic))
912 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
916 /* Temporarily switch magic to MMAP while writing to file */
917 magic = cache->magic;
918 if (magic != FC_CACHE_MAGIC_MMAP)
919 cache->magic = FC_CACHE_MAGIC_MMAP;
922 * Write cache contents to file
924 written = write (fd, cache, cache->size);
926 /* Switch magic back */
927 if (magic != FC_CACHE_MAGIC_MMAP)
928 cache->magic = magic;
930 if (written != cache->size)
932 perror ("write cache");
937 if (!FcAtomicReplaceOrig(atomic))
940 /* If the file is small, update the cache chain entry such that the
941 * new cache file is not read again. If it's large, we don't do that
942 * such that we reload it, using mmap, which is shared across processes.
944 if (cache->size < FC_CACHE_MIN_MMAP &&
945 (skip = FcCacheFindByAddr (cache)) &&
946 FcStat (cache_hashed, &cache_stat))
948 skip->cache_dev = cache_stat.st_dev;
949 skip->cache_ino = cache_stat.st_ino;
950 skip->cache_mtime = cache_stat.st_mtime;
953 FcStrFree (cache_hashed);
954 FcAtomicUnlock (atomic);
955 FcAtomicDestroy (atomic);
961 FcAtomicUnlock (atomic);
963 FcAtomicDestroy (atomic);
965 FcStrFree (cache_hashed);
970 FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose)
978 struct stat target_stat;
980 dir_base = FcStrPlus (cache_dir, (FcChar8 *) FC_DIR_SEPARATOR_S);
983 fprintf (stderr, "Fontconfig error: %s: out of memory\n", cache_dir);
986 if (access ((char *) cache_dir, W_OK) != 0)
988 if (verbose || FcDebug () & FC_DBG_CACHE)
989 printf ("%s: not cleaning %s cache directory\n", cache_dir,
990 access ((char *) cache_dir, F_OK) == 0 ? "unwritable" : "non-existent");
993 if (verbose || FcDebug () & FC_DBG_CACHE)
994 printf ("%s: cleaning cache directory\n", cache_dir);
995 d = opendir ((char *) cache_dir);
998 perror ((char *) cache_dir);
1002 while ((ent = readdir (d)))
1005 const FcChar8 *target_dir;
1007 if (ent->d_name[0] == '.')
1009 /* skip cache files for different architectures and */
1010 /* files which are not cache files at all */
1011 if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) ||
1012 strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX))
1015 file_name = FcStrPlus (dir_base, (FcChar8 *) ent->d_name);
1018 fprintf (stderr, "Fontconfig error: %s: allocation failure\n", cache_dir);
1023 cache = FcDirCacheLoadFile (file_name, NULL);
1026 if (verbose || FcDebug () & FC_DBG_CACHE)
1027 printf ("%s: invalid cache file: %s\n", cache_dir, ent->d_name);
1032 target_dir = FcCacheDir (cache);
1033 if (stat ((char *) target_dir, &target_stat) < 0)
1035 if (verbose || FcDebug () & FC_DBG_CACHE)
1036 printf ("%s: %s: missing directory: %s \n",
1037 cache_dir, ent->d_name, target_dir);
1043 if (unlink ((char *) file_name) < 0)
1045 perror ((char *) file_name);
1049 FcDirCacheUnload (cache);
1050 FcStrFree (file_name);
1055 FcStrFree (dir_base);
1061 * Hokey little macro trick to permit the definitions of C functions
1062 * with the same name as CPP macros
1064 #define args1(x) (x)
1065 #define args2(x,y) (x,y)
1068 FcCacheDir args1(const FcCache *c)
1070 return FcCacheDir (c);
1074 FcCacheCopySet args1(const FcCache *c)
1076 FcFontSet *old = FcCacheSet (c);
1077 FcFontSet *new = FcFontSetCreate ();
1082 for (i = 0; i < old->nfont; i++)
1084 FcPattern *font = FcFontSetFont (old, i);
1086 FcPatternReference (font);
1087 if (!FcFontSetAdd (new, font))
1089 FcFontSetDestroy (new);
1097 FcCacheSubdir args2(const FcCache *c, int i)
1099 return FcCacheSubdir (c, i);
1103 FcCacheNumSubdir args1(const FcCache *c)
1105 return c->dirs_count;
1109 FcCacheNumFont args1(const FcCache *c)
1111 return FcCacheSet(c)->nfont;
1115 * This code implements the MD5 message-digest algorithm.
1116 * The algorithm is due to Ron Rivest. This code was
1117 * written by Colin Plumb in 1993, no copyright is claimed.
1118 * This code is in the public domain; do with it what you wish.
1120 * Equivalent code is available from RSA Data Security, Inc.
1121 * This code has been tested against that, and is equivalent,
1122 * except that you don't need to include two pages of legalese
1125 * To compute the message digest of a chunk of bytes, declare an
1126 * MD5Context structure, pass it to MD5Init, call MD5Update as
1127 * needed on buffers full of bytes, and then call MD5Final, which
1128 * will fill a supplied 16-byte array with the digest.
1132 #define byteReverse(buf, len) /* Nothing */
1135 * Note: this code is harmless on little-endian machines.
1137 void byteReverse(unsigned char *buf, unsigned longs)
1141 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1142 ((unsigned) buf[1] << 8 | buf[0]);
1143 *(FcChar32 *) buf = t;
1150 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1151 * initialization constants.
1153 static void MD5Init(struct MD5Context *ctx)
1155 ctx->buf[0] = 0x67452301;
1156 ctx->buf[1] = 0xefcdab89;
1157 ctx->buf[2] = 0x98badcfe;
1158 ctx->buf[3] = 0x10325476;
1165 * Update context to reflect the concatenation of another buffer full
1168 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len)
1172 /* Update bitcount */
1175 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1176 ctx->bits[1]++; /* Carry from low to high */
1177 ctx->bits[1] += len >> 29;
1179 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1181 /* Handle any leading odd-sized chunks */
1184 unsigned char *p = (unsigned char *) ctx->in + t;
1188 memcpy(p, buf, len);
1192 byteReverse(ctx->in, 16);
1193 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1197 /* Process data in 64-byte chunks */
1200 memcpy(ctx->in, buf, 64);
1201 byteReverse(ctx->in, 16);
1202 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1207 /* Handle any remaining bytes of data. */
1209 memcpy(ctx->in, buf, len);
1213 * Final wrapup - pad to 64-byte boundary with the bit pattern
1214 * 1 0* (64-bit count of bits processed, MSB-first)
1216 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1221 /* Compute number of bytes mod 64 */
1222 count = (ctx->bits[0] >> 3) & 0x3F;
1224 /* Set the first char of padding to 0x80. This is safe since there is
1225 always at least one byte free */
1226 p = ctx->in + count;
1229 /* Bytes of padding needed to make 64 bytes */
1230 count = 64 - 1 - count;
1232 /* Pad out to 56 mod 64 */
1234 /* Two lots of padding: Pad the first block to 64 bytes */
1235 memset(p, 0, count);
1236 byteReverse(ctx->in, 16);
1237 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1239 /* Now fill the next block with 56 bytes */
1240 memset(ctx->in, 0, 56);
1242 /* Pad block to 56 bytes */
1243 memset(p, 0, count - 8);
1245 byteReverse(ctx->in, 14);
1247 /* Append length in bits and transform */
1248 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1249 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1251 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1252 byteReverse((unsigned char *) ctx->buf, 4);
1253 memcpy(digest, ctx->buf, 16);
1254 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
1258 /* The four core functions - F1 is optimized somewhat */
1260 /* #define F1(x, y, z) (x & y | ~x & z) */
1261 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1262 #define F2(x, y, z) F1(z, x, y)
1263 #define F3(x, y, z) (x ^ y ^ z)
1264 #define F4(x, y, z) (y ^ (x | ~z))
1266 /* This is the central step in the MD5 algorithm. */
1267 #define MD5STEP(f, w, x, y, z, data, s) \
1268 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1271 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1272 * reflect the addition of 16 longwords of new data. MD5Update blocks
1273 * the data and converts bytes into longwords for this routine.
1275 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1277 register FcChar32 a, b, c, d;
1284 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1285 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1286 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1287 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1288 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1289 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1290 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1291 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1292 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1293 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1294 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1295 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1296 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1297 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1298 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1299 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1301 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1302 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1303 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1304 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1305 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1306 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1307 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1308 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1309 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1310 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1311 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1312 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1313 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1314 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1315 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1316 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1318 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1319 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1320 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1321 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1322 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1323 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1324 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1325 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1326 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1327 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1328 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1329 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1330 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1331 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1332 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1333 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1335 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1336 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1337 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1338 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1339 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1340 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1341 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1342 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1343 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1344 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1345 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1346 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1347 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1348 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1349 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1350 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1359 FcDirCacheCreateTagFile (const FcChar8 *cache_dir)
1365 static const FcChar8 cache_tag_contents[] =
1366 "Signature: 8a477f597d28d172789f06886806bc55\n"
1367 "# This file is a cache directory tag created by fontconfig.\n"
1368 "# For information about cache directory tags, see:\n"
1369 "# http://www.brynosaurus.com/cachedir/\n";
1370 static size_t cache_tag_contents_size = sizeof (cache_tag_contents) - 1;
1371 FcBool ret = FcFalse;
1376 if (access ((char *) cache_dir, W_OK) == 0)
1378 /* Create CACHEDIR.TAG */
1379 cache_tag = FcStrPlus (cache_dir, (const FcChar8 *) FC_DIR_SEPARATOR_S "CACHEDIR.TAG");
1382 atomic = FcAtomicCreate ((FcChar8 *)cache_tag);
1385 if (!FcAtomicLock (atomic))
1387 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0644);
1390 fp = fdopen(fd, "wb");
1394 fwrite(cache_tag_contents, cache_tag_contents_size, sizeof (FcChar8), fp);
1397 if (!FcAtomicReplaceOrig(atomic))
1402 FcAtomicUnlock (atomic);
1404 FcAtomicDestroy (atomic);
1406 FcStrFree (cache_tag);
1409 if (FcDebug () & FC_DBG_CACHE)
1412 printf ("Created CACHEDIR.TAG at %s\n", cache_dir);
1414 printf ("Unable to create CACHEDIR.TAG at %s\n", cache_dir);
1421 FcCacheCreateTagFile (const FcConfig *config)
1423 FcChar8 *cache_dir = NULL;
1426 list = FcConfigGetCacheDirs (config);
1430 while ((cache_dir = FcStrListNext (list)))
1432 if (FcDirCacheCreateTagFile (cache_dir))
1435 FcStrListDone (list);
1439 #include "fcaliastail.h"