/* * libretroshare/src/services: ftturtlefiletransferitem.cc * * Services for RetroShare. * * Copyright 2013 by Cyril Soler * * 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 "csoler@users.sourceforge.net". * */ #include #include #include #include uint32_t RsTurtleFileRequestItem::serial_size() { uint32_t s = 0 ; s += 8 ; // header s += 4 ; // tunnel id s += 8 ; // file offset s += 4 ; // chunk size return s ; } uint32_t RsTurtleFileDataItem::serial_size() { uint32_t s = 0 ; s += 8 ; // header s += 4 ; // tunnel id s += 8 ; // file offset s += 4 ; // chunk size s += chunk_size ; // actual data size. return s ; } uint32_t RsTurtleFileMapRequestItem::serial_size() { uint32_t s = 0 ; s += 8 ; // header s += 4 ; // tunnel id s += 4 ; // direction return s ; } uint32_t RsTurtleFileMapItem::serial_size() { uint32_t s = 0 ; s += 8 ; // header s += 4 ; // tunnel id s += 4 ; // direction s += 4 ; // compressed_map.size() s += 4 * compressed_map._map.size() ; return s ; } uint32_t RsTurtleChunkCrcItem::serial_size() { uint32_t s = 0 ; s += 8 ; // header s += 4 ; // tunnel id s += 4 ; // chunk number s += check_sum.serial_size() ; // check_sum return s ; } uint32_t RsTurtleChunkCrcRequestItem::serial_size() { uint32_t s = 0 ; s += 8 ; // header s += 4 ; // tunnel id s += 4 ; // chunk number return s ; } bool RsTurtleFileMapRequestItem::serialize(void *data,uint32_t& pktsize) { 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, direction); if (offset != tlvsize) { ok = false; #ifdef RSSERIAL_DEBUG std::cerr << "RsFileConfigSerialiser::serialiseTransfer() Size Error! " << std::endl; #endif } return ok; } bool RsTurtleFileMapItem::serialize(void *data,uint32_t& pktsize) { 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, direction); ok &= setRawUInt32(data, tlvsize, &offset, compressed_map._map.size()); for(uint32_t i=0;i pktsize) ok = false ; /* add mandatory parts first */ ok &= getRawUInt32(data, pktsize, &offset, &tunnel_id) ; ok &= getRawUInt64(data, pktsize, &offset, &chunk_offset); ok &= getRawUInt32(data, pktsize, &offset, &chunk_size); if(chunk_size > rssize || rssize - chunk_size < offset) throw std::runtime_error("RsTurtleFileDataItem::() error while deserializing.") ; chunk_data = (void*)malloc(chunk_size) ; if(chunk_data == NULL) throw std::runtime_error("RsTurtleFileDataItem::() cannot allocate memory.") ; memcpy(chunk_data,(void*)((unsigned char*)data+offset),chunk_size) ; offset += chunk_size ; #ifdef P3TURTLE_DEBUG std::cerr << " tunnel_id=" << (void*)tunnel_id << ", chunk_offset=" << chunk_offset << ", chunk_size=" << chunk_size << std::endl ; #endif #ifdef WINDOWS_SYS // No Exceptions in Windows compile. (drbobs). UNREFERENCED_LOCAL_VARIABLE(rssize); #else if (offset != rssize) throw std::runtime_error("RsTurtleFileDataItem::() error while deserializing.") ; if (!ok) throw std::runtime_error("RsTurtleFileDataItem::() unknown error while deserializing.") ; #endif } bool RsTurtleFileDataItem::serialize(void *data,uint32_t& pktsize) { 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 &= setRawUInt64(data, tlvsize, &offset, chunk_offset); ok &= setRawUInt32(data, tlvsize, &offset, chunk_size); memcpy((void*)((unsigned char*)data+offset),chunk_data,chunk_size) ; offset += chunk_size ; if (offset != tlvsize) { ok = false; #ifdef RSSERIAL_DEBUG std::cerr << "RsTurtleTunnelOkItem::serialiseTransfer() Size Error! " << std::endl; #endif } return ok; } std::ostream& RsTurtleFileRequestItem::print(std::ostream& o, uint16_t) { o << "File request item:" << std::endl ; o << " tunnel id : " << std::hex << tunnel_id << std::dec << std::endl ; o << " offset : " << chunk_offset << std::endl ; o << " chunk size: " << chunk_size << std::endl ; return o ; } std::ostream& RsTurtleFileDataItem::print(std::ostream& o, uint16_t) { o << "File request item:" << std::endl ; o << " tunnel id : " << std::hex << tunnel_id << std::dec << std::endl ; o << " offset : " << chunk_offset << std::endl ; o << " chunk size: " << chunk_size << std::endl ; o << " data : " << std::hex << chunk_data << std::dec << std::endl ; return o ; } std::ostream& RsTurtleFileMapItem::print(std::ostream& o, uint16_t) { o << "File map item:" << std::endl ; o << " tunnel id : " << std::hex << tunnel_id << std::dec << std::endl ; o << " direction : " << direction << std::endl ; o << " map : " ; for(uint32_t i=0;i