2018-05-29 21:27:12 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
* libretroshare/src/serialiser: rstlvimage.cc *
|
|
|
|
* *
|
|
|
|
* libretroshare: retroshare core library *
|
|
|
|
* *
|
|
|
|
* Copyright 2007-2008 by Robert Fernie,Chris Parker <retroshare@lunamutt.com> *
|
|
|
|
* *
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Lesser General Public License as *
|
|
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
|
|
* License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program 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 Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
*******************************************************************************/
|
2014-03-29 15:34:37 +00:00
|
|
|
#include "serialiser/rstlvimage.h"
|
|
|
|
|
|
|
|
#if 0
|
2008-06-13 15:42:08 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "serialiser/rstlvbase.h"
|
|
|
|
#include "serialiser/rsbaseserial.h"
|
2014-03-29 15:34:37 +00:00
|
|
|
#endif
|
2008-06-13 15:42:08 +00:00
|
|
|
|
|
|
|
/***
|
2009-09-08 20:18:43 +00:00
|
|
|
* #define TLV_IMG_DEBUG 1
|
2008-06-13 15:42:08 +00:00
|
|
|
**/
|
|
|
|
|
|
|
|
/************************************* RsTlvImage ************************************/
|
|
|
|
|
|
|
|
RsTlvImage::RsTlvImage()
|
|
|
|
:RsTlvItem(), image_type(0), binData(TLV_TYPE_BIN_IMAGE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RsTlvImage::TlvClear()
|
|
|
|
{
|
|
|
|
image_type = 0;
|
|
|
|
binData.TlvClear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-29 15:34:37 +00:00
|
|
|
uint32_t RsTlvImage::TlvSize() const
|
2008-06-13 15:42:08 +00:00
|
|
|
{
|
2009-09-08 20:18:43 +00:00
|
|
|
uint32_t s = TLV_HEADER_SIZE; /* header */
|
2008-06-13 15:42:08 +00:00
|
|
|
|
|
|
|
/* collect sizes for both uInts and data length */
|
|
|
|
s+= 4;
|
|
|
|
s+= binData.TlvSize();
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-29 15:34:37 +00:00
|
|
|
bool RsTlvImage::SetTlv(void *data, uint32_t size, uint32_t *offset) const
|
2008-06-13 15:42:08 +00:00
|
|
|
{
|
2009-09-08 20:18:43 +00:00
|
|
|
#ifdef TLV_IMG_DEBUG
|
|
|
|
std::cerr << "RsTlvImage::SetTlv()" << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-06-13 15:42:08 +00:00
|
|
|
/* must check sizes */
|
2009-09-08 20:18:43 +00:00
|
|
|
uint32_t tlvsize = TlvSize();
|
2008-06-13 15:42:08 +00:00
|
|
|
uint32_t tlvend = *offset + tlvsize;
|
|
|
|
|
|
|
|
if (size < tlvend)
|
2009-09-08 20:18:43 +00:00
|
|
|
{
|
|
|
|
#ifdef TLV_IMG_DEBUG
|
|
|
|
std::cerr << "RsTlvImage::SetTlv() no space" << std::endl;
|
|
|
|
#endif
|
2008-06-13 15:42:08 +00:00
|
|
|
return false; /* not enough space */
|
2009-09-08 20:18:43 +00:00
|
|
|
}
|
2008-06-13 15:42:08 +00:00
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
/* start at data[offset] */
|
|
|
|
ok &= SetTlvBase(data, tlvend, offset, TLV_TYPE_IMAGE , tlvsize);
|
2009-09-08 20:18:43 +00:00
|
|
|
#ifdef TLV_IMG_DEBUG
|
|
|
|
if (!ok)
|
|
|
|
std::cerr << "RsTlvImage::SetTlv() NOK base" << std::endl;
|
|
|
|
#endif
|
2008-06-13 15:42:08 +00:00
|
|
|
|
|
|
|
/* add mandatory part */
|
|
|
|
ok &= setRawUInt32(data, tlvend, offset, image_type);
|
2009-09-08 20:18:43 +00:00
|
|
|
#ifdef TLV_IMG_DEBUG
|
|
|
|
if (!ok)
|
|
|
|
std::cerr << "RsTlvImage::SetTlv() NOK image" << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-06-13 15:42:08 +00:00
|
|
|
ok &= binData.SetTlv(data, size, offset);
|
2009-09-08 20:18:43 +00:00
|
|
|
#ifdef TLV_IMG_DEBUG
|
|
|
|
if (!ok)
|
|
|
|
std::cerr << "RsTlvImage::SetTlv() NOK binData" << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-06-13 15:42:08 +00:00
|
|
|
|
|
|
|
return ok;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-29 15:34:37 +00:00
|
|
|
bool RsTlvImage::GetTlv(void *data, uint32_t size, uint32_t *offset)
|
2008-06-13 15:42:08 +00:00
|
|
|
{
|
2009-09-08 20:18:43 +00:00
|
|
|
#ifdef TLV_IMG_DEBUG
|
|
|
|
std::cerr << "RsTlvImage::GetTlv()" << std::endl;
|
|
|
|
#endif
|
|
|
|
if (size < *offset + TLV_HEADER_SIZE)
|
2008-06-13 15:42:08 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t tlvtype = GetTlvType( &(((uint8_t *) data)[*offset]) );
|
2015-06-29 12:00:40 +00:00
|
|
|
uint32_t tlvsize = GetTlvSize( &(((uint8_t *) data)[*offset]) );
|
2008-06-13 15:42:08 +00:00
|
|
|
uint32_t tlvend = *offset + tlvsize;
|
|
|
|
|
|
|
|
if (size < tlvend) /* check size */
|
2009-09-08 20:18:43 +00:00
|
|
|
{
|
|
|
|
#ifdef TLV_IMG_DEBUG
|
|
|
|
std::cerr << "RsTlvImage::GetTlv() FAIL no space" << std::endl;
|
|
|
|
#endif
|
2008-06-13 15:42:08 +00:00
|
|
|
return false; /* not enough space */
|
2009-09-08 20:18:43 +00:00
|
|
|
}
|
2008-06-13 15:42:08 +00:00
|
|
|
|
|
|
|
if (tlvtype != TLV_TYPE_IMAGE) /* check type */
|
2009-09-08 20:18:43 +00:00
|
|
|
{
|
|
|
|
#ifdef TLV_IMG_DEBUG
|
|
|
|
std::cerr << "RsTlvImage::GetTlv() FAIL wrong type" << std::endl;
|
|
|
|
#endif
|
2008-06-13 15:42:08 +00:00
|
|
|
return false;
|
2009-09-08 20:18:43 +00:00
|
|
|
}
|
2008-06-13 15:42:08 +00:00
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
/* ready to load */
|
|
|
|
TlvClear();
|
|
|
|
|
|
|
|
/* skip the header */
|
2009-09-08 20:18:43 +00:00
|
|
|
(*offset) += TLV_HEADER_SIZE;
|
2008-06-13 15:42:08 +00:00
|
|
|
|
|
|
|
/* add mandatory parts first */
|
|
|
|
ok &= getRawUInt32(data, tlvend, offset, &(image_type));
|
|
|
|
ok &= binData.GetTlv(data, size, offset);
|
|
|
|
|
2009-09-08 20:18:43 +00:00
|
|
|
#ifdef TLV_IMG_DEBUG
|
|
|
|
if (!ok)
|
|
|
|
std::cerr << "RsTlvImage::GetTlv() NOK" << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* NB: extra components could be added (for future expansion of the type).
|
|
|
|
* or be present (if this code is reading an extended version).
|
|
|
|
*
|
|
|
|
* We must chew up the extra characters to conform with TLV specifications
|
|
|
|
***************************************************************************/
|
|
|
|
if (*offset != tlvend)
|
|
|
|
{
|
|
|
|
#ifdef TLV_DEBUG
|
|
|
|
std::cerr << "RsTlvImage::GetTlv() Warning extra bytes at end of item";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
*offset = tlvend;
|
|
|
|
}
|
|
|
|
|
2008-06-13 15:42:08 +00:00
|
|
|
return ok;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print it out */
|
2014-03-29 15:34:37 +00:00
|
|
|
std::ostream &RsTlvImage::print(std::ostream &out, uint16_t indent) const
|
2008-06-13 15:42:08 +00:00
|
|
|
{
|
|
|
|
printBase(out, "RsTlvImage", indent);
|
|
|
|
uint16_t int_Indent = indent + 2;
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "Image_Type: " << image_type;
|
|
|
|
out << std::endl;
|
|
|
|
|
|
|
|
binData.print(out, int_Indent);
|
|
|
|
|
|
|
|
printEnd(out, "RsTlvImage", indent);
|
|
|
|
return out;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-07-28 23:12:19 +00:00
|
|
|
RsTlvImage::RsTlvImage(const RsTlvImage& rightOp)
|
|
|
|
:RsTlvItem(), image_type(0), binData(TLV_TYPE_BIN_IMAGE)
|
|
|
|
{
|
|
|
|
this->image_type = rightOp.image_type;
|
|
|
|
this->binData.setBinData(rightOp.binData.bin_data, rightOp.binData.bin_len);
|
|
|
|
}
|