Mailing List Archive

r3438 - in trunk/varnish-cache: include lib/libvarnish
Author: phk
Date: 2008-11-25 12:45:59 +0100 (Tue, 25 Nov 2008)
New Revision: 3438

Modified:
trunk/varnish-cache/include/vsha256.h
trunk/varnish-cache/lib/libvarnish/vsha256.c
Log:
Neuther the FreeBSD specifics of SHA256 implementation.

In the end, it comes down to lack of POSIX definition of a way to find
out byte-endianess, sigh...



Modified: trunk/varnish-cache/include/vsha256.h
===================================================================
--- trunk/varnish-cache/include/vsha256.h 2008-11-25 11:09:38 UTC (rev 3437)
+++ trunk/varnish-cache/include/vsha256.h 2008-11-25 11:45:59 UTC (rev 3438)
@@ -29,7 +29,7 @@
#ifndef _SHA256_H_
#define _SHA256_H_

-#include <sys/types.h>
+#include <stdint.h>

typedef struct SHA256Context {
uint32_t state[8];
@@ -41,10 +41,6 @@
void SHA256_Init(SHA256_CTX *);
void SHA256_Update(SHA256_CTX *, const void *, size_t);
void SHA256_Final(unsigned char [32], SHA256_CTX *);
-char *SHA256_End(SHA256_CTX *, char *);
-char *SHA256_File(const char *, char *);
-char *SHA256_FileChunk(const char *, char *, off_t, off_t);
-char *SHA256_Data(const void *, unsigned int, char *);
__END_DECLS

#endif /* !_SHA256_H_ */

Modified: trunk/varnish-cache/lib/libvarnish/vsha256.c
===================================================================
--- trunk/varnish-cache/lib/libvarnish/vsha256.c 2008-11-25 11:09:38 UTC (rev 3437)
+++ trunk/varnish-cache/lib/libvarnish/vsha256.c 2008-11-25 11:45:59 UTC (rev 3438)
@@ -22,19 +22,17 @@
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
+ *
+ * From: $FreeBSD: head/lib/libmd/sha256c.c 154479 2006-01-17 15:35:57Z phk $
*/

-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/lib/libmd/sha256c.c 154479 2006-01-17 15:35:57Z phk $");
+#include <stdint.h>

-#include <sys/endian.h>
-#include <sys/types.h>
-
#include <string.h>

#include "vsha256.h"

-#if BYTE_ORDER == BIG_ENDIAN
+#if defined(BYTE_ORDER) && BYTE_ORDER == BIG_ENDIAN

/* Copy a vector of big-endian uint32_t into a vector of bytes */
#define be32enc_vect(dst, src, len) \
@@ -44,8 +42,27 @@
#define be32dec_vect(dst, src, len) \
memcpy((void *)dst, (const void *)src, (size_t)len)

-#else /* BYTE_ORDER != BIG_ENDIAN */
+#else /* BYTE_ORDER != BIG_ENDIAN or in doubt... */

+static __inline uint32_t
+mybe32dec(const void *pp)
+{
+ unsigned char const *p = (unsigned char const *)pp;
+
+ return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
+}
+
+static __inline void
+mybe32enc(void *pp, uint32_t u)
+{
+ unsigned char *p = (unsigned char *)pp;
+
+ p[0] = (u >> 24) & 0xff;
+ p[1] = (u >> 16) & 0xff;
+ p[2] = (u >> 8) & 0xff;
+ p[3] = u & 0xff;
+}
+
/*
* Encode a length len/4 vector of (uint32_t) into a length len vector of
* (unsigned char) in big-endian form. Assumes len is a multiple of 4.
@@ -56,7 +73,7 @@
size_t i;

for (i = 0; i < len / 4; i++)
- be32enc(dst + i * 4, src[i]);
+ mybe32enc(dst + i * 4, src[i]);
}

/*
@@ -69,10 +86,10 @@
size_t i;

for (i = 0; i < len / 4; i++)
- dst[i] = be32dec(src + i * 4);
+ dst[i] = mybe32dec(src + i * 4);
}

-#endif /* BYTE_ORDER != BIG_ENDIAN */
+#endif

/* Elementary functions used by SHA256 */
#define Ch(x, y, z) ((x & (y ^ z)) ^ z)