VirtualBox

source: vbox/trunk/src/VBox/Main/src-all/QMTranslatorImpl.cpp@ 66900

Last change on this file since 66900 was 65665, checked in by vboxsync, 8 years ago

gcc 7: Main: fall thru

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.8 KB
Line 
1/* $Id: QMTranslatorImpl.cpp 65665 2017-02-07 14:14:42Z vboxsync $ */
2/** @file
3 * VirtualBox API translation handling class
4 */
5
6/*
7 * Copyright (C) 2014-2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include <vector>
19#include <set>
20#include <algorithm>
21#include <iterator>
22#include <iprt/file.h>
23#include <iprt/asm.h>
24#include <VBox/com/string.h>
25#include <VBox/log.h>
26#include <QMTranslator.h>
27
28/* QM File Magic Number */
29static const size_t MagicLength = 16;
30static const uint8_t Magic[MagicLength] =
31{
32 0x3c, 0xb8, 0x64, 0x18, 0xca, 0xef, 0x9c, 0x95,
33 0xcd, 0x21, 0x1c, 0xbf, 0x60, 0xa1, 0xbd, 0xdd
34};
35
36/* Used internally */
37class QMException : public std::exception
38{
39 const char *m_str;
40public:
41 QMException(const char *str) : m_str(str) {}
42 virtual const char *what() const throw() { return m_str; }
43};
44
45/* Bytes stream. Used by the parser to iterate through the data */
46class QMBytesStream
47{
48 size_t m_cbSize;
49 const uint8_t * const m_dataStart;
50 const uint8_t *m_iter;
51 const uint8_t *m_end;
52
53 /* Function stub for transform method */
54 static uint16_t func_BE2H_U16(uint16_t value)
55 {
56 return RT_BE2H_U16(value);
57 }
58
59public:
60
61 QMBytesStream(const uint8_t *const dataStart, size_t cbSize) :
62 m_cbSize(dataStart ? cbSize : 0),
63 m_dataStart(dataStart),
64 m_iter(dataStart)
65 {
66 setEnd();
67 }
68
69 /* Sets end pointer
70 * Used in message reader to detect the end of message block */
71 inline void setEnd(size_t pos = 0)
72 {
73 m_end = m_dataStart + (pos && pos < m_cbSize ? pos : m_cbSize);
74 }
75
76 inline uint8_t read8()
77 {
78 checkSize(1);
79 return *m_iter++;
80 }
81
82 inline uint32_t read32()
83 {
84 checkSize(4);
85 uint32_t result = *reinterpret_cast<const uint32_t *>(m_iter);
86 m_iter += 4;
87 return RT_BE2H_U32(result);
88 }
89
90 /* Reads string in UTF16 and converts it into a UTF8 string */
91 inline com::Utf8Str readUtf16String()
92 {
93 uint32_t size = read32();
94 checkSize(size);
95 if (size & 1) throw QMException("Incorrect string size");
96 std::vector<uint16_t> wstr;
97 wstr.reserve(size / 2);
98
99 /* We cannot convert to host endianess without copying the data
100 * since the file might be mapped to the memory and any memory
101 * change will lead to the change of the file. */
102 std::transform(reinterpret_cast<const uint16_t *>(m_iter),
103 reinterpret_cast<const uint16_t *>(m_iter + size),
104 std::back_inserter(wstr),
105 func_BE2H_U16);
106 m_iter += size;
107 return com::Utf8Str((CBSTR) &wstr.front(), wstr.size());
108 }
109
110 /* Reads string in one-byte encoding
111 * The string is assumed to be in ISO-8859-1 encoding */
112 inline com::Utf8Str readString()
113 {
114 uint32_t size = read32();
115 checkSize(size);
116 com::Utf8Str result(reinterpret_cast<const char *>(m_iter), size);
117 m_iter += size;
118 return result;
119 }
120
121 /* Checks the magic number
122 * Should be called when in the beginning of the data */
123 inline void checkMagic()
124 {
125 checkSize(MagicLength);
126 if (memcmp(&(*m_iter), Magic, MagicLength)) throw QMException("Wrong magic number");
127 m_iter += MagicLength;
128 }
129
130 /* Has we reached the end pointer? */
131 inline bool hasFinished() { return m_iter == m_end; }
132
133 /* Returns current stream position */
134 inline size_t tellPos() { return m_iter - m_dataStart; }
135
136 /* Moves current pointer to a desired position */
137 inline void seek(int pos) { m_iter += pos; }
138
139 /* Checks whether stream has enough data to read size bytes */
140 inline void checkSize(int size)
141 {
142 if (m_end - m_iter < size) throw QMException("Incorrect item size");
143 }
144};
145
146/* Internal QMTranslator implementation */
147class QMTranslator_Impl
148{
149 struct QMMessage
150 {
151 /* Everything is in UTF-8 */
152 com::Utf8Str strContext;
153 com::Utf8Str strTranslation;
154 com::Utf8Str strComment;
155 com::Utf8Str strSource;
156 uint32_t hash;
157 QMMessage() : hash(0) {}
158 };
159
160 struct HashOffset
161 {
162 uint32_t hash;
163 uint32_t offset;
164
165 HashOffset(uint32_t _hash = 0, uint32_t _offs = 0) : hash(_hash), offset(_offs) {}
166
167 bool operator<(const HashOffset &obj) const
168 {
169 return (hash != obj.hash ? hash < obj.hash : offset < obj.offset);
170 }
171
172 };
173
174 typedef std::set<HashOffset> QMHashSet;
175 typedef QMHashSet::const_iterator QMHashSetConstIter;
176 typedef std::vector<QMMessage> QMMessageArray;
177
178 QMHashSet m_hashSet;
179 QMMessageArray m_messageArray;
180
181public:
182
183 QMTranslator_Impl() {}
184
185 const char *translate(const char *pszContext,
186 const char *pszSource,
187 const char *pszDisamb) const
188 {
189 QMHashSetConstIter iter;
190 QMHashSetConstIter lowerIter, upperIter;
191
192 do {
193 uint32_t hash = calculateHash(pszSource, pszDisamb);
194 lowerIter = m_hashSet.lower_bound(HashOffset(hash, 0));
195 upperIter = m_hashSet.upper_bound(HashOffset(hash, UINT32_MAX));
196
197 for (iter = lowerIter; iter != upperIter; ++iter)
198 {
199 const QMMessage &message = m_messageArray[iter->offset];
200 if ((!pszContext || !*pszContext || message.strContext == pszContext) &&
201 message.strSource == pszSource &&
202 ((pszDisamb && !*pszDisamb) || message.strComment == pszDisamb))
203 break;
204 }
205
206 /* Try without disambiguating comment if it isn't empty */
207 if (pszDisamb)
208 {
209 if (!*pszDisamb) pszDisamb = 0;
210 else pszDisamb = "";
211 }
212
213 } while (iter == upperIter && pszDisamb);
214
215 return (iter != upperIter ? m_messageArray[iter->offset].strTranslation.c_str() : "");
216 }
217
218 void load(QMBytesStream &stream)
219 {
220 /* Load into local variables. If we failed during the load,
221 * it would allow us to keep the object in a valid (previous) state. */
222 QMHashSet hashSet;
223 QMMessageArray messageArray;
224
225 stream.checkMagic();
226
227 while (!stream.hasFinished())
228 {
229 uint32_t sectionCode = stream.read8();
230 uint32_t sLen = stream.read32();
231
232 /* Hashes and Context sections are ignored. They contain hash tables
233 * to speed-up search which is not useful since we recalculate all hashes
234 * and don't perform context search by hash */
235 switch (sectionCode)
236 {
237 case Messages:
238 parseMessages(stream, &hashSet, &messageArray, sLen);
239 break;
240 case Hashes:
241 /* Only get size information to speed-up vector filling
242 * if Hashes section goes in the file before Message section */
243 m_messageArray.reserve(sLen >> 3);
244 /* fall thru */
245 case Context:
246 stream.seek(sLen);
247 break;
248 default:
249 throw QMException("Unkown section");
250 }
251 }
252 /* Store the data into member variables.
253 * The following functions never generate exceptions */
254 m_hashSet.swap(hashSet);
255 m_messageArray.swap(messageArray);
256 }
257
258private:
259
260 /* Some QM stuff */
261 enum SectionType
262 {
263 Hashes = 0x42,
264 Messages = 0x69,
265 Contexts = 0x2f
266 };
267
268 enum MessageType
269 {
270 End = 1,
271 SourceText16 = 2,
272 Translation = 3,
273 Context16 = 4,
274 Hash = 5,
275 SourceText = 6,
276 Context = 7,
277 Comment = 8
278 };
279
280 /* Read messages from the stream. */
281 static void parseMessages(QMBytesStream &stream, QMHashSet * const hashSet, QMMessageArray * const messageArray, size_t cbSize)
282 {
283 stream.setEnd(stream.tellPos() + cbSize);
284 uint32_t cMessage = 0;
285 while (!stream.hasFinished())
286 {
287 QMMessage message;
288 HashOffset hashOffs;
289
290 parseMessageRecord(stream, &message);
291 if (!message.hash)
292 message.hash = calculateHash(message.strSource.c_str(), message.strComment.c_str());
293
294 hashOffs.hash = message.hash;
295 hashOffs.offset = cMessage++;
296
297 hashSet->insert(hashOffs);
298 messageArray->push_back(message);
299 }
300 stream.setEnd();
301 }
302
303 /* Parse one message from the stream */
304 static void parseMessageRecord(QMBytesStream &stream, QMMessage * const message)
305 {
306 while (!stream.hasFinished())
307 {
308 uint8_t type = stream.read8();
309 switch (type)
310 {
311 case End:
312 return;
313 /* Ignored as obsolete */
314 case Context16:
315 case SourceText16:
316 stream.seek(stream.read32());
317 break;
318 case Translation:
319 {
320 com::Utf8Str str = stream.readUtf16String();
321 message->strTranslation.swap(str);
322 break;
323 }
324 case Hash:
325 message->hash = stream.read32();
326 break;
327
328 case SourceText:
329 {
330 com::Utf8Str str = stream.readString();
331 message->strSource.swap(str);
332 break;
333 }
334
335 case Context:
336 {
337 com::Utf8Str str = stream.readString();
338 message->strContext.swap(str);
339 break;
340 }
341
342 case Comment:
343 {
344 com::Utf8Str str = stream.readString();
345 message->strComment.swap(str);
346 break;
347 }
348
349 default:
350 /* Ignore unknown block */
351 LogRel(("QMTranslator::parseMessageRecord(): Unkown message block %x\n", type));
352 break;
353 }
354 }
355 }
356
357 /* Defines the so called `hashpjw' function by P.J. Weinberger
358 [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
359 1986, 1987 Bell Telephone Laboratories, Inc.] */
360 static uint32_t calculateHash(const char *pszStr1, const char *pszStr2 = 0)
361 {
362 uint32_t hash = 0, g;
363
364 for (const char *pszStr = pszStr1; pszStr != pszStr2; pszStr = pszStr2)
365 for (; pszStr && *pszStr; pszStr++)
366 {
367 hash = (hash << 4) + static_cast<uint8_t>(*pszStr);
368
369 if ((g = hash & 0xf0000000ul) != 0)
370 {
371 hash ^= g >> 24;
372 hash ^= g;
373 }
374 }
375
376 return (hash != 0 ? hash : 1);
377 }
378};
379
380/* Inteface functions implementation */
381QMTranslator::QMTranslator() : _impl(new QMTranslator_Impl) {}
382
383QMTranslator::~QMTranslator() { delete _impl; }
384
385const char *QMTranslator::translate(const char *pszContext, const char *pszSource, const char *pszDisamb) const throw()
386{
387 return _impl->translate(pszContext, pszSource, pszDisamb);
388}
389
390/* The function is noexcept for now but it may be changed
391 * to throw exceptions if required to catch them in another
392 * place. */
393int QMTranslator::load(const char *pszFilename) throw()
394{
395 /* To free safely the file in case of exception */
396 struct FileLoader
397 {
398 uint8_t *data;
399 size_t cbSize;
400 int rc;
401 FileLoader(const char *pszFname)
402 {
403 rc = RTFileReadAll(pszFname, (void**) &data, &cbSize);
404 }
405
406 ~FileLoader()
407 {
408 if (isSuccess())
409 RTFileReadAllFree(data, cbSize);
410 }
411 bool isSuccess() { return RT_SUCCESS(rc); }
412 };
413
414 try
415 {
416 FileLoader loader(pszFilename);
417 if (loader.isSuccess())
418 {
419 QMBytesStream stream(loader.data, loader.cbSize);
420 _impl->load(stream);
421 }
422 return loader.rc;
423 }
424 catch(std::exception &e)
425 {
426 LogRel(("QMTranslator::load() failed to load file '%s', reason: %s\n", pszFilename, e.what()));
427 return VERR_INTERNAL_ERROR;
428 }
429 catch(...)
430 {
431 LogRel(("QMTranslator::load() failed to load file '%s'\n", pszFilename));
432 return VERR_GENERAL_FAILURE;
433 }
434}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette