[M120 Migration][VD] Enable direct rendering for TVPlus
[platform/framework/web/chromium-efl.git] / components / crx_file / id_util.cc
1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/crx_file/id_util.h"
6
7 #include <stdint.h>
8
9 #include "base/files/file_path.h"
10 #include "base/hash/sha1.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_util.h"
13 #include "build/build_config.h"
14 #include "crypto/sha2.h"
15 #include "third_party/abseil-cpp/absl/strings/ascii.h"
16
17 namespace {
18
19 // Converts a normal hexadecimal string into the alphabet used by extensions.
20 // We use the characters 'a'-'p' instead of '0'-'f' to avoid ever having a
21 // completely numeric host, since some software interprets that as an IP
22 // address.
23 static void ConvertHexadecimalToIDAlphabet(std::string* id) {
24   for (auto& ch : *id) {
25     int val;
26     if (base::HexStringToInt(base::StringPiece(&ch, 1), &val)) {
27       ch = 'a' + val;
28     } else {
29       ch = 'a';
30     }
31   }
32 }
33
34 }  // namespace
35
36 namespace crx_file::id_util {
37
38 // First 16 bytes of SHA256 hashed public key.
39 const size_t kIdSize = 16;
40
41 std::string GenerateId(base::StringPiece input) {
42   uint8_t hash[kIdSize];
43   crypto::SHA256HashString(input, hash, sizeof(hash));
44   return GenerateIdFromHash(hash, sizeof(hash));
45 }
46
47 std::string GenerateIdFromHash(const uint8_t* hash, size_t hash_size) {
48   CHECK_GE(hash_size, kIdSize);
49   std::string result = base::HexEncode(hash, kIdSize);
50   ConvertHexadecimalToIDAlphabet(&result);
51   return result;
52 }
53
54 std::string GenerateIdFromHex(const std::string& input) {
55   std::string output = input;
56   ConvertHexadecimalToIDAlphabet(&output);
57   return output;
58 }
59
60 std::string GenerateIdForPath(const base::FilePath& path) {
61   base::FilePath new_path = MaybeNormalizePath(path);
62   const base::StringPiece path_bytes(
63       reinterpret_cast<const char*>(new_path.value().data()),
64       new_path.value().size() * sizeof(base::FilePath::CharType));
65   return GenerateId(path_bytes);
66 }
67
68 std::string HashedIdInHex(const std::string& id) {
69   const std::string id_hash = base::SHA1HashString(id);
70   DCHECK_EQ(base::kSHA1Length, id_hash.length());
71   return base::HexEncode(id_hash.c_str(), id_hash.length());
72 }
73
74 base::FilePath MaybeNormalizePath(const base::FilePath& path) {
75 #if BUILDFLAG(IS_WIN)
76   // Normalize any drive letter to upper-case. We do this for consistency with
77   // net_utils::FilePathToFileURL(), which does the same thing, to make string
78   // comparisons simpler.
79   base::FilePath::StringType path_str = path.value();
80   if (path_str.size() >= 2 && path_str[0] >= L'a' && path_str[0] <= L'z' &&
81       path_str[1] == L':')
82     path_str[0] = absl::ascii_toupper(static_cast<unsigned char>(path_str[0]));
83
84   return base::FilePath(path_str);
85 #else
86   return path;
87 #endif
88 }
89
90 bool IdIsValid(base::StringPiece id) {
91   // Verify that the id is legal.
92   if (id.size() != (crx_file::id_util::kIdSize * 2))
93     return false;
94
95   for (char ch : id) {
96     ch = base::ToLowerASCII(ch);
97     if (ch < 'a' || ch > 'p')
98       return false;
99   }
100
101   return true;
102 }
103
104 }  // namespace crx_file::id_util