1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef MRT_INCLUDE_LITERALSTRNAME_H 17 #define MRT_INCLUDE_LITERALSTRNAME_H 18 #include <map> 19 #include <cstring> 20 #include "muid.h" 21 22 // literal string naming is shared between maple compiler and runtime, thus not in namespace maplert 23 const std::string kConstString = "_C_STR_"; 24 const std::string kConstStringPtr = "_PTR_C_STR_"; 25 const std::string kLocalStringPrefix = "L_STR_"; 26 constexpr int kConstStringLen = 7; 27 28 class LiteralStrName { 29 public: 30 static int32_t CalculateHashSwapByte(const char16_t *data, uint32_t len); CalculateHash(const char16_t * data,uint32_t len,bool dataIsCompress)31 static uint32_t CalculateHash(const char16_t *data, uint32_t len, bool dataIsCompress) 32 { 33 uint32_t hash = 0; 34 if (dataIsCompress) { 35 const char *dataStart = reinterpret_cast<const char *>(data); 36 const char *end = dataStart + len; 37 while (dataStart < end) { 38 hash = (hash << 5) - hash + static_cast<uint32_t>(*dataStart++); // 5 to calculate the hash code of data 39 } 40 } else { 41 const char16_t *end = data + len; 42 while (data < end) { 43 hash = (static_cast<unsigned int>(hash) << 5) - hash + *data++; // 5 to calculate the hash code of data 44 } 45 } 46 return hash; 47 } 48 49 static std::string GetHexStr(const uint8_t *bytes, uint32_t len); 50 static std::string GetLiteralStrName(const uint8_t *bytes, uint32_t len); 51 static std::string ComputeMuid(const uint8_t *bytes, uint32_t len); 52 }; 53 54 #endif 55