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