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