]> git.wh0rd.org - fontconfig.git/commitdiff
Migrate cache files from fonts directories to /var/cache/fontconfig. This
authorPatrick Lam <plam@MIT.EDU>
Fri, 9 Dec 2005 16:36:45 +0000 (16:36 +0000)
committerPatrick Lam <plam@MIT.EDU>
Fri, 9 Dec 2005 16:36:45 +0000 (16:36 +0000)
    helps make fontconfig FHS-compliant, but requires that all caches get
    rebuilt.
Also, autogen.sh now needs the additional parameter
--localstatedir=/var.

ChangeLog
INSTALL
configure.in
fc-cache/Makefile.am
fc-cat/Makefile.am
fc-cat/fc-cat.c
fontconfig/fontconfig.h
src/Makefile.am
src/fccache.c

index 2b4b555e0cf516fbe1f01c5590c821d059dea41d..de7cf22ba5210400fa59511390a58fac85f73f75 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -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>
        * src/fcpat.c (FcPatternAddString):
 
diff --git a/INSTALL b/INSTALL
index 58dffd57a183e1c61ddb5dd0a4b2e5b206fc8349..a6dc1df24fb51c90f4f6bee496f1a59d3793451e 100644 (file)
--- a/INSTALL
+++ b/INSTALL
@@ -18,7 +18,7 @@ important steps:
                Set the date
  3.    Commit those changes
  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)
  6.    tag the tree
                cvs tag fc-2_2_xx
index 7ea261c670e5634808123e1b23977a6abb2bad8b..797d27fc48b115cc585d49490308806a6c6f31c2 100644 (file)
@@ -479,6 +479,13 @@ fi
 
 AC_SUBST(DOCDIR)
 
+#
+# Make /var/cache/fontconfig directory available to source code
+#
+
+pkgcachedir='${localstatedir}/cache/'${PACKAGE}
+AC_SUBST(pkgcachedir)
+
 AC_OUTPUT([
 Makefile 
 fontconfig/Makefile
index ad42af3b1b93674f54b9d24dc732eb51075928ec..5f179f5598a14f13e0fa21dccc813351e8454e7d 100644 (file)
@@ -27,6 +27,8 @@ FC_CACHE_SRC=${top_srcdir}/fc-cache
 
 SGML = ${FC_CACHE_SRC}/fc-cache.sgml
 
+AM_CPPFLAGS = -DPKGCACHEDIR='"${pkgcachedir}"'
+
 INCLUDES=-I${top_srcdir} $(FREETYPE_CFLAGS)
 
 bin_PROGRAMS=fc-cache
index cf340090c3b344ecaa556d044a23d66ef5fbf1e8..55a755e407d1409001403f0626d665663a87f2a3 100644 (file)
@@ -27,6 +27,8 @@ FC_CAT_SRC=${top_srcdir}/fc-cat
 
 SGML = ${FC_CAT_SRC}/fc-cat.sgml
 
+AM_CPPFLAGS = -DPKGCACHEDIR='"${pkgcachedir}"'
+
 INCLUDES=-I${top_srcdir} $(FREETYPE_CFLAGS)
 
 bin_PROGRAMS=fc-cat
index 1414bc896a65bd590d999e3b60b6b422b3c81845..f088e4585a9220ff2003f190f02014b0b8e4fe11 100644 (file)
@@ -184,7 +184,8 @@ FcCacheGlobalFileReadAndPrint (FcFontSet * set, FcStrSet *dirs, char * dir, char
     if (fd == -1)
        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)
        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];
     off_t current_arch_start = 0;
     char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
+    char name_buf[8192];
 
     if (!cache_file)
         goto bail;
@@ -234,7 +236,14 @@ FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char * dir, char *cache_file)
     if (fd == -1)
         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)
         goto bail1;
 
index aae60f44c4973d0fc9befa667404d75606a11ee0..515d8672a9efd8ccf8883a3caa22e609b056ce4a 100644 (file)
@@ -104,6 +104,7 @@ typedef int         FcBool;
 #define FC_EMBOLDEN        "embolden"          /* Bool - true if emboldening needed*/
 #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_USER_CACHE_FILE         ".fonts.cache-"FC_CACHE_VERSION
 
