2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef SIMPLE_PERF_BUILD_ID_H_
18 #define SIMPLE_PERF_BUILD_ID_H_
22 #include <android-base/stringprintf.h>
24 constexpr size_t BUILD_ID_SIZE = 20;
28 static size_t Size() {
33 memset(data_, '\0', BUILD_ID_SIZE);
36 // Copy build id from a byte array, like {0x76, 0x00, 0x32,...}.
37 BuildId(const void* data, size_t len) : BuildId() {
38 memcpy(data_, data, std::min(len, BUILD_ID_SIZE));
41 // Read build id from a hex string, like "7600329e31058e12b145d153ef27cd40e1a5f7b9".
42 explicit BuildId(const std::string& s) : BuildId() {
43 for (size_t i = 0; i < s.size() && i < BUILD_ID_SIZE * 2; i += 2) {
45 for (size_t j = i; j < i + 2; ++j) {
47 if (s[j] >= '0' && s[j] <= '9') {
49 } else if (s[j] >= 'a' && s[j] <= 'f') {
50 ch |= s[j] - 'a' + 10;
51 } else if (s[j] >= 'A' && s[j] <= 'F') {
52 ch |= s[j] - 'A' + 10;
59 const unsigned char* Data() const {
63 std::string ToString() const {
65 for (size_t i = 0; i < BUILD_ID_SIZE; ++i) {
66 s += android::base::StringPrintf("%02x", data_[i]);
71 bool operator==(const BuildId& build_id) const {
72 return memcmp(data_, build_id.data_, BUILD_ID_SIZE) == 0;
75 bool operator!=(const BuildId& build_id) const {
76 return !(*this == build_id);
79 bool IsEmpty() const {
80 static BuildId empty_build_id;
81 return *this == empty_build_id;
85 unsigned char data_[BUILD_ID_SIZE];
88 #endif // SIMPLE_PERF_BUILD_ID_H_