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