| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright (C) 2013 The Android Open Source Project |
| 3 * |
| 4 * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 * you may not use this file except in compliance with the License. |
| 6 * You may obtain a copy of the License at |
| 7 * |
| 8 * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 * |
| 10 * Unless required by applicable law or agreed to in writing, software |
| 11 * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 * See the License for the specific language governing permissions and |
| 14 * limitations under the License. |
| 15 */ |
| 16 |
| 17 #include "third_party/prediction/suggest/policyimpl/dictionary/utils/format_util
s.h" |
| 18 #include "third_party/prediction/suggest/policyimpl/dictionary/utils/byte_array_
utils.h" |
| 19 |
| 20 namespace latinime { |
| 21 |
| 22 const uint32_t FormatUtils::MAGIC_NUMBER = 0x9BC13AFE; |
| 23 |
| 24 // Magic number (4 bytes), version (2 bytes), flags (2 bytes), header size (4 |
| 25 // bytes) = 12 |
| 26 const int FormatUtils::DICTIONARY_MINIMUM_SIZE = 12; |
| 27 |
| 28 /* static */ FormatUtils::FORMAT_VERSION FormatUtils::getFormatVersion( |
| 29 const int formatVersion) { |
| 30 switch (formatVersion) { |
| 31 case VERSION_2: |
| 32 return VERSION_2; |
| 33 case VERSION_4_ONLY_FOR_TESTING: |
| 34 return VERSION_4_ONLY_FOR_TESTING; |
| 35 case VERSION_4: |
| 36 return VERSION_4; |
| 37 case VERSION_4_DEV: |
| 38 return VERSION_4_DEV; |
| 39 default: |
| 40 return UNKNOWN_VERSION; |
| 41 } |
| 42 } |
| 43 /* static */ FormatUtils::FORMAT_VERSION FormatUtils::detectFormatVersion( |
| 44 const uint8_t* const dict, |
| 45 const int dictSize) { |
| 46 // The magic number is stored big-endian. |
| 47 // If the dictionary is less than 4 bytes, we can't even read the magic |
| 48 // number, so we don't |
| 49 // understand this format. |
| 50 if (dictSize < DICTIONARY_MINIMUM_SIZE) { |
| 51 return UNKNOWN_VERSION; |
| 52 } |
| 53 const uint32_t magicNumber = ByteArrayUtils::readUint32(dict, 0); |
| 54 switch (magicNumber) { |
| 55 case MAGIC_NUMBER: |
| 56 // The layout of the header is as follows: |
| 57 // Magic number (4 bytes) 0x9B 0xC1 0x3A 0xFE |
| 58 // Dictionary format version number (2 bytes) |
| 59 // Options (2 bytes) |
| 60 // Header size (4 bytes) : integer, big endian |
| 61 // Conceptually this converts the hardcoded value of the bytes in the file |
| 62 // into |
| 63 // the symbolic value we use in the code. But we want the constants to be |
| 64 // the |
| 65 // same so we use them for both here. |
| 66 return getFormatVersion(ByteArrayUtils::readUint16(dict, 4)); |
| 67 default: |
| 68 return UNKNOWN_VERSION; |
| 69 } |
| 70 } |
| 71 |
| 72 } // namespace latinime |
| OLD | NEW |