blob: e88ce17a268d222b81723b0ef5dce9b6ffc58a07 [file] [log] [blame]
Steven Moreland0e4be1e2017-04-18 19:50:29 -07001/*
2 * Copyright (C) 2017 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
Steven Morelandec50fdf2018-07-17 14:56:20 -070017#include <hidl-hash/Hash.h>
Steven Moreland0e4be1e2017-04-18 19:50:29 -070018
Steven Moreland81bcf682017-08-23 17:16:21 -070019#include <algorithm>
Steven Moreland0e4be1e2017-04-18 19:50:29 -070020#include <fstream>
21#include <iomanip>
22#include <map>
Steven Moreland218625a2017-04-18 22:31:50 -070023#include <regex>
Steven Moreland0e4be1e2017-04-18 19:50:29 -070024#include <sstream>
25
Steven Moreland218625a2017-04-18 22:31:50 -070026#include <android-base/logging.h>
Steven Moreland0e4be1e2017-04-18 19:50:29 -070027#include <openssl/sha.h>
28
29namespace android {
30
Steven Moreland81bcf682017-08-23 17:16:21 -070031const std::vector<uint8_t> Hash::kEmptyHash = std::vector<uint8_t>(SHA256_DIGEST_LENGTH, 0);
32
33Hash& Hash::getMutableHash(const std::string& path) {
Steven Moreland0e4be1e2017-04-18 19:50:29 -070034 static std::map<std::string, Hash> hashes;
35
36 auto it = hashes.find(path);
37
38 if (hashes.find(path) == hashes.end()) {
39 it = hashes.insert(it, {path, Hash(path)});
40 }
41
42 return it->second;
43}
44
Steven Moreland81bcf682017-08-23 17:16:21 -070045const Hash& Hash::getHash(const std::string& path) {
46 return getMutableHash(path);
47}
48
49void Hash::clearHash(const std::string& path) {
50 getMutableHash(path).mHash = kEmptyHash;
51}
52
Steven Morelandec50fdf2018-07-17 14:56:20 -070053static std::vector<uint8_t> sha256File(const std::string& path) {
Steven Moreland0e4be1e2017-04-18 19:50:29 -070054 std::ifstream stream(path);
55 std::stringstream fileStream;
56 fileStream << stream.rdbuf();
57 std::string fileContent = fileStream.str();
58
59 std::vector<uint8_t> ret = std::vector<uint8_t>(SHA256_DIGEST_LENGTH);
60
Steven Morelandec50fdf2018-07-17 14:56:20 -070061 SHA256(reinterpret_cast<const uint8_t*>(fileContent.c_str()), fileContent.size(), ret.data());
Steven Moreland0e4be1e2017-04-18 19:50:29 -070062
63 return ret;
64}
65
Steven Morelandec50fdf2018-07-17 14:56:20 -070066Hash::Hash(const std::string& path) : mPath(path), mHash(sha256File(path)) {}
Steven Moreland0e4be1e2017-04-18 19:50:29 -070067
Steven Morelandec50fdf2018-07-17 14:56:20 -070068std::string Hash::hexString(const std::vector<uint8_t>& hash) {
Steven Moreland0e4be1e2017-04-18 19:50:29 -070069 std::ostringstream s;
70 s << std::hex << std::setfill('0');
Tri Vo59ce7422017-04-26 10:53:54 -070071 for (uint8_t i : hash) {
Steven Moreland0e4be1e2017-04-18 19:50:29 -070072 s << std::setw(2) << static_cast<int>(i);
73 }
74 return s.str();
75}
76
Tri Vo59ce7422017-04-26 10:53:54 -070077std::string Hash::hexString() const {
78 return hexString(mHash);
79}
80
Steven Morelandec50fdf2018-07-17 14:56:20 -070081const std::vector<uint8_t>& Hash::raw() const {
Steven Moreland0e4be1e2017-04-18 19:50:29 -070082 return mHash;
83}
84
Steven Morelandec50fdf2018-07-17 14:56:20 -070085const std::string& Hash::getPath() const {
Steven Moreland0e4be1e2017-04-18 19:50:29 -070086 return mPath;
87}
88
Steven Moreland5bdfa702017-04-18 23:20:39 -070089#define HASH "([0-9a-f]+)"
90#define FQNAME "([^\\s]+)"
Steven Moreland218625a2017-04-18 22:31:50 -070091#define SPACES " +"
92#define MAYBE_SPACES " *"
93#define OPTIONAL_COMMENT "(?:#.*)?"
Steven Morelandec50fdf2018-07-17 14:56:20 -070094static const std::regex kHashLine("(?:" MAYBE_SPACES HASH SPACES FQNAME MAYBE_SPACES
95 ")?" OPTIONAL_COMMENT);
Steven Moreland218625a2017-04-18 22:31:50 -070096
97struct HashFile {
Steven Morelandec50fdf2018-07-17 14:56:20 -070098 static const HashFile* parse(const std::string& path, std::string* err) {
Steven Moreland218625a2017-04-18 22:31:50 -070099 static std::map<std::string, HashFile*> hashfiles;
100 auto it = hashfiles.find(path);
101
102 if (it == hashfiles.end()) {
103 it = hashfiles.insert(it, {path, readHashFile(path, err)});
104 }
105
106 return it->second;
107 }
108
Steven Morelandec50fdf2018-07-17 14:56:20 -0700109 std::vector<std::string> lookup(const std::string& fqName) const {
Steven Moreland218625a2017-04-18 22:31:50 -0700110 auto it = hashes.find(fqName);
111
112 if (it == hashes.end()) {
113 return {};
114 }
115
116 return it->second;
117 }
118
Steven Morelandec50fdf2018-07-17 14:56:20 -0700119 private:
120 static HashFile* readHashFile(const std::string& path, std::string* err) {
Steven Moreland218625a2017-04-18 22:31:50 -0700121 std::ifstream stream(path);
122 if (!stream) {
123 return nullptr;
124 }
125
Steven Morelandec50fdf2018-07-17 14:56:20 -0700126 HashFile* file = new HashFile();
Steven Moreland218625a2017-04-18 22:31:50 -0700127 file->path = path;
128
129 std::string line;
Steven Morelandec50fdf2018-07-17 14:56:20 -0700130 while (std::getline(stream, line)) {
Steven Moreland218625a2017-04-18 22:31:50 -0700131 std::smatch match;
132 bool valid = std::regex_match(line, match, kHashLine);
133
134 if (!valid) {
135 *err = "Error reading line from " + path + ": " + line;
136 delete file;
137 return nullptr;
138 }
139
140 CHECK_EQ(match.size(), 3u);
141
142 std::string hash = match.str(1);
143 std::string fqName = match.str(2);
144
145 if (hash.size() == 0 && fqName.size() == 0) {
146 continue;
147 }
148
149 if (hash.size() == 0 || fqName.size() == 0) {
150 *err = "Hash or fqName empty on " + path + ": " + line;
151 delete file;
152 return nullptr;
153 }
154
155 file->hashes[fqName].push_back(hash);
156 }
157 return file;
158 }
159
160 std::string path;
Steven Morelandec50fdf2018-07-17 14:56:20 -0700161 std::map<std::string, std::vector<std::string>> hashes;
Steven Moreland218625a2017-04-18 22:31:50 -0700162};
163
Steven Moreland566b0e92018-02-16 13:51:33 -0800164std::vector<std::string> Hash::lookupHash(const std::string& path, const std::string& interfaceName,
165 std::string* err, bool* fileExists) {
Steven Moreland218625a2017-04-18 22:31:50 -0700166 *err = "";
Steven Morelandec50fdf2018-07-17 14:56:20 -0700167 const HashFile* file = HashFile::parse(path, err);
Steven Moreland218625a2017-04-18 22:31:50 -0700168
169 if (file == nullptr || err->size() > 0) {
Steven Moreland566b0e92018-02-16 13:51:33 -0800170 if (fileExists != nullptr) *fileExists = false;
Steven Moreland218625a2017-04-18 22:31:50 -0700171 return {};
172 }
173
Steven Moreland566b0e92018-02-16 13:51:33 -0800174 if (fileExists != nullptr) *fileExists = true;
175
Steven Moreland218625a2017-04-18 22:31:50 -0700176 return file->lookup(interfaceName);
177}
178
Steven Morelandec50fdf2018-07-17 14:56:20 -0700179} // namespace android