diff --git a/libretroshare/src/dbase/findex.cc b/libretroshare/src/dbase/findex.cc index c89f011eb..edf50d52c 100644 --- a/libretroshare/src/dbase/findex.cc +++ b/libretroshare/src/dbase/findex.cc @@ -22,6 +22,7 @@ */ #include +#include #include "dbase/findex.h" #include "retroshare/rsexpr.h" #include "util/rsdir.h" @@ -767,69 +768,72 @@ int FileIndex::loadIndex(const std::string& filename, const RsFileHash& expected return 0; } - /* load file into memory, close file */ - uint8_t *compressed_data = new uint8_t[size] ; - if(!compressed_data) + std::string s ; + { - std::cerr << "FileIndex::loadIndex(): can't allocate memory for " << size << " bytes." << std::endl; + /* load file into memory, close file */ + RsTemporaryMemory compressed_data(size) ; + + if(!compressed_data) + { + std::cerr << "FileIndex::loadIndex(): can't allocate memory for " << size << " bytes." << std::endl; + fclose(file); + return 0; + } + int bytesread = 0 ; + if(size != (bytesread = fread(compressed_data,1,size,file))) + { + std::cerr << "FileIndex::loadIndex(): can't read " << size << " bytes from file " << filename << ". Only " << bytesread << " actually read." << std::endl; + fclose(file); + return 0; + } fclose(file); - return 0; - } - int bytesread = 0 ; - if(size != (bytesread = fread(compressed_data,1,size,file))) - { - std::cerr << "FileIndex::loadIndex(): can't read " << size << " bytes from file " << filename << ". Only " << bytesread << " actually read." << std::endl; - fclose(file); - delete[] compressed_data; - return 0; - } - fclose(file); - RsFileHash tmpout = RsDirUtil::sha1sum((unsigned char *)(compressed_data),size); + RsFileHash tmpout = RsDirUtil::sha1sum((unsigned char *)(compressed_data),size); -// /* calculate hash */ -// unsigned char sha_buf[SHA_DIGEST_LENGTH]; -// SHA_CTX *sha_ctx = new SHA_CTX; -// SHA1_Init(sha_ctx); -// SHA1_Update(sha_ctx, s.c_str(), s.length()); -// SHA1_Final(&sha_buf[0], sha_ctx); -// delete sha_ctx; -// -// std::string tmpout; -// for(int i = 0; i < SHA_DIGEST_LENGTH; ++i) -// { -// rs_sprintf_append(tmpout, "%02x", (unsigned int) (sha_buf[i])); -// } + // /* calculate hash */ + // unsigned char sha_buf[SHA_DIGEST_LENGTH]; + // SHA_CTX *sha_ctx = new SHA_CTX; + // SHA1_Init(sha_ctx); + // SHA1_Update(sha_ctx, s.c_str(), s.length()); + // SHA1_Final(&sha_buf[0], sha_ctx); + // delete sha_ctx; + // + // std::string tmpout; + // for(int i = 0; i < SHA_DIGEST_LENGTH; ++i) + // { + // rs_sprintf_append(tmpout, "%02x", (unsigned int) (sha_buf[i])); + // } - if (!expectedHash.isNull() && expectedHash != tmpout) - { + if (!expectedHash.isNull() && expectedHash != tmpout) + { #ifdef FI_DEBUG - std::cerr << "FileIndex::loadIndex expected hash does not match" << std::endl; - std::cerr << "Expected hash: " << expectedHash << std::endl; - std::cerr << "Hash found: " << tmpout << std::endl; + std::cerr << "FileIndex::loadIndex expected hash does not match" << std::endl; + std::cerr << "Expected hash: " << expectedHash << std::endl; + std::cerr << "Hash found: " << tmpout << std::endl; #endif - return 0; + return 0; + } + // now uncompress the string + // + + uint8_t *uncompressed_data = NULL ; + unsigned int uncompressed_data_size = 0 ; + + if(!RsCompress::uncompress_memory_chunk(compressed_data,size,uncompressed_data,uncompressed_data_size)) + { + std::cerr << "FileIndex::loadIndex() Decompression failed! Fileindex can't be read." << std::endl; + return 0 ; + } + s = std::string((char *)uncompressed_data,uncompressed_data_size) ; + + std::cerr << " file = " << filename << std::endl; + std::cerr << " uncompressed size = " << uncompressed_data_size << std::endl; + std::cerr << " compressed size = " << size << std::endl; + std::cerr << " hash = " << tmpout << std::endl; + + free(uncompressed_data) ; } - // now uncompress the string - // - - uint8_t *uncompressed_data = NULL ; - unsigned int uncompressed_data_size = 0 ; - - if(!RsCompress::uncompress_memory_chunk(compressed_data,size,uncompressed_data,uncompressed_data_size)) - { - std::cerr << "FileIndex::loadIndex() Decompression failed! Fileindex can't be read." << std::endl; - return 0 ; - } - std::string s((char *)uncompressed_data,uncompressed_data_size) ; - - std::cerr << " file = " << filename << std::endl; - std::cerr << " uncompressed size = " << uncompressed_data_size << std::endl; - std::cerr << " compressed size = " << size << std::endl; - std::cerr << " hash = " << tmpout << std::endl; - - delete[] compressed_data ; - free(uncompressed_data) ; #define FIND_NEXT(s,start,end,c) end = s.find(c, start); if (end == std::string::npos) end = s.length(); diff --git a/libretroshare/src/grouter/p3grouter.cc b/libretroshare/src/grouter/p3grouter.cc index 7933d9ee4..45eb7f2db 100644 --- a/libretroshare/src/grouter/p3grouter.cc +++ b/libretroshare/src/grouter/p3grouter.cc @@ -922,9 +922,8 @@ bool p3GRouter::sendDataInTunnel(const TurtleVirtualPeerId& vpid,RsGRouterAbstra #endif uint32_t size = item->serial_size(); - unsigned char *data = NULL ; - TemporaryMemoryHolder f(data,size) ; // data will be freed on return, whatever the route taken. + RsTemporaryMemory data(size) ; // data will be freed on return, whatever the route taken. if(data == NULL) { @@ -1384,9 +1383,7 @@ bool p3GRouter::verifySignedDataItem(RsGRouterAbstractMsgItem *item) RsTlvSecurityKey signature_key ; uint32_t data_size = item->signed_data_size() ; - uint8_t *data = NULL; - - TemporaryMemoryHolder f(data,data_size) ; + RsTemporaryMemory data(data_size) ; if(data == NULL) throw std::runtime_error("Cannot allocate data.") ; diff --git a/libretroshare/src/util/rsmemory.h b/libretroshare/src/util/rsmemory.h index 8e422dd5b..a7ba409b8 100644 --- a/libretroshare/src/util/rsmemory.h +++ b/libretroshare/src/util/rsmemory.h @@ -8,22 +8,28 @@ // Usage: // // { -// unsigned char *mem = NULL ; -// TemporaryMemoryHolder mem_holder(mem,size) ; +// TemporaryMemoryHolder mem(size) ; +// +// if(mem != NULL) +// [ do something ] ; +// +// memcopy(mem, some_other_memory, size) ; // // [do something] +// // } // mem gets freed automatically // -class TemporaryMemoryHolder +class RsTemporaryMemory { public: - TemporaryMemoryHolder(unsigned char *& mem,size_t s) - : _mem(mem) + RsTemporaryMemory(size_t s) { _mem = (unsigned char *)malloc(s) ; } - ~TemporaryMemoryHolder() + operator unsigned char *() { return _mem ; } + + ~RsTemporaryMemory() { if(_mem != NULL) { @@ -33,7 +39,11 @@ class TemporaryMemoryHolder } private: - unsigned char *& _mem ; + unsigned char *_mem ; + + // make it noncopyable + RsTemporaryMemory& operator=(const RsTemporaryMemory&) { return *this ;} + RsTemporaryMemory(const RsTemporaryMemory&) {} };