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)
91 size = read (fd, dest, len-1);
98 lseek (fd, slen - size + 1, SEEK_CUR);
99 return slen < len ? dest : 0;
106 FcCacheSkipString (int fd)
112 while ( (size = read (fd, buf, sizeof (buf)-1)) > 0)
118 lseek (fd, slen - size + 1, SEEK_CUR);
125 FcCacheWriteString (int fd, const char *chars)
127 if (write (fd, chars, strlen(chars)+1) != strlen(chars)+1)
133 FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
135 FcStrSetDestroy (d->subdirs);
136 FcMemFree (FC_MEM_STRING, strlen (d->name)+1);
138 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir));
143 FcGlobalCacheCreate (void)
145 FcGlobalCache *cache;
147 cache = malloc (sizeof (FcGlobalCache));
150 FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCache));
152 cache->updated = FcFalse;
158 FcGlobalCacheDestroy (FcGlobalCache *cache)
160 FcGlobalCacheDir *d, *next;
162 for (d = cache->dirs; d; d = next)
165 FcGlobalCacheDirDestroy (d);
167 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache));
172 FcGlobalCacheLoad (FcGlobalCache *cache,
174 const FcChar8 *cache_file,
177 char name_buf[FC_MAX_FILE_LEN];
178 FcGlobalCacheDir *d, *next;
179 FcFileTime config_time = FcConfigModifiedTime (config);
180 char * current_arch_machine_name;
181 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
182 off_t current_arch_start;
184 struct stat cache_stat, dir_stat;
185 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
187 if (stat ((char *) cache_file, &cache_stat) < 0)
190 cache->fd = open ((char *) cache_file, O_RDONLY);
194 cache->updated = FcFalse;
196 if (!FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)))
197 goto bail_and_destroy;
198 if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) != 0)
199 goto bail_and_destroy;
201 current_arch_machine_name = FcCacheMachineSignature ();
202 current_arch_start = FcCacheSkipToArch(cache->fd,
203 current_arch_machine_name);
204 if (current_arch_start < 0)
205 goto bail_and_destroy;
207 lseek (cache->fd, current_arch_start, SEEK_SET);
208 if (!FcCacheReadString (cache->fd, candidate_arch_machine_name,
209 sizeof (candidate_arch_machine_name)))
210 goto bail_and_destroy;
211 if (strlen(candidate_arch_machine_name) == 0)
212 goto bail_and_destroy;
218 if (!FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)) ||
222 /* Directory must be older than the global cache file; also
223 cache must be newer than the config file. */
224 if (stat ((char *) name_buf, &dir_stat) < 0 ||
225 dir_stat.st_mtime > cache_stat.st_mtime ||
226 (config_time.set && cache_stat.st_mtime < config_time.time))
231 FcStrSetAdd (staleDirs, FcStrCopy ((FcChar8 *)name_buf));
234 while (FcCacheReadString (cache->fd, subdirName,
235 sizeof (subdirName)) &&
239 if (read (cache->fd, &md, sizeof (FcCache)) != sizeof(FcCache))
241 perror ("read metadata");
244 off = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count;
245 if (lseek (cache->fd, off, SEEK_SET) != off)
253 d = malloc (sizeof (FcGlobalCacheDir));
257 d->next = cache->dirs;
260 d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
262 d->state = FcGCDirFileRead;
264 d->subdirs = FcStrSetCreate();
267 if (!FcCacheReadString (cache->fd, subdirName,
268 sizeof (subdirName)) ||
269 !strlen (subdirName))
271 FcStrSetAdd (d->subdirs, (FcChar8 *)subdirName);
274 d->offset = lseek (cache->fd, 0, SEEK_CUR);
275 if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
277 targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
278 if (lseek (cache->fd, targ, SEEK_SET) != targ)
284 for (d = cache->dirs; d; d = next)
299 if (stat ((char *) cache_file, &cache_stat) == 0)
300 unlink ((char *)cache_file);
307 FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config)
315 if (!(dir = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)dir)))
316 return FcFalse; /* non-existing directory */
318 for (d = cache->dirs; d; d = d->next)
320 if (strcmp (d->name, dir) == 0)
322 if (d->state == FcGCDirDisabled)
325 if (d->state == FcGCDirFileRead)
327 lseek (cache->fd, d->offset, SEEK_SET);
328 if (!FcDirCacheConsume (cache->fd, d->name, set, config))
331 for (i = 0; i < d->subdirs->num; i++)
332 FcStrSetAdd (dirs, (FcChar8 *)d->subdirs->strs[i]);
334 d->state = FcGCDirConsumed;
343 static FcGlobalCacheDir *
344 FcGlobalCacheDirFind (FcGlobalCache *cache, const char *name)
346 FcGlobalCacheDir * d;
351 for (d = cache->dirs; d; d = d->next)
352 if (strcmp((const char *)d->name, (const char *)name) == 0)
359 FcGlobalCacheUpdate (FcGlobalCache *cache,
361 const char *orig_name,
369 name = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)orig_name);
372 fprintf(stderr, "Invalid directory name %s\n", orig_name);
376 d = FcGlobalCacheDirFind (cache, name);
380 d = malloc (sizeof (FcGlobalCacheDir));
383 d->next = cache->dirs;
386 /* free old resources */
387 FcStrFree ((FcChar8 *)d->name);
389 FcStrSetDestroy (d->subdirs);
392 cache->updated = FcTrue;
394 d->name = (char *)FcStrCopy ((FcChar8 *)name);
395 d->ent = FcDirCacheProduce (set, &d->metadata);
397 d->subdirs = FcStrSetCreate();
398 d->state = FcGCDirUpdated;
399 for (i = 0; i < dirs->num; i++)
400 FcStrSetAdd (d->subdirs, dirs->strs[i]);
405 FcGlobalCacheSave (FcGlobalCache *cache,
406 const FcChar8 *cache_file,
410 FcGlobalCacheDir *dir;
412 off_t current_arch_start = 0, truncate_to;
413 char * current_arch_machine_name, * header;
418 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
419 /* Set-UID programs can't safely update the cache */
420 if (getuid () != geteuid ())
424 atomic = FcAtomicCreate (cache_file);
428 if (!FcAtomicLock (atomic))
430 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT,
434 FcCacheWriteString (fd, FC_GLOBAL_MAGIC_COOKIE);
436 fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY);
438 current_arch_machine_name = FcCacheMachineSignature ();
440 current_arch_start = 0;
442 current_arch_start = FcCacheSkipToArch (fd_orig,
443 current_arch_machine_name);
445 if (current_arch_start < 0)
447 off_t i = lseek(fd_orig, 0, SEEK_END);
448 if (i < strlen (FC_GLOBAL_MAGIC_COOKIE)+1)
449 i = strlen (FC_GLOBAL_MAGIC_COOKIE)+1;
450 current_arch_start = FcCacheNextOffset (i);
453 if (!FcCacheCopyOld(fd, fd_orig, current_arch_start))
456 current_arch_start = lseek(fd, 0, SEEK_CUR);
457 if (ftruncate (fd, current_arch_start) == -1)
460 header = malloc (10 + strlen (current_arch_machine_name));
464 truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11;
465 for (dir = cache->dirs; dir; dir = dir->next)
467 if (dir->state == FcGCDirDisabled)
469 truncate_to += strlen(dir->name) + 1;
470 truncate_to += sizeof (FcCache);
471 truncate_to = FcCacheNextOffset (truncate_to);
472 truncate_to += dir->metadata.count;
474 for (i = 0; i < dir->subdirs->size; i++)
475 truncate_to += strlen((char *)dir->subdirs->strs[i]) + 1;
478 truncate_to -= current_arch_start;
480 sprintf (header, "%8x ", (int)truncate_to);
481 strcat (header, current_arch_machine_name);
482 if (!FcCacheWriteString (fd, header))
485 for (dir = cache->dirs; dir; dir = dir->next)
490 if (!dir->name || dir->state == FcGCDirDisabled)
492 d = (const char *)FcConfigNormalizeFontDir (config, (const FcChar8 *)dir->name);
496 if (dir->metadata.count && !dir->ent)
498 if (dir->state == FcGCDirUpdated || fd_orig < 0)
500 fprintf(stderr, "Invalid metadata entry for %s, skipping...\n", d);
503 /* copy the old content */
504 dir->ent = malloc (dir->metadata.count);
507 perror("malloc error");
510 off = FcCacheNextOffset (dir->offset + sizeof(FcCache));
511 if (lseek (fd_orig, off, SEEK_SET) != off)
517 if (read (fd_orig, dir->ent, dir->metadata.count)
518 != dir->metadata.count)
526 FcCacheWriteString (fd, d);
528 for (i = 0; i < dir->subdirs->size; i++)
529 FcCacheWriteString (fd, (char *)dir->subdirs->strs[i]);
530 FcCacheWriteString (fd, "");
532 if (write (fd, &dir->metadata, sizeof(FcCache)) != sizeof(FcCache))
534 perror ("write metadata");
538 off = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
539 if (lseek (fd, off, SEEK_SET) != off)
545 if (dir->metadata.count)
547 if (write (fd, dir->ent, dir->metadata.count) != dir->metadata.count)
549 perror ("write dirent");
556 FcCacheWriteString (fd, "");
558 if (close (fd) == -1)
564 if (!FcAtomicReplaceOrig (atomic))
567 FcAtomicUnlock (atomic);
568 FcAtomicDestroy (atomic);
570 cache->updated = FcFalse;
581 FcAtomicDeleteNew (atomic);
583 FcAtomicUnlock (atomic);
585 FcAtomicDestroy (atomic);
590 * Find the next presumably-mmapable offset after the supplied file
594 FcCacheNextOffset(off_t w)
596 static long pagesize = -1;
598 pagesize = sysconf(_SC_PAGESIZE);
599 if (w % pagesize == 0)
602 return ((w / pagesize)+1)*pagesize;
605 /* return the address of the segment for the provided arch,
606 * or -1 if arch not found */
608 FcCacheSkipToArch (int fd, const char * arch)
610 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
611 char * candidate_arch;
612 off_t current_arch_start = 0;
614 lseek (fd, 0, SEEK_SET);
615 FcCacheSkipString (fd);
616 current_arch_start = lseek (fd, 0, SEEK_CUR);
618 /* skip arches that are not the current arch */
623 if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
626 if (FcCacheReadString (fd, candidate_arch_machine_name_count,
627 sizeof (candidate_arch_machine_name_count)) == 0)
629 if (!strlen(candidate_arch_machine_name_count))
631 bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16);
633 // count = 0 should probably be distinguished from the !bs condition
634 if (!bs || bs < strlen (candidate_arch_machine_name_count))
637 candidate_arch++; /* skip leading space */
639 if (strcmp (candidate_arch, arch)==0)
640 return current_arch_start;
641 current_arch_start += bs;
647 /* Cuts out the segment at the file pointer (moves everything else
648 * down to cover it), and leaves the file pointer at the end of the
651 FcCacheCopyOld (int fd, int fd_orig, off_t start)
653 char * buf = malloc (8192);
654 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
656 int c, bytes_skipped;
663 lseek (fd, 0, SEEK_SET); lseek (fd_orig, 0, SEEK_SET);
670 if ((c = read (fd_orig, buf, b)) <= 0)
672 if (write (fd, buf, c) < 0)
679 lseek (fd, start, SEEK_SET);
680 if (FcCacheReadString (fd, candidate_arch_machine_name,
681 sizeof (candidate_arch_machine_name)) == 0)
683 if (!strlen(candidate_arch_machine_name))
686 bs = strtol(candidate_arch_machine_name, 0, 16);
693 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
694 if ((c = read (fd, buf, 8192)) <= 0)
696 lseek (fd, start+bytes_skipped, SEEK_SET);
697 if (write (fd, buf, c) < 0)
702 lseek (fd, start+bytes_skipped, SEEK_SET);
713 /* Does not check that the cache has the appropriate arch section. */
714 /* Also, this can be fooled if the original location has a stale
715 * cache, and the hashed location has an up-to-date cache. Oh well,
716 * sucks to be you in that case! */
718 FcDirCacheValid (const FcChar8 *dir)
720 struct stat file_stat, dir_stat;
723 if (stat ((char *) dir, &dir_stat) < 0)
726 fd = FcDirCacheOpen (dir);
730 if (fstat (fd, &file_stat) < 0)
736 * If the directory has been modified more recently than
737 * the cache file, the cache is not valid
739 if (dir_stat.st_mtime > file_stat.st_mtime)
749 /* Assumes that the cache file in 'dir' exists.
750 * Checks that the cache has the appropriate arch section. */
752 FcDirCacheHasCurrentArch (const FcChar8 *dir)
755 off_t current_arch_start;
756 char *current_arch_machine_name;
758 fd = FcDirCacheOpen (dir);
762 current_arch_machine_name = FcCacheMachineSignature();
763 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
766 if (current_arch_start < 0)
776 FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
779 char *cache_hashed = 0;
781 struct stat cache_stat;
782 char name_buf[FC_MAX_FILE_LEN];
784 dir = FcConfigNormalizeFontDir (config, dir);
785 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
789 /* First remove normal cache file. */
790 if (stat ((char *) cache_file, &cache_stat) == 0 &&
791 unlink ((char *)cache_file) != 0)
794 /* Next remove any applicable hashed files. */
795 fd = -1; collisions = 0;
799 FcStrFree ((FcChar8 *)cache_hashed);
801 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
807 fd = open(cache_hashed, O_RDONLY);
810 FcStrFree ((FcChar8 *)cache_file);
814 if (!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
816 FcStrFree ((FcChar8 *)cache_hashed);
819 } while (strcmp (name_buf, cache_file) != 0);
823 if (stat ((char *) cache_hashed, &cache_stat) == 0 &&
824 unlink ((char *)cache_hashed) != 0)
826 FcStrFree ((FcChar8 *)cache_hashed);
830 FcStrFree ((FcChar8 *)cache_file);
831 FcStrFree ((FcChar8 *)cache_hashed);
835 FcStrFree ((FcChar8 *)cache_file);
840 FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
841 FcStrList *list, FcFontSet * set, FcStrSet *processed_dirs)
851 * Read in the results from 'list'.
853 while ((dir = FcStrListNext (list)))
855 if (!FcConfigAcceptFilename (config, dir))
858 /* Skip this directory if already updated
859 * to avoid the looped directories via symlinks
860 * Clearly a dir not in fonts.conf shouldn't be globally cached.
862 dir = FcConfigNormalizeFontDir (config, dir);
866 if (FcStrSetMember (processed_dirs, dir))
868 if (!FcStrSetAdd (processed_dirs, dir))
871 subdirs = FcStrSetCreate ();
874 fprintf (stderr, "Can't create directory set\n");
879 if (access ((char *) dir, X_OK) < 0)
887 fprintf (stderr, "\"%s\": ", dir);
891 FcStrSetDestroy (subdirs);
894 if (stat ((char *) dir, &statb) == -1)
896 fprintf (stderr, "\"%s\": ", dir);
898 FcStrSetDestroy (subdirs);
902 if (!S_ISDIR (statb.st_mode))
904 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
905 FcStrSetDestroy (subdirs);
908 if (FcDirCacheValid (dir) && FcDirCacheRead (set, subdirs, dir, config))
910 /* if an old entry is found in the global cache, disable it */
911 if ((d = FcGlobalCacheDirFind (cache, (const char *)dir)) != NULL)
913 d->state = FcGCDirDisabled;
914 /* save the updated config later without this entry */
915 cache->updated = FcTrue;
920 if (FcDebug () & FC_DBG_FONTSET)
921 printf ("cache scan dir %s\n", dir);
923 FcDirScanConfig (set, subdirs, cache,
924 config->blanks, dir, FcFalse, config);
926 sublist = FcStrListCreate (subdirs);
927 FcStrSetDestroy (subdirs);
930 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
934 ret += FcCacheReadDirs (config, cache, sublist, set, processed_dirs);
936 FcStrListDone (list);
941 FcCacheRead (FcConfig *config, FcGlobalCache * cache)
943 FcFontSet *s = FcFontSetCreate();
944 FcStrSet *processed_dirs;
949 processed_dirs = FcStrSetCreate();
953 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s, processed_dirs))
956 FcStrSetDestroy (processed_dirs);
960 FcStrSetDestroy (processed_dirs);
962 FcFontSetDestroy (s);
966 static const char bin2hex[] = { '0', '1', '2', '3',
969 'c', 'd', 'e', 'f' };
972 FcDirCacheHashName (char * cache_file, int collisions)
974 unsigned char hash[16], hex_hash[33];
976 unsigned char uscore = '_';
979 struct MD5Context ctx;
982 MD5Update (&ctx, (unsigned char *)cache_file, strlen (cache_file));
984 for (i = 0; i < collisions; i++)
985 MD5Update (&ctx, &uscore, 1);
987 MD5Final (hash, &ctx);
989 for (cnt = 0; cnt < 16; ++cnt)
991 hex_hash[2*cnt] = bin2hex[hash[cnt] >> 4];
992 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
996 tmp = FcStrPlus ((FcChar8 *)hex_hash, (FcChar8 *)FC_CACHE_SUFFIX);
1000 cache_hashed = (char *)FcStrPlus ((FcChar8 *)PKGCACHEDIR"/", tmp);
1003 return cache_hashed;
1006 /* Opens the hashed name for cache_file.
1007 * This would fail in the unlikely event of a collision and subsequent
1008 * removal of the file which originally caused the collision. */
1010 FcDirCacheOpen (const FcChar8 *dir)
1013 int fd = -1, collisions = 0;
1014 char *cache_file, *cache_hashed;
1015 char name_buf[FC_MAX_FILE_LEN];
1016 struct stat dir_stat;
1018 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
1022 fd = open(cache_file, O_RDONLY);
1026 if (stat ((char *)dir, &dir_stat) == -1)
1033 FcChar8 * name_buf_dir;
1035 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
1038 FcStrFree ((FcChar8 *)cache_file);
1044 fd = open(cache_hashed, O_RDONLY);
1045 FcStrFree ((FcChar8 *)cache_hashed);
1049 FcStrFree ((FcChar8 *)cache_file);
1052 if (!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
1055 name_buf_dir = FcStrDirname ((FcChar8 *)name_buf);
1056 if (stat ((char *)name_buf_dir, &c) == -1)
1058 FcStrFree (name_buf_dir);
1061 FcStrFree (name_buf_dir);
1062 found = (c.st_ino == dir_stat.st_ino) && (c.st_dev == dir_stat.st_dev);
1064 FcStrFree ((FcChar8 *)cache_file);
1068 FcStrFree ((FcChar8 *)cache_file);
1073 /* read serialized state from the cache file */
1075 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *config)
1078 char *current_arch_machine_name;
1079 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
1080 off_t current_arch_start = 0;
1081 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
1083 fd = FcDirCacheOpen (dir);
1087 current_arch_machine_name = FcCacheMachineSignature();
1088 current_arch_start = FcCacheSkipToArch(fd,
1089 current_arch_machine_name);
1090 if (current_arch_start < 0)
1093 lseek (fd, current_arch_start, SEEK_SET);
1094 if (FcCacheReadString (fd, candidate_arch_machine_name,
1095 sizeof (candidate_arch_machine_name)) == 0)
1098 while (FcCacheReadString (fd, subdirName, sizeof (subdirName)) && strlen (subdirName) > 0)
1099 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
1101 if (!FcDirCacheConsume (fd, (const char *)dir, set, config))
1114 FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config)
1117 void * current_dir_block;
1120 if (read(fd, &metadata, sizeof(FcCache)) != sizeof(FcCache))
1122 if (metadata.magic != FC_CACHE_MAGIC)
1125 if (!metadata.count)
1127 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
1128 lseek (fd, pos, SEEK_SET);
1130 FcConfigAddFontDir (config, (FcChar8 *)dir);
1134 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
1135 current_dir_block = mmap (0, metadata.count,
1136 PROT_READ, MAP_SHARED, fd, pos);
1137 lseek (fd, pos+metadata.count, SEEK_SET);
1138 if (current_dir_block == MAP_FAILED)
1141 FcCacheAddBankDir (metadata.bank, dir);
1143 FcConfigAddFontDir (config, (FcChar8 *)dir);
1145 if (!FcFontSetUnserialize (&metadata, set, current_dir_block))
1152 FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
1154 void * current_dir_block, * final_dir_block;
1155 static unsigned int rand_state = 0;
1156 int bank, needed_bytes_no_align;
1159 rand_state = time(0L);
1160 bank = rand_r(&rand_state);
1162 while (FcCacheHaveBank(bank))
1163 bank = rand_r(&rand_state);
1165 memset (metadata, 0, sizeof(FcCache));
1167 needed_bytes_no_align = FcFontSetNeededBytes (set);
1168 metadata->count = needed_bytes_no_align +
1169 FcFontSetNeededBytesAlign ();
1170 metadata->magic = FC_CACHE_MAGIC;
1171 metadata->bank = bank;
1173 if (!needed_bytes_no_align) /* not a failure, no fonts to write */
1175 /* no, you don't really need to write any bytes at all. */
1176 metadata->count = 0;
1180 current_dir_block = malloc (metadata->count);
1181 if (!current_dir_block)
1184 memset (current_dir_block, 0, metadata->count);
1185 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
1187 if ((void *)((char *)current_dir_block+metadata->count) < final_dir_block)
1190 if (!FcFontSetSerialize (bank, set))
1193 return current_dir_block;
1196 free (current_dir_block);
1200 /* write serialized state to the cache file */
1202 FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
1206 int fd, fd_orig, i, dirs_count;
1209 off_t current_arch_start = 0, truncate_to;
1210 char name_buf[FC_MAX_FILE_LEN];
1213 char *current_arch_machine_name, * header;
1214 void *current_dir_block = 0;
1216 dir = FcConfigNormalizeFontDir (FcConfigGetCurrent(), dir);
1220 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
1224 /* Ensure that we're not trampling a cache for some other dir. */
1225 /* This is slightly different from FcDirCacheOpen, since it
1226 * needs the filename, not the file descriptor. */
1227 fd = -1; collisions = 0;
1230 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
1236 fd = open(cache_hashed, O_RDONLY);
1239 if(!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
1246 if (strcmp (name_buf, cache_file) != 0)
1250 current_dir_block = FcDirCacheProduce (set, &metadata);
1252 if (metadata.count && !current_dir_block)
1255 if (FcDebug () & FC_DBG_CACHE)
1256 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
1258 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
1262 if (!FcAtomicLock (atomic))
1264 /* Now try rewriting the original version of the file. */
1265 FcAtomicDestroy (atomic);
1267 atomic = FcAtomicCreate ((FcChar8 *)cache_file);
1268 fd_orig = open (cache_file, O_RDONLY);
1270 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1272 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1277 /* In all cases, try opening the real location of the cache file first. */
1278 /* (even if that's not atomic.) */
1279 fd_orig = open (cache_file, O_RDONLY);
1281 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1283 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1287 FcCacheWriteString (fd, cache_file);
1289 current_arch_machine_name = FcCacheMachineSignature ();
1290 current_arch_start = 0;
1293 current_arch_start =
1294 FcCacheSkipToArch(fd_orig, current_arch_machine_name);
1296 if (current_arch_start < 0)
1298 off_t i = lseek(fd_orig, 0, SEEK_END);
1299 if (i < strlen (FC_GLOBAL_MAGIC_COOKIE)+1)
1300 i = strlen (FC_GLOBAL_MAGIC_COOKIE)+1;
1301 current_arch_start = FcCacheNextOffset (i);
1304 if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
1310 current_arch_start = lseek(fd, 0, SEEK_CUR);
1311 if (ftruncate (fd, current_arch_start) == -1)
1314 /* allocate space for subdir names in this block */
1316 for (i = 0; i < dirs->size; i++)
1317 dirs_count += strlen((char *)dirs->strs[i]) + 1;
1320 /* now write the address of the next offset */
1321 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start;
1322 header = malloc (10 + strlen (current_arch_machine_name));
1325 sprintf (header, "%8x ", (int)truncate_to);
1326 strcat (header, current_arch_machine_name);
1327 if (!FcCacheWriteString (fd, header))
1330 for (i = 0; i < dirs->size; i++)
1331 FcCacheWriteString (fd, (char *)dirs->strs[i]);
1332 FcCacheWriteString (fd, "");
1334 if (write (fd, &metadata, sizeof(FcCache)) != sizeof(FcCache))
1336 perror("write metadata");
1341 off_t off = FcCacheNextOffset (lseek(fd, 0, SEEK_END));
1342 if (lseek (fd, off, SEEK_SET) != off)
1344 else if (write (fd, current_dir_block, metadata.count) !=
1346 perror("write current_dir_block");
1347 free (current_dir_block);
1350 /* this actually serves to pad out the cache file, if needed */
1351 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
1356 if (!FcAtomicReplaceOrig(atomic))
1358 FcStrFree ((FcChar8 *)cache_hashed);
1359 FcStrFree ((FcChar8 *)cache_file);
1360 FcAtomicUnlock (atomic);
1361 FcAtomicDestroy (atomic);
1369 FcAtomicUnlock (atomic);
1371 FcAtomicDestroy (atomic);
1373 FcStrFree ((FcChar8 *)cache_hashed);
1375 unlink ((char *)cache_file);
1376 FcStrFree ((FcChar8 *)cache_file);
1377 if (current_dir_block)
1378 free (current_dir_block);
1384 FcCacheMachineSignature ()
1386 static char buf[MACHINE_SIGNATURE_SIZE];
1387 int32_t magic = ENDIAN_TEST;
1388 char * m = (char *)&magic;
1390 sprintf (buf, "%2x%2x%2x%2x "
1391 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
1392 "%4x %4x %4x %4x %4x %4x %4x %4x\n",
1393 m[0], m[1], m[2], m[3],
1394 (unsigned int)sizeof (char),
1395 (unsigned int)sizeof (char *),
1396 (unsigned int)sizeof (int),
1397 (unsigned int)sizeof (FcPattern),
1398 (unsigned int)sizeof (FcPatternEltPtr),
1399 (unsigned int)sizeof (struct _FcPatternElt *),
1400 (unsigned int)sizeof (FcPatternElt),
1401 (unsigned int)sizeof (FcObjectPtr),
1402 (unsigned int)sizeof (FcValueListPtr),
1403 (unsigned int)sizeof (FcValue),
1404 (unsigned int)sizeof (FcValueBinding),
1405 (unsigned int)sizeof (struct _FcValueList *),
1406 (unsigned int)sizeof (FcCharSet),
1407 (unsigned int)sizeof (FcCharLeaf **),
1408 (unsigned int)sizeof (FcChar16 *),
1409 (unsigned int)sizeof (FcChar16),
1410 (unsigned int)sizeof (FcCharLeaf),
1411 (unsigned int)sizeof (FcChar32),
1412 (unsigned int)sizeof (FcCache),
1413 (unsigned int)sysconf(_SC_PAGESIZE));
1418 static int banks_ptr = 0, banks_alloc = 0;
1419 int * _fcBankId = 0, * _fcBankIdx = 0;
1420 static const char ** bankDirs = 0;
1423 FcCacheHaveBank (int bank)
1427 if (bank < FC_BANK_FIRST)
1430 for (i = 0; i < banks_ptr; i++)
1431 if (_fcBankId[i] == bank)
1438 FcCacheBankToIndexMTF (int bank)
1442 for (i = 0; i < banks_ptr; i++)
1443 if (_fcBankId[_fcBankIdx[i]] == bank)
1445 int t = _fcBankIdx[i];
1447 for (j = i; j > 0; j--)
1448 _fcBankIdx[j] = _fcBankIdx[j-1];
1453 if (banks_ptr >= banks_alloc)
1458 b = realloc (_fcBankId, (banks_alloc + 4) * sizeof(int));
1463 bidx = realloc (_fcBankIdx, (banks_alloc + 4) * sizeof(int));
1468 bds = realloc (bankDirs, (banks_alloc + 4) * sizeof (char *));
1477 _fcBankId[i] = bank;
1483 FcCacheAddBankDir (int bank, const char * dir)
1485 int bi = FcCacheBankToIndexMTF (bank);
1490 bankDirs[bi] = (const char *)FcStrCopy ((FcChar8 *)dir);
1494 FcCacheFindBankDir (int bank)
1496 int bi = FcCacheBankToIndex (bank);
1497 return bankDirs[bi];
1501 * This code implements the MD5 message-digest algorithm.
1502 * The algorithm is due to Ron Rivest. This code was
1503 * written by Colin Plumb in 1993, no copyright is claimed.
1504 * This code is in the public domain; do with it what you wish.
1506 * Equivalent code is available from RSA Data Security, Inc.
1507 * This code has been tested against that, and is equivalent,
1508 * except that you don't need to include two pages of legalese
1511 * To compute the message digest of a chunk of bytes, declare an
1512 * MD5Context structure, pass it to MD5Init, call MD5Update as
1513 * needed on buffers full of bytes, and then call MD5Final, which
1514 * will fill a supplied 16-byte array with the digest.
1518 #define byteReverse(buf, len) /* Nothing */
1521 * Note: this code is harmless on little-endian machines.
1523 void byteReverse(unsigned char *buf, unsigned longs)
1527 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1528 ((unsigned) buf[1] << 8 | buf[0]);
1529 *(FcChar32 *) buf = t;
1536 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1537 * initialization constants.
1539 static void MD5Init(struct MD5Context *ctx)
1541 ctx->buf[0] = 0x67452301;
1542 ctx->buf[1] = 0xefcdab89;
1543 ctx->buf[2] = 0x98badcfe;
1544 ctx->buf[3] = 0x10325476;
1551 * Update context to reflect the concatenation of another buffer full
1554 static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len)
1558 /* Update bitcount */
1561 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1562 ctx->bits[1]++; /* Carry from low to high */
1563 ctx->bits[1] += len >> 29;
1565 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1567 /* Handle any leading odd-sized chunks */
1570 unsigned char *p = (unsigned char *) ctx->in + t;
1574 memcpy(p, buf, len);
1578 byteReverse(ctx->in, 16);
1579 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1583 /* Process data in 64-byte chunks */
1586 memcpy(ctx->in, buf, 64);
1587 byteReverse(ctx->in, 16);
1588 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1593 /* Handle any remaining bytes of data. */
1595 memcpy(ctx->in, buf, len);
1599 * Final wrapup - pad to 64-byte boundary with the bit pattern
1600 * 1 0* (64-bit count of bits processed, MSB-first)
1602 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1607 /* Compute number of bytes mod 64 */
1608 count = (ctx->bits[0] >> 3) & 0x3F;
1610 /* Set the first char of padding to 0x80. This is safe since there is
1611 always at least one byte free */
1612 p = ctx->in + count;
1615 /* Bytes of padding needed to make 64 bytes */
1616 count = 64 - 1 - count;
1618 /* Pad out to 56 mod 64 */
1620 /* Two lots of padding: Pad the first block to 64 bytes */
1621 memset(p, 0, count);
1622 byteReverse(ctx->in, 16);
1623 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1625 /* Now fill the next block with 56 bytes */
1626 memset(ctx->in, 0, 56);
1628 /* Pad block to 56 bytes */
1629 memset(p, 0, count - 8);
1631 byteReverse(ctx->in, 14);
1633 /* Append length in bits and transform */
1634 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1635 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1637 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1638 byteReverse((unsigned char *) ctx->buf, 4);
1639 memcpy(digest, ctx->buf, 16);
1640 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
1644 /* The four core functions - F1 is optimized somewhat */
1646 /* #define F1(x, y, z) (x & y | ~x & z) */
1647 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1648 #define F2(x, y, z) F1(z, x, y)
1649 #define F3(x, y, z) (x ^ y ^ z)
1650 #define F4(x, y, z) (y ^ (x | ~z))
1652 /* This is the central step in the MD5 algorithm. */
1653 #define MD5STEP(f, w, x, y, z, data, s) \
1654 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1657 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1658 * reflect the addition of 16 longwords of new data. MD5Update blocks
1659 * the data and converts bytes into longwords for this routine.
1661 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1663 register FcChar32 a, b, c, d;
1670 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1671 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1672 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1673 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1674 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1675 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1676 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1677 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1678 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1679 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1680 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1681 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1682 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1683 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1684 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1685 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1687 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1688 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1689 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1690 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1691 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1692 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1693 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1694 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1695 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1696 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1697 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1698 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1699 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1700 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1701 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1702 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1704 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1705 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1706 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1707 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1708 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1709 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1710 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1711 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1712 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1713 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1714 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1715 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1716 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1717 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1718 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1719 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1721 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1722 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1723 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1724 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1725 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1726 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1727 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1728 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1729 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1730 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1731 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1732 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1733 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1734 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1735 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1736 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);