- changed Sha1CheckSum from hand-made class to instance of t_GenericIdType<> (should be backward compatible)

- updated ft/, p3msgservice and p3chatservice accordingly
- added a new class for Sha1Sum in t_GeneridIdType<>, and an additional template argument to make ids of identical size 
	incompatible.



git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@7082 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2014-02-01 19:10:45 +00:00
parent 10bf083ca3
commit 235280399f
19 changed files with 67 additions and 441 deletions

View file

@ -79,85 +79,6 @@ std::string RsDirUtil::getTopDir(const std::string& dir)
return top;
}
class CRC32Table
{
public:
inline uint32_t operator[](unsigned char i) const { return _data[i] ; }
CRC32Table()
{
_data = new uint32_t[256] ;
uint32_t polynmial = 0x04c11db7 ;
for(uint32_t i=0;i<256;++i)
{
uint32_t a = reflect(i,8)<<24 ;
for(uint32_t j=0;j<8;++j)
a = (a << 1)^ ( (a&(1<<31))?polynmial:0) ;
_data[i] = reflect(a,32) ;
}
}
// Swap bits 0-7, 1-6, etc.
//
uint32_t reflect(uint32_t ref,unsigned char ch)
{
uint32_t val = 0 ;
for(int i=1;i<ch+1;i++,ref>>=1)
if(ref & 1)
val |= (1 << (ch-i)) ;
return val ;
}
~CRC32Table() { delete[] _data ; }
private:
uint32_t *_data ;
};
static const CRC32Table crc32_table ;
uint32_t RsDirUtil::rs_CRC32(const unsigned char *data,uint32_t _len)
{
uint32_t a = 0xffffffff ;
int64_t len = _len ;
for(const unsigned char *buf=data;len>0;len--)
a = (a >> 8) ^ crc32_table[ (a & 0xff) ^ *buf++] ;
return a ^ 0xffffffff ;
}
bool RsDirUtil::crc32File(FILE *fd, uint64_t file_size,uint32_t chunk_size, CRC32Map& crc_map)
{
if(fseeko64(fd,0,SEEK_SET) != 0)
{
std::cerr << "crc32File(): cannot fseek to beginnign of the file !!" << std::endl ;
return false ;
}
crc_map = CRC32Map(file_size,chunk_size) ;
unsigned char *buff = new unsigned char[chunk_size] ;
int len ;
uint64_t total_size = 0 ;
uint32_t nb_chunk = 0;
while((len = fread(buff,(size_t)1, (size_t)chunk_size, fd)) > 0)
{
crc_map.set(nb_chunk++,rs_CRC32(buff,len)) ;
total_size += len ;
}
delete[] buff ;
if(file_size != total_size)
{
std::cerr << "RsDirUtil::crc32File(): ERROR. The file size does not match the announced size." << std::endl ;
return false ;
}
return true ;
}
const char *RsDirUtil::scanf_string_for_uint(int bytes)
{
const char *strgs[3] = { "%u","%lu","%llu" } ;
@ -799,93 +720,6 @@ Sha1CheckSum RsDirUtil::sha1sum(unsigned char *data, uint32_t size)
return Sha1CheckSum(sha_buf) ;
}
bool Sha1CheckSum::operator==(const Sha1CheckSum& s) const
{
for(int i=0;i<5;++i)
if(fourbytes[i] != s.fourbytes[i])
return false ;
return true ;
}
bool Sha1CheckSum::operator<(const Sha1CheckSum& s) const
{
for(int i=0;i<5;++i)
if(fourbytes[i] < s.fourbytes[i])
return true ;
else if(fourbytes[i] > s.fourbytes[i])
return false ;
return false ;
}
Sha1CheckSum Sha1CheckSum::random()
{
Sha1CheckSum s ;
for(int i=0;i<5;++i)
s.fourbytes[i] = RSRandom::random_u32() ;
return s;
}
std::string Sha1CheckSum::toStdString() const
{
static const char outl[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' } ;
std::string tmpout(40,' ');
for(int i = 0; i < 5; i++)
for(int j = 0; j < 4; j++)
{
int k = j + i*4 ;
uint8_t byte = (fourbytes[i] >> (8*j)) & 0xff ;
tmpout[2*k ] = outl[ (byte>>4) ] ;
tmpout[2*k+1] = outl[ byte & 0xf ] ;
//rs_sprintf_append(tmpout, "%02x", ((fourbytes[i] >> (8*j)) & 0xff ));
}
return tmpout;
}
Sha1CheckSum::Sha1CheckSum(const uint8_t *buf)
{
int n=0;
for(int i = 0; i < 5; i++)
{
fourbytes[i] = 0 ;
for(int j = 0; j < 4; j++)
fourbytes[i] += buf[n++] << (8*j) ;
}
}
Sha1CheckSum::Sha1CheckSum(const std::string& s)
{
int n=0;
if(s.length() != 40)
throw std::runtime_error("Sha1CheckSum::Sha1CheckSum: can only init from 40 chars hexadecimal string") ;
for(int i = 0; i < 5; ++i)
{
fourbytes[i] = 0 ;
for(int j = 0; j < 4; ++j)
{
for(int k=0;k<2;++k)
{
char b = s[n++] ;
if(b >= 'A' && b <= 'F')
fourbytes[i] += (b-'A'+10) << ((4*(1-k))+8*j) ;
else if(b >= 'a' && b <= 'f')
fourbytes[i] += (b-'a'+10) << ((4*(1-k))+8*j) ;
else if(b >= '0' && b <= '9')
fourbytes[i] += (b-'0') << ((4*(1-k))+8*j) ;
else
throw std::runtime_error("Sha1CheckSum::Sha1CheckSum: can't init from non pure hexadecimal string") ;
}
}
}
}
bool RsDirUtil::renameFile(const std::string& from, const std::string& to)
{
int loops = 0;