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