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