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 (char * cache_file);
43 FcDirCacheHashName (char * cache_file, int collisions);
46 FcCacheSkipToArch (int fd, const char * arch, FcBool global);
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);
58 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir);
61 FcCacheNextOffset(off_t w);
64 FcCacheMachineSignature (void);
67 FcCacheHaveBank (int bank);
70 FcCacheAddBankDir (int bank, const char * dir);
78 static void MD5Init(struct MD5Context *ctx);
79 static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len);
80 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
81 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]);
83 #define FC_DBG_CACHE_REF 1024
86 FcCacheReadString (int fd, char *dest, int len)
99 while (read (fd, &c, 1) == 1)
126 FcCacheSkipString (int fd)
132 while (read (fd, &c, 1) == 1)
153 FcCacheWriteString (int fd, const char *chars)
155 if (write (fd, chars, strlen(chars)+1) != strlen(chars)+1)
161 FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
163 FcMemFree (FC_MEM_STRING, strlen (d->name)+1);
165 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir));
170 FcGlobalCacheCreate (void)
172 FcGlobalCache *cache;
174 cache = malloc (sizeof (FcGlobalCache));
177 FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCache));
179 cache->updated = FcFalse;
185 FcGlobalCacheDestroy (FcGlobalCache *cache)
187 FcGlobalCacheDir *d, *next;
189 for (d = cache->dirs; d; d = next)
192 FcGlobalCacheDirDestroy (d);
194 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache));
199 FcGlobalCacheLoad (FcGlobalCache *cache,
201 const FcChar8 *cache_file,
204 char name_buf[FC_MAX_FILE_LEN];
205 FcGlobalCacheDir *d, *next;
206 FcFileTime config_time = FcConfigModifiedTime (config);
207 char * current_arch_machine_name;
208 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
209 off_t current_arch_start;
211 struct stat cache_stat, dir_stat;
213 if (stat ((char *) cache_file, &cache_stat) < 0)
216 cache->fd = open ((char *) cache_file, O_RDONLY);
220 cache->updated = FcFalse;
222 current_arch_machine_name = FcCacheMachineSignature ();
223 current_arch_start = FcCacheSkipToArch(cache->fd,
224 current_arch_machine_name, FcTrue);
225 if (current_arch_start < 0)
226 goto bail_and_destroy;
228 lseek (cache->fd, current_arch_start, SEEK_SET);
229 FcCacheReadString (cache->fd, candidate_arch_machine_name,
230 sizeof (candidate_arch_machine_name));
231 if (strlen(candidate_arch_machine_name) == 0)
232 goto bail_and_destroy;
238 FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
239 if (!strlen(name_buf))
242 /* Directory must be older than the global cache file; also
243 cache must be newer than the config file. */
244 if (stat ((char *) name_buf, &dir_stat) < 0 ||
245 dir_stat.st_mtime > cache_stat.st_mtime ||
246 (config_time.set && cache_stat.st_mtime < config_time.time))
250 FcStrSetAdd (staleDirs, FcStrCopy ((FcChar8 *)name_buf));
251 read (cache->fd, &md, sizeof (FcCache));
252 lseek (cache->fd, FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count, SEEK_SET);
256 d = malloc (sizeof (FcGlobalCacheDir));
260 d->next = cache->dirs;
263 d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
265 d->offset = lseek (cache->fd, 0, SEEK_CUR);
266 if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
268 targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
269 if (lseek (cache->fd, targ, SEEK_SET) != targ)
275 for (d = cache->dirs; d; d = next)
290 if (stat ((char *) cache_file, &cache_stat) == 0)
291 unlink ((char *)cache_file);
298 FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config)
301 FcBool ret = FcFalse;
306 for (d = cache->dirs; d; d = d->next)
308 if (strncmp (d->name, dir, strlen(dir)) == 0)
310 lseek (cache->fd, d->offset, SEEK_SET);
311 if (!FcDirCacheConsume (cache->fd, dir, set))
313 if (strcmp (d->name, dir) == 0)
322 FcGlobalCacheUpdate (FcGlobalCache *cache,
326 FcGlobalCacheDir * d;
331 for (d = cache->dirs; d; d = d->next)
333 if (strcmp(d->name, name) == 0)
339 d = malloc (sizeof (FcGlobalCacheDir));
342 d->next = cache->dirs;
346 cache->updated = FcTrue;
348 d->name = (char *)FcStrCopy ((FcChar8 *)name);
349 d->ent = FcDirCacheProduce (set, &d->metadata);
355 FcGlobalCacheSave (FcGlobalCache *cache,
356 const FcChar8 *cache_file)
359 FcGlobalCacheDir *dir;
361 off_t current_arch_start = 0, truncate_to;
362 char * current_arch_machine_name, * header;
367 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
368 /* Set-UID programs can't safely update the cache */
369 if (getuid () != geteuid ())
373 atomic = FcAtomicCreate (cache_file);
377 if (!FcAtomicLock (atomic))
379 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT,
384 fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY);
386 current_arch_machine_name = FcCacheMachineSignature ();
388 current_arch_start = 0;
390 current_arch_start = FcCacheSkipToArch (fd_orig,
391 current_arch_machine_name,
394 if (current_arch_start < 0)
395 current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
397 if (!FcCacheCopyOld(fd, fd_orig, current_arch_start))
403 current_arch_start = lseek(fd, 0, SEEK_CUR);
404 if (ftruncate (fd, current_arch_start) == -1)
407 header = malloc (10 + strlen (current_arch_machine_name));
411 truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11;
412 for (dir = cache->dirs; dir; dir = dir->next)
414 truncate_to += strlen(dir->name) + 1;
415 truncate_to += sizeof (FcCache);
416 truncate_to = FcCacheNextOffset (truncate_to);
417 truncate_to += dir->metadata.count;
419 truncate_to -= current_arch_start;
421 sprintf (header, "%8x ", (int)truncate_to);
422 strcat (header, current_arch_machine_name);
423 if (!FcCacheWriteString (fd, header))
426 for (dir = cache->dirs; dir; dir = dir->next)
430 FcCacheWriteString (fd, dir->name);
431 write (fd, &dir->metadata, sizeof(FcCache));
432 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET);
433 write (fd, dir->ent, dir->metadata.count);
437 FcCacheWriteString (fd, "");
439 if (close (fd) == -1)
442 if (!FcAtomicReplaceOrig (atomic))
445 FcAtomicUnlock (atomic);
446 FcAtomicDestroy (atomic);
448 cache->updated = FcFalse;
459 FcAtomicDeleteNew (atomic);
461 FcAtomicUnlock (atomic);
463 FcAtomicDestroy (atomic);
468 * Find the next presumably-mmapable offset after the supplied file
472 FcCacheNextOffset(off_t w)
474 static long pagesize = -1;
476 pagesize = sysconf(_SC_PAGESIZE);
477 if (w % pagesize == 0)
480 return ((w / pagesize)+1)*pagesize;
483 /* return the address of the segment for the provided arch,
484 * or -1 if arch not found */
486 FcCacheSkipToArch (int fd, const char * arch, FcBool global)
488 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
489 char * candidate_arch;
490 off_t current_arch_start = 0;
492 lseek (fd, 0, SEEK_SET);
494 FcCacheSkipString (fd);
495 current_arch_start = lseek (fd, 0, SEEK_CUR);
497 /* skip arches that are not the current arch */
502 if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
505 if (FcCacheReadString (fd, candidate_arch_machine_name_count,
506 sizeof (candidate_arch_machine_name_count)) == 0)
508 if (!strlen(candidate_arch_machine_name_count))
510 bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16);
512 // count = 0 should probably be distinguished from the !bs condition
513 if (!bs || bs < strlen (candidate_arch_machine_name_count))
516 candidate_arch++; /* skip leading space */
518 if (strcmp (candidate_arch, arch)==0)
519 return current_arch_start;
520 current_arch_start += bs;
526 /* Cuts out the segment at the file pointer (moves everything else
527 * down to cover it), and leaves the file pointer at the end of the
530 FcCacheCopyOld (int fd, int fd_orig, off_t start)
532 char * buf = malloc (8192);
533 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
535 int c, bytes_skipped;
542 lseek (fd, 0, SEEK_SET); lseek (fd_orig, 0, SEEK_SET);
549 if ((c = read (fd_orig, buf, b)) <= 0)
551 if (write (fd, buf, c) < 0)
558 lseek (fd, start, SEEK_SET);
559 if (FcCacheReadString (fd, candidate_arch_machine_name,
560 sizeof (candidate_arch_machine_name)) == 0)
562 if (!strlen(candidate_arch_machine_name))
565 bs = strtol(candidate_arch_machine_name, 0, 16);
572 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
573 if ((c = read (fd, buf, 8192)) <= 0)
575 lseek (fd, start+bytes_skipped, SEEK_SET);
576 if (write (fd, buf, c) < 0)
581 lseek (fd, start+bytes_skipped, SEEK_SET);
592 /* Does not check that the cache has the appropriate arch section. */
593 /* Also, this can be fooled if the original location has a stale
594 * cache, and the hashed location has an up-to-date cache. Oh well,
595 * sucks to be you in that case! */
597 FcDirCacheValid (const FcChar8 *dir)
600 struct stat file_stat, dir_stat;
603 if (stat ((char *) dir, &dir_stat) < 0)
606 cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
610 fd = FcDirCacheOpen ((char *)cache_file);
614 if (fstat (fd, &file_stat) < 0)
618 FcStrFree (cache_file);
621 * If the directory has been modified more recently than
622 * the cache file, the cache is not valid
624 if (dir_stat.st_mtime > file_stat.st_mtime)
632 FcStrFree (cache_file);
636 /* Assumes that the cache file in 'dir' exists.
637 * Checks that the cache has the appropriate arch section. */
639 FcDirCacheHasCurrentArch (const FcChar8 *dir)
643 off_t current_arch_start;
644 char *current_arch_machine_name;
646 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
650 fd = FcDirCacheOpen (cache_file);
654 current_arch_machine_name = FcCacheMachineSignature();
655 current_arch_start = FcCacheSkipToArch(fd,
656 current_arch_machine_name, FcFalse);
659 if (current_arch_start < 0)
670 FcDirCacheUnlink (const FcChar8 *dir)
672 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
673 struct stat cache_stat;
675 if (stat ((char *) cache_file, &cache_stat) == 0 &&
676 unlink ((char *)cache_file) != 0)
678 FcStrFree (cache_file);
682 FcStrFree (cache_file);
687 FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
688 FcStrList *list, FcFontSet * set)
692 FcChar8 *file, *base;
698 * Read in the results from 'list'.
700 while ((dir = FcStrListNext (list)))
702 if (!FcConfigAcceptFilename (config, dir))
706 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
710 strcpy ((char *) file, (char *) dir);
711 strcat ((char *) file, "/");
712 base = file + strlen ((char *) file);
714 subdirs = FcStrSetCreate ();
717 fprintf (stderr, "Can't create directory set\n");
723 if (access ((char *) dir, X_OK) < 0)
731 fprintf (stderr, "\"%s\": ", dir);
735 FcStrSetDestroy (subdirs);
739 if (stat ((char *) dir, &statb) == -1)
741 fprintf (stderr, "\"%s\": ", dir);
743 FcStrSetDestroy (subdirs);
748 if (!S_ISDIR (statb.st_mode))
750 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
751 FcStrSetDestroy (subdirs);
755 if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir))
757 if (FcDebug () & FC_DBG_FONTSET)
758 printf ("cache scan dir %s\n", dir);
760 FcDirScanConfig (set, subdirs, cache,
761 config->blanks, dir, FcFalse, config);
763 sublist = FcStrListCreate (subdirs);
764 FcStrSetDestroy (subdirs);
767 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
772 ret += FcCacheReadDirs (config, cache, sublist, set);
775 FcStrListDone (list);
780 FcCacheRead (FcConfig *config, FcGlobalCache * cache)
782 FcFontSet * s = FcFontSetCreate();
786 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
792 FcFontSetDestroy (s);
796 static const char bin2hex[] = { '0', '1', '2', '3',
799 'c', 'd', 'e', 'f' };
802 FcDirCacheHashName (char * cache_file, int collisions)
804 unsigned char hash[16], hex_hash[33];
806 unsigned char uscore = '_';
809 struct MD5Context ctx;
812 MD5Update (&ctx, (unsigned char *)cache_file, strlen (cache_file));
814 for (i = 0; i < collisions; i++)
815 MD5Update (&ctx, &uscore, 1);
817 MD5Final (hash, &ctx);
819 for (cnt = 0; cnt < 16; ++cnt)
821 hex_hash[2*cnt] = bin2hex[hash[cnt] >> 4];
822 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
826 tmp = FcStrPlus ((FcChar8 *)hex_hash, (FcChar8 *)FC_CACHE_SUFFIX);
830 cache_hashed = (char *)FcStrPlus ((FcChar8 *)PKGCACHEDIR"/", tmp);
836 /* Opens the hashed name for cache_file.
837 * This would fail in the unlikely event of a collision and subsequent
838 * removal of the file which originally caused the collision. */
840 FcDirCacheOpen (char *cache_file)
842 int fd = -1, collisions = 0;
844 char name_buf[FC_MAX_FILE_LEN];
846 fd = open(cache_file, O_RDONLY);
852 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
858 fd = open(cache_hashed, O_RDONLY);
861 FcCacheReadString (fd, name_buf, sizeof (name_buf));
862 if (!strlen(name_buf))
864 } while (strcmp (name_buf, cache_file) != 0);
872 /* read serialized state from the cache file */
874 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir)
878 char *current_arch_machine_name;
879 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
880 off_t current_arch_start = 0;
881 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
883 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
887 fd = FcDirCacheOpen (cache_file);
891 current_arch_machine_name = FcCacheMachineSignature();
892 current_arch_start = FcCacheSkipToArch(fd,
893 current_arch_machine_name,
895 if (current_arch_start < 0)
898 lseek (fd, current_arch_start, SEEK_SET);
899 if (FcCacheReadString (fd, candidate_arch_machine_name,
900 sizeof (candidate_arch_machine_name)) == 0)
903 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
904 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
906 if (!FcDirCacheConsume (fd, (const char *)dir, set))
921 FcDirCacheConsume (int fd, const char * dir, FcFontSet *set)
924 void * current_dir_block;
927 read(fd, &metadata, sizeof(FcCache));
928 if (metadata.magic != FC_CACHE_MAGIC)
934 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
935 current_dir_block = mmap (0, metadata.count,
936 PROT_READ, MAP_SHARED, fd, pos);
937 if (current_dir_block == MAP_FAILED)
940 FcCacheAddBankDir (metadata.bank, dir);
942 if (!FcFontSetUnserialize (&metadata, set, current_dir_block))
949 FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
951 void * current_dir_block, * final_dir_block;
952 static unsigned int rand_state = 0;
953 int bank, needed_bytes_no_align;
956 rand_state = time(0L);
957 bank = rand_r(&rand_state);
959 while (FcCacheHaveBank(bank))
960 bank = rand_r(&rand_state);
962 memset (metadata, 0, sizeof(FcCache));
964 needed_bytes_no_align = FcFontSetNeededBytes (set);
965 metadata->count = needed_bytes_no_align +
966 FcFontSetNeededBytesAlign ();
967 metadata->magic = FC_CACHE_MAGIC;
968 metadata->bank = bank;
970 if (!needed_bytes_no_align) /* not a failure, no fonts to write */
972 /* no, you don't really need to write any bytes at all. */
977 current_dir_block = malloc (metadata->count);
978 if (!current_dir_block)
981 memset (current_dir_block, 0, metadata->count);
982 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
984 if ((void *)((char *)current_dir_block+metadata->count) < final_dir_block)
987 if (!FcFontSetSerialize (bank, set))
990 return current_dir_block;
993 free (current_dir_block);
997 /* write serialized state to the cache file */
999 FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
1002 char *cache_to_open;
1003 int fd, fd_orig, i, dirs_count;
1006 off_t current_arch_start = 0, truncate_to;
1007 char name_buf[FC_MAX_FILE_LEN];
1010 char *current_arch_machine_name, * header;
1011 void *current_dir_block = 0;
1013 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
1017 /* Ensure that we're not trampling a cache for some other dir. */
1018 /* This is slightly different from FcDirCacheOpen, since it
1019 * needs the filename, not the file descriptor. */
1020 fd = -1; collisions = 0;
1023 cache_to_open = FcDirCacheHashName (cache_file, collisions++);
1029 fd = open(cache_to_open, O_RDONLY);
1032 FcCacheReadString (fd, name_buf, sizeof (name_buf));
1035 if (!strlen(name_buf))
1038 if (strcmp (name_buf, cache_file) != 0)
1042 current_dir_block = FcDirCacheProduce (set, &metadata);
1044 if (metadata.count && !current_dir_block)
1047 if (FcDebug () & FC_DBG_CACHE)
1048 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
1050 atomic = FcAtomicCreate ((FcChar8 *)cache_to_open);
1054 if (!FcAtomicLock (atomic))
1056 /* Now try rewriting the original version of the file. */
1057 FcAtomicDestroy (atomic);
1059 atomic = FcAtomicCreate (cache_file);
1060 fd_orig = open (cache_file, O_RDONLY);
1062 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1064 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1069 /* In all cases, try opening the real location of the cache file first. */
1070 /* (even if that's not atomic.) */
1071 fd_orig = open (cache_file, O_RDONLY);
1073 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1075 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1079 FcCacheWriteString (fd, cache_file);
1081 current_arch_machine_name = FcCacheMachineSignature ();
1082 current_arch_start = 0;
1085 current_arch_start =
1086 FcCacheSkipToArch(fd_orig, current_arch_machine_name, FcFalse);
1088 if (current_arch_start < 0)
1089 current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
1091 if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
1097 current_arch_start = lseek(fd, 0, SEEK_CUR);
1098 if (ftruncate (fd, current_arch_start) == -1)
1101 /* allocate space for subdir names in this block */
1103 for (i = 0; i < dirs->size; i++)
1104 dirs_count += strlen((char *)dirs->strs[i]) + 1;
1107 /* now write the address of the next offset */
1108 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start;
1109 header = malloc (10 + strlen (current_arch_machine_name));
1112 sprintf (header, "%8x ", (int)truncate_to);
1113 strcat (header, current_arch_machine_name);
1114 if (!FcCacheWriteString (fd, header))
1117 for (i = 0; i < dirs->size; i++)
1118 FcCacheWriteString (fd, (char *)dirs->strs[i]);
1119 FcCacheWriteString (fd, "");
1121 write (fd, &metadata, sizeof(FcCache));
1124 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
1125 write (fd, current_dir_block, metadata.count);
1126 free (current_dir_block);
1129 /* this actually serves to pad out the cache file, if needed */
1130 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
1134 if (!FcAtomicReplaceOrig(atomic))
1136 FcAtomicUnlock (atomic);
1137 FcAtomicDestroy (atomic);
1145 FcAtomicUnlock (atomic);
1147 FcAtomicDestroy (atomic);
1149 free (cache_to_open);
1151 unlink ((char *)cache_file);
1153 if (current_dir_block)
1154 free (current_dir_block);
1160 FcCacheMachineSignature ()
1162 static char buf[MACHINE_SIGNATURE_SIZE];
1163 int32_t magic = ENDIAN_TEST;
1164 char * m = (char *)&magic;
1166 sprintf (buf, "%2x%2x%2x%2x "
1167 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
1168 "%4x %4x %4x %4x %4x %4x %4x %4x\n",
1169 m[0], m[1], m[2], m[3],
1170 (unsigned int)sizeof (char),
1171 (unsigned int)sizeof (char *),
1172 (unsigned int)sizeof (int),
1173 (unsigned int)sizeof (FcPattern),
1174 (unsigned int)sizeof (FcPatternEltPtr),
1175 (unsigned int)sizeof (struct _FcPatternElt *),
1176 (unsigned int)sizeof (FcPatternElt),
1177 (unsigned int)sizeof (FcObjectPtr),
1178 (unsigned int)sizeof (FcValueListPtr),
1179 (unsigned int)sizeof (FcValue),
1180 (unsigned int)sizeof (FcValueBinding),
1181 (unsigned int)sizeof (struct _FcValueList *),
1182 (unsigned int)sizeof (FcCharSet),
1183 (unsigned int)sizeof (FcCharLeaf **),
1184 (unsigned int)sizeof (FcChar16 *),
1185 (unsigned int)sizeof (FcChar16),
1186 (unsigned int)sizeof (FcCharLeaf),
1187 (unsigned int)sizeof (FcChar32),
1188 (unsigned int)sizeof (FcCache),
1189 (unsigned int)sysconf(_SC_PAGESIZE));
1194 static int banks_ptr = 0, banks_alloc = 0;
1195 int * _fcBankId = 0, * _fcBankIdx = 0;
1196 static const char ** bankDirs = 0;
1199 FcCacheHaveBank (int bank)
1203 if (bank < FC_BANK_FIRST)
1206 for (i = 0; i < banks_ptr; i++)
1207 if (_fcBankId[i] == bank)
1214 FcCacheBankToIndexMTF (int bank)
1218 for (i = 0; i < banks_ptr; i++)
1219 if (_fcBankId[_fcBankIdx[i]] == bank)
1221 int t = _fcBankIdx[i];
1223 for (j = i; j > 0; j--)
1224 _fcBankIdx[j] = _fcBankIdx[j-1];
1229 if (banks_ptr >= banks_alloc)
1234 b = realloc (_fcBankId, (banks_alloc + 4) * sizeof(int));
1239 bidx = realloc (_fcBankIdx, (banks_alloc + 4) * sizeof(int));
1244 bds = realloc (bankDirs, (banks_alloc + 4) * sizeof (char *));
1253 _fcBankId[i] = bank;
1259 FcCacheAddBankDir (int bank, const char * dir)
1261 int bi = FcCacheBankToIndexMTF (bank);
1266 bankDirs[bi] = (const char *)FcStrCopy ((FcChar8 *)dir);
1270 FcCacheFindBankDir (int bank)
1272 int bi = FcCacheBankToIndex (bank);
1273 return bankDirs[bi];
1277 * This code implements the MD5 message-digest algorithm.
1278 * The algorithm is due to Ron Rivest. This code was
1279 * written by Colin Plumb in 1993, no copyright is claimed.
1280 * This code is in the public domain; do with it what you wish.
1282 * Equivalent code is available from RSA Data Security, Inc.
1283 * This code has been tested against that, and is equivalent,
1284 * except that you don't need to include two pages of legalese
1287 * To compute the message digest of a chunk of bytes, declare an
1288 * MD5Context structure, pass it to MD5Init, call MD5Update as
1289 * needed on buffers full of bytes, and then call MD5Final, which
1290 * will fill a supplied 16-byte array with the digest.
1294 #define byteReverse(buf, len) /* Nothing */
1297 * Note: this code is harmless on little-endian machines.
1299 void byteReverse(unsigned char *buf, unsigned longs)
1303 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1304 ((unsigned) buf[1] << 8 | buf[0]);
1305 *(FcChar32 *) buf = t;
1312 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1313 * initialization constants.
1315 static void MD5Init(struct MD5Context *ctx)
1317 ctx->buf[0] = 0x67452301;
1318 ctx->buf[1] = 0xefcdab89;
1319 ctx->buf[2] = 0x98badcfe;
1320 ctx->buf[3] = 0x10325476;
1327 * Update context to reflect the concatenation of another buffer full
1330 static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len)
1334 /* Update bitcount */
1337 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1338 ctx->bits[1]++; /* Carry from low to high */
1339 ctx->bits[1] += len >> 29;
1341 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1343 /* Handle any leading odd-sized chunks */
1346 unsigned char *p = (unsigned char *) ctx->in + t;
1350 memcpy(p, buf, len);
1354 byteReverse(ctx->in, 16);
1355 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1359 /* Process data in 64-byte chunks */
1362 memcpy(ctx->in, buf, 64);
1363 byteReverse(ctx->in, 16);
1364 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1369 /* Handle any remaining bytes of data. */
1371 memcpy(ctx->in, buf, len);
1375 * Final wrapup - pad to 64-byte boundary with the bit pattern
1376 * 1 0* (64-bit count of bits processed, MSB-first)
1378 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1383 /* Compute number of bytes mod 64 */
1384 count = (ctx->bits[0] >> 3) & 0x3F;
1386 /* Set the first char of padding to 0x80. This is safe since there is
1387 always at least one byte free */
1388 p = ctx->in + count;
1391 /* Bytes of padding needed to make 64 bytes */
1392 count = 64 - 1 - count;
1394 /* Pad out to 56 mod 64 */
1396 /* Two lots of padding: Pad the first block to 64 bytes */
1397 memset(p, 0, count);
1398 byteReverse(ctx->in, 16);
1399 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1401 /* Now fill the next block with 56 bytes */
1402 memset(ctx->in, 0, 56);
1404 /* Pad block to 56 bytes */
1405 memset(p, 0, count - 8);
1407 byteReverse(ctx->in, 14);
1409 /* Append length in bits and transform */
1410 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1411 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1413 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1414 byteReverse((unsigned char *) ctx->buf, 4);
1415 memcpy(digest, ctx->buf, 16);
1416 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
1420 /* The four core functions - F1 is optimized somewhat */
1422 /* #define F1(x, y, z) (x & y | ~x & z) */
1423 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1424 #define F2(x, y, z) F1(z, x, y)
1425 #define F3(x, y, z) (x ^ y ^ z)
1426 #define F4(x, y, z) (y ^ (x | ~z))
1428 /* This is the central step in the MD5 algorithm. */
1429 #define MD5STEP(f, w, x, y, z, data, s) \
1430 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1433 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1434 * reflect the addition of 16 longwords of new data. MD5Update blocks
1435 * the data and converts bytes into longwords for this routine.
1437 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1439 register FcChar32 a, b, c, d;
1446 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1447 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1448 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1449 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1450 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1451 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1452 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1453 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1454 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1455 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1456 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1457 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1458 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1459 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1460 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1461 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1463 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1464 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1465 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1466 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1467 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1468 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1469 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1470 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1471 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1472 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1473 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1474 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1475 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1476 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1477 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1478 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1480 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1481 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1482 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1483 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1484 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1485 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1486 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1487 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1488 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1489 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1490 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1491 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1492 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1493 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1494 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1495 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1497 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1498 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1499 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1500 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1501 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1502 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1503 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1504 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1505 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1506 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1507 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1508 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1509 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1510 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1511 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1512 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);