Moving base64 encode/decode to the jobid module.
authorFrantišek Dvořák <valtri@civ.zcu.cz>
Tue, 22 Apr 2008 10:53:26 +0000 (10:53 +0000)
committerFrantišek Dvořák <valtri@civ.zcu.cz>
Tue, 22 Apr 2008 10:53:26 +0000 (10:53 +0000)
org.glite.jobid.api-c/Makefile
org.glite.jobid.api-c/interface/strmd5.h
org.glite.jobid.api-c/src/strmd5.c
org.glite.jobid.api-c/test/base64_test.cpp [new file with mode: 0644]
org.glite.jp.common/Makefile
org.glite.jp.common/interface/strmd5.h [deleted file]
org.glite.jp.common/src/attr.c
org.glite.jp.common/src/strmd5.c [deleted file]
org.glite.jp.common/src/utils.c

index 2408183..3150046 100644 (file)
@@ -34,6 +34,9 @@ LIBLOBJS:=${LIBOBJS:.o=.lo}
 
 LIB:=libglite_jobid.la
 
+TEST_LIBS:=-L${cppunit_prefix}/lib -lcppunit -ldl
+TEST_INC:=-I${cppunit_prefix}/include
+
 compile all: ${LIB}
 
 offset=0
@@ -43,8 +46,12 @@ version_info:=-version-info ${shell \
 ${LIB}: ${LIBOBJS}
        ${LINK} ${version_info} -o $@ ${LIBLOBJS} -rpath ${PREFIX}/lib 
 
-check: compile
-#
+check: compile base64_test
+       ./base64_test base64_test.xml
+
+base64_test: %: %.cpp compile
+       ${CXX} -c ${CFLAGS} ${TEST_INC} $<
+       ${LINKXX} -o $@ $@.o ${LIB} ${TEST_LIBS}
 
 clean:
        rm -rvf *.o *.lo .libs lib* *.c *.cpp *.h
index c5d76b6..79a68c7 100755 (executable)
@@ -1,6 +1,10 @@
 #ifndef _GLITE_STRMD5_H
 #define _GLITE_STRMD5_H
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 #ident "$Header$"
 
 /* Compute MD5 sum of the first argument.
@@ -25,4 +29,11 @@ char *str2md5(const char *src);
  */
 char *str2md5base64(const char *src);
 
+int base64_encode(const void *enc, int enc_size, char *out, int out_max_size);
+int base64_decode(const char *enc,char *out,int out_size);
+
+#ifdef __cplusplus
+};
+#endif
+
 #endif /* _GLITE_STRMD5_H */
index 2645c61..baab53f 100755 (executable)
 #include "md5_dgst.c"
 
 
-#warning Thread unsafe!
 static char mbuf[33];
+static const char* b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
+static char *b64r;
 
