diff options
author | 2011-07-20 18:42:32 +0900 | |
---|---|---|
committer | 2011-07-20 19:43:14 +0900 | |
commit | f0a980966264fa98ef8e1b834650d9bf54de92ae (patch) | |
tree | 9d324e887198592b9d888652464e171f05241ebb /native/src/binary_format.h | |
parent | 597b1157970462dcd429be608cac21e33011e9df (diff) | |
download | latinime-f0a980966264fa98ef8e1b834650d9bf54de92ae.tar.gz latinime-f0a980966264fa98ef8e1b834650d9bf54de92ae.tar.xz latinime-f0a980966264fa98ef8e1b834650d9bf54de92ae.zip |
Check the binary dictionary magic number
...and return NULL if it does not matched an expected value.
Bug: 5052486
Change-Id: I1dc7955d2785ee080bc5c22398be9befe332f096
Diffstat (limited to 'native/src/binary_format.h')
-rw-r--r-- | native/src/binary_format.h | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/native/src/binary_format.h b/native/src/binary_format.h index e9f108e25..7deec27d3 100644 --- a/native/src/binary_format.h +++ b/native/src/binary_format.h @@ -17,6 +17,8 @@ #ifndef LATINIME_BINARY_FORMAT_H #define LATINIME_BINARY_FORMAT_H +#include "unigram_dictionary.h" + namespace latinime { class BinaryFormat { @@ -26,6 +28,11 @@ private: const static int MULTIPLE_BYTE_CHARACTER_ADDITIONAL_SIZE = 2; public: + const static int UNKNOWN_FORMAT = -1; + const static int FORMAT_VERSION_1 = 1; + const static uint16_t FORMAT_VERSION_1_MAGIC_NUMBER = 0x78B1; + + static int detectFormat(const uint8_t* const dict); static int getGroupCountAndForwardPointer(const uint8_t* const dict, int* pos); static uint8_t getFlagsAndForwardPointer(const uint8_t* const dict, int* pos); static int32_t getCharCodeAndForwardPointer(const uint8_t* const dict, int* pos); @@ -43,6 +50,12 @@ public: int *pos); }; +inline int BinaryFormat::detectFormat(const uint8_t* const dict) { + const uint16_t magicNumber = (dict[0] << 8) + dict[1]; // big endian + if (FORMAT_VERSION_1_MAGIC_NUMBER == magicNumber) return FORMAT_VERSION_1; + return UNKNOWN_FORMAT; +} + inline int BinaryFormat::getGroupCountAndForwardPointer(const uint8_t* const dict, int* pos) { return dict[(*pos)++]; } |