OSDN Git Service

am 241ffafa: Prepare for BoringSSL update.
[android-x86/system-extras.git] / simpleperf / build_id.h
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef SIMPLE_PERF_BUILD_ID_H_
18 #define SIMPLE_PERF_BUILD_ID_H_
19
20 #include <string.h>
21 #include <algorithm>
22 #include <base/stringprintf.h>
23
24 constexpr size_t BUILD_ID_SIZE = 20;
25
26 class BuildId {
27  public:
28   static size_t Size() {
29     return BUILD_ID_SIZE;
30   }
31
32   BuildId() {
33     memset(data_, '\0', BUILD_ID_SIZE);
34   }
35
36   BuildId(const void* data, size_t len = BUILD_ID_SIZE) : BuildId() {
37     memcpy(data_, data, std::min(len, BUILD_ID_SIZE));
38   }
39
40   const unsigned char* Data() const {
41     return data_;
42   }
43
44   std::string ToString() const {
45     std::string s = "0x";
46     for (size_t i = 0; i < BUILD_ID_SIZE; ++i) {
47       s += android::base::StringPrintf("%02x", data_[i]);
48     }
49     return s;
50   }
51
52   bool operator==(const BuildId& build_id) const {
53     return memcmp(data_, build_id.data_, BUILD_ID_SIZE) == 0;
54   }
55
56   bool IsEmpty() const {
57     static BuildId empty_build_id;
58     return *this == empty_build_id;
59   }
60
61  private:
62   unsigned char data_[BUILD_ID_SIZE];
63 };
64
65 #endif  // SIMPLE_PERF_BUILD_ID_H_