1 /*
2  * Copyright (C) 2014, 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 /*
18  * !!!!! DO NOT EDIT THIS FILE !!!!!
19  *
20  * This file was generated from
21  *   dictionary/structure/v4/ver4_pt_node_array_reader.cpp
22  */
23 
24 #include "dictionary/structure/backward/v402/ver4_pt_node_array_reader.h"
25 
26 #include "dictionary/structure/pt_common/dynamic_pt_reading_utils.h"
27 #include "dictionary/structure/pt_common/patricia_trie_reading_utils.h"
28 #include "dictionary/utils/buffer_with_extendable_buffer.h"
29 
30 namespace latinime {
31 namespace backward {
32 namespace v402 {
33 
readPtNodeArrayInfoAndReturnIfValid(const int ptNodeArrayPos,int * const outPtNodeCount,int * const outFirstPtNodePos) const34 bool Ver4PtNodeArrayReader::readPtNodeArrayInfoAndReturnIfValid(const int ptNodeArrayPos,
35         int *const outPtNodeCount, int *const outFirstPtNodePos) const {
36     if (ptNodeArrayPos < 0 || ptNodeArrayPos >= mBuffer->getTailPosition()) {
37         // Reading invalid position because of a bug or a broken dictionary.
38         AKLOGE("Reading PtNode array info from invalid dictionary position: %d, dict size: %d",
39                 ptNodeArrayPos, mBuffer->getTailPosition());
40         ASSERT(false);
41         return false;
42     }
43     const bool usesAdditionalBuffer = mBuffer->isInAdditionalBuffer(ptNodeArrayPos);
44     const uint8_t *const dictBuf = mBuffer->getBuffer(usesAdditionalBuffer);
45     int readingPos = ptNodeArrayPos;
46     if (usesAdditionalBuffer) {
47         readingPos -= mBuffer->getOriginalBufferSize();
48     }
49     const int ptNodeCountInArray = PatriciaTrieReadingUtils::getPtNodeArraySizeAndAdvancePosition(
50             dictBuf, &readingPos);
51     if (usesAdditionalBuffer) {
52         readingPos += mBuffer->getOriginalBufferSize();
53     }
54     if (ptNodeCountInArray < 0) {
55         AKLOGE("Invalid PtNode count in an array: %d.", ptNodeCountInArray);
56         return false;
57     }
58     *outPtNodeCount = ptNodeCountInArray;
59     *outFirstPtNodePos = readingPos;
60     return true;
61 }
62 
readForwardLinkAndReturnIfValid(const int forwordLinkPos,int * const outNextPtNodeArrayPos) const63 bool Ver4PtNodeArrayReader::readForwardLinkAndReturnIfValid(const int forwordLinkPos,
64         int *const outNextPtNodeArrayPos) const {
65     if (forwordLinkPos < 0 || forwordLinkPos >= mBuffer->getTailPosition()) {
66         // Reading invalid position because of bug or broken dictionary.
67         AKLOGE("Reading forward link from invalid dictionary position: %d, dict size: %d",
68                 forwordLinkPos, mBuffer->getTailPosition());
69         ASSERT(false);
70         return false;
71     }
72     const bool usesAdditionalBuffer = mBuffer->isInAdditionalBuffer(forwordLinkPos);
73     const uint8_t *const dictBuf = mBuffer->getBuffer(usesAdditionalBuffer);
74     int readingPos = forwordLinkPos;
75     if (usesAdditionalBuffer) {
76         readingPos -= mBuffer->getOriginalBufferSize();
77     }
78     const int nextPtNodeArrayOffset =
79             DynamicPtReadingUtils::getForwardLinkPosition(dictBuf, readingPos);
80     if (DynamicPtReadingUtils::isValidForwardLinkPosition(nextPtNodeArrayOffset)) {
81         *outNextPtNodeArrayPos = forwordLinkPos + nextPtNodeArrayOffset;
82     } else {
83         *outNextPtNodeArrayPos = NOT_A_DICT_POS;
84     }
85     return true;
86 }
87 
88 } // namespace v402
89 } // namespace backward
90 } // namespace latinime
91