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 : #include "syzygy/trace/parse/parse_utils.h"
16 :
17 : #include "base/logging.h"
18 : #include "syzygy/common/buffer_parser.h"
19 :
20 : namespace trace {
21 : namespace parser {
22 :
23 : namespace {
24 :
25 : using ::common::BinaryBufferReader;
26 :
27 E : bool ParseString(BinaryBufferReader* reader, std::wstring* output) {
28 E : DCHECK(reader != NULL);
29 :
30 E : const wchar_t* string = NULL;
31 E : size_t length = 0;
32 E : if (!reader->ReadString(&string, &length)) {
33 E : LOG(ERROR) << "Failed to parse string from TraceFileHeader blob.";
34 E : return false;
35 : }
36 :
37 E : if (output != NULL)
38 E : *output = string;
39 :
40 E : return true;
41 E : }
42 :
43 : bool ParseEnvironmentStrings(BinaryBufferReader* reader,
44 E : TraceEnvironmentStrings* env_strings) {
45 E : DCHECK(reader != NULL);
46 :
47 E : if (env_strings != NULL)
48 E : env_strings->clear();
49 :
50 : // Parse the environment string.
51 E : size_t env_string_count = 0;
52 E : while (true) {
53 E : const wchar_t* string = NULL;
54 E : size_t length = 0;
55 E : if (!reader->ReadString(&string, &length)) {
56 E : LOG(ERROR) << "Failed to parse environment strings from TraceFileHeader.";
57 E : return false;
58 : }
59 :
60 E : if (length == 0 && env_string_count > 0)
61 E : return true;
62 :
63 : // Parse this environment string by splitting it at the first '=' sign.
64 E : if (env_strings != NULL) {
65 : // If we don't find a '=' we assume the whole thing is a key. This is
66 : // actually strictly invalid, but no harm done.
67 E : const wchar_t* split = ::wcschr(string, L'=');
68 E : if (split != NULL) {
69 E : env_strings->push_back(std::make_pair(
70 : std::wstring(string, split - string),
71 : std::wstring(split + 1)));
72 E : } else {
73 i : env_strings->push_back(std::make_pair(std::wstring(string),
74 : std::wstring()));
75 : }
76 : }
77 :
78 E : ++env_string_count;
79 E : }
80 E : }
81 :
82 : } // namespace
83 :
84 : bool ParseEnvironmentStrings(const wchar_t* env_string,
85 E : TraceEnvironmentStrings* env_strings) {
86 E : DCHECK(env_string != NULL);
87 E : DCHECK(env_strings != NULL);
88 :
89 : // Search for the double zero termination.
90 E : size_t i = 2;
91 E : while (true) {
92 E : if (env_string[i - 2] == 0 && env_string[i - 1] == 0)
93 E : break;
94 :
95 E : ++i;
96 E : }
97 :
98 E : BinaryBufferReader reader(env_string, sizeof(env_string[0]) * i);
99 E : return ParseEnvironmentStrings(&reader, env_strings);
100 E : }
101 :
102 : bool ParseTraceFileHeaderBlob(const TraceFileHeader& header,
103 : std::wstring* module_path,
104 : std::wstring* command_line,
105 E : TraceEnvironmentStrings* env_strings) {
106 E : if (header.header_size < offsetof(TraceFileHeader, blob_data)) {
107 i : LOG(ERROR) << "Invalid header size.";
108 i : return false;
109 : }
110 :
111 E : size_t blob_length = header.header_size -
112 : offsetof(TraceFileHeader, blob_data);
113 :
114 E : BinaryBufferReader reader(header.blob_data, blob_length);
115 :
116 E : if (!ParseString(&reader, module_path))
117 E : return false;
118 E : if (!ParseString(&reader, command_line))
119 E : return false;
120 E : if (!ParseEnvironmentStrings(&reader, env_strings))
121 E : return false;
122 :
123 E : if (reader.RemainingBytes() > 0) {
124 E : LOG(ERROR) << "TraceFileHeader blob contains extra data.";
125 E : return false;
126 : }
127 :
128 E : return true;
129 E : }
130 :
131 : } // namespace parser
132 : } // namespace trace
|