зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1422669 - Part4 - Kill string16 and replace with our SHA1 implementation. r=gerald
The reason explained in patch Part1 of Bug 1214018 and I just copy the reason below. GetRawMachineId was returning its generated data through a 'string16', which on Windows was conveniently equivalent to a std::wstring. However on Mac, wstring uses 32-bit characters, so in order to comply with the string16 interface, a lot of non-trivial code would have to be imported and vetted. Also, in the end GMPLoader::Load passes this string16 to SHA256_Update() as a sequence of bytes, the actual type of the data is lost! So to simplify this work, GetRawMachineId will now return its data through a vector of bytes, and the platform-dependent implementations may use whatever data type they want internally. The Windows GetRawMachineId actually returns the same data in this vector, so it stays compatible with the previous code. MozReview-Commit-ID: 7xYgjndXWDX --HG-- extra : rebase_source : 6a46bd7f06d6e4bc10bb98b600e7e5bc14c136ce
This commit is contained in:
Родитель
d9be66f8f8
Коммит
8107c910bc
|
@ -6,11 +6,15 @@
|
|||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "base/sha1.h"
|
||||
#include "rlz/lib/assert.h"
|
||||
#include "rlz/lib/crc8.h"
|
||||
#include "rlz/lib/string_utils.h"
|
||||
|
||||
// Note: The original machine_id.cc code depends on Chromium's sha1 implementation.
|
||||
// Using Mozilla's implmentation as replacement to reduce the dependency of
|
||||
// some external files.
|
||||
#include "mozilla/SHA1.h"
|
||||
|
||||
namespace rlz_lib {
|
||||
|
||||
bool GetMachineId(std::string* machine_id) {
|
||||
|
@ -24,12 +28,12 @@ bool GetMachineId(std::string* machine_id) {
|
|||
return true;
|
||||
}
|
||||
|
||||
base::string16 sid_string;
|
||||
std::vector<uint8_t> sid_bytes;
|
||||
int volume_id;
|
||||
if (!GetRawMachineId(&sid_string, &volume_id))
|
||||
if (!GetRawMachineId(&sid_bytes, &volume_id))
|
||||
return false;
|
||||
|
||||
if (!testing::GetMachineIdImpl(sid_string, volume_id, machine_id))
|
||||
if (!testing::GetMachineIdImpl(sid_bytes, volume_id, machine_id))
|
||||
return false;
|
||||
|
||||
calculated = true;
|
||||
|
@ -39,40 +43,43 @@ bool GetMachineId(std::string* machine_id) {
|
|||
|
||||
namespace testing {
|
||||
|
||||
bool GetMachineIdImpl(const base::string16& sid_string,
|
||||
bool GetMachineIdImpl(const std::vector<uint8_t>& sid_bytes,
|
||||
int volume_id,
|
||||
std::string* machine_id) {
|
||||
machine_id->clear();
|
||||
|
||||
// The ID should be the SID hash + the Hard Drive SNo. + checksum byte.
|
||||
static const int kSizeWithoutChecksum = base::kSHA1Length + sizeof(int);
|
||||
static const int kSizeWithoutChecksum = mozilla::SHA1Sum::kHashSize + sizeof(int);
|
||||
std::basic_string<unsigned char> id_binary(kSizeWithoutChecksum + 1, 0);
|
||||
|
||||
if (!sid_string.empty()) {
|
||||
if (!sid_bytes.empty()) {
|
||||
// In order to be compatible with the old version of RLZ, the hash of the
|
||||
// SID must be done with all the original bytes from the unicode string.
|
||||
// However, the chromebase SHA1 hash function takes only an std::string as
|
||||
// input, so the unicode string needs to be converted to std::string
|
||||
// "as is".
|
||||
size_t byte_count = sid_string.size() * sizeof(base::string16::value_type);
|
||||
const char* buffer = reinterpret_cast<const char*>(sid_string.c_str());
|
||||
std::string sid_string_buffer(buffer, byte_count);
|
||||
size_t byte_count = sid_bytes.size() * sizeof(std::vector<uint8_t>::value_type);
|
||||
const char* buffer = reinterpret_cast<const char*>(sid_bytes.data());
|
||||
|
||||
// Note that digest can have embedded nulls.
|
||||
std::string digest(base::SHA1HashString(sid_string_buffer));
|
||||
VERIFY(digest.size() == base::kSHA1Length);
|
||||
mozilla::SHA1Sum SHA1;
|
||||
mozilla::SHA1Sum::Hash hash;
|
||||
SHA1.update(buffer, byte_count);
|
||||
SHA1.finish(hash);
|
||||
std::string digest(reinterpret_cast<char*>(hash), mozilla::SHA1Sum::kHashSize);
|
||||
VERIFY(digest.size() == mozilla::SHA1Sum::kHashSize);
|
||||
std::copy(digest.begin(), digest.end(), id_binary.begin());
|
||||
}
|
||||
|
||||
// Convert from int to binary (makes big-endian).
|
||||
for (size_t i = 0; i < sizeof(int); i++) {
|
||||
int shift_bits = 8 * (sizeof(int) - i - 1);
|
||||
id_binary[base::kSHA1Length + i] = static_cast<unsigned char>(
|
||||
id_binary[mozilla::SHA1Sum::kHashSize + i] = static_cast<unsigned char>(
|
||||
(volume_id >> shift_bits) & 0xFF);
|
||||
}
|
||||
|
||||
// Append the checksum byte.
|
||||
if (!sid_string.empty() || (0 != volume_id))
|
||||
if (!sid_bytes.empty() || (0 != volume_id))
|
||||
rlz_lib::Crc8::Generate(id_binary.c_str(),
|
||||
kSizeWithoutChecksum,
|
||||
&id_binary[kSizeWithoutChecksum]);
|
||||
|
|
|
@ -23,7 +23,7 @@ bool GetMachineId(std::string* machine_id);
|
|||
bool GetRawMachineId(std::vector<uint8_t>* data, int* more_data);
|
||||
|
||||
namespace testing {
|
||||
bool GetMachineIdImpl(const base::string16& sid_string,
|
||||
bool GetMachineIdImpl(const std::vector<uint8_t>& sid_bytes,
|
||||
int volume_id,
|
||||
std::string* machine_id);
|
||||
} // namespace testing
|
||||
|
|
|
@ -11,6 +11,8 @@ FINAL_LIBRARY = 'xul'
|
|||
|
||||
if CONFIG['OS_TARGET'] in ['WINNT', 'Darwin']:
|
||||
UNIFIED_SOURCES += [
|
||||
'lib/crc8.cc',
|
||||
'lib/machine_id.cc',
|
||||
'lib/string_utils.cc',
|
||||
]
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче