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