LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
BinaryEntry.cpp
Go to the documentation of this file.
1// Copyright 2002-2013 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6// NAUTITIA
7//
8// jys 2-OCT-2002
9//
10// Binary memory explorer.
11// BinaryEntry is the base class of other dictionnary structure
12// management class (DictionaryEntry, LingInfoEntry, etc.)
13// BinaryEntry has methods to explore dictionary in memory.
14// Dictionary memory strcture is described in NAU2002.r2128 JYS
15
16#include "BinaryEntry.h"
17
19#include <iostream>
20
21using namespace std;
22using namespace Lima::Common::Misc;
23
24namespace Lima {
25namespace LinguisticProcessing {
26namespace Dictionary {
27
28
30 _dummyString()
31{
32}
33
35 _dummyString()
36{
37 // nothing to copy
38}
39
43
45{
46 // nothing to copy
47 return *this;
48}
49
50// gets the pointed encoded number (presented in big endian)
51// advances pointer to point after the number
52uint64_t BinaryEntry::getEncodedNumber(unsigned char*& ptr) const
53{
54// cout<<"BinaryEntry::getEncodedNumber() "<<hex<<(int)*ptr<<" "<<(int)*(ptr+1)<<" "<<(int)*(ptr+2)<<endl;
55 unsigned char firstByte = *ptr++;
56 uint64_t result = firstByte;
57 if (firstByte >= 0x80) {
58 result = (result-0x80)*0x100 + *ptr++;
59 if (firstByte >= 0xC0) {
60 result = (result-0x4000)*0x100 + *ptr++;
61 if (firstByte >= 0xE0) {
62 result = (result-0x200000)*0x100 + *ptr++;
63 if (firstByte >= 0xF0)
64 throw InvNumberException();
65 }
66 }
67 }
68 return result;
69}
70
71// gets the pointed encoded number (presented in big endian)
72// pointer is not moved
73uint64_t BinaryEntry::getEncodedNumberNoMove(unsigned char* ptr) const {
74 unsigned char *ptr2 = ptr;
75 return getEncodedNumber(ptr2);
76}
77
78// gets the next number of specified number of bytes (presented in big endian)
79// advances pointer to point after the number
80uint64_t BinaryEntry::getNumber(unsigned char*& ptr,
81 const uint64_t count) const
82 {
83 if ((count > 4) || (count < 1)) throw InvNumberException();
84 uint64_t result = *ptr++;
85 for (uint64_t loop=1; loop<count; loop++)
86 result = result*0x100 + *ptr++;
87 return result;
88}
89
90// gets the next number of specified number of bytes (presented in big endian)
91// pointer is not moved
92uint64_t BinaryEntry::getNumberNoMove(unsigned char* ptr,
93 const uint64_t count) const
94{
95 unsigned char *ptr2 = ptr;
96 return getNumber(ptr2, count);
97}
98
99// gets the pointed dictionary string presented in big endian as a LimaString
100// advances pointer to point after the string
101LimaString BinaryEntry::getString(unsigned char*& ptr) const {
102 LimaString foundString;
103 uint64_t count = getEncodedNumber(ptr);
104 uint64_t testLittleEndian = 13; // if platform in little endian, reverse
105 if (testLittleEndian == (*(unsigned char*)&testLittleEndian))
106 foundString = setStringReverseEndian((unsigned short*)ptr, count/2);
107 else
108 foundString = setStringSameEndian((unsigned short*)ptr, count/2);
109 ptr = ptr+count;
110 return foundString;
111}
112
113// gets the pointed dictionary string as a LimaString
114// pointer is not moved
116{
117 unsigned char *ptr2 = ptr;
118 return getString(ptr2);
119}
120
121// gets the pointed dictionary string presented in UTF8 as a LimaString
122// advances pointer to point after the string
123LimaString BinaryEntry::getUtf8String(unsigned char*& ptr) const {
124 uint64_t maxSize = getEncodedNumber(ptr);
125 unsigned char *ptrEnd = ptr + maxSize;
126 LimaString utf16String; utf16String.resize(maxSize);
127 int j=0;
128 while (ptr < ptrEnd) {
129 QChar first{*ptr++ & 0xFF};
130 if (first < QChar{0x80}) // 7 bits available
131 utf16String[j++] = first;
132 else if (first < QChar{0xDF}) // 11 bits available
133 utf16String[j++] = LimaChar(((first.unicode()<<6)+(*ptr++&0x3F))&0x000007FF);
134 else if (first < QChar{0xEF}) // 16 bits available
135 {
136 auto firstOffset = (*ptr++&0x3F)<<6;
137 auto secondOffset = (*ptr++&0x3F);
138 utf16String[j++] = LimaChar(((first.unicode()<<12)+firstOffset+secondOffset)&0x0000FFFF);
139 }
140 else
141 throw BoundsErrorException();
142 }
143 ptr = ptrEnd;
144// #ifdef WIN32
145// LimaString result;
146// result.resize(j);
147// for (uint64_t i = 0; i < j; i++)
148// {
149// result[i] = LimaChar(utf16String[i]);
150// }
151// return result;
152// #else
153 return utf16String.left(j);
154// #endif
155}
156
157// gets the pointed dictionary string as a LimaString
158// pointer is not moved
160{
161 unsigned char *ptr2 = ptr;
162 return getUtf8String(ptr2);
163}
164
165// advances pointer to the next dictionary field
166unsigned char* BinaryEntry::nextField(unsigned char*& ptr) const
167{
168 uint64_t num = getEncodedNumber(ptr);
169 ptr += num;
170// ptr = getEncodedNumber(ptr) + ptr;
171 return ptr;
172}
173
174} // closing namespace Dictionary
175} // closing namespace LinguisticProcessing
176} // closing namespace Lima
uint64_t getNumberNoMove(unsigned char *ptr, const uint64_t count) const
Lima::LimaString getString(unsigned char *&ptr) const
uint64_t getEncodedNumber(unsigned char *&ptr) const
BinaryEntry & operator=(const BinaryEntry &entry)
unsigned char * nextField(unsigned char *&ptr) const
uint64_t getNumber(unsigned char *&ptr, const uint64_t count) const
Lima::LimaString getStringNoMove(unsigned char *ptr) const
Lima::LimaString getUtf8StringNoMove(unsigned char *ptr) const
uint64_t getEncodedNumberNoMove(unsigned char *ptr) const
Lima::LimaString getUtf8String(unsigned char *&ptr) const
LimaString setStringSameEndian(const unsigned short *s, const uint32_t count)
redefine an existing LimaString from UTF-16
LimaString setStringReverseEndian(const unsigned short *s, const uint32_t count)
redefine an existing LimaString from UTF-16 changing the endian mode
NAUTITIA.
QChar LimaChar
Definition LimaString.h:30
QString LimaString
Definition LimaString.h:33
STL namespace.