Re: [xslt] [PATCH] new crypto namespace and hash functions for libexslt



On Tue, May 04, 2004 at 10:36:25AM -0400, joel reed wrote:
> the attached patch implements a new crypto namespace for libexslt.
> the following stylesheet shows the 3 implemented functions: md5sum,
> sha1sum, and ripemd160sum:

and now for the patch. doh!

jr
--- libxslt-1.1.6-orig/configure.in	2004-04-18 18:38:39.000000000 -0400
+++ libxslt-1.1.6/configure.in	2004-05-04 09:13:18.000000000 -0400
@@ -200,6 +200,12 @@ AC_SUBST(pythondir)
 AC_SUBST(PYTHON_SUBDIR)
 
 dnl
+dnl Check for openssl 
+dnl
+
+PKG_CHECK_MODULES(OPENSSL, openssl)
+
+dnl
 dnl Debug for DV (-Wunreachable-code)
 dnl
 if [[ "${LOGNAME}" = "veillard" -a "`pwd`" = "/u/veillard/XSLT" ]] || \
@@ -408,7 +414,7 @@ AC_SUBST(XSLT_LIBS)
 
 EXSLT_LIBDIR='-L${libdir}'
 EXSLT_INCLUDEDIR='-I${includedir}'
-EXSLT_LIBS="-lexslt $XSLT_LIBS"
+EXSLT_LIBS="-lexslt $XSLT_LIBS $OPENSSL_LIBS"
 AC_SUBST(EXSLT_LIBDIR)
 AC_SUBST(EXSLT_INCLUDEDIR)
 AC_SUBST(EXSLT_LIBS)
