diff options
| author | zyp <zyp@localhost> | 2006-01-26 20:07:15 +0100 | 
|---|---|---|
| committer | zyp <zyp@localhost> | 2006-01-26 20:07:15 +0100 | 
| commit | 21559e9f4bd58b1504cc0fda7b0dceee0c833862 (patch) | |
| tree | 119240a883b5bda7e4c11f60dc39206e87b1e04f /ed2k | |
[project @ zyp-20060126190715-557e941315671b81]
[project @ 18]
Moved disccat and pyanidb to the right location.
Added pyqtmpc and ophidia.
Diffstat (limited to 'ed2k')
| -rw-r--r-- | ed2k/Makefile | 27 | ||||
| -rw-r--r-- | ed2k/__init__.py | 11 | ||||
| -rw-r--r-- | ed2k/ed2k.cpp | 68 | ||||
| -rw-r--r-- | ed2k/ed2k.h | 19 | ||||
| -rw-r--r-- | ed2k/ed2k_wrapper.cpp | 10 | 
5 files changed, 135 insertions, 0 deletions
diff --git a/ed2k/Makefile b/ed2k/Makefile new file mode 100644 index 0000000..07d23df --- /dev/null +++ b/ed2k/Makefile @@ -0,0 +1,27 @@ +CC=gcc +CFLAGS= +CPP=g++ +CPPFLAGS= +LD=gcc +LDFLAGS=-shared + +OBJECTS=ed2k_wrapper.o ed2k.o +TARGET=_ed2k.so +INCLUDE=-I /usr/include/python2.4/ +LIB=-l boost_python -l ssl + +all: $(TARGET) + +clean: +	-rm $(TARGET) $(OBJECTS) + +# M�-regel +$(TARGET): $(OBJECTS) Makefile +	$(LD) $(LDFLAGS) -o $(TARGET) $(OBJECTS) $(LIB) + +# Pseudoregler +%.o: %.c Makefile +	$(CC) $(CFLAGS) -o $@ -c $< $(INCLUDE) + +%.o: %.cpp Makefile +	$(CPP) $(CPPFLAGS) -o $@ -c $< $(INCLUDE)
\ No newline at end of file diff --git a/ed2k/__init__.py b/ed2k/__init__.py new file mode 100644 index 0000000..754b5c9 --- /dev/null +++ b/ed2k/__init__.py @@ -0,0 +1,11 @@ +from _ed2k import * + +def file_hash(name): +	e = Ed2k() +	f = open(name) +	data = f.read(32768)	 +	while data: +		e.update(data) +		data = f.read(32768) +	f.close() +	return e.digest() diff --git a/ed2k/ed2k.cpp b/ed2k/ed2k.cpp new file mode 100644 index 0000000..369f57e --- /dev/null +++ b/ed2k/ed2k.cpp @@ -0,0 +1,68 @@ +#include "ed2k.h" + +namespace Hex { +	static char* digits = "0123456789abcdef"; +	std::string hex(char* bin, int length) { +		std::string s(length * 2, ' '); +		for(int i = 0; i < length; i++) { +			s[i*2] = digits[(bin[i] >> 4) & 0xf]; +			s[i*2+1] = digits[bin[i] & 0xf]; +		} +		return s; +	} +	std::string hex(int bin) { +		std::string s(sizeof(int) * 2, ' '); +		for(int i = 0; i < sizeof(int) * 2; i++) { +			s[sizeof(int) * 2 - 1 - i] = digits[bin & 0xf]; +			bin = bin >> 4; +		} +		return s; +	} +} + +template<class T> +inline T min(T a, T b) { +	return (a > b) ? b : a; +} + +Ed2k::Ed2k() { +	MD4_Init(&md4_partial); +	MD4_Init(&md4_final); +	size_total = 0; +	digest_str = ""; +} + +void Ed2k::update(std::string data_str) { +	unsigned int length = data_str.length(); +	const char* data = data_str.c_str(); +	while(length) { +		if(!(size_total % (9500 * 1024)) && size_total) { +			unsigned char digest[16]; +			MD4_Final(digest, &md4_partial); +			MD4_Update(&md4_final, digest, 16); +			MD4_Init(&md4_partial); +		} +		int size = min<int>(length, (9500 * 1024) - (size_total % (9500 * 1024))); +		MD4_Update(&md4_partial, data, size); +		length -= size; +		data += size; +		size_total += size; +	}; +} + +std::string Ed2k::digest() { +	if(!digest_str.length()) { +		char* digest = new char[16]; +		if(size_total > (9500 * 1024)) { +			unsigned char digest_partial[16]; +			MD4_Final(digest_partial, &md4_partial); +			MD4_Update(&md4_final, digest_partial, 16); +			MD4_Final((unsigned char*)digest, &md4_final); +		} else { +			MD4_Final((unsigned char*)digest, &md4_partial); +		} +		digest_str = Hex::hex(digest, 16); +		delete digest; +	} +	return digest_str; +} diff --git a/ed2k/ed2k.h b/ed2k/ed2k.h new file mode 100644 index 0000000..97feb61 --- /dev/null +++ b/ed2k/ed2k.h @@ -0,0 +1,19 @@ +#ifndef _ED2K_H_ +#define _ED2K_H_ + +#include <string> +#include <openssl/md4.h> + +class Ed2k { +	private: +		MD4_CTX md4_partial; +		MD4_CTX md4_final; +		unsigned int size_total; +		std::string digest_str; +	public: +		Ed2k(); +		void update(std::string data); +		std::string digest(); +}; + +#endif // _ED2K_H_ diff --git a/ed2k/ed2k_wrapper.cpp b/ed2k/ed2k_wrapper.cpp new file mode 100644 index 0000000..0a9f8c8 --- /dev/null +++ b/ed2k/ed2k_wrapper.cpp @@ -0,0 +1,10 @@ +#include "ed2k.h" + +#include <boost/python.hpp> +using namespace boost::python; + +BOOST_PYTHON_MODULE(_ed2k) { +	class_<Ed2k>("Ed2k") +		.def("update", &Ed2k::update) +		.def("digest", &Ed2k::digest); +}  | 
