2 * $RCSId: xc/lib/fontconfig/src/fccache.c,v 1.12 2002/08/22 07:36:44 keithp Exp $
4 * Copyright © 2000 Keith Packard
5 * Copyright © 2005 Patrick Lam
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of Keith Packard not be used in
12 * advertising or publicity pertaining to distribution of the software without
13 * specific, written prior permission. Keith Packard makes no
14 * representations about the suitability of this software for any purpose. It
15 * is provided "as is" without express or implied warranty.
17 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
23 * PERFORMANCE OF THIS SOFTWARE.
30 #include <sys/utsname.h>
31 #include <sys/types.h>
36 #define ENDIAN_TEST 0x12345678
37 #define MACHINE_SIGNATURE_SIZE 9 + 5*20 + 1
40 FcDirCacheOpen (const FcChar8
* dir
);
43 FcDirCacheHashName (char * cache_file
, int collisions
);
46 FcCacheSkipToArch (int fd
, const char * arch
);
49 FcCacheCopyOld (int fd
, int fd_orig
, off_t start
);
52 FcDirCacheProduce (FcFontSet
*set
, FcCache
* metadata
);
55 FcDirCacheConsume (int fd
, const char * dir
, FcFontSet
*set
, FcConfig
*config
);
58 FcCacheNextOffset(off_t w
);
61 FcCacheMachineSignature (void);
64 FcCacheHaveBank (int bank
);
67 FcCacheAddBankDir (int bank
, const char * dir
);
75 static void MD5Init(struct MD5Context
*ctx
);
76 static void MD5Update(struct MD5Context
*ctx
, unsigned char *buf
, unsigned len
);
77 static void MD5Final(unsigned char digest
[16], struct MD5Context
*ctx
);
78 static void MD5Transform(FcChar32 buf
[4], FcChar32 in
[16]);
80 #define FC_DBG_CACHE_REF 1024
83 FcCacheReadString (int fd
, char *dest
, int len
)
96 while (read (fd
, &c
, 1) == 1)
123 FcCacheSkipString (int fd
)
129 while (read (fd
, &c
, 1) == 1)
150 FcCacheWriteString (int fd
, const char *chars
)
152 if (write (fd
, chars
, strlen(chars
)+1) != strlen(chars
)+1)
158 FcGlobalCacheDirDestroy (FcGlobalCacheDir
*d
)
160 FcStrSetDestroy (d
->subdirs
);
161 FcMemFree (FC_MEM_STRING
, strlen (d
->name
)+1);
163 FcMemFree (FC_MEM_CACHE
, sizeof (FcGlobalCacheDir
));
168 FcGlobalCacheCreate (void)
170 FcGlobalCache
*cache
;
172 cache
= malloc (sizeof (FcGlobalCache
));
175 FcMemAlloc (FC_MEM_CACHE
, sizeof (FcGlobalCache
));
177 cache
->updated
= FcFalse
;
183 FcGlobalCacheDestroy (FcGlobalCache
*cache
)
185 FcGlobalCacheDir
*d
, *next
;
187 for (d
= cache
->dirs
; d
; d
= next
)
190 FcGlobalCacheDirDestroy (d
);
192 FcMemFree (FC_MEM_CACHE
, sizeof (FcGlobalCache
));
197 FcGlobalCacheLoad (FcGlobalCache
*cache
,
199 const FcChar8
*cache_file
,
202 char name_buf
[FC_MAX_FILE_LEN
];
203 FcGlobalCacheDir
*d
, *next
;
204 FcFileTime config_time
= FcConfigModifiedTime (config
);
205 char * current_arch_machine_name
;
206 char candidate_arch_machine_name
[MACHINE_SIGNATURE_SIZE
+ 9];
207 off_t current_arch_start
;
209 struct stat cache_stat
, dir_stat
;
210 char subdirName
[FC_MAX_FILE_LEN
+ 1 + 12 + 1];
212 if (stat ((char *) cache_file
, &cache_stat
) < 0)
215 cache
->fd
= open ((char *) cache_file
, O_RDONLY
);
219 cache
->updated
= FcFalse
;
221 if (!FcCacheReadString (cache
->fd
, name_buf
, sizeof (name_buf
)))
222 goto bail_and_destroy
;
223 if (strcmp (name_buf
, FC_GLOBAL_MAGIC_COOKIE
) != 0)
224 goto bail_and_destroy
;
226 current_arch_machine_name
= FcCacheMachineSignature ();
227 current_arch_start
= FcCacheSkipToArch(cache
->fd
,
228 current_arch_machine_name
);
229 if (current_arch_start
< 0)
230 goto bail_and_destroy
;
232 lseek (cache
->fd
, current_arch_start
, SEEK_SET
);
233 if (!FcCacheReadString (cache
->fd
, candidate_arch_machine_name
,
234 sizeof (candidate_arch_machine_name
)))
235 goto bail_and_destroy
;
236 if (strlen(candidate_arch_machine_name
) == 0)
237 goto bail_and_destroy
;
243 if (!FcCacheReadString (cache
->fd
, name_buf
, sizeof (name_buf
)) ||
247 /* Directory must be older than the global cache file; also
248 cache must be newer than the config file. */
249 if (stat ((char *) name_buf
, &dir_stat
) < 0 ||
250 dir_stat
.st_mtime
> cache_stat
.st_mtime
||
251 (config_time
.set
&& cache_stat
.st_mtime
< config_time
.time
))
256 FcStrSetAdd (staleDirs
, FcStrCopy ((FcChar8
*)name_buf
));
257 if (read (cache
->fd
, &md
, sizeof (FcCache
)) != sizeof(FcCache
))
259 perror ("read metadata");
262 off
= FcCacheNextOffset (lseek(cache
->fd
, 0, SEEK_CUR
)) + md
.count
;
263 if (lseek (cache
->fd
, off
, SEEK_SET
) != off
) {
270 d
= malloc (sizeof (FcGlobalCacheDir
));
274 d
->next
= cache
->dirs
;
277 d
->name
= (char *)FcStrCopy ((FcChar8
*)name_buf
);
280 d
->subdirs
= FcStrSetCreate();
283 if (!FcCacheReadString (cache
->fd
, subdirName
,
284 sizeof (subdirName
)) ||
285 !strlen (subdirName
))
287 FcStrSetAdd (d
->subdirs
, (FcChar8
*)subdirName
);
290 d
->offset
= lseek (cache
->fd
, 0, SEEK_CUR
);
291 if (read (cache
->fd
, &d
->metadata
, sizeof (FcCache
)) != sizeof (FcCache
))
293 targ
= FcCacheNextOffset (lseek(cache
->fd
, 0, SEEK_CUR
)) + d
->metadata
.count
;
294 if (lseek (cache
->fd
, targ
, SEEK_SET
) != targ
)
300 for (d
= cache
->dirs
; d
; d
= next
)
315 if (stat ((char *) cache_file
, &cache_stat
) == 0)
316 unlink ((char *)cache_file
);
323 FcGlobalCacheReadDir (FcFontSet
*set
, FcStrSet
*dirs
, FcGlobalCache
* cache
, const char *dir
, FcConfig
*config
)
326 FcBool ret
= FcFalse
;
332 if (!(dir
= (char *)FcConfigNormalizeFontDir (config
, (FcChar8
*)dir
)))
333 return FcFalse
; /* non-existing directory */
335 for (d
= cache
->dirs
; d
; d
= d
->next
)
337 if (strncmp (d
->name
, dir
, strlen(dir
)) == 0)
339 lseek (cache
->fd
, d
->offset
, SEEK_SET
);
340 if (!FcDirCacheConsume (cache
->fd
, d
->name
, set
, config
))
343 if (strcmp (d
->name
, dir
) == 0)
345 for (i
= 0; i
< d
->subdirs
->num
; i
++)
346 FcStrSetAdd (dirs
, (FcChar8
*)d
->subdirs
->strs
[i
]);
357 FcGlobalCacheUpdate (FcGlobalCache
*cache
,
366 name
= (char *)FcConfigNormalizeFontDir (config
, (FcChar8
*)name
);
367 for (d
= cache
->dirs
; d
; d
= d
->next
)
369 if (strcmp(d
->name
, name
) == 0)
375 d
= malloc (sizeof (FcGlobalCacheDir
));
378 d
->next
= cache
->dirs
;
382 cache
->updated
= FcTrue
;
384 d
->name
= (char *)FcStrCopy ((FcChar8
*)name
);
385 d
->ent
= FcDirCacheProduce (set
, &d
->metadata
);
387 d
->subdirs
= FcStrSetCreate();
388 for (i
= 0; i
< dirs
->num
; i
++)
389 FcStrSetAdd (d
->subdirs
, dirs
->strs
[i
]);
394 FcGlobalCacheSave (FcGlobalCache
*cache
,
395 const FcChar8
*cache_file
,
399 FcGlobalCacheDir
*dir
;
401 off_t current_arch_start
= 0, truncate_to
;
402 char * current_arch_machine_name
, * header
;
407 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
408 /* Set-UID programs can't safely update the cache */
409 if (getuid () != geteuid ())
413 atomic
= FcAtomicCreate (cache_file
);
417 if (!FcAtomicLock (atomic
))
419 fd
= open ((char *) FcAtomicNewFile(atomic
), O_RDWR
| O_CREAT
,
423 FcCacheWriteString (fd
, FC_GLOBAL_MAGIC_COOKIE
);
425 fd_orig
= open ((char *) FcAtomicOrigFile(atomic
), O_RDONLY
);
427 current_arch_machine_name
= FcCacheMachineSignature ();
429 current_arch_start
= 0;
431 current_arch_start
= FcCacheSkipToArch (fd_orig
,
432 current_arch_machine_name
);
434 if (current_arch_start
< 0)
436 off_t i
= lseek(fd_orig
, 0, SEEK_END
);
437 if (i
< strlen (FC_GLOBAL_MAGIC_COOKIE
)+1)
438 i
= strlen (FC_GLOBAL_MAGIC_COOKIE
)+1;
439 current_arch_start
= FcCacheNextOffset (i
);
442 if (!FcCacheCopyOld(fd
, fd_orig
, current_arch_start
))
448 current_arch_start
= lseek(fd
, 0, SEEK_CUR
);
449 if (ftruncate (fd
, current_arch_start
) == -1)
452 header
= malloc (10 + strlen (current_arch_machine_name
));
456 truncate_to
= current_arch_start
+ strlen(current_arch_machine_name
) + 11;
457 for (dir
= cache
->dirs
; dir
; dir
= dir
->next
)
459 truncate_to
+= strlen(dir
->name
) + 1;
460 truncate_to
+= sizeof (FcCache
);
461 truncate_to
= FcCacheNextOffset (truncate_to
);
462 truncate_to
+= dir
->metadata
.count
;
464 for (i
= 0; i
< dir
->subdirs
->size
; i
++)
465 truncate_to
+= strlen((char *)dir
->subdirs
->strs
[i
]) + 1;
468 truncate_to
-= current_arch_start
;
470 sprintf (header
, "%8x ", (int)truncate_to
);
471 strcat (header
, current_arch_machine_name
);
472 if (!FcCacheWriteString (fd
, header
))
475 for (dir
= cache
->dirs
; dir
; dir
= dir
->next
)
479 const char * d
= (const char *)FcConfigNormalizeFontDir (config
, (const FcChar8
*)dir
->name
);
482 FcCacheWriteString (fd
, d
);
484 for (i
= 0; i
< dir
->subdirs
->size
; i
++)
485 FcCacheWriteString (fd
, (char *)dir
->subdirs
->strs
[i
]);
486 FcCacheWriteString (fd
, "");
488 if (write (fd
, &dir
->metadata
, sizeof(FcCache
)) != sizeof(FcCache
))
490 perror ("write metadata");
494 off
= FcCacheNextOffset (lseek(fd
, 0, SEEK_CUR
));
495 if (lseek (fd
, off
, SEEK_SET
) != off
)
501 if (write (fd
, dir
->ent
, dir
->metadata
.count
) != dir
->metadata
.count
)
503 perror ("write dirent");
510 FcCacheWriteString (fd
, "");
512 if (close (fd
) == -1)
515 if (!FcAtomicReplaceOrig (atomic
))
518 FcAtomicUnlock (atomic
);
519 FcAtomicDestroy (atomic
);
521 cache
->updated
= FcFalse
;
532 FcAtomicDeleteNew (atomic
);
534 FcAtomicUnlock (atomic
);
536 FcAtomicDestroy (atomic
);
541 * Find the next presumably-mmapable offset after the supplied file
545 FcCacheNextOffset(off_t w
)
547 static long pagesize
= -1;
549 pagesize
= sysconf(_SC_PAGESIZE
);
550 if (w
% pagesize
== 0)
553 return ((w
/ pagesize
)+1)*pagesize
;
556 /* return the address of the segment for the provided arch,
557 * or -1 if arch not found */
559 FcCacheSkipToArch (int fd
, const char * arch
)
561 char candidate_arch_machine_name_count
[MACHINE_SIGNATURE_SIZE
+ 9];
562 char * candidate_arch
;
563 off_t current_arch_start
= 0;
565 lseek (fd
, 0, SEEK_SET
);
566 FcCacheSkipString (fd
);
567 current_arch_start
= lseek (fd
, 0, SEEK_CUR
);
569 /* skip arches that are not the current arch */
574 if (lseek (fd
, current_arch_start
, SEEK_SET
) != current_arch_start
)
577 if (FcCacheReadString (fd
, candidate_arch_machine_name_count
,
578 sizeof (candidate_arch_machine_name_count
)) == 0)
580 if (!strlen(candidate_arch_machine_name_count
))
582 bs
= strtol(candidate_arch_machine_name_count
, &candidate_arch
, 16);
584 // count = 0 should probably be distinguished from the !bs condition
585 if (!bs
|| bs
< strlen (candidate_arch_machine_name_count
))
588 candidate_arch
++; /* skip leading space */
590 if (strcmp (candidate_arch
, arch
)==0)
591 return current_arch_start
;
592 current_arch_start
+= bs
;
598 /* Cuts out the segment at the file pointer (moves everything else
599 * down to cover it), and leaves the file pointer at the end of the
602 FcCacheCopyOld (int fd
, int fd_orig
, off_t start
)
604 char * buf
= malloc (8192);
605 char candidate_arch_machine_name
[MACHINE_SIGNATURE_SIZE
+ 9];
607 int c
, bytes_skipped
;
614 lseek (fd
, 0, SEEK_SET
); lseek (fd_orig
, 0, SEEK_SET
);
621 if ((c
= read (fd_orig
, buf
, b
)) <= 0)
623 if (write (fd
, buf
, c
) < 0)
630 lseek (fd
, start
, SEEK_SET
);
631 if (FcCacheReadString (fd
, candidate_arch_machine_name
,
632 sizeof (candidate_arch_machine_name
)) == 0)
634 if (!strlen(candidate_arch_machine_name
))
637 bs
= strtol(candidate_arch_machine_name
, 0, 16);
644 lseek (fd
, start
+bs
+bytes_skipped
, SEEK_SET
);
645 if ((c
= read (fd
, buf
, 8192)) <= 0)
647 lseek (fd
, start
+bytes_skipped
, SEEK_SET
);
648 if (write (fd
, buf
, c
) < 0)
653 lseek (fd
, start
+bytes_skipped
, SEEK_SET
);
664 /* Does not check that the cache has the appropriate arch section. */
665 /* Also, this can be fooled if the original location has a stale
666 * cache, and the hashed location has an up-to-date cache. Oh well,
667 * sucks to be you in that case! */
669 FcDirCacheValid (const FcChar8
*dir
)
671 struct stat file_stat
, dir_stat
;
674 if (stat ((char *) dir
, &dir_stat
) < 0)
677 fd
= FcDirCacheOpen (dir
);
681 if (fstat (fd
, &file_stat
) < 0)
687 * If the directory has been modified more recently than
688 * the cache file, the cache is not valid
690 if (dir_stat
.st_mtime
> file_stat
.st_mtime
)
700 /* Assumes that the cache file in 'dir' exists.
701 * Checks that the cache has the appropriate arch section. */
703 FcDirCacheHasCurrentArch (const FcChar8
*dir
)
706 off_t current_arch_start
;
707 char *current_arch_machine_name
;
709 fd
= FcDirCacheOpen (dir
);
713 current_arch_machine_name
= FcCacheMachineSignature();
714 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
717 if (current_arch_start
< 0)
727 FcDirCacheUnlink (const FcChar8
*dir
, FcConfig
*config
)
730 char *cache_hashed
= 0;
732 struct stat cache_stat
;
733 char name_buf
[FC_MAX_FILE_LEN
];
735 dir
= FcConfigNormalizeFontDir (config
, dir
);
736 cache_file
= (char *)FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
740 /* First remove normal cache file. */
741 if (stat ((char *) cache_file
, &cache_stat
) == 0 &&
742 unlink ((char *)cache_file
) != 0)
745 /* Next remove any applicable hashed files. */
746 fd
= -1; collisions
= 0;
750 FcStrFree ((FcChar8
*)cache_hashed
);
752 cache_hashed
= FcDirCacheHashName (cache_file
, collisions
++);
758 fd
= open(cache_hashed
, O_RDONLY
);
761 FcStrFree ((FcChar8
*)cache_file
);
765 if (!FcCacheReadString (fd
, name_buf
, sizeof (name_buf
)) || !strlen(name_buf
))
767 FcStrFree ((FcChar8
*)cache_hashed
);
770 } while (strcmp (name_buf
, cache_file
) != 0);
774 if (stat ((char *) cache_hashed
, &cache_stat
) == 0 &&
775 unlink ((char *)cache_hashed
) != 0)
777 FcStrFree ((FcChar8
*)cache_hashed
);
781 FcStrFree ((FcChar8
*)cache_file
);
782 FcStrFree ((FcChar8
*)cache_hashed
);
786 FcStrFree ((FcChar8
*)cache_file
);
791 FcCacheReadDirs (FcConfig
* config
, FcGlobalCache
* cache
,
792 FcStrList
*list
, FcFontSet
* set
)
796 FcChar8
*file
, *base
;
802 * Read in the results from 'list'.
804 while ((dir
= FcStrListNext (list
)))
806 if (!FcConfigAcceptFilename (config
, dir
))
810 file
= (FcChar8
*) malloc (strlen ((char *) dir
) + 1 + FC_MAX_FILE_LEN
+ 1);
814 strcpy ((char *) file
, (char *) dir
);
815 strcat ((char *) file
, "/");
816 base
= file
+ strlen ((char *) file
);
818 subdirs
= FcStrSetCreate ();
821 fprintf (stderr
, "Can't create directory set\n");
827 if (access ((char *) dir
, X_OK
) < 0)
835 fprintf (stderr
, "\"%s\": ", dir
);
839 FcStrSetDestroy (subdirs
);
843 if (stat ((char *) dir
, &statb
) == -1)
845 fprintf (stderr
, "\"%s\": ", dir
);
847 FcStrSetDestroy (subdirs
);
852 if (!S_ISDIR (statb
.st_mode
))
854 fprintf (stderr
, "\"%s\": not a directory, skipping\n", dir
);
855 FcStrSetDestroy (subdirs
);
859 if (!FcDirCacheValid (dir
) || !FcDirCacheRead (set
, subdirs
, dir
, config
))
861 if (FcDebug () & FC_DBG_FONTSET
)
862 printf ("cache scan dir %s\n", dir
);
864 FcDirScanConfig (set
, subdirs
, cache
,
865 config
->blanks
, dir
, FcFalse
, config
);
867 sublist
= FcStrListCreate (subdirs
);
868 FcStrSetDestroy (subdirs
);
871 fprintf (stderr
, "Can't create subdir list in \"%s\"\n", dir
);
876 ret
+= FcCacheReadDirs (config
, cache
, sublist
, set
);
879 FcStrListDone (list
);
884 FcCacheRead (FcConfig
*config
, FcGlobalCache
* cache
)
886 FcFontSet
* s
= FcFontSetCreate();
890 if (FcCacheReadDirs (config
, cache
, FcConfigGetConfigDirs (config
), s
))
896 FcFontSetDestroy (s
);
900 static const char bin2hex
[] = { '0', '1', '2', '3',
903 'c', 'd', 'e', 'f' };
906 FcDirCacheHashName (char * cache_file
, int collisions
)
908 unsigned char hash
[16], hex_hash
[33];
910 unsigned char uscore
= '_';
913 struct MD5Context ctx
;
916 MD5Update (&ctx
, (unsigned char *)cache_file
, strlen (cache_file
));
918 for (i
= 0; i
< collisions
; i
++)
919 MD5Update (&ctx
, &uscore
, 1);
921 MD5Final (hash
, &ctx
);
923 for (cnt
= 0; cnt
< 16; ++cnt
)
925 hex_hash
[2*cnt
] = bin2hex
[hash
[cnt
] >> 4];
926 hex_hash
[2*cnt
+1] = bin2hex
[hash
[cnt
] & 0xf];
930 tmp
= FcStrPlus ((FcChar8
*)hex_hash
, (FcChar8
*)FC_CACHE_SUFFIX
);
934 cache_hashed
= (char *)FcStrPlus ((FcChar8
*)PKGCACHEDIR
"/", tmp
);
940 /* Opens the hashed name for cache_file.
941 * This would fail in the unlikely event of a collision and subsequent
942 * removal of the file which originally caused the collision. */
944 FcDirCacheOpen (const FcChar8
*dir
)
947 int fd
= -1, collisions
= 0;
948 char *cache_file
, *cache_hashed
;
949 char name_buf
[FC_MAX_FILE_LEN
];
950 struct stat dir_stat
;
952 cache_file
= (char *)FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
956 fd
= open(cache_file
, O_RDONLY
);
960 if (stat ((char *)dir
, &dir_stat
) == -1)
967 FcChar8
* name_buf_dir
;
969 cache_hashed
= FcDirCacheHashName (cache_file
, collisions
++);
972 FcStrFree ((FcChar8
*)cache_file
);
978 fd
= open(cache_hashed
, O_RDONLY
);
979 FcStrFree ((FcChar8
*)cache_hashed
);
983 FcStrFree ((FcChar8
*)cache_file
);
986 if (!FcCacheReadString (fd
, name_buf
, sizeof (name_buf
)) || !strlen(name_buf
))
989 name_buf_dir
= FcStrDirname ((FcChar8
*)name_buf
);
990 if (stat ((char *)name_buf_dir
, &c
) == -1)
992 FcStrFree (name_buf_dir
);
995 FcStrFree (name_buf_dir
);
996 found
= (c
.st_ino
== dir_stat
.st_ino
) && (c
.st_dev
== dir_stat
.st_dev
);
998 FcStrFree ((FcChar8
*)cache_file
);
1002 FcStrFree ((FcChar8
*)cache_file
);
1007 /* read serialized state from the cache file */
1009 FcDirCacheRead (FcFontSet
* set
, FcStrSet
* dirs
, const FcChar8
*dir
, FcConfig
*config
)
1012 char *current_arch_machine_name
;
1013 char candidate_arch_machine_name
[9+MACHINE_SIGNATURE_SIZE
];
1014 off_t current_arch_start
= 0;
1015 char subdirName
[FC_MAX_FILE_LEN
+ 1 + 12 + 1];
1017 fd
= FcDirCacheOpen (dir
);
1021 current_arch_machine_name
= FcCacheMachineSignature();
1022 current_arch_start
= FcCacheSkipToArch(fd
,
1023 current_arch_machine_name
);
1024 if (current_arch_start
< 0)
1027 lseek (fd
, current_arch_start
, SEEK_SET
);
1028 if (FcCacheReadString (fd
, candidate_arch_machine_name
,
1029 sizeof (candidate_arch_machine_name
)) == 0)
1032 while (FcCacheReadString (fd
, subdirName
, sizeof (subdirName
)) && strlen (subdirName
) > 0)
1033 FcStrSetAdd (dirs
, (FcChar8
*)subdirName
);
1035 if (!FcDirCacheConsume (fd
, (const char *)dir
, set
, config
))
1048 FcDirCacheConsume (int fd
, const char * dir
, FcFontSet
*set
, FcConfig
*config
)
1051 void * current_dir_block
;
1054 if (read(fd
, &metadata
, sizeof(FcCache
)) != sizeof(FcCache
))
1056 if (metadata
.magic
!= FC_CACHE_MAGIC
)
1059 if (!metadata
.count
)
1061 pos
= FcCacheNextOffset (lseek(fd
, 0, SEEK_CUR
));
1062 lseek (fd
, pos
, SEEK_SET
);
1064 FcConfigAddFontDir (config
, (FcChar8
*)dir
);
1068 pos
= FcCacheNextOffset (lseek(fd
, 0, SEEK_CUR
));
1069 current_dir_block
= mmap (0, metadata
.count
,
1070 PROT_READ
, MAP_SHARED
, fd
, pos
);
1071 lseek (fd
, pos
+metadata
.count
, SEEK_SET
);
1072 if (current_dir_block
== MAP_FAILED
)
1075 FcCacheAddBankDir (metadata
.bank
, dir
);
1077 FcConfigAddFontDir (config
, (FcChar8
*)dir
);
1079 if (!FcFontSetUnserialize (&metadata
, set
, current_dir_block
))
1086 FcDirCacheProduce (FcFontSet
*set
, FcCache
*metadata
)
1088 void * current_dir_block
, * final_dir_block
;
1089 static unsigned int rand_state
= 0;
1090 int bank
, needed_bytes_no_align
;
1093 rand_state
= time(0L);
1094 bank
= rand_r(&rand_state
);
1096 while (FcCacheHaveBank(bank
))
1097 bank
= rand_r(&rand_state
);
1099 memset (metadata
, 0, sizeof(FcCache
));
1101 needed_bytes_no_align
= FcFontSetNeededBytes (set
);
1102 metadata
->count
= needed_bytes_no_align
+
1103 FcFontSetNeededBytesAlign ();
1104 metadata
->magic
= FC_CACHE_MAGIC
;
1105 metadata
->bank
= bank
;
1107 if (!needed_bytes_no_align
) /* not a failure, no fonts to write */
1109 /* no, you don't really need to write any bytes at all. */
1110 metadata
->count
= 0;
1114 current_dir_block
= malloc (metadata
->count
);
1115 if (!current_dir_block
)
1118 memset (current_dir_block
, 0, metadata
->count
);
1119 final_dir_block
= FcFontSetDistributeBytes (metadata
, current_dir_block
);
1121 if ((void *)((char *)current_dir_block
+metadata
->count
) < final_dir_block
)
1124 if (!FcFontSetSerialize (bank
, set
))
1127 return current_dir_block
;
1130 free (current_dir_block
);
1134 /* write serialized state to the cache file */
1136 FcDirCacheWrite (FcFontSet
*set
, FcStrSet
*dirs
, const FcChar8
*dir
)
1140 int fd
, fd_orig
, i
, dirs_count
;
1143 off_t current_arch_start
= 0, truncate_to
;
1144 char name_buf
[FC_MAX_FILE_LEN
];
1147 char *current_arch_machine_name
, * header
;
1148 void *current_dir_block
= 0;
1150 dir
= FcConfigNormalizeFontDir (FcConfigGetCurrent(), dir
);
1154 cache_file
= (char *)FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
1158 /* Ensure that we're not trampling a cache for some other dir. */
1159 /* This is slightly different from FcDirCacheOpen, since it
1160 * needs the filename, not the file descriptor. */
1161 fd
= -1; collisions
= 0;
1164 cache_hashed
= FcDirCacheHashName (cache_file
, collisions
++);
1170 fd
= open(cache_hashed
, O_RDONLY
);
1173 if(!FcCacheReadString (fd
, name_buf
, sizeof (name_buf
)) || !strlen(name_buf
))
1180 if (strcmp (name_buf
, cache_file
) != 0)
1184 current_dir_block
= FcDirCacheProduce (set
, &metadata
);
1186 if (metadata
.count
&& !current_dir_block
)
1189 if (FcDebug () & FC_DBG_CACHE
)
1190 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file
);
1192 atomic
= FcAtomicCreate ((FcChar8
*)cache_hashed
);
1196 if (!FcAtomicLock (atomic
))
1198 /* Now try rewriting the original version of the file. */
1199 FcAtomicDestroy (atomic
);
1201 atomic
= FcAtomicCreate ((FcChar8
*)cache_file
);
1202 fd_orig
= open (cache_file
, O_RDONLY
);
1204 fd_orig
= open((char *)FcAtomicOrigFile (atomic
), O_RDONLY
);
1206 fd
= open((char *)FcAtomicNewFile (atomic
), O_RDWR
| O_CREAT
, 0666);
1211 /* In all cases, try opening the real location of the cache file first. */
1212 /* (even if that's not atomic.) */
1213 fd_orig
= open (cache_file
, O_RDONLY
);
1215 fd_orig
= open((char *)FcAtomicOrigFile (atomic
), O_RDONLY
);
1217 fd
= open((char *)FcAtomicNewFile (atomic
), O_RDWR
| O_CREAT
, 0666);
1221 FcCacheWriteString (fd
, cache_file
);
1223 current_arch_machine_name
= FcCacheMachineSignature ();
1224 current_arch_start
= 0;
1227 current_arch_start
=
1228 FcCacheSkipToArch(fd_orig
, current_arch_machine_name
);
1230 if (current_arch_start
< 0)
1232 off_t i
= lseek(fd_orig
, 0, SEEK_END
);
1233 if (i
< strlen (FC_GLOBAL_MAGIC_COOKIE
)+1)
1234 i
= strlen (FC_GLOBAL_MAGIC_COOKIE
)+1;
1235 current_arch_start
= FcCacheNextOffset (i
);
1238 if (fd_orig
!= -1 && !FcCacheCopyOld(fd
, fd_orig
, current_arch_start
))
1244 current_arch_start
= lseek(fd
, 0, SEEK_CUR
);
1245 if (ftruncate (fd
, current_arch_start
) == -1)
1248 /* allocate space for subdir names in this block */
1250 for (i
= 0; i
< dirs
->size
; i
++)
1251 dirs_count
+= strlen((char *)dirs
->strs
[i
]) + 1;
1254 /* now write the address of the next offset */
1255 truncate_to
= FcCacheNextOffset (FcCacheNextOffset (current_arch_start
+ sizeof (FcCache
) + dirs_count
) + metadata
.count
) - current_arch_start
;
1256 header
= malloc (10 + strlen (current_arch_machine_name
));
1259 sprintf (header
, "%8x ", (int)truncate_to
);
1260 strcat (header
, current_arch_machine_name
);
1261 if (!FcCacheWriteString (fd
, header
))
1264 for (i
= 0; i
< dirs
->size
; i
++)
1265 FcCacheWriteString (fd
, (char *)dirs
->strs
[i
]);
1266 FcCacheWriteString (fd
, "");
1268 if (write (fd
, &metadata
, sizeof(FcCache
)) != sizeof(FcCache
)) {
1269 perror("write metadata");
1274 off_t off
= FcCacheNextOffset (lseek(fd
, 0, SEEK_END
));
1275 if (lseek (fd
, off
, SEEK_SET
) != off
)
1277 else if (write (fd
, current_dir_block
, metadata
.count
) !=
1279 perror("write current_dir_block");
1280 free (current_dir_block
);
1283 /* this actually serves to pad out the cache file, if needed */
1284 if (ftruncate (fd
, current_arch_start
+ truncate_to
) == -1)
1289 if (!FcAtomicReplaceOrig(atomic
))
1291 FcStrFree ((FcChar8
*)cache_hashed
);
1292 FcStrFree ((FcChar8
*)cache_file
);
1293 FcAtomicUnlock (atomic
);
1294 FcAtomicDestroy (atomic
);
1302 FcAtomicUnlock (atomic
);
1304 FcAtomicDestroy (atomic
);
1306 FcStrFree ((FcChar8
*)cache_hashed
);
1308 unlink ((char *)cache_file
);
1309 FcStrFree ((FcChar8
*)cache_file
);
1310 if (current_dir_block
)
1311 free (current_dir_block
);
1317 FcCacheMachineSignature ()
1319 static char buf
[MACHINE_SIGNATURE_SIZE
];
1320 int32_t magic
= ENDIAN_TEST
;
1321 char * m
= (char *)&magic
;
1323 sprintf (buf
, "%2x%2x%2x%2x "
1324 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
1325 "%4x %4x %4x %4x %4x %4x %4x %4x\n",
1326 m
[0], m
[1], m
[2], m
[3],
1327 (unsigned int)sizeof (char),
1328 (unsigned int)sizeof (char *),
1329 (unsigned int)sizeof (int),
1330 (unsigned int)sizeof (FcPattern
),
1331 (unsigned int)sizeof (FcPatternEltPtr
),
1332 (unsigned int)sizeof (struct _FcPatternElt
*),
1333 (unsigned int)sizeof (FcPatternElt
),
1334 (unsigned int)sizeof (FcObjectPtr
),
1335 (unsigned int)sizeof (FcValueListPtr
),
1336 (unsigned int)sizeof (FcValue
),
1337 (unsigned int)sizeof (FcValueBinding
),
1338 (unsigned int)sizeof (struct _FcValueList
*),
1339 (unsigned int)sizeof (FcCharSet
),
1340 (unsigned int)sizeof (FcCharLeaf
**),
1341 (unsigned int)sizeof (FcChar16
*),
1342 (unsigned int)sizeof (FcChar16
),
1343 (unsigned int)sizeof (FcCharLeaf
),
1344 (unsigned int)sizeof (FcChar32
),
1345 (unsigned int)sizeof (FcCache
),
1346 (unsigned int)sysconf(_SC_PAGESIZE
));
1351 static int banks_ptr
= 0, banks_alloc
= 0;
1352 int * _fcBankId
= 0, * _fcBankIdx
= 0;
1353 static const char ** bankDirs
= 0;
1356 FcCacheHaveBank (int bank
)
1360 if (bank
< FC_BANK_FIRST
)
1363 for (i
= 0; i
< banks_ptr
; i
++)
1364 if (_fcBankId
[i
] == bank
)
1371 FcCacheBankToIndexMTF (int bank
)
1375 for (i
= 0; i
< banks_ptr
; i
++)
1376 if (_fcBankId
[_fcBankIdx
[i
]] == bank
)
1378 int t
= _fcBankIdx
[i
];
1380 for (j
= i
; j
> 0; j
--)
1381 _fcBankIdx
[j
] = _fcBankIdx
[j
-1];
1386 if (banks_ptr
>= banks_alloc
)
1391 b
= realloc (_fcBankId
, (banks_alloc
+ 4) * sizeof(int));
1396 bidx
= realloc (_fcBankIdx
, (banks_alloc
+ 4) * sizeof(int));
1401 bds
= realloc (bankDirs
, (banks_alloc
+ 4) * sizeof (char *));
1410 _fcBankId
[i
] = bank
;
1416 FcCacheAddBankDir (int bank
, const char * dir
)
1418 int bi
= FcCacheBankToIndexMTF (bank
);
1423 bankDirs
[bi
] = (const char *)FcStrCopy ((FcChar8
*)dir
);
1427 FcCacheFindBankDir (int bank
)
1429 int bi
= FcCacheBankToIndex (bank
);
1430 return bankDirs
[bi
];
1434 * This code implements the MD5 message-digest algorithm.
1435 * The algorithm is due to Ron Rivest. This code was
1436 * written by Colin Plumb in 1993, no copyright is claimed.
1437 * This code is in the public domain; do with it what you wish.
1439 * Equivalent code is available from RSA Data Security, Inc.
1440 * This code has been tested against that, and is equivalent,
1441 * except that you don't need to include two pages of legalese
1444 * To compute the message digest of a chunk of bytes, declare an
1445 * MD5Context structure, pass it to MD5Init, call MD5Update as
1446 * needed on buffers full of bytes, and then call MD5Final, which
1447 * will fill a supplied 16-byte array with the digest.
1451 #define byteReverse(buf, len) /* Nothing */
1454 * Note: this code is harmless on little-endian machines.
1456 void byteReverse(unsigned char *buf
, unsigned longs
)
1460 t
= (FcChar32
) ((unsigned) buf
[3] << 8 | buf
[2]) << 16 |
1461 ((unsigned) buf
[1] << 8 | buf
[0]);
1462 *(FcChar32
*) buf
= t
;
1469 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1470 * initialization constants.
1472 static void MD5Init(struct MD5Context
*ctx
)
1474 ctx
->buf
[0] = 0x67452301;
1475 ctx
->buf
[1] = 0xefcdab89;
1476 ctx
->buf
[2] = 0x98badcfe;
1477 ctx
->buf
[3] = 0x10325476;
1484 * Update context to reflect the concatenation of another buffer full
1487 static void MD5Update(struct MD5Context
*ctx
, unsigned char *buf
, unsigned len
)
1491 /* Update bitcount */
1494 if ((ctx
->bits
[0] = t
+ ((FcChar32
) len
<< 3)) < t
)
1495 ctx
->bits
[1]++; /* Carry from low to high */
1496 ctx
->bits
[1] += len
>> 29;
1498 t
= (t
>> 3) & 0x3f; /* Bytes already in shsInfo->data */
1500 /* Handle any leading odd-sized chunks */
1503 unsigned char *p
= (unsigned char *) ctx
->in
+ t
;
1507 memcpy(p
, buf
, len
);
1511 byteReverse(ctx
->in
, 16);
1512 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1516 /* Process data in 64-byte chunks */
1519 memcpy(ctx
->in
, buf
, 64);
1520 byteReverse(ctx
->in
, 16);
1521 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1526 /* Handle any remaining bytes of data. */
1528 memcpy(ctx
->in
, buf
, len
);
1532 * Final wrapup - pad to 64-byte boundary with the bit pattern
1533 * 1 0* (64-bit count of bits processed, MSB-first)
1535 static void MD5Final(unsigned char digest
[16], struct MD5Context
*ctx
)
1540 /* Compute number of bytes mod 64 */
1541 count
= (ctx
->bits
[0] >> 3) & 0x3F;
1543 /* Set the first char of padding to 0x80. This is safe since there is
1544 always at least one byte free */
1545 p
= ctx
->in
+ count
;
1548 /* Bytes of padding needed to make 64 bytes */
1549 count
= 64 - 1 - count
;
1551 /* Pad out to 56 mod 64 */
1553 /* Two lots of padding: Pad the first block to 64 bytes */
1554 memset(p
, 0, count
);
1555 byteReverse(ctx
->in
, 16);
1556 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1558 /* Now fill the next block with 56 bytes */
1559 memset(ctx
->in
, 0, 56);
1561 /* Pad block to 56 bytes */
1562 memset(p
, 0, count
- 8);
1564 byteReverse(ctx
->in
, 14);
1566 /* Append length in bits and transform */
1567 ((FcChar32
*) ctx
->in
)[14] = ctx
->bits
[0];
1568 ((FcChar32
*) ctx
->in
)[15] = ctx
->bits
[1];
1570 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1571 byteReverse((unsigned char *) ctx
->buf
, 4);
1572 memcpy(digest
, ctx
->buf
, 16);
1573 memset(ctx
, 0, sizeof(ctx
)); /* In case it's sensitive */
1577 /* The four core functions - F1 is optimized somewhat */
1579 /* #define F1(x, y, z) (x & y | ~x & z) */
1580 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1581 #define F2(x, y, z) F1(z, x, y)
1582 #define F3(x, y, z) (x ^ y ^ z)
1583 #define F4(x, y, z) (y ^ (x | ~z))
1585 /* This is the central step in the MD5 algorithm. */
1586 #define MD5STEP(f, w, x, y, z, data, s) \
1587 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1590 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1591 * reflect the addition of 16 longwords of new data. MD5Update blocks
1592 * the data and converts bytes into longwords for this routine.
1594 static void MD5Transform(FcChar32 buf
[4], FcChar32 in
[16])
1596 register FcChar32 a
, b
, c
, d
;
1603 MD5STEP(F1
, a
, b
, c
, d
, in
[0] + 0xd76aa478, 7);
1604 MD5STEP(F1
, d
, a
, b
, c
, in
[1] + 0xe8c7b756, 12);
1605 MD5STEP(F1
, c
, d
, a
, b
, in
[2] + 0x242070db, 17);
1606 MD5STEP(F1
, b
, c
, d
, a
, in
[3] + 0xc1bdceee, 22);
1607 MD5STEP(F1
, a
, b
, c
, d
, in
[4] + 0xf57c0faf, 7);
1608 MD5STEP(F1
, d
, a
, b
, c
, in
[5] + 0x4787c62a, 12);
1609 MD5STEP(F1
, c
, d
, a
, b
, in
[6] + 0xa8304613, 17);
1610 MD5STEP(F1
, b
, c
, d
, a
, in
[7] + 0xfd469501, 22);
1611 MD5STEP(F1
, a
, b
, c
, d
, in
[8] + 0x698098d8, 7);
1612 MD5STEP(F1
, d
, a
, b
, c
, in
[9] + 0x8b44f7af, 12);
1613 MD5STEP(F1
, c
, d
, a
, b
, in
[10] + 0xffff5bb1, 17);
1614 MD5STEP(F1
, b
, c
, d
, a
, in
[11] + 0x895cd7be, 22);
1615 MD5STEP(F1
, a
, b
, c
, d
, in
[12] + 0x6b901122, 7);
1616 MD5STEP(F1
, d
, a
, b
, c
, in
[13] + 0xfd987193, 12);
1617 MD5STEP(F1
, c
, d
, a
, b
, in
[14] + 0xa679438e, 17);
1618 MD5STEP(F1
, b
, c
, d
, a
, in
[15] + 0x49b40821, 22);
1620 MD5STEP(F2
, a
, b
, c
, d
, in
[1] + 0xf61e2562, 5);
1621 MD5STEP(F2
, d
, a
, b
, c
, in
[6] + 0xc040b340, 9);
1622 MD5STEP(F2
, c
, d
, a
, b
, in
[11] + 0x265e5a51, 14);
1623 MD5STEP(F2
, b
, c
, d
, a
, in
[0] + 0xe9b6c7aa, 20);
1624 MD5STEP(F2
, a
, b
, c
, d
, in
[5] + 0xd62f105d, 5);
1625 MD5STEP(F2
, d
, a
, b
, c
, in
[10] + 0x02441453, 9);
1626 MD5STEP(F2
, c
, d
, a
, b
, in
[15] + 0xd8a1e681, 14);
1627 MD5STEP(F2
, b
, c
, d
, a
, in
[4] + 0xe7d3fbc8, 20);
1628 MD5STEP(F2
, a
, b
, c
, d
, in
[9] + 0x21e1cde6, 5);
1629 MD5STEP(F2
, d
, a
, b
, c
, in
[14] + 0xc33707d6, 9);
1630 MD5STEP(F2
, c
, d
, a
, b
, in
[3] + 0xf4d50d87, 14);
1631 MD5STEP(F2
, b
, c
, d
, a
, in
[8] + 0x455a14ed, 20);
1632 MD5STEP(F2
, a
, b
, c
, d
, in
[13] + 0xa9e3e905, 5);
1633 MD5STEP(F2
, d
, a
, b
, c
, in
[2] + 0xfcefa3f8, 9);
1634 MD5STEP(F2
, c
, d
, a
, b
, in
[7] + 0x676f02d9, 14);
1635 MD5STEP(F2
, b
, c
, d
, a
, in
[12] + 0x8d2a4c8a, 20);
1637 MD5STEP(F3
, a
, b
, c
, d
, in
[5] + 0xfffa3942, 4);
1638 MD5STEP(F3
, d
, a
, b
, c
, in
[8] + 0x8771f681, 11);
1639 MD5STEP(F3
, c
, d
, a
, b
, in
[11] + 0x6d9d6122, 16);
1640 MD5STEP(F3
, b
, c
, d
, a
, in
[14] + 0xfde5380c, 23);
1641 MD5STEP(F3
, a
, b
, c
, d
, in
[1] + 0xa4beea44, 4);
1642 MD5STEP(F3
, d
, a
, b
, c
, in
[4] + 0x4bdecfa9, 11);
1643 MD5STEP(F3
, c
, d
, a
, b
, in
[7] + 0xf6bb4b60, 16);
1644 MD5STEP(F3
, b
, c
, d
, a
, in
[10] + 0xbebfbc70, 23);
1645 MD5STEP(F3
, a
, b
, c
, d
, in
[13] + 0x289b7ec6, 4);
1646 MD5STEP(F3
, d
, a
, b
, c
, in
[0] + 0xeaa127fa, 11);
1647 MD5STEP(F3
, c
, d
, a
, b
, in
[3] + 0xd4ef3085, 16);
1648 MD5STEP(F3
, b
, c
, d
, a
, in
[6] + 0x04881d05, 23);
1649 MD5STEP(F3
, a
, b
, c
, d
, in
[9] + 0xd9d4d039, 4);
1650 MD5STEP(F3
, d
, a
, b
, c
, in
[12] + 0xe6db99e5, 11);
1651 MD5STEP(F3
, c
, d
, a
, b
, in
[15] + 0x1fa27cf8, 16);
1652 MD5STEP(F3
, b
, c
, d
, a
, in
[2] + 0xc4ac5665, 23);
1654 MD5STEP(F4
, a
, b
, c
, d
, in
[0] + 0xf4292244, 6);
1655 MD5STEP(F4
, d
, a
, b
, c
, in
[7] + 0x432aff97, 10);
1656 MD5STEP(F4
, c
, d
, a
, b
, in
[14] + 0xab9423a7, 15);
1657 MD5STEP(F4
, b
, c
, d
, a
, in
[5] + 0xfc93a039, 21);
1658 MD5STEP(F4
, a
, b
, c
, d
, in
[12] + 0x655b59c3, 6);
1659 MD5STEP(F4
, d
, a
, b
, c
, in
[3] + 0x8f0ccc92, 10);
1660 MD5STEP(F4
, c
, d
, a
, b
, in
[10] + 0xffeff47d, 15);
1661 MD5STEP(F4
, b
, c
, d
, a
, in
[1] + 0x85845dd1, 21);
1662 MD5STEP(F4
, a
, b
, c
, d
, in
[8] + 0x6fa87e4f, 6);
1663 MD5STEP(F4
, d
, a
, b
, c
, in
[15] + 0xfe2ce6e0, 10);
1664 MD5STEP(F4
, c
, d
, a
, b
, in
[6] + 0xa3014314, 15);
1665 MD5STEP(F4
, b
, c
, d
, a
, in
[13] + 0x4e0811a1, 21);
1666 MD5STEP(F4
, a
, b
, c
, d
, in
[4] + 0xf7537e82, 6);
1667 MD5STEP(F4
, d
, a
, b
, c
, in
[11] + 0xbd3af235, 10);
1668 MD5STEP(F4
, c
, d
, a
, b
, in
[2] + 0x2ad7d2bb, 15);
1669 MD5STEP(F4
, b
, c
, d
, a
, in
[9] + 0xeb86d391, 21);