Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / common / safe_browsing / binary_feature_extractor.cc
blob54f6c2051b4a41ee982f91fe26f7efe249f42b9c
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/common/safe_browsing/binary_feature_extractor.h"
7 #include "base/files/file.h"
8 #include "base/files/file_path.h"
9 #include "base/files/memory_mapped_file.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "chrome/common/safe_browsing/csd.pb.h"
12 #include "crypto/secure_hash.h"
13 #include "crypto/sha2.h"
15 namespace safe_browsing {
17 BinaryFeatureExtractor::BinaryFeatureExtractor() {}
19 BinaryFeatureExtractor::~BinaryFeatureExtractor() {}
21 bool BinaryFeatureExtractor::ExtractImageFeatures(
22 const base::FilePath& file_path,
23 ExtractHeadersOption options,
24 ClientDownloadRequest_ImageHeaders* image_headers,
25 google::protobuf::RepeatedPtrField<std::string>* signed_data) {
26 base::MemoryMappedFile mapped_file;
27 if (!mapped_file.Initialize(file_path))
28 return false;
29 return ExtractImageFeaturesFromData(mapped_file.data(), mapped_file.length(),
30 options, image_headers, signed_data);
33 bool BinaryFeatureExtractor::ExtractImageFeaturesFromFile(
34 base::File file,
35 ExtractHeadersOption options,
36 ClientDownloadRequest_ImageHeaders* image_headers,
37 google::protobuf::RepeatedPtrField<std::string>* signed_data) {
38 base::MemoryMappedFile mapped_file;
39 if (!mapped_file.Initialize(file.Pass()))
40 return false;
41 return ExtractImageFeaturesFromData(mapped_file.data(), mapped_file.length(),
42 options, image_headers, signed_data);
45 void BinaryFeatureExtractor::ExtractDigest(
46 const base::FilePath& file_path,
47 ClientDownloadRequest_Digests* digests) {
48 base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ);
49 if (file.IsValid()) {
50 const int kBufferSize = 1 << 12;
51 scoped_ptr<char[]> buf(new char[kBufferSize]);
52 scoped_ptr<crypto::SecureHash> ctx(
53 crypto::SecureHash::Create(crypto::SecureHash::SHA256));
54 int len = 0;
55 while (true) {
56 len = file.ReadAtCurrentPos(buf.get(), kBufferSize);
57 if (len <= 0)
58 break;
59 ctx->Update(buf.get(), len);
61 if (!len) {
62 uint8_t hash[crypto::kSHA256Length];
63 ctx->Finish(hash, sizeof(hash));
64 digests->set_sha256(hash, sizeof(hash));
69 } // namespace safe_browsing