New serialized object, public key, and private key interfaces

This introduces functions get and set, and a family of specialized
structs called STExchange. These interfaces allow efficient and
seamless interchange between serialized object fields and user
defined types, especially variable length objects.

A new base class template TypedField is mixed into existing SField
declarations to encode information on the field, allowing template
metaprograms to both customize interchange based on the type and
detect misuse at compile-time.

New types AnyPublicKey and AnySecretKey are introduced. These are
intended to replace the corresponding functionality in the deprecated
class RippleAddress. Specializations of STExchange for these types
are provided to allow interchange. New free functions verify and sign
allow signature verification and signature generation for serialized
objects.

* Add Buffer and Slice primitives
* Add TypedField and modify some SField
* Add STExchange and specializations for STBlob and STInteger
* Improve STBlob and STInteger to support STExchange
* Expose raw data in RippleAddress and Serializer
This commit is contained in:
Vinnie Falco
2015-02-19 18:00:05 -08:00
committed by Tom Ritchford
parent 79ce4ed226
commit a2acffdfa3
20 changed files with 1277 additions and 65 deletions

149
src/ripple/basics/Buffer.h Normal file
View File

@@ -0,0 +1,149 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef RIPPLE_BASICS_BUFFER_H_INCLUDED
#define RIPPLE_BASICS_BUFFER_H_INCLUDED
#include <beast/utility/noexcept.h>
#include <cstdint>
#include <cstring>
#include <memory>
#include <utility>
namespace ripple {
/** Like std::vector<char> but better.
Meets the requirements of BufferFactory.
*/
class Buffer
{
private:
std::unique_ptr<
std::uint8_t[]> p_;
std::size_t size_ = 0;
public:
Buffer() = default;
Buffer (Buffer const&) = delete;
Buffer& operator= (Buffer const&) = delete;
/** Move-construct.
The other buffer is reset.
*/
Buffer (Buffer&& other)
: p_ (std::move(other.p_))
, size_ (other.size_)
{
other.size_ = 0;
}
/** Move-assign.
The other buffer is reset.
*/
Buffer& operator= (Buffer&& other)
{
p_ = std::move(other.p_);
size_ = other.size_;
other.size_ = 0;
return *this;
}
/** Create an uninitialized buffer with the given size. */
explicit
Buffer (std::size_t size)
: p_ (size ?
new std::uint8_t[size] : nullptr)
, size_ (size)
{
}
/** Create a buffer as a copy of existing memory. */
Buffer (void const* data, std::size_t size)
: p_ (size ?
new std::uint8_t[size] : nullptr)
, size_ (size)
{
std::memcpy(p_.get(), data, size);
}
/** Returns the number of bytes in the buffer. */
std::size_t
size() const noexcept
{
return size_;
}
/** Return a pointer to beginning of the storage.
@note The return type is guaranteed to be a pointer
to a single byte, to facilitate pointer arithmetic.
*/
/** @{ */
std::uint8_t const*
data() const noexcept
{
return p_.get();
}
std::uint8_t*
data() noexcept
{
return p_.get();
}
/** @} */
/** Reset the buffer.
All memory is deallocated. The resulting size is 0.
*/
void
clear() noexcept
{
p_.reset();
size_ = 0;
}
/** Reallocate the storage.
Existing data, if any, is discarded.
*/
std::uint8_t*
alloc (std::size_t n)
{
if (n == 0)
{
clear();
return nullptr;
}
if (n != size_)
{
p_.reset(new std::uint8_t[n]);
size_ = n;
}
return p_.get();
}
// Meet the requirements of BufferFactory
void*
operator()(std::size_t n)
{
return alloc(n);
}
};
} // ripple
#endif

110
src/ripple/basics/Slice.h Normal file
View File

@@ -0,0 +1,110 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef RIPPLE_BASICS_SLICE_H_INCLUDED
#define RIPPLE_BASICS_SLICE_H_INCLUDED
#include <beast/utility/noexcept.h>
#include <algorithm>
#include <cassert>
#include <cstdint>
namespace ripple {
/** An immutable linear range of bytes.
A fully constructed Slice is guaranteed to be in a valid state.
Default construction, construction from nullptr, and zero-byte
ranges are disallowed. A Slice is lightweight and copyable, it
retains no ownership of the underlying memory.
*/
class Slice
{
private:
std::uint8_t const* data_;
std::size_t size_;
public:
// Disallowed
Slice() = delete;
Slice (Slice const&) = default;
Slice& operator= (Slice const&) = default;
/** Create a slice pointing to existing memory. */
Slice (void const* data, std::size_t size)
: data_ (reinterpret_cast<
std::uint8_t const*>(data))
, size_ (size)
{
assert(data_ != nullptr);
assert(size_ > 0);
}
/** Returns the number of bytes in the storage.
This will never be zero.
*/
std::size_t
size() const noexcept
{
return size_;
}
/** Return a pointer to beginning of the storage.
@note The return type is guaranteed to be a pointer
to a single byte, to facilitate pointer arithmetic.
*/
std::uint8_t const*
data() const noexcept
{
return data_;
}
};
template <class Hasher>
inline
void
hash_append (Hasher& h, Slice const& v)
{
h.append(v.data(), v.size());
}
inline
bool
operator== (Slice const& lhs, Slice const& rhs) noexcept
{
return lhs.size() == rhs.size() &&
std::memcmp(
lhs.data(), rhs.data(), lhs.size()) == 0;
}
inline
bool
operator< (Slice const& lhs, Slice const& rhs) noexcept
{
return std::lexicographical_compare(
lhs.data(), lhs.data() + lhs.size(),
rhs.data(), rhs.data() + rhs.size());
}
} // ripple
#endif