index 2ba99458bb4787d717bf239fcda833c833c7b2dc..1eb7f8adf6e6beb040f0cf0445045e1931da1d0b 100644 (file)
@@ -63,6 +63,8 @@ uninstall-ms-import-lib:
 
 endif
 
+AM_CPPFLAGS = -DPKGCACHEDIR='"${pkgcachedir}"'
+
 INCLUDES =                                             \
        $(FREETYPE_CFLAGS)                              \
        $(LIBXML2_CFLAGS)                               \
index 13c38963da40764226aa6241c422736fb8037c3d..45413e0b238c039642877d20b297e17aad9887a0 100644 (file)
@@ -25,6 +25,7 @@
 
 #include <fcntl.h>
 #include <dirent.h>
+#include <string.h>
 #include <sys/mman.h>
 #include <sys/utsname.h>
 #include <sys/types.h>
 #define ENDIAN_TEST 0x12345678
 #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
-FcCacheSkipToArch (int fd, const char * arch);
+FcCacheSkipToArch (int fd, const char * arch, FcBool global);
 
 static FcBool 
 FcCacheCopyOld (int fd, int fd_orig, off_t start);
@@ -62,6 +69,17 @@ FcCacheHaveBank (int bank);
 static void
 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
 
 static char *
@@ -104,6 +122,33 @@ FcCacheReadString (int fd, char *dest, int len)
     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
 FcCacheWriteString (int fd, const char *chars)
 {
@@ -156,7 +201,7 @@ FcGlobalCacheLoad (FcGlobalCache    *cache,
                   const FcChar8    *cache_file,
                   FcConfig         *config)
 {
-    char               name_buf[8192];
+    char               name_buf[FC_MAX_FILE_LEN];
     FcGlobalCacheDir   *d, *next;
     FcFileTime         config_time = FcConfigModifiedTime (config);
     char               * current_arch_machine_name;
@@ -176,7 +221,7 @@ FcGlobalCacheLoad (FcGlobalCache    *cache,
 
     current_arch_machine_name = FcCacheMachineSignature ();
     current_arch_start = FcCacheSkipToArch(cache->fd, 
-                                          current_arch_machine_name);
+                                          current_arch_machine_name, FcTrue);
     if (current_arch_start < 0)
         goto bail_and_destroy;
 
@@ -343,7 +388,8 @@ FcGlobalCacheSave (FcGlobalCache    *cache,
         current_arch_start = 0;
     else
         current_arch_start = FcCacheSkipToArch (fd_orig, 
-                                                current_arch_machine_name);
+                                                current_arch_machine_name, 
+                                               FcTrue);
 
     if (current_arch_start < 0)
        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,
  * or -1 if arch not found */
 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;
     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 */
     while (1)
     {
@@ -542,21 +593,27 @@ FcCacheCopyOld (int fd, int fd_orig, off_t start)
 FcBool
 FcDirCacheValid (const FcChar8 *dir)
 {
-    FcChar8     *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
+    FcChar8    *cache_file;
     struct stat file_stat, dir_stat;
+    int        fd;
 
     if (stat ((char *) dir, &dir_stat) < 0)
-    {
-        FcStrFree (cache_file);
-        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);
+
     /*
      * If the directory has been modified more recently than
      * the cache file, the cache is not valid
@@ -565,6 +622,12 @@ FcDirCacheValid (const FcChar8 *dir)
         return FcFalse;
 
     return FcTrue;
+
+ bail1:
+    close (fd);
+ bail:
+    FcStrFree (cache_file);
+    return FcFalse;
 }
 
 /* Assumes that the cache file in 'dir' exists.
@@ -572,23 +635,32 @@ FcDirCacheValid (const FcChar8 *dir)
 FcBool
 FcDirCacheHasCurrentArch (const FcChar8 *dir)
 {
-    FcChar8     *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
+    char       *cache_file;
     int        fd;
     off_t      current_arch_start;
     char       *current_arch_machine_name;
 
-    current_arch_machine_name = FcCacheMachineSignature();
-    fd = open ((char *)cache_file, O_RDONLY);
-    if (fd == -1)
-        return FcFalse;
+    cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
+    if (!cache_file)
+       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);
 
     if (current_arch_start < 0)
         return FcFalse;
     
     return FcTrue;
+
+ bail:
+    free (cache_file);
+    return FcFalse;
 }
 
 FcBool
@@ -718,26 +790,101 @@ FcCacheRead (FcConfig *config, FcGlobalCache * cache)
     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 */
 FcBool
 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir)
 {
-    char *cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
-    int fd;
-    char current_arch_machine_name;
-    char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
-    off_t current_arch_start = 0;
-    char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
+    char       *cache_file;
+    int        fd;
+    char       *current_arch_machine_name;
+    char       candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
+    off_t      current_arch_start = 0;
+    char       subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
 
+    cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
     if (!cache_file)
-        goto bail;
+       return FcFalse;
 
-    current_arch_machine_name = FcCacheMachineSignature();
-    fd = open(cache_file, O_RDONLY);
-    if (fd == -1)
-        goto bail;
+    fd = FcDirCacheOpen (cache_file);
+    if (fd < 0)
+       goto bail;
 
-    current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
+    current_arch_machine_name = FcCacheMachineSignature();
+    current_arch_start = FcCacheSkipToArch(fd, 
+                                          current_arch_machine_name,
+                                          FcFalse);
     if (current_arch_start < 0)
         goto bail1;
 
@@ -844,58 +991,89 @@ FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
 FcBool
 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;
     FcAtomic       *atomic;
     FcCache        metadata;
     off_t          current_arch_start = 0, truncate_to;
+    char           name_buf[FC_MAX_FILE_LEN];
+    int                    collisions;
 
     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)
         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);
 
     if (metadata.count && !current_dir_block)
-       goto bail0;
+       goto bail1;
 
     if (FcDebug () & FC_DBG_CACHE)
         printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
 
-    atomic = FcAtomicCreate (cache_file);
+    atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
     if (!atomic)
-        goto bail0;
+       goto bail1;
 
     if (!FcAtomicLock (atomic))
-        goto bail1;
+        goto bail2;
 
     fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY, 0666);
 
     fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
     if (fd == -1)
-        goto bail2;
+        goto bail3;
+
+    FcCacheWriteString (fd, cache_file);
 
     current_arch_machine_name = FcCacheMachineSignature ();
     current_arch_start = 0;
 
     if (fd_orig != -1)
         current_arch_start = 
-            FcCacheSkipToArch(fd_orig, current_arch_machine_name);
+            FcCacheSkipToArch(fd_orig, current_arch_machine_name, FcFalse);
 
     if (current_arch_start < 0)
        current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
 
     if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
-       goto bail3;
+       goto bail4;
 
     if (fd_orig != -1)
         close (fd_orig);
 
     current_arch_start = lseek(fd, 0, SEEK_CUR);
     if (ftruncate (fd, current_arch_start) == -1)
-       goto bail3;
+       goto bail4;
 
     /* allocate space for subdir names in this block */
     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;
     header = malloc (10 + strlen (current_arch_machine_name));
     if (!header)
-       goto bail3;
+       goto bail4;
     sprintf (header, "%8x ", (int)truncate_to);
     strcat (header, current_arch_machine_name);
     if (!FcCacheWriteString (fd, header))
-       goto bail4;
+       goto bail5;
 
     for (i = 0; i < dirs->size; 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 */
     if (ftruncate (fd, current_arch_start + truncate_to) == -1)
-       goto bail4;
+       goto bail5;
 
     close(fd);
     if (!FcAtomicReplaceOrig(atomic))
-        goto bail4;
+        goto bail5;
     FcAtomicUnlock (atomic);
     FcAtomicDestroy (atomic);
     return FcTrue;
 
- bail4:
+ bail5:
     free (header);
- bail3:
+ bail4:
     close (fd);
- bail2:
+ bail3:
     FcAtomicUnlock (atomic);
- bail1:
+ bail2:
     FcAtomicDestroy (atomic);
+ bail1:
+    free (cache_hashed);
  bail0:
     unlink ((char *)cache_file);
     free (cache_file);
@@ -1070,3 +1250,246 @@ FcCacheFindBankDir (int bank)
     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;
+}