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