| 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 = 3; | 20 static const uint32 kFileFormatVersion = 4; |
| 21 // Length of file header: version and entry count. | 21 // Length of file header: version, entry count and text encoding type. |
| 22 static const size_t kHeaderLength = 2 * sizeof(uint32); | 22 static const size_t kHeaderLength = 2 * sizeof(uint32) + sizeof(uint8); |
| 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 | 28 |
| 29 static int CompareById(const void* void_key, const void* void_entry) { | 29 static int CompareById(const void* void_key, const void* void_entry) { |
| 30 uint16 key = *reinterpret_cast<const uint16*>(void_key); | 30 uint16 key = *reinterpret_cast<const uint16*>(void_key); |
| 31 const DataPackEntry* entry = | 31 const DataPackEntry* entry = |
| 32 reinterpret_cast<const DataPackEntry*>(void_entry); | 32 reinterpret_cast<const DataPackEntry*>(void_entry); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 53 ENTRY_NOT_FOUND, | 53 ENTRY_NOT_FOUND, |
| 54 | 54 |
| 55 LOAD_ERRORS_COUNT, | 55 LOAD_ERRORS_COUNT, |
| 56 }; | 56 }; |
| 57 | 57 |
| 58 } // namespace | 58 } // namespace |
| 59 | 59 |
| 60 namespace ui { | 60 namespace ui { |
| 61 | 61 |
| 62 // In .cc for MemoryMappedFile dtor. | 62 // In .cc for MemoryMappedFile dtor. |
| 63 DataPack::DataPack() : resource_count_(0) { | 63 DataPack::DataPack() : resource_count_(0), text_encoding_type_(BINARY) { |
| 64 } | 64 } |
| 65 DataPack::~DataPack() { | 65 DataPack::~DataPack() { |
| 66 } | 66 } |
| 67 | 67 |
| 68 bool DataPack::Load(const FilePath& path) { | 68 bool DataPack::Load(const FilePath& path) { |
| 69 mmap_.reset(new file_util::MemoryMappedFile); | 69 mmap_.reset(new file_util::MemoryMappedFile); |
| 70 if (!mmap_->Initialize(path)) { | 70 if (!mmap_->Initialize(path)) { |
| 71 DLOG(ERROR) << "Failed to mmap datapack"; | 71 DLOG(ERROR) << "Failed to mmap datapack"; |
| 72 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INIT_FAILED, | 72 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INIT_FAILED, |
| 73 LOAD_ERRORS_COUNT); | 73 LOAD_ERRORS_COUNT); |
| 74 mmap_.reset(); | 74 mmap_.reset(); |
| 75 return false; | 75 return false; |
| 76 } | 76 } |
| 77 | 77 |
| 78 // Sanity check the header of the file. | 78 // Sanity check the header of the file. |
| 79 if (kHeaderLength > mmap_->length()) { | 79 if (kHeaderLength > mmap_->length()) { |
| 80 DLOG(ERROR) << "Data pack file corruption: incomplete file header."; | 80 DLOG(ERROR) << "Data pack file corruption: incomplete file header."; |
| 81 mmap_.reset(); | 81 mmap_.reset(); |
| 82 return false; | 82 return false; |
| 83 } | 83 } |
| 84 | 84 |
| 85 // Parse the header of the file. | 85 // Parse the header of the file. |
| 86 // First uint32: version; second: resource count. | 86 // First uint32: version; second: resource count; |
| 87 const uint32* ptr = reinterpret_cast<const uint32*>(mmap_->data()); | 87 const uint32* ptr = reinterpret_cast<const uint32*>(mmap_->data()); |
| 88 uint32 version = ptr[0]; | 88 uint32 version = ptr[0]; |
| 89 if (version != kFileFormatVersion) { | 89 if (version != kFileFormatVersion) { |
| 90 LOG(ERROR) << "Bad data pack version: got " << version << ", expected " | 90 LOG(ERROR) << "Bad data pack version: got " << version << ", expected " |
| 91 << kFileFormatVersion; | 91 << kFileFormatVersion; |
| 92 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", BAD_VERSION, | 92 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", BAD_VERSION, |
| 93 LOAD_ERRORS_COUNT); | 93 LOAD_ERRORS_COUNT); |
| 94 mmap_.reset(); | 94 mmap_.reset(); |
| 95 return false; | 95 return false; |
| 96 } | 96 } |
| 97 resource_count_ = ptr[1]; | 97 resource_count_ = ptr[1]; |
| 98 | 98 |
| 99 // third: text encoding. |
| 100 const uint8* ptr_encoding = reinterpret_cast<const uint8*>(ptr + 2); |
| 101 text_encoding_type_ = static_cast<TextEncodingType>(*ptr_encoding); |
| 102 if (text_encoding_type_ != UTF8 && text_encoding_type_ != UTF16 && |
| 103 text_encoding_type_ != BINARY) { |
| 104 LOG(ERROR) << "Bad data pack text encoding: got " << text_encoding_type_ |
| 105 << ", expected between " << BINARY << " and " << UTF16; |
| 106 mmap_.reset(); |
| 107 return false; |
| 108 } |
| 109 |
| 99 // Sanity check the file. | 110 // Sanity check the file. |
| 100 // 1) Check we have enough entries. | 111 // 1) Check we have enough entries. |
| 101 if (kHeaderLength + resource_count_ * sizeof(DataPackEntry) > | 112 if (kHeaderLength + resource_count_ * sizeof(DataPackEntry) > |
| 102 mmap_->length()) { | 113 mmap_->length()) { |
| 103 LOG(ERROR) << "Data pack file corruption: too short for number of " | 114 LOG(ERROR) << "Data pack file corruption: too short for number of " |
| 104 "entries specified."; | 115 "entries specified."; |
| 105 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INDEX_TRUNCATED, | 116 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INDEX_TRUNCATED, |
| 106 LOAD_ERRORS_COUNT); | 117 LOAD_ERRORS_COUNT); |
| 107 mmap_.reset(); | 118 mmap_.reset(); |
| 108 return false; | 119 return false; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 base::StringPiece piece; | 167 base::StringPiece piece; |
| 157 if (!GetStringPiece(resource_id, &piece)) | 168 if (!GetStringPiece(resource_id, &piece)) |
| 158 return NULL; | 169 return NULL; |
| 159 | 170 |
| 160 return new RefCountedStaticMemory( | 171 return new RefCountedStaticMemory( |
| 161 reinterpret_cast<const unsigned char*>(piece.data()), piece.length()); | 172 reinterpret_cast<const unsigned char*>(piece.data()), piece.length()); |
| 162 } | 173 } |
| 163 | 174 |
| 164 // static | 175 // static |
| 165 bool DataPack::WritePack(const FilePath& path, | 176 bool DataPack::WritePack(const FilePath& path, |
| 166 const std::map<uint16, base::StringPiece>& resources) { | 177 const std::map<uint16, base::StringPiece>& resources, |
| 178 TextEncodingType textEncodingType) { |
| 167 FILE* file = file_util::OpenFile(path, "wb"); | 179 FILE* file = file_util::OpenFile(path, "wb"); |
| 168 if (!file) | 180 if (!file) |
| 169 return false; | 181 return false; |
| 170 | 182 |
| 171 if (fwrite(&kFileFormatVersion, sizeof(kFileFormatVersion), 1, file) != 1) { | 183 if (fwrite(&kFileFormatVersion, sizeof(kFileFormatVersion), 1, file) != 1) { |
| 172 LOG(ERROR) << "Failed to write file version"; | 184 LOG(ERROR) << "Failed to write file version"; |
| 173 file_util::CloseFile(file); | 185 file_util::CloseFile(file); |
| 174 return false; | 186 return false; |
| 175 } | 187 } |
| 176 | 188 |
| 177 // Note: the python version of this function explicitly sorted keys, but | 189 // Note: the python version of this function explicitly sorted keys, but |
| 178 // std::map is a sorted associative container, we shouldn't have to do that. | 190 // std::map is a sorted associative container, we shouldn't have to do that. |
| 179 uint32 entry_count = resources.size(); | 191 uint32 entry_count = resources.size(); |
| 180 if (fwrite(&entry_count, sizeof(entry_count), 1, file) != 1) { | 192 if (fwrite(&entry_count, sizeof(entry_count), 1, file) != 1) { |
| 181 LOG(ERROR) << "Failed to write entry count"; | 193 LOG(ERROR) << "Failed to write entry count"; |
| 182 file_util::CloseFile(file); | 194 file_util::CloseFile(file); |
| 183 return false; | 195 return false; |
| 184 } | 196 } |
| 185 | 197 |
| 198 if (textEncodingType != UTF8 && textEncodingType != UTF16 && |
| 199 textEncodingType != BINARY) { |
| 200 LOG(ERROR) << "Invalid text encoding type, got " << textEncodingType |
| 201 << ", expected between " << BINARY << " and " << UTF16; |
| 202 file_util::CloseFile(file); |
| 203 return false; |
| 204 } |
| 205 |
| 206 uint8 write_buffer = textEncodingType; |
| 207 if (fwrite(&write_buffer, sizeof(uint8), 1, file) != 1) { |
| 208 LOG(ERROR) << "Failed to write file text resources encoding"; |
| 209 file_util::CloseFile(file); |
| 210 return false; |
| 211 } |
| 212 |
| 186 // Each entry is a uint16 + a uint32. We have an extra entry after the last | 213 // Each entry is a uint16 + a uint32. We have an extra entry after the last |
| 187 // item so we can compute the size of the list item. | 214 // item so we can compute the size of the list item. |
| 188 uint32 index_length = (entry_count + 1) * sizeof(DataPackEntry); | 215 uint32 index_length = (entry_count + 1) * sizeof(DataPackEntry); |
| 189 uint32 data_offset = kHeaderLength + index_length; | 216 uint32 data_offset = kHeaderLength + index_length; |
| 190 for (std::map<uint16, base::StringPiece>::const_iterator it = | 217 for (std::map<uint16, base::StringPiece>::const_iterator it = |
| 191 resources.begin(); | 218 resources.begin(); |
| 192 it != resources.end(); ++it) { | 219 it != resources.end(); ++it) { |
| 193 uint16 resource_id = it->first; | 220 uint16 resource_id = it->first; |
| 194 if (fwrite(&resource_id, sizeof(resource_id), 1, file) != 1) { | 221 if (fwrite(&resource_id, sizeof(resource_id), 1, file) != 1) { |
| 195 LOG(ERROR) << "Failed to write id for " << resource_id; | 222 LOG(ERROR) << "Failed to write id for " << resource_id; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 return false; | 257 return false; |
| 231 } | 258 } |
| 232 } | 259 } |
| 233 | 260 |
| 234 file_util::CloseFile(file); | 261 file_util::CloseFile(file); |
| 235 | 262 |
| 236 return true; | 263 return true; |
| 237 } | 264 } |
| 238 | 265 |
| 239 } // namespace ui | 266 } // namespace ui |
| OLD | NEW |