Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ui/base/resource/data_pack.h" | 5 #include "ui/base/resource/data_pack.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/string_piece.h" | 13 #include "base/string_piece.h" |
| 14 | 14 |
| 15 // For details of the file layout, see | 15 // For details of the file layout, see |
| 16 // http://dev.chromium.org/developers/design-documents/linuxresourcesandlocalize dstrings | 16 // http://dev.chromium.org/developers/design-documents/linuxresourcesandlocalize dstrings |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 static const uint32 kFileFormatVersion = 2; | 20 static const uint32 kFileFormatVersion = 3; |
| 21 // Length of file header: version and entry count. | 21 // Length of file header: version and entry count. |
| 22 static const size_t kHeaderLength = 2 * sizeof(uint32); | 22 static const size_t kHeaderLength = 2 * sizeof(uint32); |
| 23 | 23 |
| 24 #pragma pack(push,2) | 24 #pragma pack(push,2) |
| 25 struct DataPackEntry { | 25 struct DataPackEntry { |
| 26 uint16 resource_id; | 26 uint16 resource_id; |
| 27 uint32 file_offset; | 27 uint32 file_offset; |
| 28 uint32 length; | |
| 29 | 28 |
| 30 static int CompareById(const void* void_key, const void* void_entry) { | 29 static int CompareById(const void* void_key, const void* void_entry) { |
| 31 uint16 key = *reinterpret_cast<const uint16*>(void_key); | 30 uint16 key = *reinterpret_cast<const uint16*>(void_key); |
| 32 const DataPackEntry* entry = | 31 const DataPackEntry* entry = |
| 33 reinterpret_cast<const DataPackEntry*>(void_entry); | 32 reinterpret_cast<const DataPackEntry*>(void_entry); |
| 34 if (key < entry->resource_id) { | 33 if (key < entry->resource_id) { |
| 35 return -1; | 34 return -1; |
| 36 } else if (key > entry->resource_id) { | 35 } else if (key > entry->resource_id) { |
| 37 return 1; | 36 return 1; |
| 38 } else { | 37 } else { |
| 39 return 0; | 38 return 0; |
| 40 } | 39 } |
| 41 } | 40 } |
| 42 }; | 41 }; |
| 43 #pragma pack(pop) | 42 #pragma pack(pop) |
| 44 | 43 |
| 45 COMPILE_ASSERT(sizeof(DataPackEntry) == 10, size_of_header_must_be_ten); | 44 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"
| |
| 46 | 45 |
| 47 // We're crashing when trying to load a pak file on Windows. Add some error | 46 // We're crashing when trying to load a pak file on Windows. Add some error |
| 48 // codes for logging. | 47 // codes for logging. |
| 49 // http://crbug.com/58056 | 48 // http://crbug.com/58056 |
| 50 enum LoadErrors { | 49 enum LoadErrors { |
| 51 INIT_FAILED = 1, | 50 INIT_FAILED = 1, |
| 52 BAD_VERSION, | 51 BAD_VERSION, |
| 53 INDEX_TRUNCATED, | 52 INDEX_TRUNCATED, |
| 54 ENTRY_NOT_FOUND, | 53 ENTRY_NOT_FOUND, |
| 55 | 54 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 "entries specified."; | 97 "entries specified."; |
| 99 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INDEX_TRUNCATED, | 98 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INDEX_TRUNCATED, |
| 100 LOAD_ERRORS_COUNT); | 99 LOAD_ERRORS_COUNT); |
| 101 mmap_.reset(); | 100 mmap_.reset(); |
| 102 return false; | 101 return false; |
| 103 } | 102 } |
| 104 // 2) Verify the entries are within the appropriate bounds. | 103 // 2) Verify the entries are within the appropriate bounds. |
| 105 for (size_t i = 0; i < resource_count_; ++i) { | 104 for (size_t i = 0; i < resource_count_; ++i) { |
| 106 const DataPackEntry* entry = reinterpret_cast<const DataPackEntry*>( | 105 const DataPackEntry* entry = reinterpret_cast<const DataPackEntry*>( |
| 107 mmap_->data() + kHeaderLength + (i * sizeof(DataPackEntry))); | 106 mmap_->data() + kHeaderLength + (i * sizeof(DataPackEntry))); |
| 108 if (entry->file_offset + entry->length > mmap_->length()) { | 107 if (entry->file_offset > mmap_->length()) { |
| 109 LOG(ERROR) << "Entry #" << i << " in data pack points off end of file. " | 108 LOG(ERROR) << "Entry #" << i << " in data pack points off end of file. " |
| 110 << "Was the file corrupted?"; | 109 << "Was the file corrupted?"; |
| 111 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", ENTRY_NOT_FOUND, | 110 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", ENTRY_NOT_FOUND, |
| 112 LOAD_ERRORS_COUNT); | 111 LOAD_ERRORS_COUNT); |
| 113 mmap_.reset(); | 112 mmap_.reset(); |
| 114 return false; | 113 return false; |
| 115 } | 114 } |
| 116 } | 115 } |
| 117 | 116 |
| 118 return true; | 117 return true; |
| 119 } | 118 } |
| 120 | 119 |
| 121 bool DataPack::GetStringPiece(uint16 resource_id, | 120 bool DataPack::GetStringPiece(uint16 resource_id, |
| 122 base::StringPiece* data) const { | 121 base::StringPiece* data) const { |
| 123 // It won't be hard to make this endian-agnostic, but it's not worth | 122 // It won't be hard to make this endian-agnostic, but it's not worth |
| 124 // bothering to do right now. | 123 // bothering to do right now. |
| 125 #if defined(__BYTE_ORDER) | 124 #if defined(__BYTE_ORDER) |
| 126 // Linux check | 125 // Linux check |
| 127 COMPILE_ASSERT(__BYTE_ORDER == __LITTLE_ENDIAN, | 126 COMPILE_ASSERT(__BYTE_ORDER == __LITTLE_ENDIAN, |
| 128 datapack_assumes_little_endian); | 127 datapack_assumes_little_endian); |
| 129 #elif defined(__BIG_ENDIAN__) | 128 #elif defined(__BIG_ENDIAN__) |
| 130 // Mac check | 129 // Mac check |
| 131 #error DataPack assumes little endian | 130 #error DataPack assumes little endian |
| 132 #endif | 131 #endif |
| 133 | 132 |
| 134 DataPackEntry* target = reinterpret_cast<DataPackEntry*>( | 133 const DataPackEntry* target = reinterpret_cast<const DataPackEntry*>( |
| 135 bsearch(&resource_id, mmap_->data() + kHeaderLength, resource_count_, | 134 bsearch(&resource_id, mmap_->data() + kHeaderLength, resource_count_, |
| 136 sizeof(DataPackEntry), DataPackEntry::CompareById)); | 135 sizeof(DataPackEntry), DataPackEntry::CompareById)); |
| 137 if (!target) { | 136 if (!target) { |
| 138 return false; | 137 return false; |
| 139 } | 138 } |
| 140 | 139 |
| 141 data->set(mmap_->data() + target->file_offset, target->length); | 140 const DataPackEntry* past_last_entry = reinterpret_cast<const DataPackEntry*>( |
| 141 mmap_->data() + kHeaderLength + ( | |
| 142 resource_count_ * sizeof(DataPackEntry))); | |
| 143 | |
| 144 const DataPackEntry* next_entry = target + 1; | |
| 145 | |
| 146 size_t length = (next_entry == past_last_entry) | |
| 147 ? mmap_->length() - target->file_offset | |
| 148 : next_entry->file_offset - target->file_offset; | |
| 149 | |
| 150 data->set(mmap_->data() + target->file_offset, length); | |
| 142 return true; | 151 return true; |
| 143 } | 152 } |
| 144 | 153 |
| 145 RefCountedStaticMemory* DataPack::GetStaticMemory(uint16 resource_id) const { | 154 RefCountedStaticMemory* DataPack::GetStaticMemory(uint16 resource_id) const { |
| 146 base::StringPiece piece; | 155 base::StringPiece piece; |
| 147 if (!GetStringPiece(resource_id, &piece)) | 156 if (!GetStringPiece(resource_id, &piece)) |
| 148 return NULL; | 157 return NULL; |
| 149 | 158 |
| 150 return new RefCountedStaticMemory( | 159 return new RefCountedStaticMemory( |
| 151 reinterpret_cast<const unsigned char*>(piece.data()), piece.length()); | 160 reinterpret_cast<const unsigned char*>(piece.data()), piece.length()); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 166 | 175 |
| 167 // Note: the python version of this function explicitly sorted keys, but | 176 // Note: the python version of this function explicitly sorted keys, but |
| 168 // std::map is a sorted associative container, we shouldn't have to do that. | 177 // std::map is a sorted associative container, we shouldn't have to do that. |
| 169 uint32 entry_count = resources.size(); | 178 uint32 entry_count = resources.size(); |
| 170 if (fwrite(&entry_count, sizeof(entry_count), 1, file) != 1) { | 179 if (fwrite(&entry_count, sizeof(entry_count), 1, file) != 1) { |
| 171 LOG(ERROR) << "Failed to write entry count"; | 180 LOG(ERROR) << "Failed to write entry count"; |
| 172 file_util::CloseFile(file); | 181 file_util::CloseFile(file); |
| 173 return false; | 182 return false; |
| 174 } | 183 } |
| 175 | 184 |
| 176 // Each entry is 1 uint16 + 2 uint32s. | 185 // Each entry is a uint16 + a uint32. |
| 177 uint32 index_length = entry_count * sizeof(DataPackEntry); | 186 uint32 index_length = entry_count * sizeof(DataPackEntry); |
| 178 uint32 data_offset = kHeaderLength + index_length; | 187 uint32 data_offset = kHeaderLength + index_length; |
| 179 for (std::map<uint16, base::StringPiece>::const_iterator it = | 188 for (std::map<uint16, base::StringPiece>::const_iterator it = |
| 180 resources.begin(); | 189 resources.begin(); |
| 181 it != resources.end(); ++it) { | 190 it != resources.end(); ++it) { |
| 182 uint16 resource_id = it->first; | 191 uint16 resource_id = it->first; |
| 183 if (fwrite(&resource_id, sizeof(resource_id), 1, file) != 1) { | 192 if (fwrite(&resource_id, sizeof(resource_id), 1, file) != 1) { |
| 184 LOG(ERROR) << "Failed to write id for " << resource_id; | 193 LOG(ERROR) << "Failed to write id for " << resource_id; |
| 185 file_util::CloseFile(file); | 194 file_util::CloseFile(file); |
| 186 return false; | 195 return false; |
| 187 } | 196 } |
| 188 | 197 |
| 189 if (fwrite(&data_offset, sizeof(data_offset), 1, file) != 1) { | 198 if (fwrite(&data_offset, sizeof(data_offset), 1, file) != 1) { |
| 190 LOG(ERROR) << "Failed to write offset for " << resource_id; | 199 LOG(ERROR) << "Failed to write offset for " << resource_id; |
| 191 file_util::CloseFile(file); | 200 file_util::CloseFile(file); |
| 192 return false; | 201 return false; |
| 193 } | 202 } |
| 194 | 203 |
| 195 uint32 len = it->second.length(); | 204 data_offset += it->second.length(); |
| 196 if (fwrite(&len, sizeof(len), 1, file) != 1) { | |
| 197 LOG(ERROR) << "Failed to write length for " << resource_id; | |
| 198 file_util::CloseFile(file); | |
| 199 return false; | |
| 200 } | |
| 201 | |
| 202 data_offset += len; | |
| 203 } | 205 } |
| 204 | 206 |
| 205 for (std::map<uint16, base::StringPiece>::const_iterator it = | 207 for (std::map<uint16, base::StringPiece>::const_iterator it = |
| 206 resources.begin(); | 208 resources.begin(); |
| 207 it != resources.end(); ++it) { | 209 it != resources.end(); ++it) { |
| 208 if (fwrite(it->second.data(), it->second.length(), 1, file) != 1) { | 210 if (fwrite(it->second.data(), it->second.length(), 1, file) != 1) { |
| 209 LOG(ERROR) << "Failed to write data for " << it->first; | 211 LOG(ERROR) << "Failed to write data for " << it->first; |
| 210 file_util::CloseFile(file); | 212 file_util::CloseFile(file); |
| 211 return false; | 213 return false; |
| 212 } | 214 } |
| 213 } | 215 } |
| 214 | 216 |
| 215 file_util::CloseFile(file); | 217 file_util::CloseFile(file); |
| 216 | 218 |
| 217 return true; | 219 return true; |
| 218 } | 220 } |
| 219 | 221 |
| 220 } // namespace ui | 222 } // namespace ui |
| OLD | NEW |