Move file hashing into its own class, Hash.
Test: hidl output remains the same.
Bug: 34178341
Merged-In: Ic8c5de61ebba3bd556d343c4f2c12af4bafb8d6b
Change-Id: Ic8c5de61ebba3bd556d343c4f2c12af4bafb8d6b
diff --git a/Android.bp b/Android.bp
index d969017..5c03b03 100644
--- a/Android.bp
+++ b/Android.bp
@@ -40,6 +40,7 @@
"DeathRecipientType.cpp",
"EnumType.cpp",
"HandleType.cpp",
+ "Hash.cpp",
"HidlTypeAssertion.cpp",
"Interface.cpp",
"MemoryType.cpp",
diff --git a/Hash.cpp b/Hash.cpp
new file mode 100644
index 0000000..348a79f
--- /dev/null
+++ b/Hash.cpp
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "Hash.h"
+
+#include <fstream>
+#include <iomanip>
+#include <map>
+#include <sstream>
+
+#include <openssl/sha.h>
+
+namespace android {
+
+const Hash &Hash::getHash(const std::string &path) {
+ static std::map<std::string, Hash> hashes;
+
+ auto it = hashes.find(path);
+
+ if (hashes.find(path) == hashes.end()) {
+ it = hashes.insert(it, {path, Hash(path)});
+ }
+
+ return it->second;
+}
+
+static std::vector<uint8_t> sha256File(const std::string &path) {
+ std::ifstream stream(path);
+ std::stringstream fileStream;
+ fileStream << stream.rdbuf();
+ std::string fileContent = fileStream.str();
+
+ std::vector<uint8_t> ret = std::vector<uint8_t>(SHA256_DIGEST_LENGTH);
+
+ SHA256(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
+ fileContent.size(), ret.data());
+
+ return ret;
+}
+
+Hash::Hash(const std::string &path)
+ : mPath(path),
+ mHash(sha256File(path)) {}
+
+std::string Hash::hexString() const {
+ std::ostringstream s;
+ s << std::hex << std::setfill('0');
+ for (uint8_t i : mHash) {
+ s << std::setw(2) << static_cast<int>(i);
+ }
+ return s.str();
+}
+
+const std::vector<uint8_t> &Hash::raw() const {
+ return mHash;
+}
+
+const std::string &Hash::getPath() const {
+ return mPath;
+}
+
+} // android
\ No newline at end of file
diff --git a/Hash.h b/Hash.h
new file mode 100644
index 0000000..99dea31
--- /dev/null
+++ b/Hash.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef HASH_H_
+
+#define HASH_H_
+
+#include <string>
+#include <vector>
+
+namespace android {
+
+struct Hash {
+ static const Hash &getHash(const std::string &path);
+
+ std::string hexString() const;
+
+ const std::vector<uint8_t> &raw() const;
+ const std::string &getPath() const;
+
+private:
+ Hash(const std::string &path);
+
+ const std::string mPath;
+ const std::vector<uint8_t> mHash;
+};
+
+} // namespace android
+
+#endif // HASH_H_
+
diff --git a/Interface.cpp b/Interface.cpp
index d326703..99c13e1 100644
--- a/Interface.cpp
+++ b/Interface.cpp
@@ -20,6 +20,7 @@
#include "ArrayType.h"
#include "ConstantExpression.h"
#include "DeathRecipientType.h"
+#include "Hash.h"
#include "Method.h"
#include "ScalarType.h"
#include "StringType.h"
@@ -27,14 +28,12 @@
#include <unistd.h>
-#include <fstream>
#include <iostream>
#include <sstream>
#include <android-base/logging.h>
#include <hidl-util/Formatter.h>
#include <hidl-util/StringHelper.h>
-#include <openssl/sha.h>
namespace android {
@@ -288,36 +287,22 @@
return true;
}
-static void sha256File(const std::string &path, uint8_t *outDigest) {
- std::ifstream stream(path);
- std::stringstream fileStream;
- fileStream << stream.rdbuf();
- std::string fileContent = fileStream.str();
- SHA256(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
- fileContent.size(), outDigest);
-}
-
static void emitDigestChain(
Formatter &out,
const std::string &prefix,
const std::vector<const Interface *> &chain,
std::function<std::string(const ConstantExpression &)> byteToString) {
out.join(chain.begin(), chain.end(), ",\n", [&] (const auto &iface) {
- const std::string &filename = iface->location().begin().filename();
- uint8_t digest[SHA256_DIGEST_LENGTH];
- sha256File(filename, digest);
+ const Hash &hash = Hash::getHash(iface->location().begin().filename());
out << prefix;
out << "{";
- out.join(digest, digest + SHA256_DIGEST_LENGTH, ",", [&](const auto &e) {
+ out.join(hash.raw().begin(), hash.raw().end(), ",", [&](const auto &e) {
// Use ConstantExpression::cppValue / javaValue
// because Java used signed byte for uint8_t.
out << byteToString(ConstantExpression::ValueOf(ScalarType::Kind::KIND_UINT8, e));
});
out << "} /* ";
- out.join(digest, digest + SHA256_DIGEST_LENGTH, "", [&](const auto &e) {
- static const char hexes[] = "0123456789abcdef";
- out << hexes[e >> 4] << hexes[e & 0xF];
- });
+ out << hash.hexString();
out << " */";
});
}
diff --git a/utils/include/hidl-util/Formatter.h b/utils/include/hidl-util/Formatter.h
index 513efad..bb4288b 100644
--- a/utils/include/hidl-util/Formatter.h
+++ b/utils/include/hidl-util/Formatter.h
@@ -105,7 +105,7 @@
// out << toString(e);
// });
template<typename I>
- Formatter &join(I begin, I end, const std::string &separator,
+ Formatter &join(const I begin, const I end, const std::string &separator,
std::function<void(const typename std::iterator_traits<I>::value_type &)> func);
Formatter &operator<<(const std::string &out);
@@ -151,7 +151,7 @@
};
template<typename I>
-Formatter &Formatter::join(I begin, I end, const std::string &separator,
+Formatter &Formatter::join(const I begin, const I end, const std::string &separator,
std::function<void(const typename std::iterator_traits<I>::value_type &)> func) {
for (I iter = begin; iter != end; ++iter) {
if (iter != begin) {