diff --git a/src/OpaqueVal.cc b/src/OpaqueVal.cc index baac74605a..81a836754b 100644 --- a/src/OpaqueVal.cc +++ b/src/OpaqueVal.cc @@ -58,10 +58,10 @@ broker::expected OpaqueVal::Serialize() const auto type = OpaqueMgr::mgr()->TypeID(this); auto d = DoSerialize(); - if ( d == broker::none() ) - return broker::ec::invalid_data; // Cannot serialize + if ( !d ) + return d.error(); - return {broker::vector{std::move(type), std::move(d)}}; + return {broker::vector{std::move(type), std::move(*d)}}; } OpaqueVal* OpaqueVal::Unserialize(const broker::data& data) @@ -288,7 +288,7 @@ StringVal* MD5Val::DoGet() IMPLEMENT_OPAQUE_VALUE(MD5Val) -broker::data MD5Val::DoSerialize() const +broker::expected MD5Val::DoSerialize() const { if ( ! IsValid() ) return broker::vector{false}; @@ -429,7 +429,7 @@ StringVal* SHA1Val::DoGet() IMPLEMENT_OPAQUE_VALUE(SHA1Val) -broker::data SHA1Val::DoSerialize() const +broker::expected SHA1Val::DoSerialize() const { if ( ! IsValid() ) return broker::vector{false}; @@ -573,7 +573,7 @@ StringVal* SHA256Val::DoGet() IMPLEMENT_OPAQUE_VALUE(SHA256Val) -broker::data SHA256Val::DoSerialize() const +broker::expected SHA256Val::DoSerialize() const { if ( ! IsValid() ) return broker::vector{false}; @@ -659,7 +659,7 @@ bool EntropyVal::Get(double *r_ent, double *r_chisq, double *r_mean, IMPLEMENT_OPAQUE_VALUE(EntropyVal) -broker::data EntropyVal::DoSerialize() const +broker::expected EntropyVal::DoSerialize() const { broker::vector d = { @@ -872,7 +872,7 @@ BloomFilterVal::~BloomFilterVal() IMPLEMENT_OPAQUE_VALUE(BloomFilterVal) -broker::data BloomFilterVal::DoSerialize() const +broker::expected BloomFilterVal::DoSerialize() const { broker::vector d; @@ -880,7 +880,7 @@ broker::data BloomFilterVal::DoSerialize() const { auto t = SerializeType(type); if ( t == broker::none() ) - return broker::none(); + return broker::ec::invalid_data; d.emplace_back(t); } @@ -889,7 +889,7 @@ broker::data BloomFilterVal::DoSerialize() const auto bf = bloom_filter->Serialize(); if ( ! bf ) - return broker::none(); + return broker::ec::invalid_data; // Cannot serialize; d.emplace_back(*bf); return d; @@ -976,7 +976,7 @@ void CardinalityVal::Add(const Val* val) IMPLEMENT_OPAQUE_VALUE(CardinalityVal) -broker::data CardinalityVal::DoSerialize() const +broker::expected CardinalityVal::DoSerialize() const { broker::vector d; @@ -984,7 +984,7 @@ broker::data CardinalityVal::DoSerialize() const { auto t = SerializeType(type); if ( t == broker::none() ) - return broker::none(); + return broker::ec::invalid_data; d.emplace_back(t); } @@ -993,7 +993,7 @@ broker::data CardinalityVal::DoSerialize() const auto cs = c->Serialize(); if ( ! cs ) - return broker::none(); + return broker::ec::invalid_data; d.emplace_back(*cs); return d; diff --git a/src/OpaqueVal.h b/src/OpaqueVal.h index f07a0f9752..720d502aaa 100644 --- a/src/OpaqueVal.h +++ b/src/OpaqueVal.h @@ -64,7 +64,7 @@ private: /** Macro to insert into an OpaqueVal-derived class's declaration. */ #define DECLARE_OPAQUE_VALUE(T) \ friend class OpaqueMgr::Register; \ - broker::data DoSerialize() const override; \ + broker::expected DoSerialize() const override; \ bool DoUnserialize(const broker::data& data) override; \ const char* OpaqueName() const override { return #T; } \ static OpaqueVal* OpaqueInstantiate() { return new T(); } @@ -97,7 +97,7 @@ public: * Reinstantiates a value from its serialized Broker representation. * * @param data Broker representation as returned by *Serialize()*. - * @return unserialized instances with referecnce count at +1 + * @return unserialized instances with reference count at +1 */ static OpaqueVal* Unserialize(const broker::data& data); @@ -107,15 +107,19 @@ protected: OpaqueVal() { } /** - * Must be overriden to provide a serialized version of the derived - * class' state. Returns 'broker::none()' if serialization fails, or - * is not supported. + * Must be overridden to provide a serialized version of the derived + * class' state. + * + * @return the serialized data or an error if serialization + * isn't supported or failed. */ - virtual broker::data DoSerialize() const = 0; + virtual broker::expected DoSerialize() const = 0; /** - * Must be overriden to recreate the the derived class' state from a - * serialization. Returns true if successfull. + * Must be overridden to recreate the the derived class' state from a + * serialization. + * + * @return true if successful. */ virtual bool DoUnserialize(const broker::data& data) = 0; diff --git a/src/broker/Data.cc b/src/broker/Data.cc index 76ebff46df..385dbe5381 100644 --- a/src/broker/Data.cc +++ b/src/broker/Data.cc @@ -1114,7 +1114,7 @@ Val* bro_broker::DataVal::castTo(BroType* t) IMPLEMENT_OPAQUE_VALUE(bro_broker::DataVal) -broker::data bro_broker::DataVal::DoSerialize() const +broker::expected bro_broker::DataVal::DoSerialize() const { return data; } @@ -1127,7 +1127,7 @@ bool bro_broker::DataVal::DoUnserialize(const broker::data& data_) IMPLEMENT_OPAQUE_VALUE(bro_broker::SetIterator) -broker::data bro_broker::SetIterator::DoSerialize() const +broker::expected bro_broker::SetIterator::DoSerialize() const { return broker::vector{dat, *it}; } @@ -1154,7 +1154,7 @@ bool bro_broker::SetIterator::DoUnserialize(const broker::data& data) IMPLEMENT_OPAQUE_VALUE(bro_broker::TableIterator) -broker::data bro_broker::TableIterator::DoSerialize() const +broker::expected bro_broker::TableIterator::DoSerialize() const { return broker::vector{dat, it->first}; } @@ -1181,7 +1181,7 @@ bool bro_broker::TableIterator::DoUnserialize(const broker::data& data) IMPLEMENT_OPAQUE_VALUE(bro_broker::VectorIterator) -broker::data bro_broker::VectorIterator::DoSerialize() const +broker::expected bro_broker::VectorIterator::DoSerialize() const { broker::integer difference = it - dat.begin(); return broker::vector{dat, difference}; @@ -1203,7 +1203,7 @@ bool bro_broker::VectorIterator::DoUnserialize(const broker::data& data) IMPLEMENT_OPAQUE_VALUE(bro_broker::RecordIterator) -broker::data bro_broker::RecordIterator::DoSerialize() const +broker::expected bro_broker::RecordIterator::DoSerialize() const { broker::integer difference = it - dat.begin(); return broker::vector{dat, difference}; diff --git a/src/broker/Store.cc b/src/broker/Store.cc index 7462485bc3..2f61b14d37 100644 --- a/src/broker/Store.cc +++ b/src/broker/Store.cc @@ -51,10 +51,10 @@ void StoreHandleVal::ValDescribe(ODesc* d) const IMPLEMENT_OPAQUE_VALUE(StoreHandleVal) -broker::data StoreHandleVal::DoSerialize() const +broker::expected StoreHandleVal::DoSerialize() const { // Cannot serialize. - return broker::none(); + return broker::ec::invalid_data; } bool StoreHandleVal::DoUnserialize(const broker::data& data) diff --git a/src/file_analysis/analyzer/x509/X509.cc b/src/file_analysis/analyzer/x509/X509.cc index a46894dd9c..b2bd1a25e8 100644 --- a/src/file_analysis/analyzer/x509/X509.cc +++ b/src/file_analysis/analyzer/x509/X509.cc @@ -491,13 +491,13 @@ Val* X509Val::DoClone(CloneState* state) IMPLEMENT_OPAQUE_VALUE(X509Val) -broker::data X509Val::DoSerialize() const +broker::expected X509Val::DoSerialize() const { - unsigned char *buf = NULL; - int length = i2d_X509(certificate, &buf); + unsigned char *buf = NULL; + int length = i2d_X509(certificate, &buf); - if ( length < 0 ) - return broker::none(); + if ( length < 0 ) + return broker::ec::invalid_data; auto d = std::string(reinterpret_cast(buf), length); OPENSSL_free(buf); diff --git a/src/probabilistic/Topk.cc b/src/probabilistic/Topk.cc index 62ff96fe8a..3e08bed6d9 100644 --- a/src/probabilistic/Topk.cc +++ b/src/probabilistic/Topk.cc @@ -408,7 +408,7 @@ void TopkVal::IncrementCounter(Element* e, unsigned int count) IMPLEMENT_OPAQUE_VALUE(TopkVal) -broker::data TopkVal::DoSerialize() const +broker::expected TopkVal::DoSerialize() const { broker::vector d = {size, numElements, pruned}; @@ -416,7 +416,7 @@ broker::data TopkVal::DoSerialize() const { auto t = SerializeType(type); if ( t == broker::none() ) - return broker::none(); + return broker::ec::invalid_data; d.emplace_back(t); } @@ -440,7 +440,7 @@ broker::data TopkVal::DoSerialize() const d.emplace_back(element->epsilon); auto v = bro_broker::val_to_data(element->value); if ( ! v ) - return broker::none(); + return broker::ec::invalid_data; d.emplace_back(*v);