Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,8 @@ jsoncpp_lib_static.dir/
/src/lib_json/Makefile
/src/test_lib_json/Makefile
/src/test_lib_json/jsoncpp_test

# eclipse project files
.project
.cproject
/.settings/
94 changes: 94 additions & 0 deletions include/json/allocator.h
Original file line number Diff line number Diff line change
@@ -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 <cstring>
#include <memory>

namespace Json {
template<typename T>
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<pointer>(::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<typename... Args>
void construct(pointer p, Args&&... args) {
// construct using "placement new" and "perfect forwarding"
::new (static_cast<void*>(p)) T(std::forward<Args>(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<typename U> SecureAllocator(const SecureAllocator<U>&) {}
template<typename U> struct rebind { using other = SecureAllocator<U>; };
};


template<typename T, typename U>
bool operator==(const SecureAllocator<T>&, const SecureAllocator<U>&) {
return true;
}

template<typename T, typename U>
bool operator!=(const SecureAllocator<T>&, const SecureAllocator<U>&) {
return false;
}

} //namespace Json

#endif // CPPTL_JSON_ALLOCATOR_H_INCLUDED
19 changes: 19 additions & 0 deletions include/json/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef JSON_CONFIG_H_INCLUDED
#define JSON_CONFIG_H_INCLUDED
#include <stddef.h>
#include "allocator.h" //typedef Allocator
#include <string> //typdef String

/// If defined, indicates that json library is embedded in CppTL library.
Expand Down Expand Up @@ -119,6 +120,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;
Expand All @@ -139,11 +150,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<char, std::char_traits<char>, Json::SecureAllocator<char> >
#define JSONCPP_OSTRINGSTREAM std::basic_ostringstream<char, std::char_traits<char>, Json::SecureAllocator<char> >
#define JSONCPP_OSTREAM std::basic_ostream<char, std::char_traits<char>>
#define JSONCPP_ISTRINGSTREAM std::basic_istringstream<char, std::char_traits<char>, Json::SecureAllocator<char> >
#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
30 changes: 15 additions & 15 deletions include/json/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class JSON_API Reader {
struct StructuredError {
ptrdiff_t offset_start;
ptrdiff_t offset_limit;
std::string message;
JSONCPP_STRING message;
};

/** \brief Constructs a Reader allowing all features
Expand Down Expand Up @@ -111,7 +111,7 @@ class JSON_API Reader {
* \deprecated Use getFormattedErrorMessages() instead (typo fix).
*/
JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.")
std::string getFormatedErrorMessages() const;
JSONCPP_STRING getFormatedErrorMessages() const;

/** \brief Returns a user friendly string that list errors in the parsed
* document.
Expand All @@ -121,7 +121,7 @@ class JSON_API Reader {
* occurred
* during parsing.
*/
std::string getFormattedErrorMessages() const;
JSONCPP_STRING getFormattedErrorMessages() const;

/** \brief Returns a vector of structured erros encounted while parsing.
* \return A (possibly empty) vector of StructuredError objects. Currently
Expand All @@ -138,7 +138,7 @@ class JSON_API Reader {
* \return \c true if the error was successfully added, \c false if the
* Value offset exceeds the document size.
*/
bool pushError(const Value& value, const std::string& message);
bool pushError(const Value& value, const JSONCPP_STRING& message);

/** \brief Add a semantic error message with extra context.
* \param value JSON Value location associated with the error
Expand All @@ -147,7 +147,7 @@ class JSON_API Reader {
* \return \c true if the error was successfully added, \c false if either
* Value offset exceeds the document size.
*/
bool pushError(const Value& value, const std::string& message, const Value& extra);
bool pushError(const Value& value, const JSONCPP_STRING& message, const Value& extra);

/** \brief Return whether there are any errors.
* \return \c true if there are no errors to report \c false if
Expand Down Expand Up @@ -183,7 +183,7 @@ class JSON_API Reader {
class ErrorInfo {
public:
Token token_;
std::string message_;
JSONCPP_STRING message_;
Location extra_;
};

Expand All @@ -203,7 +203,7 @@ class JSON_API Reader {
bool decodeNumber(Token& token);
bool decodeNumber(Token& token, Value& decoded);
bool decodeString(Token& token);
bool decodeString(Token& token, std::string& decoded);
bool decodeString(Token& token, JSONCPP_STRING& decoded);
bool decodeDouble(Token& token);
bool decodeDouble(Token& token, Value& decoded);
bool decodeUnicodeCodePoint(Token& token,
Expand All @@ -214,30 +214,30 @@ class JSON_API Reader {
Location& current,
Location end,
unsigned int& unicode);
bool addError(const std::string& message, Token& token, Location extra = 0);
bool addError(const JSONCPP_STRING& message, Token& token, Location extra = 0);
bool recoverFromError(TokenType skipUntilToken);
bool addErrorAndRecover(const std::string& message,
bool addErrorAndRecover(const JSONCPP_STRING& message,
Token& token,
TokenType skipUntilToken);
void skipUntilSpace();
Value& currentValue();
Char getNextChar();
void
getLocationLineAndColumn(Location location, int& line, int& column) const;
std::string getLocationLineAndColumn(Location location) const;
JSONCPP_STRING getLocationLineAndColumn(Location location) const;
void addComment(Location begin, Location end, CommentPlacement placement);
void skipCommentTokens(Token& token);

typedef std::stack<Value*> Nodes;
Nodes nodes_;
Errors errors_;
std::string document_;
JSONCPP_STRING document_;
Location begin_;
Location end_;
Location current_;
Location lastValueEnd_;
Value* lastValue_;
std::string commentsBefore_;
JSONCPP_STRING commentsBefore_;
Features features_;
bool collectComments_;
}; // Reader
Expand Down Expand Up @@ -266,7 +266,7 @@ class JSON_API CharReader {
*/
virtual bool parse(
char const* beginDoc, char const* endDoc,
Value* root, std::string* errs) = 0;
Value* root, JSONCPP_STRING* errs) = 0;

class JSON_API Factory {
public:
Expand All @@ -286,7 +286,7 @@ class JSON_API CharReader {
CharReaderBuilder builder;
builder["collectComments"] = false;
Value value;
std::string errs;
JSONCPP_STRING errs;
bool ok = parseFromStream(builder, std::cin, &value, &errs);
\endcode
*/
Expand Down Expand Up @@ -344,7 +344,7 @@ class JSON_API CharReaderBuilder : public CharReader::Factory {

/** A simple way to update a specific setting.
*/
Value& operator[](std::string key);
Value& operator[](JSONCPP_STRING key);

/** Called by ctor, but you can use this to reset settings_.
* \pre 'settings' != NULL (but Json::null is fine)
Expand Down
3 changes: 3 additions & 0 deletions include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ Json::Value obj_value(Json::objectValue); // {}
int compare(const Value& other) const;

const char* asCString() const; ///< Embedded zeroes could cause you trouble!
#if JSON_USE_SECURE_MEMORY
unsigned getCStringLength() const; //Allows you to understand the length of the CString
#endif
JSONCPP_STRING asString() const; ///< Embedded zeroes are possible.
/** Get raw char* of string-value.
* \return false if !string. (Seg-fault if str or end are NULL.)
Expand Down
Loading