1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "regimpl.hxx"
31 #include "reflread.hxx"
33 #include "reflwrit.hxx"
35 #include <registry/reader.hxx>
36 #include <registry/refltype.hxx>
37 #include <registry/types.hxx>
38 #include <registry/version.h>
40 #include "reflcnst.hxx"
41 #include "keyimpl.hxx"
43 #include <osl/thread.h>
44 #include <rtl/alloc.h>
45 #include <rtl/ustring.hxx>
46 #include <rtl/ustrbuf.hxx>
47 #include <osl/file.hxx>
50 using namespace store
;
55 void printString(OUString
const & s
) {
57 for (sal_Int32 i
= 0; i
< s
.getLength(); ++i
) {
59 if (c
== '"' || c
== '\\') {
60 printf("\\%c", static_cast< char >(c
));
61 } else if (s
[i
] >= ' ' && s
[i
] <= '~') {
62 printf("%c", static_cast< char >(c
));
64 printf("\\u%04X", static_cast< unsigned int >(c
));
70 void printFieldOrReferenceFlag(
71 RTFieldAccess
* flags
, RTFieldAccess flag
, char const * name
, bool * first
)
73 if ((*flags
& flag
) != RTFieldAccess::NONE
) {
83 void printFieldOrReferenceFlags(RTFieldAccess flags
) {
84 if (flags
== RTFieldAccess::NONE
) {
88 printFieldOrReferenceFlag(
89 &flags
, RTFieldAccess::READONLY
, "readonly", &first
);
90 printFieldOrReferenceFlag(
91 &flags
, RTFieldAccess::OPTIONAL
, "optional", &first
);
92 printFieldOrReferenceFlag(
93 &flags
, RTFieldAccess::MAYBEVOID
, "maybevoid", &first
);
94 printFieldOrReferenceFlag(&flags
, RTFieldAccess::BOUND
, "bound", &first
);
95 printFieldOrReferenceFlag(
96 &flags
, RTFieldAccess::CONSTRAINED
, "constrained", &first
);
97 printFieldOrReferenceFlag(
98 &flags
, RTFieldAccess::TRANSIENT
, "transient", &first
);
99 printFieldOrReferenceFlag(
100 &flags
, RTFieldAccess::MAYBEAMBIGUOUS
, "maybeambiguous", &first
);
101 printFieldOrReferenceFlag(
102 &flags
, RTFieldAccess::MAYBEDEFAULT
, "maybedefault", &first
);
103 printFieldOrReferenceFlag(
104 &flags
, RTFieldAccess::REMOVABLE
, "removable", &first
);
105 printFieldOrReferenceFlag(
106 &flags
, RTFieldAccess::ATTRIBUTE
, "attribute", &first
);
107 printFieldOrReferenceFlag(
108 &flags
, RTFieldAccess::PROPERTY
, "property", &first
);
109 printFieldOrReferenceFlag(&flags
, RTFieldAccess::CONST
, "const", &first
);
110 printFieldOrReferenceFlag(
111 &flags
, RTFieldAccess::READWRITE
, "readwrite", &first
);
112 printFieldOrReferenceFlag(
113 &flags
, RTFieldAccess::PARAMETERIZED_TYPE
, "parameterized type", &first
);
114 printFieldOrReferenceFlag(
115 &flags
, RTFieldAccess::PUBLISHED
, "published", &first
);
116 if (flags
!= RTFieldAccess::NONE
) {
120 printf("<invalid (0x%04X)>", static_cast< unsigned int >(flags
));
125 void dumpType(typereg::Reader
const & reader
, OString
const & indent
) {
126 if (reader
.isValid()) {
127 printf("version: %ld\n", static_cast< long >(reader
.getVersion()));
128 printf("%sdocumentation: ", indent
.getStr());
129 printString(reader
.getDocumentation());
131 printf("%sfile name: ", indent
.getStr());
132 printString(reader
.getFileName());
134 printf("%stype class: ", indent
.getStr());
135 if (reader
.isPublished()) {
136 printf("published ");
138 switch (reader
.getTypeClass()) {
139 case RT_TYPE_INTERFACE
:
155 case RT_TYPE_EXCEPTION
:
159 case RT_TYPE_TYPEDEF
:
163 case RT_TYPE_SERVICE
:
167 case RT_TYPE_SINGLETON
:
171 case RT_TYPE_CONSTANTS
:
177 "<invalid (%ld)>", static_cast< long >(reader
.getTypeClass()));
181 printf("%stype name: ", indent
.getStr());
182 printString(reader
.getTypeName());
185 "%ssuper type count: %u\n", indent
.getStr(),
186 static_cast< unsigned int >(reader
.getSuperTypeCount()));
187 for (sal_uInt16 i
= 0; i
< reader
.getSuperTypeCount(); ++i
) {
189 "%ssuper type name %u: ", indent
.getStr(),
190 static_cast< unsigned int >(i
));
191 printString(reader
.getSuperTypeName(i
));
195 "%sfield count: %u\n", indent
.getStr(),
196 static_cast< unsigned int >(reader
.getFieldCount()));
197 for (sal_uInt16 i
= 0; i
< reader
.getFieldCount(); ++i
) {
199 "%sfield %u:\n", indent
.getStr(),
200 static_cast< unsigned int >(i
));
201 printf("%s documentation: ", indent
.getStr());
202 printString(reader
.getFieldDocumentation(i
));
204 printf("%s file name: ", indent
.getStr());
205 printString(reader
.getFieldFileName(i
));
207 printf("%s flags: ", indent
.getStr());
208 printFieldOrReferenceFlags(reader
.getFieldFlags(i
));
210 printf("%s name: ", indent
.getStr());
211 printString(reader
.getFieldName(i
));
213 printf("%s type name: ", indent
.getStr());
214 printString(reader
.getFieldTypeName(i
));
216 printf("%s value: ", indent
.getStr());
217 RTConstValue
value(reader
.getFieldValue(i
));
218 switch (value
.m_type
) {
224 printf("boolean %s", value
.m_value
.aBool
? "true" : "false");
228 printf("byte %d", static_cast< int >(value
.m_value
.aByte
));
232 printf("short %d", static_cast< int >(value
.m_value
.aShort
));
238 static_cast< unsigned int >(value
.m_value
.aUShort
));
242 printf("long %ld", static_cast< long >(value
.m_value
.aLong
));
248 static_cast< unsigned long >(value
.m_value
.aULong
));
252 // TODO: no portable way to print hyper values
257 // TODO: no portable way to print unsigned hyper values
258 printf("unsigned hyper");
262 // TODO: no portable way to print float values
267 // TODO: no portable way to print double values
273 printString(value
.m_value
.aString
);
277 printf("<invalid (%ld)>", static_cast< long >(value
.m_type
));
283 "%smethod count: %u\n", indent
.getStr(),
284 static_cast< unsigned int >(reader
.getMethodCount()));
285 for (sal_uInt16 i
= 0; i
< reader
.getMethodCount(); ++i
) {
287 "%smethod %u:\n", indent
.getStr(),
288 static_cast< unsigned int >(i
));
289 printf("%s documentation: ", indent
.getStr());
290 printString(reader
.getMethodDocumentation(i
));
292 printf("%s flags: ", indent
.getStr());
293 switch (reader
.getMethodFlags(i
)) {
294 case RTMethodMode::ONEWAY
:
298 case RTMethodMode::TWOWAY
:
299 printf("synchronous");
302 case RTMethodMode::ATTRIBUTE_GET
:
303 printf("attribute get");
306 case RTMethodMode::ATTRIBUTE_SET
:
307 printf("attribute set");
313 static_cast< long >(reader
.getMethodFlags(i
)));
317 printf("%s name: ", indent
.getStr());
318 printString(reader
.getMethodName(i
));
320 printf("%s return type name: ", indent
.getStr());
321 printString(reader
.getMethodReturnTypeName(i
));
324 "%s parameter count: %u\n", indent
.getStr(),
325 static_cast< unsigned int >(reader
.getMethodParameterCount(i
)));
326 // coverity[tainted_data] cid#1215304 unhelpfully warns about an
327 // untrusted loop bound here:
328 for (sal_uInt16 j
= 0; j
< reader
.getMethodParameterCount(i
); ++j
)
331 "%s parameter %u:\n", indent
.getStr(),
332 static_cast< unsigned int >(j
));
333 printf("%s flags: ", indent
.getStr());
334 RTParamMode flags
= reader
.getMethodParameterFlags(i
, j
);
335 bool rest
= (flags
& RT_PARAM_REST
) != 0;
336 switch (flags
& ~RT_PARAM_REST
) {
350 printf("<invalid (%ld)>", static_cast< long >(flags
));
358 printf("%s name: ", indent
.getStr());
359 printString(reader
.getMethodParameterName(i
, j
));
361 printf("%s type name: ", indent
.getStr());
362 printString(reader
.getMethodParameterTypeName(i
, j
));
366 "%s exception count: %u\n", indent
.getStr(),
367 static_cast< unsigned int >(reader
.getMethodExceptionCount(i
)));
368 // coverity[tainted_data] cid#1215304 unhelpfully warns about an
369 // untrusted loop bound here:
370 for (sal_uInt16 j
= 0; j
< reader
.getMethodExceptionCount(i
); ++j
)
373 "%s exception type name %u: ", indent
.getStr(),
374 static_cast< unsigned int >(j
));
375 printString(reader
.getMethodExceptionTypeName(i
, j
));
380 "%sreference count: %u\n", indent
.getStr(),
381 static_cast< unsigned int >(reader
.getReferenceCount()));
382 for (sal_uInt16 i
= 0; i
< reader
.getReferenceCount(); ++i
) {
384 "%sreference %u:\n", indent
.getStr(),
385 static_cast< unsigned int >(i
));
386 printf("%s documentation: ", indent
.getStr());
387 printString(reader
.getReferenceDocumentation(i
));
389 printf("%s flags: ", indent
.getStr());
390 printFieldOrReferenceFlags(reader
.getReferenceFlags(i
));
392 printf("%s sort: ", indent
.getStr());
393 switch (reader
.getReferenceSort(i
)) {
394 case RTReferenceType::SUPPORTS
:
398 case RTReferenceType::EXPORTS
:
402 case RTReferenceType::TYPE_PARAMETER
:
403 printf("type parameter");
409 static_cast< long >(reader
.getReferenceSort(i
)));
413 printf("%s type name: ", indent
.getStr());
414 printString(reader
.getReferenceTypeName(i
));
418 printf("<invalid>\n");
424 ORegistry::ORegistry()
432 ORegistry::~ORegistry()
434 ORegKey
* pRootKey
= m_openKeyTable
[ROOT
];
435 if (pRootKey
!= nullptr)
436 (void) releaseKey(pRootKey
);
438 if (m_file
.isValid())
442 RegError
ORegistry::initRegistry(const OUString
& regName
, RegAccessMode accessMode
, bool bCreate
)
444 RegError eRet
= RegError::INVALID_REGISTRY
;
446 storeAccessMode sAccessMode
= storeAccessMode::ReadWrite
;
451 sAccessMode
= storeAccessMode::Create
;
453 else if (accessMode
& RegAccessMode::READONLY
)
455 sAccessMode
= storeAccessMode::ReadOnly
;
459 if (regName
.isEmpty() &&
460 storeAccessMode::Create
== sAccessMode
)
462 errCode
= rRegFile
.createInMemory();
466 errCode
= rRegFile
.create(regName
, sAccessMode
);
473 case store_E_NotExists
:
474 eRet
= RegError::REGISTRY_NOT_EXISTS
;
476 case store_E_LockingViolation
:
477 eRet
= RegError::CANNOT_OPEN_FOR_READWRITE
;
480 eRet
= RegError::INVALID_REGISTRY
;
486 OStoreDirectory rStoreDir
;
487 storeError _err
= rStoreDir
.create(rRegFile
, OUString(), OUString(), sAccessMode
);
489 if (_err
== store_E_None
)
495 m_openKeyTable
[ROOT
] = new ORegKey(ROOT
, this);
496 eRet
= RegError::NO_ERROR
;
499 eRet
= RegError::INVALID_REGISTRY
;
505 RegError
ORegistry::closeRegistry()
509 if (m_file
.isValid())
511 (void) releaseKey(m_openKeyTable
[ROOT
]);
514 return RegError::NO_ERROR
;
517 return RegError::REGISTRY_NOT_EXISTS
;
521 RegError
ORegistry::destroyRegistry(const OUString
& regName
)
525 if (!regName
.isEmpty())
527 std::unique_ptr
<ORegistry
> pReg(new ORegistry());
529 if (pReg
->initRegistry(regName
, RegAccessMode::READWRITE
) == RegError::NO_ERROR
)
534 if (FileBase::getSystemPathFromFileURL(regName
, systemName
) != FileBase::E_None
)
535 systemName
= regName
;
537 OString
name(OUStringToOString(systemName
, osl_getThreadTextEncoding()));
538 if (unlink(name
.getStr()) != 0)
540 return RegError::DESTROY_REGISTRY_FAILED
;
544 return RegError::DESTROY_REGISTRY_FAILED
;
548 if (m_refCount
!= 1 || isReadOnly())
550 return RegError::DESTROY_REGISTRY_FAILED
;
553 if (m_file
.isValid())
555 releaseKey(m_openKeyTable
[ROOT
]);
559 if (!m_name
.isEmpty())
562 if (FileBase::getSystemPathFromFileURL(m_name
, systemName
) != FileBase::E_None
)
565 OString
name(OUStringToOString(systemName
, osl_getThreadTextEncoding()));
566 if (unlink(name
.getStr()) != 0)
568 return RegError::DESTROY_REGISTRY_FAILED
;
573 return RegError::REGISTRY_NOT_EXISTS
;
577 return RegError::NO_ERROR
;
580 RegError
ORegistry::acquireKey (RegKeyHandle hKey
)
582 ORegKey
* pKey
= static_cast< ORegKey
* >(hKey
);
584 return RegError::INVALID_KEY
;
589 return RegError::NO_ERROR
;
592 RegError
ORegistry::releaseKey (RegKeyHandle hKey
)
594 ORegKey
* pKey
= static_cast< ORegKey
* >(hKey
);
596 return RegError::INVALID_KEY
;
599 if (pKey
->release() == 0)
601 m_openKeyTable
.erase(pKey
->getName());
604 return RegError::NO_ERROR
;
607 RegError
ORegistry::createKey(RegKeyHandle hKey
, const OUString
& keyName
,
608 RegKeyHandle
* phNewKey
)
614 if (keyName
.isEmpty())
615 return RegError::INVALID_KEYNAME
;
620 pKey
= static_cast<ORegKey
*>(hKey
);
622 pKey
= m_openKeyTable
[ROOT
];
624 OUString sFullKeyName
= pKey
->getFullPath(keyName
);
626 if (m_openKeyTable
.count(sFullKeyName
) > 0)
628 *phNewKey
= m_openKeyTable
[sFullKeyName
];
629 static_cast<ORegKey
*>(*phNewKey
)->acquire();
630 static_cast<ORegKey
*>(*phNewKey
)->setDeleted(false);
631 return RegError::NO_ERROR
;
634 OStoreDirectory rStoreDir
;
635 OUStringBuffer
sFullPath(sFullKeyName
.getLength());
638 sFullPath
.append('/');
640 sal_Int32 nIndex
= 0;
643 token
= sFullKeyName
.getToken(0, '/', nIndex
);
644 if (!token
.isEmpty())
646 if (rStoreDir
.create(pKey
->getStoreFile(), sFullPath
.getStr(), token
, storeAccessMode::Create
))
648 return RegError::CREATE_KEY_FAILED
;
651 sFullPath
.append(token
);
652 sFullPath
.append('/');
654 } while(nIndex
!= -1);
657 pKey
= new ORegKey(sFullKeyName
, this);
659 m_openKeyTable
[sFullKeyName
] = pKey
;
661 return RegError::NO_ERROR
;
664 RegError
ORegistry::openKey(RegKeyHandle hKey
, const OUString
& keyName
,
665 RegKeyHandle
* phOpenKey
)
669 *phOpenKey
= nullptr;
671 if (keyName
.isEmpty())
673 return RegError::INVALID_KEYNAME
;
679 pKey
= static_cast<ORegKey
*>(hKey
);
681 pKey
= m_openKeyTable
[ROOT
];
683 OUString
path(pKey
->getFullPath(keyName
));
684 KeyMap::iterator
i(m_openKeyTable
.find(path
));
685 if (i
== m_openKeyTable
.end()) {
686 sal_Int32 n
= path
.lastIndexOf('/') + 1;
687 switch (OStoreDirectory().create(
688 pKey
->getStoreFile(), path
.copy(0, n
), path
.copy(n
),
689 isReadOnly() ? storeAccessMode::ReadOnly
: storeAccessMode::ReadWrite
))
691 case store_E_NotExists
:
692 return RegError::KEY_NOT_EXISTS
;
693 case store_E_WrongFormat
:
694 return RegError::INVALID_KEY
;
699 std::unique_ptr
< ORegKey
> p(new ORegKey(path
, this));
700 i
= m_openKeyTable
.insert(std::make_pair(path
, p
.get())).first
;
703 i
->second
->acquire();
705 *phOpenKey
= i
->second
;
706 return RegError::NO_ERROR
;
709 RegError
ORegistry::closeKey(RegKeyHandle hKey
)
711 ORegKey
* pKey
= static_cast< ORegKey
* >(hKey
);
715 OUString
const aKeyName (pKey
->getName());
716 if (m_openKeyTable
.count(aKeyName
) <= 0)
717 return RegError::KEY_NOT_OPEN
;
719 if (pKey
->isModified())
721 ORegKey
* pRootKey
= getRootKey();
722 if (pKey
!= pRootKey
)
724 // propagate "modified" state to RootKey.
725 pRootKey
->setModified();
729 // closing modified RootKey, flush registry file.
730 (void) m_file
.flush();
732 pKey
->setModified(false);
733 (void) releaseKey(pRootKey
);
736 return releaseKey(pKey
);
739 RegError
ORegistry::deleteKey(RegKeyHandle hKey
, const OUString
& keyName
)
741 ORegKey
* pKey
= static_cast< ORegKey
* >(hKey
);
742 if (keyName
.isEmpty())
743 return RegError::INVALID_KEYNAME
;
748 pKey
= m_openKeyTable
[ROOT
];
750 OUString
sFullKeyName(pKey
->getFullPath(keyName
));
751 return eraseKey(m_openKeyTable
[ROOT
], sFullKeyName
);
754 RegError
ORegistry::eraseKey(ORegKey
* pKey
, const OUString
& keyName
)
756 RegError _ret
= RegError::NO_ERROR
;
758 if (keyName
.isEmpty())
760 return RegError::INVALID_KEYNAME
;
763 OUString
sFullKeyName(pKey
->getName());
764 OUString
sFullPath(sFullKeyName
);
765 OUString sRelativKey
;
766 sal_Int32 lastIndex
= keyName
.lastIndexOf('/');
770 sRelativKey
+= keyName
.copy(lastIndex
+ 1);
772 if (sFullKeyName
.getLength() > 1)
773 sFullKeyName
+= keyName
;
775 sFullKeyName
+= keyName
.copy(1);
777 sFullPath
= sFullKeyName
.copy(0, keyName
.lastIndexOf('/') + 1);
780 if (sFullKeyName
.getLength() > 1)
781 sFullKeyName
+= ROOT
;
783 sRelativKey
+= keyName
;
784 sFullKeyName
+= keyName
;
786 if (sFullPath
.getLength() > 1)
790 ORegKey
* pOldKey
= nullptr;
791 _ret
= pKey
->openKey(keyName
, reinterpret_cast<RegKeyHandle
*>(&pOldKey
));
792 if (_ret
!= RegError::NO_ERROR
)
795 _ret
= deleteSubkeysAndValues(pOldKey
);
796 if (_ret
!= RegError::NO_ERROR
)
798 pKey
->closeKey(pOldKey
);
802 OUString
tmpName(sRelativKey
);
805 OStoreFile
sFile(pKey
->getStoreFile());
806 if (sFile
.isValid() && sFile
.remove(sFullPath
, tmpName
))
808 return RegError::DELETE_KEY_FAILED
;
810 pOldKey
->setModified();
812 // set flag deleted !!!
813 pOldKey
->setDeleted(true);
815 return pKey
->closeKey(pOldKey
);
818 RegError
ORegistry::deleteSubkeysAndValues(ORegKey
* pKey
)
820 OStoreDirectory::iterator iter
;
821 RegError _ret
= RegError::NO_ERROR
;
822 OStoreDirectory
rStoreDir(pKey
->getStoreDir());
823 storeError _err
= rStoreDir
.first(iter
);
825 while (_err
== store_E_None
)
827 OUString
const keyName
= iter
.m_pszName
;
829 if (iter
.m_nAttrib
& STORE_ATTRIB_ISDIR
)
831 _ret
= eraseKey(pKey
, keyName
);
832 if (_ret
!= RegError::NO_ERROR
)
837 OUString
sFullPath(pKey
->getName());
839 if (sFullPath
.getLength() > 1)
842 if (const_cast<OStoreFile
&>(pKey
->getStoreFile()).remove(sFullPath
, keyName
))
844 return RegError::DELETE_VALUE_FAILED
;
849 _err
= rStoreDir
.next(iter
);
852 return RegError::NO_ERROR
;
855 RegError
ORegistry::loadKey(RegKeyHandle hKey
, const OUString
& regFileName
,
856 bool bWarnings
, bool bReport
)
858 ORegKey
* pKey
= static_cast< ORegKey
* >(hKey
);
860 std::unique_ptr
< ORegistry
> pReg (new ORegistry());
861 RegError _ret
= pReg
->initRegistry(regFileName
, RegAccessMode::READONLY
);
862 if (_ret
!= RegError::NO_ERROR
)
864 ORegKey
* pRootKey
= pReg
->getRootKey();
868 OStoreDirectory::iterator iter
;
869 OStoreDirectory
rStoreDir(pRootKey
->getStoreDir());
870 storeError _err
= rStoreDir
.first(iter
);
872 while (_err
== store_E_None
)
874 OUString
const keyName
= iter
.m_pszName
;
876 if (iter
.m_nAttrib
& STORE_ATTRIB_ISDIR
)
878 _ret
= loadAndSaveKeys(pKey
, pRootKey
, keyName
, 0, bWarnings
, bReport
);
882 _ret
= loadAndSaveValue(pKey
, pRootKey
, keyName
, 0, bWarnings
, bReport
);
885 if (_ret
== RegError::MERGE_ERROR
)
887 if (_ret
== RegError::MERGE_CONFLICT
&& bWarnings
)
890 _err
= rStoreDir
.next(iter
);
893 rStoreDir
= OStoreDirectory();
894 (void) pReg
->releaseKey(pRootKey
);
898 RegError
ORegistry::loadAndSaveValue(ORegKey
* pTargetKey
,
899 ORegKey
const * pSourceKey
,
900 const OUString
& valueName
,
907 RegValueType valueType
;
908 sal_uInt32 valueSize
;
910 storeAccessMode sourceAccess
= storeAccessMode::ReadWrite
;
911 OUString
sTargetPath(pTargetKey
->getName());
912 OUString
sSourcePath(pSourceKey
->getName());
914 if (pSourceKey
->isReadOnly())
916 sourceAccess
= storeAccessMode::ReadOnly
;
921 sTargetPath
= sSourcePath
.copy(nCut
);
924 if (sTargetPath
.getLength() > 1)
926 if (sSourcePath
.getLength() > 1)
927 sTargetPath
+= sSourcePath
;
929 sTargetPath
= sSourcePath
;
932 if (sTargetPath
.getLength() > 1) sTargetPath
+= ROOT
;
933 if (sSourcePath
.getLength() > 1) sSourcePath
+= ROOT
;
935 if (rValue
.create(pSourceKey
->getStoreFile(), sSourcePath
, valueName
, sourceAccess
))
937 return RegError::VALUE_NOT_EXISTS
;
940 pBuffer
= static_cast<sal_uInt8
*>(rtl_allocateMemory(VALUE_HEADERSIZE
));
943 if (rValue
.readAt(0, pBuffer
, VALUE_HEADERSIZE
, rwBytes
))
945 rtl_freeMemory(pBuffer
);
946 return RegError::INVALID_VALUE
;
948 if (rwBytes
!= VALUE_HEADERSIZE
)
950 rtl_freeMemory(pBuffer
);
951 return RegError::INVALID_VALUE
;
954 RegError _ret
= RegError::NO_ERROR
;
955 sal_uInt8 type
= *pBuffer
;
956 valueType
= static_cast<RegValueType
>(type
);
957 readUINT32(pBuffer
+VALUE_TYPEOFFSET
, valueSize
);
958 rtl_freeMemory(pBuffer
);
960 nSize
= VALUE_HEADERSIZE
+ valueSize
;
961 pBuffer
= static_cast<sal_uInt8
*>(rtl_allocateMemory(nSize
));
963 if (rValue
.readAt(0, pBuffer
, nSize
, rwBytes
))
965 rtl_freeMemory(pBuffer
);
966 return RegError::INVALID_VALUE
;
968 if (rwBytes
!= nSize
)
970 rtl_freeMemory(pBuffer
);
971 return RegError::INVALID_VALUE
;
974 OStoreFile
rTargetFile(pTargetKey
->getStoreFile());
976 if (!rValue
.create(rTargetFile
, sTargetPath
, valueName
, storeAccessMode::ReadWrite
))
978 if (valueType
== RegValueType::BINARY
)
981 rValue
, sTargetPath
, valueSize
, pBuffer
+VALUE_HEADEROFFSET
,
983 if (_ret
!= RegError::NO_ERROR
)
985 if (_ret
== RegError::MERGE_ERROR
||
986 (_ret
== RegError::MERGE_CONFLICT
&& bWarnings
))
988 rtl_freeMemory(pBuffer
);
993 rtl_freeMemory(pBuffer
);
999 if (rValue
.create(rTargetFile
, sTargetPath
, valueName
, storeAccessMode::Create
))
1001 rtl_freeMemory(pBuffer
);
1002 return RegError::INVALID_VALUE
;
1004 if (rValue
.writeAt(0, pBuffer
, nSize
, rwBytes
))
1006 rtl_freeMemory(pBuffer
);
1007 return RegError::INVALID_VALUE
;
1010 if (rwBytes
!= nSize
)
1012 rtl_freeMemory(pBuffer
);
1013 return RegError::INVALID_VALUE
;
1015 pTargetKey
->setModified();
1017 rtl_freeMemory(pBuffer
);
1021 RegError
ORegistry::checkBlop(OStoreStream
& rValue
,
1022 const OUString
& sTargetPath
,
1023 sal_uInt32 srcValueSize
,
1024 sal_uInt8
const * pSrcBuffer
,
1027 RegistryTypeReader
reader(pSrcBuffer
, srcValueSize
);
1029 if (reader
.getTypeClass() == RT_TYPE_INVALID
)
1031 return RegError::INVALID_VALUE
;
1034 sal_uInt8
* pBuffer
= static_cast<sal_uInt8
*>(rtl_allocateMemory(VALUE_HEADERSIZE
));
1035 RegValueType valueType
;
1036 sal_uInt32 valueSize
;
1038 OString
targetPath(OUStringToOString(sTargetPath
, RTL_TEXTENCODING_UTF8
));
1040 if (!rValue
.readAt(0, pBuffer
, VALUE_HEADERSIZE
, rwBytes
) &&
1041 (rwBytes
== VALUE_HEADERSIZE
))
1043 sal_uInt8 type
= *pBuffer
;
1044 valueType
= static_cast<RegValueType
>(type
);
1045 readUINT32(pBuffer
+VALUE_TYPEOFFSET
, valueSize
);
1046 rtl_freeMemory(pBuffer
);
1048 if (valueType
== RegValueType::BINARY
)
1050 pBuffer
= static_cast<sal_uInt8
*>(rtl_allocateMemory(valueSize
));
1051 if (!rValue
.readAt(VALUE_HEADEROFFSET
, pBuffer
, valueSize
, rwBytes
) &&
1052 (rwBytes
== valueSize
))
1054 RegistryTypeReader
reader2(pBuffer
, valueSize
);
1056 if ((reader
.getTypeClass() != reader2
.getTypeClass())
1057 || reader2
.getTypeClass() == RT_TYPE_INVALID
)
1059 rtl_freeMemory(pBuffer
);
1063 fprintf(stdout
, "ERROR: values of blop from key \"%s\" has different types.\n",
1064 targetPath
.getStr());
1066 return RegError::MERGE_ERROR
;
1069 if (reader
.getTypeClass() == RT_TYPE_MODULE
)
1071 if (reader
.getFieldCount() > 0 &&
1072 reader2
.getFieldCount() > 0)
1074 mergeModuleValue(rValue
, reader
, reader2
);
1076 rtl_freeMemory(pBuffer
);
1077 return RegError::NO_ERROR
;
1079 if (reader2
.getFieldCount() > 0)
1081 rtl_freeMemory(pBuffer
);
1082 return RegError::NO_ERROR
;
1085 rtl_freeMemory(pBuffer
);
1086 return RegError::MERGE_CONFLICT
;
1090 rtl_freeMemory(pBuffer
);
1094 fprintf(stderr
, "WARNING: value of key \"%s\" already exists.\n",
1095 targetPath
.getStr());
1097 return RegError::MERGE_CONFLICT
;
1101 rtl_freeMemory(pBuffer
);
1104 fprintf(stderr
, "ERROR: values of key \"%s\" contains bad data.\n",
1105 targetPath
.getStr());
1107 return RegError::MERGE_ERROR
;
1111 rtl_freeMemory(pBuffer
);
1114 fprintf(stderr
, "ERROR: values of key \"%s\" has different types.\n",
1115 targetPath
.getStr());
1117 return RegError::MERGE_ERROR
;
1121 rtl_freeMemory(pBuffer
);
1122 return RegError::INVALID_VALUE
;
1126 static sal_uInt32
checkTypeReaders(RegistryTypeReader
const & reader1
,
1127 RegistryTypeReader
const & reader2
,
1128 std::set
< OUString
>& nameSet
)
1131 for (sal_uInt32 i
=0 ; i
< reader1
.getFieldCount(); i
++)
1133 nameSet
.insert(reader1
.getFieldName(i
));
1136 for (sal_uInt32 i
=0 ; i
< reader2
.getFieldCount(); i
++)
1138 if (nameSet
.find(reader2
.getFieldName(i
)) == nameSet
.end())
1140 nameSet
.insert(reader2
.getFieldName(i
));
1147 RegError
ORegistry::mergeModuleValue(OStoreStream
& rTargetValue
,
1148 RegistryTypeReader
const & reader
,
1149 RegistryTypeReader
const & reader2
)
1151 std::set
< OUString
> nameSet
;
1152 sal_uInt32 count
= checkTypeReaders(reader
, reader2
, nameSet
);
1154 if (count
!= reader
.getFieldCount())
1156 sal_uInt16 index
= 0;
1158 RegistryTypeWriter
writer(reader
.getTypeClass(),
1159 reader
.getTypeName(),
1160 reader
.getSuperTypeName(),
1161 static_cast<sal_uInt16
>(count
));
1163 for (sal_uInt32 i
=0 ; i
< reader
.getFieldCount(); i
++)
1165 writer
.setFieldData(index
,
1166 reader
.getFieldName(i
),
1167 reader
.getFieldType(i
),
1168 reader
.getFieldDoku(i
),
1169 reader
.getFieldFileName(i
),
1170 reader
.getFieldAccess(i
),
1171 reader
.getFieldConstValue(i
));
1174 for (sal_uInt32 i
=0 ; i
< reader2
.getFieldCount(); i
++)
1176 if (nameSet
.find(reader2
.getFieldName(i
)) == nameSet
.end())
1178 writer
.setFieldData(index
,
1179 reader2
.getFieldName(i
),
1180 reader2
.getFieldType(i
),
1181 reader2
.getFieldDoku(i
),
1182 reader2
.getFieldFileName(i
),
1183 reader2
.getFieldAccess(i
),
1184 reader2
.getFieldConstValue(i
));
1189 const sal_uInt8
* pBlop
= writer
.getBlop();
1190 sal_uInt32 aBlopSize
= writer
.getBlopSize();
1192 sal_uInt8 type
= sal_uInt8(RegValueType::BINARY
);
1193 sal_uInt8
* pBuffer
= static_cast<sal_uInt8
*>(rtl_allocateMemory(VALUE_HEADERSIZE
+ aBlopSize
));
1195 memcpy(pBuffer
, &type
, 1);
1196 writeUINT32(pBuffer
+VALUE_TYPEOFFSET
, aBlopSize
);
1197 memcpy(pBuffer
+VALUE_HEADEROFFSET
, pBlop
, aBlopSize
);
1200 if (rTargetValue
.writeAt(0, pBuffer
, VALUE_HEADERSIZE
+aBlopSize
, rwBytes
))
1202 rtl_freeMemory(pBuffer
);
1203 return RegError::INVALID_VALUE
;
1206 if (rwBytes
!= VALUE_HEADERSIZE
+aBlopSize
)
1208 rtl_freeMemory(pBuffer
);
1209 return RegError::INVALID_VALUE
;
1212 rtl_freeMemory(pBuffer
);
1214 return RegError::NO_ERROR
;
1217 RegError
ORegistry::loadAndSaveKeys(ORegKey
* pTargetKey
,
1218 ORegKey
* pSourceKey
,
1219 const OUString
& keyName
,
1224 RegError _ret
= RegError::NO_ERROR
;
1225 OUString
sRelPath(pSourceKey
->getName().copy(nCut
));
1228 if(pTargetKey
->getName().getLength() > 1)
1229 sFullPath
+= pTargetKey
->getName();
1230 sFullPath
+= sRelPath
;
1231 if (sRelPath
.getLength() > 1 || sFullPath
.isEmpty())
1234 OUString sFullKeyName
= sFullPath
;
1235 sFullKeyName
+= keyName
;
1237 OStoreDirectory rStoreDir
;
1238 if (rStoreDir
.create(pTargetKey
->getStoreFile(), sFullPath
, keyName
, storeAccessMode::Create
))
1240 return RegError::CREATE_KEY_FAILED
;
1243 if (m_openKeyTable
.count(sFullKeyName
) > 0)
1245 m_openKeyTable
[sFullKeyName
]->setDeleted(false);
1248 ORegKey
* pTmpKey
= nullptr;
1249 _ret
= pSourceKey
->openKey(keyName
, reinterpret_cast<RegKeyHandle
*>(&pTmpKey
));
1250 if (_ret
!= RegError::NO_ERROR
)
1253 OStoreDirectory::iterator iter
;
1254 OStoreDirectory
rTmpStoreDir(pTmpKey
->getStoreDir());
1255 storeError _err
= rTmpStoreDir
.first(iter
);
1257 while (_err
== store_E_None
)
1259 OUString
const sName
= iter
.m_pszName
;
1261 if (iter
.m_nAttrib
& STORE_ATTRIB_ISDIR
)
1263 _ret
= loadAndSaveKeys(pTargetKey
, pTmpKey
,
1264 sName
, nCut
, bWarnings
, bReport
);
1267 _ret
= loadAndSaveValue(pTargetKey
, pTmpKey
,
1268 sName
, nCut
, bWarnings
, bReport
);
1271 if (_ret
== RegError::MERGE_ERROR
)
1273 if (_ret
== RegError::MERGE_CONFLICT
&& bWarnings
)
1276 _err
= rTmpStoreDir
.next(iter
);
1279 pSourceKey
->releaseKey(pTmpKey
);
1283 ORegKey
* ORegistry::getRootKey()
1285 m_openKeyTable
[ROOT
]->acquire();
1286 return m_openKeyTable
[ROOT
];
1289 RegError
ORegistry::dumpRegistry(RegKeyHandle hKey
) const
1291 ORegKey
*pKey
= static_cast<ORegKey
*>(hKey
);
1293 RegError _ret
= RegError::NO_ERROR
;
1294 OStoreDirectory::iterator iter
;
1295 OStoreDirectory
rStoreDir(pKey
->getStoreDir());
1296 storeError _err
= rStoreDir
.first(iter
);
1298 OString
regName(OUStringToOString(getName(), osl_getThreadTextEncoding()));
1299 OString
keyName(OUStringToOString(pKey
->getName(), RTL_TEXTENCODING_UTF8
));
1300 fprintf(stdout
, "Registry \"%s\":\n\n%s\n", regName
.getStr(), keyName
.getStr());
1302 while (_err
== store_E_None
)
1304 sName
= iter
.m_pszName
;
1306 if (iter
.m_nAttrib
& STORE_ATTRIB_ISDIR
)
1308 _ret
= dumpKey(pKey
->getName(), sName
, 1);
1311 _ret
= dumpValue(pKey
->getName(), sName
, 1);
1314 if (_ret
!= RegError::NO_ERROR
)
1319 _err
= rStoreDir
.next(iter
);
1322 return RegError::NO_ERROR
;
1325 RegError
ORegistry::dumpValue(const OUString
& sPath
, const OUString
& sName
, sal_Int16 nSpc
) const
1327 OStoreStream rValue
;
1329 sal_uInt32 valueSize
;
1330 RegValueType valueType
;
1331 OUString
sFullPath(sPath
);
1333 storeAccessMode accessMode
= storeAccessMode::ReadWrite
;
1337 accessMode
= storeAccessMode::ReadOnly
;
1340 for (int i
= 0; i
< nSpc
; i
++) sIndent
+= " ";
1342 if (sFullPath
.getLength() > 1)
1346 if (rValue
.create(m_file
, sFullPath
, sName
, accessMode
))
1348 return RegError::VALUE_NOT_EXISTS
;
1351 pBuffer
= static_cast<sal_uInt8
*>(rtl_allocateMemory(VALUE_HEADERSIZE
));
1354 if (rValue
.readAt(0, pBuffer
, VALUE_HEADERSIZE
, rwBytes
))
1356 rtl_freeMemory(pBuffer
);
1357 return RegError::INVALID_VALUE
;
1359 if (rwBytes
!= (VALUE_HEADERSIZE
))
1361 rtl_freeMemory(pBuffer
);
1362 return RegError::INVALID_VALUE
;
1365 sal_uInt8 type
= *pBuffer
;
1366 valueType
= static_cast<RegValueType
>(type
);
1367 readUINT32(pBuffer
+VALUE_TYPEOFFSET
, valueSize
);
1369 pBuffer
= static_cast<sal_uInt8
*>(rtl_allocateMemory(valueSize
));
1370 if (rValue
.readAt(VALUE_HEADEROFFSET
, pBuffer
, valueSize
, rwBytes
))
1372 rtl_freeMemory(pBuffer
);
1373 return RegError::INVALID_VALUE
;
1375 if (rwBytes
!= valueSize
)
1377 rtl_freeMemory(pBuffer
);
1378 return RegError::INVALID_VALUE
;
1381 const sal_Char
* indent
= sIndent
.getStr();
1384 case RegValueType::NOT_DEFINED
:
1385 fprintf(stdout
, "%sValue: Type = VALUETYPE_NOT_DEFINED\n", indent
);
1387 case RegValueType::LONG
:
1389 fprintf(stdout
, "%sValue: Type = RegValueType::LONG\n", indent
);
1391 stdout
, "%s Size = %lu\n", indent
,
1392 sal::static_int_cast
< unsigned long >(valueSize
));
1393 fprintf(stdout
, "%s Data = ", indent
);
1396 readINT32(pBuffer
, value
);
1397 fprintf(stdout
, "%ld\n", sal::static_int_cast
< long >(value
));
1400 case RegValueType::STRING
:
1402 sal_Char
* value
= static_cast<sal_Char
*>(rtl_allocateMemory(valueSize
));
1403 readUtf8(pBuffer
, value
, valueSize
);
1404 fprintf(stdout
, "%sValue: Type = RegValueType::STRING\n", indent
);
1406 stdout
, "%s Size = %lu\n", indent
,
1407 sal::static_int_cast
< unsigned long >(valueSize
));
1408 fprintf(stdout
, "%s Data = \"%s\"\n", indent
, value
);
1409 rtl_freeMemory(value
);
1412 case RegValueType::UNICODE
:
1414 sal_uInt32 size
= (valueSize
/ 2) * sizeof(sal_Unicode
);
1415 fprintf(stdout
, "%sValue: Type = RegValueType::UNICODE\n", indent
);
1417 stdout
, "%s Size = %lu\n", indent
,
1418 sal::static_int_cast
< unsigned long >(valueSize
));
1419 fprintf(stdout
, "%s Data = ", indent
);
1421 std::unique_ptr
<sal_Unicode
[]> value(new sal_Unicode
[size
]);
1422 readString(pBuffer
, value
.get(), size
);
1424 OString uStr
= OUStringToOString(value
.get(), RTL_TEXTENCODING_UTF8
);
1425 fprintf(stdout
, "L\"%s\"\n", uStr
.getStr());
1428 case RegValueType::BINARY
:
1430 fprintf(stdout
, "%sValue: Type = RegValueType::BINARY\n", indent
);
1432 stdout
, "%s Size = %lu\n", indent
,
1433 sal::static_int_cast
< unsigned long >(valueSize
));
1434 fprintf(stdout
, "%s Data = ", indent
);
1436 typereg::Reader(pBuffer
, valueSize
),
1440 case RegValueType::LONGLIST
:
1442 sal_uInt32 offset
= 4; // initial 4 bytes for the size of the array
1445 readUINT32(pBuffer
, len
);
1447 fprintf(stdout
, "%sValue: Type = RegValueType::LONGLIST\n", indent
);
1449 stdout
, "%s Size = %lu\n", indent
,
1450 sal::static_int_cast
< unsigned long >(valueSize
));
1452 stdout
, "%s Len = %lu\n", indent
,
1453 sal::static_int_cast
< unsigned long >(len
));
1454 fprintf(stdout
, "%s Data = ", indent
);
1456 sal_Int32 longValue
;
1457 for (sal_uInt32 i
=0; i
< len
; i
++)
1459 readINT32(pBuffer
+offset
, longValue
);
1462 fprintf(stdout
, "%s ", indent
);
1465 stdout
, "%lu = %ld\n",
1466 sal::static_int_cast
< unsigned long >(i
),
1467 sal::static_int_cast
< long >(longValue
));
1468 offset
+= 4; // 4 Bytes for sal_Int32
1472 case RegValueType::STRINGLIST
:
1474 sal_uInt32 offset
= 4; // initial 4 bytes for the size of the array
1475 sal_uInt32 sLen
= 0;
1478 readUINT32(pBuffer
, len
);
1480 fprintf(stdout
, "%sValue: Type = RegValueType::STRINGLIST\n", indent
);
1482 stdout
, "%s Size = %lu\n", indent
,
1483 sal::static_int_cast
< unsigned long >(valueSize
));
1485 stdout
, "%s Len = %lu\n", indent
,
1486 sal::static_int_cast
< unsigned long >(len
));
1487 fprintf(stdout
, "%s Data = ", indent
);
1489 for (sal_uInt32 i
=0; i
< len
; i
++)
1491 readUINT32(pBuffer
+offset
, sLen
);
1493 offset
+= 4; // 4 bytes (sal_uInt32) for the string size
1495 sal_Char
*pValue
= static_cast<sal_Char
*>(rtl_allocateMemory(sLen
));
1496 readUtf8(pBuffer
+offset
, pValue
, sLen
);
1499 fprintf(stdout
, "%s ", indent
);
1502 stdout
, "%lu = \"%s\"\n",
1503 sal::static_int_cast
< unsigned long >(i
), pValue
);
1504 rtl_freeMemory(pValue
);
1509 case RegValueType::UNICODELIST
:
1511 sal_uInt32 offset
= 4; // initial 4 bytes for the size of the array
1512 sal_uInt32 sLen
= 0;
1515 readUINT32(pBuffer
, len
);
1517 fprintf(stdout
, "%sValue: Type = RegValueType::UNICODELIST\n", indent
);
1519 stdout
, "%s Size = %lu\n", indent
,
1520 sal::static_int_cast
< unsigned long >(valueSize
));
1522 stdout
, "%s Len = %lu\n", indent
,
1523 sal::static_int_cast
< unsigned long >(len
));
1524 fprintf(stdout
, "%s Data = ", indent
);
1527 for (sal_uInt32 i
=0; i
< len
; i
++)
1529 readUINT32(pBuffer
+offset
, sLen
);
1531 offset
+= 4; // 4 bytes (sal_uInt32) for the string size
1533 sal_Unicode
*pValue
= static_cast<sal_Unicode
*>(rtl_allocateMemory((sLen
/ 2) * sizeof(sal_Unicode
)));
1534 readString(pBuffer
+offset
, pValue
, sLen
);
1537 fprintf(stdout
, "%s ", indent
);
1539 uStr
= OUStringToOString(pValue
, RTL_TEXTENCODING_UTF8
);
1541 stdout
, "%lu = L\"%s\"\n",
1542 sal::static_int_cast
< unsigned long >(i
),
1547 rtl_freeMemory(pValue
);
1553 fprintf(stdout
, "\n");
1555 rtl_freeMemory(pBuffer
);
1556 return RegError::NO_ERROR
;
1559 RegError
ORegistry::dumpKey(const OUString
& sPath
, const OUString
& sName
, sal_Int16 nSpace
) const
1561 OStoreDirectory rStoreDir
;
1562 OUString
sFullPath(sPath
);
1564 storeAccessMode accessMode
= storeAccessMode::ReadWrite
;
1565 RegError _ret
= RegError::NO_ERROR
;
1569 accessMode
= storeAccessMode::ReadOnly
;
1572 for (int i
= 0; i
< nSpace
; i
++) sIndent
+= " ";
1574 if (sFullPath
.getLength() > 1)
1577 storeError _err
= rStoreDir
.create(m_file
, sFullPath
, sName
, accessMode
);
1579 if (_err
== store_E_NotExists
)
1580 return RegError::KEY_NOT_EXISTS
;
1581 else if (_err
== store_E_WrongFormat
)
1582 return RegError::INVALID_KEY
;
1584 fprintf(stdout
, "%s/ %s\n", sIndent
.getStr(), OUStringToOString(sName
, RTL_TEXTENCODING_UTF8
).getStr());
1586 OUString
sSubPath(sFullPath
);
1590 OStoreDirectory::iterator iter
;
1592 _err
= rStoreDir
.first(iter
);
1594 while (_err
== store_E_None
)
1596 sSubName
= iter
.m_pszName
;
1598 if (iter
.m_nAttrib
& STORE_ATTRIB_ISDIR
)
1600 _ret
= dumpKey(sSubPath
, sSubName
, nSpace
+2);
1603 _ret
= dumpValue(sSubPath
, sSubName
, nSpace
+2);
1606 if (_ret
!= RegError::NO_ERROR
)
1611 _err
= rStoreDir
.next(iter
);
1614 return RegError::NO_ERROR
;
1617 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */