Browse Source

added some hash function implementations (collection from more sources)

Vaclav Kubart 19 năm trước cách đây
mục cha
commit
01e5f9fcfb

+ 156 - 0
lib/cds/GeneralHashFunctions.c

@@ -0,0 +1,156 @@
+
+#include <cds/GeneralHashFunctions.h>
+
+unsigned int RSHash(const char* str, unsigned int len)
+{
+   unsigned int b    = 378551;
+   unsigned int a    = 63689;
+   unsigned int hash = 0;
+   unsigned int i    = 0;
+
+   for(i = 0; i < len; str++, i++)
+   {
+      hash = hash * a + (*str);
+      a    = a * b;
+   }
+
+   return (hash & 0x7FFFFFFF);
+}
+/* End Of RS Hash Function */
+
+
+unsigned int JSHash(const char* str, unsigned int len)
+{
+   unsigned int hash = 1315423911;
+   unsigned int i    = 0;
+
+   for(i = 0; i < len; str++, i++)
+   {
+      hash ^= ((hash << 5) + (*str) + (hash >> 2));
+   }
+
+   return (hash & 0x7FFFFFFF);
+}
+/* End Of JS Hash Function */
+
+
+unsigned int PJWHash(const char* str, unsigned int len)
+{
+   unsigned int BitsInUnsignedInt = (unsigned int)(sizeof(unsigned int) * 8);
+   unsigned int ThreeQuarters     = (unsigned int)((BitsInUnsignedInt  * 3) / 4);
+   unsigned int OneEighth         = (unsigned int)(BitsInUnsignedInt / 8);
+   unsigned int HighBits          = (unsigned int)(0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
+   unsigned int hash              = 0;
+   unsigned int test              = 0;
+   unsigned int i                 = 0;
+
+   for(i = 0; i < len; str++, i++)
+   {
+      hash = (hash << OneEighth) + (*str);
+
+      if((test = hash & HighBits)  != 0)
+      {
+         hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
+      }
+   }
+
+   return (hash & 0x7FFFFFFF);
+}
+/* End Of  P. J. Weinberger Hash Function */
+
+
+unsigned int ELFHash(const char* str, unsigned int len)
+{
+   unsigned int hash = 0;
+   unsigned int x    = 0;
+   unsigned int i    = 0;
+
+   for(i = 0; i < len; str++, i++)
+   {
+      hash = (hash << 4) + (*str);
+      if((x = hash & 0xF0000000L) != 0)
+      {
+         hash ^= (x >> 24);
+         hash &= ~x;
+      }
+   }
+
+   return (hash & 0x7FFFFFFF);
+}
+/* End Of ELF Hash Function */
+
+
+unsigned int BKDRHash(const char* str, unsigned int len)
+{
+   unsigned int seed = 131; /* 31 131 1313 13131 131313 etc.. */
+   unsigned int hash = 0;
+   unsigned int i    = 0;
+
+   for(i = 0; i < len; str++, i++)
+   {
+      hash = (hash * seed) + (*str);
+   }
+
+   return (hash & 0x7FFFFFFF);
+}
+/* End Of BKDR Hash Function */
+
+
+unsigned int SDBMHash(const char* str, unsigned int len)
+{
+   unsigned int hash = 0;
+   unsigned int i    = 0;
+
+   for(i = 0; i < len; str++, i++)
+   {
+      hash = (*str) + (hash << 6) + (hash << 16) - hash;
+   }
+
+   return (hash & 0x7FFFFFFF);
+}
+/* End Of SDBM Hash Function */
+
+
+unsigned int DJBHash(const char* str, unsigned int len)
+{
+   unsigned int hash = 5381;
+   unsigned int i    = 0;
+
+   for(i = 0; i < len; str++, i++)
+   {
+      hash = ((hash << 5) + hash) + (*str);
+   }
+
+   return (hash & 0x7FFFFFFF);
+}
+/* End Of DJB Hash Function */
+
+
+unsigned int DEKHash(const char* str, unsigned int len)
+{
+   unsigned int hash = len;
+   unsigned int i    = 0;
+
+   for(i = 0; i < len; str++, i++)
+   {
+      hash = ((hash << 5) ^ (hash >> 27)) ^ (*str);
+   }
+   return (hash & 0x7FFFFFFF);
+}
+/* End Of DEK Hash Function */
+
+
+unsigned int APHash(const char* str, unsigned int len)
+{
+   unsigned int hash = 0;
+   unsigned int i    = 0;
+
+   for(i = 0; i < len; str++, i++)
+   {
+      hash ^= ((i & 1) == 0) ? (  (hash <<  7) ^ (*str) ^ (hash >> 3)) :
+                               (~((hash << 11) ^ (*str) ^ (hash >> 5)));
+   }
+
+   return (hash & 0x7FFFFFFF);
+}
+/* End Of AP Hash Function */

+ 42 - 0
lib/cds/GeneralHashFunctions.h

@@ -0,0 +1,42 @@
+/*
+ **************************************************************************
+ *                                                                        *
+ *          General Purpose Hash Function Algorithms Library              *
+ *                                                                        *
+ * Author: Arash Partow - 2002                                            *
+ * URL: http://www.partow.net                                             *
+ * URL: http://www.partow.net/programming/hashfunctions/index.html        *
+ *                                                                        *
+ * Copyright notice:                                                      *
+ * Free use of the General Purpose Hash Function Algorithms Library is    *
+ * permitted under the guidelines and in accordance with the most current *
+ * version of the Common Public License.                                  *
+ * http://www.opensource.org/licenses/cpl.php                             *
+ *                                                                        *
+ **************************************************************************
+*/
+
+
+
+#ifndef INCLUDE_GENERALHASHFUNCTION_C_H
+#define INCLUDE_GENERALHASHFUNCTION_C_H
+
+
+#include <stdio.h>
+
+
+typedef unsigned int (*hash_function)(const char*, unsigned int len);
+
+
+unsigned int RSHash  (const char* str, unsigned int len);
+unsigned int JSHash  (const char* str, unsigned int len);
+unsigned int PJWHash (const char* str, unsigned int len);
+unsigned int ELFHash (const char* str, unsigned int len);
+unsigned int BKDRHash(const char* str, unsigned int len);
+unsigned int SDBMHash(const char* str, unsigned int len);
+unsigned int DJBHash (const char* str, unsigned int len);
+unsigned int DEKHash (const char* str, unsigned int len);
+unsigned int APHash  (const char* str, unsigned int len);
+
+
+#endif

+ 64 - 0
lib/cds/hash_functions.c

@@ -0,0 +1,64 @@
+#include <cds/hash_functions.h>
+#include <cds/GeneralHashFunctions.h>
+#include <cds/md5.h>
+
+/* hashval is 5 integers ! */
+void shahash(const char *str, int strsz, int *hashval);
+
+unsigned int hash_md5(const char *s, unsigned int len)
+{
+	MD5_CTX context;
+	unsigned char digest[16];
+	int i = 0;
+	unsigned int x = 0;
+	unsigned int result = 0;
+
+	MD5Init(&context);
+	MD5Update(&context, s, len);
+	MD5Final(digest, &context);
+
+	for (i = 0; i < sizeof(digest); i++) {
+		result = result ^ (digest[i] << x);
+		if (x == 0) x = 8;
+		else x = x << 8;
+	}
+	
+	return result;
+}
+
+unsigned int hash_sha(const char *s, unsigned int len)
+{
+	unsigned int u;
+	int hashval[5];
+
+	shahash(s, len, hashval);
+
+	u = hashval[0] ^ hashval[1] ^ hashval[2] ^ hashval[3];
+	return u;
+}
+
+unsigned int hash_domain(const char* s, unsigned int len)
+{
+	#define h_inc h+=v^(v>>3);
+		
+	const char* p;
+	register unsigned v;
+	register unsigned h = 0;/* _h from parameter? */
+
+	for(p=s; p<=(s+len-4); p+=4)
+	{
+		v=(*p<<24)+(p[1]<<16)+(p[2]<<8)+p[3];
+		h_inc;
+	}
+	
+	v=0;
+	for(;p<(s+len); p++)
+	{
+		v<<=8;
+		v+=*p;
+	}
+	h_inc;
+
+	return h;
+}
+

+ 19 - 0
lib/cds/hash_functions.h

@@ -0,0 +1,19 @@
+#ifndef __CDS_HASH_FUNCTIONS_H
+#define __CDS_HASH_FUNCTIONS_H
+
+/* hash functions taken from everywhere:
+ * http://www.partow.net/programming/hashfunctions/
+ * SER sources (from jabber module, from pa module, from core)
+ * RSA (MD5 - more in separate MD5 files)
+ *
+ * copyrights will be corrected soon
+ *
+ * */
+
+unsigned int hash_md5(const char *s, unsigned int len);
+unsigned int hash_sha(const char *s, unsigned int len);
+unsigned int hash_domain(const char* s, unsigned int len); /* :-) */
+
+#include <cds/GeneralHashFunctions.h>
+
+#endif

+ 1 - 1
lib/cds/hash_table.h

@@ -39,7 +39,7 @@ typedef struct ht_statistic {
 typedef const void* ht_key_t;
 typedef void* ht_data_t;
 
-typedef int (*hash_func_t)(ht_key_t k);
+typedef unsigned int (*hash_func_t)(ht_key_t k);
 typedef int (*key_cmp_func_t)(ht_key_t a, ht_key_t b);
 
 typedef struct ht_element {

+ 2 - 2
lib/cds/logger.h

@@ -42,8 +42,8 @@
 #include "dprint.h"
 
 #define ERROR_LOG(a,args...)		LOG(L_ERR,a,##args)
-#define DEBUG_LOG(a,args...)		LOG(L_INFO,a,##args)
-#define TRACE_LOG(a,args...)		LOG(L_INFO,a,##args)
+#define DEBUG_LOG(a,args...)		LOG(L_DBG,a,##args)
+#define TRACE_LOG(a,args...)		LOG(L_DBG,a,##args)
 #define WARN_LOG(a,args...)			LOG(L_WARN,a,##args)
 #define FLUSH_LOG()					do{}while(0)
 

+ 357 - 0
lib/cds/md5.c

@@ -0,0 +1,357 @@
+/* 
+
+$Id$
+
+MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
+
+Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
+rights reserved.
+
+License to copy and use this software is granted provided that it
+is identified as the "RSA Data Security, Inc. MD5 Message-Digest
+Algorithm" in all material mentioning or referencing this software
+or this function.
+
+License is also granted to make and use derivative works provided
+that such works are identified as "derived from the RSA Data
+Security, Inc. MD5 Message-Digest Algorithm" in all material
+mentioning or referencing the derived work.
+
+RSA Data Security, Inc. makes no representations concerning either
+the merchantability of this software or the suitability of this
+software for any particular purpose. It is provided "as is"
+without express or implied warranty of any kind.
+
+These notices must be retained in any copies of any part of this
+documentation and/or software.
+ */
+
+
+#include <string.h>
+#include <cds/md5global.h>
+#include <cds/md5.h>
+
+
+#define USE_MEM
+
+/* Constants for MD5Transform routine.
+ */
+
+
+
+
+#define S11 7
+#define S12 12
+#define S13 17
+#define S14 22
+#define S21 5
+#define S22 9
+#define S23 14
+#define S24 20
+#define S31 4
+#define S32 11
+#define S33 16
+#define S34 23
+#define S41 6
+#define S42 10
+#define S43 15
+#define S44 21
+
+static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
+static void Encode PROTO_LIST
+  ((unsigned char *, UINT4 *, unsigned int));
+static void Decode PROTO_LIST
+  ((UINT4 *, unsigned char *, unsigned int));
+static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
+static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
+
+static unsigned char PADDING[64] = {
+  0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+};
+
+/* F, G, H and I are basic MD5 functions.
+ */
+#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
+#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
+#define H(x, y, z) ((x) ^ (y) ^ (z))
+#define I(x, y, z) ((y) ^ ((x) | (~z)))
+
+/* ROTATE_LEFT rotates x left n bits.
+ */
+#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
+
+/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
+Rotation is separate from addition to prevent recomputation.
+ */
+#define FF(a, b, c, d, x, s, ac) { \
+ (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+  }
+#define GG(a, b, c, d, x, s, ac) { \
+ (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+  }
+#define HH(a, b, c, d, x, s, ac) { \
+ (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+  }
+#define II(a, b, c, d, x, s, ac) { \
+ (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+  }
+
+/* MD5 initialization. Begins an MD5 operation, writing a new context.
+ */
+void MD5Init (context)
+MD5_CTX *context;                                        /* context */
+{
+  context->count[0] = context->count[1] = 0;
+  /* Load magic initialization constants.
+*/
+  context->state[0] = 0x67452301;
+  context->state[1] = 0xefcdab89;
+  context->state[2] = 0x98badcfe;
+  context->state[3] = 0x10325476;
+}
+
+/* MD5 block update operation. Continues an MD5 message-digest
+  operation, processing another message block, and updating the
+  context.
+ */
+void MD5Update (context, input, inputLen)
+MD5_CTX *context;                                        /* context */
+unsigned char *input;                                /* input block */
+unsigned int inputLen;                     /* length of input block */
+{
+  unsigned int i, index, partLen;
+
+  /* Compute number of bytes mod 64 */
+  index = (unsigned int)((context->count[0] >> 3) & 0x3F);
+
+  /* Update number of bits */
+  if ((context->count[0] += ((UINT4)inputLen << 3))
+
+   < ((UINT4)inputLen << 3))
+ context->count[1]++;
+  context->count[1] += ((UINT4)inputLen >> 29);
+
+  partLen = 64 - index;
+
+  /* Transform as many times as possible.
+*/
+  if (inputLen >= partLen) {
+ MD5_memcpy
+   ((POINTER)&context->buffer[index], (POINTER)input, partLen);
+ MD5Transform (context->state, context->buffer);
+
+ for (i = partLen; i + 63 < inputLen; i += 64)
+   MD5Transform (context->state, &input[i]);
+
+ index = 0;
+  }
+  else
+ i = 0;
+
+  /* Buffer remaining input */
+  MD5_memcpy
+ ((POINTER)&context->buffer[index], (POINTER)&input[i],
+  inputLen-i);
+}
+
+/* MD5 finalization. Ends an MD5 message-digest operation, writing the
+  the message digest and zeroizing the context.
+ */
+void MD5Final (digest, context)
+unsigned char digest[16];                         /* message digest */
+MD5_CTX *context;                                       /* context */
+{
+  unsigned char bits[8];
+  unsigned int index, padLen;
+
+  /* Save number of bits */
+  Encode (bits, context->count, 8);
+
+  /* Pad out to 56 mod 64.
+*/
+  index = (unsigned int)((context->count[0] >> 3) & 0x3f);
+  padLen = (index < 56) ? (56 - index) : (120 - index);
+  MD5Update (context, PADDING, padLen);
+
+  /* Append length (before padding) */
+  MD5Update (context, bits, 8);
+
+  /* Store state in digest */
+  Encode (digest, context->state, 16);
+
+  /* Zeroize sensitive information.
+*/
+  MD5_memset ((POINTER)context, 0, sizeof (*context));
+}
+
+/* MD5 basic transformation. Transforms state based on block.
+ */
+static void MD5Transform (state, block)
+UINT4 state[4];
+unsigned char block[64];
+{
+  UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
+
+  Decode (x, block, 64);
+
+  /* Round 1 */
+  FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
+  FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
+  FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
+  FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
+  FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
+  FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
+  FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
+  FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
+  FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
+  FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
+  FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
+  FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
+  FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
+  FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
+  FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
+  FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
+
+ /* Round 2 */
+  GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
+  GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
+  GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
+  GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
+  GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
+  GG (d, a, b, c, x[10], S22,  0x2441453); /* 22 */
+  GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
+  GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
+  GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
+  GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
+  GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
+  GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
+  GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
+  GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
+  GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
+  GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
+
+  /* Round 3 */
+  HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
+  HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
+  HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
+  HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
+  HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
+  HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
+  HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
+  HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
+  HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
+  HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
+  HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
+  HH (b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
+  HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
+  HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
+  HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
+  HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
+
+  /* Round 4 */
+  II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
+  II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
+  II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
+  II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
+  II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
+  II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
+  II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
+  II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
+  II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
+  II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
+  II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
+  II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
+  II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
+  II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
+  II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
+  II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
+
+  state[0] += a;
+  state[1] += b;
+  state[2] += c;
+  state[3] += d;
+
+  /* Zeroize sensitive information.
+*/
+  MD5_memset ((POINTER)x, 0, sizeof (x));
+}
+
+/* Encodes input (UINT4) into output (unsigned char). Assumes len is
+  a multiple of 4.
+ */
+static void Encode (output, input, len)
+unsigned char *output;
+UINT4 *input;
+unsigned int len;
+{
+  unsigned int i, j;
+
+  for (i = 0, j = 0; j < len; i++, j += 4) {
+ output[j] = (unsigned char)(input[i] & 0xff);
+ output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
+ output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
+ output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
+  }
+}
+
+/* Decodes input (unsigned char) into output (UINT4). Assumes len is
+  a multiple of 4.
+ */
+static void Decode (output, input, len)
+UINT4 *output;
+unsigned char *input;
+unsigned int len;
+{
+  unsigned int i, j;
+
+  for (i = 0, j = 0; j < len; i++, j += 4)
+ output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
+   (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
+}
+
+/* Note: Replace "for loop" with standard memcpy if possible.
+ */
+
+static void MD5_memcpy (output, input, len)
+POINTER output;
+POINTER input;
+unsigned int len;
+{
+
+#ifndef USE_MEM
+  unsigned int i;
+
+  for (i = 0; i < len; i++)
+ output[i] = input[i];
+#else
+  memcpy( output, input, len );
+#endif
+}
+
+/* Note: Replace "for loop" with standard memset if possible.
+ */
+static void MD5_memset (output, value, len)
+POINTER output;
+int value;
+unsigned int len;
+{
+
+#ifndef USE_MEM
+  unsigned int i;
+  for (i = 0; i < len; i++)
+ ((char *)output)[i] = (char)value;
+#else
+  memset( output, value, len );
+#endif
+}
+

+ 45 - 0
lib/cds/md5.h

@@ -0,0 +1,45 @@
+/* MD5.H - header file for MD5C.C
+ * $Id$
+ */
+
+
+/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
+rights reserved.
+
+License to copy and use this software is granted provided that it
+is identified as the "RSA Data Security, Inc. MD5 Message-Digest
+Algorithm" in all material mentioning or referencing this software
+or this function.
+
+License is also granted to make and use derivative works provided
+that such works are identified as "derived from the RSA Data
+Security, Inc. MD5 Message-Digest Algorithm" in all material
+mentioning or referencing the derived work.
+
+RSA Data Security, Inc. makes no representations concerning either
+the merchantability of this software or the suitability of this
+software for any particular purpose. It is provided "as is"
+without express or implied warranty of any kind.
+
+These notices must be retained in any copies of any part of this
+documentation and/or software.
+ */
+
+#ifndef MD5_H
+#define MD5_H
+
+#include <cds/md5global.h>
+
+/* MD5 context. */
+typedef struct {
+  UINT4 state[4];                                   /* state (ABCD) */
+  UINT4 count[2];        /* number of bits, modulo 2^64 (lsb first) */
+  unsigned char buffer[64];                         /* input buffer */
+} MD5_CTX;
+
+void MD5Init PROTO_LIST ((MD5_CTX *));
+void MD5Update PROTO_LIST
+  ((MD5_CTX *, unsigned char *, unsigned int));
+void MD5Final PROTO_LIST ((unsigned char [16], MD5_CTX *));
+
+#endif /* MD5_H */

+ 38 - 0
lib/cds/md5global.h

@@ -0,0 +1,38 @@
+/* GLOBAL.H - RSAREF types and constants
+ *
+ */
+
+
+/* PROTOTYPES should be set to one if and only if the compiler supports
+  function argument prototyping.
+The following makes PROTOTYPES default to 0 if it has not already
+  been defined with C compiler flags.
+ */
+#ifndef MD5GLOBAL_H
+#define MD5GLOBAL_H
+
+
+#ifndef PROTOTYPES
+#define PROTOTYPES 0
+#endif
+
+/* POINTER defines a generic pointer type */
+typedef unsigned char *POINTER;
+
+/* UINT2 defines a two byte word */
+typedef unsigned short int UINT2;
+
+/* UINT4 defines a four byte word */
+typedef unsigned int UINT4;
+
+/* PROTO_LIST is defined depending on how PROTOTYPES is defined above.
+If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it
+  returns an empty list.
+ */
+#if PROTOTYPES
+#define PROTO_LIST(list) list
+#else
+#define PROTO_LIST(list) ()
+#endif
+
+#endif /* MD5GLOBAL_H */

+ 90 - 0
lib/cds/md5utils.c

@@ -0,0 +1,90 @@
+/* MDDRIVER.C - test driver for MD2, MD4 and MD5
+ *
+ */
+
+
+
+/* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
+rights reserved.
+
+RSA Data Security, Inc. makes no representations concerning either
+the merchantability of this software or the suitability of this
+software for any particular purpose. It is provided "as is"
+without express or implied warranty of any kind.
+
+These notices must be retained in any copies of any part of this
+documentation and/or software.
+ */
+
+/*
+
+jku: added support to deal with vectors
+
+*/
+
+#define MD 5
+
+#include <stdio.h>
+#include <time.h>
+#include <string.h>
+#include <cds/md5global.h>
+#include <cds/md5.h>
+#include <cds/md5utils.h>
+#include <cds/logger.h>
+/*#include "ut.h"*/
+
+
+/*static void MDString PROTO_LIST ((char *));*/
+
+#define MD_CTX MD5_CTX
+#define MDInit MD5Init
+#define MDUpdate MD5Update
+#define MDFinal MD5Final
+
+
+/* Digests a string array and store the result in dst; assumes
+  32 bytes in dst
+ */
+void MDStringArray (char *dst, str_t src[], int size)
+{
+	MD_CTX context;
+	unsigned char digest[16];
+ 	int i;
+	int len = 0;
+	char *s = NULL;
+
+/*
+#	ifdef EXTRA_DEBUG
+	int j;
+	int sum;
+#endif
+*/
+
+	MDInit (&context);
+	for (i=0; i<size; i++) {
+		s = src[i].s;
+		len = src[i].len;
+/*?		trim_len( len, s, src[i] );*/
+/*
+#		ifdef EXTRA_DEBUG
+		fprintf(stderr, "EXTRA_DEBUG: %d. (%d) {", i+1, len);
+		sum=0;
+		for (j=0; j<len; j++) {
+			fprintf( stderr, "%c ", *(s+j));
+			sum+=*(s+j);
+		}
+		for (j=0; j<len; j++) {
+			fprintf( stderr, "%d ", *(s+j));
+			sum+=*(s+j);
+		}
+		fprintf(stderr, " [%d]\n", sum );	
+#		endif
+*/
+  		MDUpdate (&context, s, len);
+  }
+  MDFinal (digest, &context);
+
+/*  string2hex(digest, 16, dst );
+  DBG("DEBUG: MD5 calculated: %.*s\n", MD5_LEN, dst );*/
+
+}

+ 39 - 0
lib/cds/md5utils.h

@@ -0,0 +1,39 @@
+/* 
+ * $Id$
+ *
+ *
+ * Copyright (C) 2001-2003 FhG Fokus
+ *
+ * This file is part of ser, a free SIP server.
+ *
+ * ser is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * For a license to use the ser software under conditions
+ * other than those described here, or to purchase support for this
+ * software, please contact iptel.org by e-mail at the following addresses:
+ *    [email protected]
+ *
+ * ser is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License 
+ * along with this program; if not, write to the Free Software 
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+
+#ifndef _MD5UTILS_H
+#define _MD5UTILS_H
+
+#include <cds/sstr.h>
+
+#define MD5_LEN	32
+
+void MDStringArray (char *dst, str_t src[], int size);
+
+#endif /* _MD5UTILS_H */

+ 236 - 0
lib/cds/sha.c

@@ -0,0 +1,236 @@
+/*
+ * $Id$
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * 
+ *  Gabber
+ *  Copyright (C) 1999-2000 Dave Smith & Julian Missig
+ *
+ */
+
+
+
+/* 
+   Implements the Secure Hash Algorithm (SHA1)
+
+   Copyright (C) 1999 Scott G. Miller
+
+   Released under the terms of the GNU General Public License v2
+   see file COPYING for details
+
+   Credits: 
+      Robert Klep <[email protected]>  -- Expansion function fix 
+	  Thomas "temas" Muldowney <[email protected]>:
+	  		-- shahash() for string fun
+			-- Will add the int32 stuff in a few
+	  		
+   ---
+   FIXME: This source takes int to be a 32 bit integer.  This
+   may vary from system to system.  I'd use autoconf if I was familiar
+   with it.  Anyone want to help me out?
+*/
+
+//#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <string.h>
+
+#ifndef MACOS
+#  include <sys/stat.h>
+#  include <sys/types.h>
+#endif
+#ifndef WIN32
+#  include <unistd.h>
+#  define INT64 long long
+#else
+#  define snprintf _snprintf
+#  define INT64 __int64
+#endif
+
+#define switch_endianness(x) (x<<24 & 0xff000000) | \
+                             (x<<8  & 0x00ff0000) | \
+                             (x>>8  & 0x0000ff00) | \
+                             (x>>24 & 0x000000ff)
+
+/* Initial hash values */
+#define Ai 0x67452301 
+#define Bi 0xefcdab89
+#define Ci 0x98badcfe
+#define Di 0x10325476
+#define Ei 0xc3d2e1f0
+
+/* SHA1 round constants */
+#define K1 0x5a827999
+#define K2 0x6ed9eba1
+#define K3 0x8f1bbcdc 
+#define K4 0xca62c1d6
+
+/* Round functions.  Note that f2() is used in both rounds 2 and 4 */
+#define f1(B,C,D) ((B & C) | ((~B) & D))
+#define f2(B,C,D) (B ^ C ^ D)
+#define f3(B,C,D) ((B & C) | (B & D) | (C & D))
+
+/* left circular shift functions (rotate left) */
+#define rol1(x) ((x<<1) | ((x>>31) & 1))
+#define rol5(A) ((A<<5) | ((A>>27) & 0x1f))
+#define rol30(B) ((B<<30) | ((B>>2) & 0x3fffffff))
+
+/*
+  Hashes 'data', which should be a pointer to 512 bits of data (sixteen
+  32 bit ints), into the ongoing 160 bit hash value (five 32 bit ints)
+  'hash'
+*/
+int 
+sha_hash(int *data, int *hash)  
+{
+  int W[80];
+  unsigned int A=hash[0], B=hash[1], C=hash[2], D=hash[3], E=hash[4];
+  unsigned int t, x, TEMP;
+
+  for (t=0; t<16; t++) 
+    {
+#ifndef WORDS_BIGENDIAN
+      W[t]=switch_endianness(data[t]);
+#else 
+      W[t]=data[t];
+#endif
+    }
+
+
+  /* SHA1 Data expansion */
+  for (t=16; t<80; t++) 
+    {
+      x=W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16];
+      W[t]=rol1(x);
+    }
+
+  /* SHA1 main loop (t=0 to 79) 
+   This is broken down into four subloops in order to use
+   the correct round function and constant */
+  for (t=0; t<20; t++) 
+    {
+      TEMP=rol5(A) + f1(B,C,D) + E + W[t] + K1;
+      E=D;
+      D=C;
+      C=rol30(B);
+      B=A;
+      A=TEMP;
+    }
+  for (; t<40; t++) 
+    {
+      TEMP=rol5(A) + f2(B,C,D) + E + W[t] + K2;
+      E=D;
+      D=C;
+      C=rol30(B);
+      B=A;
+      A=TEMP;
+    }
+  for (; t<60; t++) 
+    {
+      TEMP=rol5(A) + f3(B,C,D) + E + W[t] + K3;
+      E=D;
+      D=C;
+      C=rol30(B);
+      B=A;
+      A=TEMP;
+    }
+  for (; t<80; t++) 
+    {
+      TEMP=rol5(A) + f2(B,C,D) + E + W[t] + K4;
+      E=D;
+      D=C;
+      C=rol30(B);
+      B=A;
+      A=TEMP;
+    }
+  hash[0]+=A; 
+  hash[1]+=B;
+  hash[2]+=C;
+  hash[3]+=D;
+  hash[4]+=E;
+  return 0;
+}
+
+/*
+  Takes a pointer to a 160 bit block of data (five 32 bit ints) and
+  initializes it to the start constants of the SHA1 algorithm.  This
+  must be called before using hash in the call to sha_hash
+*/
+int 
+sha_init(int *hash) 
+{
+  hash[0]=Ai;
+  hash[1]=Bi;
+  hash[2]=Ci;
+  hash[3]=Di;
+  hash[4]=Ei;
+  return 0;
+}
+
+void shahash(const char *str, int strsz, int *hashval) 
+{
+	char read_buffer[65];
+	//int read_buffer[64];
+	int c=1, i;
+       
+	INT64 length=0;
+
+	sha_init(hashval);
+
+	if(strsz == 0) 
+	{
+	     memset(read_buffer, 0, 65);
+	     read_buffer[0] = 0x80;
+	     sha_hash((int *)read_buffer, hashval);
+	}
+
+	while (strsz>0) 
+	{
+		memset(read_buffer, 0, 65);
+		c = 64;
+		if (strsz < 64) c = strsz;
+		strncpy((char*)read_buffer, str, c);
+		/* c = strlen((char *)read_buffer); */
+		length+=c;
+		strsz-=c;
+		if (strsz<=0) 
+		{
+			length<<=3;	
+			read_buffer[c]=(char)0x80;
+			for (i=c+1; i<64; i++) 
+				read_buffer[i]=0;
+			if (c>55) 
+			{
+				/* we need to do an entire new block */
+				sha_hash((int *)read_buffer, hashval);
+				for (i=0; i<14; i++) 
+					((int*)read_buffer)[i]=0;
+			}      
+#ifndef WORDS_BIGENDIAN
+			for (i=0; i<8; i++) 
+			{
+				read_buffer[56+i]=(char)(length>>(56-(i*8))) & 0xff;
+			}
+#else	
+			memcpy(read_buffer+56, &length, 8);
+#endif
+		}
+		
+		sha_hash((int *)read_buffer, hashval);
+		str+=64;
+	}
+}

+ 30 - 0
lib/cds/sstr.c

@@ -197,3 +197,33 @@ char *str_strchr(const str_t *s, char c)
 	return NULL;
 }
 
+/* creates new string as concatenation of a and b */
+int str_concat(str_t *dst, str_t *a, str_t *b)
+{
+	int al;
+	int bl;
+	
+	if (!dst) return -1;
+	
+	al = str_len(a);
+	bl = str_len(b);
+	
+	dst->len = al + bl;
+	if (dst->len > 0) {
+		dst->s = (char *)cds_malloc(dst->len);
+		if (!dst->s) {
+			dst->len = 0;
+			return -1;
+		}
+	}
+	else {
+		dst->s = NULL;
+		dst->len = 0;
+		return 0;
+	}
+	
+	if (al) memcpy(dst->s, a->s, al);
+	if (bl) memcpy(dst->s + al, b->s, bl);
+	
+	return 0;
+}

+ 5 - 0
lib/cds/sstr.h

@@ -49,6 +49,8 @@ typedef struct {
 
 #define FMT_STR(str)	(str).len,((str).s ? (str).s : "")	
 
+#define str_len(ptr)	((ptr)?(ptr)->len:0)
+
 /** transalate zero-terminated string to str_t (both uses the input buffer!)*/
 str_t zt2str(char *str);
 
@@ -100,6 +102,9 @@ void str_clear(str_t *s);
 /** locate character in string */
 char *str_strchr(const str_t *s, char c);
 
+/* creates new string as concatenation of a and b */
+int str_concat(str_t *dst, str_t *a, str_t *b);
+
 #ifdef __cplusplus
 }
 #endif

+ 1 - 1
lib/xcap/Makefile

@@ -1,6 +1,6 @@
 DEFS     += 
 INCLUDES += -I/usr/include/libxml2 -I/usr/local/include/libxml2 -I/usr/local/include
-LIBS     += -L/usr/local/lib -lxml2 -lcurl
+LIBS     += -L/usr/local/lib -lxml2 -lcurl -lcds
 
 # name of result executable or library
 NAME = xcap