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