From 38543764df4e5f4071ff3e5fa8dc467e0bd26b9e Mon Sep 17 00:00:00 2001 From: Jeremy Huddleston Date: Sun, 18 Mar 2012 23:16:36 -0700 Subject: [PATCH 1/2] fcarch: Restrict FC_ARCHITECTURE usage to fcarch.h This should have no runtime effect, but it sets the stage for the following patch. Signed-off-by: Jeremy Huddleston --- fc-cache/fc-cache.c | 8 ++++++-- fc-cat/fc-cat.c | 7 +++++-- src/fcarch.c | 41 ++++------------------------------------- src/fcarch.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/fccache.c | 7 +++++-- 5 files changed, 68 insertions(+), 43 deletions(-) diff --git a/fc-cache/fc-cache.c b/fc-cache/fc-cache.c index d265350..e68ddc1 100644 --- a/fc-cache/fc-cache.c +++ b/fc-cache/fc-cache.c @@ -258,6 +258,7 @@ cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose) FcBool remove; FcCache *cache; struct stat target_stat; + FcChar8 suffix[1 + FC_ARCH_MAX_LEN + sizeof (FC_CACHE_SUFFIX) + 1]; dir_base = FcStrPlus (dir, (FcChar8 *) "/"); if (!dir_base) @@ -282,6 +283,9 @@ cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose) FcStrFree (dir_base); return FcFalse; } + strcpy((char *)suffix, "-"); + _FcGetArch(suffix + 1); + strlcpy((char *)suffix, FC_CACHE_SUFFIX, sizeof(suffix)); while ((ent = readdir (d))) { FcChar8 *file_name; @@ -291,8 +295,8 @@ cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose) continue; /* skip cache files for different architectures and */ /* files which are not cache files at all */ - if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) || - strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX)) + if (strlen(ent->d_name) != 32 + strlen((char *)suffix) || + strcmp(ent->d_name + 32, (char *)suffix)) continue; file_name = FcStrPlus (dir_base, (FcChar8 *) ent->d_name); diff --git a/fc-cat/fc-cat.c b/fc-cat/fc-cat.c index c3d6829..af6e859 100644 --- a/fc-cat/fc-cat.c +++ b/fc-cat/fc-cat.c @@ -149,13 +149,16 @@ static void usage (char *program, int error) { FILE *file = error ? stderr : stdout; + FcChar8 arch[FC_ARCH_MAX_LEN + 1]; + + _FcGetArch(arch); #if HAVE_GETOPT_LONG fprintf (file, "usage: %s [-rv] [--recurse] [--verbose] [*-%s.cache-2|directory]...\n", - program, FC_ARCHITECTURE); + program, arch); fprintf (file, " %s [-Vh] [--version] [--help]\n", program); #else fprintf (file, "usage: %s [-rvVh] [*-%s.cache-2|directory]...\n", - program, FC_ARCHITECTURE); + program, arch); #endif fprintf (file, "Reads font information cache from:\n"); fprintf (file, " 1) specified fontconfig cache file\n"); diff --git a/src/fcarch.c b/src/fcarch.c index c69397e..7df2e3e 100644 --- a/src/fcarch.c +++ b/src/fcarch.c @@ -27,47 +27,14 @@ #include #endif -/* If architecture is hardcoded, skip the assert tests */ - -#ifndef FC_ARCHITECTURE - #include "fcarch.h" -/* Make sure the cache structure is consistent with what we expect */ - -#include "fcint.h" - -FC_ASSERT_STATIC (1 == sizeof (char)); -FC_ASSERT_STATIC (2 == sizeof (FcChar16)); -FC_ASSERT_STATIC (4 == sizeof (int)); -FC_ASSERT_STATIC (4 == sizeof (FcChar32)); -FC_ASSERT_STATIC (4 == sizeof (FcObject)); -FC_ASSERT_STATIC (4 == sizeof (FcValueBinding)); -FC_ASSERT_STATIC (8 == sizeof (FcAlign)); -FC_ASSERT_STATIC (0x20 == sizeof (FcCharLeaf)); - -FC_ASSERT_STATIC (SIZEOF_VOID_P == sizeof (intptr_t)); -FC_ASSERT_STATIC (SIZEOF_VOID_P == sizeof (FcPatternEltPtr)); -FC_ASSERT_STATIC (SIZEOF_VOID_P == sizeof (FcValueListPtr)); -FC_ASSERT_STATIC (SIZEOF_VOID_P == sizeof (char *)); -FC_ASSERT_STATIC (SIZEOF_VOID_P == sizeof (struct FcPatternElt *)); -FC_ASSERT_STATIC (SIZEOF_VOID_P == sizeof (FcValueList *)); -FC_ASSERT_STATIC (SIZEOF_VOID_P == sizeof (FcStrSet *)); -FC_ASSERT_STATIC (SIZEOF_VOID_P == sizeof (FcCharLeaf **)); -FC_ASSERT_STATIC (SIZEOF_VOID_P == sizeof (FcChar16 *)); - -FC_ASSERT_STATIC (0x08 + 1*ALIGNOF_DOUBLE == sizeof (FcValue)); -FC_ASSERT_STATIC (0x00 + 2*SIZEOF_VOID_P == sizeof (FcPatternElt)); -FC_ASSERT_STATIC (0x08 + 2*SIZEOF_VOID_P == sizeof (FcPattern)); -FC_ASSERT_STATIC (0x08 + 2*SIZEOF_VOID_P == sizeof (FcCharSet)); -FC_ASSERT_STATIC (0x08 + 6*SIZEOF_VOID_P == sizeof (FcCache)); - -#endif - - int main (int argc, char **argv) { - printf ("%s\n", FC_ARCHITECTURE); + FcChar8 arch[FC_ARCH_MAX_LEN + 1]; + + _FcGetArch(arch); + printf ("%s\n", arch); return 0; } diff --git a/src/fcarch.h b/src/fcarch.h index 9cabf04..8695933 100644 --- a/src/fcarch.h +++ b/src/fcarch.h @@ -27,6 +27,36 @@ #include #endif +#include "fcint.h" + +#ifndef FC_ARCHITECTURE +/* Make sure the cache structure is consistent with what we expect */ +FC_ASSERT_STATIC (1 == sizeof (char)); +FC_ASSERT_STATIC (2 == sizeof (FcChar16)); +FC_ASSERT_STATIC (4 == sizeof (int)); +FC_ASSERT_STATIC (4 == sizeof (FcChar32)); +FC_ASSERT_STATIC (4 == sizeof (FcObject)); +FC_ASSERT_STATIC (4 == sizeof (FcValueBinding)); +FC_ASSERT_STATIC (8 == sizeof (FcAlign)); +FC_ASSERT_STATIC (0x20 == sizeof (FcCharLeaf)); + +FC_ASSERT_STATIC (SIZEOF_VOID_P == sizeof (intptr_t)); +FC_ASSERT_STATIC (SIZEOF_VOID_P == sizeof (FcPatternEltPtr)); +FC_ASSERT_STATIC (SIZEOF_VOID_P == sizeof (FcValueListPtr)); +FC_ASSERT_STATIC (SIZEOF_VOID_P == sizeof (char *)); +FC_ASSERT_STATIC (SIZEOF_VOID_P == sizeof (struct FcPatternElt *)); +FC_ASSERT_STATIC (SIZEOF_VOID_P == sizeof (FcValueList *)); +FC_ASSERT_STATIC (SIZEOF_VOID_P == sizeof (FcStrSet *)); +FC_ASSERT_STATIC (SIZEOF_VOID_P == sizeof (FcCharLeaf **)); +FC_ASSERT_STATIC (SIZEOF_VOID_P == sizeof (FcChar16 *)); + +FC_ASSERT_STATIC (0x08 + 1*ALIGNOF_DOUBLE == sizeof (FcValue)); +FC_ASSERT_STATIC (0x00 + 2*SIZEOF_VOID_P == sizeof (FcPatternElt)); +FC_ASSERT_STATIC (0x08 + 2*SIZEOF_VOID_P == sizeof (FcPattern)); +FC_ASSERT_STATIC (0x08 + 2*SIZEOF_VOID_P == sizeof (FcCharSet)); +FC_ASSERT_STATIC (0x08 + 6*SIZEOF_VOID_P == sizeof (FcCache)); +#endif + /* * Each unique machine architecture needs an entry in this file * So far the differences boil down to: endianness, 32 vs 64 bit pointers, @@ -46,6 +76,19 @@ * be64 1234 8 8 */ +#include +#include +#include + +/* Returns 0 on success or an errno on failure. + * buffer must be non-NULL and point to a character buffer with at least + * FC_ARCH_MAX_LEN + 1 bytes available. + */ +#define FC_ARCH_MAX_LEN 6 +static inline int _FcGetArch(FcChar8 *buffer) { + if (buffer == NULL) + return EINVAL; + #if defined(WORDS_BIGENDIAN) && WORDS_BIGENDIAN # define FC_ARCH_ENDIAN "be" #else /* !WORDS_BIGENDIAN */ @@ -67,4 +110,9 @@ # define FC_ARCHITECTURE FC_ARCH_ENDIAN FC_ARCH_SIZE_ALIGN #endif + snprintf((char *) buffer, FC_ARCH_MAX_LEN + 1, "%s", FC_ARCHITECTURE); + + return 0; +} + #endif /* _FCARCH_H_ */ diff --git a/src/fccache.c b/src/fccache.c index d8102d7..783e54a 100644 --- a/src/fccache.c +++ b/src/fccache.c @@ -57,7 +57,7 @@ static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned static void MD5Final(unsigned char digest[16], struct MD5Context *ctx); static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]); -#define CACHEBASE_LEN (1 + 32 + 1 + sizeof (FC_ARCHITECTURE) + sizeof (FC_CACHE_SUFFIX)) +#define CACHEBASE_LEN (1 + 32 + 1 + FC_ARCH_MAX_LEN + sizeof (FC_CACHE_SUFFIX)) #ifdef _WIN32 @@ -159,6 +159,7 @@ FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN]) FcChar8 *hex_hash; int cnt; struct MD5Context ctx; + FcChar8 arch[FC_ARCH_MAX_LEN + 1]; MD5Init (&ctx); MD5Update (&ctx, (const unsigned char *)dir, strlen ((const char *) dir)); @@ -173,7 +174,9 @@ FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN]) hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf]; } hex_hash[2*cnt] = 0; - strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX); + + _FcGetArch(arch); + snprintf((char *) cache_base, CACHEBASE_LEN, "-%s%s", arch, FC_CACHE_SUFFIX); return cache_base; } -- 1.7.9.2