Skip to content
Open
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
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,22 @@
*.xcodeproj/
*.dSYM
test
/.vs
/.vscode
/*.sln
/*.vcxproj
/*.vcxproj.filters
/*.vcxproj.user
*~
/build
/bin
/Debug
/Release
/x64
/libcborcpp.a
/libcborcpp_dynamic.so
/testing
/CMakeCache.txt
/CMakeFiles/
/Makefile
/cmake_install.cmake
27 changes: 18 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@ project(cborcpp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_BUILD_TYPE Release)

file(GLOB HEADERS "include/cborcpp/*.h")

set(SOURCE_FILES
src/encoder.cpp
src/decoder.cpp
src/input.cpp
src/listener_debug.cpp
src/output_dynamic.cpp
src/output_static.cpp
)
src/cborcpp/encoder.cpp
src/cborcpp/decoder.cpp
src/cborcpp/input.cpp
src/cborcpp/output_dynamic.cpp
src/cborcpp/output_static.cpp
src/cborcpp/cbor_object.cpp
src/cborcpp/output.cpp
)

set(TEST_SOURCE_FILES
src/tests.cpp
)
include_directories(include/)

add_library(cborcpp_dynamic SHARED ${SOURCE_FILES} ${HEADERS})
add_library(cborcpp STATIC ${SOURCE_FILES} ${HEADERS})
add_executable(testing ${SOURCE_FILES} ${HEADERS} ${TEST_SOURCE_FILES})

add_library(cborcpp SHARED ${SOURCE_FILES})
add_executable(testing ${SOURCE_FILES} ${TEST_SOURCE_FILES})
target_include_directories( cborcpp
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include"
)
57 changes: 51 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,72 @@ Just a simple SAX-like Concise Binary Object Representation (CBOR).

[http://tools.ietf.org/html/rfc7049](http://tools.ietf.org/html/rfc7049)

#### Dependencies

* Boost 1.55+

#### Examples

```C++
cbor::output_dynamic output;

{ //encoding
cbor::encoder encoder(output);
encoder.write_array(5);
// [123, "bar", 321, 321, "foo", true, false, null, undefined, [123], [], {"hello": "world", "age": 18}, b"abcde"]
encoder.write_array(13);
{
encoder.write_int(123);
encoder.write_string("bar");
encoder.write_int(321);
encoder.write_int(321);
encoder.write_string("foo");
encoder.write_bool(true);
encoder.write_bool(false);
encoder.write_null();
encoder.write_undefined();

encoder.write_array(1);
{
encoder.write_int(123);
}
encoder.write_array(0);
encoder.write_map(2);
{
encoder.write_string("hello");
encoder.write_string("world");
encoder.write_string("age");
encoder.write_int(18);
}
encoder.write_bytes((const unsigned char*)"abcde", 5);
}
}

{ // decoding
cbor::input input(output.data(), output.size());
cbor::listener_debug listener;
cbor::decoder decoder(input, listener);
decoder.run();
cbor::decoder decoder(input);
auto result = decoder.run();
assert(result->type == COT_ARRAY & result->array_or_map_size == 13);
const auto& array_value = result->as<CborArrayValue>();
auto obj1 = array_value[0]->as_int();
auto obj2 = array_value[1]->as_string();
auto obj3 = array_value[2]->as_int();
auto obj4 = array_value[3]->as_int();
auto obj5 = array_value[4]->as_string();
auto obj6 = array_value[5]->as_bool();
auto obj7 = array_value[6]->as_bool();
auto obj8 = array_value[7]->is_null();
auto obj9 = array_value[8]->is_undefined();
auto obj10 = array_value[9]->as_array();
auto obj11 = array_value[10]->as_array();
auto obj12 = array_value[11]->as_map();
auto obj13 = array_value[12]->as_bytes();
assert(obj1 == 123);
assert(obj2 == "bar");
assert(obj3 == 321 && obj4 == 321);
assert(obj5 == "foo");
assert(obj6 == true && obj7 == false);
assert(obj8 && obj9);
assert(obj10.size() == 1 && obj10[0]->as_int() == 123);
assert(obj11.empty());
assert(obj12.size() == 2 && obj12["hello"]->as_string() == "world" && obj12["age"]->as_int() == 18);
assert(obj13.size() == 5 && memcmp(obj13.data(), "abcde", 5) == 0);
}
```
18 changes: 8 additions & 10 deletions src/cbor.h → include/cborcpp/cbor.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@
limitations under the License.
*/

#ifndef CBOR_CPP_CBOR_H
#define CBOR_CPP_CBOR_H
#pragma once

#include "input.h"
#include "encoder.h"
#include "decoder.h"
#include "listener.h"
#include "output_static.h"
#include "output_dynamic.h"
#include "listener_debug.h"
#include "cborcpp/input.h"
#include "cborcpp/encoder.h"
#include "cborcpp/decoder.h"
#include "cborcpp/output_static.h"
#include "cborcpp/output_dynamic.h"
#include "cborcpp/exceptions.h"
#include "cborcpp/cbor_object.h"

#endif //CBOR_CPP_CBOR_H
126 changes: 126 additions & 0 deletions include/cborcpp/cbor_object.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#pragma once

#include <boost/variant.hpp>
#include <vector>
#include <map>
#include <memory>
#include <algorithm>
#include "cborcpp/exceptions.h"

