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