]> git.wh0rd.org - fontconfig.git/blame - src/fccache.c
Bump version to 2.3.95.
[fontconfig.git] / src / fccache.c
CommitLineData
24330d27 1/*
4bd4418a 2 * $RCSId: xc/lib/fontconfig/src/fccache.c,v 1.12 2002/08/22 07:36:44 keithp Exp $
24330d27 3 *
46b51147 4 * Copyright © 2000 Keith Packard
03a212e5 5 * Copyright © 2005 Patrick Lam
24330d27
KP
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of Keith Packard not be used in
12 * advertising or publicity pertaining to distribution of the software without
13 * specific, written prior permission. Keith Packard makes no
14 * representations about the suitability of this software for any purpose. It
15 * is provided "as is" without express or implied warranty.
16 *
17 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
23 * PERFORMANCE OF THIS SOFTWARE.
24 */
25
212c9f43 26#include <fcntl.h>
4262e0b3 27#include <dirent.h>
ea44e218 28#include <string.h>
fd77c154
PL
29#include <sys/types.h>
30#include <unistd.h>
24330d27 31#include "fcint.h"
7fd7221e 32#include <unistd.h>
d6217cc6
PL
33#if defined(HAVE_MMAP) || defined(__CYGWIN__)
34# include <sys/mman.h>
35# include <sys/utsname.h>
36#endif
24330d27 37
2dbe7597 38#define ENDIAN_TEST 0x12345678
f2fb985c 39#define MACHINE_SIGNATURE_SIZE (9 + 5*20 + 1)
d6217cc6
PL
40/* for when we don't have sysconf: */
41#define FC_HARDCODED_PAGESIZE 8192
2dbe7597 42
879af706
PL
43#ifndef O_BINARY
44#define O_BINARY 0
45#endif
46
ea44e218 47static int
df3efc11 48FcDirCacheOpen (const FcChar8 * dir);
ea44e218
PL
49
50static char *
51FcDirCacheHashName (char * cache_file, int collisions);
52
eb0cf671 53static off_t
c60ec7cc 54FcCacheSkipToArch (int fd, const char * arch);
1b7be377 55
eb0cf671 56static FcBool
1d879de2 57FcCacheCopyOld (int fd, int fd_orig, off_t start);
1b7be377 58
eb0cf671
PL
59static void *
60FcDirCacheProduce (FcFontSet *set, FcCache * metadata);
1b7be377
PL
61
62static FcBool
cd9bca69 63FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config);
1b7be377 64
eb0cf671
PL
65static int
66FcCacheNextOffset(off_t w);
1b7be377 67
eb0cf671
PL
68static char *
69FcCacheMachineSignature (void);
1b7be377
PL
70
71static FcBool
eb0cf671 72FcCacheHaveBank (int bank);
1b7be377 73
e77c1718
PL
74static void
75FcCacheAddBankDir (int bank, const char * dir);
76
ea44e218
PL
77struct MD5Context {
78 FcChar32 buf[4];
79 FcChar32 bits[2];
80 unsigned char in[64];
81};
82
83static void MD5Init(struct MD5Context *ctx);
84static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len);
85static void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
86static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]);
87
eb0cf671 88#define FC_DBG_CACHE_REF 1024
1b7be377 89
8245771d
PL
90static char *
91FcCacheReadString (int fd, char *dest, int len)
24330d27 92{
799157db
PL
93 int size;
94 int slen;
24330d27 95
24330d27 96 if (len == 0)
03a212e5 97 return 0;
799157db
PL
98
99 size = read (fd, dest, len-1);
100
101 if (size > 0)
24330d27 102 {
799157db
PL
103 dest[size] = '\0';
104 slen = strlen (dest);
105
106 lseek (fd, slen - size + 1, SEEK_CUR);
107 return slen < len ? dest : 0;
24330d27 108 }
799157db 109
ccb3e93b 110 return 0;
24330d27
KP
111}
112
ea44e218
PL
113static void
114FcCacheSkipString (int fd)
115{
799157db
PL
116 char buf[256];
117 int size;
118 int slen;
ea44e218 119
799157db 120 while ( (size = read (fd, buf, sizeof (buf)-1)) > 0)
ea44e218 121 {
799157db
PL
122 buf [size] = '\0';
123 slen = strlen (buf);
124 if (slen < size)
125 {
126 lseek (fd, slen - size + 1, SEEK_CUR);
127 return;
128 }
ea44e218 129 }
ea44e218
PL
130}
131
24330d27 132static FcBool
8245771d 133FcCacheWriteString (int fd, const char *chars)
327a7fd4 134{
212c9f43 135 if (write (fd, chars, strlen(chars)+1) != strlen(chars)+1)
327a7fd4 136 return FcFalse;
327a7fd4
KP
137 return FcTrue;
138}
139
eb0cf671
PL
140static void
141FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
1b7be377 142{
971cf180 143 FcStrSetDestroy (d->subdirs);
5e678e94
PL
144 FcMemFree (FC_MEM_STRING, strlen (d->name)+1);
145 free (d->name);
146 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir));
147 free (d);
1b7be377
PL
148}
149
eb0cf671
PL
150FcGlobalCache *
151FcGlobalCacheCreate (void)
1b7be377 152{
eb0cf671 153 FcGlobalCache *cache;
1b7be377 154
eb0cf671
PL
155 cache = malloc (sizeof (FcGlobalCache));
156 if (!cache)
157 return 0;
158 FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCache));
159 cache->dirs = 0;
160 cache->updated = FcFalse;
161 cache->fd = -1;
162 return cache;
1b7be377
PL
163}
164
eb0cf671
PL
165void
166FcGlobalCacheDestroy (FcGlobalCache *cache)
327a7fd4 167{
eb0cf671 168 FcGlobalCacheDir *d, *next;
327a7fd4 169
eb0cf671 170 for (d = cache->dirs; d; d = next)
327a7fd4 171 {
eb0cf671
PL
172 next = d->next;
173 FcGlobalCacheDirDestroy (d);
327a7fd4 174 }
eb0cf671
PL
175 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache));
176 free (cache);
327a7fd4
KP
177}
178
179void
eb0cf671 180FcGlobalCacheLoad (FcGlobalCache *cache,
03a212e5 181 FcStrSet *staleDirs,
2b25f00c
PL
182 const FcChar8 *cache_file,
183 FcConfig *config)
327a7fd4 184{
ea44e218 185 char name_buf[FC_MAX_FILE_LEN];
eb0cf671 186 FcGlobalCacheDir *d, *next;
2b25f00c 187 FcFileTime config_time = FcConfigModifiedTime (config);
eb0cf671
PL
188 char * current_arch_machine_name;
189 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
190 off_t current_arch_start;
327a7fd4 191
03a212e5 192 struct stat cache_stat, dir_stat;
971cf180 193 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
03a212e5
PL
194
195 if (stat ((char *) cache_file, &cache_stat) < 0)
196 return;
197
879af706 198 cache->fd = open ((char *) cache_file, O_RDONLY | O_BINARY);
eb0cf671
PL
199 if (cache->fd == -1)
200 return;
327a7fd4 201
eb0cf671 202 cache->updated = FcFalse;
327a7fd4 203
c5411c4c 204 if (!FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)))
28aefd01 205 goto bail_and_destroy;
c60ec7cc 206 if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) != 0)
28aefd01 207 goto bail_and_destroy;
c60ec7cc 208
eb0cf671
PL
209 current_arch_machine_name = FcCacheMachineSignature ();
210 current_arch_start = FcCacheSkipToArch(cache->fd,
c60ec7cc 211 current_arch_machine_name);
eb0cf671 212 if (current_arch_start < 0)
b17cf498 213 goto bail1;
327a7fd4 214
eb0cf671 215 lseek (cache->fd, current_arch_start, SEEK_SET);
c5411c4c
PL
216 if (!FcCacheReadString (cache->fd, candidate_arch_machine_name,
217 sizeof (candidate_arch_machine_name)))
218 goto bail_and_destroy;
03a212e5 219 if (strlen(candidate_arch_machine_name) == 0)
e58b50e8 220 goto bail_and_destroy;
1b7be377 221
eb0cf671 222 while (1)
1b7be377 223 {
8cb4c56d
PL
224 off_t targ;
225
2c4e0124
PL
226 if (!FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)) ||
227 !strlen(name_buf))
1b7be377 228 break;
1b7be377 229
9090cb9e
PL
230 /* Directory must be older than the global cache file; also
231 cache must be newer than the config file. */
03a212e5 232 if (stat ((char *) name_buf, &dir_stat) < 0 ||
2b25f00c 233 dir_stat.st_mtime > cache_stat.st_mtime ||
9090cb9e 234 (config_time.set && cache_stat.st_mtime < config_time.time))
03a212e5
PL
235 {
236 FcCache md;
68355f38 237 off_t off;
03a212e5 238
ae2aafe6 239 FcStrSetAdd (staleDirs, (FcChar8 *)name_buf);
fff5a5af
PL
240
241 /* skip subdirs */
242 while (FcCacheReadString (cache->fd, subdirName,
243 sizeof (subdirName)) &&
244 strlen (subdirName))
245 ;
246
68355f38
PL
247 if (read (cache->fd, &md, sizeof (FcCache)) != sizeof(FcCache))
248 {
249 perror ("read metadata");
250 goto bail1;
251 }
252 off = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count;
fff5a5af
PL
253 if (lseek (cache->fd, off, SEEK_SET) != off)
254 {
68355f38
PL
255 perror ("lseek");
256 goto bail1;
257 }
258 continue;
03a212e5
PL
259 }
260
eb0cf671
PL
261 d = malloc (sizeof (FcGlobalCacheDir));
262 if (!d)
263 goto bail1;
1b7be377 264
eb0cf671
PL
265 d->next = cache->dirs;
266 cache->dirs = d;
1b7be377 267
8245771d 268 d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
eb0cf671 269 d->ent = 0;
fff5a5af 270 d->state = FcGCDirFileRead;
971cf180
PL
271
272 d->subdirs = FcStrSetCreate();
2c4e0124
PL
273 do
274 {
275 if (!FcCacheReadString (cache->fd, subdirName,
276 sizeof (subdirName)) ||
277 !strlen (subdirName))
278 break;
971cf180 279 FcStrSetAdd (d->subdirs, (FcChar8 *)subdirName);
2c4e0124 280 } while (1);
971cf180 281
81d97094 282 d->offset = lseek (cache->fd, 0, SEEK_CUR);
8cb4c56d
PL
283 if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
284 goto bail1;
285 targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
286 if (lseek (cache->fd, targ, SEEK_SET) != targ)
287 goto bail1;
1b7be377 288 }
eb0cf671 289 return;
1b7be377 290
eb0cf671
PL
291 bail1:
292 for (d = cache->dirs; d; d = next)
1b7be377 293 {
eb0cf671
PL
294 next = d->next;
295 free (d);
1b7be377 296 }
eb0cf671 297 cache->dirs = 0;
e58b50e8
PL
298
299 close (cache->fd);
300 cache->fd = -1;
301 return;
302
303 bail_and_destroy:
eb0cf671
PL
304 close (cache->fd);
305 cache->fd = -1;
e58b50e8
PL
306
307 if (stat ((char *) cache_file, &cache_stat) == 0)
308 unlink ((char *)cache_file);
309
eb0cf671 310 return;
e58b50e8 311
1b7be377
PL
312}
313
1b7be377 314FcBool
8245771d 315FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config)
1b7be377 316{
3616cbe2 317 FcGlobalCacheDir *d;
3616cbe2 318 int i;
1b7be377 319
eb0cf671 320 if (cache->fd == -1)
1b7be377 321 return FcFalse;
1b7be377 322
5c90509c
PL
323 if (config)
324 if (!(dir = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)dir)))
325 return FcFalse; /* non-existing directory */
4073203d 326
eb0cf671 327 for (d = cache->dirs; d; d = d->next)
1b7be377 328 {
fff5a5af 329 if (strcmp (d->name, dir) == 0)
1b7be377 330 {
fff5a5af 331 if (d->state == FcGCDirDisabled)
eb0cf671 332 return FcFalse;
3616cbe2 333
fff5a5af
PL
334 if (d->state == FcGCDirFileRead)
335 {
336 lseek (cache->fd, d->offset, SEEK_SET);
337 if (!FcDirCacheConsume (cache->fd, d->name, set, config))
338 return FcFalse;
339
3616cbe2
PL
340 for (i = 0; i < d->subdirs->num; i++)
341 FcStrSetAdd (dirs, (FcChar8 *)d->subdirs->strs[i]);
342
fff5a5af
PL
343 d->state = FcGCDirConsumed;
344 }
345 return FcTrue;
1b7be377 346 }
1b7be377 347 }
1b7be377 348
fff5a5af 349 return FcFalse;
1b7be377
PL
350}
351
fff5a5af
PL
352static FcGlobalCacheDir *
353FcGlobalCacheDirFind (FcGlobalCache *cache, const char *name)
354{
355 FcGlobalCacheDir * d;
356
357 if (!cache || !name)
358 return NULL;
359
360 for (d = cache->dirs; d; d = d->next)
361 if (strcmp((const char *)d->name, (const char *)name) == 0)
362 return d;
363
364 return NULL;
365 }
366
eb0cf671
PL
367FcBool
368FcGlobalCacheUpdate (FcGlobalCache *cache,
971cf180 369 FcStrSet *dirs,
fff5a5af 370 const char *orig_name,
8a0b0ed6
PL
371 FcFontSet *set,
372 FcConfig *config)
1b7be377 373{
971cf180
PL
374 FcGlobalCacheDir *d;
375 int i;
fff5a5af 376 const char *name;
1b7be377 377
fff5a5af
PL
378 name = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)orig_name);
379 if (!name)
1b7be377 380 {
fff5a5af
PL
381 fprintf(stderr, "Invalid directory name %s\n", orig_name);
382 return FcFalse;
24330d27 383 }
24330d27 384
fff5a5af
PL
385 d = FcGlobalCacheDirFind (cache, name);
386
eb0cf671 387 if (!d)
24330d27 388 {
eb0cf671
PL
389 d = malloc (sizeof (FcGlobalCacheDir));
390 if (!d)
391 return FcFalse;
392 d->next = cache->dirs;
393 cache->dirs = d;
fff5a5af
PL
394 } else {
395 /* free old resources */
396 FcStrFree ((FcChar8 *)d->name);
397 free (d->ent);
398 FcStrSetDestroy (d->subdirs);
24330d27 399 }
24330d27 400
eb0cf671 401 cache->updated = FcTrue;
24330d27 402
8245771d 403 d->name = (char *)FcStrCopy ((FcChar8 *)name);
eb0cf671
PL
404 d->ent = FcDirCacheProduce (set, &d->metadata);
405 d->offset = 0;
971cf180 406 d->subdirs = FcStrSetCreate();
fff5a5af 407 d->state = FcGCDirUpdated;
971cf180
PL
408 for (i = 0; i < dirs->num; i++)
409 FcStrSetAdd (d->subdirs, dirs->strs[i]);
eb0cf671 410 return FcTrue;
24330d27
KP
411}
412
413FcBool
327a7fd4 414FcGlobalCacheSave (FcGlobalCache *cache,
8a0b0ed6
PL
415 const FcChar8 *cache_file,
416 FcConfig *config)
24330d27 417{
0d745819 418 int fd, fd_orig;
327a7fd4 419 FcGlobalCacheDir *dir;
327a7fd4 420 FcAtomic *atomic;
eb0cf671
PL
421 off_t current_arch_start = 0, truncate_to;
422 char * current_arch_machine_name, * header;
24330d27 423
eb0cf671 424 if (!cache->updated)
24330d27
KP
425 return FcTrue;
426
daeed6e0 427#if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
a391da8f
KP
428 /* Set-UID programs can't safely update the cache */
429 if (getuid () != geteuid ())
430 return FcFalse;
daeed6e0 431#endif
a391da8f 432
134f6011
KP
433 atomic = FcAtomicCreate (cache_file);
434 if (!atomic)
1d879de2
PL
435 return FcFalse;
436
134f6011 437 if (!FcAtomicLock (atomic))
24330d27 438 goto bail1;
879af706 439 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT | O_BINARY,
eb0cf671
PL
440 S_IRUSR | S_IWUSR);
441 if (fd == -1)
24330d27 442 goto bail2;
f468f568 443 FcCacheWriteString (fd, FC_GLOBAL_MAGIC_COOKIE);
24330d27 444
879af706 445 fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY | O_BINARY);
1d879de2 446
eb0cf671 447 current_arch_machine_name = FcCacheMachineSignature ();
1d879de2
PL
448 if (fd_orig == -1)
449 current_arch_start = 0;
450 else
451 current_arch_start = FcCacheSkipToArch (fd_orig,
c60ec7cc 452 current_arch_machine_name);
1d879de2 453
eb0cf671 454 if (current_arch_start < 0)
f468f568
PL
455 {
456 off_t i = lseek(fd_orig, 0, SEEK_END);
457 if (i < strlen (FC_GLOBAL_MAGIC_COOKIE)+1)
458 i = strlen (FC_GLOBAL_MAGIC_COOKIE)+1;
459 current_arch_start = FcCacheNextOffset (i);
460 }
eb0cf671 461
1d879de2
PL
462 if (!FcCacheCopyOld(fd, fd_orig, current_arch_start))
463 goto bail3;
464
eb0cf671 465 current_arch_start = lseek(fd, 0, SEEK_CUR);
d6217cc6 466#if defined (HAVE_FTRUNCATE)
eb0cf671 467 if (ftruncate (fd, current_arch_start) == -1)
1d879de2 468 goto bail3;
d6217cc6
PL
469#else
470#if defined (HAVE_CHSIZE)
471 if (chsize (fd, current_arch_start) == -1)
472 goto bail3;
473#else
474 goto bail3;
475#endif
476#endif
eb0cf671 477
03a212e5
PL
478 header = malloc (10 + strlen (current_arch_machine_name));
479 if (!header)
1d879de2 480 goto bail3;
03a212e5 481
8cb4c56d 482 truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11;
eb0cf671 483 for (dir = cache->dirs; dir; dir = dir->next)
24330d27 484 {
0d745819
PL
485 int i;
486
fff5a5af
PL
487 if (dir->state == FcGCDirDisabled)
488 continue;
eb0cf671
PL
489 truncate_to += strlen(dir->name) + 1;
490 truncate_to += sizeof (FcCache);
1c5b6345 491 truncate_to = FcCacheNextOffset (truncate_to);
eb0cf671 492 truncate_to += dir->metadata.count;
971cf180
PL
493
494 for (i = 0; i < dir->subdirs->size; i++)
495 truncate_to += strlen((char *)dir->subdirs->strs[i]) + 1;
496 truncate_to ++;
eb0cf671
PL
497 }
498 truncate_to -= current_arch_start;
eb0cf671 499
8cb4c56d
PL
500 sprintf (header, "%8x ", (int)truncate_to);
501 strcat (header, current_arch_machine_name);
502 if (!FcCacheWriteString (fd, header))
1d879de2 503 goto bail4;
8cb4c56d 504
c814c301
PL
505 free (header);
506
eb0cf671
PL
507 for (dir = cache->dirs; dir; dir = dir->next)
508 {
0d745819 509 int i;
fff5a5af
PL
510 const char * d;
511 off_t off;
971cf180 512
fff5a5af
PL
513 if (!dir->name || dir->state == FcGCDirDisabled)
514 continue;
515 d = (const char *)FcConfigNormalizeFontDir (config, (const FcChar8 *)dir->name);
516 if (!d)
517 continue;
971cf180 518
fff5a5af
PL
519 if (dir->metadata.count && !dir->ent)
520 {
521 if (dir->state == FcGCDirUpdated || fd_orig < 0)
68355f38 522 {
fff5a5af 523 fprintf(stderr, "Invalid metadata entry for %s, skipping...\n", d);
68355f38
PL
524 continue;
525 }
fff5a5af
PL
526 /* copy the old content */
527 dir->ent = malloc (dir->metadata.count);
528 if (!dir->ent)
68355f38 529 {
fff5a5af 530 perror("malloc error");
68355f38
PL
531 continue;
532 }
fff5a5af
PL
533 off = FcCacheNextOffset (dir->offset + sizeof(FcCache));
534 if (lseek (fd_orig, off, SEEK_SET) != off)
535 {
536 perror("lseek");
537 free(dir->ent);
538 continue;
539 }
540 if (read (fd_orig, dir->ent, dir->metadata.count)
541 != dir->metadata.count)
542 {
543 perror("read");
544 free(dir->ent);
545 continue;
546 }
547 }
548
549 FcCacheWriteString (fd, d);
550
551 for (i = 0; i < dir->subdirs->size; i++)
552 FcCacheWriteString (fd, (char *)dir->subdirs->strs[i]);
553 FcCacheWriteString (fd, "");
554
555 if (write (fd, &dir->metadata, sizeof(FcCache)) != sizeof(FcCache))
556 {
557 perror ("write metadata");
558 free (dir->ent);
559 continue;
560 }
561 off = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
562 if (lseek (fd, off, SEEK_SET) != off)
563 {
564 perror ("lseek");
565 free (dir->ent);
566 continue;
567 }
568 if (dir->metadata.count)
569 {
68355f38
PL
570 if (write (fd, dir->ent, dir->metadata.count) != dir->metadata.count)
571 {
572 perror ("write dirent");
573 free (dir->ent);
574 continue;
575 }
68355f38 576 }
fff5a5af 577 free (dir->ent);
24330d27 578 }
eb0cf671 579 FcCacheWriteString (fd, "");
24330d27 580
eb0cf671 581 if (close (fd) == -1)
1d879de2 582 goto bail25;
fff5a5af
PL
583
584 close (fd_orig);
585 fd_orig = -1;
24330d27 586
134f6011 587 if (!FcAtomicReplaceOrig (atomic))
1d879de2 588 goto bail25;
24330d27 589
134f6011
KP
590 FcAtomicUnlock (atomic);
591 FcAtomicDestroy (atomic);
592
24330d27
KP
593 cache->updated = FcFalse;
594 return FcTrue;
595
1d879de2
PL
596 bail4:
597 free (header);
598 bail3:
599 if (fd_orig != -1)
600 close (fd_orig);
601
602 close (fd);
603 bail25:
134f6011 604 FcAtomicDeleteNew (atomic);
1d879de2 605 bail2:
134f6011 606 FcAtomicUnlock (atomic);
1d879de2 607 bail1:
134f6011 608 FcAtomicDestroy (atomic);
24330d27
KP
609 return FcFalse;
610}
611
212c9f43 612/*
eb0cf671
PL
613 * Find the next presumably-mmapable offset after the supplied file
614 * position.
212c9f43 615 */
4262e0b3
PL
616static int
617FcCacheNextOffset(off_t w)
179c3995 618{
7fd7221e 619 static long pagesize = -1;
c001a192
PL
620
621 if (w == -1)
622 return w;
623
7fd7221e 624 if (pagesize == -1)
d6217cc6 625#if defined (HAVE_SYSCONF)
7fd7221e 626 pagesize = sysconf(_SC_PAGESIZE);
d6217cc6
PL
627#else
628 pagesize = FC_HARDCODED_PAGESIZE;
629#endif
7fd7221e 630 if (w % pagesize == 0)
212c9f43
PL
631 return w;
632 else
7fd7221e 633 return ((w / pagesize)+1)*pagesize;
179c3995
KP
634}
635
212c9f43
PL
636/* return the address of the segment for the provided arch,
637 * or -1 if arch not found */
638static off_t
c60ec7cc 639FcCacheSkipToArch (int fd, const char * arch)
212c9f43 640{
2dbe7597
PL
641 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
642 char * candidate_arch;
212c9f43
PL
643 off_t current_arch_start = 0;
644
ea44e218 645 lseek (fd, 0, SEEK_SET);
c60ec7cc 646 FcCacheSkipString (fd);
ea44e218
PL
647 current_arch_start = lseek (fd, 0, SEEK_CUR);
648
212c9f43
PL
649 /* skip arches that are not the current arch */
650 while (1)
651 {
652 long bs;
653
8cb4c56d
PL
654 if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
655 return -1;
656
eb0cf671 657 if (FcCacheReadString (fd, candidate_arch_machine_name_count,
2dbe7597 658 sizeof (candidate_arch_machine_name_count)) == 0)
5e678e94 659 return -1;
2dbe7597
PL
660 if (!strlen(candidate_arch_machine_name_count))
661 return -1;
662 bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16);
8cb4c56d 663
bb6b1993 664 /* count = 0 should probably be distinguished from the !bs condition */
8cb4c56d
PL
665 if (!bs || bs < strlen (candidate_arch_machine_name_count))
666 return -1;
667
2dbe7597 668 candidate_arch++; /* skip leading space */
212c9f43 669
2dbe7597 670 if (strcmp (candidate_arch, arch)==0)
5e678e94 671 return current_arch_start;
212c9f43 672 current_arch_start += bs;
b17cf498 673 current_arch_start = FcCacheNextOffset (current_arch_start);
212c9f43 674 }
212c9f43
PL
675}
676
677/* Cuts out the segment at the file pointer (moves everything else
678 * down to cover it), and leaves the file pointer at the end of the
679 * file. */
212c9f43 680static FcBool
1d879de2 681FcCacheCopyOld (int fd, int fd_orig, off_t start)
212c9f43 682{
eb0cf671 683 char * buf = malloc (8192);
2dbe7597 684 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
4262e0b3 685 long bs;
212c9f43 686 int c, bytes_skipped;
1d879de2 687 off_t loc;
212c9f43
PL
688
689 if (!buf)
690 return FcFalse;
691
1d879de2
PL
692 loc = 0;
693 lseek (fd, 0, SEEK_SET); lseek (fd_orig, 0, SEEK_SET);
719f4b84 694 FcCacheSkipString (fd); FcCacheSkipString (fd_orig);
1d879de2
PL
695 do
696 {
697 int b = 8192;
698 if (loc + b > start)
699 b = start - loc;
700
701 if ((c = read (fd_orig, buf, b)) <= 0)
702 break;
703 if (write (fd, buf, c) < 0)
704 goto bail;
705
706 loc += c;
707 }
708 while (c > 0);
709
212c9f43 710 lseek (fd, start, SEEK_SET);
eb0cf671 711 if (FcCacheReadString (fd, candidate_arch_machine_name,
212c9f43
PL
712 sizeof (candidate_arch_machine_name)) == 0)
713 goto done;
2dbe7597 714 if (!strlen(candidate_arch_machine_name))
212c9f43
PL
715 goto done;
716
2dbe7597 717 bs = strtol(candidate_arch_machine_name, 0, 16);
212c9f43
PL
718 if (bs == 0)
719 goto done;
720
721 bytes_skipped = 0;
722 do
723 {
724 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
eb0cf671 725 if ((c = read (fd, buf, 8192)) <= 0)
212c9f43
PL
726 break;
727 lseek (fd, start+bytes_skipped, SEEK_SET);
728 if (write (fd, buf, c) < 0)
729 goto bail;
730 bytes_skipped += c;
731 }
732 while (c > 0);
733 lseek (fd, start+bytes_skipped, SEEK_SET);
734
735 done:
736 free (buf);
737 return FcTrue;
738
739 bail:
740 free (buf);
741 return FcFalse;
742}
743
55c8fa4f 744/* Does not check that the cache has the appropriate arch section. */
ec760b17
PL
745/* Also, this can be fooled if the original location has a stale
746 * cache, and the hashed location has an up-to-date cache. Oh well,
747 * sucks to be you in that case! */
1b7be377
PL
748FcBool
749FcDirCacheValid (const FcChar8 *dir)
750{
1b7be377 751 struct stat file_stat, dir_stat;
ea44e218 752 int fd;
1b7be377
PL
753
754 if (stat ((char *) dir, &dir_stat) < 0)
1b7be377 755 return FcFalse;
6bf23804 756
df3efc11 757 fd = FcDirCacheOpen (dir);
ea44e218
PL
758
759 if (fd < 0)
c7490074 760 return FcFalse;
ea44e218 761 if (fstat (fd, &file_stat) < 0)
df3efc11 762 goto bail;
ea44e218
PL
763
764 close (fd);
ea44e218 765
55c8fa4f
PL
766 /*
767 * If the directory has been modified more recently than
768 * the cache file, the cache is not valid
769 */
2b25f00c 770 if (dir_stat.st_mtime > file_stat.st_mtime)
55c8fa4f 771 return FcFalse;
2b25f00c 772
55c8fa4f 773 return FcTrue;
ea44e218 774
ea44e218 775 bail:
df3efc11 776 close (fd);
ea44e218 777 return FcFalse;
55c8fa4f
PL
778}
779
780/* Assumes that the cache file in 'dir' exists.
781 * Checks that the cache has the appropriate arch section. */
782FcBool
783FcDirCacheHasCurrentArch (const FcChar8 *dir)
784{
55c8fa4f
PL
785 int fd;
786 off_t current_arch_start;
787 char *current_arch_machine_name;
8c0d6921 788 FcCache metadata;
a68ce952 789 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
55c8fa4f 790
df3efc11 791 fd = FcDirCacheOpen (dir);
ea44e218
PL
792 if (fd < 0)
793 goto bail;
794
795 current_arch_machine_name = FcCacheMachineSignature();
c60ec7cc 796 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
8c0d6921
PL
797
798 if (current_arch_start >= 0)
799 {
a68ce952
PL
800 if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
801 goto bail1;
802
803 FcCacheSkipString (fd);
804
805 while (FcCacheReadString (fd, subdirName, sizeof (subdirName)) && strlen (subdirName) > 0)
806 ;
807
8c0d6921 808 if (read(fd, &metadata, sizeof(FcCache)) != sizeof(FcCache))
a68ce952 809 goto bail1;
8c0d6921
PL
810
811 if (metadata.magic != FC_CACHE_MAGIC)
a68ce952 812 goto bail1;
8c0d6921
PL
813 }
814
6bf23804
PL
815 close (fd);
816
817 if (current_arch_start < 0)
818 return FcFalse;
55c8fa4f
PL
819
820 return FcTrue;
ea44e218 821
a68ce952
PL
822 bail1:
823 close (fd);
ea44e218 824 bail:
ea44e218 825 return FcFalse;
55c8fa4f 826}
6bf23804 827
55c8fa4f 828FcBool
8a0b0ed6 829FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
55c8fa4f 830{
8a0b0ed6 831 char *cache_file;
6f9fcb51 832 char *cache_hashed = 0;
3bfae75d 833 int fd, collisions;
15d7bd0a 834 struct stat cache_stat;
3bfae75d 835 char name_buf[FC_MAX_FILE_LEN];
55c8fa4f 836
8a0b0ed6
PL
837 dir = FcConfigNormalizeFontDir (config, dir);
838 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
df3efc11
PL
839 if (!cache_file)
840 return FcFalse;
841
3bfae75d 842 /* First remove normal cache file. */
719f4b84
PL
843 if (stat ((char *) cache_file, &cache_stat) == 0)
844 unlink ((char *)cache_file);
3bfae75d
PL
845
846 /* Next remove any applicable hashed files. */
847 fd = -1; collisions = 0;
848 do
1178b569 849 {
6f9fcb51
PL
850 if (cache_hashed)
851 FcStrFree ((FcChar8 *)cache_hashed);
852
3bfae75d
PL
853 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
854 if (!cache_hashed)
855 goto bail;
856
857 if (fd > 0)
858 close (fd);
879af706 859 fd = open(cache_hashed, O_RDONLY | O_BINARY);
3bfae75d
PL
860 if (fd == -1)
861 {
3ea92166 862 FcStrFree ((FcChar8 *)cache_hashed);
3bfae75d
PL
863 FcStrFree ((FcChar8 *)cache_file);
864 return FcTrue;
865 }
866
c5411c4c 867 if (!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
6f9fcb51
PL
868 {
869 FcStrFree ((FcChar8 *)cache_hashed);
3bfae75d 870 goto bail;
6f9fcb51 871 }
3bfae75d
PL
872 } while (strcmp (name_buf, cache_file) != 0);
873
3bfae75d
PL
874 close (fd);
875
876 if (stat ((char *) cache_hashed, &cache_stat) == 0 &&
877 unlink ((char *)cache_hashed) != 0)
878 {
879 FcStrFree ((FcChar8 *)cache_hashed);
880 goto bail;
1178b569 881 }
55c8fa4f 882
1af0f574 883 FcStrFree ((FcChar8 *)cache_file);
3bfae75d 884 FcStrFree ((FcChar8 *)cache_hashed);
1b7be377 885 return FcTrue;
3bfae75d
PL
886
887 bail:
888 FcStrFree ((FcChar8 *)cache_file);
889 return FcFalse;
1b7be377
PL
890}
891
4262e0b3 892static int
1b7be377 893FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
8b413bb6 894 FcStrList *list, FcFontSet * set, FcStrSet *processed_dirs)
4262e0b3 895{
4262e0b3
PL
896 int ret = 0;
897 FcChar8 *dir;
4262e0b3
PL
898 FcStrSet *subdirs;
899 FcStrList *sublist;
900 struct stat statb;
fff5a5af 901 FcGlobalCacheDir *d;
4262e0b3
PL
902
903 /*
03a212e5 904 * Read in the results from 'list'.
4262e0b3
PL
905 */
906 while ((dir = FcStrListNext (list)))
907 {
9fad72ab
PL
908 if (!FcConfigAcceptFilename (config, dir))
909 continue;
910
fff5a5af
PL
911 /* Skip this directory if already updated
912 * to avoid the looped directories via symlinks
5c3deb29 913 * Clearly a dir not in fonts.conf shouldn't be globally cached.
fff5a5af 914 */
b10e7762 915 dir = (FcChar8 *)FcConfigNormalizeFontDir (config, dir);
5c3deb29
PL
916 if (!dir)
917 continue;
918
919 if (FcStrSetMember (processed_dirs, dir))
920 continue;
921 if (!FcStrSetAdd (processed_dirs, dir))
922 continue;
4262e0b3
PL
923
924 subdirs = FcStrSetCreate ();
925 if (!subdirs)
926 {
927 fprintf (stderr, "Can't create directory set\n");
928 ret++;
4262e0b3
PL
929 continue;
930 }
931
932 if (access ((char *) dir, X_OK) < 0)
933 {
934 switch (errno) {
935 case ENOENT:
936 case ENOTDIR:
937 case EACCES:
938 break;
939 default:
940 fprintf (stderr, "\"%s\": ", dir);
941 perror ("");
942 ret++;
943 }
944 FcStrSetDestroy (subdirs);
4262e0b3
PL
945 continue;
946 }
947 if (stat ((char *) dir, &statb) == -1)
948 {
949 fprintf (stderr, "\"%s\": ", dir);
950 perror ("");
951 FcStrSetDestroy (subdirs);
952 ret++;
4262e0b3
PL
953 continue;
954 }
955 if (!S_ISDIR (statb.st_mode))
956 {
957 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
958 FcStrSetDestroy (subdirs);
4262e0b3
PL
959 continue;
960 }
530e66b0
PL
961 if (FcDirCacheValid (dir) &&
962 FcDirCacheHasCurrentArch (dir) &&
963 FcDirCacheRead (set, subdirs, dir, config))
fff5a5af
PL
964 {
965 /* if an old entry is found in the global cache, disable it */
5c3deb29 966 if ((d = FcGlobalCacheDirFind (cache, (const char *)dir)) != NULL)
fff5a5af
PL
967 {
968 d->state = FcGCDirDisabled;
969 /* save the updated config later without this entry */
970 cache->updated = FcTrue;
971 }
972 }
973 else
4262e0b3 974 {
1b7be377 975 if (FcDebug () & FC_DBG_FONTSET)
03a212e5 976 printf ("cache scan dir %s\n", dir);
2304e38f 977
1b7be377
PL
978 FcDirScanConfig (set, subdirs, cache,
979 config->blanks, dir, FcFalse, config);
4262e0b3
PL
980 }
981 sublist = FcStrListCreate (subdirs);
982 FcStrSetDestroy (subdirs);
983 if (!sublist)
984 {
985 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
986 ret++;
4262e0b3
PL
987 continue;
988 }
8b413bb6 989 ret += FcCacheReadDirs (config, cache, sublist, set, processed_dirs);
4262e0b3
PL
990 }
991 FcStrListDone (list);
992 return ret;
993}
994
995FcFontSet *
1b7be377 996FcCacheRead (FcConfig *config, FcGlobalCache * cache)
4262e0b3 997{
8b413bb6
PL
998 FcFontSet *s = FcFontSetCreate();
999 FcStrSet *processed_dirs;
1000
4262e0b3
PL
1001 if (!s)
1002 return 0;
1003
8b413bb6
PL
1004 processed_dirs = FcStrSetCreate();
1005 if (!processed_dirs)
4262e0b3
PL
1006 goto bail;
1007
8b413bb6
PL
1008 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s, processed_dirs))
1009 goto bail1;
1010
1011 FcStrSetDestroy (processed_dirs);
4262e0b3
PL
1012 return s;
1013
8b413bb6
PL
1014 bail1:
1015 FcStrSetDestroy (processed_dirs);
4262e0b3
PL
1016 bail:
1017 FcFontSetDestroy (s);
1018 return 0;
1019}
1020
ea44e218
PL
1021static const char bin2hex[] = { '0', '1', '2', '3',
1022 '4', '5', '6', '7',
1023 '8', '9', 'a', 'b',
1024 'c', 'd', 'e', 'f' };
1025
1026static char *
1027FcDirCacheHashName (char * cache_file, int collisions)
1028{
1029 unsigned char hash[16], hex_hash[33];
1030 char *cache_hashed;
1031 unsigned char uscore = '_';
1032 int cnt, i;
1033 FcChar8 *tmp;
1034 struct MD5Context ctx;
1035
1036 MD5Init (&ctx);
1037 MD5Update (&ctx, (unsigned char *)cache_file, strlen (cache_file));
1038
1039 for (i = 0; i < collisions; i++)
1040 MD5Update (&ctx, &uscore, 1);
1041
1042 MD5Final (hash, &ctx);
1043
1044 for (cnt = 0; cnt < 16; ++cnt)
1045 {
1046 hex_hash[2*cnt] = bin2hex[hash[cnt] >> 4];
1047 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
1048 }
1049 hex_hash[32] = 0;
1050
1051 tmp = FcStrPlus ((FcChar8 *)hex_hash, (FcChar8 *)FC_CACHE_SUFFIX);
1052 if (!tmp)
1053 return 0;
1054
83b67390 1055 cache_hashed = (char *)FcStrPlus ((FcChar8 *)PKGCACHEDIR"/", tmp);
ea44e218
PL
1056 free (tmp);
1057
1058 return cache_hashed;
1059}
1060
1061/* Opens the hashed name for cache_file.
1062 * This would fail in the unlikely event of a collision and subsequent
1063 * removal of the file which originally caused the collision. */
1064static int
df3efc11 1065FcDirCacheOpen (const FcChar8 *dir)
ea44e218 1066{
df3efc11 1067 FcBool found;
ea44e218 1068 int fd = -1, collisions = 0;
df3efc11 1069 char *cache_file, *cache_hashed;
ea44e218 1070 char name_buf[FC_MAX_FILE_LEN];
df3efc11
PL
1071 struct stat dir_stat;
1072
d00c3cb5 1073 if (stat ((char *)dir, &dir_stat) == -1)
df3efc11 1074 return -1;
ea44e218 1075
d00c3cb5
PL
1076 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
1077 if (!cache_file)
df3efc11
PL
1078 return -1;
1079
8a0b0ed6 1080 found = FcFalse;
ea44e218
PL
1081 do
1082 {
d00c3cb5
PL
1083 struct stat c;
1084 FcChar8 *name_buf_dir;
1085
1086 if (fd >= 0)
1087 close (fd);
df3efc11 1088
ea44e218
PL
1089 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
1090 if (!cache_hashed)
575a37b7
PL
1091 {
1092 FcStrFree ((FcChar8 *)cache_file);
ea44e218 1093 return -1;
575a37b7 1094 }
ea44e218 1095
879af706 1096 fd = open(cache_hashed, O_RDONLY | O_BINARY);
3bfae75d
PL
1097 FcStrFree ((FcChar8 *)cache_hashed);
1098
ea44e218 1099 if (fd == -1)
d00c3cb5
PL
1100 break;
1101 if (!FcCacheReadString (fd, name_buf, sizeof (name_buf)) ||
1102 !strlen(name_buf))
1103 break;
df3efc11
PL
1104
1105 name_buf_dir = FcStrDirname ((FcChar8 *)name_buf);
1106 if (stat ((char *)name_buf_dir, &c) == -1)
1107 {
1108 FcStrFree (name_buf_dir);
1109 continue;
1110 }
1111 FcStrFree (name_buf_dir);
575a37b7 1112 found = (c.st_ino == dir_stat.st_ino) && (c.st_dev == dir_stat.st_dev);
df3efc11 1113 } while (!found);
ea44e218 1114
d00c3cb5
PL
1115 if (!found || fd < 0)
1116 {
1117 if (fd >= 0)
1118 close (fd);
1119 fd = open(cache_file, O_RDONLY | O_BINARY);
1120 }
1121
575a37b7 1122 FcStrFree ((FcChar8 *)cache_file);
d00c3cb5 1123 return fd;
ea44e218
PL
1124}
1125
212c9f43 1126/* read serialized state from the cache file */
2eb84374 1127FcBool
cd9bca69 1128FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *config)
212c9f43 1129{
ea44e218
PL
1130 int fd;
1131 char *current_arch_machine_name;
1132 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
1133 off_t current_arch_start = 0;
1134 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
212c9f43 1135
df3efc11 1136 fd = FcDirCacheOpen (dir);
ea44e218
PL
1137 if (fd < 0)
1138 goto bail;
212c9f43 1139
ea44e218
PL
1140 current_arch_machine_name = FcCacheMachineSignature();
1141 current_arch_start = FcCacheSkipToArch(fd,
c60ec7cc 1142 current_arch_machine_name);
212c9f43 1143 if (current_arch_start < 0)
4262e0b3 1144 goto bail1;
212c9f43
PL
1145
1146 lseek (fd, current_arch_start, SEEK_SET);
eb0cf671 1147 if (FcCacheReadString (fd, candidate_arch_machine_name,
212c9f43 1148 sizeof (candidate_arch_machine_name)) == 0)
4262e0b3 1149 goto bail1;
2304e38f 1150
c5411c4c 1151 while (FcCacheReadString (fd, subdirName, sizeof (subdirName)) && strlen (subdirName) > 0)
8245771d 1152 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
2304e38f 1153
cd9bca69 1154 if (!FcDirCacheConsume (fd, (const char *)dir, set, config))
eb0cf671
PL
1155 goto bail1;
1156
1157 close(fd);
eb0cf671
PL
1158 return FcTrue;
1159
1160 bail1:
2304e38f 1161 close (fd);
eb0cf671 1162 bail:
eb0cf671
PL
1163 return FcFalse;
1164}
1165
1166static FcBool
cd9bca69 1167FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config)
eb0cf671
PL
1168{
1169 FcCache metadata;
1170 void * current_dir_block;
c001a192 1171 off_t pos, endpos;
212c9f43 1172
68355f38
PL
1173 if (read(fd, &metadata, sizeof(FcCache)) != sizeof(FcCache))
1174 return FcFalse;
212c9f43 1175 if (metadata.magic != FC_CACHE_MAGIC)
eb0cf671 1176 return FcFalse;
212c9f43 1177
2dbe7597 1178 if (!metadata.count)
05a98eaf
PL
1179 {
1180 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
1181 lseek (fd, pos, SEEK_SET);
eadadf48
PL
1182 if (config)
1183 FcConfigAddFontDir (config, (FcChar8 *)dir);
eb0cf671 1184 return FcTrue;
05a98eaf 1185 }
2dbe7597 1186
fd77c154 1187 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
c001a192
PL
1188
1189 /* This is not failsafe (multi-arches can break it),
1190 * but fd has got to have at least as many bytes as
1191 * metadata.count, or something's going to go horribly wrong. */
1192 if (pos == (off_t)-1)
1193 return FcFalse;
1194
1195 endpos = lseek (fd, 0, SEEK_END);
1196 if (endpos == (off_t)-1 || endpos - pos < metadata.count)
1197 return FcFalse;
1198 if (lseek (fd, pos, SEEK_SET) == -1)
1199 return FcFalse;
1200
d6217cc6 1201#if defined(HAVE_MMAP) || defined(__CYGWIN__)
2dbe7597
PL
1202 current_dir_block = mmap (0, metadata.count,
1203 PROT_READ, MAP_SHARED, fd, pos);
1204 if (current_dir_block == MAP_FAILED)
eb0cf671 1205 return FcFalse;
d6217cc6
PL
1206#else
1207 current_dir_block = malloc (metadata.count);
1208 if (!current_dir_block)
1209 return FcFalse;
1210
1211 /* could also use CreateMappedViewOfFile under MinGW... */
1212 if (read (fd, current_dir_block, metadata.count) != metadata.count)
1213 goto bail;
1214#endif
1215 lseek (fd, pos+metadata.count, SEEK_SET);
eb0cf671 1216
e77c1718 1217 FcCacheAddBankDir (metadata.bank, dir);
cd9bca69
PL
1218 if (config)
1219 FcConfigAddFontDir (config, (FcChar8 *)dir);
e77c1718 1220
b8948e85 1221 if (!FcFontSetUnserialize (&metadata, set, current_dir_block))
d6217cc6 1222 goto bail;
b8948e85 1223
212c9f43 1224 return FcTrue;
d6217cc6
PL
1225 bail:
1226#if !(defined(HAVE_MMAP) || defined(__CYGWIN__))
1227 free (current_dir_block);
1228#endif
1229 return FcFalse;
eb0cf671
PL
1230}
1231
1232static void *
1233FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
1234{
1235 void * current_dir_block, * final_dir_block;
8245771d 1236 static unsigned int rand_state = 0;
8e351527 1237 int bank, needed_bytes_no_align;
eb0cf671 1238
d6217cc6 1239#if defined (HAVE_RAND_R)
eb0cf671
PL
1240 if (!rand_state)
1241 rand_state = time(0L);
1242 bank = rand_r(&rand_state);
1243
1244 while (FcCacheHaveBank(bank))
1245 bank = rand_r(&rand_state);
d6217cc6
PL
1246#else
1247 if (!rand_state)
1248 {
1249 rand_state = 1;
1250 srand (time (0L));
1251 }
1252 bank = rand();
1253
1254 while (FcCacheHaveBank(bank))
1255 bank = rand();
1256#endif
eb0cf671
PL
1257
1258 memset (metadata, 0, sizeof(FcCache));
1259 FcFontSetNewBank();
8e351527
PL
1260 needed_bytes_no_align = FcFontSetNeededBytes (set);
1261 metadata->count = needed_bytes_no_align +
7fd7221e 1262 FcFontSetNeededBytesAlign ();
eb0cf671
PL
1263 metadata->magic = FC_CACHE_MAGIC;
1264 metadata->bank = bank;
1265
8e351527
PL
1266 if (!needed_bytes_no_align) /* not a failure, no fonts to write */
1267 {
1268 /* no, you don't really need to write any bytes at all. */
1269 metadata->count = 0;
eb0cf671 1270 return 0;
8e351527 1271 }
eb0cf671
PL
1272
1273 current_dir_block = malloc (metadata->count);
1274 if (!current_dir_block)
1275 goto bail;
bb6b1993 1276 /* shut up valgrind */
8e351527 1277 memset (current_dir_block, 0, metadata->count);
eb0cf671
PL
1278 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
1279
8e351527 1280 if ((void *)((char *)current_dir_block+metadata->count) < final_dir_block)
eb0cf671
PL
1281 goto bail;
1282
1283 if (!FcFontSetSerialize (bank, set))
1284 goto bail;
1285
1286 return current_dir_block;
212c9f43 1287
4262e0b3 1288 bail:
eb0cf671
PL
1289 free (current_dir_block);
1290 return 0;
212c9f43
PL
1291}
1292
1293/* write serialized state to the cache file */
1294FcBool
2304e38f 1295FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
212c9f43 1296{
ea44e218 1297 char *cache_file;
3bfae75d 1298 char *cache_hashed;
1d879de2
PL
1299 int fd, fd_orig, i, dirs_count;
1300 FcAtomic *atomic;
1301 FcCache metadata;
1302 off_t current_arch_start = 0, truncate_to;
ea44e218
PL
1303 char name_buf[FC_MAX_FILE_LEN];
1304 int collisions;
8245771d 1305
1d879de2 1306 char *current_arch_machine_name, * header;
ea44e218 1307 void *current_dir_block = 0;
212c9f43 1308
cd9bca69
PL
1309 dir = FcConfigNormalizeFontDir (FcConfigGetCurrent(), dir);
1310 if (!dir)
1311 return FcFalse;
1312
ea44e218 1313 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
4262e0b3
PL
1314 if (!cache_file)
1315 goto bail;
1316
ea44e218
PL
1317 /* Ensure that we're not trampling a cache for some other dir. */
1318 /* This is slightly different from FcDirCacheOpen, since it
1319 * needs the filename, not the file descriptor. */
1320 fd = -1; collisions = 0;
1321 do
1322 {
3bfae75d
PL
1323 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
1324 if (!cache_hashed)
ea44e218
PL
1325 goto bail0;
1326
1327 if (fd > 0)
1328 close (fd);
879af706 1329 fd = open(cache_hashed, O_RDONLY | O_BINARY);
ea44e218
PL
1330 if (fd == -1)
1331 break;
c5411c4c
PL
1332 if(!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
1333 {
1334 close (fd);
ae2aafe6 1335 FcStrFree ((FcChar8 *)cache_hashed);
c7490074 1336 continue;
c5411c4c
PL
1337 }
1338 close (fd);
ea44e218 1339
ae2aafe6
PL
1340 if (strcmp (name_buf, cache_file) != 0)
1341 {
1342 FcStrFree ((FcChar8 *)cache_hashed);
ea44e218 1343 continue;
ae2aafe6 1344 }
c7490074
PL
1345
1346 break;
1347 } while (1);
ea44e218 1348
eb0cf671 1349 current_dir_block = FcDirCacheProduce (set, &metadata);
212c9f43 1350
a9698bed 1351 if (metadata.count && !current_dir_block)
ea44e218 1352 goto bail1;
212c9f43 1353
4262e0b3
PL
1354 if (FcDebug () & FC_DBG_CACHE)
1355 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
1356
3bfae75d 1357 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
1d879de2 1358 if (!atomic)
ea44e218 1359 goto bail1;
212c9f43 1360
1d879de2 1361 if (!FcAtomicLock (atomic))
ec760b17
PL
1362 {
1363 /* Now try rewriting the original version of the file. */
1364 FcAtomicDestroy (atomic);
1d879de2 1365
3bfae75d 1366 atomic = FcAtomicCreate ((FcChar8 *)cache_file);
879af706 1367 fd_orig = open (cache_file, O_RDONLY | O_BINARY);
ec760b17 1368 if (fd_orig == -1)
879af706 1369 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY | O_BINARY);
ec760b17 1370
879af706 1371 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
ec760b17
PL
1372 if (fd == -1)
1373 goto bail2;
1374 }
1375
1376 /* In all cases, try opening the real location of the cache file first. */
1377 /* (even if that's not atomic.) */
879af706 1378 fd_orig = open (cache_file, O_RDONLY | O_BINARY);
ec760b17 1379 if (fd_orig == -1)
879af706 1380 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY | O_BINARY);
1d879de2 1381
879af706 1382 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
1d879de2 1383 if (fd == -1)
ec760b17 1384 goto bail3;
ea44e218
PL
1385
1386 FcCacheWriteString (fd, cache_file);
1d879de2 1387
eb0cf671 1388 current_arch_machine_name = FcCacheMachineSignature ();
1d879de2
PL
1389 current_arch_start = 0;
1390
1391 if (fd_orig != -1)
1392 current_arch_start =
c60ec7cc 1393 FcCacheSkipToArch(fd_orig, current_arch_machine_name);
1d879de2 1394
212c9f43 1395 if (current_arch_start < 0)
f468f568 1396 {
0d745819
PL
1397 off_t offset = lseek(fd_orig, 0, SEEK_END);
1398 current_arch_start = FcCacheNextOffset (offset);
f468f568 1399 }
212c9f43 1400
1d879de2 1401 if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
ea44e218 1402 goto bail4;
1d879de2
PL
1403
1404 if (fd_orig != -1)
1405 close (fd_orig);
212c9f43
PL
1406
1407 current_arch_start = lseek(fd, 0, SEEK_CUR);
d6217cc6 1408#if defined (HAVE_FTRUNCATE)
212c9f43 1409 if (ftruncate (fd, current_arch_start) == -1)
ea44e218 1410 goto bail4;
d6217cc6
PL
1411#else
1412#if defined (HAVE_CHSIZE)
1413 if (chsize (fd, current_arch_start) == -1)
1414 goto bail4;
1415#else
1416 goto bail4;
1417#endif
1418#endif
212c9f43 1419
6aee8c6f
PL
1420 /* allocate space for subdir names in this block */
1421 dirs_count = 0;
1422 for (i = 0; i < dirs->size; i++)
1423 dirs_count += strlen((char *)dirs->strs[i]) + 1;
1424 dirs_count ++;
1425
212c9f43 1426 /* now write the address of the next offset */
6aee8c6f 1427 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start;
2dbe7597 1428 header = malloc (10 + strlen (current_arch_machine_name));
eb0cf671 1429 if (!header)
ea44e218 1430 goto bail4;
2dbe7597
PL
1431 sprintf (header, "%8x ", (int)truncate_to);
1432 strcat (header, current_arch_machine_name);
eb0cf671 1433 if (!FcCacheWriteString (fd, header))
ea44e218 1434 goto bail5;
212c9f43 1435
2304e38f 1436 for (i = 0; i < dirs->size; i++)
8245771d 1437 FcCacheWriteString (fd, (char *)dirs->strs[i]);
2304e38f
PL
1438 FcCacheWriteString (fd, "");
1439
fff5a5af
PL
1440 if (write (fd, &metadata, sizeof(FcCache)) != sizeof(FcCache))
1441 {
68355f38
PL
1442 perror("write metadata");
1443 goto bail5;
1444 }
a9698bed
PL
1445 if (metadata.count)
1446 {
68355f38
PL
1447 off_t off = FcCacheNextOffset (lseek(fd, 0, SEEK_END));
1448 if (lseek (fd, off, SEEK_SET) != off)
1449 perror("lseek");
1450 else if (write (fd, current_dir_block, metadata.count) !=
1451 metadata.count)
1452 perror("write current_dir_block");
a9698bed 1453 free (current_dir_block);
f2fb985c 1454 current_dir_block = 0;
a9698bed 1455 }
4262e0b3 1456
2dbe7597 1457 /* this actually serves to pad out the cache file, if needed */
d6217cc6 1458#if defined (HAVE_FTRUNCATE)
212c9f43 1459 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
ea44e218 1460 goto bail5;
d6217cc6
PL
1461#else
1462#if defined (HAVE_CHSIZE)
1463 if (chsize (fd, current_arch_start + truncate_to) == -1)
1464 goto bail5;
1465#else
1466 goto bail5;
1467#endif
1468#endif
212c9f43 1469
6f9fcb51 1470 free (header);
212c9f43 1471 close(fd);
1d879de2 1472 if (!FcAtomicReplaceOrig(atomic))
ae2aafe6 1473 goto bail3;
3bfae75d 1474 FcStrFree ((FcChar8 *)cache_hashed);
575a37b7 1475 FcStrFree ((FcChar8 *)cache_file);
1d879de2
PL
1476 FcAtomicUnlock (atomic);
1477 FcAtomicDestroy (atomic);
212c9f43
PL
1478 return FcTrue;
1479
ea44e218 1480 bail5:
0230c9f8 1481 free (header);
ea44e218 1482 bail4:
1d879de2 1483 close (fd);
ea44e218 1484 bail3:
1d879de2 1485 FcAtomicUnlock (atomic);
ea44e218 1486 bail2:
1d879de2 1487 FcAtomicDestroy (atomic);
ea44e218 1488 bail1:
3bfae75d 1489 FcStrFree ((FcChar8 *)cache_hashed);
eb0cf671 1490 bail0:
8245771d 1491 unlink ((char *)cache_file);
575a37b7 1492 FcStrFree ((FcChar8 *)cache_file);
1d879de2
PL
1493 if (current_dir_block)
1494 free (current_dir_block);
1495 bail:
212c9f43
PL
1496 return FcFalse;
1497}
1498
2dbe7597 1499static char *
eb0cf671 1500FcCacheMachineSignature ()
2dbe7597
PL
1501{
1502 static char buf[MACHINE_SIGNATURE_SIZE];
7fd7221e 1503 int32_t magic = ENDIAN_TEST;
2dbe7597
PL
1504 char * m = (char *)&magic;
1505
1506 sprintf (buf, "%2x%2x%2x%2x "
1507 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
7fd7221e 1508 "%4x %4x %4x %4x %4x %4x %4x %4x\n",
2dbe7597 1509 m[0], m[1], m[2], m[3],
cd310911
PL
1510 (unsigned int)sizeof (char),
1511 (unsigned int)sizeof (char *),
1512 (unsigned int)sizeof (int),
1513 (unsigned int)sizeof (FcPattern),
1514 (unsigned int)sizeof (FcPatternEltPtr),
1515 (unsigned int)sizeof (struct _FcPatternElt *),
1516 (unsigned int)sizeof (FcPatternElt),
1517 (unsigned int)sizeof (FcObjectPtr),
1518 (unsigned int)sizeof (FcValueListPtr),
1519 (unsigned int)sizeof (FcValue),
1520 (unsigned int)sizeof (FcValueBinding),
1521 (unsigned int)sizeof (struct _FcValueList *),
1522 (unsigned int)sizeof (FcCharSet),
1523 (unsigned int)sizeof (FcCharLeaf **),
1524 (unsigned int)sizeof (FcChar16 *),
1525 (unsigned int)sizeof (FcChar16),
1526 (unsigned int)sizeof (FcCharLeaf),
1527 (unsigned int)sizeof (FcChar32),
7fd7221e 1528 (unsigned int)sizeof (FcCache),
d6217cc6 1529#if defined (HAVE_SYSCONF)
7fd7221e 1530 (unsigned int)sysconf(_SC_PAGESIZE));
d6217cc6
PL
1531#else
1532 (unsigned int)FC_HARDCODED_PAGESIZE);
1533#endif
2dbe7597
PL
1534
1535 return buf;
1536}
1537
4262e0b3 1538static int banks_ptr = 0, banks_alloc = 0;
b8948e85 1539int * _fcBankId = 0, * _fcBankIdx = 0;
e77c1718 1540static const char ** bankDirs = 0;
4262e0b3 1541
eb0cf671 1542static FcBool
4262e0b3
PL
1543FcCacheHaveBank (int bank)
1544{
1545 int i;
1546
1547 if (bank < FC_BANK_FIRST)
1548 return FcTrue;
1549
1550 for (i = 0; i < banks_ptr; i++)
b8948e85 1551 if (_fcBankId[i] == bank)
4262e0b3
PL
1552 return FcTrue;
1553
1554 return FcFalse;
1555}
1556
1557int
b8948e85 1558FcCacheBankToIndexMTF (int bank)
4262e0b3 1559{
751932dd 1560 int i, j;
4262e0b3
PL
1561
1562 for (i = 0; i < banks_ptr; i++)
b8948e85 1563 if (_fcBankId[_fcBankIdx[i]] == bank)
751932dd 1564 {
b8948e85 1565 int t = _fcBankIdx[i];
751932dd
PL
1566
1567 for (j = i; j > 0; j--)
b8948e85
PL
1568 _fcBankIdx[j] = _fcBankIdx[j-1];
1569 _fcBankIdx[0] = t;
751932dd
PL
1570 return t;
1571 }
4262e0b3 1572
2dbe7597 1573 if (banks_ptr >= banks_alloc)
4262e0b3 1574 {
751932dd 1575 int * b, * bidx;
e77c1718
PL
1576 const char ** bds;
1577
b8948e85 1578 b = realloc (_fcBankId, (banks_alloc + 4) * sizeof(int));
4262e0b3
PL
1579 if (!b)
1580 return -1;
b8948e85 1581 _fcBankId = b;
751932dd 1582
b8948e85 1583 bidx = realloc (_fcBankIdx, (banks_alloc + 4) * sizeof(int));
751932dd
PL
1584 if (!bidx)
1585 return -1;
b8948e85 1586 _fcBankIdx = bidx;
751932dd 1587
e77c1718
PL
1588 bds = realloc (bankDirs, (banks_alloc + 4) * sizeof (char *));
1589 if (!bds)
1590 return -1;
1591 bankDirs = bds;
1592
4262e0b3
PL
1593 banks_alloc += 4;
1594 }
1595
1596 i = banks_ptr++;
b8948e85
PL
1597 _fcBankId[i] = bank;
1598 _fcBankIdx[i] = i;
4262e0b3
PL
1599 return i;
1600}
e77c1718
PL
1601
1602static void
1603FcCacheAddBankDir (int bank, const char * dir)
1604{
b8948e85 1605 int bi = FcCacheBankToIndexMTF (bank);
e77c1718
PL
1606
1607 if (bi < 0)
1608 return;
1609
1610 bankDirs[bi] = (const char *)FcStrCopy ((FcChar8 *)dir);
1611}
1612
1613const char *
1614FcCacheFindBankDir (int bank)
1615{
1616 int bi = FcCacheBankToIndex (bank);
1617 return bankDirs[bi];
1618}
1619
ea44e218
PL
1620/*
1621 * This code implements the MD5 message-digest algorithm.
1622 * The algorithm is due to Ron Rivest. This code was
1623 * written by Colin Plumb in 1993, no copyright is claimed.
1624 * This code is in the public domain; do with it what you wish.
1625 *
1626 * Equivalent code is available from RSA Data Security, Inc.
1627 * This code has been tested against that, and is equivalent,
1628 * except that you don't need to include two pages of legalese
1629 * with every copy.
1630 *
1631 * To compute the message digest of a chunk of bytes, declare an
1632 * MD5Context structure, pass it to MD5Init, call MD5Update as
1633 * needed on buffers full of bytes, and then call MD5Final, which
1634 * will fill a supplied 16-byte array with the digest.
1635 */
1636
1637#ifndef HIGHFIRST
1638#define byteReverse(buf, len) /* Nothing */
1639#else
1640/*
1641 * Note: this code is harmless on little-endian machines.
1642 */
1643void byteReverse(unsigned char *buf, unsigned longs)
1644{
1645 FcChar32 t;
1646 do {
1647 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1648 ((unsigned) buf[1] << 8 | buf[0]);
1649 *(FcChar32 *) buf = t;
1650 buf += 4;
1651 } while (--longs);
1652}
1653#endif
1654
1655/*
1656 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1657 * initialization constants.
1658 */
1659static void MD5Init(struct MD5Context *ctx)
1660{
1661 ctx->buf[0] = 0x67452301;
1662 ctx->buf[1] = 0xefcdab89;
1663 ctx->buf[2] = 0x98badcfe;
1664 ctx->buf[3] = 0x10325476;
1665
1666 ctx->bits[0] = 0;
1667 ctx->bits[1] = 0;
1668}
1669
1670/*
1671 * Update context to reflect the concatenation of another buffer full
1672 * of bytes.
1673 */
1674static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len)
1675{
1676 FcChar32 t;
1677
1678 /* Update bitcount */
1679
1680 t = ctx->bits[0];
1681 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1682 ctx->bits[1]++; /* Carry from low to high */
1683 ctx->bits[1] += len >> 29;
1684
1685 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1686
1687 /* Handle any leading odd-sized chunks */
1688
1689 if (t) {
1690 unsigned char *p = (unsigned char *) ctx->in + t;
1691
1692 t = 64 - t;
1693 if (len < t) {
1694 memcpy(p, buf, len);
1695 return;
1696 }
1697 memcpy(p, buf, t);
1698 byteReverse(ctx->in, 16);
1699 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1700 buf += t;
1701 len -= t;
1702 }
1703 /* Process data in 64-byte chunks */
1704
1705 while (len >= 64) {
1706 memcpy(ctx->in, buf, 64);
1707 byteReverse(ctx->in, 16);
1708 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1709 buf += 64;
1710 len -= 64;
1711 }
1712
1713 /* Handle any remaining bytes of data. */
1714
1715 memcpy(ctx->in, buf, len);
1716}
1717
1718/*
1719 * Final wrapup - pad to 64-byte boundary with the bit pattern
1720 * 1 0* (64-bit count of bits processed, MSB-first)
1721 */
1722static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1723{
1724 unsigned count;
1725 unsigned char *p;
1726
1727 /* Compute number of bytes mod 64 */
1728 count = (ctx->bits[0] >> 3) & 0x3F;
1729
1730 /* Set the first char of padding to 0x80. This is safe since there is
1731 always at least one byte free */
1732 p = ctx->in + count;
1733 *p++ = 0x80;
1734
1735 /* Bytes of padding needed to make 64 bytes */
1736 count = 64 - 1 - count;
1737
1738 /* Pad out to 56 mod 64 */
1739 if (count < 8) {
1740 /* Two lots of padding: Pad the first block to 64 bytes */
1741 memset(p, 0, count);
1742 byteReverse(ctx->in, 16);
1743 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1744
1745 /* Now fill the next block with 56 bytes */
1746 memset(ctx->in, 0, 56);
1747 } else {
1748 /* Pad block to 56 bytes */
1749 memset(p, 0, count - 8);
1750 }
1751 byteReverse(ctx->in, 14);
1752
1753 /* Append length in bits and transform */
1754 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1755 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1756
1757 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1758 byteReverse((unsigned char *) ctx->buf, 4);
1759 memcpy(digest, ctx->buf, 16);
1760 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
1761}
1762
1763
1764/* The four core functions - F1 is optimized somewhat */
1765
1766/* #define F1(x, y, z) (x & y | ~x & z) */
1767#define F1(x, y, z) (z ^ (x & (y ^ z)))
1768#define F2(x, y, z) F1(z, x, y)
1769#define F3(x, y, z) (x ^ y ^ z)
1770#define F4(x, y, z) (y ^ (x | ~z))
1771
1772/* This is the central step in the MD5 algorithm. */
1773#define MD5STEP(f, w, x, y, z, data, s) \
1774 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1775
1776/*
1777 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1778 * reflect the addition of 16 longwords of new data. MD5Update blocks
1779 * the data and converts bytes into longwords for this routine.
1780 */
1781static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1782{
1783 register FcChar32 a, b, c, d;
1784
1785 a = buf[0];
1786 b = buf[1];
1787 c = buf[2];
1788 d = buf[3];
1789
1790 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1791 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1792 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1793 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1794 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1795 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1796 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1797 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1798 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1799 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1800 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1801 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1802 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1803 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1804 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1805 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1806
1807 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1808 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1809 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1810 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1811 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1812 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1813 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1814 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1815 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1816 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1817 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1818 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1819 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1820 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1821 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1822 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1823
1824 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1825 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1826 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1827 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1828 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1829 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1830 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1831 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1832 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1833 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1834 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1835 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1836 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1837 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1838 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1839 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1840
1841 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1842 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1843 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1844 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1845 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1846 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1847 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1848 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1849 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1850 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1851 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1852 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1853 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1854 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1855 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1856 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1857
1858 buf[0] += a;
1859 buf[1] += b;
1860 buf[2] += c;
1861 buf[3] += d;
1862}