mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-03 06:35:08 -04:00
- 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:
parent
10bf083ca3
commit
235280399f
19 changed files with 67 additions and 441 deletions
|
@ -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;
|
||||
|
|
|
@ -75,10 +75,6 @@ bool renameFile(const std::string& from,const std::string& to) ;
|
|||
//
|
||||
uint32_t rs_CRC32(const unsigned char *data,uint32_t len) ;
|
||||
|
||||
// Computes the CRC32 map of a complete file, with given size and chunk size.
|
||||
//
|
||||
bool crc32File(FILE *f,uint64_t file_size,uint32_t chunk_size,CRC32Map& map) ;
|
||||
|
||||
// Returns %u, %lu, or %llu, depending on the size of unsigned int, unsigned long and unsigned long long on the current system.
|
||||
// Use as;
|
||||
// sscanf(string, RsDirUtil::scanf_string_for_uint( sizeof(X) ), &X) ;
|
||||
|
|
|
@ -34,7 +34,7 @@
|
|||
#include <stdint.h>
|
||||
#include <util/rsrandom.h>
|
||||
|
||||
template<uint32_t ID_SIZE_IN_BYTES> class t_RsGenericIdType
|
||||
template<uint32_t ID_SIZE_IN_BYTES,uint32_t UNIQUE_IDENTIFIER> class t_RsGenericIdType
|
||||
{
|
||||
public:
|
||||
t_RsGenericIdType() { memset(bytes,0,ID_SIZE_IN_BYTES) ;}
|
||||
|
@ -50,9 +50,9 @@ template<uint32_t ID_SIZE_IN_BYTES> class t_RsGenericIdType
|
|||
|
||||
// Random initialization. Can be useful for testing.
|
||||
//
|
||||
static t_RsGenericIdType<ID_SIZE_IN_BYTES> random()
|
||||
static t_RsGenericIdType<ID_SIZE_IN_BYTES,UNIQUE_IDENTIFIER> random()
|
||||
{
|
||||
t_RsGenericIdType<ID_SIZE_IN_BYTES> id ;
|
||||
t_RsGenericIdType<ID_SIZE_IN_BYTES,UNIQUE_IDENTIFIER> id ;
|
||||
|
||||
for(uint32_t i=0;i<ID_SIZE_IN_BYTES;++i)
|
||||
id.bytes[i] = RSRandom::random_u32() & 0xff ;
|
||||
|
@ -66,19 +66,19 @@ template<uint32_t ID_SIZE_IN_BYTES> class t_RsGenericIdType
|
|||
const unsigned char *toByteArray() const { return &bytes[0] ; }
|
||||
static const uint32_t SIZE_IN_BYTES = ID_SIZE_IN_BYTES ;
|
||||
|
||||
bool operator==(const t_RsGenericIdType<ID_SIZE_IN_BYTES>& fp) const
|
||||
bool operator==(const t_RsGenericIdType<ID_SIZE_IN_BYTES,UNIQUE_IDENTIFIER>& fp) const
|
||||
{
|
||||
for(uint32_t i=0;i<ID_SIZE_IN_BYTES;++i)
|
||||
if(fp.bytes[i] != bytes[i])
|
||||
return false ;
|
||||
return true ;
|
||||
}
|
||||
bool operator!=(const t_RsGenericIdType<ID_SIZE_IN_BYTES>& fp) const
|
||||
bool operator!=(const t_RsGenericIdType<ID_SIZE_IN_BYTES,UNIQUE_IDENTIFIER>& fp) const
|
||||
{
|
||||
return !operator==(fp) ;
|
||||
}
|
||||
|
||||
bool operator<(const t_RsGenericIdType<ID_SIZE_IN_BYTES>& fp) const
|
||||
bool operator<(const t_RsGenericIdType<ID_SIZE_IN_BYTES,UNIQUE_IDENTIFIER>& fp) const
|
||||
{
|
||||
for(uint32_t i=0;i<ID_SIZE_IN_BYTES;++i)
|
||||
if(fp.bytes[i] != bytes[i])
|
||||
|
@ -90,7 +90,7 @@ template<uint32_t ID_SIZE_IN_BYTES> class t_RsGenericIdType
|
|||
unsigned char bytes[ID_SIZE_IN_BYTES] ;
|
||||
};
|
||||
|
||||
template<uint32_t ID_SIZE_IN_BYTES> std::string t_RsGenericIdType<ID_SIZE_IN_BYTES>::toStdString(bool upper_case) const
|
||||
template<uint32_t ID_SIZE_IN_BYTES,uint32_t UNIQUE_IDENTIFIER> std::string t_RsGenericIdType<ID_SIZE_IN_BYTES,UNIQUE_IDENTIFIER>::toStdString(bool upper_case) const
|
||||
{
|
||||
static const char outh[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' } ;
|
||||
static const char outl[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' } ;
|
||||
|
@ -112,7 +112,7 @@ template<uint32_t ID_SIZE_IN_BYTES> std::string t_RsGenericIdType<ID_SIZE_IN_BYT
|
|||
return res ;
|
||||
}
|
||||
|
||||
template<uint32_t ID_SIZE_IN_BYTES> t_RsGenericIdType<ID_SIZE_IN_BYTES>::t_RsGenericIdType(const std::string& s)
|
||||
template<uint32_t ID_SIZE_IN_BYTES,uint32_t UNIQUE_IDENTIFIER> t_RsGenericIdType<ID_SIZE_IN_BYTES,UNIQUE_IDENTIFIER>::t_RsGenericIdType(const std::string& s)
|
||||
{
|
||||
int n=0;
|
||||
if(s.length() != ID_SIZE_IN_BYTES*2)
|
||||
|
@ -138,7 +138,7 @@ template<uint32_t ID_SIZE_IN_BYTES> t_RsGenericIdType<ID_SIZE_IN_BYTES>::t_RsGen
|
|||
}
|
||||
}
|
||||
|
||||
template<uint32_t ID_SIZE_IN_BYTES> t_RsGenericIdType<ID_SIZE_IN_BYTES>::t_RsGenericIdType(const unsigned char *mem)
|
||||
template<uint32_t ID_SIZE_IN_BYTES,uint32_t UNIQUE_IDENTIFIER> t_RsGenericIdType<ID_SIZE_IN_BYTES,UNIQUE_IDENTIFIER>::t_RsGenericIdType(const unsigned char *mem)
|
||||
{
|
||||
memcpy(bytes,mem,ID_SIZE_IN_BYTES) ;
|
||||
}
|
||||
|
@ -146,8 +146,17 @@ template<uint32_t ID_SIZE_IN_BYTES> t_RsGenericIdType<ID_SIZE_IN_BYTES>::t_RsGen
|
|||
static const int SSL_ID_SIZE = 16 ;
|
||||
static const int PGP_KEY_ID_SIZE = 8 ;
|
||||
static const int PGP_KEY_FINGERPRINT_SIZE = 20 ;
|
||||
static const int SHA1_SIZE = 20 ;
|
||||
|
||||
typedef t_RsGenericIdType<SSL_ID_SIZE> SSLIdType ;
|
||||
typedef t_RsGenericIdType<PGP_KEY_ID_SIZE> PGPIdType;
|
||||
typedef t_RsGenericIdType<PGP_KEY_FINGERPRINT_SIZE> PGPFingerprintType ;
|
||||
// These constants are random, but should be different, in order to make the various IDs incompatible with each other.
|
||||
//
|
||||
static const uint32_t RS_GENERIC_ID_SSL_ID_TYPE = 0x038439ff ;
|
||||
static const uint32_t RS_GENERIC_ID_PGP_ID_TYPE = 0x80339f4f ;
|
||||
static const uint32_t RS_GENERIC_ID_SHA1_ID_TYPE = 0x9540284e ;
|
||||
static const uint32_t RS_GENERIC_ID_PGP_FINGERPRINT_TYPE = 0x102943e3 ;
|
||||
|
||||
typedef t_RsGenericIdType< SSL_ID_SIZE , RS_GENERIC_ID_SSL_ID_TYPE> SSLIdType ;
|
||||
typedef t_RsGenericIdType< PGP_KEY_ID_SIZE , RS_GENERIC_ID_PGP_ID_TYPE> PGPIdType;
|
||||
typedef t_RsGenericIdType< SHA1_SIZE , RS_GENERIC_ID_SHA1_ID_TYPE> Sha1CheckSum ;
|
||||
typedef t_RsGenericIdType< PGP_KEY_FINGERPRINT_SIZE, RS_GENERIC_ID_PGP_FINGERPRINT_TYPE> PGPFingerprintType ;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue