]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright © 2000 Keith Packard | |
3 | * Copyright © 2005 Patrick Lam | |
4 | * | |
5 | * Permission to use, copy, modify, distribute, and sell this software and its | |
6 | * documentation for any purpose is hereby granted without fee, provided that | |
7 | * the above copyright notice appear in all copies and that both that | |
8 | * copyright notice and this permission notice appear in supporting | |
9 | * documentation, and that the name of Keith Packard not be used in | |
10 | * advertising or publicity pertaining to distribution of the software without | |
11 | * specific, written prior permission. Keith Packard makes no | |
12 | * representations about the suitability of this software for any purpose. It | |
13 | * is provided "as is" without express or implied warranty. | |
14 | * | |
15 | * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, | |
16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO | |
17 | * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR | |
18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, | |
19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER | |
20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | |
21 | * PERFORMANCE OF THIS SOFTWARE. | |
22 | */ | |
23 | ||
24 | #include "fcint.h" | |
25 | #include "../fc-arch/fcarch.h" | |
26 | #include <stdio.h> | |
27 | #include <fcntl.h> | |
28 | #include <dirent.h> | |
29 | #include <string.h> | |
30 | #include <sys/types.h> | |
31 | #if defined(HAVE_MMAP) || defined(__CYGWIN__) | |
32 | # include <unistd.h> | |
33 | # include <sys/mman.h> | |
34 | #elif defined(_WIN32) | |
35 | # include <windows.h> | |
36 | #endif | |
37 | ||
38 | #ifndef O_BINARY | |
39 | #define O_BINARY 0 | |
40 | #endif | |
41 | ||
42 | struct MD5Context { | |
43 | FcChar32 buf[4]; | |
44 | FcChar32 bits[2]; | |
45 | unsigned char in[64]; | |
46 | }; | |
47 | ||
48 | static void MD5Init(struct MD5Context *ctx); | |
49 | static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len); | |
50 | static void MD5Final(unsigned char digest[16], struct MD5Context *ctx); | |
51 | static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]); | |
52 | ||
53 | #define CACHEBASE_LEN (1 + 32 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX)) | |
54 | ||
55 | static const char bin2hex[] = { '0', '1', '2', '3', | |
56 | '4', '5', '6', '7', | |
57 | '8', '9', 'a', 'b', | |
58 | 'c', 'd', 'e', 'f' }; | |
59 | ||
60 | static FcChar8 * | |
61 | FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN]) | |
62 | { | |
63 | unsigned char hash[16]; | |
64 | FcChar8 *hex_hash; | |
65 | int cnt; | |
66 | struct MD5Context ctx; | |
67 | ||
68 | MD5Init (&ctx); | |
69 | MD5Update (&ctx, (unsigned char *)dir, strlen ((char *) dir)); | |
70 | ||
71 | MD5Final (hash, &ctx); | |
72 | ||
73 | cache_base[0] = '/'; | |
74 | hex_hash = cache_base + 1; | |
75 | for (cnt = 0; cnt < 16; ++cnt) | |
76 | { | |
77 | hex_hash[2*cnt ] = bin2hex[hash[cnt] >> 4]; | |
78 | hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf]; | |
79 | } | |
80 | hex_hash[2*cnt] = 0; | |
81 | strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX); | |
82 | ||
83 | return cache_base; | |
84 | } | |
85 | ||
86 | FcBool | |
87 | FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config) | |
88 | { | |
89 | FcChar8 *cache_hashed = NULL; | |
90 | FcChar8 cache_base[CACHEBASE_LEN]; | |
91 | FcStrList *list; | |
92 | FcChar8 *cache_dir; | |
93 | ||
94 | FcDirCacheBasename (dir, cache_base); | |
95 | ||
96 | list = FcStrListCreate (config->cacheDirs); | |
97 | if (!list) | |
98 | return FcFalse; | |
99 | ||
100 | while ((cache_dir = FcStrListNext (list))) | |
101 | { | |
102 | cache_hashed = FcStrPlus (cache_dir, cache_base); | |
103 | if (!cache_hashed) | |
104 | break; | |
105 | (void) unlink ((char *) cache_hashed); | |
106 | } | |
107 | FcStrListDone (list); | |
108 | /* return FcFalse if something went wrong */ | |
109 | if (cache_dir) | |
110 | return FcFalse; | |
111 | return FcTrue; | |
112 | } | |
113 | ||
114 | static int | |
115 | FcDirCacheOpenFile (const FcChar8 *cache_file, struct stat *file_stat) | |
116 | { | |
117 | int fd; | |
118 | ||
119 | fd = open((char *) cache_file, O_RDONLY | O_BINARY); | |
120 | if (fd < 0) | |
121 | return fd; | |
122 | if (fstat (fd, file_stat) < 0) | |
123 | { | |
124 | close (fd); | |
125 | return -1; | |
126 | } | |
127 | return fd; | |
128 | } | |
129 | ||
130 | /* | |
131 | * Look for a cache file for the specified dir. Attempt | |
132 | * to use each one we find, stopping when the callback | |
133 | * indicates success | |
134 | */ | |
135 | static FcBool | |
136 | FcDirCacheProcess (FcConfig *config, const FcChar8 *dir, | |
137 | FcBool (*callback) (int fd, off_t size, void *closure), | |
138 | void *closure, FcChar8 **cache_file_ret) | |
139 | { | |
140 | int fd = -1; | |
141 | FcChar8 cache_base[CACHEBASE_LEN]; | |
142 | FcStrList *list; | |
143 | FcChar8 *cache_dir; | |
144 | struct stat file_stat, dir_stat; | |
145 | FcBool ret = FcFalse; | |
146 | ||
147 | if (stat ((char *) dir, &dir_stat) < 0) | |
148 | return FcFalse; | |
149 | ||
150 | FcDirCacheBasename (dir, cache_base); | |
151 | ||
152 | list = FcStrListCreate (config->cacheDirs); | |
153 | if (!list) | |
154 | return FcFalse; | |
155 | ||
156 | while ((cache_dir = FcStrListNext (list))) | |
157 | { | |
158 | FcChar8 *cache_hashed = FcStrPlus (cache_dir, cache_base); | |
159 | if (!cache_hashed) | |
160 | break; | |
161 | fd = FcDirCacheOpenFile (cache_hashed, &file_stat); | |
162 | if (fd >= 0) { | |
163 | if (dir_stat.st_mtime <= file_stat.st_mtime) | |
164 | { | |
165 | ret = (*callback) (fd, file_stat.st_size, closure); | |
166 | if (ret) | |
167 | { | |
168 | if (cache_file_ret) | |
169 | *cache_file_ret = cache_hashed; | |
170 | else | |
171 | FcStrFree (cache_hashed); | |
172 | close (fd); | |
173 | break; | |
174 | } | |
175 | } | |
176 | close (fd); | |
177 | } | |
178 | FcStrFree (cache_hashed); | |
179 | } | |
180 | FcStrListDone (list); | |
181 | ||
182 | return ret; | |
183 | } | |
184 | ||
185 | #define FC_CACHE_MIN_MMAP 1024 | |
186 | ||
187 | /* | |
188 | * Map a cache file into memory | |
189 | */ | |
190 | static FcCache * | |
191 | FcDirCacheMapFd (int fd, off_t size) | |
192 | { | |
193 | FcCache *cache = NULL; | |
194 | FcBool allocated = FcFalse; | |
195 | ||
196 | if (size < sizeof (FcCache)) | |
197 | return NULL; | |
198 | /* | |
199 | * For small cache files, just read them into memory | |
200 | */ | |
201 | if (size >= FC_CACHE_MIN_MMAP) | |
202 | { | |
203 | #if defined(HAVE_MMAP) || defined(__CYGWIN__) | |
204 | cache = mmap (0, size, PROT_READ, MAP_SHARED, fd, 0); | |
205 | #elif defined(_WIN32) | |
206 | { | |
207 | HANDLE hFileMap; | |
208 | ||
209 | cache = NULL; | |
210 | hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL, | |
211 | PAGE_READONLY, 0, 0, NULL); | |
212 | if (hFileMap != NULL) | |
213 | { | |
214 | cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0, size); | |
215 | CloseHandle (hFileMap); | |
216 | } | |
217 | } | |
218 | #endif | |
219 | } | |
220 | if (!cache) | |
221 | { | |
222 | cache = malloc (size); | |
223 | if (!cache) | |
224 | return NULL; | |
225 | ||
226 | if (read (fd, cache, size) != size) | |
227 | { | |
228 | free (cache); | |
229 | return NULL; | |
230 | } | |
231 | allocated = FcTrue; | |
232 | } | |
233 | if (cache->magic != FC_CACHE_MAGIC_MMAP || | |
234 | cache->version != FC_CACHE_CONTENT_VERSION || | |
235 | cache->size != size) | |
236 | { | |
237 | if (allocated) | |
238 | free (cache); | |
239 | else | |
240 | { | |
241 | #if defined(HAVE_MMAP) || defined(__CYGWIN__) | |
242 | munmap (cache, size); | |
243 | #elif defined(_WIN32) | |
244 | UnmapViewOfFile (cache); | |
245 | #endif | |
246 | } | |
247 | return NULL; | |
248 | } | |
249 | ||
250 | /* Mark allocated caches so they're freed rather than unmapped */ | |
251 | if (allocated) | |
252 | cache->magic = FC_CACHE_MAGIC_ALLOC; | |
253 | ||
254 | return cache; | |
255 | } | |
256 | ||
257 | void | |
258 | FcDirCacheUnload (FcCache *cache) | |
259 | { | |
260 | switch (cache->magic) { | |
261 | case FC_CACHE_MAGIC_ALLOC: | |
262 | free (cache); | |
263 | break; | |
264 | case FC_CACHE_MAGIC_MMAP: | |
265 | #if defined(HAVE_MMAP) || defined(__CYGWIN__) | |
266 | munmap (cache, cache->size); | |
267 | #elif defined(_WIN32) | |
268 | UnmapViewOfFile (cache); | |
269 | #endif | |
270 | break; | |
271 | } | |
272 | } | |
273 | ||
274 | static FcBool | |
275 | FcDirCacheMapHelper (int fd, off_t size, void *closure) | |
276 | { | |
277 | FcCache *cache = FcDirCacheMapFd (fd, size); | |
278 | ||
279 | if (!cache) | |
280 | return FcFalse; | |
281 | *((FcCache **) closure) = cache; | |
282 | return FcTrue; | |
283 | } | |
284 | ||
285 | FcCache * | |
286 | FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file) | |
287 | { | |
288 | FcCache *cache = NULL; | |
289 | ||
290 | if (!FcDirCacheProcess (config, dir, | |
291 | FcDirCacheMapHelper, | |
292 | &cache, cache_file)) | |
293 | return NULL; | |
294 | return cache; | |
295 | } | |
296 | ||
297 | FcCache * | |
298 | FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat) | |
299 | { | |
300 | int fd; | |
301 | FcCache *cache; | |
302 | ||
303 | fd = FcDirCacheOpenFile (cache_file, file_stat); | |
304 | if (fd < 0) | |
305 | return NULL; | |
306 | cache = FcDirCacheMapFd (fd, file_stat->st_size); | |
307 | close (fd); | |
308 | return cache; | |
309 | } | |
310 | ||
311 | /* | |
312 | * Validate a cache file by reading the header and checking | |
313 | * the magic number and the size field | |
314 | */ | |
315 | static FcBool | |
316 | FcDirCacheValidateHelper (int fd, off_t size, void *closure) | |
317 | { | |
318 | FcBool ret = FcTrue; | |
319 | FcCache c; | |
320 | struct stat file_stat; | |
321 | ||
322 | if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache)) | |
323 | ret = FcFalse; | |
324 | else if (c.magic != FC_CACHE_MAGIC_MMAP) | |
325 | ret = FcFalse; | |
326 | else if (c.version != FC_CACHE_CONTENT_VERSION) | |
327 | ret = FcFalse; | |
328 | else if (fstat (fd, &file_stat) < 0) | |
329 | ret = FcFalse; | |
330 | else if (file_stat.st_size != c.size) | |
331 | ret = FcFalse; | |
332 | return ret; | |
333 | } | |
334 | ||
335 | static FcBool | |
336 | FcDirCacheValidConfig (const FcChar8 *dir, FcConfig *config) | |
337 | { | |
338 | return FcDirCacheProcess (config, dir, | |
339 | FcDirCacheValidateHelper, | |
340 | NULL, NULL); | |
341 | } | |
342 | ||
343 | FcBool | |
344 | FcDirCacheValid (const FcChar8 *dir) | |
345 | { | |
346 | FcConfig *config; | |
347 | ||
348 | config = FcConfigGetCurrent (); | |
349 | if (!config) | |
350 | return FcFalse; | |
351 | ||
352 | return FcDirCacheValidConfig (dir, config); | |
353 | } | |
354 | ||
355 | /* | |
356 | * Build a cache structure from the given contents | |
357 | */ | |
358 | FcCache * | |
359 | FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, FcStrSet *dirs) | |
360 | { | |
361 | FcSerialize *serialize = FcSerializeCreate (); | |
362 | FcCache *cache; | |
363 | int i; | |
364 | intptr_t cache_offset; | |
365 | intptr_t dirs_offset; | |
366 | FcChar8 *dir_serialize; | |
367 | intptr_t *dirs_serialize; | |
368 | FcFontSet *set_serialize; | |
369 | ||
370 | if (!serialize) | |
371 | return NULL; | |
372 | /* | |
373 | * Space for cache structure | |
374 | */ | |
375 | cache_offset = FcSerializeReserve (serialize, sizeof (FcCache)); | |
376 | /* | |
377 | * Directory name | |
378 | */ | |
379 | if (!FcStrSerializeAlloc (serialize, dir)) | |
380 | goto bail1; | |
381 | /* | |
382 | * Subdirs | |
383 | */ | |
384 | dirs_offset = FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *)); | |
385 | for (i = 0; i < dirs->num; i++) | |
386 | if (!FcStrSerializeAlloc (serialize, dirs->strs[i])) | |
387 | goto bail1; | |
388 | ||
389 | /* | |
390 | * Patterns | |
391 | */ | |
392 | if (!FcFontSetSerializeAlloc (serialize, set)) | |
393 | goto bail1; | |
394 | ||
395 | /* Serialize layout complete. Now allocate space and fill it */ | |
396 | cache = malloc (serialize->size); | |
397 | if (!cache) | |
398 | goto bail1; | |
399 | /* shut up valgrind */ | |
400 | memset (cache, 0, serialize->size); | |
401 | ||
402 | serialize->linear = cache; | |
403 | ||
404 | cache->magic = FC_CACHE_MAGIC_ALLOC; | |
405 | cache->version = FC_CACHE_CONTENT_VERSION; | |
406 | cache->size = serialize->size; | |
407 | ||
408 | /* | |
409 | * Serialize directory name | |
410 | */ | |
411 | dir_serialize = FcStrSerialize (serialize, dir); | |
412 | if (!dir_serialize) | |
413 | goto bail2; | |
414 | cache->dir = FcPtrToOffset (cache, dir_serialize); | |
415 | ||
416 | /* | |
417 | * Serialize sub dirs | |
418 | */ | |
419 | dirs_serialize = FcSerializePtr (serialize, dirs); | |
420 | if (!dirs_serialize) | |
421 | goto bail2; | |
422 | cache->dirs = FcPtrToOffset (cache, dirs_serialize); | |
423 | cache->dirs_count = dirs->num; | |
424 | for (i = 0; i < dirs->num; i++) | |
425 | { | |
426 | FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]); | |
427 | if (!d_serialize) | |
428 | goto bail2; | |
429 | dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize); | |
430 | } | |
431 | ||
432 | /* | |
433 | * Serialize font set | |
434 | */ | |
435 | set_serialize = FcFontSetSerialize (serialize, set); | |
436 | if (!set_serialize) | |
437 | goto bail2; | |
438 | cache->set = FcPtrToOffset (cache, set_serialize); | |
439 | ||
440 | FcSerializeDestroy (serialize); | |
441 | ||
442 | return cache; | |
443 | ||
444 | bail2: | |
445 | free (cache); | |
446 | bail1: | |
447 | FcSerializeDestroy (serialize); | |
448 | return NULL; | |
449 | } | |
450 | ||
451 | static FcBool | |
452 | FcMakeDirectory (const FcChar8 *dir) | |
453 | { | |
454 | FcChar8 *parent; | |
455 | FcBool ret; | |
456 | ||
457 | if (strlen ((char *) dir) == 0) | |
458 | return FcFalse; | |
459 | ||
460 | parent = FcStrDirname (dir); | |
461 | if (!parent) | |
462 | return FcFalse; | |
463 | if (access ((char *) parent, W_OK|X_OK) == 0) | |
464 | ret = mkdir ((char *) dir, 0777) == 0; | |
465 | else if (access ((char *) parent, F_OK) == -1) | |
466 | ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0777) == 0); | |
467 | else | |
468 | ret = FcFalse; | |
469 | FcStrFree (parent); | |
470 | return ret; | |
471 | } | |
472 | ||
473 | /* write serialized state to the cache file */ | |
474 | FcBool | |
475 | FcDirCacheWrite (FcCache *cache, FcConfig *config) | |
476 | { | |
477 | FcChar8 *dir = FcCacheDir (cache); | |
478 | FcChar8 cache_base[CACHEBASE_LEN]; | |
479 | FcChar8 *cache_hashed; | |
480 | int fd; | |
481 | FcAtomic *atomic; | |
482 | FcStrList *list; | |
483 | FcChar8 *cache_dir = NULL; | |
484 | FcChar8 *test_dir; | |
485 | int magic; | |
486 | int written; | |
487 | ||
488 | /* | |
489 | * Write it to the first directory in the list which is writable | |
490 | */ | |
491 | ||
492 | list = FcStrListCreate (config->cacheDirs); | |
493 | if (!list) | |
494 | return FcFalse; | |
495 | while ((test_dir = FcStrListNext (list))) { | |
496 | if (access ((char *) test_dir, W_OK|X_OK) == 0) | |
497 | { | |
498 | cache_dir = test_dir; | |
499 | break; | |
500 | } | |
501 | else | |
502 | { | |
503 | /* | |
504 | * If the directory doesn't exist, try to create it | |
505 | */ | |
506 | if (access ((char *) test_dir, F_OK) == -1) { | |
507 | if (FcMakeDirectory (test_dir)) | |
508 | { | |
509 | cache_dir = test_dir; | |
510 | break; | |
511 | } | |
512 | } | |
513 | } | |
514 | } | |
515 | FcStrListDone (list); | |
516 | if (!cache_dir) | |
517 | return FcFalse; | |
518 | ||
519 | FcDirCacheBasename (dir, cache_base); | |
520 | cache_hashed = FcStrPlus (cache_dir, cache_base); | |
521 | if (!cache_hashed) | |
522 | return FcFalse; | |
523 | ||
524 | if (FcDebug () & FC_DBG_CACHE) | |
525 | printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n", | |
526 | dir, cache_hashed); | |
527 | ||
528 | atomic = FcAtomicCreate ((FcChar8 *)cache_hashed); | |
529 | if (!atomic) | |
530 | goto bail1; | |
531 | ||
532 | if (!FcAtomicLock (atomic)) | |
533 | goto bail3; | |
534 | ||
535 | fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666); | |
536 | if (fd == -1) | |
537 | goto bail4; | |
538 | ||
539 | /* Temporarily switch magic to MMAP while writing to file */ | |
540 | magic = cache->magic; | |
541 | if (magic != FC_CACHE_MAGIC_MMAP) | |
542 | cache->magic = FC_CACHE_MAGIC_MMAP; | |
543 | ||
544 | /* | |
545 | * Write cache contents to file | |
546 | */ | |
547 | written = write (fd, cache, cache->size); | |
548 | ||
549 | /* Switch magic back */ | |
550 | if (magic != FC_CACHE_MAGIC_MMAP) | |
551 | cache->magic = magic; | |
552 | ||
553 | if (written != cache->size) | |
554 | { | |
555 | perror ("write cache"); | |
556 | goto bail5; | |
557 | } | |
558 | ||
559 | close(fd); | |
560 | if (!FcAtomicReplaceOrig(atomic)) | |
561 | goto bail4; | |
562 | FcStrFree (cache_hashed); | |
563 | FcAtomicUnlock (atomic); | |
564 | FcAtomicDestroy (atomic); | |
565 | return FcTrue; | |
566 | ||
567 | bail5: | |
568 | close (fd); | |
569 | bail4: | |
570 | FcAtomicUnlock (atomic); | |
571 | bail3: | |
572 | FcAtomicDestroy (atomic); | |
573 | bail1: | |
574 | FcStrFree (cache_hashed); | |
575 | return FcFalse; | |
576 | } | |
577 | ||
578 | /* | |
579 | * This code implements the MD5 message-digest algorithm. | |
580 | * The algorithm is due to Ron Rivest. This code was | |
581 | * written by Colin Plumb in 1993, no copyright is claimed. | |
582 | * This code is in the public domain; do with it what you wish. | |
583 | * | |
584 | * Equivalent code is available from RSA Data Security, Inc. | |
585 | * This code has been tested against that, and is equivalent, | |
586 | * except that you don't need to include two pages of legalese | |
587 | * with every copy. | |
588 | * | |
589 | * To compute the message digest of a chunk of bytes, declare an | |
590 | * MD5Context structure, pass it to MD5Init, call MD5Update as | |
591 | * needed on buffers full of bytes, and then call MD5Final, which | |
592 | * will fill a supplied 16-byte array with the digest. | |
593 | */ | |
594 | ||
595 | #ifndef HIGHFIRST | |
596 | #define byteReverse(buf, len) /* Nothing */ | |
597 | #else | |
598 | /* | |
599 | * Note: this code is harmless on little-endian machines. | |
600 | */ | |
601 | void byteReverse(unsigned char *buf, unsigned longs) | |
602 | { | |
603 | FcChar32 t; | |
604 | do { | |
605 | t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 | | |
606 | ((unsigned) buf[1] << 8 | buf[0]); | |
607 | *(FcChar32 *) buf = t; | |
608 | buf += 4; | |
609 | } while (--longs); | |
610 | } | |
611 | #endif | |
612 | ||
613 | /* | |
614 | * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious | |
615 | * initialization constants. | |
616 | */ | |
617 | static void MD5Init(struct MD5Context *ctx) | |
618 | { | |
619 | ctx->buf[0] = 0x67452301; | |
620 | ctx->buf[1] = 0xefcdab89; | |
621 | ctx->buf[2] = 0x98badcfe; | |
622 | ctx->buf[3] = 0x10325476; | |
623 | ||
624 | ctx->bits[0] = 0; | |
625 | ctx->bits[1] = 0; | |
626 | } | |
627 | ||
628 | /* | |
629 | * Update context to reflect the concatenation of another buffer full | |
630 | * of bytes. | |
631 | */ | |
632 | static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len) | |
633 | { | |
634 | FcChar32 t; | |
635 | ||
636 | /* Update bitcount */ | |
637 | ||
638 | t = ctx->bits[0]; | |
639 | if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t) | |
640 | ctx->bits[1]++; /* Carry from low to high */ | |
641 | ctx->bits[1] += len >> 29; | |
642 | ||
643 | t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ | |
644 | ||
645 | /* Handle any leading odd-sized chunks */ | |
646 | ||
647 | if (t) { | |
648 | unsigned char *p = (unsigned char *) ctx->in + t; | |
649 | ||
650 | t = 64 - t; | |
651 | if (len < t) { | |
652 | memcpy(p, buf, len); | |
653 | return; | |
654 | } | |
655 | memcpy(p, buf, t); | |
656 | byteReverse(ctx->in, 16); | |
657 | MD5Transform(ctx->buf, (FcChar32 *) ctx->in); | |
658 | buf += t; | |
659 | len -= t; | |
660 | } | |
661 | /* Process data in 64-byte chunks */ | |
662 | ||
663 | while (len >= 64) { | |
664 | memcpy(ctx->in, buf, 64); | |
665 | byteReverse(ctx->in, 16); | |
666 | MD5Transform(ctx->buf, (FcChar32 *) ctx->in); | |
667 | buf += 64; | |
668 | len -= 64; | |
669 | } | |
670 | ||
671 | /* Handle any remaining bytes of data. */ | |
672 | ||
673 | memcpy(ctx->in, buf, len); | |
674 | } | |
675 | ||
676 | /* | |
677 | * Final wrapup - pad to 64-byte boundary with the bit pattern | |
678 | * 1 0* (64-bit count of bits processed, MSB-first) | |
679 | */ | |
680 | static void MD5Final(unsigned char digest[16], struct MD5Context *ctx) | |
681 | { | |
682 | unsigned count; | |
683 | unsigned char *p; | |
684 | ||
685 | /* Compute number of bytes mod 64 */ | |
686 | count = (ctx->bits[0] >> 3) & 0x3F; | |
687 | ||
688 | /* Set the first char of padding to 0x80. This is safe since there is | |
689 | always at least one byte free */ | |
690 | p = ctx->in + count; | |
691 | *p++ = 0x80; | |
692 | ||
693 | /* Bytes of padding needed to make 64 bytes */ | |
694 | count = 64 - 1 - count; | |
695 | ||
696 | /* Pad out to 56 mod 64 */ | |
697 | if (count < 8) { | |
698 | /* Two lots of padding: Pad the first block to 64 bytes */ | |
699 | memset(p, 0, count); | |
700 | byteReverse(ctx->in, 16); | |
701 | MD5Transform(ctx->buf, (FcChar32 *) ctx->in); | |
702 | ||
703 | /* Now fill the next block with 56 bytes */ | |
704 | memset(ctx->in, 0, 56); | |
705 | } else { | |
706 | /* Pad block to 56 bytes */ | |
707 | memset(p, 0, count - 8); | |
708 | } | |
709 | byteReverse(ctx->in, 14); | |
710 | ||
711 | /* Append length in bits and transform */ | |
712 | ((FcChar32 *) ctx->in)[14] = ctx->bits[0]; | |
713 | ((FcChar32 *) ctx->in)[15] = ctx->bits[1]; | |
714 | ||
715 | MD5Transform(ctx->buf, (FcChar32 *) ctx->in); | |
716 | byteReverse((unsigned char *) ctx->buf, 4); | |
717 | memcpy(digest, ctx->buf, 16); | |
718 | memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */ | |
719 | } | |
720 | ||
721 | ||
722 | /* The four core functions - F1 is optimized somewhat */ | |
723 | ||
724 | /* #define F1(x, y, z) (x & y | ~x & z) */ | |
725 | #define F1(x, y, z) (z ^ (x & (y ^ z))) | |
726 | #define F2(x, y, z) F1(z, x, y) | |
727 | #define F3(x, y, z) (x ^ y ^ z) | |
728 | #define F4(x, y, z) (y ^ (x | ~z)) | |
729 | ||
730 | /* This is the central step in the MD5 algorithm. */ | |
731 | #define MD5STEP(f, w, x, y, z, data, s) \ | |
732 | ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) | |
733 | ||
734 | /* | |
735 | * The core of the MD5 algorithm, this alters an existing MD5 hash to | |
736 | * reflect the addition of 16 longwords of new data. MD5Update blocks | |
737 | * the data and converts bytes into longwords for this routine. | |
738 | */ | |
739 | static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]) | |
740 | { | |
741 | register FcChar32 a, b, c, d; | |
742 | ||
743 | a = buf[0]; | |
744 | b = buf[1]; | |
745 | c = buf[2]; | |
746 | d = buf[3]; | |
747 | ||
748 | MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); | |
749 | MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); | |
750 | MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); | |
751 | MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); | |
752 | MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); | |
753 | MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); | |
754 | MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); | |
755 | MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); | |
756 | MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); | |
757 | MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); | |
758 | MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); | |
759 | MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); | |
760 | MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); | |
761 | MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); | |
762 | MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); | |
763 | MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); | |
764 | ||
765 | MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); | |
766 | MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); | |
767 | MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); | |
768 | MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); | |
769 | MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); | |
770 | MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); | |
771 | MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); | |
772 | MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); | |
773 | MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); | |
774 | MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); | |
775 | MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); | |
776 | MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); | |
777 | MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); | |
778 | MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); | |
779 | MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); | |
780 | MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); | |
781 | ||
782 | MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); | |
783 | MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); | |
784 | MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); | |
785 | MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); | |
786 | MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); | |
787 | MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); | |
788 | MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); | |
789 | MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); | |
790 | MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); | |
791 | MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); | |
792 | MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); | |
793 | MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); | |
794 | MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); | |
795 | MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); | |
796 | MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); | |
797 | MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); | |
798 | ||
799 | MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); | |
800 | MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); | |
801 | MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); | |
802 | MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); | |
803 | MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); | |
804 | MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); | |
805 | MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); | |
806 | MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); | |
807 | MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); | |
808 | MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); | |
809 | MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); | |
810 | MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); | |
811 | MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); | |
812 | MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); | |
813 | MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); | |
814 | MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); | |
815 | ||
816 | buf[0] += a; | |
817 | buf[1] += b; | |
818 | buf[2] += c; | |
819 | buf[3] += d; | |
820 | } |