| 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 "webkit/fileapi/media/picasa/pmp_column_reader.h" | 5 #include "webkit/fileapi/media/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" | 10 #include "base/files/file_path.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
| 13 | 13 |
| 14 namespace picasaimport { | 14 namespace picasaimport { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 const size_t kPmpMaxFilesize = 50*1024*1024; // Maximum of 50 MB. | 18 const size_t kPmpMaxFilesize = 50*1024*1024; // 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_INVALID_TYPE), | 24 field_type_(PMP_INVALID_TYPE), |
| 25 rows_(0) { } | 25 rows_(0) { } |
| 26 | 26 |
| 27 PmpColumnReader::~PmpColumnReader() { } | 27 PmpColumnReader::~PmpColumnReader() { } |
| 28 | 28 |
| 29 bool PmpColumnReader::Init(const base::FilePath& filepath, uint32* rows_read) { | 29 bool PmpColumnReader::Init(const base::FilePath& filepath, |
| 30 const PmpFieldType expected_type, |
| 31 uint32* rows_read) { |
| 30 DCHECK(!data_.get()); | 32 DCHECK(!data_.get()); |
| 31 base::ThreadRestrictions::AssertIOAllowed(); | 33 base::ThreadRestrictions::AssertIOAllowed(); |
| 32 | 34 |
| 33 int64 length = 0; // Signed temporary. | 35 int64 length = 0; // Signed temporary. |
| 34 if (!file_util::GetFileSize(filepath, &length)) | 36 if (!file_util::GetFileSize(filepath, &length)) |
| 35 return false; | 37 return false; |
| 36 | 38 |
| 37 length_ = length; | 39 length_ = length; |
| 38 | 40 |
| 39 if (length_ < kPmpHeaderSize || length_ > kPmpMaxFilesize) | 41 if (length_ < kPmpHeaderSize || length_ > kPmpMaxFilesize) |
| 40 return false; | 42 return false; |
| 41 | 43 |
| 42 data_.reset(new uint8[length_]); | 44 data_.reset(new uint8[length_]); |
| 43 | 45 |
| 44 char* data_begin = reinterpret_cast<char*>(data_.get()); | 46 char* data_begin = reinterpret_cast<char*>(data_.get()); |
| 45 | 47 |
| 46 return file_util::ReadFile(filepath, data_begin, length_) && | 48 return file_util::ReadFile(filepath, data_begin, length_) && |
| 47 ParseData(rows_read); | 49 ParseData(expected_type, rows_read); |
| 48 } | 50 } |
| 49 | 51 |
| 50 bool PmpColumnReader::ReadString(const uint32 row, std::string* result) const { | 52 bool PmpColumnReader::ReadString(const uint32 row, std::string* result) const { |
| 51 DCHECK(data_.get() != NULL); | 53 DCHECK(data_.get() != NULL); |
| 52 DCHECK_GT(length_, kPmpHeaderSize + row); | 54 DCHECK_GT(length_, kPmpHeaderSize + row); |
| 53 | 55 |
| 54 if (field_type_ != PMP_STRING_TYPE || row >= rows_) | 56 if (field_type_ != PMP_STRING_TYPE || row >= rows_) |
| 55 return false; | 57 return false; |
| 56 | 58 |
| 57 *result = strings_[row]; | 59 *result = strings_[row]; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 DCHECK(data_.get() != NULL); | 97 DCHECK(data_.get() != NULL); |
| 96 DCHECK_GT(length_, kPmpHeaderSize + row * sizeof(uint64)); | 98 DCHECK_GT(length_, kPmpHeaderSize + row * sizeof(uint64)); |
| 97 | 99 |
| 98 if (field_type_ != PMP_UINT64_TYPE || row >= rows_) | 100 if (field_type_ != PMP_UINT64_TYPE || row >= rows_) |
| 99 return false; | 101 return false; |
| 100 | 102 |
| 101 *result = reinterpret_cast<uint64*>(data_.get() + kPmpHeaderSize)[row]; | 103 *result = reinterpret_cast<uint64*>(data_.get() + kPmpHeaderSize)[row]; |
| 102 return true; | 104 return true; |
| 103 } | 105 } |
| 104 | 106 |
| 105 bool PmpColumnReader::ParseData(uint32* rows_read) { | 107 bool PmpColumnReader::ParseData(const PmpFieldType expected_type, |
| 108 uint32* rows_read) { |
| 106 DCHECK(data_.get() != NULL); | 109 DCHECK(data_.get() != NULL); |
| 107 DCHECK_GE(length_, kPmpHeaderSize); | 110 DCHECK_GE(length_, kPmpHeaderSize); |
| 108 | 111 |
| 109 // Check all magic bytes. | 112 // Check all magic bytes. |
| 110 if (memcmp(&kPmpMagic1, &data_[kPmpMagic1Offset], sizeof(kPmpMagic1)) != 0 || | 113 if (memcmp(&kPmpMagic1, &data_[kPmpMagic1Offset], sizeof(kPmpMagic1)) != 0 || |
| 111 memcmp(&kPmpMagic2, &data_[kPmpMagic2Offset], sizeof(kPmpMagic2)) != 0 || | 114 memcmp(&kPmpMagic2, &data_[kPmpMagic2Offset], sizeof(kPmpMagic2)) != 0 || |
| 112 memcmp(&kPmpMagic3, &data_[kPmpMagic3Offset], sizeof(kPmpMagic3)) != 0 || | 115 memcmp(&kPmpMagic3, &data_[kPmpMagic3Offset], sizeof(kPmpMagic3)) != 0 || |
| 113 memcmp(&kPmpMagic4, &data_[kPmpMagic4Offset], sizeof(kPmpMagic4)) != 0) { | 116 memcmp(&kPmpMagic4, &data_[kPmpMagic4Offset], sizeof(kPmpMagic4)) != 0) { |
| 114 return false; | 117 return false; |
| 115 } | 118 } |
| 116 | 119 |
| 117 uint16 field_type_data = | 120 uint16 field_type_data = |
| 118 *(reinterpret_cast<uint16*>(&data_[kPmpFieldType1Offset])); | 121 *(reinterpret_cast<uint16*>(&data_[kPmpFieldType1Offset])); |
| 119 | 122 |
| 120 // Verify if field type matches second declaration | 123 // Verify if field type matches second declaration |
| 121 if (field_type_data != | 124 if (field_type_data != |
| 122 *(reinterpret_cast<uint16*>(&data_[kPmpFieldType2Offset]))) { | 125 *(reinterpret_cast<uint16*>(&data_[kPmpFieldType2Offset]))) { |
| 123 return false; | 126 return false; |
| 124 } | 127 } |
| 125 | 128 |
| 126 field_type_ = static_cast<PmpFieldType>(field_type_data); | 129 field_type_ = static_cast<PmpFieldType>(field_type_data); |
| 127 | 130 |
| 131 if (field_type_ != expected_type) |
| 132 return false; |
| 133 |
| 128 rows_ = *(reinterpret_cast<uint32*>(&data_[kPmpRowCountOffset])); | 134 rows_ = *(reinterpret_cast<uint32*>(&data_[kPmpRowCountOffset])); |
| 129 | 135 |
| 130 size_t body_length = length_ - kPmpHeaderSize; | 136 size_t body_length = length_ - kPmpHeaderSize; |
| 131 size_t expected_body_length = 0; | 137 size_t expected_body_length = 0; |
| 132 switch (field_type_) { | 138 switch (field_type_) { |
| 133 case PMP_STRING_TYPE: | 139 case PMP_STRING_TYPE: |
| 134 expected_body_length = IndexStrings(); | 140 expected_body_length = IndexStrings(); |
| 135 break; | 141 break; |
| 136 case PMP_UINT32_TYPE: | 142 case PMP_UINT32_TYPE: |
| 137 expected_body_length = rows_ * sizeof(uint32); | 143 expected_body_length = rows_ * sizeof(uint32); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 | 183 |
| 178 strings_.push_back(reinterpret_cast<const char*>(data_cursor)); | 184 strings_.push_back(reinterpret_cast<const char*>(data_cursor)); |
| 179 data_cursor += length_in_bytes; | 185 data_cursor += length_in_bytes; |
| 180 bytes_parsed += length_in_bytes; | 186 bytes_parsed += length_in_bytes; |
| 181 } | 187 } |
| 182 | 188 |
| 183 return bytes_parsed - kPmpHeaderSize; | 189 return bytes_parsed - kPmpHeaderSize; |
| 184 } | 190 } |
| 185 | 191 |
| 186 } // namespace picasaimport | 192 } // namespace picasaimport |
| OLD | NEW |