--- libxslt-1.1.6-orig/libexslt/crypto.c	1969-12-31 19:00:00.000000000 -0500
+++ libxslt-1.1.6/libexslt/crypto.c	2004-05-04 09:58:44.000000000 -0400
@@ -0,0 +1,183 @@
+#define IN_LIBEXSLT
+#include "libexslt/libexslt.h"
+
+#if defined(WIN32) && !defined (__CYGWIN__) && (!__MINGW32__)
+#include <win32config.h>
+#else
+#include "config.h"
+#endif
+
+#include <openssl/md5.h>
+#include <openssl/sha.h>
+#include <openssl/ripemd.h>
+
+#include <libxml/tree.h>
+#include <libxml/xpath.h>
+#include <libxml/xpathInternals.h>
+#include <libxml/parser.h>
+#include <libxml/encoding.h>
+#include <libxml/uri.h>
+
+#include <libxslt/xsltconfig.h>
+#include <libxslt/xsltutils.h>
+#include <libxslt/xsltInternals.h>
+#include <libxslt/extensions.h>
+
+#include "exslt.h"
+
+void DIGEST_TO_HEX(const unsigned char* hash, int hashlen, 
+									 unsigned char* hex, int hexlen)
+{
+				static const char bin2hex[] = { '0', '1', '2', '3', 
+																				'4', '5', '6', '7', 
+																				'8', '9', 'a', 'b', 
+																				'c', 'd', 'e', 'f' }; 
+
+				unsigned char lo, hi;
+				int i, pos;
+				for (i = 0, pos = 0; (i < hashlen && pos < hexlen); i++)
+				{
+								lo = hash[i] & 0xf;
+								hi = hash[i] >> 4;
+								hex[pos++] = bin2hex[hi];
+								hex[pos++] = bin2hex[lo];
+				}
+
+				hex[pos] = '\0';
+}
+
+/**
+ * exsltCryptoMd5sumFunction:
+ * @ctxt: an XPath parser context
+ * @nargs: the number of arguments
+ *
+ * computes the md5sum of a string
+ */
+static void
+exsltCryptoMd5sumFunction (xmlXPathParserContextPtr ctxt, int nargs) {
+
+		int str_len = 0;
+    xmlChar *str = NULL, *ret = NULL;
+		unsigned char hash[MD5_DIGEST_LENGTH+1];
+		unsigned char hex[MD5_DIGEST_LENGTH*2+1];
+
+    if ((nargs < 1) || (nargs > 2)) {
+	xmlXPathSetArityError(ctxt);
+	return;
+    }
+
+    str = xmlXPathPopString(ctxt);
+    str_len = xmlUTF8Strlen(str);
+
+    if (str_len == 0) {
+	xmlXPathReturnEmptyString(ctxt);
+	xmlFree(str);
+	return;
+    }
+	
+		MD5(str, str_len,	hash);
+		DIGEST_TO_HEX(hash, sizeof(hash)-1, hex, sizeof(hex)-1);
+
+    ret = xmlStrdup((xmlChar *)hex);
+    xmlXPathReturnString(ctxt, ret);
+
+    if (str != NULL)
+    xmlFree(str);
+}
+
+/**
+ * exsltCryptoSha1sumFunction:
+ * @ctxt: an XPath parser context
+ * @nargs: the number of arguments
+ *
+ * computes the sha1sum of a string
+ */
+static void
+exsltCryptoSha1sumFunction (xmlXPathParserContextPtr ctxt, int nargs) {
+
+		int str_len = 0;
+    xmlChar *str = NULL, *ret = NULL;
+		unsigned char hash[SHA_DIGEST_LENGTH+1];
+		unsigned char hex[SHA_DIGEST_LENGTH*2+1];
+
+    if ((nargs < 1) || (nargs > 2)) {
+	xmlXPathSetArityError(ctxt);
+	return;
+    }
+
+    str = xmlXPathPopString(ctxt);
+    str_len = xmlUTF8Strlen(str);
+
+    if (str_len == 0) {
+	xmlXPathReturnEmptyString(ctxt);
+	xmlFree(str);
+	return;
+    }
+	
+		SHA1(str, str_len,	hash);
+		DIGEST_TO_HEX(hash, sizeof(hash)-1, hex, sizeof(hex)-1);
+
+    ret = xmlStrdup((xmlChar *)hex);
+    xmlXPathReturnString(ctxt, ret);
+
+    if (str != NULL)
+    xmlFree(str);
+}
+
+/**
+ * exsltCryptoRipemd160sumFunction:
+ * @ctxt: an XPath parser context
+ * @nargs: the number of arguments
+ *
+ * computes the ripemd160 of a string
+ */
+static void
+exsltCryptoRipemd160sumFunction (xmlXPathParserContextPtr ctxt, int nargs) {
+
+		int str_len = 0;
+    xmlChar *str = NULL, *ret = NULL;
+		unsigned char hash[RIPEMD160_DIGEST_LENGTH+1];
+		unsigned char hex[RIPEMD160_DIGEST_LENGTH*2+1];
+
+    if ((nargs < 1) || (nargs > 2)) {
+	xmlXPathSetArityError(ctxt);
+	return;
+    }
+
+    str = xmlXPathPopString(ctxt);
+    str_len = xmlUTF8Strlen(str);
+
+    if (str_len == 0) {
+	xmlXPathReturnEmptyString(ctxt);
+	xmlFree(str);
+	return;
+    }
+	
+		RIPEMD160(str, str_len,	hash);
+		DIGEST_TO_HEX(hash, sizeof(hash)-1, hex, sizeof(hex)-1);
+
+    ret = xmlStrdup((xmlChar *)hex);
+    xmlXPathReturnString(ctxt, ret);
+
+    if (str != NULL)
+    xmlFree(str);
+}
+
+/**
+ * exsltCryptoRegister:
+ *
+ * Registers the EXSLT - Crypto module
+ */
+
+void
+exsltCryptoRegister (void) {
+    xsltRegisterExtModuleFunction ((const xmlChar *) "md5sum",
+				   EXSLT_CRYPTO_NAMESPACE,
+				   exsltCryptoMd5sumFunction);
+    xsltRegisterExtModuleFunction ((const xmlChar *) "sha1sum",
+				   EXSLT_CRYPTO_NAMESPACE,
+				   exsltCryptoSha1sumFunction);
+    xsltRegisterExtModuleFunction ((const xmlChar *) "ripemd160sum",
+				   EXSLT_CRYPTO_NAMESPACE,
+				   exsltCryptoRipemd160sumFunction);
+}
--- libxslt-1.1.6-orig/libexslt/exslt.c	2003-08-18 18:29:49.000000000 -0400
+++ libxslt-1.1.6/libexslt/exslt.c	2004-05-02 16:11:29.000000000 -0400
@@ -32,6 +32,7 @@ const int exsltLibxmlVersion = LIBXML_VE
 void
 exsltRegisterAll (void) {
     exsltCommonRegister();
+    exsltCryptoRegister();
     exsltMathRegister();
     exsltSetsRegister();
     exsltFuncRegister();
--- libxslt-1.1.6-orig/libexslt/exslt.h	2003-08-25 07:35:40.000000000 -0400
+++ libxslt-1.1.6/libexslt/exslt.h	2004-05-02 16:10:33.000000000 -0400
@@ -22,6 +22,12 @@ EXSLTPUBVAR const int exsltLibxmlVersion
  */
 #define EXSLT_COMMON_NAMESPACE ((const xmlChar *) "http://exslt.org/common";)
 /**
+ * EXSLT_CRYPTO_NAMESPACE:
+ *
+ * Namespace for EXSLT crypto functions
+ */
+#define EXSLT_CRYPTO_NAMESPACE ((const xmlChar *) "http://exslt.org/crypto";)
+/**
  * EXSLT_MATH_NAMESPACE:
  *
  * Namespace for EXSLT math functions
@@ -66,6 +72,7 @@ EXSLTPUBVAR const int exsltLibxmlVersion
 #define SAXON_NAMESPACE ((const xmlChar *) "http://icl.com/saxon";)
 
 EXSLTPUBFUN void EXSLTCALL exsltCommonRegister (void);
+EXSLTPUBFUN void EXSLTCALL exsltCryptoRegister (void);
 EXSLTPUBFUN void EXSLTCALL exsltMathRegister (void);
 EXSLTPUBFUN void EXSLTCALL exsltSetsRegister (void);
 EXSLTPUBFUN void EXSLTCALL exsltFuncRegister (void);
--- libxslt-1.1.6-orig/xsltproc/Makefile.am	2003-08-18 18:29:50.000000000 -0400
+++ libxslt-1.1.6/xsltproc/Makefile.am	2004-05-04 09:24:51.000000000 -0400
@@ -1,14 +1,14 @@
 INCLUDES = -I$(top_srcdir) -I$(top_srcdir)/libxslt -I$(top_srcdir)/libexslt \
            -I$(top_builddir) -I$(top_builddir)/libxslt \
-	   -I$(top_builddir)/libexslt $(LIBXML_CFLAGS) $(CFLAGS)
+	   -I$(top_builddir)/libexslt $(LIBXML_CFLAGS) $(CFLAGS) $(OPENSSL_CFLAGS)
 
 EXTRA_PROGRAMS=
 bin_PROGRAMS = xsltproc $(XSLTPROCDV)
 
 xsltproc_SOURCES = xsltproc.c
-xsltproc_LDFLAGS =
+xsltproc_LDFLAGS = $(OPENSSL_LIBS)
 xsltproc_DEPENDENCIES = $(DEPS)
-xsltproc_LDADD = $(LDADDS)
+xsltproc_LDADD = $(LDADDS) 
 
 DEPS = $(top_builddir)/libxslt/libxslt.la \
 	$(top_builddir)/libexslt/libexslt.la 
--- libxslt-1.1.6-orig/libexslt/Makefile.am	2003-10-23 12:26:34.000000000 -0400
+++ libxslt-1.1.6/libexslt/Makefile.am	2004-05-04 09:29:18.000000000 -0400
@@ -16,6 +16,7 @@ exsltinc_HEADERS =                      
 libexslt_la_SOURCES =                   \
 	exslt.c				\
 	common.c			\
+	crypto.c			\
 	math.c				\
 	sets.c				\
 	functions.c			\
@@ -29,6 +30,9 @@ libexslt_la_SOURCES =                   
 libexslt_la_LIBADD = $(top_builddir)/libxslt/libxslt.la $(EXTRA_LIBS)
 libexslt_la_LDFLAGS = -version-info @LIBEXSLT_VERSION_INFO@
 
+AM_CFLAGS=$(OPENSSL_CFLAGS)
+AM_LDFLAGS=$(OPENSSL_LIBS)
+
 man_MANS = libexslt.3
 
 EXTRA_DIST = $(man_MANS)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]