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 "suggest/policyimpl/dictionary/utils/format_utils.h" 18 19 #include "suggest/policyimpl/dictionary/utils/byte_array_utils.h" 20 21 namespace latinime { 22 23 const uint32_t FormatUtils::MAGIC_NUMBER = 0x9BC13AFE; 24 25 // Magic number (4 bytes), version (2 bytes), flags (2 bytes), header size (4 bytes) = 12 26 const int FormatUtils::DICTIONARY_MINIMUM_SIZE = 12; 27 28 /* static */ FormatUtils::FORMAT_VERSION FormatUtils::getFormatVersion(const int formatVersion) { 29 switch (formatVersion) { 30 case VERSION_2: 31 return VERSION_2; 32 case VERSION_4_ONLY_FOR_TESTING: 33 return VERSION_4_ONLY_FOR_TESTING; 34 case VERSION_4: 35 return VERSION_4; 36 case VERSION_4_DEV: 37 return VERSION_4_DEV; 38 default: 39 return UNKNOWN_VERSION; 40 } 41 } 42 /* static */ FormatUtils::FORMAT_VERSION FormatUtils::detectFormatVersion( 43 const uint8_t *const dict, const int dictSize) { 44 // The magic number is stored big-endian. 45 // If the dictionary is less than 4 bytes, we can't even read the magic number, so we don't 46 // understand this format. 47 if (dictSize < DICTIONARY_MINIMUM_SIZE) { 48 return UNKNOWN_VERSION; 49 } 50 const uint32_t magicNumber = ByteArrayUtils::readUint32(dict, 0); 51 switch (magicNumber) { 52 case MAGIC_NUMBER: 53 // The layout of the header is as follows: 54 // Magic number (4 bytes) 0x9B 0xC1 0x3A 0xFE 55 // Dictionary format version number (2 bytes) 56 // Options (2 bytes) 57 // Header size (4 bytes) : integer, big endian 58 // Conceptually this converts the hardcoded value of the bytes in the file into 59 // the symbolic value we use in the code. But we want the constants to be the 60 // same so we use them for both here. 61 return getFormatVersion(ByteArrayUtils::readUint16(dict, 4)); 62 default: 63 return UNKNOWN_VERSION; 64 } 65 } 66 67 } // namespace latinime 68