1 : // Copyright 2014 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/kasko/crash_keys_serialization.h"
16 :
17 : #include "base/logging.h"
18 : #include "base/values.h"
19 : #include "base/files/file_path.h"
20 : #include "base/files/file_util.h"
21 : #include "base/json/json_reader.h"
22 : #include "base/json/json_writer.h"
23 : #include "base/memory/scoped_ptr.h"
24 : #include "base/strings/utf_string_conversions.h"
25 :
26 : namespace kasko {
27 :
28 : bool ReadCrashKeysFromFile(
29 : const base::FilePath& file_path,
30 E : std::map<base::string16, base::string16>* crash_keys) {
31 E : DCHECK(crash_keys);
32 E : std::string file_contents;
33 E : if (!base::ReadFileToString(file_path, &file_contents)) {
34 E : LOG(ERROR) << "Failed to read crash keys from file " << file_path.value();
35 E : return false;
36 : }
37 :
38 E : scoped_ptr<base::Value> value(base::JSONReader::Read(file_contents));
39 E : base::DictionaryValue* dictionary = nullptr;
40 E : if (!value || !value->GetAsDictionary(&dictionary)) {
41 E : LOG(ERROR) << "The crash keys file contents from " << file_path.value()
42 : << " are not a valid JSON dictionary:\n" << file_contents;
43 E : return false;
44 : }
45 :
46 E : for (base::DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd();
47 E : it.Advance()) {
48 E : base::string16 value;
49 E : if (!it.value().GetAsString(&value)) {
50 E : LOG(ERROR) << "The crash keys file contents from " << file_path.value()
51 : << " contain an invalid value for entry " << it.key()
52 : << ". File Contents:\n" << file_contents;
53 E : return false;
54 : }
55 E : crash_keys->insert(std::make_pair(base::UTF8ToWide(it.key()), value));
56 E : }
57 E : return true;
58 E : }
59 :
60 : bool WriteCrashKeysToFile(
61 : const base::FilePath& file_path,
62 E : const std::map<base::string16, base::string16>& crash_keys) {
63 E : base::DictionaryValue dictionary;
64 E : for (const auto& entry : crash_keys) {
65 : dictionary.SetStringWithoutPathExpansion(base::WideToUTF8(entry.first),
66 E : base::WideToUTF8(entry.second));
67 E : }
68 :
69 E : std::string file_contents;
70 E : if (!base::JSONWriter::Write(dictionary, &file_contents)) {
71 i : LOG(ERROR) << "Failed to serialize crash keys.";
72 i : return false;
73 : }
74 :
75 : if (!base::WriteFile(file_path, file_contents.data(),
76 E : file_contents.length())) {
77 i : LOG(ERROR) << "Failed to write serialized crash keys to "
78 : << file_path.value();
79 i : return false;
80 : }
81 :
82 E : return true;
83 E : }
84 :
85 : } // namespace kasko
|