Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(139)

Side by Side Diff: chrome/utility/media_galleries/pmp_column_reader.cc

Issue 1548153002: Switch to standard integer types in chrome/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/utility/media_galleries/pmp_column_reader.h" 5 #include "chrome/utility/media_galleries/pmp_column_reader.h"
6 6
7 #include <stddef.h>
7 #include <stdint.h> 8 #include <stdint.h>
8 9
9 #include <cstring> 10 #include <cstring>
10 #include <limits> 11 #include <limits>
11 12
12 #include "base/files/file.h" 13 #include "base/files/file.h"
13 #include "base/files/file_util.h" 14 #include "base/files/file_util.h"
14 #include "base/logging.h" 15 #include "base/logging.h"
15 #include "base/threading/thread_restrictions.h" 16 #include "base/threading/thread_restrictions.h"
16 17
17 namespace picasa { 18 namespace picasa {
18 19
19 namespace { 20 namespace {
20 21
21 static_assert(sizeof(double) == 8, "double must be 8 bytes long"); 22 static_assert(sizeof(double) == 8, "double must be 8 bytes long");
22 const int64 kPmpMaxFilesize = 50*1024*1024; // Arbitrary maximum of 50 MB. 23 const int64_t kPmpMaxFilesize =
24 50 * 1024 * 1024; // Arbitrary maximum of 50 MB.
23 25
24 } // namespace 26 } // namespace
25 27
26 PmpColumnReader::PmpColumnReader() 28 PmpColumnReader::PmpColumnReader()
27 : length_(0), 29 : length_(0),
28 field_type_(PMP_TYPE_INVALID), 30 field_type_(PMP_TYPE_INVALID),
29 rows_read_(0) {} 31 rows_read_(0) {}
30 32
31 PmpColumnReader::~PmpColumnReader() {} 33 PmpColumnReader::~PmpColumnReader() {}
32 34
33 bool PmpColumnReader::ReadFile(base::File* file, 35 bool PmpColumnReader::ReadFile(base::File* file,
34 const PmpFieldType expected_type) { 36 const PmpFieldType expected_type) {
35 DCHECK(!data_.get()); 37 DCHECK(!data_.get());
36 base::ThreadRestrictions::AssertIOAllowed(); 38 base::ThreadRestrictions::AssertIOAllowed();
37 39
38 if (!file->IsValid()) 40 if (!file->IsValid())
39 return false; 41 return false;
40 42
41 base::File::Info info; 43 base::File::Info info;
42 if (!file->GetInfo(&info)) 44 if (!file->GetInfo(&info))
43 return false; 45 return false;
44 length_ = info.size; 46 length_ = info.size;
45 47
46 if (length_ < kPmpHeaderSize || length_ > kPmpMaxFilesize) 48 if (length_ < kPmpHeaderSize || length_ > kPmpMaxFilesize)
47 return false; 49 return false;
48 50
49 data_.reset(new uint8[length_]); 51 data_.reset(new uint8_t[length_]);
50 52
51 char* data_begin = reinterpret_cast<char*>(data_.get()); 53 char* data_begin = reinterpret_cast<char*>(data_.get());
52 54
53 DCHECK(length_ < 55 DCHECK(length_ <
54 std::numeric_limits<int32_t>::max()); // ReadFile expects an int. 56 std::numeric_limits<int32_t>::max()); // ReadFile expects an int.
55 57
56 bool success = file->Read(0, data_begin, length_) && 58 bool success = file->Read(0, data_begin, length_) &&
57 ParseData(expected_type); 59 ParseData(expected_type);
58 60
59 // If any of the reading or parsing fails, prevent Read* calls. 61 // If any of the reading or parsing fails, prevent Read* calls.
60 if (!success) 62 if (!success)
61 rows_read_ = 0; 63 rows_read_ = 0;
62 64
63 return success; 65 return success;
64 } 66 }
65 67
66 bool PmpColumnReader::ReadString(const uint32 row, std::string* result) const { 68 bool PmpColumnReader::ReadString(const uint32_t row,
69 std::string* result) const {
67 DCHECK(data_.get() != NULL); 70 DCHECK(data_.get() != NULL);
68 71
69 if (field_type_ != PMP_TYPE_STRING || row >= rows_read_) 72 if (field_type_ != PMP_TYPE_STRING || row >= rows_read_)
70 return false; 73 return false;
71 74
72 DCHECK_LT(row, strings_.size()); 75 DCHECK_LT(row, strings_.size());
73 *result = strings_[row]; 76 *result = strings_[row];
74 return true; 77 return true;
75 } 78 }
76 79
77 bool PmpColumnReader::ReadUInt32(const uint32 row, uint32* result) const { 80 bool PmpColumnReader::ReadUInt32(const uint32_t row, uint32_t* result) const {
78 DCHECK(data_.get() != NULL); 81 DCHECK(data_.get() != NULL);
79 82
80 if (field_type_ != PMP_TYPE_UINT32 || row >= rows_read_) 83 if (field_type_ != PMP_TYPE_UINT32 || row >= rows_read_)
81 return false; 84 return false;
82 85
83 *result = reinterpret_cast<uint32*>(data_.get() + kPmpHeaderSize)[row]; 86 *result = reinterpret_cast<uint32_t*>(data_.get() + kPmpHeaderSize)[row];
84 return true; 87 return true;
85 } 88 }
86 89
87 bool PmpColumnReader::ReadDouble64(const uint32 row, double* result) const { 90 bool PmpColumnReader::ReadDouble64(const uint32_t row, double* result) const {
88 DCHECK(data_.get() != NULL); 91 DCHECK(data_.get() != NULL);
89 92
90 if (field_type_ != PMP_TYPE_DOUBLE64 || row >= rows_read_) 93 if (field_type_ != PMP_TYPE_DOUBLE64 || row >= rows_read_)
91 return false; 94 return false;
92 95
93 *result = reinterpret_cast<double*>(data_.get() + kPmpHeaderSize)[row]; 96 *result = reinterpret_cast<double*>(data_.get() + kPmpHeaderSize)[row];
94 return true; 97 return true;
95 } 98 }
96 99
97 bool PmpColumnReader::ReadUInt8(const uint32 row, uint8* result) const { 100 bool PmpColumnReader::ReadUInt8(const uint32_t row, uint8_t* result) const {
98 DCHECK(data_.get() != NULL); 101 DCHECK(data_.get() != NULL);
99 102
100 if (field_type_ != PMP_TYPE_UINT8 || row >= rows_read_) 103 if (field_type_ != PMP_TYPE_UINT8 || row >= rows_read_)
101 return false; 104 return false;
102 105
103 *result = reinterpret_cast<uint8*>(data_.get() + kPmpHeaderSize)[row]; 106 *result = reinterpret_cast<uint8_t*>(data_.get() + kPmpHeaderSize)[row];
104 return true; 107 return true;
105 } 108 }
106 109
107 bool PmpColumnReader::ReadUInt64(const uint32 row, uint64* result) const { 110 bool PmpColumnReader::ReadUInt64(const uint32_t row, uint64_t* result) const {
108 DCHECK(data_.get() != NULL); 111 DCHECK(data_.get() != NULL);
109 112
110 if (field_type_ != PMP_TYPE_UINT64 || row >= rows_read_) 113 if (field_type_ != PMP_TYPE_UINT64 || row >= rows_read_)
111 return false; 114 return false;
112 115
113 *result = reinterpret_cast<uint64*>(data_.get() + kPmpHeaderSize)[row]; 116 *result = reinterpret_cast<uint64_t*>(data_.get() + kPmpHeaderSize)[row];
114 return true; 117 return true;
115 } 118 }
116 119
117 uint32 PmpColumnReader::rows_read() const { 120 uint32_t PmpColumnReader::rows_read() const {
118 DCHECK(data_.get() != NULL); 121 DCHECK(data_.get() != NULL);
119 return rows_read_; 122 return rows_read_;
120 } 123 }
121 124
122 bool PmpColumnReader::ParseData(const PmpFieldType expected_type) { 125 bool PmpColumnReader::ParseData(const PmpFieldType expected_type) {
123 DCHECK(data_.get() != NULL); 126 DCHECK(data_.get() != NULL);
124 DCHECK_GE(length_, kPmpHeaderSize); 127 DCHECK_GE(length_, kPmpHeaderSize);
125 128
126 // Check all magic bytes. 129 // Check all magic bytes.
127 if (memcmp(&kPmpMagic1, &data_[kPmpMagic1Offset], sizeof(kPmpMagic1)) != 0 || 130 if (memcmp(&kPmpMagic1, &data_[kPmpMagic1Offset], sizeof(kPmpMagic1)) != 0 ||
128 memcmp(&kPmpMagic2, &data_[kPmpMagic2Offset], sizeof(kPmpMagic2)) != 0 || 131 memcmp(&kPmpMagic2, &data_[kPmpMagic2Offset], sizeof(kPmpMagic2)) != 0 ||
129 memcmp(&kPmpMagic3, &data_[kPmpMagic3Offset], sizeof(kPmpMagic3)) != 0 || 132 memcmp(&kPmpMagic3, &data_[kPmpMagic3Offset], sizeof(kPmpMagic3)) != 0 ||
130 memcmp(&kPmpMagic4, &data_[kPmpMagic4Offset], sizeof(kPmpMagic4)) != 0) { 133 memcmp(&kPmpMagic4, &data_[kPmpMagic4Offset], sizeof(kPmpMagic4)) != 0) {
131 return false; 134 return false;
132 } 135 }
133 136
134 uint16 field_type_data = 137 uint16_t field_type_data =
135 *(reinterpret_cast<uint16*>(&data_[kPmpFieldType1Offset])); 138 *(reinterpret_cast<uint16_t*>(&data_[kPmpFieldType1Offset]));
136 139
137 // Verify if field type matches second declaration 140 // Verify if field type matches second declaration
138 if (field_type_data != 141 if (field_type_data !=
139 *(reinterpret_cast<uint16*>(&data_[kPmpFieldType2Offset]))) { 142 *(reinterpret_cast<uint16_t*>(&data_[kPmpFieldType2Offset]))) {
140 return false; 143 return false;
141 } 144 }
142 145
143 field_type_ = static_cast<PmpFieldType>(field_type_data); 146 field_type_ = static_cast<PmpFieldType>(field_type_data);
144 147
145 if (field_type_ != expected_type) 148 if (field_type_ != expected_type)
146 return false; 149 return false;
147 150
148 rows_read_ = *(reinterpret_cast<uint32*>(&data_[kPmpRowCountOffset])); 151 rows_read_ = *(reinterpret_cast<uint32_t*>(&data_[kPmpRowCountOffset]));
149 152
150 // Sanity check against malicious row field. 153 // Sanity check against malicious row field.
151 if (rows_read_ > (kPmpMaxFilesize - kPmpHeaderSize)) 154 if (rows_read_ > (kPmpMaxFilesize - kPmpHeaderSize))
152 return false; 155 return false;
153 156
154 DCHECK_GE(length_, kPmpHeaderSize); 157 DCHECK_GE(length_, kPmpHeaderSize);
155 int64 body_length = length_ - kPmpHeaderSize; 158 int64_t body_length = length_ - kPmpHeaderSize;
156 int64 expected_body_length = 0; 159 int64_t expected_body_length = 0;
157 switch (field_type_) { 160 switch (field_type_) {
158 case PMP_TYPE_STRING: 161 case PMP_TYPE_STRING:
159 expected_body_length = IndexStrings(); 162 expected_body_length = IndexStrings();
160 break; 163 break;
161 case PMP_TYPE_UINT32: 164 case PMP_TYPE_UINT32:
162 expected_body_length = static_cast<int64>(rows_read_) * sizeof(uint32); 165 expected_body_length =
166 static_cast<int64_t>(rows_read_) * sizeof(uint32_t);
163 break; 167 break;
164 case PMP_TYPE_DOUBLE64: 168 case PMP_TYPE_DOUBLE64:
165 expected_body_length = static_cast<int64>(rows_read_) * sizeof(double); 169 expected_body_length = static_cast<int64_t>(rows_read_) * sizeof(double);
166 break; 170 break;
167 case PMP_TYPE_UINT8: 171 case PMP_TYPE_UINT8:
168 expected_body_length = static_cast<int64>(rows_read_) * sizeof(uint8); 172 expected_body_length = static_cast<int64_t>(rows_read_) * sizeof(uint8_t);
169 break; 173 break;
170 case PMP_TYPE_UINT64: 174 case PMP_TYPE_UINT64:
171 expected_body_length = static_cast<int64>(rows_read_) * sizeof(uint64); 175 expected_body_length =
176 static_cast<int64_t>(rows_read_) * sizeof(uint64_t);
172 break; 177 break;
173 default: 178 default:
174 return false; 179 return false;
175 break; 180 break;
176 } 181 }
177 182
178 return body_length == expected_body_length; 183 return body_length == expected_body_length;
179 } 184 }
180 185
181 int64 PmpColumnReader::IndexStrings() { 186 int64_t PmpColumnReader::IndexStrings() {
182 DCHECK(data_.get() != NULL); 187 DCHECK(data_.get() != NULL);
183 DCHECK_GE(length_, kPmpHeaderSize); 188 DCHECK_GE(length_, kPmpHeaderSize);
184 189
185 strings_.reserve(rows_read_); 190 strings_.reserve(rows_read_);
186 191
187 int64 bytes_parsed = kPmpHeaderSize; 192 int64_t bytes_parsed = kPmpHeaderSize;
188 const uint8* data_cursor = data_.get() + kPmpHeaderSize; 193 const uint8_t* data_cursor = data_.get() + kPmpHeaderSize;
189 194
190 while (strings_.size() < rows_read_) { 195 while (strings_.size() < rows_read_) {
191 const uint8* string_end = static_cast<const uint8*>( 196 const uint8_t* string_end = static_cast<const uint8_t*>(
192 memchr(data_cursor, '\0', length_ - bytes_parsed)); 197 memchr(data_cursor, '\0', length_ - bytes_parsed));
193 198
194 // Fail if cannot find null termination. String runs on past file end. 199 // Fail if cannot find null termination. String runs on past file end.
195 if (string_end == NULL) 200 if (string_end == NULL)
196 return -1; 201 return -1;
197 202
198 // Length of string. (+1 to include the termination character). 203 // Length of string. (+1 to include the termination character).
199 ptrdiff_t length_in_bytes = string_end - data_cursor + 1; 204 ptrdiff_t length_in_bytes = string_end - data_cursor + 1;
200 205
201 strings_.push_back(reinterpret_cast<const char*>(data_cursor)); 206 strings_.push_back(reinterpret_cast<const char*>(data_cursor));
202 data_cursor += length_in_bytes; 207 data_cursor += length_in_bytes;
203 bytes_parsed += length_in_bytes; 208 bytes_parsed += length_in_bytes;
204 } 209 }
205 210
206 return bytes_parsed - kPmpHeaderSize; 211 return bytes_parsed - kPmpHeaderSize;
207 } 212 }
208 213
209 } // namespace picasa 214 } // namespace picasa
OLDNEW
« no previous file with comments | « chrome/utility/media_galleries/pmp_column_reader.h ('k') | chrome/utility/media_galleries/pmp_column_reader_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698