OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/privacy_blacklist/blacklist_store.h" | 5 #include "chrome/browser/privacy_blacklist/blacklist_store.h" |
6 | 6 |
7 #include <cstdio> | 7 #include <cstdio> |
8 #include <limits> | |
8 | 9 |
9 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
10 #include "base/file_util.h" | 11 #include "base/file_util.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
15 const char cookie[] = "GCPBL100"; | 16 const char cookie[] = "GCPBL100"; |
M-A Ruel
2009/09/10 18:59:41
const char* const cookie = "..";
| |
16 | 17 |
18 const size_t kMaxBlockedTypes = 256; | |
19 const size_t kMaxStringSize = 8192; | |
20 | |
17 } | 21 } |
18 | 22 |
19 void BlacklistStoreOutput::WriteUInt(uint32 i) { | 23 bool BlacklistStoreOutput::WriteUInt(uint32 i) { |
20 if (fwrite(reinterpret_cast<char*>(&i), 1, sizeof(uint32), file_) != | 24 return fwrite(reinterpret_cast<char*>(&i), 1, sizeof(uint32), file_) == |
21 sizeof(uint32)) { | 25 sizeof(uint32); |
22 LOG(ERROR) << "fwrite failed to write uint32"; | |
23 } | |
24 } | 26 } |
25 | 27 |
26 void BlacklistStoreOutput::WriteString(const std::string& s) { | 28 bool BlacklistStoreOutput::WriteString(const std::string& s) { |
27 uint32 n = s.size(); | 29 uint32 n = s.size(); |
28 if (fwrite(reinterpret_cast<char*>(&n), 1, sizeof(uint32), file_) != | 30 return WriteUInt(n) && fwrite(s.c_str(), 1, n, file_) == n; |
29 sizeof(uint32)) { | |
30 LOG(ERROR) << "fwrite failed to write string size"; | |
31 } | |
32 if (fwrite(s.c_str(), 1, s.size(), file_) != s.size()) | |
33 LOG(ERROR) << "fwrite failed to write string data"; | |
34 } | 31 } |
35 | 32 |
36 BlacklistStoreOutput::BlacklistStoreOutput(FILE* file) : file_(file) { | 33 BlacklistStoreOutput::BlacklistStoreOutput(FILE* file) |
37 if (fwrite(cookie, 1, sizeof(cookie), file_) != sizeof(cookie)) | 34 : file_(file), is_good_(false) { |
38 LOG(ERROR) << "fwrite failed to write cookie"; | 35 is_good_ = fwrite(cookie, 1, sizeof(cookie), file_) == sizeof(cookie); |
39 } | 36 } |
40 | 37 |
41 BlacklistStoreOutput::~BlacklistStoreOutput() { | 38 BlacklistStoreOutput::~BlacklistStoreOutput() { |
42 file_util::CloseFile(file_); | 39 file_util::CloseFile(file_); |
43 } | 40 } |
44 | 41 |
45 void BlacklistStoreOutput::ReserveProviders(uint32 num) { | 42 bool BlacklistStoreOutput::ReserveProviders(uint32 num) { |
46 WriteUInt(num); | 43 return WriteUInt(num); |
47 } | 44 } |
48 | 45 |
49 void BlacklistStoreOutput::StoreProvider(const std::string& name, | 46 bool BlacklistStoreOutput::StoreProvider(const std::string& name, |
50 const std::string& url) { | 47 const std::string& url) { |
51 WriteString(name); | 48 return WriteString(name) && WriteString(url); |
52 WriteString(url); | |
53 } | 49 } |
54 | 50 |
55 void BlacklistStoreOutput::ReserveEntries(uint32 num) { | 51 bool BlacklistStoreOutput::ReserveEntries(uint32 num) { |
56 WriteUInt(num); | 52 return WriteUInt(num); |
57 } | 53 } |
58 | 54 |
59 void BlacklistStoreOutput::StoreEntry(const std::string& pattern, | 55 bool BlacklistStoreOutput::StoreEntry(const std::string& pattern, |
60 uint32 attributes, | 56 uint32 attributes, |
61 const std::vector<std::string>& types, | 57 const std::vector<std::string>& types, |
62 uint32 provider) { | 58 uint32 provider) { |
63 WriteString(pattern); | 59 if (WriteString(pattern) && |
64 WriteUInt(attributes); | 60 WriteUInt(attributes) && |
65 WriteUInt(types.size()); | 61 WriteUInt(types.size())) { |
66 for (uint32 i = 0; i < types.size(); ++i) | 62 for (uint32 i = 0; i < types.size(); ++i) { |
67 WriteString(types[i]); | 63 if (!WriteString(types[i])) |
68 WriteUInt(provider); | 64 return false; |
65 } | |
66 return WriteUInt(provider); | |
67 } | |
68 return false; | |
69 } | 69 } |
70 | 70 |
71 uint32 BlacklistStoreInput::ReadUInt() { | 71 uint32 BlacklistStoreInput::ReadUInt() { |
72 uint32 buf; | 72 uint32 buf; |
73 if (fread(&buf, 1, sizeof(uint32), file_) != sizeof(uint32)) | 73 if (fread(&buf, 1, sizeof(uint32), file_) != sizeof(uint32)) |
74 return 0; | 74 return std::numeric_limits<uint32>::max(); |
75 return buf; | 75 return buf; |
76 } | 76 } |
77 | 77 |
78 std::string BlacklistStoreInput::ReadString() { | 78 std::string BlacklistStoreInput::ReadString() { |
79 uint32 size = ReadUInt(); | 79 uint32 size = ReadUInt(); |
80 | 80 |
81 // Too long strings are not allowed. | 81 // Too long strings are not allowed. Covers the case of ReadUInt failing. |
82 if (size > 8192) { | 82 if (size > kMaxStringSize) { |
83 return std::string(); | 83 return std::string(); |
84 } | 84 } |
85 | 85 |
86 char buf[8192]; | 86 char buf[kMaxStringSize]; |
87 if (fread(buf, 1, size, file_) != size) | 87 if (fread(buf, 1, size, file_) != size) |
88 return std::string(); | 88 return std::string(); |
89 return std::string(buf, size); | 89 return std::string(buf, size); |
90 } | 90 } |
91 | 91 |
92 BlacklistStoreInput::BlacklistStoreInput(FILE* file) : file_(file) { | 92 BlacklistStoreInput::BlacklistStoreInput(FILE* file) |
93 fseek(file_, sizeof(cookie), SEEK_CUR); | 93 : file_(file), is_good_(false) { |
94 is_good_ = !fseek(file_, sizeof(cookie), SEEK_CUR); | |
94 } | 95 } |
95 | 96 |
96 BlacklistStoreInput::~BlacklistStoreInput() { | 97 BlacklistStoreInput::~BlacklistStoreInput() { |
97 file_util::CloseFile(file_); | 98 file_util::CloseFile(file_); |
98 } | 99 } |
99 | 100 |
100 uint32 BlacklistStoreInput::ReadNumProviders() { | 101 uint32 BlacklistStoreInput::ReadNumProviders() { |
101 return ReadUInt(); | 102 return ReadUInt(); |
102 } | 103 } |
103 | 104 |
104 void BlacklistStoreInput::ReadProvider(std::string* name, std::string* url) { | 105 bool BlacklistStoreInput::ReadProvider(std::string* name, std::string* url) { |
105 *name = ReadString(); | 106 *name = ReadString(); |
107 if (name->empty()) | |
108 return false; | |
106 *url = ReadString(); | 109 *url = ReadString(); |
110 return !url->empty(); | |
107 } | 111 } |
108 | 112 |
109 uint32 BlacklistStoreInput::ReadNumEntries() { | 113 uint32 BlacklistStoreInput::ReadNumEntries() { |
110 return ReadUInt(); | 114 return ReadUInt(); |
111 } | 115 } |
112 | 116 |
113 void BlacklistStoreInput::ReadEntry(std::string* pattern, | 117 bool BlacklistStoreInput::ReadEntry(std::string* pattern, |
114 uint32* attributes, | 118 uint32* attributes, |
115 std::vector<std::string>* types, | 119 std::vector<std::string>* types, |
116 uint32* provider) { | 120 uint32* provider) { |
117 *pattern = ReadString(); | 121 *pattern = ReadString(); |
122 if (pattern->empty()) | |
123 return false; | |
124 | |
118 *attributes = ReadUInt(); | 125 *attributes = ReadUInt(); |
126 if (*attributes == std::numeric_limits<uint32>::max()) | |
127 return false; | |
128 | |
119 if (uint32 n = ReadUInt()) { | 129 if (uint32 n = ReadUInt()) { |
120 while (n--) | 130 if (n >= kMaxBlockedTypes) |
121 types->push_back(ReadString()); | 131 return false; |
132 while (n--) { | |
133 std::string type = ReadString(); | |
134 if (type.empty()) | |
135 return false; | |
136 types->push_back(type); | |
137 } | |
122 } | 138 } |
123 *provider = ReadUInt(); | 139 *provider = ReadUInt(); |
140 return *provider != std::numeric_limits<uint32>::max(); | |
124 } | 141 } |
OLD | NEW |