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);
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 FcMemFree (FC_MEM_STRING, strlen (d->name)+1);
162 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir));
167 FcGlobalCacheCreate (void)
169 FcGlobalCache *cache;
171 cache = malloc (sizeof (FcGlobalCache));
174 FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCache));
176 cache->updated = FcFalse;
182 FcGlobalCacheDestroy (FcGlobalCache *cache)
184 FcGlobalCacheDir *d, *next;
186 for (d = cache->dirs; d; d = next)
189 FcGlobalCacheDirDestroy (d);
191 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache));
196 FcGlobalCacheLoad (FcGlobalCache *cache,
198 const FcChar8 *cache_file,
201 char name_buf[FC_MAX_FILE_LEN];
202 FcGlobalCacheDir *d, *next;
203 FcFileTime config_time = FcConfigModifiedTime (config);
204 char * current_arch_machine_name;
205 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
206 off_t current_arch_start;
208 struct stat cache_stat, dir_stat;
210 if (stat ((char *) cache_file, &cache_stat) < 0)
213 cache->fd = open ((char *) cache_file, O_RDONLY);
217 cache->updated = FcFalse;
219 FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
220 if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) != 0)
223 current_arch_machine_name = FcCacheMachineSignature ();
224 current_arch_start = FcCacheSkipToArch(cache->fd,
225 current_arch_machine_name);
226 if (current_arch_start < 0)
227 goto bail_and_destroy;
229 lseek (cache->fd, current_arch_start, SEEK_SET);
230 FcCacheReadString (cache->fd, candidate_arch_machine_name,
231 sizeof (candidate_arch_machine_name));
232 if (strlen(candidate_arch_machine_name) == 0)
233 goto bail_and_destroy;
239 FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
240 if (!strlen(name_buf))
243 /* Directory must be older than the global cache file; also
244 cache must be newer than the config file. */
245 if (stat ((char *) name_buf, &dir_stat) < 0 ||
246 dir_stat.st_mtime > cache_stat.st_mtime ||
247 (config_time.set && cache_stat.st_mtime < config_time.time))
251 FcStrSetAdd (staleDirs, FcStrCopy ((FcChar8 *)name_buf));
252 read (cache->fd, &md, sizeof (FcCache));
253 lseek (cache->fd, FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count, SEEK_SET);
257 d = malloc (sizeof (FcGlobalCacheDir));
261 d->next = cache->dirs;
264 d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
266 d->offset = lseek (cache->fd, 0, SEEK_CUR);
267 if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
269 targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
270 if (lseek (cache->fd, targ, SEEK_SET) != targ)
276 for (d = cache->dirs; d; d = next)
291 if (stat ((char *) cache_file, &cache_stat) == 0)
292 unlink ((char *)cache_file);
299 FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config)
302 FcBool ret = FcFalse;
307 for (d = cache->dirs; d; d = d->next)
309 if (strncmp (d->name, dir, strlen(dir)) == 0)
311 lseek (cache->fd, d->offset, SEEK_SET);
312 if (!FcDirCacheConsume (cache->fd, dir, set, config))
314 if (strcmp (d->name, dir) == 0)
323 FcGlobalCacheUpdate (FcGlobalCache *cache,
327 FcGlobalCacheDir * d;
329 for (d = cache->dirs; d; d = d->next)
331 if (strcmp(d->name, name) == 0)
337 d = malloc (sizeof (FcGlobalCacheDir));
340 d->next = cache->dirs;
344 cache->updated = FcTrue;
346 d->name = (char *)FcStrCopy ((FcChar8 *)name);
347 d->ent = FcDirCacheProduce (set, &d->metadata);
353 FcGlobalCacheSave (FcGlobalCache *cache,
354 const FcChar8 *cache_file)
357 FcGlobalCacheDir *dir;
359 off_t current_arch_start = 0, truncate_to;
360 char * current_arch_machine_name, * header;
365 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
366 /* Set-UID programs can't safely update the cache */
367 if (getuid () != geteuid ())
371 atomic = FcAtomicCreate (cache_file);
375 if (!FcAtomicLock (atomic))
377 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT,
382 fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY);
384 current_arch_machine_name = FcCacheMachineSignature ();
386 current_arch_start = 0;
388 current_arch_start = FcCacheSkipToArch (fd_orig,
389 current_arch_machine_name);
391 if (current_arch_start < 0)
392 current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
394 if (!FcCacheCopyOld(fd, fd_orig, current_arch_start))
400 current_arch_start = lseek(fd, 0, SEEK_CUR);
401 if (ftruncate (fd, current_arch_start) == -1)
404 header = malloc (10 + strlen (current_arch_machine_name));
408 truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11;
409 for (dir = cache->dirs; dir; dir = dir->next)
411 truncate_to += strlen(dir->name) + 1;
412 truncate_to += sizeof (FcCache);
413 truncate_to = FcCacheNextOffset (truncate_to);
414 truncate_to += dir->metadata.count;
416 truncate_to -= current_arch_start;
418 FcCacheWriteString (fd, FC_GLOBAL_MAGIC_COOKIE);
419 sprintf (header, "%8x ", (int)truncate_to);
420 strcat (header, current_arch_machine_name);
421 if (!FcCacheWriteString (fd, header))
424 for (dir = cache->dirs; dir; dir = dir->next)
428 FcCacheWriteString (fd, dir->name);
429 write (fd, &dir->metadata, sizeof(FcCache));
430 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET);
431 write (fd, dir->ent, dir->metadata.count);
435 FcCacheWriteString (fd, "");
437 if (close (fd) == -1)
440 if (!FcAtomicReplaceOrig (atomic))
443 FcAtomicUnlock (atomic);
444 FcAtomicDestroy (atomic);
446 cache->updated = FcFalse;
457 FcAtomicDeleteNew (atomic);
459 FcAtomicUnlock (atomic);
461 FcAtomicDestroy (atomic);
466 * Find the next presumably-mmapable offset after the supplied file
470 FcCacheNextOffset(off_t w)
472 static long pagesize = -1;
474 pagesize = sysconf(_SC_PAGESIZE);
475 if (w % pagesize == 0)
478 return ((w / pagesize)+1)*pagesize;
481 /* return the address of the segment for the provided arch,
482 * or -1 if arch not found */
484 FcCacheSkipToArch (int fd, const char * arch)
486 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
487 char * candidate_arch;
488 off_t current_arch_start = 0;
490 lseek (fd, 0, SEEK_SET);
491 FcCacheSkipString (fd);
492 current_arch_start = lseek (fd, 0, SEEK_CUR);
494 /* skip arches that are not the current arch */
499 if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
502 if (FcCacheReadString (fd, candidate_arch_machine_name_count,
503 sizeof (candidate_arch_machine_name_count)) == 0)
505 if (!strlen(candidate_arch_machine_name_count))
507 bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16);
509 // count = 0 should probably be distinguished from the !bs condition
510 if (!bs || bs < strlen (candidate_arch_machine_name_count))
513 candidate_arch++; /* skip leading space */
515 if (strcmp (candidate_arch, arch)==0)
516 return current_arch_start;
517 current_arch_start += bs;
523 /* Cuts out the segment at the file pointer (moves everything else
524 * down to cover it), and leaves the file pointer at the end of the
527 FcCacheCopyOld (int fd, int fd_orig, off_t start)
529 char * buf = malloc (8192);
530 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
532 int c, bytes_skipped;
539 lseek (fd, 0, SEEK_SET); lseek (fd_orig, 0, SEEK_SET);
546 if ((c = read (fd_orig, buf, b)) <= 0)
548 if (write (fd, buf, c) < 0)
555 lseek (fd, start, SEEK_SET);
556 if (FcCacheReadString (fd, candidate_arch_machine_name,
557 sizeof (candidate_arch_machine_name)) == 0)
559 if (!strlen(candidate_arch_machine_name))
562 bs = strtol(candidate_arch_machine_name, 0, 16);
569 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
570 if ((c = read (fd, buf, 8192)) <= 0)
572 lseek (fd, start+bytes_skipped, SEEK_SET);
573 if (write (fd, buf, c) < 0)
578 lseek (fd, start+bytes_skipped, SEEK_SET);
589 /* Does not check that the cache has the appropriate arch section. */
590 /* Also, this can be fooled if the original location has a stale
591 * cache, and the hashed location has an up-to-date cache. Oh well,
592 * sucks to be you in that case! */
594 FcDirCacheValid (const FcChar8 *dir)
597 struct stat file_stat, dir_stat;
600 if (stat ((char *) dir, &dir_stat) < 0)
603 cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
607 fd = FcDirCacheOpen ((char *)cache_file);
611 if (fstat (fd, &file_stat) < 0)
615 FcStrFree (cache_file);
618 * If the directory has been modified more recently than
619 * the cache file, the cache is not valid
621 if (dir_stat.st_mtime > file_stat.st_mtime)
629 FcStrFree (cache_file);
633 /* Assumes that the cache file in 'dir' exists.
634 * Checks that the cache has the appropriate arch section. */
636 FcDirCacheHasCurrentArch (const FcChar8 *dir)
640 off_t current_arch_start;
641 char *current_arch_machine_name;
643 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
647 fd = FcDirCacheOpen (cache_file);
651 current_arch_machine_name = FcCacheMachineSignature();
652 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
655 if (current_arch_start < 0)
666 FcDirCacheUnlink (const FcChar8 *dir)
668 char *cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
671 struct stat cache_stat;
672 char name_buf[FC_MAX_FILE_LEN];
674 /* First remove normal cache file. */
675 if (stat ((char *) cache_file, &cache_stat) == 0 &&
676 unlink ((char *)cache_file) != 0)
679 /* Next remove any applicable hashed files. */
680 fd = -1; collisions = 0;
683 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
689 fd = open(cache_hashed, O_RDONLY);
692 FcStrFree ((FcChar8 *)cache_file);
696 FcCacheReadString (fd, name_buf, sizeof (name_buf));
697 if (!strlen(name_buf))
699 } while (strcmp (name_buf, cache_file) != 0);
701 FcStrFree ((FcChar8 *)cache_file);
704 if (stat ((char *) cache_hashed, &cache_stat) == 0 &&
705 unlink ((char *)cache_hashed) != 0)
707 FcStrFree ((FcChar8 *)cache_hashed);
711 FcStrFree ((FcChar8 *)cache_hashed);
715 FcStrFree ((FcChar8 *)cache_file);
720 FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
721 FcStrList *list, FcFontSet * set)
725 FcChar8 *file, *base;
731 * Read in the results from 'list'.
733 while ((dir = FcStrListNext (list)))
735 if (!FcConfigAcceptFilename (config, dir))
739 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
743 strcpy ((char *) file, (char *) dir);
744 strcat ((char *) file, "/");
745 base = file + strlen ((char *) file);
747 subdirs = FcStrSetCreate ();
750 fprintf (stderr, "Can't create directory set\n");
756 if (access ((char *) dir, X_OK) < 0)
764 fprintf (stderr, "\"%s\": ", dir);
768 FcStrSetDestroy (subdirs);
772 if (stat ((char *) dir, &statb) == -1)
774 fprintf (stderr, "\"%s\": ", dir);
776 FcStrSetDestroy (subdirs);
781 if (!S_ISDIR (statb.st_mode))
783 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
784 FcStrSetDestroy (subdirs);
788 if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir, config))
790 if (FcDebug () & FC_DBG_FONTSET)
791 printf ("cache scan dir %s\n", dir);
793 FcDirScanConfig (set, subdirs, cache,
794 config->blanks, dir, FcFalse, config);
796 sublist = FcStrListCreate (subdirs);
797 FcStrSetDestroy (subdirs);
800 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
805 ret += FcCacheReadDirs (config, cache, sublist, set);
808 FcStrListDone (list);
813 FcCacheRead (FcConfig *config, FcGlobalCache * cache)
815 FcFontSet * s = FcFontSetCreate();
819 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
825 FcFontSetDestroy (s);
829 static const char bin2hex[] = { '0', '1', '2', '3',
832 'c', 'd', 'e', 'f' };
835 FcDirCacheHashName (char * cache_file, int collisions)
837 unsigned char hash[16], hex_hash[33];
839 unsigned char uscore = '_';
842 struct MD5Context ctx;
845 MD5Update (&ctx, (unsigned char *)cache_file, strlen (cache_file));
847 for (i = 0; i < collisions; i++)
848 MD5Update (&ctx, &uscore, 1);
850 MD5Final (hash, &ctx);
852 for (cnt = 0; cnt < 16; ++cnt)
854 hex_hash[2*cnt] = bin2hex[hash[cnt] >> 4];
855 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
859 tmp = FcStrPlus ((FcChar8 *)hex_hash, (FcChar8 *)FC_CACHE_SUFFIX);
863 cache_hashed = (char *)FcStrPlus ((FcChar8 *)PKGCACHEDIR"/", tmp);
869 /* Opens the hashed name for cache_file.
870 * This would fail in the unlikely event of a collision and subsequent
871 * removal of the file which originally caused the collision. */
873 FcDirCacheOpen (char *cache_file)
875 int fd = -1, collisions = 0;
877 char name_buf[FC_MAX_FILE_LEN];
879 fd = open(cache_file, O_RDONLY);
885 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
891 fd = open(cache_hashed, O_RDONLY);
892 FcStrFree ((FcChar8 *)cache_hashed);
896 FcCacheReadString (fd, name_buf, sizeof (name_buf));
897 if (!strlen(name_buf))
899 } while (strcmp (name_buf, cache_file) != 0);
907 /* read serialized state from the cache file */
909 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *config)
913 char *current_arch_machine_name;
914 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
915 off_t current_arch_start = 0;
916 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
918 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
922 fd = FcDirCacheOpen (cache_file);
926 current_arch_machine_name = FcCacheMachineSignature();
927 current_arch_start = FcCacheSkipToArch(fd,
928 current_arch_machine_name);
929 if (current_arch_start < 0)
932 lseek (fd, current_arch_start, SEEK_SET);
933 if (FcCacheReadString (fd, candidate_arch_machine_name,
934 sizeof (candidate_arch_machine_name)) == 0)
937 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
938 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
940 if (!FcDirCacheConsume (fd, (const char *)dir, set, config))
955 FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config)
958 void * current_dir_block;
961 read(fd, &metadata, sizeof(FcCache));
962 if (metadata.magic != FC_CACHE_MAGIC)
967 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
968 lseek (fd, pos, SEEK_SET);
972 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
973 current_dir_block = mmap (0, metadata.count,
974 PROT_READ, MAP_SHARED, fd, pos);
975 lseek (fd, pos+metadata.count, SEEK_SET);
976 if (current_dir_block == MAP_FAILED)
979 FcCacheAddBankDir (metadata.bank, dir);
981 FcConfigAddFontDir (config, (FcChar8 *)dir);
983 if (!FcFontSetUnserialize (&metadata, set, current_dir_block))
990 FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
992 void * current_dir_block, * final_dir_block;
993 static unsigned int rand_state = 0;
994 int bank, needed_bytes_no_align;
997 rand_state = time(0L);
998 bank = rand_r(&rand_state);
1000 while (FcCacheHaveBank(bank))
1001 bank = rand_r(&rand_state);
1003 memset (metadata, 0, sizeof(FcCache));
1005 needed_bytes_no_align = FcFontSetNeededBytes (set);
1006 metadata->count = needed_bytes_no_align +
1007 FcFontSetNeededBytesAlign ();
1008 metadata->magic = FC_CACHE_MAGIC;
1009 metadata->bank = bank;
1011 if (!needed_bytes_no_align) /* not a failure, no fonts to write */
1013 /* no, you don't really need to write any bytes at all. */
1014 metadata->count = 0;
1018 current_dir_block = malloc (metadata->count);
1019 if (!current_dir_block)
1022 memset (current_dir_block, 0, metadata->count);
1023 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
1025 if ((void *)((char *)current_dir_block+metadata->count) < final_dir_block)
1028 if (!FcFontSetSerialize (bank, set))
1031 return current_dir_block;
1034 free (current_dir_block);
1038 /* write serialized state to the cache file */
1040 FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
1044 int fd, fd_orig, i, dirs_count;
1047 off_t current_arch_start = 0, truncate_to;
1048 char name_buf[FC_MAX_FILE_LEN];
1051 char *current_arch_machine_name, * header;
1052 void *current_dir_block = 0;
1054 dir = FcConfigNormalizeFontDir (FcConfigGetCurrent(), dir);
1058 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
1062 /* Ensure that we're not trampling a cache for some other dir. */
1063 /* This is slightly different from FcDirCacheOpen, since it
1064 * needs the filename, not the file descriptor. */
1065 fd = -1; collisions = 0;
1068 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
1074 fd = open(cache_hashed, O_RDONLY);
1077 FcCacheReadString (fd, name_buf, sizeof (name_buf));
1080 if (!strlen(name_buf))
1083 if (strcmp (name_buf, cache_file) != 0)
1087 current_dir_block = FcDirCacheProduce (set, &metadata);
1089 if (metadata.count && !current_dir_block)
1092 if (FcDebug () & FC_DBG_CACHE)
1093 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
1095 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
1099 if (!FcAtomicLock (atomic))
1101 /* Now try rewriting the original version of the file. */
1102 FcAtomicDestroy (atomic);
1104 atomic = FcAtomicCreate ((FcChar8 *)cache_file);
1105 fd_orig = open (cache_file, O_RDONLY);
1107 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1109 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1114 /* In all cases, try opening the real location of the cache file first. */
1115 /* (even if that's not atomic.) */
1116 fd_orig = open (cache_file, O_RDONLY);
1118 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1120 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1124 FcCacheWriteString (fd, cache_file);
1126 current_arch_machine_name = FcCacheMachineSignature ();
1127 current_arch_start = 0;
1130 current_arch_start =
1131 FcCacheSkipToArch(fd_orig, current_arch_machine_name);
1133 if (current_arch_start < 0)
1134 current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
1136 if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
1142 current_arch_start = lseek(fd, 0, SEEK_CUR);
1143 if (ftruncate (fd, current_arch_start) == -1)
1146 /* allocate space for subdir names in this block */
1148 for (i = 0; i < dirs->size; i++)
1149 dirs_count += strlen((char *)dirs->strs[i]) + 1;
1152 /* now write the address of the next offset */
1153 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start;
1154 header = malloc (10 + strlen (current_arch_machine_name));
1157 sprintf (header, "%8x ", (int)truncate_to);
1158 strcat (header, current_arch_machine_name);
1159 if (!FcCacheWriteString (fd, header))
1162 for (i = 0; i < dirs->size; i++)
1163 FcCacheWriteString (fd, (char *)dirs->strs[i]);
1164 FcCacheWriteString (fd, "");
1166 write (fd, &metadata, sizeof(FcCache));
1169 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
1170 write (fd, current_dir_block, metadata.count);
1171 free (current_dir_block);
1174 /* this actually serves to pad out the cache file, if needed */
1175 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
1179 if (!FcAtomicReplaceOrig(atomic))
1181 FcStrFree ((FcChar8 *)cache_hashed);
1182 FcAtomicUnlock (atomic);
1183 FcAtomicDestroy (atomic);
1191 FcAtomicUnlock (atomic);
1193 FcAtomicDestroy (atomic);
1195 FcStrFree ((FcChar8 *)cache_hashed);
1197 unlink ((char *)cache_file);
1199 if (current_dir_block)
1200 free (current_dir_block);
1206 FcCacheMachineSignature ()
1208 static char buf[MACHINE_SIGNATURE_SIZE];
1209 int32_t magic = ENDIAN_TEST;
1210 char * m = (char *)&magic;
1212 sprintf (buf, "%2x%2x%2x%2x "
1213 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
1214 "%4x %4x %4x %4x %4x %4x %4x %4x\n",
1215 m[0], m[1], m[2], m[3],
1216 (unsigned int)sizeof (char),
1217 (unsigned int)sizeof (char *),
1218 (unsigned int)sizeof (int),
1219 (unsigned int)sizeof (FcPattern),
1220 (unsigned int)sizeof (FcPatternEltPtr),
1221 (unsigned int)sizeof (struct _FcPatternElt *),
1222 (unsigned int)sizeof (FcPatternElt),
1223 (unsigned int)sizeof (FcObjectPtr),
1224 (unsigned int)sizeof (FcValueListPtr),
1225 (unsigned int)sizeof (FcValue),
1226 (unsigned int)sizeof (FcValueBinding),
1227 (unsigned int)sizeof (struct _FcValueList *),
1228 (unsigned int)sizeof (FcCharSet),
1229 (unsigned int)sizeof (FcCharLeaf **),
1230 (unsigned int)sizeof (FcChar16 *),
1231 (unsigned int)sizeof (FcChar16),
1232 (unsigned int)sizeof (FcCharLeaf),
1233 (unsigned int)sizeof (FcChar32),
1234 (unsigned int)sizeof (FcCache),
1235 (unsigned int)sysconf(_SC_PAGESIZE));
1240 static int banks_ptr = 0, banks_alloc = 0;
1241 int * _fcBankId = 0, * _fcBankIdx = 0;
1242 static const char ** bankDirs = 0;
1245 FcCacheHaveBank (int bank)
1249 if (bank < FC_BANK_FIRST)
1252 for (i = 0; i < banks_ptr; i++)
1253 if (_fcBankId[i] == bank)
1260 FcCacheBankToIndexMTF (int bank)
1264 for (i = 0; i < banks_ptr; i++)
1265 if (_fcBankId[_fcBankIdx[i]] == bank)
1267 int t = _fcBankIdx[i];
1269 for (j = i; j > 0; j--)
1270 _fcBankIdx[j] = _fcBankIdx[j-1];
1275 if (banks_ptr >= banks_alloc)
1280 b = realloc (_fcBankId, (banks_alloc + 4) * sizeof(int));
1285 bidx = realloc (_fcBankIdx, (banks_alloc + 4) * sizeof(int));
1290 bds = realloc (bankDirs, (banks_alloc + 4) * sizeof (char *));
1299 _fcBankId[i] = bank;
1305 FcCacheAddBankDir (int bank, const char * dir)
1307 int bi = FcCacheBankToIndexMTF (bank);
1312 bankDirs[bi] = (const char *)FcStrCopy ((FcChar8 *)dir);
1316 FcCacheFindBankDir (int bank)
1318 int bi = FcCacheBankToIndex (bank);
1319 return bankDirs[bi];
1323 * This code implements the MD5 message-digest algorithm.
1324 * The algorithm is due to Ron Rivest. This code was
1325 * written by Colin Plumb in 1993, no copyright is claimed.
1326 * This code is in the public domain; do with it what you wish.
1328 * Equivalent code is available from RSA Data Security, Inc.
1329 * This code has been tested against that, and is equivalent,
1330 * except that you don't need to include two pages of legalese
1333 * To compute the message digest of a chunk of bytes, declare an
1334 * MD5Context structure, pass it to MD5Init, call MD5Update as
1335 * needed on buffers full of bytes, and then call MD5Final, which
1336 * will fill a supplied 16-byte array with the digest.
1340 #define byteReverse(buf, len) /* Nothing */
1343 * Note: this code is harmless on little-endian machines.
1345 void byteReverse(unsigned char *buf, unsigned longs)
1349 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1350 ((unsigned) buf[1] << 8 | buf[0]);
1351 *(FcChar32 *) buf = t;
1358 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1359 * initialization constants.
1361 static void MD5Init(struct MD5Context *ctx)
1363 ctx->buf[0] = 0x67452301;
1364 ctx->buf[1] = 0xefcdab89;
1365 ctx->buf[2] = 0x98badcfe;
1366 ctx->buf[3] = 0x10325476;
1373 * Update context to reflect the concatenation of another buffer full
1376 static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len)
1380 /* Update bitcount */
1383 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1384 ctx->bits[1]++; /* Carry from low to high */
1385 ctx->bits[1] += len >> 29;
1387 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1389 /* Handle any leading odd-sized chunks */
1392 unsigned char *p = (unsigned char *) ctx->in + t;
1396 memcpy(p, buf, len);
1400 byteReverse(ctx->in, 16);
1401 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1405 /* Process data in 64-byte chunks */
1408 memcpy(ctx->in, buf, 64);
1409 byteReverse(ctx->in, 16);
1410 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1415 /* Handle any remaining bytes of data. */
1417 memcpy(ctx->in, buf, len);
1421 * Final wrapup - pad to 64-byte boundary with the bit pattern
1422 * 1 0* (64-bit count of bits processed, MSB-first)
1424 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1429 /* Compute number of bytes mod 64 */
1430 count = (ctx->bits[0] >> 3) & 0x3F;
1432 /* Set the first char of padding to 0x80. This is safe since there is
1433 always at least one byte free */
1434 p = ctx->in + count;
1437 /* Bytes of padding needed to make 64 bytes */
1438 count = 64 - 1 - count;
1440 /* Pad out to 56 mod 64 */
1442 /* Two lots of padding: Pad the first block to 64 bytes */
1443 memset(p, 0, count);
1444 byteReverse(ctx->in, 16);
1445 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1447 /* Now fill the next block with 56 bytes */
1448 memset(ctx->in, 0, 56);
1450 /* Pad block to 56 bytes */
1451 memset(p, 0, count - 8);
1453 byteReverse(ctx->in, 14);
1455 /* Append length in bits and transform */
1456 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1457 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1459 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1460 byteReverse((unsigned char *) ctx->buf, 4);
1461 memcpy(digest, ctx->buf, 16);
1462 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
1466 /* The four core functions - F1 is optimized somewhat */
1468 /* #define F1(x, y, z) (x & y | ~x & z) */
1469 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1470 #define F2(x, y, z) F1(z, x, y)
1471 #define F3(x, y, z) (x ^ y ^ z)
1472 #define F4(x, y, z) (y ^ (x | ~z))
1474 /* This is the central step in the MD5 algorithm. */
1475 #define MD5STEP(f, w, x, y, z, data, s) \
1476 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1479 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1480 * reflect the addition of 16 longwords of new data. MD5Update blocks
1481 * the data and converts bytes into longwords for this routine.
1483 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1485 register FcChar32 a, b, c, d;
1492 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1493 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1494 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1495 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1496 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1497 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1498 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1499 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1500 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1501 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1502 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1503 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1504 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1505 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1506 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1507 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1509 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1510 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1511 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1512 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1513 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1514 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1515 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1516 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1517 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1518 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1519 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1520 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1521 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1522 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1523 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1524 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1526 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1527 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1528 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1529 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1530 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1531 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1532 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1533 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1534 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1535 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1536 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1537 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1538 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1539 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1540 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1541 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1543 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1544 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1545 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1546 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1547 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1548 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1549 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1550 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1551 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1552 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1553 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1554 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1555 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1556 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1557 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1558 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);