1 : // Copyright 2012 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 : #ifndef SYZYGY_BLOCK_GRAPH_BLOCK_HASH_H_
16 : #define SYZYGY_BLOCK_GRAPH_BLOCK_HASH_H_
17 :
18 : #include "base/md5.h"
19 : #include "syzygy/block_graph/block_graph.h"
20 : #include "syzygy/common/comparable.h"
21 :
22 : namespace block_graph {
23 :
24 : using block_graph::BlockGraph;
25 :
26 : // Represents a hash of the content of a block. Internally we store an 128-bit
27 : // MD5 digest, but this endows it with comparison operators. We explicitly
28 : // provide copy and assignment operators to make this STL container compatible.
29 : struct BlockHash : public common::Comparable<BlockHash> {
30 : public:
31 : BlockHash() {
32 : }
33 :
34 : // Constructor from Block.
35 E : explicit BlockHash(const BlockGraph::Block* block) {
36 E : Hash(block);
37 E : }
38 :
39 : // General comparison function, required by Comparable.
40 E : int Compare(const BlockHash& other) const {
41 E : return memcmp(&md5_digest, &other.md5_digest, sizeof(md5_digest));
42 E : }
43 :
44 : // Populates this block hash from the given block. The hash is calculated
45 : // on the block content and its references, as follows:
46 : // Block properties: type, size, data_size, reference count
47 : // References (increasing source offset): source offset, type, size
48 : // Data (skipping references)
49 : void Hash(const BlockGraph::Block* block);
50 :
51 : base::MD5Digest md5_digest;
52 : };
53 :
54 : } // namespace block_graph
55 :
56 : #endif // SYZYGY_BLOCK_GRAPH_BLOCK_HASH_H_
|