-static int base64_encode(const void *enc, int enc_size, char *out, int out_max_size)
+int base64_encode(const void *enc, int enc_size, char *out, int out_max_size)
 {
-    static const char* b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
 
     unsigned char* enc_buf = (unsigned char*)enc;
     int                  out_size = 0;
@@ -58,6 +58,35 @@ static int base64_encode(const void *enc, int enc_size, char *out, int out_max_s
     return -1;
 }
 
+int base64_decode(const char *enc,char *out,int max_out_size)
+{
+       unsigned int    bits = 0;
+       int     shift = 0;
+       int     out_size = 0;
+
+       if (!b64r) {
+               int     i;
+               b64r = calloc(128,1);
+
+               for (i=0; b64[i]; i++) b64r[(int)b64[i]] = i;
+       }
+
+       while (*enc && *enc != '=') {
+               bits <<= 6;
+               bits |= b64r[(int)*enc++];
+               shift += 6;
+
+               while (shift >= 8) {
+                       if (out_size >= max_out_size) return -1;
+                       shift -= 8;
+                       *out++ = (bits >> shift) & 0xff;
+                       out_size++;
+               }
+       }
+
+       return out_size;
+}
+
 char *strmd5(const char *s, unsigned char *digest)
 {
     MD5_CTX md5;
diff --git a/org.glite.jobid.api-c/test/base64_test.cpp b/org.glite.jobid.api-c/test/base64_test.cpp
new file mode 100644 (file)
index 0000000..0593da9
--- /dev/null
@@ -0,0 +1,72 @@
+#include <assert.h>
+#include <fstream>
+
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/CompilerOutputter.h>
+#include <cppunit/XmlOutputter.h>
+#include <cppunit/TestRunner.h>
+#include <cppunit/TestResult.h>
+#include <cppunit/TestResultCollector.h>
+
+#include "strmd5.h"
+
+class Base64Test: public  CppUnit::TestFixture
+{
+       CPPUNIT_TEST_SUITE(Base64Test);
+       CPPUNIT_TEST(test);
+       CPPUNIT_TEST_SUITE_END();
+public:
+       void test();
+};
+
+void Base64Test::test()
+{      
+       int     i;
+       unsigned char   in[2000], b[4000], out[2000];
+
+       srandom(0xDEAD);
+       in[0] = 'x';
+       for (i=1; i<2000; i++) {
+               char    s[20];
+               int     len;
+               sprintf(s,"%d",i);
+               in[i] = random() % 256;
+
+               std::cerr << '.';
+
+               base64_encode(in,i,(char *) b,sizeof b);
+               len = base64_decode((const char *) b,(char *) out,sizeof out);
+
+               CPPUNIT_ASSERT_MESSAGE(std::string("len"),i == len);
+               CPPUNIT_ASSERT_MESSAGE(std::string(s),!memcmp(in,out,i));
+       }
+       std::cerr << std::endl;
+}
+
+CPPUNIT_TEST_SUITE_REGISTRATION(Base64Test);
+
+
+int main (int argc,const char *argv[])
+{
+       CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
+
+       assert(argc == 2);
+       std::ofstream   xml(argv[1]);
+
+       CppUnit::TestResult controller;
+       CppUnit::TestResultCollector result;
+       controller.addListener( &result );
+
+       CppUnit::TestRunner runner;
+       runner.addTest(suite);
+       runner.run(controller);
+
+       CppUnit::XmlOutputter xout( &result, xml );
+       CppUnit::CompilerOutputter tout( &result, std::cout);
+       xout.write();
+       tout.write();
+
+       return result.wasSuccessful() ? 0 : 1 ;
+}
index 15183f4..445bd0e 100644 (file)
@@ -38,12 +38,12 @@ LINKXX:=libtool --mode=link ${CXX} ${LDFLAGS}
 INSTALL:=libtool --mode=install install
 COMPILE:=libtool --mode=compile ${CC} ${CFLAGS}
 
-HDRS:=types.h context.h strmd5.h attr.h known_attr.h backend.h builtin_plugins.h file_plugin.h
+HDRS:=types.h context.h attr.h known_attr.h backend.h builtin_plugins.h file_plugin.h
 
-SRCS:=context.c strmd5.c attr.c utils.c
+SRCS:=context.c attr.c utils.c
 OBJS:=${SRCS:.c=.lo}
 THROBJS:=${OBJS:.o=.thr.lo}
-LIBS:=-L${globus_prefix}/lib -lcrypto_${nothrflavour}
+LIBS:=-L${globus_prefix}/lib -lcrypto_${nothrflavour} -L${stagedir}/lib -lglite_jobid
 THRLIBS:=-L${globus_prefix}/lib -lcrypto_${thrflavour}
 
 commonlib:= libglite_jp_common_${nothrflavour}.la
@@ -63,11 +63,10 @@ ${commonlib}: ${OBJS}
 ${commonlib_thr}: ${THROBJS}
        ${LINK} -o $@ ${THROBJS} ${THRLIBS}
 
-check: base64_test type_test
-       ./base64_test base64_test.xml
+check: type_test
        ./type_test type_test.xml
 
-type_test base64_test: %: %.cpp compile
+type_test: %: %.cpp compile
        ${CXX} -c ${CFLAGS} ${TEST_INC} $<
        ${LINKXX} -o $@ $@.o ${commonlib} ${TEST_LIBS} 
 
diff --git a/org.glite.jp.common/interface/strmd5.h b/org.glite.jp.common/interface/strmd5.h
deleted file mode 100755 (executable)
index c354e6d..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-#ifndef _GLITE_STRMD5_H
-#define _GLITE_STRMD5_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#ident "$Header$"
-
-/* Compute MD5 sum of the first argument.
- * The sum is returned in the 16-byte array pointed to by 2nd argument
- *     (if not NULL)
- *
- * Return value: ASCII string of the sum, i.e. 32 characters [0-9a-f]
- *     (pointer to static area, changed by subsequent calls)
- */
-
-char *strmd5(const char *src, unsigned char *dst);
-
-/**
- * Returns: allocated 32bytes long ASCII string with md5 sum
- * of the first argument
- */
-char *str2md5(const char *src);
-
-/**
- * Returns: allocated 22bytes long ASCII string with md5 sum in base64
- * format of the source argument
- */
-char *str2md5base64(const char *src);
-
-int base64_encode(const void *enc, int enc_size, char *out, int out_max_size);
-int base64_decode(const char *enc,char *out,int out_size);
-
-#ifdef __cplusplus
-};
-#endif
-
-
-#endif /* _GLITE_STRMD5_H */
index 117cb15..8905d09 100644 (file)
@@ -4,7 +4,7 @@
 #include <string.h>
 #include <errno.h>
 
-#include "strmd5.h"
+#include "glite/jobid/strmd5.h"
 #include "types.h"
 #include "attr.h"
 #include "type_plugin.h"
diff --git a/org.glite.jp.common/src/strmd5.c b/org.glite.jp.common/src/strmd5.c
deleted file mode 100755 (executable)
index b07593f..0000000
+++ /dev/null
@@ -1,147 +0,0 @@
-#include <openssl/md5.h>
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "strmd5.h"
-
-static char mbuf[33];
-
-static const char* b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
-
-static char *b64r;
-
-int base64_encode(const void *enc, int enc_size, char *out, int out_max_size)
-{
-
-    unsigned char* enc_buf = (unsigned char*)enc;
-    int                  out_size = 0;
-    unsigned int  bits = 0;
-    unsigned int  shift = 0;
-
-    while ( out_size < out_max_size ) {
-       if ( enc_size>0 ) {
-           // Shift in byte
-           bits <<= 8;
-           bits |= *enc_buf;
-           shift += 8;
-           // Next byte
-           enc_buf++;
-           enc_size--;
-       } else if ( shift>0 ) {
-           // Pad last bits to 6 bits - will end next loop
-           bits <<= 6 - shift;
-           shift = 6;
-       } else {
-           // Terminate with Mime style '='
-           *out = '=';
-           out_size++;
-
-           return out_size;
-       }
-
-       // Encode 6 bit segments
-       while ( shift>=6 ) {
-           shift -= 6;
-           *out = b64[ (bits >> shift) & 0x3F ];
-           out++;
-           out_size++;
-       }
-    }
-
-    // Output overflow
-    return -1;
-}
-
-int base64_decode(const char *enc,char *out,int max_out_size)
-{
-       unsigned int    bits = 0;
-       int     shift = 0;
-       int     out_size = 0;
-
-       if (!b64r) {
-               int     i;
-               b64r = calloc(128,1);
-
-               for (i=0; b64[i]; i++) b64r[b64[i]] = i;
-       }
-
-       while (*enc && *enc != '=') {
-               bits <<= 6;
-               bits |= b64r[*enc++];
-               shift += 6;
-
-               while (shift >= 8) {
-                       if (out_size >= max_out_size) return -1;
-                       shift -= 8;
-                       *out++ = (bits >> shift) & 0xff;
-                       out_size++;
-               }
-       }
-
-       return out_size;
-}
-
-char *strmd5(const char *s, unsigned char *digest)
-{
-    MD5_CTX md5;
-    unsigned char d[16];
-    int        i;
-
-    MD5_Init(&md5);
-    MD5_Update(&md5,s,strlen(s));
-    MD5_Final(d,&md5);
-
-    if (digest) memcpy(digest,d,sizeof(d));
-
-    for (i=0; i<16; i++) {
-       int     dd = d[i] & 0x0f;
-       mbuf[2*i+1] = dd<10 ? dd+'0' : dd-10+'a';
-       dd = d[i] >> 4;
-       mbuf[2*i] = dd<10 ? dd+'0' : dd-10+'a';
-    }
-    mbuf[32] = 0;
-    return (char *) mbuf;
-}
-
-char *str2md5(const char *s)
-{
-    MD5_CTX md5;
-    unsigned char d[16];
-    char* ret = malloc(33);
-    int        i;
-
-    if (!ret)
-       return NULL;
-
-    MD5_Init(&md5);
-    MD5_Update(&md5, s, strlen(s));
-    MD5_Final(d, &md5);
-
-    for (i=0; i<16; i++) {
-       int  dd = d[i] & 0x0f;
-       ret[2*i+1] = dd<10 ? dd+'0' : dd-10+'a';
-       dd = d[i] >> 4;
-       ret[2*i] = dd<10 ? dd+'0' : dd-10+'a';
-    }
-    ret[32] = 0;
-    return ret;
-}
-
-char *str2md5base64(const char *s)
-{
-    MD5_CTX md5;
-    unsigned char d[16];
-    char buf[50];
-    int l;
-
-    MD5_Init(&md5);
-    MD5_Update(&md5, s, strlen(s));
-    MD5_Final(d, &md5);
-
-    l = base64_encode(d, 16, buf, sizeof(buf) - 1);
-    if (l < 1)
-       return NULL;
-    buf[l - 1] = 0;
-    return strdup(buf);
-}
index a931d3f..0be3e8e 100644 (file)
@@ -11,9 +11,9 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
+#include "glite/jobid/strmd5.h"
 #include "types.h"
 #include "context.h"
-#include "strmd5.h"
 #include "known_attr.h"
 #include "attr.h"