]> git.wh0rd.org Git - fontconfig.git/blob - src/fccache.c
Improve error message when fc-cache can't write the cache. Add missing
[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, FcBool global);
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     current_arch_machine_name = FcCacheMachineSignature ();
223     current_arch_start = FcCacheSkipToArch(cache->fd, 
224                                            current_arch_machine_name, FcTrue);
225     if (current_arch_start < 0)
226         goto bail_and_destroy;
227
228     lseek (cache->fd, current_arch_start, SEEK_SET);
229     FcCacheReadString (cache->fd, candidate_arch_machine_name, 
230                        sizeof (candidate_arch_machine_name));
231     if (strlen(candidate_arch_machine_name) == 0)
232         goto bail_and_destroy;
233
234     while (1) 
235     {
236         off_t targ;
237
238         FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
239         if (!strlen(name_buf))
240             break;
241
242         /* Directory must be older than the global cache file; also
243            cache must be newer than the config file. */
244         if (stat ((char *) name_buf, &dir_stat) < 0 || 
245             dir_stat.st_mtime > cache_stat.st_mtime ||
246             (config_time.set && cache_stat.st_mtime < config_time.time))
247         {
248             FcCache md;
249
250             FcStrSetAdd (staleDirs, FcStrCopy ((FcChar8 *)name_buf));
251             read (cache->fd, &md, sizeof (FcCache));
252             lseek (cache->fd, FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count, SEEK_SET);
253             continue;
254         }
255
256         d = malloc (sizeof (FcGlobalCacheDir));
257         if (!d)
258             goto bail1;
259
260         d->next = cache->dirs;
261         cache->dirs = d;
262
263         d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
264         d->ent = 0;
265         d->offset = lseek (cache->fd, 0, SEEK_CUR);
266         if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
267             goto bail1;
268         targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
269         if (lseek (cache->fd, targ, SEEK_SET) != targ)
270             goto bail1;
271     }
272     return;
273
274  bail1:
275     for (d = cache->dirs; d; d = next)
276     {
277         next = d->next;
278         free (d);
279     }
280     cache->dirs = 0;
281
282     close (cache->fd);
283     cache->fd = -1;
284     return;
285
286  bail_and_destroy:
287     close (cache->fd);
288     cache->fd = -1;
289
290     if (stat ((char *) cache_file, &cache_stat) == 0)
291         unlink ((char *)cache_file);
292
293     return;
294
295 }
296
297 FcBool
298 FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config)
299 {
300     FcGlobalCacheDir *d;
301     FcBool ret = FcFalse;
302
303     if (cache->fd == -1)
304         return FcFalse;
305
306     for (d = cache->dirs; d; d = d->next)
307     {
308         if (strncmp (d->name, dir, strlen(dir)) == 0)
309         {
310             lseek (cache->fd, d->offset, SEEK_SET);
311             if (!FcDirCacheConsume (cache->fd, dir, set))
312                 return FcFalse;
313             if (strcmp (d->name, dir) == 0)
314                 ret = FcTrue;
315         }
316     }
317
318     return ret;
319 }
320
321 FcBool
322 FcGlobalCacheUpdate (FcGlobalCache  *cache,
323                      const char     *name,
324                      FcFontSet      *set)
325 {
326     FcGlobalCacheDir * d;
327
328     if (!set->nfont)
329         return FcTrue;
330
331     for (d = cache->dirs; d; d = d->next)
332     {
333         if (strcmp(d->name, name) == 0)
334             break;
335     }
336
337     if (!d)
338     {
339         d = malloc (sizeof (FcGlobalCacheDir));
340         if (!d)
341             return FcFalse;
342         d->next = cache->dirs;
343         cache->dirs = d;
344     }
345
346     cache->updated = FcTrue;
347
348     d->name = (char *)FcStrCopy ((FcChar8 *)name);
349     d->ent = FcDirCacheProduce (set, &d->metadata);
350     d->offset = 0;
351     return FcTrue;
352 }
353
354 FcBool
355 FcGlobalCacheSave (FcGlobalCache    *cache,
356                    const FcChar8    *cache_file)
357 {
358     int                 fd, fd_orig;
359     FcGlobalCacheDir    *dir;
360     FcAtomic            *atomic;
361     off_t               current_arch_start = 0, truncate_to;
362     char                * current_arch_machine_name, * header;
363
364     if (!cache->updated)
365         return FcTrue;
366     
367 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
368     /* Set-UID programs can't safely update the cache */
369     if (getuid () != geteuid ())
370         return FcFalse;
371 #endif
372     
373     atomic = FcAtomicCreate (cache_file);
374     if (!atomic)
375         return FcFalse;
376
377     if (!FcAtomicLock (atomic))
378         goto bail1;
379     fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT, 
380                S_IRUSR | S_IWUSR);
381     if (fd == -1)
382         goto bail2;
383
384     fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY);
385
386     current_arch_machine_name = FcCacheMachineSignature ();
387     if (fd_orig == -1)
388         current_arch_start = 0;
389     else
390         current_arch_start = FcCacheSkipToArch (fd_orig, 
391                                                 current_arch_machine_name, 
392                                                 FcTrue);
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     sprintf (header, "%8x ", (int)truncate_to);
422     strcat (header, current_arch_machine_name);
423     if (!FcCacheWriteString (fd, header))
424         goto bail4;
425
426     for (dir = cache->dirs; dir; dir = dir->next)
427     {
428         if (dir->ent)
429         {
430             FcCacheWriteString (fd, dir->name);
431             write (fd, &dir->metadata, sizeof(FcCache));
432             lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET);
433             write (fd, dir->ent, dir->metadata.count);
434             free (dir->ent);
435         }
436     }
437     FcCacheWriteString (fd, "");
438
439     if (close (fd) == -1)
440         goto bail25;
441     
442     if (!FcAtomicReplaceOrig (atomic))
443         goto bail25;
444     
445     FcAtomicUnlock (atomic);
446     FcAtomicDestroy (atomic);
447
448     cache->updated = FcFalse;
449     return FcTrue;
450
451  bail4:
452     free (header);
453  bail3:
454     if (fd_orig != -1)
455         close (fd_orig);
456
457     close (fd);
458  bail25:
459     FcAtomicDeleteNew (atomic);
460  bail2:
461     FcAtomicUnlock (atomic);
462  bail1:
463     FcAtomicDestroy (atomic);
464     return FcFalse;
465 }
466
467 /* 
468  * Find the next presumably-mmapable offset after the supplied file
469  * position.
470  */
471 static int
472 FcCacheNextOffset(off_t w)
473 {
474     static long pagesize = -1;
475     if (pagesize == -1)
476         pagesize = sysconf(_SC_PAGESIZE);
477     if (w % pagesize == 0) 
478         return w;
479     else
480         return ((w / pagesize)+1)*pagesize;
481 }
482
483 /* return the address of the segment for the provided arch,
484  * or -1 if arch not found */
485 static off_t
486 FcCacheSkipToArch (int fd, const char * arch, FcBool global)
487 {
488     char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
489     char * candidate_arch;
490     off_t current_arch_start = 0;
491
492     lseek (fd, 0, SEEK_SET);
493     if (!global)
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 FcBool
594 FcDirCacheValid (const FcChar8 *dir)
595 {
596     FcChar8     *cache_file;
597     struct stat file_stat, dir_stat;
598     int         fd;
599
600     if (stat ((char *) dir, &dir_stat) < 0)
601         return FcFalse;
602
603     cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
604     if (!cache_file)
605         return FcFalse;
606
607     fd = FcDirCacheOpen ((char *)cache_file);
608
609     if (fd < 0)
610         goto bail;
611     if (fstat (fd, &file_stat) < 0)
612         goto bail1;
613
614     close (fd);
615     FcStrFree (cache_file);
616
617     /*
618      * If the directory has been modified more recently than
619      * the cache file, the cache is not valid
620      */
621     if (dir_stat.st_mtime > file_stat.st_mtime)
622         return FcFalse;
623
624     return FcTrue;
625
626  bail1:
627     close (fd);
628  bail:
629     FcStrFree (cache_file);
630     return FcFalse;
631 }
632
633 /* Assumes that the cache file in 'dir' exists.
634  * Checks that the cache has the appropriate arch section. */
635 FcBool
636 FcDirCacheHasCurrentArch (const FcChar8 *dir)
637 {
638     char        *cache_file;
639     int         fd;
640     off_t       current_arch_start;
641     char        *current_arch_machine_name;
642
643     cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
644     if (!cache_file)
645         return FcFalse;
646
647     fd = FcDirCacheOpen (cache_file);
648     if (fd < 0)
649         goto bail;
650
651     current_arch_machine_name = FcCacheMachineSignature();
652     current_arch_start = FcCacheSkipToArch(fd, 
653                                            current_arch_machine_name, FcFalse);
654     close (fd);
655
656     if (current_arch_start < 0)
657         return FcFalse;
658     
659     return FcTrue;
660
661  bail:
662     free (cache_file);
663     return FcFalse;
664 }
665
666 FcBool
667 FcDirCacheUnlink (const FcChar8 *dir)
668 {
669     FcChar8     *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
670     struct stat cache_stat;
671
672     if (stat ((char *) cache_file, &cache_stat) == 0 &&
673         unlink ((char *)cache_file) != 0)
674     {
675         FcStrFree (cache_file);
676         return FcFalse;
677     }
678
679     FcStrFree (cache_file);
680     return FcTrue;
681 }
682
683 static int
684 FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache, 
685                  FcStrList *list, FcFontSet * set)
686 {
687     int                 ret = 0;
688     FcChar8             *dir;
689     FcChar8             *file, *base;
690     FcStrSet            *subdirs;
691     FcStrList           *sublist;
692     struct stat         statb;
693
694     /*
695      * Read in the results from 'list'.
696      */
697     while ((dir = FcStrListNext (list)))
698     {
699         if (!FcConfigAcceptFilename (config, dir))
700             continue;
701
702         /* freed below */
703         file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
704         if (!file)
705             return FcFalse;
706
707         strcpy ((char *) file, (char *) dir);
708         strcat ((char *) file, "/");
709         base = file + strlen ((char *) file);
710
711         subdirs = FcStrSetCreate ();
712         if (!subdirs)
713         {
714             fprintf (stderr, "Can't create directory set\n");
715             ret++;
716             free (file);
717             continue;
718         }
719         
720         if (access ((char *) dir, X_OK) < 0)
721         {
722             switch (errno) {
723             case ENOENT:
724             case ENOTDIR:
725             case EACCES:
726                 break;
727             default:
728                 fprintf (stderr, "\"%s\": ", dir);
729                 perror ("");
730                 ret++;
731             }
732             FcStrSetDestroy (subdirs);
733             free (file);
734             continue;
735         }
736         if (stat ((char *) dir, &statb) == -1)
737         {
738             fprintf (stderr, "\"%s\": ", dir);
739             perror ("");
740             FcStrSetDestroy (subdirs);
741             ret++;
742             free (file);
743             continue;
744         }
745         if (!S_ISDIR (statb.st_mode))
746         {
747             fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
748             FcStrSetDestroy (subdirs);
749             free (file);
750             continue;
751         }
752         if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir))
753         {
754             if (FcDebug () & FC_DBG_FONTSET)
755                 printf ("cache scan dir %s\n", dir);
756
757             FcDirScanConfig (set, subdirs, cache, 
758                              config->blanks, dir, FcFalse, config);
759         }
760         sublist = FcStrListCreate (subdirs);
761         FcStrSetDestroy (subdirs);
762         if (!sublist)
763         {
764             fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
765             ret++;
766             free (file);
767             continue;
768         }
769         ret += FcCacheReadDirs (config, cache, sublist, set);
770         free (file);
771     }
772     FcStrListDone (list);
773     return ret;
774 }
775
776 FcFontSet *
777 FcCacheRead (FcConfig *config, FcGlobalCache * cache)
778 {
779     FcFontSet * s = FcFontSetCreate();
780     if (!s) 
781         return 0;
782
783     if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
784         goto bail;
785
786     return s;
787
788  bail:
789     FcFontSetDestroy (s);
790     return 0;
791 }
792
793 static const char bin2hex[] = { '0', '1', '2', '3',
794                                 '4', '5', '6', '7',
795                                 '8', '9', 'a', 'b',
796                                 'c', 'd', 'e', 'f' };
797
798 static char *
799 FcDirCacheHashName (char * cache_file, int collisions)
800 {
801     unsigned char       hash[16], hex_hash[33];
802     char                *cache_hashed;
803     unsigned char       uscore = '_';
804     int                 cnt, i;
805     FcChar8             *tmp;
806     struct MD5Context   ctx;
807
808     MD5Init (&ctx);
809     MD5Update (&ctx, (unsigned char *)cache_file, strlen (cache_file));
810
811     for (i = 0; i < collisions; i++)
812         MD5Update (&ctx, &uscore, 1);
813
814     MD5Final (hash, &ctx);
815
816     for (cnt = 0; cnt < 16; ++cnt)
817     {
818         hex_hash[2*cnt] = bin2hex[hash[cnt] >> 4];
819         hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
820     }
821     hex_hash[32] = 0;
822
823     tmp = FcStrPlus ((FcChar8 *)hex_hash, (FcChar8 *)FC_CACHE_SUFFIX);
824     if (!tmp)
825         return 0;
826
827     cache_hashed = (char *)FcStrPlus ((FcChar8 *)PKGCACHEDIR"/", tmp);
828     free (tmp);
829
830     return cache_hashed;
831 }
832
833 /* Opens the hashed name for cache_file.
834  * This would fail in the unlikely event of a collision and subsequent
835  * removal of the file which originally caused the collision. */
836 static int
837 FcDirCacheOpen (char *cache_file)
838 {
839     int         fd = -1, collisions = 0;
840     char        *cache_hashed;
841     char        name_buf[FC_MAX_FILE_LEN];
842
843     do
844     {
845         cache_hashed = FcDirCacheHashName (cache_file, collisions++);
846         if (!cache_hashed)
847             return -1;
848
849         if (fd > 0)
850             close (fd);
851         fd = open(cache_hashed, O_RDONLY);
852         if (fd == -1)
853             return -1;
854         FcCacheReadString (fd, name_buf, sizeof (name_buf));
855         if (!strlen(name_buf))
856             goto bail;
857     } while (strcmp (name_buf, cache_file) != 0);
858     return fd;
859
860  bail:
861     close (fd);
862     return -1;
863 }
864
865 /* read serialized state from the cache file */
866 FcBool
867 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir)
868 {
869     char        *cache_file;
870     int         fd;
871     char        *current_arch_machine_name;
872     char        candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
873     off_t       current_arch_start = 0;
874     char        subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
875
876     cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
877     if (!cache_file)
878         return FcFalse;
879
880     fd = FcDirCacheOpen (cache_file);
881     if (fd < 0)
882         goto bail;
883
884     current_arch_machine_name = FcCacheMachineSignature();
885     current_arch_start = FcCacheSkipToArch(fd, 
886                                            current_arch_machine_name,
887                                            FcFalse);
888     if (current_arch_start < 0)
889         goto bail1;
890
891     lseek (fd, current_arch_start, SEEK_SET);
892     if (FcCacheReadString (fd, candidate_arch_machine_name, 
893                            sizeof (candidate_arch_machine_name)) == 0)
894         goto bail1;
895
896     while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
897         FcStrSetAdd (dirs, (FcChar8 *)subdirName);
898
899     if (!FcDirCacheConsume (fd, (const char *)dir, set))
900         goto bail1;
901         
902     close(fd);
903     free (cache_file);
904     return FcTrue;
905
906  bail1:
907     close (fd);
908  bail:
909     free (cache_file);
910     return FcFalse;
911 }
912
913 static FcBool
914 FcDirCacheConsume (int fd, const char * dir, FcFontSet *set)
915 {
916     FcCache metadata;
917     void * current_dir_block;
918     off_t pos;
919
920     read(fd, &metadata, sizeof(FcCache));
921     if (metadata.magic != FC_CACHE_MAGIC)
922         return FcFalse;
923
924     if (!metadata.count)
925         return FcTrue;
926
927     pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
928     current_dir_block = mmap (0, metadata.count, 
929                               PROT_READ, MAP_SHARED, fd, pos);
930     if (current_dir_block == MAP_FAILED)
931         return FcFalse;
932
933     FcCacheAddBankDir (metadata.bank, dir);
934
935     if (!FcFontSetUnserialize (&metadata, set, current_dir_block))
936         return FcFalse;
937
938     return FcTrue;
939 }
940
941 static void *
942 FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
943 {
944     void * current_dir_block, * final_dir_block;
945     static unsigned int rand_state = 0;
946     int bank, needed_bytes_no_align;
947
948     if (!rand_state) 
949         rand_state = time(0L);
950     bank = rand_r(&rand_state);
951
952     while (FcCacheHaveBank(bank))
953         bank = rand_r(&rand_state);
954
955     memset (metadata, 0, sizeof(FcCache));
956     FcFontSetNewBank();
957     needed_bytes_no_align = FcFontSetNeededBytes (set);
958     metadata->count = needed_bytes_no_align + 
959         FcFontSetNeededBytesAlign ();
960     metadata->magic = FC_CACHE_MAGIC;
961     metadata->bank = bank;
962
963     if (!needed_bytes_no_align) /* not a failure, no fonts to write */
964     {
965         /* no, you don't really need to write any bytes at all. */
966         metadata->count = 0;
967         return 0;
968     }
969
970     current_dir_block = malloc (metadata->count);
971     if (!current_dir_block)
972         goto bail;
973     // shut up valgrind
974     memset (current_dir_block, 0, metadata->count);
975     final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
976
977     if ((void *)((char *)current_dir_block+metadata->count) < final_dir_block)
978         goto bail;
979                               
980     if (!FcFontSetSerialize (bank, set))
981         goto bail;
982
983     return current_dir_block;
984
985  bail:
986     free (current_dir_block);
987     return 0;
988 }
989
990 /* write serialized state to the cache file */
991 FcBool
992 FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
993 {
994     char            *cache_file;
995     char            *cache_hashed;
996     int             fd, fd_orig, i, dirs_count;
997     FcAtomic        *atomic;
998     FcCache         metadata;
999     off_t           current_arch_start = 0, truncate_to;
1000     char            name_buf[FC_MAX_FILE_LEN];
1001     int             collisions;
1002
1003     char            *current_arch_machine_name, * header;
1004     void            *current_dir_block = 0;
1005
1006     cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
1007     if (!cache_file)
1008         goto bail;
1009
1010     /* Ensure that we're not trampling a cache for some other dir. */
1011     /* This is slightly different from FcDirCacheOpen, since it 
1012      * needs the filename, not the file descriptor. */
1013     fd = -1; collisions = 0;
1014     do
1015     {
1016         cache_hashed = FcDirCacheHashName (cache_file, collisions++);
1017         if (!cache_hashed)
1018             goto bail0;
1019
1020         if (fd > 0)
1021             close (fd);
1022         fd = open(cache_hashed, O_RDONLY);
1023         if (fd == -1)
1024             break;
1025         FcCacheReadString (fd, name_buf, sizeof (name_buf));
1026         close (fd);
1027
1028         if (!strlen(name_buf))
1029             break;
1030
1031         if (strcmp (name_buf, cache_file) != 0)
1032             continue;
1033     } while (0);
1034
1035     current_dir_block = FcDirCacheProduce (set, &metadata);
1036
1037     if (metadata.count && !current_dir_block)
1038         goto bail1;
1039
1040     if (FcDebug () & FC_DBG_CACHE)
1041         printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
1042
1043     atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
1044     if (!atomic)
1045         goto bail1;
1046
1047     if (!FcAtomicLock (atomic))
1048         goto bail2;
1049
1050     fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY, 0666);
1051
1052     fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1053     if (fd == -1)
1054         goto bail3;
1055
1056     FcCacheWriteString (fd, cache_file);
1057
1058     current_arch_machine_name = FcCacheMachineSignature ();
1059     current_arch_start = 0;
1060
1061     if (fd_orig != -1)
1062         current_arch_start = 
1063             FcCacheSkipToArch(fd_orig, current_arch_machine_name, FcFalse);
1064
1065     if (current_arch_start < 0)
1066         current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
1067
1068     if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
1069         goto bail4;
1070
1071     if (fd_orig != -1)
1072         close (fd_orig);
1073
1074     current_arch_start = lseek(fd, 0, SEEK_CUR);
1075     if (ftruncate (fd, current_arch_start) == -1)
1076         goto bail4;
1077
1078     /* allocate space for subdir names in this block */
1079     dirs_count = 0;
1080     for (i = 0; i < dirs->size; i++)
1081         dirs_count += strlen((char *)dirs->strs[i]) + 1;
1082     dirs_count ++;
1083
1084     /* now write the address of the next offset */
1085     truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start;
1086     header = malloc (10 + strlen (current_arch_machine_name));
1087     if (!header)
1088         goto bail4;
1089     sprintf (header, "%8x ", (int)truncate_to);
1090     strcat (header, current_arch_machine_name);
1091     if (!FcCacheWriteString (fd, header))
1092         goto bail5;
1093
1094     for (i = 0; i < dirs->size; i++)
1095         FcCacheWriteString (fd, (char *)dirs->strs[i]);
1096     FcCacheWriteString (fd, "");
1097
1098     write (fd, &metadata, sizeof(FcCache));
1099     if (metadata.count)
1100     {
1101         lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
1102         write (fd, current_dir_block, metadata.count);
1103         free (current_dir_block);
1104     }
1105
1106     /* this actually serves to pad out the cache file, if needed */
1107     if (ftruncate (fd, current_arch_start + truncate_to) == -1)
1108         goto bail5;
1109
1110     close(fd);
1111     if (!FcAtomicReplaceOrig(atomic))
1112         goto bail5;
1113     FcAtomicUnlock (atomic);
1114     FcAtomicDestroy (atomic);
1115     return FcTrue;
1116
1117  bail5:
1118     free (header);
1119  bail4:
1120     close (fd);
1121  bail3:
1122     FcAtomicUnlock (atomic);
1123  bail2:
1124     FcAtomicDestroy (atomic);
1125  bail1:
1126     free (cache_hashed);
1127  bail0:
1128     unlink ((char *)cache_file);
1129     free (cache_file);
1130     if (current_dir_block)
1131         free (current_dir_block);
1132  bail:
1133     return FcFalse;
1134 }
1135
1136 static char *
1137 FcCacheMachineSignature ()
1138 {
1139     static char buf[MACHINE_SIGNATURE_SIZE];
1140     int32_t magic = ENDIAN_TEST;
1141     char * m = (char *)&magic;
1142
1143     sprintf (buf, "%2x%2x%2x%2x "
1144              "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
1145              "%4x %4x %4x %4x %4x %4x %4x %4x\n", 
1146              m[0], m[1], m[2], m[3],
1147              (unsigned int)sizeof (char),
1148              (unsigned int)sizeof (char *),
1149              (unsigned int)sizeof (int),
1150              (unsigned int)sizeof (FcPattern),
1151              (unsigned int)sizeof (FcPatternEltPtr),
1152              (unsigned int)sizeof (struct _FcPatternElt *),
1153              (unsigned int)sizeof (FcPatternElt),
1154              (unsigned int)sizeof (FcObjectPtr),
1155              (unsigned int)sizeof (FcValueListPtr),
1156              (unsigned int)sizeof (FcValue),
1157              (unsigned int)sizeof (FcValueBinding),
1158              (unsigned int)sizeof (struct _FcValueList *),
1159              (unsigned int)sizeof (FcCharSet),
1160              (unsigned int)sizeof (FcCharLeaf **),
1161              (unsigned int)sizeof (FcChar16 *),
1162              (unsigned int)sizeof (FcChar16),
1163              (unsigned int)sizeof (FcCharLeaf),
1164              (unsigned int)sizeof (FcChar32),
1165              (unsigned int)sizeof (FcCache),
1166              (unsigned int)sysconf(_SC_PAGESIZE));
1167
1168     return buf;
1169 }
1170
1171 static int banks_ptr = 0, banks_alloc = 0;
1172 int * _fcBankId = 0, * _fcBankIdx = 0;
1173 static const char ** bankDirs = 0;
1174
1175 static FcBool
1176 FcCacheHaveBank (int bank)
1177 {
1178     int i;
1179
1180     if (bank < FC_BANK_FIRST)
1181         return FcTrue;
1182
1183     for (i = 0; i < banks_ptr; i++)
1184         if (_fcBankId[i] == bank)
1185             return FcTrue;
1186
1187     return FcFalse;
1188 }
1189
1190 int
1191 FcCacheBankToIndexMTF (int bank)
1192 {
1193     int i, j;
1194
1195     for (i = 0; i < banks_ptr; i++)
1196         if (_fcBankId[_fcBankIdx[i]] == bank)
1197         {
1198             int t = _fcBankIdx[i];
1199
1200             for (j = i; j > 0; j--)
1201                 _fcBankIdx[j] = _fcBankIdx[j-1];
1202             _fcBankIdx[0] = t;
1203             return t;
1204         }
1205
1206     if (banks_ptr >= banks_alloc)
1207     {
1208         int * b, * bidx;
1209         const char ** bds;
1210
1211         b = realloc (_fcBankId, (banks_alloc + 4) * sizeof(int));
1212         if (!b)
1213             return -1;
1214         _fcBankId = b;
1215
1216         bidx = realloc (_fcBankIdx, (banks_alloc + 4) * sizeof(int));
1217         if (!bidx)
1218             return -1;
1219         _fcBankIdx = bidx;
1220
1221         bds = realloc (bankDirs, (banks_alloc + 4) * sizeof (char *));
1222         if (!bds)
1223             return -1;
1224         bankDirs = bds;
1225
1226         banks_alloc += 4;
1227     }
1228
1229     i = banks_ptr++;
1230     _fcBankId[i] = bank;
1231     _fcBankIdx[i] = i;
1232     return i;
1233 }
1234
1235 static void
1236 FcCacheAddBankDir (int bank, const char * dir)
1237 {
1238     int bi = FcCacheBankToIndexMTF (bank);
1239
1240     if (bi < 0)
1241         return;
1242
1243     bankDirs[bi] = (const char *)FcStrCopy ((FcChar8 *)dir);
1244 }
1245
1246 const char *
1247 FcCacheFindBankDir (int bank)
1248 {
1249     int bi = FcCacheBankToIndex (bank);
1250     return bankDirs[bi];
1251 }
1252
1253 /*
1254  * This code implements the MD5 message-digest algorithm.
1255  * The algorithm is due to Ron Rivest.  This code was
1256  * written by Colin Plumb in 1993, no copyright is claimed.
1257  * This code is in the public domain; do with it what you wish.
1258  *
1259  * Equivalent code is available from RSA Data Security, Inc.
1260  * This code has been tested against that, and is equivalent,
1261  * except that you don't need to include two pages of legalese
1262  * with every copy.
1263  *
1264  * To compute the message digest of a chunk of bytes, declare an
1265  * MD5Context structure, pass it to MD5Init, call MD5Update as
1266  * needed on buffers full of bytes, and then call MD5Final, which
1267  * will fill a supplied 16-byte array with the digest.
1268  */
1269
1270 #ifndef HIGHFIRST
1271 #define byteReverse(buf, len)   /* Nothing */
1272 #else
1273 /*
1274  * Note: this code is harmless on little-endian machines.
1275  */
1276 void byteReverse(unsigned char *buf, unsigned longs)
1277 {
1278     FcChar32 t;
1279     do {
1280         t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1281             ((unsigned) buf[1] << 8 | buf[0]);
1282         *(FcChar32 *) buf = t;
1283         buf += 4;
1284     } while (--longs);
1285 }
1286 #endif
1287
1288 /*
1289  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
1290  * initialization constants.
1291  */
1292 static void MD5Init(struct MD5Context *ctx)
1293 {
1294     ctx->buf[0] = 0x67452301;
1295     ctx->buf[1] = 0xefcdab89;
1296     ctx->buf[2] = 0x98badcfe;
1297     ctx->buf[3] = 0x10325476;
1298
1299     ctx->bits[0] = 0;
1300     ctx->bits[1] = 0;
1301 }
1302
1303 /*
1304  * Update context to reflect the concatenation of another buffer full
1305  * of bytes.
1306  */
1307 static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len)
1308 {
1309     FcChar32 t;
1310
1311     /* Update bitcount */
1312
1313     t = ctx->bits[0];
1314     if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1315         ctx->bits[1]++;         /* Carry from low to high */
1316     ctx->bits[1] += len >> 29;
1317
1318     t = (t >> 3) & 0x3f;        /* Bytes already in shsInfo->data */
1319
1320     /* Handle any leading odd-sized chunks */
1321
1322     if (t) {
1323         unsigned char *p = (unsigned char *) ctx->in + t;
1324
1325         t = 64 - t;
1326         if (len < t) {
1327             memcpy(p, buf, len);
1328             return;
1329         }
1330         memcpy(p, buf, t);
1331         byteReverse(ctx->in, 16);
1332         MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1333         buf += t;
1334         len -= t;
1335     }
1336     /* Process data in 64-byte chunks */
1337
1338     while (len >= 64) {
1339         memcpy(ctx->in, buf, 64);
1340         byteReverse(ctx->in, 16);
1341         MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1342         buf += 64;
1343         len -= 64;
1344     }
1345
1346     /* Handle any remaining bytes of data. */
1347
1348     memcpy(ctx->in, buf, len);
1349 }
1350
1351 /*
1352  * Final wrapup - pad to 64-byte boundary with the bit pattern 
1353  * 1 0* (64-bit count of bits processed, MSB-first)
1354  */
1355 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1356 {
1357     unsigned count;
1358     unsigned char *p;
1359
1360     /* Compute number of bytes mod 64 */
1361     count = (ctx->bits[0] >> 3) & 0x3F;
1362
1363     /* Set the first char of padding to 0x80.  This is safe since there is
1364        always at least one byte free */
1365     p = ctx->in + count;
1366     *p++ = 0x80;
1367
1368     /* Bytes of padding needed to make 64 bytes */
1369     count = 64 - 1 - count;
1370
1371     /* Pad out to 56 mod 64 */
1372     if (count < 8) {
1373         /* Two lots of padding:  Pad the first block to 64 bytes */
1374         memset(p, 0, count);
1375         byteReverse(ctx->in, 16);
1376         MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1377
1378         /* Now fill the next block with 56 bytes */
1379         memset(ctx->in, 0, 56);
1380     } else {
1381         /* Pad block to 56 bytes */
1382         memset(p, 0, count - 8);
1383     }
1384     byteReverse(ctx->in, 14);
1385
1386     /* Append length in bits and transform */
1387     ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1388     ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1389
1390     MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1391     byteReverse((unsigned char *) ctx->buf, 4);
1392     memcpy(digest, ctx->buf, 16);
1393     memset(ctx, 0, sizeof(ctx));        /* In case it's sensitive */
1394 }
1395
1396
1397 /* The four core functions - F1 is optimized somewhat */
1398
1399 /* #define F1(x, y, z) (x & y | ~x & z) */
1400 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1401 #define F2(x, y, z) F1(z, x, y)
1402 #define F3(x, y, z) (x ^ y ^ z)
1403 #define F4(x, y, z) (y ^ (x | ~z))
1404
1405 /* This is the central step in the MD5 algorithm. */
1406 #define MD5STEP(f, w, x, y, z, data, s) \
1407         ( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
1408
1409 /*
1410  * The core of the MD5 algorithm, this alters an existing MD5 hash to
1411  * reflect the addition of 16 longwords of new data.  MD5Update blocks
1412  * the data and converts bytes into longwords for this routine.
1413  */
1414 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1415 {
1416     register FcChar32 a, b, c, d;
1417
1418     a = buf[0];
1419     b = buf[1];
1420     c = buf[2];
1421     d = buf[3];
1422
1423     MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1424     MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1425     MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1426     MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1427     MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1428     MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1429     MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1430     MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1431     MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1432     MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1433     MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1434     MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1435     MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1436     MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1437     MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1438     MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1439
1440     MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1441     MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1442     MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1443     MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1444     MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1445     MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1446     MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1447     MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1448     MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1449     MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1450     MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1451     MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1452     MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1453     MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1454     MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1455     MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1456
1457     MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1458     MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1459     MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1460     MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1461     MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1462     MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1463     MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1464     MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1465     MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1466     MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1467     MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1468     MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1469     MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1470     MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1471     MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1472     MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1473
1474     MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1475     MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1476     MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1477     MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1478     MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1479     MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1480     MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1481     MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1482     MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1483     MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1484     MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1485     MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1486     MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1487     MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1488     MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1489     MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1490
1491     buf[0] += a;
1492     buf[1] += b;
1493     buf[2] += c;
1494     buf[3] += d;
1495 }