]>
Commit | Line | Data |
---|---|---|
24330d27 | 1 | /* |
46b51147 | 2 | * Copyright © 2000 Keith Packard |
03a212e5 | 3 | * Copyright © 2005 Patrick Lam |
24330d27 KP |
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 | ||
f045376c | 24 | #include "fcint.h" |
cf65c055 | 25 | #include "../fc-arch/fcarch.h" |
00f059e9 | 26 | #include <stdio.h> |
212c9f43 | 27 | #include <fcntl.h> |
4262e0b3 | 28 | #include <dirent.h> |
ea44e218 | 29 | #include <string.h> |
fd77c154 | 30 | #include <sys/types.h> |
9e612141 | 31 | #include <assert.h> |
d6217cc6 | 32 | #if defined(HAVE_MMAP) || defined(__CYGWIN__) |
93f67dfc | 33 | # include <unistd.h> |
d6217cc6 | 34 | # include <sys/mman.h> |
93f67dfc | 35 | #elif defined(_WIN32) |
8a3dc488 | 36 | # define _WIN32_WINNT 0x0500 |
93f67dfc | 37 | # include <windows.h> |
d6217cc6 | 38 | #endif |
24330d27 | 39 | |
879af706 PL |
40 | #ifndef O_BINARY |
41 | #define O_BINARY 0 | |
42 | #endif | |
43 | ||
ea44e218 PL |
44 | struct MD5Context { |
45 | FcChar32 buf[4]; | |
46 | FcChar32 bits[2]; | |
47 | unsigned char in[64]; | |
48 | }; | |
49 | ||
50 | static void MD5Init(struct MD5Context *ctx); | |
db6f19f1 | 51 | static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len); |
ea44e218 PL |
52 | static void MD5Final(unsigned char digest[16], struct MD5Context *ctx); |
53 | static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]); | |
54 | ||
cf65c055 | 55 | #define CACHEBASE_LEN (1 + 32 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX)) |
7410e40b | 56 | |
8a3dc488 TL |
57 | #ifdef _WIN32 |
58 | ||
59 | #include <windows.h> | |
60 | ||
61 | #ifdef __GNUC__ | |
62 | typedef long long INT64; | |
63 | #define EPOCH_OFFSET 11644473600ll | |
64 | #else | |
65 | #define EPOCH_OFFSET 11644473600i64 | |
66 | typedef __int64 INT64; | |
67 | #endif | |
68 | ||
69 | /* Workaround for problems in the stat() in the Microsoft C library: | |
70 | * | |
71 | * 1) stat() uses FindFirstFile() to get the file | |
72 | * attributes. Unfortunately this API doesn't return correct values | |
73 | * for modification time of a directory until some time after a file | |
74 | * or subdirectory has been added to the directory. (This causes | |
75 | * run-test.sh to fail, for instance.) GetFileAttributesEx() is | |
76 | * better, it returns the updated timestamp right away. | |
77 | * | |
78 | * 2) stat() does some strange things related to backward | |
79 | * compatibility with the local time timestamps on FAT volumes and | |
80 | * daylight saving time. This causes problems after the switches | |
81 | * to/from daylight saving time. See | |
82 | * http://bugzilla.gnome.org/show_bug.cgi?id=154968 , especially | |
83 | * comment #30, and http://www.codeproject.com/datetime/dstbugs.asp . | |
84 | * We don't need any of that, FAT and Win9x are as good as dead. So | |
85 | * just use the UTC timestamps from NTFS, converted to the Unix epoch. | |
86 | */ | |
87 | ||
88 | static int | |
89 | FcStat (const char *file, struct stat *statb) | |
90 | { | |
91 | WIN32_FILE_ATTRIBUTE_DATA wfad; | |
92 | char full_path_name[MAX_PATH]; | |
93 | char *basename; | |
94 | DWORD rc; | |
95 | ||
96 | if (!GetFileAttributesEx (file, GetFileExInfoStandard, &wfad)) | |
97 | return -1; | |
98 | ||
99 | statb->st_dev = 0; | |
100 | ||
101 | /* Calculate a pseudo inode number as a hash of the full path name. | |
102 | * Call GetLongPathName() to get the spelling of the path name as it | |
103 | * is on disk. | |
104 | */ | |
105 | rc = GetFullPathName (file, sizeof (full_path_name), full_path_name, &basename); | |
106 | if (rc == 0 || rc > sizeof (full_path_name)) | |
107 | return -1; | |
108 | ||
109 | rc = GetLongPathName (full_path_name, full_path_name, sizeof (full_path_name)); | |
110 | statb->st_ino = FcStringHash (full_path_name); | |
111 | ||
112 | statb->st_mode = _S_IREAD | _S_IWRITE; | |
113 | statb->st_mode |= (statb->st_mode >> 3) | (statb->st_mode >> 6); | |
114 | ||
115 | if (wfad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) | |
116 | statb->st_mode |= _S_IFDIR; | |
117 | else | |
118 | statb->st_mode |= _S_IFREG; | |
119 | ||
120 | statb->st_nlink = 1; | |
121 | statb->st_uid = statb->st_gid = 0; | |
122 | statb->st_rdev = 0; | |
123 | ||
124 | if (wfad.nFileSizeHigh > 0) | |
125 | return -1; | |
126 | statb->st_size = wfad.nFileSizeLow; | |
127 | ||
128 | statb->st_atime = (*(INT64 *)&wfad.ftLastAccessTime)/10000000 - EPOCH_OFFSET; | |
129 | statb->st_mtime = (*(INT64 *)&wfad.ftLastWriteTime)/10000000 - EPOCH_OFFSET; | |
130 | statb->st_ctime = statb->st_mtime; | |
131 | ||
132 | return 0; | |
133 | } | |
134 | ||
135 | #else | |
136 | ||
137 | #define FcStat stat | |
138 | ||
139 | #endif | |
140 | ||
7410e40b PL |
141 | static const char bin2hex[] = { '0', '1', '2', '3', |
142 | '4', '5', '6', '7', | |
143 | '8', '9', 'a', 'b', | |
144 | 'c', 'd', 'e', 'f' }; | |
145 | ||
146 | static FcChar8 * | |
147 | FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN]) | |
55c8fa4f | 148 | { |
7410e40b PL |
149 | unsigned char hash[16]; |
150 | FcChar8 *hex_hash; | |
151 | int cnt; | |
152 | struct MD5Context ctx; | |
55c8fa4f | 153 | |
7410e40b | 154 | MD5Init (&ctx); |
db6f19f1 | 155 | MD5Update (&ctx, (const unsigned char *)dir, strlen ((const char *) dir)); |
3bfae75d | 156 | |
7410e40b PL |
157 | MD5Final (hash, &ctx); |
158 | ||
159 | cache_base[0] = '/'; | |
160 | hex_hash = cache_base + 1; | |
161 | for (cnt = 0; cnt < 16; ++cnt) | |
1178b569 | 162 | { |
7410e40b PL |
163 | hex_hash[2*cnt ] = bin2hex[hash[cnt] >> 4]; |
164 | hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf]; | |
165 | } | |
166 | hex_hash[2*cnt] = 0; | |
cf65c055 | 167 | strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX); |
6f9fcb51 | 168 | |
7410e40b PL |
169 | return cache_base; |
170 | } | |
3bfae75d | 171 | |
7410e40b PL |
172 | FcBool |
173 | FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config) | |
174 | { | |
7410e40b PL |
175 | FcChar8 *cache_hashed = NULL; |
176 | FcChar8 cache_base[CACHEBASE_LEN]; | |
177 | FcStrList *list; | |
178 | FcChar8 *cache_dir; | |
3bfae75d | 179 | |
7410e40b | 180 | FcDirCacheBasename (dir, cache_base); |
3bfae75d | 181 | |
7410e40b PL |
182 | list = FcStrListCreate (config->cacheDirs); |
183 | if (!list) | |
184 | return FcFalse; | |
185 | ||
186 | while ((cache_dir = FcStrListNext (list))) | |
3bfae75d | 187 | { |
7410e40b PL |
188 | cache_hashed = FcStrPlus (cache_dir, cache_base); |
189 | if (!cache_hashed) | |
190 | break; | |
00f059e9 | 191 | (void) unlink ((char *) cache_hashed); |
3ae9258f | 192 | FcStrFree (cache_hashed); |
1178b569 | 193 | } |
7410e40b PL |
194 | FcStrListDone (list); |
195 | /* return FcFalse if something went wrong */ | |
196 | if (cache_dir) | |
197 | return FcFalse; | |
1b7be377 PL |
198 | return FcTrue; |
199 | } | |
200 | ||
4262e0b3 | 201 | static int |
bc5e487f | 202 | FcDirCacheOpenFile (const FcChar8 *cache_file, struct stat *file_stat) |
4262e0b3 | 203 | { |
bc5e487f | 204 | int fd; |
4262e0b3 | 205 | |
8a3dc488 TL |
206 | #ifdef _WIN32 |
207 | if (FcStat (cache_file, file_stat) < 0) | |
208 | return -1; | |
209 | #endif | |
bc5e487f KP |
210 | fd = open((char *) cache_file, O_RDONLY | O_BINARY); |
211 | if (fd < 0) | |
212 | return fd; | |
8a3dc488 | 213 | #ifndef _WIN32 |
bc5e487f | 214 | if (fstat (fd, file_stat) < 0) |
4262e0b3 | 215 | { |
bc5e487f KP |
216 | close (fd); |
217 | return -1; | |
4262e0b3 | 218 | } |
8a3dc488 | 219 | #endif |
bc5e487f | 220 | return fd; |
4262e0b3 PL |
221 | } |
222 | ||
2d3387fd KP |
223 | /* |
224 | * Look for a cache file for the specified dir. Attempt | |
225 | * to use each one we find, stopping when the callback | |
226 | * indicates success | |
7410e40b | 227 | */ |
2d3387fd KP |
228 | static FcBool |
229 | FcDirCacheProcess (FcConfig *config, const FcChar8 *dir, | |
db6f19f1 KP |
230 | FcBool (*callback) (int fd, struct stat *fd_stat, |
231 | struct stat *dir_stat, void *closure), | |
0a87ce71 | 232 | void *closure, FcChar8 **cache_file_ret) |
ea44e218 | 233 | { |
7ce19673 | 234 | int fd = -1; |
7410e40b PL |
235 | FcChar8 cache_base[CACHEBASE_LEN]; |
236 | FcStrList *list; | |
237 | FcChar8 *cache_dir; | |
d2f78684 | 238 | struct stat file_stat, dir_stat; |
2d3387fd | 239 | FcBool ret = FcFalse; |
d2f78684 | 240 | |
8a3dc488 | 241 | if (FcStat ((char *) dir, &dir_stat) < 0) |
2d3387fd | 242 | return FcFalse; |
df3efc11 | 243 | |
7410e40b | 244 | FcDirCacheBasename (dir, cache_base); |
ea44e218 | 245 | |
7410e40b PL |
246 | list = FcStrListCreate (config->cacheDirs); |
247 | if (!list) | |
2d3387fd | 248 | return FcFalse; |
7410e40b PL |
249 | |
250 | while ((cache_dir = FcStrListNext (list))) | |
ea44e218 | 251 | { |
7ce19673 | 252 | FcChar8 *cache_hashed = FcStrPlus (cache_dir, cache_base); |
7410e40b | 253 | if (!cache_hashed) |
d00c3cb5 | 254 | break; |
bc5e487f | 255 | fd = FcDirCacheOpenFile (cache_hashed, &file_stat); |
7ce19673 | 256 | if (fd >= 0) { |
db6f19f1 KP |
257 | ret = (*callback) (fd, &file_stat, &dir_stat, closure); |
258 | close (fd); | |
259 | if (ret) | |
d2f78684 | 260 | { |
db6f19f1 KP |
261 | if (cache_file_ret) |
262 | *cache_file_ret = cache_hashed; | |
263 | else | |
264 | FcStrFree (cache_hashed); | |
265 | break; | |
d2f78684 | 266 | } |
d2f78684 | 267 | } |
0a87ce71 | 268 | FcStrFree (cache_hashed); |
7410e40b PL |
269 | } |
270 | FcStrListDone (list); | |
7410e40b | 271 | |
2d3387fd | 272 | return ret; |
ea44e218 PL |
273 | } |
274 | ||
9e612141 KP |
275 | #define FC_CACHE_MIN_MMAP 1024 |
276 | ||
277 | /* | |
278 | * Skip list element, make sure the 'next' pointer is the last thing | |
279 | * in the structure, it will be allocated large enough to hold all | |
280 | * of the necessary pointers | |
281 | */ | |
282 | ||
283 | typedef struct _FcCacheSkip FcCacheSkip; | |
284 | ||
285 | struct _FcCacheSkip { | |
286 | FcCache *cache; | |
287 | int ref; | |
288 | intptr_t size; | |
289 | dev_t cache_dev; | |
290 | ino_t cache_ino; | |
291 | time_t cache_mtime; | |
292 | FcCacheSkip *next[1]; | |
293 | }; | |
294 | ||
295 | /* | |
296 | * The head of the skip list; pointers for every possible level | |
297 | * in the skip list, plus the largest level in the list | |
298 | */ | |
299 | ||
300 | #define FC_CACHE_MAX_LEVEL 16 | |
301 | ||
302 | static FcCacheSkip *fcCacheChains[FC_CACHE_MAX_LEVEL]; | |
303 | static int fcCacheMaxLevel; | |
304 | ||
cc104e6a KP |
305 | #if HAVE_RANDOM |
306 | # define FcRandom() random() | |
307 | #else | |
308 | # if HAVE_LRAND48 | |
309 | # define FcRandom() lrand48() | |
310 | # else | |
311 | # if HAVE_RAND | |
312 | # define FcRandom() rand() | |
313 | # endif | |
314 | # endif | |
315 | #endif | |
9e612141 KP |
316 | /* |
317 | * Generate a random level number, distributed | |
318 | * so that each level is 1/4 as likely as the one before | |
319 | * | |
320 | * Note that level numbers run 1 <= level <= MAX_LEVEL | |
321 | */ | |
322 | static int | |
323 | random_level (void) | |
324 | { | |
325 | /* tricky bit -- each bit is '1' 75% of the time */ | |
cc104e6a | 326 | long int bits = FcRandom () | FcRandom (); |
9e612141 KP |
327 | int level = 0; |
328 | ||
329 | while (++level < FC_CACHE_MAX_LEVEL) | |
330 | { | |
331 | if (bits & 1) | |
332 | break; | |
333 | bits >>= 1; | |
334 | } | |
335 | return level; | |
336 | } | |
337 | ||
338 | /* | |
339 | * Insert cache into the list | |
340 | */ | |
341 | static FcBool | |
342 | FcCacheInsert (FcCache *cache, struct stat *cache_stat) | |
343 | { | |
344 | FcCacheSkip **update[FC_CACHE_MAX_LEVEL]; | |
345 | FcCacheSkip *s, **next; | |
346 | int i, level; | |
347 | ||
348 | /* | |
349 | * Find links along each chain | |
350 | */ | |
351 | next = fcCacheChains; | |
352 | for (i = fcCacheMaxLevel; --i >= 0; ) | |
353 | { | |
354 | for (; (s = next[i]); next = s->next) | |
355 | if (s->cache > cache) | |
356 | break; | |
357 | update[i] = &next[i]; | |
358 | } | |
359 | ||
360 | /* | |
361 | * Create new list element | |
362 | */ | |
363 | level = random_level (); | |
364 | if (level > fcCacheMaxLevel) | |
365 | { | |
366 | level = fcCacheMaxLevel + 1; | |
367 | update[fcCacheMaxLevel] = &fcCacheChains[fcCacheMaxLevel]; | |
368 | fcCacheMaxLevel = level; | |
369 | } | |
370 | ||
371 | s = malloc (sizeof (FcCacheSkip) + (level - 1) * sizeof (FcCacheSkip *)); | |
372 | if (!s) | |
373 | return FcFalse; | |
374 | ||
375 | s->cache = cache; | |
376 | s->size = cache->size; | |
377 | s->ref = 1; | |
49b44b27 KP |
378 | if (cache_stat) |
379 | { | |
380 | s->cache_dev = cache_stat->st_dev; | |
381 | s->cache_ino = cache_stat->st_ino; | |
382 | s->cache_mtime = cache_stat->st_mtime; | |
383 | } | |
384 | else | |
385 | { | |
386 | s->cache_dev = 0; | |
387 | s->cache_ino = 0; | |
388 | s->cache_mtime = 0; | |
389 | } | |
9e612141 KP |
390 | |
391 | /* | |
392 | * Insert into all fcCacheChains | |
393 | */ | |
394 | for (i = 0; i < level; i++) | |
395 | { | |
396 | s->next[i] = *update[i]; | |
397 | *update[i] = s; | |
398 | } | |
399 | return FcTrue; | |
400 | } | |
401 | ||
402 | static FcCacheSkip * | |
403 | FcCacheFindByAddr (void *object) | |
404 | { | |
405 | int i; | |
406 | FcCacheSkip **next = fcCacheChains; | |
407 | FcCacheSkip *s; | |
408 | ||
409 | /* | |
410 | * Walk chain pointers one level at a time | |
411 | */ | |
412 | for (i = fcCacheMaxLevel; --i >= 0;) | |
413 | while (next[i] && (char *) object >= ((char *) next[i]->cache + next[i]->size)) | |
414 | next = next[i]->next; | |
415 | /* | |
416 | * Here we are | |
417 | */ | |
418 | s = next[0]; | |
419 | if (s && (char *) object < ((char *) s->cache + s->size)) | |
420 | return s; | |
421 | return NULL; | |
422 | } | |
423 | ||
424 | static void | |
425 | FcCacheRemove (FcCache *cache) | |
426 | { | |
427 | FcCacheSkip **update[FC_CACHE_MAX_LEVEL]; | |
428 | FcCacheSkip *s, **next; | |
429 | int i; | |
430 | ||
431 | /* | |
432 | * Find links along each chain | |
433 | */ | |
434 | next = fcCacheChains; | |
435 | for (i = fcCacheMaxLevel; --i >= 0; ) | |
436 | { | |
437 | for (; (s = next[i]); next = s->next) | |
438 | if (s->cache >= cache) | |
439 | break; | |
440 | update[i] = &next[i]; | |
441 | } | |
442 | s = next[0]; | |
9e612141 KP |
443 | for (i = 0; i < fcCacheMaxLevel && *update[i] == s; i++) |
444 | *update[i] = s->next[i]; | |
445 | while (fcCacheMaxLevel > 0 && fcCacheChains[fcCacheMaxLevel - 1] == NULL) | |
446 | fcCacheMaxLevel--; | |
447 | free (s); | |
448 | } | |
449 | ||
450 | static FcCache * | |
451 | FcCacheFindByStat (struct stat *cache_stat) | |
452 | { | |
453 | FcCacheSkip *s; | |
454 | ||
455 | for (s = fcCacheChains[0]; s; s = s->next[0]) | |
456 | if (s->cache_dev == cache_stat->st_dev && | |
457 | s->cache_ino == cache_stat->st_ino && | |
458 | s->cache_mtime == cache_stat->st_mtime) | |
323ecd0c KP |
459 | { |
460 | s->ref++; | |
9e612141 | 461 | return s->cache; |
323ecd0c | 462 | } |
9e612141 KP |
463 | return NULL; |
464 | } | |
465 | ||
8fe2104a KP |
466 | static void |
467 | FcDirCacheDispose (FcCache *cache) | |
468 | { | |
469 | switch (cache->magic) { | |
470 | case FC_CACHE_MAGIC_ALLOC: | |
471 | free (cache); | |
472 | break; | |
473 | case FC_CACHE_MAGIC_MMAP: | |
474 | #if defined(HAVE_MMAP) || defined(__CYGWIN__) | |
475 | munmap (cache, cache->size); | |
476 | #elif defined(_WIN32) | |
477 | UnmapViewOfFile (cache); | |
478 | #endif | |
479 | break; | |
480 | } | |
9e612141 | 481 | FcCacheRemove (cache); |
8fe2104a KP |
482 | } |
483 | ||
9e612141 KP |
484 | void |
485 | FcCacheObjectReference (void *object) | |
8fe2104a | 486 | { |
9e612141 | 487 | FcCacheSkip *skip = FcCacheFindByAddr (object); |
8fe2104a | 488 | |
9e612141 KP |
489 | if (skip) |
490 | skip->ref++; | |
8fe2104a KP |
491 | } |
492 | ||
9e612141 KP |
493 | void |
494 | FcCacheObjectDereference (void *object) | |
8fe2104a | 495 | { |
9e612141 | 496 | FcCacheSkip *skip = FcCacheFindByAddr (object); |
8fe2104a | 497 | |
9e612141 KP |
498 | if (skip) |
499 | { | |
500 | skip->ref--; | |
501 | if (skip->ref <= 0) | |
502 | FcDirCacheDispose (skip->cache); | |
503 | } | |
8fe2104a KP |
504 | } |
505 | ||
506 | void | |
507 | FcCacheFini (void) | |
508 | { | |
509 | int i; | |
8fe2104a | 510 | |
9e612141 KP |
511 | for (i = 0; i < FC_CACHE_MAX_LEVEL; i++) |
512 | assert (fcCacheChains[i] == NULL); | |
513 | assert (fcCacheMaxLevel == 0); | |
8fe2104a KP |
514 | } |
515 | ||
db6f19f1 KP |
516 | static FcBool |
517 | FcCacheTimeValid (FcCache *cache, struct stat *dir_stat) | |
518 | { | |
519 | struct stat dir_static; | |
520 | ||
521 | if (!dir_stat) | |
522 | { | |
9e2ed251 | 523 | if (FcStat ((const char *) FcCacheDir (cache), &dir_static) < 0) |
db6f19f1 KP |
524 | return FcFalse; |
525 | dir_stat = &dir_static; | |
526 | } | |
527 | if (FcDebug () & FC_DBG_CACHE) | |
528 | printf ("FcCacheTimeValid dir \"%s\" cache time %d dir time %d\n", | |
529 | FcCacheDir (cache), cache->mtime, (int) dir_stat->st_mtime); | |
530 | return cache->mtime == (int) dir_stat->st_mtime; | |
531 | } | |
532 | ||
bc5e487f KP |
533 | /* |
534 | * Map a cache file into memory | |
535 | */ | |
536 | static FcCache * | |
db6f19f1 | 537 | FcDirCacheMapFd (int fd, struct stat *fd_stat, struct stat *dir_stat) |
212c9f43 | 538 | { |
8fe2104a | 539 | FcCache *cache; |
7ce19673 | 540 | FcBool allocated = FcFalse; |
af180c40 | 541 | |
8fe2104a | 542 | if (fd_stat->st_size < sizeof (FcCache)) |
bc5e487f | 543 | return NULL; |
9e612141 | 544 | cache = FcCacheFindByStat (fd_stat); |
8fe2104a KP |
545 | if (cache) |
546 | return cache; | |
bc5e487f | 547 | /* |
144ca878 KP |
548 | * Lage cache files are mmap'ed, smaller cache files are read. This |
549 | * balances the system cost of mmap against per-process memory usage. | |
bc5e487f | 550 | */ |
8fe2104a | 551 | if (fd_stat->st_size >= FC_CACHE_MIN_MMAP) |
bc5e487f | 552 | { |
d6217cc6 | 553 | #if defined(HAVE_MMAP) || defined(__CYGWIN__) |
8fe2104a | 554 | cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0); |
93f67dfc | 555 | #elif defined(_WIN32) |
7ce19673 | 556 | { |
bc5e487f KP |
557 | HANDLE hFileMap; |
558 | ||
559 | cache = NULL; | |
560 | hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL, | |
561 | PAGE_READONLY, 0, 0, NULL); | |
562 | if (hFileMap != NULL) | |
563 | { | |
0596d729 HWN |
564 | cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0, |
565 | fd_stat->st_size); | |
bc5e487f KP |
566 | CloseHandle (hFileMap); |
567 | } | |
93f67dfc | 568 | } |
7ce19673 | 569 | #endif |
bc5e487f | 570 | } |
7ce19673 KP |
571 | if (!cache) |
572 | { | |
8fe2104a | 573 | cache = malloc (fd_stat->st_size); |
7ce19673 | 574 | if (!cache) |
bc5e487f | 575 | return NULL; |
eb0cf671 | 576 | |
8fe2104a | 577 | if (read (fd, cache, fd_stat->st_size) != fd_stat->st_size) |
00f059e9 | 578 | { |
7ce19673 | 579 | free (cache); |
bc5e487f | 580 | return NULL; |
00f059e9 | 581 | } |
7ce19673 KP |
582 | allocated = FcTrue; |
583 | } | |
bc5e487f | 584 | if (cache->magic != FC_CACHE_MAGIC_MMAP || |
3b8a03c0 | 585 | cache->version < FC_CACHE_CONTENT_VERSION || |
8fe2104a | 586 | cache->size != fd_stat->st_size || |
bc5e8adb KP |
587 | !FcCacheTimeValid (cache, dir_stat) || |
588 | !FcCacheInsert (cache, fd_stat)) | |
7ce19673 KP |
589 | { |
590 | if (allocated) | |
591 | free (cache); | |
592 | else | |
593 | { | |
594 | #if defined(HAVE_MMAP) || defined(__CYGWIN__) | |
8fe2104a | 595 | munmap (cache, fd_stat->st_size); |
7ce19673 KP |
596 | #elif defined(_WIN32) |
597 | UnmapViewOfFile (cache); | |
00f059e9 | 598 | #endif |
7ce19673 | 599 | } |
bc5e487f | 600 | return NULL; |
00f059e9 | 601 | } |
af180c40 | 602 | |
7ce19673 KP |
603 | /* Mark allocated caches so they're freed rather than unmapped */ |
604 | if (allocated) | |
bc5e487f | 605 | cache->magic = FC_CACHE_MAGIC_ALLOC; |
7ce19673 | 606 | |
bc5e487f KP |
607 | return cache; |
608 | } | |
609 | ||
17389539 KP |
610 | void |
611 | FcDirCacheReference (FcCache *cache, int nref) | |
612 | { | |
613 | FcCacheSkip *skip = FcCacheFindByAddr (cache); | |
614 | ||
615 | if (skip) | |
616 | skip->ref += nref; | |
617 | } | |
618 | ||
bc5e487f KP |
619 | void |
620 | FcDirCacheUnload (FcCache *cache) | |
621 | { | |
9e612141 | 622 | FcCacheObjectDereference (cache); |
bc5e487f KP |
623 | } |
624 | ||
625 | static FcBool | |
db6f19f1 | 626 | FcDirCacheMapHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure) |
bc5e487f | 627 | { |
db6f19f1 | 628 | FcCache *cache = FcDirCacheMapFd (fd, fd_stat, dir_stat); |
bc5e487f KP |
629 | |
630 | if (!cache) | |
631 | return FcFalse; | |
2d3387fd KP |
632 | *((FcCache **) closure) = cache; |
633 | return FcTrue; | |
634 | } | |
635 | ||
636 | FcCache * | |
bc5e487f | 637 | FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file) |
2d3387fd | 638 | { |
76abb77f | 639 | FcCache *cache = NULL; |
2d3387fd | 640 | |
76abb77f | 641 | if (!FcDirCacheProcess (config, dir, |
bc5e487f | 642 | FcDirCacheMapHelper, |
0a87ce71 | 643 | &cache, cache_file)) |
76abb77f KP |
644 | return NULL; |
645 | return cache; | |
af180c40 KP |
646 | } |
647 | ||
bc5e487f KP |
648 | FcCache * |
649 | FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat) | |
af180c40 | 650 | { |
bc5e487f KP |
651 | int fd; |
652 | FcCache *cache; | |
e37d10fa | 653 | struct stat my_file_stat; |
af180c40 | 654 | |
e37d10fa KP |
655 | if (!file_stat) |
656 | file_stat = &my_file_stat; | |
bc5e487f KP |
657 | fd = FcDirCacheOpenFile (cache_file, file_stat); |
658 | if (fd < 0) | |
659 | return NULL; | |
db6f19f1 | 660 | cache = FcDirCacheMapFd (fd, file_stat, NULL); |
bc5e487f KP |
661 | close (fd); |
662 | return cache; | |
eb0cf671 | 663 | } |
bc5e487f KP |
664 | |
665 | /* | |
666 | * Validate a cache file by reading the header and checking | |
667 | * the magic number and the size field | |
668 | */ | |
2d3387fd | 669 | static FcBool |
db6f19f1 | 670 | FcDirCacheValidateHelper (int fd, struct stat *fd_stat, struct stat *dir_stat, void *closure) |
2d3387fd KP |
671 | { |
672 | FcBool ret = FcTrue; | |
673 | FcCache c; | |
2d3387fd KP |
674 | |
675 | if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache)) | |
676 | ret = FcFalse; | |
bc5e487f KP |
677 | else if (c.magic != FC_CACHE_MAGIC_MMAP) |
678 | ret = FcFalse; | |
3b8a03c0 | 679 | else if (c.version < FC_CACHE_CONTENT_VERSION) |
2d3387fd | 680 | ret = FcFalse; |
8fe2104a | 681 | else if (fd_stat->st_size != c.size) |
2d3387fd | 682 | ret = FcFalse; |
db6f19f1 KP |
683 | else if (c.mtime != (int) dir_stat->st_mtime) |
684 | ret = FcFalse; | |
2d3387fd KP |
685 | return ret; |
686 | } | |
687 | ||
f57783d2 KP |
688 | static FcBool |
689 | FcDirCacheValidConfig (const FcChar8 *dir, FcConfig *config) | |
2d3387fd | 690 | { |
bc5e487f KP |
691 | return FcDirCacheProcess (config, dir, |
692 | FcDirCacheValidateHelper, | |
693 | NULL, NULL); | |
2d3387fd KP |
694 | } |
695 | ||
f57783d2 KP |
696 | FcBool |
697 | FcDirCacheValid (const FcChar8 *dir) | |
698 | { | |
699 | FcConfig *config; | |
700 | ||
701 | config = FcConfigGetCurrent (); | |
702 | if (!config) | |
703 | return FcFalse; | |
704 | ||
705 | return FcDirCacheValidConfig (dir, config); | |
706 | } | |
707 | ||
7ce19673 | 708 | /* |
bc5e487f | 709 | * Build a cache structure from the given contents |
7ce19673 | 710 | */ |
bc5e487f | 711 | FcCache * |
db6f19f1 | 712 | FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcStrSet *dirs) |
eb0cf671 | 713 | { |
7ce19673 KP |
714 | FcSerialize *serialize = FcSerializeCreate (); |
715 | FcCache *cache; | |
716 | int i; | |
717 | intptr_t cache_offset; | |
718 | intptr_t dirs_offset; | |
719 | FcChar8 *dir_serialize; | |
720 | intptr_t *dirs_serialize; | |
721 | FcFontSet *set_serialize; | |
722 | ||
723 | if (!serialize) | |
724 | return NULL; | |
725 | /* | |
726 | * Space for cache structure | |
727 | */ | |
728 | cache_offset = FcSerializeReserve (serialize, sizeof (FcCache)); | |
729 | /* | |
730 | * Directory name | |
731 | */ | |
732 | if (!FcStrSerializeAlloc (serialize, dir)) | |
733 | goto bail1; | |
734 | /* | |
735 | * Subdirs | |
736 | */ | |
737 | dirs_offset = FcSerializeAlloc (serialize, dirs, dirs->num * sizeof (FcChar8 *)); | |
738 | for (i = 0; i < dirs->num; i++) | |
739 | if (!FcStrSerializeAlloc (serialize, dirs->strs[i])) | |
740 | goto bail1; | |
eb0cf671 | 741 | |
7ce19673 KP |
742 | /* |
743 | * Patterns | |
744 | */ | |
745 | if (!FcFontSetSerializeAlloc (serialize, set)) | |
746 | goto bail1; | |
747 | ||
748 | /* Serialize layout complete. Now allocate space and fill it */ | |
749 | cache = malloc (serialize->size); | |
750 | if (!cache) | |
751 | goto bail1; | |
752 | /* shut up valgrind */ | |
753 | memset (cache, 0, serialize->size); | |
d6217cc6 | 754 | |
7ce19673 | 755 | serialize->linear = cache; |
eb0cf671 | 756 | |
bc5e487f KP |
757 | cache->magic = FC_CACHE_MAGIC_ALLOC; |
758 | cache->version = FC_CACHE_CONTENT_VERSION; | |
7ce19673 | 759 | cache->size = serialize->size; |
db6f19f1 | 760 | cache->mtime = (int) dir_stat->st_mtime; |
eb0cf671 | 761 | |
7ce19673 KP |
762 | /* |
763 | * Serialize directory name | |
764 | */ | |
765 | dir_serialize = FcStrSerialize (serialize, dir); | |
766 | if (!dir_serialize) | |
767 | goto bail2; | |
768 | cache->dir = FcPtrToOffset (cache, dir_serialize); | |
769 | ||
770 | /* | |
771 | * Serialize sub dirs | |
772 | */ | |
773 | dirs_serialize = FcSerializePtr (serialize, dirs); | |
774 | if (!dirs_serialize) | |
775 | goto bail2; | |
776 | cache->dirs = FcPtrToOffset (cache, dirs_serialize); | |
777 | cache->dirs_count = dirs->num; | |
778 | for (i = 0; i < dirs->num; i++) | |
8e351527 | 779 | { |
7ce19673 KP |
780 | FcChar8 *d_serialize = FcStrSerialize (serialize, dirs->strs[i]); |
781 | if (!d_serialize) | |
782 | goto bail2; | |
783 | dirs_serialize[i] = FcPtrToOffset (dirs_serialize, d_serialize); | |
8e351527 | 784 | } |
7ce19673 KP |
785 | |
786 | /* | |
787 | * Serialize font set | |
788 | */ | |
789 | set_serialize = FcFontSetSerialize (serialize, set); | |
790 | if (!set_serialize) | |
791 | goto bail2; | |
792 | cache->set = FcPtrToOffset (cache, set_serialize); | |
eb0cf671 | 793 | |
7ce19673 KP |
794 | FcSerializeDestroy (serialize); |
795 | ||
49b44b27 KP |
796 | FcCacheInsert (cache, NULL); |
797 | ||
7ce19673 | 798 | return cache; |
212c9f43 | 799 | |
7ce19673 KP |
800 | bail2: |
801 | free (cache); | |
802 | bail1: | |
803 | FcSerializeDestroy (serialize); | |
804 | return NULL; | |
212c9f43 PL |
805 | } |
806 | ||
1de7a4cc HWN |
807 | |
808 | #ifdef _WIN32 | |
809 | #define mkdir(path,mode) _mkdir(path) | |
810 | #endif | |
811 | ||
7410e40b PL |
812 | static FcBool |
813 | FcMakeDirectory (const FcChar8 *dir) | |
814 | { | |
815 | FcChar8 *parent; | |
816 | FcBool ret; | |
817 | ||
818 | if (strlen ((char *) dir) == 0) | |
819 | return FcFalse; | |
820 | ||
821 | parent = FcStrDirname (dir); | |
822 | if (!parent) | |
823 | return FcFalse; | |
23848903 | 824 | if (access ((char *) parent, F_OK) == 0) |
7410e40b PL |
825 | ret = mkdir ((char *) dir, 0777) == 0; |
826 | else if (access ((char *) parent, F_OK) == -1) | |
827 | ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0777) == 0); | |
828 | else | |
829 | ret = FcFalse; | |
830 | FcStrFree (parent); | |
831 | return ret; | |
832 | } | |
833 | ||
212c9f43 PL |
834 | /* write serialized state to the cache file */ |
835 | FcBool | |
bc5e487f | 836 | FcDirCacheWrite (FcCache *cache, FcConfig *config) |
212c9f43 | 837 | { |
bc5e487f | 838 | FcChar8 *dir = FcCacheDir (cache); |
7410e40b PL |
839 | FcChar8 cache_base[CACHEBASE_LEN]; |
840 | FcChar8 *cache_hashed; | |
7ce19673 | 841 | int fd; |
1d879de2 | 842 | FcAtomic *atomic; |
00f059e9 | 843 | FcStrList *list; |
d2f78684 KP |
844 | FcChar8 *cache_dir = NULL; |
845 | FcChar8 *test_dir; | |
bc5e487f KP |
846 | int magic; |
847 | int written; | |
212c9f43 | 848 | |
7410e40b | 849 | /* |
d2f78684 | 850 | * Write it to the first directory in the list which is writable |
7410e40b | 851 | */ |
d2f78684 KP |
852 | |
853 | list = FcStrListCreate (config->cacheDirs); | |
854 | if (!list) | |
855 | return FcFalse; | |
856 | while ((test_dir = FcStrListNext (list))) { | |
23848903 | 857 | if (access ((char *) test_dir, W_OK) == 0) |
d2f78684 KP |
858 | { |
859 | cache_dir = test_dir; | |
860 | break; | |
861 | } | |
862 | else | |
863 | { | |
7410e40b PL |
864 | /* |
865 | * If the directory doesn't exist, try to create it | |
866 | */ | |
d2f78684 KP |
867 | if (access ((char *) test_dir, F_OK) == -1) { |
868 | if (FcMakeDirectory (test_dir)) | |
869 | { | |
870 | cache_dir = test_dir; | |
7410e40b | 871 | break; |
d2f78684 | 872 | } |
7410e40b PL |
873 | } |
874 | } | |
7410e40b | 875 | } |
d2f78684 KP |
876 | FcStrListDone (list); |
877 | if (!cache_dir) | |
878 | return FcFalse; | |
7ce19673 | 879 | |
d2f78684 KP |
880 | FcDirCacheBasename (dir, cache_base); |
881 | cache_hashed = FcStrPlus (cache_dir, cache_base); | |
882 | if (!cache_hashed) | |
883 | return FcFalse; | |
ea44e218 | 884 | |
4262e0b3 | 885 | if (FcDebug () & FC_DBG_CACHE) |
c1c3ba06 KP |
886 | printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n", |
887 | dir, cache_hashed); | |
4262e0b3 | 888 | |
3bfae75d | 889 | atomic = FcAtomicCreate ((FcChar8 *)cache_hashed); |
1d879de2 | 890 | if (!atomic) |
bc5e487f | 891 | goto bail1; |
212c9f43 | 892 | |
1d879de2 | 893 | if (!FcAtomicLock (atomic)) |
7ce19673 | 894 | goto bail3; |
ec760b17 | 895 | |
879af706 | 896 | fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666); |
1d879de2 | 897 | if (fd == -1) |
7ce19673 | 898 | goto bail4; |
00f059e9 | 899 | |
bc5e487f KP |
900 | /* Temporarily switch magic to MMAP while writing to file */ |
901 | magic = cache->magic; | |
902 | if (magic != FC_CACHE_MAGIC_MMAP) | |
903 | cache->magic = FC_CACHE_MAGIC_MMAP; | |
904 | ||
905 | /* | |
906 | * Write cache contents to file | |
907 | */ | |
908 | written = write (fd, cache, cache->size); | |
909 | ||
910 | /* Switch magic back */ | |
911 | if (magic != FC_CACHE_MAGIC_MMAP) | |
912 | cache->magic = magic; | |
913 | ||
914 | if (written != cache->size) | |
fff5a5af | 915 | { |
7ce19673 | 916 | perror ("write cache"); |
68355f38 PL |
917 | goto bail5; |
918 | } | |
4262e0b3 | 919 | |
212c9f43 | 920 | close(fd); |
1d879de2 | 921 | if (!FcAtomicReplaceOrig(atomic)) |
7ce19673 | 922 | goto bail4; |
bc5e487f | 923 | FcStrFree (cache_hashed); |
1d879de2 PL |
924 | FcAtomicUnlock (atomic); |
925 | FcAtomicDestroy (atomic); | |
212c9f43 PL |
926 | return FcTrue; |
927 | ||
ea44e218 | 928 | bail5: |
1d879de2 | 929 | close (fd); |
7ce19673 | 930 | bail4: |
1d879de2 | 931 | FcAtomicUnlock (atomic); |
7ce19673 | 932 | bail3: |
1d879de2 | 933 | FcAtomicDestroy (atomic); |
ea44e218 | 934 | bail1: |
bc5e487f | 935 | FcStrFree (cache_hashed); |
4262e0b3 PL |
936 | return FcFalse; |
937 | } | |
938 | ||
4984242e KP |
939 | /* |
940 | * Hokey little macro trick to permit the definitions of C functions | |
941 | * with the same name as CPP macros | |
942 | */ | |
c9c68750 KJ |
943 | #define args1(x) (x) |
944 | #define args2(x,y) (x,y) | |
4984242e KP |
945 | |
946 | const FcChar8 * | |
c9c68750 | 947 | FcCacheDir args1(const FcCache *c) |
4984242e KP |
948 | { |
949 | return FcCacheDir (c); | |
950 | } | |
951 | ||
952 | FcFontSet * | |
c9c68750 | 953 | FcCacheCopySet args1(const FcCache *c) |
4984242e KP |
954 | { |
955 | FcFontSet *old = FcCacheSet (c); | |
956 | FcFontSet *new = FcFontSetCreate (); | |
957 | int i; | |
958 | ||
959 | if (!new) | |
960 | return NULL; | |
961 | for (i = 0; i < old->nfont; i++) | |
8d779ce4 KP |
962 | { |
963 | FcPattern *font = FcFontSetFont (old, i); | |
964 | ||
965 | FcPatternReference (font); | |
966 | if (!FcFontSetAdd (new, font)) | |
4984242e KP |
967 | { |
968 | FcFontSetDestroy (new); | |
969 | return NULL; | |
970 | } | |
8d779ce4 | 971 | } |
4984242e KP |
972 | return new; |
973 | } | |
974 | ||
975 | const FcChar8 * | |
c9c68750 | 976 | FcCacheSubdir args2(const FcCache *c, int i) |
4984242e KP |
977 | { |
978 | return FcCacheSubdir (c, i); | |
979 | } | |
980 | ||
981 | int | |
c9c68750 | 982 | FcCacheNumSubdir args1(const FcCache *c) |
4984242e KP |
983 | { |
984 | return c->dirs_count; | |
985 | } | |
986 | ||
987 | int | |
c9c68750 | 988 | FcCacheNumFont args1(const FcCache *c) |
4984242e KP |
989 | { |
990 | return FcCacheSet(c)->nfont; | |
991 | } | |
992 | ||
ea44e218 PL |
993 | /* |
994 | * This code implements the MD5 message-digest algorithm. | |
995 | * The algorithm is due to Ron Rivest. This code was | |
996 | * written by Colin Plumb in 1993, no copyright is claimed. | |
997 | * This code is in the public domain; do with it what you wish. | |
998 | * | |
999 | * Equivalent code is available from RSA Data Security, Inc. | |
1000 | * This code has been tested against that, and is equivalent, | |
1001 | * except that you don't need to include two pages of legalese | |
1002 | * with every copy. | |
1003 | * | |
1004 | * To compute the message digest of a chunk of bytes, declare an | |
1005 | * MD5Context structure, pass it to MD5Init, call MD5Update as | |
1006 | * needed on buffers full of bytes, and then call MD5Final, which | |
1007 | * will fill a supplied 16-byte array with the digest. | |
1008 | */ | |
1009 | ||
1010 | #ifndef HIGHFIRST | |
1011 | #define byteReverse(buf, len) /* Nothing */ | |
1012 | #else | |
1013 | /* | |
1014 | * Note: this code is harmless on little-endian machines. | |
1015 | */ | |
1016 | void byteReverse(unsigned char *buf, unsigned longs) | |
1017 | { | |
1018 | FcChar32 t; | |
1019 | do { | |
1020 | t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 | | |
1021 | ((unsigned) buf[1] << 8 | buf[0]); | |
1022 | *(FcChar32 *) buf = t; | |
1023 | buf += 4; | |
1024 | } while (--longs); | |
1025 | } | |
1026 | #endif | |
1027 | ||
1028 | /* | |
1029 | * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious | |
1030 | * initialization constants. | |
1031 | */ | |
1032 | static void MD5Init(struct MD5Context *ctx) | |
1033 | { | |
1034 | ctx->buf[0] = 0x67452301; | |
1035 | ctx->buf[1] = 0xefcdab89; | |
1036 | ctx->buf[2] = 0x98badcfe; | |
1037 | ctx->buf[3] = 0x10325476; | |
1038 | ||
1039 | ctx->bits[0] = 0; | |
1040 | ctx->bits[1] = 0; | |
1041 | } | |
1042 | ||
1043 | /* | |
1044 | * Update context to reflect the concatenation of another buffer full | |
1045 | * of bytes. | |
1046 | */ | |
db6f19f1 | 1047 | static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len) |
ea44e218 PL |
1048 | { |
1049 | FcChar32 t; | |
1050 | ||
1051 | /* Update bitcount */ | |
1052 | ||
1053 | t = ctx->bits[0]; | |
1054 | if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t) | |
1055 | ctx->bits[1]++; /* Carry from low to high */ | |
1056 | ctx->bits[1] += len >> 29; | |
1057 | ||
1058 | t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ | |
1059 | ||
1060 | /* Handle any leading odd-sized chunks */ | |
1061 | ||
1062 | if (t) { | |
1063 | unsigned char *p = (unsigned char *) ctx->in + t; | |
1064 | ||
1065 | t = 64 - t; | |
1066 | if (len < t) { | |
1067 | memcpy(p, buf, len); | |
1068 | return; | |
1069 | } | |
1070 | memcpy(p, buf, t); | |
1071 | byteReverse(ctx->in, 16); | |
1072 | MD5Transform(ctx->buf, (FcChar32 *) ctx->in); | |
1073 | buf += t; | |
1074 | len -= t; | |
1075 | } | |
1076 | /* Process data in 64-byte chunks */ | |
1077 | ||
1078 | while (len >= 64) { | |
1079 | memcpy(ctx->in, buf, 64); | |
1080 | byteReverse(ctx->in, 16); | |
1081 | MD5Transform(ctx->buf, (FcChar32 *) ctx->in); | |
1082 | buf += 64; | |
1083 | len -= 64; | |
1084 | } | |
1085 | ||
1086 | /* Handle any remaining bytes of data. */ | |
1087 | ||
1088 | memcpy(ctx->in, buf, len); | |
1089 | } | |
1090 | ||
1091 | /* | |
1092 | * Final wrapup - pad to 64-byte boundary with the bit pattern | |
1093 | * 1 0* (64-bit count of bits processed, MSB-first) | |
1094 | */ | |
1095 | static void MD5Final(unsigned char digest[16], struct MD5Context *ctx) | |
1096 | { | |
1097 | unsigned count; | |
1098 | unsigned char *p; | |
1099 | ||
1100 | /* Compute number of bytes mod 64 */ | |
1101 | count = (ctx->bits[0] >> 3) & 0x3F; | |
1102 | ||
1103 | /* Set the first char of padding to 0x80. This is safe since there is | |
1104 | always at least one byte free */ | |
1105 | p = ctx->in + count; | |
1106 | *p++ = 0x80; | |
1107 | ||
1108 | /* Bytes of padding needed to make 64 bytes */ | |
1109 | count = 64 - 1 - count; | |
1110 | ||
1111 | /* Pad out to 56 mod 64 */ | |
1112 | if (count < 8) { | |
1113 | /* Two lots of padding: Pad the first block to 64 bytes */ | |
1114 | memset(p, 0, count); | |
1115 | byteReverse(ctx->in, 16); | |
1116 | MD5Transform(ctx->buf, (FcChar32 *) ctx->in); | |
1117 | ||
1118 | /* Now fill the next block with 56 bytes */ | |
1119 | memset(ctx->in, 0, 56); | |
1120 | } else { | |
1121 | /* Pad block to 56 bytes */ | |
1122 | memset(p, 0, count - 8); | |
1123 | } | |
1124 | byteReverse(ctx->in, 14); | |
1125 | ||
1126 | /* Append length in bits and transform */ | |
1127 | ((FcChar32 *) ctx->in)[14] = ctx->bits[0]; | |
1128 | ((FcChar32 *) ctx->in)[15] = ctx->bits[1]; | |
1129 | ||
1130 | MD5Transform(ctx->buf, (FcChar32 *) ctx->in); | |
1131 | byteReverse((unsigned char *) ctx->buf, 4); | |
1132 | memcpy(digest, ctx->buf, 16); | |
1133 | memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */ | |
1134 | } | |
1135 | ||
1136 | ||
1137 | /* The four core functions - F1 is optimized somewhat */ | |
1138 | ||
1139 | /* #define F1(x, y, z) (x & y | ~x & z) */ | |
1140 | #define F1(x, y, z) (z ^ (x & (y ^ z))) | |
1141 | #define F2(x, y, z) F1(z, x, y) | |
1142 | #define F3(x, y, z) (x ^ y ^ z) | |
1143 | #define F4(x, y, z) (y ^ (x | ~z)) | |
1144 | ||
1145 | /* This is the central step in the MD5 algorithm. */ | |
1146 | #define MD5STEP(f, w, x, y, z, data, s) \ | |
1147 | ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) | |
1148 | ||
1149 | /* | |
1150 | * The core of the MD5 algorithm, this alters an existing MD5 hash to | |
1151 | * reflect the addition of 16 longwords of new data. MD5Update blocks | |
1152 | * the data and converts bytes into longwords for this routine. | |
1153 | */ | |
1154 | static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]) | |
1155 | { | |
1156 | register FcChar32 a, b, c, d; | |
1157 | ||
1158 | a = buf[0]; | |
1159 | b = buf[1]; | |
1160 | c = buf[2]; | |
1161 | d = buf[3]; | |
1162 | ||
1163 | MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); | |
1164 | MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); | |
1165 | MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); | |
1166 | MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); | |
1167 | MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); | |
1168 | MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); | |
1169 | MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); | |
1170 | MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); | |
1171 | MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); | |
1172 | MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); | |
1173 | MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); | |
1174 | MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); | |
1175 | MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); | |
1176 | MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); | |
1177 | MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); | |
1178 | MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); | |
1179 | ||
1180 | MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); | |
1181 | MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); | |
1182 | MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); | |
1183 | MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); | |
1184 | MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); | |
1185 | MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); | |
1186 | MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); | |
1187 | MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); | |
1188 | MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); | |
1189 | MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); | |
1190 | MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); | |
1191 | MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); | |
1192 | MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); | |
1193 | MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); | |
1194 | MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); | |
1195 | MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); | |
1196 | ||
1197 | MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); | |
1198 | MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); | |
1199 | MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); | |
1200 | MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); | |
1201 | MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); | |
1202 | MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); | |
1203 | MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); | |
1204 | MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); | |
1205 | MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); | |
1206 | MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); | |
1207 | MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); | |
1208 | MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); | |
1209 | MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); | |
1210 | MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); | |
1211 | MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); | |
1212 | MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); | |
1213 | ||
1214 | MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); | |
1215 | MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); | |
1216 | MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); | |
1217 | MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); | |
1218 | MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); | |
1219 | MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); | |
1220 | MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); | |
1221 | MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); | |
1222 | MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); | |
1223 | MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); | |
1224 | MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); | |
1225 | MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); | |
1226 | MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); | |
1227 | MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); | |
1228 | MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); | |
1229 | MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); | |
1230 | ||
1231 | buf[0] += a; | |
1232 | buf[1] += b; | |
1233 | buf[2] += c; | |
1234 | buf[3] += d; | |
1235 | } | |
23816bf9 KP |
1236 | #define __fccache__ |
1237 | #include "fcaliastail.h" | |
1238 | #undef __fccache__ |