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 if (!(dir
= (char *)FcConfigNormalizeFontDir (config
, (FcChar8
*)dir
)))
308 return FcFalse
; /* non-existing directory */
310 for (d
= cache
->dirs
; d
; d
= d
->next
)
312 if (strncmp (d
->name
, dir
, strlen(dir
)) == 0)
314 lseek (cache
->fd
, d
->offset
, SEEK_SET
);
315 if (!FcDirCacheConsume (cache
->fd
, dir
, set
, config
))
317 if (strcmp (d
->name
, dir
) == 0)
326 FcGlobalCacheUpdate (FcGlobalCache
*cache
,
331 FcGlobalCacheDir
* d
;
333 name
= (char *)FcConfigNormalizeFontDir (config
, (FcChar8
*)name
);
334 for (d
= cache
->dirs
; d
; d
= d
->next
)
336 if (strcmp(d
->name
, name
) == 0)
342 d
= malloc (sizeof (FcGlobalCacheDir
));
345 d
->next
= cache
->dirs
;
349 cache
->updated
= FcTrue
;
351 d
->name
= (char *)FcStrCopy ((FcChar8
*)name
);
352 d
->ent
= FcDirCacheProduce (set
, &d
->metadata
);
358 FcGlobalCacheSave (FcGlobalCache
*cache
,
359 const FcChar8
*cache_file
,
363 FcGlobalCacheDir
*dir
;
365 off_t current_arch_start
= 0, truncate_to
;
366 char * current_arch_machine_name
, * header
;
371 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
372 /* Set-UID programs can't safely update the cache */
373 if (getuid () != geteuid ())
377 atomic
= FcAtomicCreate (cache_file
);
381 if (!FcAtomicLock (atomic
))
383 fd
= open ((char *) FcAtomicNewFile(atomic
), O_RDWR
| O_CREAT
,
387 FcCacheWriteString (fd
, FC_GLOBAL_MAGIC_COOKIE
);
389 fd_orig
= open ((char *) FcAtomicOrigFile(atomic
), O_RDONLY
);
391 current_arch_machine_name
= FcCacheMachineSignature ();
393 current_arch_start
= 0;
395 current_arch_start
= FcCacheSkipToArch (fd_orig
,
396 current_arch_machine_name
);
398 if (current_arch_start
< 0)
400 off_t i
= lseek(fd_orig
, 0, SEEK_END
);
401 if (i
< strlen (FC_GLOBAL_MAGIC_COOKIE
)+1)
402 i
= strlen (FC_GLOBAL_MAGIC_COOKIE
)+1;
403 current_arch_start
= FcCacheNextOffset (i
);
406 if (!FcCacheCopyOld(fd
, fd_orig
, current_arch_start
))
412 current_arch_start
= lseek(fd
, 0, SEEK_CUR
);
413 if (ftruncate (fd
, current_arch_start
) == -1)
416 header
= malloc (10 + strlen (current_arch_machine_name
));
420 truncate_to
= current_arch_start
+ strlen(current_arch_machine_name
) + 11;
421 for (dir
= cache
->dirs
; dir
; dir
= dir
->next
)
423 truncate_to
+= strlen(dir
->name
) + 1;
424 truncate_to
+= sizeof (FcCache
);
425 truncate_to
= FcCacheNextOffset (truncate_to
);
426 truncate_to
+= dir
->metadata
.count
;
428 truncate_to
-= current_arch_start
;
430 sprintf (header
, "%8x ", (int)truncate_to
);
431 strcat (header
, current_arch_machine_name
);
432 if (!FcCacheWriteString (fd
, header
))
435 for (dir
= cache
->dirs
; dir
; dir
= dir
->next
)
439 const char * d
= (const char *)FcConfigNormalizeFontDir (config
, (const FcChar8
*)dir
->name
);
441 FcCacheWriteString (fd
, d
);
442 write (fd
, &dir
->metadata
, sizeof(FcCache
));
443 lseek (fd
, FcCacheNextOffset (lseek(fd
, 0, SEEK_CUR
)), SEEK_SET
);
444 write (fd
, dir
->ent
, dir
->metadata
.count
);
448 FcCacheWriteString (fd
, "");
450 if (close (fd
) == -1)
453 if (!FcAtomicReplaceOrig (atomic
))
456 FcAtomicUnlock (atomic
);
457 FcAtomicDestroy (atomic
);
459 cache
->updated
= FcFalse
;
470 FcAtomicDeleteNew (atomic
);
472 FcAtomicUnlock (atomic
);
474 FcAtomicDestroy (atomic
);
479 * Find the next presumably-mmapable offset after the supplied file
483 FcCacheNextOffset(off_t w
)
485 static long pagesize
= -1;
487 pagesize
= sysconf(_SC_PAGESIZE
);
488 if (w
% pagesize
== 0)
491 return ((w
/ pagesize
)+1)*pagesize
;
494 /* return the address of the segment for the provided arch,
495 * or -1 if arch not found */
497 FcCacheSkipToArch (int fd
, const char * arch
)
499 char candidate_arch_machine_name_count
[MACHINE_SIGNATURE_SIZE
+ 9];
500 char * candidate_arch
;
501 off_t current_arch_start
= 0;
503 lseek (fd
, 0, SEEK_SET
);
504 FcCacheSkipString (fd
);
505 current_arch_start
= lseek (fd
, 0, SEEK_CUR
);
507 /* skip arches that are not the current arch */
512 if (lseek (fd
, current_arch_start
, SEEK_SET
) != current_arch_start
)
515 if (FcCacheReadString (fd
, candidate_arch_machine_name_count
,
516 sizeof (candidate_arch_machine_name_count
)) == 0)
518 if (!strlen(candidate_arch_machine_name_count
))
520 bs
= strtol(candidate_arch_machine_name_count
, &candidate_arch
, 16);
522 // count = 0 should probably be distinguished from the !bs condition
523 if (!bs
|| bs
< strlen (candidate_arch_machine_name_count
))
526 candidate_arch
++; /* skip leading space */
528 if (strcmp (candidate_arch
, arch
)==0)
529 return current_arch_start
;
530 current_arch_start
+= bs
;
536 /* Cuts out the segment at the file pointer (moves everything else
537 * down to cover it), and leaves the file pointer at the end of the
540 FcCacheCopyOld (int fd
, int fd_orig
, off_t start
)
542 char * buf
= malloc (8192);
543 char candidate_arch_machine_name
[MACHINE_SIGNATURE_SIZE
+ 9];
545 int c
, bytes_skipped
;
552 lseek (fd
, 0, SEEK_SET
); lseek (fd_orig
, 0, SEEK_SET
);
559 if ((c
= read (fd_orig
, buf
, b
)) <= 0)
561 if (write (fd
, buf
, c
) < 0)
568 lseek (fd
, start
, SEEK_SET
);
569 if (FcCacheReadString (fd
, candidate_arch_machine_name
,
570 sizeof (candidate_arch_machine_name
)) == 0)
572 if (!strlen(candidate_arch_machine_name
))
575 bs
= strtol(candidate_arch_machine_name
, 0, 16);
582 lseek (fd
, start
+bs
+bytes_skipped
, SEEK_SET
);
583 if ((c
= read (fd
, buf
, 8192)) <= 0)
585 lseek (fd
, start
+bytes_skipped
, SEEK_SET
);
586 if (write (fd
, buf
, c
) < 0)
591 lseek (fd
, start
+bytes_skipped
, SEEK_SET
);
602 /* Does not check that the cache has the appropriate arch section. */
603 /* Also, this can be fooled if the original location has a stale
604 * cache, and the hashed location has an up-to-date cache. Oh well,
605 * sucks to be you in that case! */
607 FcDirCacheValid (const FcChar8
*dir
)
609 struct stat file_stat
, dir_stat
;
612 dir
= FcConfigNormalizeFontDir (FcConfigGetCurrent(), dir
);
616 if (stat ((char *) dir
, &dir_stat
) < 0)
619 fd
= FcDirCacheOpen (dir
);
623 if (fstat (fd
, &file_stat
) < 0)
629 * If the directory has been modified more recently than
630 * the cache file, the cache is not valid
632 if (dir_stat
.st_mtime
> file_stat
.st_mtime
)
642 /* Assumes that the cache file in 'dir' exists.
643 * Checks that the cache has the appropriate arch section. */
645 FcDirCacheHasCurrentArch (const FcChar8
*dir
)
648 off_t current_arch_start
;
649 char *current_arch_machine_name
;
651 dir
= FcConfigNormalizeFontDir (FcConfigGetCurrent(), dir
);
655 fd
= FcDirCacheOpen (dir
);
659 current_arch_machine_name
= FcCacheMachineSignature();
660 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
663 if (current_arch_start
< 0)
673 FcDirCacheUnlink (const FcChar8
*dir
, FcConfig
*config
)
676 char *cache_hashed
= 0;
678 struct stat cache_stat
;
679 char name_buf
[FC_MAX_FILE_LEN
];
681 dir
= FcConfigNormalizeFontDir (config
, dir
);
682 cache_file
= (char *)FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
686 /* First remove normal cache file. */
687 if (stat ((char *) cache_file
, &cache_stat
) == 0 &&
688 unlink ((char *)cache_file
) != 0)
691 /* Next remove any applicable hashed files. */
692 fd
= -1; collisions
= 0;
696 FcStrFree ((FcChar8
*)cache_hashed
);
698 cache_hashed
= FcDirCacheHashName (cache_file
, collisions
++);
704 fd
= open(cache_hashed
, O_RDONLY
);
707 FcStrFree ((FcChar8
*)cache_file
);
711 FcCacheReadString (fd
, name_buf
, sizeof (name_buf
));
712 if (!strlen(name_buf
))
714 FcStrFree ((FcChar8
*)cache_hashed
);
717 } while (strcmp (name_buf
, cache_file
) != 0);
719 FcStrFree ((FcChar8
*)cache_file
);
722 if (stat ((char *) cache_hashed
, &cache_stat
) == 0 &&
723 unlink ((char *)cache_hashed
) != 0)
725 FcStrFree ((FcChar8
*)cache_hashed
);
729 FcStrFree ((FcChar8
*)cache_hashed
);
733 FcStrFree ((FcChar8
*)cache_file
);
738 FcCacheReadDirs (FcConfig
* config
, FcGlobalCache
* cache
,
739 FcStrList
*list
, FcFontSet
* set
)
743 FcChar8
*file
, *base
;
749 * Read in the results from 'list'.
751 while ((dir
= FcStrListNext (list
)))
753 if (!FcConfigAcceptFilename (config
, dir
))
757 file
= (FcChar8
*) malloc (strlen ((char *) dir
) + 1 + FC_MAX_FILE_LEN
+ 1);
761 strcpy ((char *) file
, (char *) dir
);
762 strcat ((char *) file
, "/");
763 base
= file
+ strlen ((char *) file
);
765 subdirs
= FcStrSetCreate ();
768 fprintf (stderr
, "Can't create directory set\n");
774 if (access ((char *) dir
, X_OK
) < 0)
782 fprintf (stderr
, "\"%s\": ", dir
);
786 FcStrSetDestroy (subdirs
);
790 if (stat ((char *) dir
, &statb
) == -1)
792 fprintf (stderr
, "\"%s\": ", dir
);
794 FcStrSetDestroy (subdirs
);
799 if (!S_ISDIR (statb
.st_mode
))
801 fprintf (stderr
, "\"%s\": not a directory, skipping\n", dir
);
802 FcStrSetDestroy (subdirs
);
806 if (!FcDirCacheValid (dir
) || !FcDirCacheRead (set
, subdirs
, dir
, config
))
808 if (FcDebug () & FC_DBG_FONTSET
)
809 printf ("cache scan dir %s\n", dir
);
811 FcDirScanConfig (set
, subdirs
, cache
,
812 config
->blanks
, dir
, FcFalse
, config
);
814 sublist
= FcStrListCreate (subdirs
);
815 FcStrSetDestroy (subdirs
);
818 fprintf (stderr
, "Can't create subdir list in \"%s\"\n", dir
);
823 ret
+= FcCacheReadDirs (config
, cache
, sublist
, set
);
826 FcStrListDone (list
);
831 FcCacheRead (FcConfig
*config
, FcGlobalCache
* cache
)
833 FcFontSet
* s
= FcFontSetCreate();
837 if (FcCacheReadDirs (config
, cache
, FcConfigGetConfigDirs (config
), s
))
843 FcFontSetDestroy (s
);
847 static const char bin2hex
[] = { '0', '1', '2', '3',
850 'c', 'd', 'e', 'f' };
853 FcDirCacheHashName (char * cache_file
, int collisions
)
855 unsigned char hash
[16], hex_hash
[33];
857 unsigned char uscore
= '_';
860 struct MD5Context ctx
;
863 MD5Update (&ctx
, (unsigned char *)cache_file
, strlen (cache_file
));
865 for (i
= 0; i
< collisions
; i
++)
866 MD5Update (&ctx
, &uscore
, 1);
868 MD5Final (hash
, &ctx
);
870 for (cnt
= 0; cnt
< 16; ++cnt
)
872 hex_hash
[2*cnt
] = bin2hex
[hash
[cnt
] >> 4];
873 hex_hash
[2*cnt
+1] = bin2hex
[hash
[cnt
] & 0xf];
877 tmp
= FcStrPlus ((FcChar8
*)hex_hash
, (FcChar8
*)FC_CACHE_SUFFIX
);
881 cache_hashed
= (char *)FcStrPlus ((FcChar8
*)PKGCACHEDIR
"/", tmp
);
887 /* Opens the hashed name for cache_file.
888 * This would fail in the unlikely event of a collision and subsequent
889 * removal of the file which originally caused the collision. */
891 FcDirCacheOpen (const FcChar8
*dir
)
894 int fd
= -1, collisions
= 0;
895 char *cache_file
, *cache_hashed
;
896 char name_buf
[FC_MAX_FILE_LEN
];
897 struct stat dir_stat
;
899 cache_file
= (char *)FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
903 fd
= open(cache_file
, O_RDONLY
);
907 if (stat ((char *)dir
, &dir_stat
) == -1)
914 FcChar8
* name_buf_dir
;
916 cache_hashed
= FcDirCacheHashName (cache_file
, collisions
++);
919 FcStrFree ((FcChar8
*)cache_file
);
925 fd
= open(cache_hashed
, O_RDONLY
);
926 FcStrFree ((FcChar8
*)cache_hashed
);
930 FcStrFree ((FcChar8
*)cache_file
);
933 FcCacheReadString (fd
, name_buf
, sizeof (name_buf
));
934 if (!strlen(name_buf
))
937 name_buf_dir
= FcStrDirname ((FcChar8
*)name_buf
);
938 if (stat ((char *)name_buf_dir
, &c
) == -1)
940 FcStrFree (name_buf_dir
);
943 FcStrFree (name_buf_dir
);
944 found
= (c
.st_ino
== dir_stat
.st_ino
) && (c
.st_dev
== dir_stat
.st_dev
);
946 FcStrFree ((FcChar8
*)cache_file
);
950 FcStrFree ((FcChar8
*)cache_file
);
955 /* read serialized state from the cache file */
957 FcDirCacheRead (FcFontSet
* set
, FcStrSet
* dirs
, const FcChar8
*dir
, FcConfig
*config
)
960 char *current_arch_machine_name
;
961 char candidate_arch_machine_name
[9+MACHINE_SIGNATURE_SIZE
];
962 off_t current_arch_start
= 0;
963 char subdirName
[FC_MAX_FILE_LEN
+ 1 + 12 + 1];
965 fd
= FcDirCacheOpen (dir
);
969 current_arch_machine_name
= FcCacheMachineSignature();
970 current_arch_start
= FcCacheSkipToArch(fd
,
971 current_arch_machine_name
);
972 if (current_arch_start
< 0)
975 lseek (fd
, current_arch_start
, SEEK_SET
);
976 if (FcCacheReadString (fd
, candidate_arch_machine_name
,
977 sizeof (candidate_arch_machine_name
)) == 0)
980 while (strlen(FcCacheReadString (fd
, subdirName
, sizeof (subdirName
))) > 0)
981 FcStrSetAdd (dirs
, (FcChar8
*)subdirName
);
983 if (!FcDirCacheConsume (fd
, (const char *)dir
, set
, config
))
996 FcDirCacheConsume (int fd
, const char * dir
, FcFontSet
*set
, FcConfig
*config
)
999 void * current_dir_block
;
1002 read(fd
, &metadata
, sizeof(FcCache
));
1003 if (metadata
.magic
!= FC_CACHE_MAGIC
)
1006 if (!metadata
.count
)
1008 pos
= FcCacheNextOffset (lseek(fd
, 0, SEEK_CUR
));
1009 lseek (fd
, pos
, SEEK_SET
);
1011 FcConfigAddFontDir (config
, (FcChar8
*)dir
);
1015 pos
= FcCacheNextOffset (lseek(fd
, 0, SEEK_CUR
));
1016 current_dir_block
= mmap (0, metadata
.count
,
1017 PROT_READ
, MAP_SHARED
, fd
, pos
);
1018 lseek (fd
, pos
+metadata
.count
, SEEK_SET
);
1019 if (current_dir_block
== MAP_FAILED
)
1022 FcCacheAddBankDir (metadata
.bank
, dir
);
1024 FcConfigAddFontDir (config
, (FcChar8
*)dir
);
1026 if (!FcFontSetUnserialize (&metadata
, set
, current_dir_block
))
1033 FcDirCacheProduce (FcFontSet
*set
, FcCache
*metadata
)
1035 void * current_dir_block
, * final_dir_block
;
1036 static unsigned int rand_state
= 0;
1037 int bank
, needed_bytes_no_align
;
1040 rand_state
= time(0L);
1041 bank
= rand_r(&rand_state
);
1043 while (FcCacheHaveBank(bank
))
1044 bank
= rand_r(&rand_state
);
1046 memset (metadata
, 0, sizeof(FcCache
));
1048 needed_bytes_no_align
= FcFontSetNeededBytes (set
);
1049 metadata
->count
= needed_bytes_no_align
+
1050 FcFontSetNeededBytesAlign ();
1051 metadata
->magic
= FC_CACHE_MAGIC
;
1052 metadata
->bank
= bank
;
1054 if (!needed_bytes_no_align
) /* not a failure, no fonts to write */
1056 /* no, you don't really need to write any bytes at all. */
1057 metadata
->count
= 0;
1061 current_dir_block
= malloc (metadata
->count
);
1062 if (!current_dir_block
)
1065 memset (current_dir_block
, 0, metadata
->count
);
1066 final_dir_block
= FcFontSetDistributeBytes (metadata
, current_dir_block
);
1068 if ((void *)((char *)current_dir_block
+metadata
->count
) < final_dir_block
)
1071 if (!FcFontSetSerialize (bank
, set
))
1074 return current_dir_block
;
1077 free (current_dir_block
);
1081 /* write serialized state to the cache file */
1083 FcDirCacheWrite (FcFontSet
*set
, FcStrSet
*dirs
, const FcChar8
*dir
)
1087 int fd
, fd_orig
, i
, dirs_count
;
1090 off_t current_arch_start
= 0, truncate_to
;
1091 char name_buf
[FC_MAX_FILE_LEN
];
1094 char *current_arch_machine_name
, * header
;
1095 void *current_dir_block
= 0;
1097 dir
= FcConfigNormalizeFontDir (FcConfigGetCurrent(), dir
);
1101 cache_file
= (char *)FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
1105 /* Ensure that we're not trampling a cache for some other dir. */
1106 /* This is slightly different from FcDirCacheOpen, since it
1107 * needs the filename, not the file descriptor. */
1108 fd
= -1; collisions
= 0;
1111 cache_hashed
= FcDirCacheHashName (cache_file
, collisions
++);
1117 fd
= open(cache_hashed
, O_RDONLY
);
1120 FcCacheReadString (fd
, name_buf
, sizeof (name_buf
));
1123 if (!strlen(name_buf
))
1126 if (strcmp (name_buf
, cache_file
) != 0)
1130 current_dir_block
= FcDirCacheProduce (set
, &metadata
);
1132 if (metadata
.count
&& !current_dir_block
)
1135 if (FcDebug () & FC_DBG_CACHE
)
1136 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file
);
1138 atomic
= FcAtomicCreate ((FcChar8
*)cache_hashed
);
1142 if (!FcAtomicLock (atomic
))
1144 /* Now try rewriting the original version of the file. */
1145 FcAtomicDestroy (atomic
);
1147 atomic
= FcAtomicCreate ((FcChar8
*)cache_file
);
1148 fd_orig
= open (cache_file
, O_RDONLY
);
1150 fd_orig
= open((char *)FcAtomicOrigFile (atomic
), O_RDONLY
);
1152 fd
= open((char *)FcAtomicNewFile (atomic
), O_RDWR
| O_CREAT
, 0666);
1157 /* In all cases, try opening the real location of the cache file first. */
1158 /* (even if that's not atomic.) */
1159 fd_orig
= open (cache_file
, O_RDONLY
);
1161 fd_orig
= open((char *)FcAtomicOrigFile (atomic
), O_RDONLY
);
1163 fd
= open((char *)FcAtomicNewFile (atomic
), O_RDWR
| O_CREAT
, 0666);
1167 FcCacheWriteString (fd
, cache_file
);
1169 current_arch_machine_name
= FcCacheMachineSignature ();
1170 current_arch_start
= 0;
1173 current_arch_start
=
1174 FcCacheSkipToArch(fd_orig
, current_arch_machine_name
);
1176 if (current_arch_start
< 0)
1178 off_t i
= lseek(fd_orig
, 0, SEEK_END
);
1179 if (i
< strlen (FC_GLOBAL_MAGIC_COOKIE
)+1)
1180 i
= strlen (FC_GLOBAL_MAGIC_COOKIE
)+1;
1181 current_arch_start
= FcCacheNextOffset (i
);
1184 if (fd_orig
!= -1 && !FcCacheCopyOld(fd
, fd_orig
, current_arch_start
))
1190 current_arch_start
= lseek(fd
, 0, SEEK_CUR
);
1191 if (ftruncate (fd
, current_arch_start
) == -1)
1194 /* allocate space for subdir names in this block */
1196 for (i
= 0; i
< dirs
->size
; i
++)
1197 dirs_count
+= strlen((char *)dirs
->strs
[i
]) + 1;
1200 /* now write the address of the next offset */
1201 truncate_to
= FcCacheNextOffset (FcCacheNextOffset (current_arch_start
+ sizeof (FcCache
) + dirs_count
) + metadata
.count
) - current_arch_start
;
1202 header
= malloc (10 + strlen (current_arch_machine_name
));
1205 sprintf (header
, "%8x ", (int)truncate_to
);
1206 strcat (header
, current_arch_machine_name
);
1207 if (!FcCacheWriteString (fd
, header
))
1210 for (i
= 0; i
< dirs
->size
; i
++)
1211 FcCacheWriteString (fd
, (char *)dirs
->strs
[i
]);
1212 FcCacheWriteString (fd
, "");
1214 write (fd
, &metadata
, sizeof(FcCache
));
1217 lseek (fd
, FcCacheNextOffset (lseek(fd
, 0, SEEK_END
)), SEEK_SET
);
1218 write (fd
, current_dir_block
, metadata
.count
);
1219 free (current_dir_block
);
1222 /* this actually serves to pad out the cache file, if needed */
1223 if (ftruncate (fd
, current_arch_start
+ truncate_to
) == -1)
1228 if (!FcAtomicReplaceOrig(atomic
))
1230 FcStrFree ((FcChar8
*)cache_hashed
);
1231 FcStrFree ((FcChar8
*)cache_file
);
1232 FcAtomicUnlock (atomic
);
1233 FcAtomicDestroy (atomic
);
1241 FcAtomicUnlock (atomic
);
1243 FcAtomicDestroy (atomic
);
1245 FcStrFree ((FcChar8
*)cache_hashed
);
1247 unlink ((char *)cache_file
);
1248 FcStrFree ((FcChar8
*)cache_file
);
1249 if (current_dir_block
)
1250 free (current_dir_block
);
1256 FcCacheMachineSignature ()
1258 static char buf
[MACHINE_SIGNATURE_SIZE
];
1259 int32_t magic
= ENDIAN_TEST
;
1260 char * m
= (char *)&magic
;
1262 sprintf (buf
, "%2x%2x%2x%2x "
1263 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
1264 "%4x %4x %4x %4x %4x %4x %4x %4x\n",
1265 m
[0], m
[1], m
[2], m
[3],
1266 (unsigned int)sizeof (char),
1267 (unsigned int)sizeof (char *),
1268 (unsigned int)sizeof (int),
1269 (unsigned int)sizeof (FcPattern
),
1270 (unsigned int)sizeof (FcPatternEltPtr
),
1271 (unsigned int)sizeof (struct _FcPatternElt
*),
1272 (unsigned int)sizeof (FcPatternElt
),
1273 (unsigned int)sizeof (FcObjectPtr
),
1274 (unsigned int)sizeof (FcValueListPtr
),
1275 (unsigned int)sizeof (FcValue
),
1276 (unsigned int)sizeof (FcValueBinding
),
1277 (unsigned int)sizeof (struct _FcValueList
*),
1278 (unsigned int)sizeof (FcCharSet
),
1279 (unsigned int)sizeof (FcCharLeaf
**),
1280 (unsigned int)sizeof (FcChar16
*),
1281 (unsigned int)sizeof (FcChar16
),
1282 (unsigned int)sizeof (FcCharLeaf
),
1283 (unsigned int)sizeof (FcChar32
),
1284 (unsigned int)sizeof (FcCache
),
1285 (unsigned int)sysconf(_SC_PAGESIZE
));
1290 static int banks_ptr
= 0, banks_alloc
= 0;
1291 int * _fcBankId
= 0, * _fcBankIdx
= 0;
1292 static const char ** bankDirs
= 0;
1295 FcCacheHaveBank (int bank
)
1299 if (bank
< FC_BANK_FIRST
)
1302 for (i
= 0; i
< banks_ptr
; i
++)
1303 if (_fcBankId
[i
] == bank
)
1310 FcCacheBankToIndexMTF (int bank
)
1314 for (i
= 0; i
< banks_ptr
; i
++)
1315 if (_fcBankId
[_fcBankIdx
[i
]] == bank
)
1317 int t
= _fcBankIdx
[i
];
1319 for (j
= i
; j
> 0; j
--)
1320 _fcBankIdx
[j
] = _fcBankIdx
[j
-1];
1325 if (banks_ptr
>= banks_alloc
)
1330 b
= realloc (_fcBankId
, (banks_alloc
+ 4) * sizeof(int));
1335 bidx
= realloc (_fcBankIdx
, (banks_alloc
+ 4) * sizeof(int));
1340 bds
= realloc (bankDirs
, (banks_alloc
+ 4) * sizeof (char *));
1349 _fcBankId
[i
] = bank
;
1355 FcCacheAddBankDir (int bank
, const char * dir
)
1357 int bi
= FcCacheBankToIndexMTF (bank
);
1362 bankDirs
[bi
] = (const char *)FcStrCopy ((FcChar8
*)dir
);
1366 FcCacheFindBankDir (int bank
)
1368 int bi
= FcCacheBankToIndex (bank
);
1369 return bankDirs
[bi
];
1373 * This code implements the MD5 message-digest algorithm.
1374 * The algorithm is due to Ron Rivest. This code was
1375 * written by Colin Plumb in 1993, no copyright is claimed.
1376 * This code is in the public domain; do with it what you wish.
1378 * Equivalent code is available from RSA Data Security, Inc.
1379 * This code has been tested against that, and is equivalent,
1380 * except that you don't need to include two pages of legalese
1383 * To compute the message digest of a chunk of bytes, declare an
1384 * MD5Context structure, pass it to MD5Init, call MD5Update as
1385 * needed on buffers full of bytes, and then call MD5Final, which
1386 * will fill a supplied 16-byte array with the digest.
1390 #define byteReverse(buf, len) /* Nothing */
1393 * Note: this code is harmless on little-endian machines.
1395 void byteReverse(unsigned char *buf
, unsigned longs
)
1399 t
= (FcChar32
) ((unsigned) buf
[3] << 8 | buf
[2]) << 16 |
1400 ((unsigned) buf
[1] << 8 | buf
[0]);
1401 *(FcChar32
*) buf
= t
;
1408 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1409 * initialization constants.
1411 static void MD5Init(struct MD5Context
*ctx
)
1413 ctx
->buf
[0] = 0x67452301;
1414 ctx
->buf
[1] = 0xefcdab89;
1415 ctx
->buf
[2] = 0x98badcfe;
1416 ctx
->buf
[3] = 0x10325476;
1423 * Update context to reflect the concatenation of another buffer full
1426 static void MD5Update(struct MD5Context
*ctx
, unsigned char *buf
, unsigned len
)
1430 /* Update bitcount */
1433 if ((ctx
->bits
[0] = t
+ ((FcChar32
) len
<< 3)) < t
)
1434 ctx
->bits
[1]++; /* Carry from low to high */
1435 ctx
->bits
[1] += len
>> 29;
1437 t
= (t
>> 3) & 0x3f; /* Bytes already in shsInfo->data */
1439 /* Handle any leading odd-sized chunks */
1442 unsigned char *p
= (unsigned char *) ctx
->in
+ t
;
1446 memcpy(p
, buf
, len
);
1450 byteReverse(ctx
->in
, 16);
1451 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1455 /* Process data in 64-byte chunks */
1458 memcpy(ctx
->in
, buf
, 64);
1459 byteReverse(ctx
->in
, 16);
1460 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1465 /* Handle any remaining bytes of data. */
1467 memcpy(ctx
->in
, buf
, len
);
1471 * Final wrapup - pad to 64-byte boundary with the bit pattern
1472 * 1 0* (64-bit count of bits processed, MSB-first)
1474 static void MD5Final(unsigned char digest
[16], struct MD5Context
*ctx
)
1479 /* Compute number of bytes mod 64 */
1480 count
= (ctx
->bits
[0] >> 3) & 0x3F;
1482 /* Set the first char of padding to 0x80. This is safe since there is
1483 always at least one byte free */
1484 p
= ctx
->in
+ count
;
1487 /* Bytes of padding needed to make 64 bytes */
1488 count
= 64 - 1 - count
;
1490 /* Pad out to 56 mod 64 */
1492 /* Two lots of padding: Pad the first block to 64 bytes */
1493 memset(p
, 0, count
);
1494 byteReverse(ctx
->in
, 16);
1495 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1497 /* Now fill the next block with 56 bytes */
1498 memset(ctx
->in
, 0, 56);
1500 /* Pad block to 56 bytes */
1501 memset(p
, 0, count
- 8);
1503 byteReverse(ctx
->in
, 14);
1505 /* Append length in bits and transform */
1506 ((FcChar32
*) ctx
->in
)[14] = ctx
->bits
[0];
1507 ((FcChar32
*) ctx
->in
)[15] = ctx
->bits
[1];
1509 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1510 byteReverse((unsigned char *) ctx
->buf
, 4);
1511 memcpy(digest
, ctx
->buf
, 16);
1512 memset(ctx
, 0, sizeof(ctx
)); /* In case it's sensitive */
1516 /* The four core functions - F1 is optimized somewhat */
1518 /* #define F1(x, y, z) (x & y | ~x & z) */
1519 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1520 #define F2(x, y, z) F1(z, x, y)
1521 #define F3(x, y, z) (x ^ y ^ z)
1522 #define F4(x, y, z) (y ^ (x | ~z))
1524 /* This is the central step in the MD5 algorithm. */
1525 #define MD5STEP(f, w, x, y, z, data, s) \
1526 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1529 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1530 * reflect the addition of 16 longwords of new data. MD5Update blocks
1531 * the data and converts bytes into longwords for this routine.
1533 static void MD5Transform(FcChar32 buf
[4], FcChar32 in
[16])
1535 register FcChar32 a
, b
, c
, d
;
1542 MD5STEP(F1
, a
, b
, c
, d
, in
[0] + 0xd76aa478, 7);
1543 MD5STEP(F1
, d
, a
, b
, c
, in
[1] + 0xe8c7b756, 12);
1544 MD5STEP(F1
, c
, d
, a
, b
, in
[2] + 0x242070db, 17);
1545 MD5STEP(F1
, b
, c
, d
, a
, in
[3] + 0xc1bdceee, 22);
1546 MD5STEP(F1
, a
, b
, c
, d
, in
[4] + 0xf57c0faf, 7);
1547 MD5STEP(F1
, d
, a
, b
, c
, in
[5] + 0x4787c62a, 12);
1548 MD5STEP(F1
, c
, d
, a
, b
, in
[6] + 0xa8304613, 17);
1549 MD5STEP(F1
, b
, c
, d
, a
, in
[7] + 0xfd469501, 22);
1550 MD5STEP(F1
, a
, b
, c
, d
, in
[8] + 0x698098d8, 7);
1551 MD5STEP(F1
, d
, a
, b
, c
, in
[9] + 0x8b44f7af, 12);
1552 MD5STEP(F1
, c
, d
, a
, b
, in
[10] + 0xffff5bb1, 17);
1553 MD5STEP(F1
, b
, c
, d
, a
, in
[11] + 0x895cd7be, 22);
1554 MD5STEP(F1
, a
, b
, c
, d
, in
[12] + 0x6b901122, 7);
1555 MD5STEP(F1
, d
, a
, b
, c
, in
[13] + 0xfd987193, 12);
1556 MD5STEP(F1
, c
, d
, a
, b
, in
[14] + 0xa679438e, 17);
1557 MD5STEP(F1
, b
, c
, d
, a
, in
[15] + 0x49b40821, 22);
1559 MD5STEP(F2
, a
, b
, c
, d
, in
[1] + 0xf61e2562, 5);
1560 MD5STEP(F2
, d
, a
, b
, c
, in
[6] + 0xc040b340, 9);
1561 MD5STEP(F2
, c
, d
, a
, b
, in
[11] + 0x265e5a51, 14);
1562 MD5STEP(F2
, b
, c
, d
, a
, in
[0] + 0xe9b6c7aa, 20);
1563 MD5STEP(F2
, a
, b
, c
, d
, in
[5] + 0xd62f105d, 5);
1564 MD5STEP(F2
, d
, a
, b
, c
, in
[10] + 0x02441453, 9);
1565 MD5STEP(F2
, c
, d
, a
, b
, in
[15] + 0xd8a1e681, 14);
1566 MD5STEP(F2
, b
, c
, d
, a
, in
[4] + 0xe7d3fbc8, 20);
1567 MD5STEP(F2
, a
, b
, c
, d
, in
[9] + 0x21e1cde6, 5);
1568 MD5STEP(F2
, d
, a
, b
, c
, in
[14] + 0xc33707d6, 9);
1569 MD5STEP(F2
, c
, d
, a
, b
, in
[3] + 0xf4d50d87, 14);
1570 MD5STEP(F2
, b
, c
, d
, a
, in
[8] + 0x455a14ed, 20);
1571 MD5STEP(F2
, a
, b
, c
, d
, in
[13] + 0xa9e3e905, 5);
1572 MD5STEP(F2
, d
, a
, b
, c
, in
[2] + 0xfcefa3f8, 9);
1573 MD5STEP(F2
, c
, d
, a
, b
, in
[7] + 0x676f02d9, 14);
1574 MD5STEP(F2
, b
, c
, d
, a
, in
[12] + 0x8d2a4c8a, 20);
1576 MD5STEP(F3
, a
, b
, c
, d
, in
[5] + 0xfffa3942, 4);
1577 MD5STEP(F3
, d
, a
, b
, c
, in
[8] + 0x8771f681, 11);
1578 MD5STEP(F3
, c
, d
, a
, b
, in
[11] + 0x6d9d6122, 16);
1579 MD5STEP(F3
, b
, c
, d
, a
, in
[14] + 0xfde5380c, 23);
1580 MD5STEP(F3
, a
, b
, c
, d
, in
[1] + 0xa4beea44, 4);
1581 MD5STEP(F3
, d
, a
, b
, c
, in
[4] + 0x4bdecfa9, 11);
1582 MD5STEP(F3
, c
, d
, a
, b
, in
[7] + 0xf6bb4b60, 16);
1583 MD5STEP(F3
, b
, c
, d
, a
, in
[10] + 0xbebfbc70, 23);
1584 MD5STEP(F3
, a
, b
, c
, d
, in
[13] + 0x289b7ec6, 4);
1585 MD5STEP(F3
, d
, a
, b
, c
, in
[0] + 0xeaa127fa, 11);
1586 MD5STEP(F3
, c
, d
, a
, b
, in
[3] + 0xd4ef3085, 16);
1587 MD5STEP(F3
, b
, c
, d
, a
, in
[6] + 0x04881d05, 23);
1588 MD5STEP(F3
, a
, b
, c
, d
, in
[9] + 0xd9d4d039, 4);
1589 MD5STEP(F3
, d
, a
, b
, c
, in
[12] + 0xe6db99e5, 11);
1590 MD5STEP(F3
, c
, d
, a
, b
, in
[15] + 0x1fa27cf8, 16);
1591 MD5STEP(F3
, b
, c
, d
, a
, in
[2] + 0xc4ac5665, 23);
1593 MD5STEP(F4
, a
, b
, c
, d
, in
[0] + 0xf4292244, 6);
1594 MD5STEP(F4
, d
, a
, b
, c
, in
[7] + 0x432aff97, 10);
1595 MD5STEP(F4
, c
, d
, a
, b
, in
[14] + 0xab9423a7, 15);
1596 MD5STEP(F4
, b
, c
, d
, a
, in
[5] + 0xfc93a039, 21);
1597 MD5STEP(F4
, a
, b
, c
, d
, in
[12] + 0x655b59c3, 6);
1598 MD5STEP(F4
, d
, a
, b
, c
, in
[3] + 0x8f0ccc92, 10);
1599 MD5STEP(F4
, c
, d
, a
, b
, in
[10] + 0xffeff47d, 15);
1600 MD5STEP(F4
, b
, c
, d
, a
, in
[1] + 0x85845dd1, 21);
1601 MD5STEP(F4
, a
, b
, c
, d
, in
[8] + 0x6fa87e4f, 6);
1602 MD5STEP(F4
, d
, a
, b
, c
, in
[15] + 0xfe2ce6e0, 10);
1603 MD5STEP(F4
, c
, d
, a
, b
, in
[6] + 0xa3014314, 15);
1604 MD5STEP(F4
, b
, c
, d
, a
, in
[13] + 0x4e0811a1, 21);
1605 MD5STEP(F4
, a
, b
, c
, d
, in
[4] + 0xf7537e82, 6);
1606 MD5STEP(F4
, d
, a
, b
, c
, in
[11] + 0xbd3af235, 10);
1607 MD5STEP(F4
, c
, d
, a
, b
, in
[2] + 0x2ad7d2bb, 15);
1608 MD5STEP(F4
, b
, c
, d
, a
, in
[9] + 0xeb86d391, 21);