bool BytecodeUtil::getCustomSection()

in plugins/experimental/wasm/lib/src/bytecode_util.cc [99:138]


bool BytecodeUtil::getCustomSection(std::string_view bytecode, std::string_view name,
                                    std::string_view &ret) {
  // Check Wasm header.
  if (!checkWasmHeader(bytecode)) {
    return false;
  }

  // Skip the Wasm header.
  const char *pos = bytecode.data() + 8;
  const char *end = bytecode.data() + bytecode.size();
  while (pos < end) {
    if (pos + 1 > end) {
      return false;
    }
    const auto section_type = *pos++;
    uint32_t section_len = 0;
    if (!parseVarint(pos, end, section_len) || pos + section_len > end) {
      return false;
    }
    if (section_type == 0) {
      // Custom section.
      const char *section_end = pos + section_len;
      uint32_t section_name_len = 0;
      if (!BytecodeUtil::parseVarint(pos, section_end, section_name_len) ||
          pos + section_name_len > section_end) {
        return false;
      }
      if (section_name_len == name.size() && ::memcmp(pos, name.data(), section_name_len) == 0) {
        pos += section_name_len;
        ret = {pos, static_cast<size_t>(section_end - pos)};
        return true;
      }
      pos = section_end;
    } else {
      // Skip other sections.
      pos += section_len;
    }
  }
  return true;
}