]> git.wh0rd.org - fontconfig.git/blob - src/fccache.c
Fix addressing in the global cache file, preventing infinite loops. Get rid
[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 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
15 *
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
23 */
24
25 #include <fcntl.h>
26 #include <dirent.h>
27 #include <sys/mman.h>
28 #include <sys/utsname.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31 #include "fcint.h"
32
33 #define ENDIAN_TEST 0x12345678
34 #define MACHINE_SIGNATURE_SIZE 9 + 5*19 + 1
35
36 static off_t
37 FcCacheSkipToArch (int fd, const char * arch);
38
39 static FcBool
40 FcCacheMoveDown (int fd, off_t start);
41
42 static void *
43 FcDirCacheProduce (FcFontSet *set, FcCache * metadata);
44
45 static FcBool
46 FcDirCacheConsume (int fd, FcFontSet *set);
47
48 static FcBool
49 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir);
50
51 static int
52 FcCacheNextOffset(off_t w);
53
54 static char *
55 FcCacheMachineSignature (void);
56
57 static FcBool
58 FcCacheHaveBank (int bank);
59
60 #define FC_DBG_CACHE_REF 1024
61
62 static FcChar8 *
63 FcCacheReadString (int fd, FcChar8 *dest, int len)
64 {
65 FcChar8 c;
66 FcBool escape;
67 int size;
68 int i;
69
70 if (len == 0)
71 return FcFalse;
72
73 size = len;
74 i = 0;
75 escape = FcFalse;
76 while (read (fd, &c, 1) == 1)
77 {
78 if (!escape)
79 {
80 switch (c) {
81 case '"':
82 c = '\0';
83 break;
84 case '\\':
85 escape = FcTrue;
86 continue;
87 }
88 }
89 if (i == size)
90 {
91 dest[i++] = 0;
92 return dest;
93 }
94 dest[i++] = c;
95 if (c == '\0')
96 return dest;
97 escape = FcFalse;
98 }
99 return 0;
100 }
101
102 static FcBool
103 FcCacheWriteString (int fd, const FcChar8 *chars)
104 {
105 if (write (fd, chars, strlen(chars)+1) != strlen(chars)+1)
106 return FcFalse;
107 return FcTrue;
108 }
109
110 static void
111 FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
112 {
113 FcMemFree (FC_MEM_STRING, strlen (d->name)+1);
114 free (d->name);
115 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir));
116 free (d);
117 }
118
119 FcGlobalCache *
120 FcGlobalCacheCreate (void)
121 {
122 FcGlobalCache *cache;
123
124 cache = malloc (sizeof (FcGlobalCache));
125 if (!cache)
126 return 0;
127 FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCache));
128 cache->dirs = 0;
129 cache->updated = FcFalse;
130 cache->fd = -1;
131 return cache;
132 }
133
134 void
135 FcGlobalCacheDestroy (FcGlobalCache *cache)
136 {
137 FcGlobalCacheDir *d, *next;
138
139 for (d = cache->dirs; d; d = next)
140 {
141 next = d->next;
142 FcGlobalCacheDirDestroy (d);
143 }
144 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache));
145 free (cache);
146 }
147
148 void
149 FcGlobalCacheLoad (FcGlobalCache *cache,
150 const FcChar8 *cache_file)
151 {
152 FcChar8 name_buf[8192];
153 FcGlobalCacheDir *d, *next;
154 char * current_arch_machine_name;
155 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
156 off_t current_arch_start;
157
158 cache->fd = open ((char *) cache_file, O_RDONLY);
159 if (cache->fd == -1)
160 return;
161
162 cache->updated = FcFalse;
163
164 current_arch_machine_name = FcCacheMachineSignature ();
165 current_arch_start = FcCacheSkipToArch(cache->fd,
166 current_arch_machine_name);
167 if (current_arch_start < 0)
168 goto bail0;
169
170 lseek (cache->fd, current_arch_start, SEEK_SET);
171 if (FcCacheReadString (cache->fd, candidate_arch_machine_name,
172 sizeof (candidate_arch_machine_name)) == 0)
173 goto bail0;
174
175 while (1)
176 {
177 FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
178 if (!strlen(name_buf))
179 break;
180
181 d = malloc (sizeof (FcGlobalCacheDir));
182 if (!d)
183 goto bail1;
184
185 d->next = cache->dirs;
186 cache->dirs = d;
187
188 d->name = FcStrCopy (name_buf);
189 d->ent = 0;
190 d->offset = lseek (cache->fd, 0, SEEK_CUR);
191 read (cache->fd, &d->metadata, sizeof (FcCache));
192 lseek (cache->fd, FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count, SEEK_SET);
193 }
194 return;
195
196 bail1:
197 for (d = cache->dirs; d; d = next)
198 {
199 next = d->next;
200 free (d);
201 }
202 cache->dirs = 0;
203 bail0:
204 close (cache->fd);
205 cache->fd = -1;
206 return;
207 }
208
209 FcBool
210 FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const FcChar8 *dir, FcConfig *config)
211 {
212 FcGlobalCacheDir *d;
213
214 if (cache->fd == -1)
215 return FcFalse;
216
217 for (d = cache->dirs; d; d = d->next)
218 {
219 if (strcmp (d->name, dir) == 0)
220 {
221 lseek (cache->fd, d->offset, SEEK_SET);
222 if (!FcDirCacheConsume (cache->fd, set))
223 return FcFalse;
224 return FcTrue;
225 }
226 }
227
228 return FcFalse;
229 }
230
231 FcBool
232 FcGlobalCacheUpdate (FcGlobalCache *cache,
233 const FcChar8 *name,
234 FcFontSet *set)
235 {
236 FcGlobalCacheDir * d;
237
238 if (!set->nfont)
239 return FcTrue;
240
241 for (d = cache->dirs; d; d = d->next)
242 {
243 if (strcmp(d->name, name) == 0)
244 break;
245 }
246
247 if (!d)
248 {
249 d = malloc (sizeof (FcGlobalCacheDir));
250 if (!d)
251 return FcFalse;
252 d->next = cache->dirs;
253 cache->dirs = d;
254 }
255
256 cache->updated = FcTrue;
257
258 d->name = FcStrCopy (name);
259 d->ent = FcDirCacheProduce (set, &d->metadata);
260 d->offset = 0;
261 return FcTrue;
262 }
263
264 FcBool
265 FcGlobalCacheSave (FcGlobalCache *cache,
266 const FcChar8 *cache_file)
267 {
268 int fd;
269 FcGlobalCacheDir *dir;
270 FcAtomic *atomic;
271 off_t current_arch_start = 0, truncate_to;
272 char * current_arch_machine_name, * header;
273
274 if (!cache->updated)
275 return FcTrue;
276
277 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
278 /* Set-UID programs can't safely update the cache */
279 if (getuid () != geteuid ())
280 return FcFalse;
281 #endif
282
283 atomic = FcAtomicCreate (cache_file);
284 if (!atomic)
285 goto bail0;
286 if (!FcAtomicLock (atomic))
287 goto bail1;
288 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT,
289 S_IRUSR | S_IWUSR);
290 if (fd == -1)
291 goto bail2;
292
293 current_arch_machine_name = FcCacheMachineSignature ();
294 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
295 if (current_arch_start < 0)
296 current_arch_start = FcCacheNextOffset (lseek(fd, 0, SEEK_END));
297
298 if (!FcCacheMoveDown(fd, current_arch_start))
299 goto bail2;
300
301 current_arch_start = lseek(fd, 0, SEEK_CUR);
302 if (ftruncate (fd, current_arch_start) == -1)
303 goto bail2;
304
305 truncate_to = current_arch_start;
306 for (dir = cache->dirs; dir; dir = dir->next)
307 {
308 truncate_to += strlen(dir->name) + 1;
309 truncate_to += sizeof (FcCache);
310 truncate_to = FcCacheNextOffset (current_arch_start + truncate_to);
311 truncate_to += dir->metadata.count;
312 }
313 truncate_to -= current_arch_start;
314 header = malloc (10 + strlen (current_arch_machine_name));
315 if (!header)
316 goto bail1;
317 sprintf (header, "%8x ", (int)truncate_to);
318 strcat (header, current_arch_machine_name);
319 if (!FcCacheWriteString (fd, header))
320 goto bail1;
321
322 for (dir = cache->dirs; dir; dir = dir->next)
323 {
324 if (dir->ent)
325 {
326 FcCacheWriteString (fd, dir->name);
327 write (fd, &dir->metadata, sizeof(FcCache));
328 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET);
329 write (fd, dir->ent, dir->metadata.count);
330 free (dir->ent);
331 }
332 }
333 FcCacheWriteString (fd, "");
334
335 if (close (fd) == -1)
336 goto bail3;
337
338 if (!FcAtomicReplaceOrig (atomic))
339 goto bail3;
340
341 FcAtomicUnlock (atomic);
342 FcAtomicDestroy (atomic);
343
344 cache->updated = FcFalse;
345 return FcTrue;
346
347 bail3:
348 FcAtomicDeleteNew (atomic);
349 bail2:
350 FcAtomicUnlock (atomic);
351 bail1:
352 FcAtomicDestroy (atomic);
353 bail0:
354 return FcFalse;
355 }
356
357 #define PAGESIZE 8192
358 /*
359 * Find the next presumably-mmapable offset after the supplied file
360 * position.
361 */
362 static int
363 FcCacheNextOffset(off_t w)
364 {
365 if (w % PAGESIZE == 0)
366 return w;
367 else
368 return ((w / PAGESIZE)+1)*PAGESIZE;
369 }
370
371 /* return the address of the segment for the provided arch,
372 * or -1 if arch not found */
373 static off_t
374 FcCacheSkipToArch (int fd, const char * arch)
375 {
376 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
377 char * candidate_arch;
378 off_t current_arch_start = 0;
379
380 /* skip arches that are not the current arch */
381 while (1)
382 {
383 long bs;
384
385 lseek (fd, current_arch_start, SEEK_SET);
386 if (FcCacheReadString (fd, candidate_arch_machine_name_count,
387 sizeof (candidate_arch_machine_name_count)) == 0)
388 return -1;
389 if (!strlen(candidate_arch_machine_name_count))
390 return -1;
391 bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16);
392 candidate_arch++; /* skip leading space */
393
394 if (strcmp (candidate_arch, arch)==0)
395 return current_arch_start;
396 current_arch_start += bs;
397 }
398
399 return -1;
400 }
401
402 /* Cuts out the segment at the file pointer (moves everything else
403 * down to cover it), and leaves the file pointer at the end of the
404 * file. */
405 static FcBool
406 FcCacheMoveDown (int fd, off_t start)
407 {
408 char * buf = malloc (8192);
409 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
410 long bs;
411 int c, bytes_skipped;
412
413 if (!buf)
414 return FcFalse;
415
416 lseek (fd, start, SEEK_SET);
417 if (FcCacheReadString (fd, candidate_arch_machine_name,
418 sizeof (candidate_arch_machine_name)) == 0)
419 goto done;
420 if (!strlen(candidate_arch_machine_name))
421 goto done;
422
423 bs = strtol(candidate_arch_machine_name, 0, 16);
424 if (bs == 0)
425 goto done;
426
427 bytes_skipped = 0;
428 do
429 {
430 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
431 if ((c = read (fd, buf, 8192)) <= 0)
432 break;
433 lseek (fd, start+bytes_skipped, SEEK_SET);
434 if (write (fd, buf, c) < 0)
435 goto bail;
436 bytes_skipped += c;
437 }
438 while (c > 0);
439 lseek (fd, start+bytes_skipped, SEEK_SET);
440
441 done:
442 free (buf);
443 return FcTrue;
444
445 bail:
446 free (buf);
447 return FcFalse;
448 }
449
450 FcBool
451 FcDirCacheValid (const FcChar8 *dir)
452 {
453 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
454 struct stat file_stat, dir_stat;
455
456 if (stat ((char *) dir, &dir_stat) < 0)
457 {
458 FcStrFree (cache_file);
459 return FcFalse;
460 }
461 if (stat ((char *) cache_file, &file_stat) < 0)
462 {
463 FcStrFree (cache_file);
464 return FcFalse;
465 }
466 FcStrFree (cache_file);
467 /*
468 * If the directory has been modified more recently than
469 * the cache file, the cache is not valid
470 */
471 if (dir_stat.st_mtime - file_stat.st_mtime > 0)
472 return FcFalse;
473 return FcTrue;
474 }
475
476 static int
477 FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
478 FcStrList *list, FcFontSet * set)
479 {
480 int ret = 0;
481 FcChar8 *dir;
482 FcChar8 *file, *base;
483 FcStrSet *subdirs;
484 FcStrList *sublist;
485 struct stat statb;
486
487 /*
488 * Now scan all of the directories into separate databases
489 * and write out the results
490 */
491 while ((dir = FcStrListNext (list)))
492 {
493 /* freed below */
494 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
495 if (!file)
496 return FcFalse;
497
498 strcpy ((char *) file, (char *) dir);
499 strcat ((char *) file, "/");
500 base = file + strlen ((char *) file);
501
502 subdirs = FcStrSetCreate ();
503 if (!subdirs)
504 {
505 fprintf (stderr, "Can't create directory set\n");
506 ret++;
507 free (file);
508 continue;
509 }
510
511 if (access ((char *) dir, X_OK) < 0)
512 {
513 switch (errno) {
514 case ENOENT:
515 case ENOTDIR:
516 case EACCES:
517 break;
518 default:
519 fprintf (stderr, "\"%s\": ", dir);
520 perror ("");
521 ret++;
522 }
523 FcStrSetDestroy (subdirs);
524 free (file);
525 continue;
526 }
527 if (stat ((char *) dir, &statb) == -1)
528 {
529 fprintf (stderr, "\"%s\": ", dir);
530 perror ("");
531 FcStrSetDestroy (subdirs);
532 ret++;
533 free (file);
534 continue;
535 }
536 if (!S_ISDIR (statb.st_mode))
537 {
538 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
539 FcStrSetDestroy (subdirs);
540 free (file);
541 continue;
542 }
543 if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir))
544 {
545 if (FcDebug () & FC_DBG_FONTSET)
546 printf ("scan dir %s\n", dir);
547
548 FcDirScanConfig (set, subdirs, cache,
549 config->blanks, dir, FcFalse, config);
550 }
551 sublist = FcStrListCreate (subdirs);
552 FcStrSetDestroy (subdirs);
553 if (!sublist)
554 {
555 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
556 ret++;
557 free (file);
558 continue;
559 }
560 ret += FcCacheReadDirs (config, cache, sublist, set);
561 free (file);
562 }
563 FcStrListDone (list);
564 return ret;
565 }
566
567 FcFontSet *
568 FcCacheRead (FcConfig *config, FcGlobalCache * cache)
569 {
570 FcFontSet * s = FcFontSetCreate();
571 if (!s)
572 return 0;
573
574 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
575 goto bail;
576
577 return s;
578
579 bail:
580 FcFontSetDestroy (s);
581 return 0;
582 }
583
584 /* read serialized state from the cache file */
585 static FcBool
586 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir)
587 {
588 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
589 int fd;
590 char * current_arch_machine_name;
591 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
592 off_t current_arch_start = 0;
593 FcChar8 subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
594
595 if (!cache_file)
596 goto bail;
597
598 current_arch_machine_name = FcCacheMachineSignature();
599 fd = open(cache_file, O_RDONLY);
600 if (fd == -1)
601 goto bail;
602
603 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
604 if (current_arch_start < 0)
605 goto bail1;
606
607 lseek (fd, current_arch_start, SEEK_SET);
608 if (FcCacheReadString (fd, candidate_arch_machine_name,
609 sizeof (candidate_arch_machine_name)) == 0)
610 goto bail1;
611
612 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
613 FcStrSetAdd (dirs, subdirName);
614
615 if (!FcDirCacheConsume (fd, set))
616 goto bail1;
617
618 close(fd);
619 free (cache_file);
620 return FcTrue;
621
622 bail1:
623 close (fd);
624 bail:
625 free (cache_file);
626 return FcFalse;
627 }
628
629 static FcBool
630 FcDirCacheConsume (int fd, FcFontSet *set)
631 {
632 FcCache metadata;
633 void * current_dir_block;
634 off_t pos;
635
636 read(fd, &metadata, sizeof(FcCache));
637 if (metadata.magic != FC_CACHE_MAGIC)
638 return FcFalse;
639
640 if (!metadata.count)
641 return FcTrue;
642
643 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
644 current_dir_block = mmap (0, metadata.count,
645 PROT_READ, MAP_SHARED, fd, pos);
646 if (current_dir_block == MAP_FAILED)
647 return FcFalse;
648
649 if (!FcFontSetUnserialize (metadata, set, current_dir_block))
650 return FcFalse;
651
652 return FcTrue;
653 }
654
655 static void *
656 FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
657 {
658 void * current_dir_block, * final_dir_block;
659 static int rand_state = 0;
660 int bank;
661
662 if (!rand_state)
663 rand_state = time(0L);
664 bank = rand_r(&rand_state);
665
666 while (FcCacheHaveBank(bank))
667 bank = rand_r(&rand_state);
668
669 memset (metadata, 0, sizeof(FcCache));
670 FcFontSetNewBank();
671 metadata->count = FcFontSetNeededBytes (set);
672 metadata->magic = FC_CACHE_MAGIC;
673 metadata->bank = bank;
674
675 if (!metadata->count) /* not a failure, no fonts to write */
676 return 0;
677
678 current_dir_block = malloc (metadata->count);
679 if (!current_dir_block)
680 goto bail;
681 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
682
683 if ((char *)current_dir_block + metadata->count != final_dir_block)
684 goto bail;
685
686 if (!FcFontSetSerialize (bank, set))
687 goto bail;
688
689 return current_dir_block;
690
691 bail:
692 free (current_dir_block);
693 return 0;
694 }
695
696 /* write serialized state to the cache file */
697 FcBool
698 FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
699 {
700 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
701 int fd, i;
702 FcCache metadata;
703 off_t current_arch_start = 0, truncate_to;
704 char * current_arch_machine_name, * header;
705 void * current_dir_block;
706
707 if (!cache_file)
708 goto bail;
709
710 current_dir_block = FcDirCacheProduce (set, &metadata);
711
712 if (!metadata.count)
713 {
714 unlink (cache_file);
715 free (cache_file);
716 return FcTrue;
717 }
718
719 if (!current_dir_block)
720 goto bail;
721
722 if (FcDebug () & FC_DBG_CACHE)
723 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
724
725 fd = open(cache_file, O_RDWR | O_CREAT, 0666);
726 if (fd == -1)
727 goto bail0;
728
729 current_arch_machine_name = FcCacheMachineSignature ();
730 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
731 if (current_arch_start < 0)
732 current_arch_start = FcCacheNextOffset (lseek(fd, 0, SEEK_END));
733
734 if (!FcCacheMoveDown(fd, current_arch_start))
735 goto bail1;
736
737 current_arch_start = lseek(fd, 0, SEEK_CUR);
738 if (ftruncate (fd, current_arch_start) == -1)
739 goto bail1;
740
741 /* now write the address of the next offset */
742 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache)) + metadata.count) - current_arch_start;
743 header = malloc (10 + strlen (current_arch_machine_name));
744 if (!header)
745 goto bail1;
746 sprintf (header, "%8x ", (int)truncate_to);
747 strcat (header, current_arch_machine_name);
748 if (!FcCacheWriteString (fd, header))
749 goto bail1;
750
751 for (i = 0; i < dirs->size; i++)
752 FcCacheWriteString (fd, dirs->strs[i]);
753 FcCacheWriteString (fd, "");
754
755 write (fd, &metadata, sizeof(FcCache));
756 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
757 write (fd, current_dir_block, metadata.count);
758 free (current_dir_block);
759
760 /* this actually serves to pad out the cache file, if needed */
761 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
762 goto bail1;
763
764 close(fd);
765 return FcTrue;
766
767 bail1:
768 free (header);
769 bail0:
770 free (current_dir_block);
771 bail:
772 unlink (cache_file);
773 free (cache_file);
774 return FcFalse;
775 }
776
777 static char *
778 FcCacheMachineSignature ()
779 {
780 static char buf[MACHINE_SIGNATURE_SIZE];
781 int magic = ENDIAN_TEST;
782 char * m = (char *)&magic;
783
784 sprintf (buf, "%2x%2x%2x%2x "
785 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
786 "%4x %4x %4x %4x %4x %4x %4x\n",
787 m[0], m[1], m[2], m[3],
788 sizeof (char),
789 sizeof (char *),
790 sizeof (int),
791 sizeof (FcPattern),
792 sizeof (FcPatternEltPtr),
793 sizeof (struct _FcPatternElt *),
794 sizeof (FcPatternElt),
795 sizeof (FcObjectPtr),
796 sizeof (FcValueListPtr),
797 sizeof (FcValue),
798 sizeof (FcValueBinding),
799 sizeof (struct _FcValueList *),
800 sizeof (FcCharSet),
801 sizeof (FcCharLeaf **),
802 sizeof (FcChar16 *),
803 sizeof (FcChar16),
804 sizeof (FcCharLeaf),
805 sizeof (FcChar32),
806 sizeof (FcCache));
807
808 return buf;
809 }
810
811 static int banks_ptr = 0, banks_alloc = 0;
812 static int * bankId = 0;
813
814 static FcBool
815 FcCacheHaveBank (int bank)
816 {
817 int i;
818
819 if (bank < FC_BANK_FIRST)
820 return FcTrue;
821
822 for (i = 0; i < banks_ptr; i++)
823 if (bankId[i] == bank)
824 return FcTrue;
825
826 return FcFalse;
827 }
828
829 int
830 FcCacheBankToIndex (int bank)
831 {
832 static int lastBank = FC_BANK_DYNAMIC, lastIndex = -1;
833 int i;
834 int * b;
835
836 if (bank == lastBank)
837 return lastIndex;
838
839 for (i = 0; i < banks_ptr; i++)
840 if (bankId[i] == bank)
841 return i;
842
843 if (banks_ptr >= banks_alloc)
844 {
845 b = realloc (bankId, (banks_alloc + 4) * sizeof(int));
846 if (!b)
847 return -1;
848
849 bankId = b;
850 banks_alloc += 4;
851 }
852
853 i = banks_ptr++;
854 bankId[i] = bank;
855 return i;
856 }