Fix memory management and deprecated removal from serialization

Fix missing RsDiscPgpKeyItem initialization
Fix inconsistent new[]/delete[] usage in RsDiscPgpKeyItem and
  PGPHandler::exportPublicKey which now consistently uses malloc/free
Remove deprecated RsGenericSerializer::FORMAT_*
Move from deprecated RsServiceSerializer::SERIALIZATION_FLAG_* to
  RsSerializationFlags
Solve a bunch of compiler warnings
Stricter checks in SerializeContext costructor
This commit is contained in:
Gioacchino Mazzurco 2020-03-18 23:04:16 +01:00
parent 39bde58c29
commit 5610cc8600
No known key found for this signature in database
GPG key ID: A1FBCA3872E87051
24 changed files with 230 additions and 244 deletions

View file

@ -221,7 +221,8 @@ bool DistributedChatService::checkSignature(RsChatLobbyBouncingObject *obj,const
mGixs->requestKey(obj->signature.keyId,peer_list,RsIdentityUsage(RS_SERVICE_TYPE_CHAT,RsIdentityUsage::CHAT_LOBBY_MSG_VALIDATION,RsGxsGroupId(),RsGxsMessageId(),obj->lobby_id)); mGixs->requestKey(obj->signature.keyId,peer_list,RsIdentityUsage(RS_SERVICE_TYPE_CHAT,RsIdentityUsage::CHAT_LOBBY_MSG_VALIDATION,RsGxsGroupId(),RsGxsMessageId(),obj->lobby_id));
uint32_t size = RsChatSerialiser(RsServiceSerializer::SERIALIZATION_FLAG_SIGNATURE).size(dynamic_cast<RsItem*>(obj)) ; uint32_t size = RsChatSerialiser(RsSerializationFlags::SIGNATURE)
.size(dynamic_cast<RsItem*>(obj));
RsTemporaryMemory memory(size) ; RsTemporaryMemory memory(size) ;
#ifdef DEBUG_CHAT_LOBBIES #ifdef DEBUG_CHAT_LOBBIES
@ -229,7 +230,8 @@ bool DistributedChatService::checkSignature(RsChatLobbyBouncingObject *obj,const
std::cerr << " signature id: " << obj->signature.keyId << std::endl; std::cerr << " signature id: " << obj->signature.keyId << std::endl;
#endif #endif
if(!RsChatSerialiser(RsServiceSerializer::SERIALIZATION_FLAG_SIGNATURE).serialise(dynamic_cast<RsItem*>(obj),memory,&size)) if( !RsChatSerialiser(RsSerializationFlags::SIGNATURE)
.serialise(dynamic_cast<RsItem*>(obj),memory,&size) )
{ {
std::cerr << " (EE) Cannot serialise message item. " << std::endl; std::cerr << " (EE) Cannot serialise message item. " << std::endl;
return false ; return false ;
@ -1003,10 +1005,12 @@ bool DistributedChatService::locked_initLobbyBouncableObject(const ChatLobbyId&
// now sign the object, if the lobby expects it // now sign the object, if the lobby expects it
uint32_t size = RsChatSerialiser(RsServiceSerializer::SERIALIZATION_FLAG_SIGNATURE).size(dynamic_cast<RsItem*>(&item)) ; uint32_t size = RsChatSerialiser(RsSerializationFlags::SIGNATURE)
.size(dynamic_cast<RsItem*>(&item));
RsTemporaryMemory memory(size) ; RsTemporaryMemory memory(size) ;
if(!RsChatSerialiser(RsServiceSerializer::SERIALIZATION_FLAG_SIGNATURE).serialise(dynamic_cast<RsItem*>(&item),memory,&size)) if( !RsChatSerialiser(RsSerializationFlags::SIGNATURE)
.serialise(dynamic_cast<RsItem*>(&item),memory,&size) )
{ {
std::cerr << "(EE) Cannot sign message item. " << std::endl; std::cerr << "(EE) Cannot sign message item. " << std::endl;
return false ; return false ;

View file

@ -85,8 +85,8 @@ void RsChatLobbyBouncingObject::serial_process(RsGenericSerializer::SerializeJob
RsTypeSerializer::serial_process(j,ctx,msg_id ,"msg_id") ; RsTypeSerializer::serial_process(j,ctx,msg_id ,"msg_id") ;
RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_NAME,nick,"nick") ; RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_NAME,nick,"nick") ;
if(!(ctx.mFlags & RsServiceSerializer::SERIALIZATION_FLAG_SIGNATURE)) if(!(ctx.mFlags & RsSerializationFlags::SIGNATURE))
RsTypeSerializer::serial_process<RsTlvItem>(j,ctx,signature,"signature") ; RS_SERIAL_PROCESS(signature);
} }
void RsChatLobbyMsgItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) void RsChatLobbyMsgItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx)

View file

@ -369,9 +369,8 @@ struct PrivateOugoingMapItem : RsChatItem
struct RsChatSerialiser : RsServiceSerializer struct RsChatSerialiser : RsServiceSerializer
{ {
RsChatSerialiser(SerializationFlags flags = SERIALIZATION_FLAG_NONE) : RsChatSerialiser(RsSerializationFlags flags = RsSerializationFlags::NONE):
RsServiceSerializer( RS_SERVICE_TYPE_CHAT, RsServiceSerializer(RS_SERVICE_TYPE_CHAT, flags) {}
RsGenericSerializer::FORMAT_BINARY, flags ) {}
virtual RsItem *create_item(uint16_t service_id,uint8_t item_sub_id) const; virtual RsItem *create_item(uint16_t service_id,uint8_t item_sub_id) const;
}; };

View file

@ -61,7 +61,7 @@ RsFileTree::fromBase64(const std::string& base64)
RsGenericSerializer::SerializeContext ctx( RsGenericSerializer::SerializeContext ctx(
mem.data(), static_cast<uint32_t>(mem.size()), mem.data(), static_cast<uint32_t>(mem.size()),
SerializationFlags::fromEFT(RsSerializationFlags::INTEGER_VLQ) ); RsSerializationFlags::INTEGER_VLQ );
std::unique_ptr<RsFileTree> ft(new RsFileTree); std::unique_ptr<RsFileTree> ft(new RsFileTree);
ft->serial_process( ft->serial_process(
RsGenericSerializer::SerializeJob::DESERIALIZE, ctx); RsGenericSerializer::SerializeJob::DESERIALIZE, ctx);
@ -73,7 +73,7 @@ RsFileTree::fromBase64(const std::string& base64)
std::string RsFileTree::toBase64() const std::string RsFileTree::toBase64() const
{ {
RsGenericSerializer::SerializeContext ctx; RsGenericSerializer::SerializeContext ctx;
ctx.mFlags = SerializationFlags::fromEFT(RsSerializationFlags::INTEGER_VLQ); ctx.mFlags = RsSerializationFlags::INTEGER_VLQ;
RsFileTree* ncThis = const_cast<RsFileTree*>(this); RsFileTree* ncThis = const_cast<RsFileTree*>(this);
ncThis->serial_process( ncThis->serial_process(
RsGenericSerializer::SerializeJob::SIZE_ESTIMATE, ctx ); RsGenericSerializer::SerializeJob::SIZE_ESTIMATE, ctx );

View file

@ -84,16 +84,22 @@ class RsDiscPgpKeyItem: public RsDiscItem
{ {
public: public:
RsDiscPgpKeyItem() : RsDiscItem(RsGossipDiscoveryItemType::PGP_CERT_BINARY) RsDiscPgpKeyItem() :
RsDiscItem(RsGossipDiscoveryItemType::PGP_CERT_BINARY),
bin_data(nullptr), bin_len(0)
{ setPriorityLevel(QOS_PRIORITY_RS_DISC_PGP_CERT); } { setPriorityLevel(QOS_PRIORITY_RS_DISC_PGP_CERT); }
virtual ~RsDiscPgpKeyItem() { delete[](bin_data);bin_data=nullptr;bin_len=0;} ~RsDiscPgpKeyItem() override { free(bin_data); }
void clear() override; void clear() override;
void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx) override;
RsPgpId pgpKeyId; // duplicate information for practical reasons void serial_process(
unsigned char *bin_data; // binry key data allocated with new unsigned char[] RsGenericSerializer::SerializeJob j,
RsGenericSerializer::SerializeContext& ctx ) override;
/// duplicate information for practical reasons
RsPgpId pgpKeyId;
unsigned char* bin_data;
uint32_t bin_len; uint32_t bin_len;
}; };

View file

@ -99,12 +99,10 @@ void RsGRouterGenericDataItem::serial_process(RsGenericSerializer::SerializeJob
RsTypeSerializer::serial_process (j,ctx,destination_key,"destination_key") ; RsTypeSerializer::serial_process (j,ctx,destination_key,"destination_key") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,service_id,"service_id") ; RsTypeSerializer::serial_process<uint32_t>(j,ctx,service_id,"service_id") ;
RsTypeSerializer::TlvMemBlock_proxy prox(data_bytes,data_size) ; RsTypeSerializer::RawMemoryWrapper prox(data_bytes, data_size);
RsTypeSerializer::serial_process(j, ctx, prox, "data"); RsTypeSerializer::serial_process(j, ctx, prox, "data");
if(ctx.mFlags & RsGenericSerializer::SERIALIZATION_FLAG_SIGNATURE) if(!!(ctx.mFlags & RsSerializationFlags::SIGNATURE)) return;
return ;
RsTypeSerializer::serial_process<RsTlvItem>(j,ctx,signature,"signature") ; RsTypeSerializer::serial_process<RsTlvItem>(j,ctx,signature,"signature") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,duplication_factor,"duplication_factor") ; RsTypeSerializer::serial_process<uint32_t>(j,ctx,duplication_factor,"duplication_factor") ;
@ -133,8 +131,7 @@ void RsGRouterSignedReceiptItem::serial_process(RsGenericSerializer::SerializeJo
RsTypeSerializer::serial_process<uint32_t> (j,ctx,service_id,"service_id") ; RsTypeSerializer::serial_process<uint32_t> (j,ctx,service_id,"service_id") ;
RsTypeSerializer::serial_process (j,ctx,data_hash,"data_hash") ; RsTypeSerializer::serial_process (j,ctx,data_hash,"data_hash") ;
if(ctx.mFlags & RsGenericSerializer::SERIALIZATION_FLAG_SIGNATURE) if(!!(ctx.mFlags & RsSerializationFlags::SIGNATURE)) return;
return ;
RsTypeSerializer::serial_process<RsTlvItem>(j,ctx,signature,"signature") ; RsTypeSerializer::serial_process<RsTlvItem>(j,ctx,signature,"signature") ;
} }

