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