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