2 * $RCSId: xc/lib/fontconfig/src/fccache.c,v 1.12 2002/08/22 07:36:44 keithp Exp $
4 * Copyright © 2000 Keith Packard
5 * Copyright © 2005 Patrick Lam
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of Keith Packard not be used in
12 * advertising or publicity pertaining to distribution of the software without
13 * specific, written prior permission. Keith Packard makes no
14 * representations about the suitability of this software for any purpose. It
15 * is provided "as is" without express or implied warranty.
17 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
23 * PERFORMANCE OF THIS SOFTWARE.
30 #include <sys/utsname.h>
31 #include <sys/types.h>
36 #define ENDIAN_TEST 0x12345678
37 #define MACHINE_SIGNATURE_SIZE 9 + 5*20 + 1
40 FcDirCacheOpen (const FcChar8 * dir);
43 FcDirCacheHashName (char * cache_file, int collisions);
46 FcCacheSkipToArch (int fd, const char * arch);
49 FcCacheCopyOld (int fd, int fd_orig, off_t start);
52 FcDirCacheProduce (FcFontSet *set, FcCache * metadata);
55 FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config);
58 FcCacheNextOffset(off_t w);
61 FcCacheMachineSignature (void);
64 FcCacheHaveBank (int bank);
67 FcCacheAddBankDir (int bank, const char * dir);
75 static void MD5Init(struct MD5Context *ctx);
76 static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len);
77 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
78 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]);
80 #define FC_DBG_CACHE_REF 1024
83 FcCacheReadString (int fd, char *dest, int len)
96 while (read (fd, &c, 1) == 1)
123 FcCacheSkipString (int fd)
129 while (read (fd, &c, 1) == 1)
150 FcCacheWriteString (int fd, const char *chars)
152 if (write (fd, chars, strlen(chars)+1) != strlen(chars)+1)
158 FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
160 FcStrSetDestroy (d->subdirs);
161 FcMemFree (FC_MEM_STRING, strlen (d->name)+1);
163 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir));
168 FcGlobalCacheCreate (void)
170 FcGlobalCache *cache;
172 cache = malloc (sizeof (FcGlobalCache));
175 FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCache));
177 cache->updated = FcFalse;
183 FcGlobalCacheDestroy (FcGlobalCache *cache)
185 FcGlobalCacheDir *d, *next;
187 for (d = cache->dirs; d; d = next)
190 FcGlobalCacheDirDestroy (d);
192 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache));
197 FcGlobalCacheLoad (FcGlobalCache *cache,
199 const FcChar8 *cache_file,
202 char name_buf[FC_MAX_FILE_LEN];
203 FcGlobalCacheDir *d, *next;
204 FcFileTime config_time = FcConfigModifiedTime (config);
205 char * current_arch_machine_name;
206 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
207 off_t current_arch_start;
209 struct stat cache_stat, dir_stat;
210 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
212 if (stat ((char *) cache_file, &cache_stat) < 0)
215 cache->fd = open ((char *) cache_file, O_RDONLY);
219 cache->updated = FcFalse;
221 if (!FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)))
223 if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) != 0)
226 current_arch_machine_name = FcCacheMachineSignature ();
227 current_arch_start = FcCacheSkipToArch(cache->fd,
228 current_arch_machine_name);
229 if (current_arch_start < 0)
230 goto bail_and_destroy;
232 lseek (cache->fd, current_arch_start, SEEK_SET);
233 if (!FcCacheReadString (cache->fd, candidate_arch_machine_name,
234 sizeof (candidate_arch_machine_name)))
235 goto bail_and_destroy;
236 if (strlen(candidate_arch_machine_name) == 0)
237 goto bail_and_destroy;
243 if (!FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
246 /* Directory must be older than the global cache file; also
247 cache must be newer than the config file. */
248 if (stat ((char *) name_buf, &dir_stat) < 0 ||
249 dir_stat.st_mtime > cache_stat.st_mtime ||
250 (config_time.set && cache_stat.st_mtime < config_time.time))
254 FcStrSetAdd (staleDirs, FcStrCopy ((FcChar8 *)name_buf));
255 read (cache->fd, &md, sizeof (FcCache));
256 lseek (cache->fd, FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count, SEEK_SET);
260 d = malloc (sizeof (FcGlobalCacheDir));
264 d->next = cache->dirs;
267 d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
269 d->offset = lseek (cache->fd, 0, SEEK_CUR);
271 d->subdirs = FcStrSetCreate();
272 while (strlen(FcCacheReadString (cache->fd, subdirName, sizeof (subdirName))) > 0)
273 FcStrSetAdd (d->subdirs, (FcChar8 *)subdirName);
275 if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
277 targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
278 if (lseek (cache->fd, targ, SEEK_SET) != targ)
284 for (d = cache->dirs; d; d = next)
299 if (stat ((char *) cache_file, &cache_stat) == 0)
300 unlink ((char *)cache_file);
307 FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config)
310 FcBool ret = FcFalse;
315 if (!(dir = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)dir)))
316 return FcFalse; /* non-existing directory */
318 for (d = cache->dirs; d; d = d->next)
320 if (strncmp (d->name, dir, strlen(dir)) == 0)
322 lseek (cache->fd, d->offset, SEEK_SET);
323 if (!FcDirCacheConsume (cache->fd, d->name, set, config))
325 if (strcmp (d->name, dir) == 0)
334 FcGlobalCacheUpdate (FcGlobalCache *cache,
343 name = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)name);
344 for (d = cache->dirs; d; d = d->next)
346 if (strcmp(d->name, name) == 0)
352 d = malloc (sizeof (FcGlobalCacheDir));
355 d->next = cache->dirs;
359 cache->updated = FcTrue;
361 d->name = (char *)FcStrCopy ((FcChar8 *)name);
362 d->ent = FcDirCacheProduce (set, &d->metadata);
364 d->subdirs = FcStrSetCreate();
365 for (i = 0; i < dirs->num; i++)
366 FcStrSetAdd (d->subdirs, dirs->strs[i]);
371 FcGlobalCacheSave (FcGlobalCache *cache,
372 const FcChar8 *cache_file,
376 FcGlobalCacheDir *dir;
378 off_t current_arch_start = 0, truncate_to;
379 char * current_arch_machine_name, * header;
384 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
385 /* Set-UID programs can't safely update the cache */
386 if (getuid () != geteuid ())
390 atomic = FcAtomicCreate (cache_file);
394 if (!FcAtomicLock (atomic))
396 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT,
400 FcCacheWriteString (fd, FC_GLOBAL_MAGIC_COOKIE);
402 fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY);
404 current_arch_machine_name = FcCacheMachineSignature ();
406 current_arch_start = 0;
408 current_arch_start = FcCacheSkipToArch (fd_orig,
409 current_arch_machine_name);
411 if (current_arch_start < 0)
413 off_t i = lseek(fd_orig, 0, SEEK_END);
414 if (i < strlen (FC_GLOBAL_MAGIC_COOKIE)+1)
415 i = strlen (FC_GLOBAL_MAGIC_COOKIE)+1;
416 current_arch_start = FcCacheNextOffset (i);
419 if (!FcCacheCopyOld(fd, fd_orig, current_arch_start))
425 current_arch_start = lseek(fd, 0, SEEK_CUR);
426 if (ftruncate (fd, current_arch_start) == -1)
429 header = malloc (10 + strlen (current_arch_machine_name));
433 truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11;
434 for (dir = cache->dirs; dir; dir = dir->next)
436 truncate_to += strlen(dir->name) + 1;
437 truncate_to += sizeof (FcCache);
438 truncate_to = FcCacheNextOffset (truncate_to);
439 truncate_to += dir->metadata.count;
441 for (i = 0; i < dir->subdirs->size; i++)
442 truncate_to += strlen((char *)dir->subdirs->strs[i]) + 1;
445 truncate_to -= current_arch_start;
447 sprintf (header, "%8x ", (int)truncate_to);
448 strcat (header, current_arch_machine_name);
449 if (!FcCacheWriteString (fd, header))
452 for (dir = cache->dirs; dir; dir = dir->next)
456 const char * d = (const char *)FcConfigNormalizeFontDir (config, (const FcChar8 *)dir->name);
458 FcCacheWriteString (fd, d);
460 for (i = 0; i < dir->subdirs->size; i++)
461 FcCacheWriteString (fd, (char *)dir->subdirs->strs[i]);
462 FcCacheWriteString (fd, "");
464 write (fd, &dir->metadata, sizeof(FcCache));
465 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET);
466 write (fd, dir->ent, dir->metadata.count);
470 FcCacheWriteString (fd, "");
472 if (close (fd) == -1)
475 if (!FcAtomicReplaceOrig (atomic))
478 FcAtomicUnlock (atomic);
479 FcAtomicDestroy (atomic);
481 cache->updated = FcFalse;
492 FcAtomicDeleteNew (atomic);
494 FcAtomicUnlock (atomic);
496 FcAtomicDestroy (atomic);
501 * Find the next presumably-mmapable offset after the supplied file
505 FcCacheNextOffset(off_t w)
507 static long pagesize = -1;
509 pagesize = sysconf(_SC_PAGESIZE);
510 if (w % pagesize == 0)
513 return ((w / pagesize)+1)*pagesize;
516 /* return the address of the segment for the provided arch,
517 * or -1 if arch not found */
519 FcCacheSkipToArch (int fd, const char * arch)
521 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
522 char * candidate_arch;
523 off_t current_arch_start = 0;
525 lseek (fd, 0, SEEK_SET);
526 FcCacheSkipString (fd);
527 current_arch_start = lseek (fd, 0, SEEK_CUR);
529 /* skip arches that are not the current arch */
534 if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
537 if (FcCacheReadString (fd, candidate_arch_machine_name_count,
538 sizeof (candidate_arch_machine_name_count)) == 0)
540 if (!strlen(candidate_arch_machine_name_count))
542 bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16);
544 // count = 0 should probably be distinguished from the !bs condition
545 if (!bs || bs < strlen (candidate_arch_machine_name_count))
548 candidate_arch++; /* skip leading space */
550 if (strcmp (candidate_arch, arch)==0)
551 return current_arch_start;
552 current_arch_start += bs;
558 /* Cuts out the segment at the file pointer (moves everything else
559 * down to cover it), and leaves the file pointer at the end of the
562 FcCacheCopyOld (int fd, int fd_orig, off_t start)
564 char * buf = malloc (8192);
565 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
567 int c, bytes_skipped;
574 lseek (fd, 0, SEEK_SET); lseek (fd_orig, 0, SEEK_SET);
581 if ((c = read (fd_orig, buf, b)) <= 0)
583 if (write (fd, buf, c) < 0)
590 lseek (fd, start, SEEK_SET);
591 if (FcCacheReadString (fd, candidate_arch_machine_name,
592 sizeof (candidate_arch_machine_name)) == 0)
594 if (!strlen(candidate_arch_machine_name))
597 bs = strtol(candidate_arch_machine_name, 0, 16);
604 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
605 if ((c = read (fd, buf, 8192)) <= 0)
607 lseek (fd, start+bytes_skipped, SEEK_SET);
608 if (write (fd, buf, c) < 0)
613 lseek (fd, start+bytes_skipped, SEEK_SET);
624 /* Does not check that the cache has the appropriate arch section. */
625 /* Also, this can be fooled if the original location has a stale
626 * cache, and the hashed location has an up-to-date cache. Oh well,
627 * sucks to be you in that case! */
629 FcDirCacheValid (const FcChar8 *dir)
631 struct stat file_stat, dir_stat;
634 if (stat ((char *) dir, &dir_stat) < 0)
637 fd = FcDirCacheOpen (dir);
641 if (fstat (fd, &file_stat) < 0)
647 * If the directory has been modified more recently than
648 * the cache file, the cache is not valid
650 if (dir_stat.st_mtime > file_stat.st_mtime)
660 /* Assumes that the cache file in 'dir' exists.
661 * Checks that the cache has the appropriate arch section. */
663 FcDirCacheHasCurrentArch (const FcChar8 *dir)
666 off_t current_arch_start;
667 char *current_arch_machine_name;
669 fd = FcDirCacheOpen (dir);
673 current_arch_machine_name = FcCacheMachineSignature();
674 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
677 if (current_arch_start < 0)
687 FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
690 char *cache_hashed = 0;
692 struct stat cache_stat;
693 char name_buf[FC_MAX_FILE_LEN];
695 dir = FcConfigNormalizeFontDir (config, dir);
696 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
700 /* First remove normal cache file. */
701 if (stat ((char *) cache_file, &cache_stat) == 0 &&
702 unlink ((char *)cache_file) != 0)
705 /* Next remove any applicable hashed files. */
706 fd = -1; collisions = 0;
710 FcStrFree ((FcChar8 *)cache_hashed);
712 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
718 fd = open(cache_hashed, O_RDONLY);
721 FcStrFree ((FcChar8 *)cache_file);
725 if (!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
727 FcStrFree ((FcChar8 *)cache_hashed);
730 } while (strcmp (name_buf, cache_file) != 0);
732 FcStrFree ((FcChar8 *)cache_file);
735 if (stat ((char *) cache_hashed, &cache_stat) == 0 &&
736 unlink ((char *)cache_hashed) != 0)
738 FcStrFree ((FcChar8 *)cache_hashed);
742 FcStrFree ((FcChar8 *)cache_hashed);
746 FcStrFree ((FcChar8 *)cache_file);
751 FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
752 FcStrList *list, FcFontSet * set)
756 FcChar8 *file, *base;
762 * Read in the results from 'list'.
764 while ((dir = FcStrListNext (list)))
766 if (!FcConfigAcceptFilename (config, dir))
770 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
774 strcpy ((char *) file, (char *) dir);
775 strcat ((char *) file, "/");
776 base = file + strlen ((char *) file);
778 subdirs = FcStrSetCreate ();
781 fprintf (stderr, "Can't create directory set\n");
787 if (access ((char *) dir, X_OK) < 0)
795 fprintf (stderr, "\"%s\": ", dir);
799 FcStrSetDestroy (subdirs);
803 if (stat ((char *) dir, &statb) == -1)
805 fprintf (stderr, "\"%s\": ", dir);
807 FcStrSetDestroy (subdirs);
812 if (!S_ISDIR (statb.st_mode))
814 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
815 FcStrSetDestroy (subdirs);
819 if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir, config))
821 if (FcDebug () & FC_DBG_FONTSET)
822 printf ("cache scan dir %s\n", dir);
824 FcDirScanConfig (set, subdirs, cache,
825 config->blanks, dir, FcFalse, config);
827 sublist = FcStrListCreate (subdirs);
828 FcStrSetDestroy (subdirs);
831 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
836 ret += FcCacheReadDirs (config, cache, sublist, set);
839 FcStrListDone (list);
844 FcCacheRead (FcConfig *config, FcGlobalCache * cache)
846 FcFontSet * s = FcFontSetCreate();
850 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
856 FcFontSetDestroy (s);
860 static const char bin2hex[] = { '0', '1', '2', '3',
863 'c', 'd', 'e', 'f' };
866 FcDirCacheHashName (char * cache_file, int collisions)
868 unsigned char hash[16], hex_hash[33];
870 unsigned char uscore = '_';
873 struct MD5Context ctx;
876 MD5Update (&ctx, (unsigned char *)cache_file, strlen (cache_file));
878 for (i = 0; i < collisions; i++)
879 MD5Update (&ctx, &uscore, 1);
881 MD5Final (hash, &ctx);
883 for (cnt = 0; cnt < 16; ++cnt)
885 hex_hash[2*cnt] = bin2hex[hash[cnt] >> 4];
886 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
890 tmp = FcStrPlus ((FcChar8 *)hex_hash, (FcChar8 *)FC_CACHE_SUFFIX);
894 cache_hashed = (char *)FcStrPlus ((FcChar8 *)PKGCACHEDIR"/", tmp);
900 /* Opens the hashed name for cache_file.
901 * This would fail in the unlikely event of a collision and subsequent
902 * removal of the file which originally caused the collision. */
904 FcDirCacheOpen (const FcChar8 *dir)
907 int fd = -1, collisions = 0;
908 char *cache_file, *cache_hashed;
909 char name_buf[FC_MAX_FILE_LEN];
910 struct stat dir_stat;
912 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
916 fd = open(cache_file, O_RDONLY);
920 if (stat ((char *)dir, &dir_stat) == -1)
927 FcChar8 * name_buf_dir;
929 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
932 FcStrFree ((FcChar8 *)cache_file);
938 fd = open(cache_hashed, O_RDONLY);
939 FcStrFree ((FcChar8 *)cache_hashed);
943 FcStrFree ((FcChar8 *)cache_file);
946 if (!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
949 name_buf_dir = FcStrDirname ((FcChar8 *)name_buf);
950 if (stat ((char *)name_buf_dir, &c) == -1)
952 FcStrFree (name_buf_dir);
955 FcStrFree (name_buf_dir);
956 found = (c.st_ino == dir_stat.st_ino) && (c.st_dev == dir_stat.st_dev);
958 FcStrFree ((FcChar8 *)cache_file);
962 FcStrFree ((FcChar8 *)cache_file);
967 /* read serialized state from the cache file */
969 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *config)
972 char *current_arch_machine_name;
973 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
974 off_t current_arch_start = 0;
975 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
977 fd = FcDirCacheOpen (dir);
981 current_arch_machine_name = FcCacheMachineSignature();
982 current_arch_start = FcCacheSkipToArch(fd,
983 current_arch_machine_name);
984 if (current_arch_start < 0)
987 lseek (fd, current_arch_start, SEEK_SET);
988 if (FcCacheReadString (fd, candidate_arch_machine_name,
989 sizeof (candidate_arch_machine_name)) == 0)
992 while (FcCacheReadString (fd, subdirName, sizeof (subdirName)) && strlen (subdirName) > 0)
993 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
995 if (!FcDirCacheConsume (fd, (const char *)dir, set, config))
1008 FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config)
1011 void * current_dir_block;
1014 read(fd, &metadata, sizeof(FcCache));
1015 if (metadata.magic != FC_CACHE_MAGIC)
1018 if (!metadata.count)
1020 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
1021 lseek (fd, pos, SEEK_SET);
1023 FcConfigAddFontDir (config, (FcChar8 *)dir);
1027 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
1028 current_dir_block = mmap (0, metadata.count,
1029 PROT_READ, MAP_SHARED, fd, pos);
1030 lseek (fd, pos+metadata.count, SEEK_SET);
1031 if (current_dir_block == MAP_FAILED)
1034 FcCacheAddBankDir (metadata.bank, dir);
1036 FcConfigAddFontDir (config, (FcChar8 *)dir);
1038 if (!FcFontSetUnserialize (&metadata, set, current_dir_block))
1045 FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
1047 void * current_dir_block, * final_dir_block;
1048 static unsigned int rand_state = 0;
1049 int bank, needed_bytes_no_align;
1052 rand_state = time(0L);
1053 bank = rand_r(&rand_state);
1055 while (FcCacheHaveBank(bank))
1056 bank = rand_r(&rand_state);
1058 memset (metadata, 0, sizeof(FcCache));
1060 needed_bytes_no_align = FcFontSetNeededBytes (set);
1061 metadata->count = needed_bytes_no_align +
1062 FcFontSetNeededBytesAlign ();
1063 metadata->magic = FC_CACHE_MAGIC;
1064 metadata->bank = bank;
1066 if (!needed_bytes_no_align) /* not a failure, no fonts to write */
1068 /* no, you don't really need to write any bytes at all. */
1069 metadata->count = 0;
1073 current_dir_block = malloc (metadata->count);
1074 if (!current_dir_block)
1077 memset (current_dir_block, 0, metadata->count);
1078 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
1080 if ((void *)((char *)current_dir_block+metadata->count) < final_dir_block)
1083 if (!FcFontSetSerialize (bank, set))
1086 return current_dir_block;
1089 free (current_dir_block);
1093 /* write serialized state to the cache file */
1095 FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
1099 int fd, fd_orig, i, dirs_count;
1102 off_t current_arch_start = 0, truncate_to;
1103 char name_buf[FC_MAX_FILE_LEN];
1106 char *current_arch_machine_name, * header;
1107 void *current_dir_block = 0;
1109 dir = FcConfigNormalizeFontDir (FcConfigGetCurrent(), dir);
1113 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
1117 /* Ensure that we're not trampling a cache for some other dir. */
1118 /* This is slightly different from FcDirCacheOpen, since it
1119 * needs the filename, not the file descriptor. */
1120 fd = -1; collisions = 0;
1123 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
1129 fd = open(cache_hashed, O_RDONLY);
1132 if(!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
1139 if (strcmp (name_buf, cache_file) != 0)
1143 current_dir_block = FcDirCacheProduce (set, &metadata);
1145 if (metadata.count && !current_dir_block)
1148 if (FcDebug () & FC_DBG_CACHE)
1149 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
1151 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
1155 if (!FcAtomicLock (atomic))
1157 /* Now try rewriting the original version of the file. */
1158 FcAtomicDestroy (atomic);
1160 atomic = FcAtomicCreate ((FcChar8 *)cache_file);
1161 fd_orig = open (cache_file, O_RDONLY);
1163 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1165 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1170 /* In all cases, try opening the real location of the cache file first. */
1171 /* (even if that's not atomic.) */
1172 fd_orig = open (cache_file, O_RDONLY);
1174 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1176 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1180 FcCacheWriteString (fd, cache_file);
1182 current_arch_machine_name = FcCacheMachineSignature ();
1183 current_arch_start = 0;
1186 current_arch_start =
1187 FcCacheSkipToArch(fd_orig, current_arch_machine_name);
1189 if (current_arch_start < 0)
1191 off_t i = lseek(fd_orig, 0, SEEK_END);
1192 if (i < strlen (FC_GLOBAL_MAGIC_COOKIE)+1)
1193 i = strlen (FC_GLOBAL_MAGIC_COOKIE)+1;
1194 current_arch_start = FcCacheNextOffset (i);
1197 if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
1203 current_arch_start = lseek(fd, 0, SEEK_CUR);
1204 if (ftruncate (fd, current_arch_start) == -1)
1207 /* allocate space for subdir names in this block */
1209 for (i = 0; i < dirs->size; i++)
1210 dirs_count += strlen((char *)dirs->strs[i]) + 1;
1213 /* now write the address of the next offset */
1214 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start;
1215 header = malloc (10 + strlen (current_arch_machine_name));
1218 sprintf (header, "%8x ", (int)truncate_to);
1219 strcat (header, current_arch_machine_name);
1220 if (!FcCacheWriteString (fd, header))
1223 for (i = 0; i < dirs->size; i++)
1224 FcCacheWriteString (fd, (char *)dirs->strs[i]);
1225 FcCacheWriteString (fd, "");
1227 write (fd, &metadata, sizeof(FcCache));
1230 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
1231 write (fd, current_dir_block, metadata.count);
1232 free (current_dir_block);
1235 /* this actually serves to pad out the cache file, if needed */
1236 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
1241 if (!FcAtomicReplaceOrig(atomic))
1243 FcStrFree ((FcChar8 *)cache_hashed);
1244 FcStrFree ((FcChar8 *)cache_file);
1245 FcAtomicUnlock (atomic);
1246 FcAtomicDestroy (atomic);
1254 FcAtomicUnlock (atomic);
1256 FcAtomicDestroy (atomic);
1258 FcStrFree ((FcChar8 *)cache_hashed);
1260 unlink ((char *)cache_file);
1261 FcStrFree ((FcChar8 *)cache_file);
1262 if (current_dir_block)
1263 free (current_dir_block);
1269 FcCacheMachineSignature ()
1271 static char buf[MACHINE_SIGNATURE_SIZE];
1272 int32_t magic = ENDIAN_TEST;
1273 char * m = (char *)&magic;
1275 sprintf (buf, "%2x%2x%2x%2x "
1276 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
1277 "%4x %4x %4x %4x %4x %4x %4x %4x\n",
1278 m[0], m[1], m[2], m[3],
1279 (unsigned int)sizeof (char),
1280 (unsigned int)sizeof (char *),
1281 (unsigned int)sizeof (int),
1282 (unsigned int)sizeof (FcPattern),
1283 (unsigned int)sizeof (FcPatternEltPtr),
1284 (unsigned int)sizeof (struct _FcPatternElt *),
1285 (unsigned int)sizeof (FcPatternElt),
1286 (unsigned int)sizeof (FcObjectPtr),
1287 (unsigned int)sizeof (FcValueListPtr),
1288 (unsigned int)sizeof (FcValue),
1289 (unsigned int)sizeof (FcValueBinding),
1290 (unsigned int)sizeof (struct _FcValueList *),
1291 (unsigned int)sizeof (FcCharSet),
1292 (unsigned int)sizeof (FcCharLeaf **),
1293 (unsigned int)sizeof (FcChar16 *),
1294 (unsigned int)sizeof (FcChar16),
1295 (unsigned int)sizeof (FcCharLeaf),
1296 (unsigned int)sizeof (FcChar32),
1297 (unsigned int)sizeof (FcCache),
1298 (unsigned int)sysconf(_SC_PAGESIZE));
1303 static int banks_ptr = 0, banks_alloc = 0;
1304 int * _fcBankId = 0, * _fcBankIdx = 0;
1305 static const char ** bankDirs = 0;
1308 FcCacheHaveBank (int bank)
1312 if (bank < FC_BANK_FIRST)
1315 for (i = 0; i < banks_ptr; i++)
1316 if (_fcBankId[i] == bank)
1323 FcCacheBankToIndexMTF (int bank)
1327 for (i = 0; i < banks_ptr; i++)
1328 if (_fcBankId[_fcBankIdx[i]] == bank)
1330 int t = _fcBankIdx[i];
1332 for (j = i; j > 0; j--)
1333 _fcBankIdx[j] = _fcBankIdx[j-1];
1338 if (banks_ptr >= banks_alloc)
1343 b = realloc (_fcBankId, (banks_alloc + 4) * sizeof(int));
1348 bidx = realloc (_fcBankIdx, (banks_alloc + 4) * sizeof(int));
1353 bds = realloc (bankDirs, (banks_alloc + 4) * sizeof (char *));
1362 _fcBankId[i] = bank;
1368 FcCacheAddBankDir (int bank, const char * dir)
1370 int bi = FcCacheBankToIndexMTF (bank);
1375 bankDirs[bi] = (const char *)FcStrCopy ((FcChar8 *)dir);
1379 FcCacheFindBankDir (int bank)
1381 int bi = FcCacheBankToIndex (bank);
1382 return bankDirs[bi];
1386 * This code implements the MD5 message-digest algorithm.
1387 * The algorithm is due to Ron Rivest. This code was
1388 * written by Colin Plumb in 1993, no copyright is claimed.
1389 * This code is in the public domain; do with it what you wish.
1391 * Equivalent code is available from RSA Data Security, Inc.
1392 * This code has been tested against that, and is equivalent,
1393 * except that you don't need to include two pages of legalese
1396 * To compute the message digest of a chunk of bytes, declare an
1397 * MD5Context structure, pass it to MD5Init, call MD5Update as
1398 * needed on buffers full of bytes, and then call MD5Final, which
1399 * will fill a supplied 16-byte array with the digest.
1403 #define byteReverse(buf, len) /* Nothing */
1406 * Note: this code is harmless on little-endian machines.
1408 void byteReverse(unsigned char *buf, unsigned longs)
1412 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1413 ((unsigned) buf[1] << 8 | buf[0]);
1414 *(FcChar32 *) buf = t;
1421 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1422 * initialization constants.
1424 static void MD5Init(struct MD5Context *ctx)
1426 ctx->buf[0] = 0x67452301;
1427 ctx->buf[1] = 0xefcdab89;
1428 ctx->buf[2] = 0x98badcfe;
1429 ctx->buf[3] = 0x10325476;
1436 * Update context to reflect the concatenation of another buffer full
1439 static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len)
1443 /* Update bitcount */
1446 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1447 ctx->bits[1]++; /* Carry from low to high */
1448 ctx->bits[1] += len >> 29;
1450 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1452 /* Handle any leading odd-sized chunks */
1455 unsigned char *p = (unsigned char *) ctx->in + t;
1459 memcpy(p, buf, len);
1463 byteReverse(ctx->in, 16);
1464 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1468 /* Process data in 64-byte chunks */
1471 memcpy(ctx->in, buf, 64);
1472 byteReverse(ctx->in, 16);
1473 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1478 /* Handle any remaining bytes of data. */
1480 memcpy(ctx->in, buf, len);
1484 * Final wrapup - pad to 64-byte boundary with the bit pattern
1485 * 1 0* (64-bit count of bits processed, MSB-first)
1487 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1492 /* Compute number of bytes mod 64 */
1493 count = (ctx->bits[0] >> 3) & 0x3F;
1495 /* Set the first char of padding to 0x80. This is safe since there is
1496 always at least one byte free */
1497 p = ctx->in + count;
1500 /* Bytes of padding needed to make 64 bytes */
1501 count = 64 - 1 - count;
1503 /* Pad out to 56 mod 64 */
1505 /* Two lots of padding: Pad the first block to 64 bytes */
1506 memset(p, 0, count);
1507 byteReverse(ctx->in, 16);
1508 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1510 /* Now fill the next block with 56 bytes */
1511 memset(ctx->in, 0, 56);
1513 /* Pad block to 56 bytes */
1514 memset(p, 0, count - 8);
1516 byteReverse(ctx->in, 14);
1518 /* Append length in bits and transform */
1519 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1520 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1522 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1523 byteReverse((unsigned char *) ctx->buf, 4);
1524 memcpy(digest, ctx->buf, 16);
1525 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
1529 /* The four core functions - F1 is optimized somewhat */
1531 /* #define F1(x, y, z) (x & y | ~x & z) */
1532 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1533 #define F2(x, y, z) F1(z, x, y)
1534 #define F3(x, y, z) (x ^ y ^ z)
1535 #define F4(x, y, z) (y ^ (x | ~z))
1537 /* This is the central step in the MD5 algorithm. */
1538 #define MD5STEP(f, w, x, y, z, data, s) \
1539 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1542 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1543 * reflect the addition of 16 longwords of new data. MD5Update blocks
1544 * the data and converts bytes into longwords for this routine.
1546 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1548 register FcChar32 a, b, c, d;
1555 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1556 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1557 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1558 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1559 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1560 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1561 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1562 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1563 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1564 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1565 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1566 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1567 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1568 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1569 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1570 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1572 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1573 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1574 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1575 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1576 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1577 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1578 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1579 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1580 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1581 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1582 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1583 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1584 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1585 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1586 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1587 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1589 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1590 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1591 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1592 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1593 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1594 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1595 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1596 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1597 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1598 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1599 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1600 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1601 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1602 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1603 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1604 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1606 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1607 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1608 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1609 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1610 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1611 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1612 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1613 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1614 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1615 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1616 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1617 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1618 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1619 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1620 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1621 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);