mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-10-01 02:35:48 -04:00
improved temporary memory class
git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@8094 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
e58c9de067
commit
d8b276b7e2
@ -22,6 +22,7 @@
|
||||
*/
|
||||
|
||||
#include <util/rswin.h>
|
||||
#include <util/rsmemory.h>
|
||||
#include "dbase/findex.h"
|
||||
#include "retroshare/rsexpr.h"
|
||||
#include "util/rsdir.h"
|
||||
@ -767,8 +768,12 @@ int FileIndex::loadIndex(const std::string& filename, const RsFileHash& expected
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string s ;
|
||||
|
||||
{
|
||||
/* load file into memory, close file */
|
||||
uint8_t *compressed_data = new uint8_t[size] ;
|
||||
RsTemporaryMemory compressed_data(size) ;
|
||||
|
||||
if(!compressed_data)
|
||||
{
|
||||
std::cerr << "FileIndex::loadIndex(): can't allocate memory for " << size << " bytes." << std::endl;
|
||||
@ -780,26 +785,25 @@ int FileIndex::loadIndex(const std::string& filename, const RsFileHash& expected
|
||||
{
|
||||
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);
|
||||
|
||||
// /* 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)
|
||||
{
|
||||
@ -821,15 +825,15 @@ int FileIndex::loadIndex(const std::string& filename, const RsFileHash& expected
|
||||
std::cerr << "FileIndex::loadIndex() Decompression failed! Fileindex can't be read." << std::endl;
|
||||
return 0 ;
|
||||
}
|
||||
std::string s((char *)uncompressed_data,uncompressed_data_size) ;
|
||||
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;
|
||||
|
||||
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();
|
||||
|
||||
|
@ -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.") ;
|
||||
|
@ -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&) {}
|
||||
};
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user