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