From 6c16fac2b5b8f1dd89d37f4f340e7cd7dbd2e5d2 Mon Sep 17 00:00:00 2001
From: Michael Wallner <mike@php.net>
Date: Fri, 25 Nov 2005 23:12:44 +0000
Subject: [PATCH] - add adler32

---
 ext/hash/config.m4          | 11 +++++--
 ext/hash/config.w32         |  4 ++-
 ext/hash/hash.c             |  3 +-
 ext/hash/hash_adler32.c     | 66 +++++++++++++++++++++++++++++++++++++
 ext/hash/package.xml        |  4 ++-
 ext/hash/php_hash.h         |  3 +-
 ext/hash/php_hash_adler32.h | 43 ++++++++++++++++++++++++
 7 files changed, 128 insertions(+), 6 deletions(-)
 create mode 100644 ext/hash/hash_adler32.c
 create mode 100644 ext/hash/php_hash_adler32.h

diff --git a/ext/hash/config.m4 b/ext/hash/config.m4
index af7dc3e9a5..123f272695 100644
--- a/ext/hash/config.m4
+++ b/ext/hash/config.m4
@@ -6,8 +6,15 @@ PHP_ARG_ENABLE(hash, whether to enable hash support,
 
 if test "$PHP_HASH" != "no"; then
   AC_DEFINE(HAVE_HASH_EXT,1,[Have HASH Extension])
-  PHP_NEW_EXTENSION(hash, hash.c hash_md.c hash_sha.c hash_ripemd.c hash_haval.c hash_tiger.c hash_gost.c hash_snefru.c hash_whirlpool.c, $ext_shared)
+  
+  EXT_HASH_SOURCES="hash.c hash_md.c hash_sha.c hash_ripemd.c hash_haval.c \
+  	hash_tiger.c hash_gost.c hash_snefru.c hash_whirlpool.c hash_adler32.c"
+  EXT_HASH_HEADERS="php_hash.h php_hash_md.h php_hash_sha.h php_hash_ripemd.h \
+  	php_hash_haval.h php_hash_tiger.h php_hash_gost.h php_hash_snefru.h \
+	  php_hash_whirlpool.h php_hash_adler32.h"
+  
+  PHP_NEW_EXTENSION(hash, $EXT_HASH_SOURCES, $ext_shared)
   ifdef([PHP_INSTALL_HEADERS], [
-  	PHP_INSTALL_HEADERS(ext/hash, php_hash.h php_hash_md.h php_hash_sha.h php_hash_ripemd.h php_hash_haval.h php_hash_tiger.h php_hash_gost.h php_hash_snefru.h php_hash_whirlpool.h)
+  	PHP_INSTALL_HEADERS(ext/hash, $EXT_HASH_HEADERS)
   ])
 fi
diff --git a/ext/hash/config.w32 b/ext/hash/config.w32
index db15dbb673..d490d92950 100644
--- a/ext/hash/config.w32
+++ b/ext/hash/config.w32
@@ -5,6 +5,8 @@ ARG_ENABLE("hash", "enable hash support", "no");
 
 if (PHP_HASH != "no") {
 	AC_DEFINE('HAVE_HASH_EXT', 1);
-	EXTENSION("hash", "hash.c hash_md.c hash_sha.c hash_ripemd.c hash_haval.c hash_tiger.c hash_gost.c hash_snefru.c hash_whirlpool.c");
+	EXTENSION("hash", "hash.c hash_md.c hash_sha.c hash_ripemd.c hash_haval.c "
+		+ "hash_tiger.c hash_gost.c hash_snefru.c hash_whirlpool.c "
+		+ "hash_adler32.c");
 }
 
diff --git a/ext/hash/hash.c b/ext/hash/hash.c
index 63b5e903e6..4408ba42ea 100644
--- a/ext/hash/hash.c
+++ b/ext/hash/hash.c
@@ -398,7 +398,7 @@ PHP_MINIT_FUNCTION(hash)
 {
 	php_hash_le_hash = zend_register_list_destructors_ex(php_hash_dtor, NULL, PHP_HASH_RESNAME, module_number);
 
-	zend_hash_init(&php_hash_hashtable, 30, NULL, NULL, 1);
+	zend_hash_init(&php_hash_hashtable, 35, NULL, NULL, 1);
 
 	php_hash_register_algo("md5",			&php_hash_md5_ops);
 	php_hash_register_algo("sha1",			&php_hash_sha1_ops);
@@ -416,6 +416,7 @@ PHP_MINIT_FUNCTION(hash)
 	php_hash_register_algo("tiger192,4",	&php_hash_4tiger192_ops);
 	php_hash_register_algo("snefru",		&php_hash_snefru_ops);
 	php_hash_register_algo("gost",			&php_hash_gost_ops);
+	php_hash_register_algo("adler32",		&php_hash_adler32_ops);
 
 	PHP_HASH_HAVAL_REGISTER(3,128);
 	PHP_HASH_HAVAL_REGISTER(3,160);
diff --git a/ext/hash/hash_adler32.c b/ext/hash/hash_adler32.c
new file mode 100644
index 0000000000..48d262b02a
--- /dev/null
+++ b/ext/hash/hash_adler32.c
@@ -0,0 +1,66 @@
+/*
+  +----------------------------------------------------------------------+
+  | PHP Version 5                                                        |
+  +----------------------------------------------------------------------+
+  | Copyright (c) 1997-2005 The PHP Group                                |
+  +----------------------------------------------------------------------+
+  | This source file is subject to version 3.0 of the PHP license,       |
+  | that is bundled with this package in the file LICENSE, and is        |
+  | available through the world-wide-web at the following url:           |
+  | http://www.php.net/license/3_0.txt.                                  |
+  | If you did not receive a copy of the PHP license and are unable to   |
+  | obtain it through the world-wide-web, please send a note to          |
+  | license@php.net so we can mail you a copy immediately.               |
+  +----------------------------------------------------------------------+
+  | Authors: Michael Wallner <mike@php.net>                              |
+  |          Sara Golemon <pollita@php.net>                              |
+  +----------------------------------------------------------------------+
+*/
+
+/* $Id$ */
+
+#include "php_hash.h"
+#include "php_hash_adler32.h"
+
+PHP_HASH_API PHP_ADLER32Init(PHP_ADLER32_CTX *context)
+{
+	context->state = 1;
+}
+
+PHP_HASH_API PHP_ADLER32Update(PHP_ADLER32_CTX *context, const unsigned char *input, size_t len)
+{
+	php_uint32 i, s[2] = { context->state & 0xffff, (context->state >> 16) & 0xffff };
+	
+	for (i = 0; i < len; ++i) {
+		s[0] = (s[0] + input[i]) % 65521;
+		s[1] = (s[1] + s[0]) % 65521;
+	}
+	context->state = s[0] + (s[1] << 16);
+}
+
+PHP_HASH_API PHP_ADLER32Final(unsigned char digest[4], PHP_ADLER32_CTX *context)
+{
+	digest[3] = (unsigned char) ((context->state >> 24) & 0xff);
+	digest[2] = (unsigned char) ((context->state >> 16) & 0xff);
+	digest[1] = (unsigned char) ((context->state >> 8) & 0xff);
+	digest[0] = (unsigned char) (context->state & 0xff);
+	context->state = 0;
+}
+
+php_hash_ops php_hash_adler32_ops = {
+	(php_hash_init_func_t) PHP_ADLER32Init,
+	(php_hash_update_func_t) PHP_ADLER32Update,
+	(php_hash_final_func_t) PHP_ADLER32Final,
+	4, /* what to say here? */
+	4,
+	sizeof(PHP_ADLER32_CTX)
+};
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim600: sw=4 ts=4 fdm=marker
+ * vim<600: sw=4 ts=4
+ */
diff --git a/ext/hash/package.xml b/ext/hash/package.xml
index 76aef7d72e..8fbd9dd6db 100644
--- a/ext/hash/package.xml
+++ b/ext/hash/package.xml
@@ -30,7 +30,7 @@ Initial Release
  * sha1, sha256, sha384, sha512
  * ripemd128, ripemd160
  * tiger128, tiger160, tiger192
- * gost, snefru, whirlpool
+ * adler32, gost, snefru, whirlpool
    </notes>
   </release>
 
@@ -57,6 +57,8 @@ Initial Release
    <file role="src" name="hash_gost.c"/>
    <file role="src" name="php_hash_gost.h"/>
    <file role="src" name="php_hash_gost_tables.h"/>
+   <file role="src" name="hash_adler32.c"/>
+   <file role="src" name="php_hash_adler32.h"/>
    <file role="doc" name="README"/>
    <dir role="test" name="tests">
     <file role="test" name="hmac-md5.phpt"/>
diff --git a/ext/hash/php_hash.h b/ext/hash/php_hash.h
index 6e23bfedf9..1f11d4fb4a 100644
--- a/ext/hash/php_hash.h
+++ b/ext/hash/php_hash.h
@@ -67,6 +67,7 @@ extern php_hash_ops php_hash_4tiger160_ops;
 extern php_hash_ops php_hash_4tiger192_ops;
 extern php_hash_ops php_hash_snefru_ops;
 extern php_hash_ops php_hash_gost_ops;
+extern php_hash_ops php_hash_adler32_ops;
 
 #define PHP_HASH_HAVAL_OPS(p,b)	extern php_hash_ops php_hash_##p##haval##b##_ops;
 
@@ -104,7 +105,7 @@ extern zend_module_entry hash_module_entry;
 PHP_HASH_API php_hash_ops *php_hash_fetch_ops(const char *algo, int algo_len);
 PHP_HASH_API void php_hash_register_algo(const char *algo, php_hash_ops *ops);
 
-static inline void php_hash_bin2hex(char *out, unsigned char *in, int in_len)
+static inline void php_hash_bin2hex(char *out, const unsigned char *in, int in_len)
 {
 	static const char hexits[16] = "0123456789abcdef";
 	int i;
diff --git a/ext/hash/php_hash_adler32.h b/ext/hash/php_hash_adler32.h
new file mode 100644
index 0000000000..27bddbb787
--- /dev/null
+++ b/ext/hash/php_hash_adler32.h
@@ -0,0 +1,43 @@
+/*
+   +----------------------------------------------------------------------+
+   | PHP Version 5                                                        |
+   +----------------------------------------------------------------------+
+   | Copyright (c) 1997-2005 The PHP Group                                |
+   +----------------------------------------------------------------------+
+   | This source file is subject to version 3.0 of the PHP license,       |
+   | that is bundled with this package in the file LICENSE, and is        |
+   | available through the world-wide-web at the following url:           |
+   | http://www.php.net/license/3_0.txt.                                  |
+   | If you did not receive a copy of the PHP license and are unable to   |
+   | obtain it through the world-wide-web, please send a note to          |
+   | license@php.net so we can mail you a copy immediately.               |
+   +----------------------------------------------------------------------+
+   | Author: Michael Wallner <mike@php.net>                               |
+   +----------------------------------------------------------------------+
+*/
+
+/* $Id$ */
+
+#ifndef PHP_HASH_ADLER32_H
+#define PHP_HASH_ADLER32_H
+
+#include "ext/standard/basic_functions.h"
+
+typedef struct {
+	php_uint32 state;
+} PHP_ADLER32_CTX;
+
+PHP_HASH_API PHP_ADLER32Init(PHP_ADLER32_CTX *context);
+PHP_HASH_API PHP_ADLER32Update(PHP_ADLER32_CTX *context, const unsigned char *input, size_t len);
+PHP_HASH_API PHP_ADLER32Final(unsigned char digest[4], PHP_ADLER32_CTX *context);
+
+#endif
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim600: sw=4 ts=4 fdm=marker
+ * vim<600: sw=4 ts=4
+ */
-- 
2.40.0