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