1 // Copyright 2015 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 "chrome/common/safe_browsing/csd.pb.h"
8 #include "chrome/common/safe_browsing/mach_o_image_reader_mac.h"
10 namespace safe_browsing
{
12 bool BinaryFeatureExtractor::ExtractImageFeaturesFromData(
13 const uint8_t* data
, size_t data_size
,
14 ExtractHeadersOption options
,
15 ClientDownloadRequest_ImageHeaders
* image_headers
,
16 google::protobuf::RepeatedPtrField
<std::string
>* signed_data
) {
17 MachOImageReader mach_o_reader
;
18 if (!mach_o_reader
.Initialize(data
, data_size
))
21 // Record the entire mach_header struct.
22 auto mach_o_headers
= image_headers
->mutable_mach_o_headers();
23 if (mach_o_reader
.Is64Bit()) {
24 const mach_header_64
* header
= mach_o_reader
.GetMachHeader64();
25 mach_o_headers
->set_mach_header(header
, sizeof(*header
));
27 const mach_header
* header
= mach_o_reader
.GetMachHeader();
28 mach_o_headers
->set_mach_header(header
, sizeof(*header
));
31 // Store the load commands for the Mach-O binary.
32 auto proto_load_commands
= mach_o_headers
->mutable_load_commands();
33 const std::vector
<MachOImageReader::LoadCommand
>& load_commands
=
34 mach_o_reader
.GetLoadCommands();
35 for (const auto& load_command
: load_commands
) {
36 auto proto_load_command
= proto_load_commands
->Add();
37 proto_load_command
->set_command_id(load_command
.cmd());
38 proto_load_command
->set_command(&load_command
.data
[0],
39 load_command
.data
.size());
42 // Get the signature information.
44 std::vector
<uint8_t> code_signature
;
45 if (mach_o_reader
.GetCodeSignatureInfo(&code_signature
)) {
46 signed_data
->Add()->append(
47 reinterpret_cast<const char*>(&code_signature
[0]),
48 code_signature
.size());
55 } // namespace safe_browsing