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