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 char *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 char *file
, struct stat
*statb
)
141 return stat ((char *) file
, statb
);
146 static const char bin2hex
[] = { '0', '1', '2', '3',
149 'c', 'd', 'e', 'f' };
152 FcDirCacheBasename (const FcChar8
* dir
, FcChar8 cache_base
[CACHEBASE_LEN
])
154 unsigned char hash
[16];
157 struct MD5Context ctx
;
160 MD5Update (&ctx
, (const unsigned char *)dir
, strlen ((const char *) dir
));
162 MD5Final (hash
, &ctx
);
165 hex_hash
= cache_base
+ 1;
166 for (cnt
= 0; cnt
< 16; ++cnt
)
168 hex_hash
[2*cnt
] = bin2hex
[hash
[cnt
] >> 4];
169 hex_hash
[2*cnt
+1] = bin2hex
[hash
[cnt
] & 0xf];
172 strcat ((char *) cache_base
, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX
);
178 FcDirCacheUnlink (const FcChar8
*dir
, FcConfig
*config
)
180 FcChar8
*cache_hashed
= NULL
;
181 FcChar8 cache_base
[CACHEBASE_LEN
];
185 FcDirCacheBasename (dir
, cache_base
);
187 list
= FcStrListCreate (config
->cacheDirs
);
191 while ((cache_dir
= FcStrListNext (list
)))
193 cache_hashed
= FcStrPlus (cache_dir
, cache_base
);
196 (void) unlink ((char *) cache_hashed
);
197 FcStrFree (cache_hashed
);
199 FcStrListDone (list
);
200 /* return FcFalse if something went wrong */
207 FcDirCacheOpenFile (const FcChar8
*cache_file
, struct stat
*file_stat
)
212 if (FcStat (cache_file
, file_stat
) < 0)
215 fd
= open((char *) cache_file
, O_RDONLY
| O_BINARY
);
219 if (fstat (fd
, file_stat
) < 0)
229 * Look for a cache file for the specified dir. Attempt
230 * to use each one we find, stopping when the callback
234 FcDirCacheProcess (FcConfig
*config
, const FcChar8
*dir
,
235 FcBool (*callback
) (int fd
, struct stat
*fd_stat
,
236 struct stat
*dir_stat
, void *closure
),
237 void *closure
, FcChar8
**cache_file_ret
)
240 FcChar8 cache_base
[CACHEBASE_LEN
];
243 struct stat file_stat
, dir_stat
;
244 FcBool ret
= FcFalse
;
246 if (FcStat (dir
, &dir_stat
) < 0)
249 FcDirCacheBasename (dir
, cache_base
);
251 list
= FcStrListCreate (config
->cacheDirs
);
255 while ((cache_dir
= FcStrListNext (list
)))
257 FcChar8
*cache_hashed
= FcStrPlus (cache_dir
, cache_base
);
260 fd
= FcDirCacheOpenFile (cache_hashed
, &file_stat
);
262 ret
= (*callback
) (fd
, &file_stat
, &dir_stat
, closure
);
267 *cache_file_ret
= cache_hashed
;
269 FcStrFree (cache_hashed
);
273 FcStrFree (cache_hashed
);
275 FcStrListDone (list
);
280 #define FC_CACHE_MIN_MMAP 1024
283 * Skip list element, make sure the 'next' pointer is the last thing
284 * in the structure, it will be allocated large enough to hold all
285 * of the necessary pointers
288 typedef struct _FcCacheSkip FcCacheSkip
;
290 struct _FcCacheSkip
{
297 FcCacheSkip
*next
[1];
301 * The head of the skip list; pointers for every possible level
302 * in the skip list, plus the largest level in the list
305 #define FC_CACHE_MAX_LEVEL 16
307 static FcCacheSkip
*fcCacheChains
[FC_CACHE_MAX_LEVEL
];
308 static int fcCacheMaxLevel
;
311 # define FcRandom() random()
314 # define FcRandom() lrand48()
317 # define FcRandom() rand()
322 * Generate a random level number, distributed
323 * so that each level is 1/4 as likely as the one before
325 * Note that level numbers run 1 <= level <= MAX_LEVEL
330 /* tricky bit -- each bit is '1' 75% of the time */
331 long int bits
= FcRandom () | FcRandom ();
334 while (++level
< FC_CACHE_MAX_LEVEL
)
344 * Insert cache into the list
347 FcCacheInsert (FcCache
*cache
, struct stat
*cache_stat
)
349 FcCacheSkip
**update
[FC_CACHE_MAX_LEVEL
];
350 FcCacheSkip
*s
, **next
;
354 * Find links along each chain
356 next
= fcCacheChains
;
357 for (i
= fcCacheMaxLevel
; --i
>= 0; )
359 for (; (s
= next
[i
]); next
= s
->next
)
360 if (s
->cache
> cache
)
362 update
[i
] = &next
[i
];
366 * Create new list element
368 level
= random_level ();
369 if (level
> fcCacheMaxLevel
)
371 level
= fcCacheMaxLevel
+ 1;
372 update
[fcCacheMaxLevel
] = &fcCacheChains
[fcCacheMaxLevel
];
373 fcCacheMaxLevel
= level
;
376 s
= malloc (sizeof (FcCacheSkip
) + (level
- 1) * sizeof (FcCacheSkip
*));
381 s
->size
= cache
->size
;
385 s
->cache_dev
= cache_stat
->st_dev
;
386 s
->cache_ino
= cache_stat
->st_ino
;
387 s
->cache_mtime
= cache_stat
->st_mtime
;
397 * Insert into all fcCacheChains
399 for (i
= 0; i
< level
; i
++)
401 s
->next
[i
] = *update
[i
];
408 FcCacheFindByAddr (void *object
)
411 FcCacheSkip
**next
= fcCacheChains
;
415 * Walk chain pointers one level at a time
417 for (i
= fcCacheMaxLevel
; --i
>= 0;)
418 while (next
[i
] && (char *) object
>= ((char *) next
[i
]->cache
+ next
[i
]->size
))
419 next
= next
[i
]->next
;
424 if (s
&& (char *) object
< ((char *) s
->cache
+ s
->size
))
430 FcCacheRemove (FcCache
*cache
)
432 FcCacheSkip
**update
[FC_CACHE_MAX_LEVEL
];
433 FcCacheSkip
*s
, **next
;
437 * Find links along each chain
439 next
= fcCacheChains
;
440 for (i
= fcCacheMaxLevel
; --i
>= 0; )
442 for (; (s
= next
[i
]); next
= s
->next
)
443 if (s
->cache
>= cache
)
445 update
[i
] = &next
[i
];
448 for (i
= 0; i
< fcCacheMaxLevel
&& *update
[i
] == s
; i
++)
449 *update
[i
] = s
->next
[i
];
450 while (fcCacheMaxLevel
> 0 && fcCacheChains
[fcCacheMaxLevel
- 1] == NULL
)
456 FcCacheFindByStat (struct stat
*cache_stat
)
460 for (s
= fcCacheChains
[0]; s
; s
= s
->next
[0])
461 if (s
->cache_dev
== cache_stat
->st_dev
&&
462 s
->cache_ino
== cache_stat
->st_ino
&&
463 s
->cache_mtime
== cache_stat
->st_mtime
)
472 FcDirCacheDispose (FcCache
*cache
)
474 switch (cache
->magic
) {
475 case FC_CACHE_MAGIC_ALLOC
:
478 case FC_CACHE_MAGIC_MMAP
:
479 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
480 munmap (cache
, cache
->size
);
481 #elif defined(_WIN32)
482 UnmapViewOfFile (cache
);
486 FcCacheRemove (cache
);
490 FcCacheObjectReference (void *object
)
492 FcCacheSkip
*skip
= FcCacheFindByAddr (object
);
499 FcCacheObjectDereference (void *object
)
501 FcCacheSkip
*skip
= FcCacheFindByAddr (object
);
507 FcDirCacheDispose (skip
->cache
);
516 for (i
= 0; i
< FC_CACHE_MAX_LEVEL
; i
++)
517 assert (fcCacheChains
[i
] == NULL
);
518 assert (fcCacheMaxLevel
== 0);
522 FcCacheTimeValid (FcCache
*cache
, struct stat
*dir_stat
)
524 struct stat dir_static
;
528 if (FcStat (FcCacheDir (cache
), &dir_static
) < 0)
530 dir_stat
= &dir_static
;
532 if (FcDebug () & FC_DBG_CACHE
)
533 printf ("FcCacheTimeValid dir \"%s\" cache time %d dir time %d\n",
534 FcCacheDir (cache
), cache
->mtime
, (int) dir_stat
->st_mtime
);
535 return cache
->mtime
== (int) dir_stat
->st_mtime
;
539 * Map a cache file into memory
542 FcDirCacheMapFd (int fd
, struct stat
*fd_stat
, struct stat
*dir_stat
)
545 FcBool allocated
= FcFalse
;
547 if (fd_stat
->st_size
< sizeof (FcCache
))
549 cache
= FcCacheFindByStat (fd_stat
);
552 if (FcCacheTimeValid (cache
, dir_stat
))
554 FcDirCacheUnload (cache
);
559 * Lage cache files are mmap'ed, smaller cache files are read. This
560 * balances the system cost of mmap against per-process memory usage.
562 if (fd_stat
->st_size
>= FC_CACHE_MIN_MMAP
)
564 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
565 cache
= mmap (0, fd_stat
->st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
566 if (cache
== MAP_FAILED
)
568 #elif defined(_WIN32)
573 hFileMap
= CreateFileMapping((HANDLE
) _get_osfhandle(fd
), NULL
,
574 PAGE_READONLY
, 0, 0, NULL
);
575 if (hFileMap
!= NULL
)
577 cache
= MapViewOfFile (hFileMap
, FILE_MAP_READ
, 0, 0,
579 CloseHandle (hFileMap
);
586 cache
= malloc (fd_stat
->st_size
);
590 if (read (fd
, cache
, fd_stat
->st_size
) != fd_stat
->st_size
)
597 if (cache
->magic
!= FC_CACHE_MAGIC_MMAP
||
598 cache
->version
< FC_CACHE_CONTENT_VERSION
||
599 cache
->size
!= fd_stat
->st_size
||
600 !FcCacheTimeValid (cache
, dir_stat
) ||
601 !FcCacheInsert (cache
, fd_stat
))
607 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
608 munmap (cache
, fd_stat
->st_size
);
609 #elif defined(_WIN32)
610 UnmapViewOfFile (cache
);
616 /* Mark allocated caches so they're freed rather than unmapped */
618 cache
->magic
= FC_CACHE_MAGIC_ALLOC
;
624 FcDirCacheReference (FcCache
*cache
, int nref
)
626 FcCacheSkip
*skip
= FcCacheFindByAddr (cache
);
633 FcDirCacheUnload (FcCache
*cache
)
635 FcCacheObjectDereference (cache
);
639 FcDirCacheMapHelper (int fd
, struct stat
*fd_stat
, struct stat
*dir_stat
, void *closure
)
641 FcCache
*cache
= FcDirCacheMapFd (fd
, fd_stat
, dir_stat
);
645 *((FcCache
**) closure
) = cache
;
650 FcDirCacheLoad (const FcChar8
*dir
, FcConfig
*config
, FcChar8
**cache_file
)
652 FcCache
*cache
= NULL
;
654 if (!FcDirCacheProcess (config
, dir
,
662 FcDirCacheLoadFile (const FcChar8
*cache_file
, struct stat
*file_stat
)
666 struct stat my_file_stat
;
669 file_stat
= &my_file_stat
;
670 fd
= FcDirCacheOpenFile (cache_file
, file_stat
);
673 cache
= FcDirCacheMapFd (fd
, file_stat
, NULL
);
679 * Validate a cache file by reading the header and checking
680 * the magic number and the size field
683 FcDirCacheValidateHelper (int fd
, struct stat
*fd_stat
, struct stat
*dir_stat
, void *closure
)
688 if (read (fd
, &c
, sizeof (FcCache
)) != sizeof (FcCache
))
690 else if (c
.magic
!= FC_CACHE_MAGIC_MMAP
)
692 else if (c
.version
< FC_CACHE_CONTENT_VERSION
)
694 else if (fd_stat
->st_size
!= c
.size
)
696 else if (c
.mtime
!= (int) dir_stat
->st_mtime
)
702 FcDirCacheValidConfig (const FcChar8
*dir
, FcConfig
*config
)
704 return FcDirCacheProcess (config
, dir
,
705 FcDirCacheValidateHelper
,
710 FcDirCacheValid (const FcChar8
*dir
)
714 config
= FcConfigGetCurrent ();
718 return FcDirCacheValidConfig (dir
, config
);
722 * Build a cache structure from the given contents
725 FcDirCacheBuild (FcFontSet
*set
, const FcChar8
*dir
, struct stat
*dir_stat
, FcStrSet
*dirs
)
727 FcSerialize
*serialize
= FcSerializeCreate ();
730 intptr_t cache_offset
;
731 intptr_t dirs_offset
;
732 FcChar8
*dir_serialize
;
733 intptr_t *dirs_serialize
;
734 FcFontSet
*set_serialize
;
739 * Space for cache structure
741 cache_offset
= FcSerializeReserve (serialize
, sizeof (FcCache
));
745 if (!FcStrSerializeAlloc (serialize
, dir
))
750 dirs_offset
= FcSerializeAlloc (serialize
, dirs
, dirs
->num
* sizeof (FcChar8
*));
751 for (i
= 0; i
< dirs
->num
; i
++)
752 if (!FcStrSerializeAlloc (serialize
, dirs
->strs
[i
]))
758 if (!FcFontSetSerializeAlloc (serialize
, set
))
761 /* Serialize layout complete. Now allocate space and fill it */
762 cache
= malloc (serialize
->size
);
765 /* shut up valgrind */
766 memset (cache
, 0, serialize
->size
);
768 serialize
->linear
= cache
;
770 cache
->magic
= FC_CACHE_MAGIC_ALLOC
;
771 cache
->version
= FC_CACHE_CONTENT_VERSION
;
772 cache
->size
= serialize
->size
;
773 cache
->mtime
= (int) dir_stat
->st_mtime
;
776 * Serialize directory name
778 dir_serialize
= FcStrSerialize (serialize
, dir
);
781 cache
->dir
= FcPtrToOffset (cache
, dir_serialize
);
786 dirs_serialize
= FcSerializePtr (serialize
, dirs
);
789 cache
->dirs
= FcPtrToOffset (cache
, dirs_serialize
);
790 cache
->dirs_count
= dirs
->num
;
791 for (i
= 0; i
< dirs
->num
; i
++)
793 FcChar8
*d_serialize
= FcStrSerialize (serialize
, dirs
->strs
[i
]);
796 dirs_serialize
[i
] = FcPtrToOffset (dirs_serialize
, d_serialize
);
802 set_serialize
= FcFontSetSerialize (serialize
, set
);
805 cache
->set
= FcPtrToOffset (cache
, set_serialize
);
807 FcSerializeDestroy (serialize
);
809 FcCacheInsert (cache
, NULL
);
816 FcSerializeDestroy (serialize
);
822 #define mkdir(path,mode) _mkdir(path)
826 FcMakeDirectory (const FcChar8
*dir
)
831 if (strlen ((char *) dir
) == 0)
834 parent
= FcStrDirname (dir
);
837 if (access ((char *) parent
, F_OK
) == 0)
838 ret
= mkdir ((char *) dir
, 0755) == 0 && chmod ((char *) dir
, 0755) == 0;
839 else if (access ((char *) parent
, F_OK
) == -1)
840 ret
= FcMakeDirectory (parent
) && (mkdir ((char *) dir
, 0755) == 0) && chmod ((char *) dir
, 0755) == 0;
847 /* write serialized state to the cache file */
849 FcDirCacheWrite (FcCache
*cache
, FcConfig
*config
)
851 FcChar8
*dir
= FcCacheDir (cache
);
852 FcChar8 cache_base
[CACHEBASE_LEN
];
853 FcChar8
*cache_hashed
;
857 FcChar8
*cache_dir
= NULL
;
860 struct stat cache_stat
;
865 * Write it to the first directory in the list which is writable
868 list
= FcStrListCreate (config
->cacheDirs
);
871 while ((test_dir
= FcStrListNext (list
))) {
872 if (access ((char *) test_dir
, W_OK
|X_OK
) == 0)
874 cache_dir
= test_dir
;
880 * If the directory doesn't exist, try to create it
882 if (access ((char *) test_dir
, F_OK
) == -1) {
883 if (FcMakeDirectory (test_dir
))
885 cache_dir
= test_dir
;
890 * Otherwise, try making it writable
892 else if (chmod ((char *) test_dir
, 0755) == 0)
894 cache_dir
= test_dir
;
899 FcStrListDone (list
);
903 FcDirCacheBasename (dir
, cache_base
);
904 cache_hashed
= FcStrPlus (cache_dir
, cache_base
);
908 if (FcDebug () & FC_DBG_CACHE
)
909 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
912 atomic
= FcAtomicCreate ((FcChar8
*)cache_hashed
);
916 if (!FcAtomicLock (atomic
))
919 fd
= open((char *)FcAtomicNewFile (atomic
), O_RDWR
| O_CREAT
| O_BINARY
, 0666);
923 /* Temporarily switch magic to MMAP while writing to file */
924 magic
= cache
->magic
;
925 if (magic
!= FC_CACHE_MAGIC_MMAP
)
926 cache
->magic
= FC_CACHE_MAGIC_MMAP
;
929 * Write cache contents to file
931 written
= write (fd
, cache
, cache
->size
);
933 /* Switch magic back */
934 if (magic
!= FC_CACHE_MAGIC_MMAP
)
935 cache
->magic
= magic
;
937 if (written
!= cache
->size
)
939 perror ("write cache");
944 if (!FcAtomicReplaceOrig(atomic
))
947 /* If the file is small, update the cache chain entry such that the
948 * new cache file is not read again. If it's large, we don't do that
949 * such that we reload it, using mmap, which is shared across processes.
951 if (cache
->size
< FC_CACHE_MIN_MMAP
&&
952 (skip
= FcCacheFindByAddr (cache
)) &&
953 FcStat (cache_hashed
, &cache_stat
))
955 skip
->cache_dev
= cache_stat
.st_dev
;
956 skip
->cache_ino
= cache_stat
.st_ino
;
957 skip
->cache_mtime
= cache_stat
.st_mtime
;
960 FcStrFree (cache_hashed
);
961 FcAtomicUnlock (atomic
);
962 FcAtomicDestroy (atomic
);
968 FcAtomicUnlock (atomic
);
970 FcAtomicDestroy (atomic
);
972 FcStrFree (cache_hashed
);
977 * Hokey little macro trick to permit the definitions of C functions
978 * with the same name as CPP macros
981 #define args2(x,y) (x,y)
984 FcCacheDir
args1(const FcCache
*c
)
986 return FcCacheDir (c
);
990 FcCacheCopySet
args1(const FcCache
*c
)
992 FcFontSet
*old
= FcCacheSet (c
);
993 FcFontSet
*new = FcFontSetCreate ();
998 for (i
= 0; i
< old
->nfont
; i
++)
1000 FcPattern
*font
= FcFontSetFont (old
, i
);
1002 FcPatternReference (font
);
1003 if (!FcFontSetAdd (new, font
))
1005 FcFontSetDestroy (new);
1013 FcCacheSubdir
args2(const FcCache
*c
, int i
)
1015 return FcCacheSubdir (c
, i
);
1019 FcCacheNumSubdir
args1(const FcCache
*c
)
1021 return c
->dirs_count
;
1025 FcCacheNumFont
args1(const FcCache
*c
)
1027 return FcCacheSet(c
)->nfont
;
1031 * This code implements the MD5 message-digest algorithm.
1032 * The algorithm is due to Ron Rivest. This code was
1033 * written by Colin Plumb in 1993, no copyright is claimed.
1034 * This code is in the public domain; do with it what you wish.
1036 * Equivalent code is available from RSA Data Security, Inc.
1037 * This code has been tested against that, and is equivalent,
1038 * except that you don't need to include two pages of legalese
1041 * To compute the message digest of a chunk of bytes, declare an
1042 * MD5Context structure, pass it to MD5Init, call MD5Update as
1043 * needed on buffers full of bytes, and then call MD5Final, which
1044 * will fill a supplied 16-byte array with the digest.
1048 #define byteReverse(buf, len) /* Nothing */
1051 * Note: this code is harmless on little-endian machines.
1053 void byteReverse(unsigned char *buf
, unsigned longs
)
1057 t
= (FcChar32
) ((unsigned) buf
[3] << 8 | buf
[2]) << 16 |
1058 ((unsigned) buf
[1] << 8 | buf
[0]);
1059 *(FcChar32
*) buf
= t
;
1066 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1067 * initialization constants.
1069 static void MD5Init(struct MD5Context
*ctx
)
1071 ctx
->buf
[0] = 0x67452301;
1072 ctx
->buf
[1] = 0xefcdab89;
1073 ctx
->buf
[2] = 0x98badcfe;
1074 ctx
->buf
[3] = 0x10325476;
1081 * Update context to reflect the concatenation of another buffer full
1084 static void MD5Update(struct MD5Context
*ctx
, const unsigned char *buf
, unsigned len
)
1088 /* Update bitcount */
1091 if ((ctx
->bits
[0] = t
+ ((FcChar32
) len
<< 3)) < t
)
1092 ctx
->bits
[1]++; /* Carry from low to high */
1093 ctx
->bits
[1] += len
>> 29;
1095 t
= (t
>> 3) & 0x3f; /* Bytes already in shsInfo->data */
1097 /* Handle any leading odd-sized chunks */
1100 unsigned char *p
= (unsigned char *) ctx
->in
+ t
;
1104 memcpy(p
, buf
, len
);
1108 byteReverse(ctx
->in
, 16);
1109 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1113 /* Process data in 64-byte chunks */
1116 memcpy(ctx
->in
, buf
, 64);
1117 byteReverse(ctx
->in
, 16);
1118 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1123 /* Handle any remaining bytes of data. */
1125 memcpy(ctx
->in
, buf
, len
);
1129 * Final wrapup - pad to 64-byte boundary with the bit pattern
1130 * 1 0* (64-bit count of bits processed, MSB-first)
1132 static void MD5Final(unsigned char digest
[16], struct MD5Context
*ctx
)
1137 /* Compute number of bytes mod 64 */
1138 count
= (ctx
->bits
[0] >> 3) & 0x3F;
1140 /* Set the first char of padding to 0x80. This is safe since there is
1141 always at least one byte free */
1142 p
= ctx
->in
+ count
;
1145 /* Bytes of padding needed to make 64 bytes */
1146 count
= 64 - 1 - count
;
1148 /* Pad out to 56 mod 64 */
1150 /* Two lots of padding: Pad the first block to 64 bytes */
1151 memset(p
, 0, count
);
1152 byteReverse(ctx
->in
, 16);
1153 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1155 /* Now fill the next block with 56 bytes */
1156 memset(ctx
->in
, 0, 56);
1158 /* Pad block to 56 bytes */
1159 memset(p
, 0, count
- 8);
1161 byteReverse(ctx
->in
, 14);
1163 /* Append length in bits and transform */
1164 ((FcChar32
*) ctx
->in
)[14] = ctx
->bits
[0];
1165 ((FcChar32
*) ctx
->in
)[15] = ctx
->bits
[1];
1167 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1168 byteReverse((unsigned char *) ctx
->buf
, 4);
1169 memcpy(digest
, ctx
->buf
, 16);
1170 memset(ctx
, 0, sizeof(*ctx
)); /* In case it's sensitive */
1174 /* The four core functions - F1 is optimized somewhat */
1176 /* #define F1(x, y, z) (x & y | ~x & z) */
1177 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1178 #define F2(x, y, z) F1(z, x, y)
1179 #define F3(x, y, z) (x ^ y ^ z)
1180 #define F4(x, y, z) (y ^ (x | ~z))
1182 /* This is the central step in the MD5 algorithm. */
1183 #define MD5STEP(f, w, x, y, z, data, s) \
1184 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1187 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1188 * reflect the addition of 16 longwords of new data. MD5Update blocks
1189 * the data and converts bytes into longwords for this routine.
1191 static void MD5Transform(FcChar32 buf
[4], FcChar32 in
[16])
1193 register FcChar32 a
, b
, c
, d
;
1200 MD5STEP(F1
, a
, b
, c
, d
, in
[0] + 0xd76aa478, 7);
1201 MD5STEP(F1
, d
, a
, b
, c
, in
[1] + 0xe8c7b756, 12);
1202 MD5STEP(F1
, c
, d
, a
, b
, in
[2] + 0x242070db, 17);
1203 MD5STEP(F1
, b
, c
, d
, a
, in
[3] + 0xc1bdceee, 22);
1204 MD5STEP(F1
, a
, b
, c
, d
, in
[4] + 0xf57c0faf, 7);
1205 MD5STEP(F1
, d
, a
, b
, c
, in
[5] + 0x4787c62a, 12);
1206 MD5STEP(F1
, c
, d
, a
, b
, in
[6] + 0xa8304613, 17);
1207 MD5STEP(F1
, b
, c
, d
, a
, in
[7] + 0xfd469501, 22);
1208 MD5STEP(F1
, a
, b
, c
, d
, in
[8] + 0x698098d8, 7);
1209 MD5STEP(F1
, d
, a
, b
, c
, in
[9] + 0x8b44f7af, 12);
1210 MD5STEP(F1
, c
, d
, a
, b
, in
[10] + 0xffff5bb1, 17);
1211 MD5STEP(F1
, b
, c
, d
, a
, in
[11] + 0x895cd7be, 22);
1212 MD5STEP(F1
, a
, b
, c
, d
, in
[12] + 0x6b901122, 7);
1213 MD5STEP(F1
, d
, a
, b
, c
, in
[13] + 0xfd987193, 12);
1214 MD5STEP(F1
, c
, d
, a
, b
, in
[14] + 0xa679438e, 17);
1215 MD5STEP(F1
, b
, c
, d
, a
, in
[15] + 0x49b40821, 22);
1217 MD5STEP(F2
, a
, b
, c
, d
, in
[1] + 0xf61e2562, 5);
1218 MD5STEP(F2
, d
, a
, b
, c
, in
[6] + 0xc040b340, 9);
1219 MD5STEP(F2
, c
, d
, a
, b
, in
[11] + 0x265e5a51, 14);
1220 MD5STEP(F2
, b
, c
, d
, a
, in
[0] + 0xe9b6c7aa, 20);
1221 MD5STEP(F2
, a
, b
, c
, d
, in
[5] + 0xd62f105d, 5);
1222 MD5STEP(F2
, d
, a
, b
, c
, in
[10] + 0x02441453, 9);
1223 MD5STEP(F2
, c
, d
, a
, b
, in
[15] + 0xd8a1e681, 14);
1224 MD5STEP(F2
, b
, c
, d
, a
, in
[4] + 0xe7d3fbc8, 20);
1225 MD5STEP(F2
, a
, b
, c
, d
, in
[9] + 0x21e1cde6, 5);
1226 MD5STEP(F2
, d
, a
, b
, c
, in
[14] + 0xc33707d6, 9);
1227 MD5STEP(F2
, c
, d
, a
, b
, in
[3] + 0xf4d50d87, 14);
1228 MD5STEP(F2
, b
, c
, d
, a
, in
[8] + 0x455a14ed, 20);
1229 MD5STEP(F2
, a
, b
, c
, d
, in
[13] + 0xa9e3e905, 5);
1230 MD5STEP(F2
, d
, a
, b
, c
, in
[2] + 0xfcefa3f8, 9);
1231 MD5STEP(F2
, c
, d
, a
, b
, in
[7] + 0x676f02d9, 14);
1232 MD5STEP(F2
, b
, c
, d
, a
, in
[12] + 0x8d2a4c8a, 20);
1234 MD5STEP(F3
, a
, b
, c
, d
, in
[5] + 0xfffa3942, 4);
1235 MD5STEP(F3
, d
, a
, b
, c
, in
[8] + 0x8771f681, 11);
1236 MD5STEP(F3
, c
, d
, a
, b
, in
[11] + 0x6d9d6122, 16);
1237 MD5STEP(F3
, b
, c
, d
, a
, in
[14] + 0xfde5380c, 23);
1238 MD5STEP(F3
, a
, b
, c
, d
, in
[1] + 0xa4beea44, 4);
1239 MD5STEP(F3
, d
, a
, b
, c
, in
[4] + 0x4bdecfa9, 11);
1240 MD5STEP(F3
, c
, d
, a
, b
, in
[7] + 0xf6bb4b60, 16);
1241 MD5STEP(F3
, b
, c
, d
, a
, in
[10] + 0xbebfbc70, 23);
1242 MD5STEP(F3
, a
, b
, c
, d
, in
[13] + 0x289b7ec6, 4);
1243 MD5STEP(F3
, d
, a
, b
, c
, in
[0] + 0xeaa127fa, 11);
1244 MD5STEP(F3
, c
, d
, a
, b
, in
[3] + 0xd4ef3085, 16);
1245 MD5STEP(F3
, b
, c
, d
, a
, in
[6] + 0x04881d05, 23);
1246 MD5STEP(F3
, a
, b
, c
, d
, in
[9] + 0xd9d4d039, 4);
1247 MD5STEP(F3
, d
, a
, b
, c
, in
[12] + 0xe6db99e5, 11);
1248 MD5STEP(F3
, c
, d
, a
, b
, in
[15] + 0x1fa27cf8, 16);
1249 MD5STEP(F3
, b
, c
, d
, a
, in
[2] + 0xc4ac5665, 23);
1251 MD5STEP(F4
, a
, b
, c
, d
, in
[0] + 0xf4292244, 6);
1252 MD5STEP(F4
, d
, a
, b
, c
, in
[7] + 0x432aff97, 10);
1253 MD5STEP(F4
, c
, d
, a
, b
, in
[14] + 0xab9423a7, 15);
1254 MD5STEP(F4
, b
, c
, d
, a
, in
[5] + 0xfc93a039, 21);
1255 MD5STEP(F4
, a
, b
, c
, d
, in
[12] + 0x655b59c3, 6);
1256 MD5STEP(F4
, d
, a
, b
, c
, in
[3] + 0x8f0ccc92, 10);
1257 MD5STEP(F4
, c
, d
, a
, b
, in
[10] + 0xffeff47d, 15);
1258 MD5STEP(F4
, b
, c
, d
, a
, in
[1] + 0x85845dd1, 21);
1259 MD5STEP(F4
, a
, b
, c
, d
, in
[8] + 0x6fa87e4f, 6);
1260 MD5STEP(F4
, d
, a
, b
, c
, in
[15] + 0xfe2ce6e0, 10);
1261 MD5STEP(F4
, c
, d
, a
, b
, in
[6] + 0xa3014314, 15);
1262 MD5STEP(F4
, b
, c
, d
, a
, in
[13] + 0x4e0811a1, 21);
1263 MD5STEP(F4
, a
, b
, c
, d
, in
[4] + 0xf7537e82, 6);
1264 MD5STEP(F4
, d
, a
, b
, c
, in
[11] + 0xbd3af235, 10);
1265 MD5STEP(F4
, c
, d
, a
, b
, in
[2] + 0x2ad7d2bb, 15);
1266 MD5STEP(F4
, b
, c
, d
, a
, in
[9] + 0xeb86d391, 21);
1274 #include "fcaliastail.h"