]> git.wh0rd.org - fontconfig.git/blame - src/fccache.c
Build fontconfig.def from header files when needed.
[fontconfig.git] / src / fccache.c
CommitLineData
24330d27 1/*
46b51147 2 * Copyright © 2000 Keith Packard
03a212e5 3 * Copyright © 2005 Patrick Lam
24330d27
KP
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of Keith Packard not be used in
10 * advertising or publicity pertaining to distribution of the software without
11 * specific, written prior permission. Keith Packard makes no
12 * representations about the suitability of this software for any purpose. It
13 * is provided "as is" without express or implied warranty.
14 *
15 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21 * PERFORMANCE OF THIS SOFTWARE.
22 */
23
f045376c 24#include "fcint.h"
cf65c055 25#include "../fc-arch/fcarch.h"
00f059e9 26#include <stdio.h>
212c9f43 27#include <fcntl.h>
4262e0b3 28#include <dirent.h>
ea44e218 29#include <string.h>
fd77c154 30#include <sys/types.h>
9e612141 31#include <assert.h>
d6217cc6 32#if defined(HAVE_MMAP) || defined(__CYGWIN__)
93f67dfc 33# include <unistd.h>
d6217cc6 34# include <sys/mman.h>
93f67dfc
PL
35#elif defined(_WIN32)
36# include <windows.h>
d6217cc6 37#endif
24330d27 38
879af706
PL
39#ifndef O_BINARY
40#define O_BINARY 0
41#endif
42
ea44e218
PL
43struct MD5Context {
44 FcChar32 buf[4];
45 FcChar32 bits[2];
46 unsigned char in[64];
47};
48
49static void MD5Init(struct MD5Context *ctx);
50static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len);
51static void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
52static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]);
53
cf65c055 54#define CACHEBASE_LEN (1 + 32 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX))
7410e40b
PL
55
56static const char bin2hex[] = { '0', '1', '2', '3',
57 '4', '5', '6', '7',
58 '8', '9', 'a', 'b',
59 'c', 'd', 'e', 'f' };
60
61static FcChar8 *
62FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN])
55c8fa4f 63{
7410e40b
PL
64 unsigned char hash[16];
65 FcChar8 *hex_hash;
66 int cnt;
67 struct MD5Context ctx;
55c8fa4f 68
7410e40b
PL
69 MD5Init (&ctx);
70 MD5Update (&ctx, (unsigned char *)dir, strlen ((char *) dir));
3bfae75d 71
7410e40b
PL
72 MD5Final (hash, &ctx);
73
74 cache_base[0] = '/';
75 hex_hash = cache_base + 1;
76 for (cnt = 0; cnt < 16; ++cnt)
1178b569 77 {
7410e40b
PL
78 hex_hash[2*cnt ] = bin2hex[hash[cnt] >> 4];
79 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
80 }
81 hex_hash[2*cnt] = 0;
cf65c055 82 strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX);
6f9fcb51 83
7410e40b
PL
84 return cache_base;
85}
3bfae75d 86
7410e40b
PL
87FcBool
88FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
89{
7410e40b
PL
90 FcChar8 *cache_hashed = NULL;
91 FcChar8 cache_base[CACHEBASE_LEN];
92 FcStrList *list;
93 FcChar8 *cache_dir;
3bfae75d 94
7410e40b 95 FcDirCacheBasename (dir, cache_base);
3bfae75d 96
7410e40b
PL
97 list = FcStrListCreate (config->cacheDirs);
98 if (!list)
99 return FcFalse;
100
101 while ((cache_dir = FcStrListNext (list)))
3bfae75d 102 {
7410e40b
PL
103 cache_hashed = FcStrPlus (cache_dir, cache_base);
104 if (!cache_hashed)
105 break;
00f059e9 106 (void) unlink ((char *) cache_hashed);
1178b569 107 }
7410e40b
PL
108 FcStrListDone (list);
109 /* return FcFalse if something went wrong */
110 if (cache_dir)
111 return FcFalse;
1b7be377
PL
112 return FcTrue;
113}
114
4262e0b3 115static int
bc5e487f 116FcDirCacheOpenFile (const FcChar8 *cache_file, struct stat *file_stat)
4262e0b3 117{
bc5e487f 118 int fd;
4262e0b3 119
bc5e487f
KP
120 fd = open((char *) cache_file, O_RDONLY | O_BINARY);
121 if (fd < 0)
122 return fd;
123 if (fstat (fd, file_stat) < 0)
4262e0b3 124 {
bc5e487f
KP
125 close (fd);
126 return -1;
4262e0b3 127 }
bc5e487f 128 return fd;
4262e0b3
PL
129}
130
2d3387fd
KP
131/*
132 * Look for a cache file for the specified dir. Attempt
133 * to use each one we find, stopping when the callback
134 * indicates success
7410e40b 135 */
2d3387fd
KP
136static FcBool
137FcDirCacheProcess (FcConfig *config, const FcChar8 *dir,
8fe2104a 138 FcBool (*callback) (int fd, struct stat *stat, void *closure),
0a87ce71 139 void *closure, FcChar8 **cache_file_ret)
ea44e218 140{
7ce19673 141 int fd = -1;
7410e40b
PL
142 FcChar8 cache_base[CACHEBASE_LEN];
143 FcStrList *list;
144 FcChar8 *cache_dir;
d2f78684 145 struct stat file_stat, dir_stat;
2d3387fd 146 FcBool ret = FcFalse;
d2f78684
KP
147
148 if (stat ((char *) dir, &dir_stat) < 0)
2d3387fd 149 return FcFalse;
df3efc11 150
7410e40b 151 FcDirCacheBasename (dir, cache_base);
ea44e218 152
7410e40b
PL
153 list = FcStrListCreate (config->cacheDirs);
154 if (!list)
2d3387fd 155 return FcFalse;
7410e40b
PL
156
157 while ((cache_dir = FcStrListNext (list)))
ea44e218 158 {
7ce19673 159 FcChar8 *cache_hashed = FcStrPlus (cache_dir, cache_base);
7410e40b 160 if (!cache_hashed)
d00c3cb5 161 break;
bc5e487f 162 fd = FcDirCacheOpenFile (cache_hashed, &file_stat);
7ce19673 163 if (fd >= 0) {
bc5e487f 164 if (dir_stat.st_mtime <= file_stat.st_mtime)
d2f78684 165 {
8fe2104a 166 ret = (*callback) (fd, &file_stat, closure);
2d3387fd
KP
167 if (ret)
168 {
0a87ce71
KP
169 if (cache_file_ret)
170 *cache_file_ret = cache_hashed;
171 else
172 FcStrFree (cache_hashed);
2d3387fd
KP
173 close (fd);
174 break;
175 }
d2f78684 176 }
7ce19673 177 close (fd);
d2f78684 178 }
0a87ce71 179 FcStrFree (cache_hashed);
7410e40b
PL
180 }
181 FcStrListDone (list);
7410e40b 182
2d3387fd 183 return ret;
ea44e218
PL
184}
185
9e612141
KP
186#define FC_CACHE_MIN_MMAP 1024
187
188/*
189 * Skip list element, make sure the 'next' pointer is the last thing
190 * in the structure, it will be allocated large enough to hold all
191 * of the necessary pointers
192 */
193
194typedef struct _FcCacheSkip FcCacheSkip;
195
196struct _FcCacheSkip {
197 FcCache *cache;
198 int ref;
199 intptr_t size;
200 dev_t cache_dev;
201 ino_t cache_ino;
202 time_t cache_mtime;
203 FcCacheSkip *next[1];
204};
205
206/*
207 * The head of the skip list; pointers for every possible level
208 * in the skip list, plus the largest level in the list
209 */
210
211#define FC_CACHE_MAX_LEVEL 16
212
213static FcCacheSkip *fcCacheChains[FC_CACHE_MAX_LEVEL];
214static int fcCacheMaxLevel;
215
216/*
217 * Generate a random level number, distributed
218 * so that each level is 1/4 as likely as the one before
219 *
220 * Note that level numbers run 1 <= level <= MAX_LEVEL
221 */
222static int
223random_level (void)
224{
225 /* tricky bit -- each bit is '1' 75% of the time */
226 long int bits = random () | random ();
227 int level = 0;
228
229 while (++level < FC_CACHE_MAX_LEVEL)
230 {
231 if (bits & 1)
232 break;
233 bits >>= 1;
234 }
235 return level;
236}
237
238/*
239 * Insert cache into the list
240 */
241static FcBool
242FcCacheInsert (FcCache *cache, struct stat *cache_stat)
243{
244 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
245 FcCacheSkip *s, **next;
246 int i, level;
247
248 /*
249 * Find links along each chain
250 */
251 next = fcCacheChains;
252 for (i = fcCacheMaxLevel; --i >= 0; )
253 {
254 for (; (s = next[i]); next = s->next)
255 if (s->cache > cache)
256 break;
257 update[i] = &next[i];
258 }
259
260 /*
261 * Create new list element
262 */
263 level = random_level ();
264 if (level > fcCacheMaxLevel)
265 {
266 level = fcCacheMaxLevel + 1;
267 update[fcCacheMaxLevel] = &fcCacheChains[fcCacheMaxLevel];
268 fcCacheMaxLevel = level;
269 }
270
271 s = malloc (sizeof (FcCacheSkip) + (level - 1) * sizeof (FcCacheSkip *));
272 if (!s)
273 return FcFalse;
274
275 s->cache = cache;
276 s->size = cache->size;
277 s->ref = 1;
49b44b27
KP
278 if (cache_stat)
279 {
280 s->cache_dev = cache_stat->st_dev;
281 s->cache_ino = cache_stat->st_ino;
282 s->cache_mtime = cache_stat->st_mtime;
283 }
284 else
285 {
286 s->cache_dev = 0;
287 s->cache_ino = 0;
288 s->cache_mtime = 0;
289 }
9e612141
KP
290
291 /*
292 * Insert into all fcCacheChains
293 */
294 for (i = 0; i < level; i++)
295 {
296 s->next[i] = *update[i];
297 *update[i] = s;
298 }
299 return FcTrue;
300}
301
302static FcCacheSkip *
303FcCacheFindByAddr (void *object)
304{
305 int i;
306 FcCacheSkip **next = fcCacheChains;
307 FcCacheSkip *s;
308
309 /*
310 * Walk chain pointers one level at a time
311 */
312 for (i = fcCacheMaxLevel; --i >= 0;)
313 while (next[i] && (char *) object >= ((char *) next[i]->cache + next[i]->size))
314 next = next[i]->next;
315 /*
316 * Here we are
317 */
318 s = next[0];
319 if (s && (char *) object < ((char *) s->cache + s->size))
320 return s;
321 return NULL;
322}
323
324static void
325FcCacheRemove (FcCache *cache)
326{
327 FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
328 FcCacheSkip *s, **next;
329 int i;
330
331 /*
332 * Find links along each chain
333 */
334 next = fcCacheChains;
335 for (i = fcCacheMaxLevel; --i >= 0; )
336 {
337 for (; (s = next[i]); next = s->next)
338 if (s->cache >= cache)
339 break;
340 update[i] = &next[i];
341 }
342 s = next[0];
9e612141
KP
343 for (i = 0; i < fcCacheMaxLevel && *update[i] == s; i++)
344 *update[i] = s->next[i];
345 while (fcCacheMaxLevel > 0 && fcCacheChains[fcCacheMaxLevel - 1] == NULL)
346 fcCacheMaxLevel--;
347 free (s);
348}
349
350static FcCache *
351FcCacheFindByStat (struct stat *cache_stat)
352{
353 FcCacheSkip *s;
354
355 for (s = fcCacheChains[0]; s; s = s->next[0])
356 if (s->cache_dev == cache_stat->st_dev &&
357 s->cache_ino == cache_stat->st_ino &&
358 s->cache_mtime == cache_stat->st_mtime)
323ecd0c
KP
359 {
360 s->ref++;
9e612141 361 return s->cache;
323ecd0c 362 }
9e612141
KP
363 return NULL;
364}
365
8fe2104a
KP
366static void
367FcDirCacheDispose (FcCache *cache)
368{
369 switch (cache->magic) {
370 case FC_CACHE_MAGIC_ALLOC:
371 free (cache);
372 break;
373 case FC_CACHE_MAGIC_MMAP:
374#if defined(HAVE_MMAP) || defined(__CYGWIN__)
375 munmap (cache, cache->size);
376#elif defined(_WIN32)
377 UnmapViewOfFile (cache);
378#endif
379 break;
380 }
9e612141 381 FcCacheRemove (cache);
8fe2104a
KP
382}
383
9e612141
KP
384void
385FcCacheObjectReference (void *object)
8fe2104a 386{
9e612141 387 FcCacheSkip *skip = FcCacheFindByAddr (object);
8fe2104a 388
9e612141
KP
389 if (skip)
390 skip->ref++;
8fe2104a
KP
391}
392
9e612141
KP
393void
394FcCacheObjectDereference (void *object)
8fe2104a 395{
9e612141 396 FcCacheSkip *skip = FcCacheFindByAddr (object);
8fe2104a 397
9e612141
KP
398 if (skip)
399 {
400 skip->ref--;
401 if (skip->ref <= 0)
402 FcDirCacheDispose (skip->cache);
403 }
8fe2104a
KP
404}
405
406void
407FcCacheFini (void)
408{
409 int i;
8fe2104a 410
9e612141
KP
411 for (i = 0; i < FC_CACHE_MAX_LEVEL; i++)
412 assert (fcCacheChains[i] == NULL);
413 assert (fcCacheMaxLevel == 0);
8fe2104a
KP
414}
415
bc5e487f
KP
416/*
417 * Map a cache file into memory
418 */
419static FcCache *
8fe2104a 420FcDirCacheMapFd (int fd, struct stat *fd_stat)
212c9f43 421{
8fe2104a 422 FcCache *cache;
7ce19673 423 FcBool allocated = FcFalse;
af180c40 424
8fe2104a 425 if (fd_stat->st_size < sizeof (FcCache))
bc5e487f 426 return NULL;
9e612141 427 cache = FcCacheFindByStat (fd_stat);
8fe2104a
KP
428 if (cache)
429 return cache;
bc5e487f
KP
430 /*
431 * For small cache files, just read them into memory
432 */
8fe2104a 433 if (fd_stat->st_size >= FC_CACHE_MIN_MMAP)
bc5e487f 434 {
d6217cc6 435#if defined(HAVE_MMAP) || defined(__CYGWIN__)
8fe2104a 436 cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0);
93f67dfc 437#elif defined(_WIN32)
7ce19673 438 {
bc5e487f
KP
439 HANDLE hFileMap;
440
441 cache = NULL;
442 hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL,
443 PAGE_READONLY, 0, 0, NULL);
444 if (hFileMap != NULL)
445 {
446 cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0, size);
447 CloseHandle (hFileMap);
448 }
93f67dfc 449 }
7ce19673 450#endif
bc5e487f 451 }
7ce19673
KP
452 if (!cache)
453 {
8fe2104a 454 cache = malloc (fd_stat->st_size);
7ce19673 455 if (!cache)
bc5e487f 456 return NULL;
eb0cf671 457
8fe2104a 458 if (read (fd, cache, fd_stat->st_size) != fd_stat->st_size)
00f059e9 459 {
7ce19673 460 free (cache);
bc5e487f 461 return NULL;
00f059e9 462 }
7ce19673
KP
463 allocated = FcTrue;
464 }
bc5e487f 465 if (cache->magic != FC_CACHE_MAGIC_MMAP ||
3b8a03c0 466 cache->version < FC_CACHE_CONTENT_VERSION ||
8fe2104a 467 cache->size != fd_stat->st_size ||
9e612141 468 !FcCacheInsert (cache, fd_stat))
7ce19673
KP
469 {
470 if (allocated)
471 free (cache);
472 else
473 {
474#if defined(HAVE_MMAP) || defined(__CYGWIN__)
8fe2104a 475 munmap (cache, fd_stat->st_size);
7ce19673
KP
476#elif defined(_WIN32)
477 UnmapViewOfFile (cache);
00f059e9 478#endif
7ce19673 479 }
bc5e487f 480 return NULL;
00f059e9 481 }
af180c40 482
7ce19673
KP
483 /* Mark allocated caches so they're freed rather than unmapped */
484 if (allocated)
bc5e487f 485 cache->magic = FC_CACHE_MAGIC_ALLOC;
7ce19673 486
bc5e487f
KP
487 return cache;
488}
489
17389539
KP
490void
491FcDirCacheReference (FcCache *cache, int nref)
492{
493 FcCacheSkip *skip = FcCacheFindByAddr (cache);
494
495 if (skip)
496 skip->ref += nref;
497}
498
bc5e487f
KP
499void
500FcDirCacheUnload (FcCache *cache)
501{
9e612141 502 FcCacheObjectDereference (cache);
bc5e487f
KP
503}
504
505static FcBool
8fe2104a 506FcDirCacheMapHelper (int fd, struct stat *fd_stat, void *closure)
bc5e487f 507{
8fe2104a 508 FcCache *cache = FcDirCacheMapFd (fd, fd_stat);
bc5e487f
KP
509
510 if (!cache)
511 return FcFalse;
2d3387fd
KP
512 *((FcCache **) closure) = cache;
513 return FcTrue;
514}
515
516FcCache *
bc5e487f 517FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file)
2d3387fd 518{
76abb77f 519 FcCache *cache = NULL;
2d3387fd 520
76abb77f 521 if (!FcDirCacheProcess (config, dir,
bc5e487f 522 FcDirCacheMapHelper,
0a87ce71 523 &cache, cache_file))
76abb77f
KP
524 return NULL;
525 return cache;
af180c40
KP
526}
527
bc5e487f
KP
528FcCache *
529FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat)
af180c40 530{
bc5e487f
KP
531 int fd;
532 FcCache *cache;
af180c40 533
bc5e487f
KP
534 fd = FcDirCacheOpenFile (cache_file, file_stat);
535 if (fd < 0)
536 return NULL;
8fe2104a 537 cache = FcDirCacheMapFd (fd, file_stat);
bc5e487f
KP
538 close (fd);
539 return cache;
eb0cf671 540}
bc5e487f
KP
541
542/*
543 * Validate a cache file by reading the header and checking
544 * the magic number and the size field
545 */
2d3387fd 546static FcBool
8fe2104a 547FcDirCacheValidateHelper (int fd, struct stat *fd_stat, void *closure)
2d3387fd
KP
548{
549 FcBool ret = FcTrue;
550 FcCache c;
2d3387fd
KP
551
552 if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache))
553 ret = FcFalse;
bc5e487f
KP
554 else if (c.magic != FC_CACHE_MAGIC_MMAP)
555 ret = FcFalse;
3b8a03c0 556 else if (c.version < FC_CACHE_CONTENT_VERSION)
2d3387fd 557 ret = FcFalse;
8fe2104a 558 else if (fd_stat->st_size != c.size)
2d3387fd
KP
559 ret = FcFalse;
560 return ret;
561}
562
f57783d2
KP
563static FcBool
564FcDirCacheValidConfig (const FcChar8 *dir, FcConfig *config)
2d3387fd 565{
bc5e487f
KP
566 return FcDirCacheProcess (config, dir,
567 FcDirCacheValidateHelper,
568 NULL, NULL);
2d3387fd
KP
569}
570
f57783d2
KP
571FcBool
572FcDirCacheValid (const FcChar8 *dir)
573{
574 FcConfig *config;
575
576 config = FcConfigGetCurrent ();
577 if (!config)
578 return FcFalse;
579
580 return FcDirCacheValidConfig (dir, config);
581}
582
7ce19673 583/*
bc5e487f 584 * Build a cache structure from the given contents
7ce19673 585 */
bc5e487f
KP
586FcCache *
587FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, FcStrSet *dirs)
eb0cf671 588{
7ce19673
KP
589 FcSerialize *serialize = FcSerializeCreate ();
590 FcCache *cache;
591 int i;
592 intptr_t cache_offset;
593 intptr_t dirs_offset;
594 FcChar8 *dir_serialize;
595 intptr_t *dirs_serialize;
596 FcFontSet *set_serialize;
597
598 if (!serialize)
599 return NULL;
600 /*
601 * Space for cache structure
602 */
603 cache_offset = FcSerializeReserve (serialize, sizeof (FcCache));
604 /*
605 * Directory name
606 */
607 if (!FcStrSerializeAlloc (serialize, dir))
608 goto bail1;
609 /*
610 * Subdirs
611 */
612 dirs_offset = FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *));
613 for (i = 0; i < dirs->num; i++)
614 if (!FcStrSerializeAlloc (serialize, dirs->strs[i]))
615 goto bail1;
eb0cf671 616
7ce19673
KP
617 /*
618 * Patterns
619 */
620 if (!FcFontSetSerializeAlloc (serialize, set))
621 goto bail1;
622
623 /* Serialize layout complete. Now allocate space and fill it */
624 cache = malloc (serialize->size);
625 if (!cache)
626 goto bail1;
627 /* shut up valgrind */
628 memset (cache, 0, serialize->size);
d6217cc6 629
7ce19673 630 serialize->linear = cache;
eb0cf671 631
bc5e487f
KP
632 cache->magic = FC_CACHE_MAGIC_ALLOC;
633 cache->version = FC_CACHE_CONTENT_VERSION;
7ce19673 634 cache->size = serialize->size;
eb0cf671 635
7ce19673
KP
636 /*
637 * Serialize directory name
638 */
639 dir_serialize = FcStrSerialize (serialize, dir);
640 if (!dir_serialize)
641 goto bail2;
642 cache->dir = FcPtrToOffset (cache, dir_serialize);
643
644 /*
645 * Serialize sub dirs
646 */
647 dirs_serialize = FcSerializePtr (serialize, dirs);
648 if (!dirs_serialize)
649 goto bail2;
650 cache->dirs = FcPtrToOffset (cache, dirs_serialize);
651 cache->dirs_count = dirs->num;
652 for (i = 0; i < dirs->num; i++)
8e351527 653 {
7ce19673
KP
654 FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]);
655 if (!d_serialize)
656 goto bail2;
657 dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize);
8e351527 658 }
7ce19673
KP
659
660 /*
661 * Serialize font set
662 */
663 set_serialize = FcFontSetSerialize (serialize, set);
664 if (!set_serialize)
665 goto bail2;
666 cache->set = FcPtrToOffset (cache, set_serialize);
eb0cf671 667
7ce19673
KP
668 FcSerializeDestroy (serialize);
669
49b44b27
KP
670 FcCacheInsert (cache, NULL);
671
7ce19673 672 return cache;
212c9f43 673
7ce19673
KP
674bail2:
675 free (cache);
676bail1:
677 FcSerializeDestroy (serialize);
678 return NULL;
212c9f43
PL
679}
680
7410e40b
PL
681static FcBool
682FcMakeDirectory (const FcChar8 *dir)
683{
684 FcChar8 *parent;
685 FcBool ret;
686
687 if (strlen ((char *) dir) == 0)
688 return FcFalse;
689
690 parent = FcStrDirname (dir);
691 if (!parent)
692 return FcFalse;
693 if (access ((char *) parent, W_OK|X_OK) == 0)
694 ret = mkdir ((char *) dir, 0777) == 0;
695 else if (access ((char *) parent, F_OK) == -1)
696 ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0777) == 0);
697 else
698 ret = FcFalse;
699 FcStrFree (parent);
700 return ret;
701}
702
212c9f43
PL
703/* write serialized state to the cache file */
704FcBool
bc5e487f 705FcDirCacheWrite (FcCache *cache, FcConfig *config)
212c9f43 706{
bc5e487f 707 FcChar8 *dir = FcCacheDir (cache);
7410e40b
PL
708 FcChar8 cache_base[CACHEBASE_LEN];
709 FcChar8 *cache_hashed;
7ce19673 710 int fd;
1d879de2 711 FcAtomic *atomic;
00f059e9 712 FcStrList *list;
d2f78684
KP
713 FcChar8 *cache_dir = NULL;
714 FcChar8 *test_dir;
bc5e487f
KP
715 int magic;
716 int written;
212c9f43 717
7410e40b 718 /*
d2f78684 719 * Write it to the first directory in the list which is writable
7410e40b 720 */
d2f78684
KP
721
722 list = FcStrListCreate (config->cacheDirs);
723 if (!list)
724 return FcFalse;
725 while ((test_dir = FcStrListNext (list))) {
726 if (access ((char *) test_dir, W_OK|X_OK) == 0)
727 {
728 cache_dir = test_dir;
729 break;
730 }
731 else
732 {
7410e40b
PL
733 /*
734 * If the directory doesn't exist, try to create it
735 */
d2f78684
KP
736 if (access ((char *) test_dir, F_OK) == -1) {
737 if (FcMakeDirectory (test_dir))
738 {
739 cache_dir = test_dir;
7410e40b 740 break;
d2f78684 741 }
7410e40b
PL
742 }
743 }
7410e40b 744 }
d2f78684
KP
745 FcStrListDone (list);
746 if (!cache_dir)
747 return FcFalse;
7ce19673 748
d2f78684
KP
749 FcDirCacheBasename (dir, cache_base);
750 cache_hashed = FcStrPlus (cache_dir, cache_base);
751 if (!cache_hashed)
752 return FcFalse;
ea44e218 753
4262e0b3 754 if (FcDebug () & FC_DBG_CACHE)
c1c3ba06
KP
755 printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
756 dir, cache_hashed);
4262e0b3 757
3bfae75d 758 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
1d879de2 759 if (!atomic)
bc5e487f 760 goto bail1;
212c9f43 761
1d879de2 762 if (!FcAtomicLock (atomic))
7ce19673 763 goto bail3;
ec760b17 764
879af706 765 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
1d879de2 766 if (fd == -1)
7ce19673 767 goto bail4;
00f059e9 768
bc5e487f
KP
769 /* Temporarily switch magic to MMAP while writing to file */
770 magic = cache->magic;
771 if (magic != FC_CACHE_MAGIC_MMAP)
772 cache->magic = FC_CACHE_MAGIC_MMAP;
773
774 /*
775 * Write cache contents to file
776 */
777 written = write (fd, cache, cache->size);
778
779 /* Switch magic back */
780 if (magic != FC_CACHE_MAGIC_MMAP)
781 cache->magic = magic;
782
783 if (written != cache->size)
fff5a5af 784 {
7ce19673 785 perror ("write cache");
68355f38
PL
786 goto bail5;
787 }
4262e0b3 788
212c9f43 789 close(fd);
1d879de2 790 if (!FcAtomicReplaceOrig(atomic))
7ce19673 791 goto bail4;
bc5e487f 792 FcStrFree (cache_hashed);
1d879de2
PL
793 FcAtomicUnlock (atomic);
794 FcAtomicDestroy (atomic);
212c9f43
PL
795 return FcTrue;
796
ea44e218 797 bail5:
1d879de2 798 close (fd);
7ce19673 799 bail4:
1d879de2 800 FcAtomicUnlock (atomic);
7ce19673 801 bail3:
1d879de2 802 FcAtomicDestroy (atomic);
ea44e218 803 bail1:
bc5e487f 804 FcStrFree (cache_hashed);
4262e0b3
PL
805 return FcFalse;
806}
807
4984242e
KP
808/*
809 * Hokey little macro trick to permit the definitions of C functions
810 * with the same name as CPP macros
811 */
812#define args(x...) (x)
813
814const FcChar8 *
815FcCacheDir args(const FcCache *c)
816{
817 return FcCacheDir (c);
818}
819
820FcFontSet *
821FcCacheCopySet args(const FcCache *c)
822{
823 FcFontSet *old = FcCacheSet (c);
824 FcFontSet *new = FcFontSetCreate ();
825 int i;
826
827 if (!new)
828 return NULL;
829 for (i = 0; i < old->nfont; i++)
8d779ce4
KP
830 {
831 FcPattern *font = FcFontSetFont (old, i);
832
833 FcPatternReference (font);
834 if (!FcFontSetAdd (new, font))
4984242e
KP
835 {
836 FcFontSetDestroy (new);
837 return NULL;
838 }
8d779ce4 839 }
4984242e
KP
840 return new;
841}
842
843const FcChar8 *
844FcCacheSubdir args(const FcCache *c, int i)
845{
846 return FcCacheSubdir (c, i);
847}
848
849int
850FcCacheNumSubdir args(const FcCache *c)
851{
852 return c->dirs_count;
853}
854
855int
856FcCacheNumFont args(const FcCache *c)
857{
858 return FcCacheSet(c)->nfont;
859}
860
ea44e218
PL
861/*
862 * This code implements the MD5 message-digest algorithm.
863 * The algorithm is due to Ron Rivest. This code was
864 * written by Colin Plumb in 1993, no copyright is claimed.
865 * This code is in the public domain; do with it what you wish.
866 *
867 * Equivalent code is available from RSA Data Security, Inc.
868 * This code has been tested against that, and is equivalent,
869 * except that you don't need to include two pages of legalese
870 * with every copy.
871 *
872 * To compute the message digest of a chunk of bytes, declare an
873 * MD5Context structure, pass it to MD5Init, call MD5Update as
874 * needed on buffers full of bytes, and then call MD5Final, which
875 * will fill a supplied 16-byte array with the digest.
876 */
877
878#ifndef HIGHFIRST
879#define byteReverse(buf, len) /* Nothing */
880#else
881/*
882 * Note: this code is harmless on little-endian machines.
883 */
884void byteReverse(unsigned char *buf, unsigned longs)
885{
886 FcChar32 t;
887 do {
888 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
889 ((unsigned) buf[1] << 8 | buf[0]);
890 *(FcChar32 *) buf = t;
891 buf += 4;
892 } while (--longs);
893}
894#endif
895
896/*
897 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
898 * initialization constants.
899 */
900static void MD5Init(struct MD5Context *ctx)
901{
902 ctx->buf[0] = 0x67452301;
903 ctx->buf[1] = 0xefcdab89;
904 ctx->buf[2] = 0x98badcfe;
905 ctx->buf[3] = 0x10325476;
906
907 ctx->bits[0] = 0;
908 ctx->bits[1] = 0;
909}
910
911/*
912 * Update context to reflect the concatenation of another buffer full
913 * of bytes.
914 */
915static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len)
916{
917 FcChar32 t;
918
919 /* Update bitcount */
920
921 t = ctx->bits[0];
922 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
923 ctx->bits[1]++; /* Carry from low to high */
924 ctx->bits[1] += len >> 29;
925
926 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
927
928 /* Handle any leading odd-sized chunks */
929
930 if (t) {
931 unsigned char *p = (unsigned char *) ctx->in + t;
932
933 t = 64 - t;
934 if (len < t) {
935 memcpy(p, buf, len);
936 return;
937 }
938 memcpy(p, buf, t);
939 byteReverse(ctx->in, 16);
940 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
941 buf += t;
942 len -= t;
943 }
944 /* Process data in 64-byte chunks */
945
946 while (len >= 64) {
947 memcpy(ctx->in, buf, 64);
948 byteReverse(ctx->in, 16);
949 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
950 buf += 64;
951 len -= 64;
952 }
953
954 /* Handle any remaining bytes of data. */
955
956 memcpy(ctx->in, buf, len);
957}
958
959/*
960 * Final wrapup - pad to 64-byte boundary with the bit pattern
961 * 1 0* (64-bit count of bits processed, MSB-first)
962 */
963static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
964{
965 unsigned count;
966 unsigned char *p;
967
968 /* Compute number of bytes mod 64 */
969 count = (ctx->bits[0] >> 3) & 0x3F;
970
971 /* Set the first char of padding to 0x80. This is safe since there is
972 always at least one byte free */
973 p = ctx->in + count;
974 *p++ = 0x80;
975
976 /* Bytes of padding needed to make 64 bytes */
977 count = 64 - 1 - count;
978
979 /* Pad out to 56 mod 64 */
980 if (count < 8) {
981 /* Two lots of padding: Pad the first block to 64 bytes */
982 memset(p, 0, count);
983 byteReverse(ctx->in, 16);
984 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
985
986 /* Now fill the next block with 56 bytes */
987 memset(ctx->in, 0, 56);
988 } else {
989 /* Pad block to 56 bytes */
990 memset(p, 0, count - 8);
991 }
992 byteReverse(ctx->in, 14);
993
994 /* Append length in bits and transform */
995 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
996 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
997
998 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
999 byteReverse((unsigned char *) ctx->buf, 4);
1000 memcpy(digest, ctx->buf, 16);
1001 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
1002}
1003
1004
1005/* The four core functions - F1 is optimized somewhat */
1006
1007/* #define F1(x, y, z) (x & y | ~x & z) */
1008#define F1(x, y, z) (z ^ (x & (y ^ z)))
1009#define F2(x, y, z) F1(z, x, y)
1010#define F3(x, y, z) (x ^ y ^ z)
1011#define F4(x, y, z) (y ^ (x | ~z))
1012
1013/* This is the central step in the MD5 algorithm. */
1014#define MD5STEP(f, w, x, y, z, data, s) \
1015 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1016
1017/*
1018 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1019 * reflect the addition of 16 longwords of new data. MD5Update blocks
1020 * the data and converts bytes into longwords for this routine.
1021 */
1022static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1023{
1024 register FcChar32 a, b, c, d;
1025
1026 a = buf[0];
1027 b = buf[1];
1028 c = buf[2];
1029 d = buf[3];
1030
1031 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1032 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1033 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1034 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1035 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1036 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1037 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1038 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1039 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1040 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1041 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1042 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1043 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1044 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1045 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1046 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1047
1048 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1049 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1050 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1051 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1052 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1053 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1054 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1055 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1056 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1057 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1058 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1059 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1060 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1061 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1062 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1063 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1064
1065 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1066 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1067 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1068 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1069 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1070 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1071 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1072 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1073 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1074 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1075 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1076 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1077 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1078 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1079 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1080 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1081
1082 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1083 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1084 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1085 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1086 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1087 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1088 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1089 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1090 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1091 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1092 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1093 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1094 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1095 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1096 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1097 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1098
1099 buf[0] += a;
1100 buf[1] += b;
1101 buf[2] += c;
1102 buf[3] += d;
1103}
23816bf9
KP
1104#define __fccache__
1105#include "fcaliastail.h"
1106#undef __fccache__