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