]>
Commit | Line | Data |
---|---|---|
24330d27 | 1 | /* |
4bd4418a | 2 | * $RCSId: xc/lib/fontconfig/src/fccache.c,v 1.12 2002/08/22 07:36:44 keithp Exp $ |
24330d27 | 3 | * |
46b51147 | 4 | * Copyright © 2000 Keith Packard |
03a212e5 | 5 | * Copyright © 2005 Patrick Lam |
24330d27 KP |
6 | * |
7 | * Permission to use, copy, modify, distribute, and sell this software and its | |
8 | * documentation for any purpose is hereby granted without fee, provided that | |
9 | * the above copyright notice appear in all copies and that both that | |
10 | * copyright notice and this permission notice appear in supporting | |
11 | * documentation, and that the name of Keith Packard not be used in | |
12 | * advertising or publicity pertaining to distribution of the software without | |
13 | * specific, written prior permission. Keith Packard makes no | |
14 | * representations about the suitability of this software for any purpose. It | |
15 | * is provided "as is" without express or implied warranty. | |
16 | * | |
17 | * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, | |
18 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO | |
19 | * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR | |
20 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, | |
21 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER | |
22 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | |
23 | * PERFORMANCE OF THIS SOFTWARE. | |
24 | */ | |
25 | ||
f045376c | 26 | #include "fcint.h" |
212c9f43 | 27 | #include <fcntl.h> |
4262e0b3 | 28 | #include <dirent.h> |
ea44e218 | 29 | #include <string.h> |
fd77c154 | 30 | #include <sys/types.h> |
d6217cc6 | 31 | #if defined(HAVE_MMAP) || defined(__CYGWIN__) |
93f67dfc | 32 | # include <unistd.h> |
d6217cc6 | 33 | # include <sys/mman.h> |
93f67dfc PL |
34 | #elif defined(_WIN32) |
35 | # include <windows.h> | |
d6217cc6 | 36 | #endif |
24330d27 | 37 | |
2dbe7597 | 38 | #define ENDIAN_TEST 0x12345678 |
f2fb985c | 39 | #define MACHINE_SIGNATURE_SIZE (9 + 5*20 + 1) |
d6217cc6 PL |
40 | /* for when we don't have sysconf: */ |
41 | #define FC_HARDCODED_PAGESIZE 8192 | |
2dbe7597 | 42 | |
879af706 PL |
43 | #ifndef O_BINARY |
44 | #define O_BINARY 0 | |
45 | #endif | |
46 | ||
ea44e218 | 47 | static int |
7410e40b | 48 | FcDirCacheOpen (FcConfig *config, const FcChar8 *dir, FcChar8 **cache_path); |
ea44e218 | 49 | |
eb0cf671 | 50 | static off_t |
c60ec7cc | 51 | FcCacheSkipToArch (int fd, const char * arch); |
1b7be377 | 52 | |
eb0cf671 | 53 | static FcBool |
1d879de2 | 54 | FcCacheCopyOld (int fd, int fd_orig, off_t start); |
1b7be377 | 55 | |
eb0cf671 PL |
56 | static void * |
57 | FcDirCacheProduce (FcFontSet *set, FcCache * metadata); | |
1b7be377 PL |
58 | |
59 | static FcBool | |
cd9bca69 | 60 | FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config); |
1b7be377 | 61 | |
eb0cf671 PL |
62 | static int |
63 | FcCacheNextOffset(off_t w); | |
1b7be377 | 64 | |
eb0cf671 PL |
65 | static char * |
66 | FcCacheMachineSignature (void); | |
1b7be377 PL |
67 | |
68 | static FcBool | |
eb0cf671 | 69 | FcCacheHaveBank (int bank); |
1b7be377 | 70 | |
e77c1718 PL |
71 | static void |
72 | FcCacheAddBankDir (int bank, const char * dir); | |
73 | ||
ea44e218 PL |
74 | struct MD5Context { |
75 | FcChar32 buf[4]; | |
76 | FcChar32 bits[2]; | |
77 | unsigned char in[64]; | |
78 | }; | |
79 | ||
80 | static void MD5Init(struct MD5Context *ctx); | |
81 | static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len); | |
82 | static void MD5Final(unsigned char digest[16], struct MD5Context *ctx); | |
83 | static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]); | |
84 | ||
eb0cf671 | 85 | #define FC_DBG_CACHE_REF 1024 |
1b7be377 | 86 | |
8245771d PL |
87 | static char * |
88 | FcCacheReadString (int fd, char *dest, int len) | |
24330d27 | 89 | { |
799157db PL |
90 | int size; |
91 | int slen; | |
24330d27 | 92 | |
24330d27 | 93 | if (len == 0) |
03a212e5 | 94 | return 0; |
799157db PL |
95 | |
96 | size = read (fd, dest, len-1); | |
97 | ||
98 | if (size > 0) | |
24330d27 | 99 | { |
799157db PL |
100 | dest[size] = '\0'; |
101 | slen = strlen (dest); | |
102 | ||
103 | lseek (fd, slen - size + 1, SEEK_CUR); | |
104 | return slen < len ? dest : 0; | |
24330d27 | 105 | } |
799157db | 106 | |
ccb3e93b | 107 | return 0; |
24330d27 KP |
108 | } |
109 | ||
ea44e218 PL |
110 | static void |
111 | FcCacheSkipString (int fd) | |
112 | { | |
799157db PL |
113 | char buf[256]; |
114 | int size; | |
115 | int slen; | |
ea44e218 | 116 | |
799157db | 117 | while ( (size = read (fd, buf, sizeof (buf)-1)) > 0) |
ea44e218 | 118 | { |
799157db PL |
119 | buf [size] = '\0'; |
120 | slen = strlen (buf); | |
121 | if (slen < size) | |
122 | { | |
123 | lseek (fd, slen - size + 1, SEEK_CUR); | |
124 | return; | |
125 | } | |
ea44e218 | 126 | } |
ea44e218 PL |
127 | } |
128 | ||
24330d27 | 129 | static FcBool |
8245771d | 130 | FcCacheWriteString (int fd, const char *chars) |
327a7fd4 | 131 | { |
212c9f43 | 132 | if (write (fd, chars, strlen(chars)+1) != strlen(chars)+1) |
327a7fd4 | 133 | return FcFalse; |
327a7fd4 KP |
134 | return FcTrue; |
135 | } | |
136 | ||
eb0cf671 PL |
137 | static void |
138 | FcGlobalCacheDirDestroy (FcGlobalCacheDir *d) | |
1b7be377 | 139 | { |
971cf180 | 140 | FcStrSetDestroy (d->subdirs); |
5e678e94 PL |
141 | FcMemFree (FC_MEM_STRING, strlen (d->name)+1); |
142 | free (d->name); | |
143 | FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir)); | |
144 | free (d); | |
1b7be377 PL |
145 | } |
146 | ||
eb0cf671 PL |
147 | FcGlobalCache * |
148 | FcGlobalCacheCreate (void) | |
1b7be377 | 149 | { |
eb0cf671 | 150 | FcGlobalCache *cache; |
1b7be377 | 151 | |
eb0cf671 PL |
152 | cache = malloc (sizeof (FcGlobalCache)); |
153 | if (!cache) | |
154 | return 0; | |
155 | FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCache)); | |
156 | cache->dirs = 0; | |
157 | cache->updated = FcFalse; | |
158 | cache->fd = -1; | |
159 | return cache; | |
1b7be377 PL |
160 | } |
161 | ||
eb0cf671 PL |
162 | void |
163 | FcGlobalCacheDestroy (FcGlobalCache *cache) | |
327a7fd4 | 164 | { |
eb0cf671 | 165 | FcGlobalCacheDir *d, *next; |
327a7fd4 | 166 | |
eb0cf671 | 167 | for (d = cache->dirs; d; d = next) |
327a7fd4 | 168 | { |
eb0cf671 PL |
169 | next = d->next; |
170 | FcGlobalCacheDirDestroy (d); | |
327a7fd4 | 171 | } |
eb0cf671 | 172 | FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache)); |
62a4a845 PL |
173 | if (cache->fd != -1) |
174 | close (cache->fd); | |
eb0cf671 | 175 | free (cache); |
327a7fd4 KP |
176 | } |
177 | ||
178 | void | |
eb0cf671 | 179 | FcGlobalCacheLoad (FcGlobalCache *cache, |
03a212e5 | 180 | FcStrSet *staleDirs, |
2b25f00c PL |
181 | const FcChar8 *cache_file, |
182 | FcConfig *config) | |
327a7fd4 | 183 | { |
ea44e218 | 184 | char name_buf[FC_MAX_FILE_LEN]; |
eb0cf671 | 185 | FcGlobalCacheDir *d, *next; |
2b25f00c | 186 | FcFileTime config_time = FcConfigModifiedTime (config); |
eb0cf671 PL |
187 | char * current_arch_machine_name; |
188 | char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9]; | |
189 | off_t current_arch_start; | |
327a7fd4 | 190 | |
03a212e5 | 191 | struct stat cache_stat, dir_stat; |
971cf180 | 192 | char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1]; |
03a212e5 PL |
193 | |
194 | if (stat ((char *) cache_file, &cache_stat) < 0) | |
195 | return; | |
196 | ||
879af706 | 197 | cache->fd = open ((char *) cache_file, O_RDONLY | O_BINARY); |
eb0cf671 PL |
198 | if (cache->fd == -1) |
199 | return; | |
327a7fd4 | 200 | |
eb0cf671 | 201 | cache->updated = FcFalse; |
327a7fd4 | 202 | |
c5411c4c | 203 | if (!FcCacheReadString (cache->fd, name_buf, sizeof (name_buf))) |
28aefd01 | 204 | goto bail_and_destroy; |
c60ec7cc | 205 | if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) != 0) |
28aefd01 | 206 | goto bail_and_destroy; |
c60ec7cc | 207 | |
eb0cf671 PL |
208 | current_arch_machine_name = FcCacheMachineSignature (); |
209 | current_arch_start = FcCacheSkipToArch(cache->fd, | |
c60ec7cc | 210 | current_arch_machine_name); |
eb0cf671 | 211 | if (current_arch_start < 0) |
b17cf498 | 212 | goto bail1; |
327a7fd4 | 213 | |
eb0cf671 | 214 | lseek (cache->fd, current_arch_start, SEEK_SET); |
c5411c4c PL |
215 | if (!FcCacheReadString (cache->fd, candidate_arch_machine_name, |
216 | sizeof (candidate_arch_machine_name))) | |
217 | goto bail_and_destroy; | |
03a212e5 | 218 | if (strlen(candidate_arch_machine_name) == 0) |
e58b50e8 | 219 | goto bail_and_destroy; |
1b7be377 | 220 | |
eb0cf671 | 221 | while (1) |
1b7be377 | 222 | { |
8cb4c56d PL |
223 | off_t targ; |
224 | ||
2c4e0124 PL |
225 | if (!FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)) || |
226 | !strlen(name_buf)) | |
1b7be377 | 227 | break; |
1b7be377 | 228 | |
9090cb9e PL |
229 | /* Directory must be older than the global cache file; also |
230 | cache must be newer than the config file. */ | |
03a212e5 | 231 | if (stat ((char *) name_buf, &dir_stat) < 0 || |
2b25f00c | 232 | dir_stat.st_mtime > cache_stat.st_mtime || |
9090cb9e | 233 | (config_time.set && cache_stat.st_mtime < config_time.time)) |
03a212e5 PL |
234 | { |
235 | FcCache md; | |
68355f38 | 236 | off_t off; |
03a212e5 | 237 | |
ae2aafe6 | 238 | FcStrSetAdd (staleDirs, (FcChar8 *)name_buf); |
fff5a5af PL |
239 | |
240 | /* skip subdirs */ | |
241 | while (FcCacheReadString (cache->fd, subdirName, | |
242 | sizeof (subdirName)) && | |
243 | strlen (subdirName)) | |
244 | ; | |
245 | ||
68355f38 PL |
246 | if (read (cache->fd, &md, sizeof (FcCache)) != sizeof(FcCache)) |
247 | { | |
248 | perror ("read metadata"); | |
249 | goto bail1; | |
250 | } | |
251 | off = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count; | |
fff5a5af PL |
252 | if (lseek (cache->fd, off, SEEK_SET) != off) |
253 | { | |
68355f38 PL |
254 | perror ("lseek"); |
255 | goto bail1; | |
256 | } | |
257 | continue; | |
03a212e5 PL |
258 | } |
259 | ||
eb0cf671 PL |
260 | d = malloc (sizeof (FcGlobalCacheDir)); |
261 | if (!d) | |
262 | goto bail1; | |
1b7be377 | 263 | |
eb0cf671 PL |
264 | d->next = cache->dirs; |
265 | cache->dirs = d; | |
1b7be377 | 266 | |
8245771d | 267 | d->name = (char *)FcStrCopy ((FcChar8 *)name_buf); |
eb0cf671 | 268 | d->ent = 0; |
fff5a5af | 269 | d->state = FcGCDirFileRead; |
971cf180 PL |
270 | |
271 | d->subdirs = FcStrSetCreate(); | |
2c4e0124 PL |
272 | do |
273 | { | |
274 | if (!FcCacheReadString (cache->fd, subdirName, | |
275 | sizeof (subdirName)) || | |
276 | !strlen (subdirName)) | |
277 | break; | |
971cf180 | 278 | FcStrSetAdd (d->subdirs, (FcChar8 *)subdirName); |
2c4e0124 | 279 | } while (1); |
971cf180 | 280 | |
81d97094 | 281 | d->offset = lseek (cache->fd, 0, SEEK_CUR); |
8cb4c56d PL |
282 | if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache)) |
283 | goto bail1; | |
284 | targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count; | |
285 | if (lseek (cache->fd, targ, SEEK_SET) != targ) | |
286 | goto bail1; | |
1b7be377 | 287 | } |
eb0cf671 | 288 | return; |
1b7be377 | 289 | |
eb0cf671 PL |
290 | bail1: |
291 | for (d = cache->dirs; d; d = next) | |
1b7be377 | 292 | { |
eb0cf671 PL |
293 | next = d->next; |
294 | free (d); | |
1b7be377 | 295 | } |
eb0cf671 | 296 | cache->dirs = 0; |
e58b50e8 PL |
297 | |
298 | close (cache->fd); | |
299 | cache->fd = -1; | |
300 | return; | |
301 | ||
302 | bail_and_destroy: | |
eb0cf671 PL |
303 | close (cache->fd); |
304 | cache->fd = -1; | |
e58b50e8 PL |
305 | |
306 | if (stat ((char *) cache_file, &cache_stat) == 0) | |
307 | unlink ((char *)cache_file); | |
308 | ||
eb0cf671 | 309 | return; |
e58b50e8 | 310 | |
1b7be377 PL |
311 | } |
312 | ||
1b7be377 | 313 | FcBool |
8245771d | 314 | FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config) |
1b7be377 | 315 | { |
3616cbe2 | 316 | FcGlobalCacheDir *d; |
3616cbe2 | 317 | int i; |
1b7be377 | 318 | |
eb0cf671 | 319 | if (cache->fd == -1) |
1b7be377 | 320 | return FcFalse; |
1b7be377 | 321 | |
5c90509c PL |
322 | if (config) |
323 | if (!(dir = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)dir))) | |
324 | return FcFalse; /* non-existing directory */ | |
4073203d | 325 | |
eb0cf671 | 326 | for (d = cache->dirs; d; d = d->next) |
1b7be377 | 327 | { |
fff5a5af | 328 | if (strcmp (d->name, dir) == 0) |
1b7be377 | 329 | { |
fff5a5af | 330 | if (d->state == FcGCDirDisabled) |
eb0cf671 | 331 | return FcFalse; |
3616cbe2 | 332 | |
fff5a5af PL |
333 | if (d->state == FcGCDirFileRead) |
334 | { | |
335 | lseek (cache->fd, d->offset, SEEK_SET); | |
336 | if (!FcDirCacheConsume (cache->fd, d->name, set, config)) | |
337 | return FcFalse; | |
338 | ||
3616cbe2 PL |
339 | for (i = 0; i < d->subdirs->num; i++) |
340 | FcStrSetAdd (dirs, (FcChar8 *)d->subdirs->strs[i]); | |
341 | ||
fff5a5af PL |
342 | d->state = FcGCDirConsumed; |
343 | } | |
344 | return FcTrue; | |
1b7be377 | 345 | } |
1b7be377 | 346 | } |
1b7be377 | 347 | |
fff5a5af | 348 | return FcFalse; |
1b7be377 PL |
349 | } |
350 | ||
fff5a5af PL |
351 | static FcGlobalCacheDir * |
352 | FcGlobalCacheDirFind (FcGlobalCache *cache, const char *name) | |
353 | { | |
354 | FcGlobalCacheDir * d; | |
355 | ||
356 | if (!cache || !name) | |
357 | return NULL; | |
358 | ||
359 | for (d = cache->dirs; d; d = d->next) | |
360 | if (strcmp((const char *)d->name, (const char *)name) == 0) | |
361 | return d; | |
362 | ||
363 | return NULL; | |
364 | } | |
365 | ||
eb0cf671 PL |
366 | FcBool |
367 | FcGlobalCacheUpdate (FcGlobalCache *cache, | |
971cf180 | 368 | FcStrSet *dirs, |
fff5a5af | 369 | const char *orig_name, |
8a0b0ed6 PL |
370 | FcFontSet *set, |
371 | FcConfig *config) | |
1b7be377 | 372 | { |
971cf180 PL |
373 | FcGlobalCacheDir *d; |
374 | int i; | |
fff5a5af | 375 | const char *name; |
1b7be377 | 376 | |
fff5a5af PL |
377 | name = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)orig_name); |
378 | if (!name) | |
1b7be377 | 379 | { |
fff5a5af PL |
380 | fprintf(stderr, "Invalid directory name %s\n", orig_name); |
381 | return FcFalse; | |
24330d27 | 382 | } |
24330d27 | 383 | |
fff5a5af PL |
384 | d = FcGlobalCacheDirFind (cache, name); |
385 | ||
eb0cf671 | 386 | if (!d) |
24330d27 | 387 | { |
eb0cf671 PL |
388 | d = malloc (sizeof (FcGlobalCacheDir)); |
389 | if (!d) | |
390 | return FcFalse; | |
391 | d->next = cache->dirs; | |
392 | cache->dirs = d; | |
fff5a5af PL |
393 | } else { |
394 | /* free old resources */ | |
395 | FcStrFree ((FcChar8 *)d->name); | |
396 | free (d->ent); | |
397 | FcStrSetDestroy (d->subdirs); | |
24330d27 | 398 | } |
24330d27 | 399 | |
eb0cf671 | 400 | cache->updated = FcTrue; |
24330d27 | 401 | |
8245771d | 402 | d->name = (char *)FcStrCopy ((FcChar8 *)name); |
eb0cf671 PL |
403 | d->ent = FcDirCacheProduce (set, &d->metadata); |
404 | d->offset = 0; | |
971cf180 | 405 | d->subdirs = FcStrSetCreate(); |
fff5a5af | 406 | d->state = FcGCDirUpdated; |
971cf180 PL |
407 | for (i = 0; i < dirs->num; i++) |
408 | FcStrSetAdd (d->subdirs, dirs->strs[i]); | |
eb0cf671 | 409 | return FcTrue; |
24330d27 KP |
410 | } |
411 | ||
412 | FcBool | |
327a7fd4 | 413 | FcGlobalCacheSave (FcGlobalCache *cache, |
8a0b0ed6 PL |
414 | const FcChar8 *cache_file, |
415 | FcConfig *config) | |
24330d27 | 416 | { |
0d745819 | 417 | int fd, fd_orig; |
327a7fd4 | 418 | FcGlobalCacheDir *dir; |
327a7fd4 | 419 | FcAtomic *atomic; |
eb0cf671 PL |
420 | off_t current_arch_start = 0, truncate_to; |
421 | char * current_arch_machine_name, * header; | |
24330d27 | 422 | |
eb0cf671 | 423 | if (!cache->updated) |
24330d27 KP |
424 | return FcTrue; |
425 | ||
daeed6e0 | 426 | #if defined (HAVE_GETUID) && defined (HAVE_GETEUID) |
a391da8f KP |
427 | /* Set-UID programs can't safely update the cache */ |
428 | if (getuid () != geteuid ()) | |
429 | return FcFalse; | |
daeed6e0 | 430 | #endif |
a391da8f | 431 | |
134f6011 KP |
432 | atomic = FcAtomicCreate (cache_file); |
433 | if (!atomic) | |
1d879de2 PL |
434 | return FcFalse; |
435 | ||
134f6011 | 436 | if (!FcAtomicLock (atomic)) |
24330d27 | 437 | goto bail1; |
879af706 | 438 | fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT | O_BINARY, |
eb0cf671 PL |
439 | S_IRUSR | S_IWUSR); |
440 | if (fd == -1) | |
24330d27 | 441 | goto bail2; |
f468f568 | 442 | FcCacheWriteString (fd, FC_GLOBAL_MAGIC_COOKIE); |
24330d27 | 443 | |
879af706 | 444 | fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY | O_BINARY); |
1d879de2 | 445 | |
eb0cf671 | 446 | current_arch_machine_name = FcCacheMachineSignature (); |
1d879de2 PL |
447 | if (fd_orig == -1) |
448 | current_arch_start = 0; | |
449 | else | |
450 | current_arch_start = FcCacheSkipToArch (fd_orig, | |
c60ec7cc | 451 | current_arch_machine_name); |
1d879de2 | 452 | |
eb0cf671 | 453 | if (current_arch_start < 0) |
f468f568 PL |
454 | { |
455 | off_t i = lseek(fd_orig, 0, SEEK_END); | |
456 | if (i < strlen (FC_GLOBAL_MAGIC_COOKIE)+1) | |
457 | i = strlen (FC_GLOBAL_MAGIC_COOKIE)+1; | |
458 | current_arch_start = FcCacheNextOffset (i); | |
459 | } | |
eb0cf671 | 460 | |
1d879de2 PL |
461 | if (!FcCacheCopyOld(fd, fd_orig, current_arch_start)) |
462 | goto bail3; | |
463 | ||
eb0cf671 | 464 | current_arch_start = lseek(fd, 0, SEEK_CUR); |
d6217cc6 | 465 | #if defined (HAVE_FTRUNCATE) |
eb0cf671 | 466 | if (ftruncate (fd, current_arch_start) == -1) |
1d879de2 | 467 | goto bail3; |
d6217cc6 PL |
468 | #else |
469 | #if defined (HAVE_CHSIZE) | |
470 | if (chsize (fd, current_arch_start) == -1) | |
471 | goto bail3; | |
472 | #else | |
473 | goto bail3; | |
474 | #endif | |
475 | #endif | |
eb0cf671 | 476 | |
03a212e5 PL |
477 | header = malloc (10 + strlen (current_arch_machine_name)); |
478 | if (!header) | |
1d879de2 | 479 | goto bail3; |
03a212e5 | 480 | |
8cb4c56d | 481 | truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11; |
eb0cf671 | 482 | for (dir = cache->dirs; dir; dir = dir->next) |
24330d27 | 483 | { |
0d745819 PL |
484 | int i; |
485 | ||
fff5a5af PL |
486 | if (dir->state == FcGCDirDisabled) |
487 | continue; | |
eb0cf671 PL |
488 | truncate_to += strlen(dir->name) + 1; |
489 | truncate_to += sizeof (FcCache); | |
1c5b6345 | 490 | truncate_to = FcCacheNextOffset (truncate_to); |
eb0cf671 | 491 | truncate_to += dir->metadata.count; |
971cf180 PL |
492 | |
493 | for (i = 0; i < dir->subdirs->size; i++) | |
494 | truncate_to += strlen((char *)dir->subdirs->strs[i]) + 1; | |
495 | truncate_to ++; | |
eb0cf671 PL |
496 | } |
497 | truncate_to -= current_arch_start; | |
eb0cf671 | 498 | |
8cb4c56d PL |
499 | sprintf (header, "%8x ", (int)truncate_to); |
500 | strcat (header, current_arch_machine_name); | |
501 | if (!FcCacheWriteString (fd, header)) | |
1d879de2 | 502 | goto bail4; |
8cb4c56d | 503 | |
c814c301 PL |
504 | free (header); |
505 | ||
eb0cf671 PL |
506 | for (dir = cache->dirs; dir; dir = dir->next) |
507 | { | |
0d745819 | 508 | int i; |
fff5a5af PL |
509 | const char * d; |
510 | off_t off; | |
971cf180 | 511 | |
fff5a5af PL |
512 | if (!dir->name || dir->state == FcGCDirDisabled) |
513 | continue; | |
514 | d = (const char *)FcConfigNormalizeFontDir (config, (const FcChar8 *)dir->name); | |
515 | if (!d) | |
516 | continue; | |
971cf180 | 517 | |
fff5a5af PL |
518 | if (dir->metadata.count && !dir->ent) |
519 | { | |
520 | if (dir->state == FcGCDirUpdated || fd_orig < 0) | |
68355f38 | 521 | { |
fff5a5af | 522 | fprintf(stderr, "Invalid metadata entry for %s, skipping...\n", d); |
68355f38 PL |
523 | continue; |
524 | } | |
fff5a5af PL |
525 | /* copy the old content */ |
526 | dir->ent = malloc (dir->metadata.count); | |
527 | if (!dir->ent) | |
68355f38 | 528 | { |
fff5a5af | 529 | perror("malloc error"); |
68355f38 PL |
530 | continue; |
531 | } | |
fff5a5af PL |
532 | off = FcCacheNextOffset (dir->offset + sizeof(FcCache)); |
533 | if (lseek (fd_orig, off, SEEK_SET) != off) | |
534 | { | |
535 | perror("lseek"); | |
536 | free(dir->ent); | |
537 | continue; | |
538 | } | |
539 | if (read (fd_orig, dir->ent, dir->metadata.count) | |
540 | != dir->metadata.count) | |
541 | { | |
542 | perror("read"); | |
543 | free(dir->ent); | |
544 | continue; | |
545 | } | |
546 | } | |
547 | ||
548 | FcCacheWriteString (fd, d); | |
549 | ||
550 | for (i = 0; i < dir->subdirs->size; i++) | |
551 | FcCacheWriteString (fd, (char *)dir->subdirs->strs[i]); | |
552 | FcCacheWriteString (fd, ""); | |
553 | ||
554 | if (write (fd, &dir->metadata, sizeof(FcCache)) != sizeof(FcCache)) | |
555 | { | |
556 | perror ("write metadata"); | |
557 | free (dir->ent); | |
558 | continue; | |
559 | } | |
560 | off = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)); | |
561 | if (lseek (fd, off, SEEK_SET) != off) | |
562 | { | |
563 | perror ("lseek"); | |
564 | free (dir->ent); | |
565 | continue; | |
566 | } | |
567 | if (dir->metadata.count) | |
568 | { | |
68355f38 PL |
569 | if (write (fd, dir->ent, dir->metadata.count) != dir->metadata.count) |
570 | { | |
571 | perror ("write dirent"); | |
572 | free (dir->ent); | |
573 | continue; | |
574 | } | |
68355f38 | 575 | } |
fff5a5af | 576 | free (dir->ent); |
24330d27 | 577 | } |
eb0cf671 | 578 | FcCacheWriteString (fd, ""); |
24330d27 | 579 | |
eb0cf671 | 580 | if (close (fd) == -1) |
1d879de2 | 581 | goto bail25; |
fff5a5af PL |
582 | |
583 | close (fd_orig); | |
584 | fd_orig = -1; | |
24330d27 | 585 | |
134f6011 | 586 | if (!FcAtomicReplaceOrig (atomic)) |
1d879de2 | 587 | goto bail25; |
24330d27 | 588 | |
134f6011 KP |
589 | FcAtomicUnlock (atomic); |
590 | FcAtomicDestroy (atomic); | |
591 | ||
24330d27 KP |
592 | cache->updated = FcFalse; |
593 | return FcTrue; | |
594 | ||
1d879de2 PL |
595 | bail4: |
596 | free (header); | |
597 | bail3: | |
598 | if (fd_orig != -1) | |
599 | close (fd_orig); | |
600 | ||
601 | close (fd); | |
602 | bail25: | |
134f6011 | 603 | FcAtomicDeleteNew (atomic); |
1d879de2 | 604 | bail2: |
134f6011 | 605 | FcAtomicUnlock (atomic); |
1d879de2 | 606 | bail1: |
134f6011 | 607 | FcAtomicDestroy (atomic); |
24330d27 KP |
608 | return FcFalse; |
609 | } | |
610 | ||
212c9f43 | 611 | /* |
eb0cf671 PL |
612 | * Find the next presumably-mmapable offset after the supplied file |
613 | * position. | |
212c9f43 | 614 | */ |
4262e0b3 PL |
615 | static int |
616 | FcCacheNextOffset(off_t w) | |
179c3995 | 617 | { |
7fd7221e | 618 | static long pagesize = -1; |
c001a192 PL |
619 | |
620 | if (w == -1) | |
621 | return w; | |
622 | ||
7fd7221e | 623 | if (pagesize == -1) |
d6217cc6 | 624 | #if defined (HAVE_SYSCONF) |
7fd7221e | 625 | pagesize = sysconf(_SC_PAGESIZE); |
d6217cc6 PL |
626 | #else |
627 | pagesize = FC_HARDCODED_PAGESIZE; | |
628 | #endif | |
7fd7221e | 629 | if (w % pagesize == 0) |
212c9f43 PL |
630 | return w; |
631 | else | |
7fd7221e | 632 | return ((w / pagesize)+1)*pagesize; |
179c3995 KP |
633 | } |
634 | ||
212c9f43 PL |
635 | /* return the address of the segment for the provided arch, |
636 | * or -1 if arch not found */ | |
637 | static off_t | |
c60ec7cc | 638 | FcCacheSkipToArch (int fd, const char * arch) |
212c9f43 | 639 | { |
2dbe7597 PL |
640 | char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9]; |
641 | char * candidate_arch; | |
212c9f43 PL |
642 | off_t current_arch_start = 0; |
643 | ||
ea44e218 | 644 | lseek (fd, 0, SEEK_SET); |
c60ec7cc | 645 | FcCacheSkipString (fd); |
ea44e218 PL |
646 | current_arch_start = lseek (fd, 0, SEEK_CUR); |
647 | ||
212c9f43 PL |
648 | /* skip arches that are not the current arch */ |
649 | while (1) | |
650 | { | |
651 | long bs; | |
652 | ||
8cb4c56d PL |
653 | if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start) |
654 | return -1; | |
655 | ||
eb0cf671 | 656 | if (FcCacheReadString (fd, candidate_arch_machine_name_count, |
2dbe7597 | 657 | sizeof (candidate_arch_machine_name_count)) == 0) |
5e678e94 | 658 | return -1; |
2dbe7597 PL |
659 | if (!strlen(candidate_arch_machine_name_count)) |
660 | return -1; | |
661 | bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16); | |
8cb4c56d | 662 | |
bb6b1993 | 663 | /* count = 0 should probably be distinguished from the !bs condition */ |
8cb4c56d PL |
664 | if (!bs || bs < strlen (candidate_arch_machine_name_count)) |
665 | return -1; | |
666 | ||
2dbe7597 | 667 | candidate_arch++; /* skip leading space */ |
212c9f43 | 668 | |
2dbe7597 | 669 | if (strcmp (candidate_arch, arch)==0) |
5e678e94 | 670 | return current_arch_start; |
212c9f43 | 671 | current_arch_start += bs; |
b17cf498 | 672 | current_arch_start = FcCacheNextOffset (current_arch_start); |
212c9f43 | 673 | } |
212c9f43 PL |
674 | } |
675 | ||
676 | /* Cuts out the segment at the file pointer (moves everything else | |
677 | * down to cover it), and leaves the file pointer at the end of the | |
678 | * file. */ | |
212c9f43 | 679 | static FcBool |
1d879de2 | 680 | FcCacheCopyOld (int fd, int fd_orig, off_t start) |
212c9f43 | 681 | { |
eb0cf671 | 682 | char * buf = malloc (8192); |
2dbe7597 | 683 | char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9]; |
4262e0b3 | 684 | long bs; |
212c9f43 | 685 | int c, bytes_skipped; |
1d879de2 | 686 | off_t loc; |
212c9f43 PL |
687 | |
688 | if (!buf) | |
689 | return FcFalse; | |
690 | ||
1d879de2 PL |
691 | loc = 0; |
692 | lseek (fd, 0, SEEK_SET); lseek (fd_orig, 0, SEEK_SET); | |
719f4b84 | 693 | FcCacheSkipString (fd); FcCacheSkipString (fd_orig); |
1d879de2 PL |
694 | do |
695 | { | |
696 | int b = 8192; | |
697 | if (loc + b > start) | |
698 | b = start - loc; | |
699 | ||
700 | if ((c = read (fd_orig, buf, b)) <= 0) | |
701 | break; | |
702 | if (write (fd, buf, c) < 0) | |
703 | goto bail; | |
704 | ||
705 | loc += c; | |
706 | } | |
707 | while (c > 0); | |
708 | ||
212c9f43 | 709 | lseek (fd, start, SEEK_SET); |
eb0cf671 | 710 | if (FcCacheReadString (fd, candidate_arch_machine_name, |
212c9f43 PL |
711 | sizeof (candidate_arch_machine_name)) == 0) |
712 | goto done; | |
2dbe7597 | 713 | if (!strlen(candidate_arch_machine_name)) |
212c9f43 PL |
714 | goto done; |
715 | ||
2dbe7597 | 716 | bs = strtol(candidate_arch_machine_name, 0, 16); |
212c9f43 PL |
717 | if (bs == 0) |
718 | goto done; | |
719 | ||
720 | bytes_skipped = 0; | |
721 | do | |
722 | { | |
723 | lseek (fd, start+bs+bytes_skipped, SEEK_SET); | |
eb0cf671 | 724 | if ((c = read (fd, buf, 8192)) <= 0) |
212c9f43 PL |
725 | break; |
726 | lseek (fd, start+bytes_skipped, SEEK_SET); | |
727 | if (write (fd, buf, c) < 0) | |
728 | goto bail; | |
729 | bytes_skipped += c; | |
730 | } | |
731 | while (c > 0); | |
732 | lseek (fd, start+bytes_skipped, SEEK_SET); | |
733 | ||
734 | done: | |
735 | free (buf); | |
736 | return FcTrue; | |
737 | ||
738 | bail: | |
739 | free (buf); | |
740 | return FcFalse; | |
741 | } | |
742 | ||
55c8fa4f | 743 | /* Does not check that the cache has the appropriate arch section. */ |
ec760b17 PL |
744 | /* Also, this can be fooled if the original location has a stale |
745 | * cache, and the hashed location has an up-to-date cache. Oh well, | |
746 | * sucks to be you in that case! */ | |
1b7be377 | 747 | FcBool |
7410e40b | 748 | FcDirCacheValid (const FcChar8 *dir, FcConfig *config) |
1b7be377 | 749 | { |
1b7be377 | 750 | struct stat file_stat, dir_stat; |
ea44e218 | 751 | int fd; |
1b7be377 PL |
752 | |
753 | if (stat ((char *) dir, &dir_stat) < 0) | |
1b7be377 | 754 | return FcFalse; |
6bf23804 | 755 | |
7410e40b | 756 | fd = FcDirCacheOpen (config, dir, NULL); |
ea44e218 PL |
757 | |
758 | if (fd < 0) | |
c7490074 | 759 | return FcFalse; |
ea44e218 | 760 | if (fstat (fd, &file_stat) < 0) |
df3efc11 | 761 | goto bail; |
ea44e218 PL |
762 | |
763 | close (fd); | |
ea44e218 | 764 | |
55c8fa4f PL |
765 | /* |
766 | * If the directory has been modified more recently than | |
767 | * the cache file, the cache is not valid | |
768 | */ | |
2b25f00c | 769 | if (dir_stat.st_mtime > file_stat.st_mtime) |
55c8fa4f | 770 | return FcFalse; |
2b25f00c | 771 | |
55c8fa4f | 772 | return FcTrue; |
ea44e218 | 773 | |
ea44e218 | 774 | bail: |
df3efc11 | 775 | close (fd); |
ea44e218 | 776 | return FcFalse; |
55c8fa4f PL |
777 | } |
778 | ||
779 | /* Assumes that the cache file in 'dir' exists. | |
780 | * Checks that the cache has the appropriate arch section. */ | |
781 | FcBool | |
7410e40b | 782 | FcDirCacheHasCurrentArch (const FcChar8 *dir, FcConfig *config) |
55c8fa4f | 783 | { |
55c8fa4f PL |
784 | int fd; |
785 | off_t current_arch_start; | |
786 | char *current_arch_machine_name; | |
8c0d6921 | 787 | FcCache metadata; |
a68ce952 | 788 | char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1]; |
55c8fa4f | 789 | |
7410e40b | 790 | fd = FcDirCacheOpen (config, dir, NULL); |
ea44e218 PL |
791 | if (fd < 0) |
792 | goto bail; | |
793 | ||
794 | current_arch_machine_name = FcCacheMachineSignature(); | |
c60ec7cc | 795 | current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name); |
8c0d6921 PL |
796 | |
797 | if (current_arch_start >= 0) | |
798 | { | |
a68ce952 PL |
799 | if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start) |
800 | goto bail1; | |
801 | ||
802 | FcCacheSkipString (fd); | |
803 | ||
804 | while (FcCacheReadString (fd, subdirName, sizeof (subdirName)) && strlen (subdirName) > 0) | |
805 | ; | |
806 | ||
8c0d6921 | 807 | if (read(fd, &metadata, sizeof(FcCache)) != sizeof(FcCache)) |
a68ce952 | 808 | goto bail1; |
8c0d6921 PL |
809 | |
810 | if (metadata.magic != FC_CACHE_MAGIC) | |
a68ce952 | 811 | goto bail1; |
8c0d6921 PL |
812 | } |
813 | ||
6bf23804 PL |
814 | close (fd); |
815 | ||
816 | if (current_arch_start < 0) | |
817 | return FcFalse; | |
55c8fa4f PL |
818 | |
819 | return FcTrue; | |
ea44e218 | 820 | |
a68ce952 PL |
821 | bail1: |
822 | close (fd); | |
ea44e218 | 823 | bail: |
ea44e218 | 824 | return FcFalse; |
55c8fa4f | 825 | } |
6bf23804 | 826 | |
7410e40b PL |
827 | #define CACHEBASE_LEN (1 + 32 + sizeof (FC_CACHE_SUFFIX)) |
828 | ||
829 | static const char bin2hex[] = { '0', '1', '2', '3', | |
830 | '4', '5', '6', '7', | |
831 | '8', '9', 'a', 'b', | |
832 | 'c', 'd', 'e', 'f' }; | |
833 | ||
834 | static FcChar8 * | |
835 | FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN]) | |
55c8fa4f | 836 | { |
7410e40b PL |
837 | unsigned char hash[16]; |
838 | FcChar8 *hex_hash; | |
839 | int cnt; | |
840 | struct MD5Context ctx; | |
55c8fa4f | 841 | |
7410e40b PL |
842 | MD5Init (&ctx); |
843 | MD5Update (&ctx, (unsigned char *)dir, strlen ((char *) dir)); | |
3bfae75d | 844 | |
7410e40b PL |
845 | MD5Final (hash, &ctx); |
846 | ||
847 | cache_base[0] = '/'; | |
848 | hex_hash = cache_base + 1; | |
849 | for (cnt = 0; cnt < 16; ++cnt) | |
1178b569 | 850 | { |
7410e40b PL |
851 | hex_hash[2*cnt ] = bin2hex[hash[cnt] >> 4]; |
852 | hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf]; | |
853 | } | |
854 | hex_hash[2*cnt] = 0; | |
855 | strcat ((char *) cache_base, FC_CACHE_SUFFIX); | |
6f9fcb51 | 856 | |
7410e40b PL |
857 | return cache_base; |
858 | } | |
3bfae75d | 859 | |
7410e40b PL |
860 | FcBool |
861 | FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config) | |
862 | { | |
863 | int fd = -1; | |
864 | FcChar8 *cache_hashed = NULL; | |
865 | FcChar8 cache_base[CACHEBASE_LEN]; | |
866 | FcStrList *list; | |
867 | FcChar8 *cache_dir; | |
868 | char dir_buf[FC_MAX_FILE_LEN]; | |
3bfae75d | 869 | |
7410e40b | 870 | dir = FcConfigNormalizeFontDir (config, dir); |
3bfae75d | 871 | |
7410e40b | 872 | FcDirCacheBasename (dir, cache_base); |
3bfae75d | 873 | |
7410e40b PL |
874 | list = FcStrListCreate (config->cacheDirs); |
875 | if (!list) | |
876 | return FcFalse; | |
877 | ||
878 | while ((cache_dir = FcStrListNext (list))) | |
3bfae75d | 879 | { |
7410e40b PL |
880 | cache_hashed = FcStrPlus (cache_dir, cache_base); |
881 | if (!cache_hashed) | |
882 | break; | |
883 | fd = open((char *) cache_hashed, O_RDONLY | O_BINARY); | |
884 | FcStrFree (cache_hashed); | |
885 | if (fd >= 0) | |
886 | { | |
887 | if (FcCacheReadString (fd, dir_buf, sizeof (dir_buf)) && | |
888 | strcmp (dir_buf, (char *) dir) == 0) | |
889 | { | |
890 | close (fd); | |
891 | if (unlink ((char *) cache_hashed) < 0) | |
892 | break; | |
893 | } else | |
894 | close (fd); | |
895 | } | |
1178b569 | 896 | } |
7410e40b PL |
897 | FcStrListDone (list); |
898 | /* return FcFalse if something went wrong */ | |
899 | if (cache_dir) | |
900 | return FcFalse; | |
1b7be377 PL |
901 | return FcTrue; |
902 | } | |
903 | ||
4262e0b3 | 904 | static int |
1b7be377 | 905 | FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache, |
8b413bb6 | 906 | FcStrList *list, FcFontSet * set, FcStrSet *processed_dirs) |
4262e0b3 | 907 | { |
4262e0b3 PL |
908 | int ret = 0; |
909 | FcChar8 *dir; | |
4262e0b3 PL |
910 | FcStrSet *subdirs; |
911 | FcStrList *sublist; | |
912 | struct stat statb; | |
fff5a5af | 913 | FcGlobalCacheDir *d; |
4262e0b3 PL |
914 | |
915 | /* | |
03a212e5 | 916 | * Read in the results from 'list'. |
4262e0b3 PL |
917 | */ |
918 | while ((dir = FcStrListNext (list))) | |
919 | { | |
9fad72ab PL |
920 | if (!FcConfigAcceptFilename (config, dir)) |
921 | continue; | |
922 | ||
fff5a5af PL |
923 | /* Skip this directory if already updated |
924 | * to avoid the looped directories via symlinks | |
5c3deb29 | 925 | * Clearly a dir not in fonts.conf shouldn't be globally cached. |
fff5a5af | 926 | */ |
b10e7762 | 927 | dir = (FcChar8 *)FcConfigNormalizeFontDir (config, dir); |
5c3deb29 PL |
928 | if (!dir) |
929 | continue; | |
930 | ||
931 | if (FcStrSetMember (processed_dirs, dir)) | |
932 | continue; | |
933 | if (!FcStrSetAdd (processed_dirs, dir)) | |
934 | continue; | |
4262e0b3 PL |
935 | |
936 | subdirs = FcStrSetCreate (); | |
937 | if (!subdirs) | |
938 | { | |
939 | fprintf (stderr, "Can't create directory set\n"); | |
940 | ret++; | |
4262e0b3 PL |
941 | continue; |
942 | } | |
943 | ||
944 | if (access ((char *) dir, X_OK) < 0) | |
945 | { | |
946 | switch (errno) { | |
947 | case ENOENT: | |
948 | case ENOTDIR: | |
949 | case EACCES: | |
950 | break; | |
951 | default: | |
952 | fprintf (stderr, "\"%s\": ", dir); | |
953 | perror (""); | |
954 | ret++; | |
955 | } | |
956 | FcStrSetDestroy (subdirs); | |
4262e0b3 PL |
957 | continue; |
958 | } | |
959 | if (stat ((char *) dir, &statb) == -1) | |
960 | { | |
961 | fprintf (stderr, "\"%s\": ", dir); | |
962 | perror (""); | |
963 | FcStrSetDestroy (subdirs); | |
964 | ret++; | |
4262e0b3 PL |
965 | continue; |
966 | } | |
967 | if (!S_ISDIR (statb.st_mode)) | |
968 | { | |
969 | fprintf (stderr, "\"%s\": not a directory, skipping\n", dir); | |
970 | FcStrSetDestroy (subdirs); | |
4262e0b3 PL |
971 | continue; |
972 | } | |
7410e40b PL |
973 | if (FcDirCacheValid (dir, config) && |
974 | FcDirCacheHasCurrentArch (dir, config) && | |
530e66b0 | 975 | FcDirCacheRead (set, subdirs, dir, config)) |
fff5a5af PL |
976 | { |
977 | /* if an old entry is found in the global cache, disable it */ | |
5c3deb29 | 978 | if ((d = FcGlobalCacheDirFind (cache, (const char *)dir)) != NULL) |
fff5a5af PL |
979 | { |
980 | d->state = FcGCDirDisabled; | |
981 | /* save the updated config later without this entry */ | |
982 | cache->updated = FcTrue; | |
983 | } | |
984 | } | |
985 | else | |
4262e0b3 | 986 | { |
1b7be377 | 987 | if (FcDebug () & FC_DBG_FONTSET) |
03a212e5 | 988 | printf ("cache scan dir %s\n", dir); |
2304e38f | 989 | |
1b7be377 PL |
990 | FcDirScanConfig (set, subdirs, cache, |
991 | config->blanks, dir, FcFalse, config); | |
4262e0b3 PL |
992 | } |
993 | sublist = FcStrListCreate (subdirs); | |
994 | FcStrSetDestroy (subdirs); | |
995 | if (!sublist) | |
996 | { | |
997 | fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir); | |
998 | ret++; | |
4262e0b3 PL |
999 | continue; |
1000 | } | |
8b413bb6 | 1001 | ret += FcCacheReadDirs (config, cache, sublist, set, processed_dirs); |
4262e0b3 PL |
1002 | } |
1003 | FcStrListDone (list); | |
1004 | return ret; | |
1005 | } | |
1006 | ||
1007 | FcFontSet * | |
1b7be377 | 1008 | FcCacheRead (FcConfig *config, FcGlobalCache * cache) |
4262e0b3 | 1009 | { |
8b413bb6 PL |
1010 | FcFontSet *s = FcFontSetCreate(); |
1011 | FcStrSet *processed_dirs; | |
1012 | ||
4262e0b3 PL |
1013 | if (!s) |
1014 | return 0; | |
1015 | ||
8b413bb6 PL |
1016 | processed_dirs = FcStrSetCreate(); |
1017 | if (!processed_dirs) | |
4262e0b3 PL |
1018 | goto bail; |
1019 | ||
8b413bb6 PL |
1020 | if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s, processed_dirs)) |
1021 | goto bail1; | |
1022 | ||
1023 | FcStrSetDestroy (processed_dirs); | |
4262e0b3 PL |
1024 | return s; |
1025 | ||
8b413bb6 PL |
1026 | bail1: |
1027 | FcStrSetDestroy (processed_dirs); | |
4262e0b3 PL |
1028 | bail: |
1029 | FcFontSetDestroy (s); | |
1030 | return 0; | |
1031 | } | |
1032 | ||
7410e40b PL |
1033 | /* Opens the cache file for 'dir' for reading. |
1034 | * This searches the list of cache dirs for the relevant cache file, | |
1035 | * returning the first one found. | |
1036 | */ | |
ea44e218 | 1037 | static int |
7410e40b | 1038 | FcDirCacheOpen (FcConfig *config, const FcChar8 *dir, FcChar8 **cache_path) |
ea44e218 | 1039 | { |
7410e40b PL |
1040 | int fd = -1; |
1041 | FcChar8 *cache_hashed = NULL; | |
1042 | FcChar8 cache_base[CACHEBASE_LEN]; | |
1043 | FcStrList *list; | |
1044 | FcChar8 *cache_dir; | |
c1c3ba06 | 1045 | char dir_buf[FC_MAX_FILE_LEN]; |
df3efc11 | 1046 | |
7410e40b | 1047 | FcDirCacheBasename (dir, cache_base); |
ea44e218 | 1048 | |
7410e40b PL |
1049 | list = FcStrListCreate (config->cacheDirs); |
1050 | if (!list) | |
1051 | return -1; | |
1052 | ||
1053 | while ((cache_dir = FcStrListNext (list))) | |
ea44e218 | 1054 | { |
7410e40b PL |
1055 | cache_hashed = FcStrPlus (cache_dir, cache_base); |
1056 | if (!cache_hashed) | |
d00c3cb5 | 1057 | break; |
7410e40b PL |
1058 | fd = open((char *) cache_hashed, O_RDONLY | O_BINARY); |
1059 | if (fd >= 0) | |
d00c3cb5 | 1060 | break; |
7410e40b PL |
1061 | FcStrFree (cache_hashed); |
1062 | } | |
1063 | FcStrListDone (list); | |
df3efc11 | 1064 | |
7410e40b PL |
1065 | if (fd < 0) |
1066 | return -1; | |
1067 | ||
1068 | if (!FcCacheReadString (fd, dir_buf, sizeof (dir_buf)) || | |
1069 | strcmp (dir_buf, (char *) dir) != 0) | |
1070 | { | |
1071 | close (fd); | |
1072 | FcStrFree (cache_hashed); | |
1073 | return -1; | |
1074 | } | |
1075 | ||
1076 | if (cache_path) | |
1077 | *cache_path = cache_hashed; | |
1078 | else | |
1079 | FcStrFree (cache_hashed); | |
ea44e218 | 1080 | |
d00c3cb5 | 1081 | return fd; |
ea44e218 PL |
1082 | } |
1083 | ||
212c9f43 | 1084 | /* read serialized state from the cache file */ |
2eb84374 | 1085 | FcBool |
cd9bca69 | 1086 | FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *config) |
212c9f43 | 1087 | { |
ea44e218 PL |
1088 | int fd; |
1089 | char *current_arch_machine_name; | |
1090 | char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE]; | |
1091 | off_t current_arch_start = 0; | |
1092 | char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1]; | |
212c9f43 | 1093 | |
7410e40b | 1094 | fd = FcDirCacheOpen (config, dir, NULL); |
ea44e218 PL |
1095 | if (fd < 0) |
1096 | goto bail; | |
212c9f43 | 1097 | |
ea44e218 PL |
1098 | current_arch_machine_name = FcCacheMachineSignature(); |
1099 | current_arch_start = FcCacheSkipToArch(fd, | |
c60ec7cc | 1100 | current_arch_machine_name); |
212c9f43 | 1101 | if (current_arch_start < 0) |
4262e0b3 | 1102 | goto bail1; |
212c9f43 PL |
1103 | |
1104 | lseek (fd, current_arch_start, SEEK_SET); | |
eb0cf671 | 1105 | if (FcCacheReadString (fd, candidate_arch_machine_name, |
212c9f43 | 1106 | sizeof (candidate_arch_machine_name)) == 0) |
4262e0b3 | 1107 | goto bail1; |
2304e38f | 1108 | |
c5411c4c | 1109 | while (FcCacheReadString (fd, subdirName, sizeof (subdirName)) && strlen (subdirName) > 0) |
8245771d | 1110 | FcStrSetAdd (dirs, (FcChar8 *)subdirName); |
2304e38f | 1111 | |
cd9bca69 | 1112 | if (!FcDirCacheConsume (fd, (const char *)dir, set, config)) |
eb0cf671 PL |
1113 | goto bail1; |
1114 | ||
1115 | close(fd); | |
eb0cf671 PL |
1116 | return FcTrue; |
1117 | ||
1118 | bail1: | |
2304e38f | 1119 | close (fd); |
eb0cf671 | 1120 | bail: |
eb0cf671 PL |
1121 | return FcFalse; |
1122 | } | |
1123 | ||
1124 | static FcBool | |
cd9bca69 | 1125 | FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config) |
eb0cf671 PL |
1126 | { |
1127 | FcCache metadata; | |
1128 | void * current_dir_block; | |
c001a192 | 1129 | off_t pos, endpos; |
212c9f43 | 1130 | |
68355f38 PL |
1131 | if (read(fd, &metadata, sizeof(FcCache)) != sizeof(FcCache)) |
1132 | return FcFalse; | |
212c9f43 | 1133 | if (metadata.magic != FC_CACHE_MAGIC) |
eb0cf671 | 1134 | return FcFalse; |
212c9f43 | 1135 | |
2dbe7597 | 1136 | if (!metadata.count) |
05a98eaf PL |
1137 | { |
1138 | pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)); | |
1139 | lseek (fd, pos, SEEK_SET); | |
eadadf48 PL |
1140 | if (config) |
1141 | FcConfigAddFontDir (config, (FcChar8 *)dir); | |
eb0cf671 | 1142 | return FcTrue; |
05a98eaf | 1143 | } |
2dbe7597 | 1144 | |
fd77c154 | 1145 | pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)); |
c001a192 PL |
1146 | |
1147 | /* This is not failsafe (multi-arches can break it), | |
1148 | * but fd has got to have at least as many bytes as | |
1149 | * metadata.count, or something's going to go horribly wrong. */ | |
1150 | if (pos == (off_t)-1) | |
1151 | return FcFalse; | |
1152 | ||
1153 | endpos = lseek (fd, 0, SEEK_END); | |
1154 | if (endpos == (off_t)-1 || endpos - pos < metadata.count) | |
1155 | return FcFalse; | |
1156 | if (lseek (fd, pos, SEEK_SET) == -1) | |
1157 | return FcFalse; | |
1158 | ||
d6217cc6 | 1159 | #if defined(HAVE_MMAP) || defined(__CYGWIN__) |
2dbe7597 PL |
1160 | current_dir_block = mmap (0, metadata.count, |
1161 | PROT_READ, MAP_SHARED, fd, pos); | |
1162 | if (current_dir_block == MAP_FAILED) | |
eb0cf671 | 1163 | return FcFalse; |
93f67dfc PL |
1164 | #elif defined(_WIN32) |
1165 | { | |
1166 | HANDLE hFileMap; | |
1167 | ||
1168 | hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL, PAGE_READONLY, 0, 0, NULL); | |
1169 | if (hFileMap == NULL) | |
1170 | return FcFalse; | |
1171 | ||
1172 | current_dir_block = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0, metadata.count + pos); | |
1173 | if (current_dir_block == NULL) | |
1174 | { | |
1175 | CloseHandle (hFileMap); | |
1176 | return FcFalse; | |
1177 | } | |
1178 | ||
1179 | current_dir_block = (void *)((char *)current_dir_block + pos); | |
1180 | } | |
d6217cc6 PL |
1181 | #else |
1182 | current_dir_block = malloc (metadata.count); | |
1183 | if (!current_dir_block) | |
1184 | return FcFalse; | |
1185 | ||
1186 | /* could also use CreateMappedViewOfFile under MinGW... */ | |
1187 | if (read (fd, current_dir_block, metadata.count) != metadata.count) | |
1188 | goto bail; | |
1189 | #endif | |
1190 | lseek (fd, pos+metadata.count, SEEK_SET); | |
eb0cf671 | 1191 | |
e77c1718 | 1192 | FcCacheAddBankDir (metadata.bank, dir); |
cd9bca69 PL |
1193 | if (config) |
1194 | FcConfigAddFontDir (config, (FcChar8 *)dir); | |
e77c1718 | 1195 | |
b8948e85 | 1196 | if (!FcFontSetUnserialize (&metadata, set, current_dir_block)) |
d6217cc6 | 1197 | goto bail; |
b8948e85 | 1198 | |
212c9f43 | 1199 | return FcTrue; |
d6217cc6 PL |
1200 | bail: |
1201 | #if !(defined(HAVE_MMAP) || defined(__CYGWIN__)) | |
1202 | free (current_dir_block); | |
1203 | #endif | |
1204 | return FcFalse; | |
eb0cf671 PL |
1205 | } |
1206 | ||
1207 | static void * | |
1208 | FcDirCacheProduce (FcFontSet *set, FcCache *metadata) | |
1209 | { | |
1210 | void * current_dir_block, * final_dir_block; | |
8245771d | 1211 | static unsigned int rand_state = 0; |
8e351527 | 1212 | int bank, needed_bytes_no_align; |
eb0cf671 | 1213 | |
d6217cc6 | 1214 | #if defined (HAVE_RAND_R) |
eb0cf671 PL |
1215 | if (!rand_state) |
1216 | rand_state = time(0L); | |
1217 | bank = rand_r(&rand_state); | |
1218 | ||
1219 | while (FcCacheHaveBank(bank)) | |
1220 | bank = rand_r(&rand_state); | |
d6217cc6 PL |
1221 | #else |
1222 | if (!rand_state) | |
1223 | { | |
1224 | rand_state = 1; | |
1225 | srand (time (0L)); | |
1226 | } | |
1227 | bank = rand(); | |
1228 | ||
1229 | while (FcCacheHaveBank(bank)) | |
1230 | bank = rand(); | |
1231 | #endif | |
eb0cf671 PL |
1232 | |
1233 | memset (metadata, 0, sizeof(FcCache)); | |
1234 | FcFontSetNewBank(); | |
8e351527 PL |
1235 | needed_bytes_no_align = FcFontSetNeededBytes (set); |
1236 | metadata->count = needed_bytes_no_align + | |
7fd7221e | 1237 | FcFontSetNeededBytesAlign (); |
eb0cf671 PL |
1238 | metadata->magic = FC_CACHE_MAGIC; |
1239 | metadata->bank = bank; | |
1240 | ||
8e351527 PL |
1241 | if (!needed_bytes_no_align) /* not a failure, no fonts to write */ |
1242 | { | |
1243 | /* no, you don't really need to write any bytes at all. */ | |
1244 | metadata->count = 0; | |
eb0cf671 | 1245 | return 0; |
8e351527 | 1246 | } |
eb0cf671 PL |
1247 | |
1248 | current_dir_block = malloc (metadata->count); | |
1249 | if (!current_dir_block) | |
1250 | goto bail; | |
bb6b1993 | 1251 | /* shut up valgrind */ |
8e351527 | 1252 | memset (current_dir_block, 0, metadata->count); |
eb0cf671 PL |
1253 | final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block); |
1254 | ||
8e351527 | 1255 | if ((void *)((char *)current_dir_block+metadata->count) < final_dir_block) |
eb0cf671 PL |
1256 | goto bail; |
1257 | ||
1258 | if (!FcFontSetSerialize (bank, set)) | |
1259 | goto bail; | |
1260 | ||
1261 | return current_dir_block; | |
212c9f43 | 1262 | |
4262e0b3 | 1263 | bail: |
eb0cf671 PL |
1264 | free (current_dir_block); |
1265 | return 0; | |
212c9f43 PL |
1266 | } |
1267 | ||
7410e40b PL |
1268 | static FcBool |
1269 | FcMakeDirectory (const FcChar8 *dir) | |
1270 | { | |
1271 | FcChar8 *parent; | |
1272 | FcBool ret; | |
1273 | ||
1274 | if (strlen ((char *) dir) == 0) | |
1275 | return FcFalse; | |
1276 | ||
1277 | parent = FcStrDirname (dir); | |
1278 | if (!parent) | |
1279 | return FcFalse; | |
1280 | if (access ((char *) parent, W_OK|X_OK) == 0) | |
1281 | ret = mkdir ((char *) dir, 0777) == 0; | |
1282 | else if (access ((char *) parent, F_OK) == -1) | |
1283 | ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0777) == 0); | |
1284 | else | |
1285 | ret = FcFalse; | |
1286 | FcStrFree (parent); | |
1287 | return ret; | |
1288 | } | |
1289 | ||
212c9f43 PL |
1290 | /* write serialized state to the cache file */ |
1291 | FcBool | |
7410e40b | 1292 | FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir, FcConfig *config) |
212c9f43 | 1293 | { |
7410e40b PL |
1294 | FcChar8 cache_base[CACHEBASE_LEN]; |
1295 | FcChar8 *cache_hashed; | |
1d879de2 PL |
1296 | int fd, fd_orig, i, dirs_count; |
1297 | FcAtomic *atomic; | |
1298 | FcCache metadata; | |
1299 | off_t current_arch_start = 0, truncate_to; | |
7410e40b | 1300 | FcStrList *list; |
1d879de2 | 1301 | char *current_arch_machine_name, * header; |
ea44e218 | 1302 | void *current_dir_block = 0; |
7410e40b | 1303 | FcChar8 *cache_dir; |
212c9f43 | 1304 | |
cd9bca69 PL |
1305 | dir = FcConfigNormalizeFontDir (FcConfigGetCurrent(), dir); |
1306 | if (!dir) | |
1307 | return FcFalse; | |
1308 | ||
7410e40b PL |
1309 | /* |
1310 | * Check for an existing cache file and dump stuff in the same place | |
1311 | */ | |
1312 | fd = FcDirCacheOpen (config, dir, &cache_hashed); | |
c7490074 | 1313 | |
7410e40b PL |
1314 | if (fd >= 0) |
1315 | close (fd); | |
1316 | else { | |
1317 | list = FcStrListCreate (config->cacheDirs); | |
1318 | if (!list) | |
1319 | return FcFalse; | |
1320 | while ((cache_dir = FcStrListNext (list))) { | |
1321 | if (access ((char *) cache_dir, W_OK|X_OK) == 0) | |
1322 | break; | |
1323 | /* | |
1324 | * If the directory doesn't exist, try to create it | |
1325 | */ | |
1326 | if (access ((char *) cache_dir, F_OK) == -1) { | |
1327 | if (FcMakeDirectory (cache_dir)) | |
1328 | break; | |
1329 | } | |
1330 | } | |
1331 | FcStrListDone (list); | |
1332 | if (!cache_dir) | |
1333 | return FcFalse; | |
1334 | FcDirCacheBasename (dir, cache_base); | |
1335 | cache_hashed = FcStrPlus (cache_dir, cache_base); | |
1336 | if (!cache_hashed) | |
1337 | return FcFalse; | |
1338 | } | |
ea44e218 | 1339 | |
eb0cf671 | 1340 | current_dir_block = FcDirCacheProduce (set, &metadata); |
212c9f43 | 1341 | |
a9698bed | 1342 | if (metadata.count && !current_dir_block) |
ea44e218 | 1343 | goto bail1; |
212c9f43 | 1344 | |
4262e0b3 | 1345 | if (FcDebug () & FC_DBG_CACHE) |
c1c3ba06 KP |
1346 | printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n", |
1347 | dir, cache_hashed); | |
4262e0b3 | 1348 | |
3bfae75d | 1349 | atomic = FcAtomicCreate ((FcChar8 *)cache_hashed); |
1d879de2 | 1350 | if (!atomic) |
ea44e218 | 1351 | goto bail1; |
212c9f43 | 1352 | |
1d879de2 | 1353 | if (!FcAtomicLock (atomic)) |
c1c3ba06 | 1354 | goto bail2; |
ec760b17 | 1355 | |
c1c3ba06 KP |
1356 | /* open the original file to save relevant portions */ |
1357 | fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY | O_BINARY); | |
1d879de2 | 1358 | |
879af706 | 1359 | fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666); |
1d879de2 | 1360 | if (fd == -1) |
ec760b17 | 1361 | goto bail3; |
ea44e218 | 1362 | |
c1c3ba06 | 1363 | FcCacheWriteString (fd, (char *) dir); |
1d879de2 | 1364 | |
eb0cf671 | 1365 | current_arch_machine_name = FcCacheMachineSignature (); |
1d879de2 PL |
1366 | current_arch_start = 0; |
1367 | ||
1368 | if (fd_orig != -1) | |
1369 | current_arch_start = | |
c60ec7cc | 1370 | FcCacheSkipToArch(fd_orig, current_arch_machine_name); |
1d879de2 | 1371 | |
212c9f43 | 1372 | if (current_arch_start < 0) |
f468f568 | 1373 | { |
0d745819 PL |
1374 | off_t offset = lseek(fd_orig, 0, SEEK_END); |
1375 | current_arch_start = FcCacheNextOffset (offset); | |
f468f568 | 1376 | } |
212c9f43 | 1377 | |
1d879de2 | 1378 | if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start)) |
ea44e218 | 1379 | goto bail4; |
1d879de2 PL |
1380 | |
1381 | if (fd_orig != -1) | |
1382 | close (fd_orig); | |
212c9f43 PL |
1383 | |
1384 | current_arch_start = lseek(fd, 0, SEEK_CUR); | |
d6217cc6 | 1385 | #if defined (HAVE_FTRUNCATE) |
212c9f43 | 1386 | if (ftruncate (fd, current_arch_start) == -1) |
ea44e218 | 1387 | goto bail4; |
d6217cc6 PL |
1388 | #else |
1389 | #if defined (HAVE_CHSIZE) | |
1390 | if (chsize (fd, current_arch_start) == -1) | |
1391 | goto bail4; | |
1392 | #else | |
1393 | goto bail4; | |
1394 | #endif | |
1395 | #endif | |
212c9f43 | 1396 | |
6aee8c6f PL |
1397 | /* allocate space for subdir names in this block */ |
1398 | dirs_count = 0; | |
1399 | for (i = 0; i < dirs->size; i++) | |
1400 | dirs_count += strlen((char *)dirs->strs[i]) + 1; | |
1401 | dirs_count ++; | |
1402 | ||
212c9f43 | 1403 | /* now write the address of the next offset */ |
6aee8c6f | 1404 | truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start; |
2dbe7597 | 1405 | header = malloc (10 + strlen (current_arch_machine_name)); |
eb0cf671 | 1406 | if (!header) |
ea44e218 | 1407 | goto bail4; |
2dbe7597 PL |
1408 | sprintf (header, "%8x ", (int)truncate_to); |
1409 | strcat (header, current_arch_machine_name); | |
eb0cf671 | 1410 | if (!FcCacheWriteString (fd, header)) |
ea44e218 | 1411 | goto bail5; |
212c9f43 | 1412 | |
2304e38f | 1413 | for (i = 0; i < dirs->size; i++) |
8245771d | 1414 | FcCacheWriteString (fd, (char *)dirs->strs[i]); |
2304e38f PL |
1415 | FcCacheWriteString (fd, ""); |
1416 | ||
fff5a5af PL |
1417 | if (write (fd, &metadata, sizeof(FcCache)) != sizeof(FcCache)) |
1418 | { | |
68355f38 PL |
1419 | perror("write metadata"); |
1420 | goto bail5; | |
1421 | } | |
a9698bed PL |
1422 | if (metadata.count) |
1423 | { | |
68355f38 PL |
1424 | off_t off = FcCacheNextOffset (lseek(fd, 0, SEEK_END)); |
1425 | if (lseek (fd, off, SEEK_SET) != off) | |
1426 | perror("lseek"); | |
1427 | else if (write (fd, current_dir_block, metadata.count) != | |
1428 | metadata.count) | |
1429 | perror("write current_dir_block"); | |
a9698bed | 1430 | free (current_dir_block); |
f2fb985c | 1431 | current_dir_block = 0; |
a9698bed | 1432 | } |
4262e0b3 | 1433 | |
2dbe7597 | 1434 | /* this actually serves to pad out the cache file, if needed */ |
d6217cc6 | 1435 | #if defined (HAVE_FTRUNCATE) |
212c9f43 | 1436 | if (ftruncate (fd, current_arch_start + truncate_to) == -1) |
ea44e218 | 1437 | goto bail5; |
d6217cc6 PL |
1438 | #else |
1439 | #if defined (HAVE_CHSIZE) | |
1440 | if (chsize (fd, current_arch_start + truncate_to) == -1) | |
1441 | goto bail5; | |
1442 | #else | |
1443 | goto bail5; | |
1444 | #endif | |
1445 | #endif | |
212c9f43 | 1446 | |
6f9fcb51 | 1447 | free (header); |
212c9f43 | 1448 | close(fd); |
1d879de2 | 1449 | if (!FcAtomicReplaceOrig(atomic)) |
ae2aafe6 | 1450 | goto bail3; |
3bfae75d | 1451 | FcStrFree ((FcChar8 *)cache_hashed); |
1d879de2 PL |
1452 | FcAtomicUnlock (atomic); |
1453 | FcAtomicDestroy (atomic); | |
212c9f43 PL |
1454 | return FcTrue; |
1455 | ||
ea44e218 | 1456 | bail5: |
0230c9f8 | 1457 | free (header); |
ea44e218 | 1458 | bail4: |
1d879de2 | 1459 | close (fd); |
ea44e218 | 1460 | bail3: |
1d879de2 | 1461 | FcAtomicUnlock (atomic); |
ea44e218 | 1462 | bail2: |
1d879de2 | 1463 | FcAtomicDestroy (atomic); |
ea44e218 | 1464 | bail1: |
3bfae75d | 1465 | FcStrFree ((FcChar8 *)cache_hashed); |
1d879de2 PL |
1466 | if (current_dir_block) |
1467 | free (current_dir_block); | |
212c9f43 PL |
1468 | return FcFalse; |
1469 | } | |
1470 | ||
2dbe7597 | 1471 | static char * |
eb0cf671 | 1472 | FcCacheMachineSignature () |
2dbe7597 PL |
1473 | { |
1474 | static char buf[MACHINE_SIGNATURE_SIZE]; | |
7fd7221e | 1475 | int32_t magic = ENDIAN_TEST; |
2dbe7597 PL |
1476 | char * m = (char *)&magic; |
1477 | ||
1478 | sprintf (buf, "%2x%2x%2x%2x " | |
1479 | "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x " | |
7fd7221e | 1480 | "%4x %4x %4x %4x %4x %4x %4x %4x\n", |
2dbe7597 | 1481 | m[0], m[1], m[2], m[3], |
cd310911 PL |
1482 | (unsigned int)sizeof (char), |
1483 | (unsigned int)sizeof (char *), | |
1484 | (unsigned int)sizeof (int), | |
1485 | (unsigned int)sizeof (FcPattern), | |
1486 | (unsigned int)sizeof (FcPatternEltPtr), | |
1487 | (unsigned int)sizeof (struct _FcPatternElt *), | |
1488 | (unsigned int)sizeof (FcPatternElt), | |
1489 | (unsigned int)sizeof (FcObjectPtr), | |
1490 | (unsigned int)sizeof (FcValueListPtr), | |
1491 | (unsigned int)sizeof (FcValue), | |
1492 | (unsigned int)sizeof (FcValueBinding), | |
1493 | (unsigned int)sizeof (struct _FcValueList *), | |
1494 | (unsigned int)sizeof (FcCharSet), | |
1495 | (unsigned int)sizeof (FcCharLeaf **), | |
1496 | (unsigned int)sizeof (FcChar16 *), | |
1497 | (unsigned int)sizeof (FcChar16), | |
1498 | (unsigned int)sizeof (FcCharLeaf), | |
1499 | (unsigned int)sizeof (FcChar32), | |
7fd7221e | 1500 | (unsigned int)sizeof (FcCache), |
d6217cc6 | 1501 | #if defined (HAVE_SYSCONF) |
7fd7221e | 1502 | (unsigned int)sysconf(_SC_PAGESIZE)); |
d6217cc6 PL |
1503 | #else |
1504 | (unsigned int)FC_HARDCODED_PAGESIZE); | |
1505 | #endif | |
2dbe7597 PL |
1506 | |
1507 | return buf; | |
1508 | } | |
1509 | ||
4262e0b3 | 1510 | static int banks_ptr = 0, banks_alloc = 0; |
b8948e85 | 1511 | int * _fcBankId = 0, * _fcBankIdx = 0; |
e77c1718 | 1512 | static const char ** bankDirs = 0; |
4262e0b3 | 1513 | |
eb0cf671 | 1514 | static FcBool |
4262e0b3 PL |
1515 | FcCacheHaveBank (int bank) |
1516 | { | |
1517 | int i; | |
1518 | ||
1519 | if (bank < FC_BANK_FIRST) | |
1520 | return FcTrue; | |
1521 | ||
1522 | for (i = 0; i < banks_ptr; i++) | |
b8948e85 | 1523 | if (_fcBankId[i] == bank) |
4262e0b3 PL |
1524 | return FcTrue; |
1525 | ||
1526 | return FcFalse; | |
1527 | } | |
1528 | ||
1529 | int | |
b8948e85 | 1530 | FcCacheBankToIndexMTF (int bank) |
4262e0b3 | 1531 | { |
751932dd | 1532 | int i, j; |
4262e0b3 PL |
1533 | |
1534 | for (i = 0; i < banks_ptr; i++) | |
b8948e85 | 1535 | if (_fcBankId[_fcBankIdx[i]] == bank) |
751932dd | 1536 | { |
b8948e85 | 1537 | int t = _fcBankIdx[i]; |
751932dd PL |
1538 | |
1539 | for (j = i; j > 0; j--) | |
b8948e85 PL |
1540 | _fcBankIdx[j] = _fcBankIdx[j-1]; |
1541 | _fcBankIdx[0] = t; | |
751932dd PL |
1542 | return t; |
1543 | } | |
4262e0b3 | 1544 | |
2dbe7597 | 1545 | if (banks_ptr >= banks_alloc) |
4262e0b3 | 1546 | { |
751932dd | 1547 | int * b, * bidx; |
e77c1718 PL |
1548 | const char ** bds; |
1549 | ||
b8948e85 | 1550 | b = realloc (_fcBankId, (banks_alloc + 4) * sizeof(int)); |
4262e0b3 PL |
1551 | if (!b) |
1552 | return -1; | |
b8948e85 | 1553 | _fcBankId = b; |
751932dd | 1554 | |
b8948e85 | 1555 | bidx = realloc (_fcBankIdx, (banks_alloc + 4) * sizeof(int)); |
751932dd PL |
1556 | if (!bidx) |
1557 | return -1; | |
b8948e85 | 1558 | _fcBankIdx = bidx; |
751932dd | 1559 | |
e77c1718 PL |
1560 | bds = realloc (bankDirs, (banks_alloc + 4) * sizeof (char *)); |
1561 | if (!bds) | |
1562 | return -1; | |
1563 | bankDirs = bds; | |
1564 | ||
4262e0b3 PL |
1565 | banks_alloc += 4; |
1566 | } | |
1567 | ||
1568 | i = banks_ptr++; | |
b8948e85 PL |
1569 | _fcBankId[i] = bank; |
1570 | _fcBankIdx[i] = i; | |
4262e0b3 PL |
1571 | return i; |
1572 | } | |
e77c1718 PL |
1573 | |
1574 | static void | |
1575 | FcCacheAddBankDir (int bank, const char * dir) | |
1576 | { | |
b8948e85 | 1577 | int bi = FcCacheBankToIndexMTF (bank); |
e77c1718 PL |
1578 | |
1579 | if (bi < 0) | |
1580 | return; | |
1581 | ||
1582 | bankDirs[bi] = (const char *)FcStrCopy ((FcChar8 *)dir); | |
1583 | } | |
1584 | ||
1585 | const char * | |
1586 | FcCacheFindBankDir (int bank) | |
1587 | { | |
1588 | int bi = FcCacheBankToIndex (bank); | |
1589 | return bankDirs[bi]; | |
1590 | } | |
1591 | ||
ea44e218 PL |
1592 | /* |
1593 | * This code implements the MD5 message-digest algorithm. | |
1594 | * The algorithm is due to Ron Rivest. This code was | |
1595 | * written by Colin Plumb in 1993, no copyright is claimed. | |
1596 | * This code is in the public domain; do with it what you wish. | |
1597 | * | |
1598 | * Equivalent code is available from RSA Data Security, Inc. | |
1599 | * This code has been tested against that, and is equivalent, | |
1600 | * except that you don't need to include two pages of legalese | |
1601 | * with every copy. | |
1602 | * | |
1603 | * To compute the message digest of a chunk of bytes, declare an | |
1604 | * MD5Context structure, pass it to MD5Init, call MD5Update as | |
1605 | * needed on buffers full of bytes, and then call MD5Final, which | |
1606 | * will fill a supplied 16-byte array with the digest. | |
1607 | */ | |
1608 | ||
1609 | #ifndef HIGHFIRST | |
1610 | #define byteReverse(buf, len) /* Nothing */ | |
1611 | #else | |
1612 | /* | |
1613 | * Note: this code is harmless on little-endian machines. | |
1614 | */ | |
1615 | void byteReverse(unsigned char *buf, unsigned longs) | |
1616 | { | |
1617 | FcChar32 t; | |
1618 | do { | |
1619 | t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 | | |
1620 | ((unsigned) buf[1] << 8 | buf[0]); | |
1621 | *(FcChar32 *) buf = t; | |
1622 | buf += 4; | |
1623 | } while (--longs); | |
1624 | } | |
1625 | #endif | |
1626 | ||
1627 | /* | |
1628 | * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious | |
1629 | * initialization constants. | |
1630 | */ | |
1631 | static void MD5Init(struct MD5Context *ctx) | |
1632 | { | |
1633 | ctx->buf[0] = 0x67452301; | |
1634 | ctx->buf[1] = 0xefcdab89; | |
1635 | ctx->buf[2] = 0x98badcfe; | |
1636 | ctx->buf[3] = 0x10325476; | |
1637 | ||
1638 | ctx->bits[0] = 0; | |
1639 | ctx->bits[1] = 0; | |
1640 | } | |
1641 | ||
1642 | /* | |
1643 | * Update context to reflect the concatenation of another buffer full | |
1644 | * of bytes. | |
1645 | */ | |
1646 | static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len) | |
1647 | { | |
1648 | FcChar32 t; | |
1649 | ||
1650 | /* Update bitcount */ | |
1651 | ||
1652 | t = ctx->bits[0]; | |
1653 | if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t) | |
1654 | ctx->bits[1]++; /* Carry from low to high */ | |
1655 | ctx->bits[1] += len >> 29; | |
1656 | ||
1657 | t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ | |
1658 | ||
1659 | /* Handle any leading odd-sized chunks */ | |
1660 | ||
1661 | if (t) { | |
1662 | unsigned char *p = (unsigned char *) ctx->in + t; | |
1663 | ||
1664 | t = 64 - t; | |
1665 | if (len < t) { | |
1666 | memcpy(p, buf, len); | |
1667 | return; | |
1668 | } | |
1669 | memcpy(p, buf, t); | |
1670 | byteReverse(ctx->in, 16); | |
1671 | MD5Transform(ctx->buf, (FcChar32 *) ctx->in); | |
1672 | buf += t; | |
1673 | len -= t; | |
1674 | } | |
1675 | /* Process data in 64-byte chunks */ | |
1676 | ||
1677 | while (len >= 64) { | |
1678 | memcpy(ctx->in, buf, 64); | |
1679 | byteReverse(ctx->in, 16); | |
1680 | MD5Transform(ctx->buf, (FcChar32 *) ctx->in); | |
1681 | buf += 64; | |
1682 | len -= 64; | |
1683 | } | |
1684 | ||
1685 | /* Handle any remaining bytes of data. */ | |
1686 | ||
1687 | memcpy(ctx->in, buf, len); | |
1688 | } | |
1689 | ||
1690 | /* | |
1691 | * Final wrapup - pad to 64-byte boundary with the bit pattern | |
1692 | * 1 0* (64-bit count of bits processed, MSB-first) | |
1693 | */ | |
1694 | static void MD5Final(unsigned char digest[16], struct MD5Context *ctx) | |
1695 | { | |
1696 | unsigned count; | |
1697 | unsigned char *p; | |
1698 | ||
1699 | /* Compute number of bytes mod 64 */ | |
1700 | count = (ctx->bits[0] >> 3) & 0x3F; | |
1701 | ||
1702 | /* Set the first char of padding to 0x80. This is safe since there is | |
1703 | always at least one byte free */ | |
1704 | p = ctx->in + count; | |
1705 | *p++ = 0x80; | |
1706 | ||
1707 | /* Bytes of padding needed to make 64 bytes */ | |
1708 | count = 64 - 1 - count; | |
1709 | ||
1710 | /* Pad out to 56 mod 64 */ | |
1711 | if (count < 8) { | |
1712 | /* Two lots of padding: Pad the first block to 64 bytes */ | |
1713 | memset(p, 0, count); | |
1714 | byteReverse(ctx->in, 16); | |
1715 | MD5Transform(ctx->buf, (FcChar32 *) ctx->in); | |
1716 | ||
1717 | /* Now fill the next block with 56 bytes */ | |
1718 | memset(ctx->in, 0, 56); | |
1719 | } else { | |
1720 | /* Pad block to 56 bytes */ | |
1721 | memset(p, 0, count - 8); | |
1722 | } | |
1723 | byteReverse(ctx->in, 14); | |
1724 | ||
1725 | /* Append length in bits and transform */ | |
1726 | ((FcChar32 *) ctx->in)[14] = ctx->bits[0]; | |
1727 | ((FcChar32 *) ctx->in)[15] = ctx->bits[1]; | |
1728 | ||
1729 | MD5Transform(ctx->buf, (FcChar32 *) ctx->in); | |
1730 | byteReverse((unsigned char *) ctx->buf, 4); | |
1731 | memcpy(digest, ctx->buf, 16); | |
1732 | memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */ | |
1733 | } | |
1734 | ||
1735 | ||
1736 | /* The four core functions - F1 is optimized somewhat */ | |
1737 | ||
1738 | /* #define F1(x, y, z) (x & y | ~x & z) */ | |
1739 | #define F1(x, y, z) (z ^ (x & (y ^ z))) | |
1740 | #define F2(x, y, z) F1(z, x, y) | |
1741 | #define F3(x, y, z) (x ^ y ^ z) | |
1742 | #define F4(x, y, z) (y ^ (x | ~z)) | |
1743 | ||
1744 | /* This is the central step in the MD5 algorithm. */ | |
1745 | #define MD5STEP(f, w, x, y, z, data, s) \ | |
1746 | ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) | |
1747 | ||
1748 | /* | |
1749 | * The core of the MD5 algorithm, this alters an existing MD5 hash to | |
1750 | * reflect the addition of 16 longwords of new data. MD5Update blocks | |
1751 | * the data and converts bytes into longwords for this routine. | |
1752 | */ | |
1753 | static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]) | |
1754 | { | |
1755 | register FcChar32 a, b, c, d; | |
1756 | ||
1757 | a = buf[0]; | |
1758 | b = buf[1]; | |
1759 | c = buf[2]; | |
1760 | d = buf[3]; | |
1761 | ||
1762 | MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); | |
1763 | MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); | |
1764 | MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); | |
1765 | MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); | |
1766 | MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); | |
1767 | MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); | |
1768 | MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); | |
1769 | MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); | |
1770 | MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); | |
1771 | MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); | |
1772 | MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); | |
1773 | MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); | |
1774 | MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); | |
1775 | MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); | |
1776 | MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); | |
1777 | MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); | |
1778 | ||
1779 | MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); | |
1780 | MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); | |
1781 | MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); | |
1782 | MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); | |
1783 | MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); | |
1784 | MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); | |
1785 | MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); | |
1786 | MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); | |
1787 | MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); | |
1788 | MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); | |
1789 | MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); | |
1790 | MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); | |
1791 | MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); | |
1792 | MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); | |
1793 | MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); | |
1794 | MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); | |
1795 | ||
1796 | MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); | |
1797 | MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); | |
1798 | MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); | |
1799 | MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); | |
1800 | MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); | |
1801 | MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); | |
1802 | MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); | |
1803 | MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); | |
1804 | MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); | |
1805 | MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); | |
1806 | MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); | |
1807 | MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); | |
1808 | MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); | |
1809 | MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); | |
1810 | MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); | |
1811 | MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); | |
1812 | ||
1813 | MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); | |
1814 | MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); | |
1815 | MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); | |
1816 | MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); | |
1817 | MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); | |
1818 | MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); | |
1819 | MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); | |
1820 | MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); | |
1821 | MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); | |
1822 | MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); | |
1823 | MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); | |
1824 | MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); | |
1825 | MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); | |
1826 | MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); | |
1827 | MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); | |
1828 | MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); | |
1829 | ||
1830 | buf[0] += a; | |
1831 | buf[1] += b; | |
1832 | buf[2] += c; | |
1833 | buf[3] += d; | |
1834 | } |