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