1 /*
2 Copyright 2014-2015 Stanislav Ovsyannikov
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 implied. See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 #include "encoder.h"
18
19 using namespace cbor;
20
encoder(output & out)21 encoder::encoder(output &out) { _out = &out; }
22
~encoder()23 encoder::~encoder() {}
24
write_type_value(int major_type,unsigned int value)25 void encoder::write_type_value(int major_type, unsigned int value) {
26 major_type <<= 5;
27 if (value < 24) {
28 _out->put_byte((unsigned char)(major_type | value));
29 } else if (value < 256) {
30 _out->put_byte((unsigned char)(major_type | 24));
31 _out->put_byte((unsigned char)value);
32 } else if (value < 65536) {
33 _out->put_byte((unsigned char)(major_type | 25));
34 _out->put_byte((unsigned char)(value >> 8));
35 _out->put_byte((unsigned char)value);
36 } else {
37 _out->put_byte((unsigned char)(major_type | 26));
38 _out->put_byte((unsigned char)(value >> 24));
39 _out->put_byte((unsigned char)(value >> 16));
40 _out->put_byte((unsigned char)(value >> 8));
41 _out->put_byte((unsigned char)value);
42 }
43 }
44
write_type_value(int major_type,unsigned long long value)45 void encoder::write_type_value(int major_type, unsigned long long value) {
46 major_type <<= 5;
47 if (value < 24ULL) {
48 _out->put_byte((unsigned char)(major_type | value));
49 } else if (value < 256ULL) {
50 _out->put_byte((unsigned char)(major_type | 24));
51 _out->put_byte((unsigned char)value);
52 } else if (value < 65536ULL) {
53 _out->put_byte((unsigned char)(major_type | 25));
54 _out->put_byte((unsigned char)(value >> 8));
55 _out->put_byte((unsigned char)value);
56 } else if (value < 4294967296ULL) {
57 _out->put_byte((unsigned char)(major_type | 26));
58 _out->put_byte((unsigned char)(value >> 24));
59 _out->put_byte((unsigned char)(value >> 16));
60 _out->put_byte((unsigned char)(value >> 8));
61 _out->put_byte((unsigned char)value);
62 } else {
63 _out->put_byte((unsigned char)(major_type | 27));
64 _out->put_byte((unsigned char)(value >> 56));
65 _out->put_byte((unsigned char)(value >> 48));
66 _out->put_byte((unsigned char)(value >> 40));
67 _out->put_byte((unsigned char)(value >> 32));
68 _out->put_byte((unsigned char)(value >> 24));
69 _out->put_byte((unsigned char)(value >> 16));
70 _out->put_byte((unsigned char)(value >> 8));
71 _out->put_byte((unsigned char)value);
72 }
73 }
74
write_int(unsigned int value)75 void encoder::write_int(unsigned int value) { write_type_value(0, value); }
76
write_int(unsigned long long value)77 void encoder::write_int(unsigned long long value) {
78 write_type_value(0, value);
79 }
80
write_int(long long value)81 void encoder::write_int(long long value) {
82 if (value < 0) {
83 write_type_value(1, (unsigned long long)-(value + 1));
84 } else {
85 write_type_value(0, (unsigned long long)value);
86 }
87 }
88
write_int(int value)89 void encoder::write_int(int value) {
90 if (value < 0) {
91 write_type_value(1, (unsigned int)-(value + 1));
92 } else {
93 write_type_value(0, (unsigned int)value);
94 }
95 }
96
write_bytes(const unsigned char * data,unsigned int size)97 void encoder::write_bytes(const unsigned char *data, unsigned int size) {
98 write_type_value(2, size);
99 _out->put_bytes(data, size);
100 }
101
write_string(const char * data,unsigned int size)102 void encoder::write_string(const char *data, unsigned int size) {
103 write_type_value(3, size);
104 _out->put_bytes((const unsigned char *)data, size);
105 }
106
write_string(const std::string str)107 void encoder::write_string(const std::string str) {
108 write_type_value(3, (unsigned int)str.size());
109 _out->put_bytes((const unsigned char *)str.c_str(), (int)str.size());
110 }
111
write_array(int size)112 void encoder::write_array(int size) { write_type_value(4, (unsigned int)size); }
113
write_map(int size)114 void encoder::write_map(int size) { write_type_value(5, (unsigned int)size); }
115
write_tag(const unsigned int tag)116 void encoder::write_tag(const unsigned int tag) { write_type_value(6, tag); }
117
write_special(int special)118 void encoder::write_special(int special) {
119 write_type_value(7, (unsigned int)special);
120 }
121
write_bool(bool value)122 void encoder::write_bool(bool value) {
123 if (value == true) {
124 _out->put_byte((unsigned char)0xf5);
125 } else {
126 _out->put_byte((unsigned char)0xf4);
127 }
128 }
129
write_null()130 void encoder::write_null() { _out->put_byte((unsigned char)0xf6); }
131
write_undefined()132 void encoder::write_undefined() { _out->put_byte((unsigned char)0xf7); }