LLVM: lib/BinaryFormat/MsgPackReader.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

17

18using namespace llvm;

21

23 : InputBuffer(InputBuffer), Current(InputBuffer.getBufferStart()),

24 End(InputBuffer.getBufferEnd()) {}

25

27

29 if (Current == End)

30 return false;

31

33

34 switch (FB) {

35 case FirstByte::Nil:

37 return true;

38 case FirstByte::True:

40 Obj.Bool = true;

41 return true;

42 case FirstByte::False:

44 Obj.Bool = false;

45 return true;

46 case FirstByte::Int8:

48 return readInt<int8_t>(Obj);

49 case FirstByte::Int16:

51 return readInt<int16_t>(Obj);

52 case FirstByte::Int32:

54 return readInt<int32_t>(Obj);

55 case FirstByte::Int64:

57 return readInt<int64_t>(Obj);

58 case FirstByte::UInt8:

60 return readUInt<uint8_t>(Obj);

61 case FirstByte::UInt16:

63 return readUInt<uint16_t>(Obj);

64 case FirstByte::UInt32:

66 return readUInt<uint32_t>(Obj);

67 case FirstByte::UInt64:

69 return readUInt<uint64_t>(Obj);

70 case FirstByte::Float32:

72 if (sizeof(float) > remainingSpace())

74 "Invalid Float32 with insufficient payload",

75 std::make_error_code(std::errc::invalid_argument));

76 Obj.Float =

78 Current += sizeof(float);

79 return true;

80 case FirstByte::Float64:

82 if (sizeof(double) > remainingSpace())

84 "Invalid Float64 with insufficient payload",

85 std::make_error_code(std::errc::invalid_argument));

86 Obj.Float =

88 Current += sizeof(double);

89 return true;

90 case FirstByte::Str8:

92 return readRaw<uint8_t>(Obj);

93 case FirstByte::Str16:

95 return readRaw<uint16_t>(Obj);

96 case FirstByte::Str32:

98 return readRaw<uint32_t>(Obj);

99 case FirstByte::Bin8:

101 return readRaw<uint8_t>(Obj);

102 case FirstByte::Bin16:

104 return readRaw<uint16_t>(Obj);

105 case FirstByte::Bin32:

107 return readRaw<uint32_t>(Obj);

108 case FirstByte::Array16:

110 return readLength<uint16_t>(Obj);

111 case FirstByte::Array32:

113 return readLength<uint32_t>(Obj);

114 case FirstByte::Map16:

116 return readLength<uint16_t>(Obj);

117 case FirstByte::Map32:

119 return readLength<uint32_t>(Obj);

120 case FirstByte::FixExt1:

122 return createExt(Obj, FixLen::Ext1);

123 case FirstByte::FixExt2:

125 return createExt(Obj, FixLen::Ext2);

126 case FirstByte::FixExt4:

128 return createExt(Obj, FixLen::Ext4);

129 case FirstByte::FixExt8:

131 return createExt(Obj, FixLen::Ext8);

132 case FirstByte::FixExt16:

134 return createExt(Obj, FixLen::Ext16);

135 case FirstByte::Ext8:

137 return readExt<uint8_t>(Obj);

138 case FirstByte::Ext16:

140 return readExt<uint16_t>(Obj);

141 case FirstByte::Ext32:

143 return readExt<uint32_t>(Obj);

144 }

145

146 if ((FB & FixBitsMask::NegativeInt) == FixBits::NegativeInt) {

148 int8_t I;

149 static_assert(sizeof(I) == sizeof(FB), "Unexpected type sizes");

150 memcpy(&I, &FB, sizeof(FB));

151 Obj.Int = I;

152 return true;

153 }

154

155 if ((FB & FixBitsMask::PositiveInt) == FixBits::PositiveInt) {

157 Obj.UInt = FB;

158 return true;

159 }

160

161 if ((FB & FixBitsMask::String) == FixBits::String) {

164 return createRaw(Obj, Size);

165 }

166

167 if ((FB & FixBitsMask::Array) == FixBits::Array) {

170 return true;

171 }

172

173 if ((FB & FixBitsMask::Map) == FixBits::Map) {

176 return true;

177 }

178

180 "Invalid first byte", std::make_error_code(std::errc::invalid_argument));

181}

182

184 if (sizeof(T) > remainingSpace())

186 "Invalid Raw with insufficient payload",

187 std::make_error_code(std::errc::invalid_argument));

189 Current += sizeof(T);

190 return createRaw(Obj, Size);

191}

192

194 if (sizeof(T) > remainingSpace())

196 "Invalid Int with insufficient payload",

197 std::make_error_code(std::errc::invalid_argument));

199 Current += sizeof(T);

200 return true;

201}

202

204 if (sizeof(T) > remainingSpace())

206 "Invalid Int with insufficient payload",

207 std::make_error_code(std::errc::invalid_argument));

209 Current += sizeof(T);

210 return true;

211}

212

213template Expected Reader::readLength(Object &Obj) {

214 if (sizeof(T) > remainingSpace())

216 "Invalid Map/Array with invalid length",

217 std::make_error_code(std::errc::invalid_argument));

219 Current += sizeof(T);

220 return true;

221}

222

223template Expected Reader::readExt(Object &Obj) {

224 if (sizeof(T) > remainingSpace())

226 "Invalid Ext with invalid length",

227 std::make_error_code(std::errc::invalid_argument));

229 Current += sizeof(T);

230 return createExt(Obj, Size);

231}

232

233Expected Reader::createRaw(Object &Obj, uint32_t Size) {

234 if (Size > remainingSpace())

236 "Invalid Raw with insufficient payload",

237 std::make_error_code(std::errc::invalid_argument));

238 Obj.Raw = StringRef(Current, Size);

239 Current += Size;

240 return true;

241}

242

243Expected Reader::createExt(Object &Obj, uint32_t Size) {

244 if (Current == End)

246 "Invalid Ext with no type",

247 std::make_error_code(std::errc::invalid_argument));

248 Obj.Extension.Type = *Current++;

249 if (Size > remainingSpace())

251 "Invalid Ext with insufficient payload",

252 std::make_error_code(std::errc::invalid_argument));

253 Obj.Extension.Bytes = StringRef(Current, Size);

254 Current += Size;

255 return true;

256}

This is a MessagePack reader.

This file contains constants used for implementing MessagePack support.

The Input class is used to parse a yaml document into in-memory structs and vectors.

Tagged union holding either a T or a Error.

StringRef - Represent a constant reference to a string, i.e.

LLVM_ABI Reader(MemoryBufferRef InputBuffer)

Construct a reader, keeping a reference to the InputBuffer.

Definition MsgPackReader.cpp:22

LLVM_ABI Expected< bool > read(Object &Obj)

Read one object from the input buffer, advancing past it.

Definition MsgPackReader.cpp:28

Mask of bits used to identify "Fix" variants in MessagePack.

value_type read(const void *memory, endianness endian)

Read a value of a particular endianness from memory.

This is an optimization pass for GlobalISel generic memory operations.

Error make_error(ArgTs &&... Args)

Make a Error instance representing failure using the given error info type.

To bit_cast(const From &from) noexcept

MessagePack object, represented as a tagged union of C++ types.