OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "third_party/hunspell/google/bdict_reader.h" | 5 #include "third_party/hunspell/google/bdict_reader.h" |
6 | 6 |
| 7 #include <stdint.h> |
| 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 | 10 |
9 namespace hunspell { | 11 namespace hunspell { |
10 | 12 |
11 // Like the "Visitor" design pattern, this lightweight object provides an | 13 // Like the "Visitor" design pattern, this lightweight object provides an |
12 // interface around a serialized trie node at the given address in the memory. | 14 // interface around a serialized trie node at the given address in the memory. |
13 class NodeReader { | 15 class NodeReader { |
14 public: | 16 public: |
15 // Return values for GetChildAt. | 17 // Return values for GetChildAt. |
16 enum FindResult { | 18 enum FindResult { |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 // We may or may not need to ignore that first value we just read, since it | 327 // We may or may not need to ignore that first value we just read, since it |
326 // could be a dummy placeholder value. The |list_offset| is the starting | 328 // could be a dummy placeholder value. The |list_offset| is the starting |
327 // position in the output list to write the rest of the values, which may | 329 // position in the output list to write the rest of the values, which may |
328 // overwrite the first value. | 330 // overwrite the first value. |
329 int list_offset = 1; | 331 int list_offset = 1; |
330 if (affix_indices[0] == BDict::FIRST_AFFIX_IS_UNUSED) | 332 if (affix_indices[0] == BDict::FIRST_AFFIX_IS_UNUSED) |
331 list_offset = 0; | 333 list_offset = 0; |
332 | 334 |
333 // Save the end pointer (accounting for an odd number of bytes). | 335 // Save the end pointer (accounting for an odd number of bytes). |
334 size_t array_start = node_offset_ + additional_bytes + 2; | 336 size_t array_start = node_offset_ + additional_bytes + 2; |
335 const uint16* const bdict_short_end = reinterpret_cast<const uint16*>( | 337 const uint16_t* const bdict_short_end = reinterpret_cast<const uint16_t*>( |
336 &bdict_data_[((bdict_length_ - array_start) & -2) + array_start]); | 338 &bdict_data_[((bdict_length_ - array_start) & -2) + array_start]); |
337 // Process all remaining matches. | 339 // Process all remaining matches. |
338 const uint16* following_array = reinterpret_cast<const uint16*>( | 340 const uint16_t* following_array = |
339 &bdict_data_[array_start]); | 341 reinterpret_cast<const uint16_t*>(&bdict_data_[array_start]); |
340 for (int i = 0; i < BDict::MAX_AFFIXES_PER_WORD - list_offset; i++) { | 342 for (int i = 0; i < BDict::MAX_AFFIXES_PER_WORD - list_offset; i++) { |
341 if (&following_array[i] >= bdict_short_end) { | 343 if (&following_array[i] >= bdict_short_end) { |
342 is_valid_ = false; | 344 is_valid_ = false; |
343 return 0; | 345 return 0; |
344 } | 346 } |
345 if (following_array[i] == BDict::LEAF_NODE_FOLLOWING_LIST_TERMINATOR) | 347 if (following_array[i] == BDict::LEAF_NODE_FOLLOWING_LIST_TERMINATOR) |
346 return i + list_offset; // Found the end of the list. | 348 return i + list_offset; // Found the end of the list. |
347 affix_indices[i + list_offset] = following_array[i]; | 349 affix_indices[i + list_offset] = following_array[i]; |
348 } | 350 } |
349 return BDict::MAX_AFFIXES_PER_WORD; | 351 return BDict::MAX_AFFIXES_PER_WORD; |
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
715 header_->dic_offset > bdict_length) | 717 header_->dic_offset > bdict_length) |
716 return false; | 718 return false; |
717 | 719 |
718 // Get the affix header, make sure there is enough room for it. | 720 // Get the affix header, make sure there is enough room for it. |
719 if (header_->aff_offset + sizeof(BDict::AffHeader) > bdict_length) | 721 if (header_->aff_offset + sizeof(BDict::AffHeader) > bdict_length) |
720 return false; | 722 return false; |
721 aff_header_ = reinterpret_cast<const BDict::AffHeader*>( | 723 aff_header_ = reinterpret_cast<const BDict::AffHeader*>( |
722 &bdict_data[header_->aff_offset]); | 724 &bdict_data[header_->aff_offset]); |
723 | 725 |
724 // Make sure there is enough room for the affix group count dword. | 726 // Make sure there is enough room for the affix group count dword. |
725 if (aff_header_->affix_group_offset > bdict_length - sizeof(uint32)) | 727 if (aff_header_->affix_group_offset > bdict_length - sizeof(uint32_t)) |
726 return false; | 728 return false; |
727 | 729 |
728 // This function is called from SpellCheck::SpellCheckWord(), which blocks | 730 // This function is called from SpellCheck::SpellCheckWord(), which blocks |
729 // WebKit. To avoid blocking WebKit for a long time, we do not check the MD5 | 731 // WebKit. To avoid blocking WebKit for a long time, we do not check the MD5 |
730 // digest here. Instead we check the MD5 digest when Chrome finishes | 732 // digest here. Instead we check the MD5 digest when Chrome finishes |
731 // downloading a dictionary. | 733 // downloading a dictionary. |
732 | 734 |
733 // Don't set these until the end. This way, NULL bdict_data_ will indicate | 735 // Don't set these until the end. This way, NULL bdict_data_ will indicate |
734 // failure. | 736 // failure. |
735 bdict_data_ = bdict_data; | 737 bdict_data_ = bdict_data; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
784 aff_header_->rep_offset); | 786 aff_header_->rep_offset); |
785 } | 787 } |
786 | 788 |
787 | 789 |
788 WordIterator BDictReader::GetAllWordIterator() const { | 790 WordIterator BDictReader::GetAllWordIterator() const { |
789 NodeReader reader(bdict_data_, bdict_length_, header_->dic_offset, 0); | 791 NodeReader reader(bdict_data_, bdict_length_, header_->dic_offset, 0); |
790 return WordIterator(reader); | 792 return WordIterator(reader); |
791 } | 793 } |
792 | 794 |
793 } // namespace hunspell | 795 } // namespace hunspell |
OLD | NEW |