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.
30 #include <sys/types.h>
32 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
34 # include <sys/mman.h>
48 static void MD5Init(struct MD5Context *ctx);
49 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len);
50 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
51 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]);
53 #define CACHEBASE_LEN (1 + 32 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX))
56 FcCacheIsMmapSafe (int fd)
59 MMAP_NOT_INITIALIZED = 0,
64 static void *static_status;
66 status = (intptr_t) fc_atomic_ptr_get (&static_status);
68 if (status == MMAP_NOT_INITIALIZED)
70 const char *env = getenv ("FONTCONFIG_USE_MMAP");
72 if (env && FcNameBool ((const FcChar8 *) env, &use))
73 status = use ? MMAP_USE : MMAP_DONT_USE;
75 status = MMAP_CHECK_FS;
76 (void) fc_atomic_ptr_cmpexch (&static_status, NULL, (void *) status);
79 if (status == MMAP_CHECK_FS)
80 return FcIsFsMmapSafe (fd);
82 return status == MMAP_USE;
86 static const char bin2hex[] = { '0', '1', '2', '3',
92 FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN])
94 unsigned char hash[16];
97 struct MD5Context ctx;
100 MD5Update (&ctx, (const unsigned char *)dir, strlen ((const char *) dir));
102 MD5Final (hash, &ctx);
105 hex_hash = cache_base + 1;
106 for (cnt = 0; cnt < 16; ++cnt)
108 hex_hash[2*cnt ] = bin2hex[hash[cnt] >> 4];
109 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
112 strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX);
118 FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
120 FcChar8 *cache_hashed = NULL;
121 FcChar8 cache_base[CACHEBASE_LEN];
124 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
126 FcDirCacheBasename (dir, cache_base);
128 list = FcStrListCreate (config->cacheDirs);
132 while ((cache_dir = FcStrListNext (list)))
135 cache_hashed = FcStrBuildFilename (sysroot, cache_dir, cache_base, NULL);
137 cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL);
140 (void) unlink ((char *) cache_hashed);
141 FcStrFree (cache_hashed);
143 FcStrListDone (list);
144 /* return FcFalse if something went wrong */
151 FcDirCacheOpenFile (const FcChar8 *cache_file, struct stat *file_stat)
156 if (FcStat (cache_file, file_stat) < 0)
159 fd = FcOpen((char *) cache_file, O_RDONLY | O_BINARY);
163 if (fstat (fd, file_stat) < 0)
173 * Look for a cache file for the specified dir. Attempt
174 * to use each one we find, stopping when the callback
178 FcDirCacheProcess (FcConfig *config, const FcChar8 *dir,
179 FcBool (*callback) (int fd, struct stat *fd_stat,
180 struct stat *dir_stat, void *closure),
181 void *closure, FcChar8 **cache_file_ret)
184 FcChar8 cache_base[CACHEBASE_LEN];
187 struct stat file_stat, dir_stat;
188 FcBool ret = FcFalse;
190 if (FcStatChecksum (dir, &dir_stat) < 0)
193 FcDirCacheBasename (dir, cache_base);
195 list = FcStrListCreate (config->cacheDirs);
199 while ((cache_dir = FcStrListNext (list)))
201 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
202 FcChar8 *cache_hashed;
205 cache_hashed = FcStrBuildFilename (sysroot, cache_dir, cache_base, NULL);
207 cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL);
210 fd = FcDirCacheOpenFile (cache_hashed, &file_stat);
212 ret = (*callback) (fd, &file_stat, &dir_stat, closure);
217 *cache_file_ret = cache_hashed;
219 FcStrFree (cache_hashed);
223 FcStrFree (cache_hashed);
225 FcStrListDone (list);
230 #define FC_CACHE_MIN_MMAP 1024
233 * Skip list element, make sure the 'next' pointer is the last thing
234 * in the structure, it will be allocated large enough to hold all
235 * of the necessary pointers
238 typedef struct _FcCacheSkip FcCacheSkip;
240 struct _FcCacheSkip {
247 FcCacheSkip *next[1];
251 * The head of the skip list; pointers for every possible level
252 * in the skip list, plus the largest level in the list
255 #define FC_CACHE_MAX_LEVEL 16
257 /* Protected by cache_lock below */
258 static FcCacheSkip *fcCacheChains[FC_CACHE_MAX_LEVEL];
259 static int fcCacheMaxLevel;
262 static FcMutex *cache_lock;
269 lock = fc_atomic_ptr_get (&cache_lock);
271 lock = (FcMutex *) malloc (sizeof (FcMutex));
273 if (!fc_atomic_ptr_cmpexch (&cache_lock, NULL, lock)) {
274 FcMutexFinish (lock);
279 /* Initialize random state */
289 FcMutexUnlock (cache_lock);
296 lock = fc_atomic_ptr_get (&cache_lock);
297 if (lock && fc_atomic_ptr_cmpexch (&cache_lock, lock, NULL)) {
298 FcMutexFinish (lock);
306 * Generate a random level number, distributed
307 * so that each level is 1/4 as likely as the one before
309 * Note that level numbers run 1 <= level <= MAX_LEVEL
314 /* tricky bit -- each bit is '1' 75% of the time */
315 long int bits = FcRandom () | FcRandom ();
318 while (++level < FC_CACHE_MAX_LEVEL)
328 * Insert cache into the list
331 FcCacheInsert (FcCache *cache, struct stat *cache_stat)
333 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
334 FcCacheSkip *s, **next;
340 * Find links along each chain
342 next = fcCacheChains;
343 for (i = fcCacheMaxLevel; --i >= 0; )
345 for (; (s = next[i]); next = s->next)
346 if (s->cache > cache)
348 update[i] = &next[i];
352 * Create new list element
354 level = random_level ();
355 if (level > fcCacheMaxLevel)
357 level = fcCacheMaxLevel + 1;
358 update[fcCacheMaxLevel] = &fcCacheChains[fcCacheMaxLevel];
359 fcCacheMaxLevel = level;
362 s = malloc (sizeof (FcCacheSkip) + (level - 1) * sizeof (FcCacheSkip *));
367 s->size = cache->size;
368 FcRefInit (&s->ref, 1);
371 s->cache_dev = cache_stat->st_dev;
372 s->cache_ino = cache_stat->st_ino;
373 s->cache_mtime = cache_stat->st_mtime;
383 * Insert into all fcCacheChains
385 for (i = 0; i < level; i++)
387 s->next[i] = *update[i];
396 FcCacheFindByAddrUnlocked (void *object)
399 FcCacheSkip **next = fcCacheChains;
406 * Walk chain pointers one level at a time
408 for (i = fcCacheMaxLevel; --i >= 0;)
409 while (next[i] && (char *) object >= ((char *) next[i]->cache + next[i]->size))
410 next = next[i]->next;
415 if (s && (char *) object < ((char *) s->cache + s->size))
421 FcCacheFindByAddr (void *object)
425 ret = FcCacheFindByAddrUnlocked (object);
431 FcCacheRemoveUnlocked (FcCache *cache)
433 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
434 FcCacheSkip *s, **next;
438 * Find links along each chain
440 next = fcCacheChains;
441 for (i = fcCacheMaxLevel; --i >= 0; )
443 for (; (s = next[i]); next = s->next)
444 if (s->cache >= cache)
446 update[i] = &next[i];
449 for (i = 0; i < fcCacheMaxLevel && *update[i] == s; i++)
450 *update[i] = s->next[i];
451 while (fcCacheMaxLevel > 0 && fcCacheChains[fcCacheMaxLevel - 1] == NULL)
457 FcCacheFindByStat (struct stat *cache_stat)
462 for (s = fcCacheChains[0]; s; s = s->next[0])
463 if (s->cache_dev == cache_stat->st_dev &&
464 s->cache_ino == cache_stat->st_ino &&
465 s->cache_mtime == cache_stat->st_mtime)
476 FcDirCacheDisposeUnlocked (FcCache *cache)
478 FcCacheRemoveUnlocked (cache);
480 switch (cache->magic) {
481 case FC_CACHE_MAGIC_ALLOC:
484 case FC_CACHE_MAGIC_MMAP:
485 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
486 munmap (cache, cache->size);
487 #elif defined(_WIN32)
488 UnmapViewOfFile (cache);
495 FcCacheObjectReference (void *object)
497 FcCacheSkip *skip = FcCacheFindByAddr (object);
500 FcRefInc (&skip->ref);
504 FcCacheObjectDereference (void *object)
509 skip = FcCacheFindByAddrUnlocked (object);
512 if (FcRefDec (&skip->ref) == 1)
513 FcDirCacheDisposeUnlocked (skip->cache);
523 for (i = 0; i < FC_CACHE_MAX_LEVEL; i++)
524 assert (fcCacheChains[i] == NULL);
525 assert (fcCacheMaxLevel == 0);
531 FcCacheTimeValid (FcCache *cache, struct stat *dir_stat)
533 struct stat dir_static;
537 if (FcStatChecksum (FcCacheDir (cache), &dir_static) < 0)
539 dir_stat = &dir_static;
541 if (FcDebug () & FC_DBG_CACHE)
542 printf ("FcCacheTimeValid dir \"%s\" cache checksum %d dir checksum %d\n",
543 FcCacheDir (cache), cache->checksum, (int) dir_stat->st_mtime);
544 return cache->checksum == (int) dir_stat->st_mtime;
548 * Map a cache file into memory
551 FcDirCacheMapFd (int fd, struct stat *fd_stat, struct stat *dir_stat)
554 FcBool allocated = FcFalse;
556 if (fd_stat->st_size < (int) sizeof (FcCache))
558 cache = FcCacheFindByStat (fd_stat);
561 if (FcCacheTimeValid (cache, dir_stat))
563 FcDirCacheUnload (cache);
568 * Large cache files are mmap'ed, smaller cache files are read. This
569 * balances the system cost of mmap against per-process memory usage.
571 if (FcCacheIsMmapSafe (fd) && fd_stat->st_size >= FC_CACHE_MIN_MMAP)
573 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
574 cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0);
575 #ifdef HAVE_POSIX_FADVISE
576 posix_fadvise (fd, 0, fd_stat->st_size, POSIX_FADV_WILLNEED);
578 if (cache == MAP_FAILED)
580 #elif defined(_WIN32)
585 hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL,
586 PAGE_READONLY, 0, 0, NULL);
587 if (hFileMap != NULL)
589 cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0,
591 CloseHandle (hFileMap);
598 cache = malloc (fd_stat->st_size);
602 if (read (fd, cache, fd_stat->st_size) != fd_stat->st_size)
609 if (cache->magic != FC_CACHE_MAGIC_MMAP ||
610 cache->version < FC_CACHE_CONTENT_VERSION ||
611 cache->size != (intptr_t) fd_stat->st_size ||
612 !FcCacheTimeValid (cache, dir_stat) ||
613 !FcCacheInsert (cache, fd_stat))
619 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
620 munmap (cache, fd_stat->st_size);
621 #elif defined(_WIN32)
622 UnmapViewOfFile (cache);
628 /* Mark allocated caches so they're freed rather than unmapped */
630 cache->magic = FC_CACHE_MAGIC_ALLOC;
636 FcDirCacheReference (FcCache *cache, int nref)
638 FcCacheSkip *skip = FcCacheFindByAddr (cache);
641 FcRefAdd (&skip->ref, nref);
645 FcDirCacheUnload (FcCache *cache)
647 FcCacheObjectDereference (cache);
651 FcDirCacheMapHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
653 FcCache *cache = FcDirCacheMapFd (fd, fd_stat, dir_stat);
657 *((FcCache **) closure) = cache;
662 FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file)
664 FcCache *cache = NULL;
666 if (!FcDirCacheProcess (config, dir,
674 FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat)
678 struct stat my_file_stat;
681 file_stat = &my_file_stat;
682 fd = FcDirCacheOpenFile (cache_file, file_stat);
685 cache = FcDirCacheMapFd (fd, file_stat, NULL);
691 * Validate a cache file by reading the header and checking
692 * the magic number and the size field
695 FcDirCacheValidateHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure FC_UNUSED)
700 if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache))
702 else if (c.magic != FC_CACHE_MAGIC_MMAP)
704 else if (c.version < FC_CACHE_CONTENT_VERSION)
706 else if (fd_stat->st_size != c.size)
708 else if (c.checksum != (int) dir_stat->st_mtime)
714 FcDirCacheValidConfig (const FcChar8 *dir, FcConfig *config)
716 return FcDirCacheProcess (config, dir,
717 FcDirCacheValidateHelper,
722 FcDirCacheValid (const FcChar8 *dir)
726 config = FcConfigGetCurrent ();
730 return FcDirCacheValidConfig (dir, config);
734 * Build a cache structure from the given contents
737 FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcStrSet *dirs)
739 FcSerialize *serialize = FcSerializeCreate ();
742 FcChar8 *dir_serialize;
743 intptr_t *dirs_serialize;
744 FcFontSet *set_serialize;
749 * Space for cache structure
751 FcSerializeReserve (serialize, sizeof (FcCache));
755 if (!FcStrSerializeAlloc (serialize, dir))
760 FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *));
761 for (i = 0; i < dirs->num; i++)
762 if (!FcStrSerializeAlloc (serialize, dirs->strs[i]))
768 if (!FcFontSetSerializeAlloc (serialize, set))
771 /* Serialize layout complete. Now allocate space and fill it */
772 cache = malloc (serialize->size);
775 /* shut up valgrind */
776 memset (cache, 0, serialize->size);
778 serialize->linear = cache;
780 cache->magic = FC_CACHE_MAGIC_ALLOC;
781 cache->version = FC_CACHE_CONTENT_VERSION;
782 cache->size = serialize->size;
783 cache->checksum = (int) dir_stat->st_mtime;
786 * Serialize directory name
788 dir_serialize = FcStrSerialize (serialize, dir);
791 cache->dir = FcPtrToOffset (cache, dir_serialize);
796 dirs_serialize = FcSerializePtr (serialize, dirs);
799 cache->dirs = FcPtrToOffset (cache, dirs_serialize);
800 cache->dirs_count = dirs->num;
801 for (i = 0; i < dirs->num; i++)
803 FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]);
806 dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize);
812 set_serialize = FcFontSetSerialize (serialize, set);
815 cache->set = FcPtrToOffset (cache, set_serialize);
817 FcSerializeDestroy (serialize);
819 FcCacheInsert (cache, NULL);
826 FcSerializeDestroy (serialize);
830 /* write serialized state to the cache file */
832 FcDirCacheWrite (FcCache *cache, FcConfig *config)
834 FcChar8 *dir = FcCacheDir (cache);
835 FcChar8 cache_base[CACHEBASE_LEN];
836 FcChar8 *cache_hashed;
840 FcChar8 *cache_dir = NULL;
841 FcChar8 *test_dir, *d = NULL;
843 struct stat cache_stat;
846 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
849 * Write it to the first directory in the list which is writable
852 list = FcStrListCreate (config->cacheDirs);
855 while ((test_dir = FcStrListNext (list)))
860 d = FcStrBuildFilename (sysroot, test_dir, NULL);
862 d = FcStrCopyFilename (test_dir);
864 if (access ((char *) d, W_OK) == 0)
866 cache_dir = FcStrCopyFilename (d);
872 * If the directory doesn't exist, try to create it
874 if (access ((char *) d, F_OK) == -1) {
875 if (FcMakeDirectory (d))
877 cache_dir = FcStrCopyFilename (d);
878 /* Create CACHEDIR.TAG */
879 FcDirCacheCreateTagFile (d);
884 * Otherwise, try making it writable
886 else if (chmod ((char *) d, 0755) == 0)
888 cache_dir = FcStrCopyFilename (d);
889 /* Try to create CACHEDIR.TAG too */
890 FcDirCacheCreateTagFile (d);
897 FcStrListDone (list);
901 FcDirCacheBasename (dir, cache_base);
902 cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL);
905 FcStrFree (cache_dir);
907 if (FcDebug () & FC_DBG_CACHE)
908 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
911 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
915 if (!FcAtomicLock (atomic))
918 fd = FcOpen((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
922 /* Temporarily switch magic to MMAP while writing to file */
923 magic = cache->magic;
924 if (magic != FC_CACHE_MAGIC_MMAP)
925 cache->magic = FC_CACHE_MAGIC_MMAP;
928 * Write cache contents to file
930 written = write (fd, cache, cache->size);
932 /* Switch magic back */
933 if (magic != FC_CACHE_MAGIC_MMAP)
934 cache->magic = magic;
936 if (written != cache->size)
938 perror ("write cache");
943 if (!FcAtomicReplaceOrig(atomic))
946 /* If the file is small, update the cache chain entry such that the
947 * new cache file is not read again. If it's large, we don't do that
948 * such that we reload it, using mmap, which is shared across processes.
950 if (cache->size < FC_CACHE_MIN_MMAP && FcStat (cache_hashed, &cache_stat))
953 if ((skip = FcCacheFindByAddrUnlocked (cache)))
955 skip->cache_dev = cache_stat.st_dev;
956 skip->cache_ino = cache_stat.st_ino;
957 skip->cache_mtime = cache_stat.st_mtime;
962 FcStrFree (cache_hashed);
963 FcAtomicUnlock (atomic);
964 FcAtomicDestroy (atomic);
970 FcAtomicUnlock (atomic);
972 FcAtomicDestroy (atomic);
974 FcStrFree (cache_hashed);
979 FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose)
987 struct stat target_stat;
988 const FcChar8 *sysroot;
990 /* FIXME: this API needs to support non-current FcConfig */
991 sysroot = FcConfigGetSysRoot (NULL);
993 dir = FcStrBuildFilename (sysroot, cache_dir, NULL);
995 dir = FcStrCopyFilename (cache_dir);
998 fprintf (stderr, "Fontconfig error: %s: out of memory\n", cache_dir);
1001 if (access ((char *) dir, W_OK) != 0)
1003 if (verbose || FcDebug () & FC_DBG_CACHE)
1004 printf ("%s: not cleaning %s cache directory\n", dir,
1005 access ((char *) dir, F_OK) == 0 ? "unwritable" : "non-existent");
1008 if (verbose || FcDebug () & FC_DBG_CACHE)
1009 printf ("%s: cleaning cache directory\n", dir);
1010 d = opendir ((char *) dir);
1013 perror ((char *) dir);
1017 while ((ent = readdir (d)))
1020 const FcChar8 *target_dir;
1022 if (ent->d_name[0] == '.')
1024 /* skip cache files for different architectures and */
1025 /* files which are not cache files at all */
1026 if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) ||
1027 strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX))
1030 file_name = FcStrBuildFilename (dir, (FcChar8 *)ent->d_name, NULL);
1033 fprintf (stderr, "Fontconfig error: %s: allocation failure\n", dir);
1038 cache = FcDirCacheLoadFile (file_name, NULL);
1041 if (verbose || FcDebug () & FC_DBG_CACHE)
1042 printf ("%s: invalid cache file: %s\n", dir, ent->d_name);
1047 target_dir = FcCacheDir (cache);
1048 if (stat ((char *) target_dir, &target_stat) < 0)
1050 if (verbose || FcDebug () & FC_DBG_CACHE)
1051 printf ("%s: %s: missing directory: %s \n",
1052 dir, ent->d_name, target_dir);
1055 FcDirCacheUnload (cache);
1059 if (unlink ((char *) file_name) < 0)
1061 perror ((char *) file_name);
1065 FcStrFree (file_name);
1076 * Hokey little macro trick to permit the definitions of C functions
1077 * with the same name as CPP macros
1079 #define args1(x) (x)
1080 #define args2(x,y) (x,y)
1083 FcCacheDir args1(const FcCache *c)
1085 return FcCacheDir (c);
1089 FcCacheCopySet args1(const FcCache *c)
1091 FcFontSet *old = FcCacheSet (c);
1092 FcFontSet *new = FcFontSetCreate ();
1097 for (i = 0; i < old->nfont; i++)
1099 FcPattern *font = FcFontSetFont (old, i);
1101 FcPatternReference (font);
1102 if (!FcFontSetAdd (new, font))
1104 FcFontSetDestroy (new);
1112 FcCacheSubdir args2(const FcCache *c, int i)
1114 return FcCacheSubdir (c, i);
1118 FcCacheNumSubdir args1(const FcCache *c)
1120 return c->dirs_count;
1124 FcCacheNumFont args1(const FcCache *c)
1126 return FcCacheSet(c)->nfont;
1130 * This code implements the MD5 message-digest algorithm.
1131 * The algorithm is due to Ron Rivest. This code was
1132 * written by Colin Plumb in 1993, no copyright is claimed.
1133 * This code is in the public domain; do with it what you wish.
1135 * Equivalent code is available from RSA Data Security, Inc.
1136 * This code has been tested against that, and is equivalent,
1137 * except that you don't need to include two pages of legalese
1140 * To compute the message digest of a chunk of bytes, declare an
1141 * MD5Context structure, pass it to MD5Init, call MD5Update as
1142 * needed on buffers full of bytes, and then call MD5Final, which
1143 * will fill a supplied 16-byte array with the digest.
1147 #define byteReverse(buf, len) /* Nothing */
1150 * Note: this code is harmless on little-endian machines.
1152 void byteReverse(unsigned char *buf, unsigned longs)
1156 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1157 ((unsigned) buf[1] << 8 | buf[0]);
1158 *(FcChar32 *) buf = t;
1165 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1166 * initialization constants.
1168 static void MD5Init(struct MD5Context *ctx)
1170 ctx->buf[0] = 0x67452301;
1171 ctx->buf[1] = 0xefcdab89;
1172 ctx->buf[2] = 0x98badcfe;
1173 ctx->buf[3] = 0x10325476;
1180 * Update context to reflect the concatenation of another buffer full
1183 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len)
1187 /* Update bitcount */
1190 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1191 ctx->bits[1]++; /* Carry from low to high */
1192 ctx->bits[1] += len >> 29;
1194 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1196 /* Handle any leading odd-sized chunks */
1199 unsigned char *p = (unsigned char *) ctx->in + t;
1203 memcpy(p, buf, len);
1207 byteReverse(ctx->in, 16);
1208 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1212 /* Process data in 64-byte chunks */
1215 memcpy(ctx->in, buf, 64);
1216 byteReverse(ctx->in, 16);
1217 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1222 /* Handle any remaining bytes of data. */
1224 memcpy(ctx->in, buf, len);
1228 * Final wrapup - pad to 64-byte boundary with the bit pattern
1229 * 1 0* (64-bit count of bits processed, MSB-first)
1231 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1236 /* Compute number of bytes mod 64 */
1237 count = (ctx->bits[0] >> 3) & 0x3F;
1239 /* Set the first char of padding to 0x80. This is safe since there is
1240 always at least one byte free */
1241 p = ctx->in + count;
1244 /* Bytes of padding needed to make 64 bytes */
1245 count = 64 - 1 - count;
1247 /* Pad out to 56 mod 64 */
1249 /* Two lots of padding: Pad the first block to 64 bytes */
1250 memset(p, 0, count);
1251 byteReverse(ctx->in, 16);
1252 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1254 /* Now fill the next block with 56 bytes */
1255 memset(ctx->in, 0, 56);
1257 /* Pad block to 56 bytes */
1258 memset(p, 0, count - 8);
1260 byteReverse(ctx->in, 14);
1262 /* Append length in bits and transform */
1263 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1264 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1266 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1267 byteReverse((unsigned char *) ctx->buf, 4);
1268 memcpy(digest, ctx->buf, 16);
1269 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
1273 /* The four core functions - F1 is optimized somewhat */
1275 /* #define F1(x, y, z) (x & y | ~x & z) */
1276 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1277 #define F2(x, y, z) F1(z, x, y)
1278 #define F3(x, y, z) (x ^ y ^ z)
1279 #define F4(x, y, z) (y ^ (x | ~z))
1281 /* This is the central step in the MD5 algorithm. */
1282 #define MD5STEP(f, w, x, y, z, data, s) \
1283 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1286 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1287 * reflect the addition of 16 longwords of new data. MD5Update blocks
1288 * the data and converts bytes into longwords for this routine.
1290 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1292 register FcChar32 a, b, c, d;
1299 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1300 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1301 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1302 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1303 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1304 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1305 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1306 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1307 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1308 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1309 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1310 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1311 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1312 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1313 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1314 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1316 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1317 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1318 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1319 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1320 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1321 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1322 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1323 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1324 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1325 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1326 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1327 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1328 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1329 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1330 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1331 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1333 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1334 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1335 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1336 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1337 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1338 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1339 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1340 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1341 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1342 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1343 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1344 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1345 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1346 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1347 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1348 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1350 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1351 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1352 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1353 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1354 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1355 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1356 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1357 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1358 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1359 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1360 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1361 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1362 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1363 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1364 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1365 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1374 FcDirCacheCreateTagFile (const FcChar8 *cache_dir)
1380 static const FcChar8 cache_tag_contents[] =
1381 "Signature: 8a477f597d28d172789f06886806bc55\n"
1382 "# This file is a cache directory tag created by fontconfig.\n"
1383 "# For information about cache directory tags, see:\n"
1384 "# http://www.brynosaurus.com/cachedir/\n";
1385 static size_t cache_tag_contents_size = sizeof (cache_tag_contents) - 1;
1386 FcBool ret = FcFalse;
1391 if (access ((char *) cache_dir, W_OK) == 0)
1393 /* Create CACHEDIR.TAG */
1394 cache_tag = FcStrBuildFilename (cache_dir, "CACHEDIR.TAG", NULL);
1397 atomic = FcAtomicCreate ((FcChar8 *)cache_tag);
1400 if (!FcAtomicLock (atomic))
1402 fd = FcOpen((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0644);
1405 fp = fdopen(fd, "wb");
1409 fwrite(cache_tag_contents, cache_tag_contents_size, sizeof (FcChar8), fp);
1412 if (!FcAtomicReplaceOrig(atomic))
1417 FcAtomicUnlock (atomic);
1419 FcAtomicDestroy (atomic);
1421 FcStrFree (cache_tag);
1424 if (FcDebug () & FC_DBG_CACHE)
1427 printf ("Created CACHEDIR.TAG at %s\n", cache_dir);
1429 printf ("Unable to create CACHEDIR.TAG at %s\n", cache_dir);
1436 FcCacheCreateTagFile (const FcConfig *config)
1438 FcChar8 *cache_dir = NULL, *d = NULL;
1440 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
1442 list = FcConfigGetCacheDirs (config);
1446 while ((cache_dir = FcStrListNext (list)))
1451 d = FcStrBuildFilename (sysroot, cache_dir, NULL);
1453 d = FcStrCopyFilename (cache_dir);
1454 if (FcDirCacheCreateTagFile (d))
1459 FcStrListDone (list);
1463 #include "fcaliastail.h"