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