From ffcaaaac1f10166c388143d209bd1523aeae6169 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Tue, 20 Apr 2021 22:01:40 +0200 Subject: [PATCH 01/47] Move LICENSE to root --- src/LICENSE => LICENSE | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/LICENSE => LICENSE (100%) diff --git a/src/LICENSE b/LICENSE similarity index 100% rename from src/LICENSE rename to LICENSE From 96eccd07664f16d252657d7a78cc515e30948ae2 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Tue, 20 Apr 2021 22:07:06 +0200 Subject: [PATCH 02/47] Increased version to 3.15 --- CMakeLists.txt | 2 +- {src => test}/tests.cpp | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename {src => test}/tests.cpp (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index d50f799..7a81442 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 2.8) +cmake_minimum_required(VERSION 3.15) project(cborcpp) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") diff --git a/src/tests.cpp b/test/tests.cpp similarity index 100% rename from src/tests.cpp rename to test/tests.cpp From 541b5131dcb6c3b06c73dff2ffeede33b5ae092f Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Tue, 20 Apr 2021 22:08:22 +0200 Subject: [PATCH 03/47] Move test to separate directory --- CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a81442..1928589 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,8 +14,9 @@ set(SOURCE_FILES ) set(TEST_SOURCE_FILES - src/tests.cpp + test/tests.cpp ) add_library(cborcpp SHARED ${SOURCE_FILES}) add_executable(testing ${SOURCE_FILES} ${TEST_SOURCE_FILES}) +target_include_directories(testing PRIVATE src) \ No newline at end of file From 5584d033168763592221926c1ff9b03be2107a2c Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Tue, 20 Apr 2021 22:36:00 +0200 Subject: [PATCH 04/47] Make all functions const noexcept --- src/listener.h | 38 ++++++++++++++++++++------------------ src/listener_debug.cpp | 28 ++++++++++++++-------------- src/listener_debug.h | 30 +++++++++++++++--------------- 3 files changed, 49 insertions(+), 47 deletions(-) diff --git a/src/listener.h b/src/listener.h index b25e0ca..4923e59 100644 --- a/src/listener.h +++ b/src/listener.h @@ -20,43 +20,45 @@ #include namespace cbor { - class listener { - public: - virtual void on_integer(int value) = 0; + struct listener { + + virtual void on_integer(int value) const noexcept {} - virtual void on_float32(float value) = 0; - virtual void on_double(double value) = 0; + virtual void on_float32(float value) const noexcept {} + virtual void on_double(double value) const noexcept {} - virtual void on_bytes(unsigned char *data, int size) = 0; + virtual void on_bytes(unsigned char *data, int size) const noexcept {} - virtual void on_string(std::string &str) = 0; + virtual void on_string(std::string &str) const noexcept {} - virtual void on_array(int size) = 0; + virtual void on_array(int size) const noexcept {} - virtual void on_map(int size) = 0; + virtual void on_map(int size) const noexcept {} - virtual void on_tag(unsigned int tag) = 0; + virtual void on_tag(unsigned int tag) const noexcept {} - virtual void on_special(unsigned int code) = 0; + virtual void on_special(unsigned int code) const noexcept {} - virtual void on_bool(bool) = 0; + virtual void on_bool(bool) const noexcept {} - virtual void on_null() = 0; + virtual void on_null() const noexcept {} - virtual void on_undefined() = 0; + virtual void on_undefined() const noexcept {} - virtual void on_error(const char *error) = 0; + virtual void on_error(const char *error) const noexcept {} - virtual void on_extra_integer(unsigned long long value, int sign) { + virtual void on_extra_integer(unsigned long long value, int sign) const noexcept { } - virtual void on_extra_tag(unsigned long long tag) { + virtual void on_extra_tag(unsigned long long tag) const noexcept { } - virtual void on_extra_special(unsigned long long tag) { + virtual void on_extra_special(unsigned long long tag) const noexcept { } }; } + + #endif // CBOR_CPP_LISTENER_H diff --git a/src/listener_debug.cpp b/src/listener_debug.cpp index 5b79b74..e74c63c 100644 --- a/src/listener_debug.cpp +++ b/src/listener_debug.cpp @@ -18,51 +18,51 @@ using namespace cbor; -void listener_debug::on_integer(int value) { +void listener_debug::on_integer(int value) const noexcept { printf("integer: %d\n", value); } -void listener_debug::on_bytes(unsigned char *data, int size) { +void listener_debug::on_bytes(unsigned char *data, int size) const noexcept { printf("bytes with size: %d", size); } -void listener_debug::on_string(std::string &str) { +void listener_debug::on_string(std::string &str) const noexcept { printf("string: '%.*s'\n", (int)str.size(), str.c_str()); } -void listener_debug::on_array(int size) { +void listener_debug::on_array(int size) const noexcept { printf("array: %d\n", size); } -void listener_debug::on_map(int size) { +void listener_debug::on_map(int size) const noexcept { printf("map: %d\n", size); } -void listener_debug::on_tag(unsigned int tag) { +void listener_debug::on_tag(unsigned int tag) const noexcept { printf("tag: %d\n", tag); } -void listener_debug::on_special(unsigned int code) { +void listener_debug::on_special(unsigned int code) const noexcept { printf("special: %d\n", code); } -void listener_debug::on_bool(bool value) { +void listener_debug::on_bool(bool value) const noexcept { printf("bool: %s\n", value ? "true" : "false"); } -void listener_debug::on_null() { +void listener_debug::on_null() const noexcept { printf("special: null\n"); } -void listener_debug::on_undefined() { +void listener_debug::on_undefined() const noexcept { printf("special: undefined\n"); } -void listener_debug::on_error(const char *error) { +void listener_debug::on_error(const char *error) const noexcept { printf("error: %s\n", error); } -void listener_debug::on_extra_integer(unsigned long long value, int sign) { +void listener_debug::on_extra_integer(unsigned long long value, int sign) const noexcept { if(sign >= 0) { printf("extra integer: %llu\n", value); } else { @@ -70,10 +70,10 @@ void listener_debug::on_extra_integer(unsigned long long value, int sign) { } } -void listener_debug::on_extra_tag(unsigned long long tag) { +void listener_debug::on_extra_tag(unsigned long long tag) const noexcept { printf("extra tag: %llu\n", tag); } -void listener_debug::on_extra_special(unsigned long long tag) { +void listener_debug::on_extra_special(unsigned long long tag) const noexcept { printf("extra special: %llu\n", tag); } diff --git a/src/listener_debug.h b/src/listener_debug.h index b7d2b17..3076625 100644 --- a/src/listener_debug.h +++ b/src/listener_debug.h @@ -1,7 +1,7 @@ /* Copyright 2014-2015 Stanislav Ovsyannikov - Licensed under the Apache License, Version 2.0 (the "License"); + Licensed under the Apache License, Version 2.0 (the "License") const noexcept override; you may not use this file except in compliance with the License. You may obtain a copy of the License at @@ -24,33 +24,33 @@ namespace cbor { class listener_debug : public listener { public: - virtual void on_integer(int value); + virtual void on_integer(int value) const noexcept override; - virtual void on_bytes(unsigned char *data, int size); + virtual void on_bytes(unsigned char *data, int size) const noexcept override; - virtual void on_string(std::string &str); + virtual void on_string(std::string &str) const noexcept override; - virtual void on_array(int size); + virtual void on_array(int size) const noexcept override; - virtual void on_map(int size); + virtual void on_map(int size) const noexcept override; - virtual void on_tag(unsigned int tag); + virtual void on_tag(unsigned int tag) const noexcept override; - virtual void on_special(unsigned int code); + virtual void on_special(unsigned int code) const noexcept override; - virtual void on_bool(bool); + virtual void on_bool(bool) const noexcept override; - virtual void on_null(); + virtual void on_null() const noexcept override; - virtual void on_undefined(); + virtual void on_undefined() const noexcept override; - virtual void on_error(const char *error); + virtual void on_error(const char *error) const noexcept override; - virtual void on_extra_integer(unsigned long long value, int sign); + virtual void on_extra_integer(unsigned long long value, int sign) const noexcept override; - virtual void on_extra_tag(unsigned long long tag); + virtual void on_extra_tag(unsigned long long tag) const noexcept override; - virtual void on_extra_special(unsigned long long tag); + virtual void on_extra_special(unsigned long long tag) const noexcept override; }; } From 847be3a9161807fd6b1c02dc4f9b91244cdf5af2 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Tue, 20 Apr 2021 22:54:21 +0200 Subject: [PATCH 05/47] Introduce templated print --- src/listener.h | 7 +++++++ src/listener_debug.cpp | 20 ++++++++++---------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/listener.h b/src/listener.h index 4923e59..e895106 100644 --- a/src/listener.h +++ b/src/listener.h @@ -18,6 +18,7 @@ #define CBOR_CPP_LISTENER_H #include +#include namespace cbor { struct listener { @@ -55,6 +56,12 @@ namespace cbor { virtual void on_extra_special(unsigned long long tag) const noexcept { } + + protected: + template + void p(const std::string & tag, const T& value) const noexcept { + std::cout << "[" << tag << sizeof(T) << " = " << value << "]\n"; + } }; } diff --git a/src/listener_debug.cpp b/src/listener_debug.cpp index e74c63c..2d5893c 100644 --- a/src/listener_debug.cpp +++ b/src/listener_debug.cpp @@ -19,7 +19,7 @@ using namespace cbor; void listener_debug::on_integer(int value) const noexcept { - printf("integer: %d\n", value); + p("i", value); } void listener_debug::on_bytes(unsigned char *data, int size) const noexcept { @@ -27,7 +27,7 @@ void listener_debug::on_bytes(unsigned char *data, int size) const noexcept { } void listener_debug::on_string(std::string &str) const noexcept { - printf("string: '%.*s'\n", (int)str.size(), str.c_str()); + p("s", str); } void listener_debug::on_array(int size) const noexcept { @@ -39,27 +39,27 @@ void listener_debug::on_map(int size) const noexcept { } void listener_debug::on_tag(unsigned int tag) const noexcept { - printf("tag: %d\n", tag); + p("t", tag); } void listener_debug::on_special(unsigned int code) const noexcept { - printf("special: %d\n", code); + p("spc", code); } void listener_debug::on_bool(bool value) const noexcept { - printf("bool: %s\n", value ? "true" : "false"); + p("b", value); } void listener_debug::on_null() const noexcept { - printf("special: null\n"); + p("spc", "null"); } void listener_debug::on_undefined() const noexcept { - printf("special: undefined\n"); + p("spc", "undef"); } void listener_debug::on_error(const char *error) const noexcept { - printf("error: %s\n", error); + p("err", error); } void listener_debug::on_extra_integer(unsigned long long value, int sign) const noexcept { @@ -71,9 +71,9 @@ void listener_debug::on_extra_integer(unsigned long long value, int sign) const } void listener_debug::on_extra_tag(unsigned long long tag) const noexcept { - printf("extra tag: %llu\n", tag); + p("ext", tag); } void listener_debug::on_extra_special(unsigned long long tag) const noexcept { - printf("extra special: %llu\n", tag); + p("extspc", tag); } From 8950bced39245b873fd0516885ac36cf7a2bde1e Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Tue, 20 Apr 2021 23:03:56 +0200 Subject: [PATCH 06/47] Add print for complex types, special template for string --- src/listener.h | 5 +++++ src/listener_debug.cpp | 11 ++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/listener.h b/src/listener.h index e895106..f20d7a5 100644 --- a/src/listener.h +++ b/src/listener.h @@ -61,6 +61,11 @@ namespace cbor { template void p(const std::string & tag, const T& value) const noexcept { std::cout << "[" << tag << sizeof(T) << " = " << value << "]\n"; + } + + template<> + void p(const std::string & tag, const std::string& value) const noexcept { + std::cout << "[" << tag << value.length() << " = '" << value << "']\n"; } }; } diff --git a/src/listener_debug.cpp b/src/listener_debug.cpp index 2d5893c..a2f371d 100644 --- a/src/listener_debug.cpp +++ b/src/listener_debug.cpp @@ -23,7 +23,8 @@ void listener_debug::on_integer(int value) const noexcept { } void listener_debug::on_bytes(unsigned char *data, int size) const noexcept { - printf("bytes with size: %d", size); + p("b", data); + p("len", size); } void listener_debug::on_string(std::string &str) const noexcept { @@ -31,11 +32,11 @@ void listener_debug::on_string(std::string &str) const noexcept { } void listener_debug::on_array(int size) const noexcept { - printf("array: %d\n", size); + p("array n", size); } void listener_debug::on_map(int size) const noexcept { - printf("map: %d\n", size); + p("map", size); } void listener_debug::on_tag(unsigned int tag) const noexcept { @@ -64,9 +65,9 @@ void listener_debug::on_error(const char *error) const noexcept { void listener_debug::on_extra_integer(unsigned long long value, int sign) const noexcept { if(sign >= 0) { - printf("extra integer: %llu\n", value); + p("exint", value); } else { - printf("extra integer: -%llu\n", value); + p("exint", -value); } } From 3cbef23b4bdfd7940e9df502b636c4001eb8e7cd Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Tue, 20 Apr 2021 23:11:53 +0200 Subject: [PATCH 07/47] Convert enum decoder_state into enum class --- src/decoder.cpp | 178 ++++++++++++++++++++++++------------------------ src/decoder.h | 4 +- 2 files changed, 91 insertions(+), 91 deletions(-) diff --git a/src/decoder.cpp b/src/decoder.cpp index 529821d..0040382 100644 --- a/src/decoder.cpp +++ b/src/decoder.cpp @@ -23,13 +23,13 @@ using namespace cbor; decoder::decoder(input &in) { _in = ∈ - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; } decoder::decoder(input &in, listener &listener) { _in = ∈ _listener = &listener; - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; } decoder::~decoder() { @@ -43,7 +43,7 @@ void decoder::set_listener(listener &listener_instance) { void decoder::run() { unsigned int temp; while(1) { - if(_state == STATE_TYPE) { + if(_state == decoder_state::STATE_TYPE) { if(_in->has_bytes(1)) { unsigned char type = _in->get_byte(); unsigned char majorType = type >> 5; @@ -55,18 +55,18 @@ void decoder::run() { _listener->on_integer(minorType); } else if(minorType == 24) { // 1 byte _currentLength = 1; - _state = STATE_PINT; + _state = decoder_state::STATE_PINT; } else if(minorType == 25) { // 2 byte _currentLength = 2; - _state = STATE_PINT; + _state = decoder_state::STATE_PINT; } else if(minorType == 26) { // 4 byte _currentLength = 4; - _state = STATE_PINT; + _state = decoder_state::STATE_PINT; } else if(minorType == 27) { // 8 byte _currentLength = 8; - _state = STATE_PINT; + _state = decoder_state::STATE_PINT; } else { - _state = STATE_ERROR; + _state = decoder_state::STATE_ERROR; _listener->on_error("invalid integer type"); } break; @@ -75,60 +75,60 @@ void decoder::run() { _listener->on_integer(-1 -minorType); } else if(minorType == 24) { // 1 byte _currentLength = 1; - _state = STATE_NINT; + _state = decoder_state::STATE_NINT; } else if(minorType == 25) { // 2 byte _currentLength = 2; - _state = STATE_NINT; + _state = decoder_state::STATE_NINT; } else if(minorType == 26) { // 4 byte _currentLength = 4; - _state = STATE_NINT; + _state = decoder_state::STATE_NINT; } else if(minorType == 27) { // 8 byte _currentLength = 8; - _state = STATE_NINT; + _state = decoder_state::STATE_NINT; } else { - _state = STATE_ERROR; + _state = decoder_state::STATE_ERROR; _listener->on_error("invalid integer type"); } break; case 2: // bytes if(minorType < 24) { - _state = STATE_BYTES_DATA; + _state = decoder_state::STATE_BYTES_DATA; _currentLength = minorType; } else if(minorType == 24) { - _state = STATE_BYTES_SIZE; + _state = decoder_state::STATE_BYTES_SIZE; _currentLength = 1; } else if(minorType == 25) { // 2 byte _currentLength = 2; - _state = STATE_BYTES_SIZE; + _state = decoder_state::STATE_BYTES_SIZE; } else if(minorType == 26) { // 4 byte _currentLength = 4; - _state = STATE_BYTES_SIZE; + _state = decoder_state::STATE_BYTES_SIZE; } else if(minorType == 27) { // 8 byte _currentLength = 8; - _state = STATE_BYTES_SIZE; + _state = decoder_state::STATE_BYTES_SIZE; } else { - _state = STATE_ERROR; + _state = decoder_state::STATE_ERROR; _listener->on_error("invalid bytes type"); } break; case 3: // string if(minorType < 24) { - _state = STATE_STRING_DATA; + _state = decoder_state::STATE_STRING_DATA; _currentLength = minorType; } else if(minorType == 24) { - _state = STATE_STRING_SIZE; + _state = decoder_state::STATE_STRING_SIZE; _currentLength = 1; } else if(minorType == 25) { // 2 byte _currentLength = 2; - _state = STATE_STRING_SIZE; + _state = decoder_state::STATE_STRING_SIZE; } else if(minorType == 26) { // 4 byte _currentLength = 4; - _state = STATE_STRING_SIZE; + _state = decoder_state::STATE_STRING_SIZE; } else if(minorType == 27) { // 8 byte _currentLength = 8; - _state = STATE_STRING_SIZE; + _state = decoder_state::STATE_STRING_SIZE; } else { - _state = STATE_ERROR; + _state = decoder_state::STATE_ERROR; _listener->on_error("invalid string type"); } break; @@ -136,19 +136,19 @@ void decoder::run() { if(minorType < 24) { _listener->on_array(minorType); } else if(minorType == 24) { - _state = STATE_ARRAY; + _state = decoder_state::STATE_ARRAY; _currentLength = 1; } else if(minorType == 25) { // 2 byte _currentLength = 2; - _state = STATE_ARRAY; + _state = decoder_state::STATE_ARRAY; } else if(minorType == 26) { // 4 byte _currentLength = 4; - _state = STATE_ARRAY; + _state = decoder_state::STATE_ARRAY; } else if(minorType == 27) { // 8 byte _currentLength = 8; - _state = STATE_ARRAY; + _state = decoder_state::STATE_ARRAY; } else { - _state = STATE_ERROR; + _state = decoder_state::STATE_ERROR; _listener->on_error("invalid array type"); } break; @@ -156,19 +156,19 @@ void decoder::run() { if(minorType < 24) { _listener->on_map(minorType); } else if(minorType == 24) { - _state = STATE_MAP; + _state = decoder_state::STATE_MAP; _currentLength = 1; } else if(minorType == 25) { // 2 byte _currentLength = 2; - _state = STATE_MAP; + _state = decoder_state::STATE_MAP; } else if(minorType == 26) { // 4 byte _currentLength = 4; - _state = STATE_MAP; + _state = decoder_state::STATE_MAP; } else if(minorType == 27) { // 8 byte _currentLength = 8; - _state = STATE_MAP; + _state = decoder_state::STATE_MAP; } else { - _state = STATE_ERROR; + _state = decoder_state::STATE_ERROR; _listener->on_error("invalid array type"); } break; @@ -176,19 +176,19 @@ void decoder::run() { if(minorType < 24) { _listener->on_tag(minorType); } else if(minorType == 24) { - _state = STATE_TAG; + _state = decoder_state::STATE_TAG; _currentLength = 1; } else if(minorType == 25) { // 2 byte _currentLength = 2; - _state = STATE_TAG; + _state = decoder_state::STATE_TAG; } else if(minorType == 26) { // 4 byte _currentLength = 4; - _state = STATE_TAG; + _state = decoder_state::STATE_TAG; } else if(minorType == 27) { // 8 byte _currentLength = 8; - _state = STATE_TAG; + _state = decoder_state::STATE_TAG; } else { - _state = STATE_ERROR; + _state = decoder_state::STATE_ERROR; _listener->on_error("invalid tag type"); } break; @@ -204,34 +204,34 @@ void decoder::run() { } else if (minorType == 23) { _listener->on_undefined(); } else if(minorType == 24) { - _state = STATE_SPECIAL; + _state = decoder_state::STATE_SPECIAL; _currentLength = 1; } else if(minorType == 25) { // 2 byte _currentLength = 2; - _state = STATE_SPECIAL; + _state = decoder_state::STATE_SPECIAL; } else if(minorType == 26) { // 4 byte _currentLength = 4; - _state = STATE_SPECIAL; + _state = decoder_state::STATE_SPECIAL; } else if(minorType == 27) { // 8 byte _currentLength = 8; - _state = STATE_SPECIAL; + _state = decoder_state::STATE_SPECIAL; } else { - _state = STATE_ERROR; + _state = decoder_state::STATE_ERROR; _listener->on_error("invalid special type"); } break; } } else break; - } else if(_state == STATE_PINT) { + } else if(_state == decoder_state::STATE_PINT) { if(_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: _listener->on_integer(_in->get_byte()); - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; break; case 2: _listener->on_integer(_in->get_short()); - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; break; case 4: temp = _in->get_int(); @@ -240,24 +240,24 @@ void decoder::run() { } else { _listener->on_extra_integer(temp, 1); } - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; break; case 8: _listener->on_extra_integer(_in->get_long(), 1); - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; break; } } else break; - } else if(_state == STATE_NINT) { + } else if(_state == decoder_state::STATE_NINT) { if(_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: _listener->on_integer(-(int) _in->get_byte() - 1); - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; break; case 2: _listener->on_integer(-(int) _in->get_short() - 1); - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; break; case 4: temp = _in->get_int(); @@ -266,155 +266,155 @@ void decoder::run() { } else { _listener->on_extra_integer(temp + 1, -1); } - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; break; case 8: _listener->on_extra_integer(_in->get_long() + 1, -1); break; } } else break; - } else if(_state == STATE_BYTES_SIZE) { + } else if(_state == decoder_state::STATE_BYTES_SIZE) { if(_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: _currentLength = _in->get_byte(); - _state = STATE_BYTES_DATA; + _state = decoder_state::STATE_BYTES_DATA; break; case 2: _currentLength = _in->get_short(); - _state = STATE_BYTES_DATA; + _state = decoder_state::STATE_BYTES_DATA; break; case 4: _currentLength = _in->get_int(); - _state = STATE_BYTES_DATA; + _state = decoder_state::STATE_BYTES_DATA; break; case 8: - _state = STATE_ERROR; + _state = decoder_state::STATE_ERROR; _listener->on_error("extra long bytes"); break; } } else break; - } else if(_state == STATE_BYTES_DATA) { + } else if(_state == decoder_state::STATE_BYTES_DATA) { if(_in->has_bytes(_currentLength)) { unsigned char *data = new unsigned char[_currentLength]; _in->get_bytes(data, _currentLength); - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; _listener->on_bytes(data, _currentLength); } else break; - } else if(_state == STATE_STRING_SIZE) { + } else if(_state == decoder_state::STATE_STRING_SIZE) { if(_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: _currentLength = _in->get_byte(); - _state = STATE_STRING_DATA; + _state = decoder_state::STATE_STRING_DATA; break; case 2: _currentLength = _in->get_short(); - _state = STATE_STRING_DATA; + _state = decoder_state::STATE_STRING_DATA; break; case 4: _currentLength = _in->get_int(); - _state = STATE_STRING_DATA; + _state = decoder_state::STATE_STRING_DATA; break; case 8: - _state = STATE_ERROR; + _state = decoder_state::STATE_ERROR; _listener->on_error("extra long array"); break; } } else break; - } else if(_state == STATE_STRING_DATA) { + } else if(_state == decoder_state::STATE_STRING_DATA) { if(_in->has_bytes(_currentLength)) { unsigned char *data = new unsigned char[_currentLength]; _in->get_bytes(data, _currentLength); - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; std::string str((const char *)data, (size_t)_currentLength); _listener->on_string(str); } else break; - } else if(_state == STATE_ARRAY) { + } else if(_state == decoder_state::STATE_ARRAY) { if(_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: _listener->on_array(_in->get_byte()); - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; break; case 2: _listener->on_array(_currentLength = _in->get_short()); - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; break; case 4: _listener->on_array(_in->get_int()); - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; break; case 8: - _state = STATE_ERROR; + _state = decoder_state::STATE_ERROR; _listener->on_error("extra long array"); break; } } else break; - } else if(_state == STATE_MAP) { + } else if(_state == decoder_state::STATE_MAP) { if(_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: _listener->on_map(_in->get_byte()); - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; break; case 2: _listener->on_map(_currentLength = _in->get_short()); - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; break; case 4: _listener->on_map(_in->get_int()); - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; break; case 8: - _state = STATE_ERROR; + _state = decoder_state::STATE_ERROR; _listener->on_error("extra long map"); break; } } else break; - } else if(_state == STATE_TAG) { + } else if(_state == decoder_state::STATE_TAG) { if(_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: _listener->on_tag(_in->get_byte()); - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; break; case 2: _listener->on_tag(_in->get_short()); - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; break; case 4: _listener->on_tag(_in->get_int()); - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; break; case 8: _listener->on_extra_tag(_in->get_long()); - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; break; } } else break; - } else if(_state == STATE_SPECIAL) { + } else if(_state == decoder_state::STATE_SPECIAL) { if (_in->has_bytes(_currentLength)) { switch (_currentLength) { case 1: _listener->on_special(_in->get_byte()); - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; break; case 2: _listener->on_special(_in->get_short()); - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; break; case 4: _listener->on_special(_in->get_int()); - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; break; case 8: _listener->on_extra_special(_in->get_long()); - _state = STATE_TYPE; + _state = decoder_state::STATE_TYPE; break; } } else break; - } else if(_state == STATE_ERROR) { + } else if(_state == decoder_state::STATE_ERROR) { break; } else { logger("UNKNOWN STATE"); diff --git a/src/decoder.h b/src/decoder.h index 5dc1c53..5590f4a 100644 --- a/src/decoder.h +++ b/src/decoder.h @@ -22,7 +22,7 @@ #include "input.h" namespace cbor { - typedef enum { + enum class decoder_state : uint8_t { STATE_TYPE, STATE_PINT, STATE_NINT, @@ -35,7 +35,7 @@ namespace cbor { STATE_TAG, STATE_SPECIAL, STATE_ERROR - } decoder_state; + }; class decoder { private: From accdad372192f4d0964b9cfa31b401f89bc3c815 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Tue, 20 Apr 2021 23:14:50 +0200 Subject: [PATCH 08/47] Remove enum prefix and l'cased values --- src/decoder.cpp | 178 ++++++++++++++++++++++++------------------------ src/decoder.h | 24 +++---- 2 files changed, 101 insertions(+), 101 deletions(-) diff --git a/src/decoder.cpp b/src/decoder.cpp index 0040382..803c97f 100644 --- a/src/decoder.cpp +++ b/src/decoder.cpp @@ -23,13 +23,13 @@ using namespace cbor; decoder::decoder(input &in) { _in = ∈ - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; } decoder::decoder(input &in, listener &listener) { _in = ∈ _listener = &listener; - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; } decoder::~decoder() { @@ -43,7 +43,7 @@ void decoder::set_listener(listener &listener_instance) { void decoder::run() { unsigned int temp; while(1) { - if(_state == decoder_state::STATE_TYPE) { + if(_state == decoder_state::type) { if(_in->has_bytes(1)) { unsigned char type = _in->get_byte(); unsigned char majorType = type >> 5; @@ -55,18 +55,18 @@ void decoder::run() { _listener->on_integer(minorType); } else if(minorType == 24) { // 1 byte _currentLength = 1; - _state = decoder_state::STATE_PINT; + _state = decoder_state::pint; } else if(minorType == 25) { // 2 byte _currentLength = 2; - _state = decoder_state::STATE_PINT; + _state = decoder_state::pint; } else if(minorType == 26) { // 4 byte _currentLength = 4; - _state = decoder_state::STATE_PINT; + _state = decoder_state::pint; } else if(minorType == 27) { // 8 byte _currentLength = 8; - _state = decoder_state::STATE_PINT; + _state = decoder_state::pint; } else { - _state = decoder_state::STATE_ERROR; + _state = decoder_state::error; _listener->on_error("invalid integer type"); } break; @@ -75,60 +75,60 @@ void decoder::run() { _listener->on_integer(-1 -minorType); } else if(minorType == 24) { // 1 byte _currentLength = 1; - _state = decoder_state::STATE_NINT; + _state = decoder_state::nint; } else if(minorType == 25) { // 2 byte _currentLength = 2; - _state = decoder_state::STATE_NINT; + _state = decoder_state::nint; } else if(minorType == 26) { // 4 byte _currentLength = 4; - _state = decoder_state::STATE_NINT; + _state = decoder_state::nint; } else if(minorType == 27) { // 8 byte _currentLength = 8; - _state = decoder_state::STATE_NINT; + _state = decoder_state::nint; } else { - _state = decoder_state::STATE_ERROR; + _state = decoder_state::error; _listener->on_error("invalid integer type"); } break; case 2: // bytes if(minorType < 24) { - _state = decoder_state::STATE_BYTES_DATA; + _state = decoder_state::bytes_data; _currentLength = minorType; } else if(minorType == 24) { - _state = decoder_state::STATE_BYTES_SIZE; + _state = decoder_state::bytes_size; _currentLength = 1; } else if(minorType == 25) { // 2 byte _currentLength = 2; - _state = decoder_state::STATE_BYTES_SIZE; + _state = decoder_state::bytes_size; } else if(minorType == 26) { // 4 byte _currentLength = 4; - _state = decoder_state::STATE_BYTES_SIZE; + _state = decoder_state::bytes_size; } else if(minorType == 27) { // 8 byte _currentLength = 8; - _state = decoder_state::STATE_BYTES_SIZE; + _state = decoder_state::bytes_size; } else { - _state = decoder_state::STATE_ERROR; + _state = decoder_state::error; _listener->on_error("invalid bytes type"); } break; case 3: // string if(minorType < 24) { - _state = decoder_state::STATE_STRING_DATA; + _state = decoder_state::string_data; _currentLength = minorType; } else if(minorType == 24) { - _state = decoder_state::STATE_STRING_SIZE; + _state = decoder_state::string_size; _currentLength = 1; } else if(minorType == 25) { // 2 byte _currentLength = 2; - _state = decoder_state::STATE_STRING_SIZE; + _state = decoder_state::string_size; } else if(minorType == 26) { // 4 byte _currentLength = 4; - _state = decoder_state::STATE_STRING_SIZE; + _state = decoder_state::string_size; } else if(minorType == 27) { // 8 byte _currentLength = 8; - _state = decoder_state::STATE_STRING_SIZE; + _state = decoder_state::string_size; } else { - _state = decoder_state::STATE_ERROR; + _state = decoder_state::error; _listener->on_error("invalid string type"); } break; @@ -136,19 +136,19 @@ void decoder::run() { if(minorType < 24) { _listener->on_array(minorType); } else if(minorType == 24) { - _state = decoder_state::STATE_ARRAY; + _state = decoder_state::array; _currentLength = 1; } else if(minorType == 25) { // 2 byte _currentLength = 2; - _state = decoder_state::STATE_ARRAY; + _state = decoder_state::array; } else if(minorType == 26) { // 4 byte _currentLength = 4; - _state = decoder_state::STATE_ARRAY; + _state = decoder_state::array; } else if(minorType == 27) { // 8 byte _currentLength = 8; - _state = decoder_state::STATE_ARRAY; + _state = decoder_state::array; } else { - _state = decoder_state::STATE_ERROR; + _state = decoder_state::error; _listener->on_error("invalid array type"); } break; @@ -156,19 +156,19 @@ void decoder::run() { if(minorType < 24) { _listener->on_map(minorType); } else if(minorType == 24) { - _state = decoder_state::STATE_MAP; + _state = decoder_state::map; _currentLength = 1; } else if(minorType == 25) { // 2 byte _currentLength = 2; - _state = decoder_state::STATE_MAP; + _state = decoder_state::map; } else if(minorType == 26) { // 4 byte _currentLength = 4; - _state = decoder_state::STATE_MAP; + _state = decoder_state::map; } else if(minorType == 27) { // 8 byte _currentLength = 8; - _state = decoder_state::STATE_MAP; + _state = decoder_state::map; } else { - _state = decoder_state::STATE_ERROR; + _state = decoder_state::error; _listener->on_error("invalid array type"); } break; @@ -176,19 +176,19 @@ void decoder::run() { if(minorType < 24) { _listener->on_tag(minorType); } else if(minorType == 24) { - _state = decoder_state::STATE_TAG; + _state = decoder_state::tag; _currentLength = 1; } else if(minorType == 25) { // 2 byte _currentLength = 2; - _state = decoder_state::STATE_TAG; + _state = decoder_state::tag; } else if(minorType == 26) { // 4 byte _currentLength = 4; - _state = decoder_state::STATE_TAG; + _state = decoder_state::tag; } else if(minorType == 27) { // 8 byte _currentLength = 8; - _state = decoder_state::STATE_TAG; + _state = decoder_state::tag; } else { - _state = decoder_state::STATE_ERROR; + _state = decoder_state::error; _listener->on_error("invalid tag type"); } break; @@ -204,34 +204,34 @@ void decoder::run() { } else if (minorType == 23) { _listener->on_undefined(); } else if(minorType == 24) { - _state = decoder_state::STATE_SPECIAL; + _state = decoder_state::special; _currentLength = 1; } else if(minorType == 25) { // 2 byte _currentLength = 2; - _state = decoder_state::STATE_SPECIAL; + _state = decoder_state::special; } else if(minorType == 26) { // 4 byte _currentLength = 4; - _state = decoder_state::STATE_SPECIAL; + _state = decoder_state::special; } else if(minorType == 27) { // 8 byte _currentLength = 8; - _state = decoder_state::STATE_SPECIAL; + _state = decoder_state::special; } else { - _state = decoder_state::STATE_ERROR; + _state = decoder_state::error; _listener->on_error("invalid special type"); } break; } } else break; - } else if(_state == decoder_state::STATE_PINT) { + } else if(_state == decoder_state::pint) { if(_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: _listener->on_integer(_in->get_byte()); - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; break; case 2: _listener->on_integer(_in->get_short()); - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; break; case 4: temp = _in->get_int(); @@ -240,24 +240,24 @@ void decoder::run() { } else { _listener->on_extra_integer(temp, 1); } - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; break; case 8: _listener->on_extra_integer(_in->get_long(), 1); - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; break; } } else break; - } else if(_state == decoder_state::STATE_NINT) { + } else if(_state == decoder_state::nint) { if(_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: _listener->on_integer(-(int) _in->get_byte() - 1); - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; break; case 2: _listener->on_integer(-(int) _in->get_short() - 1); - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; break; case 4: temp = _in->get_int(); @@ -266,155 +266,155 @@ void decoder::run() { } else { _listener->on_extra_integer(temp + 1, -1); } - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; break; case 8: _listener->on_extra_integer(_in->get_long() + 1, -1); break; } } else break; - } else if(_state == decoder_state::STATE_BYTES_SIZE) { + } else if(_state == decoder_state::bytes_size) { if(_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: _currentLength = _in->get_byte(); - _state = decoder_state::STATE_BYTES_DATA; + _state = decoder_state::bytes_data; break; case 2: _currentLength = _in->get_short(); - _state = decoder_state::STATE_BYTES_DATA; + _state = decoder_state::bytes_data; break; case 4: _currentLength = _in->get_int(); - _state = decoder_state::STATE_BYTES_DATA; + _state = decoder_state::bytes_data; break; case 8: - _state = decoder_state::STATE_ERROR; + _state = decoder_state::error; _listener->on_error("extra long bytes"); break; } } else break; - } else if(_state == decoder_state::STATE_BYTES_DATA) { + } else if(_state == decoder_state::bytes_data) { if(_in->has_bytes(_currentLength)) { unsigned char *data = new unsigned char[_currentLength]; _in->get_bytes(data, _currentLength); - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; _listener->on_bytes(data, _currentLength); } else break; - } else if(_state == decoder_state::STATE_STRING_SIZE) { + } else if(_state == decoder_state::string_size) { if(_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: _currentLength = _in->get_byte(); - _state = decoder_state::STATE_STRING_DATA; + _state = decoder_state::string_data; break; case 2: _currentLength = _in->get_short(); - _state = decoder_state::STATE_STRING_DATA; + _state = decoder_state::string_data; break; case 4: _currentLength = _in->get_int(); - _state = decoder_state::STATE_STRING_DATA; + _state = decoder_state::string_data; break; case 8: - _state = decoder_state::STATE_ERROR; + _state = decoder_state::error; _listener->on_error("extra long array"); break; } } else break; - } else if(_state == decoder_state::STATE_STRING_DATA) { + } else if(_state == decoder_state::string_data) { if(_in->has_bytes(_currentLength)) { unsigned char *data = new unsigned char[_currentLength]; _in->get_bytes(data, _currentLength); - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; std::string str((const char *)data, (size_t)_currentLength); _listener->on_string(str); } else break; - } else if(_state == decoder_state::STATE_ARRAY) { + } else if(_state == decoder_state::array) { if(_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: _listener->on_array(_in->get_byte()); - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; break; case 2: _listener->on_array(_currentLength = _in->get_short()); - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; break; case 4: _listener->on_array(_in->get_int()); - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; break; case 8: - _state = decoder_state::STATE_ERROR; + _state = decoder_state::error; _listener->on_error("extra long array"); break; } } else break; - } else if(_state == decoder_state::STATE_MAP) { + } else if(_state == decoder_state::map) { if(_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: _listener->on_map(_in->get_byte()); - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; break; case 2: _listener->on_map(_currentLength = _in->get_short()); - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; break; case 4: _listener->on_map(_in->get_int()); - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; break; case 8: - _state = decoder_state::STATE_ERROR; + _state = decoder_state::error; _listener->on_error("extra long map"); break; } } else break; - } else if(_state == decoder_state::STATE_TAG) { + } else if(_state == decoder_state::tag) { if(_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: _listener->on_tag(_in->get_byte()); - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; break; case 2: _listener->on_tag(_in->get_short()); - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; break; case 4: _listener->on_tag(_in->get_int()); - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; break; case 8: _listener->on_extra_tag(_in->get_long()); - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; break; } } else break; - } else if(_state == decoder_state::STATE_SPECIAL) { + } else if(_state == decoder_state::special) { if (_in->has_bytes(_currentLength)) { switch (_currentLength) { case 1: _listener->on_special(_in->get_byte()); - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; break; case 2: _listener->on_special(_in->get_short()); - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; break; case 4: _listener->on_special(_in->get_int()); - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; break; case 8: _listener->on_extra_special(_in->get_long()); - _state = decoder_state::STATE_TYPE; + _state = decoder_state::type; break; } } else break; - } else if(_state == decoder_state::STATE_ERROR) { + } else if(_state == decoder_state::error) { break; } else { logger("UNKNOWN STATE"); diff --git a/src/decoder.h b/src/decoder.h index 5590f4a..23b6a9a 100644 --- a/src/decoder.h +++ b/src/decoder.h @@ -23,18 +23,18 @@ namespace cbor { enum class decoder_state : uint8_t { - STATE_TYPE, - STATE_PINT, - STATE_NINT, - STATE_BYTES_SIZE, - STATE_BYTES_DATA, - STATE_STRING_SIZE, - STATE_STRING_DATA, - STATE_ARRAY, - STATE_MAP, - STATE_TAG, - STATE_SPECIAL, - STATE_ERROR + type, + pint, + nint, + bytes_size, + bytes_data, + string_size, + string_data, + array, + map, + tag, + special, + error }; class decoder { From c953f3004416397fa3954951598bf5698fc3d96f Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Tue, 20 Apr 2021 23:20:22 +0200 Subject: [PATCH 09/47] Refactor _listener --- src/decoder.cpp | 100 ++++++++++++++++++++++++------------------------ src/decoder.h | 2 +- 2 files changed, 51 insertions(+), 51 deletions(-) diff --git a/src/decoder.cpp b/src/decoder.cpp index 803c97f..1145590 100644 --- a/src/decoder.cpp +++ b/src/decoder.cpp @@ -28,7 +28,7 @@ decoder::decoder(input &in) { decoder::decoder(input &in, listener &listener) { _in = ∈ - _listener = &listener; + m_listener = &listener; _state = decoder_state::type; } @@ -37,7 +37,7 @@ decoder::~decoder() { } void decoder::set_listener(listener &listener_instance) { - _listener = &listener_instance; + m_listener = &listener_instance; } void decoder::run() { @@ -52,7 +52,7 @@ void decoder::run() { switch(majorType) { case 0: // positive integer if(minorType < 24) { - _listener->on_integer(minorType); + m_listener->on_integer(minorType); } else if(minorType == 24) { // 1 byte _currentLength = 1; _state = decoder_state::pint; @@ -67,12 +67,12 @@ void decoder::run() { _state = decoder_state::pint; } else { _state = decoder_state::error; - _listener->on_error("invalid integer type"); + m_listener->on_error("invalid integer type"); } break; case 1: // negative integer if(minorType < 24) { - _listener->on_integer(-1 -minorType); + m_listener->on_integer(-1 -minorType); } else if(minorType == 24) { // 1 byte _currentLength = 1; _state = decoder_state::nint; @@ -87,7 +87,7 @@ void decoder::run() { _state = decoder_state::nint; } else { _state = decoder_state::error; - _listener->on_error("invalid integer type"); + m_listener->on_error("invalid integer type"); } break; case 2: // bytes @@ -108,7 +108,7 @@ void decoder::run() { _state = decoder_state::bytes_size; } else { _state = decoder_state::error; - _listener->on_error("invalid bytes type"); + m_listener->on_error("invalid bytes type"); } break; case 3: // string @@ -129,12 +129,12 @@ void decoder::run() { _state = decoder_state::string_size; } else { _state = decoder_state::error; - _listener->on_error("invalid string type"); + m_listener->on_error("invalid string type"); } break; case 4: // array if(minorType < 24) { - _listener->on_array(minorType); + m_listener->on_array(minorType); } else if(minorType == 24) { _state = decoder_state::array; _currentLength = 1; @@ -149,12 +149,12 @@ void decoder::run() { _state = decoder_state::array; } else { _state = decoder_state::error; - _listener->on_error("invalid array type"); + m_listener->on_error("invalid array type"); } break; case 5: // map if(minorType < 24) { - _listener->on_map(minorType); + m_listener->on_map(minorType); } else if(minorType == 24) { _state = decoder_state::map; _currentLength = 1; @@ -169,12 +169,12 @@ void decoder::run() { _state = decoder_state::map; } else { _state = decoder_state::error; - _listener->on_error("invalid array type"); + m_listener->on_error("invalid array type"); } break; case 6: // tag if(minorType < 24) { - _listener->on_tag(minorType); + m_listener->on_tag(minorType); } else if(minorType == 24) { _state = decoder_state::tag; _currentLength = 1; @@ -189,20 +189,20 @@ void decoder::run() { _state = decoder_state::tag; } else { _state = decoder_state::error; - _listener->on_error("invalid tag type"); + m_listener->on_error("invalid tag type"); } break; case 7: // special if (minorType < 20) { - _listener->on_special(minorType); + m_listener->on_special(minorType); } else if (minorType == 20) { - _listener->on_bool(false); + m_listener->on_bool(false); } else if (minorType == 21) { - _listener->on_bool(true); + m_listener->on_bool(true); } else if (minorType == 22) { - _listener->on_null(); + m_listener->on_null(); } else if (minorType == 23) { - _listener->on_undefined(); + m_listener->on_undefined(); } else if(minorType == 24) { _state = decoder_state::special; _currentLength = 1; @@ -217,7 +217,7 @@ void decoder::run() { _state = decoder_state::special; } else { _state = decoder_state::error; - _listener->on_error("invalid special type"); + m_listener->on_error("invalid special type"); } break; } @@ -226,24 +226,24 @@ void decoder::run() { if(_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: - _listener->on_integer(_in->get_byte()); + m_listener->on_integer(_in->get_byte()); _state = decoder_state::type; break; case 2: - _listener->on_integer(_in->get_short()); + m_listener->on_integer(_in->get_short()); _state = decoder_state::type; break; case 4: temp = _in->get_int(); if(temp <= INT_MAX) { - _listener->on_integer(temp); + m_listener->on_integer(temp); } else { - _listener->on_extra_integer(temp, 1); + m_listener->on_extra_integer(temp, 1); } _state = decoder_state::type; break; case 8: - _listener->on_extra_integer(_in->get_long(), 1); + m_listener->on_extra_integer(_in->get_long(), 1); _state = decoder_state::type; break; } @@ -252,24 +252,24 @@ void decoder::run() { if(_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: - _listener->on_integer(-(int) _in->get_byte() - 1); + m_listener->on_integer(-(int) _in->get_byte() - 1); _state = decoder_state::type; break; case 2: - _listener->on_integer(-(int) _in->get_short() - 1); + m_listener->on_integer(-(int) _in->get_short() - 1); _state = decoder_state::type; break; case 4: temp = _in->get_int(); if(temp <= INT_MAX) { - _listener->on_integer(-(int) temp - 1); + m_listener->on_integer(-(int) temp - 1); } else { - _listener->on_extra_integer(temp + 1, -1); + m_listener->on_extra_integer(temp + 1, -1); } _state = decoder_state::type; break; case 8: - _listener->on_extra_integer(_in->get_long() + 1, -1); + m_listener->on_extra_integer(_in->get_long() + 1, -1); break; } } else break; @@ -290,7 +290,7 @@ void decoder::run() { break; case 8: _state = decoder_state::error; - _listener->on_error("extra long bytes"); + m_listener->on_error("extra long bytes"); break; } } else break; @@ -299,7 +299,7 @@ void decoder::run() { unsigned char *data = new unsigned char[_currentLength]; _in->get_bytes(data, _currentLength); _state = decoder_state::type; - _listener->on_bytes(data, _currentLength); + m_listener->on_bytes(data, _currentLength); } else break; } else if(_state == decoder_state::string_size) { if(_in->has_bytes(_currentLength)) { @@ -318,7 +318,7 @@ void decoder::run() { break; case 8: _state = decoder_state::error; - _listener->on_error("extra long array"); + m_listener->on_error("extra long array"); break; } } else break; @@ -328,26 +328,26 @@ void decoder::run() { _in->get_bytes(data, _currentLength); _state = decoder_state::type; std::string str((const char *)data, (size_t)_currentLength); - _listener->on_string(str); + m_listener->on_string(str); } else break; } else if(_state == decoder_state::array) { if(_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: - _listener->on_array(_in->get_byte()); + m_listener->on_array(_in->get_byte()); _state = decoder_state::type; break; case 2: - _listener->on_array(_currentLength = _in->get_short()); + m_listener->on_array(_currentLength = _in->get_short()); _state = decoder_state::type; break; case 4: - _listener->on_array(_in->get_int()); + m_listener->on_array(_in->get_int()); _state = decoder_state::type; break; case 8: _state = decoder_state::error; - _listener->on_error("extra long array"); + m_listener->on_error("extra long array"); break; } } else break; @@ -355,20 +355,20 @@ void decoder::run() { if(_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: - _listener->on_map(_in->get_byte()); + m_listener->on_map(_in->get_byte()); _state = decoder_state::type; break; case 2: - _listener->on_map(_currentLength = _in->get_short()); + m_listener->on_map(_currentLength = _in->get_short()); _state = decoder_state::type; break; case 4: - _listener->on_map(_in->get_int()); + m_listener->on_map(_in->get_int()); _state = decoder_state::type; break; case 8: _state = decoder_state::error; - _listener->on_error("extra long map"); + m_listener->on_error("extra long map"); break; } } else break; @@ -376,19 +376,19 @@ void decoder::run() { if(_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: - _listener->on_tag(_in->get_byte()); + m_listener->on_tag(_in->get_byte()); _state = decoder_state::type; break; case 2: - _listener->on_tag(_in->get_short()); + m_listener->on_tag(_in->get_short()); _state = decoder_state::type; break; case 4: - _listener->on_tag(_in->get_int()); + m_listener->on_tag(_in->get_int()); _state = decoder_state::type; break; case 8: - _listener->on_extra_tag(_in->get_long()); + m_listener->on_extra_tag(_in->get_long()); _state = decoder_state::type; break; } @@ -397,19 +397,19 @@ void decoder::run() { if (_in->has_bytes(_currentLength)) { switch (_currentLength) { case 1: - _listener->on_special(_in->get_byte()); + m_listener->on_special(_in->get_byte()); _state = decoder_state::type; break; case 2: - _listener->on_special(_in->get_short()); + m_listener->on_special(_in->get_short()); _state = decoder_state::type; break; case 4: - _listener->on_special(_in->get_int()); + m_listener->on_special(_in->get_int()); _state = decoder_state::type; break; case 8: - _listener->on_extra_special(_in->get_long()); + m_listener->on_extra_special(_in->get_long()); _state = decoder_state::type; break; } diff --git a/src/decoder.h b/src/decoder.h index 23b6a9a..403de0d 100644 --- a/src/decoder.h +++ b/src/decoder.h @@ -39,7 +39,7 @@ namespace cbor { class decoder { private: - listener *_listener; + listener *m_listener; input *_in; decoder_state _state; int _currentLength; From 4a15cce1144e75a44f49089072ad1111700a0f81 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Tue, 20 Apr 2021 23:20:51 +0200 Subject: [PATCH 10/47] Refactor _in --- src/decoder.cpp | 88 ++++++++++++++++++++++++------------------------- src/decoder.h | 2 +- 2 files changed, 45 insertions(+), 45 deletions(-) diff --git a/src/decoder.cpp b/src/decoder.cpp index 1145590..d8945f6 100644 --- a/src/decoder.cpp +++ b/src/decoder.cpp @@ -22,12 +22,12 @@ using namespace cbor; decoder::decoder(input &in) { - _in = ∈ + m_in = ∈ _state = decoder_state::type; } decoder::decoder(input &in, listener &listener) { - _in = ∈ + m_in = ∈ m_listener = &listener; _state = decoder_state::type; } @@ -44,8 +44,8 @@ void decoder::run() { unsigned int temp; while(1) { if(_state == decoder_state::type) { - if(_in->has_bytes(1)) { - unsigned char type = _in->get_byte(); + if(m_in->has_bytes(1)) { + unsigned char type = m_in->get_byte(); unsigned char majorType = type >> 5; unsigned char minorType = (unsigned char) (type & 31); @@ -223,18 +223,18 @@ void decoder::run() { } } else break; } else if(_state == decoder_state::pint) { - if(_in->has_bytes(_currentLength)) { + if(m_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: - m_listener->on_integer(_in->get_byte()); + m_listener->on_integer(m_in->get_byte()); _state = decoder_state::type; break; case 2: - m_listener->on_integer(_in->get_short()); + m_listener->on_integer(m_in->get_short()); _state = decoder_state::type; break; case 4: - temp = _in->get_int(); + temp = m_in->get_int(); if(temp <= INT_MAX) { m_listener->on_integer(temp); } else { @@ -243,24 +243,24 @@ void decoder::run() { _state = decoder_state::type; break; case 8: - m_listener->on_extra_integer(_in->get_long(), 1); + m_listener->on_extra_integer(m_in->get_long(), 1); _state = decoder_state::type; break; } } else break; } else if(_state == decoder_state::nint) { - if(_in->has_bytes(_currentLength)) { + if(m_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: - m_listener->on_integer(-(int) _in->get_byte() - 1); + m_listener->on_integer(-(int) m_in->get_byte() - 1); _state = decoder_state::type; break; case 2: - m_listener->on_integer(-(int) _in->get_short() - 1); + m_listener->on_integer(-(int) m_in->get_short() - 1); _state = decoder_state::type; break; case 4: - temp = _in->get_int(); + temp = m_in->get_int(); if(temp <= INT_MAX) { m_listener->on_integer(-(int) temp - 1); } else { @@ -269,23 +269,23 @@ void decoder::run() { _state = decoder_state::type; break; case 8: - m_listener->on_extra_integer(_in->get_long() + 1, -1); + m_listener->on_extra_integer(m_in->get_long() + 1, -1); break; } } else break; } else if(_state == decoder_state::bytes_size) { - if(_in->has_bytes(_currentLength)) { + if(m_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: - _currentLength = _in->get_byte(); + _currentLength = m_in->get_byte(); _state = decoder_state::bytes_data; break; case 2: - _currentLength = _in->get_short(); + _currentLength = m_in->get_short(); _state = decoder_state::bytes_data; break; case 4: - _currentLength = _in->get_int(); + _currentLength = m_in->get_int(); _state = decoder_state::bytes_data; break; case 8: @@ -295,25 +295,25 @@ void decoder::run() { } } else break; } else if(_state == decoder_state::bytes_data) { - if(_in->has_bytes(_currentLength)) { + if(m_in->has_bytes(_currentLength)) { unsigned char *data = new unsigned char[_currentLength]; - _in->get_bytes(data, _currentLength); + m_in->get_bytes(data, _currentLength); _state = decoder_state::type; m_listener->on_bytes(data, _currentLength); } else break; } else if(_state == decoder_state::string_size) { - if(_in->has_bytes(_currentLength)) { + if(m_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: - _currentLength = _in->get_byte(); + _currentLength = m_in->get_byte(); _state = decoder_state::string_data; break; case 2: - _currentLength = _in->get_short(); + _currentLength = m_in->get_short(); _state = decoder_state::string_data; break; case 4: - _currentLength = _in->get_int(); + _currentLength = m_in->get_int(); _state = decoder_state::string_data; break; case 8: @@ -323,26 +323,26 @@ void decoder::run() { } } else break; } else if(_state == decoder_state::string_data) { - if(_in->has_bytes(_currentLength)) { + if(m_in->has_bytes(_currentLength)) { unsigned char *data = new unsigned char[_currentLength]; - _in->get_bytes(data, _currentLength); + m_in->get_bytes(data, _currentLength); _state = decoder_state::type; std::string str((const char *)data, (size_t)_currentLength); m_listener->on_string(str); } else break; } else if(_state == decoder_state::array) { - if(_in->has_bytes(_currentLength)) { + if(m_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: - m_listener->on_array(_in->get_byte()); + m_listener->on_array(m_in->get_byte()); _state = decoder_state::type; break; case 2: - m_listener->on_array(_currentLength = _in->get_short()); + m_listener->on_array(_currentLength = m_in->get_short()); _state = decoder_state::type; break; case 4: - m_listener->on_array(_in->get_int()); + m_listener->on_array(m_in->get_int()); _state = decoder_state::type; break; case 8: @@ -352,18 +352,18 @@ void decoder::run() { } } else break; } else if(_state == decoder_state::map) { - if(_in->has_bytes(_currentLength)) { + if(m_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: - m_listener->on_map(_in->get_byte()); + m_listener->on_map(m_in->get_byte()); _state = decoder_state::type; break; case 2: - m_listener->on_map(_currentLength = _in->get_short()); + m_listener->on_map(_currentLength = m_in->get_short()); _state = decoder_state::type; break; case 4: - m_listener->on_map(_in->get_int()); + m_listener->on_map(m_in->get_int()); _state = decoder_state::type; break; case 8: @@ -373,43 +373,43 @@ void decoder::run() { } } else break; } else if(_state == decoder_state::tag) { - if(_in->has_bytes(_currentLength)) { + if(m_in->has_bytes(_currentLength)) { switch(_currentLength) { case 1: - m_listener->on_tag(_in->get_byte()); + m_listener->on_tag(m_in->get_byte()); _state = decoder_state::type; break; case 2: - m_listener->on_tag(_in->get_short()); + m_listener->on_tag(m_in->get_short()); _state = decoder_state::type; break; case 4: - m_listener->on_tag(_in->get_int()); + m_listener->on_tag(m_in->get_int()); _state = decoder_state::type; break; case 8: - m_listener->on_extra_tag(_in->get_long()); + m_listener->on_extra_tag(m_in->get_long()); _state = decoder_state::type; break; } } else break; } else if(_state == decoder_state::special) { - if (_in->has_bytes(_currentLength)) { + if (m_in->has_bytes(_currentLength)) { switch (_currentLength) { case 1: - m_listener->on_special(_in->get_byte()); + m_listener->on_special(m_in->get_byte()); _state = decoder_state::type; break; case 2: - m_listener->on_special(_in->get_short()); + m_listener->on_special(m_in->get_short()); _state = decoder_state::type; break; case 4: - m_listener->on_special(_in->get_int()); + m_listener->on_special(m_in->get_int()); _state = decoder_state::type; break; case 8: - m_listener->on_extra_special(_in->get_long()); + m_listener->on_extra_special(m_in->get_long()); _state = decoder_state::type; break; } diff --git a/src/decoder.h b/src/decoder.h index 403de0d..bd362bf 100644 --- a/src/decoder.h +++ b/src/decoder.h @@ -40,7 +40,7 @@ namespace cbor { class decoder { private: listener *m_listener; - input *_in; + input *m_in; decoder_state _state; int _currentLength; public: From 95d7d52183d627cda61bb06910f6a5b751c245b2 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Tue, 20 Apr 2021 23:22:04 +0200 Subject: [PATCH 11/47] Refactor _state and _currentLength --- src/decoder.cpp | 310 ++++++++++++++++++++++++------------------------ src/decoder.h | 4 +- 2 files changed, 157 insertions(+), 157 deletions(-) diff --git a/src/decoder.cpp b/src/decoder.cpp index d8945f6..cbf4585 100644 --- a/src/decoder.cpp +++ b/src/decoder.cpp @@ -23,13 +23,13 @@ using namespace cbor; decoder::decoder(input &in) { m_in = ∈ - _state = decoder_state::type; + m_state = decoder_state::type; } decoder::decoder(input &in, listener &listener) { m_in = ∈ m_listener = &listener; - _state = decoder_state::type; + m_state = decoder_state::type; } decoder::~decoder() { @@ -43,7 +43,7 @@ void decoder::set_listener(listener &listener_instance) { void decoder::run() { unsigned int temp; while(1) { - if(_state == decoder_state::type) { + if(m_state == decoder_state::type) { if(m_in->has_bytes(1)) { unsigned char type = m_in->get_byte(); unsigned char majorType = type >> 5; @@ -54,19 +54,19 @@ void decoder::run() { if(minorType < 24) { m_listener->on_integer(minorType); } else if(minorType == 24) { // 1 byte - _currentLength = 1; - _state = decoder_state::pint; + m_currentLength = 1; + m_state = decoder_state::pint; } else if(minorType == 25) { // 2 byte - _currentLength = 2; - _state = decoder_state::pint; + m_currentLength = 2; + m_state = decoder_state::pint; } else if(minorType == 26) { // 4 byte - _currentLength = 4; - _state = decoder_state::pint; + m_currentLength = 4; + m_state = decoder_state::pint; } else if(minorType == 27) { // 8 byte - _currentLength = 8; - _state = decoder_state::pint; + m_currentLength = 8; + m_state = decoder_state::pint; } else { - _state = decoder_state::error; + m_state = decoder_state::error; m_listener->on_error("invalid integer type"); } break; @@ -74,61 +74,61 @@ void decoder::run() { if(minorType < 24) { m_listener->on_integer(-1 -minorType); } else if(minorType == 24) { // 1 byte - _currentLength = 1; - _state = decoder_state::nint; + m_currentLength = 1; + m_state = decoder_state::nint; } else if(minorType == 25) { // 2 byte - _currentLength = 2; - _state = decoder_state::nint; + m_currentLength = 2; + m_state = decoder_state::nint; } else if(minorType == 26) { // 4 byte - _currentLength = 4; - _state = decoder_state::nint; + m_currentLength = 4; + m_state = decoder_state::nint; } else if(minorType == 27) { // 8 byte - _currentLength = 8; - _state = decoder_state::nint; + m_currentLength = 8; + m_state = decoder_state::nint; } else { - _state = decoder_state::error; + m_state = decoder_state::error; m_listener->on_error("invalid integer type"); } break; case 2: // bytes if(minorType < 24) { - _state = decoder_state::bytes_data; - _currentLength = minorType; + m_state = decoder_state::bytes_data; + m_currentLength = minorType; } else if(minorType == 24) { - _state = decoder_state::bytes_size; - _currentLength = 1; + m_state = decoder_state::bytes_size; + m_currentLength = 1; } else if(minorType == 25) { // 2 byte - _currentLength = 2; - _state = decoder_state::bytes_size; + m_currentLength = 2; + m_state = decoder_state::bytes_size; } else if(minorType == 26) { // 4 byte - _currentLength = 4; - _state = decoder_state::bytes_size; + m_currentLength = 4; + m_state = decoder_state::bytes_size; } else if(minorType == 27) { // 8 byte - _currentLength = 8; - _state = decoder_state::bytes_size; + m_currentLength = 8; + m_state = decoder_state::bytes_size; } else { - _state = decoder_state::error; + m_state = decoder_state::error; m_listener->on_error("invalid bytes type"); } break; case 3: // string if(minorType < 24) { - _state = decoder_state::string_data; - _currentLength = minorType; + m_state = decoder_state::string_data; + m_currentLength = minorType; } else if(minorType == 24) { - _state = decoder_state::string_size; - _currentLength = 1; + m_state = decoder_state::string_size; + m_currentLength = 1; } else if(minorType == 25) { // 2 byte - _currentLength = 2; - _state = decoder_state::string_size; + m_currentLength = 2; + m_state = decoder_state::string_size; } else if(minorType == 26) { // 4 byte - _currentLength = 4; - _state = decoder_state::string_size; + m_currentLength = 4; + m_state = decoder_state::string_size; } else if(minorType == 27) { // 8 byte - _currentLength = 8; - _state = decoder_state::string_size; + m_currentLength = 8; + m_state = decoder_state::string_size; } else { - _state = decoder_state::error; + m_state = decoder_state::error; m_listener->on_error("invalid string type"); } break; @@ -136,19 +136,19 @@ void decoder::run() { if(minorType < 24) { m_listener->on_array(minorType); } else if(minorType == 24) { - _state = decoder_state::array; - _currentLength = 1; + m_state = decoder_state::array; + m_currentLength = 1; } else if(minorType == 25) { // 2 byte - _currentLength = 2; - _state = decoder_state::array; + m_currentLength = 2; + m_state = decoder_state::array; } else if(minorType == 26) { // 4 byte - _currentLength = 4; - _state = decoder_state::array; + m_currentLength = 4; + m_state = decoder_state::array; } else if(minorType == 27) { // 8 byte - _currentLength = 8; - _state = decoder_state::array; + m_currentLength = 8; + m_state = decoder_state::array; } else { - _state = decoder_state::error; + m_state = decoder_state::error; m_listener->on_error("invalid array type"); } break; @@ -156,19 +156,19 @@ void decoder::run() { if(minorType < 24) { m_listener->on_map(minorType); } else if(minorType == 24) { - _state = decoder_state::map; - _currentLength = 1; + m_state = decoder_state::map; + m_currentLength = 1; } else if(minorType == 25) { // 2 byte - _currentLength = 2; - _state = decoder_state::map; + m_currentLength = 2; + m_state = decoder_state::map; } else if(minorType == 26) { // 4 byte - _currentLength = 4; - _state = decoder_state::map; + m_currentLength = 4; + m_state = decoder_state::map; } else if(minorType == 27) { // 8 byte - _currentLength = 8; - _state = decoder_state::map; + m_currentLength = 8; + m_state = decoder_state::map; } else { - _state = decoder_state::error; + m_state = decoder_state::error; m_listener->on_error("invalid array type"); } break; @@ -176,19 +176,19 @@ void decoder::run() { if(minorType < 24) { m_listener->on_tag(minorType); } else if(minorType == 24) { - _state = decoder_state::tag; - _currentLength = 1; + m_state = decoder_state::tag; + m_currentLength = 1; } else if(minorType == 25) { // 2 byte - _currentLength = 2; - _state = decoder_state::tag; + m_currentLength = 2; + m_state = decoder_state::tag; } else if(minorType == 26) { // 4 byte - _currentLength = 4; - _state = decoder_state::tag; + m_currentLength = 4; + m_state = decoder_state::tag; } else if(minorType == 27) { // 8 byte - _currentLength = 8; - _state = decoder_state::tag; + m_currentLength = 8; + m_state = decoder_state::tag; } else { - _state = decoder_state::error; + m_state = decoder_state::error; m_listener->on_error("invalid tag type"); } break; @@ -204,34 +204,34 @@ void decoder::run() { } else if (minorType == 23) { m_listener->on_undefined(); } else if(minorType == 24) { - _state = decoder_state::special; - _currentLength = 1; + m_state = decoder_state::special; + m_currentLength = 1; } else if(minorType == 25) { // 2 byte - _currentLength = 2; - _state = decoder_state::special; + m_currentLength = 2; + m_state = decoder_state::special; } else if(minorType == 26) { // 4 byte - _currentLength = 4; - _state = decoder_state::special; + m_currentLength = 4; + m_state = decoder_state::special; } else if(minorType == 27) { // 8 byte - _currentLength = 8; - _state = decoder_state::special; + m_currentLength = 8; + m_state = decoder_state::special; } else { - _state = decoder_state::error; + m_state = decoder_state::error; m_listener->on_error("invalid special type"); } break; } } else break; - } else if(_state == decoder_state::pint) { - if(m_in->has_bytes(_currentLength)) { - switch(_currentLength) { + } else if(m_state == decoder_state::pint) { + if(m_in->has_bytes(m_currentLength)) { + switch(m_currentLength) { case 1: m_listener->on_integer(m_in->get_byte()); - _state = decoder_state::type; + m_state = decoder_state::type; break; case 2: m_listener->on_integer(m_in->get_short()); - _state = decoder_state::type; + m_state = decoder_state::type; break; case 4: temp = m_in->get_int(); @@ -240,24 +240,24 @@ void decoder::run() { } else { m_listener->on_extra_integer(temp, 1); } - _state = decoder_state::type; + m_state = decoder_state::type; break; case 8: m_listener->on_extra_integer(m_in->get_long(), 1); - _state = decoder_state::type; + m_state = decoder_state::type; break; } } else break; - } else if(_state == decoder_state::nint) { - if(m_in->has_bytes(_currentLength)) { - switch(_currentLength) { + } else if(m_state == decoder_state::nint) { + if(m_in->has_bytes(m_currentLength)) { + switch(m_currentLength) { case 1: m_listener->on_integer(-(int) m_in->get_byte() - 1); - _state = decoder_state::type; + m_state = decoder_state::type; break; case 2: m_listener->on_integer(-(int) m_in->get_short() - 1); - _state = decoder_state::type; + m_state = decoder_state::type; break; case 4: temp = m_in->get_int(); @@ -266,155 +266,155 @@ void decoder::run() { } else { m_listener->on_extra_integer(temp + 1, -1); } - _state = decoder_state::type; + m_state = decoder_state::type; break; case 8: m_listener->on_extra_integer(m_in->get_long() + 1, -1); break; } } else break; - } else if(_state == decoder_state::bytes_size) { - if(m_in->has_bytes(_currentLength)) { - switch(_currentLength) { + } else if(m_state == decoder_state::bytes_size) { + if(m_in->has_bytes(m_currentLength)) { + switch(m_currentLength) { case 1: - _currentLength = m_in->get_byte(); - _state = decoder_state::bytes_data; + m_currentLength = m_in->get_byte(); + m_state = decoder_state::bytes_data; break; case 2: - _currentLength = m_in->get_short(); - _state = decoder_state::bytes_data; + m_currentLength = m_in->get_short(); + m_state = decoder_state::bytes_data; break; case 4: - _currentLength = m_in->get_int(); - _state = decoder_state::bytes_data; + m_currentLength = m_in->get_int(); + m_state = decoder_state::bytes_data; break; case 8: - _state = decoder_state::error; + m_state = decoder_state::error; m_listener->on_error("extra long bytes"); break; } } else break; - } else if(_state == decoder_state::bytes_data) { - if(m_in->has_bytes(_currentLength)) { - unsigned char *data = new unsigned char[_currentLength]; - m_in->get_bytes(data, _currentLength); - _state = decoder_state::type; - m_listener->on_bytes(data, _currentLength); + } else if(m_state == decoder_state::bytes_data) { + if(m_in->has_bytes(m_currentLength)) { + unsigned char *data = new unsigned char[m_currentLength]; + m_in->get_bytes(data, m_currentLength); + m_state = decoder_state::type; + m_listener->on_bytes(data, m_currentLength); } else break; - } else if(_state == decoder_state::string_size) { - if(m_in->has_bytes(_currentLength)) { - switch(_currentLength) { + } else if(m_state == decoder_state::string_size) { + if(m_in->has_bytes(m_currentLength)) { + switch(m_currentLength) { case 1: - _currentLength = m_in->get_byte(); - _state = decoder_state::string_data; + m_currentLength = m_in->get_byte(); + m_state = decoder_state::string_data; break; case 2: - _currentLength = m_in->get_short(); - _state = decoder_state::string_data; + m_currentLength = m_in->get_short(); + m_state = decoder_state::string_data; break; case 4: - _currentLength = m_in->get_int(); - _state = decoder_state::string_data; + m_currentLength = m_in->get_int(); + m_state = decoder_state::string_data; break; case 8: - _state = decoder_state::error; + m_state = decoder_state::error; m_listener->on_error("extra long array"); break; } } else break; - } else if(_state == decoder_state::string_data) { - if(m_in->has_bytes(_currentLength)) { - unsigned char *data = new unsigned char[_currentLength]; - m_in->get_bytes(data, _currentLength); - _state = decoder_state::type; - std::string str((const char *)data, (size_t)_currentLength); + } else if(m_state == decoder_state::string_data) { + if(m_in->has_bytes(m_currentLength)) { + unsigned char *data = new unsigned char[m_currentLength]; + m_in->get_bytes(data, m_currentLength); + m_state = decoder_state::type; + std::string str((const char *)data, (size_t)m_currentLength); m_listener->on_string(str); } else break; - } else if(_state == decoder_state::array) { - if(m_in->has_bytes(_currentLength)) { - switch(_currentLength) { + } else if(m_state == decoder_state::array) { + if(m_in->has_bytes(m_currentLength)) { + switch(m_currentLength) { case 1: m_listener->on_array(m_in->get_byte()); - _state = decoder_state::type; + m_state = decoder_state::type; break; case 2: - m_listener->on_array(_currentLength = m_in->get_short()); - _state = decoder_state::type; + m_listener->on_array(m_currentLength = m_in->get_short()); + m_state = decoder_state::type; break; case 4: m_listener->on_array(m_in->get_int()); - _state = decoder_state::type; + m_state = decoder_state::type; break; case 8: - _state = decoder_state::error; + m_state = decoder_state::error; m_listener->on_error("extra long array"); break; } } else break; - } else if(_state == decoder_state::map) { - if(m_in->has_bytes(_currentLength)) { - switch(_currentLength) { + } else if(m_state == decoder_state::map) { + if(m_in->has_bytes(m_currentLength)) { + switch(m_currentLength) { case 1: m_listener->on_map(m_in->get_byte()); - _state = decoder_state::type; + m_state = decoder_state::type; break; case 2: - m_listener->on_map(_currentLength = m_in->get_short()); - _state = decoder_state::type; + m_listener->on_map(m_currentLength = m_in->get_short()); + m_state = decoder_state::type; break; case 4: m_listener->on_map(m_in->get_int()); - _state = decoder_state::type; + m_state = decoder_state::type; break; case 8: - _state = decoder_state::error; + m_state = decoder_state::error; m_listener->on_error("extra long map"); break; } } else break; - } else if(_state == decoder_state::tag) { - if(m_in->has_bytes(_currentLength)) { - switch(_currentLength) { + } else if(m_state == decoder_state::tag) { + if(m_in->has_bytes(m_currentLength)) { + switch(m_currentLength) { case 1: m_listener->on_tag(m_in->get_byte()); - _state = decoder_state::type; + m_state = decoder_state::type; break; case 2: m_listener->on_tag(m_in->get_short()); - _state = decoder_state::type; + m_state = decoder_state::type; break; case 4: m_listener->on_tag(m_in->get_int()); - _state = decoder_state::type; + m_state = decoder_state::type; break; case 8: m_listener->on_extra_tag(m_in->get_long()); - _state = decoder_state::type; + m_state = decoder_state::type; break; } } else break; - } else if(_state == decoder_state::special) { - if (m_in->has_bytes(_currentLength)) { - switch (_currentLength) { + } else if(m_state == decoder_state::special) { + if (m_in->has_bytes(m_currentLength)) { + switch (m_currentLength) { case 1: m_listener->on_special(m_in->get_byte()); - _state = decoder_state::type; + m_state = decoder_state::type; break; case 2: m_listener->on_special(m_in->get_short()); - _state = decoder_state::type; + m_state = decoder_state::type; break; case 4: m_listener->on_special(m_in->get_int()); - _state = decoder_state::type; + m_state = decoder_state::type; break; case 8: m_listener->on_extra_special(m_in->get_long()); - _state = decoder_state::type; + m_state = decoder_state::type; break; } } else break; - } else if(_state == decoder_state::error) { + } else if(m_state == decoder_state::error) { break; } else { logger("UNKNOWN STATE"); diff --git a/src/decoder.h b/src/decoder.h index bd362bf..abface9 100644 --- a/src/decoder.h +++ b/src/decoder.h @@ -41,8 +41,8 @@ namespace cbor { private: listener *m_listener; input *m_in; - decoder_state _state; - int _currentLength; + decoder_state m_state; + int m_currentLength; public: decoder(input &in); decoder(input &in, listener &listener); From c141513421ce94fd1d0dd57e2d9afeb8c5ff1e26 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Tue, 20 Apr 2021 23:25:23 +0200 Subject: [PATCH 12/47] Refactor _out --- src/encoder.cpp | 106 ++++++++++++++++++++++++------------------------ src/encoder.h | 2 +- 2 files changed, 54 insertions(+), 54 deletions(-) diff --git a/src/encoder.cpp b/src/encoder.cpp index 529cfd0..9484948 100644 --- a/src/encoder.cpp +++ b/src/encoder.cpp @@ -20,7 +20,7 @@ using namespace cbor; encoder::encoder(output &out) { - _out = &out; + m_out = &out; } encoder::~encoder() { @@ -30,50 +30,50 @@ encoder::~encoder() { void encoder::write_type_value(int major_type, unsigned int value) { major_type <<= 5; if(value < 24) { - _out->put_byte((unsigned char) (major_type | value)); + m_out->put_byte((unsigned char) (major_type | value)); } else if(value < 256) { - _out->put_byte((unsigned char) (major_type | 24)); - _out->put_byte((unsigned char) value); + m_out->put_byte((unsigned char) (major_type | 24)); + m_out->put_byte((unsigned char) value); } else if(value < 65536) { - _out->put_byte((unsigned char) (major_type | 25)); - _out->put_byte((unsigned char) (value >> 8)); - _out->put_byte((unsigned char) value); + m_out->put_byte((unsigned char) (major_type | 25)); + m_out->put_byte((unsigned char) (value >> 8)); + m_out->put_byte((unsigned char) value); } else { - _out->put_byte((unsigned char) (major_type | 26)); - _out->put_byte((unsigned char) (value >> 24)); - _out->put_byte((unsigned char) (value >> 16)); - _out->put_byte((unsigned char) (value >> 8)); - _out->put_byte((unsigned char) value); + m_out->put_byte((unsigned char) (major_type | 26)); + m_out->put_byte((unsigned char) (value >> 24)); + m_out->put_byte((unsigned char) (value >> 16)); + m_out->put_byte((unsigned char) (value >> 8)); + m_out->put_byte((unsigned char) value); } } void encoder::write_type_value(int major_type, unsigned long long value) { major_type <<= 5; if(value < 24ULL) { - _out->put_byte((unsigned char) (major_type | value)); + m_out->put_byte((unsigned char) (major_type | value)); } else if(value < 256ULL) { - _out->put_byte((unsigned char) (major_type | 24)); - _out->put_byte((unsigned char) value); + m_out->put_byte((unsigned char) (major_type | 24)); + m_out->put_byte((unsigned char) value); } else if(value < 65536ULL) { - _out->put_byte((unsigned char) (major_type | 25)); - _out->put_byte((unsigned char) (value >> 8)); - _out->put_byte((unsigned char) value); + m_out->put_byte((unsigned char) (major_type | 25)); + m_out->put_byte((unsigned char) (value >> 8)); + m_out->put_byte((unsigned char) value); } else if(value < 4294967296ULL) { - _out->put_byte((unsigned char) (major_type | 26)); - _out->put_byte((unsigned char) (value >> 24)); - _out->put_byte((unsigned char) (value >> 16)); - _out->put_byte((unsigned char) (value >> 8)); - _out->put_byte((unsigned char) value); + m_out->put_byte((unsigned char) (major_type | 26)); + m_out->put_byte((unsigned char) (value >> 24)); + m_out->put_byte((unsigned char) (value >> 16)); + m_out->put_byte((unsigned char) (value >> 8)); + m_out->put_byte((unsigned char) value); } else { - _out->put_byte((unsigned char) (major_type | 27)); - _out->put_byte((unsigned char) (value >> 56)); - _out->put_byte((unsigned char) (value >> 48)); - _out->put_byte((unsigned char) (value >> 40)); - _out->put_byte((unsigned char) (value >> 32)); - _out->put_byte((unsigned char) (value >> 24)); - _out->put_byte((unsigned char) (value >> 16)); - _out->put_byte((unsigned char) (value >> 8)); - _out->put_byte((unsigned char) value); + m_out->put_byte((unsigned char) (major_type | 27)); + m_out->put_byte((unsigned char) (value >> 56)); + m_out->put_byte((unsigned char) (value >> 48)); + m_out->put_byte((unsigned char) (value >> 40)); + m_out->put_byte((unsigned char) (value >> 32)); + m_out->put_byte((unsigned char) (value >> 24)); + m_out->put_byte((unsigned char) (value >> 16)); + m_out->put_byte((unsigned char) (value >> 8)); + m_out->put_byte((unsigned char) value); } } @@ -103,39 +103,39 @@ void encoder::write_int(int value) { void encoder::write_float(float value) { void* punny = &value; - _out->put_byte((unsigned char) (7<<5) | 26); - _out->put_byte(*((uint8_t*) punny+3)); - _out->put_byte(*((uint8_t*) punny+2)); - _out->put_byte(*((uint8_t*) punny+1)); - _out->put_byte(*((uint8_t*) punny+0)); + m_out->put_byte((unsigned char) (7<<5) | 26); + m_out->put_byte(*((uint8_t*) punny+3)); + m_out->put_byte(*((uint8_t*) punny+2)); + m_out->put_byte(*((uint8_t*) punny+1)); + m_out->put_byte(*((uint8_t*) punny+0)); } void encoder::write_double(double value) { void* punny = &value; - _out->put_byte((unsigned char) (7<<5) | 27); - _out->put_byte(*((uint8_t*) punny+7)); - _out->put_byte(*((uint8_t*) punny+6)); - _out->put_byte(*((uint8_t*) punny+5)); - _out->put_byte(*((uint8_t*) punny+4)); - _out->put_byte(*((uint8_t*) punny+3)); - _out->put_byte(*((uint8_t*) punny+2)); - _out->put_byte(*((uint8_t*) punny+1)); - _out->put_byte(*((uint8_t*) punny+0)); + m_out->put_byte((unsigned char) (7<<5) | 27); + m_out->put_byte(*((uint8_t*) punny+7)); + m_out->put_byte(*((uint8_t*) punny+6)); + m_out->put_byte(*((uint8_t*) punny+5)); + m_out->put_byte(*((uint8_t*) punny+4)); + m_out->put_byte(*((uint8_t*) punny+3)); + m_out->put_byte(*((uint8_t*) punny+2)); + m_out->put_byte(*((uint8_t*) punny+1)); + m_out->put_byte(*((uint8_t*) punny+0)); } void encoder::write_bytes(const unsigned char *data, unsigned int size) { write_type_value(2, size); - _out->put_bytes(data, size); + m_out->put_bytes(data, size); } void encoder::write_string(const char *data, unsigned int size) { write_type_value(3, size); - _out->put_bytes((const unsigned char *) data, size); + m_out->put_bytes((const unsigned char *) data, size); } void encoder::write_string(const std::string str) { write_type_value(3, (unsigned int) str.size()); - _out->put_bytes((const unsigned char *) str.c_str(), (int) str.size()); + m_out->put_bytes((const unsigned char *) str.c_str(), (int) str.size()); } @@ -157,16 +157,16 @@ void encoder::write_special(int special) { void encoder::write_bool(bool value) { if (value == true) { - _out->put_byte((unsigned char) 0xf5); + m_out->put_byte((unsigned char) 0xf5); } else { - _out->put_byte((unsigned char) 0xf4); + m_out->put_byte((unsigned char) 0xf4); } } void encoder::write_null() { - _out->put_byte((unsigned char) 0xf6); + m_out->put_byte((unsigned char) 0xf6); } void encoder::write_undefined() { - _out->put_byte((unsigned char) 0xf7); + m_out->put_byte((unsigned char) 0xf7); } diff --git a/src/encoder.h b/src/encoder.h index 6b41e1a..56357e8 100644 --- a/src/encoder.h +++ b/src/encoder.h @@ -24,7 +24,7 @@ namespace cbor { class encoder { private: - output *_out; + output *m_out; public: encoder(output &out); From 42bcb4410f7cb1c1b173e77d056e21a765816f57 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Tue, 20 Apr 2021 23:26:46 +0200 Subject: [PATCH 13/47] Refactor _bufer, _capacity and _offset --- src/input.cpp | 64 +++++++++++++++++++++--------------------- src/input.h | 2 +- src/output_dynamic.cpp | 32 ++++++++++----------- src/output_dynamic.h | 6 ++-- src/output_static.cpp | 22 +++++++-------- src/output_static.h | 6 ++-- 6 files changed, 66 insertions(+), 66 deletions(-) diff --git a/src/input.cpp b/src/input.cpp index ffd75df..bf1d6d0 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -25,7 +25,7 @@ using namespace cbor; input::input(void *data, int size) { _data = (unsigned char *)data; _size = size; - _offset = 0; + m_offset = 0; } input::~input() { @@ -33,66 +33,66 @@ input::~input() { } bool input::has_bytes(int count) { - return _size - _offset >= count; + return _size - m_offset >= count; } unsigned char input::get_byte() { - return _data[_offset++]; + return _data[m_offset++]; } unsigned short input::get_short() { - unsigned short value = ((unsigned short) _data[_offset] << 8) | ((unsigned short) _data[_offset + 1]); - _offset += 2; + unsigned short value = ((unsigned short) _data[m_offset] << 8) | ((unsigned short) _data[m_offset + 1]); + m_offset += 2; return value; } unsigned int input::get_int() { unsigned int value = \ - ((unsigned int) _data[_offset ] << 24) | - ((unsigned int) _data[_offset + 1] << 16) | - ((unsigned int) _data[_offset + 2] << 8 ) | - ((unsigned int) _data[_offset + 3]); - _offset += 4; + ((unsigned int) _data[m_offset ] << 24) | + ((unsigned int) _data[m_offset + 1] << 16) | + ((unsigned int) _data[m_offset + 2] << 8 ) | + ((unsigned int) _data[m_offset + 3]); + m_offset += 4; return value; } float input::get_float() { uint8_t value[4] = { - _data[_offset + 3], - _data[_offset + 2], - _data[_offset + 1], - _data[_offset + 0] + _data[m_offset + 3], + _data[m_offset + 2], + _data[m_offset + 1], + _data[m_offset + 0] }; - _offset += 4; + m_offset += 4; return *((float*) (&value[0])); } double input::get_double() { double ret; uint8_t* ptr = (uint8_t*)(void*) &ret; - *(ptr + 0) = _data[_offset + 7]; - *(ptr + 1) = _data[_offset + 6]; - *(ptr + 2) = _data[_offset + 5]; - *(ptr + 3) = _data[_offset + 4]; - *(ptr + 4) = _data[_offset + 3]; - *(ptr + 5) = _data[_offset + 2]; - *(ptr + 6) = _data[_offset + 1]; - *(ptr + 7) = _data[_offset + 0]; - _offset += 8; + *(ptr + 0) = _data[m_offset + 7]; + *(ptr + 1) = _data[m_offset + 6]; + *(ptr + 2) = _data[m_offset + 5]; + *(ptr + 3) = _data[m_offset + 4]; + *(ptr + 4) = _data[m_offset + 3]; + *(ptr + 5) = _data[m_offset + 2]; + *(ptr + 6) = _data[m_offset + 1]; + *(ptr + 7) = _data[m_offset + 0]; + m_offset += 8; return ret; } unsigned long long input::get_long() { - unsigned long long value = ((unsigned long long) _data[_offset] << 56) | - ((unsigned long long) _data[_offset +1] << 48) | ((unsigned long long) _data[_offset +2] << 40) | - ((unsigned long long) _data[_offset +3] << 32) | ((unsigned long long) _data[_offset +4] << 24) | - ((unsigned long long) _data[_offset +5] << 16) | ((unsigned long long) _data[_offset +6] << 8 ) | - ((unsigned long long) _data[_offset +7]); - _offset += 8; + unsigned long long value = ((unsigned long long) _data[m_offset] << 56) | + ((unsigned long long) _data[m_offset +1] << 48) | ((unsigned long long) _data[m_offset +2] << 40) | + ((unsigned long long) _data[m_offset +3] << 32) | ((unsigned long long) _data[m_offset +4] << 24) | + ((unsigned long long) _data[m_offset +5] << 16) | ((unsigned long long) _data[m_offset +6] << 8 ) | + ((unsigned long long) _data[m_offset +7]); + m_offset += 8; return value; } void input::get_bytes(void *to, int count) { - memcpy(to, _data + _offset, count); - _offset += count; + memcpy(to, _data + m_offset, count); + m_offset += count; } diff --git a/src/input.h b/src/input.h index 6cda2be..ee8111b 100644 --- a/src/input.h +++ b/src/input.h @@ -22,7 +22,7 @@ namespace cbor { private: unsigned char *_data; int _size; - int _offset; + int m_offset; public: input(void *data, int size); diff --git a/src/output_dynamic.cpp b/src/output_dynamic.cpp index 290b166..2fb98e1 100644 --- a/src/output_dynamic.cpp +++ b/src/output_dynamic.cpp @@ -23,9 +23,9 @@ using namespace cbor; void output_dynamic::init(unsigned int initalCapacity) { - this->_capacity = initalCapacity; - this->_buffer = new unsigned char[initalCapacity]; - this->_offset = 0; + this->m_capacity = initalCapacity; + this->m_buffer = new unsigned char[initalCapacity]; + this->m_offset = 0; } output_dynamic::output_dynamic() { @@ -37,33 +37,33 @@ output_dynamic::output_dynamic(unsigned int inital_capacity) { } output_dynamic::~output_dynamic() { - delete[] _buffer; + delete[] m_buffer; } unsigned char *output_dynamic::data() { - return _buffer; + return m_buffer; } unsigned int output_dynamic::size() { - return _offset; + return m_offset; } void output_dynamic::put_byte(unsigned char value) { - if(_offset < _capacity) { - _buffer[_offset++] = value; + if(m_offset < m_capacity) { + m_buffer[m_offset++] = value; } else { - _capacity *= 2; - _buffer = (unsigned char *) realloc(_buffer, _capacity); - _buffer[_offset++] = value; + m_capacity *= 2; + m_buffer = (unsigned char *) realloc(m_buffer, m_capacity); + m_buffer[m_offset++] = value; } } void output_dynamic::put_bytes(const unsigned char *data, int size) { - while(_offset + size > _capacity) { - _capacity *= 2; - _buffer = (unsigned char *) realloc(_buffer, _capacity); + while(m_offset + size > m_capacity) { + m_capacity *= 2; + m_buffer = (unsigned char *) realloc(m_buffer, m_capacity); } - memcpy(_buffer + _offset, data, size); - _offset += size; + memcpy(m_buffer + m_offset, data, size); + m_offset += size; } diff --git a/src/output_dynamic.h b/src/output_dynamic.h index 51c96e3..652397a 100644 --- a/src/output_dynamic.h +++ b/src/output_dynamic.h @@ -23,9 +23,9 @@ namespace cbor { class output_dynamic : public output { private: - unsigned char *_buffer; - unsigned int _capacity; - unsigned int _offset; + unsigned char *m_buffer; + unsigned int m_capacity; + unsigned int m_offset; public: output_dynamic(); diff --git a/src/output_static.cpp b/src/output_static.cpp index 98bac71..58ad4bd 100644 --- a/src/output_static.cpp +++ b/src/output_static.cpp @@ -22,36 +22,36 @@ using namespace cbor; output_static::output_static(unsigned int capacity) { - this->_capacity = capacity; - this->_buffer = new unsigned char[capacity]; - this->_offset = 0; + this->m_capacity = capacity; + this->m_buffer = new unsigned char[capacity]; + this->m_offset = 0; } output_static::~output_static() { - delete _buffer; + delete m_buffer; } void output_static::put_byte(unsigned char value) { - if(_offset < _capacity) { - _buffer[_offset++] = value; + if(m_offset < m_capacity) { + m_buffer[m_offset++] = value; } else { logger("buffer overflow error"); } } void output_static::put_bytes(const unsigned char *data, int size) { - if(_offset + size - 1 < _capacity) { - memcpy(_buffer + _offset, data, size); - _offset += size; + if(m_offset + size - 1 < m_capacity) { + memcpy(m_buffer + m_offset, data, size); + m_offset += size; } else { logger("buffer overflow error"); } } unsigned char *output_static::getData() { - return _buffer; + return m_buffer; } unsigned int output_static::getSize() { - return _offset; + return m_offset; } diff --git a/src/output_static.h b/src/output_static.h index 162ec0e..b885779 100644 --- a/src/output_static.h +++ b/src/output_static.h @@ -22,9 +22,9 @@ namespace cbor { class output_static : public output { private: - unsigned char *_buffer; - unsigned int _capacity; - unsigned int _offset; + unsigned char *m_buffer; + unsigned int m_capacity; + unsigned int m_offset; public: output_static(unsigned int capacity); From 6d11183450bc1bb3bfbba23c4cc6c9db306132de Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Tue, 20 Apr 2021 23:34:22 +0200 Subject: [PATCH 14/47] Make ctors with singl arg explicit --- src/decoder.h | 4 ++-- src/encoder.h | 2 +- src/input.h | 2 +- src/output_dynamic.h | 2 +- src/output_static.h | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/decoder.h b/src/decoder.h index abface9..2c05c0b 100644 --- a/src/decoder.h +++ b/src/decoder.h @@ -44,8 +44,8 @@ namespace cbor { decoder_state m_state; int m_currentLength; public: - decoder(input &in); - decoder(input &in, listener &listener); + explicit decoder(input &in); + explicit decoder(input &in, listener &listener); ~decoder(); void run(); void set_listener(listener &listener_instance); diff --git a/src/encoder.h b/src/encoder.h index 56357e8..01cf2dc 100644 --- a/src/encoder.h +++ b/src/encoder.h @@ -26,7 +26,7 @@ namespace cbor { private: output *m_out; public: - encoder(output &out); + explicit encoder(output &out); ~encoder(); diff --git a/src/input.h b/src/input.h index ee8111b..4e78a4d 100644 --- a/src/input.h +++ b/src/input.h @@ -24,7 +24,7 @@ namespace cbor { int _size; int m_offset; public: - input(void *data, int size); + explicit input(void *data, int size); ~input(); diff --git a/src/output_dynamic.h b/src/output_dynamic.h index 652397a..f4ef018 100644 --- a/src/output_dynamic.h +++ b/src/output_dynamic.h @@ -29,7 +29,7 @@ namespace cbor { public: output_dynamic(); - output_dynamic(unsigned int inital_capacity); + explicit output_dynamic(unsigned int inital_capacity); ~output_dynamic(); diff --git a/src/output_static.h b/src/output_static.h index b885779..19394d6 100644 --- a/src/output_static.h +++ b/src/output_static.h @@ -26,7 +26,7 @@ namespace cbor { unsigned int m_capacity; unsigned int m_offset; public: - output_static(unsigned int capacity); + explicit output_static(unsigned int capacity); ~output_static(); From 14f44141ed717faafa997991995de66833af3059 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Tue, 20 Apr 2021 23:40:17 +0200 Subject: [PATCH 15/47] Remove log.h --- src/decoder.cpp | 4 ++-- src/log.h | 26 --------------------- src/output_static.cpp | 54 ++++++++++++++++++++----------------------- 3 files changed, 27 insertions(+), 57 deletions(-) delete mode 100644 src/log.h diff --git a/src/decoder.cpp b/src/decoder.cpp index cbf4585..d3ab68a 100644 --- a/src/decoder.cpp +++ b/src/decoder.cpp @@ -15,9 +15,9 @@ */ #include "decoder.h" -#include "log.h" #include +#include using namespace cbor; @@ -417,7 +417,7 @@ void decoder::run() { } else if(m_state == decoder_state::error) { break; } else { - logger("UNKNOWN STATE"); + std::cerr << "UNKNOWN STATE"; } } } diff --git a/src/log.h b/src/log.h deleted file mode 100644 index 5f651df..0000000 --- a/src/log.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - Copyright 2014-2015 Stanislav Ovsyannikov - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -#ifndef LOG_H_ -#define LOG_H_ - -#include - -#define logger(line) fprintf(stderr, "%s:%d [%s]: %s\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, line) -#define loggerf(format, ...) fprintf(stderr, "%s:%d [%s]: " format "\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, __VA_ARGS__) - -#endif - diff --git a/src/output_static.cpp b/src/output_static.cpp index 58ad4bd..b553ba1 100644 --- a/src/output_static.cpp +++ b/src/output_static.cpp @@ -7,51 +7,47 @@ http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied. See the License for the specific language governing permissions and + limitations under the License. */ #include "output_static.h" -#include "log.h" #include +#include using namespace cbor; output_static::output_static(unsigned int capacity) { - this->m_capacity = capacity; - this->m_buffer = new unsigned char[capacity]; - this->m_offset = 0; + this->m_capacity = capacity; + this->m_buffer = new unsigned char[capacity]; + this->m_offset = 0; } -output_static::~output_static() { - delete m_buffer; -} +output_static::~output_static() { delete m_buffer; } void output_static::put_byte(unsigned char value) { - if(m_offset < m_capacity) { - m_buffer[m_offset++] = value; - } else { - logger("buffer overflow error"); - } + if (m_offset < m_capacity) { + m_buffer[m_offset++] = value; + } else { + std::cerr << "Buffer overflow error: " << m_offset << ">" << m_capacity + << '\n'; + } } void output_static::put_bytes(const unsigned char *data, int size) { - if(m_offset + size - 1 < m_capacity) { - memcpy(m_buffer + m_offset, data, size); - m_offset += size; - } else { - logger("buffer overflow error"); - } + if (m_offset + size - 1 < m_capacity) { + memcpy(m_buffer + m_offset, data, size); + m_offset += size; + } else { + std::cerr << "Buffer overflow error: " << m_offset << ">" << m_capacity + << '\n'; + } } -unsigned char *output_static::getData() { - return m_buffer; -} +unsigned char *output_static::getData() { return m_buffer; } -unsigned int output_static::getSize() { - return m_offset; -} +unsigned int output_static::getSize() { return m_offset; } From 4e8524b6e91ca8065659b506381ce9197bf6fbcf Mon Sep 17 00:00:00 2001 From: Oliver <56022454+Magolves@users.noreply.github.com> Date: Tue, 20 Apr 2021 23:44:17 +0200 Subject: [PATCH 16/47] Create cmake.yml Add CMake action --- .github/workflows/cmake.yml | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 .github/workflows/cmake.yml diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml new file mode 100644 index 0000000..005915e --- /dev/null +++ b/.github/workflows/cmake.yml @@ -0,0 +1,46 @@ +name: CMake + +on: [push] + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: Release + +jobs: + build: + # The CMake configure and build commands are platform agnostic and should work equally + # well on Windows or Mac. You can convert this to a matrix build if you need + # cross-platform coverage. + # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Create Build Environment + # Some projects don't allow in-source building, so create a separate build directory + # We'll use this as our working directory for all subsequent commands + run: cmake -E make_directory ${{github.workspace}}/build + + - name: Configure CMake + # Use a bash shell so we can use the same syntax for environment variable + # access regardless of the host operating system + shell: bash + working-directory: ${{github.workspace}}/build + # Note the current convention is to use the -S and -B options here to specify source + # and build directories, but this is only available with CMake 3.13 and higher. + # The CMake binaries on the Github Actions machines are (as of this writing) 3.12 + run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE + + - name: Build + working-directory: ${{github.workspace}}/build + shell: bash + # Execute the build. You can specify a specific target with "--target " + run: cmake --build . --config $BUILD_TYPE + + - name: Test + working-directory: ${{github.workspace}}/build + shell: bash + # Execute tests defined by the CMake configuration. + # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail + run: ctest -C $BUILD_TYPE From 3dae8b44843e903d084acd2669b872a10305e282 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Tue, 20 Apr 2021 23:53:10 +0200 Subject: [PATCH 17/47] Set C++ standard to 14, use cmake property for setting it --- CMakeLists.txt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1928589..9a494d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,14 @@ cmake_minimum_required(VERSION 3.15) project(cborcpp) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") set(CMAKE_BUILD_TYPE Release) +# On Mac OS 'CXX_STANDARD' is not evaluated +# Instead we have to set the compiler flag +if (APPLE) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") +endif() + set(SOURCE_FILES src/encoder.cpp src/decoder.cpp @@ -18,5 +23,6 @@ set(TEST_SOURCE_FILES ) add_library(cborcpp SHARED ${SOURCE_FILES}) +set_property(TARGET cborcpp PROPERTY CXX_STANDARD 14) add_executable(testing ${SOURCE_FILES} ${TEST_SOURCE_FILES}) target_include_directories(testing PRIVATE src) \ No newline at end of file From 541d8fe8e678f214d6b0384f1dc05968d306e980 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Tue, 20 Apr 2021 23:55:51 +0200 Subject: [PATCH 18/47] Export compile commands --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a494d3..1150e24 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.15) project(cborcpp) set(CMAKE_BUILD_TYPE Release) +set(EXPORT_COMPILE_COMMANDS ON) # On Mac OS 'CXX_STANDARD' is not evaluated # Instead we have to set the compiler flag From c6e77a7d94dd39a31baeb94a80c96f6557cd2f94 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Wed, 21 Apr 2021 00:14:43 +0200 Subject: [PATCH 19/47] Fix template decl acc. to stackoverflow --- src/listener.h | 78 ++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 41 deletions(-) diff --git a/src/listener.h b/src/listener.h index f20d7a5..c59a27c 100644 --- a/src/listener.h +++ b/src/listener.h @@ -7,70 +7,66 @@ http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied. See the License for the specific language governing permissions and + limitations under the License. */ #ifndef CBOR_CPP_LISTENER_H #define CBOR_CPP_LISTENER_H -#include #include +#include namespace cbor { - struct listener { - - virtual void on_integer(int value) const noexcept {} +struct listener { + + virtual void on_integer(int value) const noexcept {} + + virtual void on_float32(float value) const noexcept {} + virtual void on_double(double value) const noexcept {} - virtual void on_float32(float value) const noexcept {} - virtual void on_double(double value) const noexcept {} + virtual void on_bytes(unsigned char *data, int size) const noexcept {} - virtual void on_bytes(unsigned char *data, int size) const noexcept {} + virtual void on_string(std::string &str) const noexcept {} - virtual void on_string(std::string &str) const noexcept {} + virtual void on_array(int size) const noexcept {} - virtual void on_array(int size) const noexcept {} + virtual void on_map(int size) const noexcept {} - virtual void on_map(int size) const noexcept {} + virtual void on_tag(unsigned int tag) const noexcept {} - virtual void on_tag(unsigned int tag) const noexcept {} + virtual void on_special(unsigned int code) const noexcept {} - virtual void on_special(unsigned int code) const noexcept {} - - virtual void on_bool(bool) const noexcept {} - - virtual void on_null() const noexcept {} - - virtual void on_undefined() const noexcept {} + virtual void on_bool(bool) const noexcept {} - virtual void on_error(const char *error) const noexcept {} + virtual void on_null() const noexcept {} - virtual void on_extra_integer(unsigned long long value, int sign) const noexcept { - } + virtual void on_undefined() const noexcept {} - virtual void on_extra_tag(unsigned long long tag) const noexcept { - } + virtual void on_error(const char *error) const noexcept {} - virtual void on_extra_special(unsigned long long tag) const noexcept { - } + virtual void on_extra_integer(unsigned long long value, + int sign) const noexcept {} - protected: - template - void p(const std::string & tag, const T& value) const noexcept { - std::cout << "[" << tag << sizeof(T) << " = " << value << "]\n"; - } + virtual void on_extra_tag(unsigned long long tag) const noexcept {} - template<> - void p(const std::string & tag, const std::string& value) const noexcept { - std::cout << "[" << tag << value.length() << " = '" << value << "']\n"; - } - }; -} + virtual void on_extra_special(unsigned long long tag) const noexcept {} +protected: + template + void p(const std::string &tag, const T &value) const noexcept { + std::cout << "[" << tag << sizeof(T) << " = " << value << "]\n"; + } + template <> + void p(const std::string &tag, const std::string &value) const noexcept { + std::cout << "[" << tag << value.length() << " = '" << value << "']\n"; + } +}; +} // namespace cbor #endif // CBOR_CPP_LISTENER_H From 6552e1b002377cfb55d7f9db57d34f900a15ea4b Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Wed, 21 Apr 2021 00:31:06 +0200 Subject: [PATCH 20/47] Fixed template decl --- src/listener.h | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/listener.h b/src/listener.h index c59a27c..ebaffef 100644 --- a/src/listener.h +++ b/src/listener.h @@ -56,16 +56,14 @@ struct listener { virtual void on_extra_special(unsigned long long tag) const noexcept {} protected: - template - void p(const std::string &tag, const T &value) const noexcept { - std::cout << "[" << tag << sizeof(T) << " = " << value << "]\n"; - } - - template <> - void p(const std::string &tag, const std::string &value) const noexcept { - std::cout << "[" << tag << value.length() << " = '" << value << "']\n"; - } }; +template inline void p(const std::string &tag, const T &value) { + std::cout << "[" << tag << sizeof(T) << " = " << value << "]\n"; +} + +template <> inline void p(const std::string &tag, const std::string &value) { + std::cout << "[" << tag << value.length() << " = '" << value << "']\n"; +} } // namespace cbor From 9830d104b32eec00d2a31c062af240aa7258576f Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Wed, 21 Apr 2021 23:28:27 +0200 Subject: [PATCH 21/47] Replace guard clauses with pragma once --- src/cbor.h | 5 +---- src/decoder.h | 6 +----- src/encoder.h | 5 +---- src/input.h | 5 +---- src/listener.h | 5 +---- src/listener_debug.h | 5 +---- src/output.h | 5 +---- src/output_dynamic.h | 7 +------ src/output_static.h | 6 +----- 9 files changed, 9 insertions(+), 40 deletions(-) diff --git a/src/cbor.h b/src/cbor.h index 7b79c30..3c91fe2 100644 --- a/src/cbor.h +++ b/src/cbor.h @@ -14,8 +14,7 @@ limitations under the License. */ -#ifndef CBOR_CPP_CBOR_H -#define CBOR_CPP_CBOR_H +#pragma once #include "input.h" #include "encoder.h" @@ -24,5 +23,3 @@ #include "output_static.h" #include "output_dynamic.h" #include "listener_debug.h" - -#endif //CBOR_CPP_CBOR_H diff --git a/src/decoder.h b/src/decoder.h index 2c05c0b..86a5556 100644 --- a/src/decoder.h +++ b/src/decoder.h @@ -15,8 +15,7 @@ */ -#ifndef __CborDecoder_H_ -#define __CborDecoder_H_ +#pragma once #include "listener.h" #include "input.h" @@ -51,6 +50,3 @@ namespace cbor { void set_listener(listener &listener_instance); }; } - - -#endif //__CborDecoder_H_ diff --git a/src/encoder.h b/src/encoder.h index 01cf2dc..29bb970 100644 --- a/src/encoder.h +++ b/src/encoder.h @@ -15,8 +15,7 @@ */ -#ifndef __CborEncoder_H_ -#define __CborEncoder_H_ +#pragma once #include "output.h" #include @@ -68,5 +67,3 @@ namespace cbor { void write_type_value(int major_type, unsigned long long value); }; } - -#endif //__CborEncoder_H_ diff --git a/src/input.h b/src/input.h index 4e78a4d..cd42b84 100644 --- a/src/input.h +++ b/src/input.h @@ -14,8 +14,7 @@ limitations under the License. */ -#ifndef CBOR_CPP_INPUT_H -#define CBOR_CPP_INPUT_H +#pragma once namespace cbor { class input { @@ -43,5 +42,3 @@ namespace cbor { void get_bytes(void *to, int count); }; } - -#endif // CBOR_CPP_INPUT_H diff --git a/src/listener.h b/src/listener.h index ebaffef..e0e6b8e 100644 --- a/src/listener.h +++ b/src/listener.h @@ -14,8 +14,7 @@ limitations under the License. */ -#ifndef CBOR_CPP_LISTENER_H -#define CBOR_CPP_LISTENER_H +#pragma once #include #include @@ -66,5 +65,3 @@ template <> inline void p(const std::string &tag, const std::string &value) { } } // namespace cbor - -#endif // CBOR_CPP_LISTENER_H diff --git a/src/listener_debug.h b/src/listener_debug.h index 3076625..9fce61d 100644 --- a/src/listener_debug.h +++ b/src/listener_debug.h @@ -14,8 +14,7 @@ limitations under the License. */ -#ifndef __listener_debug_H_ -#define __listener_debug_H_ +#pragma once #include @@ -53,5 +52,3 @@ namespace cbor { virtual void on_extra_special(unsigned long long tag) const noexcept override; }; } - -#endif //__listener_debug_H_ diff --git a/src/output.h b/src/output.h index 0901d95..a6ca9bf 100644 --- a/src/output.h +++ b/src/output.h @@ -15,8 +15,7 @@ */ -#ifndef __CborOutput_H_ -#define __CborOutput_H_ +#pragma once namespace cbor { class output { @@ -30,5 +29,3 @@ namespace cbor { virtual void put_bytes(const unsigned char *data, int size) = 0; }; } - -#endif //__CborOutput_H_ diff --git a/src/output_dynamic.h b/src/output_dynamic.h index f4ef018..4ec5d0d 100644 --- a/src/output_dynamic.h +++ b/src/output_dynamic.h @@ -14,8 +14,7 @@ limitations under the License. */ -#ifndef __CborDynamicOutput_H_ -#define __CborDynamicOutput_H_ +#pragma once #include "output.h" @@ -45,7 +44,3 @@ namespace cbor { void init(unsigned int initalCapacity); }; } - - - -#endif //__CborDynamicOutput_H_ diff --git a/src/output_static.h b/src/output_static.h index 19394d6..0c7dc27 100644 --- a/src/output_static.h +++ b/src/output_static.h @@ -14,8 +14,7 @@ limitations under the License. */ -#ifndef __CborStaticOutput_H_ -#define __CborStaticOutput_H_ +#pragma once #include "output.h" @@ -39,6 +38,3 @@ namespace cbor { virtual void put_bytes(const unsigned char *data, int size); }; } - - -#endif //__CborStaticOutput_H_ From f178e6f3aba87e42bb4e310d3a0aa2d318a0b980 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Thu, 22 Apr 2021 23:51:26 +0200 Subject: [PATCH 22/47] Make listener shared pointer --- src/decoder.cpp | 9 +++++---- src/decoder.h | 8 +++++--- test/tests.cpp | 4 ++-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/decoder.cpp b/src/decoder.cpp index d3ab68a..1c456a5 100644 --- a/src/decoder.cpp +++ b/src/decoder.cpp @@ -26,9 +26,9 @@ decoder::decoder(input &in) { m_state = decoder_state::type; } -decoder::decoder(input &in, listener &listener) { +decoder::decoder(input &in, listener_ptr listener) { m_in = ∈ - m_listener = &listener; + m_listener = listener; m_state = decoder_state::type; } @@ -36,12 +36,13 @@ decoder::~decoder() { } -void decoder::set_listener(listener &listener_instance) { - m_listener = &listener_instance; +void decoder::set_listener(listener_ptr listener) { + m_listener = listener; } void decoder::run() { unsigned int temp; + while(1) { if(m_state == decoder_state::type) { if(m_in->has_bytes(1)) { diff --git a/src/decoder.h b/src/decoder.h index 86a5556..f3c734c 100644 --- a/src/decoder.h +++ b/src/decoder.h @@ -21,6 +21,8 @@ #include "input.h" namespace cbor { + using listener_ptr = std::shared_ptr; + enum class decoder_state : uint8_t { type, pint, @@ -38,15 +40,15 @@ namespace cbor { class decoder { private: - listener *m_listener; + listener_ptr m_listener; input *m_in; decoder_state m_state; int m_currentLength; public: explicit decoder(input &in); - explicit decoder(input &in, listener &listener); + explicit decoder(input &in, listener_ptr listener); ~decoder(); void run(); - void set_listener(listener &listener_instance); + void set_listener(listener_ptr listener); }; } diff --git a/test/tests.cpp b/test/tests.cpp index 0d0d768..e15ab67 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -39,8 +39,8 @@ int main() { { // decoding cbor::input input(output.data(), output.size()); - cbor::listener_debug listener; - cbor::decoder decoder(input, listener); + auto lp = std::make_shared(); + cbor::decoder decoder(input, std::move(lp)); decoder.run(); } From 5de7f03d7cea774ea11a060037334cf4a7d3a1db Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 00:05:28 +0200 Subject: [PATCH 23/47] Use value semantics for decode, remove unused members --- src/decoder.cpp | 162 +++++++++++++++++++++--------------------------- src/decoder.h | 15 +---- src/input.h | 4 ++ src/listener.h | 5 ++ test/tests.cpp | 6 +- 5 files changed, 87 insertions(+), 105 deletions(-) diff --git a/src/decoder.cpp b/src/decoder.cpp index 1c456a5..a25efff 100644 --- a/src/decoder.cpp +++ b/src/decoder.cpp @@ -21,39 +21,21 @@ using namespace cbor; -decoder::decoder(input &in) { - m_in = ∈ - m_state = decoder_state::type; -} - -decoder::decoder(input &in, listener_ptr listener) { - m_in = ∈ - m_listener = listener; - m_state = decoder_state::type; -} - -decoder::~decoder() { - -} - -void decoder::set_listener(listener_ptr listener) { - m_listener = listener; -} - -void decoder::run() { +void decoder::run(input& input, listener& listener) { unsigned int temp; + int m_currentLength {}; while(1) { if(m_state == decoder_state::type) { - if(m_in->has_bytes(1)) { - unsigned char type = m_in->get_byte(); + if(input.has_bytes(1)) { + unsigned char type = input.get_byte(); unsigned char majorType = type >> 5; unsigned char minorType = (unsigned char) (type & 31); switch(majorType) { case 0: // positive integer if(minorType < 24) { - m_listener->on_integer(minorType); + listener.on_integer(minorType); } else if(minorType == 24) { // 1 byte m_currentLength = 1; m_state = decoder_state::pint; @@ -68,12 +50,12 @@ void decoder::run() { m_state = decoder_state::pint; } else { m_state = decoder_state::error; - m_listener->on_error("invalid integer type"); + listener.on_error("invalid integer type"); } break; case 1: // negative integer if(minorType < 24) { - m_listener->on_integer(-1 -minorType); + listener.on_integer(-1 -minorType); } else if(minorType == 24) { // 1 byte m_currentLength = 1; m_state = decoder_state::nint; @@ -88,7 +70,7 @@ void decoder::run() { m_state = decoder_state::nint; } else { m_state = decoder_state::error; - m_listener->on_error("invalid integer type"); + listener.on_error("invalid integer type"); } break; case 2: // bytes @@ -109,7 +91,7 @@ void decoder::run() { m_state = decoder_state::bytes_size; } else { m_state = decoder_state::error; - m_listener->on_error("invalid bytes type"); + listener.on_error("invalid bytes type"); } break; case 3: // string @@ -130,12 +112,12 @@ void decoder::run() { m_state = decoder_state::string_size; } else { m_state = decoder_state::error; - m_listener->on_error("invalid string type"); + listener.on_error("invalid string type"); } break; case 4: // array if(minorType < 24) { - m_listener->on_array(minorType); + listener.on_array(minorType); } else if(minorType == 24) { m_state = decoder_state::array; m_currentLength = 1; @@ -150,12 +132,12 @@ void decoder::run() { m_state = decoder_state::array; } else { m_state = decoder_state::error; - m_listener->on_error("invalid array type"); + listener.on_error("invalid array type"); } break; case 5: // map if(minorType < 24) { - m_listener->on_map(minorType); + listener.on_map(minorType); } else if(minorType == 24) { m_state = decoder_state::map; m_currentLength = 1; @@ -170,12 +152,12 @@ void decoder::run() { m_state = decoder_state::map; } else { m_state = decoder_state::error; - m_listener->on_error("invalid array type"); + listener.on_error("invalid array type"); } break; case 6: // tag if(minorType < 24) { - m_listener->on_tag(minorType); + listener.on_tag(minorType); } else if(minorType == 24) { m_state = decoder_state::tag; m_currentLength = 1; @@ -190,20 +172,20 @@ void decoder::run() { m_state = decoder_state::tag; } else { m_state = decoder_state::error; - m_listener->on_error("invalid tag type"); + listener.on_error("invalid tag type"); } break; case 7: // special if (minorType < 20) { - m_listener->on_special(minorType); + listener.on_special(minorType); } else if (minorType == 20) { - m_listener->on_bool(false); + listener.on_bool(false); } else if (minorType == 21) { - m_listener->on_bool(true); + listener.on_bool(true); } else if (minorType == 22) { - m_listener->on_null(); + listener.on_null(); } else if (minorType == 23) { - m_listener->on_undefined(); + listener.on_undefined(); } else if(minorType == 24) { m_state = decoder_state::special; m_currentLength = 1; @@ -218,199 +200,199 @@ void decoder::run() { m_state = decoder_state::special; } else { m_state = decoder_state::error; - m_listener->on_error("invalid special type"); + listener.on_error("invalid special type"); } break; } } else break; } else if(m_state == decoder_state::pint) { - if(m_in->has_bytes(m_currentLength)) { + if(input.has_bytes(m_currentLength)) { switch(m_currentLength) { case 1: - m_listener->on_integer(m_in->get_byte()); + listener.on_integer(input.get_byte()); m_state = decoder_state::type; break; case 2: - m_listener->on_integer(m_in->get_short()); + listener.on_integer(input.get_short()); m_state = decoder_state::type; break; case 4: - temp = m_in->get_int(); + temp = input.get_int(); if(temp <= INT_MAX) { - m_listener->on_integer(temp); + listener.on_integer(temp); } else { - m_listener->on_extra_integer(temp, 1); + listener.on_extra_integer(temp, 1); } m_state = decoder_state::type; break; case 8: - m_listener->on_extra_integer(m_in->get_long(), 1); + listener.on_extra_integer(input.get_long(), 1); m_state = decoder_state::type; break; } } else break; } else if(m_state == decoder_state::nint) { - if(m_in->has_bytes(m_currentLength)) { + if(input.has_bytes(m_currentLength)) { switch(m_currentLength) { case 1: - m_listener->on_integer(-(int) m_in->get_byte() - 1); + listener.on_integer(-(int) input.get_byte() - 1); m_state = decoder_state::type; break; case 2: - m_listener->on_integer(-(int) m_in->get_short() - 1); + listener.on_integer(-(int) input.get_short() - 1); m_state = decoder_state::type; break; case 4: - temp = m_in->get_int(); + temp = input.get_int(); if(temp <= INT_MAX) { - m_listener->on_integer(-(int) temp - 1); + listener.on_integer(-(int) temp - 1); } else { - m_listener->on_extra_integer(temp + 1, -1); + listener.on_extra_integer(temp + 1, -1); } m_state = decoder_state::type; break; case 8: - m_listener->on_extra_integer(m_in->get_long() + 1, -1); + listener.on_extra_integer(input.get_long() + 1, -1); break; } } else break; } else if(m_state == decoder_state::bytes_size) { - if(m_in->has_bytes(m_currentLength)) { + if(input.has_bytes(m_currentLength)) { switch(m_currentLength) { case 1: - m_currentLength = m_in->get_byte(); + m_currentLength = input.get_byte(); m_state = decoder_state::bytes_data; break; case 2: - m_currentLength = m_in->get_short(); + m_currentLength = input.get_short(); m_state = decoder_state::bytes_data; break; case 4: - m_currentLength = m_in->get_int(); + m_currentLength = input.get_int(); m_state = decoder_state::bytes_data; break; case 8: m_state = decoder_state::error; - m_listener->on_error("extra long bytes"); + listener.on_error("extra long bytes"); break; } } else break; } else if(m_state == decoder_state::bytes_data) { - if(m_in->has_bytes(m_currentLength)) { + if(input.has_bytes(m_currentLength)) { unsigned char *data = new unsigned char[m_currentLength]; - m_in->get_bytes(data, m_currentLength); + input.get_bytes(data, m_currentLength); m_state = decoder_state::type; - m_listener->on_bytes(data, m_currentLength); + listener.on_bytes(data, m_currentLength); } else break; } else if(m_state == decoder_state::string_size) { - if(m_in->has_bytes(m_currentLength)) { + if(input.has_bytes(m_currentLength)) { switch(m_currentLength) { case 1: - m_currentLength = m_in->get_byte(); + m_currentLength = input.get_byte(); m_state = decoder_state::string_data; break; case 2: - m_currentLength = m_in->get_short(); + m_currentLength = input.get_short(); m_state = decoder_state::string_data; break; case 4: - m_currentLength = m_in->get_int(); + m_currentLength = input.get_int(); m_state = decoder_state::string_data; break; case 8: m_state = decoder_state::error; - m_listener->on_error("extra long array"); + listener.on_error("extra long array"); break; } } else break; } else if(m_state == decoder_state::string_data) { - if(m_in->has_bytes(m_currentLength)) { + if(input.has_bytes(m_currentLength)) { unsigned char *data = new unsigned char[m_currentLength]; - m_in->get_bytes(data, m_currentLength); + input.get_bytes(data, m_currentLength); m_state = decoder_state::type; std::string str((const char *)data, (size_t)m_currentLength); - m_listener->on_string(str); + listener.on_string(str); } else break; } else if(m_state == decoder_state::array) { - if(m_in->has_bytes(m_currentLength)) { + if(input.has_bytes(m_currentLength)) { switch(m_currentLength) { case 1: - m_listener->on_array(m_in->get_byte()); + listener.on_array(input.get_byte()); m_state = decoder_state::type; break; case 2: - m_listener->on_array(m_currentLength = m_in->get_short()); + listener.on_array(m_currentLength = input.get_short()); m_state = decoder_state::type; break; case 4: - m_listener->on_array(m_in->get_int()); + listener.on_array(input.get_int()); m_state = decoder_state::type; break; case 8: m_state = decoder_state::error; - m_listener->on_error("extra long array"); + listener.on_error("extra long array"); break; } } else break; } else if(m_state == decoder_state::map) { - if(m_in->has_bytes(m_currentLength)) { + if(input.has_bytes(m_currentLength)) { switch(m_currentLength) { case 1: - m_listener->on_map(m_in->get_byte()); + listener.on_map(input.get_byte()); m_state = decoder_state::type; break; case 2: - m_listener->on_map(m_currentLength = m_in->get_short()); + listener.on_map(m_currentLength = input.get_short()); m_state = decoder_state::type; break; case 4: - m_listener->on_map(m_in->get_int()); + listener.on_map(input.get_int()); m_state = decoder_state::type; break; case 8: m_state = decoder_state::error; - m_listener->on_error("extra long map"); + listener.on_error("extra long map"); break; } } else break; } else if(m_state == decoder_state::tag) { - if(m_in->has_bytes(m_currentLength)) { + if(input.has_bytes(m_currentLength)) { switch(m_currentLength) { case 1: - m_listener->on_tag(m_in->get_byte()); + listener.on_tag(input.get_byte()); m_state = decoder_state::type; break; case 2: - m_listener->on_tag(m_in->get_short()); + listener.on_tag(input.get_short()); m_state = decoder_state::type; break; case 4: - m_listener->on_tag(m_in->get_int()); + listener.on_tag(input.get_int()); m_state = decoder_state::type; break; case 8: - m_listener->on_extra_tag(m_in->get_long()); + listener.on_extra_tag(input.get_long()); m_state = decoder_state::type; break; } } else break; } else if(m_state == decoder_state::special) { - if (m_in->has_bytes(m_currentLength)) { + if (input.has_bytes(m_currentLength)) { switch (m_currentLength) { case 1: - m_listener->on_special(m_in->get_byte()); + listener.on_special(input.get_byte()); m_state = decoder_state::type; break; case 2: - m_listener->on_special(m_in->get_short()); + listener.on_special(input.get_short()); m_state = decoder_state::type; break; case 4: - m_listener->on_special(m_in->get_int()); + listener.on_special(input.get_int()); m_state = decoder_state::type; break; case 8: - m_listener->on_extra_special(m_in->get_long()); + listener.on_extra_special(input.get_long()); m_state = decoder_state::type; break; } diff --git a/src/decoder.h b/src/decoder.h index f3c734c..ecd9ac1 100644 --- a/src/decoder.h +++ b/src/decoder.h @@ -21,8 +21,7 @@ #include "input.h" namespace cbor { - using listener_ptr = std::shared_ptr; - + enum class decoder_state : uint8_t { type, pint, @@ -38,17 +37,9 @@ namespace cbor { error }; - class decoder { + struct decoder { + void run(input& input, listener& listener); private: - listener_ptr m_listener; - input *m_in; decoder_state m_state; - int m_currentLength; - public: - explicit decoder(input &in); - explicit decoder(input &in, listener_ptr listener); - ~decoder(); - void run(); - void set_listener(listener_ptr listener); }; } diff --git a/src/input.h b/src/input.h index cd42b84..6dea9de 100644 --- a/src/input.h +++ b/src/input.h @@ -16,7 +16,11 @@ #pragma once +#include + namespace cbor { + + class input { private: unsigned char *_data; diff --git a/src/listener.h b/src/listener.h index e0e6b8e..4b933f2 100644 --- a/src/listener.h +++ b/src/listener.h @@ -20,6 +20,7 @@ #include namespace cbor { + struct listener { virtual void on_integer(int value) const noexcept {} @@ -56,6 +57,10 @@ struct listener { protected: }; + +using listener_ptr = std::shared_ptr; + + template inline void p(const std::string &tag, const T &value) { std::cout << "[" << tag << sizeof(T) << " = " << value << "]\n"; } diff --git a/test/tests.cpp b/test/tests.cpp index e15ab67..d55837a 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -39,9 +39,9 @@ int main() { { // decoding cbor::input input(output.data(), output.size()); - auto lp = std::make_shared(); - cbor::decoder decoder(input, std::move(lp)); - decoder.run(); + cbor::listener_debug listener; + cbor::decoder decoder; + decoder.run(input, listener); } return 0; From 554a5bf0a0b0fb5ffee9ba5b6f9d6168338e9ae7 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 00:19:31 +0200 Subject: [PATCH 24/47] Remove listener_ptr --- src/listener.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/listener.h b/src/listener.h index 4b933f2..0c0cf81 100644 --- a/src/listener.h +++ b/src/listener.h @@ -58,9 +58,6 @@ struct listener { protected: }; -using listener_ptr = std::shared_ptr; - - template inline void p(const std::string &tag, const T &value) { std::cout << "[" << tag << sizeof(T) << " = " << value << "]\n"; } From 608db33f2b164533679cbc47b0b56f9301e751d5 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 00:42:53 +0200 Subject: [PATCH 25/47] Replace static/dynamic outpiut by common impl using a std::vector --- CMakeLists.txt | 3 +- src/cbor.h | 3 +- src/{output_static.h => output.cpp} | 35 ++++++++------- src/output.h | 24 +++++++--- src/output_dynamic.cpp | 69 ----------------------------- src/output_dynamic.h | 46 ------------------- src/output_static.cpp | 53 ---------------------- test/tests.cpp | 2 +- 8 files changed, 39 insertions(+), 196 deletions(-) rename src/{output_static.h => output.cpp} (56%) delete mode 100644 src/output_dynamic.cpp delete mode 100644 src/output_dynamic.h delete mode 100644 src/output_static.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1150e24..06ae27b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,8 +15,7 @@ set(SOURCE_FILES src/decoder.cpp src/input.cpp src/listener_debug.cpp - src/output_dynamic.cpp - src/output_static.cpp + src/output.cpp ) set(TEST_SOURCE_FILES diff --git a/src/cbor.h b/src/cbor.h index 3c91fe2..aa5635a 100644 --- a/src/cbor.h +++ b/src/cbor.h @@ -17,9 +17,8 @@ #pragma once #include "input.h" +#include "output.h" #include "encoder.h" #include "decoder.h" #include "listener.h" -#include "output_static.h" -#include "output_dynamic.h" #include "listener_debug.h" diff --git a/src/output_static.h b/src/output.cpp similarity index 56% rename from src/output_static.h rename to src/output.cpp index 0c7dc27..360abef 100644 --- a/src/output_static.h +++ b/src/output.cpp @@ -14,27 +14,30 @@ limitations under the License. */ -#pragma once - #include "output.h" -namespace cbor { - class output_static : public output { - private: - unsigned char *m_buffer; - unsigned int m_capacity; - unsigned int m_offset; - public: - explicit output_static(unsigned int capacity); +#include +#include + +using namespace cbor; - ~output_static(); +output::output(unsigned int initalCapacity) : m_buffer(initalCapacity) { +} - virtual unsigned char *getData(); +uint8_t *output::data() { + return m_buffer.data(); +} - virtual unsigned int getSize(); +unsigned int output::size() { + return m_buffer.size(); +} - virtual void put_byte(unsigned char value); +void output::put_byte(uint8_t value) { + m_buffer.push_back(value); +} - virtual void put_bytes(const unsigned char *data, int size); - }; +void output::put_bytes(const uint8_t *data, int size) { + for(auto i = 0; i < size; i++) { + m_buffer.push_back(data[i]); + } } diff --git a/src/output.h b/src/output.h index a6ca9bf..4237163 100644 --- a/src/output.h +++ b/src/output.h @@ -14,18 +14,28 @@ limitations under the License. */ - #pragma once +#include +#include + +#include "output.h" + namespace cbor { - class output { - public: - virtual unsigned char *data() = 0; + const int default_capacity = 256; + + struct output { + explicit output(unsigned int inital_capacity = default_capacity); + + uint8_t *data(); + + unsigned int size(); - virtual unsigned int size() = 0; + void put_byte(uint8_t value); - virtual void put_byte(unsigned char value) = 0; + void put_bytes(const uint8_t *data, int size); - virtual void put_bytes(const unsigned char *data, int size) = 0; + private: + std::vector m_buffer; }; } diff --git a/src/output_dynamic.cpp b/src/output_dynamic.cpp deleted file mode 100644 index 2fb98e1..0000000 --- a/src/output_dynamic.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/* - Copyright 2014-2015 Stanislav Ovsyannikov - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -#include "output_dynamic.h" - -#include -#include - -using namespace cbor; - - -void output_dynamic::init(unsigned int initalCapacity) { - this->m_capacity = initalCapacity; - this->m_buffer = new unsigned char[initalCapacity]; - this->m_offset = 0; -} - -output_dynamic::output_dynamic() { - init(256); -} - -output_dynamic::output_dynamic(unsigned int inital_capacity) { - init(inital_capacity); -} - -output_dynamic::~output_dynamic() { - delete[] m_buffer; -} - -unsigned char *output_dynamic::data() { - return m_buffer; -} - -unsigned int output_dynamic::size() { - return m_offset; -} - -void output_dynamic::put_byte(unsigned char value) { - if(m_offset < m_capacity) { - m_buffer[m_offset++] = value; - } else { - m_capacity *= 2; - m_buffer = (unsigned char *) realloc(m_buffer, m_capacity); - m_buffer[m_offset++] = value; - } -} - -void output_dynamic::put_bytes(const unsigned char *data, int size) { - while(m_offset + size > m_capacity) { - m_capacity *= 2; - m_buffer = (unsigned char *) realloc(m_buffer, m_capacity); - } - - memcpy(m_buffer + m_offset, data, size); - m_offset += size; -} diff --git a/src/output_dynamic.h b/src/output_dynamic.h deleted file mode 100644 index 4ec5d0d..0000000 --- a/src/output_dynamic.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - Copyright 2014-2015 Stanislav Ovsyannikov - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -#pragma once - - -#include "output.h" - -namespace cbor { - class output_dynamic : public output { - private: - unsigned char *m_buffer; - unsigned int m_capacity; - unsigned int m_offset; - public: - output_dynamic(); - - explicit output_dynamic(unsigned int inital_capacity); - - ~output_dynamic(); - - virtual unsigned char *data(); - - virtual unsigned int size(); - - virtual void put_byte(unsigned char value); - - virtual void put_bytes(const unsigned char *data, int size); - - private: - void init(unsigned int initalCapacity); - }; -} diff --git a/src/output_static.cpp b/src/output_static.cpp deleted file mode 100644 index b553ba1..0000000 --- a/src/output_static.cpp +++ /dev/null @@ -1,53 +0,0 @@ -/* - Copyright 2014-2015 Stanislav Ovsyannikov - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied. See the License for the specific language governing permissions and - limitations under the License. -*/ - -#include "output_static.h" - -#include -#include - -using namespace cbor; - -output_static::output_static(unsigned int capacity) { - this->m_capacity = capacity; - this->m_buffer = new unsigned char[capacity]; - this->m_offset = 0; -} - -output_static::~output_static() { delete m_buffer; } - -void output_static::put_byte(unsigned char value) { - if (m_offset < m_capacity) { - m_buffer[m_offset++] = value; - } else { - std::cerr << "Buffer overflow error: " << m_offset << ">" << m_capacity - << '\n'; - } -} - -void output_static::put_bytes(const unsigned char *data, int size) { - if (m_offset + size - 1 < m_capacity) { - memcpy(m_buffer + m_offset, data, size); - m_offset += size; - } else { - std::cerr << "Buffer overflow error: " << m_offset << ">" << m_capacity - << '\n'; - } -} - -unsigned char *output_static::getData() { return m_buffer; } - -unsigned int output_static::getSize() { return m_offset; } diff --git a/test/tests.cpp b/test/tests.cpp index d55837a..d4a19c7 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -19,7 +19,7 @@ #include "cbor.h" int main() { - cbor::output_dynamic output; + cbor::output output; { //encoding cbor::encoder encoder(output); From b470058eda51a3e1d6bf810b567a53a51439e023 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 00:49:59 +0200 Subject: [PATCH 26/47] unsigned char -> unit8_t --- src/decoder.cpp | 10 +++--- src/encoder.cpp | 80 +++++++++++++++++++++--------------------- src/encoder.h | 2 +- src/input.cpp | 4 +-- src/input.h | 4 +-- src/listener.h | 2 +- src/listener_debug.cpp | 2 +- src/listener_debug.h | 2 +- 8 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/decoder.cpp b/src/decoder.cpp index a25efff..580e8ad 100644 --- a/src/decoder.cpp +++ b/src/decoder.cpp @@ -28,9 +28,9 @@ void decoder::run(input& input, listener& listener) { while(1) { if(m_state == decoder_state::type) { if(input.has_bytes(1)) { - unsigned char type = input.get_byte(); - unsigned char majorType = type >> 5; - unsigned char minorType = (unsigned char) (type & 31); + uint8_t type = input.get_byte(); + uint8_t majorType = type >> 5; + uint8_t minorType = (uint8_t) (type & 31); switch(majorType) { case 0: // positive integer @@ -279,7 +279,7 @@ void decoder::run(input& input, listener& listener) { } else break; } else if(m_state == decoder_state::bytes_data) { if(input.has_bytes(m_currentLength)) { - unsigned char *data = new unsigned char[m_currentLength]; + uint8_t *data = new uint8_t[m_currentLength]; input.get_bytes(data, m_currentLength); m_state = decoder_state::type; listener.on_bytes(data, m_currentLength); @@ -307,7 +307,7 @@ void decoder::run(input& input, listener& listener) { } else break; } else if(m_state == decoder_state::string_data) { if(input.has_bytes(m_currentLength)) { - unsigned char *data = new unsigned char[m_currentLength]; + uint8_t *data = new uint8_t[m_currentLength]; input.get_bytes(data, m_currentLength); m_state = decoder_state::type; std::string str((const char *)data, (size_t)m_currentLength); diff --git a/src/encoder.cpp b/src/encoder.cpp index 9484948..019d204 100644 --- a/src/encoder.cpp +++ b/src/encoder.cpp @@ -30,50 +30,50 @@ encoder::~encoder() { void encoder::write_type_value(int major_type, unsigned int value) { major_type <<= 5; if(value < 24) { - m_out->put_byte((unsigned char) (major_type | value)); + m_out->put_byte((uint8_t) (major_type | value)); } else if(value < 256) { - m_out->put_byte((unsigned char) (major_type | 24)); - m_out->put_byte((unsigned char) value); + m_out->put_byte((uint8_t) (major_type | 24)); + m_out->put_byte((uint8_t) value); } else if(value < 65536) { - m_out->put_byte((unsigned char) (major_type | 25)); - m_out->put_byte((unsigned char) (value >> 8)); - m_out->put_byte((unsigned char) value); + m_out->put_byte((uint8_t) (major_type | 25)); + m_out->put_byte((uint8_t) (value >> 8)); + m_out->put_byte((uint8_t) value); } else { - m_out->put_byte((unsigned char) (major_type | 26)); - m_out->put_byte((unsigned char) (value >> 24)); - m_out->put_byte((unsigned char) (value >> 16)); - m_out->put_byte((unsigned char) (value >> 8)); - m_out->put_byte((unsigned char) value); + m_out->put_byte((uint8_t) (major_type | 26)); + m_out->put_byte((uint8_t) (value >> 24)); + m_out->put_byte((uint8_t) (value >> 16)); + m_out->put_byte((uint8_t) (value >> 8)); + m_out->put_byte((uint8_t) value); } } void encoder::write_type_value(int major_type, unsigned long long value) { major_type <<= 5; if(value < 24ULL) { - m_out->put_byte((unsigned char) (major_type | value)); + m_out->put_byte((uint8_t) (major_type | value)); } else if(value < 256ULL) { - m_out->put_byte((unsigned char) (major_type | 24)); - m_out->put_byte((unsigned char) value); + m_out->put_byte((uint8_t) (major_type | 24)); + m_out->put_byte((uint8_t) value); } else if(value < 65536ULL) { - m_out->put_byte((unsigned char) (major_type | 25)); - m_out->put_byte((unsigned char) (value >> 8)); - m_out->put_byte((unsigned char) value); + m_out->put_byte((uint8_t) (major_type | 25)); + m_out->put_byte((uint8_t) (value >> 8)); + m_out->put_byte((uint8_t) value); } else if(value < 4294967296ULL) { - m_out->put_byte((unsigned char) (major_type | 26)); - m_out->put_byte((unsigned char) (value >> 24)); - m_out->put_byte((unsigned char) (value >> 16)); - m_out->put_byte((unsigned char) (value >> 8)); - m_out->put_byte((unsigned char) value); + m_out->put_byte((uint8_t) (major_type | 26)); + m_out->put_byte((uint8_t) (value >> 24)); + m_out->put_byte((uint8_t) (value >> 16)); + m_out->put_byte((uint8_t) (value >> 8)); + m_out->put_byte((uint8_t) value); } else { - m_out->put_byte((unsigned char) (major_type | 27)); - m_out->put_byte((unsigned char) (value >> 56)); - m_out->put_byte((unsigned char) (value >> 48)); - m_out->put_byte((unsigned char) (value >> 40)); - m_out->put_byte((unsigned char) (value >> 32)); - m_out->put_byte((unsigned char) (value >> 24)); - m_out->put_byte((unsigned char) (value >> 16)); - m_out->put_byte((unsigned char) (value >> 8)); - m_out->put_byte((unsigned char) value); + m_out->put_byte((uint8_t) (major_type | 27)); + m_out->put_byte((uint8_t) (value >> 56)); + m_out->put_byte((uint8_t) (value >> 48)); + m_out->put_byte((uint8_t) (value >> 40)); + m_out->put_byte((uint8_t) (value >> 32)); + m_out->put_byte((uint8_t) (value >> 24)); + m_out->put_byte((uint8_t) (value >> 16)); + m_out->put_byte((uint8_t) (value >> 8)); + m_out->put_byte((uint8_t) value); } } @@ -103,7 +103,7 @@ void encoder::write_int(int value) { void encoder::write_float(float value) { void* punny = &value; - m_out->put_byte((unsigned char) (7<<5) | 26); + m_out->put_byte((uint8_t) (7<<5) | 26); m_out->put_byte(*((uint8_t*) punny+3)); m_out->put_byte(*((uint8_t*) punny+2)); m_out->put_byte(*((uint8_t*) punny+1)); @@ -112,7 +112,7 @@ void encoder::write_float(float value) { void encoder::write_double(double value) { void* punny = &value; - m_out->put_byte((unsigned char) (7<<5) | 27); + m_out->put_byte((uint8_t) (7<<5) | 27); m_out->put_byte(*((uint8_t*) punny+7)); m_out->put_byte(*((uint8_t*) punny+6)); m_out->put_byte(*((uint8_t*) punny+5)); @@ -123,19 +123,19 @@ void encoder::write_double(double value) { m_out->put_byte(*((uint8_t*) punny+0)); } -void encoder::write_bytes(const unsigned char *data, unsigned int size) { +void encoder::write_bytes(const uint8_t *data, unsigned int size) { write_type_value(2, size); m_out->put_bytes(data, size); } void encoder::write_string(const char *data, unsigned int size) { write_type_value(3, size); - m_out->put_bytes((const unsigned char *) data, size); + m_out->put_bytes((const uint8_t *) data, size); } void encoder::write_string(const std::string str) { write_type_value(3, (unsigned int) str.size()); - m_out->put_bytes((const unsigned char *) str.c_str(), (int) str.size()); + m_out->put_bytes((const uint8_t *) str.c_str(), (int) str.size()); } @@ -157,16 +157,16 @@ void encoder::write_special(int special) { void encoder::write_bool(bool value) { if (value == true) { - m_out->put_byte((unsigned char) 0xf5); + m_out->put_byte((uint8_t) 0xf5); } else { - m_out->put_byte((unsigned char) 0xf4); + m_out->put_byte((uint8_t) 0xf4); } } void encoder::write_null() { - m_out->put_byte((unsigned char) 0xf6); + m_out->put_byte((uint8_t) 0xf6); } void encoder::write_undefined() { - m_out->put_byte((unsigned char) 0xf7); + m_out->put_byte((uint8_t) 0xf7); } diff --git a/src/encoder.h b/src/encoder.h index 29bb970..4937926 100644 --- a/src/encoder.h +++ b/src/encoder.h @@ -39,7 +39,7 @@ namespace cbor { void write_int(unsigned long long value); - void write_bytes(const unsigned char *data, unsigned int size); + void write_bytes(const uint8_t *data, unsigned int size); void write_string(const char *data, unsigned int size); diff --git a/src/input.cpp b/src/input.cpp index bf1d6d0..36345ab 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -23,7 +23,7 @@ using namespace cbor; input::input(void *data, int size) { - _data = (unsigned char *)data; + _data = (uint8_t *)data; _size = size; m_offset = 0; } @@ -36,7 +36,7 @@ bool input::has_bytes(int count) { return _size - m_offset >= count; } -unsigned char input::get_byte() { +uint8_t input::get_byte() { return _data[m_offset++]; } diff --git a/src/input.h b/src/input.h index 6dea9de..b71a150 100644 --- a/src/input.h +++ b/src/input.h @@ -23,7 +23,7 @@ namespace cbor { class input { private: - unsigned char *_data; + uint8_t *_data; int _size; int m_offset; public: @@ -33,7 +33,7 @@ namespace cbor { bool has_bytes(int count); - unsigned char get_byte(); + uint8_t get_byte(); unsigned short get_short(); diff --git a/src/listener.h b/src/listener.h index 0c0cf81..c0d2421 100644 --- a/src/listener.h +++ b/src/listener.h @@ -28,7 +28,7 @@ struct listener { virtual void on_float32(float value) const noexcept {} virtual void on_double(double value) const noexcept {} - virtual void on_bytes(unsigned char *data, int size) const noexcept {} + virtual void on_bytes(uint8_t *data, int size) const noexcept {} virtual void on_string(std::string &str) const noexcept {} diff --git a/src/listener_debug.cpp b/src/listener_debug.cpp index a2f371d..04fab9b 100644 --- a/src/listener_debug.cpp +++ b/src/listener_debug.cpp @@ -22,7 +22,7 @@ void listener_debug::on_integer(int value) const noexcept { p("i", value); } -void listener_debug::on_bytes(unsigned char *data, int size) const noexcept { +void listener_debug::on_bytes(uint8_t *data, int size) const noexcept { p("b", data); p("len", size); } diff --git a/src/listener_debug.h b/src/listener_debug.h index 9fce61d..6004389 100644 --- a/src/listener_debug.h +++ b/src/listener_debug.h @@ -25,7 +25,7 @@ namespace cbor { public: virtual void on_integer(int value) const noexcept override; - virtual void on_bytes(unsigned char *data, int size) const noexcept override; + virtual void on_bytes(uint8_t *data, int size) const noexcept override; virtual void on_string(std::string &str) const noexcept override; From b44ac73126cf23088d1a1e226937beabcdaf5105 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 00:51:54 +0200 Subject: [PATCH 27/47] Use size_t --- src/output.cpp | 2 +- src/output.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/output.cpp b/src/output.cpp index 360abef..6ea5e4a 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -28,7 +28,7 @@ uint8_t *output::data() { return m_buffer.data(); } -unsigned int output::size() { +size_t output::size() { return m_buffer.size(); } diff --git a/src/output.h b/src/output.h index 4237163..1700b1c 100644 --- a/src/output.h +++ b/src/output.h @@ -25,11 +25,11 @@ namespace cbor { const int default_capacity = 256; struct output { - explicit output(unsigned int inital_capacity = default_capacity); + explicit output(unsigned int inital_capacity = default_capacity); uint8_t *data(); - unsigned int size(); + size_t size(); void put_byte(uint8_t value); From db4ccfce5347755e32f2ebd821c4557910853838 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 01:38:46 +0200 Subject: [PATCH 28/47] Pass string as const ref --- src/listener.h | 2 +- src/listener_debug.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/listener.h b/src/listener.h index c0d2421..344f3aa 100644 --- a/src/listener.h +++ b/src/listener.h @@ -30,7 +30,7 @@ struct listener { virtual void on_bytes(uint8_t *data, int size) const noexcept {} - virtual void on_string(std::string &str) const noexcept {} + virtual void on_string(const std::string &str) const noexcept {} virtual void on_array(int size) const noexcept {} diff --git a/src/listener_debug.h b/src/listener_debug.h index 6004389..676045a 100644 --- a/src/listener_debug.h +++ b/src/listener_debug.h @@ -27,7 +27,7 @@ namespace cbor { virtual void on_bytes(uint8_t *data, int size) const noexcept override; - virtual void on_string(std::string &str) const noexcept override; + virtual void on_string(const std::string &str) const noexcept override; virtual void on_array(int size) const noexcept override; From 91383495b15d6acdda658dc74c55e6d16385ee64 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 01:39:04 +0200 Subject: [PATCH 29/47] Pass string as const ref --- src/listener_debug.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/listener_debug.cpp b/src/listener_debug.cpp index 04fab9b..c475e9f 100644 --- a/src/listener_debug.cpp +++ b/src/listener_debug.cpp @@ -27,7 +27,7 @@ void listener_debug::on_bytes(uint8_t *data, int size) const noexcept { p("len", size); } -void listener_debug::on_string(std::string &str) const noexcept { +void listener_debug::on_string(const std::string &str) const noexcept { p("s", str); } From 5a81e29c9a77bc1eebcaac45deb6aa9a1efa65fc Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 01:39:51 +0200 Subject: [PATCH 30/47] Replace pointer with vector --- src/decoder.cpp | 12 ++++---- src/input.cpp | 78 ++++++++++++++++++++++++++----------------------- src/input.h | 20 ++++++------- src/output.cpp | 4 +-- src/output.h | 2 +- 5 files changed, 58 insertions(+), 58 deletions(-) diff --git a/src/decoder.cpp b/src/decoder.cpp index 580e8ad..7f0cb91 100644 --- a/src/decoder.cpp +++ b/src/decoder.cpp @@ -24,6 +24,7 @@ using namespace cbor; void decoder::run(input& input, listener& listener) { unsigned int temp; int m_currentLength {}; + m_state = decoder_state::type; while(1) { if(m_state == decoder_state::type) { @@ -279,10 +280,9 @@ void decoder::run(input& input, listener& listener) { } else break; } else if(m_state == decoder_state::bytes_data) { if(input.has_bytes(m_currentLength)) { - uint8_t *data = new uint8_t[m_currentLength]; - input.get_bytes(data, m_currentLength); + auto data = input.get_bytes(m_currentLength); m_state = decoder_state::type; - listener.on_bytes(data, m_currentLength); + listener.on_bytes(data.data(), m_currentLength); } else break; } else if(m_state == decoder_state::string_size) { if(input.has_bytes(m_currentLength)) { @@ -307,11 +307,8 @@ void decoder::run(input& input, listener& listener) { } else break; } else if(m_state == decoder_state::string_data) { if(input.has_bytes(m_currentLength)) { - uint8_t *data = new uint8_t[m_currentLength]; - input.get_bytes(data, m_currentLength); m_state = decoder_state::type; - std::string str((const char *)data, (size_t)m_currentLength); - listener.on_string(str); + listener.on_string(input.get_str(m_currentLength)); } else break; } else if(m_state == decoder_state::array) { if(input.has_bytes(m_currentLength)) { @@ -401,6 +398,7 @@ void decoder::run(input& input, listener& listener) { break; } else { std::cerr << "UNKNOWN STATE"; + exit(2); } } } diff --git a/src/input.cpp b/src/input.cpp index 36345ab..a5a4f94 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -19,49 +19,41 @@ #include #include #include +#include +#include using namespace cbor; -input::input(void *data, int size) { - _data = (uint8_t *)data; - _size = size; - m_offset = 0; -} - -input::~input() { - -} - bool input::has_bytes(int count) { - return _size - m_offset >= count; + return m_data.size() - m_offset >= count; } uint8_t input::get_byte() { - return _data[m_offset++]; + return m_data[m_offset++]; } unsigned short input::get_short() { - unsigned short value = ((unsigned short) _data[m_offset] << 8) | ((unsigned short) _data[m_offset + 1]); + unsigned short value = ((unsigned short) m_data[m_offset] << 8) | ((unsigned short) m_data[m_offset + 1]); m_offset += 2; return value; } unsigned int input::get_int() { unsigned int value = \ - ((unsigned int) _data[m_offset ] << 24) | - ((unsigned int) _data[m_offset + 1] << 16) | - ((unsigned int) _data[m_offset + 2] << 8 ) | - ((unsigned int) _data[m_offset + 3]); + ((unsigned int) m_data[m_offset ] << 24) | + ((unsigned int) m_data[m_offset + 1] << 16) | + ((unsigned int) m_data[m_offset + 2] << 8 ) | + ((unsigned int) m_data[m_offset + 3]); m_offset += 4; return value; } float input::get_float() { uint8_t value[4] = { - _data[m_offset + 3], - _data[m_offset + 2], - _data[m_offset + 1], - _data[m_offset + 0] + m_data[m_offset + 3], + m_data[m_offset + 2], + m_data[m_offset + 1], + m_data[m_offset + 0] }; m_offset += 4; return *((float*) (&value[0])); @@ -70,29 +62,41 @@ float input::get_float() { double input::get_double() { double ret; uint8_t* ptr = (uint8_t*)(void*) &ret; - *(ptr + 0) = _data[m_offset + 7]; - *(ptr + 1) = _data[m_offset + 6]; - *(ptr + 2) = _data[m_offset + 5]; - *(ptr + 3) = _data[m_offset + 4]; - *(ptr + 4) = _data[m_offset + 3]; - *(ptr + 5) = _data[m_offset + 2]; - *(ptr + 6) = _data[m_offset + 1]; - *(ptr + 7) = _data[m_offset + 0]; + *(ptr + 0) = m_data[m_offset + 7]; + *(ptr + 1) = m_data[m_offset + 6]; + *(ptr + 2) = m_data[m_offset + 5]; + *(ptr + 3) = m_data[m_offset + 4]; + *(ptr + 4) = m_data[m_offset + 3]; + *(ptr + 5) = m_data[m_offset + 2]; + *(ptr + 6) = m_data[m_offset + 1]; + *(ptr + 7) = m_data[m_offset + 0]; m_offset += 8; return ret; } unsigned long long input::get_long() { - unsigned long long value = ((unsigned long long) _data[m_offset] << 56) | - ((unsigned long long) _data[m_offset +1] << 48) | ((unsigned long long) _data[m_offset +2] << 40) | - ((unsigned long long) _data[m_offset +3] << 32) | ((unsigned long long) _data[m_offset +4] << 24) | - ((unsigned long long) _data[m_offset +5] << 16) | ((unsigned long long) _data[m_offset +6] << 8 ) | - ((unsigned long long) _data[m_offset +7]); + unsigned long long value = ((unsigned long long) m_data[m_offset] << 56) | + ((unsigned long long) m_data[m_offset +1] << 48) | ((unsigned long long) m_data[m_offset +2] << 40) | + ((unsigned long long) m_data[m_offset +3] << 32) | ((unsigned long long) m_data[m_offset +4] << 24) | + ((unsigned long long) m_data[m_offset +5] << 16) | ((unsigned long long) m_data[m_offset +6] << 8 ) | + ((unsigned long long) m_data[m_offset +7]); m_offset += 8; return value; } -void input::get_bytes(void *to, int count) { - memcpy(to, _data + m_offset, count); - m_offset += count; +std::vector input::get_bytes(int count) { + auto start = m_data.begin() + m_offset; + return std::vector(start, start + count); } + +std::string input::get_str(int count) { + std::stringstream ss; + auto begin = m_data.begin() + m_offset; + auto end = m_data.begin() + m_offset + count; + for(auto it = begin; it != m_data.end() && it != end; it++) { + ss << *it; + m_offset++; + } + + return ss.str(); +} \ No newline at end of file diff --git a/src/input.h b/src/input.h index b71a150..40bb395 100644 --- a/src/input.h +++ b/src/input.h @@ -16,20 +16,13 @@ #pragma once -#include +#include namespace cbor { - class input { - private: - uint8_t *_data; - int _size; - int m_offset; - public: - explicit input(void *data, int size); - - ~input(); + struct input { + explicit input(const std::vector& data) : m_data(data), m_offset(0) {} bool has_bytes(int count); @@ -43,6 +36,11 @@ namespace cbor { unsigned long long get_long(); - void get_bytes(void *to, int count); + std::vector get_bytes(int count); + + std::string get_str(int count); + private: + std::vector m_data{}; + int m_offset{}; }; } diff --git a/src/output.cpp b/src/output.cpp index 6ea5e4a..e0df5a8 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -24,8 +24,8 @@ using namespace cbor; output::output(unsigned int initalCapacity) : m_buffer(initalCapacity) { } -uint8_t *output::data() { - return m_buffer.data(); +std::vector output::data() { + return m_buffer; } size_t output::size() { diff --git a/src/output.h b/src/output.h index 1700b1c..496b9a7 100644 --- a/src/output.h +++ b/src/output.h @@ -27,7 +27,7 @@ namespace cbor { struct output { explicit output(unsigned int inital_capacity = default_capacity); - uint8_t *data(); + std::vector data(); size_t size(); From 287c39cce25c7988bcbefc98fc6856c5bc26cb04 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 01:40:46 +0200 Subject: [PATCH 31/47] Replace pointer with vector --- test/tests.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/tests.cpp b/test/tests.cpp index d4a19c7..de2faad 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -38,7 +38,7 @@ int main() { } { // decoding - cbor::input input(output.data(), output.size()); + cbor::input input(output.data()); cbor::listener_debug listener; cbor::decoder decoder; decoder.run(input, listener); From f9b35fd3fdf2a8151663bba31f84e7b1f50dca3e Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 01:49:18 +0200 Subject: [PATCH 32/47] Add missing include, offset now size_t --- src/input.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/input.h b/src/input.h index 40bb395..266767e 100644 --- a/src/input.h +++ b/src/input.h @@ -16,6 +16,7 @@ #pragma once +#include #include namespace cbor { @@ -41,6 +42,6 @@ namespace cbor { std::string get_str(int count); private: std::vector m_data{}; - int m_offset{}; + size_t m_offset{}; }; } From 68a19fb22e0cf0b3f89824b3583fd6db065a3370 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 01:53:51 +0200 Subject: [PATCH 33/47] Add string.h --- src/input.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/input.h b/src/input.h index 266767e..8a9fe47 100644 --- a/src/input.h +++ b/src/input.h @@ -18,6 +18,7 @@ #include #include +#include namespace cbor { From 2928265a28a427ce117b2b672bf78f0a2f9601cc Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 02:04:52 +0200 Subject: [PATCH 34/47] Add cmake badge --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 015cad3..305fb82 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,8 @@ cbor-cpp [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/naphaso/cbor-cpp?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +![build](https://github.com/Magolves/cbor-cpp/blob/master/.github/workflows/cmake.yml/badge.svg) + CBOR C++ serialization library Just a simple SAX-like Concise Binary Object Representation (CBOR). From 92c82fa16bea0be91a6d0d148a615ad008b90d5a Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 02:11:26 +0200 Subject: [PATCH 35/47] Add cmake badge (2nd try) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 305fb82..0983e10 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ cbor-cpp [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/naphaso/cbor-cpp?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -![build](https://github.com/Magolves/cbor-cpp/blob/master/.github/workflows/cmake.yml/badge.svg) +[[!build](https://github.com/Magolves/cbor-cpp/blob/master/.github/workflows/cmake.yml/badge.svg)] CBOR C++ serialization library From 9bb017d0a535bf86b5df33671159eb9da58bd281 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 02:13:28 +0200 Subject: [PATCH 36/47] Add cmake badge (3rd try) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0983e10..a18aabc 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ cbor-cpp [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/naphaso/cbor-cpp?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -[[!build](https://github.com/Magolves/cbor-cpp/blob/master/.github/workflows/cmake.yml/badge.svg)] +[![build](https://github.com/Magolves/cbor-cpp/blob/master/.github/workflows/cmake.yml/badge.svg)](https://github.com/Magolves/cbor-cpp/blob/master/.github/workflows/cmake.yml/badge.svg) CBOR C++ serialization library From 81c0c412943fcccc1cc1c5b66629422cf0bd6142 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 02:14:43 +0200 Subject: [PATCH 37/47] Add cmake badge (4th try) --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index a18aabc..480587c 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ cbor-cpp [![build](https://github.com/Magolves/cbor-cpp/blob/master/.github/workflows/cmake.yml/badge.svg)](https://github.com/Magolves/cbor-cpp/blob/master/.github/workflows/cmake.yml/badge.svg) +![cmake](https://github.com/github/docs/actions/workflows/cmake.yml/badge.svg) + CBOR C++ serialization library Just a simple SAX-like Concise Binary Object Representation (CBOR). From 78d11a1d5d5a2381393633d3ebb56eef9d38b76b Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 02:15:59 +0200 Subject: [PATCH 38/47] Add cmake badge (5th try) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 480587c..33ac43b 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ cbor-cpp [![build](https://github.com/Magolves/cbor-cpp/blob/master/.github/workflows/cmake.yml/badge.svg)](https://github.com/Magolves/cbor-cpp/blob/master/.github/workflows/cmake.yml/badge.svg) -![cmake](https://github.com/github/docs/actions/workflows/cmake.yml/badge.svg) +![cmake](https://github.com/Magolves/cbor-cpp/actions/workflows/cmake.yml/badge.svg) CBOR C++ serialization library From 187bcd31a4dded35892358a896be19f3979461d4 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 02:16:51 +0200 Subject: [PATCH 39/47] Remove obsolete badge --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 33ac43b..f182a36 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,6 @@ cbor-cpp [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/naphaso/cbor-cpp?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -[![build](https://github.com/Magolves/cbor-cpp/blob/master/.github/workflows/cmake.yml/badge.svg)](https://github.com/Magolves/cbor-cpp/blob/master/.github/workflows/cmake.yml/badge.svg) - ![cmake](https://github.com/Magolves/cbor-cpp/actions/workflows/cmake.yml/badge.svg) CBOR C++ serialization library From 20a5c557ef127a814a2eec552286fc9e16ec6a3f Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 02:37:47 +0200 Subject: [PATCH 40/47] Add cmake files from cpp_start_project --- CMakeLists.txt | 8 ++- cmake/Cache.cmake | 29 +++++++++++ cmake/CompilerWarnings.cmake | 78 +++++++++++++++++++++++++++++ cmake/Conan.cmake | 29 +++++++++++ cmake/Doxygen.cmake | 11 ++++ cmake/PreventInSourceBuilds.cmake | 18 +++++++ cmake/Sanitizers.cmake | 66 ++++++++++++++++++++++++ cmake/StandardProjectSettings.cmake | 42 ++++++++++++++++ cmake/StaticAnalyzers.cmake | 37 ++++++++++++++ 9 files changed, 313 insertions(+), 5 deletions(-) create mode 100644 cmake/Cache.cmake create mode 100644 cmake/CompilerWarnings.cmake create mode 100644 cmake/Conan.cmake create mode 100644 cmake/Doxygen.cmake create mode 100644 cmake/PreventInSourceBuilds.cmake create mode 100644 cmake/Sanitizers.cmake create mode 100644 cmake/StandardProjectSettings.cmake create mode 100644 cmake/StaticAnalyzers.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 06ae27b..014371f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,14 +1,12 @@ cmake_minimum_required(VERSION 3.15) project(cborcpp) +include(cmake/StandardProjectSettings.cmake) + set(CMAKE_BUILD_TYPE Release) set(EXPORT_COMPILE_COMMANDS ON) -# On Mac OS 'CXX_STANDARD' is not evaluated -# Instead we have to set the compiler flag -if (APPLE) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") -endif() + set(SOURCE_FILES src/encoder.cpp diff --git a/cmake/Cache.cmake b/cmake/Cache.cmake new file mode 100644 index 0000000..31f5e7e --- /dev/null +++ b/cmake/Cache.cmake @@ -0,0 +1,29 @@ +option(ENABLE_CACHE "Enable cache if available" ON) +if(NOT ENABLE_CACHE) + return() +endif() + +set(CACHE_OPTION + "ccache" + CACHE STRING "Compiler cache to be used") +set(CACHE_OPTION_VALUES "ccache" "sccache") +set_property(CACHE CACHE_OPTION PROPERTY STRINGS ${CACHE_OPTION_VALUES}) +list( + FIND + CACHE_OPTION_VALUES + ${CACHE_OPTION} + CACHE_OPTION_INDEX) + +if(${CACHE_OPTION_INDEX} EQUAL -1) + message( + STATUS + "Using custom compiler cache system: '${CACHE_OPTION}', explicitly supported entries are ${CACHE_OPTION_VALUES}") +endif() + +find_program(CACHE_BINARY ${CACHE_OPTION}) +if(CACHE_BINARY) + message(STATUS "${CACHE_OPTION} found and enabled") + set(CMAKE_CXX_COMPILER_LAUNCHER ${CACHE_BINARY}) +else() + message(WARNING "${CACHE_OPTION} is enabled but was not found. Not using it") +endif() diff --git a/cmake/CompilerWarnings.cmake b/cmake/CompilerWarnings.cmake new file mode 100644 index 0000000..8243154 --- /dev/null +++ b/cmake/CompilerWarnings.cmake @@ -0,0 +1,78 @@ +# from here: +# +# https://github.com/lefticus/cppbestpractices/blob/master/02-Use_the_Tools_Available.md + +function(set_project_warnings project_name) + option(WARNINGS_AS_ERRORS "Treat compiler warnings as errors" TRUE) + + set(MSVC_WARNINGS + /W4 # Baseline reasonable warnings + /w14242 # 'identifier': conversion from 'type1' to 'type1', possible loss of data + /w14254 # 'operator': conversion from 'type1:field_bits' to 'type2:field_bits', possible loss of data + /w14263 # 'function': member function does not override any base class virtual member function + /w14265 # 'classname': class has virtual functions, but destructor is not virtual instances of this class may not + # be destructed correctly + /w14287 # 'operator': unsigned/negative constant mismatch + /we4289 # nonstandard extension used: 'variable': loop control variable declared in the for-loop is used outside + # the for-loop scope + /w14296 # 'operator': expression is always 'boolean_value' + /w14311 # 'variable': pointer truncation from 'type1' to 'type2' + /w14545 # expression before comma evaluates to a function which is missing an argument list + /w14546 # function call before comma missing argument list + /w14547 # 'operator': operator before comma has no effect; expected operator with side-effect + /w14549 # 'operator': operator before comma has no effect; did you intend 'operator'? + /w14555 # expression has no effect; expected expression with side- effect + /w14619 # pragma warning: there is no warning number 'number' + /w14640 # Enable warning on thread un-safe static member initialization + /w14826 # Conversion from 'type1' to 'type_2' is sign-extended. This may cause unexpected runtime behavior. + /w14905 # wide string literal cast to 'LPSTR' + /w14906 # string literal cast to 'LPWSTR' + /w14928 # illegal copy-initialization; more than one user-defined conversion has been implicitly applied + /permissive- # standards conformance mode for MSVC compiler. + ) + + set(CLANG_WARNINGS + -Wall + -Wextra # reasonable and standard + -Wshadow # warn the user if a variable declaration shadows one from a parent context + -Wnon-virtual-dtor # warn the user if a class with virtual functions has a non-virtual destructor. This helps + # catch hard to track down memory errors + -Wold-style-cast # warn for c-style casts + -Wcast-align # warn for potential performance problem casts + -Wunused # warn on anything being unused + -Woverloaded-virtual # warn if you overload (not override) a virtual function + -Wpedantic # warn if non-standard C++ is used + -Wconversion # warn on type conversions that may lose data + -Wsign-conversion # warn on sign conversions + -Wnull-dereference # warn if a null dereference is detected + -Wdouble-promotion # warn if float is implicit promoted to double + -Wformat=2 # warn on security issues around functions that format output (ie printf) + ) + + if(WARNINGS_AS_ERRORS) + set(CLANG_WARNINGS ${CLANG_WARNINGS} -Werror) + set(MSVC_WARNINGS ${MSVC_WARNINGS} /WX) + endif() + + set(GCC_WARNINGS + ${CLANG_WARNINGS} + -Wmisleading-indentation # warn if indentation implies blocks where blocks do not exist + -Wduplicated-cond # warn if if / else chain has duplicated conditions + -Wduplicated-branches # warn if if / else branches have duplicated code + -Wlogical-op # warn about logical operations being used where bitwise were probably wanted + -Wuseless-cast # warn if you perform a cast to the same type + ) + + if(MSVC) + set(PROJECT_WARNINGS ${MSVC_WARNINGS}) + elseif(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") + set(PROJECT_WARNINGS ${CLANG_WARNINGS}) + elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + set(PROJECT_WARNINGS ${GCC_WARNINGS}) + else() + message(AUTHOR_WARNING "No compiler warnings set for '${CMAKE_CXX_COMPILER_ID}' compiler.") + endif() + + target_compile_options(${project_name} INTERFACE ${PROJECT_WARNINGS}) + +endfunction() diff --git a/cmake/Conan.cmake b/cmake/Conan.cmake new file mode 100644 index 0000000..6aa6f47 --- /dev/null +++ b/cmake/Conan.cmake @@ -0,0 +1,29 @@ +macro(run_conan) + # Download automatically, you can also just copy the conan.cmake file + if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake") + message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan") + file(DOWNLOAD "https://github.com/conan-io/cmake-conan/raw/v0.15/conan.cmake" "${CMAKE_BINARY_DIR}/conan.cmake") + endif() + + include(${CMAKE_BINARY_DIR}/conan.cmake) + + conan_add_remote( + NAME + bincrafters + URL + https://api.bintray.com/conan/bincrafters/public-conan) + + conan_cmake_run( + REQUIRES + ${CONAN_EXTRA_REQUIRES} + catch2/2.13.3 + docopt.cpp/0.6.2 + fmt/6.2.0 + spdlog/1.5.0 + OPTIONS + ${CONAN_EXTRA_OPTIONS} + BASIC_SETUP + CMAKE_TARGETS # individual targets to link to + BUILD + missing) +endmacro() diff --git a/cmake/Doxygen.cmake b/cmake/Doxygen.cmake new file mode 100644 index 0000000..4dad807 --- /dev/null +++ b/cmake/Doxygen.cmake @@ -0,0 +1,11 @@ +function(enable_doxygen) + option(ENABLE_DOXYGEN "Enable doxygen doc builds of source" OFF) + if(ENABLE_DOXYGEN) + set(DOXYGEN_CALLER_GRAPH YES) + set(DOXYGEN_CALL_GRAPH YES) + set(DOXYGEN_EXTRACT_ALL YES) + find_package(Doxygen REQUIRED dot) + doxygen_add_docs(doxygen-docs ${PROJECT_SOURCE_DIR}) + + endif() +endfunction() diff --git a/cmake/PreventInSourceBuilds.cmake b/cmake/PreventInSourceBuilds.cmake new file mode 100644 index 0000000..57d9c59 --- /dev/null +++ b/cmake/PreventInSourceBuilds.cmake @@ -0,0 +1,18 @@ +# +# This function will prevent in-source builds +function(AssureOutOfSourceBuilds) + # make sure the user doesn't play dirty with symlinks + get_filename_component(srcdir "${CMAKE_SOURCE_DIR}" REALPATH) + get_filename_component(bindir "${CMAKE_BINARY_DIR}" REALPATH) + + # disallow in-source builds + if("${srcdir}" STREQUAL "${bindir}") + message("######################################################") + message("Warning: in-source builds are disabled") + message("Please create a separate build directory and run cmake from there") + message("######################################################") + message(FATAL_ERROR "Quitting configuration") + endif() +endfunction() + +assureoutofsourcebuilds() diff --git a/cmake/Sanitizers.cmake b/cmake/Sanitizers.cmake new file mode 100644 index 0000000..6c6ff8f --- /dev/null +++ b/cmake/Sanitizers.cmake @@ -0,0 +1,66 @@ +function(enable_sanitizers project_name) + + if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") + option(ENABLE_COVERAGE "Enable coverage reporting for gcc/clang" FALSE) + + if(ENABLE_COVERAGE) + target_compile_options(${project_name} INTERFACE --coverage -O0 -g) + target_link_libraries(${project_name} INTERFACE --coverage) + endif() + + set(SANITIZERS "") + + option(ENABLE_SANITIZER_ADDRESS "Enable address sanitizer" FALSE) + if(ENABLE_SANITIZER_ADDRESS) + list(APPEND SANITIZERS "address") + endif() + + option(ENABLE_SANITIZER_LEAK "Enable leak sanitizer" FALSE) + if(ENABLE_SANITIZER_LEAK) + list(APPEND SANITIZERS "leak") + endif() + + option(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "Enable undefined behavior sanitizer" FALSE) + if(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR) + list(APPEND SANITIZERS "undefined") + endif() + + option(ENABLE_SANITIZER_THREAD "Enable thread sanitizer" FALSE) + if(ENABLE_SANITIZER_THREAD) + if("address" IN_LIST SANITIZERS OR "leak" IN_LIST SANITIZERS) + message(WARNING "Thread sanitizer does not work with Address and Leak sanitizer enabled") + else() + list(APPEND SANITIZERS "thread") + endif() + endif() + + option(ENABLE_SANITIZER_MEMORY "Enable memory sanitizer" FALSE) + if(ENABLE_SANITIZER_MEMORY AND CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") + if("address" IN_LIST SANITIZERS + OR "thread" IN_LIST SANITIZERS + OR "leak" IN_LIST SANITIZERS) + message(WARNING "Memory sanitizer does not work with Address, Thread and Leak sanitizer enabled") + else() + list(APPEND SANITIZERS "memory") + endif() + endif() + + list( + JOIN + SANITIZERS + "," + LIST_OF_SANITIZERS) + + endif() + + if(LIST_OF_SANITIZERS) + if(NOT + "${LIST_OF_SANITIZERS}" + STREQUAL + "") + target_compile_options(${project_name} INTERFACE -fsanitize=${LIST_OF_SANITIZERS}) + target_link_options(${project_name} INTERFACE -fsanitize=${LIST_OF_SANITIZERS}) + endif() + endif() + +endfunction() diff --git a/cmake/StandardProjectSettings.cmake b/cmake/StandardProjectSettings.cmake new file mode 100644 index 0000000..76418c1 --- /dev/null +++ b/cmake/StandardProjectSettings.cmake @@ -0,0 +1,42 @@ +# Set a default build type if none was specified +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + message(STATUS "Setting build type to 'RelWithDebInfo' as none was specified.") + set(CMAKE_BUILD_TYPE + RelWithDebInfo + CACHE STRING "Choose the type of build." FORCE) + # Set the possible values of build type for cmake-gui, ccmake + set_property( + CACHE CMAKE_BUILD_TYPE + PROPERTY STRINGS + "Debug" + "Release" + "MinSizeRel" + "RelWithDebInfo") +endif() + +# Generate compile_commands.json to make it easier to work with clang based tools +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +option(ENABLE_IPO "Enable Interprocedural Optimization, aka Link Time Optimization (LTO)" OFF) + +if(ENABLE_IPO) + include(CheckIPOSupported) + check_ipo_supported( + RESULT + result + OUTPUT + output) + if(result) + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) + else() + message(SEND_ERROR "IPO is not supported: ${output}") + endif() +endif() +if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") + add_compile_options(-fcolor-diagnostics) +elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + add_compile_options(-fdiagnostics-color=always) +else() + message(STATUS "No colored compiler diagnostic set for '${CMAKE_CXX_COMPILER_ID}' compiler.") +endif() + diff --git a/cmake/StaticAnalyzers.cmake b/cmake/StaticAnalyzers.cmake new file mode 100644 index 0000000..4396444 --- /dev/null +++ b/cmake/StaticAnalyzers.cmake @@ -0,0 +1,37 @@ +option(ENABLE_CPPCHECK "Enable static analysis with cppcheck" OFF) +option(ENABLE_CLANG_TIDY "Enable static analysis with clang-tidy" OFF) +option(ENABLE_INCLUDE_WHAT_YOU_USE "Enable static analysis with include-what-you-use" OFF) + +if(ENABLE_CPPCHECK) + find_program(CPPCHECK cppcheck) + if(CPPCHECK) + set(CMAKE_CXX_CPPCHECK + ${CPPCHECK} + --suppress=missingInclude + --enable=all + --inline-suppr + --inconclusive + -i + ${CMAKE_SOURCE_DIR}/imgui/lib) + else() + message(SEND_ERROR "cppcheck requested but executable not found") + endif() +endif() + +if(ENABLE_CLANG_TIDY) + find_program(CLANGTIDY clang-tidy) + if(CLANGTIDY) + set(CMAKE_CXX_CLANG_TIDY ${CLANGTIDY} -extra-arg=-Wno-unknown-warning-option) + else() + message(SEND_ERROR "clang-tidy requested but executable not found") + endif() +endif() + +if(ENABLE_INCLUDE_WHAT_YOU_USE) + find_program(INCLUDE_WHAT_YOU_USE include-what-you-use) + if(INCLUDE_WHAT_YOU_USE) + set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${INCLUDE_WHAT_YOU_USE}) + else() + message(SEND_ERROR "include-what-you-use requested but executable not found") + endif() +endif() From d347423efce3b65ba2b8c0529537cb026e97823d Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 02:42:05 +0200 Subject: [PATCH 41/47] Activated project settings and warnings --- CMakeLists.txt | 11 +++++++++++ cmake/StandardProjectSettings.cmake | 28 +++++----------------------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 014371f..c871b9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,16 @@ project(cborcpp) include(cmake/StandardProjectSettings.cmake) +# Link this 'library' to set the c++ standard / compile-time options requested +add_library(project_options INTERFACE) +target_compile_features(project_options INTERFACE cxx_std_17) + +# Link this 'library' to use the warnings specified in CompilerWarnings.cmake +add_library(project_warnings INTERFACE) +# standard compiler warnings +include(cmake/CompilerWarnings.cmake) +set_project_warnings(project_warnings) + set(CMAKE_BUILD_TYPE Release) set(EXPORT_COMPILE_COMMANDS ON) @@ -21,6 +31,7 @@ set(TEST_SOURCE_FILES ) add_library(cborcpp SHARED ${SOURCE_FILES}) +target_link_libraries(cborcpp INTERFACE project_options) set_property(TARGET cborcpp PROPERTY CXX_STANDARD 14) add_executable(testing ${SOURCE_FILES} ${TEST_SOURCE_FILES}) target_include_directories(testing PRIVATE src) \ No newline at end of file diff --git a/cmake/StandardProjectSettings.cmake b/cmake/StandardProjectSettings.cmake index 76418c1..52e859a 100644 --- a/cmake/StandardProjectSettings.cmake +++ b/cmake/StandardProjectSettings.cmake @@ -17,26 +17,8 @@ endif() # Generate compile_commands.json to make it easier to work with clang based tools set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -option(ENABLE_IPO "Enable Interprocedural Optimization, aka Link Time Optimization (LTO)" OFF) - -if(ENABLE_IPO) - include(CheckIPOSupported) - check_ipo_supported( - RESULT - result - OUTPUT - output) - if(result) - set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) - else() - message(SEND_ERROR "IPO is not supported: ${output}") - endif() -endif() -if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") - add_compile_options(-fcolor-diagnostics) -elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - add_compile_options(-fdiagnostics-color=always) -else() - message(STATUS "No colored compiler diagnostic set for '${CMAKE_CXX_COMPILER_ID}' compiler.") -endif() - +# On Mac OS 'CXX_STANDARD' is not evaluated +# Instead we have to set the compiler flag +if (APPLE) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") +endif() From 24e7ed461357c81daea713586aee397337e9feaf Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 02:42:26 +0200 Subject: [PATCH 42/47] Lower C++ standard to 14 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c871b9d..126efcc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ include(cmake/StandardProjectSettings.cmake) # Link this 'library' to set the c++ standard / compile-time options requested add_library(project_options INTERFACE) -target_compile_features(project_options INTERFACE cxx_std_17) +target_compile_features(project_options INTERFACE cxx_std_14) # Link this 'library' to use the warnings specified in CompilerWarnings.cmake add_library(project_warnings INTERFACE) From 742779e0c10a228ae75f828e22d2e171722f2843 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 02:45:24 +0200 Subject: [PATCH 43/47] Setup conan --- CMakeLists.txt | 5 +++++ cmake/Conan.cmake | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 126efcc..a78daaa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,11 @@ set_project_warnings(project_warnings) set(CMAKE_BUILD_TYPE Release) set(EXPORT_COMPILE_COMMANDS ON) +# Set up some extra Conan dependencies based on our needs before loading Conan +set(CONAN_EXTRA_REQUIRES "") +set(CONAN_EXTRA_OPTIONS "") +include(cmake/Conan.cmake) +run_conan() set(SOURCE_FILES diff --git a/cmake/Conan.cmake b/cmake/Conan.cmake index 6aa6f47..fc91b65 100644 --- a/cmake/Conan.cmake +++ b/cmake/Conan.cmake @@ -17,9 +17,9 @@ macro(run_conan) REQUIRES ${CONAN_EXTRA_REQUIRES} catch2/2.13.3 - docopt.cpp/0.6.2 - fmt/6.2.0 - spdlog/1.5.0 + #docopt.cpp/0.6.2 + #fmt/6.2.0 + #spdlog/1.5.0 OPTIONS ${CONAN_EXTRA_OPTIONS} BASIC_SETUP From 19fe5d67e87097dc2a2d6c47d22137189e0b7639 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 02:48:49 +0200 Subject: [PATCH 44/47] Remove test dir --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4d6824c..1f84a48 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ .idea/ *.xcodeproj/ *.dSYM -test + From 7ba4b59887134b06982339cbf31fe843d7eb23e1 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 02:54:29 +0200 Subject: [PATCH 45/47] Add Catch2 --- CMakeLists.txt | 10 ++++------ test/CMakeLists.txt | 28 ++++++++++++++++++++++++++++ test/catch_main.cpp | 5 +++++ 3 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 test/CMakeLists.txt create mode 100644 test/catch_main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a78daaa..88b2531 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,12 +31,10 @@ set(SOURCE_FILES src/output.cpp ) -set(TEST_SOURCE_FILES - test/tests.cpp - ) - add_library(cborcpp SHARED ${SOURCE_FILES}) target_link_libraries(cborcpp INTERFACE project_options) +target_include_directories(cborcpp PUBLIC src) set_property(TARGET cborcpp PROPERTY CXX_STANDARD 14) -add_executable(testing ${SOURCE_FILES} ${TEST_SOURCE_FILES}) -target_include_directories(testing PRIVATE src) \ No newline at end of file + +enable_testing() +add_subdirectory(test) \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..f5e0474 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,28 @@ +# Automatically enable catch2 to generate ctest targets +if(CONAN_CATCH2_ROOT_DEBUG) + include(${CONAN_CATCH2_ROOT_DEBUG}/lib/cmake/Catch2/Catch.cmake) +else() + include(${CONAN_CATCH2_ROOT}/lib/cmake/Catch2/Catch.cmake) +endif() + +add_library(catch_main STATIC catch_main.cpp) +target_link_libraries(catch_main PUBLIC CONAN_PKG::catch2) +target_link_libraries(catch_main PRIVATE project_options) + +add_executable(tests tests.cpp) +target_link_libraries(tests PUBLIC cborcpp PRIVATE project_warnings project_options catch_main) + +# automatically discover tests that are defined in catch based test files you can modify the unittests. Set TEST_PREFIX +# to whatever you want, or use different for different binaries +catch_discover_tests( + tests + TEST_PREFIX + "unittests." + REPORTER + xml + OUTPUT_DIR + . + OUTPUT_PREFIX + "unittests." + OUTPUT_SUFFIX + .xml) diff --git a/test/catch_main.cpp b/test/catch_main.cpp new file mode 100644 index 0000000..d8d2eca --- /dev/null +++ b/test/catch_main.cpp @@ -0,0 +1,5 @@ +#define CATCH_CONFIG_MAIN // This tells the catch header to generate a main + +#include + + From 7ec20ef6c45ceb0367577cd9df15c9805e527f38 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 03:00:00 +0200 Subject: [PATCH 46/47] Fix compiler warnings; add catch2 --- CMakeLists.txt | 1 + src/listener.h | 29 +++++++++++++++-------------- test/CMakeLists.txt | 15 --------------- 3 files changed, 16 insertions(+), 29 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 88b2531..9c26096 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ set_project_warnings(project_warnings) set(CMAKE_BUILD_TYPE Release) set(EXPORT_COMPILE_COMMANDS ON) +option(BUILD_SHARED_LIBS "Enable compilation of shared libraries" OFF) # Set up some extra Conan dependencies based on our needs before loading Conan set(CONAN_EXTRA_REQUIRES "") diff --git a/src/listener.h b/src/listener.h index 344f3aa..647c1dd 100644 --- a/src/listener.h +++ b/src/listener.h @@ -22,23 +22,24 @@ namespace cbor { struct listener { + virtual ~listener() = default; - virtual void on_integer(int value) const noexcept {} + virtual void on_integer(int ) const noexcept {} - virtual void on_float32(float value) const noexcept {} - virtual void on_double(double value) const noexcept {} + virtual void on_float32(float ) const noexcept {} + virtual void on_double(double ) const noexcept {} - virtual void on_bytes(uint8_t *data, int size) const noexcept {} + virtual void on_bytes(uint8_t *, int ) const noexcept {} - virtual void on_string(const std::string &str) const noexcept {} + virtual void on_string(const std::string &) const noexcept {} - virtual void on_array(int size) const noexcept {} + virtual void on_array(int ) const noexcept {} - virtual void on_map(int size) const noexcept {} + virtual void on_map(int ) const noexcept {} - virtual void on_tag(unsigned int tag) const noexcept {} + virtual void on_tag(unsigned int ) const noexcept {} - virtual void on_special(unsigned int code) const noexcept {} + virtual void on_special(unsigned int ) const noexcept {} virtual void on_bool(bool) const noexcept {} @@ -46,14 +47,14 @@ struct listener { virtual void on_undefined() const noexcept {} - virtual void on_error(const char *error) const noexcept {} + virtual void on_error(const char *) const noexcept {} - virtual void on_extra_integer(unsigned long long value, - int sign) const noexcept {} + virtual void on_extra_integer(unsigned long long , + int ) const noexcept {} - virtual void on_extra_tag(unsigned long long tag) const noexcept {} + virtual void on_extra_tag(unsigned long long) const noexcept {} - virtual void on_extra_special(unsigned long long tag) const noexcept {} + virtual void on_extra_special(unsigned long long) const noexcept {} protected: }; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f5e0474..699a72b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -11,18 +11,3 @@ target_link_libraries(catch_main PRIVATE project_options) add_executable(tests tests.cpp) target_link_libraries(tests PUBLIC cborcpp PRIVATE project_warnings project_options catch_main) - -# automatically discover tests that are defined in catch based test files you can modify the unittests. Set TEST_PREFIX -# to whatever you want, or use different for different binaries -catch_discover_tests( - tests - TEST_PREFIX - "unittests." - REPORTER - xml - OUTPUT_DIR - . - OUTPUT_PREFIX - "unittests." - OUTPUT_SUFFIX - .xml) From 18971fe928c5646cf043026e24b17eae780ffb90 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Fri, 23 Apr 2021 03:10:09 +0200 Subject: [PATCH 47/47] Add coann to workflow --- .github/workflows/cmake.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index 005915e..38329de 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -17,6 +17,16 @@ jobs: steps: - uses: actions/checkout@v2 + - name: Download Conan package manager. + run: | + pwd + pip3 install wheel setuptools + pip3 install conan + export PATH=$PATH:/home/runner/.local/bin + echo $PATH + conan --version + ls + - name: Create Build Environment # Some projects don't allow in-source building, so create a separate build directory # We'll use this as our working directory for all subsequent commands