MsgPackWriter.cpp
4.92 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//===- MsgPackWriter.cpp - Simple MsgPack writer ----------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file implements a MessagePack writer.
///
//===----------------------------------------------------------------------===//
#include "llvm/BinaryFormat/MsgPackWriter.h"
#include "llvm/BinaryFormat/MsgPack.h"
using namespace llvm;
using namespace msgpack;
Writer::Writer(raw_ostream &OS, bool Compatible)
: EW(OS, Endianness), Compatible(Compatible) {}
void Writer::writeNil() { EW.write(FirstByte::Nil); }
void Writer::write(bool b) { EW.write(b ? FirstByte::True : FirstByte::False); }
void Writer::write(int64_t i) {
if (i >= 0) {
write(static_cast<uint64_t>(i));
return;
}
if (i >= FixMin::NegativeInt) {
EW.write(static_cast<int8_t>(i));
return;
}
if (i >= INT8_MIN) {
EW.write(FirstByte::Int8);
EW.write(static_cast<int8_t>(i));
return;
}
if (i >= INT16_MIN) {
EW.write(FirstByte::Int16);
EW.write(static_cast<int16_t>(i));
return;
}
if (i >= INT32_MIN) {
EW.write(FirstByte::Int32);
EW.write(static_cast<int32_t>(i));
return;
}
EW.write(FirstByte::Int64);
EW.write(i);
}
void Writer::write(uint64_t u) {
if (u <= FixMax::PositiveInt) {
EW.write(static_cast<uint8_t>(u));
return;
}
if (u <= UINT8_MAX) {
EW.write(FirstByte::UInt8);
EW.write(static_cast<uint8_t>(u));
return;
}
if (u <= UINT16_MAX) {
EW.write(FirstByte::UInt16);
EW.write(static_cast<uint16_t>(u));
return;
}
if (u <= UINT32_MAX) {
EW.write(FirstByte::UInt32);
EW.write(static_cast<uint32_t>(u));
return;
}
EW.write(FirstByte::UInt64);
EW.write(u);
}
void Writer::write(double d) {
// If no loss of precision, encode as a Float32.
double a = std::fabs(d);
if (a >= std::numeric_limits<float>::min() &&
a <= std::numeric_limits<float>::max()) {
EW.write(FirstByte::Float32);
EW.write(static_cast<float>(d));
} else {
EW.write(FirstByte::Float64);
EW.write(d);
}
}
void Writer::write(StringRef s) {
size_t Size = s.size();
if (Size <= FixMax::String)
EW.write(static_cast<uint8_t>(FixBits::String | Size));
else if (!Compatible && Size <= UINT8_MAX) {
EW.write(FirstByte::Str8);
EW.write(static_cast<uint8_t>(Size));
} else if (Size <= UINT16_MAX) {
EW.write(FirstByte::Str16);
EW.write(static_cast<uint16_t>(Size));
} else {
assert(Size <= UINT32_MAX && "String object too long to be encoded");
EW.write(FirstByte::Str32);
EW.write(static_cast<uint32_t>(Size));
}
EW.OS << s;
}
void Writer::write(MemoryBufferRef Buffer) {
assert(!Compatible && "Attempt to write Bin format in compatible mode");
size_t Size = Buffer.getBufferSize();
if (Size <= UINT8_MAX) {
EW.write(FirstByte::Bin8);
EW.write(static_cast<uint8_t>(Size));
} else if (Size <= UINT16_MAX) {
EW.write(FirstByte::Bin16);
EW.write(static_cast<uint16_t>(Size));
} else {
assert(Size <= UINT32_MAX && "Binary object too long to be encoded");
EW.write(FirstByte::Bin32);
EW.write(static_cast<uint32_t>(Size));
}
EW.OS.write(Buffer.getBufferStart(), Size);
}
void Writer::writeArraySize(uint32_t Size) {
if (Size <= FixMax::Array) {
EW.write(static_cast<uint8_t>(FixBits::Array | Size));
return;
}
if (Size <= UINT16_MAX) {
EW.write(FirstByte::Array16);
EW.write(static_cast<uint16_t>(Size));
return;
}
EW.write(FirstByte::Array32);
EW.write(Size);
}
void Writer::writeMapSize(uint32_t Size) {
if (Size <= FixMax::Map) {
EW.write(static_cast<uint8_t>(FixBits::Map | Size));
return;
}
if (Size <= UINT16_MAX) {
EW.write(FirstByte::Map16);
EW.write(static_cast<uint16_t>(Size));
return;
}
EW.write(FirstByte::Map32);
EW.write(Size);
}
void Writer::writeExt(int8_t Type, MemoryBufferRef Buffer) {
size_t Size = Buffer.getBufferSize();
switch (Size) {
case FixLen::Ext1:
EW.write(FirstByte::FixExt1);
break;
case FixLen::Ext2:
EW.write(FirstByte::FixExt2);
break;
case FixLen::Ext4:
EW.write(FirstByte::FixExt4);
break;
case FixLen::Ext8:
EW.write(FirstByte::FixExt8);
break;
case FixLen::Ext16:
EW.write(FirstByte::FixExt16);
break;
default:
if (Size <= UINT8_MAX) {
EW.write(FirstByte::Ext8);
EW.write(static_cast<uint8_t>(Size));
} else if (Size <= UINT16_MAX) {
EW.write(FirstByte::Ext16);
EW.write(static_cast<uint16_t>(Size));
} else {
assert(Size <= UINT32_MAX && "Ext size too large to be encoded");
EW.write(FirstByte::Ext32);
EW.write(static_cast<uint32_t>(Size));
}
}
EW.write(Type);
EW.OS.write(Buffer.getBufferStart(), Size);
}