]> git.wh0rd.org Git - fontconfig.git/blob - src/fccache.c
Normalize font dirs by using the form, as given in fonts.conf, and recorded
[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, 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     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, current_arch_machine_name);
653     close (fd);
654
655     if (current_arch_start < 0)
656         return FcFalse;
657     
658     return FcTrue;
659
660  bail:
661     free (cache_file);
662     return FcFalse;
663 }
664
665 FcBool
666 FcDirCacheUnlink (const FcChar8 *dir)
667 {
668     char        *cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
669     char        *cache_hashed;
670     int         fd, collisions;
671     struct stat cache_stat;
672     char        name_buf[FC_MAX_FILE_LEN];
673
674     /* First remove normal cache file. */
675     if (stat ((char *) cache_file, &cache_stat) == 0 &&
676         unlink ((char *)cache_file) != 0)
677         goto bail;
678
679     /* Next remove any applicable hashed files. */
680     fd = -1; collisions = 0;
681     do
682     {
683         cache_hashed = FcDirCacheHashName (cache_file, collisions++);
684         if (!cache_hashed)
685             goto bail;
686
687         if (fd > 0)
688             close (fd);
689         fd = open(cache_hashed, O_RDONLY);
690         if (fd == -1)
691         {
692             FcStrFree ((FcChar8 *)cache_file);
693             return FcTrue;
694         }
695
696         FcCacheReadString (fd, name_buf, sizeof (name_buf));
697         if (!strlen(name_buf))
698             goto bail;
699     } while (strcmp (name_buf, cache_file) != 0);
700
701     FcStrFree ((FcChar8 *)cache_file);
702     close (fd);
703
704     if (stat ((char *) cache_hashed, &cache_stat) == 0 &&
705         unlink ((char *)cache_hashed) != 0)
706     {
707         FcStrFree ((FcChar8 *)cache_hashed);
708         goto bail;
709     }
710
711     FcStrFree ((FcChar8 *)cache_hashed);
712     return FcTrue;
713
714  bail:
715     FcStrFree ((FcChar8 *)cache_file);
716     return FcFalse;
717 }
718
719 static int
720 FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache, 
721                  FcStrList *list, FcFontSet * set)
722 {
723     int                 ret = 0;
724     FcChar8             *dir;
725     FcChar8             *file, *base;
726     FcStrSet            *subdirs;
727     FcStrList           *sublist;
728     struct stat         statb;
729
730     /*
731      * Read in the results from 'list'.
732      */
733     while ((dir = FcStrListNext (list)))
734     {
735         if (!FcConfigAcceptFilename (config, dir))
736             continue;
737
738         /* freed below */
739         file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
740         if (!file)
741             return FcFalse;
742
743         strcpy ((char *) file, (char *) dir);
744         strcat ((char *) file, "/");
745         base = file + strlen ((char *) file);
746
747         subdirs = FcStrSetCreate ();
748         if (!subdirs)
749         {
750             fprintf (stderr, "Can't create directory set\n");
751             ret++;
752             free (file);
753             continue;
754         }
755         
756         if (access ((char *) dir, X_OK) < 0)
757         {
758             switch (errno) {
759             case ENOENT:
760             case ENOTDIR:
761             case EACCES:
762                 break;
763             default:
764                 fprintf (stderr, "\"%s\": ", dir);
765                 perror ("");
766                 ret++;
767             }
768             FcStrSetDestroy (subdirs);
769             free (file);
770             continue;
771         }
772         if (stat ((char *) dir, &statb) == -1)
773         {
774             fprintf (stderr, "\"%s\": ", dir);
775             perror ("");
776             FcStrSetDestroy (subdirs);
777             ret++;
778             free (file);
779             continue;
780         }
781         if (!S_ISDIR (statb.st_mode))
782         {
783             fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
784             FcStrSetDestroy (subdirs);
785             free (file);
786             continue;
787         }
788         if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir, config))
789         {
790             if (FcDebug () & FC_DBG_FONTSET)
791                 printf ("cache scan dir %s\n", dir);
792
793             FcDirScanConfig (set, subdirs, cache, 
794                              config->blanks, dir, FcFalse, config);
795         }
796         sublist = FcStrListCreate (subdirs);
797         FcStrSetDestroy (subdirs);
798         if (!sublist)
799         {
800             fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
801             ret++;
802             free (file);
803             continue;
804         }
805         ret += FcCacheReadDirs (config, cache, sublist, set);
806         free (file);
807     }
808     FcStrListDone (list);
809     return ret;
810 }
811
812 FcFontSet *
813 FcCacheRead (FcConfig *config, FcGlobalCache * cache)
814 {
815     FcFontSet * s = FcFontSetCreate();
816     if (!s) 
817         return 0;
818
819     if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
820         goto bail;
821
822     return s;
823
824  bail:
825     FcFontSetDestroy (s);
826     return 0;
827 }
828
829 static const char bin2hex[] = { '0', '1', '2', '3',
830                                 '4', '5', '6', '7',
831                                 '8', '9', 'a', 'b',
832                                 'c', 'd', 'e', 'f' };
833
834 static char *
835 FcDirCacheHashName (char * cache_file, int collisions)
836 {
837     unsigned char       hash[16], hex_hash[33];
838     char                *cache_hashed;
839     unsigned char       uscore = '_';
840     int                 cnt, i;
841     FcChar8             *tmp;
842     struct MD5Context   ctx;
843
844     MD5Init (&ctx);
845     MD5Update (&ctx, (unsigned char *)cache_file, strlen (cache_file));
846
847     for (i = 0; i < collisions; i++)
848         MD5Update (&ctx, &uscore, 1);
849
850     MD5Final (hash, &ctx);
851
852     for (cnt = 0; cnt < 16; ++cnt)
853     {
854         hex_hash[2*cnt] = bin2hex[hash[cnt] >> 4];
855         hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
856     }
857     hex_hash[32] = 0;
858
859     tmp = FcStrPlus ((FcChar8 *)hex_hash, (FcChar8 *)FC_CACHE_SUFFIX);
860     if (!tmp)
861         return 0;
862
863     cache_hashed = (char *)FcStrPlus ((FcChar8 *)PKGCACHEDIR"/", tmp);
864     free (tmp);
865
866     return cache_hashed;
867 }
868
869 /* Opens the hashed name for cache_file.
870  * This would fail in the unlikely event of a collision and subsequent
871  * removal of the file which originally caused the collision. */
872 static int
873 FcDirCacheOpen (char *cache_file)
874 {
875     int         fd = -1, collisions = 0;
876     char        *cache_hashed;
877     char        name_buf[FC_MAX_FILE_LEN];
878
879     fd = open(cache_file, O_RDONLY);
880     if (fd != -1)
881         return fd;
882
883     do
884     {
885         cache_hashed = FcDirCacheHashName (cache_file, collisions++);
886         if (!cache_hashed)
887             return -1;
888
889         if (fd > 0)
890             close (fd);
891         fd = open(cache_hashed, O_RDONLY);
892         FcStrFree ((FcChar8 *)cache_hashed);
893
894         if (fd == -1)
895             return -1;
896         FcCacheReadString (fd, name_buf, sizeof (name_buf));
897         if (!strlen(name_buf))
898             goto bail;
899     } while (strcmp (name_buf, cache_file) != 0);
900     return fd;
901
902  bail:
903     close (fd);
904     return -1;
905 }
906
907 /* read serialized state from the cache file */
908 FcBool
909 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *config)
910 {
911     char        *cache_file;
912     int         fd;
913     char        *current_arch_machine_name;
914     char        candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
915     off_t       current_arch_start = 0;
916     char        subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
917
918     cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
919     if (!cache_file)
920         return FcFalse;
921
922     fd = FcDirCacheOpen (cache_file);
923     if (fd < 0)
924         goto bail;
925
926     current_arch_machine_name = FcCacheMachineSignature();
927     current_arch_start = FcCacheSkipToArch(fd, 
928                                            current_arch_machine_name);
929     if (current_arch_start < 0)
930         goto bail1;
931
932     lseek (fd, current_arch_start, SEEK_SET);
933     if (FcCacheReadString (fd, candidate_arch_machine_name, 
934                            sizeof (candidate_arch_machine_name)) == 0)
935         goto bail1;
936
937     while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
938         FcStrSetAdd (dirs, (FcChar8 *)subdirName);
939
940     if (!FcDirCacheConsume (fd, (const char *)dir, set, config))
941         goto bail1;
942         
943     close(fd);
944     free (cache_file);
945     return FcTrue;
946
947  bail1:
948     close (fd);
949  bail:
950     free (cache_file);
951     return FcFalse;
952 }
953
954 static FcBool
955 FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config)
956 {
957     FcCache metadata;
958     void * current_dir_block;
959     off_t pos;
960
961     read(fd, &metadata, sizeof(FcCache));
962     if (metadata.magic != FC_CACHE_MAGIC)
963         return FcFalse;
964
965     if (!metadata.count)
966     {
967         pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
968         lseek (fd, pos, SEEK_SET);
969         return FcTrue;
970     }
971
972     pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
973     current_dir_block = mmap (0, metadata.count, 
974                               PROT_READ, MAP_SHARED, fd, pos);
975     lseek (fd, pos+metadata.count, SEEK_SET);
976     if (current_dir_block == MAP_FAILED)
977         return FcFalse;
978
979     FcCacheAddBankDir (metadata.bank, dir);
980     if (config)
981         FcConfigAddFontDir (config, (FcChar8 *)dir);
982
983     if (!FcFontSetUnserialize (&metadata, set, current_dir_block))
984         return FcFalse;
985
986     return FcTrue;
987 }
988
989 static void *
990 FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
991 {
992     void * current_dir_block, * final_dir_block;
993     static unsigned int rand_state = 0;
994     int bank, needed_bytes_no_align;
995
996     if (!rand_state) 
997         rand_state = time(0L);
998     bank = rand_r(&rand_state);
999
1000     while (FcCacheHaveBank(bank))
1001         bank = rand_r(&rand_state);
1002
1003     memset (metadata, 0, sizeof(FcCache));
1004     FcFontSetNewBank();
1005     needed_bytes_no_align = FcFontSetNeededBytes (set);
1006     metadata->count = needed_bytes_no_align + 
1007         FcFontSetNeededBytesAlign ();
1008     metadata->magic = FC_CACHE_MAGIC;
1009     metadata->bank = bank;
1010
1011     if (!needed_bytes_no_align) /* not a failure, no fonts to write */
1012     {
1013         /* no, you don't really need to write any bytes at all. */
1014         metadata->count = 0;
1015         return 0;
1016     }
1017
1018     current_dir_block = malloc (metadata->count);
1019     if (!current_dir_block)
1020         goto bail;
1021     // shut up valgrind
1022     memset (current_dir_block, 0, metadata->count);
1023     final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
1024
1025     if ((void *)((char *)current_dir_block+metadata->count) < final_dir_block)
1026         goto bail;
1027                               
1028     if (!FcFontSetSerialize (bank, set))
1029         goto bail;
1030
1031     return current_dir_block;
1032
1033  bail:
1034     free (current_dir_block);
1035     return 0;
1036 }
1037
1038 /* write serialized state to the cache file */
1039 FcBool
1040 FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
1041 {
1042     char            *cache_file;
1043     char            *cache_hashed;
1044     int             fd, fd_orig, i, dirs_count;
1045     FcAtomic        *atomic;
1046     FcCache         metadata;
1047     off_t           current_arch_start = 0, truncate_to;
1048     char            name_buf[FC_MAX_FILE_LEN];
1049     int             collisions;
1050
1051     char            *current_arch_machine_name, * header;
1052     void            *current_dir_block = 0;
1053
1054     dir = FcConfigNormalizeFontDir (FcConfigGetCurrent(), dir);
1055     if (!dir)
1056         return FcFalse;
1057
1058     cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
1059     if (!cache_file)
1060         goto bail;
1061
1062     /* Ensure that we're not trampling a cache for some other dir. */
1063     /* This is slightly different from FcDirCacheOpen, since it 
1064      * needs the filename, not the file descriptor. */
1065     fd = -1; collisions = 0;
1066     do
1067     {
1068         cache_hashed = FcDirCacheHashName (cache_file, collisions++);
1069         if (!cache_hashed)
1070             goto bail0;
1071
1072         if (fd > 0)
1073             close (fd);
1074         fd = open(cache_hashed, O_RDONLY);
1075         if (fd == -1)
1076             break;
1077         FcCacheReadString (fd, name_buf, sizeof (name_buf));
1078         close (fd);
1079
1080         if (!strlen(name_buf))
1081             break;
1082
1083         if (strcmp (name_buf, cache_file) != 0)
1084             continue;
1085     } while (0);
1086
1087     current_dir_block = FcDirCacheProduce (set, &metadata);
1088
1089     if (metadata.count && !current_dir_block)
1090         goto bail1;
1091
1092     if (FcDebug () & FC_DBG_CACHE)
1093         printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
1094
1095     atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
1096     if (!atomic)
1097         goto bail1;
1098
1099     if (!FcAtomicLock (atomic))
1100     {
1101         /* Now try rewriting the original version of the file. */
1102         FcAtomicDestroy (atomic);
1103
1104         atomic = FcAtomicCreate ((FcChar8 *)cache_file);
1105         fd_orig = open (cache_file, O_RDONLY);
1106         if (fd_orig == -1)
1107             fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1108
1109         fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1110         if (fd == -1)
1111             goto bail2;
1112     }
1113
1114     /* In all cases, try opening the real location of the cache file first. */
1115     /* (even if that's not atomic.) */
1116     fd_orig = open (cache_file, O_RDONLY);
1117     if (fd_orig == -1)
1118         fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY);
1119
1120     fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
1121     if (fd == -1)
1122         goto bail3;
1123
1124     FcCacheWriteString (fd, cache_file);
1125
1126     current_arch_machine_name = FcCacheMachineSignature ();
1127     current_arch_start = 0;
1128
1129     if (fd_orig != -1)
1130         current_arch_start = 
1131             FcCacheSkipToArch(fd_orig, current_arch_machine_name);
1132
1133     if (current_arch_start < 0)
1134         current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
1135
1136     if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
1137         goto bail4;
1138
1139     if (fd_orig != -1)
1140         close (fd_orig);
1141
1142     current_arch_start = lseek(fd, 0, SEEK_CUR);
1143     if (ftruncate (fd, current_arch_start) == -1)
1144         goto bail4;
1145
1146     /* allocate space for subdir names in this block */
1147     dirs_count = 0;
1148     for (i = 0; i < dirs->size; i++)
1149         dirs_count += strlen((char *)dirs->strs[i]) + 1;
1150     dirs_count ++;
1151
1152     /* now write the address of the next offset */
1153     truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start;
1154     header = malloc (10 + strlen (current_arch_machine_name));
1155     if (!header)
1156         goto bail4;
1157     sprintf (header, "%8x ", (int)truncate_to);
1158     strcat (header, current_arch_machine_name);
1159     if (!FcCacheWriteString (fd, header))
1160         goto bail5;
1161
1162     for (i = 0; i < dirs->size; i++)
1163         FcCacheWriteString (fd, (char *)dirs->strs[i]);
1164     FcCacheWriteString (fd, "");
1165
1166     write (fd, &metadata, sizeof(FcCache));
1167     if (metadata.count)
1168     {
1169         lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
1170         write (fd, current_dir_block, metadata.count);
1171         free (current_dir_block);
1172     }
1173
1174     /* this actually serves to pad out the cache file, if needed */
1175     if (ftruncate (fd, current_arch_start + truncate_to) == -1)
1176         goto bail5;
1177
1178     close(fd);
1179     if (!FcAtomicReplaceOrig(atomic))
1180         goto bail5;
1181     FcStrFree ((FcChar8 *)cache_hashed);
1182     FcAtomicUnlock (atomic);
1183     FcAtomicDestroy (atomic);
1184     return FcTrue;
1185
1186  bail5:
1187     free (header);
1188  bail4:
1189     close (fd);
1190  bail3:
1191     FcAtomicUnlock (atomic);
1192  bail2:
1193     FcAtomicDestroy (atomic);
1194  bail1:
1195     FcStrFree ((FcChar8 *)cache_hashed);
1196  bail0:
1197     unlink ((char *)cache_file);
1198     free (cache_file);
1199     if (current_dir_block)
1200         free (current_dir_block);
1201  bail:
1202     return FcFalse;
1203 }
1204
1205 static char *
1206 FcCacheMachineSignature ()
1207 {
1208     static char buf[MACHINE_SIGNATURE_SIZE];
1209     int32_t magic = ENDIAN_TEST;
1210     char * m = (char *)&magic;
1211
1212     sprintf (buf, "%2x%2x%2x%2x "
1213              "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
1214              "%4x %4x %4x %4x %4x %4x %4x %4x\n", 
1215              m[0], m[1], m[2], m[3],
1216              (unsigned int)sizeof (char),
1217              (unsigned int)sizeof (char *),
1218              (unsigned int)sizeof (int),
1219              (unsigned int)sizeof (FcPattern),
1220              (unsigned int)sizeof (FcPatternEltPtr),
1221              (unsigned int)sizeof (struct _FcPatternElt *),
1222              (unsigned int)sizeof (FcPatternElt),
1223              (unsigned int)sizeof (FcObjectPtr),
1224              (unsigned int)sizeof (FcValueListPtr),
1225              (unsigned int)sizeof (FcValue),
1226              (unsigned int)sizeof (FcValueBinding),
1227              (unsigned int)sizeof (struct _FcValueList *),
1228              (unsigned int)sizeof (FcCharSet),
1229              (unsigned int)sizeof (FcCharLeaf **),
1230              (unsigned int)sizeof (FcChar16 *),
1231              (unsigned int)sizeof (FcChar16),
1232              (unsigned int)sizeof (FcCharLeaf),
1233              (unsigned int)sizeof (FcChar32),
1234              (unsigned int)sizeof (FcCache),
1235              (unsigned int)sysconf(_SC_PAGESIZE));
1236
1237     return buf;
1238 }
1239
1240 static int banks_ptr = 0, banks_alloc = 0;
1241 int * _fcBankId = 0, * _fcBankIdx = 0;
1242 static const char ** bankDirs = 0;
1243
1244 static FcBool
1245 FcCacheHaveBank (int bank)
1246 {
1247     int i;
1248
1249     if (bank < FC_BANK_FIRST)
1250         return FcTrue;
1251
1252     for (i = 0; i < banks_ptr; i++)
1253         if (_fcBankId[i] == bank)
1254             return FcTrue;
1255
1256     return FcFalse;
1257 }
1258
1259 int
1260 FcCacheBankToIndexMTF (int bank)
1261 {
1262     int i, j;
1263
1264     for (i = 0; i < banks_ptr; i++)
1265         if (_fcBankId[_fcBankIdx[i]] == bank)
1266         {
1267             int t = _fcBankIdx[i];
1268
1269             for (j = i; j > 0; j--)
1270                 _fcBankIdx[j] = _fcBankIdx[j-1];
1271             _fcBankIdx[0] = t;
1272             return t;
1273         }
1274
1275     if (banks_ptr >= banks_alloc)
1276     {
1277         int * b, * bidx;
1278         const char ** bds;
1279
1280         b = realloc (_fcBankId, (banks_alloc + 4) * sizeof(int));
1281         if (!b)
1282             return -1;
1283         _fcBankId = b;
1284
1285         bidx = realloc (_fcBankIdx, (banks_alloc + 4) * sizeof(int));
1286         if (!bidx)
1287             return -1;
1288         _fcBankIdx = bidx;
1289
1290         bds = realloc (bankDirs, (banks_alloc + 4) * sizeof (char *));
1291         if (!bds)
1292             return -1;
1293         bankDirs = bds;
1294
1295         banks_alloc += 4;
1296     }
1297
1298     i = banks_ptr++;
1299     _fcBankId[i] = bank;
1300     _fcBankIdx[i] = i;
1301     return i;
1302 }
1303
1304 static void
1305 FcCacheAddBankDir (int bank, const char * dir)
1306 {
1307     int bi = FcCacheBankToIndexMTF (bank);
1308
1309     if (bi < 0)
1310         return;
1311
1312     bankDirs[bi] = (const char *)FcStrCopy ((FcChar8 *)dir);
1313 }
1314
1315 const char *
1316 FcCacheFindBankDir (int bank)
1317 {
1318     int bi = FcCacheBankToIndex (bank);
1319     return bankDirs[bi];
1320 }
1321
1322 /*
1323  * This code implements the MD5 message-digest algorithm.
1324  * The algorithm is due to Ron Rivest.  This code was
1325  * written by Colin Plumb in 1993, no copyright is claimed.
1326  * This code is in the public domain; do with it what you wish.
1327  *
1328  * Equivalent code is available from RSA Data Security, Inc.
1329  * This code has been tested against that, and is equivalent,
1330  * except that you don't need to include two pages of legalese
1331  * with every copy.
1332  *
1333  * To compute the message digest of a chunk of bytes, declare an
1334  * MD5Context structure, pass it to MD5Init, call MD5Update as
1335  * needed on buffers full of bytes, and then call MD5Final, which
1336  * will fill a supplied 16-byte array with the digest.
1337  */
1338
1339 #ifndef HIGHFIRST
1340 #define byteReverse(buf, len)   /* Nothing */
1341 #else
1342 /*
1343  * Note: this code is harmless on little-endian machines.
1344  */
1345 void byteReverse(unsigned char *buf, unsigned longs)
1346 {
1347     FcChar32 t;
1348     do {
1349         t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1350             ((unsigned) buf[1] << 8 | buf[0]);
1351         *(FcChar32 *) buf = t;
1352         buf += 4;
1353     } while (--longs);
1354 }
1355 #endif
1356
1357 /*
1358  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
1359  * initialization constants.
1360  */
1361 static void MD5Init(struct MD5Context *ctx)
1362 {
1363     ctx->buf[0] = 0x67452301;
1364     ctx->buf[1] = 0xefcdab89;
1365     ctx->buf[2] = 0x98badcfe;
1366     ctx->buf[3] = 0x10325476;
1367
1368     ctx->bits[0] = 0;
1369     ctx->bits[1] = 0;
1370 }
1371
1372 /*
1373  * Update context to reflect the concatenation of another buffer full
1374  * of bytes.
1375  */
1376 static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len)
1377 {
1378     FcChar32 t;
1379
1380     /* Update bitcount */
1381
1382     t = ctx->bits[0];
1383     if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1384         ctx->bits[1]++;         /* Carry from low to high */
1385     ctx->bits[1] += len >> 29;
1386
1387     t = (t >> 3) & 0x3f;        /* Bytes already in shsInfo->data */
1388
1389     /* Handle any leading odd-sized chunks */
1390
1391     if (t) {
1392         unsigned char *p = (unsigned char *) ctx->in + t;
1393
1394         t = 64 - t;
1395         if (len < t) {
1396             memcpy(p, buf, len);
1397             return;
1398         }
1399         memcpy(p, buf, t);
1400         byteReverse(ctx->in, 16);
1401         MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1402         buf += t;
1403         len -= t;
1404     }
1405     /* Process data in 64-byte chunks */
1406
1407     while (len >= 64) {
1408         memcpy(ctx->in, buf, 64);
1409         byteReverse(ctx->in, 16);
1410         MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1411         buf += 64;
1412         len -= 64;
1413     }
1414
1415     /* Handle any remaining bytes of data. */
1416
1417     memcpy(ctx->in, buf, len);
1418 }
1419
1420 /*
1421  * Final wrapup - pad to 64-byte boundary with the bit pattern 
1422  * 1 0* (64-bit count of bits processed, MSB-first)
1423  */
1424 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1425 {
1426     unsigned count;
1427     unsigned char *p;
1428
1429     /* Compute number of bytes mod 64 */
1430     count = (ctx->bits[0] >> 3) & 0x3F;
1431
1432     /* Set the first char of padding to 0x80.  This is safe since there is
1433        always at least one byte free */
1434     p = ctx->in + count;
1435     *p++ = 0x80;
1436
1437     /* Bytes of padding needed to make 64 bytes */
1438     count = 64 - 1 - count;
1439
1440     /* Pad out to 56 mod 64 */
1441     if (count < 8) {
1442         /* Two lots of padding:  Pad the first block to 64 bytes */
1443         memset(p, 0, count);
1444         byteReverse(ctx->in, 16);
1445         MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1446
1447         /* Now fill the next block with 56 bytes */
1448         memset(ctx->in, 0, 56);
1449     } else {
1450         /* Pad block to 56 bytes */
1451         memset(p, 0, count - 8);
1452     }
1453     byteReverse(ctx->in, 14);
1454
1455     /* Append length in bits and transform */
1456     ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1457     ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1458
1459     MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1460     byteReverse((unsigned char *) ctx->buf, 4);
1461     memcpy(digest, ctx->buf, 16);
1462     memset(ctx, 0, sizeof(ctx));        /* In case it's sensitive */
1463 }
1464
1465
1466 /* The four core functions - F1 is optimized somewhat */
1467
1468 /* #define F1(x, y, z) (x & y | ~x & z) */
1469 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1470 #define F2(x, y, z) F1(z, x, y)
1471 #define F3(x, y, z) (x ^ y ^ z)
1472 #define F4(x, y, z) (y ^ (x | ~z))
1473
1474 /* This is the central step in the MD5 algorithm. */
1475 #define MD5STEP(f, w, x, y, z, data, s) \
1476         ( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
1477
1478 /*
1479  * The core of the MD5 algorithm, this alters an existing MD5 hash to
1480  * reflect the addition of 16 longwords of new data.  MD5Update blocks
1481  * the data and converts bytes into longwords for this routine.
1482  */
1483 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1484 {
1485     register FcChar32 a, b, c, d;
1486
1487     a = buf[0];
1488     b = buf[1];
1489     c = buf[2];
1490     d = buf[3];
1491
1492     MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1493     MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1494     MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1495     MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1496     MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1497     MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1498     MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1499     MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1500     MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1501     MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1502     MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1503     MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1504     MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1505     MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1506     MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1507     MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1508
1509     MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1510     MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1511     MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1512     MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1513     MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1514     MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1515     MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1516     MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1517     MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1518     MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1519     MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1520     MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1521     MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1522     MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1523     MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1524     MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1525
1526     MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1527     MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1528     MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1529     MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1530     MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1531     MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1532     MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1533     MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1534     MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1535     MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1536     MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1537     MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1538     MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1539     MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1540     MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1541     MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1542
1543     MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1544     MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1545     MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1546     MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1547     MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1548     MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1549     MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1550     MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1551     MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1552     MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1553     MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1554     MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1555     MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1556     MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1557     MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1558     MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1559
1560     buf[0] += a;
1561     buf[1] += b;
1562     buf[2] += c;
1563     buf[3] += d;
1564 }