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