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