2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* libretroshare/src/serialiser: rsbaseitems.cc
|
|
|
|
*
|
|
|
|
* RetroShare Serialiser.
|
|
|
|
*
|
|
|
|
* Copyright 2007-2008 by Robert Fernie.
|
|
|
|
*
|
|
|
|
* 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@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "serialiser/rsbaseserial.h"
|
|
|
|
#include "serialiser/rsmsgitems.h"
|
|
|
|
#include "serialiser/rstlvbase.h"
|
|
|
|
|
2008-03-31 10:06:59 -04:00
|
|
|
/***
|
2007-12-11 19:54:42 -05:00
|
|
|
#define RSSERIAL_DEBUG 1
|
2008-03-31 10:06:59 -04:00
|
|
|
***/
|
|
|
|
|
2007-12-11 19:54:42 -05:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
RsChatItem::~RsChatItem()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RsChatItem::clear()
|
|
|
|
{
|
|
|
|
chatFlags = 0;
|
|
|
|
sendTime = 0;
|
2008-01-26 08:00:57 -05:00
|
|
|
message.clear();
|
2007-12-11 19:54:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream &RsChatItem::print(std::ostream &out, uint16_t indent)
|
|
|
|
{
|
|
|
|
printRsItemBase(out, "RsChatItem", indent);
|
|
|
|
uint16_t int_Indent = indent + 2;
|
|
|
|
printIndent(out, int_Indent);
|
2008-06-04 06:59:24 -04:00
|
|
|
out << "QblogMs " << chatFlags << std::endl;
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "sendTime: " << sendTime << std::endl;
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
2008-01-26 08:00:57 -05:00
|
|
|
|
|
|
|
std::string cnv_message(message.begin(), message.end());
|
|
|
|
out << "msg: " << cnv_message << std::endl;
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
printRsItemEnd(out, "RsChatItem", indent);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t RsChatSerialiser::sizeItem(RsChatItem *item)
|
|
|
|
{
|
|
|
|
uint32_t s = 8; /* header */
|
|
|
|
s += 4; /* chatFlags */
|
|
|
|
s += 4; /* sendTime */
|
2008-01-26 08:00:57 -05:00
|
|
|
s += GetTlvWideStringSize(item->message);
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* serialise the data to the buffer */
|
|
|
|
bool RsChatSerialiser::serialiseItem(RsChatItem *item, void *data, uint32_t *pktsize)
|
|
|
|
{
|
|
|
|
uint32_t tlvsize = sizeItem(item);
|
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
if (*pktsize < tlvsize)
|
|
|
|
return false; /* not enough space */
|
|
|
|
|
|
|
|
*pktsize = tlvsize;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
|
|
|
|
|
2009-02-22 12:36:39 -05:00
|
|
|
//#ifdef RSSERIAL_DEBUG
|
2007-12-11 19:54:42 -05:00
|
|
|
std::cerr << "RsChatSerialiser::serialiseItem() Header: " << ok << std::endl;
|
|
|
|
std::cerr << "RsChatSerialiser::serialiseItem() Size: " << tlvsize << std::endl;
|
2009-02-22 12:36:39 -05:00
|
|
|
//#endif
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* add mandatory parts first */
|
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, item->chatFlags);
|
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, item->sendTime);
|
2008-01-26 08:00:57 -05:00
|
|
|
ok &= SetTlvWideString(data, tlvsize, &offset, TLV_TYPE_WSTR_MSG, item->message);
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
if (offset != tlvsize)
|
|
|
|
{
|
|
|
|
ok = false;
|
2009-02-22 12:36:39 -05:00
|
|
|
//#ifdef RSSERIAL_DEBUG
|
2007-12-11 19:54:42 -05:00
|
|
|
std::cerr << "RsChatSerialiser::serialiseItem() Size Error! " << std::endl;
|
2009-02-22 12:36:39 -05:00
|
|
|
//#endif
|
2007-12-11 19:54:42 -05:00
|
|
|
}
|
2009-02-22 12:36:39 -05:00
|
|
|
std::cerr << "computed size: " << 256*((unsigned char*)data)[6]+((unsigned char*)data)[7] << std::endl ;
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsChatItem *RsChatSerialiser::deserialiseItem(void *data, uint32_t *pktsize)
|
|
|
|
{
|
|
|
|
/* get the type and size */
|
|
|
|
uint32_t rstype = getRsItemId(data);
|
|
|
|
uint32_t rssize = getRsItemSize(data);
|
|
|
|
|
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
|
|
|
|
(RS_SERVICE_TYPE_CHAT != getRsItemService(rstype)) ||
|
|
|
|
(RS_PKT_SUBTYPE_DEFAULT != getRsItemSubType(rstype)))
|
|
|
|
{
|
|
|
|
return NULL; /* wrong type */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*pktsize < rssize) /* check size */
|
|
|
|
return NULL; /* not enough data */
|
|
|
|
|
|
|
|
/* set the packet length */
|
|
|
|
*pktsize = rssize;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
/* ready to load */
|
|
|
|
RsChatItem *item = new RsChatItem();
|
|
|
|
item->clear();
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* get mandatory parts first */
|
|
|
|
ok &= getRawUInt32(data, rssize, &offset, &(item->chatFlags));
|
|
|
|
ok &= getRawUInt32(data, rssize, &offset, &(item->sendTime));
|
2008-01-26 08:00:57 -05:00
|
|
|
ok &= GetTlvWideString(data, rssize, &offset, TLV_TYPE_WSTR_MSG, item->message);
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
if (offset != rssize)
|
|
|
|
{
|
|
|
|
/* error */
|
|
|
|
delete item;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
delete item;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t RsChatSerialiser::size(RsItem *item)
|
|
|
|
{
|
|
|
|
return sizeItem((RsChatItem *) item);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RsChatSerialiser::serialise(RsItem *item, void *data, uint32_t *pktsize)
|
|
|
|
{
|
|
|
|
return serialiseItem((RsChatItem *) item, data, pktsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
RsItem *RsChatSerialiser::deserialise(void *data, uint32_t *pktsize)
|
|
|
|
{
|
|
|
|
return deserialiseItem(data, pktsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
RsMsgItem::~RsMsgItem()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RsMsgItem::clear()
|
|
|
|
{
|
|
|
|
msgId = 0;
|
|
|
|
msgFlags = 0;
|
|
|
|
sendTime = 0;
|
|
|
|
recvTime = 0;
|
2008-01-26 08:00:57 -05:00
|
|
|
subject.clear();
|
|
|
|
message.clear();
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
msgto.TlvClear();
|
|
|
|
msgcc.TlvClear();
|
|
|
|
msgbcc.TlvClear();
|
|
|
|
|
|
|
|
attachment.TlvClear();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream &RsMsgItem::print(std::ostream &out, uint16_t indent)
|
|
|
|
{
|
|
|
|
printRsItemBase(out, "RsMsgItem", indent);
|
|
|
|
uint16_t int_Indent = indent + 2;
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "msgId (not serialised): " << msgId << std::endl;
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "msgFlags: " << msgFlags << std::endl;
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "sendTime: " << sendTime << std::endl;
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "recvTime: " << recvTime << std::endl;
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "Message To: " << std::endl;
|
|
|
|
msgto.print(out, int_Indent);
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "Message CC: " << std::endl;
|
|
|
|
msgcc.print(out, int_Indent);
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "Message BCC: " << std::endl;
|
|
|
|
msgbcc.print(out, int_Indent);
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
2008-01-26 08:00:57 -05:00
|
|
|
std::string cnv_subject(subject.begin(), subject.end());
|
|
|
|
out << "subject: " << cnv_subject << std::endl;
|
|
|
|
|
2007-12-11 19:54:42 -05:00
|
|
|
printIndent(out, int_Indent);
|
2008-01-26 08:00:57 -05:00
|
|
|
std::string cnv_message(message.begin(), message.end());
|
|
|
|
out << "msg: " << cnv_message << std::endl;
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "Attachment: " << std::endl;
|
|
|
|
attachment.print(out, int_Indent);
|
|
|
|
|
|
|
|
printRsItemEnd(out, "RsMsgItem", indent);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t RsMsgSerialiser::sizeItem(RsMsgItem *item)
|
|
|
|
{
|
|
|
|
uint32_t s = 8; /* header */
|
|
|
|
s += 4; /* msgFlags */
|
|
|
|
s += 4; /* sendTime */
|
|
|
|
s += 4; /* recvTime */
|
|
|
|
|
2008-01-26 08:00:57 -05:00
|
|
|
s += GetTlvWideStringSize(item->subject);
|
|
|
|
s += GetTlvWideStringSize(item->message);
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
s += item->msgto.TlvSize();
|
|
|
|
s += item->msgcc.TlvSize();
|
|
|
|
s += item->msgbcc.TlvSize();
|
|
|
|
s += item->attachment.TlvSize();
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* serialise the data to the buffer */
|
|
|
|
bool RsMsgSerialiser::serialiseItem(RsMsgItem *item, void *data, uint32_t *pktsize)
|
|
|
|
{
|
|
|
|
uint32_t tlvsize = sizeItem(item);
|
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
if (*pktsize < tlvsize)
|
|
|
|
return false; /* not enough space */
|
|
|
|
|
|
|
|
*pktsize = tlvsize;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
|
|
|
|
|
2008-03-31 10:06:59 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
2007-12-11 19:54:42 -05:00
|
|
|
std::cerr << "RsMsgSerialiser::serialiseItem() Header: " << ok << std::endl;
|
|
|
|
std::cerr << "RsMsgSerialiser::serialiseItem() Size: " << tlvsize << std::endl;
|
2008-03-31 10:06:59 -04:00
|
|
|
#endif
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* add mandatory parts first */
|
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, item->msgFlags);
|
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, item->sendTime);
|
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, item->recvTime);
|
|
|
|
|
2008-01-26 08:00:57 -05:00
|
|
|
ok &= SetTlvWideString(data,tlvsize,&offset,TLV_TYPE_WSTR_SUBJECT,item->subject);
|
|
|
|
ok &= SetTlvWideString(data, tlvsize, &offset, TLV_TYPE_WSTR_MSG, item->message);
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
ok &= item->msgto.SetTlv(data, tlvsize, &offset);
|
|
|
|
ok &= item->msgcc.SetTlv(data, tlvsize, &offset);
|
|
|
|
ok &= item->msgbcc.SetTlv(data, tlvsize, &offset);
|
|
|
|
|
|
|
|
ok &= item->attachment.SetTlv(data, tlvsize, &offset);
|
|
|
|
if (offset != tlvsize)
|
|
|
|
{
|
|
|
|
ok = false;
|
2008-03-31 10:06:59 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
2007-12-11 19:54:42 -05:00
|
|
|
std::cerr << "RsMsgSerialiser::serialiseItem() Size Error! " << std::endl;
|
2008-03-31 10:06:59 -04:00
|
|
|
#endif
|
2007-12-11 19:54:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsMsgItem *RsMsgSerialiser::deserialiseItem(void *data, uint32_t *pktsize)
|
|
|
|
{
|
|
|
|
/* get the type and size */
|
|
|
|
uint32_t rstype = getRsItemId(data);
|
|
|
|
uint32_t rssize = getRsItemSize(data);
|
|
|
|
|
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
|
|
|
|
(RS_SERVICE_TYPE_MSG != getRsItemService(rstype)) ||
|
|
|
|
(RS_PKT_SUBTYPE_DEFAULT != getRsItemSubType(rstype)))
|
|
|
|
{
|
|
|
|
return NULL; /* wrong type */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*pktsize < rssize) /* check size */
|
|
|
|
return NULL; /* not enough data */
|
|
|
|
|
|
|
|
/* set the packet length */
|
|
|
|
*pktsize = rssize;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
/* ready to load */
|
|
|
|
RsMsgItem *item = new RsMsgItem();
|
|
|
|
item->clear();
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* get mandatory parts first */
|
|
|
|
ok &= getRawUInt32(data, rssize, &offset, &(item->msgFlags));
|
|
|
|
ok &= getRawUInt32(data, rssize, &offset, &(item->sendTime));
|
|
|
|
ok &= getRawUInt32(data, rssize, &offset, &(item->recvTime));
|
|
|
|
|
2008-01-26 08:00:57 -05:00
|
|
|
ok &= GetTlvWideString(data,rssize,&offset,TLV_TYPE_WSTR_SUBJECT,item->subject);
|
|
|
|
ok &= GetTlvWideString(data, rssize, &offset, TLV_TYPE_WSTR_MSG, item->message);
|
2007-12-11 19:54:42 -05:00
|
|
|
ok &= item->msgto.GetTlv(data, rssize, &offset);
|
|
|
|
ok &= item->msgcc.GetTlv(data, rssize, &offset);
|
|
|
|
ok &= item->msgbcc.GetTlv(data, rssize, &offset);
|
|
|
|
ok &= item->attachment.GetTlv(data, rssize, &offset);
|
|
|
|
|
|
|
|
if (offset != rssize)
|
|
|
|
{
|
|
|
|
/* error */
|
|
|
|
delete item;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
delete item;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t RsMsgSerialiser::size(RsItem *item)
|
|
|
|
{
|
|
|
|
return sizeItem((RsMsgItem *) item);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RsMsgSerialiser::serialise(RsItem *item, void *data, uint32_t *pktsize)
|
|
|
|
{
|
|
|
|
return serialiseItem((RsMsgItem *) item, data, pktsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
RsItem *RsMsgSerialiser::deserialise(void *data, uint32_t *pktsize)
|
|
|
|
{
|
|
|
|
return deserialiseItem(data, pktsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
|