From 2c3dcd8f703801af62dca9557e2ef6097105d85f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Franti=C5=A1ek=20Dvo=C5=99=C3=A1k?= Date: Tue, 22 Apr 2008 10:53:26 +0000 Subject: [PATCH] Moving base64 encode/decode to the jobid module. --- org.glite.jobid.api-c/Makefile | 11 ++- org.glite.jobid.api-c/interface/strmd5.h | 11 +++ org.glite.jobid.api-c/src/strmd5.c | 35 ++++++- org.glite.jobid.api-c/test/base64_test.cpp | 72 ++++++++++++++ org.glite.jp.common/Makefile | 11 +-- org.glite.jp.common/interface/strmd5.h | 40 -------- org.glite.jp.common/src/attr.c | 2 +- org.glite.jp.common/src/strmd5.c | 147 ----------------------------- org.glite.jp.common/src/utils.c | 2 +- 9 files changed, 131 insertions(+), 200 deletions(-) create mode 100644 org.glite.jobid.api-c/test/base64_test.cpp delete mode 100755 org.glite.jp.common/interface/strmd5.h delete mode 100755 org.glite.jp.common/src/strmd5.c diff --git a/org.glite.jobid.api-c/Makefile b/org.glite.jobid.api-c/Makefile index 2408183..3150046 100644 --- a/org.glite.jobid.api-c/Makefile +++ b/org.glite.jobid.api-c/Makefile @@ -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 diff --git a/org.glite.jobid.api-c/interface/strmd5.h b/org.glite.jobid.api-c/interface/strmd5.h index c5d76b6..79a68c7 100755 --- a/org.glite.jobid.api-c/interface/strmd5.h +++ b/org.glite.jobid.api-c/interface/strmd5.h @@ -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 */ diff --git a/org.glite.jobid.api-c/src/strmd5.c b/org.glite.jobid.api-c/src/strmd5.c index 2645c61..baab53f 100755 --- a/org.glite.jobid.api-c/src/strmd5.c +++ b/org.glite.jobid.api-c/src/strmd5.c @@ -12,12 +12,12 @@ #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 index 0000000..0593da9 --- /dev/null +++ b/org.glite.jobid.api-c/test/base64_test.cpp @@ -0,0 +1,72 @@ +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#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 ; +} diff --git a/org.glite.jp.common/Makefile b/org.glite.jp.common/Makefile index 15183f4..445bd0e 100644 --- a/org.glite.jp.common/Makefile +++ b/org.glite.jp.common/Makefile @@ -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 index c354e6d..0000000 --- a/org.glite.jp.common/interface/strmd5.h +++ /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 */ diff --git a/org.glite.jp.common/src/attr.c b/org.glite.jp.common/src/attr.c index 117cb15..8905d09 100644 --- a/org.glite.jp.common/src/attr.c +++ b/org.glite.jp.common/src/attr.c @@ -4,7 +4,7 @@ #include #include -#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 index b07593f..0000000 --- a/org.glite.jp.common/src/strmd5.c +++ /dev/null @@ -1,147 +0,0 @@ -#include -#include -#include -#include - -#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); -} diff --git a/org.glite.jp.common/src/utils.c b/org.glite.jp.common/src/utils.c index a931d3f..0be3e8e 100644 --- a/org.glite.jp.common/src/utils.c +++ b/org.glite.jp.common/src/utils.c @@ -11,9 +11,9 @@ #include #include +#include "glite/jobid/strmd5.h" #include "types.h" #include "context.h" -#include "strmd5.h" #include "known_attr.h" #include "attr.h" -- 1.8.2.3