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