From 733ad438fbfc9cc8a04e75174ab0c190e8532a40 Mon Sep 17 00:00:00 2001 From: mr-alice Date: Fri, 12 Aug 2016 15:20:23 +0200 Subject: [PATCH] added IO for hash cache --- libretroshare/src/file_sharing/filelist_io.cc | 99 +++++++++++++++++++ libretroshare/src/file_sharing/filelist_io.h | 45 ++++++++- libretroshare/src/file_sharing/hash_cache.cc | 5 +- 3 files changed, 144 insertions(+), 5 deletions(-) diff --git a/libretroshare/src/file_sharing/filelist_io.cc b/libretroshare/src/file_sharing/filelist_io.cc index e8e1207f7..3cd153ae8 100644 --- a/libretroshare/src/file_sharing/filelist_io.cc +++ b/libretroshare/src/file_sharing/filelist_io.cc @@ -1,2 +1,101 @@ #include "filelist_io.h" +bool FileListIO::writeField( unsigned char*&buff,uint32_t& buff_size,uint32_t& offset,uint8_t section_tag,const unsigned char * val,uint32_t size) +{ + if(!checkSectionSize(buff,buff_size,offset,size)) + return false; + + if(!writeSectionHeader(buff,buff_size,offset,FILE_LIST_IO_TAG_BINARY_DATA,size)) + return false; + + memcpy(&buff[offset],val,size) ; + offset += size ; + + return true; +} + +bool FileListIO::readField (const unsigned char *buff,uint32_t buff_size,uint32_t& offset,uint8_t check_section_tag, unsigned char *& val,uint32_t& size) +{ + if(!readSectionHeader(buff,buff_size,offset,FILE_LIST_IO_TAG_BINARY_DATA,size)) + return false; + + val = (unsigned char *)rs_malloc(size) ; + + if(!val) + return false; + + memcpy(val,&buff[offset],size); + offset += size ; + + return true ; +} + +bool FileListIO::write125Size(unsigned char *data,uint32_t data_size,uint32_t& offset,uint32_t S) +{ + if(S < 192) + { + if(offset+1 > data_size) + return false; + + data[offset++] = (uint8_t)S ; + return true; + } + else if(S < 8384) + { + if(offset+2 > data_size) + return false; + + data[offset++] = (uint8_t)((S >> 8) + 192) ; + data[offset++] = (uint8_t)((S & 255) - 192) ; + + return true; + } + else + { + if(offset+5 > data_size) + return false; + + data[offset++] = 0xff ; + data[offset++] = (uint8_t)((S >> 24) & 255) ; + data[offset++] = (uint8_t)((S >> 16) & 255) ; + data[offset++] = (uint8_t)((S >> 8) & 255) ; + data[offset++] = (uint8_t)((S ) & 255) ; + + return true ; + } +} + +bool FileListIO::read125Size(const unsigned char *data,uint32_t data_size,uint32_t& offset,uint32_t& S) +{ + if(offset + 1 >= data_size) return false; + + uint8_t b1 = data[offset++] ; + + if(b1 < 192) + { + S = b1; + return true ; + } + if(offset + 1 >= data_size) return false; + + uint8_t b2 = data[offset++] ; + + if(b1 < 224) + { + S = ((b1-192) << 8) + b2 + 192 ; + return true; + } + + if(b1 != 0xff) + return false; + + if(offset + 3 >= data_size) return false; + + uint8_t b3 = data[offset++]; + uint8_t b4 = data[offset++]; + uint8_t b5 = data[offset++]; + + S = (b2 << 24) | (b3 << 16) | (b4 << 8) | b5 ; + return true; +} + diff --git a/libretroshare/src/file_sharing/filelist_io.h b/libretroshare/src/file_sharing/filelist_io.h index d8f5f0d10..9c5818025 100644 --- a/libretroshare/src/file_sharing/filelist_io.h +++ b/libretroshare/src/file_sharing/filelist_io.h @@ -1,6 +1,10 @@ #pragma once +#include #include +#include + +#include "util/rsmemory.h" // This file implements load/save of various fields used for file lists and directory content. // WARNING: the encoding is system-dependent, so this should *not* be used to exchange data between computers. @@ -15,6 +19,7 @@ static const uint8_t FILE_LIST_IO_TAG_MODIF_TS = 0x05 ; static const uint8_t FILE_LIST_IO_TAG_RECURS_MODIF_TS = 0x06 ; static const uint8_t FILE_LIST_IO_TAG_HASH_STORAGE_ENTRY = 0x07 ; static const uint8_t FILE_LIST_IO_TAG_UPDATE_TS = 0x08 ; +static const uint8_t FILE_LIST_IO_TAG_BINARY_DATA = 0x09 ; class FileListIO { @@ -34,7 +39,30 @@ public: return true; } + template + static bool readField(const unsigned char *buff,uint32_t buff_size,uint32_t& offset,uint8_t check_section_tag,T& val) + { + uint32_t section_size ; + + if(!readSectionHeader(buff,buff_size,offset,check_section_tag,section_size)) + return false; + + if(section_size != sizeof(T)) + return false ; + + memcpy(reinterpret_cast(&val),&buff[offset],sizeof(T)) ; + offset += sizeof(T) ; + + return true; + } + + static bool writeField( unsigned char*&buff,uint32_t& buff_size,uint32_t& offset,uint8_t section_tag,const unsigned char * val,uint32_t size) ; + static bool readField (const unsigned char *buff,uint32_t buff_size,uint32_t& offset,uint8_t check_section_tag, unsigned char *& val,uint32_t& size) ; + private: + static bool write125Size(unsigned char *data,uint32_t total_size,uint32_t& offset,uint32_t size) ; + static bool read125Size (const unsigned char *data,uint32_t total_size,uint32_t& offset,uint32_t& size) ; + static bool checkSectionSize(unsigned char *& buff,uint32_t& buff_size,uint32_t offset,uint32_t S) { if(offset + S > buff_size) @@ -48,11 +76,24 @@ private: return true ; } - static bool writeSectionHeader(unsigned char *& buff,uint32_t& buff_size,uint32_t offset,uint8_t section_tag,uint32_t S) + static bool writeSectionHeader(unsigned char *& buff,uint32_t& buff_size,uint32_t& offset,uint8_t section_tag,uint32_t S) { buff[offset++] = section_tag ; - if(!write125Size(buff,offset,buff_size,S)) return false ; + if(!write125Size(buff,buff_size,offset,S)) return false ; return true; } + + static bool readSectionHeader(const unsigned char *& buff,uint32_t buff_size,uint32_t& offset,uint8_t check_section_tag,uint32_t& S) + { + if(offset + 1 > buff_size) + return false ; + + uint8_t section_tag = buff[offset++] ; + + if(section_tag != check_section_tag) + return false; + + return read125Size(buff,buff_size,offset,S) ; + } }; diff --git a/libretroshare/src/file_sharing/hash_cache.cc b/libretroshare/src/file_sharing/hash_cache.cc index c86e6e8cc..743e959dd 100644 --- a/libretroshare/src/file_sharing/hash_cache.cc +++ b/libretroshare/src/file_sharing/hash_cache.cc @@ -237,7 +237,7 @@ void HashStorage::save() uint32_t total_size = 0; for(std::map::const_iterator it(mFiles.begin());it!=mFiles.end();++it) - writeHashStorageInfo(it->second,data,total_size,offset) ; + writeHashStorageInfo(data,total_size,offset,it->second) ; void *encryptedData = NULL ; int encDataLen = 0 ; @@ -277,6 +277,7 @@ bool HashStorage::readHashStorageInfo(const unsigned char *data,uint32_t total_s { unsigned char *section_data = NULL ; uint32_t section_size = 0; + uint32_t section_offset = 0; if(!FileListIO::readField(data,total_size,offset,FILE_LIST_IO_TAG_HASH_STORAGE_ENTRY,section_data,section_size)) return false; @@ -316,5 +317,3 @@ std::ostream& operator<<(std::ostream& o,const HashStorage::HashStorageInfo& inf { return o << info.hash << " " << info.size << " " << info.filename ; } - -