1// Copyright (c) 2011, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8//     * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10//     * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14//     * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30// minidump_memory_range.h: Define the google_breakpad::MinidumpMemoryRange
31// class, which adds methods for handling minidump specific data structures
32// on top of google_breakpad::MemoryRange. See common/memory_range.h for
33// more details on MemoryRange.
34
35#ifndef TOOLS_LINUX_MD2CORE_MINIDUMP_MEMORY_RANGE_H_
36#define TOOLS_LINUX_MD2CORE_MINIDUMP_MEMORY_RANGE_H_
37
38#include <string>
39
40#include "common/memory_range.h"
41#include "google_breakpad/common/minidump_format.h"
42
43namespace google_breakpad {
44
45// A derived class of MemoryRange with added methods for handling minidump
46// specific data structures. To avoid virtual functions, it is not designed
47// to be used polymorphically.
48class MinidumpMemoryRange : public MemoryRange {
49 public:
50  MinidumpMemoryRange() {}
51
52  MinidumpMemoryRange(const void* data, size_t length)
53      : MemoryRange(data, length) {}
54
55  // Returns a subrange of |length| bytes at |offset| bytes of this memory
56  // range, or an empty range if the subrange is out of bounds.
57  // This methods overrides the base implemementation in order to return
58  // an instance of MinidumpMemoryRange instead of MemoryRange.
59  MinidumpMemoryRange Subrange(size_t sub_offset, size_t sub_length) const {
60    if (Covers(sub_offset, sub_length))
61      return MinidumpMemoryRange(data() + sub_offset, sub_length);
62    return MinidumpMemoryRange();
63  }
64
65  // Returns a subrange that covers the offset and length specified by
66  // |location|, or an empty range if the subrange is out of bounds.
67  MinidumpMemoryRange Subrange(const MDLocationDescriptor& location) const {
68    return MinidumpMemoryRange::Subrange(location.rva, location.data_size);
69  }
70
71  // Gets a STL string from a MDString at |sub_offset| bytes of this memory
72  // range. This method only works correctly for ASCII characters and does
73  // not convert between UTF-16 and UTF-8.
74  const std::string GetAsciiMDString(size_t sub_offset) const {
75    std::string str;
76    const MDString* md_str = GetData<MDString>(sub_offset);
77    if (md_str) {
78      const uint16_t* buffer = &md_str->buffer[0];
79      for (uint32_t i = 0; i < md_str->length && buffer[i]; ++i) {
80        str.push_back(buffer[i]);
81      }
82    }
83    return str;
84  }
85};
86
87}  // namespace google_breakpad
88
89#endif  // TOOLS_LINUX_MD2CORE_MINIDUMP_MEMORY_RANGE_H_
90