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 |
| 41 length_ = info.size; |
| 42 |
38 if (length_ < kPmpHeaderSize || length_ > kPmpMaxFilesize) | 43 if (length_ < kPmpHeaderSize || length_ > kPmpMaxFilesize) |
39 return false; | 44 return false; |
40 | 45 |
41 data_.reset(new uint8[length_]); | 46 data_.reset(new uint8[length_]); |
42 | 47 |
43 char* data_begin = reinterpret_cast<char*>(data_.get()); | 48 char* data_begin = reinterpret_cast<char*>(data_.get()); |
44 | 49 |
45 DCHECK(length_ < kint32max); // ReadFile expects an int. | 50 DCHECK(length_ < kint32max); // ReadFile expects an int. |
46 | 51 |
47 bool success = file_util::ReadFile(filepath, data_begin, length_) && | 52 bool success = base::ReadPlatformFile(file, 0, data_begin, length_) && |
48 ParseData(expected_type, rows_read); | 53 ParseData(expected_type); |
49 | 54 |
50 if (!success) | 55 if (!success) |
51 rows_ = 0; // If any of the reading or parsing fails, prevent Read* calls. | 56 rows_ = 0; // If any of the reading or parsing fails, prevent Read* calls. |
52 | 57 |
53 return success; | 58 return success; |
54 } | 59 } |
55 | 60 |
56 bool PmpColumnReader::ReadString(const uint32 row, std::string* result) const { | 61 bool PmpColumnReader::ReadString(const uint32 row, std::string* result) const { |
57 DCHECK(data_.get() != NULL); | 62 DCHECK(data_.get() != NULL); |
58 | 63 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 bool PmpColumnReader::ReadUInt64(const uint32 row, uint64* result) const { | 102 bool PmpColumnReader::ReadUInt64(const uint32 row, uint64* result) const { |
98 DCHECK(data_.get() != NULL); | 103 DCHECK(data_.get() != NULL); |
99 | 104 |
100 if (field_type_ != PMP_TYPE_UINT64 || row >= rows_) | 105 if (field_type_ != PMP_TYPE_UINT64 || row >= rows_) |
101 return false; | 106 return false; |
102 | 107 |
103 *result = reinterpret_cast<uint64*>(data_.get() + kPmpHeaderSize)[row]; | 108 *result = reinterpret_cast<uint64*>(data_.get() + kPmpHeaderSize)[row]; |
104 return true; | 109 return true; |
105 } | 110 } |
106 | 111 |
107 bool PmpColumnReader::ParseData(const PmpFieldType expected_type, | 112 uint32 PmpColumnReader::rows() const { |
108 uint32* rows_read) { | 113 DCHECK(data_.get() != NULL); |
| 114 return rows_; |
| 115 } |
| 116 |
| 117 bool PmpColumnReader::ParseData(const PmpFieldType expected_type) { |
109 DCHECK(data_.get() != NULL); | 118 DCHECK(data_.get() != NULL); |
110 DCHECK_GE(length_, kPmpHeaderSize); | 119 DCHECK_GE(length_, kPmpHeaderSize); |
111 | 120 |
112 // Check all magic bytes. | 121 // Check all magic bytes. |
113 if (memcmp(&kPmpMagic1, &data_[kPmpMagic1Offset], sizeof(kPmpMagic1)) != 0 || | 122 if (memcmp(&kPmpMagic1, &data_[kPmpMagic1Offset], sizeof(kPmpMagic1)) != 0 || |
114 memcmp(&kPmpMagic2, &data_[kPmpMagic2Offset], sizeof(kPmpMagic2)) != 0 || | 123 memcmp(&kPmpMagic2, &data_[kPmpMagic2Offset], sizeof(kPmpMagic2)) != 0 || |
115 memcmp(&kPmpMagic3, &data_[kPmpMagic3Offset], sizeof(kPmpMagic3)) != 0 || | 124 memcmp(&kPmpMagic3, &data_[kPmpMagic3Offset], sizeof(kPmpMagic3)) != 0 || |
116 memcmp(&kPmpMagic4, &data_[kPmpMagic4Offset], sizeof(kPmpMagic4)) != 0) { | 125 memcmp(&kPmpMagic4, &data_[kPmpMagic4Offset], sizeof(kPmpMagic4)) != 0) { |
117 return false; | 126 return false; |
118 } | 127 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 expected_body_length = static_cast<int64>(rows_) * sizeof(uint8); | 163 expected_body_length = static_cast<int64>(rows_) * sizeof(uint8); |
155 break; | 164 break; |
156 case PMP_TYPE_UINT64: | 165 case PMP_TYPE_UINT64: |
157 expected_body_length = static_cast<int64>(rows_) * sizeof(uint64); | 166 expected_body_length = static_cast<int64>(rows_) * sizeof(uint64); |
158 break; | 167 break; |
159 default: | 168 default: |
160 return false; | 169 return false; |
161 break; | 170 break; |
162 } | 171 } |
163 | 172 |
164 if (body_length == expected_body_length && rows_read) | |
165 *rows_read = rows_; | |
166 return body_length == expected_body_length; | 173 return body_length == expected_body_length; |
167 } | 174 } |
168 | 175 |
169 int64 PmpColumnReader::IndexStrings() { | 176 int64 PmpColumnReader::IndexStrings() { |
170 DCHECK(data_.get() != NULL); | 177 DCHECK(data_.get() != NULL); |
171 DCHECK_GE(length_, kPmpHeaderSize); | 178 DCHECK_GE(length_, kPmpHeaderSize); |
172 | 179 |
173 strings_.reserve(rows_); | 180 strings_.reserve(rows_); |
174 | 181 |
175 int64 bytes_parsed = kPmpHeaderSize; | 182 int64 bytes_parsed = kPmpHeaderSize; |
(...skipping 12 matching lines...) Expand all Loading... |
188 | 195 |
189 strings_.push_back(reinterpret_cast<const char*>(data_cursor)); | 196 strings_.push_back(reinterpret_cast<const char*>(data_cursor)); |
190 data_cursor += length_in_bytes; | 197 data_cursor += length_in_bytes; |
191 bytes_parsed += length_in_bytes; | 198 bytes_parsed += length_in_bytes; |
192 } | 199 } |
193 | 200 |
194 return bytes_parsed - kPmpHeaderSize; | 201 return bytes_parsed - kPmpHeaderSize; |
195 } | 202 } |
196 | 203 |
197 } // namespace picasa | 204 } // namespace picasa |
OLD | NEW |