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)))
222 goto bail_and_destroy;
223 if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) != 0)
224 goto bail_and_destroy;
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))
256 FcStrSetAdd (staleDirs, FcStrCopy ((FcChar8 *)name_buf));
259 while (FcCacheReadString (cache->fd, subdirName,
260 sizeof (subdirName)) &&
264 if (read (cache->fd, &md, sizeof (FcCache)) != sizeof(FcCache))
266 perror ("read metadata");
269 off = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count;
270 if (lseek (cache->fd, off, SEEK_SET) != off)
278 d = malloc (sizeof (FcGlobalCacheDir));
282 d->next = cache->dirs;
285 d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
287 d->state = FcGCDirFileRead;
289 d->subdirs = FcStrSetCreate();
292 if (!FcCacheReadString (cache->fd, subdirName,
293 sizeof (subdirName)) ||
294 !strlen (subdirName))
296 FcStrSetAdd (d->subdirs, (FcChar8 *)subdirName);
299 d->offset = lseek (cache->fd, 0, SEEK_CUR);
300 if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
302 targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
303 if (lseek (cache->fd, targ, SEEK_SET) != targ)
309 for (d = cache->dirs; d; d = next)
324 if (stat ((char *) cache_file, &cache_stat) == 0)
325 unlink ((char *)cache_file);
332 FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config)
340 if (!(dir = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)dir)))
341 return FcFalse; /* non-existing directory */
343 for (d = cache->dirs; d; d = d->next)
345 if (strcmp (d->name, dir) == 0)
347 if (d->state == FcGCDirDisabled)
350 if (d->state == FcGCDirFileRead)
352 lseek (cache->fd, d->offset, SEEK_SET);
353 if (!FcDirCacheConsume (cache->fd, d->name, set, config))
356 for (i = 0; i < d->subdirs->num; i++)
357 FcStrSetAdd (dirs, (FcChar8 *)d->subdirs->strs[i]);
359 d->state = FcGCDirConsumed;
368 static FcGlobalCacheDir *
369 FcGlobalCacheDirFind (FcGlobalCache *cache, const char *name)
371 FcGlobalCacheDir * d;
376 for (d = cache->dirs; d; d = d->next)
377 if (strcmp((const char *)d->name, (const char *)name) == 0)
384 FcGlobalCacheUpdate (FcGlobalCache *cache,
386 const char *orig_name,
394 name = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)orig_name);
397 fprintf(stderr, "Invalid directory name %s\n", orig_name);
401 d = FcGlobalCacheDirFind (cache, name);
405 d = malloc (sizeof (FcGlobalCacheDir));
408 d->next = cache->dirs;
411 /* free old resources */
412 FcStrFree ((FcChar8 *)d->name);
414 FcStrSetDestroy (d->subdirs);
417 cache->updated = FcTrue;
419 d->name = (char *)FcStrCopy ((FcChar8 *)name);
420 d->ent = FcDirCacheProduce (set, &d->metadata);
422 d->subdirs = FcStrSetCreate();
423 d->state = FcGCDirUpdated;
424 for (i = 0; i < dirs->num; i++)
425 FcStrSetAdd (d->subdirs, dirs->strs[i]);
430 FcGlobalCacheSave (FcGlobalCache *cache,
431 const FcChar8 *cache_file,
435 FcGlobalCacheDir *dir;
437 off_t current_arch_start = 0, truncate_to;
438 char * current_arch_machine_name, * header;
443 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
444 /* Set-UID programs can't safely update the cache */
445 if (getuid () != geteuid ())
449 atomic = FcAtomicCreate (cache_file);
453 if (!FcAtomicLock (atomic))
455 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT,
459 FcCacheWriteString (fd, FC_GLOBAL_MAGIC_COOKIE);
461 fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY);
463 current_arch_machine_name = FcCacheMachineSignature ();
465 current_arch_start = 0;
467 current_arch_start = FcCacheSkipToArch (fd_orig,
468 current_arch_machine_name);
470 if (current_arch_start < 0)
472 off_t i = lseek(fd_orig, 0, SEEK_END);
473 if (i < strlen (FC_GLOBAL_MAGIC_COOKIE)+1)
474 i = strlen (FC_GLOBAL_MAGIC_COOKIE)+1;
475 current_arch_start = FcCacheNextOffset (i);
478 if (!FcCacheCopyOld(fd, fd_orig, current_arch_start))
481 current_arch_start = lseek(fd, 0, SEEK_CUR);
482 if (ftruncate (fd, current_arch_start) == -1)
485 header = malloc (10 + strlen (current_arch_machine_name));
489 truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11;
490 for (dir = cache->dirs; dir; dir = dir->next)
492 if (dir->state == FcGCDirDisabled)
494 truncate_to += strlen(dir->name) + 1;
495 truncate_to += sizeof (FcCache);
496 truncate_to = FcCacheNextOffset (truncate_to);
497 truncate_to += dir->metadata.count;
499 for (i = 0; i < dir->subdirs->size; i++)
500 truncate_to += strlen((char *)dir->subdirs->strs[i]) + 1;
503 truncate_to -= current_arch_start;
505 sprintf (header, "%8x ", (int)truncate_to);
506 strcat (header, current_arch_machine_name);
507 if (!FcCacheWriteString (fd, header))
510 for (dir = cache->dirs; dir; dir = dir->next)
515 if (!dir->name || dir->state == FcGCDirDisabled)
517 d = (const char *)FcConfigNormalizeFontDir (config, (const FcChar8 *)dir->name);
521 if (dir->metadata.count && !dir->ent)
523 if (dir->state == FcGCDirUpdated || fd_orig < 0)
525 fprintf(stderr, "Invalid metadata entry for %s, skipping...\n", d);
528 /* copy the old content */
529 dir->ent = malloc (dir->metadata.count);
532 perror("malloc error");
535 off = FcCacheNextOffset (dir->offset + sizeof(FcCache));
536 if (lseek (fd_orig, off, SEEK_SET) != off)
542 if (read (fd_orig, dir->ent, dir->metadata.count)
543 != dir->metadata.count)
551 FcCacheWriteString (fd, d);
553 for (i = 0; i < dir->subdirs->size; i++)
554 FcCacheWriteString (fd, (char *)dir->subdirs->strs[i]);
555 FcCacheWriteString (fd, "");
557 if (write (fd, &dir->metadata, sizeof(FcCache)) != sizeof(FcCache))
559 perror ("write metadata");
563 off = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
564 if (lseek (fd, off, SEEK_SET) != off)
570 if (dir->metadata.count)
572 if (write (fd, dir->ent, dir->metadata.count) != dir->metadata.count)
574 perror ("write dirent");
581 FcCacheWriteString (fd, "");
583 if (close (fd) == -1)
589 if (!FcAtomicReplaceOrig (atomic))
592 FcAtomicUnlock (atomic);
593 FcAtomicDestroy (atomic);
595 cache->updated = FcFalse;
606 FcAtomicDeleteNew (atomic);
608 FcAtomicUnlock (atomic);
610 FcAtomicDestroy (atomic);
615 * Find the next presumably-mmapable offset after the supplied file
619 FcCacheNextOffset(off_t w)
621 static long pagesize = -1;
623 pagesize = sysconf(_SC_PAGESIZE);
624 if (w % pagesize == 0)
627 return ((w / pagesize)+1)*pagesize;
630 /* return the address of the segment for the provided arch,
631 * or -1 if arch not found */
633 FcCacheSkipToArch (int fd, const char * arch)
635 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
636 char * candidate_arch;
637 off_t current_arch_start = 0;
639 lseek (fd, 0, SEEK_SET);
640 FcCacheSkipString (fd);
641 current_arch_start = lseek (fd, 0, SEEK_CUR);
643 /* skip arches that are not the current arch */
648 if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
651 if (FcCacheReadString (fd, candidate_arch_machine_name_count,
652 sizeof (candidate_arch_machine_name_count)) == 0)
654 if (!strlen(candidate_arch_machine_name_count))
656 bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16);
658 // count = 0 should probably be distinguished from the !bs condition
659 if (!bs || bs < strlen (candidate_arch_machine_name_count))
662 candidate_arch++; /* skip leading space */
664 if (strcmp (candidate_arch, arch)==0)
665 return current_arch_start;
666 current_arch_start += bs;
672 /* Cuts out the segment at the file pointer (moves everything else
673 * down to cover it), and leaves the file pointer at the end of the
676 FcCacheCopyOld (int fd, int fd_orig, off_t start)
678 char * buf = malloc (8192);
679 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
681 int c, bytes_skipped;
688 lseek (fd, 0, SEEK_SET); lseek (fd_orig, 0, SEEK_SET);
695 if ((c = read (fd_orig, buf, b)) <= 0)
697 if (write (fd, buf, c) < 0)
704 lseek (fd, start, SEEK_SET);
705 if (FcCacheReadString (fd, candidate_arch_machine_name,
706 sizeof (candidate_arch_machine_name)) == 0)
708 if (!strlen(candidate_arch_machine_name))
711 bs = strtol(candidate_arch_machine_name, 0, 16);
718 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
719 if ((c = read (fd, buf, 8192)) <= 0)
721 lseek (fd, start+bytes_skipped, SEEK_SET);
722 if (write (fd, buf, c) < 0)
727 lseek (fd, start+bytes_skipped, SEEK_SET);
738 /* Does not check that the cache has the appropriate arch section. */
739 /* Also, this can be fooled if the original location has a stale
740 * cache, and the hashed location has an up-to-date cache. Oh well,
741 * sucks to be you in that case! */
743 FcDirCacheValid (const FcChar8 *dir)
745 struct stat file_stat, dir_stat;
748 if (stat ((char *) dir, &dir_stat) < 0)
751 fd = FcDirCacheOpen (dir);
755 if (fstat (fd, &file_stat) < 0)
761 * If the directory has been modified more recently than
762 * the cache file, the cache is not valid
764 if (dir_stat.st_mtime > file_stat.st_mtime)
774 /* Assumes that the cache file in 'dir' exists.
775 * Checks that the cache has the appropriate arch section. */
777 FcDirCacheHasCurrentArch (const FcChar8 *dir)
780 off_t current_arch_start;
781 char *current_arch_machine_name;
783 fd = FcDirCacheOpen (dir);
787 current_arch_machine_name = FcCacheMachineSignature();
788 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
791 if (current_arch_start < 0)
801 FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
804 char *cache_hashed = 0;
806 struct stat cache_stat;
807 char name_buf[FC_MAX_FILE_LEN];
809 dir = FcConfigNormalizeFontDir (config, dir);
810 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
814 /* First remove normal cache file. */
815 if (stat ((char *) cache_file, &cache_stat) == 0 &&
816 unlink ((char *)cache_file) != 0)
819 /* Next remove any applicable hashed files. */
820 fd = -1; collisions = 0;
824 FcStrFree ((FcChar8 *)cache_hashed);
826 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
832 fd = open(cache_hashed, O_RDONLY);
835 FcStrFree ((FcChar8 *)cache_file);
839 if (!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
841 FcStrFree ((FcChar8 *)cache_hashed);
844 } while (strcmp (name_buf, cache_file) != 0);
848 if (stat ((char *) cache_hashed, &cache_stat) == 0 &&
849 unlink ((char *)cache_hashed) != 0)
851 FcStrFree ((FcChar8 *)cache_hashed);
855 FcStrFree ((FcChar8 *)cache_file);
856 FcStrFree ((FcChar8 *)cache_hashed);
860 FcStrFree ((FcChar8 *)cache_file);
865 FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
866 FcStrList *list, FcFontSet * set)
877 * Read in the results from 'list'.
879 while ((dir = FcStrListNext (list)))
881 if (!FcConfigAcceptFilename (config, dir))
884 /* Skip this directory if already updated
885 * to avoid the looped directories via symlinks
887 name = FcConfigNormalizeFontDir (config, dir);
890 if ((d = FcGlobalCacheDirFind (cache, (const char *)name)) != NULL &&
891 d->state == FcGCDirUpdated)
895 subdirs = FcStrSetCreate ();
898 fprintf (stderr, "Can't create directory set\n");
903 if (access ((char *) dir, X_OK) < 0)
911 fprintf (stderr, "\"%s\": ", dir);
915 FcStrSetDestroy (subdirs);
918 if (stat ((char *) dir, &statb) == -1)
920 fprintf (stderr, "\"%s\": ", dir);
922 FcStrSetDestroy (subdirs);
926 if (!S_ISDIR (statb.st_mode))
928 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
929 FcStrSetDestroy (subdirs);
932 if (FcDirCacheValid (dir) && FcDirCacheRead (set, subdirs, dir, config))
934 /* if an old entry is found in the global cache, disable it */
935 if ((d = FcGlobalCacheDirFind (cache, (const char *)name)) != NULL)
937 d->state = FcGCDirDisabled;
938 /* save the updated config later without this entry */
939 cache->updated = FcTrue;
944 if (FcDebug () & FC_DBG_FONTSET)
945 printf ("cache scan dir %s\n", dir);
947 FcDirScanConfig (set, subdirs, cache,
948 config->blanks, dir, FcFalse, config);
950 sublist = FcStrListCreate (subdirs);
951 FcStrSetDestroy (subdirs);
954 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
958 ret += FcCacheReadDirs (config, cache, sublist, set);
960 FcStrListDone (list);
965 FcCacheRead (FcConfig *config, FcGlobalCache * cache)
967 FcFontSet * s = FcFontSetCreate();
971 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
977 FcFontSetDestroy (s);
981 static const char bin2hex[] = { '0', '1', '2', '3',
984 'c', 'd', 'e', 'f' };
987 FcDirCacheHashName (char * cache_file, int collisions)
989 unsigned char hash[16], hex_hash[33];
991 unsigned char uscore = '_';
994 struct MD5Context ctx;
997 MD5Update (&ctx, (unsigned char *)cache_file, strlen (cache_file));
999 for (i = 0; i < collisions; i++)
1000 MD5Update (&ctx, &uscore, 1);
1002 MD5Final (hash, &ctx);
1004 for (cnt = 0; cnt < 16; ++cnt)
1006 hex_hash[2*cnt] = bin2hex[hash[cnt] >> 4];
1007 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
1011 tmp = FcStrPlus ((FcChar8 *)hex_hash, (FcChar8 *)FC_CACHE_SUFFIX);
1015 cache_hashed = (char *)FcStrPlus ((FcChar8 *)PKGCACHEDIR"/", tmp);
1018 return cache_hashed;
1021 /* Opens the hashed name for cache_file.
1022 * This would fail in the unlikely event of a collision and subsequent
1023 * removal of the file which originally caused the collision. */
1025 FcDirCacheOpen (const FcChar8 *dir)
1028 int fd = -1, collisions = 0;
1029 char *cache_file, *cache_hashed;
1030 char name_buf[FC_MAX_FILE_LEN];
1031 struct stat dir_stat;
1033 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
1037 fd = open(cache_file, O_RDONLY);
1041 if (stat ((char *)dir, &dir_stat) == -1)
1048 FcChar8 * name_buf_dir;
1050 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
1053 FcStrFree ((FcChar8 *)cache_file);
1059 fd = open(cache_hashed, O_RDONLY);
1060 FcStrFree ((FcChar8 *)cache_hashed);
1064 FcStrFree ((FcChar8 *)cache_file);
1067 if (!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
1070 name_buf_dir = FcStrDirname ((FcChar8 *)name_buf);
1071 if (stat ((char *)name_buf_dir, &c) == -1)
1073 FcStrFree (name_buf_dir);
1076 FcStrFree (name_buf_dir);
1077 found = (c.st_ino == dir_stat.st_ino) && (c.st_dev == dir_stat.st_dev);
1079 FcStrFree ((FcChar8 *)cache_file);
1083 FcStrFree ((FcChar8 *)cache_file);
1088 /* read serialized state from the cache file */
1090 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *config)
1093 char *current_arch_machine_name;
1094 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
1095 off_t current_arch_start = 0;
1096 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
1098 fd = FcDirCacheOpen (dir);
1102 current_arch_machine_name = FcCacheMachineSignature();
1103 current_arch_start = FcCacheSkipToArch(fd,
1104 current_arch_machine_name);
1105 if (current_arch_start < 0)
1108 lseek (fd, current_arch_start, SEEK_SET);
1109 if (FcCacheReadString (fd, candidate_arch_machine_name,
1110 sizeof (candidate_arch_machine_name)) == 0)
1113 while (FcCacheReadString (fd, subdirName, sizeof (subdirName)) && strlen (subdirName) > 0)
1114 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
1116 if (!FcDirCacheConsume (fd, (const char *)dir, set, config))
1129 FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config)
1132 void * current_dir_block;
1135 if (read(fd, &metadata, sizeof(FcCache)) != sizeof(FcCache))
1137 if (metadata.magic != FC_CACHE_MAGIC)
1140 if (!metadata.count)
1142 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
1143 lseek (fd, pos, SEEK_SET);
1145 FcConfigAddFontDir (config, (FcChar8 *)dir);
1149 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
1150 current_dir_block = mmap (0, metadata.count,
1151 PROT_READ, MAP_SHARED, fd, pos);
1152 lseek (fd, pos+metadata.count, SEEK_SET);
1153 if (current_dir_block == MAP_FAILED)
1156 FcCacheAddBankDir (metadata.bank, dir);
1158 FcConfigAddFontDir (config, (FcChar8 *)dir);
1160 if (!FcFontSetUnserialize (&metadata, set, current_dir_block))
1167 FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
1169 void * current_dir_block, * final_dir_block;
1170 static unsigned int rand_state = 0;
1171 int bank, needed_bytes_no_align;
1174 rand_state = time(0L);
1175 bank = rand_r(&rand_state);
1177 while (FcCacheHaveBank(bank))
1178 bank = rand_r(&rand_state);
1180 memset (metadata, 0, sizeof(FcCache));
1182 needed_bytes_no_align = FcFontSetNeededBytes (set);
1183 metadata->count = needed_bytes_no_align +
1184 FcFontSetNeededBytesAlign ();
1185 metadata->magic = FC_CACHE_MAGIC;
1186 metadata->bank = bank;
1188 if (!needed_bytes_no_align) /* not a failure, no fonts to write */
1190 /* no, you don't really need to write any bytes at all. */
1191 metadata->count = 0;
1195 current_dir_block = malloc (metadata->count);
1196 if (!current_dir_block)
1199 memset (current_dir_block, 0, metadata->count);
1200 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
1202 if ((void *)((char *)current_dir_block+metadata->count) < final_dir_block)
1205 if (!FcFontSetSerialize (bank, set))
1208 return current_dir_block;
1211 free (current_dir_block);
1215 /* write serialized state to the cache file */
1217 FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
1221 int fd, fd_orig, i, dirs_count;
1224 off_t current_arch_start = 0, truncate_to;
1225 char name_buf[FC_MAX_FILE_LEN];
1228 char *current_arch_machine_name, * header;
1229 void *current_dir_block = 0;
1231 dir = FcConfigNormalizeFontDir (FcConfigGetCurrent(), dir);
1235 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
1239 /* Ensure that we're not trampling a cache for some other dir. */
1240 /* This is slightly different from FcDirCacheOpen, since it
1241 * needs the filename, not the file descriptor. */
1242 fd = -1; collisions = 0;
1245 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
1251 fd = open(cache_hashed, O_RDONLY);
1254 if(!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
1261 if (strcmp (name_buf, cache_file) != 0)
1265 current_dir_block = FcDirCacheProduce (set, &metadata);
1267 if (metadata.count && !current_dir_block)
1270 if (FcDebug () & FC_DBG_CACHE)
1271 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
1273 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
1277 if (!FcAtomicLock (atomic))
1279 /* Now try rewriting the original version of the file. */
1280 FcAtomicDestroy (atomic);
1282 atomic = FcAtomicCreate ((FcChar8 *)cache_file);
1283 fd_orig = open (cache_file, O_RDONLY);
1285 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1287 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1292 /* In all cases, try opening the real location of the cache file first. */
1293 /* (even if that's not atomic.) */
1294 fd_orig = open (cache_file, O_RDONLY);
1296 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1298 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1302 FcCacheWriteString (fd, cache_file);
1304 current_arch_machine_name = FcCacheMachineSignature ();
1305 current_arch_start = 0;
1308 current_arch_start =
1309 FcCacheSkipToArch(fd_orig, current_arch_machine_name);
1311 if (current_arch_start < 0)
1313 off_t i = lseek(fd_orig, 0, SEEK_END);
1314 if (i < strlen (FC_GLOBAL_MAGIC_COOKIE)+1)
1315 i = strlen (FC_GLOBAL_MAGIC_COOKIE)+1;
1316 current_arch_start = FcCacheNextOffset (i);
1319 if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
1325 current_arch_start = lseek(fd, 0, SEEK_CUR);
1326 if (ftruncate (fd, current_arch_start) == -1)
1329 /* allocate space for subdir names in this block */
1331 for (i = 0; i < dirs->size; i++)
1332 dirs_count += strlen((char *)dirs->strs[i]) + 1;
1335 /* now write the address of the next offset */
1336 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start;
1337 header = malloc (10 + strlen (current_arch_machine_name));
1340 sprintf (header, "%8x ", (int)truncate_to);
1341 strcat (header, current_arch_machine_name);
1342 if (!FcCacheWriteString (fd, header))
1345 for (i = 0; i < dirs->size; i++)
1346 FcCacheWriteString (fd, (char *)dirs->strs[i]);
1347 FcCacheWriteString (fd, "");
1349 if (write (fd, &metadata, sizeof(FcCache)) != sizeof(FcCache))
1351 perror("write metadata");
1356 off_t off = FcCacheNextOffset (lseek(fd, 0, SEEK_END));
1357 if (lseek (fd, off, SEEK_SET) != off)
1359 else if (write (fd, current_dir_block, metadata.count) !=
1361 perror("write current_dir_block");
1362 free (current_dir_block);
1365 /* this actually serves to pad out the cache file, if needed */
1366 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
1371 if (!FcAtomicReplaceOrig(atomic))
1373 FcStrFree ((FcChar8 *)cache_hashed);
1374 FcStrFree ((FcChar8 *)cache_file);
1375 FcAtomicUnlock (atomic);
1376 FcAtomicDestroy (atomic);
1384 FcAtomicUnlock (atomic);
1386 FcAtomicDestroy (atomic);
1388 FcStrFree ((FcChar8 *)cache_hashed);
1390 unlink ((char *)cache_file);
1391 FcStrFree ((FcChar8 *)cache_file);
1392 if (current_dir_block)
1393 free (current_dir_block);
1399 FcCacheMachineSignature ()
1401 static char buf[MACHINE_SIGNATURE_SIZE];
1402 int32_t magic = ENDIAN_TEST;
1403 char * m = (char *)&magic;
1405 sprintf (buf, "%2x%2x%2x%2x "
1406 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
1407 "%4x %4x %4x %4x %4x %4x %4x %4x\n",
1408 m[0], m[1], m[2], m[3],
1409 (unsigned int)sizeof (char),
1410 (unsigned int)sizeof (char *),
1411 (unsigned int)sizeof (int),
1412 (unsigned int)sizeof (FcPattern),
1413 (unsigned int)sizeof (FcPatternEltPtr),
1414 (unsigned int)sizeof (struct _FcPatternElt *),
1415 (unsigned int)sizeof (FcPatternElt),
1416 (unsigned int)sizeof (FcObjectPtr),
1417 (unsigned int)sizeof (FcValueListPtr),
1418 (unsigned int)sizeof (FcValue),
1419 (unsigned int)sizeof (FcValueBinding),
1420 (unsigned int)sizeof (struct _FcValueList *),
1421 (unsigned int)sizeof (FcCharSet),
1422 (unsigned int)sizeof (FcCharLeaf **),
1423 (unsigned int)sizeof (FcChar16 *),
1424 (unsigned int)sizeof (FcChar16),
1425 (unsigned int)sizeof (FcCharLeaf),
1426 (unsigned int)sizeof (FcChar32),
1427 (unsigned int)sizeof (FcCache),
1428 (unsigned int)sysconf(_SC_PAGESIZE));
1433 static int banks_ptr = 0, banks_alloc = 0;
1434 int * _fcBankId = 0, * _fcBankIdx = 0;
1435 static const char ** bankDirs = 0;
1438 FcCacheHaveBank (int bank)
1442 if (bank < FC_BANK_FIRST)
1445 for (i = 0; i < banks_ptr; i++)
1446 if (_fcBankId[i] == bank)
1453 FcCacheBankToIndexMTF (int bank)
1457 for (i = 0; i < banks_ptr; i++)
1458 if (_fcBankId[_fcBankIdx[i]] == bank)
1460 int t = _fcBankIdx[i];
1462 for (j = i; j > 0; j--)
1463 _fcBankIdx[j] = _fcBankIdx[j-1];
1468 if (banks_ptr >= banks_alloc)
1473 b = realloc (_fcBankId, (banks_alloc + 4) * sizeof(int));
1478 bidx = realloc (_fcBankIdx, (banks_alloc + 4) * sizeof(int));
1483 bds = realloc (bankDirs, (banks_alloc + 4) * sizeof (char *));
1492 _fcBankId[i] = bank;
1498 FcCacheAddBankDir (int bank, const char * dir)
1500 int bi = FcCacheBankToIndexMTF (bank);
1505 bankDirs[bi] = (const char *)FcStrCopy ((FcChar8 *)dir);
1509 FcCacheFindBankDir (int bank)
1511 int bi = FcCacheBankToIndex (bank);
1512 return bankDirs[bi];
1516 * This code implements the MD5 message-digest algorithm.
1517 * The algorithm is due to Ron Rivest. This code was
1518 * written by Colin Plumb in 1993, no copyright is claimed.
1519 * This code is in the public domain; do with it what you wish.
1521 * Equivalent code is available from RSA Data Security, Inc.
1522 * This code has been tested against that, and is equivalent,
1523 * except that you don't need to include two pages of legalese
1526 * To compute the message digest of a chunk of bytes, declare an
1527 * MD5Context structure, pass it to MD5Init, call MD5Update as
1528 * needed on buffers full of bytes, and then call MD5Final, which
1529 * will fill a supplied 16-byte array with the digest.
1533 #define byteReverse(buf, len) /* Nothing */
1536 * Note: this code is harmless on little-endian machines.
1538 void byteReverse(unsigned char *buf, unsigned longs)
1542 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1543 ((unsigned) buf[1] << 8 | buf[0]);
1544 *(FcChar32 *) buf = t;
1551 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1552 * initialization constants.
1554 static void MD5Init(struct MD5Context *ctx)
1556 ctx->buf[0] = 0x67452301;
1557 ctx->buf[1] = 0xefcdab89;
1558 ctx->buf[2] = 0x98badcfe;
1559 ctx->buf[3] = 0x10325476;
1566 * Update context to reflect the concatenation of another buffer full
1569 static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len)
1573 /* Update bitcount */
1576 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1577 ctx->bits[1]++; /* Carry from low to high */
1578 ctx->bits[1] += len >> 29;
1580 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1582 /* Handle any leading odd-sized chunks */
1585 unsigned char *p = (unsigned char *) ctx->in + t;
1589 memcpy(p, buf, len);
1593 byteReverse(ctx->in, 16);
1594 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1598 /* Process data in 64-byte chunks */
1601 memcpy(ctx->in, buf, 64);
1602 byteReverse(ctx->in, 16);
1603 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1608 /* Handle any remaining bytes of data. */
1610 memcpy(ctx->in, buf, len);
1614 * Final wrapup - pad to 64-byte boundary with the bit pattern
1615 * 1 0* (64-bit count of bits processed, MSB-first)
1617 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1622 /* Compute number of bytes mod 64 */
1623 count = (ctx->bits[0] >> 3) & 0x3F;
1625 /* Set the first char of padding to 0x80. This is safe since there is
1626 always at least one byte free */
1627 p = ctx->in + count;
1630 /* Bytes of padding needed to make 64 bytes */
1631 count = 64 - 1 - count;
1633 /* Pad out to 56 mod 64 */
1635 /* Two lots of padding: Pad the first block to 64 bytes */
1636 memset(p, 0, count);
1637 byteReverse(ctx->in, 16);
1638 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1640 /* Now fill the next block with 56 bytes */
1641 memset(ctx->in, 0, 56);
1643 /* Pad block to 56 bytes */
1644 memset(p, 0, count - 8);
1646 byteReverse(ctx->in, 14);
1648 /* Append length in bits and transform */
1649 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1650 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1652 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1653 byteReverse((unsigned char *) ctx->buf, 4);
1654 memcpy(digest, ctx->buf, 16);
1655 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
1659 /* The four core functions - F1 is optimized somewhat */
1661 /* #define F1(x, y, z) (x & y | ~x & z) */
1662 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1663 #define F2(x, y, z) F1(z, x, y)
1664 #define F3(x, y, z) (x ^ y ^ z)
1665 #define F4(x, y, z) (y ^ (x | ~z))
1667 /* This is the central step in the MD5 algorithm. */
1668 #define MD5STEP(f, w, x, y, z, data, s) \
1669 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1672 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1673 * reflect the addition of 16 longwords of new data. MD5Update blocks
1674 * the data and converts bytes into longwords for this routine.
1676 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1678 register FcChar32 a, b, c, d;
1685 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1686 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1687 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1688 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1689 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1690 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1691 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1692 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1693 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1694 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1695 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1696 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1697 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1698 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1699 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1700 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1702 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1703 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1704 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1705 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1706 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1707 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1708 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1709 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1710 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1711 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1712 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1713 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1714 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1715 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1716 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1717 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1719 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1720 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1721 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1722 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1723 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1724 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1725 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1726 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1727 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1728 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1729 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1730 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1731 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1732 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1733 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1734 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1736 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1737 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1738 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1739 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1740 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1741 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1742 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1743 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1744 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1745 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1746 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1747 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1748 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1749 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1750 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1751 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);