Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / tools / safe_browsing / sb_sigutil.cc
blob88ca8d6ac4ad92cb82a3601bf19bda6c9098042e
1 // Copyright (c) 2011 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.
4 //
5 // Simple tool that uses the SignatureUtil class to extract signature
6 // information from an executable. The output is an encoded
7 // ClientDownloadRequest_SignatureInfo protocol buffer.
8 //
9 // Example usage: sb_sigutil --executable=blah.exe --output=siginfo.pb
11 #include <string>
13 #include "base/command_line.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/logging.h"
17 #include "base/memory/ref_counted.h"
18 #include "chrome/common/safe_browsing/binary_feature_extractor.h"
19 #include "chrome/common/safe_browsing/csd.pb.h"
21 // Command-line switch for the executable to extract a signature from.
22 static const char kExecutable[] = "executable";
24 // File to write the output protocol buffer to.
25 static const char kOutputFile[] = "output";
27 int main(int argc, char* argv[]) {
28 base::CommandLine::Init(argc, argv);
30 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
31 if (!cmd_line->HasSwitch(kExecutable)) {
32 LOG(ERROR) << "Must specify executable to open with --executable";
33 return 1;
35 if (!cmd_line->HasSwitch(kOutputFile)) {
36 LOG(ERROR) << "Must specify output file with --output";
37 return 1;
40 scoped_refptr<safe_browsing::BinaryFeatureExtractor> extractor(
41 new safe_browsing::BinaryFeatureExtractor());
42 safe_browsing::ClientDownloadRequest_SignatureInfo signature_info;
43 extractor->CheckSignature(cmd_line->GetSwitchValuePath(kExecutable),
44 &signature_info);
46 std::string serialized_info = signature_info.SerializeAsString();
47 int bytes_written = base::WriteFile(
48 cmd_line->GetSwitchValuePath(kOutputFile),
49 serialized_info.data(),
50 serialized_info.size());
51 if (bytes_written != static_cast<int>(serialized_info.size())) {
52 LOG(ERROR) << "Error writing output file";
53 return 1;
56 return 0;