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