Migrate cache files from fonts directories to /var/cache/fontconfig. This

helps make fontconfig FHS-compliant, but requires that all caches get
    rebuilt.
Also, autogen.sh now needs the additional parameter
--localstatedir=/var.
This commit is contained in:
Patrick Lam 2005-12-09 16:36:45 +00:00
parent 204da5a8b8
commit ea44e21841
9 changed files with 519 additions and 53 deletions

View File

@ -1,3 +1,23 @@
2005-12-09 Patrick Lam <plam@mit.edu>
* INSTALL:
* configure.in:
* fc-cache/Makefile.am:
* fc-cat/Makefile.am:
* src/Makefile.am:
* fc-cat/fc-cat.c (FcCacheGlobalFileReadAndPrint, FcCacheFileRead):
* fontconfig/fontconfig.h (FC_CACHE_SUFFIX):
* src/fccache.c (FcCacheSkipString, FcGlobalCacheLoad,
FcGlobalCacheSave, FcCacheNextOffset, FcDirCacheValid,
FcDirCacheHasCurrentArch, FcDirCacheHashName,
FcDirCacheOpen, FcDirCacheRead, FcDirCacheWrite):
Migrate cache files from fonts directories to
/var/cache/fontconfig. This helps make fontconfig FHS-compliant,
but requires that all caches get rebuilt.
Also, autogen.sh now needs the additional parameter
--localstatedir=/var.
2005-12-07 Patrick Lam <plam@mit.edu> 2005-12-07 Patrick Lam <plam@mit.edu>
* src/fcpat.c (FcPatternAddString): * src/fcpat.c (FcPatternAddString):

View File

@ -18,7 +18,7 @@ important steps:
Set the date Set the date
3. Commit those changes 3. Commit those changes
4. rebuild the configuration files with autogen.sh 4. rebuild the configuration files with autogen.sh
sh autogen.sh --sysconfdir=/etc --prefix=/usr --mandir=/usr/share/man sh autogen.sh --sysconfdir=/etc --prefix=/usr --mandir=/usr/share/man --localstatedir=/var
5. make distcheck (NB: won't work without docbook-utils -PL) 5. make distcheck (NB: won't work without docbook-utils -PL)
6. tag the tree 6. tag the tree
cvs tag fc-2_2_xx cvs tag fc-2_2_xx

View File

@ -479,6 +479,13 @@ fi
AC_SUBST(DOCDIR) AC_SUBST(DOCDIR)
#
# Make /var/cache/fontconfig directory available to source code
#
pkgcachedir='${localstatedir}/cache/'${PACKAGE}
AC_SUBST(pkgcachedir)
AC_OUTPUT([ AC_OUTPUT([
Makefile Makefile
fontconfig/Makefile fontconfig/Makefile

View File

@ -27,6 +27,8 @@ FC_CACHE_SRC=${top_srcdir}/fc-cache
SGML = ${FC_CACHE_SRC}/fc-cache.sgml SGML = ${FC_CACHE_SRC}/fc-cache.sgml
AM_CPPFLAGS = -DPKGCACHEDIR='"${pkgcachedir}"'
INCLUDES=-I${top_srcdir} $(FREETYPE_CFLAGS) INCLUDES=-I${top_srcdir} $(FREETYPE_CFLAGS)
bin_PROGRAMS=fc-cache bin_PROGRAMS=fc-cache

View File

@ -27,6 +27,8 @@ FC_CAT_SRC=${top_srcdir}/fc-cat
SGML = ${FC_CAT_SRC}/fc-cat.sgml SGML = ${FC_CAT_SRC}/fc-cat.sgml
AM_CPPFLAGS = -DPKGCACHEDIR='"${pkgcachedir}"'
INCLUDES=-I${top_srcdir} $(FREETYPE_CFLAGS) INCLUDES=-I${top_srcdir} $(FREETYPE_CFLAGS)
bin_PROGRAMS=fc-cat bin_PROGRAMS=fc-cat

View File

@ -184,7 +184,8 @@ FcCacheGlobalFileReadAndPrint (FcFontSet * set, FcStrSet *dirs, char * dir, char
if (fd == -1) if (fd == -1)
goto bail; goto bail;
current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name); current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name,
FcTrue);
if (current_arch_start < 0) if (current_arch_start < 0)
goto bail1; goto bail1;
@ -225,6 +226,7 @@ FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char * dir, char *cache_file)
char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE]; char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
off_t current_arch_start = 0; off_t current_arch_start = 0;
char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1]; char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
char name_buf[8192];
if (!cache_file) if (!cache_file)
goto bail; goto bail;
@ -234,7 +236,14 @@ FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char * dir, char *cache_file)
if (fd == -1) if (fd == -1)
goto bail; goto bail;
current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name); FcCacheReadString (fd, name_buf, sizeof (name_buf));
if (!strlen (name_buf))
goto bail;
printf ("fc-cat: printing directory cache contents for dir %s\n",
name_buf);
current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name,
FcFalse);
if (current_arch_start < 0) if (current_arch_start < 0)
goto bail1; goto bail1;

