QMTranslatorImpl.cpp revision cb702d6d593134e47854c00a7fa1156fe48a14cc
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync/* $Id$ */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync/** @file
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * VirtualBox API translation handling class
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync/*
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * Copyright (C) 2014 Oracle Corporation
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync *
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * available from http://www.virtualbox.org. This file is free software;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * you can redistribute it and/or modify it under the terms of the GNU
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * General Public License (GPL) as published by the Free Software
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync#include <vector>
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync#include <set>
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync#include <algorithm>
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync#include <iterator>
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync#include <iprt/file.h>
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync#include <iprt/asm.h>
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync#include <VBox/com/string.h>
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync#include <VBox/log.h>
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync#include <QMTranslator.h>
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync/* QM File Magic Number */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsyncstatic const size_t MagicLength = 16;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsyncstatic const uint8_t Magic[MagicLength] = {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync 0x3c, 0xb8, 0x64, 0x18, 0xca, 0xef, 0x9c, 0x95,
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync 0xcd, 0x21, 0x1c, 0xbf, 0x60, 0xa1, 0xbd, 0xdd
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync};
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync/* Used internally */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsyncclass QMException : public std::exception
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync{
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync const char *m_str;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsyncpublic:
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync QMException(const char *str) : m_str(str) {}
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync virtual const char *what() const throw() { return m_str; }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync};
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync/* Bytes stream. Used by the parser to iterate through the data */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsyncclass QMBytesStream
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync{
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync size_t m_cbSize;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync const uint8_t * const m_dataStart;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync const uint8_t *m_iter;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync const uint8_t *m_end;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Function stub for transform method */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync static uint16_t func_BE2H_U16(uint16_t value)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync return RT_BE2H_U16(value);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
cb702d6d593134e47854c00a7fa1156fe48a14ccvboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsyncpublic:
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync QMBytesStream(const uint8_t *const dataStart, size_t cbSize) :
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync m_cbSize(dataStart ? cbSize : 0),
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync m_dataStart(dataStart),
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync m_iter(dataStart)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync setEnd();
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Sets end pointer
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * Used in message reader to detect the end of message block */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync inline void setEnd(size_t pos = 0)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync m_end = m_dataStart + (pos && pos < m_cbSize ? pos : m_cbSize);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync inline uint8_t read8()
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync checkSize(1);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync return *m_iter++;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync inline uint32_t read32()
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync checkSize(4);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync uint32_t result = *reinterpret_cast<const uint32_t *>(m_iter);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync m_iter += 4;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync return RT_BE2H_U32(result);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Reads string in UTF16 and converts it into a UTF8 string */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync inline com::Utf8Str readUtf16String()
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync uint32_t size = read32();
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync checkSize(size);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync if (size & 1) throw QMException("Incorrect string size");
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync std::vector<uint16_t> wstr;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync wstr.reserve(size / 2);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* We cannot convert to host endianess without copying the data
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * since the file might be mapped to the memory and any memory
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * change will lead to the change of the file. */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync std::transform(reinterpret_cast<const uint16_t *>(m_iter),
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync reinterpret_cast<const uint16_t *>(m_iter + size),
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync std::back_inserter(wstr),
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync func_BE2H_U16);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync m_iter += size;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync return com::Utf8Str((CBSTR) &wstr.front(), wstr.size());
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Reads string in one-byte encoding
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * The string is assumed to be in ISO-8859-1 encoding */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync inline com::Utf8Str readString()
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync uint32_t size = read32();
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync checkSize(size);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync com::Utf8Str result(reinterpret_cast<const char *>(m_iter), size);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync m_iter += size;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync return result;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Checks the magic number
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * Should be called when in the beginning of the data */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync inline void checkMagic()
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync checkSize(MagicLength);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync if (memcmp(&(*m_iter), Magic, MagicLength)) throw QMException("Wrong magic number");
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync m_iter += MagicLength;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Has we reached the end pointer? */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync inline bool hasFinished() { return m_iter == m_end; }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Returns current stream position */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync inline size_t tellPos() { return m_iter - m_dataStart; }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Moves current pointer to a desired position */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync inline void seek(int pos) { m_iter += pos; }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Checks whether stream has enough data to read size bytes */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync inline void checkSize(int size)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync if (m_end - m_iter < size) throw QMException("Incorrect item size");
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync};
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync/* Internal QMTranslator implementation */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsyncclass QMTranslator_Impl
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync{
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync struct QMMessage
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Everything is in UTF-8 */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync com::Utf8Str strContext;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync com::Utf8Str strTranslation;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync com::Utf8Str strComment;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync com::Utf8Str strSource;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync uint32_t hash;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync QMMessage() : hash(0) {}
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync };
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync struct HashOffset
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync uint32_t hash;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync uint32_t offset;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync HashOffset(uint32_t _hash = 0, uint32_t _offs = 0) : hash(_hash), offset(_offs) {}
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync bool operator<(const HashOffset &obj) const
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync return (hash != obj.hash ? hash < obj.hash : offset < obj.offset);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync };
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync typedef std::set<HashOffset> QMHashSet;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync typedef QMHashSet::const_iterator QMHashSetConstIter;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync typedef std::vector<QMMessage> QMMessageArray;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync QMHashSet m_hashSet;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync QMMessageArray m_messageArray;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsyncpublic:
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync QMTranslator_Impl() {}
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync const char *translate(const char *pszContext,
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync const char *pszSource,
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync const char *pszDisamb) const
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync QMHashSetConstIter iter;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync QMHashSetConstIter lowerIter, upperIter;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync do {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync uint32_t hash = calculateHash(pszSource, pszDisamb);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync lowerIter = m_hashSet.lower_bound(HashOffset(hash, 0));
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync upperIter = m_hashSet.upper_bound(HashOffset(hash, ~0));
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync for (iter = lowerIter; iter != upperIter; ++iter)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync const QMMessage &message = m_messageArray[iter->offset];
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync if ((!pszContext || !*pszContext || message.strContext == pszContext) &&
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync message.strSource == pszSource &&
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync ((pszDisamb && !*pszDisamb) || message.strComment == pszDisamb))
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync break;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Try without disambiguating comment if it isn't empty */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync if (pszDisamb)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync if (!*pszDisamb) pszDisamb = 0;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync else pszDisamb = "";
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync } while (iter == upperIter && pszDisamb);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync return (iter != upperIter ? m_messageArray[iter->offset].strTranslation.c_str() : "");
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync void load(QMBytesStream &stream)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Load into local variables. If we failed during the load,
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * it would allow us to keep the object in a valid (previous) state. */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync QMHashSet hashSet;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync QMMessageArray messageArray;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync stream.checkMagic();
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync while (!stream.hasFinished())
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync uint32_t sectionCode = stream.read8();
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync uint32_t sLen = stream.read32();
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Hashes and Context sections are ignored. They contain hash tables
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * to speed-up search which is not useful since we recalculate all hashes
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * and don't perform context search by hash */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync switch (sectionCode)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync case Messages:
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync parseMessages(stream, &hashSet, &messageArray, sLen);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync break;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync case Hashes:
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Only get size information to speed-up vector filling
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * if Hashes section goes in the file before Message section */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync m_messageArray.reserve(sLen >> 3);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* NB! NO BREAK HERE */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync case Context:
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync stream.seek(sLen);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync break;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync default:
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync throw QMException("Unkown section");
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Store the data into member variables.
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * The following functions never generate exceptions */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync m_hashSet.swap(hashSet);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync m_messageArray.swap(messageArray);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsyncprivate:
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Some QM stuff */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync enum SectionType
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync Hashes = 0x42,
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync Messages = 0x69,
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync Contexts = 0x2f
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync };
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync enum MessageType
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync End = 1,
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync SourceText16 = 2,
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync Translation = 3,
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync Context16 = 4,
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync Hash = 5,
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync SourceText = 6,
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync Context = 7,
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync Comment = 8
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync };
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Read messages from the stream. */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync static void parseMessages(QMBytesStream &stream, QMHashSet * const hashSet, QMMessageArray * const messageArray, size_t cbSize)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync stream.setEnd(stream.tellPos() + cbSize);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync uint32_t cMessage = 0;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync while (!stream.hasFinished())
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync QMMessage message;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync HashOffset hashOffs;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync parseMessageRecord(stream, &message);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync if (!message.hash)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync message.hash = calculateHash(message.strSource.c_str(), message.strComment.c_str());
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync hashOffs.hash = message.hash;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync hashOffs.offset = cMessage++;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync hashSet->insert(hashOffs);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync messageArray->push_back(message);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync stream.setEnd();
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Parse one message from the stream */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync static void parseMessageRecord(QMBytesStream &stream, QMMessage * const message)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync while(!stream.hasFinished())
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync uint8_t type = stream.read8();
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync switch(type)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync case End:
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync return;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Ignored as obsolete */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync case Context16:
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync case SourceText16:
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync stream.seek(stream.read32());
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync break;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync case Translation:
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync com::Utf8Str str = stream.readUtf16String();
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync message->strTranslation.swap(str);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync break;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync case Hash:
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync message->hash = stream.read32();
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync break;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync case SourceText:
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync com::Utf8Str str = stream.readString();
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync message->strSource.swap(str);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync break;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync case Context:
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync com::Utf8Str str = stream.readString();
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync message->strContext.swap(str);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync break;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync case Comment:
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync com::Utf8Str str = stream.readString();
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync message->strComment.swap(str);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync break;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync default:
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Ignore unknown block */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync LogRel(("QMTranslator::parseMessageRecord(): Unkown message block %x\n", type));
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync break;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Defines the so called `hashpjw' function by P.J. Weinberger
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync 1986, 1987 Bell Telephone Laboratories, Inc.] */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync static uint32_t calculateHash(const char *pszStr1, const char *pszStr2 = 0)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync uint32_t hash = 0, g;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync for (const char *pszStr = pszStr1; pszStr != pszStr2; pszStr = pszStr2)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync for (; pszStr && *pszStr; pszStr++)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync hash = (hash << 4) + static_cast<uint8_t>(*pszStr);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync if ((g = hash & 0xf0000000ul) != 0)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync hash ^= g >> 24;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync hash ^= g;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync return (hash != 0 ? hash : 1);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync};
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync/* Inteface functions implementation */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsyncQMTranslator::QMTranslator() : _impl(new QMTranslator_Impl) {}
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsyncQMTranslator::~QMTranslator() { delete _impl; }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsyncconst char *QMTranslator::translate(const char *pszContext, const char *pszSource, const char *pszDisamb) const throw()
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync{
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync return _impl->translate(pszContext, pszSource, pszDisamb);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync}
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync/* The function is noexcept for now but it may be changed
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * to throw exceptions if required to catch them in another
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * place. */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsyncint QMTranslator::load(const char *pszFilename) throw()
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync{
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* To free safely the file in case of exception */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync struct FileLoader
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync uint8_t *data;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync size_t cbSize;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync int rc;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync FileLoader(const char *pszFname)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync rc = RTFileReadAll(pszFname, (void**) &data, &cbSize);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync ~FileLoader()
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync if (isSuccess())
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync RTFileReadAllFree(data, cbSize);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync bool isSuccess() { return RT_SUCCESS(rc); }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync };
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync
cb702d6d593134e47854c00a7fa1156fe48a14ccvboxsync try
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync FileLoader loader(pszFilename);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync if (loader.isSuccess())
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync QMBytesStream stream(loader.data, loader.cbSize);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync _impl->load(stream);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync return loader.rc;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync catch(std::exception &e)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync LogRel(("QMTranslator::load() failed to load file '%s', reason: %s\n", pszFilename, e.what()));
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync return VERR_INTERNAL_ERROR;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync catch(...)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync {
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync LogRel(("QMTranslator::load() failed to load file '%s'\n", pszFilename));
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync return VERR_GENERAL_FAILURE;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync}