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