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