Simplify hash code

This commit is contained in:
Behdad Esfahbod 2014-05-16 14:03:19 -06:00
parent e4d8847eee
commit 748e77e89f
3 changed files with 48 additions and 56 deletions

View File

@ -1715,7 +1715,7 @@ FcFreeTypeQueryFace (const FT_Face face,
goto bail3; goto bail3;
} }
memset (&fontdata[len], 0, alen - len); memset (&fontdata[len], 0, alen - len);
hashstr = FcHashGetSHA256DigestFromMemory (fontdata, len); hashstr = FcHashGetDigestFromMemory (fontdata, len);
free (fontdata); free (fontdata);
} }
else if (err == FT_Err_Invalid_Face_Handle) else if (err == FT_Err_Invalid_Face_Handle)
@ -1723,7 +1723,7 @@ FcFreeTypeQueryFace (const FT_Face face,
/* font may not support SFNT. falling back to /* font may not support SFNT. falling back to
* read the font data from file directly * read the font data from file directly
*/ */
hashstr = FcHashGetSHA256DigestFromFile (file); hashstr = FcHashGetDigestFromFile (file);
} }
else else
{ {

View File

@ -3,7 +3,9 @@
* *
* Copyright © 2003 Keith Packard * Copyright © 2003 Keith Packard
* Copyright © 2013 Red Hat, Inc. * Copyright © 2013 Red Hat, Inc.
* Copyright © 2014 Google, Inc.
* Red Hat Author(s): Akira TAGOH * Red Hat Author(s): Akira TAGOH
* Google Author(s): Behdad Esfahbod
* *
* Permission to use, copy, modify, distribute, and sell this software and its * Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that * documentation for any purpose is hereby granted without fee, provided that
@ -27,6 +29,9 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
/* SHA256 */
#define ROTRN(w, v, n) ((((FcChar32)v) >> n) | (((FcChar32)v) << (w - n))) #define ROTRN(w, v, n) ((((FcChar32)v) >> n) | (((FcChar32)v) << (w - n)))
#define ROTR32(v, n) ROTRN(32, v, n) #define ROTR32(v, n) ROTRN(32, v, n)
#define SHR(v, n) (v >> n) #define SHR(v, n) (v >> n)
@ -38,28 +43,22 @@
#define ss1(x) (ROTR32(x, 17) ^ ROTR32(x, 19) ^ SHR(x, 10)) #define ss1(x) (ROTR32(x, 17) ^ ROTR32(x, 19) ^ SHR(x, 10))
static FcChar32 * typedef FcChar32 FcHashDigest[8];
FcHashInitSHA256Digest (void)
static void
FcHashInitDigest (FcHashDigest digest)
{ {
int i; static const FcHashDigest init = {
static const FcChar32 h[] = {
0x6a09e667UL, 0xbb67ae85UL, 0x3c6ef372UL, 0xa54ff53aUL, 0x6a09e667UL, 0xbb67ae85UL, 0x3c6ef372UL, 0xa54ff53aUL,
0x510e527fUL, 0x9b05688cUL, 0x1f83d9abUL, 0x5be0cd19UL 0x510e527fUL, 0x9b05688cUL, 0x1f83d9abUL, 0x5be0cd19UL
}; };
FcChar32 *ret = malloc (sizeof (FcChar32) * 8);
if (!ret) memcpy (digest, init, sizeof (FcHashDigest));
return NULL;
for (i = 0; i < 8; i++)
ret[i] = h[i];
return ret;
} }
static void static void
FcHashComputeSHA256Digest (FcChar32 *hash, FcHashDigestAddBlock (FcHashDigest digest,
const char *block) const char block[64])
{ {
static const FcChar32 k[] = { static const FcChar32 k[] = {
0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
@ -82,7 +81,7 @@ FcHashComputeSHA256Digest (FcChar32 *hash,
FcChar32 w[64], i, j, t1, t2; FcChar32 w[64], i, j, t1, t2;
FcChar32 a, b, c, d, e, f, g, h; FcChar32 a, b, c, d, e, f, g, h;
#define H(n) (hash[n]) #define H(n) (digest[n])
a = H(0); a = H(0);
b = H(1); b = H(1);
@ -131,51 +130,46 @@ FcHashComputeSHA256Digest (FcChar32 *hash,
} }
static FcChar8 * static FcChar8 *
FcHashSHA256ToString (FcChar32 *hash) FcHashToString (const FcHashDigest digest)
{ {
FcChar8 *ret = NULL; FcChar8 *ret = NULL;
static const char hex[] = "0123456789abcdef"; static const char hex[] = "0123456789abcdef";
int i, j; int i, j;
if (hash) ret = malloc (sizeof (FcChar8) * (8 * 8 + 7 + 1));
if (!ret)
return NULL;
memcpy (ret, "sha256:", 7);
#define H(n) digest[n]
for (i = 0; i < 8; i++)
{ {
ret = malloc (sizeof (FcChar8) * (8 * 8 + 7 + 1)); FcChar32 v = H(i);
if (!ret)
return NULL;
memcpy (ret, "sha256:", 7);
#define H(n) hash[n]
for (i = 0; i < 8; i++)
{
FcChar32 v = H(i);
for (j = 0; j < 8; j++) for (j = 0; j < 8; j++)
ret[7 + (i * 8) + j] = hex[(v >> (28 - j * 4)) & 0xf]; ret[7 + (i * 8) + j] = hex[(v >> (28 - j * 4)) & 0xf];
}
ret[7 + i * 8] = 0;
#undef H
free (hash);
} }
ret[7 + i * 8] = 0;
#undef H
return ret; return ret;
} }
FcChar8 * FcChar8 *
FcHashGetSHA256DigestFromFile (const FcChar8 *filename) FcHashGetDigestFromFile (const FcChar8 *filename)
{ {
FILE *fp = fopen ((const char *)filename, "rb"); FcHashDigest digest;
FILE *fp;
char ibuf[64]; char ibuf[64];
FcChar32 *ret;
size_t len; size_t len;
struct stat st; struct stat st;
fp = fopen ((const char *)filename, "rb");
if (!fp) if (!fp)
return NULL; return NULL;
if (FcStat (filename, &st)) FcHashInitDigest (digest);
goto bail0;
ret = FcHashInitSHA256Digest (); if (FcStat (filename, &st))
if (!ret)
goto bail0; goto bail0;
while (!feof (fp)) while (!feof (fp))
@ -190,7 +184,7 @@ FcHashGetSHA256DigestFromFile (const FcChar8 *filename)
if ((64 - len) < 9) if ((64 - len) < 9)
{ {
/* process a block once */ /* process a block once */
FcHashComputeSHA256Digest (ret, ibuf); FcHashDigestAddBlock (digest, ibuf);
memset (ibuf, 0, 64); memset (ibuf, 0, 64);
} }
/* set input size at the end */ /* set input size at the end */
@ -203,17 +197,17 @@ FcHashGetSHA256DigestFromFile (const FcChar8 *filename)
ibuf[63 - 5] = (v >> 40) & 0xff; ibuf[63 - 5] = (v >> 40) & 0xff;
ibuf[63 - 6] = (v >> 48) & 0xff; ibuf[63 - 6] = (v >> 48) & 0xff;
ibuf[63 - 7] = (v >> 56) & 0xff; ibuf[63 - 7] = (v >> 56) & 0xff;
FcHashComputeSHA256Digest (ret, ibuf); FcHashDigestAddBlock (digest, ibuf);
break; break;
} }
else else
{ {
FcHashComputeSHA256Digest (ret, ibuf); FcHashDigestAddBlock (digest, ibuf);
} }
} }
fclose (fp); fclose (fp);
return FcHashSHA256ToString (ret); return FcHashToString (digest);
bail0: bail0:
fclose (fp); fclose (fp);
@ -222,16 +216,14 @@ bail0:
} }
FcChar8 * FcChar8 *
FcHashGetSHA256DigestFromMemory (const char *fontdata, FcHashGetDigestFromMemory (const char *fontdata,
size_t length) size_t length)
{ {
FcHashDigest digest;
char ibuf[64]; char ibuf[64];
FcChar32 *ret;
size_t i = 0; size_t i = 0;
ret = FcHashInitSHA256Digest (); FcHashInitDigest (digest);
if (!ret)
return NULL;
while (i <= length) while (i <= length)
{ {
@ -249,7 +241,7 @@ FcHashGetSHA256DigestFromMemory (const char *fontdata,
if ((64 - n) < 9) if ((64 - n) < 9)
{ {
/* process a block once */ /* process a block once */
FcHashComputeSHA256Digest (ret, ibuf); FcHashDigestAddBlock (digest, ibuf);
memset (ibuf, 0, 64); memset (ibuf, 0, 64);
} }
/* set input size at the end */ /* set input size at the end */
@ -262,15 +254,15 @@ FcHashGetSHA256DigestFromMemory (const char *fontdata,
ibuf[63 - 5] = (v >> 40) & 0xff; ibuf[63 - 5] = (v >> 40) & 0xff;
ibuf[63 - 6] = (v >> 48) & 0xff; ibuf[63 - 6] = (v >> 48) & 0xff;
ibuf[63 - 7] = (v >> 56) & 0xff; ibuf[63 - 7] = (v >> 56) & 0xff;
FcHashComputeSHA256Digest (ret, ibuf); FcHashDigestAddBlock (digest, ibuf);
break; break;
} }
else else
{ {
FcHashComputeSHA256Digest (ret, &fontdata[i]); FcHashDigestAddBlock (digest, &fontdata[i]);
} }
i += 64; i += 64;
} }
return FcHashSHA256ToString (ret); return FcHashToString (digest);
} }

View File

@ -868,11 +868,11 @@ FcFontSetDeserialize (const FcFontSet *set);
/* fchash.c */ /* fchash.c */
FcPrivate FcChar8 * FcPrivate FcChar8 *
FcHashGetSHA256DigestFromFile (const FcChar8 *filename); FcHashGetDigestFromFile (const FcChar8 *filename);
FcPrivate FcChar8 * FcPrivate FcChar8 *
FcHashGetSHA256DigestFromMemory (const char *fontdata, FcHashGetDigestFromMemory (const char *fontdata,
size_t length); size_t length);
/* fcinit.c */ /* fcinit.c */
FcPrivate FcConfig * FcPrivate FcConfig *