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