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 Keith Packard not be used in
10 * advertising or publicity pertaining to distribution of the software without
11 * specific, written prior permission. Keith Packard makes no
12 * representations about the suitability of this software for any purpose. It
13 * is provided "as is" without express or implied warranty.
15 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL KEITH PACKARD 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.
25 #include "../fc-arch/fcarch.h"
30 #include <sys/types.h>
32 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
34 # include <sys/mman.h>
36 # define _WIN32_WINNT 0x0500
50 static void MD5Init(struct MD5Context *ctx);
51 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len);
52 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
53 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]);
55 #define CACHEBASE_LEN (1 + 32 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX))
62 typedef long long INT64;
63 #define EPOCH_OFFSET 11644473600ll
65 #define EPOCH_OFFSET 11644473600i64
66 typedef __int64 INT64;
69 /* Workaround for problems in the stat() in the Microsoft C library:
71 * 1) stat() uses FindFirstFile() to get the file
72 * attributes. Unfortunately this API doesn't return correct values
73 * for modification time of a directory until some time after a file
74 * or subdirectory has been added to the directory. (This causes
75 * run-test.sh to fail, for instance.) GetFileAttributesEx() is
76 * better, it returns the updated timestamp right away.
78 * 2) stat() does some strange things related to backward
79 * compatibility with the local time timestamps on FAT volumes and
80 * daylight saving time. This causes problems after the switches
81 * to/from daylight saving time. See
82 * http://bugzilla.gnome.org/show_bug.cgi?id=154968 , especially
83 * comment #30, and http://www.codeproject.com/datetime/dstbugs.asp .
84 * We don't need any of that, FAT and Win9x are as good as dead. So
85 * just use the UTC timestamps from NTFS, converted to the Unix epoch.
89 FcStat (const char *file, struct stat *statb)
91 WIN32_FILE_ATTRIBUTE_DATA wfad;
92 char full_path_name[MAX_PATH];
96 if (!GetFileAttributesEx (file, GetFileExInfoStandard, &wfad))
101 /* Calculate a pseudo inode number as a hash of the full path name.
102 * Call GetLongPathName() to get the spelling of the path name as it
105 rc = GetFullPathName (file, sizeof (full_path_name), full_path_name, &basename);
106 if (rc == 0 || rc > sizeof (full_path_name))
109 rc = GetLongPathName (full_path_name, full_path_name, sizeof (full_path_name));
110 statb->st_ino = FcStringHash (full_path_name);
112 statb->st_mode = _S_IREAD | _S_IWRITE;
113 statb->st_mode |= (statb->st_mode >> 3) | (statb->st_mode >> 6);
115 if (wfad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
116 statb->st_mode |= _S_IFDIR;
118 statb->st_mode |= _S_IFREG;
121 statb->st_uid = statb->st_gid = 0;
124 if (wfad.nFileSizeHigh > 0)
126 statb->st_size = wfad.nFileSizeLow;
128 statb->st_atime = (*(INT64 *)&wfad.ftLastAccessTime)/10000000 - EPOCH_OFFSET;
129 statb->st_mtime = (*(INT64 *)&wfad.ftLastWriteTime)/10000000 - EPOCH_OFFSET;
130 statb->st_ctime = statb->st_mtime;
136 static const char bin2hex[] = { '0', '1', '2', '3',
139 'c', 'd', 'e', 'f' };
142 FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN])
144 unsigned char hash[16];
147 struct MD5Context ctx;
150 MD5Update (&ctx, (const unsigned char *)dir, strlen ((const char *) dir));
152 MD5Final (hash, &ctx);
155 hex_hash = cache_base + 1;
156 for (cnt = 0; cnt < 16; ++cnt)
158 hex_hash[2*cnt ] = bin2hex[hash[cnt] >> 4];
159 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
162 strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX);
168 FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
170 FcChar8 *cache_hashed = NULL;
171 FcChar8 cache_base[CACHEBASE_LEN];
175 FcDirCacheBasename (dir, cache_base);
177 list = FcStrListCreate (config->cacheDirs);
181 while ((cache_dir = FcStrListNext (list)))
183 cache_hashed = FcStrPlus (cache_dir, cache_base);
186 (void) unlink ((char *) cache_hashed);
187 FcStrFree (cache_hashed);
189 FcStrListDone (list);
190 /* return FcFalse if something went wrong */
197 FcDirCacheOpenFile (const FcChar8 *cache_file, struct stat *file_stat)
202 if (FcStat (cache_file, file_stat) < 0)
205 fd = open((char *) cache_file, O_RDONLY | O_BINARY);
209 if (fstat (fd, file_stat) < 0)
219 * Look for a cache file for the specified dir. Attempt
220 * to use each one we find, stopping when the callback
224 FcDirCacheProcess (FcConfig *config, const FcChar8 *dir,
225 FcBool (*callback) (int fd, struct stat *fd_stat,
226 struct stat *dir_stat, void *closure),
227 void *closure, FcChar8 **cache_file_ret)
230 FcChar8 cache_base[CACHEBASE_LEN];
233 struct stat file_stat, dir_stat;
234 FcBool ret = FcFalse;
236 if (FcStat ((char *) dir, &dir_stat) < 0)
239 FcDirCacheBasename (dir, cache_base);
241 list = FcStrListCreate (config->cacheDirs);
245 while ((cache_dir = FcStrListNext (list)))
247 FcChar8 *cache_hashed = FcStrPlus (cache_dir, cache_base);
250 fd = FcDirCacheOpenFile (cache_hashed, &file_stat);
252 ret = (*callback) (fd, &file_stat, &dir_stat, closure);
257 *cache_file_ret = cache_hashed;
259 FcStrFree (cache_hashed);
263 FcStrFree (cache_hashed);
265 FcStrListDone (list);
270 #define FC_CACHE_MIN_MMAP 1024
273 * Skip list element, make sure the 'next' pointer is the last thing
274 * in the structure, it will be allocated large enough to hold all
275 * of the necessary pointers
278 typedef struct _FcCacheSkip FcCacheSkip;
280 struct _FcCacheSkip {
287 FcCacheSkip *next[1];
291 * The head of the skip list; pointers for every possible level
292 * in the skip list, plus the largest level in the list
295 #define FC_CACHE_MAX_LEVEL 16
297 static FcCacheSkip *fcCacheChains[FC_CACHE_MAX_LEVEL];
298 static int fcCacheMaxLevel;
301 # define FcRandom() random()
304 # define FcRandom() lrand48()
307 # define FcRandom() rand()
312 * Generate a random level number, distributed
313 * so that each level is 1/4 as likely as the one before
315 * Note that level numbers run 1 <= level <= MAX_LEVEL
320 /* tricky bit -- each bit is '1' 75% of the time */
321 long int bits = FcRandom () | FcRandom ();
324 while (++level < FC_CACHE_MAX_LEVEL)
334 * Insert cache into the list
337 FcCacheInsert (FcCache *cache, struct stat *cache_stat)
339 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
340 FcCacheSkip *s, **next;
344 * Find links along each chain
346 next = fcCacheChains;
347 for (i = fcCacheMaxLevel; --i >= 0; )
349 for (; (s = next[i]); next = s->next)
350 if (s->cache > cache)
352 update[i] = &next[i];
356 * Create new list element
358 level = random_level ();
359 if (level > fcCacheMaxLevel)
361 level = fcCacheMaxLevel + 1;
362 update[fcCacheMaxLevel] = &fcCacheChains[fcCacheMaxLevel];
363 fcCacheMaxLevel = level;
366 s = malloc (sizeof (FcCacheSkip) + (level - 1) * sizeof (FcCacheSkip *));
371 s->size = cache->size;
375 s->cache_dev = cache_stat->st_dev;
376 s->cache_ino = cache_stat->st_ino;
377 s->cache_mtime = cache_stat->st_mtime;
387 * Insert into all fcCacheChains
389 for (i = 0; i < level; i++)
391 s->next[i] = *update[i];
398 FcCacheFindByAddr (void *object)
401 FcCacheSkip **next = fcCacheChains;
405 * Walk chain pointers one level at a time
407 for (i = fcCacheMaxLevel; --i >= 0;)
408 while (next[i] && (char *) object >= ((char *) next[i]->cache + next[i]->size))
409 next = next[i]->next;
414 if (s && (char *) object < ((char *) s->cache + s->size))
420 FcCacheRemove (FcCache *cache)
422 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
423 FcCacheSkip *s, **next;
427 * Find links along each chain
429 next = fcCacheChains;
430 for (i = fcCacheMaxLevel; --i >= 0; )
432 for (; (s = next[i]); next = s->next)
433 if (s->cache >= cache)
435 update[i] = &next[i];
438 for (i = 0; i < fcCacheMaxLevel && *update[i] == s; i++)
439 *update[i] = s->next[i];
440 while (fcCacheMaxLevel > 0 && fcCacheChains[fcCacheMaxLevel - 1] == NULL)
446 FcCacheFindByStat (struct stat *cache_stat)
450 for (s = fcCacheChains[0]; s; s = s->next[0])
451 if (s->cache_dev == cache_stat->st_dev &&
452 s->cache_ino == cache_stat->st_ino &&
453 s->cache_mtime == cache_stat->st_mtime)
462 FcDirCacheDispose (FcCache *cache)
464 switch (cache->magic) {
465 case FC_CACHE_MAGIC_ALLOC:
468 case FC_CACHE_MAGIC_MMAP:
469 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
470 munmap (cache, cache->size);
471 #elif defined(_WIN32)
472 UnmapViewOfFile (cache);
476 FcCacheRemove (cache);
480 FcCacheObjectReference (void *object)
482 FcCacheSkip *skip = FcCacheFindByAddr (object);
489 FcCacheObjectDereference (void *object)
491 FcCacheSkip *skip = FcCacheFindByAddr (object);
497 FcDirCacheDispose (skip->cache);
506 for (i = 0; i < FC_CACHE_MAX_LEVEL; i++)
507 assert (fcCacheChains[i] == NULL);
508 assert (fcCacheMaxLevel == 0);
512 FcCacheTimeValid (FcCache *cache, struct stat *dir_stat)
514 struct stat dir_static;
518 if (FcStat ((const char *) FcCacheDir (cache), &dir_static) < 0)
520 dir_stat = &dir_static;
522 if (FcDebug () & FC_DBG_CACHE)
523 printf ("FcCacheTimeValid dir \"%s\" cache time %d dir time %d\n",
524 FcCacheDir (cache), cache->mtime, (int) dir_stat->st_mtime);
525 return cache->mtime == (int) dir_stat->st_mtime;
529 * Map a cache file into memory
532 FcDirCacheMapFd (int fd, struct stat *fd_stat, struct stat *dir_stat)
535 FcBool allocated = FcFalse;
537 if (fd_stat->st_size < sizeof (FcCache))
539 cache = FcCacheFindByStat (fd_stat);
542 if (FcCacheTimeValid (cache, dir_stat))
544 FcDirCacheUnload (cache);
549 * Lage cache files are mmap'ed, smaller cache files are read. This
550 * balances the system cost of mmap against per-process memory usage.
552 if (fd_stat->st_size >= FC_CACHE_MIN_MMAP)
554 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
555 cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0);
556 #elif defined(_WIN32)
561 hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL,
562 PAGE_READONLY, 0, 0, NULL);
563 if (hFileMap != NULL)
565 cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0,
567 CloseHandle (hFileMap);
574 cache = malloc (fd_stat->st_size);
578 if (read (fd, cache, fd_stat->st_size) != fd_stat->st_size)
585 if (cache->magic != FC_CACHE_MAGIC_MMAP ||
586 cache->version < FC_CACHE_CONTENT_VERSION ||
587 cache->size != fd_stat->st_size ||
588 !FcCacheTimeValid (cache, dir_stat) ||
589 !FcCacheInsert (cache, fd_stat))
595 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
596 munmap (cache, fd_stat->st_size);
597 #elif defined(_WIN32)
598 UnmapViewOfFile (cache);
604 /* Mark allocated caches so they're freed rather than unmapped */
606 cache->magic = FC_CACHE_MAGIC_ALLOC;
612 FcDirCacheReference (FcCache *cache, int nref)
614 FcCacheSkip *skip = FcCacheFindByAddr (cache);
621 FcDirCacheUnload (FcCache *cache)
623 FcCacheObjectDereference (cache);
627 FcDirCacheMapHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
629 FcCache *cache = FcDirCacheMapFd (fd, fd_stat, dir_stat);
633 *((FcCache **) closure) = cache;
638 FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file)
640 FcCache *cache = NULL;
642 if (!FcDirCacheProcess (config, dir,
650 FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat)
654 struct stat my_file_stat;
657 file_stat = &my_file_stat;
658 fd = FcDirCacheOpenFile (cache_file, file_stat);
661 cache = FcDirCacheMapFd (fd, file_stat, NULL);
667 * Validate a cache file by reading the header and checking
668 * the magic number and the size field
671 FcDirCacheValidateHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
676 if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache))
678 else if (c.magic != FC_CACHE_MAGIC_MMAP)
680 else if (c.version < FC_CACHE_CONTENT_VERSION)
682 else if (fd_stat->st_size != c.size)
684 else if (c.mtime != (int) dir_stat->st_mtime)
690 FcDirCacheValidConfig (const FcChar8 *dir, FcConfig *config)
692 return FcDirCacheProcess (config, dir,
693 FcDirCacheValidateHelper,
698 FcDirCacheValid (const FcChar8 *dir)
702 config = FcConfigGetCurrent ();
706 return FcDirCacheValidConfig (dir, config);
710 * Build a cache structure from the given contents
713 FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcStrSet *dirs)
715 FcSerialize *serialize = FcSerializeCreate ();
718 intptr_t cache_offset;
719 intptr_t dirs_offset;
720 FcChar8 *dir_serialize;
721 intptr_t *dirs_serialize;
722 FcFontSet *set_serialize;
727 * Space for cache structure
729 cache_offset = FcSerializeReserve (serialize, sizeof (FcCache));
733 if (!FcStrSerializeAlloc (serialize, dir))
738 dirs_offset = FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *));
739 for (i = 0; i < dirs->num; i++)
740 if (!FcStrSerializeAlloc (serialize, dirs->strs[i]))
746 if (!FcFontSetSerializeAlloc (serialize, set))
749 /* Serialize layout complete. Now allocate space and fill it */
750 cache = malloc (serialize->size);
753 /* shut up valgrind */
754 memset (cache, 0, serialize->size);
756 serialize->linear = cache;
758 cache->magic = FC_CACHE_MAGIC_ALLOC;
759 cache->version = FC_CACHE_CONTENT_VERSION;
760 cache->size = serialize->size;
761 cache->mtime = (int) dir_stat->st_mtime;
764 * Serialize directory name
766 dir_serialize = FcStrSerialize (serialize, dir);
769 cache->dir = FcPtrToOffset (cache, dir_serialize);
774 dirs_serialize = FcSerializePtr (serialize, dirs);
777 cache->dirs = FcPtrToOffset (cache, dirs_serialize);
778 cache->dirs_count = dirs->num;
779 for (i = 0; i < dirs->num; i++)
781 FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]);
784 dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize);
790 set_serialize = FcFontSetSerialize (serialize, set);
793 cache->set = FcPtrToOffset (cache, set_serialize);
795 FcSerializeDestroy (serialize);
797 FcCacheInsert (cache, NULL);
804 FcSerializeDestroy (serialize);
810 #define mkdir(path,mode) _mkdir(path)
814 FcMakeDirectory (const FcChar8 *dir)
819 if (strlen ((char *) dir) == 0)
822 parent = FcStrDirname (dir);
825 if (access ((char *) parent, F_OK) == 0)
826 ret = mkdir ((char *) dir, 0755) == 0 && chmod ((char *) dir, 0755) == 0;
827 else if (access ((char *) parent, F_OK) == -1)
828 ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0755) == 0) && chmod ((char *) dir, 0755) == 0;
835 /* write serialized state to the cache file */
837 FcDirCacheWrite (FcCache *cache, FcConfig *config)
839 FcChar8 *dir = FcCacheDir (cache);
840 FcChar8 cache_base[CACHEBASE_LEN];
841 FcChar8 *cache_hashed;
845 FcChar8 *cache_dir = NULL;
848 struct stat cache_stat;
853 * Write it to the first directory in the list which is writable
856 list = FcStrListCreate (config->cacheDirs);
859 while ((test_dir = FcStrListNext (list))) {
860 if (access ((char *) test_dir, W_OK|X_OK) == 0)
862 cache_dir = test_dir;
868 * If the directory doesn't exist, try to create it
870 if (access ((char *) test_dir, F_OK) == -1) {
871 if (FcMakeDirectory (test_dir))
873 cache_dir = test_dir;
878 * Otherwise, try making it writable
880 else if (chmod ((char *) test_dir, 0755) == 0)
882 cache_dir = test_dir;
887 FcStrListDone (list);
891 FcDirCacheBasename (dir, cache_base);
892 cache_hashed = FcStrPlus (cache_dir, cache_base);
896 if (FcDebug () & FC_DBG_CACHE)
897 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
900 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
904 if (!FcAtomicLock (atomic))
907 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
911 /* Temporarily switch magic to MMAP while writing to file */
912 magic = cache->magic;
913 if (magic != FC_CACHE_MAGIC_MMAP)
914 cache->magic = FC_CACHE_MAGIC_MMAP;
917 * Write cache contents to file
919 written = write (fd, cache, cache->size);
921 /* Switch magic back */
922 if (magic != FC_CACHE_MAGIC_MMAP)
923 cache->magic = magic;
925 if (written != cache->size)
927 perror ("write cache");
932 if (!FcAtomicReplaceOrig(atomic))
935 /* If the file is small, update the cache chain entry such that the
936 * new cache file is not read again. If it's large, we don't do that
937 * such that we reload it, using mmap, which is shared across processes.
939 if (cache->size < FC_CACHE_MIN_MMAP &&
940 (skip = FcCacheFindByAddr (cache)) &&
941 FcStat (cache_hashed, &cache_stat))
943 skip->cache_dev = cache_stat.st_dev;
944 skip->cache_ino = cache_stat.st_ino;
945 skip->cache_mtime = cache_stat.st_mtime;
948 FcStrFree (cache_hashed);
949 FcAtomicUnlock (atomic);
950 FcAtomicDestroy (atomic);
956 FcAtomicUnlock (atomic);
958 FcAtomicDestroy (atomic);
960 FcStrFree (cache_hashed);
965 * Hokey little macro trick to permit the definitions of C functions
966 * with the same name as CPP macros
969 #define args2(x,y) (x,y)
972 FcCacheDir args1(const FcCache *c)
974 return FcCacheDir (c);
978 FcCacheCopySet args1(const FcCache *c)
980 FcFontSet *old = FcCacheSet (c);
981 FcFontSet *new = FcFontSetCreate ();
986 for (i = 0; i < old->nfont; i++)
988 FcPattern *font = FcFontSetFont (old, i);
990 FcPatternReference (font);
991 if (!FcFontSetAdd (new, font))
993 FcFontSetDestroy (new);
1001 FcCacheSubdir args2(const FcCache *c, int i)
1003 return FcCacheSubdir (c, i);
1007 FcCacheNumSubdir args1(const FcCache *c)
1009 return c->dirs_count;
1013 FcCacheNumFont args1(const FcCache *c)
1015 return FcCacheSet(c)->nfont;
1019 * This code implements the MD5 message-digest algorithm.
1020 * The algorithm is due to Ron Rivest. This code was
1021 * written by Colin Plumb in 1993, no copyright is claimed.
1022 * This code is in the public domain; do with it what you wish.
1024 * Equivalent code is available from RSA Data Security, Inc.
1025 * This code has been tested against that, and is equivalent,
1026 * except that you don't need to include two pages of legalese
1029 * To compute the message digest of a chunk of bytes, declare an
1030 * MD5Context structure, pass it to MD5Init, call MD5Update as
1031 * needed on buffers full of bytes, and then call MD5Final, which
1032 * will fill a supplied 16-byte array with the digest.
1036 #define byteReverse(buf, len) /* Nothing */
1039 * Note: this code is harmless on little-endian machines.
1041 void byteReverse(unsigned char *buf, unsigned longs)
1045 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1046 ((unsigned) buf[1] << 8 | buf[0]);
1047 *(FcChar32 *) buf = t;
1054 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1055 * initialization constants.
1057 static void MD5Init(struct MD5Context *ctx)
1059 ctx->buf[0] = 0x67452301;
1060 ctx->buf[1] = 0xefcdab89;
1061 ctx->buf[2] = 0x98badcfe;
1062 ctx->buf[3] = 0x10325476;
1069 * Update context to reflect the concatenation of another buffer full
1072 static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len)
1076 /* Update bitcount */
1079 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1080 ctx->bits[1]++; /* Carry from low to high */
1081 ctx->bits[1] += len >> 29;
1083 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1085 /* Handle any leading odd-sized chunks */
1088 unsigned char *p = (unsigned char *) ctx->in + t;
1092 memcpy(p, buf, len);
1096 byteReverse(ctx->in, 16);
1097 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1101 /* Process data in 64-byte chunks */
1104 memcpy(ctx->in, buf, 64);
1105 byteReverse(ctx->in, 16);
1106 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1111 /* Handle any remaining bytes of data. */
1113 memcpy(ctx->in, buf, len);
1117 * Final wrapup - pad to 64-byte boundary with the bit pattern
1118 * 1 0* (64-bit count of bits processed, MSB-first)
1120 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1125 /* Compute number of bytes mod 64 */
1126 count = (ctx->bits[0] >> 3) & 0x3F;
1128 /* Set the first char of padding to 0x80. This is safe since there is
1129 always at least one byte free */
1130 p = ctx->in + count;
1133 /* Bytes of padding needed to make 64 bytes */
1134 count = 64 - 1 - count;
1136 /* Pad out to 56 mod 64 */
1138 /* Two lots of padding: Pad the first block to 64 bytes */
1139 memset(p, 0, count);
1140 byteReverse(ctx->in, 16);
1141 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1143 /* Now fill the next block with 56 bytes */
1144 memset(ctx->in, 0, 56);
1146 /* Pad block to 56 bytes */
1147 memset(p, 0, count - 8);
1149 byteReverse(ctx->in, 14);
1151 /* Append length in bits and transform */
1152 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1153 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1155 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1156 byteReverse((unsigned char *) ctx->buf, 4);
1157 memcpy(digest, ctx->buf, 16);
1158 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
1162 /* The four core functions - F1 is optimized somewhat */
1164 /* #define F1(x, y, z) (x & y | ~x & z) */
1165 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1166 #define F2(x, y, z) F1(z, x, y)
1167 #define F3(x, y, z) (x ^ y ^ z)
1168 #define F4(x, y, z) (y ^ (x | ~z))
1170 /* This is the central step in the MD5 algorithm. */
1171 #define MD5STEP(f, w, x, y, z, data, s) \
1172 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1175 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1176 * reflect the addition of 16 longwords of new data. MD5Update blocks
1177 * the data and converts bytes into longwords for this routine.
1179 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1181 register FcChar32 a, b, c, d;
1188 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1189 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1190 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1191 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1192 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1193 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1194 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1195 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1196 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1197 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1198 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1199 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1200 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1201 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1202 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1203 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1205 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1206 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1207 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1208 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1209 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1210 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1211 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1212 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1213 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1214 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1215 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1216 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1217 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1218 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1219 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1220 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1222 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1223 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1224 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1225 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1226 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1227 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1228 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1229 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1230 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1231 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1232 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1233 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1234 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1235 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1236 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1237 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1239 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1240 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1241 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1242 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1243 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1244 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1245 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1246 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1247 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1248 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1249 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1250 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1251 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1252 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1253 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1254 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1262 #include "fcaliastail.h"