]>
Commit | Line | Data |
---|---|---|
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 | |
5aaf466d | 9 | * documentation, and that the name of the author(s) not be used in |
24330d27 | 10 | * advertising or publicity pertaining to distribution of the software without |
5aaf466d | 11 | * specific, written prior permission. The authors make no |
24330d27 KP |
12 | * representations about the suitability of this software for any purpose. It |
13 | * is provided "as is" without express or implied warranty. | |
14 | * | |
3074a73b | 15 | * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
24330d27 | 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
3074a73b | 17 | * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
24330d27 KP |
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" |
d1a0fca3 | 25 | #include "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 | 35 | #elif defined(_WIN32) |
8a3dc488 | 36 | # define _WIN32_WINNT 0x0500 |
93f67dfc | 37 | # include <windows.h> |
d6217cc6 | 38 | #endif |
24330d27 | 39 | |
879af706 PL |
40 | #ifndef O_BINARY |
41 | #define O_BINARY 0 | |
42 | #endif | |
43 | ||
d1a0fca3 | 44 | |
ea44e218 PL |
45 | struct MD5Context { |
46 | FcChar32 buf[4]; | |
47 | FcChar32 bits[2]; | |
48 | unsigned char in[64]; | |
49 | }; | |
50 | ||
51 | static void MD5Init(struct MD5Context *ctx); | |
db6f19f1 | 52 | static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len); |
ea44e218 PL |
53 | static void MD5Final(unsigned char digest[16], struct MD5Context *ctx); |
54 | static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]); | |
55 | ||
cf65c055 | 56 | #define CACHEBASE_LEN (1 + 32 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX)) |
7410e40b | 57 | |
8a3dc488 TL |
58 | #ifdef _WIN32 |
59 | ||
60 | #include <windows.h> | |
61 | ||
62 | #ifdef __GNUC__ | |
63 | typedef long long INT64; | |
64 | #define EPOCH_OFFSET 11644473600ll | |
65 | #else | |
66 | #define EPOCH_OFFSET 11644473600i64 | |
67 | typedef __int64 INT64; | |
68 | #endif | |
69 | ||
70 | /* Workaround for problems in the stat() in the Microsoft C library: | |
71 | * | |
72 | * 1) stat() uses FindFirstFile() to get the file | |
73 | * attributes. Unfortunately this API doesn't return correct values | |
74 | * for modification time of a directory until some time after a file | |
75 | * or subdirectory has been added to the directory. (This causes | |
76 | * run-test.sh to fail, for instance.) GetFileAttributesEx() is | |
77 | * better, it returns the updated timestamp right away. | |
78 | * | |
79 | * 2) stat() does some strange things related to backward | |
80 | * compatibility with the local time timestamps on FAT volumes and | |
81 | * daylight saving time. This causes problems after the switches | |
82 | * to/from daylight saving time. See | |
83 | * http://bugzilla.gnome.org/show_bug.cgi?id=154968 , especially | |
84 | * comment #30, and http://www.codeproject.com/datetime/dstbugs.asp . | |
85 | * We don't need any of that, FAT and Win9x are as good as dead. So | |
86 | * just use the UTC timestamps from NTFS, converted to the Unix epoch. | |
87 | */ | |
88 | ||
f69db8d4 | 89 | int |
d0471dd2 | 90 | FcStat (FcConfig *config, const FcChar8 *file, struct stat *statb) |
8a3dc488 TL |
91 | { |
92 | WIN32_FILE_ATTRIBUTE_DATA wfad; | |
93 | char full_path_name[MAX_PATH]; | |
94 | char *basename; | |
95 | DWORD rc; | |
594dcef0 | 96 | |
8a3dc488 TL |
97 | if (!GetFileAttributesEx (file, GetFileExInfoStandard, &wfad)) |
98 | return -1; | |
594dcef0 | 99 | |
8a3dc488 TL |
100 | statb->st_dev = 0; |
101 | ||
102 | /* Calculate a pseudo inode number as a hash of the full path name. | |
103 | * Call GetLongPathName() to get the spelling of the path name as it | |
104 | * is on disk. | |
105 | */ | |
106 | rc = GetFullPathName (file, sizeof (full_path_name), full_path_name, &basename); | |
107 | if (rc == 0 || rc > sizeof (full_path_name)) | |
108 | return -1; | |
109 | ||
110 | rc = GetLongPathName (full_path_name, full_path_name, sizeof (full_path_name)); | |
111 | statb->st_ino = FcStringHash (full_path_name); | |
594dcef0 | 112 | |
8a3dc488 TL |
113 | statb->st_mode = _S_IREAD | _S_IWRITE; |
114 | statb->st_mode |= (statb->st_mode >> 3) | (statb->st_mode >> 6); | |
115 | ||
116 | if (wfad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) | |
117 | statb->st_mode |= _S_IFDIR; | |
118 | else | |
119 | statb->st_mode |= _S_IFREG; | |
594dcef0 | 120 | |
8a3dc488 TL |
121 | statb->st_nlink = 1; |
122 | statb->st_uid = statb->st_gid = 0; | |
123 | statb->st_rdev = 0; | |
594dcef0 | 124 | |
8a3dc488 TL |
125 | if (wfad.nFileSizeHigh > 0) |
126 | return -1; | |
127 | statb->st_size = wfad.nFileSizeLow; | |
594dcef0 | 128 | |
8a3dc488 TL |
129 | statb->st_atime = (*(INT64 *)&wfad.ftLastAccessTime)/10000000 - EPOCH_OFFSET; |
130 | statb->st_mtime = (*(INT64 *)&wfad.ftLastWriteTime)/10000000 - EPOCH_OFFSET; | |
131 | statb->st_ctime = statb->st_mtime; | |
594dcef0 | 132 | |
8a3dc488 TL |
133 | return 0; |
134 | } | |
c21fb9ac BE |
135 | |
136 | #else | |
137 | ||
138 | int | |
d0471dd2 | 139 | FcStat (FcConfig *config, const FcChar8 *file, struct stat *statb) |
c21fb9ac | 140 | { |
d0471dd2 MF |
141 | int ret; |
142 | FcChar8 *fullFile = FcConfigGetRootPlus (config, file); | |
143 | ||
144 | if (fullFile) | |
145 | file = fullFile; | |
146 | ret = stat ((char *) file, statb); | |
147 | if (fullFile) | |
148 | FcStrFree (fullFile); | |
149 | ||
150 | return ret; | |
c21fb9ac BE |
151 | } |
152 | ||
8a3dc488 TL |
153 | #endif |
154 | ||
7410e40b PL |
155 | static const char bin2hex[] = { '0', '1', '2', '3', |
156 | '4', '5', '6', '7', | |
157 | '8', '9', 'a', 'b', | |
158 | 'c', 'd', 'e', 'f' }; | |
159 | ||
160 | static FcChar8 * | |
161 | FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN]) | |
55c8fa4f | 162 | { |
7410e40b PL |
163 | unsigned char hash[16]; |
164 | FcChar8 *hex_hash; | |
165 | int cnt; | |
166 | struct MD5Context ctx; | |
55c8fa4f | 167 | |
7410e40b | 168 | MD5Init (&ctx); |
db6f19f1 | 169 | MD5Update (&ctx, (const unsigned char *)dir, strlen ((const char *) dir)); |
3bfae75d | 170 | |
7410e40b PL |
171 | MD5Final (hash, &ctx); |
172 | ||
173 | cache_base[0] = '/'; | |
174 | hex_hash = cache_base + 1; | |
175 | for (cnt = 0; cnt < 16; ++cnt) | |
1178b569 | 176 | { |
7410e40b PL |
177 | hex_hash[2*cnt ] = bin2hex[hash[cnt] >> 4]; |
178 | hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf]; | |
179 | } | |
180 | hex_hash[2*cnt] = 0; | |
cf65c055 | 181 | strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX); |
6f9fcb51 | 182 | |
7410e40b PL |
183 | return cache_base; |
184 | } | |
3bfae75d | 185 | |
7410e40b PL |
186 | FcBool |
187 | FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config) | |
188 | { | |
7410e40b PL |
189 | FcChar8 *cache_hashed = NULL; |
190 | FcChar8 cache_base[CACHEBASE_LEN]; | |
191 | FcStrList *list; | |
192 | FcChar8 *cache_dir; | |
3bfae75d | 193 | |
7410e40b | 194 | FcDirCacheBasename (dir, cache_base); |
3bfae75d | 195 | |
7410e40b PL |
196 | list = FcStrListCreate (config->cacheDirs); |
197 | if (!list) | |
198 | return FcFalse; | |
199 | ||
200 | while ((cache_dir = FcStrListNext (list))) | |
3bfae75d | 201 | { |
7410e40b PL |
202 | cache_hashed = FcStrPlus (cache_dir, cache_base); |
203 | if (!cache_hashed) | |
204 | break; | |
00f059e9 | 205 | (void) unlink ((char *) cache_hashed); |
3ae9258f | 206 | FcStrFree (cache_hashed); |
1178b569 | 207 | } |
7410e40b PL |
208 | FcStrListDone (list); |
209 | /* return FcFalse if something went wrong */ | |
210 | if (cache_dir) | |
211 | return FcFalse; | |
1b7be377 PL |
212 | return FcTrue; |
213 | } | |
214 | ||
4262e0b3 | 215 | static int |
d0471dd2 | 216 | FcDirCacheOpenFile (FcConfig *config, const FcChar8 *cache_file, struct stat *file_stat) |
4262e0b3 | 217 | { |
bc5e487f | 218 | int fd; |
d0471dd2 | 219 | FcChar8 *fullFile; |
4262e0b3 | 220 | |
8a3dc488 | 221 | #ifdef _WIN32 |
d0471dd2 | 222 | if (FcStat (config, cache_file, file_stat) < 0) |
8a3dc488 TL |
223 | return -1; |
224 | #endif | |
d0471dd2 MF |
225 | fullFile = FcConfigGetRootPlus (config, cache_file); |
226 | if (fullFile) | |
227 | cache_file = fullFile; | |
228 | fd = open ((char *) cache_file, O_RDONLY | O_BINARY); | |
229 | if (fullFile) | |
230 | FcStrFree (fullFile); | |
bc5e487f KP |
231 | if (fd < 0) |
232 | return fd; | |
8a3dc488 | 233 | #ifndef _WIN32 |
bc5e487f | 234 | if (fstat (fd, file_stat) < 0) |
4262e0b3 | 235 | { |
bc5e487f KP |
236 | close (fd); |
237 | return -1; | |
4262e0b3 | 238 | } |
8a3dc488 | 239 | #endif |
bc5e487f | 240 | return fd; |
4262e0b3 PL |
241 | } |
242 | ||
594dcef0 | 243 | /* |
2d3387fd KP |
244 | * Look for a cache file for the specified dir. Attempt |
245 | * to use each one we find, stopping when the callback | |
246 | * indicates success | |
7410e40b | 247 | */ |
2d3387fd | 248 | static FcBool |
594dcef0 | 249 | FcDirCacheProcess (FcConfig *config, const FcChar8 *dir, |
d0471dd2 | 250 | FcBool (*callback) (FcConfig *config, int fd, struct stat *fd_stat, |
db6f19f1 | 251 | struct stat *dir_stat, void *closure), |
0a87ce71 | 252 | void *closure, FcChar8 **cache_file_ret) |
ea44e218 | 253 | { |
7ce19673 | 254 | int fd = -1; |
7410e40b PL |
255 | FcChar8 cache_base[CACHEBASE_LEN]; |
256 | FcStrList *list; | |
257 | FcChar8 *cache_dir; | |
d2f78684 | 258 | struct stat file_stat, dir_stat; |
2d3387fd | 259 | FcBool ret = FcFalse; |
d2f78684 | 260 | |
d0471dd2 | 261 | if (FcStat (config, dir, &dir_stat) < 0) |
2d3387fd | 262 | return FcFalse; |
df3efc11 | 263 | |
7410e40b | 264 | FcDirCacheBasename (dir, cache_base); |
ea44e218 | 265 | |
7410e40b PL |
266 | list = FcStrListCreate (config->cacheDirs); |
267 | if (!list) | |
2d3387fd | 268 | return FcFalse; |
7410e40b PL |
269 | |
270 | while ((cache_dir = FcStrListNext (list))) | |
ea44e218 | 271 | { |
7ce19673 | 272 | FcChar8 *cache_hashed = FcStrPlus (cache_dir, cache_base); |
7410e40b | 273 | if (!cache_hashed) |
d00c3cb5 | 274 | break; |
d0471dd2 | 275 | fd = FcDirCacheOpenFile (config, cache_hashed, &file_stat); |
7ce19673 | 276 | if (fd >= 0) { |
d0471dd2 | 277 | ret = (*callback) (config, fd, &file_stat, &dir_stat, closure); |
db6f19f1 KP |
278 | close (fd); |
279 | if (ret) | |
d2f78684 | 280 | { |
db6f19f1 KP |
281 | if (cache_file_ret) |
282 | *cache_file_ret = cache_hashed; | |
283 | else | |
284 | FcStrFree (cache_hashed); | |
285 | break; | |
d2f78684 | 286 | } |
d2f78684 | 287 | } |
0a87ce71 | 288 | FcStrFree (cache_hashed); |
7410e40b PL |
289 | } |
290 | FcStrListDone (list); | |
594dcef0 | 291 | |
2d3387fd | 292 | return ret; |
ea44e218 PL |
293 | } |
294 | ||
9e612141 KP |
295 | #define FC_CACHE_MIN_MMAP 1024 |
296 | ||
297 | /* | |
298 | * Skip list element, make sure the 'next' pointer is the last thing | |
299 | * in the structure, it will be allocated large enough to hold all | |
300 | * of the necessary pointers | |
301 | */ | |
302 | ||
303 | typedef struct _FcCacheSkip FcCacheSkip; | |
304 | ||
305 | struct _FcCacheSkip { | |
306 | FcCache *cache; | |
307 | int ref; | |
308 | intptr_t size; | |
309 | dev_t cache_dev; | |
310 | ino_t cache_ino; | |
311 | time_t cache_mtime; | |
312 | FcCacheSkip *next[1]; | |
313 | }; | |
314 | ||
315 | /* | |
316 | * The head of the skip list; pointers for every possible level | |
317 | * in the skip list, plus the largest level in the list | |
318 | */ | |
319 | ||
320 | #define FC_CACHE_MAX_LEVEL 16 | |
321 | ||
322 | static FcCacheSkip *fcCacheChains[FC_CACHE_MAX_LEVEL]; | |
323 | static int fcCacheMaxLevel; | |
324 | ||
cc104e6a KP |
325 | #if HAVE_RANDOM |
326 | # define FcRandom() random() | |
327 | #else | |
328 | # if HAVE_LRAND48 | |
329 | # define FcRandom() lrand48() | |
330 | # else | |
331 | # if HAVE_RAND | |
332 | # define FcRandom() rand() | |
333 | # endif | |
334 | # endif | |
335 | #endif | |
9e612141 KP |
336 | /* |
337 | * Generate a random level number, distributed | |
338 | * so that each level is 1/4 as likely as the one before | |
339 | * | |
340 | * Note that level numbers run 1 <= level <= MAX_LEVEL | |
341 | */ | |
342 | static int | |
343 | random_level (void) | |
344 | { | |
345 | /* tricky bit -- each bit is '1' 75% of the time */ | |
cc104e6a | 346 | long int bits = FcRandom () | FcRandom (); |
9e612141 KP |
347 | int level = 0; |
348 | ||
349 | while (++level < FC_CACHE_MAX_LEVEL) | |
350 | { | |
351 | if (bits & 1) | |
352 | break; | |
353 | bits >>= 1; | |
354 | } | |
355 | return level; | |
356 | } | |
357 | ||
358 | /* | |
359 | * Insert cache into the list | |
360 | */ | |
361 | static FcBool | |
362 | FcCacheInsert (FcCache *cache, struct stat *cache_stat) | |
363 | { | |
364 | FcCacheSkip **update[FC_CACHE_MAX_LEVEL]; | |
365 | FcCacheSkip *s, **next; | |
366 | int i, level; | |
367 | ||
368 | /* | |
369 | * Find links along each chain | |
370 | */ | |
371 | next = fcCacheChains; | |
372 | for (i = fcCacheMaxLevel; --i >= 0; ) | |
373 | { | |
374 | for (; (s = next[i]); next = s->next) | |
375 | if (s->cache > cache) | |
376 | break; | |
377 | update[i] = &next[i]; | |
378 | } | |
379 | ||
380 | /* | |
381 | * Create new list element | |
382 | */ | |
383 | level = random_level (); | |
384 | if (level > fcCacheMaxLevel) | |
385 | { | |
386 | level = fcCacheMaxLevel + 1; | |
387 | update[fcCacheMaxLevel] = &fcCacheChains[fcCacheMaxLevel]; | |
388 | fcCacheMaxLevel = level; | |
389 | } | |
594dcef0 | 390 | |
9e612141 KP |
391 | s = malloc (sizeof (FcCacheSkip) + (level - 1) * sizeof (FcCacheSkip *)); |
392 | if (!s) | |
393 | return FcFalse; | |
394 | ||
395 | s->cache = cache; | |
396 | s->size = cache->size; | |
397 | s->ref = 1; | |
49b44b27 KP |
398 | if (cache_stat) |
399 | { | |
400 | s->cache_dev = cache_stat->st_dev; | |
401 | s->cache_ino = cache_stat->st_ino; | |
402 | s->cache_mtime = cache_stat->st_mtime; | |
403 | } | |
404 | else | |
405 | { | |
406 | s->cache_dev = 0; | |
407 | s->cache_ino = 0; | |
408 | s->cache_mtime = 0; | |
409 | } | |
594dcef0 | 410 | |
9e612141 KP |
411 | /* |
412 | * Insert into all fcCacheChains | |
413 | */ | |
414 | for (i = 0; i < level; i++) | |
415 | { | |
416 | s->next[i] = *update[i]; | |
417 | *update[i] = s; | |
418 | } | |
419 | return FcTrue; | |
420 | } | |
421 | ||
422 | static FcCacheSkip * | |
423 | FcCacheFindByAddr (void *object) | |
424 | { | |
425 | int i; | |
426 | FcCacheSkip **next = fcCacheChains; | |
427 | FcCacheSkip *s; | |
428 | ||
429 | /* | |
430 | * Walk chain pointers one level at a time | |
431 | */ | |
432 | for (i = fcCacheMaxLevel; --i >= 0;) | |
433 | while (next[i] && (char *) object >= ((char *) next[i]->cache + next[i]->size)) | |
434 | next = next[i]->next; | |
435 | /* | |
436 | * Here we are | |
437 | */ | |
438 | s = next[0]; | |
439 | if (s && (char *) object < ((char *) s->cache + s->size)) | |
440 | return s; | |
441 | return NULL; | |
442 | } | |
443 | ||
444 | static void | |
445 | FcCacheRemove (FcCache *cache) | |
446 | { | |
447 | FcCacheSkip **update[FC_CACHE_MAX_LEVEL]; | |
448 | FcCacheSkip *s, **next; | |
449 | int i; | |
450 | ||
451 | /* | |
452 | * Find links along each chain | |
453 | */ | |
454 | next = fcCacheChains; | |
455 | for (i = fcCacheMaxLevel; --i >= 0; ) | |
456 | { | |
457 | for (; (s = next[i]); next = s->next) | |
458 | if (s->cache >= cache) | |
459 | break; | |
460 | update[i] = &next[i]; | |
461 | } | |
462 | s = next[0]; | |
9e612141 KP |
463 | for (i = 0; i < fcCacheMaxLevel && *update[i] == s; i++) |
464 | *update[i] = s->next[i]; | |
465 | while (fcCacheMaxLevel > 0 && fcCacheChains[fcCacheMaxLevel - 1] == NULL) | |
466 | fcCacheMaxLevel--; | |
467 | free (s); | |
468 | } | |
469 | ||
470 | static FcCache * | |
471 | FcCacheFindByStat (struct stat *cache_stat) | |
472 | { | |
473 | FcCacheSkip *s; | |
474 | ||
475 | for (s = fcCacheChains[0]; s; s = s->next[0]) | |
476 | if (s->cache_dev == cache_stat->st_dev && | |
477 | s->cache_ino == cache_stat->st_ino && | |
478 | s->cache_mtime == cache_stat->st_mtime) | |
323ecd0c KP |
479 | { |
480 | s->ref++; | |
9e612141 | 481 | return s->cache; |
323ecd0c | 482 | } |
9e612141 KP |
483 | return NULL; |
484 | } | |
485 | ||
8fe2104a KP |
486 | static void |
487 | FcDirCacheDispose (FcCache *cache) | |
488 | { | |
489 | switch (cache->magic) { | |
490 | case FC_CACHE_MAGIC_ALLOC: | |
491 | free (cache); | |
492 | break; | |
493 | case FC_CACHE_MAGIC_MMAP: | |
494 | #if defined(HAVE_MMAP) || defined(__CYGWIN__) | |
495 | munmap (cache, cache->size); | |
496 | #elif defined(_WIN32) | |
497 | UnmapViewOfFile (cache); | |
498 | #endif | |
499 | break; | |
500 | } | |
9e612141 | 501 | FcCacheRemove (cache); |
8fe2104a KP |
502 | } |
503 | ||
9e612141 KP |
504 | void |
505 | FcCacheObjectReference (void *object) | |
8fe2104a | 506 | { |
9e612141 | 507 | FcCacheSkip *skip = FcCacheFindByAddr (object); |
8fe2104a | 508 | |
9e612141 KP |
509 | if (skip) |
510 | skip->ref++; | |
8fe2104a KP |
511 | } |
512 | ||
9e612141 KP |
513 | void |
514 | FcCacheObjectDereference (void *object) | |
8fe2104a | 515 | { |
9e612141 | 516 | FcCacheSkip *skip = FcCacheFindByAddr (object); |
8fe2104a | 517 | |
9e612141 KP |
518 | if (skip) |
519 | { | |
520 | skip->ref--; | |
521 | if (skip->ref <= 0) | |
522 | FcDirCacheDispose (skip->cache); | |
523 | } | |
8fe2104a KP |
524 | } |
525 | ||
526 | void | |
527 | FcCacheFini (void) | |
528 | { | |
529 | int i; | |
8fe2104a | 530 | |
9e612141 KP |
531 | for (i = 0; i < FC_CACHE_MAX_LEVEL; i++) |
532 | assert (fcCacheChains[i] == NULL); | |
533 | assert (fcCacheMaxLevel == 0); | |
8fe2104a KP |
534 | } |
535 | ||
db6f19f1 | 536 | static FcBool |
d0471dd2 | 537 | FcCacheTimeValid (FcConfig *config, FcCache *cache, struct stat *dir_stat) |
db6f19f1 KP |
538 | { |
539 | struct stat dir_static; | |
540 | ||
541 | if (!dir_stat) | |
542 | { | |
d0471dd2 | 543 | if (FcStat (config, FcCacheDir (cache), &dir_static) < 0) |
db6f19f1 KP |
544 | return FcFalse; |
545 | dir_stat = &dir_static; | |
546 | } | |
547 | if (FcDebug () & FC_DBG_CACHE) | |
548 | printf ("FcCacheTimeValid dir \"%s\" cache time %d dir time %d\n", | |
549 | FcCacheDir (cache), cache->mtime, (int) dir_stat->st_mtime); | |
550 | return cache->mtime == (int) dir_stat->st_mtime; | |
551 | } | |
552 | ||
bc5e487f KP |
553 | /* |
554 | * Map a cache file into memory | |
555 | */ | |
556 | static FcCache * | |
d0471dd2 | 557 | FcDirCacheMapFd (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat) |
212c9f43 | 558 | { |
8fe2104a | 559 | FcCache *cache; |
7ce19673 | 560 | FcBool allocated = FcFalse; |
af180c40 | 561 | |
8fe2104a | 562 | if (fd_stat->st_size < sizeof (FcCache)) |
bc5e487f | 563 | return NULL; |
9e612141 | 564 | cache = FcCacheFindByStat (fd_stat); |
8fe2104a | 565 | if (cache) |
6bb5d72f | 566 | { |
d0471dd2 | 567 | if (FcCacheTimeValid (config, cache, dir_stat)) |
6bb5d72f BE |
568 | return cache; |
569 | FcDirCacheUnload (cache); | |
570 | cache = NULL; | |
571 | } | |
572 | ||
bc5e487f | 573 | /* |
144ca878 KP |
574 | * Lage cache files are mmap'ed, smaller cache files are read. This |
575 | * balances the system cost of mmap against per-process memory usage. | |
bc5e487f | 576 | */ |
8fe2104a | 577 | if (fd_stat->st_size >= FC_CACHE_MIN_MMAP) |
bc5e487f | 578 | { |
d6217cc6 | 579 | #if defined(HAVE_MMAP) || defined(__CYGWIN__) |
8fe2104a | 580 | cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0); |
1dd95fcb SB |
581 | if (cache == MAP_FAILED) |
582 | cache = NULL; | |
93f67dfc | 583 | #elif defined(_WIN32) |
7ce19673 | 584 | { |
bc5e487f KP |
585 | HANDLE hFileMap; |
586 | ||
587 | cache = NULL; | |
588 | hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL, | |
589 | PAGE_READONLY, 0, 0, NULL); | |
590 | if (hFileMap != NULL) | |
591 | { | |
594dcef0 | 592 | cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0, |
0596d729 | 593 | fd_stat->st_size); |
bc5e487f KP |
594 | CloseHandle (hFileMap); |
595 | } | |
93f67dfc | 596 | } |
7ce19673 | 597 | #endif |
bc5e487f | 598 | } |
7ce19673 KP |
599 | if (!cache) |
600 | { | |
8fe2104a | 601 | cache = malloc (fd_stat->st_size); |
7ce19673 | 602 | if (!cache) |
bc5e487f | 603 | return NULL; |
eb0cf671 | 604 | |
8fe2104a | 605 | if (read (fd, cache, fd_stat->st_size) != fd_stat->st_size) |
00f059e9 | 606 | { |
7ce19673 | 607 | free (cache); |
bc5e487f | 608 | return NULL; |
00f059e9 | 609 | } |
7ce19673 | 610 | allocated = FcTrue; |
594dcef0 BE |
611 | } |
612 | if (cache->magic != FC_CACHE_MAGIC_MMAP || | |
3b8a03c0 | 613 | cache->version < FC_CACHE_CONTENT_VERSION || |
8fe2104a | 614 | cache->size != fd_stat->st_size || |
d0471dd2 | 615 | !FcCacheTimeValid (config, cache, dir_stat) || |
bc5e8adb | 616 | !FcCacheInsert (cache, fd_stat)) |
7ce19673 KP |
617 | { |
618 | if (allocated) | |
619 | free (cache); | |
620 | else | |
621 | { | |
622 | #if defined(HAVE_MMAP) || defined(__CYGWIN__) | |
8fe2104a | 623 | munmap (cache, fd_stat->st_size); |
7ce19673 KP |
624 | #elif defined(_WIN32) |
625 | UnmapViewOfFile (cache); | |
00f059e9 | 626 | #endif |
7ce19673 | 627 | } |
bc5e487f | 628 | return NULL; |
00f059e9 | 629 | } |
af180c40 | 630 | |
7ce19673 KP |
631 | /* Mark allocated caches so they're freed rather than unmapped */ |
632 | if (allocated) | |
bc5e487f | 633 | cache->magic = FC_CACHE_MAGIC_ALLOC; |
7ce19673 | 634 | |
bc5e487f KP |
635 | return cache; |
636 | } | |
637 | ||
17389539 KP |
638 | void |
639 | FcDirCacheReference (FcCache *cache, int nref) | |
640 | { | |
641 | FcCacheSkip *skip = FcCacheFindByAddr (cache); | |
642 | ||
643 | if (skip) | |
644 | skip->ref += nref; | |
645 | } | |
646 | ||
bc5e487f KP |
647 | void |
648 | FcDirCacheUnload (FcCache *cache) | |
649 | { | |
9e612141 | 650 | FcCacheObjectDereference (cache); |
bc5e487f KP |
651 | } |
652 | ||
653 | static FcBool | |
d0471dd2 | 654 | FcDirCacheMapHelper (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure) |
bc5e487f | 655 | { |
d0471dd2 | 656 | FcCache *cache = FcDirCacheMapFd (config, fd, fd_stat, dir_stat); |
bc5e487f KP |
657 | |
658 | if (!cache) | |
659 | return FcFalse; | |
2d3387fd KP |
660 | *((FcCache **) closure) = cache; |
661 | return FcTrue; | |
662 | } | |
663 | ||
664 | FcCache * | |
bc5e487f | 665 | FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file) |
2d3387fd | 666 | { |
76abb77f | 667 | FcCache *cache = NULL; |
2d3387fd | 668 | |
76abb77f | 669 | if (!FcDirCacheProcess (config, dir, |
bc5e487f | 670 | FcDirCacheMapHelper, |
0a87ce71 | 671 | &cache, cache_file)) |
76abb77f KP |
672 | return NULL; |
673 | return cache; | |
af180c40 KP |
674 | } |
675 | ||
bc5e487f KP |
676 | FcCache * |
677 | FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat) | |
d0471dd2 MF |
678 | { |
679 | return FcDirCacheLoadFile2 (cache_file, FcConfigGetCurrent (), file_stat); | |
680 | } | |
681 | ||
682 | FcCache * | |
683 | FcDirCacheLoadFile2 (const FcChar8 *cache_file, FcConfig *config, struct stat *file_stat) | |
af180c40 | 684 | { |
bc5e487f KP |
685 | int fd; |
686 | FcCache *cache; | |
e37d10fa | 687 | struct stat my_file_stat; |
af180c40 | 688 | |
e37d10fa KP |
689 | if (!file_stat) |
690 | file_stat = &my_file_stat; | |
d0471dd2 | 691 | fd = FcDirCacheOpenFile (config, cache_file, file_stat); |
bc5e487f KP |
692 | if (fd < 0) |
693 | return NULL; | |
d0471dd2 | 694 | cache = FcDirCacheMapFd (config, fd, file_stat, NULL); |
bc5e487f KP |
695 | close (fd); |
696 | return cache; | |
eb0cf671 | 697 | } |
bc5e487f KP |
698 | |
699 | /* | |
700 | * Validate a cache file by reading the header and checking | |
701 | * the magic number and the size field | |
702 | */ | |
2d3387fd | 703 | static FcBool |
d0471dd2 | 704 | FcDirCacheValidateHelper (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure) |
2d3387fd KP |
705 | { |
706 | FcBool ret = FcTrue; | |
707 | FcCache c; | |
594dcef0 | 708 | |
2d3387fd KP |
709 | if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache)) |
710 | ret = FcFalse; | |
bc5e487f KP |
711 | else if (c.magic != FC_CACHE_MAGIC_MMAP) |
712 | ret = FcFalse; | |
3b8a03c0 | 713 | else if (c.version < FC_CACHE_CONTENT_VERSION) |
2d3387fd | 714 | ret = FcFalse; |
8fe2104a | 715 | else if (fd_stat->st_size != c.size) |
2d3387fd | 716 | ret = FcFalse; |
db6f19f1 KP |
717 | else if (c.mtime != (int) dir_stat->st_mtime) |
718 | ret = FcFalse; | |
2d3387fd KP |
719 | return ret; |
720 | } | |
721 | ||
f57783d2 KP |
722 | static FcBool |
723 | FcDirCacheValidConfig (const FcChar8 *dir, FcConfig *config) | |
2d3387fd | 724 | { |
594dcef0 | 725 | return FcDirCacheProcess (config, dir, |
bc5e487f KP |
726 | FcDirCacheValidateHelper, |
727 | NULL, NULL); | |
2d3387fd KP |
728 | } |
729 | ||
f57783d2 KP |
730 | FcBool |
731 | FcDirCacheValid (const FcChar8 *dir) | |
732 | { | |
733 | FcConfig *config; | |
594dcef0 | 734 | |
f57783d2 KP |
735 | config = FcConfigGetCurrent (); |
736 | if (!config) | |
737 | return FcFalse; | |
738 | ||
739 | return FcDirCacheValidConfig (dir, config); | |
740 | } | |
741 | ||
7ce19673 | 742 | /* |
bc5e487f | 743 | * Build a cache structure from the given contents |
7ce19673 | 744 | */ |
bc5e487f | 745 | FcCache * |
db6f19f1 | 746 | FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcStrSet *dirs) |
eb0cf671 | 747 | { |
7ce19673 KP |
748 | FcSerialize *serialize = FcSerializeCreate (); |
749 | FcCache *cache; | |
750 | int i; | |
7ce19673 KP |
751 | FcChar8 *dir_serialize; |
752 | intptr_t *dirs_serialize; | |
753 | FcFontSet *set_serialize; | |
594dcef0 | 754 | |
7ce19673 KP |
755 | if (!serialize) |
756 | return NULL; | |
757 | /* | |
758 | * Space for cache structure | |
759 | */ | |
621f3113 | 760 | FcSerializeReserve (serialize, sizeof (FcCache)); |
7ce19673 KP |
761 | /* |
762 | * Directory name | |
763 | */ | |
764 | if (!FcStrSerializeAlloc (serialize, dir)) | |
765 | goto bail1; | |
766 | /* | |
767 | * Subdirs | |
768 | */ | |
621f3113 | 769 | FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *)); |
7ce19673 KP |
770 | for (i = 0; i < dirs->num; i++) |
771 | if (!FcStrSerializeAlloc (serialize, dirs->strs[i])) | |
772 | goto bail1; | |
eb0cf671 | 773 | |
7ce19673 KP |
774 | /* |
775 | * Patterns | |
776 | */ | |
777 | if (!FcFontSetSerializeAlloc (serialize, set)) | |
778 | goto bail1; | |
594dcef0 | 779 | |
7ce19673 KP |
780 | /* Serialize layout complete. Now allocate space and fill it */ |
781 | cache = malloc (serialize->size); | |
782 | if (!cache) | |
783 | goto bail1; | |
784 | /* shut up valgrind */ | |
785 | memset (cache, 0, serialize->size); | |
d6217cc6 | 786 | |
7ce19673 | 787 | serialize->linear = cache; |
eb0cf671 | 788 | |
bc5e487f KP |
789 | cache->magic = FC_CACHE_MAGIC_ALLOC; |
790 | cache->version = FC_CACHE_CONTENT_VERSION; | |
7ce19673 | 791 | cache->size = serialize->size; |
db6f19f1 | 792 | cache->mtime = (int) dir_stat->st_mtime; |
eb0cf671 | 793 | |
7ce19673 KP |
794 | /* |
795 | * Serialize directory name | |
796 | */ | |
797 | dir_serialize = FcStrSerialize (serialize, dir); | |
798 | if (!dir_serialize) | |
799 | goto bail2; | |
800 | cache->dir = FcPtrToOffset (cache, dir_serialize); | |
594dcef0 | 801 | |
7ce19673 KP |
802 | /* |
803 | * Serialize sub dirs | |
804 | */ | |
805 | dirs_serialize = FcSerializePtr (serialize, dirs); | |
806 | if (!dirs_serialize) | |
807 | goto bail2; | |
808 | cache->dirs = FcPtrToOffset (cache, dirs_serialize); | |
809 | cache->dirs_count = dirs->num; | |
594dcef0 | 810 | for (i = 0; i < dirs->num; i++) |
8e351527 | 811 | { |
7ce19673 KP |
812 | FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]); |
813 | if (!d_serialize) | |
814 | goto bail2; | |
815 | dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize); | |
8e351527 | 816 | } |
594dcef0 | 817 | |
7ce19673 KP |
818 | /* |
819 | * Serialize font set | |
820 | */ | |
821 | set_serialize = FcFontSetSerialize (serialize, set); | |
822 | if (!set_serialize) | |
823 | goto bail2; | |
824 | cache->set = FcPtrToOffset (cache, set_serialize); | |
eb0cf671 | 825 | |
7ce19673 | 826 | FcSerializeDestroy (serialize); |
594dcef0 | 827 | |
49b44b27 KP |
828 | FcCacheInsert (cache, NULL); |
829 | ||
7ce19673 | 830 | return cache; |
212c9f43 | 831 | |
7ce19673 KP |
832 | bail2: |
833 | free (cache); | |
834 | bail1: | |
835 | FcSerializeDestroy (serialize); | |
836 | return NULL; | |
212c9f43 PL |
837 | } |
838 | ||
1de7a4cc HWN |
839 | |
840 | #ifdef _WIN32 | |
841 | #define mkdir(path,mode) _mkdir(path) | |
842 | #endif | |
843 | ||
7410e40b PL |
844 | static FcBool |
845 | FcMakeDirectory (const FcChar8 *dir) | |
846 | { | |
847 | FcChar8 *parent; | |
848 | FcBool ret; | |
594dcef0 | 849 | |
7410e40b PL |
850 | if (strlen ((char *) dir) == 0) |
851 | return FcFalse; | |
594dcef0 | 852 | |
7410e40b PL |
853 | parent = FcStrDirname (dir); |
854 | if (!parent) | |
855 | return FcFalse; | |
23848903 | 856 | if (access ((char *) parent, F_OK) == 0) |
8ae1e3d5 | 857 | ret = mkdir ((char *) dir, 0755) == 0 && chmod ((char *) dir, 0755) == 0; |
7410e40b | 858 | else if (access ((char *) parent, F_OK) == -1) |
8ae1e3d5 | 859 | ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0755) == 0) && chmod ((char *) dir, 0755) == 0; |
7410e40b PL |
860 | else |
861 | ret = FcFalse; | |
862 | FcStrFree (parent); | |
863 | return ret; | |
864 | } | |
865 | ||
212c9f43 PL |
866 | /* write serialized state to the cache file */ |
867 | FcBool | |
bc5e487f | 868 | FcDirCacheWrite (FcCache *cache, FcConfig *config) |
212c9f43 | 869 | { |
bc5e487f | 870 | FcChar8 *dir = FcCacheDir (cache); |
7410e40b PL |
871 | FcChar8 cache_base[CACHEBASE_LEN]; |
872 | FcChar8 *cache_hashed; | |
7ce19673 | 873 | int fd; |
1d879de2 | 874 | FcAtomic *atomic; |
00f059e9 | 875 | FcStrList *list; |
d2f78684 KP |
876 | FcChar8 *cache_dir = NULL; |
877 | FcChar8 *test_dir; | |
d0471dd2 MF |
878 | FcChar8 *full_test_dir; |
879 | FcBool free_test_dir; | |
b697fa25 BE |
880 | FcCacheSkip *skip; |
881 | struct stat cache_stat; | |
bc5e487f KP |
882 | int magic; |
883 | int written; | |
212c9f43 | 884 | |
7410e40b | 885 | /* |
d2f78684 | 886 | * Write it to the first directory in the list which is writable |
7410e40b | 887 | */ |
594dcef0 | 888 | |
d2f78684 KP |
889 | list = FcStrListCreate (config->cacheDirs); |
890 | if (!list) | |
891 | return FcFalse; | |
d0471dd2 | 892 | free_test_dir = FcFalse; |
d2f78684 | 893 | while ((test_dir = FcStrListNext (list))) { |
d0471dd2 MF |
894 | if (free_test_dir) |
895 | FcStrFree (full_test_dir); | |
896 | free_test_dir = FcFalse; | |
897 | ||
898 | full_test_dir = FcConfigGetRootPlus (config, test_dir); | |
899 | if (full_test_dir) | |
900 | { | |
901 | free_test_dir = FcTrue; | |
902 | test_dir = full_test_dir; | |
903 | } | |
904 | ||
8ae1e3d5 | 905 | if (access ((char *) test_dir, W_OK|X_OK) == 0) |
d2f78684 KP |
906 | { |
907 | cache_dir = test_dir; | |
908 | break; | |
909 | } | |
910 | else | |
911 | { | |
7410e40b PL |
912 | /* |
913 | * If the directory doesn't exist, try to create it | |
914 | */ | |
d2f78684 KP |
915 | if (access ((char *) test_dir, F_OK) == -1) { |
916 | if (FcMakeDirectory (test_dir)) | |
917 | { | |
918 | cache_dir = test_dir; | |
7410e40b | 919 | break; |
d2f78684 | 920 | } |
7410e40b | 921 | } |
8ae1e3d5 BE |
922 | /* |
923 | * Otherwise, try making it writable | |
924 | */ | |
925 | else if (chmod ((char *) test_dir, 0755) == 0) | |
926 | { | |
927 | cache_dir = test_dir; | |
928 | break; | |
929 | } | |
7410e40b | 930 | } |
7410e40b | 931 | } |
d2f78684 KP |
932 | FcStrListDone (list); |
933 | if (!cache_dir) | |
d0471dd2 | 934 | goto bail0; |
7ce19673 | 935 | |
d2f78684 KP |
936 | FcDirCacheBasename (dir, cache_base); |
937 | cache_hashed = FcStrPlus (cache_dir, cache_base); | |
938 | if (!cache_hashed) | |
d0471dd2 | 939 | goto bail0; |
ea44e218 | 940 | |
4262e0b3 | 941 | if (FcDebug () & FC_DBG_CACHE) |
c1c3ba06 KP |
942 | printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n", |
943 | dir, cache_hashed); | |
4262e0b3 | 944 | |
3bfae75d | 945 | atomic = FcAtomicCreate ((FcChar8 *)cache_hashed); |
1d879de2 | 946 | if (!atomic) |
bc5e487f | 947 | goto bail1; |
212c9f43 | 948 | |
d0471dd2 | 949 | if (!FcAtomicLock2 (config, atomic)) |
7ce19673 | 950 | goto bail3; |
ec760b17 | 951 | |
879af706 | 952 | fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666); |
1d879de2 | 953 | if (fd == -1) |
7ce19673 | 954 | goto bail4; |
594dcef0 | 955 | |
bc5e487f KP |
956 | /* Temporarily switch magic to MMAP while writing to file */ |
957 | magic = cache->magic; | |
958 | if (magic != FC_CACHE_MAGIC_MMAP) | |
959 | cache->magic = FC_CACHE_MAGIC_MMAP; | |
594dcef0 | 960 | |
bc5e487f KP |
961 | /* |
962 | * Write cache contents to file | |
963 | */ | |
964 | written = write (fd, cache, cache->size); | |
594dcef0 | 965 | |
bc5e487f KP |
966 | /* Switch magic back */ |
967 | if (magic != FC_CACHE_MAGIC_MMAP) | |
968 | cache->magic = magic; | |
594dcef0 | 969 | |
bc5e487f | 970 | if (written != cache->size) |
fff5a5af | 971 | { |
7ce19673 | 972 | perror ("write cache"); |
68355f38 PL |
973 | goto bail5; |
974 | } | |
4262e0b3 | 975 | |
212c9f43 | 976 | close(fd); |
1d879de2 | 977 | if (!FcAtomicReplaceOrig(atomic)) |
7ce19673 | 978 | goto bail4; |
b697fa25 BE |
979 | |
980 | /* If the file is small, update the cache chain entry such that the | |
981 | * new cache file is not read again. If it's large, we don't do that | |
982 | * such that we reload it, using mmap, which is shared across processes. | |
983 | */ | |
984 | if (cache->size < FC_CACHE_MIN_MMAP && | |
985 | (skip = FcCacheFindByAddr (cache)) && | |
d0471dd2 | 986 | FcStat (config, cache_hashed, &cache_stat)) |
b697fa25 BE |
987 | { |
988 | skip->cache_dev = cache_stat.st_dev; | |
989 | skip->cache_ino = cache_stat.st_ino; | |
990 | skip->cache_mtime = cache_stat.st_mtime; | |
991 | } | |
992 | ||
bc5e487f | 993 | FcStrFree (cache_hashed); |
1d879de2 PL |
994 | FcAtomicUnlock (atomic); |
995 | FcAtomicDestroy (atomic); | |
212c9f43 PL |
996 | return FcTrue; |
997 | ||
ea44e218 | 998 | bail5: |
1d879de2 | 999 | close (fd); |
7ce19673 | 1000 | bail4: |
1d879de2 | 1001 | FcAtomicUnlock (atomic); |
7ce19673 | 1002 | bail3: |
1d879de2 | 1003 | FcAtomicDestroy (atomic); |
ea44e218 | 1004 | bail1: |
bc5e487f | 1005 | FcStrFree (cache_hashed); |
d0471dd2 MF |
1006 | bail0: |
1007 | if (free_test_dir) | |
1008 | FcStrFree (full_test_dir); | |
4262e0b3 PL |
1009 | return FcFalse; |
1010 | } | |
1011 | ||
8ba2b3c5 MF |
1012 | static void |
1013 | align_fd (int fd, off_t *pos, int align) | |
1014 | { | |
1015 | int adj = *pos & (align - 1); | |
1016 | if (adj) | |
1017 | { | |
1018 | lseek (fd, adj, SEEK_CUR); | |
1019 | *pos += adj; | |
1020 | } | |
1021 | } | |
1022 | ||
1023 | static uint64_t | |
1024 | _conv_num (int src_fd, int dst_fd, off_t *src_pos, off_t *dst_pos, | |
1025 | FcBool src_be, FcBool dst_be, int src_psize, int dst_psize, | |
1026 | int src_read, int dst_write) | |
1027 | { | |
1028 | unsigned char src[8], dst[8], *s, *d; | |
1029 | int i, src_step, dst_step; | |
1030 | uint64_t ret; | |
1031 | ||
1032 | align_fd (src_fd, src_pos, src_read); | |
1033 | read (src_fd, src, src_read); | |
1034 | *src_pos += src_read; | |
1035 | ||
1036 | if (src_be) | |
1037 | s = &src[src_read - 1], src_step = -1; | |
1038 | else | |
1039 | s = src, src_step = 1; | |
1040 | if (dst_be) | |
1041 | d = &dst[dst_write - 1], dst_step = -1; | |
1042 | else | |
1043 | d = dst, dst_step = 1; | |
1044 | ||
1045 | ret = 0; | |
1046 | for (i = 0; i < src_read; ++i) | |
1047 | { | |
1048 | ret |= ((uint64_t) *s) << (8 * i); | |
1049 | *d = *s; | |
1050 | s += src_step; | |
1051 | d += dst_step; | |
1052 | } | |
1053 | for (; i < dst_write; ++i) | |
1054 | { | |
1055 | *d = 0; | |
1056 | d += dst_step; | |
1057 | } | |
1058 | ||
1059 | align_fd (dst_fd, dst_pos, dst_write); | |
1060 | write (dst_fd, dst, dst_write); | |
1061 | *dst_pos += dst_write; | |
1062 | ||
1063 | return ret; | |
1064 | } | |
1065 | #define conv_num(r, w) _conv_num (src_fd, dst_fd, &src_pos, &dst_pos, src_be, dst_be, src_psize, dst_psize, r, w) | |
1066 | #define conv_ptr() conv_num (src_psize, dst_psize) | |
1067 | #define conv_int() conv_num (4, 4) | |
1068 | ||
1069 | FcBool | |
1070 | FcDirCacheConvert (const FcChar8 *src_cache, const FcChar8 *src_type, | |
1071 | const FcChar8 *dst_cache, const FcChar8 *dst_type) | |
1072 | { | |
1073 | /* See fcarch.h for the spec used here */ | |
1074 | FcCache c; | |
1075 | FcBool src_be; | |
1076 | FcBool dst_be; | |
1077 | int src_psize; | |
1078 | int dst_psize; | |
1079 | int src_dalign; | |
1080 | int dst_dalign; | |
1081 | int src_fd; | |
1082 | int dst_fd; | |
1083 | off_t src_pos; | |
1084 | off_t dst_pos; | |
1085 | struct stat src_st; | |
1086 | ||
1087 | src_be = !strncmp ((const char *) src_type, "be", 2); | |
1088 | dst_be = !strncmp ((const char *) dst_type, "be", 2); | |
1089 | ||
1090 | src_psize = !strncmp ((const char *) src_type + 2, "32", 2) ? 4 : 8; | |
1091 | dst_psize = !strncmp ((const char *) dst_type + 2, "32", 2) ? 4 : 8; | |
1092 | ||
1093 | if (src_psize == 4) | |
1094 | src_dalign = !strncmp ((const char *) src_type + 4, "d4", 2) ? 4 : 8; | |
1095 | else | |
1096 | src_dalign = src_psize; | |
1097 | ||
1098 | if (dst_psize == 4) | |
1099 | dst_dalign = !strncmp ((const char *) dst_type + 4, "d4", 2) ? 4 : 8; | |
1100 | else | |
1101 | dst_dalign = dst_psize; | |
1102 | ||
1103 | src_fd = open ((const char *) src_cache, O_RDONLY); | |
1104 | if (src_fd == -1) | |
1105 | return FcFalse; | |
1106 | dst_fd = open ((const char *) dst_cache, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666); | |
1107 | if (dst_fd == -1) | |
1108 | { | |
1109 | close (src_fd); | |
1110 | return FcFalse; | |
1111 | } | |
1112 | src_pos = dst_pos = 0; | |
1113 | ||
1114 | /* Process FcCache at start of file */ | |
1115 | c.magic = conv_int (); | |
1116 | c.version = conv_int (); | |
1117 | c.size = conv_ptr (); | |
1118 | c.dir = conv_ptr (); | |
1119 | c.dirs = conv_ptr (); | |
1120 | c.dirs_count = conv_int (); | |
1121 | c.set = conv_ptr (); | |
1122 | c.mtime = conv_int (); | |
1123 | align_fd (dst_fd, &dst_pos, dst_psize); | |
1124 | ||
1125 | return FcFalse; | |
1126 | } | |
1127 | ||
4984242e KP |
1128 | /* |
1129 | * Hokey little macro trick to permit the definitions of C functions | |
1130 | * with the same name as CPP macros | |
1131 | */ | |
c9c68750 KJ |
1132 | #define args1(x) (x) |
1133 | #define args2(x,y) (x,y) | |
4984242e KP |
1134 | |
1135 | const FcChar8 * | |
c9c68750 | 1136 | FcCacheDir args1(const FcCache *c) |
4984242e KP |
1137 | { |
1138 | return FcCacheDir (c); | |
1139 | } | |
1140 | ||
1141 | FcFontSet * | |
c9c68750 | 1142 | FcCacheCopySet args1(const FcCache *c) |
4984242e KP |
1143 | { |
1144 | FcFontSet *old = FcCacheSet (c); | |
1145 | FcFontSet *new = FcFontSetCreate (); | |
1146 | int i; | |
594dcef0 | 1147 | |
4984242e KP |
1148 | if (!new) |
1149 | return NULL; | |
1150 | for (i = 0; i < old->nfont; i++) | |
8d779ce4 KP |
1151 | { |
1152 | FcPattern *font = FcFontSetFont (old, i); | |
1153 | ||
1154 | FcPatternReference (font); | |
1155 | if (!FcFontSetAdd (new, font)) | |
4984242e KP |
1156 | { |
1157 | FcFontSetDestroy (new); | |
1158 | return NULL; | |
1159 | } | |
8d779ce4 | 1160 | } |
4984242e KP |
1161 | return new; |
1162 | } | |
1163 | ||
1164 | const FcChar8 * | |
c9c68750 | 1165 | FcCacheSubdir args2(const FcCache *c, int i) |
4984242e KP |
1166 | { |
1167 | return FcCacheSubdir (c, i); | |
1168 | } | |
1169 | ||
1170 | int | |
c9c68750 | 1171 | FcCacheNumSubdir args1(const FcCache *c) |
4984242e KP |
1172 | { |
1173 | return c->dirs_count; | |
1174 | } | |
1175 | ||
1176 | int | |
c9c68750 | 1177 | FcCacheNumFont args1(const FcCache *c) |
4984242e KP |
1178 | { |
1179 | return FcCacheSet(c)->nfont; | |
1180 | } | |
1181 | ||
ea44e218 PL |
1182 | /* |
1183 | * This code implements the MD5 message-digest algorithm. | |
1184 | * The algorithm is due to Ron Rivest. This code was | |
1185 | * written by Colin Plumb in 1993, no copyright is claimed. | |
1186 | * This code is in the public domain; do with it what you wish. | |
1187 | * | |
1188 | * Equivalent code is available from RSA Data Security, Inc. | |
1189 | * This code has been tested against that, and is equivalent, | |
1190 | * except that you don't need to include two pages of legalese | |
1191 | * with every copy. | |
1192 | * | |
1193 | * To compute the message digest of a chunk of bytes, declare an | |
1194 | * MD5Context structure, pass it to MD5Init, call MD5Update as | |
1195 | * needed on buffers full of bytes, and then call MD5Final, which | |
1196 | * will fill a supplied 16-byte array with the digest. | |
1197 | */ | |
1198 | ||
1199 | #ifndef HIGHFIRST | |
1200 | #define byteReverse(buf, len) /* Nothing */ | |
1201 | #else | |
1202 | /* | |
1203 | * Note: this code is harmless on little-endian machines. | |
1204 | */ | |
1205 | void byteReverse(unsigned char *buf, unsigned longs) | |
1206 | { | |
1207 | FcChar32 t; | |
1208 | do { | |
1209 | t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 | | |
1210 | ((unsigned) buf[1] << 8 | buf[0]); | |
1211 | *(FcChar32 *) buf = t; | |
1212 | buf += 4; | |
1213 | } while (--longs); | |
1214 | } | |
1215 | #endif | |
1216 | ||
1217 | /* | |
1218 | * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious | |
1219 | * initialization constants. | |
1220 | */ | |
1221 | static void MD5Init(struct MD5Context *ctx) | |
1222 | { | |
1223 | ctx->buf[0] = 0x67452301; | |
1224 | ctx->buf[1] = 0xefcdab89; | |
1225 | ctx->buf[2] = 0x98badcfe; | |
1226 | ctx->buf[3] = 0x10325476; | |
1227 | ||
1228 | ctx->bits[0] = 0; | |
1229 | ctx->bits[1] = 0; | |
1230 | } | |
1231 | ||
1232 | /* | |
1233 | * Update context to reflect the concatenation of another buffer full | |
1234 | * of bytes. | |
1235 | */ | |
db6f19f1 | 1236 | static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len) |
ea44e218 PL |
1237 | { |
1238 | FcChar32 t; | |
1239 | ||
1240 | /* Update bitcount */ | |
1241 | ||
1242 | t = ctx->bits[0]; | |
1243 | if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t) | |
1244 | ctx->bits[1]++; /* Carry from low to high */ | |
1245 | ctx->bits[1] += len >> 29; | |
1246 | ||
1247 | t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ | |
1248 | ||
1249 | /* Handle any leading odd-sized chunks */ | |
1250 | ||
1251 | if (t) { | |
1252 | unsigned char *p = (unsigned char *) ctx->in + t; | |
1253 | ||
1254 | t = 64 - t; | |
1255 | if (len < t) { | |
1256 | memcpy(p, buf, len); | |
1257 | return; | |
1258 | } | |
1259 | memcpy(p, buf, t); | |
1260 | byteReverse(ctx->in, 16); | |
1261 | MD5Transform(ctx->buf, (FcChar32 *) ctx->in); | |
1262 | buf += t; | |
1263 | len -= t; | |
1264 | } | |
1265 | /* Process data in 64-byte chunks */ | |
1266 | ||
1267 | while (len >= 64) { | |
1268 | memcpy(ctx->in, buf, 64); | |
1269 | byteReverse(ctx->in, 16); | |
1270 | MD5Transform(ctx->buf, (FcChar32 *) ctx->in); | |
1271 | buf += 64; | |
1272 | len -= 64; | |
1273 | } | |
1274 | ||
1275 | /* Handle any remaining bytes of data. */ | |
1276 | ||
1277 | memcpy(ctx->in, buf, len); | |
1278 | } | |
1279 | ||
1280 | /* | |
594dcef0 | 1281 | * Final wrapup - pad to 64-byte boundary with the bit pattern |
ea44e218 PL |
1282 | * 1 0* (64-bit count of bits processed, MSB-first) |
1283 | */ | |
1284 | static void MD5Final(unsigned char digest[16], struct MD5Context *ctx) | |
1285 | { | |
1286 | unsigned count; | |
1287 | unsigned char *p; | |
1288 | ||
1289 | /* Compute number of bytes mod 64 */ | |
1290 | count = (ctx->bits[0] >> 3) & 0x3F; | |
1291 | ||
1292 | /* Set the first char of padding to 0x80. This is safe since there is | |
1293 | always at least one byte free */ | |
1294 | p = ctx->in + count; | |
1295 | *p++ = 0x80; | |
1296 | ||
1297 | /* Bytes of padding needed to make 64 bytes */ | |
1298 | count = 64 - 1 - count; | |
1299 | ||
1300 | /* Pad out to 56 mod 64 */ | |
1301 | if (count < 8) { | |
1302 | /* Two lots of padding: Pad the first block to 64 bytes */ | |
1303 | memset(p, 0, count); | |
1304 | byteReverse(ctx->in, 16); | |
1305 | MD5Transform(ctx->buf, (FcChar32 *) ctx->in); | |
1306 | ||
1307 | /* Now fill the next block with 56 bytes */ | |
1308 | memset(ctx->in, 0, 56); | |
1309 | } else { | |
1310 | /* Pad block to 56 bytes */ | |
1311 | memset(p, 0, count - 8); | |
1312 | } | |
1313 | byteReverse(ctx->in, 14); | |
1314 | ||
1315 | /* Append length in bits and transform */ | |
1316 | ((FcChar32 *) ctx->in)[14] = ctx->bits[0]; | |
1317 | ((FcChar32 *) ctx->in)[15] = ctx->bits[1]; | |
1318 | ||
1319 | MD5Transform(ctx->buf, (FcChar32 *) ctx->in); | |
1320 | byteReverse((unsigned char *) ctx->buf, 4); | |
1321 | memcpy(digest, ctx->buf, 16); | |
082caefb | 1322 | memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */ |
ea44e218 PL |
1323 | } |
1324 | ||
1325 | ||
1326 | /* The four core functions - F1 is optimized somewhat */ | |
1327 | ||
1328 | /* #define F1(x, y, z) (x & y | ~x & z) */ | |
1329 | #define F1(x, y, z) (z ^ (x & (y ^ z))) | |
1330 | #define F2(x, y, z) F1(z, x, y) | |
1331 | #define F3(x, y, z) (x ^ y ^ z) | |
1332 | #define F4(x, y, z) (y ^ (x | ~z)) | |
1333 | ||
1334 | /* This is the central step in the MD5 algorithm. */ | |
1335 | #define MD5STEP(f, w, x, y, z, data, s) \ | |
1336 | ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) | |
1337 | ||
1338 | /* | |
1339 | * The core of the MD5 algorithm, this alters an existing MD5 hash to | |
1340 | * reflect the addition of 16 longwords of new data. MD5Update blocks | |
1341 | * the data and converts bytes into longwords for this routine. | |
1342 | */ | |
1343 | static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]) | |
1344 | { | |
1345 | register FcChar32 a, b, c, d; | |
1346 | ||
1347 | a = buf[0]; | |
1348 | b = buf[1]; | |
1349 | c = buf[2]; | |
1350 | d = buf[3]; | |
1351 | ||
1352 | MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); | |
1353 | MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); | |
1354 | MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); | |
1355 | MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); | |
1356 | MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); | |
1357 | MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); | |
1358 | MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); | |
1359 | MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); | |
1360 | MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); | |
1361 | MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); | |
1362 | MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); | |
1363 | MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); | |
1364 | MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); | |
1365 | MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); | |
1366 | MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); | |
1367 | MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); | |
1368 | ||
1369 | MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); | |
1370 | MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); | |
1371 | MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); | |
1372 | MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); | |
1373 | MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); | |
1374 | MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); | |
1375 | MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); | |
1376 | MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); | |
1377 | MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); | |
1378 | MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); | |
1379 | MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); | |
1380 | MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); | |
1381 | MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); | |
1382 | MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); | |
1383 | MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); | |
1384 | MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); | |
1385 | ||
1386 | MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); | |
1387 | MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); | |
1388 | MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); | |
1389 | MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); | |
1390 | MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); | |
1391 | MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); | |
1392 | MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); | |
1393 | MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); | |
1394 | MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); | |
1395 | MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); | |
1396 | MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); | |
1397 | MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); | |
1398 | MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); | |
1399 | MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); | |
1400 | MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); | |
1401 | MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); | |
1402 | ||
1403 | MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); | |
1404 | MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); | |
1405 | MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); | |
1406 | MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); | |
1407 | MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); | |
1408 | MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); | |
1409 | MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); | |
1410 | MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); | |
1411 | MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); | |
1412 | MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); | |
1413 | MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); | |
1414 | MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); | |
1415 | MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); | |
1416 | MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); | |
1417 | MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); | |
1418 | MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); | |
1419 | ||
1420 | buf[0] += a; | |
1421 | buf[1] += b; | |
1422 | buf[2] += c; | |
1423 | buf[3] += d; | |
1424 | } | |
23816bf9 KP |
1425 | #define __fccache__ |
1426 | #include "fcaliastail.h" | |
1427 | #undef __fccache__ |