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