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/types.h>
31 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
33 # include <sys/mman.h>
38 #define ENDIAN_TEST 0x12345678
39 #define MACHINE_SIGNATURE_SIZE (9 + 5*20 + 1)
40 /* for when we don't have sysconf: */
41 #define FC_HARDCODED_PAGESIZE 8192
48 FcDirCacheOpen (FcConfig *config, const FcChar8 *dir, FcChar8 **cache_path);
51 FcCacheSkipToArch (int fd, const char * arch);
54 FcCacheCopyOld (int fd, int fd_orig, off_t start);
57 FcDirCacheProduce (FcFontSet *set, FcCache * metadata);
60 FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config);
63 FcCacheNextOffset(off_t w);
66 FcCacheMachineSignature (void);
69 FcCacheHaveBank (int bank);
72 FcCacheAddBankDir (int bank, const char * dir);
80 static void MD5Init(struct MD5Context *ctx);
81 static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len);
82 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
83 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]);
85 #define FC_DBG_CACHE_REF 1024
88 FcCacheReadString (int fd, char *dest, int len)
96 size = read (fd, dest, len-1);
101 slen = strlen (dest);
103 lseek (fd, slen - size + 1, SEEK_CUR);
104 return slen < len ? dest : 0;
111 FcCacheSkipString (int fd)
117 while ( (size = read (fd, buf, sizeof (buf)-1)) > 0)
123 lseek (fd, slen - size + 1, SEEK_CUR);
130 FcCacheWriteString (int fd, const char *chars)
132 if (write (fd, chars, strlen(chars)+1) != strlen(chars)+1)
138 FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
140 FcStrSetDestroy (d->subdirs);
141 FcMemFree (FC_MEM_STRING, strlen (d->name)+1);
143 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir));
148 FcGlobalCacheCreate (void)
150 FcGlobalCache *cache;
152 cache = malloc (sizeof (FcGlobalCache));
155 FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCache));
157 cache->updated = FcFalse;
163 FcGlobalCacheDestroy (FcGlobalCache *cache)
165 FcGlobalCacheDir *d, *next;
167 for (d = cache->dirs; d; d = next)
170 FcGlobalCacheDirDestroy (d);
172 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache));
179 FcGlobalCacheLoad (FcGlobalCache *cache,
181 const FcChar8 *cache_file,
184 char name_buf[FC_MAX_FILE_LEN];
185 FcGlobalCacheDir *d, *next;
186 FcFileTime config_time = FcConfigModifiedTime (config);
187 char * current_arch_machine_name;
188 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
189 off_t current_arch_start;
191 struct stat cache_stat, dir_stat;
192 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
194 if (stat ((char *) cache_file, &cache_stat) < 0)
197 cache->fd = open ((char *) cache_file, O_RDONLY | O_BINARY);
201 cache->updated = FcFalse;
203 if (!FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)))
204 goto bail_and_destroy;
205 if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) != 0)
206 goto bail_and_destroy;
208 current_arch_machine_name = FcCacheMachineSignature ();
209 current_arch_start = FcCacheSkipToArch(cache->fd,
210 current_arch_machine_name);
211 if (current_arch_start < 0)
214 lseek (cache->fd, current_arch_start, SEEK_SET);
215 if (!FcCacheReadString (cache->fd, candidate_arch_machine_name,
216 sizeof (candidate_arch_machine_name)))
217 goto bail_and_destroy;
218 if (strlen(candidate_arch_machine_name) == 0)
219 goto bail_and_destroy;
225 if (!FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)) ||
229 /* Directory must be older than the global cache file; also
230 cache must be newer than the config file. */
231 if (stat ((char *) name_buf, &dir_stat) < 0 ||
232 dir_stat.st_mtime > cache_stat.st_mtime ||
233 (config_time.set && cache_stat.st_mtime < config_time.time))
238 FcStrSetAdd (staleDirs, (FcChar8 *)name_buf);
241 while (FcCacheReadString (cache->fd, subdirName,
242 sizeof (subdirName)) &&
246 if (read (cache->fd, &md, sizeof (FcCache)) != sizeof(FcCache))
248 perror ("read metadata");
251 off = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count;
252 if (lseek (cache->fd, off, SEEK_SET) != off)
260 d = malloc (sizeof (FcGlobalCacheDir));
264 d->next = cache->dirs;
267 d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
269 d->state = FcGCDirFileRead;
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)
322 for (d = cache->dirs; d; d = d->next)
324 if (strcmp (d->name, dir) == 0)
326 if (d->state == FcGCDirDisabled)
329 if (d->state == FcGCDirFileRead)
331 lseek (cache->fd, d->offset, SEEK_SET);
332 if (!FcDirCacheConsume (cache->fd, d->name, set, config))
335 for (i = 0; i < d->subdirs->num; i++)
336 FcStrSetAdd (dirs, (FcChar8 *)d->subdirs->strs[i]);
338 d->state = FcGCDirConsumed;
347 static FcGlobalCacheDir *
348 FcGlobalCacheDirFind (FcGlobalCache *cache, const char *name)
350 FcGlobalCacheDir * d;
355 for (d = cache->dirs; d; d = d->next)
356 if (strcmp((const char *)d->name, (const char *)name) == 0)
363 FcGlobalCacheUpdate (FcGlobalCache *cache,
372 d = FcGlobalCacheDirFind (cache, name);
376 d = malloc (sizeof (FcGlobalCacheDir));
379 d->next = cache->dirs;
382 /* free old resources */
383 FcStrFree ((FcChar8 *)d->name);
385 FcStrSetDestroy (d->subdirs);
388 cache->updated = FcTrue;
390 d->name = (char *)FcStrCopy ((FcChar8 *)name);
391 d->ent = FcDirCacheProduce (set, &d->metadata);
393 d->subdirs = FcStrSetCreate();
394 d->state = FcGCDirUpdated;
395 for (i = 0; i < dirs->num; i++)
396 FcStrSetAdd (d->subdirs, dirs->strs[i]);
401 FcGlobalCacheSave (FcGlobalCache *cache,
402 const FcChar8 *cache_file,
406 FcGlobalCacheDir *dir;
408 off_t current_arch_start = 0, truncate_to;
409 char * current_arch_machine_name, * header;
414 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
415 /* Set-UID programs can't safely update the cache */
416 if (getuid () != geteuid ())
420 atomic = FcAtomicCreate (cache_file);
424 if (!FcAtomicLock (atomic))
426 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT | O_BINARY,
430 FcCacheWriteString (fd, FC_GLOBAL_MAGIC_COOKIE);
432 fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY | O_BINARY);
434 current_arch_machine_name = FcCacheMachineSignature ();
436 current_arch_start = 0;
438 current_arch_start = FcCacheSkipToArch (fd_orig,
439 current_arch_machine_name);
441 if (current_arch_start < 0)
443 off_t i = lseek(fd_orig, 0, SEEK_END);
444 if (i < strlen (FC_GLOBAL_MAGIC_COOKIE)+1)
445 i = strlen (FC_GLOBAL_MAGIC_COOKIE)+1;
446 current_arch_start = FcCacheNextOffset (i);
449 if (!FcCacheCopyOld(fd, fd_orig, current_arch_start))
452 current_arch_start = lseek(fd, 0, SEEK_CUR);
453 #if defined (HAVE_FTRUNCATE)
454 if (ftruncate (fd, current_arch_start) == -1)
457 #if defined (HAVE_CHSIZE)
458 if (chsize (fd, current_arch_start) == -1)
465 header = malloc (10 + strlen (current_arch_machine_name));
469 truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11;
470 for (dir = cache->dirs; dir; dir = dir->next)
474 if (dir->state == FcGCDirDisabled)
476 truncate_to += strlen(dir->name) + 1;
477 truncate_to += sizeof (FcCache);
478 truncate_to = FcCacheNextOffset (truncate_to);
479 truncate_to += dir->metadata.count;
481 for (i = 0; i < dir->subdirs->size; i++)
482 truncate_to += strlen((char *)dir->subdirs->strs[i]) + 1;
485 truncate_to -= current_arch_start;
487 sprintf (header, "%8x ", (int)truncate_to);
488 strcat (header, current_arch_machine_name);
489 if (!FcCacheWriteString (fd, header))
494 for (dir = cache->dirs; dir; dir = dir->next)
500 if (!dir->name || dir->state == FcGCDirDisabled)
506 if (dir->metadata.count && !dir->ent)
508 if (dir->state == FcGCDirUpdated || fd_orig < 0)
510 fprintf(stderr, "Invalid metadata entry for %s, skipping...\n", d);
513 /* copy the old content */
514 dir->ent = malloc (dir->metadata.count);
517 perror("malloc error");
520 off = FcCacheNextOffset (dir->offset + sizeof(FcCache));
521 if (lseek (fd_orig, off, SEEK_SET) != off)
527 if (read (fd_orig, dir->ent, dir->metadata.count)
528 != dir->metadata.count)
536 FcCacheWriteString (fd, d);
538 for (i = 0; i < dir->subdirs->size; i++)
539 FcCacheWriteString (fd, (char *)dir->subdirs->strs[i]);
540 FcCacheWriteString (fd, "");
542 if (write (fd, &dir->metadata, sizeof(FcCache)) != sizeof(FcCache))
544 perror ("write metadata");
548 off = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
549 if (lseek (fd, off, SEEK_SET) != off)
555 if (dir->metadata.count)
557 if (write (fd, dir->ent, dir->metadata.count) != dir->metadata.count)
559 perror ("write dirent");
566 FcCacheWriteString (fd, "");
568 if (close (fd) == -1)
574 if (!FcAtomicReplaceOrig (atomic))
577 FcAtomicUnlock (atomic);
578 FcAtomicDestroy (atomic);
580 cache->updated = FcFalse;
591 FcAtomicDeleteNew (atomic);
593 FcAtomicUnlock (atomic);
595 FcAtomicDestroy (atomic);
600 * Find the next presumably-mmapable offset after the supplied file
604 FcCacheNextOffset(off_t w)
606 static long pagesize = -1;
612 #if defined (HAVE_SYSCONF)
613 pagesize = sysconf(_SC_PAGESIZE);
615 pagesize = FC_HARDCODED_PAGESIZE;
617 if (w % pagesize == 0)
620 return ((w / pagesize)+1)*pagesize;
623 /* return the address of the segment for the provided arch,
624 * or -1 if arch not found */
626 FcCacheSkipToArch (int fd, const char * arch)
628 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
629 char * candidate_arch;
630 off_t current_arch_start = 0;
632 lseek (fd, 0, SEEK_SET);
633 FcCacheSkipString (fd);
634 current_arch_start = lseek (fd, 0, SEEK_CUR);
636 /* skip arches that are not the current arch */
641 if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
644 if (FcCacheReadString (fd, candidate_arch_machine_name_count,
645 sizeof (candidate_arch_machine_name_count)) == 0)
647 if (!strlen(candidate_arch_machine_name_count))
649 bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16);
651 /* count = 0 should probably be distinguished from the !bs condition */
652 if (!bs || bs < strlen (candidate_arch_machine_name_count))
655 candidate_arch++; /* skip leading space */
657 if (strcmp (candidate_arch, arch)==0)
658 return current_arch_start;
659 current_arch_start += bs;
660 current_arch_start = FcCacheNextOffset (current_arch_start);
664 /* Cuts out the segment at the file pointer (moves everything else
665 * down to cover it), and leaves the file pointer at the end of the
668 FcCacheCopyOld (int fd, int fd_orig, off_t start)
670 char * buf = malloc (8192);
671 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
673 int c, bytes_skipped;
680 lseek (fd, 0, SEEK_SET); lseek (fd_orig, 0, SEEK_SET);
681 FcCacheSkipString (fd); FcCacheSkipString (fd_orig);
688 if ((c = read (fd_orig, buf, b)) <= 0)
690 if (write (fd, buf, c) < 0)
697 lseek (fd, start, SEEK_SET);
698 if (FcCacheReadString (fd, candidate_arch_machine_name,
699 sizeof (candidate_arch_machine_name)) == 0)
701 if (!strlen(candidate_arch_machine_name))
704 bs = strtol(candidate_arch_machine_name, 0, 16);
711 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
712 if ((c = read (fd, buf, 8192)) <= 0)
714 lseek (fd, start+bytes_skipped, SEEK_SET);
715 if (write (fd, buf, c) < 0)
720 lseek (fd, start+bytes_skipped, SEEK_SET);
731 /* Does not check that the cache has the appropriate arch section. */
733 FcDirCacheValid (const FcChar8 *dir, FcConfig *config)
737 fd = FcDirCacheOpen (config, dir, NULL);
746 /* Assumes that the cache file in 'dir' exists.
747 * Checks that the cache has the appropriate arch section. */
749 FcDirCacheHasCurrentArch (const FcChar8 *dir, FcConfig *config)
752 off_t current_arch_start;
753 char *current_arch_machine_name;
755 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
757 fd = FcDirCacheOpen (config, dir, NULL);
761 current_arch_machine_name = FcCacheMachineSignature();
762 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
764 if (current_arch_start >= 0)
766 if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
769 FcCacheSkipString (fd);
771 while (FcCacheReadString (fd, subdirName, sizeof (subdirName)) && strlen (subdirName) > 0)
774 if (read(fd, &metadata, sizeof(FcCache)) != sizeof(FcCache))
777 if (metadata.magic != FC_CACHE_MAGIC)
783 if (current_arch_start < 0)
794 #define CACHEBASE_LEN (1 + 32 + sizeof (FC_CACHE_SUFFIX))
796 static const char bin2hex[] = { '0', '1', '2', '3',
799 'c', 'd', 'e', 'f' };
802 FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN])
804 unsigned char hash[16];
807 struct MD5Context ctx;
810 MD5Update (&ctx, (unsigned char *)dir, strlen ((char *) dir));
812 MD5Final (hash, &ctx);
815 hex_hash = cache_base + 1;
816 for (cnt = 0; cnt < 16; ++cnt)
818 hex_hash[2*cnt ] = bin2hex[hash[cnt] >> 4];
819 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
822 strcat ((char *) cache_base, FC_CACHE_SUFFIX);
828 FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
831 FcChar8 *cache_hashed = NULL;
832 FcChar8 cache_base[CACHEBASE_LEN];
835 char dir_buf[FC_MAX_FILE_LEN];
837 FcDirCacheBasename (dir, cache_base);
839 list = FcStrListCreate (config->cacheDirs);
843 while ((cache_dir = FcStrListNext (list)))
845 cache_hashed = FcStrPlus (cache_dir, cache_base);
848 fd = open((char *) cache_hashed, O_RDONLY | O_BINARY);
849 FcStrFree (cache_hashed);
852 if (FcCacheReadString (fd, dir_buf, sizeof (dir_buf)) &&
853 strcmp (dir_buf, (char *) dir) == 0)
856 if (unlink ((char *) cache_hashed) < 0)
862 FcStrListDone (list);
863 /* return FcFalse if something went wrong */
870 FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
871 FcStrList *list, FcFontSet * set, FcStrSet *processed_dirs)
880 * Read in the results from 'list'.
882 while ((dir = FcStrListNext (list)))
884 if (!FcConfigAcceptFilename (config, dir))
887 /* Skip this directory if already updated
888 * to avoid the looped directories via symlinks
889 * Clearly a dir not in fonts.conf shouldn't be globally cached.
892 if (FcStrSetMember (processed_dirs, dir))
894 if (!FcStrSetAdd (processed_dirs, dir))
897 subdirs = FcStrSetCreate ();
900 fprintf (stderr, "Can't create directory set\n");
905 if (FcDirCacheRead (set, subdirs, dir, config))
907 /* if an old entry is found in the global cache, disable it */
908 if ((d = FcGlobalCacheDirFind (cache, (const char *)dir)) != NULL)
910 d->state = FcGCDirDisabled;
911 /* save the updated config later without this entry */
912 cache->updated = FcTrue;
917 if (FcDebug () & FC_DBG_FONTSET)
918 printf ("cache scan dir %s\n", dir);
920 FcDirScanConfig (set, subdirs, cache,
921 config->blanks, dir, FcFalse, config);
923 sublist = FcStrListCreate (subdirs);
924 FcStrSetDestroy (subdirs);
927 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
931 ret += FcCacheReadDirs (config, cache, sublist, set, processed_dirs);
933 FcStrListDone (list);
938 FcCacheRead (FcConfig *config, FcGlobalCache * cache)
940 FcFontSet *s = FcFontSetCreate();
941 FcStrSet *processed_dirs;
946 processed_dirs = FcStrSetCreate();
950 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s, processed_dirs))
953 FcStrSetDestroy (processed_dirs);
957 FcStrSetDestroy (processed_dirs);
959 FcFontSetDestroy (s);
963 /* Opens the cache file for 'dir' for reading.
964 * This searches the list of cache dirs for the relevant cache file,
965 * returning the first one found.
968 FcDirCacheOpen (FcConfig *config, const FcChar8 *dir, FcChar8 **cache_path)
971 FcChar8 *cache_hashed = NULL;
972 FcChar8 cache_base[CACHEBASE_LEN];
975 char dir_buf[FC_MAX_FILE_LEN];
976 struct stat file_stat, dir_stat;
978 if (stat ((char *) dir, &dir_stat) < 0)
981 FcDirCacheBasename (dir, cache_base);
983 list = FcStrListCreate (config->cacheDirs);
987 while ((cache_dir = FcStrListNext (list)))
989 cache_hashed = FcStrPlus (cache_dir, cache_base);
992 fd = open((char *) cache_hashed, O_RDONLY | O_BINARY);
994 if (fstat (fd, &file_stat) >= 0 &&
995 dir_stat.st_mtime <= file_stat.st_mtime)
1002 FcStrFree (cache_hashed);
1003 cache_hashed = NULL;
1005 FcStrListDone (list);
1010 if (!FcCacheReadString (fd, dir_buf, sizeof (dir_buf)) ||
1011 strcmp (dir_buf, (char *) dir) != 0)
1014 FcStrFree (cache_hashed);
1019 *cache_path = cache_hashed;
1021 FcStrFree (cache_hashed);
1026 /* read serialized state from the cache file */
1028 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *config)
1031 char *current_arch_machine_name;
1032 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
1033 off_t current_arch_start = 0;
1034 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
1036 fd = FcDirCacheOpen (config, dir, NULL);
1040 current_arch_machine_name = FcCacheMachineSignature();
1041 current_arch_start = FcCacheSkipToArch(fd,
1042 current_arch_machine_name);
1043 if (current_arch_start < 0)
1046 lseek (fd, current_arch_start, SEEK_SET);
1047 if (FcCacheReadString (fd, candidate_arch_machine_name,
1048 sizeof (candidate_arch_machine_name)) == 0)
1051 while (FcCacheReadString (fd, subdirName, sizeof (subdirName)) && strlen (subdirName) > 0)
1052 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
1054 if (!FcDirCacheConsume (fd, (const char *)dir, set, config))
1067 FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config)
1070 void * current_dir_block;
1073 if (read(fd, &metadata, sizeof(FcCache)) != sizeof(FcCache))
1075 if (metadata.magic != FC_CACHE_MAGIC)
1078 if (!metadata.count)
1080 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
1081 lseek (fd, pos, SEEK_SET);
1083 FcConfigAddFontDir (config, (FcChar8 *)dir);
1087 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
1089 /* This is not failsafe (multi-arches can break it),
1090 * but fd has got to have at least as many bytes as
1091 * metadata.count, or something's going to go horribly wrong. */
1092 if (pos == (off_t)-1)
1095 endpos = lseek (fd, 0, SEEK_END);
1096 if (endpos == (off_t)-1 || endpos - pos < metadata.count)
1098 if (lseek (fd, pos, SEEK_SET) == -1)
1101 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
1102 current_dir_block = mmap (0, metadata.count,
1103 PROT_READ, MAP_SHARED, fd, pos);
1104 if (current_dir_block == MAP_FAILED)
1106 #elif defined(_WIN32)
1110 hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL, PAGE_READONLY, 0, 0, NULL);
1111 if (hFileMap == NULL)
1114 current_dir_block = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0, metadata.count + pos);
1115 if (current_dir_block == NULL)
1117 CloseHandle (hFileMap);
1121 current_dir_block = (void *)((char *)current_dir_block + pos);
1124 current_dir_block = malloc (metadata.count);
1125 if (!current_dir_block)
1128 /* could also use CreateMappedViewOfFile under MinGW... */
1129 if (read (fd, current_dir_block, metadata.count) != metadata.count)
1132 lseek (fd, pos+metadata.count, SEEK_SET);
1134 FcCacheAddBankDir (metadata.bank, dir);
1136 FcConfigAddFontDir (config, (FcChar8 *)dir);
1138 if (!FcFontSetUnserialize (&metadata, set, current_dir_block))
1143 #if !(defined(HAVE_MMAP) || defined(__CYGWIN__))
1144 free (current_dir_block);
1150 FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
1152 void * current_dir_block, * final_dir_block;
1153 static unsigned int rand_state = 0;
1154 int bank, needed_bytes_no_align;
1156 #if defined (HAVE_RAND_R)
1158 rand_state = time(0L);
1159 bank = rand_r(&rand_state);
1161 while (FcCacheHaveBank(bank))
1162 bank = rand_r(&rand_state);
1171 while (FcCacheHaveBank(bank))
1175 memset (metadata, 0, sizeof(FcCache));
1177 needed_bytes_no_align = FcFontSetNeededBytes (set);
1178 metadata->count = needed_bytes_no_align +
1179 FcFontSetNeededBytesAlign ();
1180 metadata->magic = FC_CACHE_MAGIC;
1181 metadata->bank = bank;
1183 if (!needed_bytes_no_align) /* not a failure, no fonts to write */
1185 /* no, you don't really need to write any bytes at all. */
1186 metadata->count = 0;
1190 current_dir_block = malloc (metadata->count);
1191 if (!current_dir_block)
1193 /* shut up valgrind */
1194 memset (current_dir_block, 0, metadata->count);
1195 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
1197 if ((void *)((char *)current_dir_block+metadata->count) < final_dir_block)
1200 if (!FcFontSetSerialize (bank, set))
1203 return current_dir_block;
1206 free (current_dir_block);
1211 FcMakeDirectory (const FcChar8 *dir)
1216 if (strlen ((char *) dir) == 0)
1219 parent = FcStrDirname (dir);
1222 if (access ((char *) parent, W_OK|X_OK) == 0)
1223 ret = mkdir ((char *) dir, 0777) == 0;
1224 else if (access ((char *) parent, F_OK) == -1)
1225 ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0777) == 0);
1232 /* write serialized state to the cache file */
1234 FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir, FcConfig *config)
1236 FcChar8 cache_base[CACHEBASE_LEN];
1237 FcChar8 *cache_hashed;
1238 int fd, fd_orig, i, dirs_count;
1241 off_t current_arch_start = 0, truncate_to;
1243 char *current_arch_machine_name, * header;
1244 void *current_dir_block = 0;
1245 FcChar8 *cache_dir = NULL;
1249 * Write it to the first directory in the list which is writable
1252 list = FcStrListCreate (config->cacheDirs);
1255 while ((test_dir = FcStrListNext (list))) {
1256 if (access ((char *) test_dir, W_OK|X_OK) == 0)
1258 cache_dir = test_dir;
1264 * If the directory doesn't exist, try to create it
1266 if (access ((char *) test_dir, F_OK) == -1) {
1267 if (FcMakeDirectory (test_dir))
1269 cache_dir = test_dir;
1275 FcStrListDone (list);
1278 FcDirCacheBasename (dir, cache_base);
1279 cache_hashed = FcStrPlus (cache_dir, cache_base);
1283 current_dir_block = FcDirCacheProduce (set, &metadata);
1285 if (metadata.count && !current_dir_block)
1288 if (FcDebug () & FC_DBG_CACHE)
1289 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
1292 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
1296 if (!FcAtomicLock (atomic))
1299 /* open the original file to save relevant portions */
1300 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY | O_BINARY);
1302 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
1306 FcCacheWriteString (fd, (char *) dir);
1308 current_arch_machine_name = FcCacheMachineSignature ();
1309 current_arch_start = 0;
1312 current_arch_start =
1313 FcCacheSkipToArch(fd_orig, current_arch_machine_name);
1315 if (current_arch_start < 0)
1317 off_t offset = lseek(fd_orig, 0, SEEK_END);
1318 current_arch_start = FcCacheNextOffset (offset);
1321 if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
1327 current_arch_start = lseek(fd, 0, SEEK_CUR);
1328 #if defined (HAVE_FTRUNCATE)
1329 if (ftruncate (fd, current_arch_start) == -1)
1332 #if defined (HAVE_CHSIZE)
1333 if (chsize (fd, current_arch_start) == -1)
1340 /* allocate space for subdir names in this block */
1342 for (i = 0; i < dirs->size; i++)
1343 dirs_count += strlen((char *)dirs->strs[i]) + 1;
1346 /* now write the address of the next offset */
1347 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start;
1348 header = malloc (10 + strlen (current_arch_machine_name));
1351 sprintf (header, "%8x ", (int)truncate_to);
1352 strcat (header, current_arch_machine_name);
1353 if (!FcCacheWriteString (fd, header))
1356 for (i = 0; i < dirs->size; i++)
1357 FcCacheWriteString (fd, (char *)dirs->strs[i]);
1358 FcCacheWriteString (fd, "");
1360 if (write (fd, &metadata, sizeof(FcCache)) != sizeof(FcCache))
1362 perror("write metadata");
1367 off_t off = FcCacheNextOffset (lseek(fd, 0, SEEK_END));
1368 if (lseek (fd, off, SEEK_SET) != off)
1370 else if (write (fd, current_dir_block, metadata.count) !=
1372 perror("write current_dir_block");
1373 free (current_dir_block);
1374 current_dir_block = 0;
1377 /* this actually serves to pad out the cache file, if needed */
1378 #if defined (HAVE_FTRUNCATE)
1379 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
1382 #if defined (HAVE_CHSIZE)
1383 if (chsize (fd, current_arch_start + truncate_to) == -1)
1392 if (!FcAtomicReplaceOrig(atomic))
1394 FcStrFree ((FcChar8 *)cache_hashed);
1395 FcAtomicUnlock (atomic);
1396 FcAtomicDestroy (atomic);
1404 FcAtomicUnlock (atomic);
1406 FcAtomicDestroy (atomic);
1408 FcStrFree ((FcChar8 *)cache_hashed);
1409 if (current_dir_block)
1410 free (current_dir_block);
1415 FcCacheMachineSignature ()
1417 static char buf[MACHINE_SIGNATURE_SIZE];
1418 int32_t magic = ENDIAN_TEST;
1419 char * m = (char *)&magic;
1421 sprintf (buf, "%2x%2x%2x%2x "
1422 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
1423 "%4x %4x %4x %4x %4x %4x %4x %4x\n",
1424 m[0], m[1], m[2], m[3],
1425 (unsigned int)sizeof (char),
1426 (unsigned int)sizeof (char *),
1427 (unsigned int)sizeof (int),
1428 (unsigned int)sizeof (FcPattern),
1429 (unsigned int)sizeof (FcPatternEltPtr),
1430 (unsigned int)sizeof (struct _FcPatternElt *),
1431 (unsigned int)sizeof (FcPatternElt),
1432 (unsigned int)sizeof (FcObjectPtr),
1433 (unsigned int)sizeof (FcValueListPtr),
1434 (unsigned int)sizeof (FcValue),
1435 (unsigned int)sizeof (FcValueBinding),
1436 (unsigned int)sizeof (struct _FcValueList *),
1437 (unsigned int)sizeof (FcCharSet),
1438 (unsigned int)sizeof (FcCharLeaf **),
1439 (unsigned int)sizeof (FcChar16 *),
1440 (unsigned int)sizeof (FcChar16),
1441 (unsigned int)sizeof (FcCharLeaf),
1442 (unsigned int)sizeof (FcChar32),
1443 (unsigned int)sizeof (FcCache),
1444 #if defined (HAVE_SYSCONF)
1445 (unsigned int)sysconf(_SC_PAGESIZE));
1447 (unsigned int)FC_HARDCODED_PAGESIZE);
1453 static int banks_ptr = 0, banks_alloc = 0;
1454 int * _fcBankId = 0, * _fcBankIdx = 0;
1455 static const char ** bankDirs = 0;
1458 FcCacheHaveBank (int bank)
1462 if (bank < FC_BANK_FIRST)
1465 for (i = 0; i < banks_ptr; i++)
1466 if (_fcBankId[i] == bank)
1473 FcCacheBankToIndexMTF (int bank)
1477 for (i = 0; i < banks_ptr; i++)
1478 if (_fcBankId[_fcBankIdx[i]] == bank)
1480 int t = _fcBankIdx[i];
1482 for (j = i; j > 0; j--)
1483 _fcBankIdx[j] = _fcBankIdx[j-1];
1488 if (banks_ptr >= banks_alloc)
1493 b = realloc (_fcBankId, (banks_alloc + 4) * sizeof(int));
1498 bidx = realloc (_fcBankIdx, (banks_alloc + 4) * sizeof(int));
1503 bds = realloc (bankDirs, (banks_alloc + 4) * sizeof (char *));
1512 _fcBankId[i] = bank;
1518 FcCacheAddBankDir (int bank, const char * dir)
1520 int bi = FcCacheBankToIndexMTF (bank);
1525 bankDirs[bi] = (const char *)FcStrCopy ((FcChar8 *)dir);
1529 FcCacheFindBankDir (int bank)
1531 int bi = FcCacheBankToIndex (bank);
1532 return bankDirs[bi];
1536 * This code implements the MD5 message-digest algorithm.
1537 * The algorithm is due to Ron Rivest. This code was
1538 * written by Colin Plumb in 1993, no copyright is claimed.
1539 * This code is in the public domain; do with it what you wish.
1541 * Equivalent code is available from RSA Data Security, Inc.
1542 * This code has been tested against that, and is equivalent,
1543 * except that you don't need to include two pages of legalese
1546 * To compute the message digest of a chunk of bytes, declare an
1547 * MD5Context structure, pass it to MD5Init, call MD5Update as
1548 * needed on buffers full of bytes, and then call MD5Final, which
1549 * will fill a supplied 16-byte array with the digest.
1553 #define byteReverse(buf, len) /* Nothing */
1556 * Note: this code is harmless on little-endian machines.
1558 void byteReverse(unsigned char *buf, unsigned longs)
1562 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1563 ((unsigned) buf[1] << 8 | buf[0]);
1564 *(FcChar32 *) buf = t;
1571 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1572 * initialization constants.
1574 static void MD5Init(struct MD5Context *ctx)
1576 ctx->buf[0] = 0x67452301;
1577 ctx->buf[1] = 0xefcdab89;
1578 ctx->buf[2] = 0x98badcfe;
1579 ctx->buf[3] = 0x10325476;
1586 * Update context to reflect the concatenation of another buffer full
1589 static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len)
1593 /* Update bitcount */
1596 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1597 ctx->bits[1]++; /* Carry from low to high */
1598 ctx->bits[1] += len >> 29;
1600 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1602 /* Handle any leading odd-sized chunks */
1605 unsigned char *p = (unsigned char *) ctx->in + t;
1609 memcpy(p, buf, len);
1613 byteReverse(ctx->in, 16);
1614 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1618 /* Process data in 64-byte chunks */
1621 memcpy(ctx->in, buf, 64);
1622 byteReverse(ctx->in, 16);
1623 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1628 /* Handle any remaining bytes of data. */
1630 memcpy(ctx->in, buf, len);
1634 * Final wrapup - pad to 64-byte boundary with the bit pattern
1635 * 1 0* (64-bit count of bits processed, MSB-first)
1637 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1642 /* Compute number of bytes mod 64 */
1643 count = (ctx->bits[0] >> 3) & 0x3F;
1645 /* Set the first char of padding to 0x80. This is safe since there is
1646 always at least one byte free */
1647 p = ctx->in + count;
1650 /* Bytes of padding needed to make 64 bytes */
1651 count = 64 - 1 - count;
1653 /* Pad out to 56 mod 64 */
1655 /* Two lots of padding: Pad the first block to 64 bytes */
1656 memset(p, 0, count);
1657 byteReverse(ctx->in, 16);
1658 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1660 /* Now fill the next block with 56 bytes */
1661 memset(ctx->in, 0, 56);
1663 /* Pad block to 56 bytes */
1664 memset(p, 0, count - 8);
1666 byteReverse(ctx->in, 14);
1668 /* Append length in bits and transform */
1669 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1670 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1672 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1673 byteReverse((unsigned char *) ctx->buf, 4);
1674 memcpy(digest, ctx->buf, 16);
1675 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
1679 /* The four core functions - F1 is optimized somewhat */
1681 /* #define F1(x, y, z) (x & y | ~x & z) */
1682 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1683 #define F2(x, y, z) F1(z, x, y)
1684 #define F3(x, y, z) (x ^ y ^ z)
1685 #define F4(x, y, z) (y ^ (x | ~z))
1687 /* This is the central step in the MD5 algorithm. */
1688 #define MD5STEP(f, w, x, y, z, data, s) \
1689 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1692 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1693 * reflect the addition of 16 longwords of new data. MD5Update blocks
1694 * the data and converts bytes into longwords for this routine.
1696 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1698 register FcChar32 a, b, c, d;
1705 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1706 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1707 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1708 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1709 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1710 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1711 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1712 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1713 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1714 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1715 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1716 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1717 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1718 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1719 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1720 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1722 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1723 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1724 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1725 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1726 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1727 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1728 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1729 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1730 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1731 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1732 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1733 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1734 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1735 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1736 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1737 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1739 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1740 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1741 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1742 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1743 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1744 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1745 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1746 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1747 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1748 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1749 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1750 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1751 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1752 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1753 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1754 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1756 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1757 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1758 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1759 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1760 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1761 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1762 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1763 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1764 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1765 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1766 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1767 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1768 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1769 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1770 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1771 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);