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