diff options
Diffstat (limited to 'registry/source')
-rw-r--r-- | registry/source/keyimpl.cxx | 927 | ||||
-rw-r--r-- | registry/source/keyimpl.hxx | 140 | ||||
-rw-r--r-- | registry/source/reflcnst.hxx | 188 | ||||
-rw-r--r-- | registry/source/reflread.cxx | 1609 | ||||
-rw-r--r-- | registry/source/reflwrit.cxx | 79 | ||||
-rw-r--r-- | registry/source/regimpl.cxx | 1177 | ||||
-rw-r--r-- | registry/source/regimpl.hxx | 123 | ||||
-rw-r--r-- | registry/source/registry.cxx | 301 | ||||
-rw-r--r-- | registry/source/regkey.cxx | 618 | ||||
-rw-r--r-- | registry/source/regkey.hxx | 66 |
10 files changed, 5228 insertions, 0 deletions
diff --git a/registry/source/keyimpl.cxx b/registry/source/keyimpl.cxx new file mode 100644 index 0000000000..34e51483e7 --- /dev/null +++ b/registry/source/keyimpl.cxx @@ -0,0 +1,927 @@ +/* -*- 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> +#include <utility> + +using namespace store; + +namespace { char const VALUE_PREFIX[] = "$VL_"; } + +ORegKey::ORegKey(OUString keyName, ORegistry* pReg) + : m_refCount(1) + , m_name(std::move(keyName)) + , m_bDeleted(false) + , m_bModified(false) + , m_pRegistry(pReg) +{ +} + +ORegKey::~ORegKey() +{ + SAL_WARN_IF(m_refCount != 0, "registry", "registry::ORegKey::dtor(): refcount not zero."); +} + +RegError ORegKey::releaseKey(RegKeyHandle hKey) +{ + return m_pRegistry->releaseKey(hKey); +} + +RegError ORegKey::createKey(std::u16string_view keyName, RegKeyHandle* phNewKey) +{ + return m_pRegistry->createKey(this, keyName, phNewKey); +} + +RegError ORegKey::openKey(std::u16string_view keyName, RegKeyHandle* phOpenKey) +{ + return m_pRegistry->openKey(this, keyName, phOpenKey); +} + +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; +} + +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; +} + +RegError ORegKey::closeKey(RegKeyHandle hKey) +{ + return m_pRegistry->closeKey(hKey); +} + +RegError ORegKey::deleteKey(std::u16string_view keyName) +{ + return m_pRegistry->deleteKey(this, keyName); +} + +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; +} + +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; +} + +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; +} + +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; +} + +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; +} + +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; +} + +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; +} + +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; +} + +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; +} + +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 0000000000..4bfbcec3b7 --- /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(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 0000000000..2c93a9cd22 --- /dev/null +++ b/registry/source/reflcnst.hxx @@ -0,0 +1,188 @@ +/* -*- 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 . + */ + +#pragma once + +#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 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 +}; + +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 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 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); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/registry/source/reflread.cxx b/registry/source/reflread.cxx new file mode 100644 index 0000000000..fe223c7b46 --- /dev/null +++ b/registry/source/reflread.cxx @@ -0,0 +1,1609 @@ +/* -*- 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 <sal/log.hxx> + +#include <registry/refltype.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; + +namespace { + +/** + Holds any data in a flat memory buffer + */ +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; +} + +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; +} + +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; +} + +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; +} + +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; +} + +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; +} + +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); +} + +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; + } +} + +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); +} + + +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; +} + +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; +} + +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); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/registry/source/reflwrit.cxx b/registry/source/reflwrit.cxx new file mode 100644 index 0000000000..5304d03463 --- /dev/null +++ b/registry/source/reflwrit.cxx @@ -0,0 +1,79 @@ +/* -*- 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/types.h> +#include <rtl/ustring.h> + +#include <registry/refltype.hxx> + +#include "reflcnst.hxx" + + +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); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/registry/source/regimpl.cxx b/registry/source/regimpl.cxx new file mode 100644 index 0000000000..7dcbaa272d --- /dev/null +++ b/registry/source/regimpl.cxx @@ -0,0 +1,1177 @@ +/* -*- 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 <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 + "/"); + } + } 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; + // coverity[leaked_storage : FALSE] - ownership transferred to m_openKeyTable + 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; +} + +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 0000000000..d13d42b6d3 --- /dev/null +++ b/registry/source/regimpl.hxx @@ -0,0 +1,123 @@ +/* -*- 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 . + */ + +#pragma once + +#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 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 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); + + 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 OUString ROOT { u"/"_ustr }; +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/registry/source/registry.cxx b/registry/source/registry.cxx new file mode 100644 index 0000000000..c498f113c7 --- /dev/null +++ b/registry/source/registry.cxx @@ -0,0 +1,301 @@ +/* -*- 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" { + +static void REGISTRY_CALLTYPE acquire(RegHandle hReg) +{ + ORegistry* pReg = static_cast<ORegistry*>(hReg); + + if (pReg != nullptr) + pReg->acquire(); +} + +static void REGISTRY_CALLTYPE release(RegHandle hReg) +{ + ORegistry* pReg = static_cast<ORegistry*>(hReg); + + if (pReg && pReg->release() == 0) + { + delete pReg; + hReg = nullptr; + } +} + +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; +} + +static sal_Bool REGISTRY_CALLTYPE isReadOnly(RegHandle hReg) +{ + if (hReg) + return static_cast<ORegistry*>(hReg)->isReadOnly(); + else + return false; +} + +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; +} + +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; +} + +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; +} + +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; + } +} + +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; + } +} + + +// 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); +} + +Registry_Api* REGISTRY_CALLTYPE initRegistry_Api() +{ + static Registry_Api aApi= {&acquire, + &release, + &isReadOnly, + &openRootKey, + &getName, + &createRegistry, + &openRegistry, + &closeRegistry, + &destroyRegistry, + &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); +} + +} + +RegError REGISTRY_CALLTYPE reg_openRootKey(RegHandle hRegistry, + RegKeyHandle* phRootKey) +{ + return openRootKey(hRegistry, phRootKey); +} + +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; +} + +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; + } +} + +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 0000000000..b2eeb63e67 --- /dev/null +++ b/registry/source/regkey.cxx @@ -0,0 +1,618 @@ +/* -*- 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" + +void REGISTRY_CALLTYPE acquireKey(RegKeyHandle hKey) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (pKey != nullptr) + { + ORegistry* pReg = pKey->getRegistry(); + (void) pReg->acquireKey(pKey); + } +} + +void REGISTRY_CALLTYPE releaseKey(RegKeyHandle hKey) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (pKey != nullptr) + { + ORegistry* pReg = pKey->getRegistry(); + (void) pReg->releaseKey(pKey); + } +} + +sal_Bool REGISTRY_CALLTYPE isKeyReadOnly(RegKeyHandle hKey) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + return pKey != nullptr && pKey->isReadOnly(); +} + +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; + } +} + +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); +} + +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); +} + +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); +} + +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; +} + +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)); +} + +RegError REGISTRY_CALLTYPE closeKey(RegKeyHandle hKey) +{ + ORegKey* pKey = static_cast< ORegKey* >(hKey); + if (!pKey) + return RegError::INVALID_KEY; + + return pKey->closeKey(hKey); +} + +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); +} + +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); +} + +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); +} + +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); +} + +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; +} + +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); +} + +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); +} + +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); +} + +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); +} + +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; +} + +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; +} + +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); +} + +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; +} + +RegError REGISTRY_CALLTYPE reg_openKey(RegKeyHandle hKey, + rtl_uString* keyName, + RegKeyHandle* phOpenKey) +{ + if (!hKey) + return RegError::INVALID_KEY; + + return openKey(hKey, keyName, phOpenKey); +} + +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 0000000000..51114e5ccc --- /dev/null +++ b/registry/source/regkey.hxx @@ -0,0 +1,66 @@ +/* -*- 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 . + */ + +#pragma once + +#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); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |