OLD | NEW |
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/browser/media_galleries/fileapi/picasa/pmp_column_reader.h" | 5 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_column_reader.h" |
6 | 6 |
7 #include <cstring> | 7 #include <cstring> |
8 | 8 |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | |
11 #include "base/logging.h" | 10 #include "base/logging.h" |
12 #include "base/threading/thread_restrictions.h" | 11 #include "base/threading/thread_restrictions.h" |
13 | 12 |
14 namespace picasa { | 13 namespace picasa { |
15 | 14 |
16 namespace { | 15 namespace { |
17 | 16 |
| 17 COMPILE_ASSERT(sizeof(double) == 8, double_must_be_8_bytes_long); |
18 const int64 kPmpMaxFilesize = 50*1024*1024; // Arbitrary maximum of 50 MB. | 18 const int64 kPmpMaxFilesize = 50*1024*1024; // Arbitrary maximum of 50 MB. |
19 | 19 |
20 } // namespace | 20 } // namespace |
21 | 21 |
22 PmpColumnReader::PmpColumnReader() | 22 PmpColumnReader::PmpColumnReader() |
23 : length_(0), | 23 : length_(0), |
24 field_type_(PMP_TYPE_INVALID), | 24 field_type_(PMP_TYPE_INVALID), |
25 rows_(0) {} | 25 rows_(0) {} |
26 | 26 |
27 PmpColumnReader::~PmpColumnReader() {} | 27 PmpColumnReader::~PmpColumnReader() {} |
28 | 28 |
29 bool PmpColumnReader::Init(const base::FilePath& filepath, | 29 bool PmpColumnReader::Init(const base::PlatformFile& file, |
30 const PmpFieldType expected_type, | 30 const PmpFieldType expected_type) { |
31 uint32* rows_read) { | |
32 DCHECK(!data_.get()); | 31 DCHECK(!data_.get()); |
33 base::ThreadRestrictions::AssertIOAllowed(); | 32 base::ThreadRestrictions::AssertIOAllowed(); |
34 | 33 |
35 if (!file_util::GetFileSize(filepath, &length_)) | 34 if (file == base::kInvalidPlatformFileValue) |
36 return false; | 35 return false; |
37 | 36 |
| 37 base::PlatformFileInfo info; |
| 38 if (!base::GetPlatformFileInfo(file, &info)) |
| 39 return false; |
| 40 length_ = info.size; |
| 41 |
38 if (length_ < kPmpHeaderSize || length_ > kPmpMaxFilesize) | 42 if (length_ < kPmpHeaderSize || length_ > kPmpMaxFilesize) |
39 return false; | 43 return false; |
40 | 44 |
41 data_.reset(new uint8[length_]); | 45 data_.reset(new uint8[length_]); |
42 | 46 |
43 char* data_begin = reinterpret_cast<char*>(data_.get()); | 47 char* data_begin = reinterpret_cast<char*>(data_.get()); |
44 | 48 |
45 DCHECK(length_ < kint32max); // ReadFile expects an int. | 49 DCHECK(length_ < kint32max); // ReadFile expects an int. |
46 | 50 |
47 bool success = file_util::ReadFile(filepath, data_begin, length_) && | 51 bool success = base::ReadPlatformFile(file, 0, data_begin, length_) && |
48 ParseData(expected_type, rows_read); | 52 ParseData(expected_type); |
49 | 53 |
50 if (!success) | 54 if (!success) |
51 rows_ = 0; // If any of the reading or parsing fails, prevent Read* calls. | 55 rows_ = 0; // If any of the reading or parsing fails, prevent Read* calls. |
52 | 56 |
53 return success; | 57 return success; |
54 } | 58 } |
55 | 59 |
56 bool PmpColumnReader::ReadString(const uint32 row, std::string* result) const { | 60 bool PmpColumnReader::ReadString(const uint32 row, std::string* result) const { |
57 DCHECK(data_.get() != NULL); | 61 DCHECK(data_.get() != NULL); |
58 | 62 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 bool PmpColumnReader::ReadUInt64(const uint32 row, uint64* result) const { | 101 bool PmpColumnReader::ReadUInt64(const uint32 row, uint64* result) const { |
98 DCHECK(data_.get() != NULL); | 102 DCHECK(data_.get() != NULL); |
99 | 103 |
100 if (field_type_ != PMP_TYPE_UINT64 || row >= rows_) | 104 if (field_type_ != PMP_TYPE_UINT64 || row >= rows_) |
101 return false; | 105 return false; |
102 | 106 |
103 *result = reinterpret_cast<uint64*>(data_.get() + kPmpHeaderSize)[row]; | 107 *result = reinterpret_cast<uint64*>(data_.get() + kPmpHeaderSize)[row]; |
104 return true; | 108 return true; |
105 } | 109 } |
106 | 110 |
107 bool PmpColumnReader::ParseData(const PmpFieldType expected_type, | 111 uint32 PmpColumnReader::rows() const { |
108 uint32* rows_read) { | 112 DCHECK(data_.get() != NULL); |
| 113 return rows_; |
| 114 } |
| 115 |
| 116 bool PmpColumnReader::ParseData(const PmpFieldType expected_type) { |
109 DCHECK(data_.get() != NULL); | 117 DCHECK(data_.get() != NULL); |
110 DCHECK_GE(length_, kPmpHeaderSize); | 118 DCHECK_GE(length_, kPmpHeaderSize); |
111 | 119 |
112 // Check all magic bytes. | 120 // Check all magic bytes. |
113 if (memcmp(&kPmpMagic1, &data_[kPmpMagic1Offset], sizeof(kPmpMagic1)) != 0 || | 121 if (memcmp(&kPmpMagic1, &data_[kPmpMagic1Offset], sizeof(kPmpMagic1)) != 0 || |
114 memcmp(&kPmpMagic2, &data_[kPmpMagic2Offset], sizeof(kPmpMagic2)) != 0 || | 122 memcmp(&kPmpMagic2, &data_[kPmpMagic2Offset], sizeof(kPmpMagic2)) != 0 || |
115 memcmp(&kPmpMagic3, &data_[kPmpMagic3Offset], sizeof(kPmpMagic3)) != 0 || | 123 memcmp(&kPmpMagic3, &data_[kPmpMagic3Offset], sizeof(kPmpMagic3)) != 0 || |
116 memcmp(&kPmpMagic4, &data_[kPmpMagic4Offset], sizeof(kPmpMagic4)) != 0) { | 124 memcmp(&kPmpMagic4, &data_[kPmpMagic4Offset], sizeof(kPmpMagic4)) != 0) { |
117 return false; | 125 return false; |
118 } | 126 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 expected_body_length = static_cast<int64>(rows_) * sizeof(uint8); | 162 expected_body_length = static_cast<int64>(rows_) * sizeof(uint8); |
155 break; | 163 break; |
156 case PMP_TYPE_UINT64: | 164 case PMP_TYPE_UINT64: |
157 expected_body_length = static_cast<int64>(rows_) * sizeof(uint64); | 165 expected_body_length = static_cast<int64>(rows_) * sizeof(uint64); |
158 break; | 166 break; |
159 default: | 167 default: |
160 return false; | 168 return false; |
161 break; | 169 break; |
162 } | 170 } |
163 | 171 |
164 if (body_length == expected_body_length && rows_read) | |
165 *rows_read = rows_; | |
166 return body_length == expected_body_length; | 172 return body_length == expected_body_length; |
167 } | 173 } |
168 | 174 |
169 int64 PmpColumnReader::IndexStrings() { | 175 int64 PmpColumnReader::IndexStrings() { |
170 DCHECK(data_.get() != NULL); | 176 DCHECK(data_.get() != NULL); |
171 DCHECK_GE(length_, kPmpHeaderSize); | 177 DCHECK_GE(length_, kPmpHeaderSize); |
172 | 178 |
173 strings_.reserve(rows_); | 179 strings_.reserve(rows_); |
174 | 180 |
175 int64 bytes_parsed = kPmpHeaderSize; | 181 int64 bytes_parsed = kPmpHeaderSize; |
(...skipping 12 matching lines...) Expand all Loading... |
188 | 194 |
189 strings_.push_back(reinterpret_cast<const char*>(data_cursor)); | 195 strings_.push_back(reinterpret_cast<const char*>(data_cursor)); |
190 data_cursor += length_in_bytes; | 196 data_cursor += length_in_bytes; |
191 bytes_parsed += length_in_bytes; | 197 bytes_parsed += length_in_bytes; |
192 } | 198 } |
193 | 199 |
194 return bytes_parsed - kPmpHeaderSize; | 200 return bytes_parsed - kPmpHeaderSize; |
195 } | 201 } |
196 | 202 |
197 } // namespace picasa | 203 } // namespace picasa |
OLD | NEW |