2 * Copyright © 2000 Keith Packard
3 * Copyright © 2005 Patrick Lam
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the author(s) not be used in
10 * advertising or publicity pertaining to distribution of the software without
11 * specific, written prior permission. The authors make no
12 * representations about the suitability of this software for any purpose. It
13 * is provided "as is" without express or implied warranty.
15 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21 * PERFORMANCE OF THIS SOFTWARE.
30 #include <sys/types.h>
32 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
34 # include <sys/mman.h>
36 # define _WIN32_WINNT 0x0500
51 static void MD5Init(struct MD5Context
*ctx
);
52 static void MD5Update(struct MD5Context
*ctx
, const unsigned char *buf
, unsigned len
);
53 static void MD5Final(unsigned char digest
[16], struct MD5Context
*ctx
);
54 static void MD5Transform(FcChar32 buf
[4], FcChar32 in
[16]);
56 #define CACHEBASE_LEN (1 + 32 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX))
63 typedef long long INT64
;
64 #define EPOCH_OFFSET 11644473600ll
66 #define EPOCH_OFFSET 11644473600i64
67 typedef __int64 INT64
;
70 /* Workaround for problems in the stat() in the Microsoft C library:
72 * 1) stat() uses FindFirstFile() to get the file
73 * attributes. Unfortunately this API doesn't return correct values
74 * for modification time of a directory until some time after a file
75 * or subdirectory has been added to the directory. (This causes
76 * run-test.sh to fail, for instance.) GetFileAttributesEx() is
77 * better, it returns the updated timestamp right away.
79 * 2) stat() does some strange things related to backward
80 * compatibility with the local time timestamps on FAT volumes and
81 * daylight saving time. This causes problems after the switches
82 * to/from daylight saving time. See
83 * http://bugzilla.gnome.org/show_bug.cgi?id=154968 , especially
84 * comment #30, and http://www.codeproject.com/datetime/dstbugs.asp .
85 * We don't need any of that, FAT and Win9x are as good as dead. So
86 * just use the UTC timestamps from NTFS, converted to the Unix epoch.
90 FcStat (const FcChar8
*file
, struct stat
*statb
)
92 WIN32_FILE_ATTRIBUTE_DATA wfad
;
93 char full_path_name
[MAX_PATH
];
97 if (!GetFileAttributesEx (file
, GetFileExInfoStandard
, &wfad
))
102 /* Calculate a pseudo inode number as a hash of the full path name.
103 * Call GetLongPathName() to get the spelling of the path name as it
106 rc
= GetFullPathName (file
, sizeof (full_path_name
), full_path_name
, &basename
);
107 if (rc
== 0 || rc
> sizeof (full_path_name
))
110 rc
= GetLongPathName (full_path_name
, full_path_name
, sizeof (full_path_name
));
111 statb
->st_ino
= FcStringHash (full_path_name
);
113 statb
->st_mode
= _S_IREAD
| _S_IWRITE
;
114 statb
->st_mode
|= (statb
->st_mode
>> 3) | (statb
->st_mode
>> 6);
116 if (wfad
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
117 statb
->st_mode
|= _S_IFDIR
;
119 statb
->st_mode
|= _S_IFREG
;
122 statb
->st_uid
= statb
->st_gid
= 0;
125 if (wfad
.nFileSizeHigh
> 0)
127 statb
->st_size
= wfad
.nFileSizeLow
;
129 statb
->st_atime
= (*(INT64
*)&wfad
.ftLastAccessTime
)/10000000 - EPOCH_OFFSET
;
130 statb
->st_mtime
= (*(INT64
*)&wfad
.ftLastWriteTime
)/10000000 - EPOCH_OFFSET
;
131 statb
->st_ctime
= statb
->st_mtime
;
139 FcStat (const FcChar8
*file
, struct stat
*statb
)
142 FcChar8
*fullFile
= FcConfigGetRootPlus (file
);
146 ret
= stat ((char *) file
, statb
);
148 FcStrFree (fullFile
);
155 static const char bin2hex
[] = { '0', '1', '2', '3',
158 'c', 'd', 'e', 'f' };
161 FcDirCacheBasename (const FcChar8
* dir
, FcChar8 cache_base
[CACHEBASE_LEN
])
163 unsigned char hash
[16];
166 struct MD5Context ctx
;
169 MD5Update (&ctx
, (const unsigned char *)dir
, strlen ((const char *) dir
));
171 MD5Final (hash
, &ctx
);
174 hex_hash
= cache_base
+ 1;
175 for (cnt
= 0; cnt
< 16; ++cnt
)
177 hex_hash
[2*cnt
] = bin2hex
[hash
[cnt
] >> 4];
178 hex_hash
[2*cnt
+1] = bin2hex
[hash
[cnt
] & 0xf];
181 strcat ((char *) cache_base
, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX
);
187 FcDirCacheUnlink (const FcChar8
*dir
, FcConfig
*config
)
189 FcChar8
*cache_hashed
= NULL
;
190 FcChar8 cache_base
[CACHEBASE_LEN
];
194 FcDirCacheBasename (dir
, cache_base
);
196 list
= FcStrListCreate (config
->cacheDirs
);
200 while ((cache_dir
= FcStrListNext (list
)))
202 cache_hashed
= FcStrPlus (cache_dir
, cache_base
);
205 (void) unlink ((char *) cache_hashed
);
206 FcStrFree (cache_hashed
);
208 FcStrListDone (list
);
209 /* return FcFalse if something went wrong */
216 FcDirCacheOpenFile (const FcChar8
*cache_file
, struct stat
*file_stat
)
222 if (FcStat (cache_file
, file_stat
) < 0)
225 fullFile
= FcConfigGetRootPlus (cache_file
);
227 cache_file
= fullFile
;
228 fd
= open ((char *) cache_file
, O_RDONLY
| O_BINARY
);
230 FcStrFree (fullFile
);
234 if (fstat (fd
, file_stat
) < 0)
244 * Look for a cache file for the specified dir. Attempt
245 * to use each one we find, stopping when the callback
249 FcDirCacheProcess (FcConfig
*config
, const FcChar8
*dir
,
250 FcBool (*callback
) (int fd
, struct stat
*fd_stat
,
251 struct stat
*dir_stat
, void *closure
),
252 void *closure
, FcChar8
**cache_file_ret
)
255 FcChar8 cache_base
[CACHEBASE_LEN
];
258 struct stat file_stat
, dir_stat
;
259 FcBool ret
= FcFalse
;
261 if (FcStat (dir
, &dir_stat
) < 0)
264 FcDirCacheBasename (dir
, cache_base
);
266 list
= FcStrListCreate (config
->cacheDirs
);
270 while ((cache_dir
= FcStrListNext (list
)))
272 FcChar8
*cache_hashed
= FcStrPlus (cache_dir
, cache_base
);
275 fd
= FcDirCacheOpenFile (cache_hashed
, &file_stat
);
277 ret
= (*callback
) (fd
, &file_stat
, &dir_stat
, closure
);
282 *cache_file_ret
= cache_hashed
;
284 FcStrFree (cache_hashed
);
288 FcStrFree (cache_hashed
);
290 FcStrListDone (list
);
295 #define FC_CACHE_MIN_MMAP 1024
298 * Skip list element, make sure the 'next' pointer is the last thing
299 * in the structure, it will be allocated large enough to hold all
300 * of the necessary pointers
303 typedef struct _FcCacheSkip FcCacheSkip
;
305 struct _FcCacheSkip
{
312 FcCacheSkip
*next
[1];
316 * The head of the skip list; pointers for every possible level
317 * in the skip list, plus the largest level in the list
320 #define FC_CACHE_MAX_LEVEL 16
322 static FcCacheSkip
*fcCacheChains
[FC_CACHE_MAX_LEVEL
];
323 static int fcCacheMaxLevel
;
326 # define FcRandom() random()
329 # define FcRandom() lrand48()
332 # define FcRandom() rand()
337 * Generate a random level number, distributed
338 * so that each level is 1/4 as likely as the one before
340 * Note that level numbers run 1 <= level <= MAX_LEVEL
345 /* tricky bit -- each bit is '1' 75% of the time */
346 long int bits
= FcRandom () | FcRandom ();
349 while (++level
< FC_CACHE_MAX_LEVEL
)
359 * Insert cache into the list
362 FcCacheInsert (FcCache
*cache
, struct stat
*cache_stat
)
364 FcCacheSkip
**update
[FC_CACHE_MAX_LEVEL
];
365 FcCacheSkip
*s
, **next
;
369 * Find links along each chain
371 next
= fcCacheChains
;
372 for (i
= fcCacheMaxLevel
; --i
>= 0; )
374 for (; (s
= next
[i
]); next
= s
->next
)
375 if (s
->cache
> cache
)
377 update
[i
] = &next
[i
];
381 * Create new list element
383 level
= random_level ();
384 if (level
> fcCacheMaxLevel
)
386 level
= fcCacheMaxLevel
+ 1;
387 update
[fcCacheMaxLevel
] = &fcCacheChains
[fcCacheMaxLevel
];
388 fcCacheMaxLevel
= level
;
391 s
= malloc (sizeof (FcCacheSkip
) + (level
- 1) * sizeof (FcCacheSkip
*));
396 s
->size
= cache
->size
;
400 s
->cache_dev
= cache_stat
->st_dev
;
401 s
->cache_ino
= cache_stat
->st_ino
;
402 s
->cache_mtime
= cache_stat
->st_mtime
;
412 * Insert into all fcCacheChains
414 for (i
= 0; i
< level
; i
++)
416 s
->next
[i
] = *update
[i
];
423 FcCacheFindByAddr (void *object
)
426 FcCacheSkip
**next
= fcCacheChains
;
430 * Walk chain pointers one level at a time
432 for (i
= fcCacheMaxLevel
; --i
>= 0;)
433 while (next
[i
] && (char *) object
>= ((char *) next
[i
]->cache
+ next
[i
]->size
))
434 next
= next
[i
]->next
;
439 if (s
&& (char *) object
< ((char *) s
->cache
+ s
->size
))
445 FcCacheRemove (FcCache
*cache
)
447 FcCacheSkip
**update
[FC_CACHE_MAX_LEVEL
];
448 FcCacheSkip
*s
, **next
;
452 * Find links along each chain
454 next
= fcCacheChains
;
455 for (i
= fcCacheMaxLevel
; --i
>= 0; )
457 for (; (s
= next
[i
]); next
= s
->next
)
458 if (s
->cache
>= cache
)
460 update
[i
] = &next
[i
];
463 for (i
= 0; i
< fcCacheMaxLevel
&& *update
[i
] == s
; i
++)
464 *update
[i
] = s
->next
[i
];
465 while (fcCacheMaxLevel
> 0 && fcCacheChains
[fcCacheMaxLevel
- 1] == NULL
)
471 FcCacheFindByStat (struct stat
*cache_stat
)
475 for (s
= fcCacheChains
[0]; s
; s
= s
->next
[0])
476 if (s
->cache_dev
== cache_stat
->st_dev
&&
477 s
->cache_ino
== cache_stat
->st_ino
&&
478 s
->cache_mtime
== cache_stat
->st_mtime
)
487 FcDirCacheDispose (FcCache
*cache
)
489 switch (cache
->magic
) {
490 case FC_CACHE_MAGIC_ALLOC
:
493 case FC_CACHE_MAGIC_MMAP
:
494 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
495 munmap (cache
, cache
->size
);
496 #elif defined(_WIN32)
497 UnmapViewOfFile (cache
);
501 FcCacheRemove (cache
);
505 FcCacheObjectReference (void *object
)
507 FcCacheSkip
*skip
= FcCacheFindByAddr (object
);
514 FcCacheObjectDereference (void *object
)
516 FcCacheSkip
*skip
= FcCacheFindByAddr (object
);
522 FcDirCacheDispose (skip
->cache
);
531 for (i
= 0; i
< FC_CACHE_MAX_LEVEL
; i
++)
532 assert (fcCacheChains
[i
] == NULL
);
533 assert (fcCacheMaxLevel
== 0);
537 FcCacheTimeValid (FcCache
*cache
, struct stat
*dir_stat
)
539 struct stat dir_static
;
543 if (FcStat (FcCacheDir (cache
), &dir_static
) < 0)
545 dir_stat
= &dir_static
;
547 if (FcDebug () & FC_DBG_CACHE
)
548 printf ("FcCacheTimeValid dir \"%s\" cache time %d dir time %d\n",
549 FcCacheDir (cache
), cache
->mtime
, (int) dir_stat
->st_mtime
);
550 return cache
->mtime
== (int) dir_stat
->st_mtime
;
554 * Map a cache file into memory
557 FcDirCacheMapFd (int fd
, struct stat
*fd_stat
, struct stat
*dir_stat
)
560 FcBool allocated
= FcFalse
;
562 if (fd_stat
->st_size
< sizeof (FcCache
))
564 cache
= FcCacheFindByStat (fd_stat
);
567 if (FcCacheTimeValid (cache
, dir_stat
))
569 FcDirCacheUnload (cache
);
574 * Lage cache files are mmap'ed, smaller cache files are read. This
575 * balances the system cost of mmap against per-process memory usage.
577 if (fd_stat
->st_size
>= FC_CACHE_MIN_MMAP
)
579 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
580 cache
= mmap (0, fd_stat
->st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
581 if (cache
== MAP_FAILED
)
583 #elif defined(_WIN32)
588 hFileMap
= CreateFileMapping((HANDLE
) _get_osfhandle(fd
), NULL
,
589 PAGE_READONLY
, 0, 0, NULL
);
590 if (hFileMap
!= NULL
)
592 cache
= MapViewOfFile (hFileMap
, FILE_MAP_READ
, 0, 0,
594 CloseHandle (hFileMap
);
601 cache
= malloc (fd_stat
->st_size
);
605 if (read (fd
, cache
, fd_stat
->st_size
) != fd_stat
->st_size
)
612 if (cache
->magic
!= FC_CACHE_MAGIC_MMAP
||
613 cache
->version
< FC_CACHE_CONTENT_VERSION
||
614 cache
->size
!= fd_stat
->st_size
||
615 !FcCacheTimeValid (cache
, dir_stat
) ||
616 !FcCacheInsert (cache
, fd_stat
))
622 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
623 munmap (cache
, fd_stat
->st_size
);
624 #elif defined(_WIN32)
625 UnmapViewOfFile (cache
);
631 /* Mark allocated caches so they're freed rather than unmapped */
633 cache
->magic
= FC_CACHE_MAGIC_ALLOC
;
639 FcDirCacheReference (FcCache
*cache
, int nref
)
641 FcCacheSkip
*skip
= FcCacheFindByAddr (cache
);
648 FcDirCacheUnload (FcCache
*cache
)
650 FcCacheObjectDereference (cache
);
654 FcDirCacheMapHelper (int fd
, struct stat
*fd_stat
, struct stat
*dir_stat
, void *closure
)
656 FcCache
*cache
= FcDirCacheMapFd (fd
, fd_stat
, dir_stat
);
660 *((FcCache
**) closure
) = cache
;
665 FcDirCacheLoad (const FcChar8
*dir
, FcConfig
*config
, FcChar8
**cache_file
)
667 FcCache
*cache
= NULL
;
669 if (!FcDirCacheProcess (config
, dir
,
677 FcDirCacheLoadFile (const FcChar8
*cache_file
, struct stat
*file_stat
)
681 struct stat my_file_stat
;
684 file_stat
= &my_file_stat
;
685 fd
= FcDirCacheOpenFile (cache_file
, file_stat
);
688 cache
= FcDirCacheMapFd (fd
, file_stat
, NULL
);
694 * Validate a cache file by reading the header and checking
695 * the magic number and the size field
698 FcDirCacheValidateHelper (int fd
, struct stat
*fd_stat
, struct stat
*dir_stat
, void *closure
)
703 if (read (fd
, &c
, sizeof (FcCache
)) != sizeof (FcCache
))
705 else if (c
.magic
!= FC_CACHE_MAGIC_MMAP
)
707 else if (c
.version
< FC_CACHE_CONTENT_VERSION
)
709 else if (fd_stat
->st_size
!= c
.size
)
711 else if (c
.mtime
!= (int) dir_stat
->st_mtime
)
717 FcDirCacheValidConfig (const FcChar8
*dir
, FcConfig
*config
)
719 return FcDirCacheProcess (config
, dir
,
720 FcDirCacheValidateHelper
,
725 FcDirCacheValid (const FcChar8
*dir
)
729 config
= FcConfigGetCurrent ();
733 return FcDirCacheValidConfig (dir
, config
);
737 * Build a cache structure from the given contents
740 FcDirCacheBuild (FcFontSet
*set
, const FcChar8
*dir
, struct stat
*dir_stat
, FcStrSet
*dirs
)
742 FcSerialize
*serialize
= FcSerializeCreate ();
745 FcChar8
*dir_serialize
;
746 intptr_t *dirs_serialize
;
747 FcFontSet
*set_serialize
;
752 * Space for cache structure
754 FcSerializeReserve (serialize
, sizeof (FcCache
));
758 if (!FcStrSerializeAlloc (serialize
, dir
))
763 FcSerializeAlloc (serialize
, dirs
, dirs
->num
* sizeof (FcChar8
*));
764 for (i
= 0; i
< dirs
->num
; i
++)
765 if (!FcStrSerializeAlloc (serialize
, dirs
->strs
[i
]))
771 if (!FcFontSetSerializeAlloc (serialize
, set
))
774 /* Serialize layout complete. Now allocate space and fill it */
775 cache
= malloc (serialize
->size
);
778 /* shut up valgrind */
779 memset (cache
, 0, serialize
->size
);
781 serialize
->linear
= cache
;
783 cache
->magic
= FC_CACHE_MAGIC_ALLOC
;
784 cache
->version
= FC_CACHE_CONTENT_VERSION
;
785 cache
->size
= serialize
->size
;
786 cache
->mtime
= (int) dir_stat
->st_mtime
;
789 * Serialize directory name
791 dir_serialize
= FcStrSerialize (serialize
, dir
);
794 cache
->dir
= FcPtrToOffset (cache
, dir_serialize
);
799 dirs_serialize
= FcSerializePtr (serialize
, dirs
);
802 cache
->dirs
= FcPtrToOffset (cache
, dirs_serialize
);
803 cache
->dirs_count
= dirs
->num
;
804 for (i
= 0; i
< dirs
->num
; i
++)
806 FcChar8
*d_serialize
= FcStrSerialize (serialize
, dirs
->strs
[i
]);
809 dirs_serialize
[i
] = FcPtrToOffset (dirs_serialize
, d_serialize
);
815 set_serialize
= FcFontSetSerialize (serialize
, set
);
818 cache
->set
= FcPtrToOffset (cache
, set_serialize
);
820 FcSerializeDestroy (serialize
);
822 FcCacheInsert (cache
, NULL
);
829 FcSerializeDestroy (serialize
);
835 #define mkdir(path,mode) _mkdir(path)
839 FcMakeDirectory (const FcChar8
*dir
)
844 if (strlen ((char *) dir
) == 0)
847 parent
= FcStrDirname (dir
);
850 if (access ((char *) parent
, F_OK
) == 0)
851 ret
= mkdir ((char *) dir
, 0755) == 0 && chmod ((char *) dir
, 0755) == 0;
852 else if (access ((char *) parent
, F_OK
) == -1)
853 ret
= FcMakeDirectory (parent
) && (mkdir ((char *) dir
, 0755) == 0) && chmod ((char *) dir
, 0755) == 0;
860 /* write serialized state to the cache file */
862 FcDirCacheWrite (FcCache
*cache
, FcConfig
*config
)
864 FcChar8
*dir
= FcCacheDir (cache
);
865 FcChar8 cache_base
[CACHEBASE_LEN
];
866 FcChar8
*cache_hashed
;
870 FcChar8
*cache_dir
= NULL
;
872 FcChar8
*full_test_dir
;
873 FcBool free_test_dir
;
875 struct stat cache_stat
;
880 * Write it to the first directory in the list which is writable
883 list
= FcStrListCreate (config
->cacheDirs
);
886 free_test_dir
= FcFalse
;
887 while ((test_dir
= FcStrListNext (list
))) {
889 FcStrFree (full_test_dir
);
890 free_test_dir
= FcFalse
;
892 full_test_dir
= FcConfigGetRootPlus (test_dir
);
895 free_test_dir
= FcTrue
;
896 test_dir
= full_test_dir
;
899 if (access ((char *) test_dir
, W_OK
|X_OK
) == 0)
901 cache_dir
= test_dir
;
907 * If the directory doesn't exist, try to create it
909 if (access ((char *) test_dir
, F_OK
) == -1) {
910 if (FcMakeDirectory (test_dir
))
912 cache_dir
= test_dir
;
917 * Otherwise, try making it writable
919 else if (chmod ((char *) test_dir
, 0755) == 0)
921 cache_dir
= test_dir
;
926 FcStrListDone (list
);
930 FcDirCacheBasename (dir
, cache_base
);
931 cache_hashed
= FcStrPlus (cache_dir
, cache_base
);
935 if (FcDebug () & FC_DBG_CACHE
)
936 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
939 atomic
= FcAtomicCreate ((FcChar8
*)cache_hashed
);
943 if (!FcAtomicLock (atomic
))
946 fd
= open((char *)FcAtomicNewFile (atomic
), O_RDWR
| O_CREAT
| O_BINARY
, 0666);
950 /* Temporarily switch magic to MMAP while writing to file */
951 magic
= cache
->magic
;
952 if (magic
!= FC_CACHE_MAGIC_MMAP
)
953 cache
->magic
= FC_CACHE_MAGIC_MMAP
;
956 * Write cache contents to file
958 written
= write (fd
, cache
, cache
->size
);
960 /* Switch magic back */
961 if (magic
!= FC_CACHE_MAGIC_MMAP
)
962 cache
->magic
= magic
;
964 if (written
!= cache
->size
)
966 perror ("write cache");
971 if (!FcAtomicReplaceOrig(atomic
))
974 /* If the file is small, update the cache chain entry such that the
975 * new cache file is not read again. If it's large, we don't do that
976 * such that we reload it, using mmap, which is shared across processes.
978 if (cache
->size
< FC_CACHE_MIN_MMAP
&&
979 (skip
= FcCacheFindByAddr (cache
)) &&
980 FcStat (cache_hashed
, &cache_stat
))
982 skip
->cache_dev
= cache_stat
.st_dev
;
983 skip
->cache_ino
= cache_stat
.st_ino
;
984 skip
->cache_mtime
= cache_stat
.st_mtime
;
987 FcStrFree (cache_hashed
);
988 FcAtomicUnlock (atomic
);
989 FcAtomicDestroy (atomic
);
995 FcAtomicUnlock (atomic
);
997 FcAtomicDestroy (atomic
);
999 FcStrFree (cache_hashed
);
1002 FcStrFree (full_test_dir
);
1007 * Hokey little macro trick to permit the definitions of C functions
1008 * with the same name as CPP macros
1010 #define args1(x) (x)
1011 #define args2(x,y) (x,y)
1014 FcCacheDir
args1(const FcCache
*c
)
1016 return FcCacheDir (c
);
1020 FcCacheCopySet
args1(const FcCache
*c
)
1022 FcFontSet
*old
= FcCacheSet (c
);
1023 FcFontSet
*new = FcFontSetCreate ();
1028 for (i
= 0; i
< old
->nfont
; i
++)
1030 FcPattern
*font
= FcFontSetFont (old
, i
);
1032 FcPatternReference (font
);
1033 if (!FcFontSetAdd (new, font
))
1035 FcFontSetDestroy (new);
1043 FcCacheSubdir
args2(const FcCache
*c
, int i
)
1045 return FcCacheSubdir (c
, i
);
1049 FcCacheNumSubdir
args1(const FcCache
*c
)
1051 return c
->dirs_count
;
1055 FcCacheNumFont
args1(const FcCache
*c
)
1057 return FcCacheSet(c
)->nfont
;
1061 * This code implements the MD5 message-digest algorithm.
1062 * The algorithm is due to Ron Rivest. This code was
1063 * written by Colin Plumb in 1993, no copyright is claimed.
1064 * This code is in the public domain; do with it what you wish.
1066 * Equivalent code is available from RSA Data Security, Inc.
1067 * This code has been tested against that, and is equivalent,
1068 * except that you don't need to include two pages of legalese
1071 * To compute the message digest of a chunk of bytes, declare an
1072 * MD5Context structure, pass it to MD5Init, call MD5Update as
1073 * needed on buffers full of bytes, and then call MD5Final, which
1074 * will fill a supplied 16-byte array with the digest.
1078 #define byteReverse(buf, len) /* Nothing */
1081 * Note: this code is harmless on little-endian machines.
1083 void byteReverse(unsigned char *buf
, unsigned longs
)
1087 t
= (FcChar32
) ((unsigned) buf
[3] << 8 | buf
[2]) << 16 |
1088 ((unsigned) buf
[1] << 8 | buf
[0]);
1089 *(FcChar32
*) buf
= t
;
1096 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1097 * initialization constants.
1099 static void MD5Init(struct MD5Context
*ctx
)
1101 ctx
->buf
[0] = 0x67452301;
1102 ctx
->buf
[1] = 0xefcdab89;
1103 ctx
->buf
[2] = 0x98badcfe;
1104 ctx
->buf
[3] = 0x10325476;
1111 * Update context to reflect the concatenation of another buffer full
1114 static void MD5Update(struct MD5Context
*ctx
, const unsigned char *buf
, unsigned len
)
1118 /* Update bitcount */
1121 if ((ctx
->bits
[0] = t
+ ((FcChar32
) len
<< 3)) < t
)
1122 ctx
->bits
[1]++; /* Carry from low to high */
1123 ctx
->bits
[1] += len
>> 29;
1125 t
= (t
>> 3) & 0x3f; /* Bytes already in shsInfo->data */
1127 /* Handle any leading odd-sized chunks */
1130 unsigned char *p
= (unsigned char *) ctx
->in
+ t
;
1134 memcpy(p
, buf
, len
);
1138 byteReverse(ctx
->in
, 16);
1139 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1143 /* Process data in 64-byte chunks */
1146 memcpy(ctx
->in
, buf
, 64);
1147 byteReverse(ctx
->in
, 16);
1148 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1153 /* Handle any remaining bytes of data. */
1155 memcpy(ctx
->in
, buf
, len
);
1159 * Final wrapup - pad to 64-byte boundary with the bit pattern
1160 * 1 0* (64-bit count of bits processed, MSB-first)
1162 static void MD5Final(unsigned char digest
[16], struct MD5Context
*ctx
)
1167 /* Compute number of bytes mod 64 */
1168 count
= (ctx
->bits
[0] >> 3) & 0x3F;
1170 /* Set the first char of padding to 0x80. This is safe since there is
1171 always at least one byte free */
1172 p
= ctx
->in
+ count
;
1175 /* Bytes of padding needed to make 64 bytes */
1176 count
= 64 - 1 - count
;
1178 /* Pad out to 56 mod 64 */
1180 /* Two lots of padding: Pad the first block to 64 bytes */
1181 memset(p
, 0, count
);
1182 byteReverse(ctx
->in
, 16);
1183 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1185 /* Now fill the next block with 56 bytes */
1186 memset(ctx
->in
, 0, 56);
1188 /* Pad block to 56 bytes */
1189 memset(p
, 0, count
- 8);
1191 byteReverse(ctx
->in
, 14);
1193 /* Append length in bits and transform */
1194 ((FcChar32
*) ctx
->in
)[14] = ctx
->bits
[0];
1195 ((FcChar32
*) ctx
->in
)[15] = ctx
->bits
[1];
1197 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1198 byteReverse((unsigned char *) ctx
->buf
, 4);
1199 memcpy(digest
, ctx
->buf
, 16);
1200 memset(ctx
, 0, sizeof(*ctx
)); /* In case it's sensitive */
1204 /* The four core functions - F1 is optimized somewhat */
1206 /* #define F1(x, y, z) (x & y | ~x & z) */
1207 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1208 #define F2(x, y, z) F1(z, x, y)
1209 #define F3(x, y, z) (x ^ y ^ z)
1210 #define F4(x, y, z) (y ^ (x | ~z))
1212 /* This is the central step in the MD5 algorithm. */
1213 #define MD5STEP(f, w, x, y, z, data, s) \
1214 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1217 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1218 * reflect the addition of 16 longwords of new data. MD5Update blocks
1219 * the data and converts bytes into longwords for this routine.
1221 static void MD5Transform(FcChar32 buf
[4], FcChar32 in
[16])
1223 register FcChar32 a
, b
, c
, d
;
1230 MD5STEP(F1
, a
, b
, c
, d
, in
[0] + 0xd76aa478, 7);
1231 MD5STEP(F1
, d
, a
, b
, c
, in
[1] + 0xe8c7b756, 12);
1232 MD5STEP(F1
, c
, d
, a
, b
, in
[2] + 0x242070db, 17);
1233 MD5STEP(F1
, b
, c
, d
, a
, in
[3] + 0xc1bdceee, 22);
1234 MD5STEP(F1
, a
, b
, c
, d
, in
[4] + 0xf57c0faf, 7);
1235 MD5STEP(F1
, d
, a
, b
, c
, in
[5] + 0x4787c62a, 12);
1236 MD5STEP(F1
, c
, d
, a
, b
, in
[6] + 0xa8304613, 17);
1237 MD5STEP(F1
, b
, c
, d
, a
, in
[7] + 0xfd469501, 22);
1238 MD5STEP(F1
, a
, b
, c
, d
, in
[8] + 0x698098d8, 7);
1239 MD5STEP(F1
, d
, a
, b
, c
, in
[9] + 0x8b44f7af, 12);
1240 MD5STEP(F1
, c
, d
, a
, b
, in
[10] + 0xffff5bb1, 17);
1241 MD5STEP(F1
, b
, c
, d
, a
, in
[11] + 0x895cd7be, 22);
1242 MD5STEP(F1
, a
, b
, c
, d
, in
[12] + 0x6b901122, 7);
1243 MD5STEP(F1
, d
, a
, b
, c
, in
[13] + 0xfd987193, 12);
1244 MD5STEP(F1
, c
, d
, a
, b
, in
[14] + 0xa679438e, 17);
1245 MD5STEP(F1
, b
, c
, d
, a
, in
[15] + 0x49b40821, 22);
1247 MD5STEP(F2
, a
, b
, c
, d
, in
[1] + 0xf61e2562, 5);
1248 MD5STEP(F2
, d
, a
, b
, c
, in
[6] + 0xc040b340, 9);
1249 MD5STEP(F2
, c
, d
, a
, b
, in
[11] + 0x265e5a51, 14);
1250 MD5STEP(F2
, b
, c
, d
, a
, in
[0] + 0xe9b6c7aa, 20);
1251 MD5STEP(F2
, a
, b
, c
, d
, in
[5] + 0xd62f105d, 5);
1252 MD5STEP(F2
, d
, a
, b
, c
, in
[10] + 0x02441453, 9);
1253 MD5STEP(F2
, c
, d
, a
, b
, in
[15] + 0xd8a1e681, 14);
1254 MD5STEP(F2
, b
, c
, d
, a
, in
[4] + 0xe7d3fbc8, 20);
1255 MD5STEP(F2
, a
, b
, c
, d
, in
[9] + 0x21e1cde6, 5);
1256 MD5STEP(F2
, d
, a
, b
, c
, in
[14] + 0xc33707d6, 9);
1257 MD5STEP(F2
, c
, d
, a
, b
, in
[3] + 0xf4d50d87, 14);
1258 MD5STEP(F2
, b
, c
, d
, a
, in
[8] + 0x455a14ed, 20);
1259 MD5STEP(F2
, a
, b
, c
, d
, in
[13] + 0xa9e3e905, 5);
1260 MD5STEP(F2
, d
, a
, b
, c
, in
[2] + 0xfcefa3f8, 9);
1261 MD5STEP(F2
, c
, d
, a
, b
, in
[7] + 0x676f02d9, 14);
1262 MD5STEP(F2
, b
, c
, d
, a
, in
[12] + 0x8d2a4c8a, 20);
1264 MD5STEP(F3
, a
, b
, c
, d
, in
[5] + 0xfffa3942, 4);
1265 MD5STEP(F3
, d
, a
, b
, c
, in
[8] + 0x8771f681, 11);
1266 MD5STEP(F3
, c
, d
, a
, b
, in
[11] + 0x6d9d6122, 16);
1267 MD5STEP(F3
, b
, c
, d
, a
, in
[14] + 0xfde5380c, 23);
1268 MD5STEP(F3
, a
, b
, c
, d
, in
[1] + 0xa4beea44, 4);
1269 MD5STEP(F3
, d
, a
, b
, c
, in
[4] + 0x4bdecfa9, 11);
1270 MD5STEP(F3
, c
, d
, a
, b
, in
[7] + 0xf6bb4b60, 16);
1271 MD5STEP(F3
, b
, c
, d
, a
, in
[10] + 0xbebfbc70, 23);
1272 MD5STEP(F3
, a
, b
, c
, d
, in
[13] + 0x289b7ec6, 4);
1273 MD5STEP(F3
, d
, a
, b
, c
, in
[0] + 0xeaa127fa, 11);
1274 MD5STEP(F3
, c
, d
, a
, b
, in
[3] + 0xd4ef3085, 16);
1275 MD5STEP(F3
, b
, c
, d
, a
, in
[6] + 0x04881d05, 23);
1276 MD5STEP(F3
, a
, b
, c
, d
, in
[9] + 0xd9d4d039, 4);
1277 MD5STEP(F3
, d
, a
, b
, c
, in
[12] + 0xe6db99e5, 11);
1278 MD5STEP(F3
, c
, d
, a
, b
, in
[15] + 0x1fa27cf8, 16);
1279 MD5STEP(F3
, b
, c
, d
, a
, in
[2] + 0xc4ac5665, 23);
1281 MD5STEP(F4
, a
, b
, c
, d
, in
[0] + 0xf4292244, 6);
1282 MD5STEP(F4
, d
, a
, b
, c
, in
[7] + 0x432aff97, 10);
1283 MD5STEP(F4
, c
, d
, a
, b
, in
[14] + 0xab9423a7, 15);
1284 MD5STEP(F4
, b
, c
, d
, a
, in
[5] + 0xfc93a039, 21);
1285 MD5STEP(F4
, a
, b
, c
, d
, in
[12] + 0x655b59c3, 6);
1286 MD5STEP(F4
, d
, a
, b
, c
, in
[3] + 0x8f0ccc92, 10);
1287 MD5STEP(F4
, c
, d
, a
, b
, in
[10] + 0xffeff47d, 15);
1288 MD5STEP(F4
, b
, c
, d
, a
, in
[1] + 0x85845dd1, 21);
1289 MD5STEP(F4
, a
, b
, c
, d
, in
[8] + 0x6fa87e4f, 6);
1290 MD5STEP(F4
, d
, a
, b
, c
, in
[15] + 0xfe2ce6e0, 10);
1291 MD5STEP(F4
, c
, d
, a
, b
, in
[6] + 0xa3014314, 15);
1292 MD5STEP(F4
, b
, c
, d
, a
, in
[13] + 0x4e0811a1, 21);
1293 MD5STEP(F4
, a
, b
, c
, d
, in
[4] + 0xf7537e82, 6);
1294 MD5STEP(F4
, d
, a
, b
, c
, in
[11] + 0xbd3af235, 10);
1295 MD5STEP(F4
, c
, d
, a
, b
, in
[2] + 0x2ad7d2bb, 15);
1296 MD5STEP(F4
, b
, c
, d
, a
, in
[9] + 0xeb86d391, 21);
1304 #include "fcaliastail.h"