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