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