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