]> git.wh0rd.org - fontconfig.git/blob - src/fccache.c
FcCharSetSerialize was using wrong offset for leaves. Make fc-cat work.
[fontconfig.git] / src / fccache.c
1 /*
2 * Copyright © 2000 Keith Packard
3 * Copyright © 2005 Patrick Lam
4 *
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.
14 *
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.
22 */
23
24 #include "fcint.h"
25 #include "../fc-arch/fcarch.h"
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <dirent.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
32 # include <unistd.h>
33 # include <sys/mman.h>
34 #elif defined(_WIN32)
35 # include <windows.h>
36 #endif
37
38 #ifndef O_BINARY
39 #define O_BINARY 0
40 #endif
41
42 static int
43 FcDirCacheOpen (FcConfig *config, const FcChar8 *dir, off_t *size);
44
45 struct MD5Context {
46 FcChar32 buf[4];
47 FcChar32 bits[2];
48 unsigned char in[64];
49 };
50
51 static void MD5Init(struct MD5Context *ctx);
52 static void MD5Update(struct MD5Context *ctx, 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]);
55
56 #define FC_DBG_CACHE_REF 1024
57
58 /* Does not check that the cache has the appropriate arch section. */
59 FcBool
60 FcDirCacheValid (const FcChar8 *dir, FcConfig *config)
61 {
62 int fd;
63
64 fd = FcDirCacheOpen (config, dir, NULL);
65
66 if (fd < 0)
67 return FcFalse;
68 close (fd);
69
70 return FcTrue;
71 }
72
73 #define CACHEBASE_LEN (1 + 32 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX))
74
75 static const char bin2hex[] = { '0', '1', '2', '3',
76 '4', '5', '6', '7',
77 '8', '9', 'a', 'b',
78 'c', 'd', 'e', 'f' };
79
80 static FcChar8 *
81 FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN])
82 {
83 unsigned char hash[16];
84 FcChar8 *hex_hash;
85 int cnt;
86 struct MD5Context ctx;
87
88 MD5Init (&ctx);
89 MD5Update (&ctx, (unsigned char *)dir, strlen ((char *) dir));
90
91 MD5Final (hash, &ctx);
92
93 cache_base[0] = '/';
94 hex_hash = cache_base + 1;
95 for (cnt = 0; cnt < 16; ++cnt)
96 {
97 hex_hash[2*cnt ] = bin2hex[hash[cnt] >> 4];
98 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
99 }
100 hex_hash[2*cnt] = 0;
101 strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX);
102
103 return cache_base;
104 }
105
106 FcBool
107 FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
108 {
109 FcChar8 *cache_hashed = NULL;
110 FcChar8 cache_base[CACHEBASE_LEN];
111 FcStrList *list;
112 FcChar8 *cache_dir;
113
114 FcDirCacheBasename (dir, cache_base);
115
116 list = FcStrListCreate (config->cacheDirs);
117 if (!list)
118 return FcFalse;
119
120 while ((cache_dir = FcStrListNext (list)))
121 {
122 cache_hashed = FcStrPlus (cache_dir, cache_base);
123 if (!cache_hashed)
124 break;
125 (void) unlink ((char *) cache_hashed);
126 }
127 FcStrListDone (list);
128 /* return FcFalse if something went wrong */
129 if (cache_dir)
130 return FcFalse;
131 return FcTrue;
132 }
133
134 static int
135 FcCacheReadDirs (FcConfig * config,
136 FcStrList *list, FcFontSet * set, FcStrSet *processed_dirs)
137 {
138 int ret = 0;
139 FcChar8 *dir;
140 FcStrSet *subdirs;
141 FcStrList *sublist;
142
143 /*
144 * Read in the results from 'list'.
145 */
146 while ((dir = FcStrListNext (list)))
147 {
148 if (!FcConfigAcceptFilename (config, dir))
149 continue;
150
151 /* Skip this directory if already updated
152 * to avoid the looped directories via symlinks
153 * Clearly a dir not in fonts.conf shouldn't be globally cached.
154 */
155
156 if (FcStrSetMember (processed_dirs, dir))
157 continue;
158 if (!FcStrSetAdd (processed_dirs, dir))
159 continue;
160
161 subdirs = FcStrSetCreate ();
162 if (!subdirs)
163 {
164 fprintf (stderr, "Can't create directory set\n");
165 ret++;
166 continue;
167 }
168
169 FcDirScanConfig (set, subdirs,
170 config->blanks, dir, FcFalse, config);
171
172 sublist = FcStrListCreate (subdirs);
173 FcStrSetDestroy (subdirs);
174 if (!sublist)
175 {
176 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
177 ret++;
178 continue;
179 }
180 ret += FcCacheReadDirs (config, sublist, set, processed_dirs);
181 }
182 FcStrListDone (list);
183 return ret;
184 }
185
186 FcFontSet *
187 FcCacheRead (FcConfig *config)
188 {
189 FcFontSet *s = FcFontSetCreate();
190 FcStrSet *processed_dirs;
191
192 if (!s)
193 return 0;
194
195 processed_dirs = FcStrSetCreate();
196 if (!processed_dirs)
197 goto bail;
198
199 if (FcCacheReadDirs (config, FcConfigGetConfigDirs (config), s, processed_dirs))
200 goto bail1;
201
202 FcStrSetDestroy (processed_dirs);
203 return s;
204
205 bail1:
206 FcStrSetDestroy (processed_dirs);
207 bail:
208 FcFontSetDestroy (s);
209 return 0;
210 }
211
212 /* Opens the cache file for 'dir' for reading.
213 * This searches the list of cache dirs for the relevant cache file,
214 * returning the first one found.
215 */
216 static int
217 FcDirCacheOpen (FcConfig *config, const FcChar8 *dir, off_t *size)
218 {
219 int fd = -1;
220 FcChar8 cache_base[CACHEBASE_LEN];
221 FcStrList *list;
222 FcChar8 *cache_dir;
223 struct stat file_stat, dir_stat;
224
225 if (stat ((char *) dir, &dir_stat) < 0)
226 return -1;
227
228 FcDirCacheBasename (dir, cache_base);
229
230 list = FcStrListCreate (config->cacheDirs);
231 if (!list)
232 return -1;
233
234 while ((cache_dir = FcStrListNext (list)))
235 {
236 FcChar8 *cache_hashed = FcStrPlus (cache_dir, cache_base);
237 if (!cache_hashed)
238 break;
239 fd = open((char *) cache_hashed, O_RDONLY | O_BINARY);
240 FcStrFree (cache_hashed);
241 if (fd >= 0) {
242 if (fstat (fd, &file_stat) >= 0 &&
243 dir_stat.st_mtime <= file_stat.st_mtime)
244 {
245 if (size)
246 *size = file_stat.st_size;
247 break;
248 }
249 close (fd);
250 fd = -1;
251 }
252 }
253 FcStrListDone (list);
254
255 return fd;
256 }
257
258 void
259 FcDirCacheUnmap (FcCache *cache)
260 {
261 if (cache->magic == FC_CACHE_MAGIC_COPY)
262 {
263 free (cache);
264 return;
265 }
266 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
267 munmap (cache, cache->size);
268 #elif defined(_WIN32)
269 UnmapViewOfFile (cache);
270 #endif
271 }
272
273 /* read serialized state from the cache file */
274 FcCache *
275 FcDirCacheMap (int fd, off_t size)
276 {
277 FcCache *cache;
278 FcBool allocated = FcFalse;
279
280 if (size < sizeof (FcCache))
281 return NULL;
282 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
283 cache = mmap (0, size, PROT_READ, MAP_SHARED, fd, 0);
284 #elif defined(_WIN32)
285 {
286 HANDLE hFileMap;
287
288 cache = NULL;
289 hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL,
290 PAGE_READONLY, 0, 0, NULL);
291 if (hFileMap != NULL)
292 {
293 cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0, size);
294 CloseHandle (hFileMap);
295 }
296 }
297 #endif
298 if (!cache)
299 {
300 cache = malloc (size);
301 if (!cache)
302 return NULL;
303
304 if (read (fd, cache, size) != size)
305 {
306 free (cache);
307 return NULL;
308 }
309 allocated = FcTrue;
310 }
311 if (cache->magic != FC_CACHE_MAGIC ||
312 cache->size != size)
313 {
314 if (allocated)
315 free (cache);
316 else
317 {
318 #if defined(HAVE_MMAP) || defined(__CYGWIN__)
319 munmap (cache, size);
320 #elif defined(_WIN32)
321 UnmapViewOfFile (cache);
322 #endif
323 }
324 return NULL;
325 }
326
327 /* Mark allocated caches so they're freed rather than unmapped */
328 if (allocated)
329 cache->magic = FC_CACHE_MAGIC_COPY;
330
331 return cache;
332 }
333
334 FcBool
335 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs,
336 const FcChar8 *dir, FcConfig *config)
337 {
338 int fd;
339 FcCache *cache;
340 off_t size;
341 int i;
342 FcFontSet *cache_set;
343 intptr_t *cache_dirs;
344 FcPattern **cache_fonts;
345
346 fd = FcDirCacheOpen (config, dir, &size);
347 if (fd < 0)
348 return FcFalse;
349
350 cache = FcDirCacheMap (fd, size);
351
352 if (!cache)
353 {
354 if (FcDebug() & FC_DBG_CACHE)
355 printf ("FcDirCacheRead failed to map cache for %s\n", dir);
356 close (fd);
357 return FcFalse;
358 }
359
360 cache_set = FcCacheSet (cache);
361 cache_fonts = FcFontSetFonts(cache_set);
362 if (FcDebug() & FC_DBG_CACHE)
363 printf ("FcDirCacheRead mapped cache for %s (%d fonts %d subdirs)\n",
364 dir, cache_set->nfont, cache->dirs_count);
365 for (i = 0; i < cache_set->nfont; i++)
366 {
367 FcPattern *font = FcEncodedOffsetToPtr (cache_set,
368 cache_fonts[i],
369 FcPattern);
370 if (FcDebug() & FC_DBG_CACHEV) {
371 printf ("Mapped font %d\n", i);
372 FcPatternPrint (font);
373 }
374 FcFontSetAdd (set, font);
375 }
376
377 cache_dirs = FcCacheDirs (cache);
378 for (i = 0; i < cache->dirs_count; i++)
379 FcStrSetAdd (dirs, FcOffsetToPtr (cache_dirs,
380 cache_dirs[i],
381 FcChar8));
382
383 if (config)
384 FcConfigAddFontDir (config, (FcChar8 *)dir);
385
386 close (fd);
387 return FcTrue;
388 }
389
390 /*
391 * Cache file is:
392 *
393 * FcCache
394 * dir name
395 * subdirs
396 * FcFontSet
397 */
398
399 static FcCache *
400 FcDirCacheProduce (FcFontSet *set, const FcChar8 *dir, FcStrSet *dirs)
401 {
402 FcSerialize *serialize = FcSerializeCreate ();
403 FcCache *cache;
404 int i;
405 intptr_t cache_offset;
406 intptr_t dirs_offset;
407 FcChar8 *dir_serialize;
408 intptr_t *dirs_serialize;
409 FcFontSet *set_serialize;
410
411 if (!serialize)
412 return NULL;
413 /*
414 * Space for cache structure
415 */
416 cache_offset = FcSerializeReserve (serialize, sizeof (FcCache));
417 /*
418 * Directory name
419 */
420 if (!FcStrSerializeAlloc (serialize, dir))
421 goto bail1;
422 /*
423 * Subdirs
424 */
425 dirs_offset = FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *));
426 for (i = 0; i < dirs->num; i++)
427 if (!FcStrSerializeAlloc (serialize, dirs->strs[i]))
428 goto bail1;
429
430 /*
431 * Patterns
432 */
433 if (!FcFontSetSerializeAlloc (serialize, set))
434 goto bail1;
435
436 /* Serialize layout complete. Now allocate space and fill it */
437 cache = malloc (serialize->size);
438 if (!cache)
439 goto bail1;
440 /* shut up valgrind */
441 memset (cache, 0, serialize->size);
442
443 serialize->linear = cache;
444
445 cache->magic = FC_CACHE_MAGIC;
446 cache->size = serialize->size;
447
448 /*
449 * Serialize directory name
450 */
451 dir_serialize = FcStrSerialize (serialize, dir);
452 if (!dir_serialize)
453 goto bail2;
454 cache->dir = FcPtrToOffset (cache, dir_serialize);
455
456 /*
457 * Serialize sub dirs
458 */
459 dirs_serialize = FcSerializePtr (serialize, dirs);
460 if (!dirs_serialize)
461 goto bail2;
462 cache->dirs = FcPtrToOffset (cache, dirs_serialize);
463 cache->dirs_count = dirs->num;
464 for (i = 0; i < dirs->num; i++)
465 {
466 FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]);
467 if (!d_serialize)
468 goto bail2;
469 dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize);
470 }
471
472 /*
473 * Serialize font set
474 */
475 set_serialize = FcFontSetSerialize (serialize, set);
476 if (!set_serialize)
477 goto bail2;
478 cache->set = FcPtrToOffset (cache, set_serialize);
479
480 FcSerializeDestroy (serialize);
481
482 return cache;
483
484 bail2:
485 free (cache);
486 bail1:
487 FcSerializeDestroy (serialize);
488 return NULL;
489 }
490
491 static FcBool
492 FcMakeDirectory (const FcChar8 *dir)
493 {
494 FcChar8 *parent;
495 FcBool ret;
496
497 if (strlen ((char *) dir) == 0)
498 return FcFalse;
499
500 parent = FcStrDirname (dir);
501 if (!parent)
502 return FcFalse;
503 if (access ((char *) parent, W_OK|X_OK) == 0)
504 ret = mkdir ((char *) dir, 0777) == 0;
505 else if (access ((char *) parent, F_OK) == -1)
506 ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0777) == 0);
507 else
508 ret = FcFalse;
509 FcStrFree (parent);
510 return ret;
511 }
512
513 /* write serialized state to the cache file */
514 FcBool
515 FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir, FcConfig *config)
516 {
517 FcChar8 cache_base[CACHEBASE_LEN];
518 FcChar8 *cache_hashed;
519 int fd;
520 FcAtomic *atomic;
521 FcCache *cache;
522 FcStrList *list;
523 FcChar8 *cache_dir = NULL;
524 FcChar8 *test_dir;
525
526 /*
527 * Write it to the first directory in the list which is writable
528 */
529
530 list = FcStrListCreate (config->cacheDirs);
531 if (!list)
532 return FcFalse;
533 while ((test_dir = FcStrListNext (list))) {
534 if (access ((char *) test_dir, W_OK|X_OK) == 0)
535 {
536 cache_dir = test_dir;
537 break;
538 }
539 else
540 {
541 /*
542 * If the directory doesn't exist, try to create it
543 */
544 if (access ((char *) test_dir, F_OK) == -1) {
545 if (FcMakeDirectory (test_dir))
546 {
547 cache_dir = test_dir;
548 break;
549 }
550 }
551 }
552 }
553 FcStrListDone (list);
554 if (!cache_dir)
555 return FcFalse;
556
557 FcDirCacheBasename (dir, cache_base);
558 cache_hashed = FcStrPlus (cache_dir, cache_base);
559 if (!cache_hashed)
560 return FcFalse;
561
562 cache = FcDirCacheProduce (set, dir, dirs);
563
564 if (!cache)
565 goto bail1;
566
567 if (FcDebug () & FC_DBG_CACHE)
568 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
569 dir, cache_hashed);
570
571 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
572 if (!atomic)
573 goto bail2;
574
575 if (!FcAtomicLock (atomic))
576 goto bail3;
577
578 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
579 if (fd == -1)
580 goto bail4;
581
582 if (write (fd, cache, cache->size) != cache->size)
583 {
584 perror ("write cache");
585 goto bail5;
586 }
587
588 close(fd);
589 if (!FcAtomicReplaceOrig(atomic))
590 goto bail4;
591 FcStrFree ((FcChar8 *)cache_hashed);
592 FcAtomicUnlock (atomic);
593 FcAtomicDestroy (atomic);
594 return FcTrue;
595
596 bail5:
597 close (fd);
598 bail4:
599 FcAtomicUnlock (atomic);
600 bail3:
601 FcAtomicDestroy (atomic);
602 bail2:
603 free (cache);
604 bail1:
605 FcStrFree ((FcChar8 *)cache_hashed);
606 return FcFalse;
607 }
608
609 /*
610 * This code implements the MD5 message-digest algorithm.
611 * The algorithm is due to Ron Rivest. This code was
612 * written by Colin Plumb in 1993, no copyright is claimed.
613 * This code is in the public domain; do with it what you wish.
614 *
615 * Equivalent code is available from RSA Data Security, Inc.
616 * This code has been tested against that, and is equivalent,
617 * except that you don't need to include two pages of legalese
618 * with every copy.
619 *
620 * To compute the message digest of a chunk of bytes, declare an
621 * MD5Context structure, pass it to MD5Init, call MD5Update as
622 * needed on buffers full of bytes, and then call MD5Final, which
623 * will fill a supplied 16-byte array with the digest.
624 */
625
626 #ifndef HIGHFIRST
627 #define byteReverse(buf, len) /* Nothing */
628 #else
629 /*
630 * Note: this code is harmless on little-endian machines.
631 */
632 void byteReverse(unsigned char *buf, unsigned longs)
633 {
634 FcChar32 t;
635 do {
636 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
637 ((unsigned) buf[1] << 8 | buf[0]);
638 *(FcChar32 *) buf = t;
639 buf += 4;
640 } while (--longs);
641 }
642 #endif
643
644 /*
645 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
646 * initialization constants.
647 */
648 static void MD5Init(struct MD5Context *ctx)
649 {
650 ctx->buf[0] = 0x67452301;
651 ctx->buf[1] = 0xefcdab89;
652 ctx->buf[2] = 0x98badcfe;
653 ctx->buf[3] = 0x10325476;
654
655 ctx->bits[0] = 0;
656 ctx->bits[1] = 0;
657 }
658
659 /*
660 * Update context to reflect the concatenation of another buffer full
661 * of bytes.
662 */
663 static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len)
664 {
665 FcChar32 t;
666
667 /* Update bitcount */
668
669 t = ctx->bits[0];
670 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
671 ctx->bits[1]++; /* Carry from low to high */
672 ctx->bits[1] += len >> 29;
673
674 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
675
676 /* Handle any leading odd-sized chunks */
677
678 if (t) {
679 unsigned char *p = (unsigned char *) ctx->in + t;
680
681 t = 64 - t;
682 if (len < t) {
683 memcpy(p, buf, len);
684 return;
685 }
686 memcpy(p, buf, t);
687 byteReverse(ctx->in, 16);
688 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
689 buf += t;
690 len -= t;
691 }
692 /* Process data in 64-byte chunks */
693
694 while (len >= 64) {
695 memcpy(ctx->in, buf, 64);
696 byteReverse(ctx->in, 16);
697 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
698 buf += 64;
699 len -= 64;
700 }
701
702 /* Handle any remaining bytes of data. */
703
704 memcpy(ctx->in, buf, len);
705 }
706
707 /*
708 * Final wrapup - pad to 64-byte boundary with the bit pattern
709 * 1 0* (64-bit count of bits processed, MSB-first)
710 */
711 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
712 {
713 unsigned count;
714 unsigned char *p;
715
716 /* Compute number of bytes mod 64 */
717 count = (ctx->bits[0] >> 3) & 0x3F;
718
719 /* Set the first char of padding to 0x80. This is safe since there is
720 always at least one byte free */
721 p = ctx->in + count;
722 *p++ = 0x80;
723
724 /* Bytes of padding needed to make 64 bytes */
725 count = 64 - 1 - count;
726
727 /* Pad out to 56 mod 64 */
728 if (count < 8) {
729 /* Two lots of padding: Pad the first block to 64 bytes */
730 memset(p, 0, count);
731 byteReverse(ctx->in, 16);
732 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
733
734 /* Now fill the next block with 56 bytes */
735 memset(ctx->in, 0, 56);
736 } else {
737 /* Pad block to 56 bytes */
738 memset(p, 0, count - 8);
739 }
740 byteReverse(ctx->in, 14);
741
742 /* Append length in bits and transform */
743 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
744 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
745
746 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
747 byteReverse((unsigned char *) ctx->buf, 4);
748 memcpy(digest, ctx->buf, 16);
749 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
750 }
751
752
753 /* The four core functions - F1 is optimized somewhat */
754
755 /* #define F1(x, y, z) (x & y | ~x & z) */
756 #define F1(x, y, z) (z ^ (x & (y ^ z)))
757 #define F2(x, y, z) F1(z, x, y)
758 #define F3(x, y, z) (x ^ y ^ z)
759 #define F4(x, y, z) (y ^ (x | ~z))
760
761 /* This is the central step in the MD5 algorithm. */
762 #define MD5STEP(f, w, x, y, z, data, s) \
763 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
764
765 /*
766 * The core of the MD5 algorithm, this alters an existing MD5 hash to
767 * reflect the addition of 16 longwords of new data. MD5Update blocks
768 * the data and converts bytes into longwords for this routine.
769 */
770 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
771 {
772 register FcChar32 a, b, c, d;
773
774 a = buf[0];
775 b = buf[1];
776 c = buf[2];
777 d = buf[3];
778
779 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
780 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
781 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
782 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
783 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
784 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
785 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
786 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
787 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
788 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
789 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
790 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
791 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
792 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
793 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
794 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
795
796 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
797 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
798 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
799 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
800 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
801 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
802 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
803 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
804 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
805 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
806 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
807 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
808 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
809 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
810 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
811 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
812
813 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
814 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
815 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
816 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
817 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
818 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
819 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
820 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
821 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
822 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
823 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
824 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
825 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
826 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
827 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
828 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
829
830 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
831 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
832 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
833 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
834 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
835 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
836 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
837 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
838 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
839 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
840 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
841 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
842 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
843 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
844 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
845 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
846
847 buf[0] += a;
848 buf[1] += b;
849 buf[2] += c;
850 buf[3] += d;
851 }