From f8674c63b1d2e4faf4160b2a51e46cbe3a24dbfa Mon Sep 17 00:00:00 2001 From: dawesc Date: Sun, 6 Mar 2016 11:42:11 -0600 Subject: [PATCH] allocator.h --- include/json/allocator.h | 94 ++++++++++++++++++++++++++++++++++++++++ include/json/config.h | 18 ++++++++ 2 files changed, 112 insertions(+) create mode 100644 include/json/allocator.h diff --git a/include/json/allocator.h b/include/json/allocator.h new file mode 100644 index 0000000..9d8b9fc --- /dev/null +++ b/include/json/allocator.h @@ -0,0 +1,94 @@ +// Copyright 2007-2010 Baptiste Lepilleur +// Distributed under MIT license, or public domain if desired and +// recognized in your jurisdiction. +// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE + +#ifndef CPPTL_JSON_ALLOCATOR_H_INCLUDED +#define CPPTL_JSON_ALLOCATOR_H_INCLUDED + +#include +#include + +namespace Json { +template +class SecureAllocator { + public: + // Type definitions + using value_type = T; + using pointer = T*; + using const_pointer = const T*; + using reference = T&; + using const_reference = const T&; + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; + + /** + * Allocate memory for N items using the standard allocator. + */ + pointer allocate(size_type n) { + // allocate using "global operator new" + return static_cast(::operator new(n * sizeof(T))); + } + + /** + * Release memory which was allocated for N items at pointer P. + * + * The memory block is filled with zeroes before being released. + * The pointer argument is tagged as "volatile" to prevent the + * compiler optimizing out this critical step. + */ + void deallocate(volatile pointer p, size_type n) { + std::memset(p, 0, n * sizeof(T)); + // free using "global operator delete" + ::operator delete(p); + } + + /** + * Construct an item in-place at pointer P. + */ + template + void construct(pointer p, Args&&... args) { + // construct using "placement new" and "perfect forwarding" + ::new (static_cast(p)) T(std::forward(args)...); + } + + size_type max_size() const { + return size_t(-1) / sizeof(T); + } + + pointer address( reference x ) const { + return std::addressof(x); + } + + const_pointer address( const_reference x ) const { + return std::addressof(x); + } + + /** + * Destroy an item in-place at pointer P. + */ + void destroy(pointer p) { + // destroy using "explicit destructor" + p->~T(); + } + + // Boilerplate + SecureAllocator() {} + template SecureAllocator(const SecureAllocator&) {} + template struct rebind { using other = SecureAllocator; }; +}; + + +template +bool operator==(const SecureAllocator&, const SecureAllocator&) { + return true; +} + +template +bool operator!=(const SecureAllocator&, const SecureAllocator&) { + return false; +} + +} //namespace Json + +#endif // CPPTL_JSON_ALLOCATOR_H_INCLUDED diff --git a/include/json/config.h b/include/json/config.h index 8254727..213d9cb 100644 --- a/include/json/config.h +++ b/include/json/config.h @@ -119,6 +119,16 @@ # define JSON_USE_INT64_DOUBLE_CONVERSION 1 #endif +// If non-zero, the library zeroes any memory that it has allocated before +// it frees its +#ifndef JSON_USE_SECURE_MEMORY +#define JSON_USE_SECURE_MEMORY 0 +#endif + +#if JSON_USE_SECURE_MEMORY +#include "allocator.h" //typedef Allocator +#endif + namespace Json { typedef int Int; typedef unsigned int UInt; @@ -139,11 +149,19 @@ typedef Int64 LargestInt; typedef UInt64 LargestUInt; #define JSON_HAS_INT64 #endif // if defined(JSON_NO_INT64) +#if JSON_USE_SECURE_MEMORY +#define JSONCPP_STRING std::basic_string, SecureAllocator > +#define JSONCPP_OSTRINGSTREAM std::basic_ostringstream, SecureAllocator > +#define JSONCPP_OSTREAM std::basic_ostream> +#define JSONCPP_ISTRINGSTREAM std::basic_istringstream, SecureAllocator > +#define JSONCPP_ISTREAM std::istream +#else #define JSONCPP_STRING std::string #define JSONCPP_OSTRINGSTREAM std::ostringstream #define JSONCPP_OSTREAM std::ostream #define JSONCPP_ISTRINGSTREAM std::istringstream #define JSONCPP_ISTREAM std::istream +#endif // if JSON_USE_SECURE_MEMORY } // end namespace Json #endif // JSON_CONFIG_H_INCLUDED