/******************************************************************************* * libretroshare/src/serialiser: rstlvgenericmap.inl * * * * libretroshare: retroshare core library * * * * Copyright 2014 by Robert Fernie * * * * 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 . * * * *******************************************************************************/ #include "serialiser/rstlvbase.h" // Must be different - as this is inline in headers. //#define TLV_GENERICMAP_DEBUG 1 /*********************************** RsTlvGenericPairRef ***********************************/ template void RsTlvGenericPairRef::TlvClear() { RsTlvParamRef key(mKeyType, mKey); RsTlvParamRef value(mValueType, mValue); key.TlvClear(); value.TlvClear(); } template uint32_t RsTlvGenericPairRef::TlvSize() const { uint32_t s = TLV_HEADER_SIZE; /* header */ RsTlvParamRef key(mKeyType, mKey); RsTlvParamRef value(mValueType, mValue); s += key.TlvSize(); s += value.TlvSize(); return s; } template bool RsTlvGenericPairRef::SetTlv(void *data, uint32_t size, uint32_t *offset) const { /* must check sizes */ uint32_t tlvsize = TlvSize(); uint32_t tlvend = *offset + tlvsize; if (size < tlvend) return false; /* not enough space */ bool ok = true; /* start at data[offset] */ ok &= SetTlvBase(data, tlvend, offset, mPairType, tlvsize); RsTlvParamRef key(mKeyType, mKey); RsTlvParamRef value(mValueType, mValue); ok &= key.SetTlv(data, tlvend, offset); ok &= value.SetTlv(data, tlvend, offset); if (!ok) { std::cerr << "RsTlvGenericPairRef<>::SetTlv() Failed"; std::cerr << std::endl; } #ifdef TLV_GENERICMAP_DEBUG else { std::cerr << "RsTlvGenericPairRef<>::SetTlv() Ok"; std::cerr << std::endl; } #endif return ok; } template bool RsTlvGenericPairRef::GetTlv(void *data, uint32_t size, uint32_t *offset) { if (size < *offset + TLV_HEADER_SIZE) return false; uint16_t tlvtype = GetTlvType( &(((uint8_t *) data)[*offset]) ); uint32_t tlvsize = GetTlvSize( &(((uint8_t *) data)[*offset]) ); uint32_t tlvend = *offset + tlvsize; if (size < tlvend) /* check size */ return false; /* not enough space */ if (tlvtype != mPairType) /* check type */ return false; bool ok = true; /* ready to load */ TlvClear(); /* skip the header */ (*offset) += TLV_HEADER_SIZE; RsTlvParamRef key(mKeyType, mKey); RsTlvParamRef value(mValueType, mValue); ok &= key.GetTlv(data, tlvend, offset); ok &= value.GetTlv(data, tlvend, offset); /*************************************************************************** * 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_GENERICMAP_DEBUG std::cerr << "RsTlvGenericPairRef::GetTlv() Warning extra bytes at end of item"; std::cerr << std::endl; #endif *offset = tlvend; } return ok; } template std::ostream &RsTlvGenericPairRef::print(std::ostream &out, uint16_t indent) const { //printBase(out, "RsTlvGenericPairRef", indent); uint16_t int_Indent = indent + 2; RsTlvParamRef key(mKeyType, mKey); RsTlvParamRef value(mValueType, mValue); printIndent(out, int_Indent); out << "Key:"; key.print(out, 0); out << std::endl; printIndent(out, int_Indent); out << "Value:"; value.print(out, 0); out << std::endl; //printEnd(out, "RsTlvGenericPairRef", indent); return out; } // declare likely combinations. //template<> class RsTlvGenericPairRef; //template<> class RsTlvGenericPairRef; //template<> class RsTlvGenericPairRef; //template<> class RsTlvGenericPairRef; /************************************ RsTlvGenericMapRef ***********************************/ template void RsTlvGenericMapRef::TlvClear() { mRefMap.clear(); //empty list } template uint32_t RsTlvGenericMapRef::TlvSize() const { uint32_t s = TLV_HEADER_SIZE; /* header */ typename std::map::iterator it; for(it = mRefMap.begin(); it != mRefMap.end(); ++it) { RsTlvGenericPairRef pair(mPairType, mKeyType, mValueType, it->first, it->second); s += pair.TlvSize(); } return s; } template bool RsTlvGenericMapRef::SetTlv(void *data, uint32_t size, uint32_t *offset) const { /* must check sizes */ uint32_t tlvsize = TlvSize(); uint32_t tlvend = *offset + tlvsize; if (size < tlvend) return false; /* not enough space */ bool ok = true; /* start at data[offset] */ ok &= SetTlvBase(data, tlvend, offset, mMapType, tlvsize); typename std::map::iterator it; for(it = mRefMap.begin(); it != mRefMap.end(); ++it) { RsTlvGenericPairRef pair(mPairType, mKeyType, mValueType, it->first, it->second); ok &= pair.SetTlv(data, size, offset); } if (!ok) { std::cerr << "RsTlvGenericMapRef<>::SetTlv() Failed"; std::cerr << std::endl; } return ok; } template bool RsTlvGenericMapRef::GetTlv(void *data, uint32_t size, uint32_t *offset) { if (size < *offset + TLV_HEADER_SIZE) return false; uint16_t tlvtype = GetTlvType( &(((uint8_t *) data)[*offset]) ); uint32_t tlvsize = GetTlvSize( &(((uint8_t *) data)[*offset]) ); uint32_t tlvend = *offset + tlvsize; if (size < tlvend) /* check size */ return false; /* not enough space */ if (tlvtype != mMapType) /* check type */ return false; bool ok = true; /* ready to load */ TlvClear(); /* skip the header */ (*offset) += TLV_HEADER_SIZE; /* while there is TLV */ while((*offset) + 2 < tlvend) { /* get the next type */ uint16_t tlvsubtype = GetTlvType( &(((uint8_t *) data)[*offset]) ); if (tlvsubtype == mPairType) { K k; V v; RsTlvGenericPairRef pair(mPairType, mKeyType, mValueType, k, v); ok &= pair.GetTlv(data, size, offset); if (ok) { mRefMap[k] = v; } } else { ok &= SkipUnknownTlv(data, tlvend, offset); ok = false; } if (!ok) break; } /*************************************************************************** * 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_GENERICMAP_DEBUG std::cerr << "RsTlvGenericMapRef::GetTlv() Warning extra bytes at end of item"; std::cerr << std::endl; #endif *offset = tlvend; } return ok; } template std::ostream &RsTlvGenericMapRef::print(std::ostream &out, uint16_t indent) const { //printBase(out, "RsTlvGenericMapRef", indent); uint16_t int_Indent = indent + 2; printIndent(out, int_Indent); out << "MapType:" << mMapType << std::endl; typename std::map::iterator it; for(it = mRefMap.begin(); it != mRefMap.end() ; ++it) { RsTlvGenericPairRef pair(mPairType, mKeyType, mValueType, it->first, it->second); pair.print(out, int_Indent); } //printEnd(out, "RsTlvGenericMapRef", indent); return out; }