1 : // Copyright 2012 Google Inc.
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/block_graph/transforms/trim_transform.h"
16 :
17 : namespace block_graph {
18 : namespace transforms {
19 :
20 : namespace {
21 :
22 : // Returns the length of the initialized data for the given block.
23 E : size_t InitializedDataSize(const BlockGraph::Block* block) {
24 E : size_t size = 0;
25 :
26 E : if (!block->references().empty()) {
27 : // NOTE: This relies on ReferenceMap being sorted by the offset!
28 : BlockGraph::Block::ReferenceMap::const_iterator ref =
29 E : --block->references().end();
30 E : size = ref->first + ref->second.size();
31 E : }
32 :
33 : // If we have no data it consists of implicit zeros.
34 E : if (block->data() == NULL || block->data_size() <= size)
35 E : return size;
36 :
37 : // If we do have data then we peel off any explicit zeros at the end of the
38 : // buffer.
39 E : const uint8* data = block->data() + block->data_size() - 1;
40 E : for (size_t i = block->data_size(); i > size; --i, --data) {
41 E : if (*data != 0)
42 E : return i;
43 E : }
44 :
45 E : return size;
46 E : }
47 :
48 : } // namespace
49 :
50 : const char TrimTransform::kTransformName[] = "TrimTransform";
51 :
52 : bool TrimTransform::OnBlock(BlockGraph* /* block_graph */,
53 E : BlockGraph::Block* block) {
54 E : DCHECK(block != NULL);
55 :
56 E : size_t init_size = InitializedDataSize(block);
57 E : if (init_size != block->data_size())
58 E : block->ResizeData(init_size);
59 :
60 E : return true;
61 E : }
62 :
63 : } // namespace transforms
64 : } // namespace block_graph
|