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