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