mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-02-18 05:44:14 -05: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
@ -38,12 +38,12 @@
|
||||
#include <string>
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <util/rsid.h>
|
||||
#include <retroshare/rstypes.h>
|
||||
|
||||
/*************** SEND INTERFACE *******************/
|
||||
|
||||
class CompressedChunkMap ;
|
||||
class Sha1CheckSum ;
|
||||
|
||||
class ftDataSend
|
||||
{
|
||||
|
@ -696,61 +696,5 @@ bool ftFileCreator::verifyChunk(uint32_t chunk_number,const Sha1CheckSum& sum)
|
||||
return true ;
|
||||
}
|
||||
|
||||
bool ftFileCreator::crossCheckChunkMap(const CRC32Map& ref,uint32_t& bad_chunks,uint32_t& incomplete_chunks)
|
||||
{
|
||||
{
|
||||
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
||||
|
||||
CompressedChunkMap map ;
|
||||
|
||||
// Start with a full map.
|
||||
ChunkMap::buildPlainMap(mSize,map) ;
|
||||
uint32_t nb_chunks = ref.size() ;
|
||||
static const uint32_t chunk_size = ChunkMap::CHUNKMAP_FIXED_CHUNK_SIZE ;
|
||||
|
||||
std::cerr << "ftFileCreator::crossCheckChunkMap(): comparing chunks..." << std::endl;
|
||||
|
||||
if(!locked_initializeFileAttrs() )
|
||||
return false ;
|
||||
|
||||
unsigned char *buff = new unsigned char[chunk_size] ;
|
||||
incomplete_chunks = 0 ;
|
||||
bad_chunks = 0 ;
|
||||
uint32_t len = 0 ;
|
||||
|
||||
for(uint32_t i=0;i<nb_chunks;++i)
|
||||
{
|
||||
printf(" Chunk %05d/%05d:",i,nb_chunks) ;
|
||||
|
||||
if(fseeko64(fd,(uint64_t)i * (uint64_t)chunk_size,SEEK_SET)==0 && (len = fread(buff,1,chunk_size,fd)) > 0)
|
||||
{
|
||||
uint32_t crc = RsDirUtil::rs_CRC32(buff,len) ;
|
||||
|
||||
printf(" crc: %08x, ref: %08x : ",crc,ref[i]) ;
|
||||
|
||||
if(crc != ref[i])
|
||||
{
|
||||
printf(" CRC ERROR!!\n") ;
|
||||
++bad_chunks ;
|
||||
map.reset(i) ;
|
||||
}
|
||||
else
|
||||
printf(" matched\n") ;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf(" cannot fseek!\n") ;
|
||||
++bad_chunks ;
|
||||
map.reset(i) ;
|
||||
}
|
||||
}
|
||||
|
||||
delete[] buff ;
|
||||
|
||||
chunkMap.setAvailabilityMap(map) ;
|
||||
}
|
||||
closeFile() ;
|
||||
return true ;
|
||||
}
|
||||
|
||||
|
||||
|
@ -71,12 +71,6 @@ class ftFileCreator: public ftFileProvider
|
||||
|
||||
bool hashReceivedData(std::string& hash) ;
|
||||
|
||||
// Checks the CRC32 of all chunks against the given CRC32 map. Re-flag the bad chunks as being void.
|
||||
// bad_chunks: count of achieved chunks that don't match the CRC
|
||||
// incomplete_chunks: count of any bad or not yet downloaded chunk
|
||||
//
|
||||
bool crossCheckChunkMap(const CRC32Map& ref,uint32_t& bad_chunks,uint32_t& incomplete_chunks) ;
|
||||
|
||||
// Sets all chunks to checking state
|
||||
//
|
||||
void forceCheck() ;
|
||||
|
@ -463,8 +463,6 @@ RsTurtleGenericTunnelItem *ftServer::deserialiseItem(void *data,uint32_t size) c
|
||||
case RS_TURTLE_SUBTYPE_FILE_DATA : return new RsTurtleFileDataItem(data,size) ;
|
||||
case RS_TURTLE_SUBTYPE_FILE_MAP_REQUEST : return new RsTurtleFileMapRequestItem(data,size) ;
|
||||
case RS_TURTLE_SUBTYPE_FILE_MAP : return new RsTurtleFileMapItem(data,size) ;
|
||||
case RS_TURTLE_SUBTYPE_FILE_CRC_REQUEST : return new RsTurtleFileCrcRequestItem(data,size) ;
|
||||
case RS_TURTLE_SUBTYPE_FILE_CRC : return new RsTurtleFileCrcItem(data,size) ;
|
||||
case RS_TURTLE_SUBTYPE_CHUNK_CRC_REQUEST : return new RsTurtleChunkCrcRequestItem(data,size) ;
|
||||
case RS_TURTLE_SUBTYPE_CHUNK_CRC : return new RsTurtleChunkCrcItem(data,size) ;
|
||||
|
||||
|
@ -92,10 +92,6 @@ ftTransferModule::ftTransferModule(ftFileCreator *fc, ftDataMultiplex *dm, ftCon
|
||||
//mChunkSize = 10000;
|
||||
desiredRate = FT_TM_MAX_PEER_RATE; /* 1MB/s ??? */
|
||||
actualRate = 0;
|
||||
_crcmap_state = FT_TM_CRC_MAP_STATE_NOCHECK ;
|
||||
_crcmap_last_asked_time = 0 ;
|
||||
_crcmap_last_tunnel_keepup = 0 ;
|
||||
_crcreq_source = "";
|
||||
|
||||
_last_activity_time_stamp = time(NULL) ;
|
||||
}
|
||||
|
@ -136,7 +136,6 @@ public:
|
||||
bool completeFileTransfer();
|
||||
bool isCheckingHash() ;
|
||||
void forceCheck() ;
|
||||
void addCRC32Map(const CRC32Map& map) ;
|
||||
|
||||
//interface to multiplex module
|
||||
bool recvFileData(const std::string& peerId, uint64_t offset, uint32_t chunk_size, void *data);
|
||||
@ -188,12 +187,6 @@ private:
|
||||
double desiredRate;
|
||||
double actualRate;
|
||||
|
||||
CRC32Map _crcmap ;
|
||||
uint32_t _crcmap_state ;
|
||||
time_t _crcmap_last_asked_time ;
|
||||
time_t _crcmap_last_tunnel_keepup ;
|
||||
std::string _crcreq_source ;
|
||||
|
||||
time_t _last_activity_time_stamp ;
|
||||
|
||||
ftFileStatus mFileStatus; //used for pause/resume file transfer
|
||||
|
@ -109,21 +109,6 @@ uint32_t RsTurtleChunkCrcRequestItem::serial_size()
|
||||
|
||||
return s ;
|
||||
}
|
||||
uint32_t RsTurtleFileCrcItem::serial_size()
|
||||
{
|
||||
uint32_t s = 0 ;
|
||||
|
||||
s += 8 ; // header
|
||||
s += 4 ; // tunnel id
|
||||
|
||||
s += 4 ; // size of _map
|
||||
s += 4 ; // size of _crcs
|
||||
|
||||
s += 4 * crc_map._crcs.size() ;
|
||||
s += 4 * crc_map._ccmap._map.size() ;
|
||||
|
||||
return s ;
|
||||
}
|
||||
bool RsTurtleFileMapRequestItem::serialize(void *data,uint32_t& pktsize)
|
||||
{
|
||||
uint32_t tlvsize = serial_size();
|
||||
@ -262,47 +247,7 @@ bool RsTurtleChunkCrcRequestItem::serialize(void *data,uint32_t& pktsize)
|
||||
|
||||
return ok;
|
||||
}
|
||||
bool RsTurtleFileCrcItem::serialize(void *data,uint32_t& pktsize)
|
||||
{
|
||||
#ifdef P3TURTLE_DEBUG
|
||||
std::cerr << "RsTurtleFileCrcItem::serialize(): serializing packet:" << std::endl ;
|
||||
print(std::cerr,2) ;
|
||||
#endif
|
||||
uint32_t tlvsize = serial_size();
|
||||
uint32_t offset = 0;
|
||||
|
||||
if (pktsize < tlvsize)
|
||||
return false; /* not enough space */
|
||||
|
||||
pktsize = tlvsize;
|
||||
|
||||
bool ok = true;
|
||||
|
||||
ok &= setRsItemHeader(data,tlvsize,PacketId(), tlvsize);
|
||||
|
||||
/* skip the header */
|
||||
offset += 8;
|
||||
|
||||
/* add mandatory parts first */
|
||||
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, tunnel_id);
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, crc_map._ccmap._map.size());
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, crc_map._crcs.size());
|
||||
|
||||
for(uint32_t i=0;i<crc_map._ccmap._map.size() && ok;++i)
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, crc_map._ccmap._map[i]);
|
||||
|
||||
for(uint32_t i=0;i<crc_map._crcs.size() && ok;++i)
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, crc_map._crcs[i]);
|
||||
|
||||
if (offset != tlvsize)
|
||||
{
|
||||
ok = false;
|
||||
std::cerr << "RsFileConfigSerialiser::serialiseTransfer() Size Error! " << std::endl;
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
bool RsTurtleChunkCrcItem::serialize(void *data,uint32_t& pktsize)
|
||||
{
|
||||
#ifdef P3TURTLE_DEBUG
|
||||
@ -328,11 +273,7 @@ bool RsTurtleChunkCrcItem::serialize(void *data,uint32_t& pktsize)
|
||||
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, tunnel_id);
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, chunk_number);
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, check_sum.fourbytes[0]);
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, check_sum.fourbytes[1]);
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, check_sum.fourbytes[2]);
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, check_sum.fourbytes[3]);
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, check_sum.fourbytes[4]);
|
||||
ok &= setRawSha1(data, tlvsize, &offset, check_sum);
|
||||
|
||||
if (offset != tlvsize)
|
||||
{
|
||||
@ -398,40 +339,6 @@ RsTurtleFileMapRequestItem::RsTurtleFileMapRequestItem(void *data,uint32_t pktsi
|
||||
#endif
|
||||
}
|
||||
|
||||
RsTurtleFileCrcItem::RsTurtleFileCrcItem(void *data,uint32_t pktsize)
|
||||
: RsTurtleGenericTunnelItem(RS_TURTLE_SUBTYPE_FILE_CRC)
|
||||
{
|
||||
setPriorityLevel(QOS_PRIORITY_RS_TURTLE_FILE_CRC) ;
|
||||
#ifdef P3TURTLE_DEBUG
|
||||
std::cerr << " type = file map item" << std::endl ;
|
||||
#endif
|
||||
uint32_t offset = 8; // skip the header
|
||||
|
||||
/* add mandatory parts first */
|
||||
|
||||
bool ok = true ;
|
||||
uint32_t s1,s2 ;
|
||||
ok &= getRawUInt32(data, pktsize, &offset, &tunnel_id);
|
||||
ok &= getRawUInt32(data, pktsize, &offset, &s1) ;
|
||||
ok &= getRawUInt32(data, pktsize, &offset, &s2) ;
|
||||
|
||||
crc_map._ccmap._map.resize(s1) ;
|
||||
crc_map._crcs.resize(s2) ;
|
||||
|
||||
for(uint32_t i=0;i<s1 && ok;++i)
|
||||
ok &= getRawUInt32(data, pktsize, &offset, &(crc_map._ccmap._map[i])) ;
|
||||
|
||||
for(uint32_t i=0;i<s2 && ok;++i)
|
||||
ok &= getRawUInt32(data, pktsize, &offset, &(crc_map._crcs[i])) ;
|
||||
|
||||
#ifdef WINDOWS_SYS // No Exceptions in Windows compile. (drbobs).
|
||||
#else
|
||||
if (offset != pktsize)
|
||||
throw std::runtime_error("Size error while deserializing.") ;
|
||||
if (!ok)
|
||||
throw std::runtime_error("Unknown error while deserializing.") ;
|
||||
#endif
|
||||
}
|
||||
RsTurtleChunkCrcItem::RsTurtleChunkCrcItem(void *data,uint32_t pktsize)
|
||||
: RsTurtleGenericTunnelItem(RS_TURTLE_SUBTYPE_CHUNK_CRC)
|
||||
{
|
||||
@ -446,11 +353,7 @@ RsTurtleChunkCrcItem::RsTurtleChunkCrcItem(void *data,uint32_t pktsize)
|
||||
bool ok = true ;
|
||||
ok &= getRawUInt32(data, pktsize, &offset, &tunnel_id) ;
|
||||
ok &= getRawUInt32(data, pktsize, &offset, &chunk_number) ;
|
||||
ok &= getRawUInt32(data, pktsize, &offset, &check_sum.fourbytes[0]) ;
|
||||
ok &= getRawUInt32(data, pktsize, &offset, &check_sum.fourbytes[1]) ;
|
||||
ok &= getRawUInt32(data, pktsize, &offset, &check_sum.fourbytes[2]) ;
|
||||
ok &= getRawUInt32(data, pktsize, &offset, &check_sum.fourbytes[3]) ;
|
||||
ok &= getRawUInt32(data, pktsize, &offset, &check_sum.fourbytes[4]) ;
|
||||
ok &= getRawSha1(data, pktsize, &offset, check_sum) ;
|
||||
|
||||
#ifdef WINDOWS_SYS // No Exceptions in Windows compile. (drbobs).
|
||||
#else
|
||||
@ -692,23 +595,7 @@ std::ostream& RsTurtleFileMapRequestItem::print(std::ostream& o, uint16_t)
|
||||
return o ;
|
||||
}
|
||||
|
||||
std::ostream& RsTurtleFileCrcItem::print(std::ostream& o, uint16_t)
|
||||
{
|
||||
o << "File CRC item:" << std::endl ;
|
||||
|
||||
o << " tunnel id : " << std::hex << tunnel_id << std::dec << std::endl ;
|
||||
o << " map : " ;
|
||||
|
||||
for(uint32_t i=0;i<crc_map._ccmap._map.size();++i)
|
||||
o << std::hex << crc_map._ccmap._map[i] << std::endl ;
|
||||
|
||||
o << " CRC : " ;
|
||||
|
||||
for(uint32_t i=0;i<crc_map._crcs.size();++i)
|
||||
o << std::hex << crc_map._crcs[i] << std::dec << std::endl ;
|
||||
|
||||
return o ;
|
||||
}
|
||||
std::ostream& RsTurtleFileCrcRequestItem::print(std::ostream& o, uint16_t)
|
||||
{
|
||||
o << "File CRC request item:" << std::endl ;
|
||||
|
@ -132,24 +132,6 @@ class RsTurtleChunkCrcRequestItem: public RsTurtleGenericTunnelItem
|
||||
virtual uint32_t serial_size() ;
|
||||
};
|
||||
|
||||
class RsTurtleFileCrcItem: public RsTurtleGenericTunnelItem
|
||||
{
|
||||
public:
|
||||
RsTurtleFileCrcItem() : RsTurtleGenericTunnelItem(RS_TURTLE_SUBTYPE_FILE_CRC) { setPriorityLevel(QOS_PRIORITY_RS_FILE_CRC);}
|
||||
RsTurtleFileCrcItem(void *data,uint32_t size) ; // deserialization
|
||||
|
||||
virtual bool shouldStampTunnel() const { return true ; }
|
||||
virtual Direction travelingDirection() const { return DIRECTION_CLIENT ; }
|
||||
|
||||
CRC32Map crc_map ;// Map info for the file in compressed format. Each *bit* in the array uint's says "I have" or "I don't have"
|
||||
// by default, we suppose the peer has all the chunks. This info will thus be and-ed
|
||||
// with the default file map for this source.
|
||||
|
||||
virtual std::ostream& print(std::ostream& o, uint16_t) ;
|
||||
virtual bool serialize(void *data,uint32_t& size) ;
|
||||
virtual uint32_t serial_size() ;
|
||||
};
|
||||
|
||||
class RsTurtleChunkCrcItem: public RsTurtleGenericTunnelItem
|
||||
{
|
||||
public:
|
||||
|
@ -26,6 +26,7 @@
|
||||
#ifndef PQI_HASH_
|
||||
#define PQI_HASH_
|
||||
|
||||
#include <util/rsid.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <string>
|
||||
#include <iomanip>
|
||||
|
@ -33,6 +33,7 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <stdint.h>
|
||||
#include <util/rsid.h>
|
||||
|
||||
#include <retroshare/rsflags.h>
|
||||
|
||||
@ -60,24 +61,24 @@ const uint32_t RS_CONFIG_DIRECTORY = 0x0002 ;
|
||||
const uint32_t RS_PGP_DIRECTORY = 0x0003 ;
|
||||
const uint32_t RS_DIRECTORY_COUNT = 0x0004 ;
|
||||
|
||||
class Sha1CheckSum
|
||||
{
|
||||
public:
|
||||
Sha1CheckSum() {}
|
||||
explicit Sha1CheckSum(const uint8_t *twenty_bytes_digest) ; // inits form a 20-bytes digest.
|
||||
explicit Sha1CheckSum(const std::string& fourty_bytes_string) ; // inits form a 40 bytes hexadecimal string.
|
||||
|
||||
static Sha1CheckSum random() ;
|
||||
|
||||
std::string toStdString() const ;
|
||||
|
||||
bool operator==(const Sha1CheckSum& s) const ;
|
||||
bool operator<(const Sha1CheckSum& s) const ;
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& out,const Sha1CheckSum& sum) { return out << sum.toStdString() ; }
|
||||
// private:
|
||||
uint32_t fourbytes[5] ;
|
||||
};
|
||||
//class Sha1CheckSum
|
||||
//{
|
||||
// public:
|
||||
// Sha1CheckSum() {}
|
||||
// explicit Sha1CheckSum(const uint8_t *twenty_bytes_digest) ; // inits form a 20-bytes digest.
|
||||
// explicit Sha1CheckSum(const std::string& fourty_bytes_string) ; // inits form a 40 bytes hexadecimal string.
|
||||
//
|
||||
// static Sha1CheckSum random() ;
|
||||
//
|
||||
// std::string toStdString() const ;
|
||||
//
|
||||
// bool operator==(const Sha1CheckSum& s) const ;
|
||||
// bool operator<(const Sha1CheckSum& s) const ;
|
||||
//
|
||||
// friend std::ostream& operator<<(std::ostream& out,const Sha1CheckSum& sum) { return out << sum.toStdString() ; }
|
||||
//// private:
|
||||
// uint32_t fourbytes[5] ;
|
||||
//};
|
||||
|
||||
class TransferInfo
|
||||
{
|
||||
@ -372,7 +373,7 @@ template<class CRCTYPE> class t_CRCMap
|
||||
friend class RsFileCRC32Map ;
|
||||
};
|
||||
|
||||
typedef t_CRCMap<uint32_t> CRC32Map ;
|
||||
//typedef t_CRCMap<uint32_t> CRC32Map ;
|
||||
typedef t_CRCMap<Sha1CheckSum> Sha1Map ;
|
||||
|
||||
/* class which encapsulates download details */
|
||||
|
@ -217,7 +217,7 @@ bool setRawUFloat32(void *data,uint32_t size,uint32_t *offset,float f)
|
||||
|
||||
bool getRawSha1(void *data,uint32_t size,uint32_t *offset,Sha1CheckSum& cs)
|
||||
{
|
||||
uint32_t len = 20 ; // SHA1 length in bytes
|
||||
uint32_t len = Sha1CheckSum::SIZE_IN_BYTES ; // SHA1 length in bytes = 20
|
||||
|
||||
/* check there is space for string */
|
||||
if (size < *offset + len)
|
||||
@ -227,18 +227,15 @@ bool getRawSha1(void *data,uint32_t size,uint32_t *offset,Sha1CheckSum& cs)
|
||||
}
|
||||
bool ok = true ;
|
||||
|
||||
ok = ok && getRawUInt32(data, size, offset, &cs.fourbytes[0]) ;
|
||||
ok = ok && getRawUInt32(data, size, offset, &cs.fourbytes[1]) ;
|
||||
ok = ok && getRawUInt32(data, size, offset, &cs.fourbytes[2]) ;
|
||||
ok = ok && getRawUInt32(data, size, offset, &cs.fourbytes[3]) ;
|
||||
ok = ok && getRawUInt32(data, size, offset, &cs.fourbytes[4]) ;
|
||||
cs = Sha1CheckSum(&((uint8_t*)data)[*offset]) ;
|
||||
*offset += Sha1CheckSum::SIZE_IN_BYTES ;
|
||||
|
||||
return ok ;
|
||||
}
|
||||
|
||||
bool setRawSha1(void *data,uint32_t size,uint32_t *offset,const Sha1CheckSum& cs)
|
||||
{
|
||||
uint32_t len = 20 ; // SHA1 length in bytes
|
||||
uint32_t len = Sha1CheckSum::SIZE_IN_BYTES ; // SHA1 length in bytes
|
||||
|
||||
if (size < *offset + len)
|
||||
{
|
||||
@ -248,12 +245,9 @@ bool setRawSha1(void *data,uint32_t size,uint32_t *offset,const Sha1CheckSum& cs
|
||||
|
||||
bool ok = true ;
|
||||
/* pack it in */
|
||||
ok = ok && setRawUInt32(data, size, offset, cs.fourbytes[0]);
|
||||
ok = ok && setRawUInt32(data, size, offset, cs.fourbytes[1]);
|
||||
ok = ok && setRawUInt32(data, size, offset, cs.fourbytes[2]);
|
||||
ok = ok && setRawUInt32(data, size, offset, cs.fourbytes[3]);
|
||||
ok = ok && setRawUInt32(data, size, offset, cs.fourbytes[4]);
|
||||
|
||||
memcpy((void *) &(((uint8_t *) data)[*offset]), cs.toByteArray(), Sha1CheckSum::SIZE_IN_BYTES) ;
|
||||
offset += Sha1CheckSum::SIZE_IN_BYTES ;
|
||||
|
||||
return true ;
|
||||
}
|
||||
bool getRawSSLId(void *data,uint32_t size,uint32_t *offset,SSLIdType& cs)
|
||||
|
@ -48,8 +48,6 @@
|
||||
*
|
||||
******************************************************************/
|
||||
|
||||
class Sha1CheckSum ;
|
||||
|
||||
bool getRawUInt8(void *data, uint32_t size, uint32_t *offset, uint8_t *out);
|
||||
bool setRawUInt8(void *data, uint32_t size, uint32_t *offset, uint8_t in);
|
||||
|
||||
|
@ -308,11 +308,14 @@ bool RsFileTransferSingleChunkCrcItem::serialise(void *data, uint32_t& pktsize)
|
||||
/* add mandatory parts first */
|
||||
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, hash);
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, chunk_number) ;
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, check_sum.fourbytes[0]) ;
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, check_sum.fourbytes[1]) ;
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, check_sum.fourbytes[2]) ;
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, check_sum.fourbytes[3]) ;
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, check_sum.fourbytes[4]) ;
|
||||
|
||||
setRawSha1(data,tlvsize,&offset,check_sum) ;
|
||||
|
||||
//ok &= setRawUInt32(data, tlvsize, &offset, check_sum.fourbytes[0]) ;
|
||||
//ok &= setRawUInt32(data, tlvsize, &offset, check_sum.fourbytes[1]) ;
|
||||
//ok &= setRawUInt32(data, tlvsize, &offset, check_sum.fourbytes[2]) ;
|
||||
//ok &= setRawUInt32(data, tlvsize, &offset, check_sum.fourbytes[3]) ;
|
||||
//ok &= setRawUInt32(data, tlvsize, &offset, check_sum.fourbytes[4]) ;
|
||||
|
||||
if (offset != tlvsize)
|
||||
{
|
||||
@ -776,11 +779,7 @@ RsFileTransferItem *RsFileTransferSerialiser::deserialise_RsFileTransferSingleCh
|
||||
offset += 8;
|
||||
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->hash); // file hash
|
||||
ok &= getRawUInt32(data, rssize, &offset, &(item->chunk_number));
|
||||
ok &= getRawUInt32(data, rssize, &offset, &(item->check_sum.fourbytes[0]));
|
||||
ok &= getRawUInt32(data, rssize, &offset, &(item->check_sum.fourbytes[1]));
|
||||
ok &= getRawUInt32(data, rssize, &offset, &(item->check_sum.fourbytes[2]));
|
||||
ok &= getRawUInt32(data, rssize, &offset, &(item->check_sum.fourbytes[3]));
|
||||
ok &= getRawUInt32(data, rssize, &offset, &(item->check_sum.fourbytes[4]));
|
||||
getRawSha1(data,rssize,&offset,item->check_sum) ;
|
||||
|
||||
if (offset != rssize)
|
||||
{
|
||||
|
@ -66,7 +66,7 @@ static const time_t LOBBY_LIST_AUTO_UPDATE_TIME = 121 ; // regularly as
|
||||
static const time_t DISTANT_CHAT_CLEANING_PERIOD = 60 ; // clean distant chat caches every 60 secs (remove old invites)
|
||||
static const time_t DISTANT_CHAT_KEEP_ALIVE_PERIOD = 10 ; // sens keep alive distant chat packets every 10 secs.
|
||||
static const uint32_t DISTANT_CHAT_AES_KEY_SIZE = 16 ; // size of AES encryption key for distant chat.
|
||||
static const uint32_t DISTANT_CHAT_HASH_SIZE = 20 ; // This is sha1 size in bytes.
|
||||
static const uint32_t DISTANT_CHAT_HASH_SIZE = Sha1CheckSum::SIZE_IN_BYTES ; // This is sha1 size in bytes.
|
||||
|
||||
static const uint32_t MAX_AVATAR_JPEG_SIZE = 32767; // Maximum size in bytes for an avatar. Too large packets
|
||||
// don't transfer correctly and can kill the system.
|
||||
@ -3394,7 +3394,7 @@ bool p3ChatService::createDistantChatInvite(const std::string& pgp_id,time_t tim
|
||||
unsigned char hash_bytes[DISTANT_CHAT_HASH_SIZE] ;
|
||||
RAND_bytes( hash_bytes, DISTANT_CHAT_HASH_SIZE) ;
|
||||
|
||||
std::string hash = t_RsGenericIdType<DISTANT_CHAT_HASH_SIZE>(hash_bytes).toStdString(false) ;
|
||||
std::string hash = Sha1CheckSum(hash_bytes).toStdString(false) ;
|
||||
|
||||
#ifdef DEBUG_DISTANT_CHAT
|
||||
std::cerr << "Created new distant chat invite: " << std::endl;
|
||||
@ -3513,7 +3513,7 @@ bool p3ChatService::initiateDistantChatConnexion(const std::string& encrypted_st
|
||||
#ifdef DEBUG_DISTANT_CHAT
|
||||
std::cerr << "Signature successfuly verified!" << std::endl;
|
||||
#endif
|
||||
hash = t_RsGenericIdType<DISTANT_CHAT_HASH_SIZE>(data).toStdString(false) ;
|
||||
hash = Sha1CheckSum(data).toStdString(false) ;
|
||||
|
||||
startClientDistantChatConnection(hash,pgp_id.toStdString(),data+DISTANT_CHAT_HASH_SIZE) ;
|
||||
|
||||
|
@ -1840,7 +1840,7 @@ void p3IdService::CacheArbitrationDone(uint32_t mode)
|
||||
|
||||
//const int SHA_DIGEST_LENGTH = 20;
|
||||
|
||||
typedef t_RsGenericIdType<SHA_DIGEST_LENGTH> GxsIdPgpHash;
|
||||
typedef Sha1CheckSum GxsIdPgpHash;
|
||||
|
||||
static void calcPGPHash(const RsGxsId &id, const PGPFingerprintType &pgp, GxsIdPgpHash &hash);
|
||||
|
||||
|
@ -59,7 +59,7 @@ const int msgservicezone = 54319;
|
||||
|
||||
static const uint32_t RS_DISTANT_MSG_STATUS_TUNNEL_OK = 0x0001 ;
|
||||
static const uint32_t RS_DISTANT_MSG_STATUS_TUNNEL_DN = 0x0000 ;
|
||||
static const uint32_t DISTANT_MSG_HASH_SIZE = 20 ;
|
||||
static const uint32_t DISTANT_MSG_HASH_SIZE = Sha1CheckSum::SIZE_IN_BYTES ;
|
||||
|
||||
static const uint8_t ENCRYPTED_MSG_PROTOCOL_VERSION_01 = 0x37 ;
|
||||
|
||||
@ -2101,7 +2101,7 @@ bool p3MsgService::createDistantOfflineMessengingInvite(time_t time_of_validity,
|
||||
unsigned char hash_bytes[DISTANT_MSG_HASH_SIZE] ;
|
||||
RSRandom::random_bytes( hash_bytes, DISTANT_MSG_HASH_SIZE) ;
|
||||
|
||||
hash = t_RsGenericIdType<DISTANT_MSG_HASH_SIZE>(hash_bytes).toStdString(false) ;
|
||||
hash = Sha1CheckSum(hash_bytes).toStdString(false) ;
|
||||
|
||||
DistantMessengingInvite invite ;
|
||||
invite.time_of_validity = time_of_validity + time(NULL);
|
||||
|
@ -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…
x
Reference in New Issue
Block a user