View File

@ -104,6 +104,7 @@ typedef int FcBool;
#define FC_EMBOLDEN "embolden" /* Bool - true if emboldening needed*/ #define FC_EMBOLDEN "embolden" /* Bool - true if emboldening needed*/
#define FC_EMBEDDED_BITMAP "embeddedbitmap" /* Bool - true to enable embedded bitmaps */ #define FC_EMBEDDED_BITMAP "embeddedbitmap" /* Bool - true to enable embedded bitmaps */
#define FC_CACHE_SUFFIX ".cache-"FC_CACHE_VERSION
#define FC_DIR_CACHE_FILE "fonts.cache-"FC_CACHE_VERSION #define FC_DIR_CACHE_FILE "fonts.cache-"FC_CACHE_VERSION
#define FC_USER_CACHE_FILE ".fonts.cache-"FC_CACHE_VERSION #define FC_USER_CACHE_FILE ".fonts.cache-"FC_CACHE_VERSION

View File

@ -63,6 +63,8 @@ uninstall-ms-import-lib:
endif endif
AM_CPPFLAGS = -DPKGCACHEDIR='"${pkgcachedir}"'
INCLUDES = \ INCLUDES = \
$(FREETYPE_CFLAGS) \ $(FREETYPE_CFLAGS) \
$(LIBXML2_CFLAGS) \ $(LIBXML2_CFLAGS) \

View File

@ -25,6 +25,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <dirent.h> #include <dirent.h>
#include <string.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/utsname.h> #include <sys/utsname.h>
#include <sys/types.h> #include <sys/types.h>
@ -35,8 +36,14 @@
#define ENDIAN_TEST 0x12345678 #define ENDIAN_TEST 0x12345678
#define MACHINE_SIGNATURE_SIZE 9 + 5*20 + 1 #define MACHINE_SIGNATURE_SIZE 9 + 5*20 + 1
static int
FcDirCacheOpen (char * cache_file);
static char *
FcDirCacheHashName (char * cache_file, int collisions);
static off_t static off_t
FcCacheSkipToArch (int fd, const char * arch); FcCacheSkipToArch (int fd, const char * arch, FcBool global);
static FcBool static FcBool
FcCacheCopyOld (int fd, int fd_orig, off_t start); FcCacheCopyOld (int fd, int fd_orig, off_t start);
@ -62,6 +69,17 @@ FcCacheHaveBank (int bank);
static void static void
FcCacheAddBankDir (int bank, const char * dir); FcCacheAddBankDir (int bank, const char * dir);
struct MD5Context {
FcChar32 buf[4];
FcChar32 bits[2];
unsigned char in[64];
};
static void MD5Init(struct MD5Context *ctx);
static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len);
static void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]);
#define FC_DBG_CACHE_REF 1024 #define FC_DBG_CACHE_REF 1024
static char * static char *
@ -104,6 +122,33 @@ FcCacheReadString (int fd, char *dest, int len)
return 0; return 0;
} }
static void
FcCacheSkipString (int fd)
{
FcChar8 c;
FcBool escape;
escape = FcFalse;
while (read (fd, &c, 1) == 1)
{
if (!escape)
{
switch (c) {
case '"':
c = '\0';
break;
case '\\':
escape = FcTrue;
continue;
}
}
if (c == '\0')
return;
escape = FcFalse;
}
return;
}
static FcBool static FcBool
FcCacheWriteString (int fd, const char *chars) FcCacheWriteString (int fd, const char *chars)
{ {
@ -156,7 +201,7 @@ FcGlobalCacheLoad (FcGlobalCache *cache,
const FcChar8 *cache_file, const FcChar8 *cache_file,
FcConfig *config) FcConfig *config)
{ {
char name_buf[8192]; char name_buf[FC_MAX_FILE_LEN];
FcGlobalCacheDir *d, *next; FcGlobalCacheDir *d, *next;
FcFileTime config_time = FcConfigModifiedTime (config); FcFileTime config_time = FcConfigModifiedTime (config);
char * current_arch_machine_name; char * current_arch_machine_name;
@ -176,7 +221,7 @@ FcGlobalCacheLoad (FcGlobalCache *cache,
current_arch_machine_name = FcCacheMachineSignature (); current_arch_machine_name = FcCacheMachineSignature ();
current_arch_start = FcCacheSkipToArch(cache->fd, current_arch_start = FcCacheSkipToArch(cache->fd,
current_arch_machine_name); current_arch_machine_name, FcTrue);
if (current_arch_start < 0) if (current_arch_start < 0)
goto bail_and_destroy; goto bail_and_destroy;
@ -343,7 +388,8 @@ FcGlobalCacheSave (FcGlobalCache *cache,
current_arch_start = 0; current_arch_start = 0;
else else
current_arch_start = FcCacheSkipToArch (fd_orig, current_arch_start = FcCacheSkipToArch (fd_orig,
current_arch_machine_name); current_arch_machine_name,
FcTrue);
if (current_arch_start < 0) if (current_arch_start < 0)
current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END)); current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
@ -437,12 +483,17 @@ FcCacheNextOffset(off_t w)
/* return the address of the segment for the provided arch, /* return the address of the segment for the provided arch,
* or -1 if arch not found */ * or -1 if arch not found */
static off_t static off_t
FcCacheSkipToArch (int fd, const char * arch) FcCacheSkipToArch (int fd, const char * arch, FcBool global)
{ {
char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9]; char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
char * candidate_arch; char * candidate_arch;
off_t current_arch_start = 0; off_t current_arch_start = 0;
lseek (fd, 0, SEEK_SET);
if (!global)
FcCacheSkipString (fd);
current_arch_start = lseek (fd, 0, SEEK_CUR);
/* skip arches that are not the current arch */ /* skip arches that are not the current arch */
while (1) while (1)
{ {
@ -542,21 +593,27 @@ FcCacheCopyOld (int fd, int fd_orig, off_t start)
FcBool FcBool
FcDirCacheValid (const FcChar8 *dir) FcDirCacheValid (const FcChar8 *dir)
{ {
FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE); FcChar8 *cache_file;
struct stat file_stat, dir_stat; struct stat file_stat, dir_stat;
int fd;
if (stat ((char *) dir, &dir_stat) < 0) if (stat ((char *) dir, &dir_stat) < 0)
{
FcStrFree (cache_file);
return FcFalse; return FcFalse;
}
if (stat ((char *) cache_file, &file_stat) < 0)
{
FcStrFree (cache_file);
return FcFalse;
}
cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
if (!cache_file)
return FcFalse;
fd = FcDirCacheOpen ((char *)cache_file);
if (fd < 0)
goto bail;
if (fstat (fd, &file_stat) < 0)
goto bail1;
close (fd);
FcStrFree (cache_file); FcStrFree (cache_file);
/* /*
* If the directory has been modified more recently than * If the directory has been modified more recently than
* the cache file, the cache is not valid * the cache file, the cache is not valid
@ -565,6 +622,12 @@ FcDirCacheValid (const FcChar8 *dir)
return FcFalse; return FcFalse;
return FcTrue; return FcTrue;
bail1:
close (fd);
bail:
FcStrFree (cache_file);
return FcFalse;
} }
/* Assumes that the cache file in 'dir' exists. /* Assumes that the cache file in 'dir' exists.
@ -572,23 +635,32 @@ FcDirCacheValid (const FcChar8 *dir)
FcBool FcBool
FcDirCacheHasCurrentArch (const FcChar8 *dir) FcDirCacheHasCurrentArch (const FcChar8 *dir)
{ {
FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE); char *cache_file;
int fd; int fd;
off_t current_arch_start; off_t current_arch_start;
char *current_arch_machine_name; char *current_arch_machine_name;
current_arch_machine_name = FcCacheMachineSignature(); cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
fd = open ((char *)cache_file, O_RDONLY); if (!cache_file)
if (fd == -1)
return FcFalse; return FcFalse;
current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name); fd = FcDirCacheOpen (cache_file);
if (fd < 0)
goto bail;
current_arch_machine_name = FcCacheMachineSignature();
current_arch_start = FcCacheSkipToArch(fd,
current_arch_machine_name, FcFalse);
close (fd); close (fd);
if (current_arch_start < 0) if (current_arch_start < 0)
return FcFalse; return FcFalse;
return FcTrue; return FcTrue;
bail:
free (cache_file);
return FcFalse;
} }
FcBool FcBool
@ -718,26 +790,101 @@ FcCacheRead (FcConfig *config, FcGlobalCache * cache)
return 0; return 0;
} }
static const char bin2hex[] = { '0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f' };
static char *
FcDirCacheHashName (char * cache_file, int collisions)
{
unsigned char hash[16], hex_hash[33];
char *cache_hashed;
unsigned char uscore = '_';
int cnt, i;
FcChar8 *tmp;
struct MD5Context ctx;
MD5Init (&ctx);
MD5Update (&ctx, (unsigned char *)cache_file, strlen (cache_file));
for (i = 0; i < collisions; i++)
MD5Update (&ctx, &uscore, 1);
MD5Final (hash, &ctx);
for (cnt = 0; cnt < 16; ++cnt)
{
hex_hash[2*cnt] = bin2hex[hash[cnt] >> 4];
hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
}
hex_hash[32] = 0;
tmp = FcStrPlus ((FcChar8 *)hex_hash, (FcChar8 *)FC_CACHE_SUFFIX);
if (!tmp)
return 0;
cache_hashed = (char *)FcStrPlus ((FcChar8 *)PKGCACHEDIR, tmp);
free (tmp);
return cache_hashed;
}
/* Opens the hashed name for cache_file.
* This would fail in the unlikely event of a collision and subsequent
* removal of the file which originally caused the collision. */
static int
FcDirCacheOpen (char *cache_file)
{
int fd = -1, collisions = 0;
char *cache_hashed;
char name_buf[FC_MAX_FILE_LEN];
do
{
cache_hashed = FcDirCacheHashName (cache_file, collisions++);
if (!cache_hashed)
return -1;
if (fd > 0)
close (fd);
fd = open(cache_hashed, O_RDONLY);
if (fd == -1)
return -1;
FcCacheReadString (fd, name_buf, sizeof (name_buf));
if (!strlen(name_buf))
goto bail;
} while (strcmp (name_buf, cache_file) != 0);
return fd;
bail:
close (fd);
return -1;
}
/* read serialized state from the cache file */ /* read serialized state from the cache file */
FcBool FcBool
FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir) FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir)
{ {
char *cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE); char *cache_file;
int fd; int fd;
char * current_arch_machine_name; char *current_arch_machine_name;
char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE]; char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
off_t current_arch_start = 0; off_t current_arch_start = 0;
char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1]; char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
if (!cache_file) if (!cache_file)
return FcFalse;
fd = FcDirCacheOpen (cache_file);
if (fd < 0)
goto bail; goto bail;
current_arch_machine_name = FcCacheMachineSignature(); current_arch_machine_name = FcCacheMachineSignature();
fd = open(cache_file, O_RDONLY); current_arch_start = FcCacheSkipToArch(fd,
if (fd == -1) current_arch_machine_name,
goto bail; FcFalse);
current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
if (current_arch_start < 0) if (current_arch_start < 0)
goto bail1; goto bail1;
@ -844,58 +991,89 @@ FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
FcBool FcBool
FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir) FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
{ {
FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE); char *cache_file;
char *cache_hashed;
int fd, fd_orig, i, dirs_count; int fd, fd_orig, i, dirs_count;
FcAtomic *atomic; FcAtomic *atomic;
FcCache metadata; FcCache metadata;
off_t current_arch_start = 0, truncate_to; off_t current_arch_start = 0, truncate_to;
char name_buf[FC_MAX_FILE_LEN];
int collisions;
char *current_arch_machine_name, * header; char *current_arch_machine_name, * header;
void *current_dir_block; void *current_dir_block = 0;
cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
if (!cache_file) if (!cache_file)
goto bail; goto bail;
/* Ensure that we're not trampling a cache for some other dir. */
/* This is slightly different from FcDirCacheOpen, since it
* needs the filename, not the file descriptor. */
fd = -1; collisions = 0;
do
{
cache_hashed = FcDirCacheHashName (cache_file, collisions++);
if (!cache_hashed)
goto bail0;
if (fd > 0)
close (fd);
fd = open(cache_hashed, O_RDONLY);
if (fd == -1)
break;
FcCacheReadString (fd, name_buf, sizeof (name_buf));
close (fd);
if (!strlen(name_buf))
break;
if (strcmp (name_buf, cache_file) != 0)
continue;
} while (0);
current_dir_block = FcDirCacheProduce (set, &metadata); current_dir_block = FcDirCacheProduce (set, &metadata);
if (metadata.count && !current_dir_block) if (metadata.count && !current_dir_block)
goto bail0; goto bail1;
if (FcDebug () & FC_DBG_CACHE) if (FcDebug () & FC_DBG_CACHE)
printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file); printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
atomic = FcAtomicCreate (cache_file); atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
if (!atomic) if (!atomic)
goto bail0; goto bail1;
if (!FcAtomicLock (atomic)) if (!FcAtomicLock (atomic))
goto bail1; goto bail2;
fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY, 0666); fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY, 0666);
fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666); fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
if (fd == -1) if (fd == -1)
goto bail2; goto bail3;
FcCacheWriteString (fd, cache_file);
current_arch_machine_name = FcCacheMachineSignature (); current_arch_machine_name = FcCacheMachineSignature ();
current_arch_start = 0; current_arch_start = 0;
if (fd_orig != -1) if (fd_orig != -1)
current_arch_start = current_arch_start =
FcCacheSkipToArch(fd_orig, current_arch_machine_name); FcCacheSkipToArch(fd_orig, current_arch_machine_name, FcFalse);
if (current_arch_start < 0) if (current_arch_start < 0)
current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END)); current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start)) if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
goto bail3; goto bail4;
if (fd_orig != -1) if (fd_orig != -1)
close (fd_orig); close (fd_orig);
current_arch_start = lseek(fd, 0, SEEK_CUR); current_arch_start = lseek(fd, 0, SEEK_CUR);
if (ftruncate (fd, current_arch_start) == -1) if (ftruncate (fd, current_arch_start) == -1)
goto bail3; goto bail4;
/* allocate space for subdir names in this block */ /* allocate space for subdir names in this block */
dirs_count = 0; dirs_count = 0;
@ -907,11 +1085,11 @@ FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start; truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start;
header = malloc (10 + strlen (current_arch_machine_name)); header = malloc (10 + strlen (current_arch_machine_name));
if (!header) if (!header)
goto bail3; goto bail4;
sprintf (header, "%8x ", (int)truncate_to); sprintf (header, "%8x ", (int)truncate_to);
strcat (header, current_arch_machine_name); strcat (header, current_arch_machine_name);
if (!FcCacheWriteString (fd, header)) if (!FcCacheWriteString (fd, header))
goto bail4; goto bail5;
for (i = 0; i < dirs->size; i++) for (i = 0; i < dirs->size; i++)
FcCacheWriteString (fd, (char *)dirs->strs[i]); FcCacheWriteString (fd, (char *)dirs->strs[i]);
@ -927,23 +1105,25 @@ FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
/* this actually serves to pad out the cache file, if needed */ /* this actually serves to pad out the cache file, if needed */
if (ftruncate (fd, current_arch_start + truncate_to) == -1) if (ftruncate (fd, current_arch_start + truncate_to) == -1)
goto bail4; goto bail5;
close(fd); close(fd);
if (!FcAtomicReplaceOrig(atomic)) if (!FcAtomicReplaceOrig(atomic))
goto bail4; goto bail5;
FcAtomicUnlock (atomic); FcAtomicUnlock (atomic);
FcAtomicDestroy (atomic); FcAtomicDestroy (atomic);
return FcTrue; return FcTrue;
bail4: bail5:
free (header); free (header);
bail3: bail4:
close (fd); close (fd);
bail2: bail3:
FcAtomicUnlock (atomic); FcAtomicUnlock (atomic);
bail1: bail2:
FcAtomicDestroy (atomic); FcAtomicDestroy (atomic);
bail1:
free (cache_hashed);
bail0: bail0:
unlink ((char *)cache_file); unlink ((char *)cache_file);
free (cache_file); free (cache_file);
@ -1070,3 +1250,246 @@ FcCacheFindBankDir (int bank)
return bankDirs[bi]; return bankDirs[bi];
} }
/*
* This code implements the MD5 message-digest algorithm.
* The algorithm is due to Ron Rivest. This code was
* written by Colin Plumb in 1993, no copyright is claimed.
* This code is in the public domain; do with it what you wish.
*
* Equivalent code is available from RSA Data Security, Inc.
* This code has been tested against that, and is equivalent,
* except that you don't need to include two pages of legalese
* with every copy.
*
* To compute the message digest of a chunk of bytes, declare an
* MD5Context structure, pass it to MD5Init, call MD5Update as
* needed on buffers full of bytes, and then call MD5Final, which
* will fill a supplied 16-byte array with the digest.
*/
#ifndef HIGHFIRST
#define byteReverse(buf, len) /* Nothing */
#else
/*
* Note: this code is harmless on little-endian machines.
*/
void byteReverse(unsigned char *buf, unsigned longs)
{
FcChar32 t;
do {
t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
((unsigned) buf[1] << 8 | buf[0]);
*(FcChar32 *) buf = t;
buf += 4;
} while (--longs);
}
#endif
/*
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
* initialization constants.
*/
static void MD5Init(struct MD5Context *ctx)
{
ctx->buf[0] = 0x67452301;
ctx->buf[1] = 0xefcdab89;
ctx->buf[2] = 0x98badcfe;
ctx->buf[3] = 0x10325476;
ctx->bits[0] = 0;
ctx->bits[1] = 0;
}
/*
* Update context to reflect the concatenation of another buffer full
* of bytes.
*/
static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len)
{
FcChar32 t;
/* Update bitcount */
t = ctx->bits[0];
if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
ctx->bits[1]++; /* Carry from low to high */
ctx->bits[1] += len >> 29;
t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
/* Handle any leading odd-sized chunks */
if (t) {
unsigned char *p = (unsigned char *) ctx->in + t;
t = 64 - t;
if (len < t) {
memcpy(p, buf, len);
return;
}
memcpy(p, buf, t);
byteReverse(ctx->in, 16);
MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
buf += t;
len -= t;
}
/* Process data in 64-byte chunks */
while (len >= 64) {
memcpy(ctx->in, buf, 64);
byteReverse(ctx->in, 16);
MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
buf += 64;
len -= 64;
}
/* Handle any remaining bytes of data. */
memcpy(ctx->in, buf, len);
}
/*
* Final wrapup - pad to 64-byte boundary with the bit pattern
* 1 0* (64-bit count of bits processed, MSB-first)
*/
static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
{
unsigned count;
unsigned char *p;
/* Compute number of bytes mod 64 */
count = (ctx->bits[0] >> 3) & 0x3F;
/* Set the first char of padding to 0x80. This is safe since there is
always at least one byte free */
p = ctx->in + count;
*p++ = 0x80;
/* Bytes of padding needed to make 64 bytes */
count = 64 - 1 - count;
/* Pad out to 56 mod 64 */
if (count < 8) {
/* Two lots of padding: Pad the first block to 64 bytes */
memset(p, 0, count);
byteReverse(ctx->in, 16);
MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
/* Now fill the next block with 56 bytes */
memset(ctx->in, 0, 56);
} else {
/* Pad block to 56 bytes */
memset(p, 0, count - 8);
}
byteReverse(ctx->in, 14);
/* Append length in bits and transform */
((FcChar32 *) ctx->in)[14] = ctx->bits[0];
((FcChar32 *) ctx->in)[15] = ctx->bits[1];
MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
byteReverse((unsigned char *) ctx->buf, 4);
memcpy(digest, ctx->buf, 16);
memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
}
/* The four core functions - F1 is optimized somewhat */
/* #define F1(x, y, z) (x & y | ~x & z) */
#define F1(x, y, z) (z ^ (x & (y ^ z)))
#define F2(x, y, z) F1(z, x, y)
#define F3(x, y, z) (x ^ y ^ z)
#define F4(x, y, z) (y ^ (x | ~z))
/* This is the central step in the MD5 algorithm. */
#define MD5STEP(f, w, x, y, z, data, s) \
( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
/*
* The core of the MD5 algorithm, this alters an existing MD5 hash to
* reflect the addition of 16 longwords of new data. MD5Update blocks
* the data and converts bytes into longwords for this routine.
*/
static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
{
register FcChar32 a, b, c, d;
a = buf[0];
b = buf[1];
c = buf[2];
d = buf[3];
MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
buf[0] += a;
buf[1] += b;
buf[2] += c;
buf[3] += d;
}