Index: chrome/browser/chromeos/login/hwid_checker.cc |
diff --git a/chrome/browser/chromeos/login/hwid_checker.cc b/chrome/browser/chromeos/login/hwid_checker.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bda93c179454046ca80ade024aa915c48976304c |
--- /dev/null |
+++ b/chrome/browser/chromeos/login/hwid_checker.cc |
@@ -0,0 +1,59 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
Nikita (slow)
2013/02/11 17:09:03
nit: 2013
dzhioev (left Google)
2013/02/11 23:32:56
Done.
|
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/chromeos/login/hwid_checker.h" |
+ |
+#include <cstdio> |
+ |
+#include "base/chromeos/chromeos_version.h" |
+#include "base/command_line.h" |
+#include "base/logging.h" |
+#include "chrome/browser/chromeos/system/statistics_provider.h" |
+#include "chrome/common/chrome_switches.h" |
+#include "third_party/re2/re2/re2.h" |
+#include "third_party/zlib/zlib.h" |
+ |
+namespace chromeos { |
+ |
+std::string CalculateHWIDChecksum(const std::string& data) { |
+ unsigned crc32_checksum = static_cast<unsigned>(crc32( |
+ 0, |
+ reinterpret_cast<const Bytef*>(data.c_str()), |
+ data.length())); |
+ // We take four least significant decimal digits of CRC-32. |
+ char checksum[5]; |
+ int snprintf_result = |
+ snprintf(checksum, 5, "%04u", crc32_checksum % 10000); |
+ LOG_ASSERT(snprintf_result == 4); |
+ return checksum; |
+} |
+ |
+bool IsHWIDCorrect(const std::string& hwid) { |
+ // TODO(dzhioev): add support of HWID v3 format. |
+ std::string body; |
+ std::string checksum; |
+ if (!RE2::FullMatch(hwid, "([\\s\\S]*) (\\d{4})", &body, &checksum)) |
+ return false; |
+ return CalculateHWIDChecksum(body) == checksum; |
+} |
+ |
+bool IsMachineHWIDCorrect() { |
+#if !defined(GOOGLE_CHROME_BUILD) |
+ return true; |
+#endif |
+ if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) { |
Nikita (slow)
2013/02/11 17:09:03
nit: Drop {}
dzhioev (left Google)
2013/02/11 23:32:56
Done.
|
+ return true; |
+ } |
+ if (!base::chromeos::IsRunningOnChromeOS()) { |
Nikita (slow)
2013/02/11 17:09:03
nit: Drop {}
dzhioev (left Google)
2013/02/11 23:32:56
Done.
|
+ return true; |
+ } |
+ std::string hwid; |
+ chromeos::system::StatisticsProvider* stats = |
+ chromeos::system::StatisticsProvider::GetInstance(); |
+ return stats->GetMachineStatistic("hardware_class", &hwid) && |
Nikita (slow)
2013/02/11 17:09:03
Please verify that GetMachineStatistic() call does
Nikita (slow)
2013/02/11 17:09:03
nit: Add LOG(ERROR) when stats->GetMachineStatisti
dzhioev (left Google)
2013/02/11 23:32:56
Done.
|
+ chromeos::IsHWIDCorrect(hwid); |
Nikita (slow)
2013/02/11 17:09:03
Please add LOG(ERROR) with the hwid value if IsHWI
dzhioev (left Google)
2013/02/11 23:32:56
Done.
|
+} |
+ |
+} // namespace chromeos |
+ |