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