View file

@ -286,7 +286,9 @@ class RsGRouterRoutingInfoItem: public RsGRouterItem, public GRouterRoutingInfo,
class RsGRouterSerialiser: public RsServiceSerializer class RsGRouterSerialiser: public RsServiceSerializer
{ {
public: public:
explicit RsGRouterSerialiser(SerializationFlags flags = SERIALIZATION_FLAG_NONE) : RsServiceSerializer(RS_SERVICE_TYPE_GROUTER,RsGenericSerializer::FORMAT_BINARY,flags) {} explicit RsGRouterSerialiser(
RsSerializationFlags flags = RsSerializationFlags::NONE ):
RsServiceSerializer(RS_SERVICE_TYPE_GROUTER, flags) {}
virtual RsItem *create_item(uint16_t service,uint8_t subtype) const ; virtual RsItem *create_item(uint16_t service,uint8_t subtype) const ;
}; };

View file

@ -1658,7 +1658,9 @@ void p3GRouter::handleIncomingReceiptItem(const RsGRouterSignedReceiptItem *rece
Sha1CheckSum p3GRouter::computeDataItemHash(const RsGRouterGenericDataItem *data_item) Sha1CheckSum p3GRouter::computeDataItemHash(const RsGRouterGenericDataItem *data_item)
{ {
RsGRouterSerialiser signature_serializer(RsGenericSerializer::SERIALIZATION_FLAG_SIGNATURE | RsGenericSerializer::SERIALIZATION_FLAG_SKIP_HEADER); RsGRouterSerialiser signature_serializer(
RsSerializationFlags::SIGNATURE |
RsSerializationFlags::SKIP_HEADER );
uint32_t signed_data_size = signature_serializer.size(const_cast<RsGRouterGenericDataItem*>(data_item)); uint32_t signed_data_size = signature_serializer.size(const_cast<RsGRouterGenericDataItem*>(data_item));
uint32_t total_size = signed_data_size + data_item->signature.TlvSize() ; uint32_t total_size = signed_data_size + data_item->signature.TlvSize() ;
@ -2034,7 +2036,9 @@ bool p3GRouter::signDataItem(RsGRouterAbstractMsgItem *item,const RsGxsId& signi
std::cerr << " Key ID = " << signing_id << std::endl; std::cerr << " Key ID = " << signing_id << std::endl;
std::cerr << " Getting key material..." << std::endl; std::cerr << " Getting key material..." << std::endl;
//#endif //#endif
RsGRouterSerialiser signature_serializer(RsGenericSerializer::SERIALIZATION_FLAG_SIGNATURE | RsGenericSerializer::SERIALIZATION_FLAG_SKIP_HEADER) ; RsGRouterSerialiser signature_serializer(
RsSerializationFlags::SIGNATURE |
RsSerializationFlags::SKIP_HEADER );
uint32_t data_size = signature_serializer.size(item) ; uint32_t data_size = signature_serializer.size(item) ;
RsTemporaryMemory data(data_size) ; RsTemporaryMemory data(data_size) ;
@ -2092,8 +2096,8 @@ bool p3GRouter::verifySignedDataItem(const RsGRouterAbstractMsgItem *item,const
} }
RsGRouterSerialiser signature_serializer( RsGRouterSerialiser signature_serializer(
RsGenericSerializer::SERIALIZATION_FLAG_SIGNATURE | RsSerializationFlags::SIGNATURE |
RsGenericSerializer::SERIALIZATION_FLAG_SKIP_HEADER ); RsSerializationFlags::SKIP_HEADER );
uint32_t data_size = signature_serializer.size(const_cast<RsGRouterAbstractMsgItem*>(item)); // the const cast shouldn't be necessary if size() took a const. uint32_t data_size = signature_serializer.size(const_cast<RsGRouterAbstractMsgItem*>(item)); // the const cast shouldn't be necessary if size() took a const.
RsTemporaryMemory data(data_size); RsTemporaryMemory data(data_size);

View file

@ -78,7 +78,7 @@ JsonApiServer::corsOptionsHeaders =
#define INITIALIZE_API_CALL_JSON_CONTEXT \ #define INITIALIZE_API_CALL_JSON_CONTEXT \
RsGenericSerializer::SerializeContext cReq( \ RsGenericSerializer::SerializeContext cReq( \
nullptr, 0, \ nullptr, 0, \
RsGenericSerializer::SERIALIZATION_FLAG_YIELDING ); \ RsSerializationFlags::YIELDING ); \
RsJson& jReq(cReq.mJson); \ RsJson& jReq(cReq.mJson); \
if(session->get_request()->get_method() == "GET") \ if(session->get_request()->get_method() == "GET") \
{ \ { \

View file

@ -630,38 +630,49 @@ std::string PGPHandler::SaveCertificateToString(const RsPgpId& id,bool include_s
return makeRadixEncodedPGPKey(key,include_signatures) ; return makeRadixEncodedPGPKey(key,include_signatures) ;
} }
bool PGPHandler::exportPublicKey(const RsPgpId& id,unsigned char *& mem_block,size_t& mem_size,bool armoured,bool include_signatures) const bool PGPHandler::exportPublicKey(
const RsPgpId& id,
rs_view_ptr<unsigned char>& mem_block, size_t& mem_size,
bool armoured, bool include_signatures ) const
{ {
RsStackMutex mtx(pgphandlerMtx) ; // lock access to PGP memory structures. mem_block = nullptr; mem_size = 0; // clear just in case
const ops_keydata_t *key = locked_getPublicKey(id,false) ;
mem_block = NULL ;
if(armoured) if(armoured)
{ {
std::cerr << __PRETTY_FUNCTION__ << ": should not be used with armoured=true, because there's a bug in the armoured export of OPS" << std::endl; RsErr() << __PRETTY_FUNCTION__ << " should not be used with "
<< "armoured=true, because there's a bug in the armoured export"
<< " of OPS" << std::endl;
print_stacktrace();
return false; return false;
} }
if(key == NULL) RS_STACK_MUTEX(pgphandlerMtx);
const ops_keydata_t* key = locked_getPublicKey(id,false);
if(!key)
{ {
std::cerr << "Cannot output key " << id.toStdString() << ": not found in keyring." << std::endl; RsErr() << __PRETTY_FUNCTION__ << " key id: " << id
<< " not found in keyring." << std::endl;
return false; return false;
} }
ops_create_info_t* cinfo; ops_create_info_t* cinfo;
ops_memory_t *buf = NULL ; ops_memory_t *buf = nullptr;
ops_setup_memory_write(&cinfo, &buf, 0); ops_setup_memory_write(&cinfo, &buf, 0);
if(ops_write_transferable_public_key_from_packet_data(key,armoured,cinfo) != ops_true) if(ops_write_transferable_public_key_from_packet_data(
key, armoured, cinfo ) != ops_true)
{ {
std::cerr << "ERROR: This key cannot be processed by RetroShare because\nDSA certificates are not yet handled." << std::endl; RsErr() << __PRETTY_FUNCTION__ << " This key id " << id
<< " cannot be processed by RetroShare because DSA certificates"
<< " support is not implemented yet." << std::endl;
return false; return false;
} }
ops_writer_close(cinfo); ops_writer_close(cinfo);
mem_block = new unsigned char[ops_memory_get_length(buf)] ;
mem_size = ops_memory_get_length(buf); mem_size = ops_memory_get_length(buf);
mem_block = reinterpret_cast<unsigned char*>(malloc(mem_size));
memcpy(mem_block,ops_memory_get_data(buf),mem_size); memcpy(mem_block,ops_memory_get_data(buf),mem_size);
ops_teardown_memory_write(cinfo,buf); ops_teardown_memory_write(cinfo,buf);

View file

@ -107,7 +107,9 @@ public:
bool LoadCertificateFromBinaryData(const unsigned char *bin_data,uint32_t bin_data_len, RsPgpId& gpg_id, std::string& error_string); bool LoadCertificateFromBinaryData(const unsigned char *bin_data,uint32_t bin_data_len, RsPgpId& gpg_id, std::string& error_string);
std::string SaveCertificateToString(const RsPgpId& id,bool include_signatures) const ; std::string SaveCertificateToString(const RsPgpId& id,bool include_signatures) const ;
bool exportPublicKey(const RsPgpId& id,unsigned char *& mem,size_t& mem_size,bool armoured,bool include_signatures) const ; bool exportPublicKey( const RsPgpId& id,
rs_view_ptr<unsigned char>& mem,size_t& mem_size,
bool armoured, bool include_signatures) const;
bool parseSignature(unsigned char *sign, unsigned int signlen,RsPgpId& issuer_id) ; bool parseSignature(unsigned char *sign, unsigned int signlen,RsPgpId& issuer_id) ;
bool SignDataBin(const RsPgpId& id, const void *data, const uint32_t len, unsigned char *sign, unsigned int *signlen, bool make_raw_signature=false, std::string reason = "") ; bool SignDataBin(const RsPgpId& id, const void *data, const uint32_t len, unsigned char *sign, unsigned int *signlen, bool make_raw_signature=false, std::string reason = "") ;

View file

@ -241,7 +241,3 @@ typedef t_RsFlags32<FLAGS_TAG_SERVICE_PERM > ServicePermissionFlags ;
// //
typedef t_RsFlags32<FLAGS_TAG_SERVICE_CHAT > ChatLobbyFlags ; typedef t_RsFlags32<FLAGS_TAG_SERVICE_CHAT > ChatLobbyFlags ;
/// @deprecated Flags for serializer
RS_DEPRECATED_FOR(RsSerializationFlags)
typedef t_RsFlags32<FLAGS_TAG_SERIALIZER > SerializationFlags ;

View file

@ -803,7 +803,7 @@ public:
ServicePermissionFlags flags = RS_NODE_PERM_DEFAULT ) = 0; ServicePermissionFlags flags = RS_NODE_PERM_DEFAULT ) = 0;
/* Auth Stuff */ RS_DEPRECATED /// This function doesn't provide meaningful error reporting
virtual std::string getPGPKey(const RsPgpId& pgp_id,bool include_signatures) = 0; virtual std::string getPGPKey(const RsPgpId& pgp_id,bool include_signatures) = 0;
virtual bool GetPGPBase64StringAndCheckSum(const RsPgpId& gpg_id,std::string& gpg_base64_string,std::string& gpg_base64_checksum) = 0; virtual bool GetPGPBase64StringAndCheckSum(const RsPgpId& gpg_id,std::string& gpg_base64_string,std::string& gpg_base64_checksum) = 0;

View file

@ -45,11 +45,12 @@ struct RsItem : RsMemoryManagement::SmallObject, RsSerializable
/// TODO: Do this make sense with the new serialization system? /// TODO: Do this make sense with the new serialization system?
virtual void clear() = 0; virtual void clear() = 0;
/// @deprecated use << ostream operator instead
RS_DEPRECATED_FOR("<< ostream operator")
virtual std::ostream &print(std::ostream &out, uint16_t /* indent */ = 0) virtual std::ostream &print(std::ostream &out, uint16_t /* indent */ = 0)
{ {
RsGenericSerializer::SerializeContext ctx( RsGenericSerializer::SerializeContext ctx(
NULL, 0, RsGenericSerializer::FORMAT_BINARY, nullptr, 0, RsSerializationFlags::NONE );
RsGenericSerializer::SERIALIZATION_FLAG_NONE );
serial_process(RsGenericSerializer::PRINT,ctx); serial_process(RsGenericSerializer::PRINT,ctx);
return out; return out;
} }

View file

@ -147,8 +147,8 @@ void RsMsgItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSeri
RsTypeSerializer::serial_process<RsTlvItem>(j,ctx,attachment,"attachment"); RsTypeSerializer::serial_process<RsTlvItem>(j,ctx,attachment,"attachment");
if(ctx.mFlags & RsServiceSerializer::SERIALIZATION_FLAG_CONFIG) if(!!(ctx.mFlags & RsSerializationFlags::CONFIG))
RsTypeSerializer::serial_process<uint32_t>(j,ctx,msgId,"msgId"); RS_SERIAL_PROCESS(msgId);
} }
void RsMsgTagType::clear() void RsMsgTagType::clear()

View file

@ -19,8 +19,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * * along with this program. If not, see <https://www.gnu.org/licenses/>. *
* * * *
*******************************************************************************/ *******************************************************************************/
#ifndef RS_MSG_ITEMS_H #pragma once
#define RS_MSG_ITEMS_H
#include <map> #include <map>
@ -33,10 +32,6 @@
#include "serialiser/rstlvfileitem.h" #include "serialiser/rstlvfileitem.h"
#include "grouter/grouteritems.h" #include "grouter/grouteritems.h"
#if 0
#include "serialiser/rstlvtypes.h"
#include "serialiser/rstlvfileitem.h"
#endif
/**************************************************************************/ /**************************************************************************/
@ -219,16 +214,11 @@ class RsMsgParentId : public RsMessageItem
class RsMsgSerialiser: public RsServiceSerializer class RsMsgSerialiser: public RsServiceSerializer
{ {
public: public:
RsMsgSerialiser(SerializationFlags flags = RsServiceSerializer::SERIALIZATION_FLAG_NONE) RsMsgSerialiser(
:RsServiceSerializer(RS_SERVICE_TYPE_MSG,RsGenericSerializer::FORMAT_BINARY,flags){} RsSerializationFlags flags = RsSerializationFlags::NONE ):
RsServiceSerializer(RS_SERVICE_TYPE_MSG, flags){}
virtual ~RsMsgSerialiser() {} RsItem* create_item(uint16_t service,uint8_t type) const override;
virtual RsItem *create_item(uint16_t service,uint8_t type) const ; ~RsMsgSerialiser() override = default;
}; };
/**************************************************************************/
#endif /* RS_MSG_ITEMS_H */

View file

@ -3,8 +3,9 @@
* * * *
* libretroshare: retroshare core library * * libretroshare: retroshare core library *
* * * *
* Copyright 2004-2008 by Robert Fernie <retroshare@lunamutt.com> * * Copyright (C) 2004-2008 Robert Fernie <retroshare@lunamutt.com> *
* Copyright (C) 2015-2018 Gioacchino Mazzurco <gio@eigenlab.org> * * Copyright (C) 2015-2020 Gioacchino Mazzurco <gio@eigenlab.org> *
* Copyright (C) 2020 Asociación Civil Altermundi <info@altermundi.net> *
* * * *
* This program is free software: you can redistribute it and/or modify * * This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as * * it under the terms of the GNU Lesser General Public License as *
@ -20,7 +21,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * * along with this program. If not, see <https://www.gnu.org/licenses/>. *
* * * *
*******************************************************************************/ *******************************************************************************/
#include "util/radix64.h"
#include "pgp/pgpkeyutil.h" #include "pgp/pgpkeyutil.h"
#include "rsserver/p3peers.h" #include "rsserver/p3peers.h"
@ -35,7 +36,8 @@
#include "retroshare/rsinit.h" #include "retroshare/rsinit.h"
#include "retroshare/rsfiles.h" #include "retroshare/rsfiles.h"
#include "util/rsurl.h" #include "util/rsurl.h"
#include "util/radix64.h"
#include "util/rsbase64.h"
#include "pgp/rscertificate.h" #include "pgp/rscertificate.h"
#include <iostream> #include <iostream>
@ -1095,49 +1097,57 @@ bool p3Peers::setProxyServer(const uint32_t type, const std::string &addr_str, c
std::string p3Peers::getPGPKey(const RsPgpId& pgp_id,bool include_signatures) std::string p3Peers::getPGPKey(const RsPgpId& pgp_id,bool include_signatures)
{ {
unsigned char *mem_block = NULL; rs_owner_ptr<unsigned char> mem_block = nullptr;
size_t mem_block_size = 0; size_t mem_block_size = 0;
if( !AuthGPG::getAuthGPG()->exportPublicKey( if( !AuthGPG::getAuthGPG()->exportPublicKey(
RsPgpId(pgp_id), mem_block, mem_block_size, RsPgpId(pgp_id), mem_block, mem_block_size,
false, include_signatures ) ) false, include_signatures ) )
{ {
std::cerr << "Cannot output certificate for id \"" << pgp_id RsErr() << __PRETTY_FUNCTION__
<< "\". Sorry." << std::endl; << " Failure retriving certificate for id " << pgp_id
<< std::endl;
return ""; return "";
} }
RsPeerDetails Detail; RsPeerDetails details;
if(!getGPGDetails(pgp_id,Detail)) return ""; if(!getGPGDetails(pgp_id, details)) return "";
RsCertificate cert( Detail,mem_block,mem_block_size ); auto certPtr =
delete[] mem_block ; RsCertificate::fromMemoryBlock(details, mem_block, mem_block_size);
return cert.armouredPGPKey(); free(mem_block);
if(certPtr) return certPtr->armouredPGPKey();
return "";
} }
bool p3Peers::GetPGPBase64StringAndCheckSum( const RsPgpId& gpg_id, bool p3Peers::GetPGPBase64StringAndCheckSum(
std::string& gpg_base64_string, const RsPgpId& gpg_id,
std::string& gpg_base64_checksum) std::string& gpg_base64_string, std::string& gpg_base64_checksum )
{ {
gpg_base64_string = "" ; gpg_base64_string = "" ;
gpg_base64_checksum = "" ; gpg_base64_checksum = "" ;
unsigned char *mem_block ; rs_owner_ptr<unsigned char> mem_block = nullptr;
size_t mem_block_size ; size_t mem_block_size = 0;
if(!AuthGPG::getAuthGPG()->exportPublicKey(
if(!AuthGPG::getAuthGPG()->exportPublicKey(gpg_id,mem_block,mem_block_size,false,false)) gpg_id,mem_block,mem_block_size,false,false ))
return false; return false;
Radix64::encode(mem_block,mem_block_size,gpg_base64_string) ; RsBase64::encode(mem_block, mem_block_size, gpg_base64_string, true, false);
uint32_t crc = PGPKeyManagement::compute24bitsCRC((unsigned char *)mem_block,mem_block_size) ; uint32_t crc = PGPKeyManagement::compute24bitsCRC(mem_block,mem_block_size);
unsigned char tmp[3] = { uint8_t((crc >> 16) & 0xff), uint8_t((crc >> 8) & 0xff), uint8_t(crc & 0xff) } ; free(mem_block);
Radix64::encode(tmp,3,gpg_base64_checksum) ;
delete[] mem_block ; unsigned char tmp[3] = {
uint8_t((crc >> 16) & 0xff),
uint8_t((crc >> 8) & 0xff),
uint8_t(crc & 0xff) } ;
RsBase64::encode(tmp, 3, gpg_base64_checksum, true, false);
return true; return true;
} }

View file

@ -141,7 +141,8 @@ public:
virtual std::string GetRetroshareInvite( virtual std::string GetRetroshareInvite(
const RsPeerId& ssl_id = RsPeerId(), const RsPeerId& ssl_id = RsPeerId(),
bool include_signatures = false, bool includeExtraLocators = true ); bool include_signatures = false, bool includeExtraLocators = true );
virtual std::string getPGPKey(const RsPgpId& pgp_id,bool include_signatures); RS_DEPRECATED /// @see RsPeers
std::string getPGPKey(const RsPgpId& pgp_id,bool include_signatures) override;
virtual bool GetPGPBase64StringAndCheckSum(const RsPgpId& gpg_id,std::string& gpg_base64_string,std::string& gpg_base64_checksum); virtual bool GetPGPBase64StringAndCheckSum(const RsPgpId& gpg_id,std::string& gpg_base64_string,std::string& gpg_base64_checksum);

View file

@ -4,6 +4,8 @@
* libretroshare: retroshare core library * * libretroshare: retroshare core library *
* * * *
* Copyright (C) 2016 Cyril Soler <csoler@users.sourceforge.net> * * Copyright (C) 2016 Cyril Soler <csoler@users.sourceforge.net> *
* Copyright (C) 2020 Gioacchino Mazzurco <gio@eigenlab.org> *
* Copyright (C) 2020 Asociación Civil Altermundi <info@altermundi.net> *
* * * *
* This program is free software: you can redistribute it and/or modify * * This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as * * it under the terms of the GNU Lesser General Public License as *
@ -18,23 +20,17 @@
* You should have received a copy of the GNU Lesser General Public License * * 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/>. * * along with this program. If not, see <https://www.gnu.org/licenses/>. *
* * * *
*******************************************************************************/ ******************************************************************************/
#include <typeinfo> #include <typeinfo>
#include "rsitems/rsitem.h" #include "rsitems/rsitem.h"
#include "util/rsprint.h" #include "util/rsprint.h"
#include "serialiser/rsserializer.h" #include "serialiser/rsserializer.h"
#include "serialiser/rstypeserializer.h" #include "serialiser/rstypeserializer.h"
#include "util/stacktrace.h" #include "util/stacktrace.h"
#include "util/rsdebug.h" #include "util/rsdebug.h"
const SerializationFlags RsGenericSerializer::SERIALIZATION_FLAG_NONE ( 0x0000 );
const SerializationFlags RsGenericSerializer::SERIALIZATION_FLAG_CONFIG ( 0x0001 );
const SerializationFlags RsGenericSerializer::SERIALIZATION_FLAG_SIGNATURE ( 0x0002 );
const SerializationFlags RsGenericSerializer::SERIALIZATION_FLAG_SKIP_HEADER ( 0x0004 );
const SerializationFlags RsGenericSerializer::SERIALIZATION_FLAG_YIELDING ( 0x0008 );
RsItem *RsServiceSerializer::deserialise(void *data, uint32_t *size) RsItem *RsServiceSerializer::deserialise(void *data, uint32_t *size)
{ {
if(!data || !size || !*size) if(!data || !size || !*size)
@ -47,10 +43,12 @@ RsItem *RsServiceSerializer::deserialise(void *data, uint32_t *size)
return nullptr; return nullptr;
} }
if(mFlags & SERIALIZATION_FLAG_SKIP_HEADER) if(!!(mFlags & RsSerializationFlags::SKIP_HEADER))
{ {
std::cerr << "(EE) Cannot deserialise item with flags SERIALIZATION_FLAG_SKIP_HEADER. Check your code!" << std::endl; RsErr() << __PRETTY_FUNCTION__ << " Cannot deserialise item with flag "
return NULL ; << "SKIP_HEADER. Check your code!" << std::endl;
print_stacktrace();
return nullptr;
} }
uint32_t rstype = getRsItemId(const_cast<void*>((const void*)data)) ; uint32_t rstype = getRsItemId(const_cast<void*>((const void*)data)) ;
@ -64,7 +62,9 @@ RsItem *RsServiceSerializer::deserialise(void *data, uint32_t *size)
return NULL ; return NULL ;
} }
SerializeContext ctx(const_cast<uint8_t*>(static_cast<uint8_t*>(data)),*size,mFormat,mFlags); SerializeContext ctx(
const_cast<uint8_t*>(static_cast<uint8_t*>(data)), *size,
mFlags );
ctx.mOffset = 8 ; ctx.mOffset = 8 ;
item->serial_process(RsGenericSerializer::DESERIALIZE, ctx) ; item->serial_process(RsGenericSerializer::DESERIALIZE, ctx) ;
@ -85,10 +85,12 @@ RsItem *RsServiceSerializer::deserialise(void *data, uint32_t *size)
} }
RsItem *RsConfigSerializer::deserialise(void *data, uint32_t *size) RsItem *RsConfigSerializer::deserialise(void *data, uint32_t *size)
{ {
if(mFlags & SERIALIZATION_FLAG_SKIP_HEADER) if(!!(mFlags & RsSerializationFlags::SKIP_HEADER))
{ {
std::cerr << "(EE) Cannot deserialise item with flags SERIALIZATION_FLAG_SKIP_HEADER. Check your code!" << std::endl; RsErr() << __PRETTY_FUNCTION__ << " Cannot deserialise item with flag "
return NULL ; << "SKIP_HEADER. Check your code!" << std::endl;
print_stacktrace();
return nullptr;
} }
uint32_t rstype = getRsItemId(const_cast<void*>((const void*)data)) ; uint32_t rstype = getRsItemId(const_cast<void*>((const void*)data)) ;
@ -102,7 +104,9 @@ RsItem *RsConfigSerializer::deserialise(void *data, uint32_t *size)
return NULL ; return NULL ;
} }
SerializeContext ctx(const_cast<uint8_t*>(static_cast<uint8_t*>(data)),*size,mFormat,mFlags); SerializeContext ctx(
const_cast<uint8_t*>(static_cast<uint8_t*>(data)), *size,
mFlags );
ctx.mOffset = 8 ; ctx.mOffset = 8 ;
item->serial_process(DESERIALIZE, ctx) ; item->serial_process(DESERIALIZE, ctx) ;
@ -121,50 +125,44 @@ RsItem *RsConfigSerializer::deserialise(void *data, uint32_t *size)
delete item ; delete item ;
return NULL ; return NULL ;
} }
bool RsGenericSerializer::serialise(RsItem* item, void* data, uint32_t* size) bool RsGenericSerializer::serialise(RsItem* item, void* data, uint32_t* size)
{ {
SerializeContext ctx(static_cast<uint8_t*>(data),0,mFormat,mFlags);
uint32_t tlvsize = this->size(item); uint32_t tlvsize = this->size(item);
if(tlvsize > *size) constexpr auto fName = __PRETTY_FUNCTION__;
throw std::runtime_error("Cannot serialise: not enough room.") ; const auto failure = [=](std::error_condition ec)
{
RsErr() << fName << " " << ec << std::endl;
print_stacktrace();
return false;
};
if(tlvsize > *size) return failure(std::errc::no_buffer_space);
if(mFlags & SERIALIZATION_FLAG_SKIP_HEADER) SerializeContext ctx(static_cast<uint8_t*>(data), tlvsize, mFlags);
ctx.mOffset = 0;
else if(!(mFlags & RsSerializationFlags::SKIP_HEADER))
{ {
if(!setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize)) if(!setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize))
{ return failure(std::errc::no_buffer_space);
std::cerr << "RsSerializer::serialise_item(): ERROR. Not enough size!" << std::endl;
return false ;
}
ctx.mOffset = 8; ctx.mOffset = 8;
} }
ctx.mSize = tlvsize;
item->serial_process(RsGenericSerializer::SERIALIZE,ctx); item->serial_process(RsGenericSerializer::SERIALIZE,ctx);
if(ctx.mSize != ctx.mOffset) if(ctx.mSize != ctx.mOffset) return failure(std::errc::message_size);
{
std::cerr << "RsSerializer::serialise(): ERROR. offset does not match expected size!" << std::endl;
return false ;
}
*size = ctx.mOffset ;
*size = ctx.mOffset;
return true; return true;
} }
uint32_t RsGenericSerializer::size(RsItem *item) uint32_t RsGenericSerializer::size(RsItem *item)
{ {
SerializeContext ctx(NULL,0,mFormat,mFlags); SerializeContext ctx(nullptr, 0, mFlags);
if(mFlags & SERIALIZATION_FLAG_SKIP_HEADER) if(!!(mFlags & RsSerializationFlags::SKIP_HEADER)) ctx.mOffset = 0;
ctx.mOffset = 0; else ctx.mOffset = 8; // header size
else
ctx.mOffset = 8 ; // header size
item->serial_process(SIZE_ESTIMATE, ctx) ; item->serial_process(SIZE_ESTIMATE, ctx) ;
return ctx.mOffset ; return ctx.mOffset ;
@ -172,7 +170,7 @@ uint32_t RsGenericSerializer::size(RsItem *item)
void RsGenericSerializer::print(RsItem *item) void RsGenericSerializer::print(RsItem *item)
{ {
SerializeContext ctx(NULL,0,mFormat,mFlags); SerializeContext ctx(nullptr, 0, mFlags);
std::cerr << "***** RsItem class: \"" << typeid(*item).name() << "\" *****" << std::endl; std::cerr << "***** RsItem class: \"" << typeid(*item).name() << "\" *****" << std::endl;
item->serial_process(PRINT, ctx) ; item->serial_process(PRINT, ctx) ;
@ -255,7 +253,7 @@ RsItem *RsRawSerialiser::deserialise(void *data, uint32_t *pktsize)
RsGenericSerializer::SerializeContext::SerializeContext( RsGenericSerializer::SerializeContext::SerializeContext(
uint8_t* data, uint32_t size, SerializationFlags flags, uint8_t* data, uint32_t size, RsSerializationFlags flags,
RsJson::AllocatorType* allocator ) : RsJson::AllocatorType* allocator ) :
mData(data), mSize(size), mOffset(0), mOk(true), mFlags(flags), mData(data), mSize(size), mOffset(0), mOk(true), mFlags(flags),
mJson(rapidjson::kObjectType, allocator) mJson(rapidjson::kObjectType, allocator)
@ -264,20 +262,23 @@ RsGenericSerializer::SerializeContext::SerializeContext(
{ {
if(size == 0) if(size == 0)
{ {
std::cerr << __PRETTY_FUNCTION__ << " data passed without " RsFatal() << __PRETTY_FUNCTION__ << " data passed without "
<< "size! This make no sense report to developers!" << "size! This make no sense report to developers!"
<< std::endl; << std::endl;
print_stacktrace(); print_stacktrace();
exit(-EINVAL);
} }
if(flags & SERIALIZATION_FLAG_YIELDING) if(!!(flags & RsSerializationFlags::YIELDING))
{ {
std::cerr << __PRETTY_FUNCTION__ << " Attempt to create a " RsFatal() << __PRETTY_FUNCTION__
<< " Attempt to create a "
<< "binary serialization context with " << "binary serialization context with "
<< "SERIALIZATION_FLAG_YIELDING! " << "SERIALIZATION_FLAG_YIELDING! "
<< "This make no sense report to developers!" << "This make no sense report to developers!"
<< std::endl; << std::endl;
print_stacktrace(); print_stacktrace();
exit(-EINVAL);
} }
} }
} }

View file

@ -238,58 +238,23 @@ struct RsGenericSerializer : RsSerialType
FROM_JSON FROM_JSON
} SerializeJob; } SerializeJob;
/** @deprecated use SerializeJob instead */
RS_DEPRECATED typedef enum
{
FORMAT_BINARY = 0x01,
FORMAT_JSON = 0x02
} SerializationFormat;
struct SerializeContext struct SerializeContext
{ {
/** Allow shared allocator usage to avoid costly JSON deepcopy for /** Allow shared allocator usage to avoid costly JSON deepcopy for
* nested RsSerializable */ * nested RsSerializable */
SerializeContext( SerializeContext(
uint8_t* data = nullptr, uint32_t size = 0, uint8_t* data = nullptr, uint32_t size = 0,
SerializationFlags flags = SERIALIZATION_FLAG_NONE, RsSerializationFlags flags = RsSerializationFlags::NONE,
RsJson::AllocatorType* allocator = nullptr); RsJson::AllocatorType* allocator = nullptr);
RS_DEPRECATED SerializeContext(
uint8_t *data, uint32_t size, SerializationFormat format,
SerializationFlags flags,
RsJson::AllocatorType* allocator = nullptr) :
mData(data), mSize(size), mOffset(0), mOk(true), mFormat(format),
mFlags{flags}, mJson(rapidjson::kObjectType, allocator) {}
unsigned char *mData; unsigned char *mData;
uint32_t mSize; uint32_t mSize;
uint32_t mOffset; uint32_t mOffset;
bool mOk; bool mOk;
RS_DEPRECATED SerializationFormat mFormat; RsSerializationFlags mFlags;
SerializationFlags mFlags;
RsJson mJson; RsJson mJson;
}; };
/** These are convenience flags to be used by the items when processing the
* data. The names of the flags are not very important. What matters is that
* the serial_process() method of each item correctly deals with the data
* when it sees the flags, if the serialiser sets them.
* By default the flags are not set and shouldn't be handled.
* When deriving a new serializer, the user can set his own flags, using
* compatible values
*/
static const SerializationFlags SERIALIZATION_FLAG_NONE; // 0x0000
static const SerializationFlags SERIALIZATION_FLAG_CONFIG; // 0x0001
static const SerializationFlags SERIALIZATION_FLAG_SIGNATURE; // 0x0002
static const SerializationFlags SERIALIZATION_FLAG_SKIP_HEADER; // 0x0004
/** Used for JSON deserialization in JSON API, it causes the deserialization
* to continue even if some field is missing (or incorrect), this way the
* API is more user friendly as some methods need just part of the structs
* they take as parameters. */
static const SerializationFlags SERIALIZATION_FLAG_YIELDING; // 0x0008
/** /**
* The following functions overload RsSerialType. * The following functions overload RsSerialType.
* They *should not* need to be further overloaded. * They *should not* need to be further overloaded.
@ -302,18 +267,15 @@ struct RsGenericSerializer : RsSerialType
protected: protected:
RsGenericSerializer( RsGenericSerializer(
uint8_t serial_class, uint8_t serial_type, uint8_t serial_class, uint8_t serial_type,
SerializationFormat format, SerializationFlags flags ) : RsSerializationFlags flags ):
RsSerialType( RS_PKT_VERSION1, serial_class, serial_type), RsSerialType( RS_PKT_VERSION1, serial_class, serial_type),
mFormat(format), mFlags(flags) {}
RsGenericSerializer(
uint16_t service, SerializationFormat format,
SerializationFlags flags ) :
RsSerialType( RS_PKT_VERSION_SERVICE, service ), mFormat(format),
mFlags(flags) {} mFlags(flags) {}
SerializationFormat mFormat; RsGenericSerializer(
SerializationFlags mFlags; uint16_t service, RsSerializationFlags flags ):
RsSerialType( RS_PKT_VERSION_SERVICE, service ), mFlags(flags) {}
RsSerializationFlags mFlags;
}; };
@ -323,9 +285,9 @@ protected:
struct RsServiceSerializer : RsGenericSerializer struct RsServiceSerializer : RsGenericSerializer
{ {
RsServiceSerializer( RsServiceSerializer(
uint16_t service_id, SerializationFormat format = FORMAT_BINARY, uint16_t service_id,
SerializationFlags flags = SERIALIZATION_FLAG_NONE ) : RsSerializationFlags flags = RsSerializationFlags::NONE ) :
RsGenericSerializer(service_id, format, flags) {} RsGenericSerializer(service_id, flags) {}
/*! should be overloaded to create the correct type of item depending on the /*! should be overloaded to create the correct type of item depending on the
* data */ * data */
@ -342,11 +304,10 @@ struct RsServiceSerializer : RsGenericSerializer
*/ */
struct RsConfigSerializer : RsGenericSerializer struct RsConfigSerializer : RsGenericSerializer
{ {
RsConfigSerializer(uint8_t config_class, RsConfigSerializer(
uint8_t config_type, uint8_t config_class, uint8_t config_type,
SerializationFormat format = FORMAT_BINARY, RsSerializationFlags flags = RsSerializationFlags::NONE ) :
SerializationFlags flags = SERIALIZATION_FLAG_NONE) : RsGenericSerializer(config_class, config_type, flags) {}
RsGenericSerializer(config_class,config_type,format,flags) {}
/*! should be overloaded to create the correct type of item depending on the /*! should be overloaded to create the correct type of item depending on the
* data */ * data */

View file

@ -555,6 +555,8 @@ void RsTypeSerializer::RawMemoryWrapper::serial_process(
if(!serialSize) if(!serialSize)
{ {
Dbg3() << __PRETTY_FUNCTION__ << " Deserialized empty memory chunk"
<< std::endl;
clear(); clear();
break; break;
} }
@ -573,7 +575,7 @@ void RsTypeSerializer::RawMemoryWrapper::serial_process(
if(serialSize != second) if(serialSize != second)
{ {
first = reinterpret_cast<uint8_t*>(realloc(first, serialSize)); first = reinterpret_cast<uint8_t*>(realloc(first, serialSize));
second = static_cast<uint32_t>(serialSize); second = serialSize;
} }
memcpy(first, ctx.mData + ctx.mOffset, second); memcpy(first, ctx.mData + ctx.mOffset, second);
@ -593,8 +595,8 @@ void RsTypeSerializer::RawMemoryWrapper::serial_process(
} }
case RsGenericSerializer::FROM_JSON: case RsGenericSerializer::FROM_JSON:
{ {
const bool yelding = !!( RsSerializationFlags::YIELDING & const bool yelding = !!(
ctx.mFlags.toEFT<RsSerializationFlags>() ); RsSerializationFlags::YIELDING & ctx.mFlags );
if(!(ctx.mOk || yelding)) if(!(ctx.mOk || yelding))
{ {
clear(); clear();

View file

@ -82,8 +82,7 @@ struct RsTypeSerializer
INTT& member, const std::string& member_name ) INTT& member, const std::string& member_name )
{ {
const bool VLQ_ENCODING = !!( const bool VLQ_ENCODING = !!(
RsSerializationFlags::INTEGER_VLQ & RsSerializationFlags::INTEGER_VLQ & ctx.mFlags );
ctx.mFlags.toEFT<RsSerializationFlags>() );
switch(j) switch(j)
{ {
@ -149,8 +148,7 @@ struct RsTypeSerializer
break; break;
case RsGenericSerializer::FROM_JSON: case RsGenericSerializer::FROM_JSON:
ctx.mOk &= ( ctx.mOk || ctx.mOk &= ( ctx.mOk ||
!!( RsSerializationFlags::YIELDING & !!(RsSerializationFlags::YIELDING & ctx.mFlags) )
ctx.mFlags.toEFT<RsSerializationFlags>() ) )
&& from_JSON(member_name, member, ctx.mJson); && from_JSON(member_name, member, ctx.mJson);
break; break;
default: fatalUnknownSerialJob(j); default: fatalUnknownSerialJob(j);
@ -198,7 +196,8 @@ struct RsTypeSerializer
ctx.mOk = ctx.mOk && to_JSON(member_name, member, ctx.mJson); ctx.mOk = ctx.mOk && to_JSON(member_name, member, ctx.mJson);
break; break;
case RsGenericSerializer::FROM_JSON: case RsGenericSerializer::FROM_JSON:
ctx.mOk &= (ctx.mOk || ctx.mFlags & RsGenericSerializer::SERIALIZATION_FLAG_YIELDING) ctx.mOk &= ( ctx.mOk ||
!!(ctx.mFlags & RsSerializationFlags::YIELDING) )
&& from_JSON(member_name, member, ctx.mJson); && from_JSON(member_name, member, ctx.mJson);
break; break;
default: fatalUnknownSerialJob(j); default: fatalUnknownSerialJob(j);
@ -236,7 +235,7 @@ struct RsTypeSerializer
break; break;
case RsGenericSerializer::FROM_JSON: case RsGenericSerializer::FROM_JSON:
ctx.mOk &= ctx.mOk &=
(ctx.mOk || ctx.mFlags & RsGenericSerializer::SERIALIZATION_FLAG_YIELDING) (ctx.mOk || !!(ctx.mFlags & RsSerializationFlags::YIELDING))
&& from_JSON(member_name, type_id, member, ctx.mJson); && from_JSON(member_name, type_id, member, ctx.mJson);
break; break;
default: fatalUnknownSerialJob(j); default: fatalUnknownSerialJob(j);
@ -324,7 +323,7 @@ struct RsTypeSerializer
{ {
using namespace rapidjson; using namespace rapidjson;
bool ok = ctx.mOk || ctx.mFlags & RsGenericSerializer::SERIALIZATION_FLAG_YIELDING; bool ok = ctx.mOk || !!(ctx.mFlags & RsSerializationFlags::YIELDING);
Document& jDoc(ctx.mJson); Document& jDoc(ctx.mJson);
Document::AllocatorType& allocator = jDoc.GetAllocator(); Document::AllocatorType& allocator = jDoc.GetAllocator();
@ -424,8 +423,7 @@ struct RsTypeSerializer
RsJson& jDoc(ctx.mJson); RsJson& jDoc(ctx.mJson);
const char* mName = memberName.c_str(); const char* mName = memberName.c_str();
bool hasMember = jDoc.HasMember(mName); bool hasMember = jDoc.HasMember(mName);
bool yielding = ctx.mFlags & bool yielding = !!(ctx.mFlags & RsSerializationFlags::YIELDING);
RsGenericSerializer::SERIALIZATION_FLAG_YIELDING;
if(!hasMember) if(!hasMember)
{ {
@ -560,8 +558,7 @@ struct RsTypeSerializer
{ {
using namespace rapidjson; using namespace rapidjson;
bool ok = ctx.mOk || ctx.mFlags & bool ok = ctx.mOk || !!(ctx.mFlags & RsSerializationFlags::YIELDING);
RsGenericSerializer::SERIALIZATION_FLAG_YIELDING;
Document& jDoc(ctx.mJson); Document& jDoc(ctx.mJson);
Document::AllocatorType& allocator = jDoc.GetAllocator(); Document::AllocatorType& allocator = jDoc.GetAllocator();
@ -669,8 +666,8 @@ struct RsTypeSerializer
break; break;
case RsGenericSerializer::FROM_JSON: case RsGenericSerializer::FROM_JSON:
{ {
bool ok = ctx.mOk || !!( ctx.mFlags.toEFT<RsSerializationFlags>() bool ok = ctx.mOk || !!(
& RsSerializationFlags::YIELDING ); ctx.mFlags & RsSerializationFlags::YIELDING );
ctx.mOk = ok && from_JSON(memberName, member, ctx.mJson) && ctx.mOk; ctx.mOk = ok && from_JSON(memberName, member, ctx.mJson) && ctx.mOk;
break; break;
} }
@ -760,8 +757,7 @@ struct RsTypeSerializer
RsJson& jDoc(ctx.mJson); RsJson& jDoc(ctx.mJson);
const char* mName = memberName.c_str(); const char* mName = memberName.c_str();
bool hasMember = jDoc.HasMember(mName); bool hasMember = jDoc.HasMember(mName);
bool yielding = ctx.mFlags & bool yielding = !!(ctx.mFlags & RsSerializationFlags::YIELDING);
RsGenericSerializer::SERIALIZATION_FLAG_YIELDING;
if(!hasMember) if(!hasMember)
{ {

View file

@ -80,7 +80,9 @@ p3MsgService::p3MsgService( p3ServiceControl *sc, p3IdService *id_serv,
recentlyReceivedMutex("p3MsgService recently received hash mutex"), recentlyReceivedMutex("p3MsgService recently received hash mutex"),
mGxsTransServ(gxsMS) mGxsTransServ(gxsMS)
{ {
_serialiser = new RsMsgSerialiser(RsServiceSerializer::SERIALIZATION_FLAG_NONE); // this serialiser is used for services. It's not the same than the one returned by setupSerialiser(). We need both!! /* this serialiser is used for services. It's not the same than the one
* returned by setupSerialiser(). We need both!! */
_serialiser = new RsMsgSerialiser();
addSerialType(_serialiser); addSerialType(_serialiser);
/* MsgIds are not transmitted, but only used locally as a storage index. /* MsgIds are not transmitted, but only used locally as a storage index.
@ -509,7 +511,7 @@ RsSerialiser* p3MsgService::setupSerialiser() // this serialiser is used for con
{ {
RsSerialiser *rss = new RsSerialiser ; RsSerialiser *rss = new RsSerialiser ;
rss->addSerialType(new RsMsgSerialiser(RsServiceSerializer::SERIALIZATION_FLAG_CONFIG)); rss->addSerialType(new RsMsgSerialiser(RsSerializationFlags::CONFIG));
rss->addSerialType(new RsGeneralConfigSerialiser()); rss->addSerialType(new RsGeneralConfigSerialiser());
return rss; return rss;
@ -2386,10 +2388,11 @@ void p3MsgService::sendDistantMsgItem(RsMsgItem *msgitem)
/* The item is serialized and turned into a generic turtle item. Use use the /* The item is serialized and turned into a generic turtle item. Use use the
* explicit serialiser to make sure that the msgId is not included */ * explicit serialiser to make sure that the msgId is not included */
uint32_t msg_serialized_rssize = RsMsgSerialiser(RsServiceSerializer::SERIALIZATION_FLAG_NONE).size(msgitem) ; uint32_t msg_serialized_rssize = RsMsgSerialiser().size(msgitem);
RsTemporaryMemory msg_serialized_data(msg_serialized_rssize) ; RsTemporaryMemory msg_serialized_data(msg_serialized_rssize) ;
if(!RsMsgSerialiser(RsServiceSerializer::SERIALIZATION_FLAG_NONE).serialise(msgitem,msg_serialized_data,&msg_serialized_rssize)) if( !RsMsgSerialiser().
serialise(msgitem,msg_serialized_data,&msg_serialized_rssize) )
{ {
std::cerr << "(EE) p3MsgService::sendTurtleData(): Serialization error." << std::endl; std::cerr << "(EE) p3MsgService::sendTurtleData(): Serialization error." << std::endl;
return ; return ;