1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
/*
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 "encoder.h"
using namespace cbor;
encoder::encoder(output &out) {
_out = &out;
}
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));
} else if(value < 256) {
_out->put_byte((unsigned char) (major_type | 24));
_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);
} 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);
}
}
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));
} else if(value < 256ULL) {
_out->put_byte((unsigned char) (major_type | 24));
_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);
} 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);
} 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);
}
}
void encoder::write_int(unsigned int value) {
write_type_value(0, value);
}
void encoder::write_int(unsigned long long value) {
write_type_value(0, value);
}
void encoder::write_int(long long value) {
if(value < 0) {
write_type_value(1, (unsigned long long) -(value+1));
} else {
write_type_value(0, (unsigned long long) value);
}
}
void encoder::write_int(int value) {
if(value < 0) {
write_type_value(1, (unsigned int) -(value+1));
} else {
write_type_value(0, (unsigned int) value);
}
}
void encoder::write_bytes(const unsigned char *data, unsigned int size) {
write_type_value(2, size);
_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);
}
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());
}
void encoder::write_array(int size) {
write_type_value(4, (unsigned int) size);
}
void encoder::write_map(int size) {
write_type_value(5, (unsigned int) size);
}
void encoder::write_tag(const unsigned int tag) {
write_type_value(6, tag);
}
void encoder::write_special(int special) {
write_type_value(7, (unsigned int) special);
}
void encoder::write_bool(bool value) {
if (value == true) {
_out->put_byte((unsigned char) 0xf5);
} else {
_out->put_byte((unsigned char) 0xf4);
}
}
void encoder::write_null() {
_out->put_byte((unsigned char) 0xf6);
}
void encoder::write_undefined() {
_out->put_byte((unsigned char) 0xf7);
}
|