OLD | NEW |
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include "minidump/test/minidump_string_writer_test_util.h" | 15 #include "minidump/test/minidump_string_writer_test_util.h" |
16 | 16 |
17 #include "gtest/gtest.h" | 17 #include "gtest/gtest.h" |
18 #include "minidump/minidump_extensions.h" | 18 #include "minidump/minidump_extensions.h" |
| 19 #include "minidump/test/minidump_writable_test_util.h" |
19 | 20 |
20 namespace crashpad { | 21 namespace crashpad { |
21 namespace test { | 22 namespace test { |
22 | 23 |
23 std::string MinidumpUTF8StringAtRVA(const StringFileWriter& file_writer, | 24 namespace { |
24 RVA rva) { | 25 |
25 const std::string& contents = file_writer.string(); | 26 template <typename T> |
26 if (rva == 0) { | 27 const T* TMinidumpStringAtRVA(const std::string& file_contents, RVA rva) { |
27 return std::string(); | 28 const T* string_base = MinidumpWritableAtRVA<T>(file_contents, rva); |
| 29 if (!string_base) { |
| 30 return nullptr; |
28 } | 31 } |
29 | 32 |
30 if (rva + sizeof(MinidumpUTF8String) > contents.size()) { | 33 // |Length| must indicate the ability to store an integral number of code |
31 ADD_FAILURE() | 34 // units. |
32 << "rva " << rva << " too large for contents " << contents.size(); | 35 const size_t kCodeUnitSize = sizeof(string_base->Buffer[0]); |
33 return std::string(); | 36 if (string_base->Length % kCodeUnitSize != 0) { |
| 37 EXPECT_EQ(0u, string_base->Length % kCodeUnitSize); |
| 38 return nullptr; |
34 } | 39 } |
35 | 40 |
36 const MinidumpUTF8String* minidump_string = | 41 // |Length| does not include space for the required NUL terminator. |
37 reinterpret_cast<const MinidumpUTF8String*>(&contents[rva]); | 42 MINIDUMP_LOCATION_DESCRIPTOR location; |
38 | 43 location.DataSize = |
39 // Verify that the file has enough data for the string’s stated length plus | 44 sizeof(*string_base) + string_base->Length + kCodeUnitSize; |
40 // its required NUL terminator. | 45 location.Rva = rva; |
41 if (rva + sizeof(MinidumpUTF8String) + minidump_string->Length + 1 > | 46 const T* string = |
42 contents.size()) { | 47 MinidumpWritableAtLocationDescriptor<T>(file_contents, location); |
43 ADD_FAILURE() | 48 if (!string) { |
44 << "rva " << rva << ", length " << minidump_string->Length | 49 return nullptr; |
45 << " too large for contents " << contents.size(); | |
46 return std::string(); | |
47 } | 50 } |
48 | 51 |
49 std::string minidump_string_data( | 52 EXPECT_EQ(string_base, string); |
50 reinterpret_cast<const char*>(&minidump_string->Buffer[0]), | 53 |
51 minidump_string->Length); | 54 // Require the NUL terminator to be NUL. |
| 55 if (string->Buffer[string->Length / kCodeUnitSize] != '\0') { |
| 56 EXPECT_EQ('\0', string->Buffer[string->Length / kCodeUnitSize]); |
| 57 return nullptr; |
| 58 } |
| 59 |
| 60 return string; |
| 61 } |
| 62 |
| 63 template <typename StringType, typename MinidumpStringType> |
| 64 StringType TMinidumpStringAtRVAAsString(const std::string& file_contents, |
| 65 RVA rva) { |
| 66 const MinidumpStringType* minidump_string = |
| 67 TMinidumpStringAtRVA<MinidumpStringType>(file_contents, rva); |
| 68 if (!minidump_string) { |
| 69 return StringType(); |
| 70 } |
| 71 |
| 72 StringType minidump_string_data( |
| 73 reinterpret_cast<const typename StringType::value_type*>( |
| 74 &minidump_string->Buffer[0]), |
| 75 minidump_string->Length / sizeof(minidump_string->Buffer[0])); |
52 return minidump_string_data; | 76 return minidump_string_data; |
53 } | 77 } |
54 | 78 |
| 79 } // namespace |
| 80 |
| 81 const MINIDUMP_STRING* MinidumpStringAtRVA(const std::string& file_contents, |
| 82 RVA rva) { |
| 83 return TMinidumpStringAtRVA<MINIDUMP_STRING>(file_contents, rva); |
| 84 } |
| 85 |
| 86 const MinidumpUTF8String* MinidumpUTF8StringAtRVA( |
| 87 const std::string& file_contents, |
| 88 RVA rva) { |
| 89 return TMinidumpStringAtRVA<MinidumpUTF8String>(file_contents, rva); |
| 90 } |
| 91 |
| 92 string16 MinidumpStringAtRVAAsString(const std::string& file_contents, |
| 93 RVA rva) { |
| 94 return TMinidumpStringAtRVAAsString<string16, MINIDUMP_STRING>(file_contents, |
| 95 rva); |
| 96 } |
| 97 |
| 98 std::string MinidumpUTF8StringAtRVAAsString(const std::string& file_contents, |
| 99 RVA rva) { |
| 100 return TMinidumpStringAtRVAAsString<std::string, MinidumpUTF8String>( |
| 101 file_contents, rva); |
| 102 } |
| 103 |
55 } // namespace test | 104 } // namespace test |
56 } // namespace crashpad | 105 } // namespace crashpad |
OLD | NEW |