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