1 : // Copyright 2015 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/refinery/process_state/layer_data.h"
16 :
17 : namespace refinery {
18 :
19 : std::size_t PESignatureHasher::operator()(
20 E : pe::PEFile::Signature const& s) const {
21 : base::MD5Context ctx;
22 E : base::MD5Init(&ctx);
23 :
24 E : uint32_t base_address = s.base_address.value();
25 : base::MD5Update(
26 : &ctx, base::StringPiece(reinterpret_cast<const char*>(s.path.data()),
27 E : s.path.size() * sizeof(wchar_t)));
28 : base::MD5Update(
29 : &ctx, base::StringPiece(reinterpret_cast<const char*>(&base_address),
30 E : sizeof(base_address)));
31 : base::MD5Update(
32 : &ctx, base::StringPiece(reinterpret_cast<const char*>(&s.module_size),
33 E : sizeof(s.module_size)));
34 : base::MD5Update(
35 : &ctx, base::StringPiece(reinterpret_cast<const char*>(&s.module_checksum),
36 E : sizeof(s.module_checksum)));
37 : base::MD5Update(&ctx, base::StringPiece(reinterpret_cast<const char*>(
38 : &s.module_time_date_stamp),
39 E : sizeof(s.module_time_date_stamp)));
40 : base::MD5Digest digest;
41 E : base::MD5Final(&digest, &ctx);
42 :
43 : // Note: only using the first bytes of the digest.
44 E : return *reinterpret_cast<size_t*>(&digest);
45 E : }
46 :
47 E : ModuleLayerData::ModuleLayerData() {
48 E : }
49 :
50 E : ModuleId ModuleLayerData::Find(const pe::PEFile::Signature& signature) const {
51 E : auto it = signature_to_id_.find(signature);
52 E : if (it != signature_to_id_.end())
53 E : return it->second;
54 E : return kNoModuleId;
55 E : }
56 :
57 E : ModuleId ModuleLayerData::FindOrIndex(const pe::PEFile::Signature& signature) {
58 E : ModuleId id = Find(signature);
59 E : if (id != kNoModuleId)
60 E : return id;
61 :
62 E : id = signature_to_id_.size();
63 :
64 E : signature_to_id_[signature] = id;
65 E : signatures_.push_back(signature);
66 :
67 E : return id;
68 E : }
69 :
70 : bool ModuleLayerData::Find(ModuleId id,
71 E : pe::PEFile::Signature* signature) const {
72 E : if (id >= signatures_.size())
73 E : return false;
74 :
75 E : *signature = signatures_[id];
76 E : return true;
77 E : }
78 :
79 : } // namespace refinery
|