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