diff options
Diffstat (limited to 'registry/source')
-rw-r--r-- | registry/source/keyimpl.cxx | 983 | ||||
-rw-r--r-- | registry/source/keyimpl.hxx | 140 | ||||
-rw-r--r-- | registry/source/reflcnst.hxx | 229 | ||||
-rw-r--r-- | registry/source/reflread.cxx | 1760 | ||||
-rw-r--r-- | registry/source/reflread.hxx | 114 | ||||
-rw-r--r-- | registry/source/reflwrit.cxx | 1336 | ||||
-rw-r--r-- | registry/source/reflwrit.hxx | 103 | ||||
-rw-r--r-- | registry/source/regimpl.cxx | 1577 | ||||
-rw-r--r-- | registry/source/regimpl.hxx | 156 | ||||
-rw-r--r-- | registry/source/registry.cxx | 393 | ||||
-rw-r--r-- | registry/source/regkey.cxx | 694 | ||||
-rw-r--r-- | registry/source/regkey.hxx | 69 |
12 files changed, 7554 insertions, 0 deletions
diff --git a/registry/source/keyimpl.cxx b/registry/source/keyimpl.cxx new file mode 100644 index 000000000..abf8e0d1e --- /dev/null +++ b/registry/source/keyimpl.cxx @@ -0,0 +1,983 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include <string.h> +#include <string_view> + +#include "keyimpl.hxx" + +#include "reflcnst.hxx" +#include <rtl/alloc.h> +#include <rtl/ustrbuf.hxx> +#include <osl/diagnose.h> +#include <sal/log.hxx> +#include <memory> + +using namespace store; + +namespace { char const VALUE_PREFIX[] = "$VL_"; } + + +// ORegKey() + +ORegKey::ORegKey(const OUString& keyName, ORegistry* pReg) + : m_refCount(1) + , m_name(keyName) + , m_bDeleted(false) + , m_bModified(false) + , m_pRegistry(pReg) +{ +} + + +// ~ORegKey() + +ORegKey::~ORegKey() +{ + SAL_WARN_IF(m_refCount != 0, "registry", "registry::ORegKey::dtor(): refcount not zero."); +} + + +// releaseKey + +RegError ORegKey::releaseKey(RegKeyHandle hKey) +{ + return m_pRegistry->releaseKey(hKey); +} + + +// createKey + +RegError ORegKey::createKey(std::u16string_view keyName, RegKeyHandle* phNewKey) +{ + return m_pRegistry->createKey(this, keyName, phNewKey); +} + + +// openKey + +RegError ORegKey::openKey(std::u16string_view keyName, RegKeyHandle* phOpenKey) +{ + return m_pRegistry->openKey(this, keyName, phOpenKey); +} + + +// openSubKeys + +RegError ORegKey::openSubKeys(std::u16string_view keyName, RegKeyHandle** phOpenSubKeys, sal_uInt32* pnSubKeys) +{ + RegError _ret = RegError::NO_ERROR; + + *phOpenSubKeys = nullptr; + *pnSubKeys = 0; + + ORegKey* pKey = this; + if ( !keyName.empty() ) + { + _ret = openKey(keyName, reinterpret_cast<RegKeyHandle*>(&pKey)); + if (_ret != RegError::NO_ERROR) + return _ret; + } + + sal_uInt32 nSubKeys = pKey->countSubKeys(); + *pnSubKeys = nSubKeys; + + ORegKey** pSubKeys; + pSubKeys = static_cast<ORegKey**>(rtl_allocateZeroMemory(nSubKeys * sizeof(ORegKey*))); + + OStoreDirectory::iterator iter; + OStoreDirectory rStoreDir(pKey->getStoreDir()); + storeError _err = rStoreDir.first(iter); + + nSubKeys = 0; + while ( _err == store_E_None ) + { + if ( iter.m_nAttrib & STORE_ATTRIB_ISDIR ) + { + OUString const sSubKeyName(iter.m_pszName, iter.m_nLength); + + ORegKey* pOpenSubKey = nullptr; + _ret = pKey->openKey(sSubKeyName, reinterpret_cast<RegKeyHandle*>(&pOpenSubKey)); + if (_ret != RegError::NO_ERROR) + { + *phOpenSubKeys = nullptr; + *pnSubKeys = 0; + std::free(pSubKeys); // @@@ leaking 'pSubKeys[0...nSubkeys-1]' + return _ret; // @@@ leaking 'pKey' + } + + pSubKeys[nSubKeys] = pOpenSubKey; + + nSubKeys++; + } + + _err = rStoreDir.next(iter); + } + + *phOpenSubKeys = reinterpret_cast<RegKeyHandle*>(pSubKeys); + if (!keyName.empty()) + { + (void) releaseKey(pKey); + } + return RegError::NO_ERROR; +} + + +// getKeyNames + +RegError ORegKey::getKeyNames(std::u16string_view keyName, + rtl_uString*** pSubKeyNames, + sal_uInt32* pnSubKeys) +{ + *pSubKeyNames = nullptr; + *pnSubKeys = 0; + + ORegKey* pKey = this; + if (!keyName.empty()) + { + RegError _ret = openKey(keyName, reinterpret_cast<RegKeyHandle*>(&pKey)); + if (_ret != RegError::NO_ERROR) + return _ret; + } + + sal_uInt32 nSubKeys = pKey->countSubKeys(); + *pnSubKeys = nSubKeys; + + rtl_uString** pSubKeys + = static_cast<rtl_uString**>(rtl_allocateZeroMemory(nSubKeys * sizeof(rtl_uString*))); + + OStoreDirectory::iterator iter; + OStoreDirectory rStoreDir(pKey->getStoreDir()); + storeError _err = rStoreDir.first(iter); + + nSubKeys = 0; + + while ( _err == store_E_None ) + { + if ( iter.m_nAttrib & STORE_ATTRIB_ISDIR) + { + OUString const sSubKeyName(iter.m_pszName, iter.m_nLength); + + OUString sFullKeyName(pKey->getName()); + if (sFullKeyName.getLength() > 1) + sFullKeyName += ORegistry::ROOT; + sFullKeyName += sSubKeyName; + + rtl_uString_newFromString(&pSubKeys[nSubKeys], sFullKeyName.pData); + + nSubKeys++; + } + + _err = rStoreDir.next(iter); + } + + *pSubKeyNames = pSubKeys; + if (!keyName.empty()) + { + releaseKey(pKey); + } + return RegError::NO_ERROR; +} + + +// closeKey + +RegError ORegKey::closeKey(RegKeyHandle hKey) +{ + return m_pRegistry->closeKey(hKey); +} + + +// deleteKey + +RegError ORegKey::deleteKey(std::u16string_view keyName) +{ + return m_pRegistry->deleteKey(this, keyName); +} + + +// getValueType + +RegError ORegKey::getValueInfo(std::u16string_view valueName, RegValueType* pValueType, sal_uInt32* pValueSize) const +{ + OStoreStream rValue; + std::unique_ptr<sal_uInt8[]> pBuffer; + storeAccessMode accessMode = storeAccessMode::ReadWrite; + + if (m_pRegistry->isReadOnly()) + { + accessMode = storeAccessMode::ReadOnly; + } + + OUString sImplValueName = OUString::Concat(VALUE_PREFIX) + valueName; + + REG_GUARD(m_pRegistry->m_mutex); + + if ( rValue.create(m_pRegistry->getStoreFile(), m_name + ORegistry::ROOT, sImplValueName, accessMode) ) + { + *pValueType = RegValueType::NOT_DEFINED; + *pValueSize = 0; + return RegError::VALUE_NOT_EXISTS; + } + + pBuffer.reset(new sal_uInt8[VALUE_HEADERSIZE]); + + sal_uInt32 readBytes; + if ( rValue.readAt(0, pBuffer.get(), VALUE_HEADERSIZE, readBytes) ) + { + return RegError::INVALID_VALUE; + } + if (readBytes != VALUE_HEADERSIZE) + { + return RegError::INVALID_VALUE; + } + + sal_uInt32 size; + sal_uInt8 type = pBuffer[0]; + readUINT32(pBuffer.get()+VALUE_TYPEOFFSET, size); + + *pValueType = static_cast<RegValueType>(type); + if (*pValueType > RegValueType::BINARY) + { + pBuffer.reset(new sal_uInt8[4]); + rValue.readAt(VALUE_HEADEROFFSET, pBuffer.get(), 4, readBytes); + + readUINT32(pBuffer.get(), size); + } + + *pValueSize = size; + + return RegError::NO_ERROR; +} + + +// setValue + +RegError ORegKey::setValue(std::u16string_view valueName, RegValueType vType, RegValue value, sal_uInt32 vSize) +{ + OStoreStream rValue; + std::unique_ptr<sal_uInt8[]> pBuffer; + + if (m_pRegistry->isReadOnly()) + { + return RegError::REGISTRY_READONLY; + } + + if (vType > RegValueType::BINARY) + { + return RegError::INVALID_VALUE; + } + + OUString sImplValueName = OUString::Concat(VALUE_PREFIX) + valueName; + + REG_GUARD(m_pRegistry->m_mutex); + + if ( rValue.create(getStoreFile(), m_name + ORegistry::ROOT , sImplValueName, storeAccessMode::Create) ) + { + return RegError::SET_VALUE_FAILED; + } + + sal_uInt32 size = vSize; + + sal_uInt8 type = static_cast<sal_uInt8>(vType); + pBuffer.reset(new sal_uInt8[VALUE_HEADERSIZE + size]); + memcpy(pBuffer.get(), &type, 1); + + writeUINT32(pBuffer.get()+VALUE_TYPEOFFSET, size); + + switch (vType) + { + case RegValueType::NOT_DEFINED: + memcpy(pBuffer.get()+VALUE_HEADEROFFSET, value, size); + break; + case RegValueType::LONG: + writeINT32(pBuffer.get()+VALUE_HEADEROFFSET, *static_cast<sal_Int32*>(value)); + break; + case RegValueType::STRING: + writeUtf8(pBuffer.get()+VALUE_HEADEROFFSET, static_cast<const char*>(value)); + break; + case RegValueType::UNICODE: + writeString(pBuffer.get()+VALUE_HEADEROFFSET, static_cast<const sal_Unicode*>(value)); + break; + case RegValueType::BINARY: + memcpy(pBuffer.get()+VALUE_HEADEROFFSET, value, size); + break; + default: + OSL_ASSERT(false); + break; + } + + sal_uInt32 writenBytes; + if ( rValue.writeAt(0, pBuffer.get(), VALUE_HEADERSIZE+size, writenBytes) ) + { + return RegError::SET_VALUE_FAILED; + } + if (writenBytes != (VALUE_HEADERSIZE+size)) + { + return RegError::SET_VALUE_FAILED; + } + setModified(); + + return RegError::NO_ERROR; +} + + +// setLongListValue + +RegError ORegKey::setLongListValue(std::u16string_view valueName, sal_Int32 const * pValueList, sal_uInt32 len) +{ + OStoreStream rValue; + std::unique_ptr<sal_uInt8[]> pBuffer; + + if (m_pRegistry->isReadOnly()) + { + return RegError::REGISTRY_READONLY; + } + + OUString sImplValueName = OUString::Concat(VALUE_PREFIX) + valueName; + + REG_GUARD(m_pRegistry->m_mutex); + + if (rValue.create(getStoreFile(), m_name + ORegistry::ROOT, sImplValueName, storeAccessMode::Create) ) + { + return RegError::SET_VALUE_FAILED; + } + + sal_uInt32 size = 4; // 4 bytes (sal_uInt32) for the length + + size += len * 4; + + sal_uInt8 type = sal_uInt8(RegValueType::LONGLIST); + pBuffer.reset(new sal_uInt8[VALUE_HEADERSIZE + size]); + memcpy(pBuffer.get(), &type, 1); + + writeUINT32(pBuffer.get()+VALUE_TYPEOFFSET, size); + writeUINT32(pBuffer.get()+VALUE_HEADEROFFSET, len); + + sal_uInt32 offset = 4; // initial 4 bytes for the size of the array + + for (sal_uInt32 i=0; i < len; i++) + { + writeINT32(pBuffer.get()+VALUE_HEADEROFFSET+offset, pValueList[i]); + offset += 4; + } + + sal_uInt32 writenBytes; + if ( rValue.writeAt(0, pBuffer.get(), VALUE_HEADERSIZE+size, writenBytes) ) + { + return RegError::SET_VALUE_FAILED; + } + if (writenBytes != (VALUE_HEADEROFFSET+size)) + { + return RegError::SET_VALUE_FAILED; + } + setModified(); + + return RegError::NO_ERROR; +} + + +// setStringListValue + +RegError ORegKey::setStringListValue( + std::u16string_view valueName, char** pValueList, sal_uInt32 len) +{ + OStoreStream rValue; + std::unique_ptr<sal_uInt8[]> pBuffer; + + if (m_pRegistry->isReadOnly()) + { + return RegError::REGISTRY_READONLY; + } + + OUString sImplValueName = OUString::Concat(VALUE_PREFIX) + valueName; + + REG_GUARD(m_pRegistry->m_mutex); + + if (rValue.create(getStoreFile(), m_name + ORegistry::ROOT, sImplValueName, storeAccessMode::Create) ) + { + return RegError::SET_VALUE_FAILED; + } + + sal_uInt32 size = 4; // 4 bytes (sal_uInt32) for the length + + sal_uInt32 i; + for (i=0; i < len; i++) + { + size += 4 + strlen(pValueList[i]) + 1; + } + + sal_uInt8 type = sal_uInt8(RegValueType::STRINGLIST); + pBuffer.reset(new sal_uInt8[VALUE_HEADERSIZE + size]); + memcpy(pBuffer.get(), &type, 1); + + writeUINT32(pBuffer.get()+VALUE_TYPEOFFSET, size); + writeUINT32(pBuffer.get()+VALUE_HEADEROFFSET, len); + + sal_uInt32 offset = 4; // initial 4 bytes for the size of the array; + sal_uInt32 sLen = 0; + + for (i=0; i < len; i++) + { + sLen = strlen(pValueList[i]) + 1; + writeUINT32(pBuffer.get()+VALUE_HEADEROFFSET+offset, sLen); + + offset += 4; + writeUtf8(pBuffer.get()+VALUE_HEADEROFFSET+offset, pValueList[i]); + offset += sLen; + } + + sal_uInt32 writenBytes; + if ( rValue.writeAt(0, pBuffer.get(), VALUE_HEADERSIZE+size, writenBytes) ) + { + return RegError::SET_VALUE_FAILED; + } + if (writenBytes != (VALUE_HEADERSIZE+size)) + { + return RegError::SET_VALUE_FAILED; + } + setModified(); + + return RegError::NO_ERROR; +} + + +// setUnicodeListValue + +RegError ORegKey::setUnicodeListValue(std::u16string_view valueName, sal_Unicode** pValueList, sal_uInt32 len) +{ + OStoreStream rValue; + std::unique_ptr<sal_uInt8[]> pBuffer; + + if (m_pRegistry->isReadOnly()) + { + return RegError::REGISTRY_READONLY; + } + + OUString sImplValueName = OUString::Concat(VALUE_PREFIX) + valueName; + + REG_GUARD(m_pRegistry->m_mutex); + + if (rValue.create(getStoreFile(), m_name + ORegistry::ROOT, sImplValueName, storeAccessMode::Create) ) + { + return RegError::SET_VALUE_FAILED; + } + + sal_uInt32 size = 4; // 4 bytes (sal_uInt32) for the length + + sal_uInt32 i; + for (i=0; i < len; i++) + { + size += 4 + ((rtl_ustr_getLength(pValueList[i]) +1) * 2); + } + + sal_uInt8 type = sal_uInt8(RegValueType::UNICODELIST); + pBuffer.reset(new sal_uInt8[VALUE_HEADERSIZE + size]); + memcpy(pBuffer.get(), &type, 1); + + writeUINT32(pBuffer.get()+VALUE_TYPEOFFSET, size); + writeUINT32(pBuffer.get()+VALUE_HEADEROFFSET, len); + + sal_uInt32 offset = 4; // initial 4 bytes for the size of the array; + sal_uInt32 sLen = 0; + + for (i=0; i < len; i++) + { + sLen = (rtl_ustr_getLength(pValueList[i]) + 1) * 2; + writeUINT32(pBuffer.get()+VALUE_HEADEROFFSET+offset, sLen); + + offset += 4; + writeString(pBuffer.get()+VALUE_HEADEROFFSET+offset, pValueList[i]); + offset += sLen; + } + + sal_uInt32 writenBytes; + if ( rValue.writeAt(0, pBuffer.get(), VALUE_HEADERSIZE+size, writenBytes) ) + { + return RegError::SET_VALUE_FAILED; + } + if (writenBytes != (VALUE_HEADERSIZE+size)) + { + return RegError::SET_VALUE_FAILED; + } + setModified(); + + return RegError::NO_ERROR; +} + + +// getValue + +RegError ORegKey::getValue(std::u16string_view valueName, RegValue value) const +{ + OStoreStream rValue; + std::unique_ptr<sal_uInt8[]> pBuffer; + RegValueType valueType; + sal_uInt32 valueSize; + storeAccessMode accessMode = storeAccessMode::ReadWrite; + + if (m_pRegistry->isReadOnly()) + { + accessMode = storeAccessMode::ReadOnly; + } + + OUString sImplValueName = OUString::Concat(VALUE_PREFIX) + valueName; + + REG_GUARD(m_pRegistry->m_mutex); + + if (rValue.create(getStoreFile(), m_name + ORegistry::ROOT, sImplValueName, accessMode) ) + { + return RegError::VALUE_NOT_EXISTS; + } + + pBuffer.reset(new sal_uInt8[VALUE_HEADERSIZE]); + + sal_uInt32 readBytes; + if ( rValue.readAt(0, pBuffer.get(), VALUE_HEADERSIZE, readBytes) ) + { + return RegError::INVALID_VALUE; + } + if (readBytes != VALUE_HEADERSIZE) + { + return RegError::INVALID_VALUE; + } + + sal_uInt8 type = pBuffer[0]; + valueType = static_cast<RegValueType>(type); + readUINT32(pBuffer.get()+VALUE_TYPEOFFSET, valueSize); + + if (valueType > RegValueType::BINARY) + { + return RegError::INVALID_VALUE; + } + + pBuffer.reset(new sal_uInt8[valueSize]); + + if ( rValue.readAt(VALUE_HEADEROFFSET, pBuffer.get(), valueSize, readBytes) ) + { + return RegError::INVALID_VALUE; + } + if (readBytes != valueSize) + { + return RegError::INVALID_VALUE; + } + + switch (valueType) + { + case RegValueType::LONG: + readINT32(pBuffer.get(), *static_cast<sal_Int32*>(value)); + break; + case RegValueType::STRING: + readUtf8(pBuffer.get(), static_cast<char*>(value), valueSize); + break; + case RegValueType::UNICODE: + readString(pBuffer.get(), static_cast<sal_Unicode*>(value), valueSize); + break; + case RegValueType::BINARY: + memcpy(value, pBuffer.get(), valueSize); + break; + default: + memcpy(value, pBuffer.get(), valueSize); + break; + } + + return RegError::NO_ERROR; +} + + +// getLongListValue + +RegError ORegKey::getLongListValue(std::u16string_view valueName, sal_Int32** pValueList, sal_uInt32* pLen) const +{ + OStoreStream rValue; + std::unique_ptr<sal_uInt8[]> pBuffer; + RegValueType valueType; + sal_uInt32 valueSize; + storeAccessMode accessMode = storeAccessMode::ReadWrite; + + if (m_pRegistry->isReadOnly()) + { + accessMode = storeAccessMode::ReadOnly; + } + + OUString sImplValueName = OUString::Concat(VALUE_PREFIX) + valueName; + + REG_GUARD(m_pRegistry->m_mutex); + + if (rValue.create(getStoreFile(), m_name + ORegistry::ROOT, sImplValueName, accessMode) ) + { + pValueList = nullptr; + *pLen = 0; + return RegError::VALUE_NOT_EXISTS; + } + + pBuffer.reset(new sal_uInt8[VALUE_HEADERSIZE]); + + sal_uInt32 readBytes; + if ( rValue.readAt(0, pBuffer.get(), VALUE_HEADERSIZE, readBytes) ) + { + pValueList = nullptr; + *pLen = 0; + return RegError::INVALID_VALUE; + } + if (readBytes != VALUE_HEADERSIZE) + { + pValueList = nullptr; + *pLen = 0; + return RegError::INVALID_VALUE; + } + + sal_uInt8 type = pBuffer[0]; + valueType = static_cast<RegValueType>(type); + + if (valueType != RegValueType::LONGLIST) + { + pValueList = nullptr; + *pLen = 0; + return RegError::INVALID_VALUE; + } + + readUINT32(pBuffer.get()+VALUE_TYPEOFFSET, valueSize); + + /* check for 'reasonable' value */ + /* surely 10 millions entry in a registry list should be enough */ + if(valueSize > 40000000) + { + pValueList = nullptr; + *pLen = 0; + return RegError::INVALID_VALUE; + } + pBuffer.reset(new sal_uInt8[valueSize]); + + if ( rValue.readAt(VALUE_HEADEROFFSET, pBuffer.get(), valueSize, readBytes) ) + { + pValueList = nullptr; + *pLen = 0; + return RegError::INVALID_VALUE; + } + if (readBytes != valueSize) + { + pValueList = nullptr; + *pLen = 0; + return RegError::INVALID_VALUE; + } + + sal_uInt32 len = 0; + readUINT32(pBuffer.get(), len); + + /* make sure the declared size of the array is consistent with the amount of data we have read */ + if(len > (valueSize - 4) / 4) + { + pValueList = nullptr; + *pLen = 0; + return RegError::INVALID_VALUE; + } + *pLen = len; + sal_Int32* pVList = static_cast<sal_Int32*>(rtl_allocateZeroMemory(len * sizeof(sal_Int32))); + + sal_uInt32 offset = 4; // initial 4 bytes for the size of the array; + + for (sal_uInt32 i = 0; i < len; i++) + { + readINT32(pBuffer.get()+offset, pVList[i]); + offset += 4; + } + + *pValueList = pVList; + return RegError::NO_ERROR; +} + + +// getStringListValue + +RegError ORegKey::getStringListValue(std::u16string_view valueName, char*** pValueList, sal_uInt32* pLen) const +{ + OStoreStream rValue; + std::unique_ptr<sal_uInt8[]> pBuffer; + RegValueType valueType; + sal_uInt32 valueSize; + storeAccessMode accessMode = storeAccessMode::ReadWrite; + + if (m_pRegistry->isReadOnly()) + { + accessMode = storeAccessMode::ReadOnly; + } + + OUString sImplValueName = OUString::Concat(VALUE_PREFIX) + valueName; + + REG_GUARD(m_pRegistry->m_mutex); + + if ( rValue.create(getStoreFile(), m_name + ORegistry::ROOT, sImplValueName, accessMode) ) + { + pValueList = nullptr; + *pLen = 0; + return RegError::VALUE_NOT_EXISTS; + } + + pBuffer.reset(new sal_uInt8[VALUE_HEADERSIZE]); + + sal_uInt32 readBytes; + if ( rValue.readAt(0, pBuffer.get(), VALUE_HEADERSIZE, readBytes) ) + { + pValueList = nullptr; + *pLen = 0; + return RegError::INVALID_VALUE; + } + if (readBytes != VALUE_HEADERSIZE) + { + pValueList = nullptr; + *pLen = 0; + return RegError::INVALID_VALUE; + } + + sal_uInt8 type = pBuffer[0]; + valueType = static_cast<RegValueType>(type); + + if (valueType != RegValueType::STRINGLIST) + { + pValueList = nullptr; + *pLen = 0; + return RegError::INVALID_VALUE; + } + + readUINT32(pBuffer.get()+VALUE_TYPEOFFSET, valueSize); + + pBuffer.reset(new sal_uInt8[valueSize]); + + if ( rValue.readAt(VALUE_HEADEROFFSET, pBuffer.get(), valueSize, readBytes) ) + { + pValueList = nullptr; + *pLen = 0; + return RegError::INVALID_VALUE; + } + if (readBytes != valueSize) + { + pValueList = nullptr; + *pLen = 0; + return RegError::INVALID_VALUE; + } + + sal_uInt32 len = 0; + readUINT32(pBuffer.get(), len); + + *pLen = len; + char** pVList = static_cast<char**>(rtl_allocateZeroMemory(len * sizeof(char*))); + + sal_uInt32 offset = 4; // initial 4 bytes for the size of the array; + sal_uInt32 sLen = 0; + + char *pValue; + for (sal_uInt32 i=0; i < len; i++) + { + readUINT32(pBuffer.get()+offset, sLen); + + offset += 4; + + pValue = static_cast<char*>(std::malloc(sLen)); + readUtf8(pBuffer.get()+offset, pValue, sLen); + pVList[i] = pValue; + + offset += sLen; + } + + *pValueList = pVList; + return RegError::NO_ERROR; +} + + +// getUnicodeListValue + +RegError ORegKey::getUnicodeListValue(std::u16string_view valueName, sal_Unicode*** pValueList, sal_uInt32* pLen) const +{ + OStoreStream rValue; + std::unique_ptr<sal_uInt8[]> pBuffer; + RegValueType valueType; + sal_uInt32 valueSize; + storeAccessMode accessMode = storeAccessMode::ReadWrite; + + if (m_pRegistry->isReadOnly()) + { + accessMode = storeAccessMode::ReadOnly; + } + + OUString sImplValueName = OUString::Concat(VALUE_PREFIX) + valueName; + + REG_GUARD(m_pRegistry->m_mutex); + + if ( rValue.create(getStoreFile(), m_name + ORegistry::ROOT, sImplValueName, accessMode) ) + { + pValueList = nullptr; + *pLen = 0; + return RegError::VALUE_NOT_EXISTS; + } + + pBuffer.reset(new sal_uInt8[VALUE_HEADERSIZE]); + + sal_uInt32 readBytes; + if ( rValue.readAt(0, pBuffer.get(), VALUE_HEADERSIZE, readBytes) ) + { + pValueList = nullptr; + *pLen = 0; + return RegError::INVALID_VALUE; + } + if (readBytes != VALUE_HEADERSIZE) + { + pValueList = nullptr; + *pLen = 0; + return RegError::INVALID_VALUE; + } + + sal_uInt8 type = pBuffer[0]; + valueType = static_cast<RegValueType>(type); + + if (valueType != RegValueType::UNICODELIST) + { + pValueList = nullptr; + *pLen = 0; + return RegError::INVALID_VALUE; + } + + readUINT32(pBuffer.get()+VALUE_TYPEOFFSET, valueSize); + + pBuffer.reset(new sal_uInt8[valueSize]); + + if ( rValue.readAt(VALUE_HEADEROFFSET, pBuffer.get(), valueSize, readBytes) ) + { + pValueList = nullptr; + *pLen = 0; + return RegError::INVALID_VALUE; + } + if (readBytes != valueSize) + { + pValueList = nullptr; + *pLen = 0; + return RegError::INVALID_VALUE; + } + + sal_uInt32 len = 0; + readUINT32(pBuffer.get(), len); + + *pLen = len; + sal_Unicode** pVList = static_cast<sal_Unicode**>(rtl_allocateZeroMemory(len * sizeof(sal_Unicode*))); + + sal_uInt32 offset = 4; // initial 4 bytes for the size of the array; + sal_uInt32 sLen = 0; + + sal_Unicode *pValue; + for (sal_uInt32 i=0; i < len; i++) + { + readUINT32(pBuffer.get()+offset, sLen); + + offset += 4; + + pValue = static_cast<sal_Unicode*>(std::malloc((sLen / 2) * sizeof(sal_Unicode))); + readString(pBuffer.get()+offset, pValue, sLen); + pVList[i] = pValue; + + offset += sLen; + } + + *pValueList = pVList; + return RegError::NO_ERROR; +} + + +RegError ORegKey::getResolvedKeyName(std::u16string_view keyName, + OUString& resolvedName) const +{ + if (keyName.empty()) + return RegError::INVALID_KEYNAME; + + resolvedName = getFullPath(keyName); + return RegError::NO_ERROR; +} + + +// countSubKeys() + +sal_uInt32 ORegKey::countSubKeys() +{ + REG_GUARD(m_pRegistry->m_mutex); + + OStoreDirectory::iterator iter; + OStoreDirectory rStoreDir = getStoreDir(); + storeError _err = rStoreDir.first(iter); + sal_uInt32 count = 0; + + while ( _err == store_E_None ) + { + if ( iter.m_nAttrib & STORE_ATTRIB_ISDIR ) + { + count++; + } + + _err = rStoreDir.next(iter); + } + + return count; +} + +OStoreDirectory ORegKey::getStoreDir() const +{ + OStoreDirectory rStoreDir; + OUString fullPath; + OUString relativName; + storeAccessMode accessMode = storeAccessMode::ReadWrite; + + if ( m_name == ORegistry::ROOT ) + { + fullPath.clear(); + relativName.clear(); + } else + { + fullPath = m_name.copy(0, m_name.lastIndexOf('/') + 1); + relativName = m_name.copy(m_name.lastIndexOf('/') + 1); + } + + if (m_pRegistry->isReadOnly()) + { + accessMode = storeAccessMode::ReadOnly; + } + + rStoreDir.create(getStoreFile(), fullPath, relativName, accessMode); + + return rStoreDir; +} + +OUString ORegKey::getFullPath(std::u16string_view path) const { + OSL_ASSERT(!m_name.isEmpty() && !path.empty()); + OUStringBuffer b(32); + b.append(m_name); + if (!b.isEmpty() && b[b.getLength() - 1] == '/') { + if (path[0] == '/') { + b.append(path.substr(1)); + } else { + b.append(path); + } + } else { + if (path[0] != '/') { + b.append('/'); + } + b.append(path); + } + return b.makeStringAndClear(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/registry/source/keyimpl.hxx b/registry/source/keyimpl.hxx new file mode 100644 index 000000000..1958a7b62 --- /dev/null +++ b/registry/source/keyimpl.hxx @@ -0,0 +1,140 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_REGISTRY_SOURCE_KEYIMPL_HXX +#define INCLUDED_REGISTRY_SOURCE_KEYIMPL_HXX + +#include <sal/config.h> + +#include <string_view> + +#include "regimpl.hxx" +#include <rtl/ustring.hxx> + +class ORegKey +{ +public: + + ORegKey(const OUString& keyName, ORegistry* pReg); + ~ORegKey(); + + void acquire() + { ++m_refCount; } + + sal_uInt32 release() + { return --m_refCount; } + + RegError releaseKey(RegKeyHandle hKey); + + RegError createKey(std::u16string_view keyName, RegKeyHandle* phNewKey); + + RegError openKey(std::u16string_view keyName, RegKeyHandle* phOpenKey); + + RegError openSubKeys(std::u16string_view keyName, + RegKeyHandle** phOpenSubKeys, + sal_uInt32* pnSubKeys); + + RegError getKeyNames(std::u16string_view keyName, + rtl_uString*** pSubKeyNames, + sal_uInt32* pnSubKeys); + + RegError closeKey(RegKeyHandle hKey); + + RegError deleteKey(std::u16string_view keyName); + + RegError getValueInfo(std::u16string_view valueName, + RegValueType* pValueTye, + sal_uInt32* pValueSize) const; + + RegError setValue(std::u16string_view valueName, + RegValueType vType, + RegValue value, + sal_uInt32 vSize); + + RegError setLongListValue(std::u16string_view valueName, + sal_Int32 const * pValueList, + sal_uInt32 len); + + RegError setStringListValue(std::u16string_view valueName, + char** pValueList, + sal_uInt32 len); + + RegError setUnicodeListValue(std::u16string_view valueName, + sal_Unicode** pValueList, + sal_uInt32 len); + + RegError getValue(std::u16string_view valueName, RegValue value) const; + + RegError getLongListValue(std::u16string_view valueName, + sal_Int32** pValueList, + sal_uInt32* pLen) const; + + RegError getStringListValue(std::u16string_view valueName, + char*** pValueList, + sal_uInt32* pLen) const; + + RegError getUnicodeListValue(std::u16string_view valueName, + sal_Unicode*** pValueList, + sal_uInt32* pLen) const; + + RegError getResolvedKeyName(std::u16string_view keyName, + OUString& resolvedName) const; + + bool isDeleted() const + { return m_bDeleted; } + + void setDeleted (bool bKeyDeleted) + { m_bDeleted = bKeyDeleted; } + + bool isModified() const + { return m_bModified; } + + void setModified (bool bModified = true) + { m_bModified = bModified; } + + bool isReadOnly() const + { return m_pRegistry->isReadOnly(); } + + sal_uInt32 countSubKeys(); + + ORegistry* getRegistry() const + { return m_pRegistry; } + + const store::OStoreFile& getStoreFile() const + { return m_pRegistry->getStoreFile(); } + + store::OStoreDirectory getStoreDir() const; + + const OUString& getName() const + { return m_name; } + + OUString getFullPath(std::u16string_view path) const; + +private: + sal_uInt32 m_refCount; + OUString m_name; + bool m_bDeleted:1; + bool m_bModified:1; + ORegistry* m_pRegistry; +}; + +#endif + + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/registry/source/reflcnst.hxx b/registry/source/reflcnst.hxx new file mode 100644 index 000000000..2254b94b5 --- /dev/null +++ b/registry/source/reflcnst.hxx @@ -0,0 +1,229 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_REGISTRY_SOURCE_REFLCNST_HXX +#define INCLUDED_REGISTRY_SOURCE_REFLCNST_HXX + +#include <registry/types.hxx> + +#include <string.h> + +#define REGTYPE_IEEE_NATIVE 1 + +extern const sal_uInt32 magic; +extern const sal_uInt16 minorVersion; +extern const sal_uInt16 majorVersion; + +#define OFFSET_MAGIC 0 +#define OFFSET_SIZE static_cast<sal_uInt32>(OFFSET_MAGIC + sizeof(magic)) +#define OFFSET_MINOR_VERSION static_cast<sal_uInt32>(OFFSET_SIZE + sizeof(sal_uInt32)) +#define OFFSET_MAJOR_VERSION static_cast<sal_uInt32>(OFFSET_MINOR_VERSION + sizeof(minorVersion)) +#define OFFSET_N_ENTRIES static_cast<sal_uInt32>(OFFSET_MAJOR_VERSION + sizeof(majorVersion)) +#define OFFSET_TYPE_SOURCE static_cast<sal_uInt32>(OFFSET_N_ENTRIES + sizeof(sal_uInt16)) +#define OFFSET_TYPE_CLASS static_cast<sal_uInt32>(OFFSET_TYPE_SOURCE + sizeof(sal_uInt16)) +#define OFFSET_THIS_TYPE static_cast<sal_uInt32>(OFFSET_TYPE_CLASS + sizeof(sal_uInt16)) +#define OFFSET_UIK static_cast<sal_uInt32>(OFFSET_THIS_TYPE + sizeof(sal_uInt16)) +#define OFFSET_DOKU static_cast<sal_uInt32>(OFFSET_UIK + sizeof(sal_uInt16)) +#define OFFSET_FILENAME static_cast<sal_uInt32>(OFFSET_DOKU + sizeof(sal_uInt16)) + +#define OFFSET_N_SUPERTYPES static_cast<sal_uInt32>(OFFSET_FILENAME + sizeof(sal_uInt16)) +#define OFFSET_SUPERTYPES static_cast<sal_uInt32>(OFFSET_N_SUPERTYPES + sizeof(sal_uInt16)) + +#define OFFSET_CP_SIZE static_cast<sal_uInt32>(OFFSET_SUPERTYPES + sizeof(sal_uInt16)) +#define OFFSET_CP static_cast<sal_uInt32>(OFFSET_CP_SIZE + sizeof(sal_uInt16)) + +#define CP_OFFSET_ENTRY_SIZE 0 +#define CP_OFFSET_ENTRY_TAG static_cast<sal_uInt32>(CP_OFFSET_ENTRY_SIZE + sizeof(sal_uInt32)) +#define CP_OFFSET_ENTRY_DATA static_cast<sal_uInt32>(CP_OFFSET_ENTRY_TAG + sizeof(sal_uInt16)) +#define CP_OFFSET_ENTRY_UIK1 static_cast<sal_uInt32>(CP_OFFSET_ENTRY_DATA) +#define CP_OFFSET_ENTRY_UIK2 static_cast<sal_uInt32>(CP_OFFSET_ENTRY_UIK1 + sizeof(sal_uInt32)) +#define CP_OFFSET_ENTRY_UIK3 static_cast<sal_uInt32>(CP_OFFSET_ENTRY_UIK2 + sizeof(sal_uInt16)) +#define CP_OFFSET_ENTRY_UIK4 static_cast<sal_uInt32>(CP_OFFSET_ENTRY_UIK3 + sizeof(sal_uInt16)) +#define CP_OFFSET_ENTRY_UIK5 static_cast<sal_uInt32>(CP_OFFSET_ENTRY_UIK4 + sizeof(sal_uInt32)) + +#define FIELD_OFFSET_ACCESS 0 +#define FIELD_OFFSET_NAME static_cast<sal_uInt32>(FIELD_OFFSET_ACCESS + sizeof(sal_uInt16)) +#define FIELD_OFFSET_TYPE static_cast<sal_uInt32>(FIELD_OFFSET_NAME + sizeof(sal_uInt16)) +#define FIELD_OFFSET_VALUE static_cast<sal_uInt32>(FIELD_OFFSET_TYPE + sizeof(sal_uInt16)) +#define FIELD_OFFSET_DOKU static_cast<sal_uInt32>(FIELD_OFFSET_VALUE + sizeof(sal_uInt16)) +#define FIELD_OFFSET_FILENAME static_cast<sal_uInt32>(FIELD_OFFSET_DOKU + sizeof(sal_uInt16)) + +#define PARAM_OFFSET_TYPE 0 +#define PARAM_OFFSET_MODE static_cast<sal_uInt32>(PARAM_OFFSET_TYPE + sizeof(sal_uInt16)) +#define PARAM_OFFSET_NAME static_cast<sal_uInt32>(PARAM_OFFSET_MODE + sizeof(sal_uInt16)) + +#define METHOD_OFFSET_SIZE 0 +#define METHOD_OFFSET_MODE static_cast<sal_uInt32>(METHOD_OFFSET_SIZE + sizeof(sal_uInt16)) +#define METHOD_OFFSET_NAME static_cast<sal_uInt32>(METHOD_OFFSET_MODE + sizeof(sal_uInt16)) +#define METHOD_OFFSET_RETURN static_cast<sal_uInt32>(METHOD_OFFSET_NAME + sizeof(sal_uInt16)) +#define METHOD_OFFSET_DOKU static_cast<sal_uInt32>(METHOD_OFFSET_RETURN + sizeof(sal_uInt16)) +#define METHOD_OFFSET_PARAM_COUNT static_cast<sal_uInt32>(METHOD_OFFSET_DOKU + sizeof(sal_uInt16)) + +#define REFERENCE_OFFSET_TYPE 0 +#define REFERENCE_OFFSET_NAME static_cast<sal_uInt32>(REFERENCE_OFFSET_TYPE + sizeof(sal_uInt16)) +#define REFERENCE_OFFSET_DOKU static_cast<sal_uInt32>(REFERENCE_OFFSET_NAME + sizeof(sal_uInt16)) +#define REFERENCE_OFFSET_ACCESS static_cast<sal_uInt32>(REFERENCE_OFFSET_DOKU + sizeof(sal_uInt16)) + +enum CPInfoTag +{ + CP_TAG_INVALID = RT_TYPE_NONE, + CP_TAG_CONST_BOOL = RT_TYPE_BOOL, + CP_TAG_CONST_BYTE = RT_TYPE_BYTE, + CP_TAG_CONST_INT16 = RT_TYPE_INT16, + CP_TAG_CONST_UINT16 = RT_TYPE_UINT16, + CP_TAG_CONST_INT32 = RT_TYPE_INT32, + CP_TAG_CONST_UINT32 = RT_TYPE_UINT32, + CP_TAG_CONST_INT64 = RT_TYPE_INT64, + CP_TAG_CONST_UINT64 = RT_TYPE_UINT64, + CP_TAG_CONST_FLOAT = RT_TYPE_FLOAT, + CP_TAG_CONST_DOUBLE = RT_TYPE_DOUBLE, + CP_TAG_CONST_STRING = RT_TYPE_STRING, + CP_TAG_UTF8_NAME, + CP_TAG_UIK +}; + +inline sal_uInt32 writeBYTE(sal_uInt8* buffer, sal_uInt8 v) +{ + buffer[0] = v; + + return sizeof(sal_uInt8); +} + +inline sal_uInt32 writeINT16(sal_uInt8* buffer, sal_Int16 v) +{ + buffer[0] = static_cast<sal_uInt8>((v >> 8) & 0xFF); + buffer[1] = static_cast<sal_uInt8>((v >> 0) & 0xFF); + + return sizeof(sal_Int16); +} + +inline sal_uInt32 writeUINT16(sal_uInt8* buffer, sal_uInt16 v) +{ + buffer[0] = static_cast<sal_uInt8>((v >> 8) & 0xFF); + buffer[1] = static_cast<sal_uInt8>((v >> 0) & 0xFF); + + return sizeof(sal_uInt16); +} + +inline sal_uInt32 readUINT16(const sal_uInt8* buffer, sal_uInt16& v) +{ + //This is untainted data which comes from a controlled source + //so, using a byte-swapping pattern which coverity doesn't + //detect as such + //http://security.coverity.com/blog/2014/Apr/on-detecting-heartbleed-with-static-analysis.html + v = *buffer++; v <<= 8; + v |= *buffer; + return sizeof(sal_uInt16); +} + +inline sal_uInt32 writeINT32(sal_uInt8* buffer, sal_Int32 v) +{ + buffer[0] = static_cast<sal_uInt8>((v >> 24) & 0xFF); + buffer[1] = static_cast<sal_uInt8>((v >> 16) & 0xFF); + buffer[2] = static_cast<sal_uInt8>((v >> 8) & 0xFF); + buffer[3] = static_cast<sal_uInt8>((v >> 0) & 0xFF); + + return sizeof(sal_Int32); +} + +inline sal_uInt32 readINT32(const sal_uInt8* buffer, sal_Int32& v) +{ + v = ( + (buffer[0] << 24) | + (buffer[1] << 16) | + (buffer[2] << 8) | + (buffer[3] << 0) + ); + + return sizeof(sal_Int32); +} + +inline sal_uInt32 writeUINT32(sal_uInt8* buffer, sal_uInt32 v) +{ + buffer[0] = static_cast<sal_uInt8>((v >> 24) & 0xFF); + buffer[1] = static_cast<sal_uInt8>((v >> 16) & 0xFF); + buffer[2] = static_cast<sal_uInt8>((v >> 8) & 0xFF); + buffer[3] = static_cast<sal_uInt8>((v >> 0) & 0xFF); + + return sizeof(sal_uInt32); +} + +inline sal_uInt32 readUINT32(const sal_uInt8* buffer, sal_uInt32& v) +{ + //This is untainted data which comes from a controlled source + //so, using a byte-swapping pattern which coverity doesn't + //detect as such + //http://security.coverity.com/blog/2014/Apr/on-detecting-heartbleed-with-static-analysis.html + v = *buffer++; v <<= 8; + v |= *buffer++; v <<= 8; + v |= *buffer++; v <<= 8; + v |= *buffer; + return sizeof(sal_uInt32); +} + +inline sal_uInt32 writeUINT64(sal_uInt8* buffer, sal_uInt64 v) +{ + buffer[0] = static_cast<sal_uInt8>((v >> 56) & 0xFF); + buffer[1] = static_cast<sal_uInt8>((v >> 48) & 0xFF); + buffer[2] = static_cast<sal_uInt8>((v >> 40) & 0xFF); + buffer[3] = static_cast<sal_uInt8>((v >> 32) & 0xFF); + buffer[4] = static_cast<sal_uInt8>((v >> 24) & 0xFF); + buffer[5] = static_cast<sal_uInt8>((v >> 16) & 0xFF); + buffer[6] = static_cast<sal_uInt8>((v >> 8) & 0xFF); + buffer[7] = static_cast<sal_uInt8>((v >> 0) & 0xFF); + + return sizeof(sal_uInt64); +} + +inline sal_uInt32 writeUtf8(sal_uInt8* buffer, const char* v) +{ + sal_uInt32 size = strlen(v) + 1; + + memcpy(buffer, v, size); + + return size; +} + +inline sal_uInt32 readUtf8(const sal_uInt8* buffer, char* v, sal_uInt32 maxSize) +{ + sal_uInt32 size = strlen(reinterpret_cast<const char*>(buffer)) + 1; + if(size > maxSize) + { + size = maxSize; + } + + memcpy(v, buffer, size); + + if (size == maxSize) v[size - 1] = '\0'; + + return size; +} + + +sal_uInt32 writeFloat(sal_uInt8* buffer, float v); +sal_uInt32 writeDouble(sal_uInt8* buffer, double v); +sal_uInt32 writeString(sal_uInt8* buffer, const sal_Unicode* v); +sal_uInt32 readString(const sal_uInt8* buffer, sal_Unicode* v, sal_uInt32 maxSize); + +sal_uInt32 UINT16StringLen(const sal_uInt8* wstring); + +#endif + + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/registry/source/reflread.cxx b/registry/source/reflread.cxx new file mode 100644 index 000000000..57884e110 --- /dev/null +++ b/registry/source/reflread.cxx @@ -0,0 +1,1760 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <cstring> +#include <memory> +#include <new> +#include <vector> + +#include <sal/types.h> +#include <osl/endian.h> +#include <osl/diagnose.h> +#include "reflread.hxx" +#include <sal/log.hxx> + +#include <registry/typereg_reader.hxx> +#include <registry/version.h> + +#include "reflcnst.hxx" + +#include <cstddef> + +const char NULL_STRING[1] = { 0 }; +const sal_Unicode NULL_WSTRING[1] = { 0 }; + +const sal_uInt32 magic = 0x12345678; +const sal_uInt16 minorVersion = 0x0000; +const sal_uInt16 majorVersion = 0x0001; + +/************************************************************************** + + class BlopObject + + holds any data in a flat memory buffer + +**************************************************************************/ + +namespace { + +class BlopObject +{ +public: + struct BoundsError {}; + + const sal_uInt8* m_pBuffer; + sal_uInt32 m_bufferLen; + + BlopObject(const sal_uInt8* buffer, sal_uInt32 len); + // throws std::bad_alloc + + sal_uInt8 readBYTE(sal_uInt32 index) const + { + if (index >= m_bufferLen) { + throw BoundsError(); + } + return m_pBuffer[index]; + } + + sal_Int16 readINT16(sal_uInt32 index) const + { + if (m_bufferLen < 2 || index >= m_bufferLen - 1) { + throw BoundsError(); + } + return ((m_pBuffer[index] << 8) | (m_pBuffer[index+1] << 0)); + } + + sal_uInt16 readUINT16(sal_uInt32 index) const + { + if (m_bufferLen < 2 || index >= m_bufferLen - 1) { + throw BoundsError(); + } + return ((m_pBuffer[index] << 8) | (m_pBuffer[index+1] << 0)); + } + + sal_Int32 readINT32(sal_uInt32 index) const + { + if (m_bufferLen < 4 || index >= m_bufferLen - 3) { + throw BoundsError(); + } + return ( + (m_pBuffer[index] << 24) | + (m_pBuffer[index+1] << 16) | + (m_pBuffer[index+2] << 8) | + (m_pBuffer[index+3] << 0) + ); + } + + sal_uInt32 readUINT32(sal_uInt32 index) const + { + if (m_bufferLen < 4 || index >= m_bufferLen - 3) { + throw BoundsError(); + } + return ( + (m_pBuffer[index] << 24) | + (m_pBuffer[index+1] << 16) | + (m_pBuffer[index+2] << 8) | + (m_pBuffer[index+3] << 0) + ); + } + + sal_Int64 readINT64(sal_uInt32 index) const + { + if (m_bufferLen < 8 || index >= m_bufferLen - 7) { + throw BoundsError(); + } + return ( + (static_cast<sal_Int64>(m_pBuffer[index]) << 56) | + (static_cast<sal_Int64>(m_pBuffer[index+1]) << 48) | + (static_cast<sal_Int64>(m_pBuffer[index+2]) << 40) | + (static_cast<sal_Int64>(m_pBuffer[index+3]) << 32) | + (static_cast<sal_Int64>(m_pBuffer[index+4]) << 24) | + (static_cast<sal_Int64>(m_pBuffer[index+5]) << 16) | + (static_cast<sal_Int64>(m_pBuffer[index+6]) << 8) | + (static_cast<sal_Int64>(m_pBuffer[index+7]) << 0) + ); + } + + sal_uInt64 readUINT64(sal_uInt32 index) const + { + if (m_bufferLen < 8 || index >= m_bufferLen - 7) { + throw BoundsError(); + } + return ( + (static_cast<sal_uInt64>(m_pBuffer[index]) << 56) | + (static_cast<sal_uInt64>(m_pBuffer[index+1]) << 48) | + (static_cast<sal_uInt64>(m_pBuffer[index+2]) << 40) | + (static_cast<sal_uInt64>(m_pBuffer[index+3]) << 32) | + (static_cast<sal_uInt64>(m_pBuffer[index+4]) << 24) | + (static_cast<sal_uInt64>(m_pBuffer[index+5]) << 16) | + (static_cast<sal_uInt64>(m_pBuffer[index+6]) << 8) | + (static_cast<sal_uInt64>(m_pBuffer[index+7]) << 0) + ); + } +}; + +} + +BlopObject::BlopObject(const sal_uInt8* buffer, sal_uInt32 len) + : m_bufferLen(len) +{ + m_pBuffer = buffer; +} + +/************************************************************************** + + class StringCache + +**************************************************************************/ + +namespace { + +class StringCache +{ +public: + std::vector<std::unique_ptr<sal_Unicode[]>> m_stringTable; + sal_uInt16 m_stringsCopied; + + explicit StringCache(sal_uInt16 size); // throws std::bad_alloc + + const sal_Unicode* getString(sal_uInt16 index) const; + sal_uInt16 createString(const sal_uInt8* buffer); // throws std::bad_alloc +}; + +} + +StringCache::StringCache(sal_uInt16 size) + : m_stringTable(size) + , m_stringsCopied(0) +{ +} + +const sal_Unicode* StringCache::getString(sal_uInt16 index) const +{ + if ((index > 0) && (index <= m_stringsCopied)) + return m_stringTable[index - 1].get(); + else + return nullptr; +} + +sal_uInt16 StringCache::createString(const sal_uInt8* buffer) +{ + if (m_stringsCopied < m_stringTable.size()) + { + sal_uInt32 len = UINT16StringLen(buffer); + + m_stringTable[m_stringsCopied].reset( new sal_Unicode[len + 1] ); + + readString(buffer, m_stringTable[m_stringsCopied].get(), (len + 1) * sizeof(sal_Unicode)); + + return ++m_stringsCopied; + } + else + return 0; +} + +/************************************************************************** + + class ConstantPool + +**************************************************************************/ + +namespace { + +class ConstantPool : public BlopObject +{ +public: + + sal_uInt16 m_numOfEntries; + std::unique_ptr<sal_Int32[]> m_pIndex; // index values may be < 0 for cached string constants + + std::unique_ptr<StringCache> m_pStringCache; + + ConstantPool(const sal_uInt8* buffer, sal_uInt32 len, sal_uInt16 numEntries) + : BlopObject(buffer, len) + , m_numOfEntries(numEntries) + { + } + + sal_uInt32 parseIndex(); // throws std::bad_alloc + + CPInfoTag readTag(sal_uInt16 index) const; + + const char* readUTF8NameConstant(sal_uInt16 index) const; + bool readBOOLConstant(sal_uInt16 index) const; + sal_Int8 readBYTEConstant(sal_uInt16 index) const; + sal_Int16 readINT16Constant(sal_uInt16 index) const; + sal_uInt16 readUINT16Constant(sal_uInt16 index) const; + sal_Int32 readINT32Constant(sal_uInt16 index) const; + sal_uInt32 readUINT32Constant(sal_uInt16 index) const; + sal_Int64 readINT64Constant(sal_uInt16 index) const; + sal_uInt64 readUINT64Constant(sal_uInt16 index) const; + float readFloatConstant(sal_uInt16 index) const; + double readDoubleConstant(sal_uInt16 index) const; + const sal_Unicode* readStringConstant(sal_uInt16 index) const; + // throws std::bad_alloc +}; + +} + +sal_uInt32 ConstantPool::parseIndex() +{ + m_pIndex.reset(); + m_pStringCache.reset(); + + sal_uInt32 offset = 0; + sal_uInt16 numOfStrings = 0; + + if (m_numOfEntries) + { + m_pIndex.reset( new sal_Int32[m_numOfEntries] ); + + for (int i = 0; i < m_numOfEntries; i++) + { + m_pIndex[i] = offset; + + offset += readUINT32(offset); + + if ( static_cast<CPInfoTag>(readUINT16(m_pIndex[i] + CP_OFFSET_ENTRY_TAG)) == + CP_TAG_CONST_STRING ) + { + numOfStrings++; + } + + } + } + + if (numOfStrings) + { + m_pStringCache.reset( new StringCache(numOfStrings) ); + } + + m_bufferLen = offset; + + return offset; +} + +CPInfoTag ConstantPool::readTag(sal_uInt16 index) const +{ + CPInfoTag tag = CP_TAG_INVALID; + + if (m_pIndex && (index > 0) && (index <= m_numOfEntries)) + { + tag = static_cast<CPInfoTag>(readUINT16(m_pIndex[index - 1] + CP_OFFSET_ENTRY_TAG)); + } + + return tag; +} + +const char* ConstantPool::readUTF8NameConstant(sal_uInt16 index) const +{ + const char* aName = NULL_STRING; + + if (m_pIndex && (index > 0) && (index <= m_numOfEntries)) + { + if (readUINT16(m_pIndex[index - 1] + CP_OFFSET_ENTRY_TAG) == CP_TAG_UTF8_NAME) + { + sal_uInt32 n = m_pIndex[index - 1] + CP_OFFSET_ENTRY_DATA; + if (n < m_bufferLen + && std::memchr(m_pBuffer + n, 0, m_bufferLen - n) != nullptr) + { + aName = reinterpret_cast<const char*>(m_pBuffer + n); + } + } + } + + return aName; +} + +bool ConstantPool::readBOOLConstant(sal_uInt16 index) const +{ + bool aBool = false; + + if (m_pIndex && (index> 0) && (index <= m_numOfEntries)) + { + if (readUINT16(m_pIndex[index - 1] + CP_OFFSET_ENTRY_TAG) == CP_TAG_CONST_BOOL) + { + aBool = readBYTE(m_pIndex[index - 1] + CP_OFFSET_ENTRY_DATA) != 0; + } + } + + return aBool; +} + +sal_Int8 ConstantPool::readBYTEConstant(sal_uInt16 index) const +{ + sal_Int8 aByte = 0; + + if (m_pIndex && (index> 0) && (index <= m_numOfEntries)) + { + if (readUINT16(m_pIndex[index - 1] + CP_OFFSET_ENTRY_TAG) == CP_TAG_CONST_BYTE) + { + aByte = static_cast< sal_Int8 >( + readBYTE(m_pIndex[index - 1] + CP_OFFSET_ENTRY_DATA)); + } + } + + return aByte; +} + +sal_Int16 ConstantPool::readINT16Constant(sal_uInt16 index) const +{ + sal_Int16 aINT16 = 0; + + if (m_pIndex && (index> 0) && (index <= m_numOfEntries)) + { + if (readUINT16(m_pIndex[index - 1] + CP_OFFSET_ENTRY_TAG) == CP_TAG_CONST_INT16) + { + aINT16 = readINT16(m_pIndex[index - 1] + CP_OFFSET_ENTRY_DATA); + } + } + + return aINT16; +} + +sal_uInt16 ConstantPool::readUINT16Constant(sal_uInt16 index) const +{ + sal_uInt16 asal_uInt16 = 0; + + if (m_pIndex && (index> 0) && (index <= m_numOfEntries)) + { + if (readUINT16(m_pIndex[index - 1] + CP_OFFSET_ENTRY_TAG) == CP_TAG_CONST_UINT16) + { + asal_uInt16 = readUINT16(m_pIndex[index - 1] + CP_OFFSET_ENTRY_DATA); + } + } + + return asal_uInt16; +} + +sal_Int32 ConstantPool::readINT32Constant(sal_uInt16 index) const +{ + sal_Int32 aINT32 = 0; + + if (m_pIndex && (index> 0) && (index <= m_numOfEntries)) + { + if (readUINT16(m_pIndex[index - 1] + CP_OFFSET_ENTRY_TAG) == CP_TAG_CONST_INT32) + { + aINT32 = readINT32(m_pIndex[index - 1] + CP_OFFSET_ENTRY_DATA); + } + } + + return aINT32; +} + +sal_uInt32 ConstantPool::readUINT32Constant(sal_uInt16 index) const +{ + sal_uInt32 aUINT32 = 0; + + if (m_pIndex && (index> 0) && (index <= m_numOfEntries)) + { + if (readUINT16(m_pIndex[index - 1] + CP_OFFSET_ENTRY_TAG) == CP_TAG_CONST_UINT32) + { + aUINT32 = readUINT32(m_pIndex[index - 1] + CP_OFFSET_ENTRY_DATA); + } + } + + return aUINT32; +} + +sal_Int64 ConstantPool::readINT64Constant(sal_uInt16 index) const +{ + sal_Int64 aINT64 = 0; + + if (m_pIndex && (index> 0) && (index <= m_numOfEntries)) + { + if (readUINT16(m_pIndex[index - 1] + CP_OFFSET_ENTRY_TAG) == CP_TAG_CONST_INT64) + { + aINT64 = readINT64(m_pIndex[index - 1] + CP_OFFSET_ENTRY_DATA); + } + } + + return aINT64; +} + +sal_uInt64 ConstantPool::readUINT64Constant(sal_uInt16 index) const +{ + sal_uInt64 aUINT64 = 0; + + if (m_pIndex && (index> 0) && (index <= m_numOfEntries)) + { + if (readUINT16(m_pIndex[index - 1] + CP_OFFSET_ENTRY_TAG) == CP_TAG_CONST_UINT64) + { + aUINT64 = readUINT64(m_pIndex[index - 1] + CP_OFFSET_ENTRY_DATA); + } + } + + return aUINT64; +} + +float ConstantPool::readFloatConstant(sal_uInt16 index) const +{ + union + { + float v; + sal_uInt32 b; + } x = { 0.0f }; + + if (m_pIndex && (index> 0) && (index <= m_numOfEntries)) + { + if (readUINT16(m_pIndex[index - 1] + CP_OFFSET_ENTRY_TAG) == CP_TAG_CONST_FLOAT) + { +#ifdef REGTYPE_IEEE_NATIVE + x.b = readUINT32(m_pIndex[index - 1] + CP_OFFSET_ENTRY_DATA); +#else +# error no IEEE +#endif + } + } + + return x.v; +} + +double ConstantPool::readDoubleConstant(sal_uInt16 index) const +{ + union + { + double v; + struct + { + sal_uInt32 b1; + sal_uInt32 b2; + } b; + } x = { 0.0 }; + + if (m_pIndex && (index> 0) && (index <= m_numOfEntries)) + { + if (readUINT16(m_pIndex[index - 1] + CP_OFFSET_ENTRY_TAG) == CP_TAG_CONST_DOUBLE) + { + +#ifdef REGTYPE_IEEE_NATIVE +# ifdef OSL_BIGENDIAN + x.b.b1 = readUINT32(m_pIndex[index - 1] + CP_OFFSET_ENTRY_DATA); + x.b.b2 = readUINT32(m_pIndex[index - 1] + CP_OFFSET_ENTRY_DATA + sizeof(sal_uInt32)); +# else + x.b.b1 = readUINT32(m_pIndex[index - 1] + CP_OFFSET_ENTRY_DATA + sizeof(sal_uInt32)); + x.b.b2 = readUINT32(m_pIndex[index - 1] + CP_OFFSET_ENTRY_DATA); +# endif +#else +# error no IEEE +#endif + } + } + + return x.v; +} + +const sal_Unicode* ConstantPool::readStringConstant(sal_uInt16 index) const +{ + const sal_Unicode* aString = NULL_WSTRING; + + if (m_pIndex && (index> 0) && (index <= m_numOfEntries) && m_pStringCache) + { + if (m_pIndex[index - 1] >= 0) + { + // create cached string now + + if (readUINT16(m_pIndex[index - 1] + CP_OFFSET_ENTRY_TAG) == CP_TAG_CONST_STRING) + { + sal_uInt32 n = m_pIndex[index - 1] + CP_OFFSET_ENTRY_DATA; + if (n >= m_bufferLen + || (std::memchr(m_pBuffer + n, 0, m_bufferLen - n) + == nullptr)) + { + throw BoundsError(); + } + m_pIndex[index - 1] = -1 * m_pStringCache->createString(m_pBuffer + n); + } + } + + aString = m_pStringCache->getString(static_cast<sal_uInt16>(m_pIndex[index - 1] * -1)); + } + + return aString; +} + +/************************************************************************** + + class FieldList + +**************************************************************************/ + +namespace { + +class FieldList : public BlopObject +{ +public: + + sal_uInt16 m_numOfEntries; + size_t m_FIELD_ENTRY_SIZE; + ConstantPool* m_pCP; + + FieldList(const sal_uInt8* buffer, sal_uInt32 len, sal_uInt16 numEntries, ConstantPool* pCP) + : BlopObject(buffer, len) + , m_numOfEntries(numEntries) + , m_pCP(pCP) + { + if ( m_numOfEntries > 0 ) + { + sal_uInt16 numOfFieldEntries = readUINT16(0); + m_FIELD_ENTRY_SIZE = numOfFieldEntries * sizeof(sal_uInt16); + } else + { + m_FIELD_ENTRY_SIZE = 0; + } + } + + sal_uInt32 parseIndex() const { return ((m_numOfEntries ? sizeof(sal_uInt16) : 0) + (m_numOfEntries * m_FIELD_ENTRY_SIZE));} + + const char* getFieldName(sal_uInt16 index) const; + const char* getFieldType(sal_uInt16 index) const; + RTFieldAccess getFieldAccess(sal_uInt16 index) const; + RTValueType getFieldConstValue(sal_uInt16 index, RTConstValueUnion* value) const; + // throws std::bad_alloc + const char* getFieldDoku(sal_uInt16 index) const; + const char* getFieldFileName(sal_uInt16 index) const; +}; + +} + +const char* FieldList::getFieldName(sal_uInt16 index) const +{ + const char* aName = nullptr; + + if ((m_numOfEntries > 0) && (index <= m_numOfEntries)) + { + try { + aName = m_pCP->readUTF8NameConstant(readUINT16(sizeof(sal_uInt16) + (index * m_FIELD_ENTRY_SIZE) + FIELD_OFFSET_NAME)); + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + + return aName; +} + +const char* FieldList::getFieldType(sal_uInt16 index) const +{ + const char* aName = nullptr; + + if ((m_numOfEntries > 0) && (index <= m_numOfEntries)) + { + try { + aName = m_pCP->readUTF8NameConstant(readUINT16(sizeof(sal_uInt16) + (index * m_FIELD_ENTRY_SIZE) + FIELD_OFFSET_TYPE)); + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + + return aName; +} + +RTFieldAccess FieldList::getFieldAccess(sal_uInt16 index) const +{ + RTFieldAccess aAccess = RTFieldAccess::INVALID; + + if ((m_numOfEntries > 0) && (index <= m_numOfEntries)) + { + try { + aAccess = static_cast<RTFieldAccess>(readUINT16(sizeof(sal_uInt16) + (index * m_FIELD_ENTRY_SIZE) + FIELD_OFFSET_ACCESS)); + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + + return aAccess; +} + +RTValueType FieldList::getFieldConstValue(sal_uInt16 index, RTConstValueUnion* value) const +{ + RTValueType ret = RT_TYPE_NONE; + try { + if ((m_numOfEntries > 0) && (index <= m_numOfEntries)) + { + sal_uInt16 cpIndex = readUINT16(sizeof(sal_uInt16) + (index * m_FIELD_ENTRY_SIZE) + FIELD_OFFSET_VALUE); + switch (m_pCP->readTag(cpIndex)) + { + case CP_TAG_CONST_BOOL: + value->aBool = m_pCP->readBOOLConstant(cpIndex); + ret = RT_TYPE_BOOL; + break; + case CP_TAG_CONST_BYTE: + value->aByte = m_pCP->readBYTEConstant(cpIndex); + ret = RT_TYPE_BYTE; + break; + case CP_TAG_CONST_INT16: + value->aShort = m_pCP->readINT16Constant(cpIndex); + ret = RT_TYPE_INT16; + break; + case CP_TAG_CONST_UINT16: + value->aUShort = m_pCP->readUINT16Constant(cpIndex); + ret = RT_TYPE_UINT16; + break; + case CP_TAG_CONST_INT32: + value->aLong = m_pCP->readINT32Constant(cpIndex); + ret = RT_TYPE_INT32; + break; + case CP_TAG_CONST_UINT32: + value->aULong = m_pCP->readUINT32Constant(cpIndex); + ret = RT_TYPE_UINT32; + break; + case CP_TAG_CONST_INT64: + value->aHyper = m_pCP->readINT64Constant(cpIndex); + ret = RT_TYPE_INT64; + break; + case CP_TAG_CONST_UINT64: + value->aUHyper = m_pCP->readUINT64Constant(cpIndex); + ret = RT_TYPE_UINT64; + break; + case CP_TAG_CONST_FLOAT: + value->aFloat = m_pCP->readFloatConstant(cpIndex); + ret = RT_TYPE_FLOAT; + break; + case CP_TAG_CONST_DOUBLE: + value->aDouble = m_pCP->readDoubleConstant(cpIndex); + ret = RT_TYPE_DOUBLE; + break; + case CP_TAG_CONST_STRING: + value->aString = m_pCP->readStringConstant(cpIndex); + ret = RT_TYPE_STRING; + break; + default: + break; + } + } + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + return ret; +} + +const char* FieldList::getFieldDoku(sal_uInt16 index) const +{ + const char* aDoku = nullptr; + + if ((m_numOfEntries > 0) && (index <= m_numOfEntries)) + { + try { + aDoku = m_pCP->readUTF8NameConstant(readUINT16(sizeof(sal_uInt16) + (index * m_FIELD_ENTRY_SIZE) + FIELD_OFFSET_DOKU)); + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + + return aDoku; +} + +const char* FieldList::getFieldFileName(sal_uInt16 index) const +{ + const char* aFileName = nullptr; + + if ((m_numOfEntries > 0) && (index <= m_numOfEntries)) + { + try { + aFileName = m_pCP->readUTF8NameConstant(readUINT16(sizeof(sal_uInt16) + (index * m_FIELD_ENTRY_SIZE) + FIELD_OFFSET_FILENAME)); + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + + return aFileName; +} + +/************************************************************************** + + class ReferenceList + +**************************************************************************/ + +namespace { + +class ReferenceList : public BlopObject +{ +public: + + sal_uInt16 m_numOfEntries; + size_t m_REFERENCE_ENTRY_SIZE; + ConstantPool* m_pCP; + + ReferenceList(const sal_uInt8* buffer, sal_uInt32 len, sal_uInt16 numEntries, ConstantPool* pCP) + : BlopObject(buffer, len) + , m_numOfEntries(numEntries) + , m_pCP(pCP) + { + if ( m_numOfEntries > 0 ) + { + sal_uInt16 numOfReferenceEntries = readUINT16(0); + m_REFERENCE_ENTRY_SIZE = numOfReferenceEntries * sizeof(sal_uInt16); + } else + { + m_REFERENCE_ENTRY_SIZE = 0; + } + } + + const char* getReferenceName(sal_uInt16 index) const; + RTReferenceType getReferenceType(sal_uInt16 index) const; + const char* getReferenceDoku(sal_uInt16 index) const; + RTFieldAccess getReferenceAccess(sal_uInt16 index) const; +}; + +} + +const char* ReferenceList::getReferenceName(sal_uInt16 index) const +{ + const char* aName = nullptr; + + if ((m_numOfEntries > 0) && (index <= m_numOfEntries)) + { + try { + aName = m_pCP->readUTF8NameConstant(readUINT16(sizeof(sal_uInt16) + (index * m_REFERENCE_ENTRY_SIZE) + REFERENCE_OFFSET_NAME)); + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + + return aName; +} + +RTReferenceType ReferenceList::getReferenceType(sal_uInt16 index) const +{ + RTReferenceType refType = RTReferenceType::INVALID; + + if ((m_numOfEntries > 0) && (index <= m_numOfEntries)) + { + try { + refType = static_cast<RTReferenceType>(readUINT16(sizeof(sal_uInt16) + (index * m_REFERENCE_ENTRY_SIZE) + REFERENCE_OFFSET_TYPE)); + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + + return refType; +} + +const char* ReferenceList::getReferenceDoku(sal_uInt16 index) const +{ + const char* aDoku = nullptr; + + if ((m_numOfEntries > 0) && (index <= m_numOfEntries)) + { + try { + aDoku = m_pCP->readUTF8NameConstant(readUINT16(sizeof(sal_uInt16) + (index * m_REFERENCE_ENTRY_SIZE) + REFERENCE_OFFSET_DOKU)); + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + + return aDoku; +} + +RTFieldAccess ReferenceList::getReferenceAccess(sal_uInt16 index) const +{ + RTFieldAccess aAccess = RTFieldAccess::INVALID; + + if ((m_numOfEntries > 0) && (index <= m_numOfEntries)) + { + try { + aAccess = static_cast<RTFieldAccess>(readUINT16(sizeof(sal_uInt16) + (index * m_REFERENCE_ENTRY_SIZE) + REFERENCE_OFFSET_ACCESS)); + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + + return aAccess; +} + +/************************************************************************** + + class MethodList + +**************************************************************************/ + +namespace { + +class MethodList : public BlopObject +{ +public: + + sal_uInt16 m_numOfEntries; + size_t m_PARAM_ENTRY_SIZE; + std::unique_ptr<sal_uInt32[]> m_pIndex; + ConstantPool* m_pCP; + + MethodList(const sal_uInt8* buffer, sal_uInt32 len, sal_uInt16 numEntries, ConstantPool* pCP) + : BlopObject(buffer, len) + , m_numOfEntries(numEntries) + , m_pCP(pCP) + { + if ( m_numOfEntries > 0 ) + { + readUINT16(0) /* numOfMethodEntries */; + sal_uInt16 numOfParamEntries = readUINT16(sizeof(sal_uInt16)); + m_PARAM_ENTRY_SIZE = numOfParamEntries * sizeof(sal_uInt16); + } else + { + m_PARAM_ENTRY_SIZE = 0; + } + } + + sal_uInt32 parseIndex(); // throws std::bad_alloc + + const char* getMethodName(sal_uInt16 index) const; + sal_uInt16 getMethodParamCount(sal_uInt16 index) const; + const char* getMethodParamType(sal_uInt16 index, sal_uInt16 paramIndex) const; + const char* getMethodParamName(sal_uInt16 index, sal_uInt16 paramIndex) const; + RTParamMode getMethodParamMode(sal_uInt16 index, sal_uInt16 paramIndex) const; + sal_uInt16 getMethodExcCount(sal_uInt16 index) const; + const char* getMethodExcType(sal_uInt16 index, sal_uInt16 excIndex) const; + const char* getMethodReturnType(sal_uInt16 index) const; + RTMethodMode getMethodMode(sal_uInt16 index) const; + const char* getMethodDoku(sal_uInt16 index) const; + +private: + sal_uInt16 calcMethodParamIndex( const sal_uInt16 index ) const; +}; + +} + +sal_uInt16 MethodList::calcMethodParamIndex( const sal_uInt16 index ) const +{ + return (METHOD_OFFSET_PARAM_COUNT + sizeof(sal_uInt16) + (index * m_PARAM_ENTRY_SIZE)); +} + +sal_uInt32 MethodList::parseIndex() +{ + m_pIndex.reset(); + + sal_uInt32 offset = 0; + + if (m_numOfEntries) + { + offset = 2 * sizeof(sal_uInt16); + m_pIndex.reset( new sal_uInt32[m_numOfEntries] ); + + for (int i = 0; i < m_numOfEntries; i++) + { + m_pIndex[i] = offset; + + offset += readUINT16(offset); + } + } + + return offset; +} + +const char* MethodList::getMethodName(sal_uInt16 index) const +{ + const char* aName = nullptr; + + if ((m_numOfEntries > 0) && (index <= m_numOfEntries)) + { + try { + aName = m_pCP->readUTF8NameConstant(readUINT16(m_pIndex[index] + METHOD_OFFSET_NAME)); + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + + return aName; +} + +sal_uInt16 MethodList::getMethodParamCount(sal_uInt16 index) const +{ + sal_uInt16 aCount = 0; + + if ((m_numOfEntries > 0) && (index <= m_numOfEntries)) + { + try { + aCount = readUINT16(m_pIndex[index] + METHOD_OFFSET_PARAM_COUNT); + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + + return aCount; +} + +const char* MethodList::getMethodParamType(sal_uInt16 index, sal_uInt16 paramIndex) const +{ + const char* aName = nullptr; + try { + if ((m_numOfEntries > 0) && + (index <= m_numOfEntries) && + (paramIndex <= readUINT16(m_pIndex[index] + METHOD_OFFSET_PARAM_COUNT))) + { + aName = m_pCP->readUTF8NameConstant( + readUINT16( + m_pIndex[index] + + calcMethodParamIndex(paramIndex) + + PARAM_OFFSET_TYPE)); + } + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + return aName; +} + +const char* MethodList::getMethodParamName(sal_uInt16 index, sal_uInt16 paramIndex) const +{ + const char* aName = nullptr; + try { + if ((m_numOfEntries > 0) && + (index <= m_numOfEntries) && + (paramIndex <= readUINT16(m_pIndex[index] + METHOD_OFFSET_PARAM_COUNT))) + { + aName = m_pCP->readUTF8NameConstant( + readUINT16( + m_pIndex[index] + + calcMethodParamIndex(paramIndex) + + PARAM_OFFSET_NAME)); + } + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + return aName; +} + +RTParamMode MethodList::getMethodParamMode(sal_uInt16 index, sal_uInt16 paramIndex) const +{ + RTParamMode aMode = RT_PARAM_INVALID; + try { + if ((m_numOfEntries > 0) && + (index <= m_numOfEntries) && + (paramIndex <= readUINT16(m_pIndex[index] + METHOD_OFFSET_PARAM_COUNT))) + { + aMode = static_cast<RTParamMode>(readUINT16( + m_pIndex[index] + + calcMethodParamIndex(paramIndex) + + PARAM_OFFSET_MODE)); + } + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + return aMode; +} + +#if defined(__COVERITY__) +extern "C" void __coverity_tainted_data_sanitize__(void *); +#endif + +sal_uInt16 MethodList::getMethodExcCount(sal_uInt16 index) const +{ + sal_uInt16 aCount = 0; + + if ((m_numOfEntries > 0) && (index <= m_numOfEntries)) + { + try { + aCount = readUINT16(m_pIndex[index] + calcMethodParamIndex(readUINT16(m_pIndex[index] + METHOD_OFFSET_PARAM_COUNT))); +#if defined(__COVERITY__) + __coverity_tainted_data_sanitize__(&aCount); +#endif + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + + return aCount; +} + +const char* MethodList::getMethodExcType(sal_uInt16 index, sal_uInt16 excIndex) const +{ + const char* aName = nullptr; + + if ((m_numOfEntries > 0) && (index <= m_numOfEntries)) + { + try { + sal_uInt32 excOffset = m_pIndex[index] + calcMethodParamIndex(readUINT16(m_pIndex[index] + METHOD_OFFSET_PARAM_COUNT)); + if (excIndex <= readUINT16(excOffset)) + { + aName = m_pCP->readUTF8NameConstant( + readUINT16( + excOffset + + sizeof(sal_uInt16) + + (excIndex * sizeof(sal_uInt16)))); + } + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + + return aName; +} + +const char* MethodList::getMethodReturnType(sal_uInt16 index) const +{ + const char* aName = nullptr; + + if ((m_numOfEntries > 0) && (index <= m_numOfEntries)) + { + try { + aName = m_pCP->readUTF8NameConstant(readUINT16(m_pIndex[index] + METHOD_OFFSET_RETURN)); + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + + return aName; +} + +RTMethodMode MethodList::getMethodMode(sal_uInt16 index) const +{ + RTMethodMode aMode = RTMethodMode::INVALID; + + if ((m_numOfEntries > 0) && (index <= m_numOfEntries)) + { + try { + aMode = static_cast<RTMethodMode>(readUINT16(m_pIndex[index] + METHOD_OFFSET_MODE)); + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + + return aMode; +} + +const char* MethodList::getMethodDoku(sal_uInt16 index) const +{ + const char* aDoku = nullptr; + + if ((m_numOfEntries > 0) && (index <= m_numOfEntries)) + { + try { + aDoku = m_pCP->readUTF8NameConstant(readUINT16(m_pIndex[index] + METHOD_OFFSET_DOKU)); + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + + return aDoku; +} + +/************************************************************************** + + class TypeRegistryEntry + +**************************************************************************/ + +namespace { + +class TypeRegistryEntry: public BlopObject { +public: + std::unique_ptr<ConstantPool> m_pCP; + std::unique_ptr<FieldList> m_pFields; + std::unique_ptr<MethodList> m_pMethods; + std::unique_ptr<ReferenceList> m_pReferences; + sal_uInt32 m_refCount; + sal_uInt16 m_nSuperTypes; + sal_uInt32 m_offset_SUPERTYPES; + + TypeRegistryEntry( + const sal_uInt8* buffer, sal_uInt32 len); + // throws std::bad_alloc + + typereg_Version getVersion() const; +}; + +} + +TypeRegistryEntry::TypeRegistryEntry( + const sal_uInt8* buffer, sal_uInt32 len): + BlopObject(buffer, len), m_refCount(1), m_nSuperTypes(0), + m_offset_SUPERTYPES(0) +{ + std::size_t const entrySize = sizeof(sal_uInt16); + sal_uInt16 nHeaderEntries = readUINT16(OFFSET_N_ENTRIES); + sal_uInt32 offset_N_SUPERTYPES = OFFSET_N_ENTRIES + entrySize + (nHeaderEntries * entrySize); // cannot overflow + m_offset_SUPERTYPES = offset_N_SUPERTYPES + entrySize; // cannot overflow + m_nSuperTypes = readUINT16(offset_N_SUPERTYPES); + + sal_uInt32 offset_CP_SIZE = m_offset_SUPERTYPES + (m_nSuperTypes * entrySize); // cannot overflow + sal_uInt32 offset_CP = offset_CP_SIZE + entrySize; // cannot overflow + + if (offset_CP > m_bufferLen) { + throw BoundsError(); + } + m_pCP.reset( + new ConstantPool( + m_pBuffer + offset_CP, m_bufferLen - offset_CP, + readUINT16(offset_CP_SIZE))); + + sal_uInt32 offset = offset_CP + m_pCP->parseIndex(); //TODO: overflow + + assert(m_bufferLen >= entrySize); + if (offset > m_bufferLen - entrySize) { + throw BoundsError(); + } + m_pFields.reset( + new FieldList( + m_pBuffer + offset + entrySize, m_bufferLen - (offset + entrySize), + readUINT16(offset), m_pCP.get())); + + offset += sizeof(sal_uInt16) + m_pFields->parseIndex(); //TODO: overflow + + assert(m_bufferLen >= entrySize); + if (offset > m_bufferLen - entrySize) { + throw BoundsError(); + } + m_pMethods.reset( + new MethodList( + m_pBuffer + offset + entrySize, m_bufferLen - (offset + entrySize), + readUINT16(offset), m_pCP.get())); + + offset += sizeof(sal_uInt16) + m_pMethods->parseIndex(); //TODO: overflow + + assert(m_bufferLen >= entrySize); + if (offset > m_bufferLen - entrySize) { + throw BoundsError(); + } + m_pReferences.reset( + new ReferenceList( + m_pBuffer + offset + entrySize, m_bufferLen - (offset + entrySize), + readUINT16(offset), m_pCP.get())); +} + +typereg_Version TypeRegistryEntry::getVersion() const { + // Assumes two's complement arithmetic with modulo-semantics: + return static_cast< typereg_Version >(readUINT32(OFFSET_MAGIC) - magic); +} + +/************************************************************************** + + C-API + +**************************************************************************/ + +bool TYPEREG_CALLTYPE typereg_reader_create( + void const * buffer, sal_uInt32 length, + void ** result) +{ + if (length < OFFSET_CP || length > SAL_MAX_UINT32) { + *result = nullptr; + return true; + } + std::unique_ptr< TypeRegistryEntry > entry; + try { + try { + entry.reset( + new TypeRegistryEntry( + static_cast< sal_uInt8 const * >(buffer), length)); + } catch (std::bad_alloc &) { + return false; + } + if (entry->readUINT32(OFFSET_SIZE) != length) { + *result = nullptr; + return true; + } + typereg_Version version = entry->getVersion(); + if (version < TYPEREG_VERSION_0 || version > TYPEREG_VERSION_1) { + *result = nullptr; + return true; + } + *result = entry.release(); + return true; + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + return false; + } +} + +static TypeReaderImpl TYPEREG_CALLTYPE createEntry(const sal_uInt8* buffer, sal_uInt32 len) +{ + void * handle; + typereg_reader_create(buffer, len, &handle); + return handle; +} + +void TYPEREG_CALLTYPE typereg_reader_acquire(void * hEntry) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry != nullptr) + pEntry->m_refCount++; +} + +void TYPEREG_CALLTYPE typereg_reader_release(void * hEntry) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry != nullptr) + { + if (--pEntry->m_refCount == 0) + delete pEntry; + } +} + +typereg_Version TYPEREG_CALLTYPE typereg_reader_getVersion(void const * handle) { + if (handle != nullptr) { + try { + return static_cast< TypeRegistryEntry const * >(handle)->getVersion(); + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + return TYPEREG_VERSION_0; +} + +RTTypeClass TYPEREG_CALLTYPE typereg_reader_getTypeClass(void * hEntry) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + if (pEntry != nullptr) { + try { + return static_cast<RTTypeClass>(pEntry->readUINT16(OFFSET_TYPE_CLASS) & ~RT_TYPE_PUBLISHED); + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + return RT_TYPE_INVALID; +} + +bool TYPEREG_CALLTYPE typereg_reader_isPublished(void * hEntry) +{ + TypeRegistryEntry * entry = static_cast< TypeRegistryEntry * >(hEntry); + if (entry != nullptr) { + try { + return (entry->readUINT16(OFFSET_TYPE_CLASS) & RT_TYPE_PUBLISHED) != 0; + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + return false; +} + +void TYPEREG_CALLTYPE typereg_reader_getTypeName(void * hEntry, rtl_uString** pTypeName) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + if (pEntry != nullptr) { + try { + const char* pTmp = pEntry->m_pCP->readUTF8NameConstant(pEntry->readUINT16(OFFSET_THIS_TYPE)); + rtl_string2UString( + pTypeName, pTmp, pTmp == nullptr ? 0 : rtl_str_getLength(pTmp), + RTL_TEXTENCODING_UTF8, OSTRING_TO_OUSTRING_CVTFLAGS); + return; + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + rtl_uString_new(pTypeName); +} + + +static void TYPEREG_CALLTYPE getSuperTypeName(TypeReaderImpl hEntry, rtl_uString** pSuperTypeName) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + if (pEntry != nullptr && pEntry->m_nSuperTypes != 0) { + try { + const char* pTmp = pEntry->m_pCP->readUTF8NameConstant(pEntry->readUINT16(pEntry->m_offset_SUPERTYPES )); //+ (index * sizeof(sal_uInt16)))); + rtl_string2UString( + pSuperTypeName, pTmp, pTmp == nullptr ? 0 : rtl_str_getLength(pTmp), + RTL_TEXTENCODING_UTF8, OSTRING_TO_OUSTRING_CVTFLAGS); + return; + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + rtl_uString_new(pSuperTypeName); +} + +void TYPEREG_CALLTYPE typereg_reader_getDocumentation(void * hEntry, rtl_uString** pDoku) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + if (pEntry != nullptr) { + try { + const char* pTmp = pEntry->m_pCP->readUTF8NameConstant(pEntry->readUINT16(OFFSET_DOKU)); + rtl_string2UString( + pDoku, pTmp, pTmp == nullptr ? 0 : rtl_str_getLength(pTmp), + RTL_TEXTENCODING_UTF8, OSTRING_TO_OUSTRING_CVTFLAGS); + return; + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + rtl_uString_new(pDoku); +} + +void TYPEREG_CALLTYPE typereg_reader_getFileName(void * hEntry, rtl_uString** pFileName) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + if (pEntry != nullptr) { + try { + const char* pTmp = pEntry->m_pCP->readUTF8NameConstant(pEntry->readUINT16(OFFSET_FILENAME)); + rtl_string2UString( + pFileName, pTmp, pTmp == nullptr ? 0 : rtl_str_getLength(pTmp), + RTL_TEXTENCODING_UTF8, OSTRING_TO_OUSTRING_CVTFLAGS); + return; + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + rtl_uString_new(pFileName); +} + + +sal_uInt16 TYPEREG_CALLTYPE typereg_reader_getFieldCount(void * hEntry) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) return 0; + + return pEntry->m_pFields->m_numOfEntries; +} + +static sal_uInt32 TYPEREG_CALLTYPE getFieldCount(TypeReaderImpl hEntry) +{ + return typereg_reader_getFieldCount(hEntry); +} + +void TYPEREG_CALLTYPE typereg_reader_getFieldName(void * hEntry, rtl_uString** pFieldName, sal_uInt16 index) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) + { + rtl_uString_new(pFieldName); + return; + } + const char* pTmp = pEntry->m_pFields->getFieldName(index); + rtl_string2UString( + pFieldName, pTmp, pTmp == nullptr ? 0 : rtl_str_getLength(pTmp), + RTL_TEXTENCODING_UTF8, OSTRING_TO_OUSTRING_CVTFLAGS); +} + +void TYPEREG_CALLTYPE typereg_reader_getFieldTypeName(void * hEntry, rtl_uString** pFieldType, sal_uInt16 index) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) + { + rtl_uString_new(pFieldType); + return; + } + + const char* pTmp = pEntry->m_pFields->getFieldType(index); + rtl_string2UString( + pFieldType, pTmp, pTmp == nullptr ? 0 : rtl_str_getLength(pTmp), + RTL_TEXTENCODING_UTF8, OSTRING_TO_OUSTRING_CVTFLAGS); +} + +RTFieldAccess TYPEREG_CALLTYPE typereg_reader_getFieldFlags(void * hEntry, sal_uInt16 index) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) return RTFieldAccess::INVALID; + + return pEntry->m_pFields->getFieldAccess(index); +} + +bool TYPEREG_CALLTYPE typereg_reader_getFieldValue( + void * hEntry, sal_uInt16 index, RTValueType * type, + RTConstValueUnion * value) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) { + *type = RT_TYPE_NONE; + return true; + } + + try { + *type = pEntry->m_pFields->getFieldConstValue(index, value); + } catch (std::bad_alloc &) { + return false; + } + return true; +} + +static RTValueType TYPEREG_CALLTYPE getFieldConstValue(TypeReaderImpl hEntry, sal_uInt16 index, RTConstValueUnion* value) +{ + RTValueType t = RT_TYPE_NONE; + typereg_reader_getFieldValue(hEntry, index, &t, value); + return t; +} + +void TYPEREG_CALLTYPE typereg_reader_getFieldDocumentation(void * hEntry, rtl_uString** pDoku, sal_uInt16 index) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) + { + rtl_uString_new(pDoku); + return; + } + + const char* pTmp = pEntry->m_pFields->getFieldDoku(index); + rtl_string2UString( + pDoku, pTmp, pTmp == nullptr ? 0 : rtl_str_getLength(pTmp), + RTL_TEXTENCODING_UTF8, OSTRING_TO_OUSTRING_CVTFLAGS); +} + +void TYPEREG_CALLTYPE typereg_reader_getFieldFileName(void * hEntry, rtl_uString** pFieldFileName, sal_uInt16 index) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) + { + rtl_uString_new(pFieldFileName); + return; + } + + const char* pTmp = pEntry->m_pFields->getFieldFileName(index); + rtl_string2UString( + pFieldFileName, pTmp, pTmp == nullptr ? 0 : rtl_str_getLength(pTmp), + RTL_TEXTENCODING_UTF8, OSTRING_TO_OUSTRING_CVTFLAGS); +} + + +sal_uInt16 TYPEREG_CALLTYPE typereg_reader_getMethodCount(void * hEntry) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) return 0; + + return pEntry->m_pMethods->m_numOfEntries; +} + +void TYPEREG_CALLTYPE typereg_reader_getMethodName(void * hEntry, rtl_uString** pMethodName, sal_uInt16 index) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) + { + rtl_uString_new(pMethodName); + return; + } + + const char* pTmp = pEntry->m_pMethods->getMethodName(index); + rtl_string2UString( + pMethodName, pTmp, pTmp == nullptr ? 0 : rtl_str_getLength(pTmp), + RTL_TEXTENCODING_UTF8, OSTRING_TO_OUSTRING_CVTFLAGS); +} + +sal_uInt16 TYPEREG_CALLTYPE typereg_reader_getMethodParameterCount( + void * hEntry, sal_uInt16 index) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) return 0; + + return pEntry->m_pMethods->getMethodParamCount(index); +} + +void TYPEREG_CALLTYPE typereg_reader_getMethodParameterTypeName(void * hEntry, rtl_uString** pMethodParamType, sal_uInt16 index, sal_uInt16 paramIndex) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) + { + rtl_uString_new(pMethodParamType); + return; + } + + const char* pTmp = pEntry->m_pMethods->getMethodParamType(index, paramIndex); + rtl_string2UString( + pMethodParamType, pTmp, pTmp == nullptr ? 0 : rtl_str_getLength(pTmp), + RTL_TEXTENCODING_UTF8, OSTRING_TO_OUSTRING_CVTFLAGS); +} + +void TYPEREG_CALLTYPE typereg_reader_getMethodParameterName(void * hEntry, rtl_uString** pMethodParamName, sal_uInt16 index, sal_uInt16 paramIndex) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) + { + rtl_uString_new(pMethodParamName); + return; + } + + const char* pTmp = pEntry->m_pMethods->getMethodParamName(index, paramIndex); + rtl_string2UString( + pMethodParamName, pTmp, pTmp == nullptr ? 0 : rtl_str_getLength(pTmp), + RTL_TEXTENCODING_UTF8, OSTRING_TO_OUSTRING_CVTFLAGS); +} + +RTParamMode TYPEREG_CALLTYPE typereg_reader_getMethodParameterFlags(void * hEntry, sal_uInt16 index, sal_uInt16 paramIndex) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) return RT_PARAM_INVALID; + + return pEntry->m_pMethods->getMethodParamMode(index, paramIndex); +} + +sal_uInt16 TYPEREG_CALLTYPE typereg_reader_getMethodExceptionCount( + void * hEntry, sal_uInt16 index) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) return 0; + + return pEntry->m_pMethods->getMethodExcCount(index); +} + +void TYPEREG_CALLTYPE typereg_reader_getMethodExceptionTypeName(void * hEntry, rtl_uString** pMethodExcpType, sal_uInt16 index, sal_uInt16 excIndex) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) + { + rtl_uString_new(pMethodExcpType); + return; + } + + const char* pTmp = pEntry->m_pMethods->getMethodExcType(index, excIndex); + rtl_string2UString( + pMethodExcpType, pTmp, pTmp == nullptr ? 0 : rtl_str_getLength(pTmp), + RTL_TEXTENCODING_UTF8, OSTRING_TO_OUSTRING_CVTFLAGS); +} + +void TYPEREG_CALLTYPE typereg_reader_getMethodReturnTypeName(void * hEntry, rtl_uString** pMethodReturnType, sal_uInt16 index) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) + { + rtl_uString_new(pMethodReturnType); + return; + } + + const char* pTmp = pEntry->m_pMethods->getMethodReturnType(index); + rtl_string2UString( + pMethodReturnType, pTmp, pTmp == nullptr ? 0 : rtl_str_getLength(pTmp), + RTL_TEXTENCODING_UTF8, OSTRING_TO_OUSTRING_CVTFLAGS); +} + +RTMethodMode TYPEREG_CALLTYPE typereg_reader_getMethodFlags(void * hEntry, sal_uInt16 index) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) return RTMethodMode::INVALID; + + return pEntry->m_pMethods->getMethodMode(index); +} + +void TYPEREG_CALLTYPE typereg_reader_getMethodDocumentation(void * hEntry, rtl_uString** pMethodDoku, sal_uInt16 index) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) + { + rtl_uString_new(pMethodDoku); + return; + } + + const char* pTmp = pEntry->m_pMethods->getMethodDoku(index); + rtl_string2UString( + pMethodDoku, pTmp, pTmp == nullptr ? 0 : rtl_str_getLength(pTmp), + RTL_TEXTENCODING_UTF8, OSTRING_TO_OUSTRING_CVTFLAGS); +} + +sal_uInt16 TYPEREG_CALLTYPE typereg_reader_getReferenceCount(void * hEntry) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) return 0; + + return pEntry->m_pReferences->m_numOfEntries; +} + +void TYPEREG_CALLTYPE typereg_reader_getReferenceTypeName(void * hEntry, rtl_uString** pReferenceName, sal_uInt16 index) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) + { + rtl_uString_new(pReferenceName); + return; + } + + const char* pTmp = pEntry->m_pReferences->getReferenceName(index); + rtl_string2UString( + pReferenceName, pTmp, pTmp == nullptr ? 0 : rtl_str_getLength(pTmp), + RTL_TEXTENCODING_UTF8, OSTRING_TO_OUSTRING_CVTFLAGS); +} + +RTReferenceType TYPEREG_CALLTYPE typereg_reader_getReferenceSort(void * hEntry, sal_uInt16 index) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) return RTReferenceType::INVALID; + + return pEntry->m_pReferences->getReferenceType(index); +} + +void TYPEREG_CALLTYPE typereg_reader_getReferenceDocumentation(void * hEntry, rtl_uString** pReferenceDoku, sal_uInt16 index) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) + { + rtl_uString_new(pReferenceDoku); + return; + } + + const char* pTmp = pEntry->m_pReferences->getReferenceDoku(index); + rtl_string2UString( + pReferenceDoku, pTmp, pTmp == nullptr ? 0 : rtl_str_getLength(pTmp), + RTL_TEXTENCODING_UTF8, OSTRING_TO_OUSTRING_CVTFLAGS); +} + +RTFieldAccess TYPEREG_CALLTYPE typereg_reader_getReferenceFlags(void * hEntry, sal_uInt16 index) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) return RTFieldAccess::INVALID; + + return pEntry->m_pReferences->getReferenceAccess(index); +} + +sal_uInt16 TYPEREG_CALLTYPE typereg_reader_getSuperTypeCount(void * hEntry) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + + if (pEntry == nullptr) return 0; + + return pEntry->m_nSuperTypes; +} + +void TYPEREG_CALLTYPE typereg_reader_getSuperTypeName( + void * hEntry, rtl_uString ** pSuperTypeName, sal_uInt16 index) +{ + TypeRegistryEntry* pEntry = static_cast<TypeRegistryEntry*>(hEntry); + if (pEntry != nullptr) { + try { + OSL_ASSERT(index < pEntry->m_nSuperTypes); + const char* pTmp = pEntry->m_pCP->readUTF8NameConstant(pEntry->readUINT16(pEntry->m_offset_SUPERTYPES + (index * sizeof(sal_uInt16)))); + rtl_string2UString( + pSuperTypeName, pTmp, pTmp == nullptr ? 0 : rtl_str_getLength(pTmp), + RTL_TEXTENCODING_UTF8, OSTRING_TO_OUSTRING_CVTFLAGS); + return; + } catch (BlopObject::BoundsError &) { + SAL_WARN("registry", "bad data"); + } + } + rtl_uString_new(pSuperTypeName); +} + +RegistryTypeReader::RegistryTypeReader(const sal_uInt8* buffer, + sal_uInt32 bufferLen) + : m_hImpl(nullptr) +{ + m_hImpl = createEntry(buffer, bufferLen); +} + +RegistryTypeReader::~RegistryTypeReader() +{ typereg_reader_release(m_hImpl); } + +RTTypeClass RegistryTypeReader::getTypeClass() const +{ return typereg_reader_getTypeClass(m_hImpl); } + +OUString RegistryTypeReader::getTypeName() const +{ + OUString sRet; + typereg_reader_getTypeName(m_hImpl, &sRet.pData); + return sRet; +} + +OUString RegistryTypeReader::getSuperTypeName() const +{ + OUString sRet; + ::getSuperTypeName(m_hImpl, &sRet.pData); + return sRet; +} + +sal_uInt32 RegistryTypeReader::getFieldCount() const +{ return ::getFieldCount(m_hImpl); } + +OUString RegistryTypeReader::getFieldName( sal_uInt16 index ) const +{ + OUString sRet; + typereg_reader_getFieldName(m_hImpl, &sRet.pData, index); + return sRet; +} + +OUString RegistryTypeReader::getFieldType( sal_uInt16 index ) const +{ + OUString sRet; + typereg_reader_getFieldTypeName(m_hImpl, &sRet.pData, index); + return sRet; +} + +RTFieldAccess RegistryTypeReader::getFieldAccess( sal_uInt16 index ) const +{ return typereg_reader_getFieldFlags(m_hImpl, index); } + +RTConstValue RegistryTypeReader::getFieldConstValue( sal_uInt16 index ) const +{ + RTConstValue ret; + ret.m_type = ::getFieldConstValue(m_hImpl, index, &ret.m_value); + return ret; +} + +OUString RegistryTypeReader::getFieldDoku( sal_uInt16 index ) const +{ + OUString sRet; + typereg_reader_getFieldDocumentation(m_hImpl, &sRet.pData, index); + return sRet; +} + +OUString RegistryTypeReader::getFieldFileName( sal_uInt16 index ) const +{ + OUString sRet; + typereg_reader_getFieldFileName(m_hImpl, &sRet.pData, index); + return sRet; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/registry/source/reflread.hxx b/registry/source/reflread.hxx new file mode 100644 index 000000000..5fc428212 --- /dev/null +++ b/registry/source/reflread.hxx @@ -0,0 +1,114 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_REGISTRY_SOURCE_REFLREAD_HXX +#define INCLUDED_REGISTRY_SOURCE_REFLREAD_HXX + +#include <registry/refltype.hxx> +#include <rtl/ustring.hxx> + +/// Implementation handle +typedef void* TypeReaderImpl; + +/** RegistryTypeReades reads a binary type blob. + + This class provides the necessary functions to read type information + for all kinds of types of a type blob. + + @deprecated + use typereg::Reader instead +*/ +class RegistryTypeReader +{ +public: + + /** Constructor. + + @param buffer points to the binary data block. + @param bufferLen specifies the size of the binary data block. + */ + RegistryTypeReader(const sal_uInt8* buffer, + sal_uInt32 bufferLen); + + /// Destructor. The Destructor frees the data block if the copyData flag was TRUE. + ~RegistryTypeReader(); + + /** returns the typeclass of the type represented by this blob. + + This function will always return the type class without the internal + RT_TYPE_PUBLISHED flag set. + */ + RTTypeClass getTypeClass() const; + + /** returns the full qualified name of the type. + */ + OUString getTypeName() const; + + /** returns the full qualified name of the supertype. + */ + OUString getSuperTypeName() const; + + /** returns the number of fields (attributes/properties, enum values or number + of constants in a module). + + */ + sal_uInt32 getFieldCount() const; + + /** returns the name of the field specified by index. + */ + OUString getFieldName( sal_uInt16 index ) const; + + /** returns the full qualified name of the field specified by index. + */ + OUString getFieldType( sal_uInt16 index ) const; + + /** returns the access mode of the field specified by index. + */ + RTFieldAccess getFieldAccess( sal_uInt16 index ) const; + + /** returns the value of the field specified by index. + + This function returns the value of an enum value or of a constant. + */ + RTConstValue getFieldConstValue( sal_uInt16 index ) const; + + /** returns the documentation string for the field specified by index. + + Each field of a type can have their own documentation. + */ + OUString getFieldDoku( sal_uInt16 index ) const; + + /** returns the IDL filename of the field specified by index. + + The IDL filename of a field can differ from the filename of the ype itself + because modules and also constants can be defined in different IDL files. + */ + OUString getFieldFileName( sal_uInt16 index ) const; + +private: + RegistryTypeReader(RegistryTypeReader const &) = delete; + void operator =(RegistryTypeReader const &) = delete; + + /// stores the handle of an implementation class + TypeReaderImpl m_hImpl; +}; + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/registry/source/reflwrit.cxx b/registry/source/reflwrit.cxx new file mode 100644 index 000000000..8f8e83318 --- /dev/null +++ b/registry/source/reflwrit.cxx @@ -0,0 +1,1336 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include <new> +#include <memory> +#include <algorithm> +#include <sal/types.h> +#include <osl/endian.h> +#include <rtl/string.hxx> +#include <rtl/ustring.hxx> + +#include "reflwrit.hxx" +#include <registry/refltype.hxx> +#include <registry/version.h> +#include <registry/writer.h> + +#include "reflcnst.hxx" + + +namespace { + +OString toByteString(rtl_uString const * str) { + return OString( + str->buffer, str->length, RTL_TEXTENCODING_UTF8, + OUSTRING_TO_OSTRING_CVTFLAGS); +} + +} + +const sal_Unicode NULL_WSTRING[1] = { 0 }; + +#define BLOP_OFFSET_MAGIC 0 +#define BLOP_OFFSET_SIZE (BLOP_OFFSET_MAGIC + sizeof(sal_uInt32)) +#define BLOP_OFFSET_MINOR (BLOP_OFFSET_SIZE + sizeof(sal_uInt32)) +#define BLOP_OFFSET_MAJOR (BLOP_OFFSET_MINOR + sizeof(sal_uInt16)) +#define BLOP_OFFSET_N_ENTRIES (BLOP_OFFSET_MAJOR + sizeof(sal_uInt16)) +#define BLOP_HEADER_N_ENTRIES 6 + +#define BLOP_FIELD_N_ENTRIES 6 + +#define BLOP_METHOD_N_ENTRIES 5 + +#define BLOP_PARAM_N_ENTRIES 3 + +#define BLOP_REFERENCE_N_ENTRIES 4 + +sal_uInt32 UINT16StringLen(const sal_uInt8* wstring) +{ + if (!wstring) return 0; + + const sal_uInt8* b = wstring; + + while (b[0] || b[1]) b += sizeof(sal_uInt16); + + return ((b - wstring) / sizeof(sal_uInt16)); +} + +sal_uInt32 writeString(sal_uInt8* buffer, const sal_Unicode* v) +{ + sal_uInt32 len = rtl_ustr_getLength(v) + 1; + sal_uInt32 i; + sal_uInt8* buff = buffer; + + for (i = 0; i < len; i++) + { + buff += writeUINT16(buff, static_cast<sal_uInt16>(v[i])); + } + + return (buff - buffer); +} + +sal_uInt32 readString(const sal_uInt8* buffer, sal_Unicode* v, sal_uInt32 maxSize) +{ + sal_uInt32 len = UINT16StringLen(buffer) + 1; + sal_uInt32 i; + sal_uInt8* buff = const_cast<sal_uInt8*>(buffer); + + if(len > maxSize / 2) + { + len = maxSize / 2; + } + + for (i = 0; i < (len - 1); i++) + { + sal_uInt16 aChar; + + buff += readUINT16(buff, aChar); + + v[i] = static_cast<sal_Unicode>(aChar); + } + + v[len - 1] = L'\0'; + + return (buff - buffer); +} + +sal_uInt32 writeFloat(sal_uInt8* buffer, float v) +{ + union + { + float v; + sal_uInt32 b; + } x; + + x.v = v; + +#ifdef REGTYPE_IEEE_NATIVE + writeUINT32(buffer, x.b); +#else +# error no IEEE +#endif + + return sizeof(sal_uInt32); +} + +sal_uInt32 writeDouble(sal_uInt8* buffer, double v) +{ + union + { + double v; + struct + { + sal_uInt32 b1; + sal_uInt32 b2; + } b; + } x; + + x.v = v; + +#ifdef REGTYPE_IEEE_NATIVE +# ifdef OSL_BIGENDIAN + writeUINT32(buffer, x.b.b1); + writeUINT32(buffer + sizeof(sal_uInt32), x.b.b2); +# else + writeUINT32(buffer, x.b.b2); + writeUINT32(buffer + sizeof(sal_uInt32), x.b.b1); +# endif +#else +# error no IEEE +#endif + + return (sizeof(sal_uInt32) + sizeof(sal_uInt32)); +} + +/************************************************************************** + + buffer write functions + +**************************************************************************/ + +namespace { + +/************************************************************************** + + struct CPInfo + +**************************************************************************/ + +struct CPInfo +{ + union + { + const char* aUtf8; + RTUik* aUik; + RTConstValueUnion aConst; + } m_value; + struct CPInfo* m_next; + CPInfoTag m_tag; + sal_uInt16 m_index; + + CPInfo(CPInfoTag tag, struct CPInfo* prev); + + sal_uInt32 getBlopSize() const; + + sal_uInt32 toBlop(sal_uInt8* buffer); +}; + +CPInfo::CPInfo(CPInfoTag tag, struct CPInfo* prev) + : m_next(nullptr) + , m_tag(tag) + , m_index(0) +{ + if (prev) + { + m_index = prev->m_index + 1; + prev->m_next = this; + } +} + +sal_uInt32 CPInfo::getBlopSize() const +{ + sal_uInt32 size = sizeof(sal_uInt32) /* size */ + sizeof(sal_uInt16) /* tag */; + + switch (m_tag) + { + case CP_TAG_CONST_BOOL: + size += sizeof(sal_uInt8); + break; + case CP_TAG_CONST_BYTE: + size += sizeof(sal_uInt8); + break; + case CP_TAG_CONST_INT16: + size += sizeof(sal_Int16); + break; + case CP_TAG_CONST_UINT16: + size += sizeof(sal_uInt16); + break; + case CP_TAG_CONST_INT32: + size += sizeof(sal_Int32); + break; + case CP_TAG_CONST_UINT32: + size += sizeof(sal_uInt32); + break; + case CP_TAG_CONST_INT64: + size += sizeof(sal_Int64); + break; + case CP_TAG_CONST_UINT64: + size += sizeof(sal_uInt64); + break; + case CP_TAG_CONST_FLOAT: + size += sizeof(sal_uInt32); + break; + case CP_TAG_CONST_DOUBLE: + size += sizeof(sal_uInt32) + sizeof(sal_uInt32); + break; + case CP_TAG_CONST_STRING: + size += (rtl_ustr_getLength(m_value.aConst.aString) + 1) * sizeof(sal_uInt16); + break; + case CP_TAG_UTF8_NAME: + size += strlen(m_value.aUtf8) + 1; + break; + case CP_TAG_UIK: + size += sizeof(sal_uInt32) + sizeof(sal_uInt16) + sizeof(sal_uInt16) + sizeof(sal_uInt32) + sizeof(sal_uInt32); + break; + default: + break; + } + + return size; +} + + +sal_uInt32 CPInfo::toBlop(sal_uInt8* buffer) +{ + sal_uInt8* buff = buffer; + + buff += writeUINT32(buff, getBlopSize()); + buff += writeUINT16(buff, static_cast<sal_uInt16>(m_tag)); + + switch (m_tag) + { + case CP_TAG_CONST_BOOL: + buff += writeBYTE(buff, static_cast<sal_uInt8>(m_value.aConst.aBool)); + break; + case CP_TAG_CONST_BYTE: + buff += writeBYTE( + buff, static_cast< sal_uInt8 >(m_value.aConst.aByte)); + break; + case CP_TAG_CONST_INT16: + buff += writeINT16(buff, m_value.aConst.aShort); + break; + case CP_TAG_CONST_UINT16: + buff += writeINT16(buff, m_value.aConst.aUShort); + break; + case CP_TAG_CONST_INT32: + buff += writeINT32(buff, m_value.aConst.aLong); + break; + case CP_TAG_CONST_UINT32: + buff += writeUINT32(buff, m_value.aConst.aULong); + break; + case CP_TAG_CONST_INT64: + buff += writeUINT64(buff, m_value.aConst.aHyper); + break; + case CP_TAG_CONST_UINT64: + buff += writeUINT64(buff, m_value.aConst.aUHyper); + break; + case CP_TAG_CONST_FLOAT: + buff += writeFloat(buff, m_value.aConst.aFloat); + break; + case CP_TAG_CONST_DOUBLE: + buff += writeDouble(buff, m_value.aConst.aDouble); + break; + case CP_TAG_CONST_STRING: + buff += writeString(buff, m_value.aConst.aString); + break; + case CP_TAG_UTF8_NAME: + buff += writeUtf8(buff, m_value.aUtf8); + break; + case CP_TAG_UIK: + buff += writeUINT32(buff, m_value.aUik->m_Data1); + buff += writeUINT16(buff, m_value.aUik->m_Data2); + buff += writeUINT16(buff, m_value.aUik->m_Data3); + buff += writeUINT32(buff, m_value.aUik->m_Data4); + buff += writeUINT32(buff, m_value.aUik->m_Data5); + break; + default: + break; + } + + return (buff - buffer); +} + + +/************************************************************************** + + class FieldEntry + +**************************************************************************/ + +class FieldEntry +{ + +public: + + OString m_name; + OString m_typeName; + OString m_doku; + OString m_fileName; + RTFieldAccess m_access; + RTValueType m_constValueType; + RTConstValueUnion m_constValue; + + FieldEntry(); + ~FieldEntry(); + + void setData(const OString& name, + const OString& typeName, + const OString& doku, + const OString& fileName, + RTFieldAccess access, + RTValueType constValueType, + RTConstValueUnion constValue); + // throws std::bad_alloc +}; + +FieldEntry::FieldEntry() + : m_access(RTFieldAccess::INVALID) + , m_constValueType(RT_TYPE_NONE) +{ +} + +FieldEntry::~FieldEntry() +{ + if ( + (m_constValueType == RT_TYPE_STRING) && + m_constValue.aString && + (m_constValue.aString != NULL_WSTRING) + ) + { + delete[] m_constValue.aString; + } +} + +void FieldEntry::setData(const OString& name, + const OString& typeName, + const OString& doku, + const OString& fileName, + RTFieldAccess access, + RTValueType constValueType, + RTConstValueUnion constValue) +{ + std::unique_ptr<sal_Unicode[]> newValue; + if (constValueType == RT_TYPE_STRING && constValue.aString != nullptr) { + sal_Int32 n = rtl_ustr_getLength(constValue.aString) + 1; + newValue.reset(new sal_Unicode[n]); + memcpy(newValue.get(), constValue.aString, n * sizeof (sal_Unicode)); + } + + m_name = name; + m_typeName = typeName; + m_doku = doku; + m_fileName = fileName; + + if ( + (m_constValueType == RT_TYPE_STRING) && + m_constValue.aString && + (m_constValue.aString != NULL_WSTRING) + ) + { + delete[] m_constValue.aString; + } + + m_access = access; + m_constValueType = constValueType; + + if (m_constValueType == RT_TYPE_STRING) + { + if (constValue.aString == nullptr) + m_constValue.aString = NULL_WSTRING; + else + { + m_constValue.aString = newValue.release(); + } + } + else + { + m_constValue = constValue; + } +} + +/************************************************************************** + + class ParamEntry + +**************************************************************************/ + +class ParamEntry +{ +public: + + OString m_typeName; + OString m_name; + RTParamMode m_mode; + + ParamEntry(); + + void setData(const OString& typeName, + const OString& name, + RTParamMode mode); +}; + +ParamEntry::ParamEntry() + : m_mode(RT_PARAM_INVALID) +{ +} + +void ParamEntry::setData(const OString& typeName, + const OString& name, + RTParamMode mode) +{ + m_name = name; + m_typeName = typeName; + m_mode = mode; +} + +/************************************************************************** + + class ReferenceEntry + +**************************************************************************/ + +class ReferenceEntry +{ +public: + + OString m_name; + OString m_doku; + RTReferenceType m_type; + RTFieldAccess m_access; + + ReferenceEntry(); + + void setData(const OString& name, + RTReferenceType refType, + const OString& doku, + RTFieldAccess access); +}; + +ReferenceEntry::ReferenceEntry() + : m_type(RTReferenceType::INVALID) + , m_access(RTFieldAccess::INVALID) +{ +} + +void ReferenceEntry::setData(const OString& name, + RTReferenceType refType, + const OString& doku, + RTFieldAccess access) +{ + m_name = name; + m_doku = doku; + m_type = refType; + m_access = access; +} + +/************************************************************************** + + class MethodEntry + +**************************************************************************/ + +class MethodEntry +{ +public: + + OString m_name; + OString m_returnTypeName; + RTMethodMode m_mode; + sal_uInt16 m_paramCount; + std::unique_ptr<ParamEntry[]> m_params; + sal_uInt16 m_excCount; + std::unique_ptr<OString[]> m_excNames; + OString m_doku; + + MethodEntry(); + + void setData(const OString& name, + const OString& returnTypeName, + RTMethodMode mode, + sal_uInt16 paramCount, + sal_uInt16 excCount, + const OString& doku); + + void setExcName(sal_uInt16 excIndex, const OString& name) const; + +protected: + + void reallocParams(sal_uInt16 size); + void reallocExcs(sal_uInt16 size); +}; + +MethodEntry::MethodEntry() + : m_mode(RTMethodMode::INVALID) + , m_paramCount(0) + , m_excCount(0) +{ +} + +void MethodEntry::setData(const OString& name, + const OString& returnTypeName, + RTMethodMode mode, + sal_uInt16 paramCount, + sal_uInt16 excCount, + const OString& doku) +{ + m_name = name; + m_returnTypeName = returnTypeName; + m_doku = doku; + + m_mode = mode; + + reallocParams(paramCount); + reallocExcs(excCount); +} + +void MethodEntry::setExcName(sal_uInt16 excIndex, const OString& name) const +{ + if (excIndex < m_excCount) + { + m_excNames[excIndex] = name; + } +} + +void MethodEntry::reallocParams(sal_uInt16 size) +{ + ParamEntry* newParams; + + if (size) + newParams = new ParamEntry[size]; + else + newParams = nullptr; + + if (m_paramCount) + { + sal_uInt16 i; + sal_uInt16 mn = std::min(size, m_paramCount); + + for (i = 0; i < mn; i++) + { + newParams[i].setData(m_params[i].m_typeName, m_params[i].m_name, m_params[i].m_mode); + } + + m_params.reset(); + } + + m_paramCount = size; + m_params.reset( newParams ); +} + +void MethodEntry::reallocExcs(sal_uInt16 size) +{ + OString* newExcNames; + + if (size) + newExcNames = new OString[size]; + else + newExcNames = nullptr; + + sal_uInt16 i; + sal_uInt16 mn = std::min(size, m_excCount); + + for (i = 0; i < mn; i++) + { + newExcNames[i] = m_excNames[i]; + } + + m_excCount = size; + m_excNames.reset( newExcNames ); +} + + +/************************************************************************** + + class TypeRegistryEntry + +**************************************************************************/ + +class TypeWriter +{ + +public: + + sal_uInt32 m_refCount; + typereg_Version m_version; + RTTypeClass m_typeClass; + OString m_typeName; + sal_uInt16 m_nSuperTypes; + std::unique_ptr<OString[]> + m_superTypeNames; + OString m_doku; + OString m_fileName; + sal_uInt16 m_fieldCount; + FieldEntry* m_fields; + sal_uInt16 m_methodCount; + MethodEntry* m_methods; + sal_uInt16 m_referenceCount; + ReferenceEntry* m_references; + + std::unique_ptr<sal_uInt8[]> m_blop; + sal_uInt32 m_blopSize; + + TypeWriter(typereg_Version version, + OString const & documentation, + OString const & fileName, + RTTypeClass RTTypeClass, + bool published, + const OString& typeName, + sal_uInt16 superTypeCount, + sal_uInt16 FieldCount, + sal_uInt16 methodCount, + sal_uInt16 referenceCount); + + ~TypeWriter(); + + void setSuperType(sal_uInt16 index, OString const & name) const; + + void createBlop(); // throws std::bad_alloc +}; + +TypeWriter::TypeWriter(typereg_Version version, + OString const & documentation, + OString const & fileName, + RTTypeClass RTTypeClass, + bool published, + const OString& typeName, + sal_uInt16 superTypeCount, + sal_uInt16 fieldCount, + sal_uInt16 methodCount, + sal_uInt16 referenceCount) + : m_refCount(1) + , m_version(version) + , m_typeClass( + static_cast< enum RTTypeClass >( + RTTypeClass | (published ? RT_TYPE_PUBLISHED : 0))) + , m_typeName(typeName) + , m_nSuperTypes(superTypeCount) + , m_doku(documentation) + , m_fileName(fileName) + , m_fieldCount(fieldCount) + , m_fields(nullptr) + , m_methodCount(methodCount) + , m_methods(nullptr) + , m_referenceCount(referenceCount) + , m_references(nullptr) + , m_blopSize(0) +{ + if (m_nSuperTypes > 0) + { + m_superTypeNames.reset( new OString[m_nSuperTypes] ); + } + + if (m_fieldCount) + m_fields = new FieldEntry[fieldCount]; + + if (m_methodCount) + m_methods = new MethodEntry[methodCount]; + + if (m_referenceCount) + m_references = new ReferenceEntry[referenceCount]; +} + +TypeWriter::~TypeWriter() +{ + if (m_fieldCount) + delete[] m_fields; + + if (m_methodCount) + delete[] m_methods; + + if (m_referenceCount) + delete[] m_references; +} + +void TypeWriter::setSuperType(sal_uInt16 index, OString const & name) const +{ + m_superTypeNames[index] = name; +} + +void TypeWriter::createBlop() +{ + //TODO: Fix memory leaks that occur when std::bad_alloc is thrown + + std::unique_ptr<sal_uInt8[]> pBlopFields; + std::unique_ptr<sal_uInt8[]> pBlopMethods; + std::unique_ptr<sal_uInt8[]> pBlopReferences; + sal_uInt8* pBuffer = nullptr; + sal_uInt32 blopFieldsSize = 0; + sal_uInt32 blopMethodsSize = 0; + sal_uInt32 blopReferenceSize = 0; + + CPInfo root(CP_TAG_INVALID, nullptr); + sal_uInt16 cpIndexThisName = 0; + std::unique_ptr<sal_uInt16[]> cpIndexSuperNames; + sal_uInt16 cpIndexDoku = 0; + sal_uInt16 cpIndexFileName = 0; + CPInfo* pInfo = nullptr; + + sal_uInt16 entrySize = sizeof(sal_uInt16); + sal_uInt32 blopHeaderEntrySize = BLOP_OFFSET_N_ENTRIES + entrySize + (BLOP_HEADER_N_ENTRIES * entrySize); + sal_uInt32 blopFieldEntrySize = BLOP_FIELD_N_ENTRIES * entrySize; + sal_uInt32 blopMethodEntrySize = BLOP_METHOD_N_ENTRIES * entrySize; + sal_uInt32 blopParamEntrySize = BLOP_PARAM_N_ENTRIES * entrySize; + sal_uInt32 blopReferenceEntrySize = BLOP_REFERENCE_N_ENTRIES * entrySize; + + sal_uInt32 blopSize = blopHeaderEntrySize; + + // create CP entry for this name + pInfo = new CPInfo(CP_TAG_UTF8_NAME, &root); + pInfo->m_value.aUtf8 = m_typeName.getStr(); + cpIndexThisName = pInfo->m_index; + + // nSuperTypes + blopSize += entrySize; + + // create CP entry for super names + if (m_nSuperTypes) + { + blopSize += m_nSuperTypes * entrySize; + + cpIndexSuperNames.reset(new sal_uInt16[m_nSuperTypes]); + + for (sal_uInt32 i=0; i < m_nSuperTypes; i++) + { + pInfo = new CPInfo(CP_TAG_UTF8_NAME, pInfo); + pInfo->m_value.aUtf8 = m_superTypeNames[i].getStr(); + cpIndexSuperNames[i] = pInfo->m_index; + } + } + + // create CP entry for doku + if (!m_doku.isEmpty()) + { + pInfo = new CPInfo(CP_TAG_UTF8_NAME, pInfo); + pInfo->m_value.aUtf8 = m_doku.getStr(); + cpIndexDoku = pInfo->m_index; + } + + // create CP entry for idl source filename + if (!m_fileName.isEmpty()) + { + pInfo = new CPInfo(CP_TAG_UTF8_NAME, pInfo); + pInfo->m_value.aUtf8 = m_fileName.getStr(); + cpIndexFileName = pInfo->m_index; + } + + // fields blop + blopSize += sizeof(sal_uInt16); // fieldCount + nFieldEntries + + if (m_fieldCount) + { + sal_uInt16 cpIndexName = 0; + sal_uInt16 cpIndexTypeName = 0; + sal_uInt16 cpIndexValue = 0; + sal_uInt16 cpIndexDoku2 = 0; + sal_uInt16 cpIndexFileName2 = 0; + + // nFieldEntries + n fields + blopFieldsSize = sizeof(sal_uInt16) + (m_fieldCount * blopFieldEntrySize); + + blopSize += blopFieldsSize; + + pBlopFields.reset(new sal_uInt8[blopFieldsSize]); + pBuffer = pBlopFields.get(); + + pBuffer += writeUINT16(pBuffer, BLOP_FIELD_N_ENTRIES); + + for (sal_uInt16 i = 0; i < m_fieldCount; i++) + { + cpIndexName = 0; + cpIndexTypeName = 0; + cpIndexValue = 0; + cpIndexDoku2 = 0; + cpIndexFileName2 = 0; + + pBuffer += writeUINT16(pBuffer, static_cast<sal_uInt16>(m_fields[i].m_access)); + + if (!m_fields[i].m_name.isEmpty()) + { + pInfo = new CPInfo(CP_TAG_UTF8_NAME, pInfo); + pInfo->m_value.aUtf8 = m_fields[i].m_name.getStr(); + cpIndexName = pInfo->m_index; + } + pBuffer += writeUINT16(pBuffer, cpIndexName); + + if (!m_fields[i].m_typeName.isEmpty()) + { + pInfo = new CPInfo(CP_TAG_UTF8_NAME, pInfo); + pInfo->m_value.aUtf8 = m_fields[i].m_typeName.getStr(); + cpIndexTypeName = pInfo->m_index; + } + pBuffer += writeUINT16(pBuffer, cpIndexTypeName); + + if (m_fields[i].m_constValueType != RT_TYPE_NONE) + { + pInfo = new CPInfo(static_cast<CPInfoTag>(m_fields[i].m_constValueType), pInfo); + pInfo->m_value.aConst = m_fields[i].m_constValue; + cpIndexValue = pInfo->m_index; + } + pBuffer += writeUINT16(pBuffer, cpIndexValue); + + if (!m_fields[i].m_doku.isEmpty()) + { + pInfo = new CPInfo(CP_TAG_UTF8_NAME, pInfo); + pInfo->m_value.aUtf8 = m_fields[i].m_doku.getStr(); + cpIndexDoku2 = pInfo->m_index; + } + pBuffer += writeUINT16(pBuffer, cpIndexDoku2); + + if (!m_fields[i].m_fileName.isEmpty()) + { + pInfo = new CPInfo(CP_TAG_UTF8_NAME, pInfo); + pInfo->m_value.aUtf8 = m_fields[i].m_fileName.getStr(); + cpIndexFileName2 = pInfo->m_index; + } + pBuffer += writeUINT16(pBuffer, cpIndexFileName2); + } + } + + // methods blop + blopSize += sizeof(sal_uInt16); // methodCount + + if (m_methodCount) + { + std::unique_ptr<sal_uInt16[]> pMethodEntrySize( new sal_uInt16[m_methodCount] ); + sal_uInt16 cpIndexName = 0; + sal_uInt16 cpIndexReturn = 0; + sal_uInt16 cpIndexDoku2 = 0; + + // nMethodEntries + nParamEntries + blopMethodsSize = (2 * sizeof(sal_uInt16)); + + for (sal_uInt16 i = 0; i < m_methodCount; i++) + { + pMethodEntrySize[i] = static_cast<sal_uInt16>( blopMethodEntrySize + // header + sizeof(sal_uInt16) + // parameterCount + (m_methods[i].m_paramCount * blopParamEntrySize) + // exceptions + sizeof(sal_uInt16) + // exceptionCount + (m_methods[i].m_excCount * sizeof(sal_uInt16)) ); // exceptions + + blopMethodsSize += pMethodEntrySize[i]; + } + + pBlopMethods.reset(new sal_uInt8[blopMethodsSize]); + + blopSize += blopMethodsSize; + + pBuffer = pBlopMethods.get(); + + pBuffer += writeUINT16(pBuffer, BLOP_METHOD_N_ENTRIES); + pBuffer += writeUINT16(pBuffer, BLOP_PARAM_N_ENTRIES ); + + for (sal_uInt16 i = 0; i < m_methodCount; i++) + { + cpIndexReturn = 0; + cpIndexDoku2 = 0; + + pBuffer += writeUINT16(pBuffer, pMethodEntrySize[i]); + pBuffer += writeUINT16( + pBuffer, + sal::static_int_cast< sal_uInt16 >(m_methods[i].m_mode)); + + if (!m_methods[i].m_name.isEmpty()) + { + pInfo = new CPInfo(CP_TAG_UTF8_NAME, pInfo); + pInfo->m_value.aUtf8 = m_methods[i].m_name.getStr(); + cpIndexName = pInfo->m_index; + } + pBuffer += writeUINT16(pBuffer, cpIndexName); + cpIndexName = 0; + + if (!m_methods[i].m_returnTypeName.isEmpty()) + { + pInfo = new CPInfo(CP_TAG_UTF8_NAME, pInfo); + pInfo->m_value.aUtf8 = m_methods[i].m_returnTypeName.getStr(); + cpIndexReturn = pInfo->m_index; + } + pBuffer += writeUINT16(pBuffer, cpIndexReturn); + + if (!m_methods[i].m_doku.isEmpty()) + { + pInfo = new CPInfo(CP_TAG_UTF8_NAME, pInfo); + pInfo->m_value.aUtf8 = m_methods[i].m_doku.getStr(); + cpIndexDoku2 = pInfo->m_index; + } + pBuffer += writeUINT16(pBuffer, cpIndexDoku2); + + sal_uInt16 j; + + pBuffer += writeUINT16(pBuffer, m_methods[i].m_paramCount); + + for (j = 0; j < m_methods[i].m_paramCount; j++) + { + if (!m_methods[i].m_params[j].m_typeName.isEmpty()) + { + pInfo = new CPInfo(CP_TAG_UTF8_NAME, pInfo); + pInfo->m_value.aUtf8 = m_methods[i].m_params[j].m_typeName.getStr(); + cpIndexName = pInfo->m_index; + } + pBuffer += writeUINT16(pBuffer, cpIndexName); + cpIndexName = 0; + + pBuffer += writeUINT16( + pBuffer, + sal::static_int_cast< sal_uInt16 >( + m_methods[i].m_params[j].m_mode)); + + if (!m_methods[i].m_params[j].m_name.isEmpty()) + { + pInfo = new CPInfo(CP_TAG_UTF8_NAME, pInfo); + pInfo->m_value.aUtf8 = m_methods[i].m_params[j].m_name.getStr(); + cpIndexName = pInfo->m_index; + } + pBuffer += writeUINT16(pBuffer, cpIndexName); + cpIndexName = 0; + } + + pBuffer += writeUINT16(pBuffer, m_methods[i].m_excCount); + + for (j = 0; j < m_methods[i].m_excCount; j++) + { + if (!m_methods[i].m_excNames[j].isEmpty()) + { + pInfo = new CPInfo(CP_TAG_UTF8_NAME, pInfo); + pInfo->m_value.aUtf8 = m_methods[i].m_excNames[j].getStr(); + cpIndexName = pInfo->m_index; + } + pBuffer += writeUINT16(pBuffer, cpIndexName); + cpIndexName = 0; + } + } + } + + // reference blop + blopSize += entrySize; // referenceCount + + if (m_referenceCount) + { + sal_uInt16 cpIndexName = 0; + sal_uInt16 cpIndexDoku2 = 0; + + // nReferenceEntries + n references + blopReferenceSize = entrySize + (m_referenceCount * blopReferenceEntrySize); + + blopSize += blopReferenceSize; + + pBlopReferences.reset(new sal_uInt8[blopReferenceSize]); + pBuffer = pBlopReferences.get(); + + pBuffer += writeUINT16(pBuffer, BLOP_REFERENCE_N_ENTRIES); + + for (sal_uInt16 i = 0; i < m_referenceCount; i++) + { + pBuffer += writeUINT16( + pBuffer, + sal::static_int_cast< sal_uInt16 >(m_references[i].m_type)); + + cpIndexName = 0; + cpIndexDoku2 = 0; + + if (!m_references[i].m_name.isEmpty()) + { + pInfo = new CPInfo(CP_TAG_UTF8_NAME, pInfo); + pInfo->m_value.aUtf8 = m_references[i].m_name.getStr(); + cpIndexName = pInfo->m_index; + } + pBuffer += writeUINT16(pBuffer, cpIndexName); + + if (!m_references[i].m_doku.isEmpty()) + { + pInfo = new CPInfo(CP_TAG_UTF8_NAME, pInfo); + pInfo->m_value.aUtf8 = m_references[i].m_doku.getStr(); + cpIndexDoku2 = pInfo->m_index; + } + pBuffer += writeUINT16(pBuffer, cpIndexDoku2); + + pBuffer += writeUINT16(pBuffer, static_cast<sal_uInt16>(m_references[i].m_access)); + } + } + + + // get CP infos blop-length + pInfo = root.m_next; + sal_uInt32 cpBlopSize = 0; + sal_uInt16 cpCount = 0; + + while (pInfo) + { + cpBlopSize += pInfo->getBlopSize(); + cpCount++; + pInfo = pInfo->m_next; + } + + blopSize += cpBlopSize; + blopSize += sizeof(sal_uInt16); // constantPoolCount + + // write all in flat buffer + + sal_uInt8 * blop = new sal_uInt8[blopSize]; + + pBuffer = blop; + + // Assumes two's complement arithmetic with modulo-semantics: + pBuffer += writeUINT32(pBuffer, magic + m_version); + pBuffer += writeUINT32(pBuffer, blopSize); + pBuffer += writeUINT16(pBuffer, minorVersion); + pBuffer += writeUINT16(pBuffer, majorVersion); + pBuffer += writeUINT16(pBuffer, BLOP_HEADER_N_ENTRIES); + + pBuffer += writeUINT16(pBuffer, sal_uInt16(RT_UNO_IDL)); + pBuffer += writeUINT16(pBuffer, static_cast<sal_uInt16>(m_typeClass)); + pBuffer += writeUINT16(pBuffer, cpIndexThisName); + pBuffer += writeUINT16(pBuffer, 0); // cpIndexUik + pBuffer += writeUINT16(pBuffer, cpIndexDoku); + pBuffer += writeUINT16(pBuffer, cpIndexFileName); + + // write supertypes + pBuffer += writeUINT16(pBuffer, m_nSuperTypes); + if (m_nSuperTypes) + { + for (sal_uInt32 i=0; i < m_nSuperTypes; i++) + { + pBuffer += writeUINT16(pBuffer, cpIndexSuperNames[i]); + } + cpIndexSuperNames.reset(); + } + + pBuffer += writeUINT16(pBuffer, cpCount); + + // write and delete CP infos + pInfo = root.m_next; + + while (pInfo) + { + CPInfo* pNextInfo = pInfo->m_next; + + pBuffer += pInfo->toBlop(pBuffer); + delete pInfo; + + pInfo = pNextInfo; + } + + auto writeList = [&pBuffer]( + sal_uInt16 count, sal_uInt8 * data, sal_uInt32 size) + { + pBuffer += writeUINT16(pBuffer, count); + if (size != 0) { + memcpy(pBuffer, data, size); + pBuffer += size; + } + }; + + // write fields + writeList(m_fieldCount, pBlopFields.get(), blopFieldsSize); + + // write methods + writeList(m_methodCount, pBlopMethods.get(), blopMethodsSize); + + // write references + writeList(m_referenceCount, pBlopReferences.get(), blopReferenceSize); + + m_blop.reset( blop ); + m_blopSize = blopSize; +} + +} // unnamed namespace + +/************************************************************************** + + C-API + +**************************************************************************/ + +extern "C" { + +static void TYPEREG_CALLTYPE release(TypeWriterImpl hEntry) +{ + TypeWriter* pEntry = static_cast<TypeWriter*>(hEntry); + + if (pEntry != nullptr) + { + if (--pEntry->m_refCount == 0) + delete pEntry; + } +} + +sal_Bool TYPEREG_CALLTYPE typereg_writer_setFieldData( + void * handle, sal_uInt16 index, rtl_uString const * documentation, + rtl_uString const * fileName, RTFieldAccess flags, rtl_uString const * name, + rtl_uString const * typeName, RTValueType valueType, + RTConstValueUnion valueValue) + SAL_THROW_EXTERN_C() +{ + try { + static_cast< TypeWriter * >(handle)->m_fields[index].setData( + toByteString(name), toByteString(typeName), + toByteString(documentation), toByteString(fileName), flags, + valueType, valueValue); + } catch (std::bad_alloc &) { + return false; + } + return true; +} + +static void TYPEREG_CALLTYPE setFieldData(TypeWriterImpl hEntry, + sal_uInt16 index, + rtl_uString const * name, + rtl_uString const * typeName, + rtl_uString const * doku, + rtl_uString const * fileName, + RTFieldAccess access, + RTValueType valueType, + RTConstValueUnion constValue) +{ + typereg_writer_setFieldData( + hEntry, index, doku, fileName, access, name, typeName, valueType, + constValue); +} + +sal_Bool TYPEREG_CALLTYPE typereg_writer_setMethodData( + void * handle, sal_uInt16 index, rtl_uString const * documentation, + RTMethodMode flags, rtl_uString const * name, + rtl_uString const * returnTypeName, sal_uInt16 parameterCount, + sal_uInt16 exceptionCount) + SAL_THROW_EXTERN_C() +{ + try { + static_cast< TypeWriter * >(handle)->m_methods[index].setData( + toByteString(name), toByteString(returnTypeName), flags, + parameterCount, exceptionCount, toByteString(documentation)); + } catch (std::bad_alloc &) { + return false; + } + return true; +} + +sal_Bool TYPEREG_CALLTYPE typereg_writer_setMethodParameterData( + void const * handle, sal_uInt16 methodIndex, sal_uInt16 parameterIndex, + RTParamMode flags, rtl_uString const * name, rtl_uString const * typeName) + SAL_THROW_EXTERN_C() +{ + try { + static_cast< TypeWriter const * >(handle)-> + m_methods[methodIndex].m_params[parameterIndex].setData( + toByteString(typeName), toByteString(name), flags); + } catch (std::bad_alloc &) { + return false; + } + return true; +} + +sal_Bool TYPEREG_CALLTYPE typereg_writer_setMethodExceptionTypeName( + void const * handle, sal_uInt16 methodIndex, sal_uInt16 exceptionIndex, + rtl_uString const * typeName) + SAL_THROW_EXTERN_C() +{ + try { + static_cast< TypeWriter const * >(handle)->m_methods[methodIndex].setExcName( + exceptionIndex, toByteString(typeName)); + } catch (std::bad_alloc &) { + return false; + } + return true; +} + +void const * TYPEREG_CALLTYPE typereg_writer_getBlob(void * handle, sal_uInt32 * size) + SAL_THROW_EXTERN_C() +{ + TypeWriter * writer = static_cast< TypeWriter * >(handle); + if (!writer->m_blop) { + try { + writer->createBlop(); + } catch (std::bad_alloc &) { + *size = 0; + return nullptr; + } + } + *size = writer->m_blopSize; + return writer->m_blop.get(); +} + +static const sal_uInt8* TYPEREG_CALLTYPE getBlop(TypeWriterImpl hEntry) +{ + sal_uInt32 size; + return static_cast< sal_uInt8 const * >( + typereg_writer_getBlob(hEntry, &size)); +} + +static sal_uInt32 TYPEREG_CALLTYPE getBlopSize(TypeWriterImpl hEntry) +{ + sal_uInt32 size; + typereg_writer_getBlob(hEntry, &size); + return size; +} + +sal_Bool TYPEREG_CALLTYPE typereg_writer_setReferenceData( + void * handle, sal_uInt16 index, rtl_uString const * documentation, + RTReferenceType sort, RTFieldAccess flags, rtl_uString const * typeName) + SAL_THROW_EXTERN_C() +{ + try { + static_cast< TypeWriter * >(handle)->m_references[index].setData( + toByteString(typeName), sort, toByteString(documentation), flags); + } catch (std::bad_alloc &) { + return false; + } + return true; +} + +void * TYPEREG_CALLTYPE typereg_writer_create( + typereg_Version version, rtl_uString const * documentation, + rtl_uString const * fileName, RTTypeClass typeClass, sal_Bool published, + rtl_uString const * typeName, sal_uInt16 superTypeCount, + sal_uInt16 fieldCount, sal_uInt16 methodCount, sal_uInt16 referenceCount) + SAL_THROW_EXTERN_C() +{ + try { + return new TypeWriter( + version, toByteString(documentation), toByteString(fileName), + typeClass, published, toByteString(typeName), superTypeCount, + fieldCount, methodCount, referenceCount); + } catch (std::bad_alloc &) { + return nullptr; + } +} + +void TYPEREG_CALLTYPE typereg_writer_destroy(void * handle) SAL_THROW_EXTERN_C() { + delete static_cast< TypeWriter * >(handle); +} + +sal_Bool TYPEREG_CALLTYPE typereg_writer_setSuperTypeName( + void const * handle, sal_uInt16 index, rtl_uString const * typeName) + SAL_THROW_EXTERN_C() +{ + try { + static_cast< TypeWriter const * >(handle)->setSuperType( + index, toByteString(typeName)); + } catch (std::bad_alloc &) { + return false; + } + return true; +} + +static TypeWriterImpl TYPEREG_CALLTYPE createEntry( + RTTypeClass typeClass, rtl_uString const * typeName, rtl_uString const * superTypeName, + sal_uInt16 fieldCount) +{ + OUString empty; + sal_uInt16 superTypeCount = rtl_uString_getLength(superTypeName) == 0 + ? 0 : 1; + TypeWriterImpl t = typereg_writer_create( + TYPEREG_VERSION_0, empty.pData, empty.pData, typeClass, false, typeName, + superTypeCount, fieldCount, 0/*methodCount*/, 0/*referenceCount*/); + if (superTypeCount > 0) { + typereg_writer_setSuperTypeName(t, 0, superTypeName); + } + return t; +} + +} + +RegistryTypeWriter::RegistryTypeWriter(RTTypeClass RTTypeClass, + const OUString& typeName, + const OUString& superTypeName, + sal_uInt16 fieldCount) + : m_hImpl(nullptr) +{ + m_hImpl = createEntry(RTTypeClass, + typeName.pData, + superTypeName.pData, + fieldCount); +} + +RegistryTypeWriter::~RegistryTypeWriter() +{ + release(m_hImpl); +} + +void RegistryTypeWriter::setFieldData( sal_uInt16 index, + const OUString& name, + const OUString& typeName, + const OUString& doku, + const OUString& fileName, + RTFieldAccess access, + const RTConstValue& constValue) +{ + ::setFieldData(m_hImpl, index, name.pData, typeName.pData, doku.pData, fileName.pData, access, constValue.m_type, constValue.m_value); +} + +const sal_uInt8* RegistryTypeWriter::getBlop() +{ + return ::getBlop(m_hImpl); +} + +sal_uInt32 RegistryTypeWriter::getBlopSize() +{ + return ::getBlopSize(m_hImpl); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/registry/source/reflwrit.hxx b/registry/source/reflwrit.hxx new file mode 100644 index 000000000..93cee0697 --- /dev/null +++ b/registry/source/reflwrit.hxx @@ -0,0 +1,103 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_REGISTRY_SOURCE_REFLWRIT_HXX +#define INCLUDED_REGISTRY_SOURCE_REFLWRIT_HXX + +#include <registry/types.hxx> +#include <rtl/ustring.hxx> + +class RTConstValue; + +/// Implementation handle +typedef void* TypeWriterImpl; + +/** RegistryTypeWriter writes/creates a binary type blob. + + This class provides the necessary functions to write type information + for all kinds of types into a blob. + + @deprecated + use typereg::Writer instead +*/ +class RegistryTypeWriter +{ +public: + + /** Constructor. + + @param RTTypeClass specifies the type of the new blob. + @param typeName specifies the full qualified type name with '/' as separator. + @param superTypeName specifies the full qualified type name of the base type + with '/' as separator. + @param fieldCount specifies the number of fields (eg. number of attributes/properties, + enum values or constants). + */ + RegistryTypeWriter(RTTypeClass RTTypeClass, + const OUString& typeName, + const OUString& superTypeName, + sal_uInt16 fieldCount); + + /** Destructor. The Destructor frees the internal data block. + + The pointer (returned by getBlop) will be set to NULL. + */ + ~RegistryTypeWriter(); + + /** sets the data for a field member of a type blob. + + @param index indicates the index of the field. + @param name specifies the name. + @param typeName specifies the full qualified typename. + @param doku specifies the documentation string of the field. + @param fileName specifies the name of the IDL file where the field is defined. + @param access specifies the access mode of the field. + @param constValue specifies the value of the field. The value is only interesting + for enum values or constants. + */ + void setFieldData( sal_uInt16 index, + const OUString& name, + const OUString& typeName, + const OUString& doku, + const OUString& fileName, + RTFieldAccess access, + const RTConstValue& constValue); + + /** returns a pointer to the new type blob. + + The pointer will be invalid (NULL) if the instance of + the RegistryTypeWriter will be destroyed. + */ + const sal_uInt8* getBlop(); + + /** returns the size of the new type blob in bytes. + */ + sal_uInt32 getBlopSize(); + +private: + RegistryTypeWriter(RegistryTypeWriter const &) = delete; + void operator =(RegistryTypeWriter const &) = delete; + + /// stores the handle of an implementation class + TypeWriterImpl m_hImpl; +}; + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/registry/source/regimpl.cxx b/registry/source/regimpl.cxx new file mode 100644 index 000000000..0d69c64cd --- /dev/null +++ b/registry/source/regimpl.cxx @@ -0,0 +1,1577 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include "regimpl.hxx" + +#include <cstddef> +#include <memory> +#include <set> +#include <string_view> +#include <vector> +#include <string.h> +#include <stdio.h> + +#if defined(UNX) +#include <unistd.h> +#endif + +#include "reflread.hxx" + +#include "reflwrit.hxx" + +#include <registry/reader.hxx> +#include <registry/refltype.hxx> +#include <registry/types.hxx> + +#include "reflcnst.hxx" +#include "keyimpl.hxx" + +#include <osl/thread.h> +#include <rtl/ustring.hxx> +#include <rtl/ustrbuf.hxx> +#include <osl/file.hxx> + +using namespace osl; +using namespace store; + + +namespace { + +void printString(std::u16string_view s) { + printf("\""); + for (std::size_t i = 0; i < s.size(); ++i) { + sal_Unicode c = s[i]; + if (c == '"' || c == '\\') { + printf("\\%c", static_cast< char >(c)); + } else if (s[i] >= ' ' && s[i] <= '~') { + printf("%c", static_cast< char >(c)); + } else { + printf("\\u%04X", static_cast< unsigned int >(c)); + } + } + printf("\""); +} + +void printFieldOrReferenceFlag( + RTFieldAccess * flags, RTFieldAccess flag, char const * name, bool * first) +{ + if ((*flags & flag) != RTFieldAccess::NONE) { + if (!*first) { + printf("|"); + } + *first = false; + printf("%s", name); + *flags &= ~flag; + } +} + +void printFieldOrReferenceFlags(RTFieldAccess flags) { + if (flags == RTFieldAccess::NONE) { + printf("none"); + } else { + bool first = true; + printFieldOrReferenceFlag( + &flags, RTFieldAccess::READONLY, "readonly", &first); + printFieldOrReferenceFlag( + &flags, RTFieldAccess::OPTIONAL, "optional", &first); + printFieldOrReferenceFlag( + &flags, RTFieldAccess::MAYBEVOID, "maybevoid", &first); + printFieldOrReferenceFlag(&flags, RTFieldAccess::BOUND, "bound", &first); + printFieldOrReferenceFlag( + &flags, RTFieldAccess::CONSTRAINED, "constrained", &first); + printFieldOrReferenceFlag( + &flags, RTFieldAccess::TRANSIENT, "transient", &first); + printFieldOrReferenceFlag( + &flags, RTFieldAccess::MAYBEAMBIGUOUS, "maybeambiguous", &first); + printFieldOrReferenceFlag( + &flags, RTFieldAccess::MAYBEDEFAULT, "maybedefault", &first); + printFieldOrReferenceFlag( + &flags, RTFieldAccess::REMOVABLE, "removable", &first); + printFieldOrReferenceFlag( + &flags, RTFieldAccess::ATTRIBUTE, "attribute", &first); + printFieldOrReferenceFlag( + &flags, RTFieldAccess::PROPERTY, "property", &first); + printFieldOrReferenceFlag(&flags, RTFieldAccess::CONST, "const", &first); + printFieldOrReferenceFlag( + &flags, RTFieldAccess::READWRITE, "readwrite", &first); + printFieldOrReferenceFlag( + &flags, RTFieldAccess::PARAMETERIZED_TYPE, "parameterized type", &first); + printFieldOrReferenceFlag( + &flags, RTFieldAccess::PUBLISHED, "published", &first); + if (flags != RTFieldAccess::NONE) { + if (!first) { + printf("|"); + } + printf("<invalid (0x%04X)>", static_cast< unsigned int >(flags)); + } + } +} + +void dumpType(typereg::Reader const & reader, OString const & indent) { + if (reader.isValid()) { + printf("version: %ld\n", static_cast< long >(reader.getVersion())); + printf("%sdocumentation: ", indent.getStr()); + printString(reader.getDocumentation()); + printf("\n"); + printf("%sfile name: ", indent.getStr()); + printString(reader.getFileName()); + printf("\n"); + printf("%stype class: ", indent.getStr()); + if (reader.isPublished()) { + printf("published "); + } + switch (reader.getTypeClass()) { + case RT_TYPE_INTERFACE: + printf("interface"); + break; + + case RT_TYPE_MODULE: + printf("module"); + break; + + case RT_TYPE_STRUCT: + printf("struct"); + break; + + case RT_TYPE_ENUM: + printf("enum"); + break; + + case RT_TYPE_EXCEPTION: + printf("exception"); + break; + + case RT_TYPE_TYPEDEF: + printf("typedef"); + break; + + case RT_TYPE_SERVICE: + printf("service"); + break; + + case RT_TYPE_SINGLETON: + printf("singleton"); + break; + + case RT_TYPE_CONSTANTS: + printf("constants"); + break; + + default: + printf( + "<invalid (%ld)>", static_cast< long >(reader.getTypeClass())); + break; + } + printf("\n"); + printf("%stype name: ", indent.getStr()); + printString(reader.getTypeName()); + printf("\n"); + printf( + "%ssuper type count: %u\n", indent.getStr(), + static_cast< unsigned int >(reader.getSuperTypeCount())); + for (sal_uInt16 i = 0; i < reader.getSuperTypeCount(); ++i) { + printf( + "%ssuper type name %u: ", indent.getStr(), + static_cast< unsigned int >(i)); + printString(reader.getSuperTypeName(i)); + printf("\n"); + } + printf( + "%sfield count: %u\n", indent.getStr(), + static_cast< unsigned int >(reader.getFieldCount())); + for (sal_uInt16 i = 0; i < reader.getFieldCount(); ++i) { + printf( + "%sfield %u:\n", indent.getStr(), + static_cast< unsigned int >(i)); + printf("%s documentation: ", indent.getStr()); + printString(reader.getFieldDocumentation(i)); + printf("\n"); + printf("%s file name: ", indent.getStr()); + printString(reader.getFieldFileName(i)); + printf("\n"); + printf("%s flags: ", indent.getStr()); + printFieldOrReferenceFlags(reader.getFieldFlags(i)); + printf("\n"); + printf("%s name: ", indent.getStr()); + printString(reader.getFieldName(i)); + printf("\n"); + printf("%s type name: ", indent.getStr()); + printString(reader.getFieldTypeName(i)); + printf("\n"); + printf("%s value: ", indent.getStr()); + RTConstValue value(reader.getFieldValue(i)); + switch (value.m_type) { + case RT_TYPE_NONE: + printf("none"); + break; + + case RT_TYPE_BOOL: + printf("boolean %s", value.m_value.aBool ? "true" : "false"); + break; + + case RT_TYPE_BYTE: + printf("byte %d", static_cast< int >(value.m_value.aByte)); + break; + + case RT_TYPE_INT16: + printf("short %d", static_cast< int >(value.m_value.aShort)); + break; + + case RT_TYPE_UINT16: + printf( + "unsigned short %u", + static_cast< unsigned int >(value.m_value.aUShort)); + break; + + case RT_TYPE_INT32: + printf("long %ld", static_cast< long >(value.m_value.aLong)); + break; + + case RT_TYPE_UINT32: + printf( + "unsigned long %lu", + static_cast< unsigned long >(value.m_value.aULong)); + break; + + case RT_TYPE_INT64: + // TODO: no portable way to print hyper values + printf("hyper"); + break; + + case RT_TYPE_UINT64: + // TODO: no portable way to print unsigned hyper values + printf("unsigned hyper"); + break; + + case RT_TYPE_FLOAT: + // TODO: no portable way to print float values + printf("float"); + break; + + case RT_TYPE_DOUBLE: + // TODO: no portable way to print double values + printf("double"); + break; + + case RT_TYPE_STRING: + printf("string "); + printString(value.m_value.aString); + break; + + default: + printf("<invalid (%ld)>", static_cast< long >(value.m_type)); + break; + } + printf("\n"); + } + printf( + "%smethod count: %u\n", indent.getStr(), + static_cast< unsigned int >(reader.getMethodCount())); + for (sal_uInt16 i = 0; i < reader.getMethodCount(); ++i) { + printf( + "%smethod %u:\n", indent.getStr(), + static_cast< unsigned int >(i)); + printf("%s documentation: ", indent.getStr()); + printString(reader.getMethodDocumentation(i)); + printf("\n"); + printf("%s flags: ", indent.getStr()); + switch (reader.getMethodFlags(i)) { + case RTMethodMode::ONEWAY: + printf("oneway"); + break; + + case RTMethodMode::TWOWAY: + printf("synchronous"); + break; + + case RTMethodMode::ATTRIBUTE_GET: + printf("attribute get"); + break; + + case RTMethodMode::ATTRIBUTE_SET: + printf("attribute set"); + break; + + default: + printf( + "<invalid (%ld)>", + static_cast< long >(reader.getMethodFlags(i))); + break; + } + printf("\n"); + printf("%s name: ", indent.getStr()); + printString(reader.getMethodName(i)); + printf("\n"); + printf("%s return type name: ", indent.getStr()); + printString(reader.getMethodReturnTypeName(i)); + printf("\n"); + printf( + "%s parameter count: %u\n", indent.getStr(), + static_cast< unsigned int >(reader.getMethodParameterCount(i))); + // coverity[tainted_data] - cid#1215304 unhelpfully warns about untrusted loop bound + for (sal_uInt16 j = 0; j < reader.getMethodParameterCount(i); ++j) + { + printf( + "%s parameter %u:\n", indent.getStr(), + static_cast< unsigned int >(j)); + printf("%s flags: ", indent.getStr()); + RTParamMode flags = reader.getMethodParameterFlags(i, j); + bool rest = (flags & RT_PARAM_REST) != 0; + switch (flags & ~RT_PARAM_REST) { + case RT_PARAM_IN: + printf("in"); + break; + + case RT_PARAM_OUT: + printf("out"); + break; + + case RT_PARAM_INOUT: + printf("inout"); + break; + + default: + printf("<invalid (%ld)>", static_cast< long >(flags)); + rest = false; + break; + } + if (rest) { + printf("|rest"); + } + printf("\n"); + printf("%s name: ", indent.getStr()); + printString(reader.getMethodParameterName(i, j)); + printf("\n"); + printf("%s type name: ", indent.getStr()); + printString(reader.getMethodParameterTypeName(i, j)); + printf("\n"); + } + printf( + "%s exception count: %u\n", indent.getStr(), + static_cast< unsigned int >(reader.getMethodExceptionCount(i))); + // coverity[tainted_data] - cid#1215304 unhelpfully warns about untrusted loop bound + for (sal_uInt16 j = 0; j < reader.getMethodExceptionCount(i); ++j) + { + printf( + "%s exception type name %u: ", indent.getStr(), + static_cast< unsigned int >(j)); + printString(reader.getMethodExceptionTypeName(i, j)); + printf("\n"); + } + } + printf( + "%sreference count: %u\n", indent.getStr(), + static_cast< unsigned int >(reader.getReferenceCount())); + for (sal_uInt16 i = 0; i < reader.getReferenceCount(); ++i) { + printf( + "%sreference %u:\n", indent.getStr(), + static_cast< unsigned int >(i)); + printf("%s documentation: ", indent.getStr()); + printString(reader.getReferenceDocumentation(i)); + printf("\n"); + printf("%s flags: ", indent.getStr()); + printFieldOrReferenceFlags(reader.getReferenceFlags(i)); + printf("\n"); + printf("%s sort: ", indent.getStr()); + switch (reader.getReferenceSort(i)) { + case RTReferenceType::SUPPORTS: + printf("supports"); + break; + + case RTReferenceType::EXPORTS: + printf("exports"); + break; + + case RTReferenceType::TYPE_PARAMETER: + printf("type parameter"); + break; + + default: + printf( + "<invalid (%ld)>", + static_cast< long >(reader.getReferenceSort(i))); + break; + } + printf("\n"); + printf("%s type name: ", indent.getStr()); + printString(reader.getReferenceTypeName(i)); + printf("\n"); + } + } else { + printf("<invalid>\n"); + } +} + +} + +ORegistry::ORegistry() + : m_refCount(1) + , m_readOnly(false) + , m_isOpen(false) +{ +} + +ORegistry::~ORegistry() +{ + ORegKey* pRootKey = m_openKeyTable[ROOT]; + if (pRootKey != nullptr) + (void) releaseKey(pRootKey); + + if (m_file.isValid()) + m_file.close(); +} + +RegError ORegistry::initRegistry(const OUString& regName, RegAccessMode accessMode, bool bCreate) +{ + RegError eRet = RegError::INVALID_REGISTRY; + OStoreFile rRegFile; + storeAccessMode sAccessMode = storeAccessMode::ReadWrite; + storeError errCode; + + if (bCreate) + { + sAccessMode = storeAccessMode::Create; + } + else if (accessMode & RegAccessMode::READONLY) + { + sAccessMode = storeAccessMode::ReadOnly; + m_readOnly = true; + } + + if (regName.isEmpty() && + storeAccessMode::Create == sAccessMode) + { + errCode = rRegFile.createInMemory(); + } + else + { + errCode = rRegFile.create(regName, sAccessMode); + } + + if (errCode) + { + switch (errCode) + { + case store_E_NotExists: + eRet = RegError::REGISTRY_NOT_EXISTS; + break; + case store_E_LockingViolation: + eRet = RegError::CANNOT_OPEN_FOR_READWRITE; + break; + default: + eRet = RegError::INVALID_REGISTRY; + break; + } + } + else + { + OStoreDirectory rStoreDir; + storeError _err = rStoreDir.create(rRegFile, OUString(), OUString(), sAccessMode); + + if (_err == store_E_None) + { + m_file = rRegFile; + m_name = regName; + m_isOpen = true; + + m_openKeyTable[ROOT] = new ORegKey(ROOT, this); + eRet = RegError::NO_ERROR; + } + else + eRet = RegError::INVALID_REGISTRY; + } + + return eRet; +} + +RegError ORegistry::closeRegistry() +{ + REG_GUARD(m_mutex); + + if (m_file.isValid()) + { + (void) releaseKey(m_openKeyTable[ROOT]); + m_file.close(); + m_isOpen = false; + return RegError::NO_ERROR; + } else + { + return RegError::REGISTRY_NOT_EXISTS; + } +} + +RegError ORegistry::destroyRegistry(const OUString& regName) +{ + REG_GUARD(m_mutex); + + if (!regName.isEmpty()) + { + std::unique_ptr<ORegistry> pReg(new ORegistry()); + + if (pReg->initRegistry(regName, RegAccessMode::READWRITE) == RegError::NO_ERROR) + { + pReg.reset(); + + OUString systemName; + if (FileBase::getSystemPathFromFileURL(regName, systemName) != FileBase::E_None) + systemName = regName; + + OString name(OUStringToOString(systemName, osl_getThreadTextEncoding())); + if (unlink(name.getStr()) != 0) + { + return RegError::DESTROY_REGISTRY_FAILED; + } + } else + { + return RegError::DESTROY_REGISTRY_FAILED; + } + } else + { + if (m_refCount != 1 || isReadOnly()) + { + return RegError::DESTROY_REGISTRY_FAILED; + } + + if (m_file.isValid()) + { + releaseKey(m_openKeyTable[ROOT]); + m_file.close(); + m_isOpen = false; + + if (!m_name.isEmpty()) + { + OUString systemName; + if (FileBase::getSystemPathFromFileURL(m_name, systemName) != FileBase::E_None) + systemName = m_name; + + OString name(OUStringToOString(systemName, osl_getThreadTextEncoding())); + if (unlink(name.getStr()) != 0) + { + return RegError::DESTROY_REGISTRY_FAILED; + } + } + } else + { + return RegError::REGISTRY_NOT_EXISTS; + } + } + + return RegError::NO_ERROR; +} + +RegError ORegistry::acquireKey (RegKeyHandle hKey) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (!pKey) + return RegError::INVALID_KEY; + + REG_GUARD(m_mutex); + pKey->acquire(); + + return RegError::NO_ERROR; +} + +RegError ORegistry::releaseKey (RegKeyHandle hKey) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (!pKey) + return RegError::INVALID_KEY; + + REG_GUARD(m_mutex); + if (pKey->release() == 0) + { + m_openKeyTable.erase(pKey->getName()); + delete pKey; + } + return RegError::NO_ERROR; +} + +RegError ORegistry::createKey(RegKeyHandle hKey, std::u16string_view keyName, + RegKeyHandle* phNewKey) +{ + ORegKey* pKey; + + *phNewKey = nullptr; + + if (keyName.empty()) + return RegError::INVALID_KEYNAME; + + REG_GUARD(m_mutex); + + if (hKey) + pKey = static_cast<ORegKey*>(hKey); + else + pKey = m_openKeyTable[ROOT]; + + OUString sFullKeyName = pKey->getFullPath(keyName); + + if (m_openKeyTable.count(sFullKeyName) > 0) + { + *phNewKey = m_openKeyTable[sFullKeyName]; + static_cast<ORegKey*>(*phNewKey)->acquire(); + static_cast<ORegKey*>(*phNewKey)->setDeleted(false); + return RegError::NO_ERROR; + } + + OStoreDirectory rStoreDir; + OUStringBuffer sFullPath(sFullKeyName.getLength()+16); + OUString token; + + sFullPath.append('/'); + + sal_Int32 nIndex = 0; + do + { + token = sFullKeyName.getToken(0, '/', nIndex); + if (!token.isEmpty()) + { + if (rStoreDir.create(pKey->getStoreFile(), sFullPath.toString(), token, storeAccessMode::Create)) + { + return RegError::CREATE_KEY_FAILED; + } + + sFullPath.append(token); + sFullPath.append('/'); + } + } while(nIndex != -1); + + + pKey = new ORegKey(sFullKeyName, this); + *phNewKey = pKey; + m_openKeyTable[sFullKeyName] = pKey; + + return RegError::NO_ERROR; +} + +RegError ORegistry::openKey(RegKeyHandle hKey, std::u16string_view keyName, + RegKeyHandle* phOpenKey) +{ + ORegKey* pKey; + + *phOpenKey = nullptr; + + if (keyName.empty()) + { + return RegError::INVALID_KEYNAME; + } + + REG_GUARD(m_mutex); + + if (hKey) + pKey = static_cast<ORegKey*>(hKey); + else + pKey = m_openKeyTable[ROOT]; + + OUString path(pKey->getFullPath(keyName)); + KeyMap::iterator i(m_openKeyTable.find(path)); + if (i == m_openKeyTable.end()) { + sal_Int32 n = path.lastIndexOf('/') + 1; + switch (OStoreDirectory().create( + pKey->getStoreFile(), path.copy(0, n), path.copy(n), + isReadOnly() ? storeAccessMode::ReadOnly : storeAccessMode::ReadWrite)) + { + case store_E_NotExists: + return RegError::KEY_NOT_EXISTS; + case store_E_WrongFormat: + return RegError::INVALID_KEY; + default: + break; + } + + std::unique_ptr< ORegKey > p(new ORegKey(path, this)); + i = m_openKeyTable.insert(std::make_pair(path, p.get())).first; + p.release(); + } else { + i->second->acquire(); + } + *phOpenKey = i->second; + return RegError::NO_ERROR; +} + +RegError ORegistry::closeKey(RegKeyHandle hKey) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + + REG_GUARD(m_mutex); + + OUString const aKeyName (pKey->getName()); + if (m_openKeyTable.count(aKeyName) <= 0) + return RegError::KEY_NOT_OPEN; + + if (pKey->isModified()) + { + ORegKey * pRootKey = getRootKey(); + if (pKey != pRootKey) + { + // propagate "modified" state to RootKey. + pRootKey->setModified(); + } + else + { + // closing modified RootKey, flush registry file. + (void) m_file.flush(); + } + pKey->setModified(false); + (void) releaseKey(pRootKey); + } + + return releaseKey(pKey); +} + +RegError ORegistry::deleteKey(RegKeyHandle hKey, std::u16string_view keyName) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (keyName.empty()) + return RegError::INVALID_KEYNAME; + + REG_GUARD(m_mutex); + + if (!pKey) + pKey = m_openKeyTable[ROOT]; + + OUString sFullKeyName(pKey->getFullPath(keyName)); + return eraseKey(m_openKeyTable[ROOT], sFullKeyName); +} + +RegError ORegistry::eraseKey(ORegKey* pKey, std::u16string_view keyName) +{ + RegError _ret = RegError::NO_ERROR; + + if (keyName.empty()) + { + return RegError::INVALID_KEYNAME; + } + + OUString sFullKeyName(pKey->getName()); + OUString sFullPath(sFullKeyName); + OUString sRelativKey; + size_t lastIndex = keyName.rfind('/'); + + if (lastIndex != std::u16string_view::npos) + { + sRelativKey += keyName.substr(lastIndex + 1); + + if (sFullKeyName.getLength() > 1) + sFullKeyName += keyName; + else + sFullKeyName += keyName.substr(1); + + sFullPath = sFullKeyName.copy(0, keyName.rfind('/') + 1); + } else + { + if (sFullKeyName.getLength() > 1) + sFullKeyName += ROOT; + + sRelativKey += keyName; + sFullKeyName += keyName; + + if (sFullPath.getLength() > 1) + sFullPath += ROOT; + } + + ORegKey* pOldKey = nullptr; + _ret = pKey->openKey(keyName, reinterpret_cast<RegKeyHandle*>(&pOldKey)); + if (_ret != RegError::NO_ERROR) + return _ret; + + _ret = deleteSubkeysAndValues(pOldKey); + if (_ret != RegError::NO_ERROR) + { + pKey->closeKey(pOldKey); + return _ret; + } + + OUString tmpName = sRelativKey + ROOT; + + OStoreFile sFile(pKey->getStoreFile()); + if (sFile.isValid() && sFile.remove(sFullPath, tmpName)) + { + return RegError::DELETE_KEY_FAILED; + } + pOldKey->setModified(); + + // set flag deleted !!! + pOldKey->setDeleted(true); + + return pKey->closeKey(pOldKey); +} + +RegError ORegistry::deleteSubkeysAndValues(ORegKey* pKey) +{ + OStoreDirectory::iterator iter; + RegError _ret = RegError::NO_ERROR; + OStoreDirectory rStoreDir(pKey->getStoreDir()); + storeError _err = rStoreDir.first(iter); + + while (_err == store_E_None) + { + OUString const keyName(iter.m_pszName, iter.m_nLength); + + if (iter.m_nAttrib & STORE_ATTRIB_ISDIR) + { + _ret = eraseKey(pKey, keyName); + if (_ret != RegError::NO_ERROR) + return _ret; + } + else + { + OUString sFullPath(pKey->getName()); + + if (sFullPath.getLength() > 1) + sFullPath += ROOT; + + if (const_cast<OStoreFile&>(pKey->getStoreFile()).remove(sFullPath, keyName)) + { + return RegError::DELETE_VALUE_FAILED; + } + pKey->setModified(); + } + + _err = rStoreDir.next(iter); + } + + return RegError::NO_ERROR; +} + +RegError ORegistry::loadKey(RegKeyHandle hKey, const OUString& regFileName, + bool bWarnings, bool bReport) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + + ORegistry aReg; + RegError _ret = aReg.initRegistry(regFileName, RegAccessMode::READONLY); + if (_ret != RegError::NO_ERROR) + return _ret; + ORegKey* pRootKey = aReg.getRootKey(); + + REG_GUARD(m_mutex); + + OStoreDirectory::iterator iter; + OStoreDirectory rStoreDir(pRootKey->getStoreDir()); + storeError _err = rStoreDir.first(iter); + + while (_err == store_E_None) + { + OUString const keyName(iter.m_pszName, iter.m_nLength); + + if (iter.m_nAttrib & STORE_ATTRIB_ISDIR) + { + _ret = loadAndSaveKeys(pKey, pRootKey, keyName, 0, bWarnings, bReport); + } + else + { + _ret = loadAndSaveValue(pKey, pRootKey, keyName, 0, bWarnings, bReport); + } + + if (_ret == RegError::MERGE_ERROR) + break; + if (_ret == RegError::MERGE_CONFLICT && bWarnings) + break; + + _err = rStoreDir.next(iter); + } + + rStoreDir = OStoreDirectory(); + (void) aReg.releaseKey(pRootKey); + return _ret; +} + +RegError ORegistry::loadAndSaveValue(ORegKey* pTargetKey, + ORegKey const * pSourceKey, + const OUString& valueName, + sal_uInt32 nCut, + bool bWarnings, + bool bReport) +{ + OStoreStream rValue; + RegValueType valueType; + sal_uInt32 valueSize; + sal_uInt32 nSize; + storeAccessMode sourceAccess = storeAccessMode::ReadWrite; + OUString sTargetPath(pTargetKey->getName()); + OUString sSourcePath(pSourceKey->getName()); + + if (pSourceKey->isReadOnly()) + { + sourceAccess = storeAccessMode::ReadOnly; + } + + if (nCut) + { + sTargetPath = sSourcePath.copy(nCut); + } else + { + if (sTargetPath.getLength() > 1) + { + if (sSourcePath.getLength() > 1) + sTargetPath += sSourcePath; + } else + sTargetPath = sSourcePath; + } + + if (sTargetPath.getLength() > 1) sTargetPath += ROOT; + if (sSourcePath.getLength() > 1) sSourcePath += ROOT; + + if (rValue.create(pSourceKey->getStoreFile(), sSourcePath, valueName, sourceAccess)) + { + return RegError::VALUE_NOT_EXISTS; + } + + std::vector<sal_uInt8> aBuffer(VALUE_HEADERSIZE); + + sal_uInt32 rwBytes; + if (rValue.readAt(0, aBuffer.data(), VALUE_HEADERSIZE, rwBytes)) + { + return RegError::INVALID_VALUE; + } + if (rwBytes != VALUE_HEADERSIZE) + { + return RegError::INVALID_VALUE; + } + + RegError _ret = RegError::NO_ERROR; + sal_uInt8 type = aBuffer[0]; + valueType = static_cast<RegValueType>(type); + readUINT32(aBuffer.data() + VALUE_TYPEOFFSET, valueSize); + + nSize = VALUE_HEADERSIZE + valueSize; + aBuffer.resize(nSize); + + if (rValue.readAt(0, aBuffer.data(), nSize, rwBytes)) + { + return RegError::INVALID_VALUE; + } + if (rwBytes != nSize) + { + return RegError::INVALID_VALUE; + } + + OStoreFile rTargetFile(pTargetKey->getStoreFile()); + + if (!rValue.create(rTargetFile, sTargetPath, valueName, storeAccessMode::ReadWrite)) + { + if (valueType == RegValueType::BINARY) + { + _ret = checkBlop( + rValue, sTargetPath, valueSize, aBuffer.data() + VALUE_HEADEROFFSET, + bReport); + if (_ret != RegError::NO_ERROR) + { + if (_ret == RegError::MERGE_ERROR || + (_ret == RegError::MERGE_CONFLICT && bWarnings)) + { + return _ret; + } + } else + { + return _ret; + } + } + } + + if (rValue.create(rTargetFile, sTargetPath, valueName, storeAccessMode::Create)) + { + return RegError::INVALID_VALUE; + } + if (rValue.writeAt(0, aBuffer.data(), nSize, rwBytes)) + { + return RegError::INVALID_VALUE; + } + + if (rwBytes != nSize) + { + return RegError::INVALID_VALUE; + } + pTargetKey->setModified(); + + return _ret; +} + +RegError ORegistry::checkBlop(OStoreStream& rValue, + std::u16string_view sTargetPath, + sal_uInt32 srcValueSize, + sal_uInt8 const * pSrcBuffer, + bool bReport) +{ + RegistryTypeReader reader(pSrcBuffer, srcValueSize); + + if (reader.getTypeClass() == RT_TYPE_INVALID) + { + return RegError::INVALID_VALUE; + } + + std::vector<sal_uInt8> aBuffer(VALUE_HEADERSIZE); + sal_uInt32 rwBytes; + OString targetPath(OUStringToOString(sTargetPath, RTL_TEXTENCODING_UTF8)); + + if (!rValue.readAt(0, aBuffer.data(), VALUE_HEADERSIZE, rwBytes) && + (rwBytes == VALUE_HEADERSIZE)) + { + sal_uInt8 type = aBuffer[0]; + RegValueType valueType = static_cast<RegValueType>(type); + sal_uInt32 valueSize; + readUINT32(aBuffer.data() + VALUE_TYPEOFFSET, valueSize); + + if (valueType == RegValueType::BINARY) + { + aBuffer.resize(valueSize); + if (!rValue.readAt(VALUE_HEADEROFFSET, aBuffer.data(), valueSize, rwBytes) && + (rwBytes == valueSize)) + { + RegistryTypeReader reader2(aBuffer.data(), valueSize); + + if ((reader.getTypeClass() != reader2.getTypeClass()) + || reader2.getTypeClass() == RT_TYPE_INVALID) + { + if (bReport) + { + fprintf(stdout, "ERROR: values of blop from key \"%s\" has different types.\n", + targetPath.getStr()); + } + return RegError::MERGE_ERROR; + } + + if (reader.getTypeClass() == RT_TYPE_MODULE) + { + if (reader.getFieldCount() > 0 && + reader2.getFieldCount() > 0) + { + mergeModuleValue(rValue, reader, reader2); + + return RegError::NO_ERROR; + } else + if (reader2.getFieldCount() > 0) + { + return RegError::NO_ERROR; + } else + { + return RegError::MERGE_CONFLICT; + } + } else + { + if (bReport) + { + fprintf(stderr, "WARNING: value of key \"%s\" already exists.\n", + targetPath.getStr()); + } + return RegError::MERGE_CONFLICT; + } + } else + { + if (bReport) + { + fprintf(stderr, "ERROR: values of key \"%s\" contains bad data.\n", + targetPath.getStr()); + } + return RegError::MERGE_ERROR; + } + } else + { + if (bReport) + { + fprintf(stderr, "ERROR: values of key \"%s\" has different types.\n", + targetPath.getStr()); + } + return RegError::MERGE_ERROR; + } + } else + { + return RegError::INVALID_VALUE; + } +} + +static sal_uInt32 checkTypeReaders(RegistryTypeReader const & reader1, + RegistryTypeReader const & reader2, + std::set< OUString >& nameSet) +{ + sal_uInt32 count=0; + for (sal_uInt32 i=0 ; i < reader1.getFieldCount(); i++) + { + nameSet.insert(reader1.getFieldName(i)); + count++; + } + for (sal_uInt32 i=0 ; i < reader2.getFieldCount(); i++) + { + if (nameSet.insert(reader2.getFieldName(i)).second) + count++; + } + return count; +} + +RegError ORegistry::mergeModuleValue(OStoreStream& rTargetValue, + RegistryTypeReader const & reader, + RegistryTypeReader const & reader2) +{ + std::set< OUString > nameSet; + sal_uInt32 count = checkTypeReaders(reader, reader2, nameSet); + + if (count == reader.getFieldCount()) + return RegError::NO_ERROR; + + sal_uInt16 index = 0; + + RegistryTypeWriter writer(reader.getTypeClass(), + reader.getTypeName(), + reader.getSuperTypeName(), + static_cast<sal_uInt16>(count)); + + for (sal_uInt32 i=0 ; i < reader.getFieldCount(); i++) + { + writer.setFieldData(index, + reader.getFieldName(i), + reader.getFieldType(i), + reader.getFieldDoku(i), + reader.getFieldFileName(i), + reader.getFieldAccess(i), + reader.getFieldConstValue(i)); + index++; + } + for (sal_uInt32 i=0 ; i < reader2.getFieldCount(); i++) + { + if (nameSet.find(reader2.getFieldName(i)) == nameSet.end()) + { + writer.setFieldData(index, + reader2.getFieldName(i), + reader2.getFieldType(i), + reader2.getFieldDoku(i), + reader2.getFieldFileName(i), + reader2.getFieldAccess(i), + reader2.getFieldConstValue(i)); + index++; + } + } + + const sal_uInt8* pBlop = writer.getBlop(); + sal_uInt32 aBlopSize = writer.getBlopSize(); + + sal_uInt8 type = sal_uInt8(RegValueType::BINARY); + std::vector<sal_uInt8> aBuffer(VALUE_HEADERSIZE + aBlopSize); + + memcpy(aBuffer.data(), &type, 1); + writeUINT32(aBuffer.data() + VALUE_TYPEOFFSET, aBlopSize); + memcpy(aBuffer.data() + VALUE_HEADEROFFSET, pBlop, aBlopSize); + + sal_uInt32 rwBytes; + if (rTargetValue.writeAt(0, aBuffer.data(), VALUE_HEADERSIZE+aBlopSize, rwBytes)) + { + return RegError::INVALID_VALUE; + } + + if (rwBytes != VALUE_HEADERSIZE+aBlopSize) + { + return RegError::INVALID_VALUE; + } + return RegError::NO_ERROR; +} + +RegError ORegistry::loadAndSaveKeys(ORegKey* pTargetKey, + ORegKey* pSourceKey, + const OUString& keyName, + sal_uInt32 nCut, + bool bWarnings, + bool bReport) +{ + RegError _ret = RegError::NO_ERROR; + OUString sRelPath(pSourceKey->getName().copy(nCut)); + OUString sFullPath; + + if(pTargetKey->getName().getLength() > 1) + sFullPath += pTargetKey->getName(); + sFullPath += sRelPath; + if (sRelPath.getLength() > 1 || sFullPath.isEmpty()) + sFullPath += ROOT; + + OUString sFullKeyName = sFullPath + keyName; + + OStoreDirectory rStoreDir; + if (rStoreDir.create(pTargetKey->getStoreFile(), sFullPath, keyName, storeAccessMode::Create)) + { + return RegError::CREATE_KEY_FAILED; + } + + if (m_openKeyTable.count(sFullKeyName) > 0) + { + m_openKeyTable[sFullKeyName]->setDeleted(false); + } + + ORegKey* pTmpKey = nullptr; + _ret = pSourceKey->openKey(keyName, reinterpret_cast<RegKeyHandle*>(&pTmpKey)); + if (_ret != RegError::NO_ERROR) + return _ret; + + OStoreDirectory::iterator iter; + OStoreDirectory rTmpStoreDir(pTmpKey->getStoreDir()); + storeError _err = rTmpStoreDir.first(iter); + + while (_err == store_E_None) + { + OUString const sName(iter.m_pszName, iter.m_nLength); + + if (iter.m_nAttrib & STORE_ATTRIB_ISDIR) + { + _ret = loadAndSaveKeys(pTargetKey, pTmpKey, + sName, nCut, bWarnings, bReport); + } else + { + _ret = loadAndSaveValue(pTargetKey, pTmpKey, + sName, nCut, bWarnings, bReport); + } + + if (_ret == RegError::MERGE_ERROR) + break; + if (_ret == RegError::MERGE_CONFLICT && bWarnings) + break; + + _err = rTmpStoreDir.next(iter); + } + + pSourceKey->releaseKey(pTmpKey); + return _ret; +} + +ORegKey* ORegistry::getRootKey() +{ + m_openKeyTable[ROOT]->acquire(); + return m_openKeyTable[ROOT]; +} + +RegError ORegistry::dumpRegistry(RegKeyHandle hKey) const +{ + ORegKey *pKey = static_cast<ORegKey*>(hKey); + OUString sName; + RegError _ret = RegError::NO_ERROR; + OStoreDirectory::iterator iter; + OStoreDirectory rStoreDir(pKey->getStoreDir()); + storeError _err = rStoreDir.first(iter); + + OString regName(OUStringToOString(getName(), osl_getThreadTextEncoding())); + OString keyName(OUStringToOString(pKey->getName(), RTL_TEXTENCODING_UTF8)); + fprintf(stdout, "Registry \"%s\":\n\n%s\n", regName.getStr(), keyName.getStr()); + + while (_err == store_E_None) + { + sName = OUString(iter.m_pszName, iter.m_nLength); + + if (iter.m_nAttrib & STORE_ATTRIB_ISDIR) + { + _ret = dumpKey(pKey->getName(), sName, 1); + } else + { + _ret = dumpValue(pKey->getName(), sName, 1); + } + + if (_ret != RegError::NO_ERROR) + { + return _ret; + } + + _err = rStoreDir.next(iter); + } + + return RegError::NO_ERROR; +} + +RegError ORegistry::dumpValue(const OUString& sPath, const OUString& sName, sal_Int16 nSpc) const +{ + OStoreStream rValue; + sal_uInt32 valueSize; + RegValueType valueType; + OUString sFullPath(sPath); + OString sIndent; + storeAccessMode accessMode = storeAccessMode::ReadWrite; + + if (isReadOnly()) + { + accessMode = storeAccessMode::ReadOnly; + } + + for (int i= 0; i < nSpc; i++) sIndent += " "; + + if (sFullPath.getLength() > 1) + { + sFullPath += ROOT; + } + if (rValue.create(m_file, sFullPath, sName, accessMode)) + { + return RegError::VALUE_NOT_EXISTS; + } + + std::vector<sal_uInt8> aBuffer(VALUE_HEADERSIZE); + + sal_uInt32 rwBytes; + if (rValue.readAt(0, aBuffer.data(), VALUE_HEADERSIZE, rwBytes)) + { + return RegError::INVALID_VALUE; + } + if (rwBytes != (VALUE_HEADERSIZE)) + { + return RegError::INVALID_VALUE; + } + + sal_uInt8 type = aBuffer[0]; + valueType = static_cast<RegValueType>(type); + readUINT32(aBuffer.data() + VALUE_TYPEOFFSET, valueSize); + + aBuffer.resize(valueSize); + if (rValue.readAt(VALUE_HEADEROFFSET, aBuffer.data(), valueSize, rwBytes)) + { + return RegError::INVALID_VALUE; + } + if (rwBytes != valueSize) + { + return RegError::INVALID_VALUE; + } + + const char* indent = sIndent.getStr(); + switch (valueType) + { + case RegValueType::NOT_DEFINED: + fprintf(stdout, "%sValue: Type = VALUETYPE_NOT_DEFINED\n", indent); + break; + case RegValueType::LONG: + { + fprintf(stdout, "%sValue: Type = RegValueType::LONG\n", indent); + fprintf( + stdout, "%s Size = %lu\n", indent, + sal::static_int_cast< unsigned long >(valueSize)); + fprintf(stdout, "%s Data = ", indent); + + sal_Int32 value; + readINT32(aBuffer.data(), value); + fprintf(stdout, "%ld\n", sal::static_int_cast< long >(value)); + } + break; + case RegValueType::STRING: + { + char* value = static_cast<char*>(std::malloc(valueSize)); + readUtf8(aBuffer.data(), value, valueSize); + fprintf(stdout, "%sValue: Type = RegValueType::STRING\n", indent); + fprintf( + stdout, "%s Size = %lu\n", indent, + sal::static_int_cast< unsigned long >(valueSize)); + fprintf(stdout, "%s Data = \"%s\"\n", indent, value); + std::free(value); + } + break; + case RegValueType::UNICODE: + { + sal_uInt32 size = (valueSize / 2) * sizeof(sal_Unicode); + fprintf(stdout, "%sValue: Type = RegValueType::UNICODE\n", indent); + fprintf( + stdout, "%s Size = %lu\n", indent, + sal::static_int_cast< unsigned long >(valueSize)); + fprintf(stdout, "%s Data = ", indent); + + std::unique_ptr<sal_Unicode[]> value(new sal_Unicode[size]); + readString(aBuffer.data(), value.get(), size); + + OString uStr(value.get(), rtl_ustr_getLength(value.get()), RTL_TEXTENCODING_UTF8); + fprintf(stdout, "L\"%s\"\n", uStr.getStr()); + } + break; + case RegValueType::BINARY: + { + fprintf(stdout, "%sValue: Type = RegValueType::BINARY\n", indent); + fprintf( + stdout, "%s Size = %lu\n", indent, + sal::static_int_cast< unsigned long >(valueSize)); + fprintf(stdout, "%s Data = ", indent); + dumpType( + typereg::Reader(aBuffer.data(), valueSize), + sIndent + " "); + } + break; + case RegValueType::LONGLIST: + { + sal_uInt32 offset = 4; // initial 4 bytes for the size of the array + sal_uInt32 len = 0; + + readUINT32(aBuffer.data(), len); + + fprintf(stdout, "%sValue: Type = RegValueType::LONGLIST\n", indent); + fprintf( + stdout, "%s Size = %lu\n", indent, + sal::static_int_cast< unsigned long >(valueSize)); + fprintf( + stdout, "%s Len = %lu\n", indent, + sal::static_int_cast< unsigned long >(len)); + fprintf(stdout, "%s Data = ", indent); + + sal_Int32 longValue; + for (sal_uInt32 i=0; i < len; i++) + { + readINT32(aBuffer.data() + offset, longValue); + + if (offset > 4) + fprintf(stdout, "%s ", indent); + + fprintf( + stdout, "%lu = %ld\n", + sal::static_int_cast< unsigned long >(i), + sal::static_int_cast< long >(longValue)); + offset += 4; // 4 Bytes for sal_Int32 + } + } + break; + case RegValueType::STRINGLIST: + { + sal_uInt32 offset = 4; // initial 4 bytes for the size of the array + sal_uInt32 sLen = 0; + sal_uInt32 len = 0; + + readUINT32(aBuffer.data(), len); + + fprintf(stdout, "%sValue: Type = RegValueType::STRINGLIST\n", indent); + fprintf( + stdout, "%s Size = %lu\n", indent, + sal::static_int_cast< unsigned long >(valueSize)); + fprintf( + stdout, "%s Len = %lu\n", indent, + sal::static_int_cast< unsigned long >(len)); + fprintf(stdout, "%s Data = ", indent); + + for (sal_uInt32 i=0; i < len; i++) + { + readUINT32(aBuffer.data() + offset, sLen); + + offset += 4; // 4 bytes (sal_uInt32) for the string size + + char *pValue = static_cast<char*>(std::malloc(sLen)); + readUtf8(aBuffer.data() + offset, pValue, sLen); + + if (offset > 8) + fprintf(stdout, "%s ", indent); + + fprintf( + stdout, "%lu = \"%s\"\n", + sal::static_int_cast< unsigned long >(i), pValue); + std::free(pValue); + offset += sLen; + } + } + break; + case RegValueType::UNICODELIST: + { + sal_uInt32 offset = 4; // initial 4 bytes for the size of the array + sal_uInt32 sLen = 0; + sal_uInt32 len = 0; + + readUINT32(aBuffer.data(), len); + + fprintf(stdout, "%sValue: Type = RegValueType::UNICODELIST\n", indent); + fprintf( + stdout, "%s Size = %lu\n", indent, + sal::static_int_cast< unsigned long >(valueSize)); + fprintf( + stdout, "%s Len = %lu\n", indent, + sal::static_int_cast< unsigned long >(len)); + fprintf(stdout, "%s Data = ", indent); + + OString uStr; + for (sal_uInt32 i=0; i < len; i++) + { + readUINT32(aBuffer.data() + offset, sLen); + + offset += 4; // 4 bytes (sal_uInt32) for the string size + + sal_Unicode *pValue = static_cast<sal_Unicode*>(std::malloc((sLen / 2) * sizeof(sal_Unicode))); + readString(aBuffer.data() + offset, pValue, sLen); + + if (offset > 8) + fprintf(stdout, "%s ", indent); + + uStr = OString(pValue, rtl_ustr_getLength(pValue), RTL_TEXTENCODING_UTF8); + fprintf( + stdout, "%lu = L\"%s\"\n", + sal::static_int_cast< unsigned long >(i), + uStr.getStr()); + + offset += sLen; + + std::free(pValue); + } + } + break; + } + + fprintf(stdout, "\n"); + + return RegError::NO_ERROR; +} + +RegError ORegistry::dumpKey(const OUString& sPath, const OUString& sName, sal_Int16 nSpace) const +{ + OStoreDirectory rStoreDir; + OUString sFullPath(sPath); + OString sIndent; + storeAccessMode accessMode = storeAccessMode::ReadWrite; + RegError _ret = RegError::NO_ERROR; + + if (isReadOnly()) + { + accessMode = storeAccessMode::ReadOnly; + } + + for (int i= 0; i < nSpace; i++) sIndent += " "; + + if (sFullPath.getLength() > 1) + sFullPath += ROOT; + + storeError _err = rStoreDir.create(m_file, sFullPath, sName, accessMode); + + if (_err == store_E_NotExists) + return RegError::KEY_NOT_EXISTS; + else if (_err == store_E_WrongFormat) + return RegError::INVALID_KEY; + + fprintf(stdout, "%s/ %s\n", sIndent.getStr(), OUStringToOString(sName, RTL_TEXTENCODING_UTF8).getStr()); + + OUString sSubPath(sFullPath); + OUString sSubName; + sSubPath += sName; + + OStoreDirectory::iterator iter; + + _err = rStoreDir.first(iter); + + while (_err == store_E_None) + { + sSubName = OUString(iter.m_pszName, iter.m_nLength); + + if (iter.m_nAttrib & STORE_ATTRIB_ISDIR) + { + _ret = dumpKey(sSubPath, sSubName, nSpace+2); + } else + { + _ret = dumpValue(sSubPath, sSubName, nSpace+2); + } + + if (_ret != RegError::NO_ERROR) + { + return _ret; + } + + _err = rStoreDir.next(iter); + } + + return RegError::NO_ERROR; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/registry/source/regimpl.hxx b/registry/source/regimpl.hxx new file mode 100644 index 000000000..973a24986 --- /dev/null +++ b/registry/source/regimpl.hxx @@ -0,0 +1,156 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_REGISTRY_SOURCE_REGIMPL_HXX +#define INCLUDED_REGISTRY_SOURCE_REGIMPL_HXX + +#include <sal/config.h> + +#include <string_view> +#include <unordered_map> + +#include <regapi.hxx> +#include <registry/regtype.h> +#include <rtl/ustring.hxx> +#include <osl/mutex.hxx> +#include <store/store.hxx> + +// 5 bytes = 1 (byte for the type) + 4 (bytes for the size of the data) +#define VALUE_HEADERSIZE 5 +#define VALUE_TYPEOFFSET 1 +#define VALUE_HEADEROFFSET 5 + +#define REG_GUARD(mutex) \ + osl::Guard< osl::Mutex > aGuard( mutex ); + +class ORegKey; +class RegistryTypeReader; + +class ORegistry +{ +public: + ORegistry(); + + sal_uInt32 acquire() + { return ++m_refCount; } + + sal_uInt32 release() + { return --m_refCount; } + + RegError initRegistry(const OUString& name, + RegAccessMode accessMode, + bool bCreate = false); + + RegError closeRegistry(); + + RegError destroyRegistry(const OUString& name); + + RegError acquireKey(RegKeyHandle hKey); + RegError releaseKey(RegKeyHandle hKey); + + RegError createKey(RegKeyHandle hKey, + std::u16string_view keyName, + RegKeyHandle* phNewKey); + + RegError openKey(RegKeyHandle hKey, + std::u16string_view keyName, + RegKeyHandle* phOpenKey); + + RegError closeKey(RegKeyHandle hKey); + + RegError deleteKey(RegKeyHandle hKey, std::u16string_view keyName); + + RegError loadKey(RegKeyHandle hKey, + const OUString& regFileName, + bool bWarnings, + bool bReport); + + RegError dumpRegistry(RegKeyHandle hKey) const; + + ~ORegistry(); + + bool isReadOnly() const + { return m_readOnly; } + + bool isOpen() const + { return m_isOpen; } + + ORegKey* getRootKey(); + + const store::OStoreFile& getStoreFile() const + { return m_file; } + + const OUString& getName() const + { return m_name; } + + friend class ORegKey; + +private: + RegError eraseKey(ORegKey* pKey, std::u16string_view keyName); + + RegError deleteSubkeysAndValues(ORegKey* pKey); + + static RegError loadAndSaveValue(ORegKey* pTargetKey, + ORegKey const * pSourceKey, + const OUString& valueName, + sal_uInt32 nCut, + bool bWarnings, + bool bReport); + + static RegError checkBlop(store::OStoreStream& rValue, + std::u16string_view sTargetPath, + sal_uInt32 srcValueSize, + sal_uInt8 const * pSrcBuffer, + bool bReport); + + static RegError mergeModuleValue(store::OStoreStream& rTargetValue, + RegistryTypeReader const & reader, + RegistryTypeReader const & reader2); + + RegError loadAndSaveKeys(ORegKey* pTargetKey, + ORegKey* pSourceKey, + const OUString& keyName, + sal_uInt32 nCut, + bool bWarnings, + bool bReport); + + RegError dumpValue(const OUString& sPath, + const OUString& sName, + sal_Int16 nSpace) const; + + RegError dumpKey(const OUString& sPath, + const OUString& sName, + sal_Int16 nSpace) const; + + typedef std::unordered_map< OUString, ORegKey* > KeyMap; + + sal_uInt32 m_refCount; + osl::Mutex m_mutex; + bool m_readOnly; + bool m_isOpen; + OUString m_name; + store::OStoreFile m_file; + KeyMap m_openKeyTable; + + static constexpr OUStringLiteral ROOT { u"/" }; +}; + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/registry/source/registry.cxx b/registry/source/registry.cxx new file mode 100644 index 000000000..1727dfc11 --- /dev/null +++ b/registry/source/registry.cxx @@ -0,0 +1,393 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include <registry/registry.hxx> + +#include "keyimpl.hxx" +#include "regimpl.hxx" +#include "regkey.hxx" + +#if defined(_WIN32) +#include <io.h> +#endif + +extern "C" { + + +// acquire + +static void REGISTRY_CALLTYPE acquire(RegHandle hReg) +{ + ORegistry* pReg = static_cast<ORegistry*>(hReg); + + if (pReg != nullptr) + pReg->acquire(); +} + + +// release + +static void REGISTRY_CALLTYPE release(RegHandle hReg) +{ + ORegistry* pReg = static_cast<ORegistry*>(hReg); + + if (pReg && pReg->release() == 0) + { + delete pReg; + hReg = nullptr; + } +} + + +// getName + +static RegError REGISTRY_CALLTYPE getName(RegHandle hReg, rtl_uString** pName) +{ + if (hReg) + { + ORegistry* pReg = static_cast<ORegistry*>(hReg); + if ( pReg->isOpen() ) + { + rtl_uString_assign(pName, pReg->getName().pData); + return RegError::NO_ERROR; + } else + { + rtl_uString_new(pName); + return RegError::REGISTRY_NOT_OPEN; + } + } + + rtl_uString_new(pName); + return RegError::INVALID_REGISTRY; +} + + +// isReadOnly + +static sal_Bool REGISTRY_CALLTYPE isReadOnly(RegHandle hReg) +{ + if (hReg) + return static_cast<ORegistry*>(hReg)->isReadOnly(); + else + return false; +} + + +// createRegistry + +static RegError REGISTRY_CALLTYPE createRegistry(rtl_uString* registryName, + RegHandle* phRegistry) +{ + RegError ret; + + ORegistry* pReg = new ORegistry(); + if ((ret = pReg->initRegistry(registryName, RegAccessMode::READWRITE, true/*bCreate*/)) != RegError::NO_ERROR) + { + delete pReg; + *phRegistry = nullptr; + return ret; + } + + *phRegistry = pReg; + + return RegError::NO_ERROR; +} + + +// openRootKey + +static RegError REGISTRY_CALLTYPE openRootKey(RegHandle hReg, + RegKeyHandle* phRootKey) +{ + ORegistry* pReg; + + if (hReg) + { + pReg = static_cast<ORegistry*>(hReg); + if (!pReg->isOpen()) + return RegError::REGISTRY_NOT_OPEN; + } else + { + phRootKey = nullptr; + return RegError::INVALID_REGISTRY; + } + + *phRootKey = pReg->getRootKey(); + + return RegError::NO_ERROR; +} + + +// openRegistry + +static RegError REGISTRY_CALLTYPE openRegistry(rtl_uString* registryName, + RegHandle* phRegistry, + RegAccessMode accessMode) +{ + RegError _ret; + + ORegistry* pReg = new ORegistry(); + if ((_ret = pReg->initRegistry(registryName, accessMode)) != RegError::NO_ERROR) + { + *phRegistry = nullptr; + delete pReg; + return _ret; + } + + + *phRegistry = pReg; + + return RegError::NO_ERROR; +} + + +// closeRegistry + +static RegError REGISTRY_CALLTYPE closeRegistry(RegHandle hReg) +{ + if (hReg) + { + ORegistry *pReg = static_cast<ORegistry*>(hReg); + if (!pReg->isOpen()) + return RegError::REGISTRY_NOT_OPEN; + + RegError ret = RegError::NO_ERROR; + if (pReg->release() == 0) + { + delete pReg; + hReg = nullptr; + } + else + ret = pReg->closeRegistry(); + + return ret; + } else + { + return RegError::INVALID_REGISTRY; + } +} + + +// destroyRegistry + +static RegError REGISTRY_CALLTYPE destroyRegistry(RegHandle hReg, + rtl_uString* registryName) +{ + if (hReg) + { + ORegistry *pReg = static_cast<ORegistry*>(hReg); + if (!pReg->isOpen()) + return RegError::INVALID_REGISTRY; + + RegError ret = pReg->destroyRegistry(registryName); + if (ret == RegError::NO_ERROR) + { + if (!registryName->length) + { + delete pReg; + hReg = nullptr; + } + } + return ret; + } else + { + return RegError::INVALID_REGISTRY; + } +} + + +// mergeKey + +static RegError REGISTRY_CALLTYPE mergeKey(RegHandle hReg, + RegKeyHandle hKey, + rtl_uString* keyName, + rtl_uString* regFileName, + sal_Bool bWarnings, + sal_Bool bReport) +{ + ORegistry* pReg = static_cast< ORegistry* >(hReg); + if (!pReg) + return RegError::INVALID_REGISTRY; + if (!pReg->isOpen()) + return RegError::REGISTRY_NOT_OPEN; + + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (!pKey) + return RegError::INVALID_KEY; + if (pKey->getRegistry() != pReg) + return RegError::INVALID_KEY; + if (pKey->isDeleted()) + return RegError::INVALID_KEY; + if (pKey->isReadOnly()) + return RegError::REGISTRY_READONLY; + + if (keyName->length) + { + ORegKey* pNewKey = nullptr; + RegError _ret = pKey->createKey(OUString::unacquired(&keyName), reinterpret_cast<RegKeyHandle*>(&pNewKey)); + if (_ret != RegError::NO_ERROR) + return _ret; + + _ret = pReg->loadKey(pNewKey, regFileName, bWarnings, bReport); + if (_ret != RegError::NO_ERROR && (_ret != RegError::MERGE_CONFLICT || bWarnings)) + { + if (pNewKey != pKey) + (void) pKey->closeKey(pNewKey); + else + (void) pKey->releaseKey(pNewKey); + return _ret; + } + + return (pNewKey != pKey) ? pKey->closeKey(pNewKey) : pKey->releaseKey(pNewKey); + } + + return pReg->loadKey(pKey, regFileName, bWarnings, bReport); +} + + +// dumpRegistry + +static RegError REGISTRY_CALLTYPE dumpRegistry(RegHandle hReg, + RegKeyHandle hKey) +{ + ORegistry* pReg = static_cast< ORegistry* >(hReg); + if (!pReg) + return RegError::INVALID_REGISTRY; + if (!pReg->isOpen()) + return RegError::REGISTRY_NOT_OPEN; + + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (!pKey) + return RegError::INVALID_KEY; + if (pKey->getRegistry() != pReg) + return RegError::INVALID_KEY; + if (pKey->isDeleted()) + return RegError::INVALID_KEY; + + return pReg->dumpRegistry(hKey); +} + + +// initRegistry_Api + +Registry_Api* REGISTRY_CALLTYPE initRegistry_Api() +{ + static Registry_Api aApi= {&acquire, + &release, + &isReadOnly, + &openRootKey, + &getName, + &createRegistry, + &openRegistry, + &closeRegistry, + &destroyRegistry, + &mergeKey, + &acquireKey, + &releaseKey, + &isKeyReadOnly, + &getKeyName, + &createKey, + &openKey, + &openSubKeys, + &closeSubKeys, + &deleteKey, + &closeKey, + &setValue, + &setLongListValue, + &setStringListValue, + &setUnicodeListValue, + &getValueInfo, + &getValue, + &getLongListValue, + &getStringListValue, + &getUnicodeListValue, + &freeValueList, + &getResolvedKeyName, + &getKeyNames, + &freeKeyNames}; + + return (&aApi); +} + +} + + +// reg_openRootKey + +RegError REGISTRY_CALLTYPE reg_openRootKey(RegHandle hRegistry, + RegKeyHandle* phRootKey) +{ + return openRootKey(hRegistry, phRootKey); +} + + +// reg_openRegistry + +RegError REGISTRY_CALLTYPE reg_openRegistry(rtl_uString* registryName, + RegHandle* phRegistry) +{ + RegError _ret; + + ORegistry* pReg = new ORegistry(); + if ((_ret = pReg->initRegistry(registryName, RegAccessMode::READONLY)) != RegError::NO_ERROR) + { + delete pReg; + *phRegistry = nullptr; + return _ret; + } + + *phRegistry = pReg; + + return RegError::NO_ERROR; +} + + +// reg_closeRegistry + +RegError REGISTRY_CALLTYPE reg_closeRegistry(RegHandle hRegistry) +{ + if (hRegistry) + { + ORegistry* pReg = static_cast<ORegistry*>(hRegistry); + delete pReg; + return RegError::NO_ERROR; + } else + { + return RegError::REGISTRY_NOT_OPEN; + } +} + + +// reg_dumpRegistry + +RegError REGISTRY_CALLTYPE reg_dumpRegistry(RegKeyHandle hKey) +{ + ORegKey *pKey; + + if (hKey) + pKey = static_cast<ORegKey*>(hKey); + else + return RegError::INVALID_KEY; + + return dumpRegistry(pKey->getRegistry(), hKey); +} + + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/registry/source/regkey.cxx b/registry/source/regkey.cxx new file mode 100644 index 000000000..4f0d51897 --- /dev/null +++ b/registry/source/regkey.cxx @@ -0,0 +1,694 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include "regkey.hxx" + +#include <osl/diagnose.h> +#include "regimpl.hxx" +#include "keyimpl.hxx" + + +// acquireKey + +void REGISTRY_CALLTYPE acquireKey(RegKeyHandle hKey) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (pKey != nullptr) + { + ORegistry* pReg = pKey->getRegistry(); + (void) pReg->acquireKey(pKey); + } +} + + +// releaseKey + +void REGISTRY_CALLTYPE releaseKey(RegKeyHandle hKey) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (pKey != nullptr) + { + ORegistry* pReg = pKey->getRegistry(); + (void) pReg->releaseKey(pKey); + } +} + + +// isKeyReadOnly + +sal_Bool REGISTRY_CALLTYPE isKeyReadOnly(RegKeyHandle hKey) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + return pKey != nullptr && pKey->isReadOnly(); +} + + +// getKeyName + +RegError REGISTRY_CALLTYPE getKeyName(RegKeyHandle hKey, rtl_uString** pKeyName) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (pKey) + { + rtl_uString_assign( pKeyName, pKey->getName().pData ); + return RegError::NO_ERROR; + } else + { + rtl_uString_new(pKeyName); + return RegError::INVALID_KEY; + } +} + + +// createKey + +RegError REGISTRY_CALLTYPE createKey(RegKeyHandle hKey, + rtl_uString* keyName, + RegKeyHandle* phNewKey) +{ + *phNewKey = nullptr; + + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (!pKey) + return RegError::INVALID_KEY; + + if (pKey->isDeleted()) + return RegError::INVALID_KEY; + + if (pKey->isReadOnly()) + return RegError::REGISTRY_READONLY; + + return pKey->createKey(OUString::unacquired(&keyName), phNewKey); +} + + +// openKey + +RegError REGISTRY_CALLTYPE openKey(RegKeyHandle hKey, + rtl_uString* keyName, + RegKeyHandle* phOpenKey) +{ + *phOpenKey = nullptr; + + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (!pKey) + return RegError::INVALID_KEY; + + if (pKey->isDeleted()) + return RegError::INVALID_KEY; + + return pKey->openKey(OUString::unacquired(&keyName), phOpenKey); +} + + +// openSubKeys + +RegError REGISTRY_CALLTYPE openSubKeys(RegKeyHandle hKey, + rtl_uString* keyName, + RegKeyHandle** pphSubKeys, + sal_uInt32* pnSubKeys) +{ + *pphSubKeys = nullptr; + *pnSubKeys = 0; + + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (!pKey) + return RegError::INVALID_KEY; + + if (pKey->isDeleted()) + return RegError::INVALID_KEY; + + return pKey->openSubKeys(OUString::unacquired(&keyName), pphSubKeys, pnSubKeys); +} + + +// closeSubKeys + +RegError REGISTRY_CALLTYPE closeSubKeys(RegKeyHandle* phSubKeys, + sal_uInt32 nSubKeys) +{ + if (phSubKeys == nullptr || nSubKeys == 0) + return RegError::INVALID_KEY; + + ORegistry* pReg = static_cast<ORegKey*>(phSubKeys[0])->getRegistry(); + for (sal_uInt32 i = 0; i < nSubKeys; i++) + { + (void) pReg->closeKey(phSubKeys[i]); + } + std::free(phSubKeys); + + return RegError::NO_ERROR; +} + + +// deleteKey + +RegError REGISTRY_CALLTYPE deleteKey(RegKeyHandle hKey, + rtl_uString* keyName) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (!pKey) + return RegError::INVALID_KEY; + + if (pKey->isDeleted()) + return RegError::INVALID_KEY; + + if (pKey->isReadOnly()) + return RegError::REGISTRY_READONLY; + + return pKey->deleteKey(OUString::unacquired(&keyName)); +} + + +// closeKey + +RegError REGISTRY_CALLTYPE closeKey(RegKeyHandle hKey) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (!pKey) + return RegError::INVALID_KEY; + + return pKey->closeKey(hKey); +} + + +// setValue + +RegError REGISTRY_CALLTYPE setValue(RegKeyHandle hKey, + rtl_uString* keyName, + RegValueType valueType, + RegValue pData, + sal_uInt32 valueSize) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (!pKey) + return RegError::INVALID_KEY; + + if (pKey->isDeleted()) + return RegError::INVALID_KEY; + + if (pKey->isReadOnly()) + return RegError::REGISTRY_READONLY; + + OUString valueName("value"); + if (keyName->length) + { + ORegKey* pSubKey = nullptr; + RegError _ret1 = pKey->openKey(OUString::unacquired(&keyName), reinterpret_cast<RegKeyHandle*>(&pSubKey)); + if (_ret1 != RegError::NO_ERROR) + return _ret1; + + _ret1 = pSubKey->setValue(valueName, valueType, pData, valueSize); + if (_ret1 != RegError::NO_ERROR) + { + RegError _ret2 = pKey->closeKey(pSubKey); + if (_ret2 != RegError::NO_ERROR) + return _ret2; + else + return _ret1; + } + + return pKey->closeKey(pSubKey); + } + + return pKey->setValue(valueName, valueType, pData, valueSize); +} + + +// setLongValueList + +RegError REGISTRY_CALLTYPE setLongListValue(RegKeyHandle hKey, + rtl_uString* keyName, + sal_Int32 const * pValueList, + sal_uInt32 len) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (!pKey) + return RegError::INVALID_KEY; + + if (pKey->isDeleted()) + return RegError::INVALID_KEY; + + if (pKey->isReadOnly()) + return RegError::REGISTRY_READONLY; + + OUString valueName("value"); + if (keyName->length) + { + ORegKey* pSubKey = nullptr; + RegError _ret1 = pKey->openKey(OUString::unacquired(&keyName), reinterpret_cast<RegKeyHandle*>(&pSubKey)); + if (_ret1 != RegError::NO_ERROR) + return _ret1; + + _ret1 = pSubKey->setLongListValue(valueName, pValueList, len); + if (_ret1 != RegError::NO_ERROR) + { + RegError _ret2 = pKey->closeKey(pSubKey); + if (_ret2 != RegError::NO_ERROR) + return _ret2; + else + return _ret1; + } + + return pKey->closeKey(pSubKey); + } + + return pKey->setLongListValue(valueName, pValueList, len); +} + + +// setStringValueList + +RegError REGISTRY_CALLTYPE setStringListValue(RegKeyHandle hKey, + rtl_uString* keyName, + char** pValueList, + sal_uInt32 len) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (!pKey) + return RegError::INVALID_KEY; + + if (pKey->isDeleted()) + return RegError::INVALID_KEY; + + if (pKey->isReadOnly()) + return RegError::REGISTRY_READONLY; + + OUString valueName("value"); + if (keyName->length) + { + ORegKey* pSubKey = nullptr; + RegError _ret1 = pKey->openKey(OUString::unacquired(&keyName), reinterpret_cast<RegKeyHandle*>(&pSubKey)); + if (_ret1 != RegError::NO_ERROR) + return _ret1; + + _ret1 = pSubKey->setStringListValue(valueName, pValueList, len); + if (_ret1 != RegError::NO_ERROR) + { + RegError _ret2 = pKey->closeKey(pSubKey); + if (_ret2 != RegError::NO_ERROR) + return _ret2; + else + return _ret1; + } + + return pKey->closeKey(pSubKey); + } + + return pKey->setStringListValue(valueName, pValueList, len); +} + + +// setUnicodeValueList + +RegError REGISTRY_CALLTYPE setUnicodeListValue(RegKeyHandle hKey, + rtl_uString* keyName, + sal_Unicode** pValueList, + sal_uInt32 len) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (!pKey) + return RegError::INVALID_KEY; + + if (pKey->isDeleted()) + return RegError::INVALID_KEY; + + if (pKey->isReadOnly()) + return RegError::REGISTRY_READONLY; + + OUString valueName("value"); + if (keyName->length) + { + ORegKey* pSubKey = nullptr; + RegError _ret1 = pKey->openKey(OUString::unacquired(&keyName), reinterpret_cast<RegKeyHandle*>(&pSubKey)); + if (_ret1 != RegError::NO_ERROR) + return _ret1; + + _ret1 = pSubKey->setUnicodeListValue(valueName, pValueList, len); + if (_ret1 != RegError::NO_ERROR) + { + RegError _ret2 = pKey->closeKey(pSubKey); + if (_ret2 != RegError::NO_ERROR) + return _ret2; + else + return _ret1; + } + + return pKey->closeKey(pSubKey); + } + + return pKey->setUnicodeListValue(valueName, pValueList, len); +} + + +// getValueInfo + +RegError REGISTRY_CALLTYPE getValueInfo(RegKeyHandle hKey, + rtl_uString* keyName, + RegValueType* pValueType, + sal_uInt32* pValueSize) +{ + *pValueType = RegValueType::NOT_DEFINED; + *pValueSize = 0; + + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (!pKey) + return RegError::INVALID_KEY; + + if (pKey->isDeleted()) + return RegError::INVALID_KEY; + + RegValueType valueType; + sal_uInt32 valueSize; + + OUString valueName("value"); + if (keyName->length) + { + ORegKey* pSubKey = nullptr; + RegError _ret = pKey->openKey(OUString::unacquired(&keyName), reinterpret_cast<RegKeyHandle*>(&pSubKey)); + if (_ret != RegError::NO_ERROR) + return _ret; + + if (pSubKey->getValueInfo(valueName, &valueType, &valueSize) != RegError::NO_ERROR) + { + (void) pKey->releaseKey(pSubKey); + return RegError::INVALID_VALUE; + } + + *pValueType = valueType; + *pValueSize = valueSize; + + return pKey->releaseKey(pSubKey); + } + + + if (pKey->getValueInfo(valueName, &valueType, &valueSize) != RegError::NO_ERROR) + { + return RegError::INVALID_VALUE; + } + + *pValueType = valueType; + *pValueSize = valueSize; + + return RegError::NO_ERROR; +} + + +// getValueInfo + +RegError REGISTRY_CALLTYPE getValue(RegKeyHandle hKey, + rtl_uString* keyName, + RegValue pValue) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (!pKey) + return RegError::INVALID_KEY; + + if (pKey->isDeleted()) + return RegError::INVALID_KEY; + + OUString valueName("value"); + if (keyName->length) + { + ORegKey* pSubKey = nullptr; + RegError _ret1 = pKey->openKey(OUString::unacquired(&keyName), reinterpret_cast<RegKeyHandle*>(&pSubKey)); + if (_ret1 != RegError::NO_ERROR) + return _ret1; + + _ret1 = pSubKey->getValue(valueName, pValue); + if (_ret1 != RegError::NO_ERROR) + { + (void) pKey->releaseKey(pSubKey); + return _ret1; + } + + return pKey->releaseKey(pSubKey); + } + + return pKey->getValue(valueName, pValue); +} + + +// getLongValueList + +RegError REGISTRY_CALLTYPE getLongListValue(RegKeyHandle hKey, + rtl_uString* keyName, + sal_Int32** pValueList, + sal_uInt32* pLen) +{ + assert((pValueList != nullptr) && (pLen != nullptr) && "registry::getLongListValue(): invalid parameter"); + *pValueList = nullptr; + *pLen = 0; + + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (!pKey) + return RegError::INVALID_KEY; + + if (pKey->isDeleted()) + return RegError::INVALID_KEY; + + OUString valueName("value"); + if (keyName->length) + { + ORegKey* pSubKey = nullptr; + RegError _ret1 = pKey->openKey(OUString::unacquired(&keyName), reinterpret_cast<RegKeyHandle*>(&pSubKey)); + if (_ret1 != RegError::NO_ERROR) + return _ret1; + + _ret1 = pSubKey->getLongListValue(valueName, pValueList, pLen); + if (_ret1 != RegError::NO_ERROR) + { + (void) pKey->releaseKey(pSubKey); + return _ret1; + } + + return pKey->releaseKey(pSubKey); + } + + return pKey->getLongListValue(valueName, pValueList, pLen); +} + + +// getStringValueList + +RegError REGISTRY_CALLTYPE getStringListValue(RegKeyHandle hKey, + rtl_uString* keyName, + char*** pValueList, + sal_uInt32* pLen) +{ + OSL_PRECOND((pValueList != nullptr) && (pLen != nullptr), "registry::getStringListValue(): invalid parameter"); + *pValueList = nullptr; + *pLen = 0; + + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (!pKey) + return RegError::INVALID_KEY; + + if (pKey->isDeleted()) + return RegError::INVALID_KEY; + + OUString valueName("value"); + if (keyName->length) + { + ORegKey* pSubKey = nullptr; + RegError _ret1 = pKey->openKey(OUString::unacquired(&keyName), reinterpret_cast<RegKeyHandle*>(&pSubKey)); + if (_ret1 != RegError::NO_ERROR) + return _ret1; + + _ret1 = pSubKey->getStringListValue(valueName, pValueList, pLen); + if (_ret1 != RegError::NO_ERROR) + { + (void) pKey->releaseKey(pSubKey); + return _ret1; + } + + return pKey->releaseKey(pSubKey); + } + + return pKey->getStringListValue(valueName, pValueList, pLen); +} + +// getUnicodeListValue +RegError REGISTRY_CALLTYPE getUnicodeListValue(RegKeyHandle hKey, + rtl_uString* keyName, + sal_Unicode*** pValueList, + sal_uInt32* pLen) +{ + assert((pValueList != nullptr) && (pLen != nullptr) && "registry::getUnicodeListValue(): invalid parameter"); + *pValueList = nullptr; + *pLen = 0; + + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (!pKey) + return RegError::INVALID_KEY; + + if (pKey->isDeleted()) + return RegError::INVALID_KEY; + + OUString valueName("value"); + if (keyName->length) + { + ORegKey* pSubKey = nullptr; + RegError _ret1 = pKey->openKey(OUString::unacquired(&keyName), reinterpret_cast<RegKeyHandle*>(&pSubKey)); + if (_ret1 != RegError::NO_ERROR) + return _ret1; + + _ret1 = pSubKey->getUnicodeListValue(valueName, pValueList, pLen); + if (_ret1 != RegError::NO_ERROR) + { + (void) pKey->releaseKey(pSubKey); + return _ret1; + } + + return pKey->releaseKey(pSubKey); + } + + return pKey->getUnicodeListValue(valueName, pValueList, pLen); +} + + +// freeValueList + +RegError REGISTRY_CALLTYPE freeValueList(RegValueType valueType, + RegValue pValueList, + sal_uInt32 len) +{ + switch (valueType) + { + case RegValueType::LONGLIST: + { + std::free(pValueList); + } + break; + case RegValueType::STRINGLIST: + { + char** pVList = static_cast<char**>(pValueList); + for (sal_uInt32 i=0; i < len; i++) + { + std::free(pVList[i]); + } + + std::free(pVList); + } + break; + case RegValueType::UNICODELIST: + { + sal_Unicode** pVList = static_cast<sal_Unicode**>(pValueList); + for (sal_uInt32 i=0; i < len; i++) + { + std::free(pVList[i]); + } + + std::free(pVList); + } + break; + default: + return RegError::INVALID_VALUE; + } + + pValueList = nullptr; + return RegError::NO_ERROR; +} + + +// getName + +RegError REGISTRY_CALLTYPE getResolvedKeyName(RegKeyHandle hKey, + rtl_uString* keyName, + SAL_UNUSED_PARAMETER sal_Bool, + rtl_uString** pResolvedName) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (!pKey) + return RegError::INVALID_KEY; + + if (pKey->isDeleted()) + return RegError::INVALID_KEY; + + OUString resolvedName; + RegError _ret = pKey->getResolvedKeyName(OUString::unacquired(&keyName), resolvedName); + if (_ret == RegError::NO_ERROR) + rtl_uString_assign(pResolvedName, resolvedName.pData); + return _ret; +} + + +// getKeyNames + +RegError REGISTRY_CALLTYPE getKeyNames(RegKeyHandle hKey, + rtl_uString* keyName, + rtl_uString*** pSubKeyNames, + sal_uInt32* pnSubKeys) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (!pKey) + return RegError::INVALID_KEY; + + if (pKey->isDeleted()) + return RegError::INVALID_KEY; + + return pKey->getKeyNames(OUString::unacquired(&keyName), pSubKeyNames, pnSubKeys); +} + + +// freeKeyNames + +RegError REGISTRY_CALLTYPE freeKeyNames(rtl_uString** pKeyNames, + sal_uInt32 nKeys) +{ + for (sal_uInt32 i=0; i < nKeys; i++) + { + rtl_uString_release(pKeyNames[i]); + } + + std::free(pKeyNames); + + return RegError::NO_ERROR; +} + + +// C API + + +// reg_openKey + +RegError REGISTRY_CALLTYPE reg_openKey(RegKeyHandle hKey, + rtl_uString* keyName, + RegKeyHandle* phOpenKey) +{ + if (!hKey) + return RegError::INVALID_KEY; + + return openKey(hKey, keyName, phOpenKey); +} + + +// reg_closeKey + +RegError REGISTRY_CALLTYPE reg_closeKey(RegKeyHandle hKey) +{ + if (!hKey) + return RegError::INVALID_KEY; + + return closeKey(hKey); +} + + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/registry/source/regkey.hxx b/registry/source/regkey.hxx new file mode 100644 index 000000000..76e8c2993 --- /dev/null +++ b/registry/source/regkey.hxx @@ -0,0 +1,69 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_REGISTRY_SOURCE_REGKEY_HXX +#define INCLUDED_REGISTRY_SOURCE_REGKEY_HXX + +#include <sal/config.h> +#include <registry/regtype.h> +#include <rtl/ustring.h> +#include <sal/types.h> + +extern "C" { + +void REGISTRY_CALLTYPE acquireKey(RegKeyHandle); +void REGISTRY_CALLTYPE releaseKey(RegKeyHandle); +sal_Bool REGISTRY_CALLTYPE isKeyReadOnly(RegKeyHandle); +RegError REGISTRY_CALLTYPE getKeyName(RegKeyHandle, rtl_uString**); +RegError REGISTRY_CALLTYPE createKey(RegKeyHandle, rtl_uString*, RegKeyHandle*); +RegError REGISTRY_CALLTYPE openKey(RegKeyHandle, rtl_uString*, RegKeyHandle*); +RegError REGISTRY_CALLTYPE openSubKeys( + RegKeyHandle, rtl_uString*, RegKeyHandle**, sal_uInt32*); +RegError REGISTRY_CALLTYPE closeSubKeys(RegKeyHandle*, sal_uInt32); +RegError REGISTRY_CALLTYPE deleteKey(RegKeyHandle, rtl_uString*); +RegError REGISTRY_CALLTYPE closeKey(RegKeyHandle); +RegError REGISTRY_CALLTYPE setValue( + RegKeyHandle, rtl_uString*, RegValueType, RegValue, sal_uInt32); +RegError REGISTRY_CALLTYPE setLongListValue( + RegKeyHandle, rtl_uString*, sal_Int32 const *, sal_uInt32); +RegError REGISTRY_CALLTYPE setStringListValue( + RegKeyHandle, rtl_uString*, char**, sal_uInt32); +RegError REGISTRY_CALLTYPE setUnicodeListValue( + RegKeyHandle, rtl_uString*, sal_Unicode**, sal_uInt32); +RegError REGISTRY_CALLTYPE getValueInfo( + RegKeyHandle, rtl_uString*, RegValueType*, sal_uInt32*); +RegError REGISTRY_CALLTYPE getValue(RegKeyHandle, rtl_uString*, RegValue); +RegError REGISTRY_CALLTYPE getLongListValue( + RegKeyHandle, rtl_uString*, sal_Int32**, sal_uInt32*); +RegError REGISTRY_CALLTYPE getStringListValue( + RegKeyHandle, rtl_uString*, char***, sal_uInt32*); +RegError REGISTRY_CALLTYPE getUnicodeListValue( + RegKeyHandle, rtl_uString*, sal_Unicode***, sal_uInt32*); +RegError REGISTRY_CALLTYPE freeValueList(RegValueType, RegValue, sal_uInt32); +RegError REGISTRY_CALLTYPE getResolvedKeyName( + RegKeyHandle, rtl_uString*, sal_Bool, rtl_uString**); +RegError REGISTRY_CALLTYPE getKeyNames( + RegKeyHandle, rtl_uString*, rtl_uString***, sal_uInt32*); +RegError REGISTRY_CALLTYPE freeKeyNames(rtl_uString**, sal_uInt32); + +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |