/**************************************************************** * RetroShare GUI is distributed under the following license: * * Copyright (C) 2012 by Thunder * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. ****************************************************************/ #include "serialiser/rsbaseserial.h" #include "serialiser/rstlvbase.h" #include "rsFeedReaderItems.h" /*************************************************************************/ RsFeedReaderFeed::RsFeedReaderFeed() : RsItem(RS_PKT_VERSION1, RS_PKT_CLASS_CONFIG, RS_PKT_TYPE_FEEDREADER_CONFIG, RS_PKT_SUBTYPE_FEEDREADER_FEED) { clear(); } void RsFeedReaderFeed::clear() { feedId.clear(); parentId.clear(); name.clear(); url.clear(); user.clear(); password.clear(); proxyAddress.clear(); proxyPort = 0; updateInterval = 0; lastUpdate = 0; storageTime = 0; flag = 0; forumId.clear(); description.clear(); icon.clear(); errorState = RS_FEED_ERRORSTATE_OK; errorString.clear(); workstate = WAITING; content.clear(); } std::ostream &RsFeedReaderFeed::print(std::ostream &out, uint16_t /*indent*/) { return out; } uint32_t RsFeedReaderSerialiser::sizeFeed(RsFeedReaderFeed *item) { uint32_t s = 8; /* header */ s += GetTlvStringSize(item->feedId); s += GetTlvStringSize(item->parentId); s += GetTlvStringSize(item->url); s += GetTlvStringSize(item->name); s += GetTlvStringSize(item->description); s += GetTlvStringSize(item->icon); s += GetTlvStringSize(item->user); s += GetTlvStringSize(item->password); s += GetTlvStringSize(item->proxyAddress); s += sizeof(uint16_t); /* proxyPort */ s += sizeof(uint32_t); /* updateInterval */ s += sizeof(time_t); /* lastscan */ s += sizeof(uint32_t); /* storageTime */ s += sizeof(uint32_t); /* flag */ s += GetTlvStringSize(item->forumId); s += sizeof(uint32_t); /* errorstate */ s += GetTlvStringSize(item->errorString); return s; } /* serialise the data to the buffer */ bool RsFeedReaderSerialiser::serialiseFeed(RsFeedReaderFeed *item, void *data, uint32_t *pktsize) { uint32_t tlvsize = sizeFeed(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); /* skip the header */ offset += 8; /* add values */ ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_GENID, item->feedId); ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, item->parentId); ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_LINK, item->url); ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_NAME, item->name); ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_COMMENT, item->description); ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, item->icon); ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, item->user); ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, item->password); ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, item->proxyAddress); ok &= setRawUInt16(data, tlvsize, &offset, item->proxyPort); ok &= setRawUInt32(data, tlvsize, &offset, item->updateInterval); ok &= setRawUInt32(data, tlvsize, &offset, item->lastUpdate); ok &= setRawUInt32(data, tlvsize, &offset, item->storageTime); ok &= setRawUInt32(data, tlvsize, &offset, item->flag); ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, item->forumId); ok &= setRawUInt32(data, tlvsize, &offset, item->errorState); ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, item->errorString); if (offset != tlvsize) { ok = false; std::cerr << "RsFeedReaderSerialiser::serialiseFeed() Size Error! " << std::endl; } return ok; } RsFeedReaderFeed *RsFeedReaderSerialiser::deserialiseFeed(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_VERSION1 != getRsItemVersion(rstype)) || (RS_PKT_CLASS_CONFIG != getRsItemClass(rstype)) || (RS_PKT_TYPE_FEEDREADER_CONFIG != getRsItemType(rstype)) || (RS_PKT_SUBTYPE_FEEDREADER_FEED != 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 */ RsFeedReaderFeed *item = new RsFeedReaderFeed(); item->clear(); /* skip the header */ offset += 8; /* get values */ ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_GENID, item->feedId); ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->parentId); ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_LINK, item->url); ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_NAME, item->name); ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_COMMENT, item->description); ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->icon); ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->user); ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->password); ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->proxyAddress); ok &= getRawUInt16(data, rssize, &offset, &(item->proxyPort)); ok &= getRawUInt32(data, rssize, &offset, &(item->updateInterval)); ok &= getRawUInt32(data, rssize, &offset, (uint32_t*) &(item->lastUpdate)); ok &= getRawUInt32(data, rssize, &offset, &(item->storageTime)); ok &= getRawUInt32(data, rssize, &offset, &(item->flag)); ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->forumId); ok &= getRawUInt32(data, rssize, &offset, &(item->errorState)); ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->errorString); if (offset != rssize) { /* error */ delete item; return NULL; } if (!ok) { delete item; return NULL; } return item; } /*************************************************************************/ RsFeedReaderMsg::RsFeedReaderMsg() : RsItem(RS_PKT_VERSION1, RS_PKT_CLASS_CONFIG, RS_PKT_TYPE_FEEDREADER_CONFIG, RS_PKT_SUBTYPE_FEEDREADER_MSG) { clear(); } void RsFeedReaderMsg::clear() { msgId.clear(); feedId.clear(); title.clear(); link.clear(); author.clear(); description.clear(); pubDate = 0; flag = 0; } std::ostream &RsFeedReaderMsg::print(std::ostream &out, uint16_t /*indent*/) { return out; } uint32_t RsFeedReaderSerialiser::sizeMsg(RsFeedReaderMsg *item) { uint32_t s = 8; /* header */ s += GetTlvStringSize(item->msgId); s += GetTlvStringSize(item->feedId); s += GetTlvStringSize(item->title); s += GetTlvStringSize(item->link); s += GetTlvStringSize(item->author); s += GetTlvStringSize(item->description); s += sizeof(time_t); /* pubDate */ s += sizeof(uint32_t); /* flag */ return s; } /* serialise the data to the buffer */ bool RsFeedReaderSerialiser::serialiseMsg(RsFeedReaderMsg *item, void *data, uint32_t *pktsize) { uint32_t tlvsize = sizeMsg(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); /* skip the header */ offset += 8; /* add values */ ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_GENID, item->msgId); ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, item->feedId); ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_NAME, item->title); ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_LINK, item->link); ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, item->author); ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_COMMENT, item->description); ok &= setRawUInt32(data, tlvsize, &offset, item->pubDate); ok &= setRawUInt32(data, tlvsize, &offset, item->flag); if (offset != tlvsize) { ok = false; std::cerr << "RsFeedReaderSerialiser::serialiseMsg() Size Error! " << std::endl; } return ok; } RsFeedReaderMsg *RsFeedReaderSerialiser::deserialiseMsg(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_VERSION1 != getRsItemVersion(rstype)) || (RS_PKT_CLASS_CONFIG != getRsItemClass(rstype)) || (RS_PKT_TYPE_FEEDREADER_CONFIG != getRsItemType(rstype)) || (RS_PKT_SUBTYPE_FEEDREADER_MSG != 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 */ RsFeedReaderMsg *item = new RsFeedReaderMsg(); item->clear(); /* skip the header */ offset += 8; /* get values */ ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_GENID, item->msgId); ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->feedId); ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_NAME, item->title); ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_LINK, item->link); ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->author); ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_COMMENT, item->description); ok &= getRawUInt32(data, rssize, &offset, (uint32_t*) &(item->pubDate)); ok &= getRawUInt32(data, rssize, &offset, &(item->flag)); if (offset != rssize) { /* error */ delete item; return NULL; } if (!ok) { delete item; return NULL; } return item; } /*************************************************************************/ uint32_t RsFeedReaderSerialiser::size(RsItem *item) { RsFeedReaderFeed *fi; RsFeedReaderMsg *ei; if (NULL != (fi = dynamic_cast(item))) { return sizeFeed((RsFeedReaderFeed*) item); } if (NULL != (ei = dynamic_cast(item))) { return sizeMsg((RsFeedReaderMsg*) item); } return 0; } bool RsFeedReaderSerialiser::serialise(RsItem *item, void *data, uint32_t *pktsize) { RsFeedReaderFeed *fi; RsFeedReaderMsg *ei; if (NULL != (fi = dynamic_cast(item))) { return serialiseFeed((RsFeedReaderFeed*) item, data, pktsize); } if (NULL != (ei = dynamic_cast(item))) { return serialiseMsg((RsFeedReaderMsg*) item, data, pktsize); } return false; } RsItem *RsFeedReaderSerialiser::deserialise(void *data, uint32_t *pktsize) { /* get the type and size */ uint32_t rstype = getRsItemId(data); if ((RS_PKT_VERSION1 != getRsItemVersion(rstype)) || (RS_PKT_CLASS_CONFIG != getRsItemClass(rstype)) || (RS_PKT_TYPE_FEEDREADER_CONFIG != getRsItemType(rstype))) { return NULL; /* wrong type */ } switch (getRsItemSubType(rstype)) { case RS_PKT_SUBTYPE_FEEDREADER_FEED: return deserialiseFeed(data, pktsize); case RS_PKT_SUBTYPE_FEEDREADER_MSG: return deserialiseMsg(data, pktsize); } return NULL; }