Chromium Code Reviews| Index: ui/base/resource/data_pack.cc |
| diff --git a/ui/base/resource/data_pack.cc b/ui/base/resource/data_pack.cc |
| index 18fee46d0699aeda0910a51328cd336d87855155..8fe83c146a598ef279034b384a646c4bcafbbc35 100644 |
| --- a/ui/base/resource/data_pack.cc |
| +++ b/ui/base/resource/data_pack.cc |
| @@ -17,7 +17,7 @@ |
| namespace { |
| -static const uint32 kFileFormatVersion = 2; |
| +static const uint32 kFileFormatVersion = 3; |
| // Length of file header: version and entry count. |
| static const size_t kHeaderLength = 2 * sizeof(uint32); |
| @@ -25,7 +25,6 @@ static const size_t kHeaderLength = 2 * sizeof(uint32); |
| struct DataPackEntry { |
| uint16 resource_id; |
| uint32 file_offset; |
| - uint32 length; |
| static int CompareById(const void* void_key, const void* void_entry) { |
| uint16 key = *reinterpret_cast<const uint16*>(void_key); |
| @@ -42,7 +41,7 @@ struct DataPackEntry { |
| }; |
| #pragma pack(pop) |
| -COMPILE_ASSERT(sizeof(DataPackEntry) == 10, size_of_header_must_be_ten); |
| +COMPILE_ASSERT(sizeof(DataPackEntry) == 6, size_of_header_must_be_six); |
|
Evan Martin
2011/08/10 17:28:56
I guess this should be "size_of_ENTRY_must_be_six"
|
| // We're crashing when trying to load a pak file on Windows. Add some error |
| // codes for logging. |
| @@ -105,7 +104,7 @@ bool DataPack::Load(const FilePath& path) { |
| for (size_t i = 0; i < resource_count_; ++i) { |
| const DataPackEntry* entry = reinterpret_cast<const DataPackEntry*>( |
| mmap_->data() + kHeaderLength + (i * sizeof(DataPackEntry))); |
| - if (entry->file_offset + entry->length > mmap_->length()) { |
| + if (entry->file_offset > mmap_->length()) { |
| LOG(ERROR) << "Entry #" << i << " in data pack points off end of file. " |
| << "Was the file corrupted?"; |
| UMA_HISTOGRAM_ENUMERATION("DataPack.Load", ENTRY_NOT_FOUND, |
| @@ -131,14 +130,24 @@ bool DataPack::GetStringPiece(uint16 resource_id, |
| #error DataPack assumes little endian |
| #endif |
| - DataPackEntry* target = reinterpret_cast<DataPackEntry*>( |
| + const DataPackEntry* target = reinterpret_cast<const DataPackEntry*>( |
| bsearch(&resource_id, mmap_->data() + kHeaderLength, resource_count_, |
| sizeof(DataPackEntry), DataPackEntry::CompareById)); |
| if (!target) { |
| return false; |
| } |
| - data->set(mmap_->data() + target->file_offset, target->length); |
| + const DataPackEntry* past_last_entry = reinterpret_cast<const DataPackEntry*>( |
| + mmap_->data() + kHeaderLength + ( |
| + resource_count_ * sizeof(DataPackEntry))); |
| + |
| + const DataPackEntry* next_entry = target + 1; |
| + |
| + size_t length = (next_entry == past_last_entry) |
| + ? mmap_->length() - target->file_offset |
| + : next_entry->file_offset - target->file_offset; |
| + |
| + data->set(mmap_->data() + target->file_offset, length); |
| return true; |
| } |
| @@ -173,7 +182,7 @@ bool DataPack::WritePack(const FilePath& path, |
| return false; |
| } |
| - // Each entry is 1 uint16 + 2 uint32s. |
| + // Each entry is a uint16 + a uint32. |
| uint32 index_length = entry_count * sizeof(DataPackEntry); |
| uint32 data_offset = kHeaderLength + index_length; |
| for (std::map<uint16, base::StringPiece>::const_iterator it = |
| @@ -192,14 +201,7 @@ bool DataPack::WritePack(const FilePath& path, |
| return false; |
| } |
| - uint32 len = it->second.length(); |
| - if (fwrite(&len, sizeof(len), 1, file) != 1) { |
| - LOG(ERROR) << "Failed to write length for " << resource_id; |
| - file_util::CloseFile(file); |
| - return false; |
| - } |
| - |
| - data_offset += len; |
| + data_offset += it->second.length(); |
| } |
| for (std::map<uint16, base::StringPiece>::const_iterator it = |