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 FcMemFree (FC_MEM_STRING
, strlen (d
->name
)+1);
162 FcMemFree (FC_MEM_CACHE
, sizeof (FcGlobalCacheDir
));
167 FcGlobalCacheCreate (void)
169 FcGlobalCache
*cache
;
171 cache
= malloc (sizeof (FcGlobalCache
));
174 FcMemAlloc (FC_MEM_CACHE
, sizeof (FcGlobalCache
));
176 cache
->updated
= FcFalse
;
182 FcGlobalCacheDestroy (FcGlobalCache
*cache
)
184 FcGlobalCacheDir
*d
, *next
;
186 for (d
= cache
->dirs
; d
; d
= next
)
189 FcGlobalCacheDirDestroy (d
);
191 FcMemFree (FC_MEM_CACHE
, sizeof (FcGlobalCache
));
196 FcGlobalCacheLoad (FcGlobalCache
*cache
,
198 const FcChar8
*cache_file
,
201 char name_buf
[FC_MAX_FILE_LEN
];
202 FcGlobalCacheDir
*d
, *next
;
203 FcFileTime config_time
= FcConfigModifiedTime (config
);
204 char * current_arch_machine_name
;
205 char candidate_arch_machine_name
[MACHINE_SIGNATURE_SIZE
+ 9];
206 off_t current_arch_start
;
208 struct stat cache_stat
, dir_stat
;
210 if (stat ((char *) cache_file
, &cache_stat
) < 0)
213 cache
->fd
= open ((char *) cache_file
, O_RDONLY
);
217 cache
->updated
= FcFalse
;
219 FcCacheReadString (cache
->fd
, name_buf
, sizeof (name_buf
));
220 if (strcmp (name_buf
, FC_GLOBAL_MAGIC_COOKIE
) != 0)
223 current_arch_machine_name
= FcCacheMachineSignature ();
224 current_arch_start
= FcCacheSkipToArch(cache
->fd
,
225 current_arch_machine_name
);
226 if (current_arch_start
< 0)
227 goto bail_and_destroy
;
229 lseek (cache
->fd
, current_arch_start
, SEEK_SET
);
230 FcCacheReadString (cache
->fd
, candidate_arch_machine_name
,
231 sizeof (candidate_arch_machine_name
));
232 if (strlen(candidate_arch_machine_name
) == 0)
233 goto bail_and_destroy
;
239 FcCacheReadString (cache
->fd
, name_buf
, sizeof (name_buf
));
240 if (!strlen(name_buf
))
243 /* Directory must be older than the global cache file; also
244 cache must be newer than the config file. */
245 if (stat ((char *) name_buf
, &dir_stat
) < 0 ||
246 dir_stat
.st_mtime
> cache_stat
.st_mtime
||
247 (config_time
.set
&& cache_stat
.st_mtime
< config_time
.time
))
251 FcStrSetAdd (staleDirs
, FcStrCopy ((FcChar8
*)name_buf
));
252 read (cache
->fd
, &md
, sizeof (FcCache
));
253 lseek (cache
->fd
, FcCacheNextOffset (lseek(cache
->fd
, 0, SEEK_CUR
)) + md
.count
, SEEK_SET
);
257 d
= malloc (sizeof (FcGlobalCacheDir
));
261 d
->next
= cache
->dirs
;
264 d
->name
= (char *)FcStrCopy ((FcChar8
*)name_buf
);
266 d
->offset
= lseek (cache
->fd
, 0, SEEK_CUR
);
267 if (read (cache
->fd
, &d
->metadata
, sizeof (FcCache
)) != sizeof (FcCache
))
269 targ
= FcCacheNextOffset (lseek(cache
->fd
, 0, SEEK_CUR
)) + d
->metadata
.count
;
270 if (lseek (cache
->fd
, targ
, SEEK_SET
) != targ
)
276 for (d
= cache
->dirs
; d
; d
= next
)
291 if (stat ((char *) cache_file
, &cache_stat
) == 0)
292 unlink ((char *)cache_file
);
299 FcGlobalCacheReadDir (FcFontSet
*set
, FcStrSet
*dirs
, FcGlobalCache
* cache
, const char *dir
, FcConfig
*config
)
302 FcBool ret
= FcFalse
;
307 dir
= (char *)FcConfigNormalizeFontDir (config
, (FcChar8
*)dir
);
308 for (d
= cache
->dirs
; d
; d
= d
->next
)
310 if (strncmp (d
->name
, dir
, strlen(dir
)) == 0)
312 lseek (cache
->fd
, d
->offset
, SEEK_SET
);
313 if (!FcDirCacheConsume (cache
->fd
, dir
, set
, config
))
315 if (strcmp (d
->name
, dir
) == 0)
324 FcGlobalCacheUpdate (FcGlobalCache
*cache
,
329 FcGlobalCacheDir
* d
;
331 name
= (char *)FcConfigNormalizeFontDir (config
, (FcChar8
*)name
);
332 for (d
= cache
->dirs
; d
; d
= d
->next
)
334 if (strcmp(d
->name
, name
) == 0)
340 d
= malloc (sizeof (FcGlobalCacheDir
));
343 d
->next
= cache
->dirs
;
347 cache
->updated
= FcTrue
;
349 d
->name
= (char *)FcStrCopy ((FcChar8
*)name
);
350 d
->ent
= FcDirCacheProduce (set
, &d
->metadata
);
356 FcGlobalCacheSave (FcGlobalCache
*cache
,
357 const FcChar8
*cache_file
,
361 FcGlobalCacheDir
*dir
;
363 off_t current_arch_start
= 0, truncate_to
;
364 char * current_arch_machine_name
, * header
;
369 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
370 /* Set-UID programs can't safely update the cache */
371 if (getuid () != geteuid ())
375 atomic
= FcAtomicCreate (cache_file
);
379 if (!FcAtomicLock (atomic
))
381 fd
= open ((char *) FcAtomicNewFile(atomic
), O_RDWR
| O_CREAT
,
385 FcCacheWriteString (fd
, FC_GLOBAL_MAGIC_COOKIE
);
387 fd_orig
= open ((char *) FcAtomicOrigFile(atomic
), O_RDONLY
);
389 current_arch_machine_name
= FcCacheMachineSignature ();
391 current_arch_start
= 0;
393 current_arch_start
= FcCacheSkipToArch (fd_orig
,
394 current_arch_machine_name
);
396 if (current_arch_start
< 0)
398 off_t i
= lseek(fd_orig
, 0, SEEK_END
);
399 if (i
< strlen (FC_GLOBAL_MAGIC_COOKIE
)+1)
400 i
= strlen (FC_GLOBAL_MAGIC_COOKIE
)+1;
401 current_arch_start
= FcCacheNextOffset (i
);
404 if (!FcCacheCopyOld(fd
, fd_orig
, current_arch_start
))
410 current_arch_start
= lseek(fd
, 0, SEEK_CUR
);
411 if (ftruncate (fd
, current_arch_start
) == -1)
414 header
= malloc (10 + strlen (current_arch_machine_name
));
418 truncate_to
= current_arch_start
+ strlen(current_arch_machine_name
) + 11;
419 for (dir
= cache
->dirs
; dir
; dir
= dir
->next
)
421 truncate_to
+= strlen(dir
->name
) + 1;
422 truncate_to
+= sizeof (FcCache
);
423 truncate_to
= FcCacheNextOffset (truncate_to
);
424 truncate_to
+= dir
->metadata
.count
;
426 truncate_to
-= current_arch_start
;
428 sprintf (header
, "%8x ", (int)truncate_to
);
429 strcat (header
, current_arch_machine_name
);
430 if (!FcCacheWriteString (fd
, header
))
433 for (dir
= cache
->dirs
; dir
; dir
= dir
->next
)
437 const char * d
= (const char *)FcConfigNormalizeFontDir (config
, (const FcChar8
*)dir
->name
);
439 FcCacheWriteString (fd
, d
);
440 write (fd
, &dir
->metadata
, sizeof(FcCache
));
441 lseek (fd
, FcCacheNextOffset (lseek(fd
, 0, SEEK_CUR
)), SEEK_SET
);
442 write (fd
, dir
->ent
, dir
->metadata
.count
);
446 FcCacheWriteString (fd
, "");
448 if (close (fd
) == -1)
451 if (!FcAtomicReplaceOrig (atomic
))
454 FcAtomicUnlock (atomic
);
455 FcAtomicDestroy (atomic
);
457 cache
->updated
= FcFalse
;
468 FcAtomicDeleteNew (atomic
);
470 FcAtomicUnlock (atomic
);
472 FcAtomicDestroy (atomic
);
477 * Find the next presumably-mmapable offset after the supplied file
481 FcCacheNextOffset(off_t w
)
483 static long pagesize
= -1;
485 pagesize
= sysconf(_SC_PAGESIZE
);
486 if (w
% pagesize
== 0)
489 return ((w
/ pagesize
)+1)*pagesize
;
492 /* return the address of the segment for the provided arch,
493 * or -1 if arch not found */
495 FcCacheSkipToArch (int fd
, const char * arch
)
497 char candidate_arch_machine_name_count
[MACHINE_SIGNATURE_SIZE
+ 9];
498 char * candidate_arch
;
499 off_t current_arch_start
= 0;
501 lseek (fd
, 0, SEEK_SET
);
502 FcCacheSkipString (fd
);
503 current_arch_start
= lseek (fd
, 0, SEEK_CUR
);
505 /* skip arches that are not the current arch */
510 if (lseek (fd
, current_arch_start
, SEEK_SET
) != current_arch_start
)
513 if (FcCacheReadString (fd
, candidate_arch_machine_name_count
,
514 sizeof (candidate_arch_machine_name_count
)) == 0)
516 if (!strlen(candidate_arch_machine_name_count
))
518 bs
= strtol(candidate_arch_machine_name_count
, &candidate_arch
, 16);
520 // count = 0 should probably be distinguished from the !bs condition
521 if (!bs
|| bs
< strlen (candidate_arch_machine_name_count
))
524 candidate_arch
++; /* skip leading space */
526 if (strcmp (candidate_arch
, arch
)==0)
527 return current_arch_start
;
528 current_arch_start
+= bs
;
534 /* Cuts out the segment at the file pointer (moves everything else
535 * down to cover it), and leaves the file pointer at the end of the
538 FcCacheCopyOld (int fd
, int fd_orig
, off_t start
)
540 char * buf
= malloc (8192);
541 char candidate_arch_machine_name
[MACHINE_SIGNATURE_SIZE
+ 9];
543 int c
, bytes_skipped
;
550 lseek (fd
, 0, SEEK_SET
); lseek (fd_orig
, 0, SEEK_SET
);
557 if ((c
= read (fd_orig
, buf
, b
)) <= 0)
559 if (write (fd
, buf
, c
) < 0)
566 lseek (fd
, start
, SEEK_SET
);
567 if (FcCacheReadString (fd
, candidate_arch_machine_name
,
568 sizeof (candidate_arch_machine_name
)) == 0)
570 if (!strlen(candidate_arch_machine_name
))
573 bs
= strtol(candidate_arch_machine_name
, 0, 16);
580 lseek (fd
, start
+bs
+bytes_skipped
, SEEK_SET
);
581 if ((c
= read (fd
, buf
, 8192)) <= 0)
583 lseek (fd
, start
+bytes_skipped
, SEEK_SET
);
584 if (write (fd
, buf
, c
) < 0)
589 lseek (fd
, start
+bytes_skipped
, SEEK_SET
);
600 /* Does not check that the cache has the appropriate arch section. */
601 /* Also, this can be fooled if the original location has a stale
602 * cache, and the hashed location has an up-to-date cache. Oh well,
603 * sucks to be you in that case! */
605 FcDirCacheValid (const FcChar8
*dir
)
607 struct stat file_stat
, dir_stat
;
610 if (stat ((char *) dir
, &dir_stat
) < 0)
613 fd
= FcDirCacheOpen (dir
);
617 if (fstat (fd
, &file_stat
) < 0)
623 * If the directory has been modified more recently than
624 * the cache file, the cache is not valid
626 if (dir_stat
.st_mtime
> file_stat
.st_mtime
)
636 /* Assumes that the cache file in 'dir' exists.
637 * Checks that the cache has the appropriate arch section. */
639 FcDirCacheHasCurrentArch (const FcChar8
*dir
)
642 off_t current_arch_start
;
643 char *current_arch_machine_name
;
645 fd
= FcDirCacheOpen (dir
);
649 current_arch_machine_name
= FcCacheMachineSignature();
650 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
653 if (current_arch_start
< 0)
663 FcDirCacheUnlink (const FcChar8
*dir
, FcConfig
*config
)
666 char *cache_hashed
= 0;
668 struct stat cache_stat
;
669 char name_buf
[FC_MAX_FILE_LEN
];
671 dir
= FcConfigNormalizeFontDir (config
, dir
);
672 cache_file
= (char *)FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
676 /* First remove normal cache file. */
677 if (stat ((char *) cache_file
, &cache_stat
) == 0 &&
678 unlink ((char *)cache_file
) != 0)
681 /* Next remove any applicable hashed files. */
682 fd
= -1; collisions
= 0;
686 FcStrFree ((FcChar8
*)cache_hashed
);
688 cache_hashed
= FcDirCacheHashName (cache_file
, collisions
++);
694 fd
= open(cache_hashed
, O_RDONLY
);
697 FcStrFree ((FcChar8
*)cache_file
);
701 FcCacheReadString (fd
, name_buf
, sizeof (name_buf
));
702 if (!strlen(name_buf
))
704 FcStrFree ((FcChar8
*)cache_hashed
);
707 } while (strcmp (name_buf
, cache_file
) != 0);
709 FcStrFree ((FcChar8
*)cache_file
);
712 if (stat ((char *) cache_hashed
, &cache_stat
) == 0 &&
713 unlink ((char *)cache_hashed
) != 0)
715 FcStrFree ((FcChar8
*)cache_hashed
);
719 FcStrFree ((FcChar8
*)cache_hashed
);
723 FcStrFree ((FcChar8
*)cache_file
);
728 FcCacheReadDirs (FcConfig
* config
, FcGlobalCache
* cache
,
729 FcStrList
*list
, FcFontSet
* set
)
733 FcChar8
*file
, *base
;
739 * Read in the results from 'list'.
741 while ((dir
= FcStrListNext (list
)))
743 if (!FcConfigAcceptFilename (config
, dir
))
747 file
= (FcChar8
*) malloc (strlen ((char *) dir
) + 1 + FC_MAX_FILE_LEN
+ 1);
751 strcpy ((char *) file
, (char *) dir
);
752 strcat ((char *) file
, "/");
753 base
= file
+ strlen ((char *) file
);
755 subdirs
= FcStrSetCreate ();
758 fprintf (stderr
, "Can't create directory set\n");
764 if (access ((char *) dir
, X_OK
) < 0)
772 fprintf (stderr
, "\"%s\": ", dir
);
776 FcStrSetDestroy (subdirs
);
780 if (stat ((char *) dir
, &statb
) == -1)
782 fprintf (stderr
, "\"%s\": ", dir
);
784 FcStrSetDestroy (subdirs
);
789 if (!S_ISDIR (statb
.st_mode
))
791 fprintf (stderr
, "\"%s\": not a directory, skipping\n", dir
);
792 FcStrSetDestroy (subdirs
);
796 if (!FcDirCacheValid (dir
) || !FcDirCacheRead (set
, subdirs
, dir
, config
))
798 if (FcDebug () & FC_DBG_FONTSET
)
799 printf ("cache scan dir %s\n", dir
);
801 FcDirScanConfig (set
, subdirs
, cache
,
802 config
->blanks
, dir
, FcFalse
, config
);
804 sublist
= FcStrListCreate (subdirs
);
805 FcStrSetDestroy (subdirs
);
808 fprintf (stderr
, "Can't create subdir list in \"%s\"\n", dir
);
813 ret
+= FcCacheReadDirs (config
, cache
, sublist
, set
);
816 FcStrListDone (list
);
821 FcCacheRead (FcConfig
*config
, FcGlobalCache
* cache
)
823 FcFontSet
* s
= FcFontSetCreate();
827 if (FcCacheReadDirs (config
, cache
, FcConfigGetConfigDirs (config
), s
))
833 FcFontSetDestroy (s
);
837 static const char bin2hex
[] = { '0', '1', '2', '3',
840 'c', 'd', 'e', 'f' };
843 FcDirCacheHashName (char * cache_file
, int collisions
)
845 unsigned char hash
[16], hex_hash
[33];
847 unsigned char uscore
= '_';
850 struct MD5Context ctx
;
853 MD5Update (&ctx
, (unsigned char *)cache_file
, strlen (cache_file
));
855 for (i
= 0; i
< collisions
; i
++)
856 MD5Update (&ctx
, &uscore
, 1);
858 MD5Final (hash
, &ctx
);
860 for (cnt
= 0; cnt
< 16; ++cnt
)
862 hex_hash
[2*cnt
] = bin2hex
[hash
[cnt
] >> 4];
863 hex_hash
[2*cnt
+1] = bin2hex
[hash
[cnt
] & 0xf];
867 tmp
= FcStrPlus ((FcChar8
*)hex_hash
, (FcChar8
*)FC_CACHE_SUFFIX
);
871 cache_hashed
= (char *)FcStrPlus ((FcChar8
*)PKGCACHEDIR
"/", tmp
);
877 /* Opens the hashed name for cache_file.
878 * This would fail in the unlikely event of a collision and subsequent
879 * removal of the file which originally caused the collision. */
881 FcDirCacheOpen (const FcChar8
*dir
)
884 int fd
= -1, collisions
= 0;
885 char *cache_file
, *cache_hashed
;
886 char name_buf
[FC_MAX_FILE_LEN
];
887 struct stat dir_stat
;
889 cache_file
= (char *)FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
893 fd
= open(cache_file
, O_RDONLY
);
897 if (stat ((char *)dir
, &dir_stat
) == -1)
904 FcChar8
* name_buf_dir
;
906 cache_hashed
= FcDirCacheHashName (cache_file
, collisions
++);
909 FcStrFree ((FcChar8
*)cache_file
);
915 fd
= open(cache_hashed
, O_RDONLY
);
916 FcStrFree ((FcChar8
*)cache_hashed
);
920 FcStrFree ((FcChar8
*)cache_file
);
923 FcCacheReadString (fd
, name_buf
, sizeof (name_buf
));
924 if (!strlen(name_buf
))
927 name_buf_dir
= FcStrDirname ((FcChar8
*)name_buf
);
928 if (stat ((char *)name_buf_dir
, &c
) == -1)
930 FcStrFree (name_buf_dir
);
933 FcStrFree (name_buf_dir
);
934 found
= (c
.st_ino
== dir_stat
.st_ino
) && (c
.st_dev
== dir_stat
.st_dev
);
936 FcStrFree ((FcChar8
*)cache_file
);
940 FcStrFree ((FcChar8
*)cache_file
);
945 /* read serialized state from the cache file */
947 FcDirCacheRead (FcFontSet
* set
, FcStrSet
* dirs
, const FcChar8
*dir
, FcConfig
*config
)
950 char *current_arch_machine_name
;
951 char candidate_arch_machine_name
[9+MACHINE_SIGNATURE_SIZE
];
952 off_t current_arch_start
= 0;
953 char subdirName
[FC_MAX_FILE_LEN
+ 1 + 12 + 1];
955 fd
= FcDirCacheOpen (dir
);
959 current_arch_machine_name
= FcCacheMachineSignature();
960 current_arch_start
= FcCacheSkipToArch(fd
,
961 current_arch_machine_name
);
962 if (current_arch_start
< 0)
965 lseek (fd
, current_arch_start
, SEEK_SET
);
966 if (FcCacheReadString (fd
, candidate_arch_machine_name
,
967 sizeof (candidate_arch_machine_name
)) == 0)
970 while (strlen(FcCacheReadString (fd
, subdirName
, sizeof (subdirName
))) > 0)
971 FcStrSetAdd (dirs
, (FcChar8
*)subdirName
);
973 if (!FcDirCacheConsume (fd
, (const char *)dir
, set
, config
))
986 FcDirCacheConsume (int fd
, const char * dir
, FcFontSet
*set
, FcConfig
*config
)
989 void * current_dir_block
;
992 read(fd
, &metadata
, sizeof(FcCache
));
993 if (metadata
.magic
!= FC_CACHE_MAGIC
)
998 pos
= FcCacheNextOffset (lseek(fd
, 0, SEEK_CUR
));
999 lseek (fd
, pos
, SEEK_SET
);
1001 FcConfigAddFontDir (config
, (FcChar8
*)dir
);
1005 pos
= FcCacheNextOffset (lseek(fd
, 0, SEEK_CUR
));
1006 current_dir_block
= mmap (0, metadata
.count
,
1007 PROT_READ
, MAP_SHARED
, fd
, pos
);
1008 lseek (fd
, pos
+metadata
.count
, SEEK_SET
);
1009 if (current_dir_block
== MAP_FAILED
)
1012 FcCacheAddBankDir (metadata
.bank
, dir
);
1014 FcConfigAddFontDir (config
, (FcChar8
*)dir
);
1016 if (!FcFontSetUnserialize (&metadata
, set
, current_dir_block
))
1023 FcDirCacheProduce (FcFontSet
*set
, FcCache
*metadata
)
1025 void * current_dir_block
, * final_dir_block
;
1026 static unsigned int rand_state
= 0;
1027 int bank
, needed_bytes_no_align
;
1030 rand_state
= time(0L);
1031 bank
= rand_r(&rand_state
);
1033 while (FcCacheHaveBank(bank
))
1034 bank
= rand_r(&rand_state
);
1036 memset (metadata
, 0, sizeof(FcCache
));
1038 needed_bytes_no_align
= FcFontSetNeededBytes (set
);
1039 metadata
->count
= needed_bytes_no_align
+
1040 FcFontSetNeededBytesAlign ();
1041 metadata
->magic
= FC_CACHE_MAGIC
;
1042 metadata
->bank
= bank
;
1044 if (!needed_bytes_no_align
) /* not a failure, no fonts to write */
1046 /* no, you don't really need to write any bytes at all. */
1047 metadata
->count
= 0;
1051 current_dir_block
= malloc (metadata
->count
);
1052 if (!current_dir_block
)
1055 memset (current_dir_block
, 0, metadata
->count
);
1056 final_dir_block
= FcFontSetDistributeBytes (metadata
, current_dir_block
);
1058 if ((void *)((char *)current_dir_block
+metadata
->count
) < final_dir_block
)
1061 if (!FcFontSetSerialize (bank
, set
))
1064 return current_dir_block
;
1067 free (current_dir_block
);
1071 /* write serialized state to the cache file */
1073 FcDirCacheWrite (FcFontSet
*set
, FcStrSet
*dirs
, const FcChar8
*dir
)
1077 int fd
, fd_orig
, i
, dirs_count
;
1080 off_t current_arch_start
= 0, truncate_to
;
1081 char name_buf
[FC_MAX_FILE_LEN
];
1084 char *current_arch_machine_name
, * header
;
1085 void *current_dir_block
= 0;
1087 dir
= FcConfigNormalizeFontDir (FcConfigGetCurrent(), dir
);
1091 cache_file
= (char *)FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
1095 /* Ensure that we're not trampling a cache for some other dir. */
1096 /* This is slightly different from FcDirCacheOpen, since it
1097 * needs the filename, not the file descriptor. */
1098 fd
= -1; collisions
= 0;
1101 cache_hashed
= FcDirCacheHashName (cache_file
, collisions
++);
1107 fd
= open(cache_hashed
, O_RDONLY
);
1110 FcCacheReadString (fd
, name_buf
, sizeof (name_buf
));
1113 if (!strlen(name_buf
))
1116 if (strcmp (name_buf
, cache_file
) != 0)
1120 current_dir_block
= FcDirCacheProduce (set
, &metadata
);
1122 if (metadata
.count
&& !current_dir_block
)
1125 if (FcDebug () & FC_DBG_CACHE
)
1126 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file
);
1128 atomic
= FcAtomicCreate ((FcChar8
*)cache_hashed
);
1132 if (!FcAtomicLock (atomic
))
1134 /* Now try rewriting the original version of the file. */
1135 FcAtomicDestroy (atomic
);
1137 atomic
= FcAtomicCreate ((FcChar8
*)cache_file
);
1138 fd_orig
= open (cache_file
, O_RDONLY
);
1140 fd_orig
= open((char *)FcAtomicOrigFile (atomic
), O_RDONLY
);
1142 fd
= open((char *)FcAtomicNewFile (atomic
), O_RDWR
| O_CREAT
, 0666);
1147 /* In all cases, try opening the real location of the cache file first. */
1148 /* (even if that's not atomic.) */
1149 fd_orig
= open (cache_file
, O_RDONLY
);
1151 fd_orig
= open((char *)FcAtomicOrigFile (atomic
), O_RDONLY
);
1153 fd
= open((char *)FcAtomicNewFile (atomic
), O_RDWR
| O_CREAT
, 0666);
1157 FcCacheWriteString (fd
, cache_file
);
1159 current_arch_machine_name
= FcCacheMachineSignature ();
1160 current_arch_start
= 0;
1163 current_arch_start
=
1164 FcCacheSkipToArch(fd_orig
, current_arch_machine_name
);
1166 if (current_arch_start
< 0)
1168 off_t i
= lseek(fd_orig
, 0, SEEK_END
);
1169 if (i
< strlen (FC_GLOBAL_MAGIC_COOKIE
)+1)
1170 i
= strlen (FC_GLOBAL_MAGIC_COOKIE
)+1;
1171 current_arch_start
= FcCacheNextOffset (i
);
1174 if (fd_orig
!= -1 && !FcCacheCopyOld(fd
, fd_orig
, current_arch_start
))
1180 current_arch_start
= lseek(fd
, 0, SEEK_CUR
);
1181 if (ftruncate (fd
, current_arch_start
) == -1)
1184 /* allocate space for subdir names in this block */
1186 for (i
= 0; i
< dirs
->size
; i
++)
1187 dirs_count
+= strlen((char *)dirs
->strs
[i
]) + 1;
1190 /* now write the address of the next offset */
1191 truncate_to
= FcCacheNextOffset (FcCacheNextOffset (current_arch_start
+ sizeof (FcCache
) + dirs_count
) + metadata
.count
) - current_arch_start
;
1192 header
= malloc (10 + strlen (current_arch_machine_name
));
1195 sprintf (header
, "%8x ", (int)truncate_to
);
1196 strcat (header
, current_arch_machine_name
);
1197 if (!FcCacheWriteString (fd
, header
))
1200 for (i
= 0; i
< dirs
->size
; i
++)
1201 FcCacheWriteString (fd
, (char *)dirs
->strs
[i
]);
1202 FcCacheWriteString (fd
, "");
1204 write (fd
, &metadata
, sizeof(FcCache
));
1207 lseek (fd
, FcCacheNextOffset (lseek(fd
, 0, SEEK_END
)), SEEK_SET
);
1208 write (fd
, current_dir_block
, metadata
.count
);
1209 free (current_dir_block
);
1212 /* this actually serves to pad out the cache file, if needed */
1213 if (ftruncate (fd
, current_arch_start
+ truncate_to
) == -1)
1218 if (!FcAtomicReplaceOrig(atomic
))
1220 FcStrFree ((FcChar8
*)cache_hashed
);
1221 FcStrFree ((FcChar8
*)cache_file
);
1222 FcAtomicUnlock (atomic
);
1223 FcAtomicDestroy (atomic
);
1231 FcAtomicUnlock (atomic
);
1233 FcAtomicDestroy (atomic
);
1235 FcStrFree ((FcChar8
*)cache_hashed
);
1237 unlink ((char *)cache_file
);
1238 FcStrFree ((FcChar8
*)cache_file
);
1239 if (current_dir_block
)
1240 free (current_dir_block
);
1246 FcCacheMachineSignature ()
1248 static char buf
[MACHINE_SIGNATURE_SIZE
];
1249 int32_t magic
= ENDIAN_TEST
;
1250 char * m
= (char *)&magic
;
1252 sprintf (buf
, "%2x%2x%2x%2x "
1253 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
1254 "%4x %4x %4x %4x %4x %4x %4x %4x\n",
1255 m
[0], m
[1], m
[2], m
[3],
1256 (unsigned int)sizeof (char),
1257 (unsigned int)sizeof (char *),
1258 (unsigned int)sizeof (int),
1259 (unsigned int)sizeof (FcPattern
),
1260 (unsigned int)sizeof (FcPatternEltPtr
),
1261 (unsigned int)sizeof (struct _FcPatternElt
*),
1262 (unsigned int)sizeof (FcPatternElt
),
1263 (unsigned int)sizeof (FcObjectPtr
),
1264 (unsigned int)sizeof (FcValueListPtr
),
1265 (unsigned int)sizeof (FcValue
),
1266 (unsigned int)sizeof (FcValueBinding
),
1267 (unsigned int)sizeof (struct _FcValueList
*),
1268 (unsigned int)sizeof (FcCharSet
),
1269 (unsigned int)sizeof (FcCharLeaf
**),
1270 (unsigned int)sizeof (FcChar16
*),
1271 (unsigned int)sizeof (FcChar16
),
1272 (unsigned int)sizeof (FcCharLeaf
),
1273 (unsigned int)sizeof (FcChar32
),
1274 (unsigned int)sizeof (FcCache
),
1275 (unsigned int)sysconf(_SC_PAGESIZE
));
1280 static int banks_ptr
= 0, banks_alloc
= 0;
1281 int * _fcBankId
= 0, * _fcBankIdx
= 0;
1282 static const char ** bankDirs
= 0;
1285 FcCacheHaveBank (int bank
)
1289 if (bank
< FC_BANK_FIRST
)
1292 for (i
= 0; i
< banks_ptr
; i
++)
1293 if (_fcBankId
[i
] == bank
)
1300 FcCacheBankToIndexMTF (int bank
)
1304 for (i
= 0; i
< banks_ptr
; i
++)
1305 if (_fcBankId
[_fcBankIdx
[i
]] == bank
)
1307 int t
= _fcBankIdx
[i
];
1309 for (j
= i
; j
> 0; j
--)
1310 _fcBankIdx
[j
] = _fcBankIdx
[j
-1];
1315 if (banks_ptr
>= banks_alloc
)
1320 b
= realloc (_fcBankId
, (banks_alloc
+ 4) * sizeof(int));
1325 bidx
= realloc (_fcBankIdx
, (banks_alloc
+ 4) * sizeof(int));
1330 bds
= realloc (bankDirs
, (banks_alloc
+ 4) * sizeof (char *));
1339 _fcBankId
[i
] = bank
;
1345 FcCacheAddBankDir (int bank
, const char * dir
)
1347 int bi
= FcCacheBankToIndexMTF (bank
);
1352 bankDirs
[bi
] = (const char *)FcStrCopy ((FcChar8
*)dir
);
1356 FcCacheFindBankDir (int bank
)
1358 int bi
= FcCacheBankToIndex (bank
);
1359 return bankDirs
[bi
];
1363 * This code implements the MD5 message-digest algorithm.
1364 * The algorithm is due to Ron Rivest. This code was
1365 * written by Colin Plumb in 1993, no copyright is claimed.
1366 * This code is in the public domain; do with it what you wish.
1368 * Equivalent code is available from RSA Data Security, Inc.
1369 * This code has been tested against that, and is equivalent,
1370 * except that you don't need to include two pages of legalese
1373 * To compute the message digest of a chunk of bytes, declare an
1374 * MD5Context structure, pass it to MD5Init, call MD5Update as
1375 * needed on buffers full of bytes, and then call MD5Final, which
1376 * will fill a supplied 16-byte array with the digest.
1380 #define byteReverse(buf, len) /* Nothing */
1383 * Note: this code is harmless on little-endian machines.
1385 void byteReverse(unsigned char *buf
, unsigned longs
)
1389 t
= (FcChar32
) ((unsigned) buf
[3] << 8 | buf
[2]) << 16 |
1390 ((unsigned) buf
[1] << 8 | buf
[0]);
1391 *(FcChar32
*) buf
= t
;
1398 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1399 * initialization constants.
1401 static void MD5Init(struct MD5Context
*ctx
)
1403 ctx
->buf
[0] = 0x67452301;
1404 ctx
->buf
[1] = 0xefcdab89;
1405 ctx
->buf
[2] = 0x98badcfe;
1406 ctx
->buf
[3] = 0x10325476;
1413 * Update context to reflect the concatenation of another buffer full
1416 static void MD5Update(struct MD5Context
*ctx
, unsigned char *buf
, unsigned len
)
1420 /* Update bitcount */
1423 if ((ctx
->bits
[0] = t
+ ((FcChar32
) len
<< 3)) < t
)
1424 ctx
->bits
[1]++; /* Carry from low to high */
1425 ctx
->bits
[1] += len
>> 29;
1427 t
= (t
>> 3) & 0x3f; /* Bytes already in shsInfo->data */
1429 /* Handle any leading odd-sized chunks */
1432 unsigned char *p
= (unsigned char *) ctx
->in
+ t
;
1436 memcpy(p
, buf
, len
);
1440 byteReverse(ctx
->in
, 16);
1441 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1445 /* Process data in 64-byte chunks */
1448 memcpy(ctx
->in
, buf
, 64);
1449 byteReverse(ctx
->in
, 16);
1450 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1455 /* Handle any remaining bytes of data. */
1457 memcpy(ctx
->in
, buf
, len
);
1461 * Final wrapup - pad to 64-byte boundary with the bit pattern
1462 * 1 0* (64-bit count of bits processed, MSB-first)
1464 static void MD5Final(unsigned char digest
[16], struct MD5Context
*ctx
)
1469 /* Compute number of bytes mod 64 */
1470 count
= (ctx
->bits
[0] >> 3) & 0x3F;
1472 /* Set the first char of padding to 0x80. This is safe since there is
1473 always at least one byte free */
1474 p
= ctx
->in
+ count
;
1477 /* Bytes of padding needed to make 64 bytes */
1478 count
= 64 - 1 - count
;
1480 /* Pad out to 56 mod 64 */
1482 /* Two lots of padding: Pad the first block to 64 bytes */
1483 memset(p
, 0, count
);
1484 byteReverse(ctx
->in
, 16);
1485 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1487 /* Now fill the next block with 56 bytes */
1488 memset(ctx
->in
, 0, 56);
1490 /* Pad block to 56 bytes */
1491 memset(p
, 0, count
- 8);
1493 byteReverse(ctx
->in
, 14);
1495 /* Append length in bits and transform */
1496 ((FcChar32
*) ctx
->in
)[14] = ctx
->bits
[0];
1497 ((FcChar32
*) ctx
->in
)[15] = ctx
->bits
[1];
1499 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1500 byteReverse((unsigned char *) ctx
->buf
, 4);
1501 memcpy(digest
, ctx
->buf
, 16);
1502 memset(ctx
, 0, sizeof(ctx
)); /* In case it's sensitive */
1506 /* The four core functions - F1 is optimized somewhat */
1508 /* #define F1(x, y, z) (x & y | ~x & z) */
1509 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1510 #define F2(x, y, z) F1(z, x, y)
1511 #define F3(x, y, z) (x ^ y ^ z)
1512 #define F4(x, y, z) (y ^ (x | ~z))
1514 /* This is the central step in the MD5 algorithm. */
1515 #define MD5STEP(f, w, x, y, z, data, s) \
1516 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1519 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1520 * reflect the addition of 16 longwords of new data. MD5Update blocks
1521 * the data and converts bytes into longwords for this routine.
1523 static void MD5Transform(FcChar32 buf
[4], FcChar32 in
[16])
1525 register FcChar32 a
, b
, c
, d
;
1532 MD5STEP(F1
, a
, b
, c
, d
, in
[0] + 0xd76aa478, 7);
1533 MD5STEP(F1
, d
, a
, b
, c
, in
[1] + 0xe8c7b756, 12);
1534 MD5STEP(F1
, c
, d
, a
, b
, in
[2] + 0x242070db, 17);
1535 MD5STEP(F1
, b
, c
, d
, a
, in
[3] + 0xc1bdceee, 22);
1536 MD5STEP(F1
, a
, b
, c
, d
, in
[4] + 0xf57c0faf, 7);
1537 MD5STEP(F1
, d
, a
, b
, c
, in
[5] + 0x4787c62a, 12);
1538 MD5STEP(F1
, c
, d
, a
, b
, in
[6] + 0xa8304613, 17);
1539 MD5STEP(F1
, b
, c
, d
, a
, in
[7] + 0xfd469501, 22);
1540 MD5STEP(F1
, a
, b
, c
, d
, in
[8] + 0x698098d8, 7);
1541 MD5STEP(F1
, d
, a
, b
, c
, in
[9] + 0x8b44f7af, 12);
1542 MD5STEP(F1
, c
, d
, a
, b
, in
[10] + 0xffff5bb1, 17);
1543 MD5STEP(F1
, b
, c
, d
, a
, in
[11] + 0x895cd7be, 22);
1544 MD5STEP(F1
, a
, b
, c
, d
, in
[12] + 0x6b901122, 7);
1545 MD5STEP(F1
, d
, a
, b
, c
, in
[13] + 0xfd987193, 12);
1546 MD5STEP(F1
, c
, d
, a
, b
, in
[14] + 0xa679438e, 17);
1547 MD5STEP(F1
, b
, c
, d
, a
, in
[15] + 0x49b40821, 22);
1549 MD5STEP(F2
, a
, b
, c
, d
, in
[1] + 0xf61e2562, 5);
1550 MD5STEP(F2
, d
, a
, b
, c
, in
[6] + 0xc040b340, 9);
1551 MD5STEP(F2
, c
, d
, a
, b
, in
[11] + 0x265e5a51, 14);
1552 MD5STEP(F2
, b
, c
, d
, a
, in
[0] + 0xe9b6c7aa, 20);
1553 MD5STEP(F2
, a
, b
, c
, d
, in
[5] + 0xd62f105d, 5);
1554 MD5STEP(F2
, d
, a
, b
, c
, in
[10] + 0x02441453, 9);
1555 MD5STEP(F2
, c
, d
, a
, b
, in
[15] + 0xd8a1e681, 14);
1556 MD5STEP(F2
, b
, c
, d
, a
, in
[4] + 0xe7d3fbc8, 20);
1557 MD5STEP(F2
, a
, b
, c
, d
, in
[9] + 0x21e1cde6, 5);
1558 MD5STEP(F2
, d
, a
, b
, c
, in
[14] + 0xc33707d6, 9);
1559 MD5STEP(F2
, c
, d
, a
, b
, in
[3] + 0xf4d50d87, 14);
1560 MD5STEP(F2
, b
, c
, d
, a
, in
[8] + 0x455a14ed, 20);
1561 MD5STEP(F2
, a
, b
, c
, d
, in
[13] + 0xa9e3e905, 5);
1562 MD5STEP(F2
, d
, a
, b
, c
, in
[2] + 0xfcefa3f8, 9);
1563 MD5STEP(F2
, c
, d
, a
, b
, in
[7] + 0x676f02d9, 14);
1564 MD5STEP(F2
, b
, c
, d
, a
, in
[12] + 0x8d2a4c8a, 20);
1566 MD5STEP(F3
, a
, b
, c
, d
, in
[5] + 0xfffa3942, 4);
1567 MD5STEP(F3
, d
, a
, b
, c
, in
[8] + 0x8771f681, 11);
1568 MD5STEP(F3
, c
, d
, a
, b
, in
[11] + 0x6d9d6122, 16);
1569 MD5STEP(F3
, b
, c
, d
, a
, in
[14] + 0xfde5380c, 23);
1570 MD5STEP(F3
, a
, b
, c
, d
, in
[1] + 0xa4beea44, 4);
1571 MD5STEP(F3
, d
, a
, b
, c
, in
[4] + 0x4bdecfa9, 11);
1572 MD5STEP(F3
, c
, d
, a
, b
, in
[7] + 0xf6bb4b60, 16);
1573 MD5STEP(F3
, b
, c
, d
, a
, in
[10] + 0xbebfbc70, 23);
1574 MD5STEP(F3
, a
, b
, c
, d
, in
[13] + 0x289b7ec6, 4);
1575 MD5STEP(F3
, d
, a
, b
, c
, in
[0] + 0xeaa127fa, 11);
1576 MD5STEP(F3
, c
, d
, a
, b
, in
[3] + 0xd4ef3085, 16);
1577 MD5STEP(F3
, b
, c
, d
, a
, in
[6] + 0x04881d05, 23);
1578 MD5STEP(F3
, a
, b
, c
, d
, in
[9] + 0xd9d4d039, 4);
1579 MD5STEP(F3
, d
, a
, b
, c
, in
[12] + 0xe6db99e5, 11);
1580 MD5STEP(F3
, c
, d
, a
, b
, in
[15] + 0x1fa27cf8, 16);
1581 MD5STEP(F3
, b
, c
, d
, a
, in
[2] + 0xc4ac5665, 23);
1583 MD5STEP(F4
, a
, b
, c
, d
, in
[0] + 0xf4292244, 6);
1584 MD5STEP(F4
, d
, a
, b
, c
, in
[7] + 0x432aff97, 10);
1585 MD5STEP(F4
, c
, d
, a
, b
, in
[14] + 0xab9423a7, 15);
1586 MD5STEP(F4
, b
, c
, d
, a
, in
[5] + 0xfc93a039, 21);
1587 MD5STEP(F4
, a
, b
, c
, d
, in
[12] + 0x655b59c3, 6);
1588 MD5STEP(F4
, d
, a
, b
, c
, in
[3] + 0x8f0ccc92, 10);
1589 MD5STEP(F4
, c
, d
, a
, b
, in
[10] + 0xffeff47d, 15);
1590 MD5STEP(F4
, b
, c
, d
, a
, in
[1] + 0x85845dd1, 21);
1591 MD5STEP(F4
, a
, b
, c
, d
, in
[8] + 0x6fa87e4f, 6);
1592 MD5STEP(F4
, d
, a
, b
, c
, in
[15] + 0xfe2ce6e0, 10);
1593 MD5STEP(F4
, c
, d
, a
, b
, in
[6] + 0xa3014314, 15);
1594 MD5STEP(F4
, b
, c
, d
, a
, in
[13] + 0x4e0811a1, 21);
1595 MD5STEP(F4
, a
, b
, c
, d
, in
[4] + 0xf7537e82, 6);
1596 MD5STEP(F4
, d
, a
, b
, c
, in
[11] + 0xbd3af235, 10);
1597 MD5STEP(F4
, c
, d
, a
, b
, in
[2] + 0x2ad7d2bb, 15);
1598 MD5STEP(F4
, b
, c
, d
, a
, in
[9] + 0xeb86d391, 21);