| 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 // A word is four bytes. |
| 21 static const size_t kWord = 4; |
| 22 |
| 23 static const uint32 kFileFormatVersion = 1; |
| 21 // Length of file header: version and entry count. | 24 // Length of file header: version and entry count. |
| 22 static const size_t kHeaderLength = 2 * sizeof(uint32); | 25 static const size_t kHeaderLength = 2 * sizeof(uint32); |
| 23 | 26 |
| 24 #pragma pack(push,2) | |
| 25 struct DataPackEntry { | 27 struct DataPackEntry { |
| 26 uint16 resource_id; | 28 uint32 resource_id; |
| 27 uint32 file_offset; | 29 uint32 file_offset; |
| 28 uint32 length; | 30 uint32 length; |
| 29 | 31 |
| 30 static int CompareById(const void* void_key, const void* void_entry) { | 32 static int CompareById(const void* void_key, const void* void_entry) { |
| 31 uint16 key = *reinterpret_cast<const uint16*>(void_key); | 33 uint32 key = *reinterpret_cast<const uint32*>(void_key); |
| 32 const DataPackEntry* entry = | 34 const DataPackEntry* entry = |
| 33 reinterpret_cast<const DataPackEntry*>(void_entry); | 35 reinterpret_cast<const DataPackEntry*>(void_entry); |
| 34 if (key < entry->resource_id) { | 36 if (key < entry->resource_id) { |
| 35 return -1; | 37 return -1; |
| 36 } else if (key > entry->resource_id) { | 38 } else if (key > entry->resource_id) { |
| 37 return 1; | 39 return 1; |
| 38 } else { | 40 } else { |
| 39 return 0; | 41 return 0; |
| 40 } | 42 } |
| 41 } | 43 } |
| 42 }; | 44 }; |
| 43 #pragma pack(pop) | |
| 44 | 45 |
| 45 COMPILE_ASSERT(sizeof(DataPackEntry) == 10, size_of_header_must_be_ten); | 46 COMPILE_ASSERT(sizeof(DataPackEntry) == 12, size_of_header_must_be_twelve); |
| 46 | 47 |
| 47 // We're crashing when trying to load a pak file on Windows. Add some error | 48 // We're crashing when trying to load a pak file on Windows. Add some error |
| 48 // codes for logging. | 49 // codes for logging. |
| 49 // http://crbug.com/58056 | 50 // http://crbug.com/58056 |
| 50 enum LoadErrors { | 51 enum LoadErrors { |
| 51 INIT_FAILED = 1, | 52 INIT_FAILED = 1, |
| 52 BAD_VERSION, | 53 BAD_VERSION, |
| 53 INDEX_TRUNCATED, | 54 INDEX_TRUNCATED, |
| 54 ENTRY_NOT_FOUND, | 55 ENTRY_NOT_FOUND, |
| 55 | 56 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", ENTRY_NOT_FOUND, | 112 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", ENTRY_NOT_FOUND, |
| 112 LOAD_ERRORS_COUNT); | 113 LOAD_ERRORS_COUNT); |
| 113 mmap_.reset(); | 114 mmap_.reset(); |
| 114 return false; | 115 return false; |
| 115 } | 116 } |
| 116 } | 117 } |
| 117 | 118 |
| 118 return true; | 119 return true; |
| 119 } | 120 } |
| 120 | 121 |
| 121 bool DataPack::GetStringPiece(uint16 resource_id, | 122 bool DataPack::GetStringPiece(uint32 resource_id, |
| 122 base::StringPiece* data) const { | 123 base::StringPiece* data) const { |
| 123 // It won't be hard to make this endian-agnostic, but it's not worth | 124 // It won't be hard to make this endian-agnostic, but it's not worth |
| 124 // bothering to do right now. | 125 // bothering to do right now. |
| 125 #if defined(__BYTE_ORDER) | 126 #if defined(__BYTE_ORDER) |
| 126 // Linux check | 127 // Linux check |
| 127 COMPILE_ASSERT(__BYTE_ORDER == __LITTLE_ENDIAN, | 128 COMPILE_ASSERT(__BYTE_ORDER == __LITTLE_ENDIAN, |
| 128 datapack_assumes_little_endian); | 129 datapack_assumes_little_endian); |
| 129 #elif defined(__BIG_ENDIAN__) | 130 #elif defined(__BIG_ENDIAN__) |
| 130 // Mac check | 131 // Mac check |
| 131 #error DataPack assumes little endian | 132 #error DataPack assumes little endian |
| 132 #endif | 133 #endif |
| 133 | 134 |
| 134 DataPackEntry* target = reinterpret_cast<DataPackEntry*>( | 135 DataPackEntry* target = reinterpret_cast<DataPackEntry*>( |
| 135 bsearch(&resource_id, mmap_->data() + kHeaderLength, resource_count_, | 136 bsearch(&resource_id, mmap_->data() + kHeaderLength, resource_count_, |
| 136 sizeof(DataPackEntry), DataPackEntry::CompareById)); | 137 sizeof(DataPackEntry), DataPackEntry::CompareById)); |
| 137 if (!target) { | 138 if (!target) { |
| 138 return false; | 139 return false; |
| 139 } | 140 } |
| 140 | 141 |
| 141 data->set(mmap_->data() + target->file_offset, target->length); | 142 data->set(mmap_->data() + target->file_offset, target->length); |
| 142 return true; | 143 return true; |
| 143 } | 144 } |
| 144 | 145 |
| 145 RefCountedStaticMemory* DataPack::GetStaticMemory(uint16 resource_id) const { | 146 RefCountedStaticMemory* DataPack::GetStaticMemory(uint32 resource_id) const { |
| 146 base::StringPiece piece; | 147 base::StringPiece piece; |
| 147 if (!GetStringPiece(resource_id, &piece)) | 148 if (!GetStringPiece(resource_id, &piece)) |
| 148 return NULL; | 149 return NULL; |
| 149 | 150 |
| 150 return new RefCountedStaticMemory( | 151 return new RefCountedStaticMemory( |
| 151 reinterpret_cast<const unsigned char*>(piece.data()), piece.length()); | 152 reinterpret_cast<const unsigned char*>(piece.data()), piece.length()); |
| 152 } | 153 } |
| 153 | 154 |
| 154 // static | 155 // static |
| 155 bool DataPack::WritePack(const FilePath& path, | 156 bool DataPack::WritePack(const FilePath& path, |
| 156 const std::map<uint16, base::StringPiece>& resources) { | 157 const std::map<uint32, base::StringPiece>& resources) { |
| 157 FILE* file = file_util::OpenFile(path, "wb"); | 158 FILE* file = file_util::OpenFile(path, "wb"); |
| 158 if (!file) | 159 if (!file) |
| 159 return false; | 160 return false; |
| 160 | 161 |
| 161 if (fwrite(&kFileFormatVersion, sizeof(kFileFormatVersion), 1, file) != 1) { | 162 if (fwrite(&kFileFormatVersion, 1, kWord, file) != kWord) { |
| 162 LOG(ERROR) << "Failed to write file version"; | 163 LOG(ERROR) << "Failed to write file version"; |
| 163 file_util::CloseFile(file); | 164 file_util::CloseFile(file); |
| 164 return false; | 165 return false; |
| 165 } | 166 } |
| 166 | 167 |
| 167 // Note: the python version of this function explicitly sorted keys, but | 168 // 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. | 169 // std::map is a sorted associative container, we shouldn't have to do that. |
| 169 uint32 entry_count = resources.size(); | 170 uint32 entry_count = resources.size(); |
| 170 if (fwrite(&entry_count, sizeof(entry_count), 1, file) != 1) { | 171 if (fwrite(&entry_count, 1, kWord, file) != kWord) { |
| 171 LOG(ERROR) << "Failed to write entry count"; | 172 LOG(ERROR) << "Failed to write entry count"; |
| 172 file_util::CloseFile(file); | 173 file_util::CloseFile(file); |
| 173 return false; | 174 return false; |
| 174 } | 175 } |
| 175 | 176 |
| 176 // Each entry is 1 uint16 + 2 uint32s. | 177 // Each entry is 3 uint32s. |
| 177 uint32 index_length = entry_count * sizeof(DataPackEntry); | 178 uint32 index_length = entry_count * 3 * kWord; |
| 178 uint32 data_offset = kHeaderLength + index_length; | 179 uint32 data_offset = kHeaderLength + index_length; |
| 179 for (std::map<uint16, base::StringPiece>::const_iterator it = | 180 for (std::map<uint32, base::StringPiece>::const_iterator it = |
| 180 resources.begin(); | 181 resources.begin(); |
| 181 it != resources.end(); ++it) { | 182 it != resources.end(); ++it) { |
| 182 uint16 resource_id = it->first; | 183 if (fwrite(&it->first, 1, kWord, file) != kWord) { |
| 183 if (fwrite(&resource_id, sizeof(resource_id), 1, file) != 1) { | 184 LOG(ERROR) << "Failed to write id for " << it->first; |
| 184 LOG(ERROR) << "Failed to write id for " << resource_id; | |
| 185 file_util::CloseFile(file); | 185 file_util::CloseFile(file); |
| 186 return false; | 186 return false; |
| 187 } | 187 } |
| 188 | 188 |
| 189 if (fwrite(&data_offset, sizeof(data_offset), 1, file) != 1) { | 189 if (fwrite(&data_offset, 1, kWord, file) != kWord) { |
| 190 LOG(ERROR) << "Failed to write offset for " << resource_id; | 190 LOG(ERROR) << "Failed to write offset for " << it->first; |
| 191 file_util::CloseFile(file); | 191 file_util::CloseFile(file); |
| 192 return false; | 192 return false; |
| 193 } | 193 } |
| 194 | 194 |
| 195 uint32 len = it->second.length(); | 195 uint32 len = it->second.length(); |
| 196 if (fwrite(&len, sizeof(len), 1, file) != 1) { | 196 if (fwrite(&len, 1, kWord, file) != kWord) { |
| 197 LOG(ERROR) << "Failed to write length for " << resource_id; | 197 LOG(ERROR) << "Failed to write length for " << it->first; |
| 198 file_util::CloseFile(file); | 198 file_util::CloseFile(file); |
| 199 return false; | 199 return false; |
| 200 } | 200 } |
| 201 | 201 |
| 202 data_offset += len; | 202 data_offset += len; |
| 203 } | 203 } |
| 204 | 204 |
| 205 for (std::map<uint16, base::StringPiece>::const_iterator it = | 205 for (std::map<uint32, base::StringPiece>::const_iterator it = |
| 206 resources.begin(); | 206 resources.begin(); |
| 207 it != resources.end(); ++it) { | 207 it != resources.end(); ++it) { |
| 208 if (fwrite(it->second.data(), it->second.length(), 1, file) != 1) { | 208 if (fwrite(it->second.data(), it->second.length(), 1, file) != 1) { |
| 209 LOG(ERROR) << "Failed to write data for " << it->first; | 209 LOG(ERROR) << "Failed to write data for " << it->first; |
| 210 file_util::CloseFile(file); | 210 file_util::CloseFile(file); |
| 211 return false; | 211 return false; |
| 212 } | 212 } |
| 213 } | 213 } |
| 214 | 214 |
| 215 file_util::CloseFile(file); | 215 file_util::CloseFile(file); |
| 216 | 216 |
| 217 return true; | 217 return true; |
| 218 } | 218 } |
| 219 | 219 |
| 220 } // namespace ui | 220 } // namespace ui |
| OLD | NEW |