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