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))
255 FcStrSetAdd (staleDirs, FcStrCopy ((FcChar8 *)name_buf));
256 read (cache->fd, &md, sizeof (FcCache));
257 lseek (cache->fd, FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count, SEEK_SET);
261 d = malloc (sizeof (FcGlobalCacheDir));
265 d->next = cache->dirs;
268 d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
271 d->subdirs = FcStrSetCreate();
274 if (!FcCacheReadString (cache->fd, subdirName,
275 sizeof (subdirName)) ||
276 !strlen (subdirName))
278 FcStrSetAdd (d->subdirs, (FcChar8 *)subdirName);
281 d->offset = lseek (cache->fd, 0, SEEK_CUR);
282 if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
284 targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
285 if (lseek (cache->fd, targ, SEEK_SET) != targ)
291 for (d = cache->dirs; d; d = next)
306 if (stat ((char *) cache_file, &cache_stat) == 0)
307 unlink ((char *)cache_file);
314 FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config)
317 FcBool ret = FcFalse;
323 if (!(dir = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)dir)))
324 return FcFalse; /* non-existing directory */
326 for (d = cache->dirs; d; d = d->next)
328 if (strncmp (d->name, dir, strlen(dir)) == 0)
330 lseek (cache->fd, d->offset, SEEK_SET);
331 if (!FcDirCacheConsume (cache->fd, d->name, set, config))
334 if (strcmp (d->name, dir) == 0)
336 for (i = 0; i < d->subdirs->num; i++)
337 FcStrSetAdd (dirs, (FcChar8 *)d->subdirs->strs[i]);
348 FcGlobalCacheUpdate (FcGlobalCache *cache,
357 name = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)name);
358 for (d = cache->dirs; d; d = d->next)
360 if (strcmp(d->name, name) == 0)
366 d = malloc (sizeof (FcGlobalCacheDir));
369 d->next = cache->dirs;
373 cache->updated = FcTrue;
375 d->name = (char *)FcStrCopy ((FcChar8 *)name);
376 d->ent = FcDirCacheProduce (set, &d->metadata);
378 d->subdirs = FcStrSetCreate();
379 for (i = 0; i < dirs->num; i++)
380 FcStrSetAdd (d->subdirs, dirs->strs[i]);
385 FcGlobalCacheSave (FcGlobalCache *cache,
386 const FcChar8 *cache_file,
390 FcGlobalCacheDir *dir;
392 off_t current_arch_start = 0, truncate_to;
393 char * current_arch_machine_name, * header;
398 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
399 /* Set-UID programs can't safely update the cache */
400 if (getuid () != geteuid ())
404 atomic = FcAtomicCreate (cache_file);
408 if (!FcAtomicLock (atomic))
410 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT,
414 FcCacheWriteString (fd, FC_GLOBAL_MAGIC_COOKIE);
416 fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY);
418 current_arch_machine_name = FcCacheMachineSignature ();
420 current_arch_start = 0;
422 current_arch_start = FcCacheSkipToArch (fd_orig,
423 current_arch_machine_name);
425 if (current_arch_start < 0)
427 off_t i = lseek(fd_orig, 0, SEEK_END);
428 if (i < strlen (FC_GLOBAL_MAGIC_COOKIE)+1)
429 i = strlen (FC_GLOBAL_MAGIC_COOKIE)+1;
430 current_arch_start = FcCacheNextOffset (i);
433 if (!FcCacheCopyOld(fd, fd_orig, current_arch_start))
439 current_arch_start = lseek(fd, 0, SEEK_CUR);
440 if (ftruncate (fd, current_arch_start) == -1)
443 header = malloc (10 + strlen (current_arch_machine_name));
447 truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11;
448 for (dir = cache->dirs; dir; dir = dir->next)
450 truncate_to += strlen(dir->name) + 1;
451 truncate_to += sizeof (FcCache);
452 truncate_to = FcCacheNextOffset (truncate_to);
453 truncate_to += dir->metadata.count;
455 for (i = 0; i < dir->subdirs->size; i++)
456 truncate_to += strlen((char *)dir->subdirs->strs[i]) + 1;
459 truncate_to -= current_arch_start;
461 sprintf (header, "%8x ", (int)truncate_to);
462 strcat (header, current_arch_machine_name);
463 if (!FcCacheWriteString (fd, header))
466 for (dir = cache->dirs; dir; dir = dir->next)
470 const char * d = (const char *)FcConfigNormalizeFontDir (config, (const FcChar8 *)dir->name);
472 FcCacheWriteString (fd, d);
474 for (i = 0; i < dir->subdirs->size; i++)
475 FcCacheWriteString (fd, (char *)dir->subdirs->strs[i]);
476 FcCacheWriteString (fd, "");
478 write (fd, &dir->metadata, sizeof(FcCache));
479 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET);
480 write (fd, dir->ent, dir->metadata.count);
484 FcCacheWriteString (fd, "");
486 if (close (fd) == -1)
489 if (!FcAtomicReplaceOrig (atomic))
492 FcAtomicUnlock (atomic);
493 FcAtomicDestroy (atomic);
495 cache->updated = FcFalse;
506 FcAtomicDeleteNew (atomic);
508 FcAtomicUnlock (atomic);
510 FcAtomicDestroy (atomic);
515 * Find the next presumably-mmapable offset after the supplied file
519 FcCacheNextOffset(off_t w)
521 static long pagesize = -1;
523 pagesize = sysconf(_SC_PAGESIZE);
524 if (w % pagesize == 0)
527 return ((w / pagesize)+1)*pagesize;
530 /* return the address of the segment for the provided arch,
531 * or -1 if arch not found */
533 FcCacheSkipToArch (int fd, const char * arch)
535 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
536 char * candidate_arch;
537 off_t current_arch_start = 0;
539 lseek (fd, 0, SEEK_SET);
540 FcCacheSkipString (fd);
541 current_arch_start = lseek (fd, 0, SEEK_CUR);
543 /* skip arches that are not the current arch */
548 if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
551 if (FcCacheReadString (fd, candidate_arch_machine_name_count,
552 sizeof (candidate_arch_machine_name_count)) == 0)
554 if (!strlen(candidate_arch_machine_name_count))
556 bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16);
558 // count = 0 should probably be distinguished from the !bs condition
559 if (!bs || bs < strlen (candidate_arch_machine_name_count))
562 candidate_arch++; /* skip leading space */
564 if (strcmp (candidate_arch, arch)==0)
565 return current_arch_start;
566 current_arch_start += bs;
572 /* Cuts out the segment at the file pointer (moves everything else
573 * down to cover it), and leaves the file pointer at the end of the
576 FcCacheCopyOld (int fd, int fd_orig, off_t start)
578 char * buf = malloc (8192);
579 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
581 int c, bytes_skipped;
588 lseek (fd, 0, SEEK_SET); lseek (fd_orig, 0, SEEK_SET);
595 if ((c = read (fd_orig, buf, b)) <= 0)
597 if (write (fd, buf, c) < 0)
604 lseek (fd, start, SEEK_SET);
605 if (FcCacheReadString (fd, candidate_arch_machine_name,
606 sizeof (candidate_arch_machine_name)) == 0)
608 if (!strlen(candidate_arch_machine_name))
611 bs = strtol(candidate_arch_machine_name, 0, 16);
618 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
619 if ((c = read (fd, buf, 8192)) <= 0)
621 lseek (fd, start+bytes_skipped, SEEK_SET);
622 if (write (fd, buf, c) < 0)
627 lseek (fd, start+bytes_skipped, SEEK_SET);
638 /* Does not check that the cache has the appropriate arch section. */
639 /* Also, this can be fooled if the original location has a stale
640 * cache, and the hashed location has an up-to-date cache. Oh well,
641 * sucks to be you in that case! */
643 FcDirCacheValid (const FcChar8 *dir)
645 struct stat file_stat, dir_stat;
648 if (stat ((char *) dir, &dir_stat) < 0)
651 fd = FcDirCacheOpen (dir);
655 if (fstat (fd, &file_stat) < 0)
661 * If the directory has been modified more recently than
662 * the cache file, the cache is not valid
664 if (dir_stat.st_mtime > file_stat.st_mtime)
674 /* Assumes that the cache file in 'dir' exists.
675 * Checks that the cache has the appropriate arch section. */
677 FcDirCacheHasCurrentArch (const FcChar8 *dir)
680 off_t current_arch_start;
681 char *current_arch_machine_name;
683 fd = FcDirCacheOpen (dir);
687 current_arch_machine_name = FcCacheMachineSignature();
688 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
691 if (current_arch_start < 0)
701 FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
704 char *cache_hashed = 0;
706 struct stat cache_stat;
707 char name_buf[FC_MAX_FILE_LEN];
709 dir = FcConfigNormalizeFontDir (config, dir);
710 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
714 /* First remove normal cache file. */
715 if (stat ((char *) cache_file, &cache_stat) == 0 &&
716 unlink ((char *)cache_file) != 0)
719 /* Next remove any applicable hashed files. */
720 fd = -1; collisions = 0;
724 FcStrFree ((FcChar8 *)cache_hashed);
726 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
732 fd = open(cache_hashed, O_RDONLY);
735 FcStrFree ((FcChar8 *)cache_file);
739 if (!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
741 FcStrFree ((FcChar8 *)cache_hashed);
744 } while (strcmp (name_buf, cache_file) != 0);
746 FcStrFree ((FcChar8 *)cache_file);
749 if (stat ((char *) cache_hashed, &cache_stat) == 0 &&
750 unlink ((char *)cache_hashed) != 0)
752 FcStrFree ((FcChar8 *)cache_hashed);
756 FcStrFree ((FcChar8 *)cache_hashed);
760 FcStrFree ((FcChar8 *)cache_file);
765 FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
766 FcStrList *list, FcFontSet * set)
770 FcChar8 *file, *base;
776 * Read in the results from 'list'.
778 while ((dir = FcStrListNext (list)))
780 if (!FcConfigAcceptFilename (config, dir))
784 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
788 strcpy ((char *) file, (char *) dir);
789 strcat ((char *) file, "/");
790 base = file + strlen ((char *) file);
792 subdirs = FcStrSetCreate ();
795 fprintf (stderr, "Can't create directory set\n");
801 if (access ((char *) dir, X_OK) < 0)
809 fprintf (stderr, "\"%s\": ", dir);
813 FcStrSetDestroy (subdirs);
817 if (stat ((char *) dir, &statb) == -1)
819 fprintf (stderr, "\"%s\": ", dir);
821 FcStrSetDestroy (subdirs);
826 if (!S_ISDIR (statb.st_mode))
828 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
829 FcStrSetDestroy (subdirs);
833 if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir, config))
835 if (FcDebug () & FC_DBG_FONTSET)
836 printf ("cache scan dir %s\n", dir);
838 FcDirScanConfig (set, subdirs, cache,
839 config->blanks, dir, FcFalse, config);
841 sublist = FcStrListCreate (subdirs);
842 FcStrSetDestroy (subdirs);
845 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
850 ret += FcCacheReadDirs (config, cache, sublist, set);
853 FcStrListDone (list);
858 FcCacheRead (FcConfig *config, FcGlobalCache * cache)
860 FcFontSet * s = FcFontSetCreate();
864 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
870 FcFontSetDestroy (s);
874 static const char bin2hex[] = { '0', '1', '2', '3',
877 'c', 'd', 'e', 'f' };
880 FcDirCacheHashName (char * cache_file, int collisions)
882 unsigned char hash[16], hex_hash[33];
884 unsigned char uscore = '_';
887 struct MD5Context ctx;
890 MD5Update (&ctx, (unsigned char *)cache_file, strlen (cache_file));
892 for (i = 0; i < collisions; i++)
893 MD5Update (&ctx, &uscore, 1);
895 MD5Final (hash, &ctx);
897 for (cnt = 0; cnt < 16; ++cnt)
899 hex_hash[2*cnt] = bin2hex[hash[cnt] >> 4];
900 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
904 tmp = FcStrPlus ((FcChar8 *)hex_hash, (FcChar8 *)FC_CACHE_SUFFIX);
908 cache_hashed = (char *)FcStrPlus ((FcChar8 *)PKGCACHEDIR"/", tmp);
914 /* Opens the hashed name for cache_file.
915 * This would fail in the unlikely event of a collision and subsequent
916 * removal of the file which originally caused the collision. */
918 FcDirCacheOpen (const FcChar8 *dir)
921 int fd = -1, collisions = 0;
922 char *cache_file, *cache_hashed;
923 char name_buf[FC_MAX_FILE_LEN];
924 struct stat dir_stat;
926 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
930 fd = open(cache_file, O_RDONLY);
934 if (stat ((char *)dir, &dir_stat) == -1)
941 FcChar8 * name_buf_dir;
943 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
946 FcStrFree ((FcChar8 *)cache_file);
952 fd = open(cache_hashed, O_RDONLY);
953 FcStrFree ((FcChar8 *)cache_hashed);
957 FcStrFree ((FcChar8 *)cache_file);
960 if (!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
963 name_buf_dir = FcStrDirname ((FcChar8 *)name_buf);
964 if (stat ((char *)name_buf_dir, &c) == -1)
966 FcStrFree (name_buf_dir);
969 FcStrFree (name_buf_dir);
970 found = (c.st_ino == dir_stat.st_ino) && (c.st_dev == dir_stat.st_dev);
972 FcStrFree ((FcChar8 *)cache_file);
976 FcStrFree ((FcChar8 *)cache_file);
981 /* read serialized state from the cache file */
983 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *config)
986 char *current_arch_machine_name;
987 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
988 off_t current_arch_start = 0;
989 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
991 fd = FcDirCacheOpen (dir);
995 current_arch_machine_name = FcCacheMachineSignature();
996 current_arch_start = FcCacheSkipToArch(fd,
997 current_arch_machine_name);
998 if (current_arch_start < 0)
1001 lseek (fd, current_arch_start, SEEK_SET);
1002 if (FcCacheReadString (fd, candidate_arch_machine_name,
1003 sizeof (candidate_arch_machine_name)) == 0)
1006 while (FcCacheReadString (fd, subdirName, sizeof (subdirName)) && strlen (subdirName) > 0)
1007 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
1009 if (!FcDirCacheConsume (fd, (const char *)dir, set, config))
1022 FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config)
1025 void * current_dir_block;
1028 read(fd, &metadata, sizeof(FcCache));
1029 if (metadata.magic != FC_CACHE_MAGIC)
1032 if (!metadata.count)
1034 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
1035 lseek (fd, pos, SEEK_SET);
1037 FcConfigAddFontDir (config, (FcChar8 *)dir);
1041 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
1042 current_dir_block = mmap (0, metadata.count,
1043 PROT_READ, MAP_SHARED, fd, pos);
1044 lseek (fd, pos+metadata.count, SEEK_SET);
1045 if (current_dir_block == MAP_FAILED)
1048 FcCacheAddBankDir (metadata.bank, dir);
1050 FcConfigAddFontDir (config, (FcChar8 *)dir);
1052 if (!FcFontSetUnserialize (&metadata, set, current_dir_block))
1059 FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
1061 void * current_dir_block, * final_dir_block;
1062 static unsigned int rand_state = 0;
1063 int bank, needed_bytes_no_align;
1066 rand_state = time(0L);
1067 bank = rand_r(&rand_state);
1069 while (FcCacheHaveBank(bank))
1070 bank = rand_r(&rand_state);
1072 memset (metadata, 0, sizeof(FcCache));
1074 needed_bytes_no_align = FcFontSetNeededBytes (set);
1075 metadata->count = needed_bytes_no_align +
1076 FcFontSetNeededBytesAlign ();
1077 metadata->magic = FC_CACHE_MAGIC;
1078 metadata->bank = bank;
1080 if (!needed_bytes_no_align) /* not a failure, no fonts to write */
1082 /* no, you don't really need to write any bytes at all. */
1083 metadata->count = 0;
1087 current_dir_block = malloc (metadata->count);
1088 if (!current_dir_block)
1091 memset (current_dir_block, 0, metadata->count);
1092 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
1094 if ((void *)((char *)current_dir_block+metadata->count) < final_dir_block)
1097 if (!FcFontSetSerialize (bank, set))
1100 return current_dir_block;
1103 free (current_dir_block);
1107 /* write serialized state to the cache file */
1109 FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
1113 int fd, fd_orig, i, dirs_count;
1116 off_t current_arch_start = 0, truncate_to;
1117 char name_buf[FC_MAX_FILE_LEN];
1120 char *current_arch_machine_name, * header;
1121 void *current_dir_block = 0;
1123 dir = FcConfigNormalizeFontDir (FcConfigGetCurrent(), dir);
1127 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
1131 /* Ensure that we're not trampling a cache for some other dir. */
1132 /* This is slightly different from FcDirCacheOpen, since it
1133 * needs the filename, not the file descriptor. */
1134 fd = -1; collisions = 0;
1137 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
1143 fd = open(cache_hashed, O_RDONLY);
1146 if(!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
1153 if (strcmp (name_buf, cache_file) != 0)
1157 current_dir_block = FcDirCacheProduce (set, &metadata);
1159 if (metadata.count && !current_dir_block)
1162 if (FcDebug () & FC_DBG_CACHE)
1163 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
1165 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
1169 if (!FcAtomicLock (atomic))
1171 /* Now try rewriting the original version of the file. */
1172 FcAtomicDestroy (atomic);
1174 atomic = FcAtomicCreate ((FcChar8 *)cache_file);
1175 fd_orig = open (cache_file, O_RDONLY);
1177 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1179 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1184 /* In all cases, try opening the real location of the cache file first. */
1185 /* (even if that's not atomic.) */
1186 fd_orig = open (cache_file, O_RDONLY);
1188 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1190 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1194 FcCacheWriteString (fd, cache_file);
1196 current_arch_machine_name = FcCacheMachineSignature ();
1197 current_arch_start = 0;
1200 current_arch_start =
1201 FcCacheSkipToArch(fd_orig, current_arch_machine_name);
1203 if (current_arch_start < 0)
1205 off_t i = lseek(fd_orig, 0, SEEK_END);
1206 if (i < strlen (FC_GLOBAL_MAGIC_COOKIE)+1)
1207 i = strlen (FC_GLOBAL_MAGIC_COOKIE)+1;
1208 current_arch_start = FcCacheNextOffset (i);
1211 if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
1217 current_arch_start = lseek(fd, 0, SEEK_CUR);
1218 if (ftruncate (fd, current_arch_start) == -1)
1221 /* allocate space for subdir names in this block */
1223 for (i = 0; i < dirs->size; i++)
1224 dirs_count += strlen((char *)dirs->strs[i]) + 1;
1227 /* now write the address of the next offset */
1228 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start;
1229 header = malloc (10 + strlen (current_arch_machine_name));
1232 sprintf (header, "%8x ", (int)truncate_to);
1233 strcat (header, current_arch_machine_name);
1234 if (!FcCacheWriteString (fd, header))
1237 for (i = 0; i < dirs->size; i++)
1238 FcCacheWriteString (fd, (char *)dirs->strs[i]);
1239 FcCacheWriteString (fd, "");
1241 write (fd, &metadata, sizeof(FcCache));
1244 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
1245 write (fd, current_dir_block, metadata.count);
1246 free (current_dir_block);
1249 /* this actually serves to pad out the cache file, if needed */
1250 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
1255 if (!FcAtomicReplaceOrig(atomic))
1257 FcStrFree ((FcChar8 *)cache_hashed);
1258 FcStrFree ((FcChar8 *)cache_file);
1259 FcAtomicUnlock (atomic);
1260 FcAtomicDestroy (atomic);
1268 FcAtomicUnlock (atomic);
1270 FcAtomicDestroy (atomic);
1272 FcStrFree ((FcChar8 *)cache_hashed);
1274 unlink ((char *)cache_file);
1275 FcStrFree ((FcChar8 *)cache_file);
1276 if (current_dir_block)
1277 free (current_dir_block);
1283 FcCacheMachineSignature ()
1285 static char buf[MACHINE_SIGNATURE_SIZE];
1286 int32_t magic = ENDIAN_TEST;
1287 char * m = (char *)&magic;
1289 sprintf (buf, "%2x%2x%2x%2x "
1290 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
1291 "%4x %4x %4x %4x %4x %4x %4x %4x\n",
1292 m[0], m[1], m[2], m[3],
1293 (unsigned int)sizeof (char),
1294 (unsigned int)sizeof (char *),
1295 (unsigned int)sizeof (int),
1296 (unsigned int)sizeof (FcPattern),
1297 (unsigned int)sizeof (FcPatternEltPtr),
1298 (unsigned int)sizeof (struct _FcPatternElt *),
1299 (unsigned int)sizeof (FcPatternElt),
1300 (unsigned int)sizeof (FcObjectPtr),
1301 (unsigned int)sizeof (FcValueListPtr),
1302 (unsigned int)sizeof (FcValue),
1303 (unsigned int)sizeof (FcValueBinding),
1304 (unsigned int)sizeof (struct _FcValueList *),
1305 (unsigned int)sizeof (FcCharSet),
1306 (unsigned int)sizeof (FcCharLeaf **),
1307 (unsigned int)sizeof (FcChar16 *),
1308 (unsigned int)sizeof (FcChar16),
1309 (unsigned int)sizeof (FcCharLeaf),
1310 (unsigned int)sizeof (FcChar32),
1311 (unsigned int)sizeof (FcCache),
1312 (unsigned int)sysconf(_SC_PAGESIZE));
1317 static int banks_ptr = 0, banks_alloc = 0;
1318 int * _fcBankId = 0, * _fcBankIdx = 0;
1319 static const char ** bankDirs = 0;
1322 FcCacheHaveBank (int bank)
1326 if (bank < FC_BANK_FIRST)
1329 for (i = 0; i < banks_ptr; i++)
1330 if (_fcBankId[i] == bank)
1337 FcCacheBankToIndexMTF (int bank)
1341 for (i = 0; i < banks_ptr; i++)
1342 if (_fcBankId[_fcBankIdx[i]] == bank)
1344 int t = _fcBankIdx[i];
1346 for (j = i; j > 0; j--)
1347 _fcBankIdx[j] = _fcBankIdx[j-1];
1352 if (banks_ptr >= banks_alloc)
1357 b = realloc (_fcBankId, (banks_alloc + 4) * sizeof(int));
1362 bidx = realloc (_fcBankIdx, (banks_alloc + 4) * sizeof(int));
1367 bds = realloc (bankDirs, (banks_alloc + 4) * sizeof (char *));
1376 _fcBankId[i] = bank;
1382 FcCacheAddBankDir (int bank, const char * dir)
1384 int bi = FcCacheBankToIndexMTF (bank);
1389 bankDirs[bi] = (const char *)FcStrCopy ((FcChar8 *)dir);
1393 FcCacheFindBankDir (int bank)
1395 int bi = FcCacheBankToIndex (bank);
1396 return bankDirs[bi];
1400 * This code implements the MD5 message-digest algorithm.
1401 * The algorithm is due to Ron Rivest. This code was
1402 * written by Colin Plumb in 1993, no copyright is claimed.
1403 * This code is in the public domain; do with it what you wish.
1405 * Equivalent code is available from RSA Data Security, Inc.
1406 * This code has been tested against that, and is equivalent,
1407 * except that you don't need to include two pages of legalese
1410 * To compute the message digest of a chunk of bytes, declare an
1411 * MD5Context structure, pass it to MD5Init, call MD5Update as
1412 * needed on buffers full of bytes, and then call MD5Final, which
1413 * will fill a supplied 16-byte array with the digest.
1417 #define byteReverse(buf, len) /* Nothing */
1420 * Note: this code is harmless on little-endian machines.
1422 void byteReverse(unsigned char *buf, unsigned longs)
1426 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1427 ((unsigned) buf[1] << 8 | buf[0]);
1428 *(FcChar32 *) buf = t;
1435 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1436 * initialization constants.
1438 static void MD5Init(struct MD5Context *ctx)
1440 ctx->buf[0] = 0x67452301;
1441 ctx->buf[1] = 0xefcdab89;
1442 ctx->buf[2] = 0x98badcfe;
1443 ctx->buf[3] = 0x10325476;
1450 * Update context to reflect the concatenation of another buffer full
1453 static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len)
1457 /* Update bitcount */
1460 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1461 ctx->bits[1]++; /* Carry from low to high */
1462 ctx->bits[1] += len >> 29;
1464 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1466 /* Handle any leading odd-sized chunks */
1469 unsigned char *p = (unsigned char *) ctx->in + t;
1473 memcpy(p, buf, len);
1477 byteReverse(ctx->in, 16);
1478 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1482 /* Process data in 64-byte chunks */
1485 memcpy(ctx->in, buf, 64);
1486 byteReverse(ctx->in, 16);
1487 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1492 /* Handle any remaining bytes of data. */
1494 memcpy(ctx->in, buf, len);
1498 * Final wrapup - pad to 64-byte boundary with the bit pattern
1499 * 1 0* (64-bit count of bits processed, MSB-first)
1501 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1506 /* Compute number of bytes mod 64 */
1507 count = (ctx->bits[0] >> 3) & 0x3F;
1509 /* Set the first char of padding to 0x80. This is safe since there is
1510 always at least one byte free */
1511 p = ctx->in + count;
1514 /* Bytes of padding needed to make 64 bytes */
1515 count = 64 - 1 - count;
1517 /* Pad out to 56 mod 64 */
1519 /* Two lots of padding: Pad the first block to 64 bytes */
1520 memset(p, 0, count);
1521 byteReverse(ctx->in, 16);
1522 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1524 /* Now fill the next block with 56 bytes */
1525 memset(ctx->in, 0, 56);
1527 /* Pad block to 56 bytes */
1528 memset(p, 0, count - 8);
1530 byteReverse(ctx->in, 14);
1532 /* Append length in bits and transform */
1533 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1534 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1536 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1537 byteReverse((unsigned char *) ctx->buf, 4);
1538 memcpy(digest, ctx->buf, 16);
1539 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
1543 /* The four core functions - F1 is optimized somewhat */
1545 /* #define F1(x, y, z) (x & y | ~x & z) */
1546 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1547 #define F2(x, y, z) F1(z, x, y)
1548 #define F3(x, y, z) (x ^ y ^ z)
1549 #define F4(x, y, z) (y ^ (x | ~z))
1551 /* This is the central step in the MD5 algorithm. */
1552 #define MD5STEP(f, w, x, y, z, data, s) \
1553 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1556 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1557 * reflect the addition of 16 longwords of new data. MD5Update blocks
1558 * the data and converts bytes into longwords for this routine.
1560 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1562 register FcChar32 a, b, c, d;
1569 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1570 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1571 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1572 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1573 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1574 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1575 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1576 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1577 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1578 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1579 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1580 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1581 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1582 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1583 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1584 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1586 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1587 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1588 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1589 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1590 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1591 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1592 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1593 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1594 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1595 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1596 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1597 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1598 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1599 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1600 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1601 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1603 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1604 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1605 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1606 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1607 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1608 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1609 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1610 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1611 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1612 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1613 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1614 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1615 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1616 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1617 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1618 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1620 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1621 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1622 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1623 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1624 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1625 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1626 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1627 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1628 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1629 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1630 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1631 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1632 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1633 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1634 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1635 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);