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