/* * Copyright (c) 2023 Huawei Device Co., Ltd. * 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. */ #ifndef ECMASCRIPT_BASE_FILE_HEADER_H #define ECMASCRIPT_BASE_FILE_HEADER_H #include "ecmascript/base/string_helper.h" #include "ecmascript/log_wrapper.h" #include #include #include namespace panda::ecmascript::base { class FileHeader { protected: static constexpr size_t MAGIC_SIZE = 8; static constexpr size_t VERSION_SIZE = 4; static constexpr std::array MAGIC = {'P', 'A', 'N', 'D', 'A', '\0', '\0', '\0'}; FileHeader(const std::array &lastVersion) : magic_(MAGIC), version_(lastVersion) {} bool VerifyInner(const char* fileDesc, const std::array &lastVersion) const { if (magic_ != MAGIC) { LOG_HOST_TOOL_ERROR << "Magic mismatch, please make sure " << fileDesc << " and the source code are matched"; LOG_ECMA(ERROR) << "magic error, expected magic is " << ConvToStr(MAGIC) << ", but got " << ConvToStr(magic_); return false; } if (version_ > lastVersion) { LOG_HOST_TOOL_ERROR << fileDesc << " version error, expected version should be less or equal than " << ConvToStr(lastVersion) << ", but got " << GetVersionInner(); return false; } LOG_ECMA(DEBUG) << "Magic:" << ConvToStr(magic_) << ", version:" << GetVersionInner(); return true; } std::string GetVersionInner() const { return ConvToStr(version_); } bool SetVersionInner(std::string version) { std::vector versionNumber = StringHelper::SplitString(version, "."); if (versionNumber.size() != VERSION_SIZE) { LOG_ECMA(ERROR) << "version: " << version << " format error"; return false; } for (uint32_t i = 0; i < VERSION_SIZE; i++) { uint32_t result; if (!StringHelper::StrToUInt32(versionNumber[i].c_str(), &result)) { LOG_ECMA(ERROR) << "version: " << version << " format error"; return false; } version_[i] = static_cast(result); } return true; } private: template std::string ConvToStr(std::array array) const { std::string ret = ""; for (size_t i = 0; i < size; ++i) { if (i) { ret += "."; } ret += std::to_string(array[i]); } return ret; } std::array magic_; std::array version_; }; } // namespace panda::ecmascript::base #endif // ECMASCRIPT_BASE_FILE_HEADER_H