namespace cbor {
enum CborObjectType {
COT_BOOL,
COT_INT,
COT_BYTES,
COT_STRING,
COT_ARRAY,
COT_MAP,
COT_TAG,
COT_SPECIAL,
COT_UNDEFINED,
COT_NULL,
COT_ERROR,
COT_EXTRA_INT,
COT_EXTRA_TAG,
COT_EXTRA_SPECIAL
};
struct CborObject;
typedef std::shared_ptr<CborObject> CborObjectP;
typedef bool CborBoolValue;
typedef int64_t CborIntValue;
typedef uint32_t CborTagValue;
typedef std::vector<char> CborBytesValue;
typedef std::vector<CborObjectP> CborArrayValue;
typedef std::map<std::string, CborObjectP, std::less<std::string>> CborMapValue;
typedef std::string CborStringValue;
typedef std::string CborErrorValue;
typedef uint64_t CborExtraIntValue;
typedef uint32_t CborSpecialValue;
typedef uint64_t CborExtraSpecialValue;
typedef boost::variant<CborBoolValue, CborIntValue, CborExtraIntValue, CborTagValue, CborStringValue, CborBytesValue, CborArrayValue, CborMapValue> CborObjectValue;

struct CborObject {
CborObjectType type;
CborObjectValue value;
uint32_t array_or_map_size = 0;
bool is_positive_extra = false;

template <typename T>
const T& as() const {
return boost::get<T>(value);
}

inline bool is_null() const {
return COT_NULL == type;
}
inline bool is_undefined() const {
return COT_UNDEFINED == type;
}
inline bool is_int() const {
return COT_INT == type;
}
inline bool is_string() const {
return COT_STRING == type;
}
inline bool is_bytes() const {
return COT_BYTES == type;
}
inline bool is_bool() const {
return COT_BOOL == type;
}
inline bool is_array() const {
return COT_ARRAY == type;
}
inline bool is_map() const {
return COT_MAP == type;
}
inline bool is_tag() const {
return COT_TAG == type;
}
inline bool is_special() const {
return COT_SPECIAL == type;
}
inline CborObjectType object_type() const {
return type;
}
inline const CborBoolValue& as_bool() const {
return as<CborBoolValue>();
}
inline const CborIntValue& as_int() const {
return as<CborIntValue>();
}
inline const CborTagValue& as_tag() const {
return as<CborTagValue>();
}
inline const CborSpecialValue& as_special() const {
return as<CborSpecialValue>();
}
inline const CborBytesValue& as_bytes() const {
return as<CborBytesValue>();
}
inline const CborArrayValue& as_array() const {
return as<CborArrayValue>();
}
inline const CborMapValue& as_map() const {
return as<CborMapValue>();
}
inline const CborStringValue& as_string() const {
return as<CborStringValue>();
}


static CborObjectP from_int(CborIntValue value);
static CborObjectP from_bool(CborBoolValue value);
static CborObjectP from_bytes(const CborBytesValue& value);
static CborObjectP from_string(const std::string& value);
static CborObjectP create_array(size_t size);
static CborObjectP create_map(size_t size);
static CborObjectP from_tag(CborTagValue value);
static CborObjectP create_undefined();
static CborObjectP create_null();
static CborObjectP from_special(uint32_t value);
static CborObjectP from_extra_integer(uint64_t value, bool sign);
static CborObjectP from_extra_tag(uint64_t value);
static CborObjectP from_extra_special(uint64_t value);
};
}
17 changes: 6 additions & 11 deletions src/decoder.h → include/cborcpp/decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
*/


#ifndef __CborDecoder_H_
#define __CborDecoder_H_
#pragma once

#include "listener.h"
#include "input.h"
#include "cborcpp/input.h"
#include "cborcpp/cbor_object.h"

namespace cbor {
typedef enum {
Expand All @@ -39,18 +38,14 @@ namespace cbor {

class decoder {
private:
listener *_listener;
// listener *_listener;
input *_in;
decoder_state _state;
int _currentLength;
public:
decoder(input &in);
decoder(input &in, listener &listener);
~decoder();
void run();
void set_listener(listener &listener_instance);
CborObjectP run();
//void set_listener(listener &listener_instance);
};
}


#endif //__CborDecoder_H_
24 changes: 12 additions & 12 deletions src/encoder.h → include/cborcpp/encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
limitations under the License.
*/

#pragma once

#ifndef __CborEncoder_H_
#define __CborEncoder_H_

#include "output.h"
#include "cborcpp/output.h"
#include "cborcpp/cbor_object.h"
#include <string>
#include <cstdint>

namespace cbor {
class encoder {
Expand All @@ -32,13 +32,13 @@ namespace cbor {

void write_bool(bool value);

void write_int(int value);
void write_int(int32_t value);

void write_int(long long value);
void write_int(int64_t value);

void write_int(unsigned int value);
void write_int(uint32_t value);

void write_int(unsigned long long value);
void write_int(uint64_t value);

void write_bytes(const unsigned char *data, unsigned int size);

Expand All @@ -58,11 +58,11 @@ namespace cbor {

void write_undefined();

void write_cbor_object(CborObjectP value);

private:
void write_type_value(int major_type, unsigned int value);
void write_type_value(int major_type, uint32_t value);

void write_type_value(int major_type, unsigned long long value);
void write_type_value(int major_type, uint64_t value);
};
}

#endif //__CborEncoder_H_
Loading