GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Works better with move semantics, double as native float type. · RantyDave/cppbor@5fccec8

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • .gitignore
  • CMakeLists.txt
  • bench.cpp
  • cppbor
    • cppbor.cpp
    • cppbor.hpp
    • main.cpp
    • main.h
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.gitignore‎

Copy file name to clipboardExpand all lines: .gitignore
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
*.user
33
cppbor/.gitattributes
44
*.autosave
5+
cbor-python-wrote
Collapse file

‎CMakeLists.txt‎

Copy file name to clipboardExpand all lines: CMakeLists.txt
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ project (cppbor)
33

44
set(CMAKE_CXX_COMPILER /Library/Developer/CommandLineTools/usr/bin/clang++)
55
set(CMAKE_CXX_STANDARD 17)
6-
set(CMAKE_C_FLAGS "-Wno-reorder")
76
include_directories(SYSTEM /Library/Developer/CommandLineTools/usr/include/c++/v1)
87
include_directories(/usr/local/include)
98
link_directories(/usr/local/lib)
109
link_directories(/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib)
11-
set(CMAKE_EXE_LINKER_FLAGS "-lc++ -lcppunit")
1210

1311
file(GLOB test_sources cppbor/test_sources/*)
1412
file(COPY ${test_sources} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
1513
add_executable(cppbor cppbor/main cppbor/cppbor)
14+
target_link_libraries(cppbor c++ cppunit)
1615
add_executable(bench bench.cpp cppbor/cppbor)
16+
target_link_libraries(bench c++)
1717
set(CMAKE_BUILD_TYPE Release)
Collapse file

‎bench.cpp‎

Copy file name to clipboardExpand all lines: bench.cpp
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <iostream>
22
#include <chrono>
3+
#include <unistd.h>
34
#include "cppbor/cppbor.hpp"
45

56
using namespace std;
@@ -19,10 +20,11 @@ int main()
1920
fclose(f);
2021

2122
// decode cbor
23+
sleep(1);
2224
high_resolution_clock::time_point start=high_resolution_clock::now();
23-
cbor_variant cbor_original;
24-
cbor_variant::construct_from_into(cbor_data_in, &cbor_original);
25+
cbor_variant cbor_original=cbor_variant::construct_from(cbor_data_in);
2526
high_resolution_clock::time_point end=high_resolution_clock::now();
27+
sleep(1);
2628

2729
// report
2830
duration<float> time_taken=duration_cast<duration<float>>(end-start);
Collapse file

‎cppbor/cppbor.cpp‎

Copy file name to clipboardExpand all lines: cppbor/cppbor.cpp
+58-58Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
#include <arpa/inet.h>
2020

2121
using namespace std;
22-
void cbor_variant::construct_from_into(const std::vector<uint8_t>& in, cbor_variant* dest)
22+
cbor_variant cbor_variant::construct_from(const std::vector<uint8_t>& in)
2323
{
2424
unsigned int dummy_offset=0;
25-
return construct_from_into(in, dest, &dummy_offset);
25+
return construct_from(in, &dummy_offset);
2626
}
2727

28-
void cbor_variant::construct_from_into(const std::vector<uint8_t>& in, cbor_variant* dest, unsigned int* offset)
28+
cbor_variant cbor_variant::construct_from(const std::vector<uint8_t>& in, unsigned int* offset)
2929
{
3030
// nothing to read?
3131
if (in.size()<=*offset) throw length_error("No header byte while decoding cbor");
@@ -35,16 +35,8 @@ void cbor_variant::construct_from_into(const std::vector<uint8_t>& in, cbor_vari
3535

3636
// integers
3737
switch (h->major) {
38-
case 0: {
39-
dest->emplace<integer>(read_integer_header(in, h, offset)); // +ve integer
40-
return;
41-
}
42-
43-
case 1: {
44-
dest->emplace<integer>(-1-read_integer_header(in, h, offset)); // -ve integer
45-
return;
46-
}
47-
38+
case 0: return cbor_variant { read_integer_header(in, h, offset) };
39+
case 1: return cbor_variant { -1-read_integer_header(in, h, offset) };
4840
case 2: // bytes and strings
4941
case 3: {
5042
int length=read_integer_header(in, h, offset);
@@ -53,26 +45,22 @@ void cbor_variant::construct_from_into(const std::vector<uint8_t>& in, cbor_vari
5345
*offset+=static_cast<unsigned int>(length);
5446
if (in.size()<*offset) throw length_error("Insufficient data bytes while decoding cbor");
5547
if (h->major==2)
56-
dest->emplace<bytes>(vector<uint8_t>(&in[offset_at_begin], &in[offset_at_begin+static_cast<unsigned int>(length)]));
48+
return cbor_variant { vector<uint8_t>(&in[offset_at_begin], &in[offset_at_begin+static_cast<unsigned int>(length)]) };
5749
else
58-
dest->emplace<unicode_string>(&in[offset_at_begin], &in[offset_at_begin+static_cast<unsigned int>(length)]);
59-
return;
50+
return cbor_variant { string(&in[offset_at_begin], &in[offset_at_begin+static_cast<unsigned int>(length)]) };
6051
}
6152

6253
case 4: { // arrays
6354
int total_items=read_integer_header(in, h, offset);
64-
dest->emplace<array>(static_cast<unsigned>(total_items), cbor_variant());
65-
cbor_array* current_array=&get<array>(*dest);
55+
cbor_variant rtn=cbor_variant { cbor_array(static_cast<unsigned>(total_items), cbor_variant()) };
6656
for (unsigned int this_item=0; this_item<static_cast<unsigned>(total_items); this_item++) {
67-
cbor_variant* value_dest=&current_array->at(this_item);
68-
construct_from_into(in, value_dest, offset);
57+
get<cbor_array>(rtn)[this_item]=construct_from(in, offset);
6958
}
70-
return;
59+
return rtn;
7160
}
7261

7362
case 5: { // maps
74-
dest->emplace<map>();
75-
cbor_map* current_map=&get<map>(*dest);
63+
cbor_variant rtn=cbor_variant { cbor_map() };
7664
for (int pending_items=read_integer_header(in, h, offset); pending_items>0; pending_items--) {
7765
// get the key
7866
h=reinterpret_cast<const header*>(&in[*offset]);
@@ -83,38 +71,33 @@ void cbor_variant::construct_from_into(const std::vector<uint8_t>& in, cbor_vari
8371
*offset+=static_cast<unsigned int>(key_length);
8472

8573
// create the variant
86-
current_map->emplace(key, cbor_variant());
87-
cbor_variant* value_dest=&current_map->at(key);
88-
construct_from_into(in, value_dest, offset);
74+
get<cbor_map>(rtn)[key]=construct_from(in, offset);
8975
}
90-
return;
76+
return rtn;
9177
}
9278

9379
case 6: { // tags (are ignored)
94-
read_integer_header(in, h, offset);
95-
return;
80+
read_integer_header(in, h, offset); // skip
81+
return construct_from(in, offset);
9682
}
9783

9884
case 7: { // floats and none
9985
const uint8_t* first_data_byte=in.data()+*offset+1;
10086
if (h->additional==26) { // single precision
10187
*offset+=5;
10288
float rtn;
103-
float_to_big_endian(first_data_byte, reinterpret_cast<uint8_t*>(&rtn), 4);
104-
dest->emplace<floating_point>(rtn);
105-
return;
89+
float_to_big_endian(first_data_byte, reinterpret_cast<uint8_t*>(&rtn));
90+
return cbor_variant { rtn };
10691
}
107-
if (h->additional==27) { // double precision, gets cast down to single
92+
if (h->additional==27) { // double precision
10893
*offset+=9;
10994
double rtn;
110-
float_to_big_endian(first_data_byte, reinterpret_cast<uint8_t*>(&rtn), 8);
111-
dest->emplace<floating_point>(static_cast<float>(rtn));
112-
return;
95+
double_to_big_endian(first_data_byte, reinterpret_cast<uint8_t*>(&rtn));
96+
return cbor_variant { rtn };
11397
}
11498
if (h->additional==22) {
11599
*offset+=1;
116-
dest->emplace<none>();
117-
return;
100+
return cbor_variant { monostate() };
118101
}
119102
throw runtime_error("Asked to process a major type 7 that is neither a float nor a double");
120103
}
@@ -138,13 +121,13 @@ void cbor_variant::encode_onto(std::vector<uint8_t>* in) const
138121

139122
// https://tools.ietf.org/html/rfc7049#section-2.3
140123
case floating_point: { // floats
141-
header h(7, 26);
124+
header h(7, 27);
142125
h.append_onto(in);
143-
float val=get<floating_point>(*this);
144-
float big_endian;
126+
double val=get<floating_point>(*this);
127+
double big_endian;
145128
uint8_t* p_big_endian=reinterpret_cast<uint8_t*>(&big_endian);
146-
float_to_big_endian(reinterpret_cast<uint8_t*>(&val), p_big_endian, sizeof(float));
147-
in->insert(in->end(), p_big_endian, p_big_endian+sizeof(float));
129+
double_to_big_endian(reinterpret_cast<uint8_t*>(&val), p_big_endian);
130+
in->insert(in->end(), p_big_endian, p_big_endian+sizeof(double));
148131
return;
149132
}
150133

@@ -268,23 +251,40 @@ int cbor_variant::read_integer_header(const std::vector<uint8_t>& in, const head
268251
}
269252
}
270253

271-
void cbor_variant::float_to_big_endian(const uint8_t* p_src, uint8_t* p_dest, unsigned int bytes)
254+
void cbor_variant::float_to_big_endian(const uint8_t* p_src, uint8_t* p_dest)
272255
{
273256
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
274-
p_dest+=bytes-1;
275-
while (bytes>0) {
276-
*p_dest=*p_src;
277-
++p_src;
278-
--p_dest;
279-
--bytes;
280-
}
257+
*p_dest++=p_src[3];
258+
*p_dest++=p_src[2];
259+
*p_dest++=p_src[1];
260+
*p_dest =p_src[0];
281261
#else
282-
while (bytes>0) {
283-
*p_dest=*p_src;
284-
++p_src;
285-
++p_dest;
286-
--bytes;
287-
}
288-
}
262+
*p_dest++=*p_src++;
263+
*p_dest++=*p_src++;
264+
*p_dest++=*p_src++;
265+
*p_dest =*p_src;
266+
#endif
267+
};
268+
269+
void cbor_variant::double_to_big_endian(const uint8_t* p_src, uint8_t* p_dest)
270+
{
271+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
272+
*p_dest++=p_src[7];
273+
*p_dest++=p_src[6];
274+
*p_dest++=p_src[5];
275+
*p_dest++=p_src[4];
276+
*p_dest++=p_src[3];
277+
*p_dest++=p_src[2];
278+
*p_dest++=p_src[1];
279+
*p_dest =p_src[0];
280+
#else
281+
*p_dest++=*p_src++;
282+
*p_dest++=*p_src++;
283+
*p_dest++=*p_src++;
284+
*p_dest++=*p_src++;
285+
*p_dest++=*p_src++;
286+
*p_dest++=*p_src++;
287+
*p_dest++=*p_src++;
288+
*p_dest =*p_src;
289289
#endif
290290
};
Collapse file

‎cppbor/cppbor.hpp‎

Copy file name to clipboardExpand all lines: cppbor/cppbor.hpp
+10-6Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,22 @@
1919
#include <variant>
2020
#include <string>
2121
#include <vector>
22-
#include <unordered_map>
22+
#include <map>
23+
#include <initializer_list>
24+
#include <iostream>
2325

2426
struct cbor_variant;
2527
typedef std::vector<cbor_variant> cbor_array;
26-
typedef std::unordered_map<std::string, cbor_variant> cbor_map;
28+
typedef std::map<std::string, cbor_variant> cbor_map;
29+
typedef std::variant<int, double, std::string, std::monostate, std::vector<uint8_t>, cbor_array, cbor_map> cbor_baseclass;
2730

2831
// Not supported: 64 bit ints, indefinite lengths
2932
// Map keys are assumed to be std::string
30-
struct cbor_variant : std::variant<int, float, std::string, std::monostate, std::vector<uint8_t>, cbor_array, cbor_map>
33+
struct cbor_variant : cbor_baseclass
3134
{
3235
// construct a variant from a vector of bytes
33-
static void construct_from_into(const std::vector<uint8_t>& in, cbor_variant* dest);
34-
static void construct_from_into(const std::vector<uint8_t>& in, cbor_variant* dest, unsigned int* offset);
36+
static cbor_variant construct_from(const std::vector<uint8_t>& in);
37+
static cbor_variant construct_from(const std::vector<uint8_t>& in, unsigned int* offset);
3538

3639
// encode this variant onto the end of the passed vector
3740
void encode_onto(std::vector<uint8_t>* in) const;
@@ -81,7 +84,8 @@ struct cbor_variant : std::variant<int, float, std::string, std::monostate, std:
8184
static unsigned int integer_length(int additional);
8285
static void append_integer_header(unsigned int major, unsigned int val, std::vector<uint8_t>* in);
8386
static int read_integer_header(const std::vector<uint8_t>& in, const header* h, unsigned int* offset);
84-
static void float_to_big_endian(const uint8_t* p_src, uint8_t* p_dest, unsigned int bytes);
87+
static void float_to_big_endian(const uint8_t* p_src, uint8_t* p_dest);
88+
static void double_to_big_endian(const uint8_t* p_src, uint8_t* p_dest);
8589
};
8690

8791
#endif /* cppbor_hpp */

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.