]> git.wh0rd.org - fontconfig.git/blame - src/fccache.c
Cleanup copyright notices to replace "Keith Packard" with "the author(s)"
[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
5aaf466d 9 * documentation, and that the name of the author(s) not be used in
24330d27 10 * advertising or publicity pertaining to distribution of the software without
5aaf466d 11 * specific, written prior permission. The authors make no
24330d27
KP
12 * representations about the suitability of this software for any purpose. It
13 * is provided "as is" without express or implied warranty.
14 *
3074a73b 15 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
24330d27 16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
3074a73b 17 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
24330d27
KP
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>
9e612141 31#include <assert.h>
d6217cc6 32#if defined(HAVE_MMAP) || defined(__CYGWIN__)
93f67dfc 33# include <unistd.h>
d6217cc6 34# include <sys/mman.h>
93f67dfc 35#elif defined(_WIN32)
8a3dc488 36# define _WIN32_WINNT 0x0500
93f67dfc 37# include <windows.h>
d6217cc6 38#endif
24330d27 39
879af706
PL
40#ifndef O_BINARY
41#define O_BINARY 0
42#endif
43
ea44e218
PL
44struct MD5Context {
45 FcChar32 buf[4];
46 FcChar32 bits[2];
47 unsigned char in[64];
48};
49
50static void MD5Init(struct MD5Context *ctx);
db6f19f1 51static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len);
ea44e218
PL
52static void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
53static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]);
54
cf65c055 55#define CACHEBASE_LEN (1 + 32 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX))
7410e40b 56
8a3dc488
TL
57#ifdef _WIN32
58
59#include <windows.h>
60
61#ifdef __GNUC__
62typedef long long INT64;
63#define EPOCH_OFFSET 11644473600ll
64#else
65#define EPOCH_OFFSET 11644473600i64
66typedef __int64 INT64;
67#endif
68
69/* Workaround for problems in the stat() in the Microsoft C library:
70 *
71 * 1) stat() uses FindFirstFile() to get the file
72 * attributes. Unfortunately this API doesn't return correct values
73 * for modification time of a directory until some time after a file
74 * or subdirectory has been added to the directory. (This causes
75 * run-test.sh to fail, for instance.) GetFileAttributesEx() is
76 * better, it returns the updated timestamp right away.
77 *
78 * 2) stat() does some strange things related to backward
79 * compatibility with the local time timestamps on FAT volumes and
80 * daylight saving time. This causes problems after the switches
81 * to/from daylight saving time. See
82 * http://bugzilla.gnome.org/show_bug.cgi?id=154968 , especially
83 * comment #30, and http://www.codeproject.com/datetime/dstbugs.asp .
84 * We don't need any of that, FAT and Win9x are as good as dead. So
85 * just use the UTC timestamps from NTFS, converted to the Unix epoch.
86 */
87
f69db8d4 88int
8a3dc488
TL
89FcStat (const char *file, struct stat *statb)
90{
91 WIN32_FILE_ATTRIBUTE_DATA wfad;
92 char full_path_name[MAX_PATH];
93 char *basename;
94 DWORD rc;
594dcef0 95
8a3dc488
TL
96 if (!GetFileAttributesEx (file, GetFileExInfoStandard, &wfad))
97 return -1;
594dcef0 98
8a3dc488
TL
99 statb->st_dev = 0;
100
101 /* Calculate a pseudo inode number as a hash of the full path name.
102 * Call GetLongPathName() to get the spelling of the path name as it
103 * is on disk.
104 */
105 rc = GetFullPathName (file, sizeof (full_path_name), full_path_name, &basename);
106 if (rc == 0 || rc > sizeof (full_path_name))
107 return -1;
108
109 rc = GetLongPathName (full_path_name, full_path_name, sizeof (full_path_name));
110 statb->st_ino = FcStringHash (full_path_name);
594dcef0 111
8a3dc488
TL
112 statb->st_mode = _S_IREAD | _S_IWRITE;
113 statb->st_mode |= (statb->st_mode >> 3) | (statb->st_mode >> 6);
114
115 if (wfad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
116 statb->st_mode |= _S_IFDIR;
117 else
118 statb->st_mode |= _S_IFREG;
594dcef0 119
8a3dc488
TL
120 statb->st_nlink = 1;
121 statb->st_uid = statb->st_gid = 0;
122 statb->st_rdev = 0;
594dcef0 123
8a3dc488
TL
124 if (wfad.nFileSizeHigh > 0)
125 return -1;
126 statb->st_size = wfad.nFileSizeLow;
594dcef0 127
8a3dc488
TL
128 statb->st_atime = (*(INT64 *)&wfad.ftLastAccessTime)/10000000 - EPOCH_OFFSET;
129 statb->st_mtime = (*(INT64 *)&wfad.ftLastWriteTime)/10000000 - EPOCH_OFFSET;
130 statb->st_ctime = statb->st_mtime;
594dcef0 131
8a3dc488
TL
132 return 0;
133}
8a3dc488
TL
134#endif
135
7410e40b
PL
136static const char bin2hex[] = { '0', '1', '2', '3',
137 '4', '5', '6', '7',
138 '8', '9', 'a', 'b',
139 'c', 'd', 'e', 'f' };
140
141static FcChar8 *
142FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN])
55c8fa4f 143{
7410e40b
PL
144 unsigned char hash[16];
145 FcChar8 *hex_hash;
146 int cnt;
147 struct MD5Context ctx;
55c8fa4f 148
7410e40b 149 MD5Init (&ctx);
db6f19f1 150 MD5Update (&ctx, (const unsigned char *)dir, strlen ((const char *) dir));
3bfae75d 151
7410e40b
PL
152 MD5Final (hash, &ctx);
153
154 cache_base[0] = '/';
155 hex_hash = cache_base + 1;
156 for (cnt = 0; cnt < 16; ++cnt)
1178b569 157 {
7410e40b
PL
158 hex_hash[2*cnt ] = bin2hex[hash[cnt] >> 4];
159 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
160 }
161 hex_hash[2*cnt] = 0;
cf65c055 162 strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX);
6f9fcb51 163
7410e40b
PL
164 return cache_base;
165}
3bfae75d 166
7410e40b
PL
167FcBool
168FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
169{
7410e40b
PL
170 FcChar8 *cache_hashed = NULL;
171 FcChar8 cache_base[CACHEBASE_LEN];
172 FcStrList *list;
173 FcChar8 *cache_dir;
3bfae75d 174
7410e40b 175 FcDirCacheBasename (dir, cache_base);
3bfae75d 176
7410e40b
PL
177 list = FcStrListCreate (config->cacheDirs);
178 if (!list)
179 return FcFalse;
180
181 while ((cache_dir = FcStrListNext (list)))
3bfae75d 182 {
7410e40b
PL
183 cache_hashed = FcStrPlus (cache_dir, cache_base);
184 if (!cache_hashed)
185 break;
00f059e9 186 (void) unlink ((char *) cache_hashed);
3ae9258f 187 FcStrFree (cache_hashed);
1178b569 188 }
7410e40b
PL
189 FcStrListDone (list);
190 /* return FcFalse if something went wrong */
191 if (cache_dir)
192 return FcFalse;
1b7be377
PL
193 return FcTrue;
194}
195
4262e0b3 196static int
bc5e487f 197FcDirCacheOpenFile (const FcChar8 *cache_file, struct stat *file_stat)
4262e0b3 198{
bc5e487f 199 int fd;
4262e0b3 200
8a3dc488
TL
201#ifdef _WIN32
202 if (FcStat (cache_file, file_stat) < 0)
203 return -1;
204#endif
bc5e487f
KP
205 fd = open((char *) cache_file, O_RDONLY | O_BINARY);
206 if (fd < 0)
207 return fd;
8a3dc488 208#ifndef _WIN32
bc5e487f 209 if (fstat (fd, file_stat) < 0)
4262e0b3 210 {
bc5e487f
KP
211 close (fd);
212 return -1;
4262e0b3 213 }
8a3dc488 214#endif
bc5e487f 215 return fd;
4262e0b3
PL
216}
217
594dcef0 218/*
2d3387fd
KP
219 * Look for a cache file for the specified dir. Attempt
220 * to use each one we find, stopping when the callback
221 * indicates success
7410e40b 222 */
2d3387fd 223static FcBool
594dcef0 224FcDirCacheProcess (FcConfig *config, const FcChar8 *dir,
db6f19f1
KP
225 FcBool (*callback) (int fd, struct stat *fd_stat,
226 struct stat *dir_stat, void *closure),
0a87ce71 227 void *closure, FcChar8 **cache_file_ret)
ea44e218 228{
7ce19673 229 int fd = -1;
7410e40b
PL
230 FcChar8 cache_base[CACHEBASE_LEN];
231 FcStrList *list;
232 FcChar8 *cache_dir;
d2f78684 233 struct stat file_stat, dir_stat;
2d3387fd 234 FcBool ret = FcFalse;
d2f78684 235
8a3dc488 236 if (FcStat ((char *) dir, &dir_stat) < 0)
2d3387fd 237 return FcFalse;
df3efc11 238
7410e40b 239 FcDirCacheBasename (dir, cache_base);
ea44e218 240
7410e40b
PL
241 list = FcStrListCreate (config->cacheDirs);
242 if (!list)
2d3387fd 243 return FcFalse;
7410e40b
PL
244
245 while ((cache_dir = FcStrListNext (list)))
ea44e218 246 {
7ce19673 247 FcChar8 *cache_hashed = FcStrPlus (cache_dir, cache_base);
7410e40b 248 if (!cache_hashed)
d00c3cb5 249 break;
bc5e487f 250 fd = FcDirCacheOpenFile (cache_hashed, &file_stat);
7ce19673 251 if (fd >= 0) {
db6f19f1
KP
252 ret = (*callback) (fd, &file_stat, &dir_stat, closure);
253 close (fd);
254 if (ret)
d2f78684 255 {
db6f19f1
KP
256 if (cache_file_ret)
257 *cache_file_ret = cache_hashed;
258 else
259 FcStrFree (cache_hashed);
260 break;
d2f78684 261 }
d2f78684 262 }
0a87ce71 263 FcStrFree (cache_hashed);
7410e40b
PL
264 }
265 FcStrListDone (list);
594dcef0 266
2d3387fd 267 return ret;
ea44e218
PL
268}
269
9e612141
KP
270#define FC_CACHE_MIN_MMAP 1024
271
272/*
273 * Skip list element, make sure the 'next' pointer is the last thing
274 * in the structure, it will be allocated large enough to hold all
275 * of the necessary pointers
276 */
277
278typedef struct _FcCacheSkip FcCacheSkip;
279
280struct _FcCacheSkip {
281 FcCache *cache;
282 int ref;
283 intptr_t size;
284 dev_t cache_dev;
285 ino_t cache_ino;
286 time_t cache_mtime;
287 FcCacheSkip *next[1];
288};
289
290/*
291 * The head of the skip list; pointers for every possible level
292 * in the skip list, plus the largest level in the list
293 */
294
295#define FC_CACHE_MAX_LEVEL 16
296
297static FcCacheSkip *fcCacheChains[FC_CACHE_MAX_LEVEL];
298static int fcCacheMaxLevel;
299
cc104e6a
KP
300#if HAVE_RANDOM
301# define FcRandom() random()
302#else
303# if HAVE_LRAND48
304# define FcRandom() lrand48()
305# else
306# if HAVE_RAND
307# define FcRandom() rand()
308# endif
309# endif
310#endif
9e612141
KP
311/*
312 * Generate a random level number, distributed
313 * so that each level is 1/4 as likely as the one before
314 *
315 * Note that level numbers run 1 <= level <= MAX_LEVEL
316 */
317static int
318random_level (void)
319{
320 /* tricky bit -- each bit is '1' 75% of the time */
cc104e6a 321 long int bits = FcRandom () | FcRandom ();
9e612141
KP
322 int level = 0;
323
324 while (++level < FC_CACHE_MAX_LEVEL)
325 {
326 if (bits & 1)
327 break;
328 bits >>= 1;
329 }
330 return level;
331}
332
333/*
334 * Insert cache into the list
335 */
336static FcBool
337FcCacheInsert (FcCache *cache, struct stat *cache_stat)
338{
339 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
340 FcCacheSkip *s, **next;
341 int i, level;
342
343 /*
344 * Find links along each chain
345 */
346 next = fcCacheChains;
347 for (i = fcCacheMaxLevel; --i >= 0; )
348 {
349 for (; (s = next[i]); next = s->next)
350 if (s->cache > cache)
351 break;
352 update[i] = &next[i];
353 }
354
355 /*
356 * Create new list element
357 */
358 level = random_level ();
359 if (level > fcCacheMaxLevel)
360 {
361 level = fcCacheMaxLevel + 1;
362 update[fcCacheMaxLevel] = &fcCacheChains[fcCacheMaxLevel];
363 fcCacheMaxLevel = level;
364 }
594dcef0 365
9e612141
KP
366 s = malloc (sizeof (FcCacheSkip) + (level - 1) * sizeof (FcCacheSkip *));
367 if (!s)
368 return FcFalse;
369
370 s->cache = cache;
371 s->size = cache->size;
372 s->ref = 1;
49b44b27
KP
373 if (cache_stat)
374 {
375 s->cache_dev = cache_stat->st_dev;
376 s->cache_ino = cache_stat->st_ino;
377 s->cache_mtime = cache_stat->st_mtime;
378 }
379 else
380 {
381 s->cache_dev = 0;
382 s->cache_ino = 0;
383 s->cache_mtime = 0;
384 }
594dcef0 385
9e612141
KP
386 /*
387 * Insert into all fcCacheChains
388 */
389 for (i = 0; i < level; i++)
390 {
391 s->next[i] = *update[i];
392 *update[i] = s;
393 }
394 return FcTrue;
395}
396
397static FcCacheSkip *
398FcCacheFindByAddr (void *object)
399{
400 int i;
401 FcCacheSkip **next = fcCacheChains;
402 FcCacheSkip *s;
403
404 /*
405 * Walk chain pointers one level at a time
406 */
407 for (i = fcCacheMaxLevel; --i >= 0;)
408 while (next[i] && (char *) object >= ((char *) next[i]->cache + next[i]->size))
409 next = next[i]->next;
410 /*
411 * Here we are
412 */
413 s = next[0];
414 if (s && (char *) object < ((char *) s->cache + s->size))
415 return s;
416 return NULL;
417}
418
419static void
420FcCacheRemove (FcCache *cache)
421{
422 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
423 FcCacheSkip *s, **next;
424 int i;
425
426 /*
427 * Find links along each chain
428 */
429 next = fcCacheChains;
430 for (i = fcCacheMaxLevel; --i >= 0; )
431 {
432 for (; (s = next[i]); next = s->next)
433 if (s->cache >= cache)
434 break;
435 update[i] = &next[i];
436 }
437 s = next[0];
9e612141
KP
438 for (i = 0; i < fcCacheMaxLevel && *update[i] == s; i++)
439 *update[i] = s->next[i];
440 while (fcCacheMaxLevel > 0 && fcCacheChains[fcCacheMaxLevel - 1] == NULL)
441 fcCacheMaxLevel--;
442 free (s);
443}
444
445static FcCache *
446FcCacheFindByStat (struct stat *cache_stat)
447{
448 FcCacheSkip *s;
449
450 for (s = fcCacheChains[0]; s; s = s->next[0])
451 if (s->cache_dev == cache_stat->st_dev &&
452 s->cache_ino == cache_stat->st_ino &&
453 s->cache_mtime == cache_stat->st_mtime)
323ecd0c
KP
454 {
455 s->ref++;
9e612141 456 return s->cache;
323ecd0c 457 }
9e612141
KP
458 return NULL;
459}
460
8fe2104a
KP
461static void
462FcDirCacheDispose (FcCache *cache)
463{
464 switch (cache->magic) {
465 case FC_CACHE_MAGIC_ALLOC:
466 free (cache);
467 break;
468 case FC_CACHE_MAGIC_MMAP:
469#if defined(HAVE_MMAP) || defined(__CYGWIN__)
470 munmap (cache, cache->size);
471#elif defined(_WIN32)
472 UnmapViewOfFile (cache);
473#endif
474 break;
475 }
9e612141 476 FcCacheRemove (cache);
8fe2104a
KP
477}
478
9e612141
KP
479void
480FcCacheObjectReference (void *object)
8fe2104a 481{
9e612141 482 FcCacheSkip *skip = FcCacheFindByAddr (object);
8fe2104a 483
9e612141
KP
484 if (skip)
485 skip->ref++;
8fe2104a
KP
486}
487
9e612141
KP
488void
489FcCacheObjectDereference (void *object)
8fe2104a 490{
9e612141 491 FcCacheSkip *skip = FcCacheFindByAddr (object);
8fe2104a 492
9e612141
KP
493 if (skip)
494 {
495 skip->ref--;
496 if (skip->ref <= 0)
497 FcDirCacheDispose (skip->cache);
498 }
8fe2104a
KP
499}
500
501void
502FcCacheFini (void)
503{
504 int i;
8fe2104a 505
9e612141
KP
506 for (i = 0; i < FC_CACHE_MAX_LEVEL; i++)
507 assert (fcCacheChains[i] == NULL);
508 assert (fcCacheMaxLevel == 0);
8fe2104a
KP
509}
510
db6f19f1
KP
511static FcBool
512FcCacheTimeValid (FcCache *cache, struct stat *dir_stat)
513{
514 struct stat dir_static;
515
516 if (!dir_stat)
517 {
9e2ed251 518 if (FcStat ((const char *) FcCacheDir (cache), &dir_static) < 0)
db6f19f1
KP
519 return FcFalse;
520 dir_stat = &dir_static;
521 }
522 if (FcDebug () & FC_DBG_CACHE)
523 printf ("FcCacheTimeValid dir \"%s\" cache time %d dir time %d\n",
524 FcCacheDir (cache), cache->mtime, (int) dir_stat->st_mtime);
525 return cache->mtime == (int) dir_stat->st_mtime;
526}
527
bc5e487f
KP
528/*
529 * Map a cache file into memory
530 */
531static FcCache *
db6f19f1 532FcDirCacheMapFd (int fd, struct stat *fd_stat, struct stat *dir_stat)
212c9f43 533{
8fe2104a 534 FcCache *cache;
7ce19673 535 FcBool allocated = FcFalse;
af180c40 536
8fe2104a 537 if (fd_stat->st_size < sizeof (FcCache))
bc5e487f 538 return NULL;
9e612141 539 cache = FcCacheFindByStat (fd_stat);
8fe2104a 540 if (cache)
6bb5d72f
BE
541 {
542 if (FcCacheTimeValid (cache, dir_stat))
543 return cache;
544 FcDirCacheUnload (cache);
545 cache = NULL;
546 }
547
bc5e487f 548 /*
144ca878
KP
549 * Lage cache files are mmap'ed, smaller cache files are read. This
550 * balances the system cost of mmap against per-process memory usage.
bc5e487f 551 */
8fe2104a 552 if (fd_stat->st_size >= FC_CACHE_MIN_MMAP)
bc5e487f 553 {
d6217cc6 554#if defined(HAVE_MMAP) || defined(__CYGWIN__)
8fe2104a 555 cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0);
1dd95fcb
SB
556 if (cache == MAP_FAILED)
557 cache = NULL;
93f67dfc 558#elif defined(_WIN32)
7ce19673 559 {
bc5e487f
KP
560 HANDLE hFileMap;
561
562 cache = NULL;
563 hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL,
564 PAGE_READONLY, 0, 0, NULL);
565 if (hFileMap != NULL)
566 {
594dcef0 567 cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0,
0596d729 568 fd_stat->st_size);
bc5e487f
KP
569 CloseHandle (hFileMap);
570 }
93f67dfc 571 }
7ce19673 572#endif
bc5e487f 573 }
7ce19673
KP
574 if (!cache)
575 {
8fe2104a 576 cache = malloc (fd_stat->st_size);
7ce19673 577 if (!cache)
bc5e487f 578 return NULL;
eb0cf671 579
8fe2104a 580 if (read (fd, cache, fd_stat->st_size) != fd_stat->st_size)
00f059e9 581 {
7ce19673 582 free (cache);
bc5e487f 583 return NULL;
00f059e9 584 }
7ce19673 585 allocated = FcTrue;
594dcef0
BE
586 }
587 if (cache->magic != FC_CACHE_MAGIC_MMAP ||
3b8a03c0 588 cache->version < FC_CACHE_CONTENT_VERSION ||
8fe2104a 589 cache->size != fd_stat->st_size ||
bc5e8adb
KP
590 !FcCacheTimeValid (cache, dir_stat) ||
591 !FcCacheInsert (cache, fd_stat))
7ce19673
KP
592 {
593 if (allocated)
594 free (cache);
595 else
596 {
597#if defined(HAVE_MMAP) || defined(__CYGWIN__)
8fe2104a 598 munmap (cache, fd_stat->st_size);
7ce19673
KP
599#elif defined(_WIN32)
600 UnmapViewOfFile (cache);
00f059e9 601#endif
7ce19673 602 }
bc5e487f 603 return NULL;
00f059e9 604 }
af180c40 605
7ce19673
KP
606 /* Mark allocated caches so they're freed rather than unmapped */
607 if (allocated)
bc5e487f 608 cache->magic = FC_CACHE_MAGIC_ALLOC;
7ce19673 609
bc5e487f
KP
610 return cache;
611}
612
17389539
KP
613void
614FcDirCacheReference (FcCache *cache, int nref)
615{
616 FcCacheSkip *skip = FcCacheFindByAddr (cache);
617
618 if (skip)
619 skip->ref += nref;
620}
621
bc5e487f
KP
622void
623FcDirCacheUnload (FcCache *cache)
624{
9e612141 625 FcCacheObjectDereference (cache);
bc5e487f
KP
626}
627
628static FcBool
db6f19f1 629FcDirCacheMapHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
bc5e487f 630{
db6f19f1 631 FcCache *cache = FcDirCacheMapFd (fd, fd_stat, dir_stat);
bc5e487f
KP
632
633 if (!cache)
634 return FcFalse;
2d3387fd
KP
635 *((FcCache **) closure) = cache;
636 return FcTrue;
637}
638
639FcCache *
bc5e487f 640FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file)
2d3387fd 641{
76abb77f 642 FcCache *cache = NULL;
2d3387fd 643
76abb77f 644 if (!FcDirCacheProcess (config, dir,
bc5e487f 645 FcDirCacheMapHelper,
0a87ce71 646 &cache, cache_file))
76abb77f
KP
647 return NULL;
648 return cache;
af180c40
KP
649}
650
bc5e487f
KP
651FcCache *
652FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat)
af180c40 653{
bc5e487f
KP
654 int fd;
655 FcCache *cache;
e37d10fa 656 struct stat my_file_stat;
af180c40 657
e37d10fa
KP
658 if (!file_stat)
659 file_stat = &my_file_stat;
bc5e487f
KP
660 fd = FcDirCacheOpenFile (cache_file, file_stat);
661 if (fd < 0)
662 return NULL;
db6f19f1 663 cache = FcDirCacheMapFd (fd, file_stat, NULL);
bc5e487f
KP
664 close (fd);
665 return cache;
eb0cf671 666}
bc5e487f
KP
667
668/*
669 * Validate a cache file by reading the header and checking
670 * the magic number and the size field
671 */
2d3387fd 672static FcBool
db6f19f1 673FcDirCacheValidateHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure)
2d3387fd
KP
674{
675 FcBool ret = FcTrue;
676 FcCache c;
594dcef0 677
2d3387fd
KP
678 if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache))
679 ret = FcFalse;
bc5e487f
KP
680 else if (c.magic != FC_CACHE_MAGIC_MMAP)
681 ret = FcFalse;
3b8a03c0 682 else if (c.version < FC_CACHE_CONTENT_VERSION)
2d3387fd 683 ret = FcFalse;
8fe2104a 684 else if (fd_stat->st_size != c.size)
2d3387fd 685 ret = FcFalse;
db6f19f1
KP
686 else if (c.mtime != (int) dir_stat->st_mtime)
687 ret = FcFalse;
2d3387fd
KP
688 return ret;
689}
690
f57783d2
KP
691static FcBool
692FcDirCacheValidConfig (const FcChar8 *dir, FcConfig *config)
2d3387fd 693{
594dcef0 694 return FcDirCacheProcess (config, dir,
bc5e487f
KP
695 FcDirCacheValidateHelper,
696 NULL, NULL);
2d3387fd
KP
697}
698
f57783d2
KP
699FcBool
700FcDirCacheValid (const FcChar8 *dir)
701{
702 FcConfig *config;
594dcef0 703
f57783d2
KP
704 config = FcConfigGetCurrent ();
705 if (!config)
706 return FcFalse;
707
708 return FcDirCacheValidConfig (dir, config);
709}
710
7ce19673 711/*
bc5e487f 712 * Build a cache structure from the given contents
7ce19673 713 */
bc5e487f 714FcCache *
db6f19f1 715FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcStrSet *dirs)
eb0cf671 716{
7ce19673
KP
717 FcSerialize *serialize = FcSerializeCreate ();
718 FcCache *cache;
719 int i;
720 intptr_t cache_offset;
721 intptr_t dirs_offset;
722 FcChar8 *dir_serialize;
723 intptr_t *dirs_serialize;
724 FcFontSet *set_serialize;
594dcef0 725
7ce19673
KP
726 if (!serialize)
727 return NULL;
728 /*
729 * Space for cache structure
730 */
731 cache_offset = FcSerializeReserve (serialize, sizeof (FcCache));
732 /*
733 * Directory name
734 */
735 if (!FcStrSerializeAlloc (serialize, dir))
736 goto bail1;
737 /*
738 * Subdirs
739 */
740 dirs_offset = FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *));
741 for (i = 0; i < dirs->num; i++)
742 if (!FcStrSerializeAlloc (serialize, dirs->strs[i]))
743 goto bail1;
eb0cf671 744
7ce19673
KP
745 /*
746 * Patterns
747 */
748 if (!FcFontSetSerializeAlloc (serialize, set))
749 goto bail1;
594dcef0 750
7ce19673
KP
751 /* Serialize layout complete. Now allocate space and fill it */
752 cache = malloc (serialize->size);
753 if (!cache)
754 goto bail1;
755 /* shut up valgrind */
756 memset (cache, 0, serialize->size);
d6217cc6 757
7ce19673 758 serialize->linear = cache;
eb0cf671 759
bc5e487f
KP
760 cache->magic = FC_CACHE_MAGIC_ALLOC;
761 cache->version = FC_CACHE_CONTENT_VERSION;
7ce19673 762 cache->size = serialize->size;
db6f19f1 763 cache->mtime = (int) dir_stat->st_mtime;
eb0cf671 764
7ce19673
KP
765 /*
766 * Serialize directory name
767 */
768 dir_serialize = FcStrSerialize (serialize, dir);
769 if (!dir_serialize)
770 goto bail2;
771 cache->dir = FcPtrToOffset (cache, dir_serialize);
594dcef0 772
7ce19673
KP
773 /*
774 * Serialize sub dirs
775 */
776 dirs_serialize = FcSerializePtr (serialize, dirs);
777 if (!dirs_serialize)
778 goto bail2;
779 cache->dirs = FcPtrToOffset (cache, dirs_serialize);
780 cache->dirs_count = dirs->num;
594dcef0 781 for (i = 0; i < dirs->num; i++)
8e351527 782 {
7ce19673
KP
783 FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]);
784 if (!d_serialize)
785 goto bail2;
786 dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize);
8e351527 787 }
594dcef0 788
7ce19673
KP
789 /*
790 * Serialize font set
791 */
792 set_serialize = FcFontSetSerialize (serialize, set);
793 if (!set_serialize)
794 goto bail2;
795 cache->set = FcPtrToOffset (cache, set_serialize);
eb0cf671 796
7ce19673 797 FcSerializeDestroy (serialize);
594dcef0 798
49b44b27
KP
799 FcCacheInsert (cache, NULL);
800
7ce19673 801 return cache;
212c9f43 802
7ce19673
KP
803bail2:
804 free (cache);
805bail1:
806 FcSerializeDestroy (serialize);
807 return NULL;
212c9f43
PL
808}
809
1de7a4cc
HWN
810
811#ifdef _WIN32
812#define mkdir(path,mode) _mkdir(path)
813#endif
814
7410e40b
PL
815static FcBool
816FcMakeDirectory (const FcChar8 *dir)
817{
818 FcChar8 *parent;
819 FcBool ret;
594dcef0 820
7410e40b
PL
821 if (strlen ((char *) dir) == 0)
822 return FcFalse;
594dcef0 823
7410e40b
PL
824 parent = FcStrDirname (dir);
825 if (!parent)
826 return FcFalse;
23848903 827 if (access ((char *) parent, F_OK) == 0)
8ae1e3d5 828 ret = mkdir ((char *) dir, 0755) == 0 && chmod ((char *) dir, 0755) == 0;
7410e40b 829 else if (access ((char *) parent, F_OK) == -1)
8ae1e3d5 830 ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0755) == 0) && chmod ((char *) dir, 0755) == 0;
7410e40b
PL
831 else
832 ret = FcFalse;
833 FcStrFree (parent);
834 return ret;
835}
836
212c9f43
PL
837/* write serialized state to the cache file */
838FcBool
bc5e487f 839FcDirCacheWrite (FcCache *cache, FcConfig *config)
212c9f43 840{
bc5e487f 841 FcChar8 *dir = FcCacheDir (cache);
7410e40b
PL
842 FcChar8 cache_base[CACHEBASE_LEN];
843 FcChar8 *cache_hashed;
7ce19673 844 int fd;
1d879de2 845 FcAtomic *atomic;
00f059e9 846 FcStrList *list;
d2f78684
KP
847 FcChar8 *cache_dir = NULL;
848 FcChar8 *test_dir;
b697fa25
BE
849 FcCacheSkip *skip;
850 struct stat cache_stat;
bc5e487f
KP
851 int magic;
852 int written;
212c9f43 853
7410e40b 854 /*
d2f78684 855 * Write it to the first directory in the list which is writable
7410e40b 856 */
594dcef0 857
d2f78684
KP
858 list = FcStrListCreate (config->cacheDirs);
859 if (!list)
860 return FcFalse;
861 while ((test_dir = FcStrListNext (list))) {
8ae1e3d5 862 if (access ((char *) test_dir, W_OK|X_OK) == 0)
d2f78684
KP
863 {
864 cache_dir = test_dir;
865 break;
866 }
867 else
868 {
7410e40b
PL
869 /*
870 * If the directory doesn't exist, try to create it
871 */
d2f78684
KP
872 if (access ((char *) test_dir, F_OK) == -1) {
873 if (FcMakeDirectory (test_dir))
874 {
875 cache_dir = test_dir;
7410e40b 876 break;
d2f78684 877 }
7410e40b 878 }
8ae1e3d5
BE
879 /*
880 * Otherwise, try making it writable
881 */
882 else if (chmod ((char *) test_dir, 0755) == 0)
883 {
884 cache_dir = test_dir;
885 break;
886 }
7410e40b 887 }
7410e40b 888 }
d2f78684
KP
889 FcStrListDone (list);
890 if (!cache_dir)
891 return FcFalse;
7ce19673 892
d2f78684
KP
893 FcDirCacheBasename (dir, cache_base);
894 cache_hashed = FcStrPlus (cache_dir, cache_base);
895 if (!cache_hashed)
896 return FcFalse;
ea44e218 897
4262e0b3 898 if (FcDebug () & FC_DBG_CACHE)
c1c3ba06
KP
899 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
900 dir, cache_hashed);
4262e0b3 901
3bfae75d 902 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
1d879de2 903 if (!atomic)
bc5e487f 904 goto bail1;
212c9f43 905
1d879de2 906 if (!FcAtomicLock (atomic))
7ce19673 907 goto bail3;
ec760b17 908
879af706 909 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
1d879de2 910 if (fd == -1)
7ce19673 911 goto bail4;
594dcef0 912
bc5e487f
KP
913 /* Temporarily switch magic to MMAP while writing to file */
914 magic = cache->magic;
915 if (magic != FC_CACHE_MAGIC_MMAP)
916 cache->magic = FC_CACHE_MAGIC_MMAP;
594dcef0 917
bc5e487f
KP
918 /*
919 * Write cache contents to file
920 */
921 written = write (fd, cache, cache->size);
594dcef0 922
bc5e487f
KP
923 /* Switch magic back */
924 if (magic != FC_CACHE_MAGIC_MMAP)
925 cache->magic = magic;
594dcef0 926
bc5e487f 927 if (written != cache->size)
fff5a5af 928 {
7ce19673 929 perror ("write cache");
68355f38
PL
930 goto bail5;
931 }
4262e0b3 932
212c9f43 933 close(fd);
1d879de2 934 if (!FcAtomicReplaceOrig(atomic))
7ce19673 935 goto bail4;
b697fa25
BE
936
937 /* If the file is small, update the cache chain entry such that the
938 * new cache file is not read again. If it's large, we don't do that
939 * such that we reload it, using mmap, which is shared across processes.
940 */
941 if (cache->size < FC_CACHE_MIN_MMAP &&
942 (skip = FcCacheFindByAddr (cache)) &&
943 FcStat (cache_hashed, &cache_stat))
944 {
945 skip->cache_dev = cache_stat.st_dev;
946 skip->cache_ino = cache_stat.st_ino;
947 skip->cache_mtime = cache_stat.st_mtime;
948 }
949
bc5e487f 950 FcStrFree (cache_hashed);
1d879de2
PL
951 FcAtomicUnlock (atomic);
952 FcAtomicDestroy (atomic);
212c9f43
PL
953 return FcTrue;
954
ea44e218 955 bail5:
1d879de2 956 close (fd);
7ce19673 957 bail4:
1d879de2 958 FcAtomicUnlock (atomic);
7ce19673 959 bail3:
1d879de2 960 FcAtomicDestroy (atomic);
ea44e218 961 bail1:
bc5e487f 962 FcStrFree (cache_hashed);
4262e0b3
PL
963 return FcFalse;
964}
965
4984242e
KP
966/*
967 * Hokey little macro trick to permit the definitions of C functions
968 * with the same name as CPP macros
969 */
c9c68750
KJ
970#define args1(x) (x)
971#define args2(x,y) (x,y)
4984242e
KP
972
973const FcChar8 *
c9c68750 974FcCacheDir args1(const FcCache *c)
4984242e
KP
975{
976 return FcCacheDir (c);
977}
978
979FcFontSet *
c9c68750 980FcCacheCopySet args1(const FcCache *c)
4984242e
KP
981{
982 FcFontSet *old = FcCacheSet (c);
983 FcFontSet *new = FcFontSetCreate ();
984 int i;
594dcef0 985
4984242e
KP
986 if (!new)
987 return NULL;
988 for (i = 0; i < old->nfont; i++)
8d779ce4
KP
989 {
990 FcPattern *font = FcFontSetFont (old, i);
991
992 FcPatternReference (font);
993 if (!FcFontSetAdd (new, font))
4984242e
KP
994 {
995 FcFontSetDestroy (new);
996 return NULL;
997 }
8d779ce4 998 }
4984242e
KP
999 return new;
1000}
1001
1002const FcChar8 *
c9c68750 1003FcCacheSubdir args2(const FcCache *c, int i)
4984242e
KP
1004{
1005 return FcCacheSubdir (c, i);
1006}
1007
1008int
c9c68750 1009FcCacheNumSubdir args1(const FcCache *c)
4984242e
KP
1010{
1011 return c->dirs_count;
1012}
1013
1014int
c9c68750 1015FcCacheNumFont args1(const FcCache *c)
4984242e
KP
1016{
1017 return FcCacheSet(c)->nfont;
1018}
1019
ea44e218
PL
1020/*
1021 * This code implements the MD5 message-digest algorithm.
1022 * The algorithm is due to Ron Rivest. This code was
1023 * written by Colin Plumb in 1993, no copyright is claimed.
1024 * This code is in the public domain; do with it what you wish.
1025 *
1026 * Equivalent code is available from RSA Data Security, Inc.
1027 * This code has been tested against that, and is equivalent,
1028 * except that you don't need to include two pages of legalese
1029 * with every copy.
1030 *
1031 * To compute the message digest of a chunk of bytes, declare an
1032 * MD5Context structure, pass it to MD5Init, call MD5Update as
1033 * needed on buffers full of bytes, and then call MD5Final, which
1034 * will fill a supplied 16-byte array with the digest.
1035 */
1036
1037#ifndef HIGHFIRST
1038#define byteReverse(buf, len) /* Nothing */
1039#else
1040/*
1041 * Note: this code is harmless on little-endian machines.
1042 */
1043void byteReverse(unsigned char *buf, unsigned longs)
1044{
1045 FcChar32 t;
1046 do {
1047 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1048 ((unsigned) buf[1] << 8 | buf[0]);
1049 *(FcChar32 *) buf = t;
1050 buf += 4;
1051 } while (--longs);
1052}
1053#endif
1054
1055/*
1056 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1057 * initialization constants.
1058 */
1059static void MD5Init(struct MD5Context *ctx)
1060{
1061 ctx->buf[0] = 0x67452301;
1062 ctx->buf[1] = 0xefcdab89;
1063 ctx->buf[2] = 0x98badcfe;
1064 ctx->buf[3] = 0x10325476;
1065
1066 ctx->bits[0] = 0;
1067 ctx->bits[1] = 0;
1068}
1069
1070/*
1071 * Update context to reflect the concatenation of another buffer full
1072 * of bytes.
1073 */
db6f19f1 1074static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len)
ea44e218
PL
1075{
1076 FcChar32 t;
1077
1078 /* Update bitcount */
1079
1080 t = ctx->bits[0];
1081 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1082 ctx->bits[1]++; /* Carry from low to high */
1083 ctx->bits[1] += len >> 29;
1084
1085 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1086
1087 /* Handle any leading odd-sized chunks */
1088
1089 if (t) {
1090 unsigned char *p = (unsigned char *) ctx->in + t;
1091
1092 t = 64 - t;
1093 if (len < t) {
1094 memcpy(p, buf, len);
1095 return;
1096 }
1097 memcpy(p, buf, t);
1098 byteReverse(ctx->in, 16);
1099 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1100 buf += t;
1101 len -= t;
1102 }
1103 /* Process data in 64-byte chunks */
1104
1105 while (len >= 64) {
1106 memcpy(ctx->in, buf, 64);
1107 byteReverse(ctx->in, 16);
1108 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1109 buf += 64;
1110 len -= 64;
1111 }
1112
1113 /* Handle any remaining bytes of data. */
1114
1115 memcpy(ctx->in, buf, len);
1116}
1117
1118/*
594dcef0 1119 * Final wrapup - pad to 64-byte boundary with the bit pattern
ea44e218
PL
1120 * 1 0* (64-bit count of bits processed, MSB-first)
1121 */
1122static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1123{
1124 unsigned count;
1125 unsigned char *p;
1126
1127 /* Compute number of bytes mod 64 */
1128 count = (ctx->bits[0] >> 3) & 0x3F;
1129
1130 /* Set the first char of padding to 0x80. This is safe since there is
1131 always at least one byte free */
1132 p = ctx->in + count;
1133 *p++ = 0x80;
1134
1135 /* Bytes of padding needed to make 64 bytes */
1136 count = 64 - 1 - count;
1137
1138 /* Pad out to 56 mod 64 */
1139 if (count < 8) {
1140 /* Two lots of padding: Pad the first block to 64 bytes */
1141 memset(p, 0, count);
1142 byteReverse(ctx->in, 16);
1143 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1144
1145 /* Now fill the next block with 56 bytes */
1146 memset(ctx->in, 0, 56);
1147 } else {
1148 /* Pad block to 56 bytes */
1149 memset(p, 0, count - 8);
1150 }
1151 byteReverse(ctx->in, 14);
1152
1153 /* Append length in bits and transform */
1154 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1155 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1156
1157 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1158 byteReverse((unsigned char *) ctx->buf, 4);
1159 memcpy(digest, ctx->buf, 16);
1160 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
1161}
1162
1163
1164/* The four core functions - F1 is optimized somewhat */
1165
1166/* #define F1(x, y, z) (x & y | ~x & z) */
1167#define F1(x, y, z) (z ^ (x & (y ^ z)))
1168#define F2(x, y, z) F1(z, x, y)
1169#define F3(x, y, z) (x ^ y ^ z)
1170#define F4(x, y, z) (y ^ (x | ~z))
1171
1172/* This is the central step in the MD5 algorithm. */
1173#define MD5STEP(f, w, x, y, z, data, s) \
1174 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1175
1176/*
1177 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1178 * reflect the addition of 16 longwords of new data. MD5Update blocks
1179 * the data and converts bytes into longwords for this routine.
1180 */
1181static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1182{
1183 register FcChar32 a, b, c, d;
1184
1185 a = buf[0];
1186 b = buf[1];
1187 c = buf[2];
1188 d = buf[3];
1189
1190 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1191 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1192 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1193 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1194 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1195 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1196 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1197 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1198 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1199 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1200 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1201 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1202 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1203 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1204 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1205 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1206
1207 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1208 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1209 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1210 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1211 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1212 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1213 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1214 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1215 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1216 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1217 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1218 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1219 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1220 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1221 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1222 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1223
1224 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1225 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1226 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1227 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1228 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1229 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1230 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1231 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1232 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1233 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1234 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1235 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1236 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1237 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1238 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1239 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1240
1241 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1242 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1243 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1244 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1245 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1246 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1247 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1248 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1249 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1250 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1251 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1252 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1253 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1254 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1255 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1256 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1257
1258 buf[0] += a;
1259 buf[1] += b;
1260 buf[2] += c;
1261 buf[3] += d;
1262}
23816bf9
KP
1263#define __fccache__
1264#include "fcaliastail.h"
1265#undef __fccache__