• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 MUID_H
17 #define MUID_H
18 
19 // This is shared between maple compiler and runtime.
20 #include <cstdint>
21 #include <string>
22 #include <sstream>
23 #include <iomanip>
24 
25 #ifdef USE_32BIT_REF
26 #define USE_64BIT_MUID
27 #endif  // USE_32BIT_REF
28 
29 constexpr unsigned int kSystemNamespace = 0xc0;
30 constexpr unsigned int kApkNamespace = 0x80;
31 constexpr unsigned int kBitMask = 0x3f;
32 constexpr unsigned int kGroupSize = 64;
33 constexpr unsigned int kShiftAmount = 32;
34 constexpr unsigned int kBlockLength = 16;
35 constexpr unsigned int kByteLength = 8;
36 constexpr unsigned int kNumLowAndHigh = 2;
37 
38 #ifdef USE_64BIT_MUID
39 constexpr unsigned int kMuidLength = 8;
40 #else
41 constexpr unsigned int kMuidLength = 16;
42 #endif  // USE_64BIT_MUID
43 
44 constexpr unsigned int kDigestShortHashLength = 8;
45 constexpr unsigned int kDigestHashLength = 16;
46 union DigestHash {
47     uint8_t bytes[kDigestHashLength];
48     struct {
49         uint64_t first;
50         uint64_t second;
51     } data;
52 };
53 
54 // muid-related files are shared between maple compiler and runtime, thus not in
55 // namespace maplert
56 struct MuidContext {
57     unsigned int a;
58     unsigned int b;
59     unsigned int c;
60     unsigned int d;
61     unsigned int count[kNumLowAndHigh];
62     unsigned int block[kBlockLength];
63     unsigned char buffer[kGroupSize];
64 };
65 
66 class MUID {
67 public:
68     union {
69 #ifdef USE_64BIT_MUID
70         uint32_t words[kNumLowAndHigh] = {0};
71         uint8_t bytes[kMuidLength];
72         uint64_t raw;
73 #else
74         uint64_t words[kNumLowAndHigh] = {0};
75         uint8_t bytes[kMuidLength];
76 #endif  // USE_64BIT_MUID
77     } data;
78 
IsSystemNameSpace()79     inline bool IsSystemNameSpace() const
80     {
81         return (data.bytes[kMuidLength - 1] & ~kBitMask) == kSystemNamespace;
82     }
IsApkNameSpace()83     inline bool IsApkNameSpace() const
84     {
85         return (data.bytes[kMuidLength - 1] & ~kBitMask) == kApkNamespace;
86     }
SetSystemNameSpace()87     inline void SetSystemNameSpace()
88     {
89         data.bytes[kMuidLength - 1] &= kBitMask;
90         data.bytes[kMuidLength - 1] |= kSystemNamespace;
91     }
SetApkNameSpace()92     inline void SetApkNameSpace()
93     {
94         data.bytes[kMuidLength - 1] &= kBitMask;
95         data.bytes[kMuidLength - 1] |= kApkNamespace;
96     }
97     bool operator<(const MUID &muid) const
98     {
99         return (data.words[1] < muid.data.words[1] ||
100                 (data.words[1] == muid.data.words[1] && data.words[0] < muid.data.words[0]));
101     }
102     bool operator>(const MUID &muid) const
103     {
104         return (data.words[1] > muid.data.words[1] ||
105                 (data.words[1] == muid.data.words[1] && data.words[0] > muid.data.words[0]));
106     }
107     bool operator==(const MUID &muid) const
108     {
109         return data.words[1] == muid.data.words[1] && data.words[0] == muid.data.words[0];
110     }
111     bool operator!=(const MUID &muid) const
112     {
113         return data.words[1] != muid.data.words[1] || data.words[0] != muid.data.words[0];
114     }
ToStr()115     std::string ToStr() const
116     {
117         std::stringstream sbuf;
118 #ifdef USE_64BIT_MUID
119         // 8 spaces to 64 bit
120         sbuf << std::setfill('0') << std::setw(8) << std::hex << data.words[1] << std::setfill('0') << std::setw(8)
121              << std::hex << data.words[0];
122 #else
123         // 16 spaces to 32 bit
124         sbuf << std::setfill('0') << std::setw(16) << std::hex << data.words[1] << std::setfill('0') << std::setw(16)
125              << std::hex << data.words[0];
126 #endif  // USE_64BIT_MUID
127         return sbuf.str();
128     }
129 };
130 
131 void MuidInit(MuidContext &status);
132 void MuidDecode(MuidContext &status, const unsigned char &data, size_t size);
133 
134 template <typename T>
135 void FullEncode(T &result, MuidContext &status);
136 void MuidEncode(unsigned char (&result)[kDigestShortHashLength], MuidContext &status);
137 void MuidEncode(unsigned char (&result)[kDigestHashLength], MuidContext &status, bool use64Bit = false);
138 
139 void GetMUIDHash(const unsigned char &data, size_t size, MUID &muid);
140 DigestHash GetDigestHash(const unsigned char &bytes, uint32_t len);
141 MUID GetMUID(const std::string &symbolName, bool forSystem = true);
142 #endif
143