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