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)
62 static FcBool is_initialized = FcFalse;
63 static FcBool is_env_available = FcFalse;
64 static FcBool use_mmap = FcFalse;
70 env = getenv ("FONTCONFIG_USE_MMAP");
73 if (FcNameBool ((const FcChar8 *)env, &use_mmap))
74 is_env_available = FcTrue;
76 is_initialized = FcTrue;
81 return FcIsFsMmapSafe (fd);
84 static const char bin2hex[] = { '0', '1', '2', '3',
90 FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN])
92 unsigned char hash[16];
95 struct MD5Context ctx;
98 MD5Update (&ctx, (const unsigned char *)dir, strlen ((const char *) dir));
100 MD5Final (hash, &ctx);
103 hex_hash = cache_base + 1;
104 for (cnt = 0; cnt < 16; ++cnt)
106 hex_hash[2*cnt ] = bin2hex[hash[cnt] >> 4];
107 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
110 strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX);
116 FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
118 FcChar8 *cache_hashed = NULL;
119 FcChar8 cache_base[CACHEBASE_LEN];
123 FcDirCacheBasename (dir, cache_base);
125 list = FcStrListCreate (config->cacheDirs);
129 while ((cache_dir = FcStrListNext (list)))
131 cache_hashed = FcStrPlus (cache_dir, cache_base);
134 (void) unlink ((char *) cache_hashed);
135 FcStrFree (cache_hashed);
137 FcStrListDone (list);
138 /* return FcFalse if something went wrong */
145 FcDirCacheOpenFile (const FcChar8 *cache_file, struct stat *file_stat)
150 if (FcStat (cache_file, file_stat) < 0)
153 fd = open((char *) cache_file, O_RDONLY | O_BINARY);
157 if (fstat (fd, file_stat) < 0)
167 * Look for a cache file for the specified dir. Attempt
168 * to use each one we find, stopping when the callback
172 FcDirCacheProcess (FcConfig *config, const FcChar8 *dir,
173 FcBool (*callback) (int fd, struct stat *fd_stat,
174 struct stat *dir_stat, void *closure),
175 void *closure, FcChar8 **cache_file_ret)
178 FcChar8 cache_base[CACHEBASE_LEN];
181 struct stat file_stat, dir_stat;
182 FcBool ret = FcFalse;
184 if (FcStatChecksum (dir, &dir_stat) < 0)
187 FcDirCacheBasename (dir, cache_base);
189 list = FcStrListCreate (config->cacheDirs);
193 while ((cache_dir = FcStrListNext (list)))
195 FcChar8 *cache_hashed = FcStrPlus (cache_dir, cache_base);
198 fd = FcDirCacheOpenFile (cache_hashed, &file_stat);
200 ret = (*callback) (fd, &file_stat, &dir_stat, closure);
205 *cache_file_ret = cache_hashed;
207 FcStrFree (cache_hashed);
211 FcStrFree (cache_hashed);
213 FcStrListDone (list);
218 #define FC_CACHE_MIN_MMAP 1024
221 * Skip list element, make sure the 'next' pointer is the last thing
222 * in the structure, it will be allocated large enough to hold all
223 * of the necessary pointers
226 typedef struct _FcCacheSkip FcCacheSkip;
228 struct _FcCacheSkip {
235 FcCacheSkip *next[1];
239 * The head of the skip list; pointers for every possible level
240 * in the skip list, plus the largest level in the list
243 #define FC_CACHE_MAX_LEVEL 16
245 static FcCacheSkip *fcCacheChains[FC_CACHE_MAX_LEVEL];
246 static int fcCacheMaxLevel;
255 static struct random_data fcrandbuf;
256 static char statebuf[256];
257 static FcBool initialized = FcFalse;
259 if (initialized != FcTrue)
261 initstate_r(time(NULL), statebuf, 256, &fcrandbuf);
262 initialized = FcTrue;
265 random_r(&fcrandbuf, &result);
267 static char statebuf[256];
269 static FcBool initialized = FcFalse;
271 if (initialized != FcTrue)
273 state = initstate(time(NULL), statebuf, 256);
274 initialized = FcTrue;
277 state = setstate(statebuf);
285 static unsigned int seed = time(NULL);
287 result = rand_r(&seed);
289 static FcBool initialized = FcFalse;
291 if (initialized != FcTrue)
294 initialized = FcTrue;
298 # error no random number generator function available.
305 * Generate a random level number, distributed
306 * so that each level is 1/4 as likely as the one before
308 * Note that level numbers run 1 <= level <= MAX_LEVEL
313 /* tricky bit -- each bit is '1' 75% of the time */
314 long int bits = FcRandom () | FcRandom ();
317 while (++level < FC_CACHE_MAX_LEVEL)
327 * Insert cache into the list
330 FcCacheInsert (FcCache *cache, struct stat *cache_stat)
332 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
333 FcCacheSkip *s, **next;
337 * Find links along each chain
339 next = fcCacheChains;
340 for (i = fcCacheMaxLevel; --i >= 0; )
342 for (; (s = next[i]); next = s->next)
343 if (s->cache > cache)
345 update[i] = &next[i];
349 * Create new list element
351 level = random_level ();
352 if (level > fcCacheMaxLevel)
354 level = fcCacheMaxLevel + 1;
355 update[fcCacheMaxLevel] = &fcCacheChains[fcCacheMaxLevel];
356 fcCacheMaxLevel = level;
359 s = malloc (sizeof (FcCacheSkip) + (level - 1) * sizeof (FcCacheSkip *));
364 s->size = cache->size;
368 s->cache_dev = cache_stat->st_dev;
369 s->cache_ino = cache_stat->st_ino;
370 s->cache_mtime = cache_stat->st_mtime;
380 * Insert into all fcCacheChains
382 for (i = 0; i < level; i++)
384 s->next[i] = *update[i];
391 FcCacheFindByAddr (void *object)
394 FcCacheSkip **next = fcCacheChains;
398 * Walk chain pointers one level at a time
400 for (i = fcCacheMaxLevel; --i >= 0;)
401 while (next[i] && (char *) object >= ((char *) next[i]->cache + next[i]->size))
402 next = next[i]->next;
407 if (s && (char *) object < ((char *) s->cache + s->size))
413 FcCacheRemove (FcCache *cache)
415 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
416 FcCacheSkip *s, **next;
420 * Find links along each chain
422 next = fcCacheChains;
423 for (i = fcCacheMaxLevel; --i >= 0; )
425 for (; (s = next[i]); next = s->next)
426 if (s->cache >= cache)
428 update[i] = &next[i];
431 for (i = 0; i < fcCacheMaxLevel && *update[i] == s; i++)
432 *update[i] = s->next[i];
433 while (fcCacheMaxLevel > 0 && fcCacheChains[fcCacheMaxLevel - 1] == NULL)
439 FcCacheFindByStat (struct stat *cache_stat)
443 for (s = fcCacheChains[0]; s; s = s->next[0])
444 if (s->cache_dev == cache_stat->st_dev &&
445 s->cache_ino == cache_stat->st_ino &&
446 s->cache_mtime == cache_stat->st_mtime)
455 FcDirCacheDispose (FcCache *cache)
457 switch (cache->magic) {
458 case FC_CACHE_MAGIC_ALLOC:
461 case FC_CACHE_MAGIC_MMAP:
462 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
463 munmap (cache, cache->size);
464 #elif defined(_WIN32)
465 UnmapViewOfFile (cache);
469 FcCacheRemove (cache);
473 FcCacheObjectReference (void *object)
475 FcCacheSkip *skip = FcCacheFindByAddr (object);
482 FcCacheObjectDereference (void *object)
484 FcCacheSkip *skip = FcCacheFindByAddr (object);
490 FcDirCacheDispose (skip->cache);
499 for (i = 0; i < FC_CACHE_MAX_LEVEL; i++)
500 assert (fcCacheChains[i] == NULL);
501 assert (fcCacheMaxLevel == 0);
505 FcCacheTimeValid (FcCache *cache, struct stat *dir_stat)
507 struct stat dir_static;
511 if (FcStatChecksum (FcCacheDir (cache), &dir_static) < 0)
513 dir_stat = &dir_static;
515 if (FcDebug () & FC_DBG_CACHE)
516 printf ("FcCacheTimeValid dir \"%s\" cache checksum %d dir checksum %d\n",
517 FcCacheDir (cache), cache->checksum, (int) dir_stat->st_mtime);
518 return cache->checksum == (int) dir_stat->st_mtime;
522 * Map a cache file into memory
525 FcDirCacheMapFd (int fd, struct stat *fd_stat, struct stat *dir_stat)
528 FcBool allocated = FcFalse;
530 if (fd_stat->st_size < sizeof (FcCache))
532 cache = FcCacheFindByStat (fd_stat);
535 if (FcCacheTimeValid (cache, dir_stat))
537 FcDirCacheUnload (cache);
542 * Large cache files are mmap'ed, smaller cache files are read. This
543 * balances the system cost of mmap against per-process memory usage.
545 if (FcCacheIsMmapSafe (fd) && fd_stat->st_size >= FC_CACHE_MIN_MMAP)
547 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
548 cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0);
549 #ifdef HAVE_POSIX_FADVISE
550 posix_fadvise (fd, 0, fd_stat->st_size, POSIX_FADV_WILLNEED);
552 if (cache == MAP_FAILED)
554 #elif defined(_WIN32)
559 hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL,
560 PAGE_READONLY, 0, 0, NULL);
561 if (hFileMap != NULL)
563 cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0,
565 CloseHandle (hFileMap);
572 cache = malloc (fd_stat->st_size);
576 if (read (fd, cache, fd_stat->st_size) != fd_stat->st_size)
583 if (cache->magic != FC_CACHE_MAGIC_MMAP ||
584 cache->version < FC_CACHE_CONTENT_VERSION ||
585 cache->size != fd_stat->st_size ||
586 !FcCacheTimeValid (cache, dir_stat) ||
587 !FcCacheInsert (cache, fd_stat))
593 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
594 munmap (cache, fd_stat->st_size);
595 #elif defined(_WIN32)
596 UnmapViewOfFile (cache);
602 /* Mark allocated caches so they're freed rather than unmapped */
604 cache->magic = FC_CACHE_MAGIC_ALLOC;
610 FcDirCacheReference (FcCache *cache, int nref)
612 FcCacheSkip *skip = FcCacheFindByAddr (cache);
619 FcDirCacheUnload (FcCache *cache)
621 FcCacheObjectDereference (cache);
625 FcDirCacheMapHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
627 FcCache *cache = FcDirCacheMapFd (fd, fd_stat, dir_stat);
631 *((FcCache **) closure) = cache;
636 FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file)
638 FcCache *cache = NULL;
640 if (!FcDirCacheProcess (config, dir,
648 FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat)
652 struct stat my_file_stat;
655 file_stat = &my_file_stat;
656 fd = FcDirCacheOpenFile (cache_file, file_stat);
659 cache = FcDirCacheMapFd (fd, file_stat, NULL);
665 * Validate a cache file by reading the header and checking
666 * the magic number and the size field
669 FcDirCacheValidateHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure FC_UNUSED)
674 if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache))
676 else if (c.magic != FC_CACHE_MAGIC_MMAP)
678 else if (c.version < FC_CACHE_CONTENT_VERSION)
680 else if (fd_stat->st_size != c.size)
682 else if (c.checksum != (int) dir_stat->st_mtime)
688 FcDirCacheValidConfig (const FcChar8 *dir, FcConfig *config)
690 return FcDirCacheProcess (config, dir,
691 FcDirCacheValidateHelper,
696 FcDirCacheValid (const FcChar8 *dir)
700 config = FcConfigGetCurrent ();
704 return FcDirCacheValidConfig (dir, config);
708 * Build a cache structure from the given contents
711 FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcStrSet *dirs)
713 FcSerialize *serialize = FcSerializeCreate ();
716 FcChar8 *dir_serialize;
717 intptr_t *dirs_serialize;
718 FcFontSet *set_serialize;
723 * Space for cache structure
725 FcSerializeReserve (serialize, sizeof (FcCache));
729 if (!FcStrSerializeAlloc (serialize, dir))
734 FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *));
735 for (i = 0; i < dirs->num; i++)
736 if (!FcStrSerializeAlloc (serialize, dirs->strs[i]))
742 if (!FcFontSetSerializeAlloc (serialize, set))
745 /* Serialize layout complete. Now allocate space and fill it */
746 cache = malloc (serialize->size);
749 /* shut up valgrind */
750 memset (cache, 0, serialize->size);
752 serialize->linear = cache;
754 cache->magic = FC_CACHE_MAGIC_ALLOC;
755 cache->version = FC_CACHE_CONTENT_VERSION;
756 cache->size = serialize->size;
757 cache->checksum = (int) dir_stat->st_mtime;
760 * Serialize directory name
762 dir_serialize = FcStrSerialize (serialize, dir);
765 cache->dir = FcPtrToOffset (cache, dir_serialize);
770 dirs_serialize = FcSerializePtr (serialize, dirs);
773 cache->dirs = FcPtrToOffset (cache, dirs_serialize);
774 cache->dirs_count = dirs->num;
775 for (i = 0; i < dirs->num; i++)
777 FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]);
780 dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize);
786 set_serialize = FcFontSetSerialize (serialize, set);
789 cache->set = FcPtrToOffset (cache, set_serialize);
791 FcSerializeDestroy (serialize);
793 FcCacheInsert (cache, NULL);
800 FcSerializeDestroy (serialize);
806 #define mkdir(path,mode) _mkdir(path)
810 FcMakeDirectory (const FcChar8 *dir)
815 if (strlen ((char *) dir) == 0)
818 parent = FcStrDirname (dir);
821 if (access ((char *) parent, F_OK) == 0)
822 ret = mkdir ((char *) dir, 0755) == 0 && chmod ((char *) dir, 0755) == 0;
823 else if (access ((char *) parent, F_OK) == -1)
824 ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0755) == 0) && chmod ((char *) dir, 0755) == 0;
831 /* write serialized state to the cache file */
833 FcDirCacheWrite (FcCache *cache, FcConfig *config)
835 FcChar8 *dir = FcCacheDir (cache);
836 FcChar8 cache_base[CACHEBASE_LEN];
837 FcChar8 *cache_hashed;
841 FcChar8 *cache_dir = NULL;
844 struct stat cache_stat;
849 * Write it to the first directory in the list which is writable
852 list = FcStrListCreate (config->cacheDirs);
855 while ((test_dir = FcStrListNext (list))) {
856 if (access ((char *) test_dir, W_OK) == 0)
858 cache_dir = test_dir;
864 * If the directory doesn't exist, try to create it
866 if (access ((char *) test_dir, F_OK) == -1) {
867 if (FcMakeDirectory (test_dir))
869 cache_dir = test_dir;
870 /* Create CACHEDIR.TAG */
871 FcDirCacheCreateTagFile (cache_dir);
876 * Otherwise, try making it writable
878 else if (chmod ((char *) test_dir, 0755) == 0)
880 cache_dir = test_dir;
881 /* Try to create CACHEDIR.TAG too */
882 FcDirCacheCreateTagFile (cache_dir);
887 FcStrListDone (list);
891 FcDirCacheBasename (dir, cache_base);
892 cache_hashed = FcStrPlus (cache_dir, cache_base);
896 if (FcDebug () & FC_DBG_CACHE)
897 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
900 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
904 if (!FcAtomicLock (atomic))
907 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
911 /* Temporarily switch magic to MMAP while writing to file */
912 magic = cache->magic;
913 if (magic != FC_CACHE_MAGIC_MMAP)
914 cache->magic = FC_CACHE_MAGIC_MMAP;
917 * Write cache contents to file
919 written = write (fd, cache, cache->size);
921 /* Switch magic back */
922 if (magic != FC_CACHE_MAGIC_MMAP)
923 cache->magic = magic;
925 if (written != cache->size)
927 perror ("write cache");
932 if (!FcAtomicReplaceOrig(atomic))
935 /* If the file is small, update the cache chain entry such that the
936 * new cache file is not read again. If it's large, we don't do that
937 * such that we reload it, using mmap, which is shared across processes.
939 if (cache->size < FC_CACHE_MIN_MMAP &&
940 (skip = FcCacheFindByAddr (cache)) &&
941 FcStat (cache_hashed, &cache_stat))
943 skip->cache_dev = cache_stat.st_dev;
944 skip->cache_ino = cache_stat.st_ino;
945 skip->cache_mtime = cache_stat.st_mtime;
948 FcStrFree (cache_hashed);
949 FcAtomicUnlock (atomic);
950 FcAtomicDestroy (atomic);
956 FcAtomicUnlock (atomic);
958 FcAtomicDestroy (atomic);
960 FcStrFree (cache_hashed);
965 FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose)
973 struct stat target_stat;
975 dir_base = FcStrPlus (cache_dir, (FcChar8 *) FC_DIR_SEPARATOR_S);
978 fprintf (stderr, "Fontconfig error: %s: out of memory\n", cache_dir);
981 if (access ((char *) cache_dir, W_OK) != 0)
983 if (verbose || FcDebug () & FC_DBG_CACHE)
984 printf ("%s: not cleaning %s cache directory\n", cache_dir,
985 access ((char *) cache_dir, F_OK) == 0 ? "unwritable" : "non-existent");
988 if (verbose || FcDebug () & FC_DBG_CACHE)
989 printf ("%s: cleaning cache directory\n", cache_dir);
990 d = opendir ((char *) cache_dir);
993 perror ((char *) cache_dir);
997 while ((ent = readdir (d)))
1000 const FcChar8 *target_dir;
1002 if (ent->d_name[0] == '.')
1004 /* skip cache files for different architectures and */
1005 /* files which are not cache files at all */
1006 if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) ||
1007 strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX))
1010 file_name = FcStrPlus (dir_base, (FcChar8 *) ent->d_name);
1013 fprintf (stderr, "Fontconfig error: %s: allocation failure\n", cache_dir);
1018 cache = FcDirCacheLoadFile (file_name, NULL);
1021 if (verbose || FcDebug () & FC_DBG_CACHE)
1022 printf ("%s: invalid cache file: %s\n", cache_dir, ent->d_name);
1027 target_dir = FcCacheDir (cache);
1028 if (stat ((char *) target_dir, &target_stat) < 0)
1030 if (verbose || FcDebug () & FC_DBG_CACHE)
1031 printf ("%s: %s: missing directory: %s \n",
1032 cache_dir, ent->d_name, target_dir);
1038 if (unlink ((char *) file_name) < 0)
1040 perror ((char *) file_name);
1044 FcDirCacheUnload (cache);
1045 FcStrFree (file_name);
1050 FcStrFree (dir_base);
1056 * Hokey little macro trick to permit the definitions of C functions
1057 * with the same name as CPP macros
1059 #define args1(x) (x)
1060 #define args2(x,y) (x,y)
1063 FcCacheDir args1(const FcCache *c)
1065 return FcCacheDir (c);
1069 FcCacheCopySet args1(const FcCache *c)
1071 FcFontSet *old = FcCacheSet (c);
1072 FcFontSet *new = FcFontSetCreate ();
1077 for (i = 0; i < old->nfont; i++)
1079 FcPattern *font = FcFontSetFont (old, i);
1081 FcPatternReference (font);
1082 if (!FcFontSetAdd (new, font))
1084 FcFontSetDestroy (new);
1092 FcCacheSubdir args2(const FcCache *c, int i)
1094 return FcCacheSubdir (c, i);
1098 FcCacheNumSubdir args1(const FcCache *c)
1100 return c->dirs_count;
1104 FcCacheNumFont args1(const FcCache *c)
1106 return FcCacheSet(c)->nfont;
1110 * This code implements the MD5 message-digest algorithm.
1111 * The algorithm is due to Ron Rivest. This code was
1112 * written by Colin Plumb in 1993, no copyright is claimed.
1113 * This code is in the public domain; do with it what you wish.
1115 * Equivalent code is available from RSA Data Security, Inc.
1116 * This code has been tested against that, and is equivalent,
1117 * except that you don't need to include two pages of legalese
1120 * To compute the message digest of a chunk of bytes, declare an
1121 * MD5Context structure, pass it to MD5Init, call MD5Update as
1122 * needed on buffers full of bytes, and then call MD5Final, which
1123 * will fill a supplied 16-byte array with the digest.
1127 #define byteReverse(buf, len) /* Nothing */
1130 * Note: this code is harmless on little-endian machines.
1132 void byteReverse(unsigned char *buf, unsigned longs)
1136 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1137 ((unsigned) buf[1] << 8 | buf[0]);
1138 *(FcChar32 *) buf = t;
1145 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1146 * initialization constants.
1148 static void MD5Init(struct MD5Context *ctx)
1150 ctx->buf[0] = 0x67452301;
1151 ctx->buf[1] = 0xefcdab89;
1152 ctx->buf[2] = 0x98badcfe;
1153 ctx->buf[3] = 0x10325476;
1160 * Update context to reflect the concatenation of another buffer full
1163 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len)
1167 /* Update bitcount */
1170 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1171 ctx->bits[1]++; /* Carry from low to high */
1172 ctx->bits[1] += len >> 29;
1174 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1176 /* Handle any leading odd-sized chunks */
1179 unsigned char *p = (unsigned char *) ctx->in + t;
1183 memcpy(p, buf, len);
1187 byteReverse(ctx->in, 16);
1188 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1192 /* Process data in 64-byte chunks */
1195 memcpy(ctx->in, buf, 64);
1196 byteReverse(ctx->in, 16);
1197 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1202 /* Handle any remaining bytes of data. */
1204 memcpy(ctx->in, buf, len);
1208 * Final wrapup - pad to 64-byte boundary with the bit pattern
1209 * 1 0* (64-bit count of bits processed, MSB-first)
1211 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1216 /* Compute number of bytes mod 64 */
1217 count = (ctx->bits[0] >> 3) & 0x3F;
1219 /* Set the first char of padding to 0x80. This is safe since there is
1220 always at least one byte free */
1221 p = ctx->in + count;
1224 /* Bytes of padding needed to make 64 bytes */
1225 count = 64 - 1 - count;
1227 /* Pad out to 56 mod 64 */
1229 /* Two lots of padding: Pad the first block to 64 bytes */
1230 memset(p, 0, count);
1231 byteReverse(ctx->in, 16);
1232 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1234 /* Now fill the next block with 56 bytes */
1235 memset(ctx->in, 0, 56);
1237 /* Pad block to 56 bytes */
1238 memset(p, 0, count - 8);
1240 byteReverse(ctx->in, 14);
1242 /* Append length in bits and transform */
1243 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1244 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1246 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1247 byteReverse((unsigned char *) ctx->buf, 4);
1248 memcpy(digest, ctx->buf, 16);
1249 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
1253 /* The four core functions - F1 is optimized somewhat */
1255 /* #define F1(x, y, z) (x & y | ~x & z) */
1256 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1257 #define F2(x, y, z) F1(z, x, y)
1258 #define F3(x, y, z) (x ^ y ^ z)
1259 #define F4(x, y, z) (y ^ (x | ~z))
1261 /* This is the central step in the MD5 algorithm. */
1262 #define MD5STEP(f, w, x, y, z, data, s) \
1263 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1266 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1267 * reflect the addition of 16 longwords of new data. MD5Update blocks
1268 * the data and converts bytes into longwords for this routine.
1270 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1272 register FcChar32 a, b, c, d;
1279 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1280 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1281 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1282 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1283 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1284 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1285 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1286 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1287 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1288 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1289 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1290 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1291 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1292 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1293 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1294 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1296 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1297 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1298 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1299 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1300 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1301 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1302 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1303 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1304 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1305 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1306 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1307 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1308 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1309 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1310 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1311 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1313 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1314 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1315 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1316 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1317 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1318 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1319 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1320 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1321 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1322 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1323 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1324 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1325 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1326 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1327 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1328 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1330 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1331 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1332 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1333 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1334 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1335 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1336 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1337 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1338 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1339 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1340 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1341 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1342 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1343 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1344 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1345 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1354 FcDirCacheCreateTagFile (const FcChar8 *cache_dir)
1360 static const FcChar8 cache_tag_contents[] =
1361 "Signature: 8a477f597d28d172789f06886806bc55\n"
1362 "# This file is a cache directory tag created by fontconfig.\n"
1363 "# For information about cache directory tags, see:\n"
1364 "# http://www.brynosaurus.com/cachedir/\n";
1365 static size_t cache_tag_contents_size = sizeof (cache_tag_contents) - 1;
1366 FcBool ret = FcFalse;
1371 if (access ((char *) cache_dir, W_OK) == 0)
1373 /* Create CACHEDIR.TAG */
1374 cache_tag = FcStrPlus (cache_dir, (const FcChar8 *) FC_DIR_SEPARATOR_S "CACHEDIR.TAG");
1377 atomic = FcAtomicCreate ((FcChar8 *)cache_tag);
1380 if (!FcAtomicLock (atomic))
1382 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0644);
1385 fp = fdopen(fd, "wb");
1389 fwrite(cache_tag_contents, cache_tag_contents_size, sizeof (FcChar8), fp);
1392 if (!FcAtomicReplaceOrig(atomic))
1397 FcAtomicUnlock (atomic);
1399 FcAtomicDestroy (atomic);
1401 FcStrFree (cache_tag);
1404 if (FcDebug () & FC_DBG_CACHE)
1407 printf ("Created CACHEDIR.TAG at %s\n", cache_dir);
1409 printf ("Unable to create CACHEDIR.TAG at %s\n", cache_dir);
1416 FcCacheCreateTagFile (const FcConfig *config)
1418 FcChar8 *cache_dir = NULL;
1421 list = FcConfigGetCacheDirs (config);
1425 while ((cache_dir = FcStrListNext (list)))
1427 if (FcDirCacheCreateTagFile (cache_dir))
1430 FcStrListDone (list);
1434 #include "fcaliastail.h"