MCPcopy Create free account
hub / github.com/WasmEdge/WasmEdge / readName

Method readName

lib/loader/filemgr.cpp:270–352  ·  view source on GitHub ↗

Read a vector of bytes. See "include/loader/filemgr.h".

Source from the content-addressed store, hash-verified

268
269// Read a vector of bytes. See "include/loader/filemgr.h".
270Expect<std::string> FileMgr::readName() {
271 if (unlikely(Status != ErrCode::Value::Success)) {
272 return Unexpect(Status);
273 }
274 // If UTF-8 validation, readU32(), or readBytes() failed, the last successful
275 // reading offset will be at the start of `Name`.
276 LastPos = Pos;
277
278 // Read the name size.
279 EXPECTED_TRY(uint32_t SizeToRead, readU32());
280
281 // Check whether the string length exceeds the data boundary.
282 if (auto Res = testRead(SizeToRead); unlikely(!Res)) {
283 return Unexpect(ErrCode::Value::LengthOutOfBounds);
284 }
285
286 // Read the UTF-8 bytes.
287 std::string Str(SizeToRead, '\0');
288 EXPECTED_TRY(
289 readBytes(Span<Byte>(reinterpret_cast<Byte *>(Str.data()), Str.size())));
290
291 // UTF-8 validation.
292 bool Valid = true;
293 for (uint32_t I = 0; I < Str.size() && Valid; ++I) {
294 char C = Str.data()[I];
295 uint32_t N = 0;
296 if ((C & '\x80') == 0) {
297 // 0xxxxxxx, 7 bits UCS, ASCII
298 N = 0;
299 } else if ((C & '\xE0') == '\xC0') {
300 // 110xxxxx, 11 bits UCS, U+80 to U+7FF
301 N = 1;
302 } else if ((C & '\xF0') == '\xE0') {
303 // 1110xxxx, 16 bits UCS, U+800 to U+D7FF and U+E000 to U+FFFF
304 N = 2;
305 } else if ((C & '\xF8') == '\xF0') {
306 // 11110xxx, 21 bits UCS, U+10000 to U+10FFFF
307 N = 3;
308 } else {
309 Valid = false;
310 }
311
312 // Need N more bytes.
313 if (I + N >= Str.size()) {
314 Valid = false;
315 }
316 // Invalid ranges
317 if (N == 1 && (C & '\xDE') == '\xC0') {
318 // 11 bits UCS, U+0 to U+80, FAIL
319 Valid = false;
320 } else if (N == 2 &&
321 ((C == '\xE0' && (Str.data()[I + 1] & '\xA0') == '\x80') ||
322 // 16 bits UCS, U+0 to U+7FF, FAIL
323 (C == '\xED' && (Str.data()[I + 1] & '\xA0') == '\xA0')
324 // 16 bits UCS, U+D800 to U+DFFF, FAIL
325 )) {
326 Valid = false;
327 } else if (N == 3 &&

Callers 8

loadDescMethod · 0.80
loadModuleAOTMethod · 0.80
loadExternNameMethod · 0.80
loadTypeMethod · 0.80
loadDeclMethod · 0.80
loadCoreInstanceMethod · 0.80
loadInstanceMethod · 0.80
TESTFunction · 0.80

Calls 5

unlikelyFunction · 0.85
UnexpectFunction · 0.85
EXPECTED_TRYFunction · 0.50
dataMethod · 0.45
sizeMethod · 0.45

Tested by 1

TESTFunction · 0.64