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