Module: sems Branch: master Commit: 93a5978f38158e58479e81703e641e4a5925ba51 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sems/?a=commit;h=93a5978f38158e58479e81703e641e4a5925ba51
Author: Stefan Sayer <[email protected]> Committer: Stefan Sayer <[email protected]> Date: Sat Nov 27 20:07:33 2010 +0100 add MD5 hash generation of file to configreader --- core/AmConfigReader.cpp | 29 +++++++++++++++++++++++++++++ core/AmConfigReader.h | 2 ++ core/AmUtils.cpp | 19 +++++++++++++++++-- core/AmUtils.h | 7 ++++++- core/{plug-in/uac_auth => }/md5.cpp | 0 core/{plug-in/uac_auth => }/md5.h | 0 core/{plug-in/uac_auth => }/md5global.h | 0 7 files changed, 54 insertions(+), 3 deletions(-) diff --git a/core/AmConfigReader.cpp b/core/AmConfigReader.cpp index 72802d9..8c29e4a 100644 --- a/core/AmConfigReader.cpp +++ b/core/AmConfigReader.cpp @@ -28,8 +28,10 @@ #include "AmConfig.h" #include "log.h" #include "AmUtils.h" +#include "md5.h" #include <errno.h> +#include <fstream> #define IS_SPACE(c) ((c == ' ') || (c == '\t')) @@ -147,6 +149,33 @@ int AmConfigReader::loadFile(const string& path) return -1; } +bool AmConfigReader::getMD5(const string& path, string& md5hash, bool lowercase) { + std::ifstream data_file(path.c_str(), std::ios::in | std::ios::binary); + if (!data_file) { + DBG("could not read file '%s'\n", path.c_str()); + return false; + } + // that one is clever... + // (see http://www.gamedev.net/community/forums/topic.asp?topic_id=353162 ) + string file_data((std::istreambuf_iterator<char>(data_file)), + std::istreambuf_iterator<char>()); + + if (file_data.empty()) { + return false; + } + + MD5_CTX md5ctx; + MD5Init(&md5ctx); + MD5Update(&md5ctx, (unsigned char*)file_data.c_str(), file_data.length()); + unsigned char _md5hash[16]; + MD5Final(_md5hash, &md5ctx); + md5hash = ""; + for (size_t i=0;i<16;i++) { + md5hash+=char2hex(_md5hash[i], lowercase); + } + return true; +} + bool AmConfigReader::hasParameter(const string& param) { return (keys.find(param) != keys.end()); diff --git a/core/AmConfigReader.h b/core/AmConfigReader.h index 1303ce8..89a0c00 100644 --- a/core/AmConfigReader.h +++ b/core/AmConfigReader.h @@ -48,6 +48,8 @@ class AmConfigReader public: int loadFile(const string& path); + /** get md5 hash of file contents */ + bool getMD5(const string& path, string& md5hash, bool lowercase = true); bool hasParameter(const string& param); const string& getParameter(const string& param, const string& defval = ""); unsigned int getParameterInt(const string& param, unsigned int defval = 0); diff --git a/core/AmUtils.cpp b/core/AmUtils.cpp index 10c107f..3c1b58c 100644 --- a/core/AmUtils.cpp +++ b/core/AmUtils.cpp @@ -109,8 +109,22 @@ string long2str(long int val) } static char _int2hex_lookup[] = { '0', '1', '2', '3', '4', '5', '6' , '7', '8', '9','A','B','C','D','E','F' }; +static char _int2hex_lookup_l[] = { '0', '1', '2', '3', '4', '5', '6' , '7', '8', '9','a','b','c','d','e','f' }; -string int2hex(unsigned int val) +string char2hex(unsigned char val, bool lowercase) +{ + string res; + if (lowercase) { + res += _int2hex_lookup_l[val >> 4]; + res += _int2hex_lookup_l[val & 0x0f]; + } else { + res += _int2hex_lookup[val >> 4]; + res += _int2hex_lookup[val & 0x0f]; + } + return res; +} + +string int2hex(unsigned int val, bool lowercase) { unsigned int digit=0; @@ -120,7 +134,8 @@ string int2hex(unsigned int val) for(i=0; i<int(2*sizeof(int)); i++){ digit = val >> 4*(2*sizeof(int)-1); val = val << 4; - buffer[j++] = _int2hex_lookup[(unsigned char)digit]; + buffer[j++] = lowercase ? + _int2hex_lookup_l[(unsigned char)digit] : _int2hex_lookup[(unsigned char)digit]; } return string((char*)buffer); diff --git a/core/AmUtils.h b/core/AmUtils.h index 88c72f6..6b9562c 100644 --- a/core/AmUtils.h +++ b/core/AmUtils.h @@ -56,9 +56,14 @@ string int2str(int val); string long2str(long int val); /** + * Convert a a byte to a string using hexdecimal representation. + */ +string char2hex(unsigned char val, bool lowercase = false); + +/** * Convert an unsigned int to a string using hexdecimal representation. */ -string int2hex(unsigned int val); +string int2hex(unsigned int val, bool lowercase = false); /** * Convert an unsigned long to a string using hexdecimal representation. diff --git a/core/plug-in/uac_auth/md5.cpp b/core/md5.cpp similarity index 100% rename from core/plug-in/uac_auth/md5.cpp rename to core/md5.cpp diff --git a/core/plug-in/uac_auth/md5.h b/core/md5.h similarity index 100% rename from core/plug-in/uac_auth/md5.h rename to core/md5.h diff --git a/core/plug-in/uac_auth/md5global.h b/core/md5global.h similarity index 100% rename from core/plug-in/uac_auth/md5global.h rename to core/md5global.h _______________________________________________ Semsdev mailing list [email protected] http://lists.iptel.org/mailman/listinfo/semsdev
