2016-09-12 18:01:26 -04:00
|
|
|
/*
|
|
|
|
* RetroShare File lists IO methods.
|
|
|
|
*
|
|
|
|
* file_sharing/filelist_io.h
|
|
|
|
*
|
|
|
|
* Copyright 2016 Mr.Alice
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare.project@gmail.com".
|
|
|
|
*
|
|
|
|
*/
|
2016-08-27 17:56:23 -04:00
|
|
|
#include "retroshare/rsids.h"
|
2016-09-03 12:46:03 -04:00
|
|
|
#include "pqi/authssl.h"
|
|
|
|
#include "util/rsdir.h"
|
2016-08-27 17:56:23 -04:00
|
|
|
#include "serialiser/rsbaseserial.h"
|
2016-08-09 09:07:02 -04:00
|
|
|
#include "filelist_io.h"
|
|
|
|
|
2016-08-27 17:56:23 -04:00
|
|
|
template<> bool FileListIO::serialise(unsigned char *buff,uint32_t size,uint32_t& offset,const uint32_t & val) { return setRawUInt32(buff,size,&offset,val) ; }
|
2016-09-23 12:32:36 -04:00
|
|
|
template<> bool FileListIO::serialise(unsigned char *buff,uint32_t size,uint32_t& offset,const uint64_t & val) { return setRawUInt64(buff,size,&offset,val) ; }
|
2016-08-27 17:56:23 -04:00
|
|
|
template<> bool FileListIO::serialise(unsigned char *buff,uint32_t size,uint32_t& offset,const std::string & val) { return setRawString(buff,size,&offset,val) ; }
|
|
|
|
template<> bool FileListIO::serialise(unsigned char *buff,uint32_t size,uint32_t& offset,const Sha1CheckSum & val) { return val.serialise(buff,size,offset) ; }
|
|
|
|
|
|
|
|
template<> bool FileListIO::deserialise(const unsigned char *buff,uint32_t size,uint32_t& offset,uint32_t & val) { return getRawUInt32(const_cast<uint8_t*>(buff),size,&offset,&val) ; }
|
2016-09-23 12:32:36 -04:00
|
|
|
template<> bool FileListIO::deserialise(const unsigned char *buff,uint32_t size,uint32_t& offset,uint64_t & val) { return getRawUInt64(const_cast<uint8_t*>(buff),size,&offset,&val) ; }
|
2016-08-27 17:56:23 -04:00
|
|
|
template<> bool FileListIO::deserialise(const unsigned char *buff,uint32_t size,uint32_t& offset,std::string & val) { return getRawString(const_cast<uint8_t*>(buff),size,&offset,val) ; }
|
|
|
|
template<> bool FileListIO::deserialise(const unsigned char *buff,uint32_t size,uint32_t& offset,Sha1CheckSum & val) { return val.deserialise(const_cast<uint8_t*>(buff),size,offset) ; }
|
|
|
|
|
|
|
|
template<> uint32_t FileListIO::serial_size(const uint32_t & ) { return 4 ; }
|
2016-09-23 12:32:36 -04:00
|
|
|
template<> uint32_t FileListIO::serial_size(const uint64_t & ) { return 8 ; }
|
2016-08-27 17:56:23 -04:00
|
|
|
template<> uint32_t FileListIO::serial_size(const std::string & val) { return getRawStringSize(val) ; }
|
|
|
|
template<> uint32_t FileListIO::serial_size(const Sha1CheckSum & ) { return Sha1CheckSum::serial_size(); }
|
|
|
|
|
2016-08-12 09:20:23 -04:00
|
|
|
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;
|
|
|
|
|
2016-08-27 17:56:23 -04:00
|
|
|
if(!writeSectionHeader(buff,buff_size,offset,section_tag,size))
|
2016-08-12 09:20:23 -04:00
|
|
|
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)
|
|
|
|
{
|
2016-08-27 17:56:23 -04:00
|
|
|
if(!readSectionHeader(buff,buff_size,offset,check_section_tag,size))
|
2016-08-12 09:20:23 -04:00
|
|
|
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;
|
|
|
|
|
2016-09-14 16:41:02 -04:00
|
|
|
data[offset+1] = (S - 192) & 0xff ;
|
|
|
|
data[offset ] = ((S - 192 - data[offset+1]) >> 8) + 192;
|
|
|
|
|
|
|
|
offset += 2 ;
|
2016-08-12 09:20:23 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-09-03 12:46:03 -04:00
|
|
|
bool FileListIO::saveEncryptedDataToFile(const std::string& fname,const unsigned char *data,uint32_t total_size)
|
|
|
|
{
|
|
|
|
void *encryptedData = NULL ;
|
|
|
|
int encDataLen = 0 ;
|
|
|
|
|
|
|
|
if(!AuthSSL::getAuthSSL()->encrypt( encryptedData, encDataLen, data,total_size, AuthSSL::getAuthSSL()->OwnId()))
|
|
|
|
{
|
2016-09-25 12:51:33 -04:00
|
|
|
std::cerr << "Cannot encrypt encrypted file. Something's wrong." << std::endl;
|
2016-09-03 12:46:03 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *F = fopen( (fname+".tmp").c_str(),"wb" ) ;
|
|
|
|
|
|
|
|
if(!F)
|
|
|
|
{
|
2016-09-25 12:51:33 -04:00
|
|
|
std::cerr << "Cannot open encrypted file for writing: " << fname+".tmp" << std::endl;
|
2016-09-03 12:46:03 -04:00
|
|
|
|
|
|
|
free(encryptedData);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(fwrite(encryptedData,1,encDataLen,F) != (uint32_t)encDataLen)
|
|
|
|
{
|
2016-09-25 12:51:33 -04:00
|
|
|
std::cerr << "Could not write entire encrypted file. Out of disc space??" << std::endl;
|
2016-09-03 12:46:03 -04:00
|
|
|
fclose(F) ;
|
|
|
|
|
|
|
|
free(encryptedData);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(F) ;
|
|
|
|
|
|
|
|
RsDirUtil::renameFile(fname+".tmp",fname) ;
|
|
|
|
#ifdef FIM_DEBUG
|
|
|
|
std::cerr << "done." << std::endl ;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
free(encryptedData);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileListIO::loadEncryptedDataFromFile(const std::string& fname,unsigned char *& data,uint32_t& total_size)
|
|
|
|
{
|
|
|
|
uint64_t file_size ;
|
|
|
|
|
|
|
|
if(!RsDirUtil::checkFile( fname,file_size,false ) )
|
|
|
|
{
|
2016-10-12 17:31:32 -04:00
|
|
|
#ifdef FIM_DEBUG
|
2016-09-25 12:51:33 -04:00
|
|
|
std::cerr << "Encrypted file " << fname << " not available." << std::endl;
|
2016-10-12 17:31:32 -04:00
|
|
|
#endif
|
2016-09-03 12:46:03 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read the binary stream into memory.
|
|
|
|
//
|
|
|
|
RsTemporaryMemory buffer(file_size) ;
|
|
|
|
|
|
|
|
if(buffer == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
FILE *F = fopen( fname.c_str(),"rb") ;
|
|
|
|
if (!F)
|
|
|
|
{
|
2016-09-25 12:51:33 -04:00
|
|
|
std::cerr << "Cannot open encrypted file, filename " << fname << std::endl;
|
2016-09-03 12:46:03 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(fread(buffer,1,file_size,F) != file_size)
|
|
|
|
{
|
|
|
|
std::cerr << "Cannot read from file " + fname << ": something's wrong." << std::endl;
|
|
|
|
fclose(F) ;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fclose(F) ;
|
|
|
|
|
|
|
|
// now decrypt
|
|
|
|
void *decrypted_data =NULL;
|
|
|
|
int decrypted_data_size =0;
|
|
|
|
|
|
|
|
if(!AuthSSL::getAuthSSL()->decrypt(decrypted_data, decrypted_data_size, buffer, file_size))
|
|
|
|
{
|
2016-09-25 12:51:33 -04:00
|
|
|
std::cerr << "Cannot decrypt encrypted file. Something's wrong." << std::endl;
|
2016-09-03 12:46:03 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = (unsigned char*)decrypted_data ;
|
|
|
|
total_size = decrypted_data_size ;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|