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)) ||
247 /* Directory must be older than the global cache file; also
248 cache must be newer than the config file. */
249 if (stat ((char *) name_buf, &dir_stat) < 0 ||
250 dir_stat.st_mtime > cache_stat.st_mtime ||
251 (config_time.set && cache_stat.st_mtime < config_time.time))
255 FcStrSetAdd (staleDirs, FcStrCopy ((FcChar8 *)name_buf));
256 read (cache->fd, &md, sizeof (FcCache));
257 lseek (cache->fd, FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count, SEEK_SET);
261 d = malloc (sizeof (FcGlobalCacheDir));
265 d->next = cache->dirs;
268 d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
270 d->offset = lseek (cache->fd, 0, SEEK_CUR);
272 d->subdirs = FcStrSetCreate();
275 if (!FcCacheReadString (cache->fd, subdirName,
276 sizeof (subdirName)) ||
277 !strlen (subdirName))
279 FcStrSetAdd (d->subdirs, (FcChar8 *)subdirName);
282 if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
284 targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
285 if (lseek (cache->fd, targ, SEEK_SET) != targ)
291 for (d = cache->dirs; d; d = next)
306 if (stat ((char *) cache_file, &cache_stat) == 0)
307 unlink ((char *)cache_file);
314 FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config)
317 FcBool ret = FcFalse;
322 if (!(dir = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)dir)))
323 return FcFalse; /* non-existing directory */
325 for (d = cache->dirs; d; d = d->next)
327 if (strncmp (d->name, dir, strlen(dir)) == 0)
329 lseek (cache->fd, d->offset, SEEK_SET);
330 if (!FcDirCacheConsume (cache->fd, d->name, set, config))
332 if (strcmp (d->name, dir) == 0)
341 FcGlobalCacheUpdate (FcGlobalCache *cache,
350 name = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)name);
351 for (d = cache->dirs; d; d = d->next)
353 if (strcmp(d->name, name) == 0)
359 d = malloc (sizeof (FcGlobalCacheDir));
362 d->next = cache->dirs;
366 cache->updated = FcTrue;
368 d->name = (char *)FcStrCopy ((FcChar8 *)name);
369 d->ent = FcDirCacheProduce (set, &d->metadata);
371 d->subdirs = FcStrSetCreate();
372 for (i = 0; i < dirs->num; i++)
373 FcStrSetAdd (d->subdirs, dirs->strs[i]);
378 FcGlobalCacheSave (FcGlobalCache *cache,
379 const FcChar8 *cache_file,
383 FcGlobalCacheDir *dir;
385 off_t current_arch_start = 0, truncate_to;
386 char * current_arch_machine_name, * header;
391 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
392 /* Set-UID programs can't safely update the cache */
393 if (getuid () != geteuid ())
397 atomic = FcAtomicCreate (cache_file);
401 if (!FcAtomicLock (atomic))
403 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT,
407 FcCacheWriteString (fd, FC_GLOBAL_MAGIC_COOKIE);
409 fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY);
411 current_arch_machine_name = FcCacheMachineSignature ();
413 current_arch_start = 0;
415 current_arch_start = FcCacheSkipToArch (fd_orig,
416 current_arch_machine_name);
418 if (current_arch_start < 0)
420 off_t i = lseek(fd_orig, 0, SEEK_END);
421 if (i < strlen (FC_GLOBAL_MAGIC_COOKIE)+1)
422 i = strlen (FC_GLOBAL_MAGIC_COOKIE)+1;
423 current_arch_start = FcCacheNextOffset (i);
426 if (!FcCacheCopyOld(fd, fd_orig, current_arch_start))
432 current_arch_start = lseek(fd, 0, SEEK_CUR);
433 if (ftruncate (fd, current_arch_start) == -1)
436 header = malloc (10 + strlen (current_arch_machine_name));
440 truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11;
441 for (dir = cache->dirs; dir; dir = dir->next)
443 truncate_to += strlen(dir->name) + 1;
444 truncate_to += sizeof (FcCache);
445 truncate_to = FcCacheNextOffset (truncate_to);
446 truncate_to += dir->metadata.count;
448 for (i = 0; i < dir->subdirs->size; i++)
449 truncate_to += strlen((char *)dir->subdirs->strs[i]) + 1;
452 truncate_to -= current_arch_start;
454 sprintf (header, "%8x ", (int)truncate_to);
455 strcat (header, current_arch_machine_name);
456 if (!FcCacheWriteString (fd, header))
459 for (dir = cache->dirs; dir; dir = dir->next)
463 const char * d = (const char *)FcConfigNormalizeFontDir (config, (const FcChar8 *)dir->name);
465 FcCacheWriteString (fd, d);
467 for (i = 0; i < dir->subdirs->size; i++)
468 FcCacheWriteString (fd, (char *)dir->subdirs->strs[i]);
469 FcCacheWriteString (fd, "");
471 write (fd, &dir->metadata, sizeof(FcCache));
472 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET);
473 write (fd, dir->ent, dir->metadata.count);
477 FcCacheWriteString (fd, "");
479 if (close (fd) == -1)
482 if (!FcAtomicReplaceOrig (atomic))
485 FcAtomicUnlock (atomic);
486 FcAtomicDestroy (atomic);
488 cache->updated = FcFalse;
499 FcAtomicDeleteNew (atomic);
501 FcAtomicUnlock (atomic);
503 FcAtomicDestroy (atomic);
508 * Find the next presumably-mmapable offset after the supplied file
512 FcCacheNextOffset(off_t w)
514 static long pagesize = -1;
516 pagesize = sysconf(_SC_PAGESIZE);
517 if (w % pagesize == 0)
520 return ((w / pagesize)+1)*pagesize;
523 /* return the address of the segment for the provided arch,
524 * or -1 if arch not found */
526 FcCacheSkipToArch (int fd, const char * arch)
528 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
529 char * candidate_arch;
530 off_t current_arch_start = 0;
532 lseek (fd, 0, SEEK_SET);
533 FcCacheSkipString (fd);
534 current_arch_start = lseek (fd, 0, SEEK_CUR);
536 /* skip arches that are not the current arch */
541 if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
544 if (FcCacheReadString (fd, candidate_arch_machine_name_count,
545 sizeof (candidate_arch_machine_name_count)) == 0)
547 if (!strlen(candidate_arch_machine_name_count))
549 bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16);
551 // count = 0 should probably be distinguished from the !bs condition
552 if (!bs || bs < strlen (candidate_arch_machine_name_count))
555 candidate_arch++; /* skip leading space */
557 if (strcmp (candidate_arch, arch)==0)
558 return current_arch_start;
559 current_arch_start += bs;
565 /* Cuts out the segment at the file pointer (moves everything else
566 * down to cover it), and leaves the file pointer at the end of the
569 FcCacheCopyOld (int fd, int fd_orig, off_t start)
571 char * buf = malloc (8192);
572 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
574 int c, bytes_skipped;
581 lseek (fd, 0, SEEK_SET); lseek (fd_orig, 0, SEEK_SET);
588 if ((c = read (fd_orig, buf, b)) <= 0)
590 if (write (fd, buf, c) < 0)
597 lseek (fd, start, SEEK_SET);
598 if (FcCacheReadString (fd, candidate_arch_machine_name,
599 sizeof (candidate_arch_machine_name)) == 0)
601 if (!strlen(candidate_arch_machine_name))
604 bs = strtol(candidate_arch_machine_name, 0, 16);
611 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
612 if ((c = read (fd, buf, 8192)) <= 0)
614 lseek (fd, start+bytes_skipped, SEEK_SET);
615 if (write (fd, buf, c) < 0)
620 lseek (fd, start+bytes_skipped, SEEK_SET);
631 /* Does not check that the cache has the appropriate arch section. */
632 /* Also, this can be fooled if the original location has a stale
633 * cache, and the hashed location has an up-to-date cache. Oh well,
634 * sucks to be you in that case! */
636 FcDirCacheValid (const FcChar8 *dir)
638 struct stat file_stat, dir_stat;
641 if (stat ((char *) dir, &dir_stat) < 0)
644 fd = FcDirCacheOpen (dir);
648 if (fstat (fd, &file_stat) < 0)
654 * If the directory has been modified more recently than
655 * the cache file, the cache is not valid
657 if (dir_stat.st_mtime > file_stat.st_mtime)
667 /* Assumes that the cache file in 'dir' exists.
668 * Checks that the cache has the appropriate arch section. */
670 FcDirCacheHasCurrentArch (const FcChar8 *dir)
673 off_t current_arch_start;
674 char *current_arch_machine_name;
676 fd = FcDirCacheOpen (dir);
680 current_arch_machine_name = FcCacheMachineSignature();
681 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
684 if (current_arch_start < 0)
694 FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
697 char *cache_hashed = 0;
699 struct stat cache_stat;
700 char name_buf[FC_MAX_FILE_LEN];
702 dir = FcConfigNormalizeFontDir (config, dir);
703 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
707 /* First remove normal cache file. */
708 if (stat ((char *) cache_file, &cache_stat) == 0 &&
709 unlink ((char *)cache_file) != 0)
712 /* Next remove any applicable hashed files. */
713 fd = -1; collisions = 0;
717 FcStrFree ((FcChar8 *)cache_hashed);
719 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
725 fd = open(cache_hashed, O_RDONLY);
728 FcStrFree ((FcChar8 *)cache_file);
732 if (!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
734 FcStrFree ((FcChar8 *)cache_hashed);
737 } while (strcmp (name_buf, cache_file) != 0);
739 FcStrFree ((FcChar8 *)cache_file);
742 if (stat ((char *) cache_hashed, &cache_stat) == 0 &&
743 unlink ((char *)cache_hashed) != 0)
745 FcStrFree ((FcChar8 *)cache_hashed);
749 FcStrFree ((FcChar8 *)cache_hashed);
753 FcStrFree ((FcChar8 *)cache_file);
758 FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
759 FcStrList *list, FcFontSet * set)
763 FcChar8 *file, *base;
769 * Read in the results from 'list'.
771 while ((dir = FcStrListNext (list)))
773 if (!FcConfigAcceptFilename (config, dir))
777 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
781 strcpy ((char *) file, (char *) dir);
782 strcat ((char *) file, "/");
783 base = file + strlen ((char *) file);
785 subdirs = FcStrSetCreate ();
788 fprintf (stderr, "Can't create directory set\n");
794 if (access ((char *) dir, X_OK) < 0)
802 fprintf (stderr, "\"%s\": ", dir);
806 FcStrSetDestroy (subdirs);
810 if (stat ((char *) dir, &statb) == -1)
812 fprintf (stderr, "\"%s\": ", dir);
814 FcStrSetDestroy (subdirs);
819 if (!S_ISDIR (statb.st_mode))
821 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
822 FcStrSetDestroy (subdirs);
826 if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir, config))
828 if (FcDebug () & FC_DBG_FONTSET)
829 printf ("cache scan dir %s\n", dir);
831 FcDirScanConfig (set, subdirs, cache,
832 config->blanks, dir, FcFalse, config);
834 sublist = FcStrListCreate (subdirs);
835 FcStrSetDestroy (subdirs);
838 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
843 ret += FcCacheReadDirs (config, cache, sublist, set);
846 FcStrListDone (list);
851 FcCacheRead (FcConfig *config, FcGlobalCache * cache)
853 FcFontSet * s = FcFontSetCreate();
857 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
863 FcFontSetDestroy (s);
867 static const char bin2hex[] = { '0', '1', '2', '3',
870 'c', 'd', 'e', 'f' };
873 FcDirCacheHashName (char * cache_file, int collisions)
875 unsigned char hash[16], hex_hash[33];
877 unsigned char uscore = '_';
880 struct MD5Context ctx;
883 MD5Update (&ctx, (unsigned char *)cache_file, strlen (cache_file));
885 for (i = 0; i < collisions; i++)
886 MD5Update (&ctx, &uscore, 1);
888 MD5Final (hash, &ctx);
890 for (cnt = 0; cnt < 16; ++cnt)
892 hex_hash[2*cnt] = bin2hex[hash[cnt] >> 4];
893 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
897 tmp = FcStrPlus ((FcChar8 *)hex_hash, (FcChar8 *)FC_CACHE_SUFFIX);
901 cache_hashed = (char *)FcStrPlus ((FcChar8 *)PKGCACHEDIR"/", tmp);
907 /* Opens the hashed name for cache_file.
908 * This would fail in the unlikely event of a collision and subsequent
909 * removal of the file which originally caused the collision. */
911 FcDirCacheOpen (const FcChar8 *dir)
914 int fd = -1, collisions = 0;
915 char *cache_file, *cache_hashed;
916 char name_buf[FC_MAX_FILE_LEN];
917 struct stat dir_stat;
919 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
923 fd = open(cache_file, O_RDONLY);
927 if (stat ((char *)dir, &dir_stat) == -1)
934 FcChar8 * name_buf_dir;
936 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
939 FcStrFree ((FcChar8 *)cache_file);
945 fd = open(cache_hashed, O_RDONLY);
946 FcStrFree ((FcChar8 *)cache_hashed);
950 FcStrFree ((FcChar8 *)cache_file);
953 if (!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
956 name_buf_dir = FcStrDirname ((FcChar8 *)name_buf);
957 if (stat ((char *)name_buf_dir, &c) == -1)
959 FcStrFree (name_buf_dir);
962 FcStrFree (name_buf_dir);
963 found = (c.st_ino == dir_stat.st_ino) && (c.st_dev == dir_stat.st_dev);
965 FcStrFree ((FcChar8 *)cache_file);
969 FcStrFree ((FcChar8 *)cache_file);
974 /* read serialized state from the cache file */
976 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *config)
979 char *current_arch_machine_name;
980 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
981 off_t current_arch_start = 0;
982 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
984 fd = FcDirCacheOpen (dir);
988 current_arch_machine_name = FcCacheMachineSignature();
989 current_arch_start = FcCacheSkipToArch(fd,
990 current_arch_machine_name);
991 if (current_arch_start < 0)
994 lseek (fd, current_arch_start, SEEK_SET);
995 if (FcCacheReadString (fd, candidate_arch_machine_name,
996 sizeof (candidate_arch_machine_name)) == 0)
999 while (FcCacheReadString (fd, subdirName, sizeof (subdirName)) && strlen (subdirName) > 0)
1000 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
1002 if (!FcDirCacheConsume (fd, (const char *)dir, set, config))
1015 FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config)
1018 void * current_dir_block;
1021 read(fd, &metadata, sizeof(FcCache));
1022 if (metadata.magic != FC_CACHE_MAGIC)
1025 if (!metadata.count)
1027 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
1028 lseek (fd, pos, SEEK_SET);
1030 FcConfigAddFontDir (config, (FcChar8 *)dir);
1034 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
1035 current_dir_block = mmap (0, metadata.count,
1036 PROT_READ, MAP_SHARED, fd, pos);
1037 lseek (fd, pos+metadata.count, SEEK_SET);
1038 if (current_dir_block == MAP_FAILED)
1041 FcCacheAddBankDir (metadata.bank, dir);
1043 FcConfigAddFontDir (config, (FcChar8 *)dir);
1045 if (!FcFontSetUnserialize (&metadata, set, current_dir_block))
1052 FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
1054 void * current_dir_block, * final_dir_block;
1055 static unsigned int rand_state = 0;
1056 int bank, needed_bytes_no_align;
1059 rand_state = time(0L);
1060 bank = rand_r(&rand_state);
1062 while (FcCacheHaveBank(bank))
1063 bank = rand_r(&rand_state);
1065 memset (metadata, 0, sizeof(FcCache));
1067 needed_bytes_no_align = FcFontSetNeededBytes (set);
1068 metadata->count = needed_bytes_no_align +
1069 FcFontSetNeededBytesAlign ();
1070 metadata->magic = FC_CACHE_MAGIC;
1071 metadata->bank = bank;
1073 if (!needed_bytes_no_align) /* not a failure, no fonts to write */
1075 /* no, you don't really need to write any bytes at all. */
1076 metadata->count = 0;
1080 current_dir_block = malloc (metadata->count);
1081 if (!current_dir_block)
1084 memset (current_dir_block, 0, metadata->count);
1085 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
1087 if ((void *)((char *)current_dir_block+metadata->count) < final_dir_block)
1090 if (!FcFontSetSerialize (bank, set))
1093 return current_dir_block;
1096 free (current_dir_block);
1100 /* write serialized state to the cache file */
1102 FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
1106 int fd, fd_orig, i, dirs_count;
1109 off_t current_arch_start = 0, truncate_to;
1110 char name_buf[FC_MAX_FILE_LEN];
1113 char *current_arch_machine_name, * header;
1114 void *current_dir_block = 0;
1116 dir = FcConfigNormalizeFontDir (FcConfigGetCurrent(), dir);
1120 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
1124 /* Ensure that we're not trampling a cache for some other dir. */
1125 /* This is slightly different from FcDirCacheOpen, since it
1126 * needs the filename, not the file descriptor. */
1127 fd = -1; collisions = 0;
1130 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
1136 fd = open(cache_hashed, O_RDONLY);
1139 if(!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
1146 if (strcmp (name_buf, cache_file) != 0)
1150 current_dir_block = FcDirCacheProduce (set, &metadata);
1152 if (metadata.count && !current_dir_block)
1155 if (FcDebug () & FC_DBG_CACHE)
1156 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
1158 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
1162 if (!FcAtomicLock (atomic))
1164 /* Now try rewriting the original version of the file. */
1165 FcAtomicDestroy (atomic);
1167 atomic = FcAtomicCreate ((FcChar8 *)cache_file);
1168 fd_orig = open (cache_file, O_RDONLY);
1170 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1172 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1177 /* In all cases, try opening the real location of the cache file first. */
1178 /* (even if that's not atomic.) */
1179 fd_orig = open (cache_file, O_RDONLY);
1181 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1183 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1187 FcCacheWriteString (fd, cache_file);
1189 current_arch_machine_name = FcCacheMachineSignature ();
1190 current_arch_start = 0;
1193 current_arch_start =
1194 FcCacheSkipToArch(fd_orig, current_arch_machine_name);
1196 if (current_arch_start < 0)
1198 off_t i = lseek(fd_orig, 0, SEEK_END);
1199 if (i < strlen (FC_GLOBAL_MAGIC_COOKIE)+1)
1200 i = strlen (FC_GLOBAL_MAGIC_COOKIE)+1;
1201 current_arch_start = FcCacheNextOffset (i);
1204 if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
1210 current_arch_start = lseek(fd, 0, SEEK_CUR);
1211 if (ftruncate (fd, current_arch_start) == -1)
1214 /* allocate space for subdir names in this block */
1216 for (i = 0; i < dirs->size; i++)
1217 dirs_count += strlen((char *)dirs->strs[i]) + 1;
1220 /* now write the address of the next offset */
1221 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start;
1222 header = malloc (10 + strlen (current_arch_machine_name));
1225 sprintf (header, "%8x ", (int)truncate_to);
1226 strcat (header, current_arch_machine_name);
1227 if (!FcCacheWriteString (fd, header))
1230 for (i = 0; i < dirs->size; i++)
1231 FcCacheWriteString (fd, (char *)dirs->strs[i]);
1232 FcCacheWriteString (fd, "");
1234 write (fd, &metadata, sizeof(FcCache));
1237 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
1238 write (fd, current_dir_block, metadata.count);
1239 free (current_dir_block);
1242 /* this actually serves to pad out the cache file, if needed */
1243 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
1248 if (!FcAtomicReplaceOrig(atomic))
1250 FcStrFree ((FcChar8 *)cache_hashed);
1251 FcStrFree ((FcChar8 *)cache_file);
1252 FcAtomicUnlock (atomic);
1253 FcAtomicDestroy (atomic);
1261 FcAtomicUnlock (atomic);
1263 FcAtomicDestroy (atomic);
1265 FcStrFree ((FcChar8 *)cache_hashed);
1267 unlink ((char *)cache_file);
1268 FcStrFree ((FcChar8 *)cache_file);
1269 if (current_dir_block)
1270 free (current_dir_block);
1276 FcCacheMachineSignature ()
1278 static char buf[MACHINE_SIGNATURE_SIZE];
1279 int32_t magic = ENDIAN_TEST;
1280 char * m = (char *)&magic;
1282 sprintf (buf, "%2x%2x%2x%2x "
1283 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
1284 "%4x %4x %4x %4x %4x %4x %4x %4x\n",
1285 m[0], m[1], m[2], m[3],
1286 (unsigned int)sizeof (char),
1287 (unsigned int)sizeof (char *),
1288 (unsigned int)sizeof (int),
1289 (unsigned int)sizeof (FcPattern),
1290 (unsigned int)sizeof (FcPatternEltPtr),
1291 (unsigned int)sizeof (struct _FcPatternElt *),
1292 (unsigned int)sizeof (FcPatternElt),
1293 (unsigned int)sizeof (FcObjectPtr),
1294 (unsigned int)sizeof (FcValueListPtr),
1295 (unsigned int)sizeof (FcValue),
1296 (unsigned int)sizeof (FcValueBinding),
1297 (unsigned int)sizeof (struct _FcValueList *),
1298 (unsigned int)sizeof (FcCharSet),
1299 (unsigned int)sizeof (FcCharLeaf **),
1300 (unsigned int)sizeof (FcChar16 *),
1301 (unsigned int)sizeof (FcChar16),
1302 (unsigned int)sizeof (FcCharLeaf),
1303 (unsigned int)sizeof (FcChar32),
1304 (unsigned int)sizeof (FcCache),
1305 (unsigned int)sysconf(_SC_PAGESIZE));
1310 static int banks_ptr = 0, banks_alloc = 0;
1311 int * _fcBankId = 0, * _fcBankIdx = 0;
1312 static const char ** bankDirs = 0;
1315 FcCacheHaveBank (int bank)
1319 if (bank < FC_BANK_FIRST)
1322 for (i = 0; i < banks_ptr; i++)
1323 if (_fcBankId[i] == bank)
1330 FcCacheBankToIndexMTF (int bank)
1334 for (i = 0; i < banks_ptr; i++)
1335 if (_fcBankId[_fcBankIdx[i]] == bank)
1337 int t = _fcBankIdx[i];
1339 for (j = i; j > 0; j--)
1340 _fcBankIdx[j] = _fcBankIdx[j-1];
1345 if (banks_ptr >= banks_alloc)
1350 b = realloc (_fcBankId, (banks_alloc + 4) * sizeof(int));
1355 bidx = realloc (_fcBankIdx, (banks_alloc + 4) * sizeof(int));
1360 bds = realloc (bankDirs, (banks_alloc + 4) * sizeof (char *));
1369 _fcBankId[i] = bank;
1375 FcCacheAddBankDir (int bank, const char * dir)
1377 int bi = FcCacheBankToIndexMTF (bank);
1382 bankDirs[bi] = (const char *)FcStrCopy ((FcChar8 *)dir);
1386 FcCacheFindBankDir (int bank)
1388 int bi = FcCacheBankToIndex (bank);
1389 return bankDirs[bi];
1393 * This code implements the MD5 message-digest algorithm.
1394 * The algorithm is due to Ron Rivest. This code was
1395 * written by Colin Plumb in 1993, no copyright is claimed.
1396 * This code is in the public domain; do with it what you wish.
1398 * Equivalent code is available from RSA Data Security, Inc.
1399 * This code has been tested against that, and is equivalent,
1400 * except that you don't need to include two pages of legalese
1403 * To compute the message digest of a chunk of bytes, declare an
1404 * MD5Context structure, pass it to MD5Init, call MD5Update as
1405 * needed on buffers full of bytes, and then call MD5Final, which
1406 * will fill a supplied 16-byte array with the digest.
1410 #define byteReverse(buf, len) /* Nothing */
1413 * Note: this code is harmless on little-endian machines.
1415 void byteReverse(unsigned char *buf, unsigned longs)
1419 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1420 ((unsigned) buf[1] << 8 | buf[0]);
1421 *(FcChar32 *) buf = t;
1428 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1429 * initialization constants.
1431 static void MD5Init(struct MD5Context *ctx)
1433 ctx->buf[0] = 0x67452301;
1434 ctx->buf[1] = 0xefcdab89;
1435 ctx->buf[2] = 0x98badcfe;
1436 ctx->buf[3] = 0x10325476;
1443 * Update context to reflect the concatenation of another buffer full
1446 static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len)
1450 /* Update bitcount */
1453 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1454 ctx->bits[1]++; /* Carry from low to high */
1455 ctx->bits[1] += len >> 29;
1457 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1459 /* Handle any leading odd-sized chunks */
1462 unsigned char *p = (unsigned char *) ctx->in + t;
1466 memcpy(p, buf, len);
1470 byteReverse(ctx->in, 16);
1471 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1475 /* Process data in 64-byte chunks */
1478 memcpy(ctx->in, buf, 64);
1479 byteReverse(ctx->in, 16);
1480 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1485 /* Handle any remaining bytes of data. */
1487 memcpy(ctx->in, buf, len);
1491 * Final wrapup - pad to 64-byte boundary with the bit pattern
1492 * 1 0* (64-bit count of bits processed, MSB-first)
1494 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1499 /* Compute number of bytes mod 64 */
1500 count = (ctx->bits[0] >> 3) & 0x3F;
1502 /* Set the first char of padding to 0x80. This is safe since there is
1503 always at least one byte free */
1504 p = ctx->in + count;
1507 /* Bytes of padding needed to make 64 bytes */
1508 count = 64 - 1 - count;
1510 /* Pad out to 56 mod 64 */
1512 /* Two lots of padding: Pad the first block to 64 bytes */
1513 memset(p, 0, count);
1514 byteReverse(ctx->in, 16);
1515 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1517 /* Now fill the next block with 56 bytes */
1518 memset(ctx->in, 0, 56);
1520 /* Pad block to 56 bytes */
1521 memset(p, 0, count - 8);
1523 byteReverse(ctx->in, 14);
1525 /* Append length in bits and transform */
1526 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1527 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1529 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1530 byteReverse((unsigned char *) ctx->buf, 4);
1531 memcpy(digest, ctx->buf, 16);
1532 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
1536 /* The four core functions - F1 is optimized somewhat */
1538 /* #define F1(x, y, z) (x & y | ~x & z) */
1539 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1540 #define F2(x, y, z) F1(z, x, y)
1541 #define F3(x, y, z) (x ^ y ^ z)
1542 #define F4(x, y, z) (y ^ (x | ~z))
1544 /* This is the central step in the MD5 algorithm. */
1545 #define MD5STEP(f, w, x, y, z, data, s) \
1546 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1549 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1550 * reflect the addition of 16 longwords of new data. MD5Update blocks
1551 * the data and converts bytes into longwords for this routine.
1553 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1555 register FcChar32 a, b, c, d;
1562 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1563 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1564 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1565 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1566 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1567 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1568 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1569 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1570 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1571 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1572 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1573 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1574 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1575 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1576 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1577 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1579 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1580 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1581 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1582 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1583 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1584 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1585 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1586 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1587 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1588 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1589 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1590 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1591 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1592 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1593 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1594 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1596 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1597 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1598 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1599 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1600 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1601 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1602 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1603 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1604 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1605 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1606 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1607 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1608 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1609 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1610 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1611 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1613 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1614 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1615 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1616 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1617 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1618 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1619 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1620 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1621 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1622 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1623 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1624 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1625 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1626 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1627 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1628 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);