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