]> git.wh0rd.org - fontconfig.git/blob - src/fccache.c
Fix embarassing attempt to free a static buffer.
[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, d->metadata.count, SEEK_CUR);
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_END)), 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 DIR *d;
481 struct dirent *e;
482 int ret = 0;
483 FcChar8 *dir;
484 FcChar8 *file, *base;
485 FcStrSet *subdirs;
486 FcStrList *sublist;
487 struct stat statb;
488
489 /*
490 * Now scan all of the directories into separate databases
491 * and write out the results
492 */
493 while ((dir = FcStrListNext (list)))
494 {
495 /* freed below */
496 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
497 if (!file)
498 return FcFalse;
499
500 strcpy ((char *) file, (char *) dir);
501 strcat ((char *) file, "/");
502 base = file + strlen ((char *) file);
503
504 subdirs = FcStrSetCreate ();
505 if (!subdirs)
506 {
507 fprintf (stderr, "Can't create directory set\n");
508 ret++;
509 free (file);
510 continue;
511 }
512
513 if (access ((char *) dir, X_OK) < 0)
514 {
515 switch (errno) {
516 case ENOENT:
517 case ENOTDIR:
518 case EACCES:
519 break;
520 default:
521 fprintf (stderr, "\"%s\": ", dir);
522 perror ("");
523 ret++;
524 }
525 FcStrSetDestroy (subdirs);
526 free (file);
527 continue;
528 }
529 if (stat ((char *) dir, &statb) == -1)
530 {
531 fprintf (stderr, "\"%s\": ", dir);
532 perror ("");
533 FcStrSetDestroy (subdirs);
534 ret++;
535 free (file);
536 continue;
537 }
538 if (!S_ISDIR (statb.st_mode))
539 {
540 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
541 FcStrSetDestroy (subdirs);
542 free (file);
543 continue;
544 }
545 if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir))
546 {
547 if (FcDebug () & FC_DBG_FONTSET)
548 printf ("scan dir %s\n", dir);
549
550 FcDirScanConfig (set, subdirs, cache,
551 config->blanks, dir, FcFalse, config);
552 }
553 sublist = FcStrListCreate (subdirs);
554 FcStrSetDestroy (subdirs);
555 if (!sublist)
556 {
557 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
558 ret++;
559 free (file);
560 continue;
561 }
562 ret += FcCacheReadDirs (config, cache, sublist, set);
563 free (file);
564 }
565 FcStrListDone (list);
566 return ret;
567 }
568
569 FcFontSet *
570 FcCacheRead (FcConfig *config, FcGlobalCache * cache)
571 {
572 FcFontSet * s = FcFontSetCreate();
573 if (!s)
574 return 0;
575
576 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
577 goto bail;
578
579 return s;
580
581 bail:
582 FcFontSetDestroy (s);
583 return 0;
584 }
585
586 /* read serialized state from the cache file */
587 static FcBool
588 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir)
589 {
590 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
591 int fd;
592 char * current_arch_machine_name;
593 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
594 off_t current_arch_start = 0;
595 FcChar8 subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
596
597 if (!cache_file)
598 goto bail;
599
600 current_arch_machine_name = FcCacheMachineSignature();
601 fd = open(cache_file, O_RDONLY);
602 if (fd == -1)
603 goto bail;
604
605 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
606 if (current_arch_start < 0)
607 goto bail1;
608
609 lseek (fd, current_arch_start, SEEK_SET);
610 if (FcCacheReadString (fd, candidate_arch_machine_name,
611 sizeof (candidate_arch_machine_name)) == 0)
612 goto bail1;
613
614 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
615 FcStrSetAdd (dirs, subdirName);
616
617 if (!FcDirCacheConsume (fd, set))
618 goto bail1;
619
620 close(fd);
621 free (cache_file);
622 return FcTrue;
623
624 bail1:
625 close (fd);
626 bail:
627 free (cache_file);
628 return FcFalse;
629 }
630
631 static FcBool
632 FcDirCacheConsume (int fd, FcFontSet *set)
633 {
634 FcCache metadata;
635 void * current_dir_block;
636 off_t pos;
637
638 read(fd, &metadata, sizeof(FcCache));
639 if (metadata.magic != FC_CACHE_MAGIC)
640 return FcFalse;
641
642 if (!metadata.count)
643 return FcTrue;
644
645 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
646 current_dir_block = mmap (0, metadata.count,
647 PROT_READ, MAP_SHARED, fd, pos);
648 if (current_dir_block == MAP_FAILED)
649 return FcFalse;
650
651 if (!FcFontSetUnserialize (metadata, set, current_dir_block))
652 return FcFalse;
653
654 return FcTrue;
655 }
656
657 static void *
658 FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
659 {
660 void * current_dir_block, * final_dir_block;
661 static int rand_state = 0;
662 int bank;
663
664 if (!rand_state)
665 rand_state = time(0L);
666 bank = rand_r(&rand_state);
667
668 while (FcCacheHaveBank(bank))
669 bank = rand_r(&rand_state);
670
671 memset (metadata, 0, sizeof(FcCache));
672 FcFontSetNewBank();
673 metadata->count = FcFontSetNeededBytes (set);
674 metadata->magic = FC_CACHE_MAGIC;
675 metadata->bank = bank;
676
677 if (!metadata->count) /* not a failure, no fonts to write */
678 return 0;
679
680 current_dir_block = malloc (metadata->count);
681 if (!current_dir_block)
682 goto bail;
683 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
684
685 if ((char *)current_dir_block + metadata->count != final_dir_block)
686 goto bail;
687
688 if (!FcFontSetSerialize (bank, set))
689 goto bail;
690
691 return current_dir_block;
692
693 bail:
694 free (current_dir_block);
695 return 0;
696 }
697
698 /* write serialized state to the cache file */
699 FcBool
700 FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
701 {
702 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
703 int fd, i;
704 FcCache metadata;
705 off_t current_arch_start = 0, truncate_to;
706 char * current_arch_machine_name, * header;
707 void * current_dir_block;
708
709 if (!cache_file)
710 goto bail;
711
712 current_dir_block = FcDirCacheProduce (set, &metadata);
713
714 if (!metadata.count)
715 {
716 unlink (cache_file);
717 free (cache_file);
718 return FcTrue;
719 }
720
721 if (!current_dir_block)
722 goto bail;
723
724 if (FcDebug () & FC_DBG_CACHE)
725 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
726
727 fd = open(cache_file, O_RDWR | O_CREAT, 0666);
728 if (fd == -1)
729 goto bail0;
730
731 current_arch_machine_name = FcCacheMachineSignature ();
732 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
733 if (current_arch_start < 0)
734 current_arch_start = FcCacheNextOffset (lseek(fd, 0, SEEK_END));
735
736 if (!FcCacheMoveDown(fd, current_arch_start))
737 goto bail1;
738
739 current_arch_start = lseek(fd, 0, SEEK_CUR);
740 if (ftruncate (fd, current_arch_start) == -1)
741 goto bail1;
742
743 /* now write the address of the next offset */
744 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache)) + metadata.count) - current_arch_start;
745 header = malloc (10 + strlen (current_arch_machine_name));
746 if (!header)
747 goto bail1;
748 sprintf (header, "%8x ", (int)truncate_to);
749 strcat (header, current_arch_machine_name);
750 if (!FcCacheWriteString (fd, header))
751 goto bail1;
752
753 for (i = 0; i < dirs->size; i++)
754 FcCacheWriteString (fd, dirs->strs[i]);
755 FcCacheWriteString (fd, "");
756
757 write (fd, &metadata, sizeof(FcCache));
758 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
759 write (fd, current_dir_block, metadata.count);
760 free (current_dir_block);
761
762 /* this actually serves to pad out the cache file, if needed */
763 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
764 goto bail1;
765
766 close(fd);
767 return FcTrue;
768
769 bail1:
770 free (header);
771 bail0:
772 free (current_dir_block);
773 bail:
774 unlink (cache_file);
775 free (cache_file);
776 return FcFalse;
777 }
778
779 static char *
780 FcCacheMachineSignature ()
781 {
782 static char buf[MACHINE_SIGNATURE_SIZE];
783 int magic = ENDIAN_TEST;
784 char * m = (char *)&magic;
785
786 sprintf (buf, "%2x%2x%2x%2x "
787 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
788 "%4x %4x %4x %4x %4x %4x %4x\n",
789 m[0], m[1], m[2], m[3],
790 sizeof (char),
791 sizeof (char *),
792 sizeof (int),
793 sizeof (FcPattern),
794 sizeof (FcPatternEltPtr),
795 sizeof (struct _FcPatternElt *),
796 sizeof (FcPatternElt),
797 sizeof (FcObjectPtr),
798 sizeof (FcValueListPtr),
799 sizeof (FcValue),
800 sizeof (FcValueBinding),
801 sizeof (struct _FcValueList *),
802 sizeof (FcCharSet),
803 sizeof (FcCharLeaf **),
804 sizeof (FcChar16 *),
805 sizeof (FcChar16),
806 sizeof (FcCharLeaf),
807 sizeof (FcChar32),
808 sizeof (FcCache));
809
810 return buf;
811 }
812
813 static int banks_ptr = 0, banks_alloc = 0;
814 static int * bankId = 0;
815
816 static FcBool
817 FcCacheHaveBank (int bank)
818 {
819 int i;
820
821 if (bank < FC_BANK_FIRST)
822 return FcTrue;
823
824 for (i = 0; i < banks_ptr; i++)
825 if (bankId[i] == bank)
826 return FcTrue;
827
828 return FcFalse;
829 }
830
831 int
832 FcCacheBankToIndex (int bank)
833 {
834 static int lastBank = FC_BANK_DYNAMIC, lastIndex = -1;
835 int i;
836 int * b;
837
838 if (bank == lastBank)
839 return lastIndex;
840
841 for (i = 0; i < banks_ptr; i++)
842 if (bankId[i] == bank)
843 return i;
844
845 if (banks_ptr >= banks_alloc)
846 {
847 b = realloc (bankId, (banks_alloc + 4) * sizeof(int));
848 if (!b)
849 return -1;
850
851 bankId = b;
852 banks_alloc += 4;
853 }
854
855 i = banks_ptr++;
856 bankId[i] = bank;
857 return i;
858 }