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>
49 static void MD5Init(struct MD5Context
*ctx
);
50 static void MD5Update(struct MD5Context
*ctx
, const unsigned char *buf
, unsigned len
);
51 static void MD5Final(unsigned char digest
[16], struct MD5Context
*ctx
);
52 static void MD5Transform(FcChar32 buf
[4], FcChar32 in
[16]);
54 #define CACHEBASE_LEN (1 + 32 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX))
56 static const char bin2hex
[] = { '0', '1', '2', '3',
62 FcDirCacheBasename (const FcChar8
* dir
, FcChar8 cache_base
[CACHEBASE_LEN
])
64 unsigned char hash
[16];
67 struct MD5Context ctx
;
70 MD5Update (&ctx
, (const unsigned char *)dir
, strlen ((const char *) dir
));
72 MD5Final (hash
, &ctx
);
75 hex_hash
= cache_base
+ 1;
76 for (cnt
= 0; cnt
< 16; ++cnt
)
78 hex_hash
[2*cnt
] = bin2hex
[hash
[cnt
] >> 4];
79 hex_hash
[2*cnt
+1] = bin2hex
[hash
[cnt
] & 0xf];
82 strcat ((char *) cache_base
, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX
);
88 FcDirCacheUnlink (const FcChar8
*dir
, FcConfig
*config
)
90 FcChar8
*cache_hashed
= NULL
;
91 FcChar8 cache_base
[CACHEBASE_LEN
];
95 FcDirCacheBasename (dir
, cache_base
);
97 list
= FcStrListCreate (config
->cacheDirs
);
101 while ((cache_dir
= FcStrListNext (list
)))
103 cache_hashed
= FcStrPlus (cache_dir
, cache_base
);
106 (void) unlink ((char *) cache_hashed
);
107 FcStrFree (cache_hashed
);
109 FcStrListDone (list
);
110 /* return FcFalse if something went wrong */
117 FcDirCacheOpenFile (const FcChar8
*cache_file
, struct stat
*file_stat
)
121 fd
= open((char *) cache_file
, O_RDONLY
| O_BINARY
);
124 if (fstat (fd
, file_stat
) < 0)
133 * Look for a cache file for the specified dir. Attempt
134 * to use each one we find, stopping when the callback
138 FcDirCacheProcess (FcConfig
*config
, const FcChar8
*dir
,
139 FcBool (*callback
) (int fd
, struct stat
*fd_stat
,
140 struct stat
*dir_stat
, void *closure
),
141 void *closure
, FcChar8
**cache_file_ret
)
144 FcChar8 cache_base
[CACHEBASE_LEN
];
147 struct stat file_stat
, dir_stat
;
148 FcBool ret
= FcFalse
;
150 if (stat ((char *) dir
, &dir_stat
) < 0)
153 FcDirCacheBasename (dir
, cache_base
);
155 list
= FcStrListCreate (config
->cacheDirs
);
159 while ((cache_dir
= FcStrListNext (list
)))
161 FcChar8
*cache_hashed
= FcStrPlus (cache_dir
, cache_base
);
164 fd
= FcDirCacheOpenFile (cache_hashed
, &file_stat
);
166 ret
= (*callback
) (fd
, &file_stat
, &dir_stat
, closure
);
171 *cache_file_ret
= cache_hashed
;
173 FcStrFree (cache_hashed
);
177 FcStrFree (cache_hashed
);
179 FcStrListDone (list
);
184 #define FC_CACHE_MIN_MMAP 1024
187 * Skip list element, make sure the 'next' pointer is the last thing
188 * in the structure, it will be allocated large enough to hold all
189 * of the necessary pointers
192 typedef struct _FcCacheSkip FcCacheSkip
;
194 struct _FcCacheSkip
{
201 FcCacheSkip
*next
[1];
205 * The head of the skip list; pointers for every possible level
206 * in the skip list, plus the largest level in the list
209 #define FC_CACHE_MAX_LEVEL 16
211 static FcCacheSkip
*fcCacheChains
[FC_CACHE_MAX_LEVEL
];
212 static int fcCacheMaxLevel
;
215 # define FcRandom() random()
218 # define FcRandom() lrand48()
221 # define FcRandom() rand()
226 * Generate a random level number, distributed
227 * so that each level is 1/4 as likely as the one before
229 * Note that level numbers run 1 <= level <= MAX_LEVEL
234 /* tricky bit -- each bit is '1' 75% of the time */
235 long int bits
= FcRandom () | FcRandom ();
238 while (++level
< FC_CACHE_MAX_LEVEL
)
248 * Insert cache into the list
251 FcCacheInsert (FcCache
*cache
, struct stat
*cache_stat
)
253 FcCacheSkip
**update
[FC_CACHE_MAX_LEVEL
];
254 FcCacheSkip
*s
, **next
;
258 * Find links along each chain
260 next
= fcCacheChains
;
261 for (i
= fcCacheMaxLevel
; --i
>= 0; )
263 for (; (s
= next
[i
]); next
= s
->next
)
264 if (s
->cache
> cache
)
266 update
[i
] = &next
[i
];
270 * Create new list element
272 level
= random_level ();
273 if (level
> fcCacheMaxLevel
)
275 level
= fcCacheMaxLevel
+ 1;
276 update
[fcCacheMaxLevel
] = &fcCacheChains
[fcCacheMaxLevel
];
277 fcCacheMaxLevel
= level
;
280 s
= malloc (sizeof (FcCacheSkip
) + (level
- 1) * sizeof (FcCacheSkip
*));
285 s
->size
= cache
->size
;
289 s
->cache_dev
= cache_stat
->st_dev
;
290 s
->cache_ino
= cache_stat
->st_ino
;
291 s
->cache_mtime
= cache_stat
->st_mtime
;
301 * Insert into all fcCacheChains
303 for (i
= 0; i
< level
; i
++)
305 s
->next
[i
] = *update
[i
];
312 FcCacheFindByAddr (void *object
)
315 FcCacheSkip
**next
= fcCacheChains
;
319 * Walk chain pointers one level at a time
321 for (i
= fcCacheMaxLevel
; --i
>= 0;)
322 while (next
[i
] && (char *) object
>= ((char *) next
[i
]->cache
+ next
[i
]->size
))
323 next
= next
[i
]->next
;
328 if (s
&& (char *) object
< ((char *) s
->cache
+ s
->size
))
334 FcCacheRemove (FcCache
*cache
)
336 FcCacheSkip
**update
[FC_CACHE_MAX_LEVEL
];
337 FcCacheSkip
*s
, **next
;
341 * Find links along each chain
343 next
= fcCacheChains
;
344 for (i
= fcCacheMaxLevel
; --i
>= 0; )
346 for (; (s
= next
[i
]); next
= s
->next
)
347 if (s
->cache
>= cache
)
349 update
[i
] = &next
[i
];
352 for (i
= 0; i
< fcCacheMaxLevel
&& *update
[i
] == s
; i
++)
353 *update
[i
] = s
->next
[i
];
354 while (fcCacheMaxLevel
> 0 && fcCacheChains
[fcCacheMaxLevel
- 1] == NULL
)
360 FcCacheFindByStat (struct stat
*cache_stat
)
364 for (s
= fcCacheChains
[0]; s
; s
= s
->next
[0])
365 if (s
->cache_dev
== cache_stat
->st_dev
&&
366 s
->cache_ino
== cache_stat
->st_ino
&&
367 s
->cache_mtime
== cache_stat
->st_mtime
)
376 FcDirCacheDispose (FcCache
*cache
)
378 switch (cache
->magic
) {
379 case FC_CACHE_MAGIC_ALLOC
:
382 case FC_CACHE_MAGIC_MMAP
:
383 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
384 munmap (cache
, cache
->size
);
385 #elif defined(_WIN32)
386 UnmapViewOfFile (cache
);
390 FcCacheRemove (cache
);
394 FcCacheObjectReference (void *object
)
396 FcCacheSkip
*skip
= FcCacheFindByAddr (object
);
403 FcCacheObjectDereference (void *object
)
405 FcCacheSkip
*skip
= FcCacheFindByAddr (object
);
411 FcDirCacheDispose (skip
->cache
);
420 for (i
= 0; i
< FC_CACHE_MAX_LEVEL
; i
++)
421 assert (fcCacheChains
[i
] == NULL
);
422 assert (fcCacheMaxLevel
== 0);
426 FcCacheTimeValid (FcCache
*cache
, struct stat
*dir_stat
)
428 struct stat dir_static
;
432 if (stat ((const char *) FcCacheDir (cache
), &dir_static
) < 0)
434 dir_stat
= &dir_static
;
436 if (FcDebug () & FC_DBG_CACHE
)
437 printf ("FcCacheTimeValid dir \"%s\" cache time %d dir time %d\n",
438 FcCacheDir (cache
), cache
->mtime
, (int) dir_stat
->st_mtime
);
439 return cache
->mtime
== (int) dir_stat
->st_mtime
;
443 * Map a cache file into memory
446 FcDirCacheMapFd (int fd
, struct stat
*fd_stat
, struct stat
*dir_stat
)
449 FcBool allocated
= FcFalse
;
451 if (fd_stat
->st_size
< sizeof (FcCache
))
453 cache
= FcCacheFindByStat (fd_stat
);
457 * Lage cache files are mmap'ed, smaller cache files are read. This
458 * balances the system cost of mmap against per-process memory usage.
460 if (fd_stat
->st_size
>= FC_CACHE_MIN_MMAP
)
462 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
463 cache
= mmap (0, fd_stat
->st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
464 #elif defined(_WIN32)
469 hFileMap
= CreateFileMapping((HANDLE
) _get_osfhandle(fd
), NULL
,
470 PAGE_READONLY
, 0, 0, NULL
);
471 if (hFileMap
!= NULL
)
473 cache
= MapViewOfFile (hFileMap
, FILE_MAP_READ
, 0, 0,
475 CloseHandle (hFileMap
);
482 cache
= malloc (fd_stat
->st_size
);
486 if (read (fd
, cache
, fd_stat
->st_size
) != fd_stat
->st_size
)
493 if (cache
->magic
!= FC_CACHE_MAGIC_MMAP
||
494 cache
->version
< FC_CACHE_CONTENT_VERSION
||
495 cache
->size
!= fd_stat
->st_size
||
496 !FcCacheTimeValid (cache
, dir_stat
) ||
497 !FcCacheInsert (cache
, fd_stat
))
503 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
504 munmap (cache
, fd_stat
->st_size
);
505 #elif defined(_WIN32)
506 UnmapViewOfFile (cache
);
512 /* Mark allocated caches so they're freed rather than unmapped */
514 cache
->magic
= FC_CACHE_MAGIC_ALLOC
;
520 FcDirCacheReference (FcCache
*cache
, int nref
)
522 FcCacheSkip
*skip
= FcCacheFindByAddr (cache
);
529 FcDirCacheUnload (FcCache
*cache
)
531 FcCacheObjectDereference (cache
);
535 FcDirCacheMapHelper (int fd
, struct stat
*fd_stat
, struct stat
*dir_stat
, void *closure
)
537 FcCache
*cache
= FcDirCacheMapFd (fd
, fd_stat
, dir_stat
);
541 *((FcCache
**) closure
) = cache
;
546 FcDirCacheLoad (const FcChar8
*dir
, FcConfig
*config
, FcChar8
**cache_file
)
548 FcCache
*cache
= NULL
;
550 if (!FcDirCacheProcess (config
, dir
,
558 FcDirCacheLoadFile (const FcChar8
*cache_file
, struct stat
*file_stat
)
562 struct stat my_file_stat
;
565 file_stat
= &my_file_stat
;
566 fd
= FcDirCacheOpenFile (cache_file
, file_stat
);
569 cache
= FcDirCacheMapFd (fd
, file_stat
, NULL
);
575 * Validate a cache file by reading the header and checking
576 * the magic number and the size field
579 FcDirCacheValidateHelper (int fd
, struct stat
*fd_stat
, struct stat
*dir_stat
, void *closure
)
584 if (read (fd
, &c
, sizeof (FcCache
)) != sizeof (FcCache
))
586 else if (c
.magic
!= FC_CACHE_MAGIC_MMAP
)
588 else if (c
.version
< FC_CACHE_CONTENT_VERSION
)
590 else if (fd_stat
->st_size
!= c
.size
)
592 else if (c
.mtime
!= (int) dir_stat
->st_mtime
)
598 FcDirCacheValidConfig (const FcChar8
*dir
, FcConfig
*config
)
600 return FcDirCacheProcess (config
, dir
,
601 FcDirCacheValidateHelper
,
606 FcDirCacheValid (const FcChar8
*dir
)
610 config
= FcConfigGetCurrent ();
614 return FcDirCacheValidConfig (dir
, config
);
618 * Build a cache structure from the given contents
621 FcDirCacheBuild (FcFontSet
*set
, const FcChar8
*dir
, struct stat
*dir_stat
, FcStrSet
*dirs
)
623 FcSerialize
*serialize
= FcSerializeCreate ();
626 intptr_t cache_offset
;
627 intptr_t dirs_offset
;
628 FcChar8
*dir_serialize
;
629 intptr_t *dirs_serialize
;
630 FcFontSet
*set_serialize
;
635 * Space for cache structure
637 cache_offset
= FcSerializeReserve (serialize
, sizeof (FcCache
));
641 if (!FcStrSerializeAlloc (serialize
, dir
))
646 dirs_offset
= FcSerializeAlloc (serialize
, dirs
, dirs
->num
* sizeof (FcChar8
*));
647 for (i
= 0; i
< dirs
->num
; i
++)
648 if (!FcStrSerializeAlloc (serialize
, dirs
->strs
[i
]))
654 if (!FcFontSetSerializeAlloc (serialize
, set
))
657 /* Serialize layout complete. Now allocate space and fill it */
658 cache
= malloc (serialize
->size
);
661 /* shut up valgrind */
662 memset (cache
, 0, serialize
->size
);
664 serialize
->linear
= cache
;
666 cache
->magic
= FC_CACHE_MAGIC_ALLOC
;
667 cache
->version
= FC_CACHE_CONTENT_VERSION
;
668 cache
->size
= serialize
->size
;
669 cache
->mtime
= (int) dir_stat
->st_mtime
;
672 * Serialize directory name
674 dir_serialize
= FcStrSerialize (serialize
, dir
);
677 cache
->dir
= FcPtrToOffset (cache
, dir_serialize
);
682 dirs_serialize
= FcSerializePtr (serialize
, dirs
);
685 cache
->dirs
= FcPtrToOffset (cache
, dirs_serialize
);
686 cache
->dirs_count
= dirs
->num
;
687 for (i
= 0; i
< dirs
->num
; i
++)
689 FcChar8
*d_serialize
= FcStrSerialize (serialize
, dirs
->strs
[i
]);
692 dirs_serialize
[i
] = FcPtrToOffset (dirs_serialize
, d_serialize
);
698 set_serialize
= FcFontSetSerialize (serialize
, set
);
701 cache
->set
= FcPtrToOffset (cache
, set_serialize
);
703 FcSerializeDestroy (serialize
);
705 FcCacheInsert (cache
, NULL
);
712 FcSerializeDestroy (serialize
);
718 #define mkdir(path,mode) _mkdir(path)
722 FcMakeDirectory (const FcChar8
*dir
)
727 if (strlen ((char *) dir
) == 0)
730 parent
= FcStrDirname (dir
);
733 if (access ((char *) parent
, F_OK
) == 0)
734 ret
= mkdir ((char *) dir
, 0777) == 0;
735 else if (access ((char *) parent
, F_OK
) == -1)
736 ret
= FcMakeDirectory (parent
) && (mkdir ((char *) dir
, 0777) == 0);
743 /* write serialized state to the cache file */
745 FcDirCacheWrite (FcCache
*cache
, FcConfig
*config
)
747 FcChar8
*dir
= FcCacheDir (cache
);
748 FcChar8 cache_base
[CACHEBASE_LEN
];
749 FcChar8
*cache_hashed
;
753 FcChar8
*cache_dir
= NULL
;
759 * Write it to the first directory in the list which is writable
762 list
= FcStrListCreate (config
->cacheDirs
);
765 while ((test_dir
= FcStrListNext (list
))) {
766 if (access ((char *) test_dir
, W_OK
) == 0)
768 cache_dir
= test_dir
;
774 * If the directory doesn't exist, try to create it
776 if (access ((char *) test_dir
, F_OK
) == -1) {
777 if (FcMakeDirectory (test_dir
))
779 cache_dir
= test_dir
;
785 FcStrListDone (list
);
789 FcDirCacheBasename (dir
, cache_base
);
790 cache_hashed
= FcStrPlus (cache_dir
, cache_base
);
794 if (FcDebug () & FC_DBG_CACHE
)
795 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
798 atomic
= FcAtomicCreate ((FcChar8
*)cache_hashed
);
802 if (!FcAtomicLock (atomic
))
805 fd
= open((char *)FcAtomicNewFile (atomic
), O_RDWR
| O_CREAT
| O_BINARY
, 0666);
809 /* Temporarily switch magic to MMAP while writing to file */
810 magic
= cache
->magic
;
811 if (magic
!= FC_CACHE_MAGIC_MMAP
)
812 cache
->magic
= FC_CACHE_MAGIC_MMAP
;
815 * Write cache contents to file
817 written
= write (fd
, cache
, cache
->size
);
819 /* Switch magic back */
820 if (magic
!= FC_CACHE_MAGIC_MMAP
)
821 cache
->magic
= magic
;
823 if (written
!= cache
->size
)
825 perror ("write cache");
830 if (!FcAtomicReplaceOrig(atomic
))
832 FcStrFree (cache_hashed
);
833 FcAtomicUnlock (atomic
);
834 FcAtomicDestroy (atomic
);
840 FcAtomicUnlock (atomic
);
842 FcAtomicDestroy (atomic
);
844 FcStrFree (cache_hashed
);
849 * Hokey little macro trick to permit the definitions of C functions
850 * with the same name as CPP macros
853 #define args2(x,y) (x,y)
856 FcCacheDir
args1(const FcCache
*c
)
858 return FcCacheDir (c
);
862 FcCacheCopySet
args1(const FcCache
*c
)
864 FcFontSet
*old
= FcCacheSet (c
);
865 FcFontSet
*new = FcFontSetCreate ();
870 for (i
= 0; i
< old
->nfont
; i
++)
872 FcPattern
*font
= FcFontSetFont (old
, i
);
874 FcPatternReference (font
);
875 if (!FcFontSetAdd (new, font
))
877 FcFontSetDestroy (new);
885 FcCacheSubdir
args2(const FcCache
*c
, int i
)
887 return FcCacheSubdir (c
, i
);
891 FcCacheNumSubdir
args1(const FcCache
*c
)
893 return c
->dirs_count
;
897 FcCacheNumFont
args1(const FcCache
*c
)
899 return FcCacheSet(c
)->nfont
;
903 * This code implements the MD5 message-digest algorithm.
904 * The algorithm is due to Ron Rivest. This code was
905 * written by Colin Plumb in 1993, no copyright is claimed.
906 * This code is in the public domain; do with it what you wish.
908 * Equivalent code is available from RSA Data Security, Inc.
909 * This code has been tested against that, and is equivalent,
910 * except that you don't need to include two pages of legalese
913 * To compute the message digest of a chunk of bytes, declare an
914 * MD5Context structure, pass it to MD5Init, call MD5Update as
915 * needed on buffers full of bytes, and then call MD5Final, which
916 * will fill a supplied 16-byte array with the digest.
920 #define byteReverse(buf, len) /* Nothing */
923 * Note: this code is harmless on little-endian machines.
925 void byteReverse(unsigned char *buf
, unsigned longs
)
929 t
= (FcChar32
) ((unsigned) buf
[3] << 8 | buf
[2]) << 16 |
930 ((unsigned) buf
[1] << 8 | buf
[0]);
931 *(FcChar32
*) buf
= t
;
938 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
939 * initialization constants.
941 static void MD5Init(struct MD5Context
*ctx
)
943 ctx
->buf
[0] = 0x67452301;
944 ctx
->buf
[1] = 0xefcdab89;
945 ctx
->buf
[2] = 0x98badcfe;
946 ctx
->buf
[3] = 0x10325476;
953 * Update context to reflect the concatenation of another buffer full
956 static void MD5Update(struct MD5Context
*ctx
, const unsigned char *buf
, unsigned len
)
960 /* Update bitcount */
963 if ((ctx
->bits
[0] = t
+ ((FcChar32
) len
<< 3)) < t
)
964 ctx
->bits
[1]++; /* Carry from low to high */
965 ctx
->bits
[1] += len
>> 29;
967 t
= (t
>> 3) & 0x3f; /* Bytes already in shsInfo->data */
969 /* Handle any leading odd-sized chunks */
972 unsigned char *p
= (unsigned char *) ctx
->in
+ t
;
980 byteReverse(ctx
->in
, 16);
981 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
985 /* Process data in 64-byte chunks */
988 memcpy(ctx
->in
, buf
, 64);
989 byteReverse(ctx
->in
, 16);
990 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
995 /* Handle any remaining bytes of data. */
997 memcpy(ctx
->in
, buf
, len
);
1001 * Final wrapup - pad to 64-byte boundary with the bit pattern
1002 * 1 0* (64-bit count of bits processed, MSB-first)
1004 static void MD5Final(unsigned char digest
[16], struct MD5Context
*ctx
)
1009 /* Compute number of bytes mod 64 */
1010 count
= (ctx
->bits
[0] >> 3) & 0x3F;
1012 /* Set the first char of padding to 0x80. This is safe since there is
1013 always at least one byte free */
1014 p
= ctx
->in
+ count
;
1017 /* Bytes of padding needed to make 64 bytes */
1018 count
= 64 - 1 - count
;
1020 /* Pad out to 56 mod 64 */
1022 /* Two lots of padding: Pad the first block to 64 bytes */
1023 memset(p
, 0, count
);
1024 byteReverse(ctx
->in
, 16);
1025 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1027 /* Now fill the next block with 56 bytes */
1028 memset(ctx
->in
, 0, 56);
1030 /* Pad block to 56 bytes */
1031 memset(p
, 0, count
- 8);
1033 byteReverse(ctx
->in
, 14);
1035 /* Append length in bits and transform */
1036 ((FcChar32
*) ctx
->in
)[14] = ctx
->bits
[0];
1037 ((FcChar32
*) ctx
->in
)[15] = ctx
->bits
[1];
1039 MD5Transform(ctx
->buf
, (FcChar32
*) ctx
->in
);
1040 byteReverse((unsigned char *) ctx
->buf
, 4);
1041 memcpy(digest
, ctx
->buf
, 16);
1042 memset(ctx
, 0, sizeof(ctx
)); /* In case it's sensitive */
1046 /* The four core functions - F1 is optimized somewhat */
1048 /* #define F1(x, y, z) (x & y | ~x & z) */
1049 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1050 #define F2(x, y, z) F1(z, x, y)
1051 #define F3(x, y, z) (x ^ y ^ z)
1052 #define F4(x, y, z) (y ^ (x | ~z))
1054 /* This is the central step in the MD5 algorithm. */
1055 #define MD5STEP(f, w, x, y, z, data, s) \
1056 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1059 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1060 * reflect the addition of 16 longwords of new data. MD5Update blocks
1061 * the data and converts bytes into longwords for this routine.
1063 static void MD5Transform(FcChar32 buf
[4], FcChar32 in
[16])
1065 register FcChar32 a
, b
, c
, d
;
1072 MD5STEP(F1
, a
, b
, c
, d
, in
[0] + 0xd76aa478, 7);
1073 MD5STEP(F1
, d
, a
, b
, c
, in
[1] + 0xe8c7b756, 12);
1074 MD5STEP(F1
, c
, d
, a
, b
, in
[2] + 0x242070db, 17);
1075 MD5STEP(F1
, b
, c
, d
, a
, in
[3] + 0xc1bdceee, 22);
1076 MD5STEP(F1
, a
, b
, c
, d
, in
[4] + 0xf57c0faf, 7);
1077 MD5STEP(F1
, d
, a
, b
, c
, in
[5] + 0x4787c62a, 12);
1078 MD5STEP(F1
, c
, d
, a
, b
, in
[6] + 0xa8304613, 17);
1079 MD5STEP(F1
, b
, c
, d
, a
, in
[7] + 0xfd469501, 22);
1080 MD5STEP(F1
, a
, b
, c
, d
, in
[8] + 0x698098d8, 7);
1081 MD5STEP(F1
, d
, a
, b
, c
, in
[9] + 0x8b44f7af, 12);
1082 MD5STEP(F1
, c
, d
, a
, b
, in
[10] + 0xffff5bb1, 17);
1083 MD5STEP(F1
, b
, c
, d
, a
, in
[11] + 0x895cd7be, 22);
1084 MD5STEP(F1
, a
, b
, c
, d
, in
[12] + 0x6b901122, 7);
1085 MD5STEP(F1
, d
, a
, b
, c
, in
[13] + 0xfd987193, 12);
1086 MD5STEP(F1
, c
, d
, a
, b
, in
[14] + 0xa679438e, 17);
1087 MD5STEP(F1
, b
, c
, d
, a
, in
[15] + 0x49b40821, 22);
1089 MD5STEP(F2
, a
, b
, c
, d
, in
[1] + 0xf61e2562, 5);
1090 MD5STEP(F2
, d
, a
, b
, c
, in
[6] + 0xc040b340, 9);
1091 MD5STEP(F2
, c
, d
, a
, b
, in
[11] + 0x265e5a51, 14);
1092 MD5STEP(F2
, b
, c
, d
, a
, in
[0] + 0xe9b6c7aa, 20);
1093 MD5STEP(F2
, a
, b
, c
, d
, in
[5] + 0xd62f105d, 5);
1094 MD5STEP(F2
, d
, a
, b
, c
, in
[10] + 0x02441453, 9);
1095 MD5STEP(F2
, c
, d
, a
, b
, in
[15] + 0xd8a1e681, 14);
1096 MD5STEP(F2
, b
, c
, d
, a
, in
[4] + 0xe7d3fbc8, 20);
1097 MD5STEP(F2
, a
, b
, c
, d
, in
[9] + 0x21e1cde6, 5);
1098 MD5STEP(F2
, d
, a
, b
, c
, in
[14] + 0xc33707d6, 9);
1099 MD5STEP(F2
, c
, d
, a
, b
, in
[3] + 0xf4d50d87, 14);
1100 MD5STEP(F2
, b
, c
, d
, a
, in
[8] + 0x455a14ed, 20);
1101 MD5STEP(F2
, a
, b
, c
, d
, in
[13] + 0xa9e3e905, 5);
1102 MD5STEP(F2
, d
, a
, b
, c
, in
[2] + 0xfcefa3f8, 9);
1103 MD5STEP(F2
, c
, d
, a
, b
, in
[7] + 0x676f02d9, 14);
1104 MD5STEP(F2
, b
, c
, d
, a
, in
[12] + 0x8d2a4c8a, 20);
1106 MD5STEP(F3
, a
, b
, c
, d
, in
[5] + 0xfffa3942, 4);
1107 MD5STEP(F3
, d
, a
, b
, c
, in
[8] + 0x8771f681, 11);
1108 MD5STEP(F3
, c
, d
, a
, b
, in
[11] + 0x6d9d6122, 16);
1109 MD5STEP(F3
, b
, c
, d
, a
, in
[14] + 0xfde5380c, 23);
1110 MD5STEP(F3
, a
, b
, c
, d
, in
[1] + 0xa4beea44, 4);
1111 MD5STEP(F3
, d
, a
, b
, c
, in
[4] + 0x4bdecfa9, 11);
1112 MD5STEP(F3
, c
, d
, a
, b
, in
[7] + 0xf6bb4b60, 16);
1113 MD5STEP(F3
, b
, c
, d
, a
, in
[10] + 0xbebfbc70, 23);
1114 MD5STEP(F3
, a
, b
, c
, d
, in
[13] + 0x289b7ec6, 4);
1115 MD5STEP(F3
, d
, a
, b
, c
, in
[0] + 0xeaa127fa, 11);
1116 MD5STEP(F3
, c
, d
, a
, b
, in
[3] + 0xd4ef3085, 16);
1117 MD5STEP(F3
, b
, c
, d
, a
, in
[6] + 0x04881d05, 23);
1118 MD5STEP(F3
, a
, b
, c
, d
, in
[9] + 0xd9d4d039, 4);
1119 MD5STEP(F3
, d
, a
, b
, c
, in
[12] + 0xe6db99e5, 11);
1120 MD5STEP(F3
, c
, d
, a
, b
, in
[15] + 0x1fa27cf8, 16);
1121 MD5STEP(F3
, b
, c
, d
, a
, in
[2] + 0xc4ac5665, 23);
1123 MD5STEP(F4
, a
, b
, c
, d
, in
[0] + 0xf4292244, 6);
1124 MD5STEP(F4
, d
, a
, b
, c
, in
[7] + 0x432aff97, 10);
1125 MD5STEP(F4
, c
, d
, a
, b
, in
[14] + 0xab9423a7, 15);
1126 MD5STEP(F4
, b
, c
, d
, a
, in
[5] + 0xfc93a039, 21);
1127 MD5STEP(F4
, a
, b
, c
, d
, in
[12] + 0x655b59c3, 6);
1128 MD5STEP(F4
, d
, a
, b
, c
, in
[3] + 0x8f0ccc92, 10);
1129 MD5STEP(F4
, c
, d
, a
, b
, in
[10] + 0xffeff47d, 15);
1130 MD5STEP(F4
, b
, c
, d
, a
, in
[1] + 0x85845dd1, 21);
1131 MD5STEP(F4
, a
, b
, c
, d
, in
[8] + 0x6fa87e4f, 6);
1132 MD5STEP(F4
, d
, a
, b
, c
, in
[15] + 0xfe2ce6e0, 10);
1133 MD5STEP(F4
, c
, d
, a
, b
, in
[6] + 0xa3014314, 15);
1134 MD5STEP(F4
, b
, c
, d
, a
, in
[13] + 0x4e0811a1, 21);
1135 MD5STEP(F4
, a
, b
, c
, d
, in
[4] + 0xf7537e82, 6);
1136 MD5STEP(F4
, d
, a
, b
, c
, in
[11] + 0xbd3af235, 10);
1137 MD5STEP(F4
, c
, d
, a
, b
, in
[2] + 0x2ad7d2bb, 15);
1138 MD5STEP(F4
, b
, c
, d
, a
, in
[9] + 0xeb86d391, 21);
1146 #include "fcaliastail.h"