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"
33 #include "reflread.hxx"
35 #include "reflwrit.hxx"
37 #include <registry/reader.hxx>
38 #include <registry/refltype.hxx>
39 #include <registry/types.hxx>
41 #include "reflcnst.hxx"
42 #include "keyimpl.hxx"
44 #include <osl/thread.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 untrusted loop bound
327 for (sal_uInt16 j
= 0; j
< reader
.getMethodParameterCount(i
); ++j
)
330 "%s parameter %u:\n", indent
.getStr(),
331 static_cast< unsigned int >(j
));
332 printf("%s flags: ", indent
.getStr());
333 RTParamMode flags
= reader
.getMethodParameterFlags(i
, j
);
334 bool rest
= (flags
& RT_PARAM_REST
) != 0;
335 switch (flags
& ~RT_PARAM_REST
) {
349 printf("<invalid (%ld)>", static_cast< long >(flags
));
357 printf("%s name: ", indent
.getStr());
358 printString(reader
.getMethodParameterName(i
, j
));
360 printf("%s type name: ", indent
.getStr());
361 printString(reader
.getMethodParameterTypeName(i
, j
));
365 "%s exception count: %u\n", indent
.getStr(),
366 static_cast< unsigned int >(reader
.getMethodExceptionCount(i
)));
367 // coverity[tainted_data] - cid#1215304 unhelpfully warns about untrusted loop bound
368 for (sal_uInt16 j
= 0; j
< reader
.getMethodExceptionCount(i
); ++j
)
371 "%s exception type name %u: ", indent
.getStr(),
372 static_cast< unsigned int >(j
));
373 printString(reader
.getMethodExceptionTypeName(i
, j
));
378 "%sreference count: %u\n", indent
.getStr(),
379 static_cast< unsigned int >(reader
.getReferenceCount()));
380 for (sal_uInt16 i
= 0; i
< reader
.getReferenceCount(); ++i
) {
382 "%sreference %u:\n", indent
.getStr(),
383 static_cast< unsigned int >(i
));
384 printf("%s documentation: ", indent
.getStr());
385 printString(reader
.getReferenceDocumentation(i
));
387 printf("%s flags: ", indent
.getStr());
388 printFieldOrReferenceFlags(reader
.getReferenceFlags(i
));
390 printf("%s sort: ", indent
.getStr());
391 switch (reader
.getReferenceSort(i
)) {
392 case RTReferenceType::SUPPORTS
:
396 case RTReferenceType::EXPORTS
:
400 case RTReferenceType::TYPE_PARAMETER
:
401 printf("type parameter");
407 static_cast< long >(reader
.getReferenceSort(i
)));
411 printf("%s type name: ", indent
.getStr());
412 printString(reader
.getReferenceTypeName(i
));
416 printf("<invalid>\n");
422 ORegistry::ORegistry()
429 ORegistry::~ORegistry()
431 ORegKey
* pRootKey
= m_openKeyTable
[ROOT
];
432 if (pRootKey
!= nullptr)
433 (void) releaseKey(pRootKey
);
435 if (m_file
.isValid())
439 RegError
ORegistry::initRegistry(const OUString
& regName
, RegAccessMode accessMode
, bool bCreate
)
441 RegError eRet
= RegError::INVALID_REGISTRY
;
443 storeAccessMode sAccessMode
= storeAccessMode::ReadWrite
;
448 sAccessMode
= storeAccessMode::Create
;
450 else if (accessMode
& RegAccessMode::READONLY
)
452 sAccessMode
= storeAccessMode::ReadOnly
;
456 if (regName
.isEmpty() &&
457 storeAccessMode::Create
== sAccessMode
)
459 errCode
= rRegFile
.createInMemory();
463 errCode
= rRegFile
.create(regName
, sAccessMode
);
470 case store_E_NotExists
:
471 eRet
= RegError::REGISTRY_NOT_EXISTS
;
473 case store_E_LockingViolation
:
474 eRet
= RegError::CANNOT_OPEN_FOR_READWRITE
;
477 eRet
= RegError::INVALID_REGISTRY
;
483 OStoreDirectory rStoreDir
;
484 storeError _err
= rStoreDir
.create(rRegFile
, OUString(), OUString(), sAccessMode
);
486 if (_err
== store_E_None
)
492 m_openKeyTable
[ROOT
] = new ORegKey(ROOT
, this);
493 eRet
= RegError::NO_ERROR
;
496 eRet
= RegError::INVALID_REGISTRY
;
502 RegError
ORegistry::closeRegistry()
506 if (m_file
.isValid())
508 (void) releaseKey(m_openKeyTable
[ROOT
]);
511 return RegError::NO_ERROR
;
514 return RegError::REGISTRY_NOT_EXISTS
;
518 RegError
ORegistry::destroyRegistry(const OUString
& regName
)
522 if (!regName
.isEmpty())
524 std::unique_ptr
<ORegistry
> pReg(new ORegistry());
526 if (pReg
->initRegistry(regName
, RegAccessMode::READWRITE
) == RegError::NO_ERROR
)
531 if (FileBase::getSystemPathFromFileURL(regName
, systemName
) != FileBase::E_None
)
532 systemName
= regName
;
534 OString
name(OUStringToOString(systemName
, osl_getThreadTextEncoding()));
535 if (unlink(name
.getStr()) != 0)
537 return RegError::DESTROY_REGISTRY_FAILED
;
541 return RegError::DESTROY_REGISTRY_FAILED
;
545 if (m_refCount
!= 1 || isReadOnly())
547 return RegError::DESTROY_REGISTRY_FAILED
;
550 if (m_file
.isValid())
552 releaseKey(m_openKeyTable
[ROOT
]);
556 if (!m_name
.isEmpty())
559 if (FileBase::getSystemPathFromFileURL(m_name
, systemName
) != FileBase::E_None
)
562 OString
name(OUStringToOString(systemName
, osl_getThreadTextEncoding()));
563 if (unlink(name
.getStr()) != 0)
565 return RegError::DESTROY_REGISTRY_FAILED
;
570 return RegError::REGISTRY_NOT_EXISTS
;
574 return RegError::NO_ERROR
;
577 RegError
ORegistry::acquireKey (RegKeyHandle hKey
)
579 ORegKey
* pKey
= static_cast< ORegKey
* >(hKey
);
581 return RegError::INVALID_KEY
;
586 return RegError::NO_ERROR
;
589 RegError
ORegistry::releaseKey (RegKeyHandle hKey
)
591 ORegKey
* pKey
= static_cast< ORegKey
* >(hKey
);
593 return RegError::INVALID_KEY
;
596 if (pKey
->release() == 0)
598 m_openKeyTable
.erase(pKey
->getName());
601 return RegError::NO_ERROR
;
604 RegError
ORegistry::createKey(RegKeyHandle hKey
, const OUString
& keyName
,
605 RegKeyHandle
* phNewKey
)
611 if (keyName
.isEmpty())
612 return RegError::INVALID_KEYNAME
;
617 pKey
= static_cast<ORegKey
*>(hKey
);
619 pKey
= m_openKeyTable
[ROOT
];
621 OUString sFullKeyName
= pKey
->getFullPath(keyName
);
623 if (m_openKeyTable
.count(sFullKeyName
) > 0)
625 *phNewKey
= m_openKeyTable
[sFullKeyName
];
626 static_cast<ORegKey
*>(*phNewKey
)->acquire();
627 static_cast<ORegKey
*>(*phNewKey
)->setDeleted(false);
628 return RegError::NO_ERROR
;
631 OStoreDirectory rStoreDir
;
632 OUStringBuffer
sFullPath(sFullKeyName
.getLength());
635 sFullPath
.append('/');
637 sal_Int32 nIndex
= 0;
640 token
= sFullKeyName
.getToken(0, '/', nIndex
);
641 if (!token
.isEmpty())
643 if (rStoreDir
.create(pKey
->getStoreFile(), sFullPath
.getStr(), token
, storeAccessMode::Create
))
645 return RegError::CREATE_KEY_FAILED
;
648 sFullPath
.append(token
);
649 sFullPath
.append('/');
651 } while(nIndex
!= -1);
654 pKey
= new ORegKey(sFullKeyName
, this);
656 m_openKeyTable
[sFullKeyName
] = pKey
;
658 return RegError::NO_ERROR
;
661 RegError
ORegistry::openKey(RegKeyHandle hKey
, const OUString
& keyName
,
662 RegKeyHandle
* phOpenKey
)
666 *phOpenKey
= nullptr;
668 if (keyName
.isEmpty())
670 return RegError::INVALID_KEYNAME
;
676 pKey
= static_cast<ORegKey
*>(hKey
);
678 pKey
= m_openKeyTable
[ROOT
];
680 OUString
path(pKey
->getFullPath(keyName
));
681 KeyMap::iterator
i(m_openKeyTable
.find(path
));
682 if (i
== m_openKeyTable
.end()) {
683 sal_Int32 n
= path
.lastIndexOf('/') + 1;
684 switch (OStoreDirectory().create(
685 pKey
->getStoreFile(), path
.copy(0, n
), path
.copy(n
),
686 isReadOnly() ? storeAccessMode::ReadOnly
: storeAccessMode::ReadWrite
))
688 case store_E_NotExists
:
689 return RegError::KEY_NOT_EXISTS
;
690 case store_E_WrongFormat
:
691 return RegError::INVALID_KEY
;
696 std::unique_ptr
< ORegKey
> p(new ORegKey(path
, this));
697 i
= m_openKeyTable
.insert(std::make_pair(path
, p
.get())).first
;
700 i
->second
->acquire();
702 *phOpenKey
= i
->second
;
703 return RegError::NO_ERROR
;
706 RegError
ORegistry::closeKey(RegKeyHandle hKey
)
708 ORegKey
* pKey
= static_cast< ORegKey
* >(hKey
);
712 OUString
const aKeyName (pKey
->getName());
713 if (m_openKeyTable
.count(aKeyName
) <= 0)
714 return RegError::KEY_NOT_OPEN
;
716 if (pKey
->isModified())
718 ORegKey
* pRootKey
= getRootKey();
719 if (pKey
!= pRootKey
)
721 // propagate "modified" state to RootKey.
722 pRootKey
->setModified();
726 // closing modified RootKey, flush registry file.
727 (void) m_file
.flush();
729 pKey
->setModified(false);
730 (void) releaseKey(pRootKey
);
733 return releaseKey(pKey
);
736 RegError
ORegistry::deleteKey(RegKeyHandle hKey
, const OUString
& keyName
)
738 ORegKey
* pKey
= static_cast< ORegKey
* >(hKey
);
739 if (keyName
.isEmpty())
740 return RegError::INVALID_KEYNAME
;
745 pKey
= m_openKeyTable
[ROOT
];
747 OUString
sFullKeyName(pKey
->getFullPath(keyName
));
748 return eraseKey(m_openKeyTable
[ROOT
], sFullKeyName
);
751 RegError
ORegistry::eraseKey(ORegKey
* pKey
, const OUString
& keyName
)
753 RegError _ret
= RegError::NO_ERROR
;
755 if (keyName
.isEmpty())
757 return RegError::INVALID_KEYNAME
;
760 OUString
sFullKeyName(pKey
->getName());
761 OUString
sFullPath(sFullKeyName
);
762 OUString sRelativKey
;
763 sal_Int32 lastIndex
= keyName
.lastIndexOf('/');
767 sRelativKey
+= keyName
.copy(lastIndex
+ 1);
769 if (sFullKeyName
.getLength() > 1)
770 sFullKeyName
+= keyName
;
772 sFullKeyName
+= keyName
.copy(1);
774 sFullPath
= sFullKeyName
.copy(0, keyName
.lastIndexOf('/') + 1);
777 if (sFullKeyName
.getLength() > 1)
778 sFullKeyName
+= ROOT
;
780 sRelativKey
+= keyName
;
781 sFullKeyName
+= keyName
;
783 if (sFullPath
.getLength() > 1)
787 ORegKey
* pOldKey
= nullptr;
788 _ret
= pKey
->openKey(keyName
, reinterpret_cast<RegKeyHandle
*>(&pOldKey
));
789 if (_ret
!= RegError::NO_ERROR
)
792 _ret
= deleteSubkeysAndValues(pOldKey
);
793 if (_ret
!= RegError::NO_ERROR
)
795 pKey
->closeKey(pOldKey
);
799 OUString
tmpName(sRelativKey
);
802 OStoreFile
sFile(pKey
->getStoreFile());
803 if (sFile
.isValid() && sFile
.remove(sFullPath
, tmpName
))
805 return RegError::DELETE_KEY_FAILED
;
807 pOldKey
->setModified();
809 // set flag deleted !!!
810 pOldKey
->setDeleted(true);
812 return pKey
->closeKey(pOldKey
);
815 RegError
ORegistry::deleteSubkeysAndValues(ORegKey
* pKey
)
817 OStoreDirectory::iterator iter
;
818 RegError _ret
= RegError::NO_ERROR
;
819 OStoreDirectory
rStoreDir(pKey
->getStoreDir());
820 storeError _err
= rStoreDir
.first(iter
);
822 while (_err
== store_E_None
)
824 OUString
const keyName
= iter
.m_pszName
;
826 if (iter
.m_nAttrib
& STORE_ATTRIB_ISDIR
)
828 _ret
= eraseKey(pKey
, keyName
);
829 if (_ret
!= RegError::NO_ERROR
)
834 OUString
sFullPath(pKey
->getName());
836 if (sFullPath
.getLength() > 1)
839 if (const_cast<OStoreFile
&>(pKey
->getStoreFile()).remove(sFullPath
, keyName
))
841 return RegError::DELETE_VALUE_FAILED
;
846 _err
= rStoreDir
.next(iter
);
849 return RegError::NO_ERROR
;
852 RegError
ORegistry::loadKey(RegKeyHandle hKey
, const OUString
& regFileName
,
853 bool bWarnings
, bool bReport
)
855 ORegKey
* pKey
= static_cast< ORegKey
* >(hKey
);
857 std::unique_ptr
< ORegistry
> pReg (new ORegistry());
858 RegError _ret
= pReg
->initRegistry(regFileName
, RegAccessMode::READONLY
);
859 if (_ret
!= RegError::NO_ERROR
)
861 ORegKey
* pRootKey
= pReg
->getRootKey();
865 OStoreDirectory::iterator iter
;
866 OStoreDirectory
rStoreDir(pRootKey
->getStoreDir());
867 storeError _err
= rStoreDir
.first(iter
);
869 while (_err
== store_E_None
)
871 OUString
const keyName
= iter
.m_pszName
;
873 if (iter
.m_nAttrib
& STORE_ATTRIB_ISDIR
)
875 _ret
= loadAndSaveKeys(pKey
, pRootKey
, keyName
, 0, bWarnings
, bReport
);
879 _ret
= loadAndSaveValue(pKey
, pRootKey
, keyName
, 0, bWarnings
, bReport
);
882 if (_ret
== RegError::MERGE_ERROR
)
884 if (_ret
== RegError::MERGE_CONFLICT
&& bWarnings
)
887 _err
= rStoreDir
.next(iter
);
890 rStoreDir
= OStoreDirectory();
891 (void) pReg
->releaseKey(pRootKey
);
895 RegError
ORegistry::loadAndSaveValue(ORegKey
* pTargetKey
,
896 ORegKey
const * pSourceKey
,
897 const OUString
& valueName
,
903 RegValueType valueType
;
904 sal_uInt32 valueSize
;
906 storeAccessMode sourceAccess
= storeAccessMode::ReadWrite
;
907 OUString
sTargetPath(pTargetKey
->getName());
908 OUString
sSourcePath(pSourceKey
->getName());
910 if (pSourceKey
->isReadOnly())
912 sourceAccess
= storeAccessMode::ReadOnly
;
917 sTargetPath
= sSourcePath
.copy(nCut
);
920 if (sTargetPath
.getLength() > 1)
922 if (sSourcePath
.getLength() > 1)
923 sTargetPath
+= sSourcePath
;
925 sTargetPath
= sSourcePath
;
928 if (sTargetPath
.getLength() > 1) sTargetPath
+= ROOT
;
929 if (sSourcePath
.getLength() > 1) sSourcePath
+= ROOT
;
931 if (rValue
.create(pSourceKey
->getStoreFile(), sSourcePath
, valueName
, sourceAccess
))
933 return RegError::VALUE_NOT_EXISTS
;
936 std::vector
<sal_uInt8
> aBuffer(VALUE_HEADERSIZE
);
939 if (rValue
.readAt(0, aBuffer
.data(), VALUE_HEADERSIZE
, rwBytes
))
941 return RegError::INVALID_VALUE
;
943 if (rwBytes
!= VALUE_HEADERSIZE
)
945 return RegError::INVALID_VALUE
;
948 RegError _ret
= RegError::NO_ERROR
;
949 sal_uInt8 type
= aBuffer
[0];
950 valueType
= static_cast<RegValueType
>(type
);
951 readUINT32(aBuffer
.data() + VALUE_TYPEOFFSET
, valueSize
);
953 nSize
= VALUE_HEADERSIZE
+ valueSize
;
954 aBuffer
.resize(nSize
);
956 if (rValue
.readAt(0, aBuffer
.data(), nSize
, rwBytes
))
958 return RegError::INVALID_VALUE
;
960 if (rwBytes
!= nSize
)
962 return RegError::INVALID_VALUE
;
965 OStoreFile
rTargetFile(pTargetKey
->getStoreFile());
967 if (!rValue
.create(rTargetFile
, sTargetPath
, valueName
, storeAccessMode::ReadWrite
))
969 if (valueType
== RegValueType::BINARY
)
972 rValue
, sTargetPath
, valueSize
, aBuffer
.data() + VALUE_HEADEROFFSET
,
974 if (_ret
!= RegError::NO_ERROR
)
976 if (_ret
== RegError::MERGE_ERROR
||
977 (_ret
== RegError::MERGE_CONFLICT
&& bWarnings
))
988 if (rValue
.create(rTargetFile
, sTargetPath
, valueName
, storeAccessMode::Create
))
990 return RegError::INVALID_VALUE
;
992 if (rValue
.writeAt(0, aBuffer
.data(), nSize
, rwBytes
))
994 return RegError::INVALID_VALUE
;
997 if (rwBytes
!= nSize
)
999 return RegError::INVALID_VALUE
;
1001 pTargetKey
->setModified();
1006 RegError
ORegistry::checkBlop(OStoreStream
& rValue
,
1007 const OUString
& sTargetPath
,
1008 sal_uInt32 srcValueSize
,
1009 sal_uInt8
const * pSrcBuffer
,
1012 RegistryTypeReader
reader(pSrcBuffer
, srcValueSize
);
1014 if (reader
.getTypeClass() == RT_TYPE_INVALID
)
1016 return RegError::INVALID_VALUE
;
1019 std::vector
<sal_uInt8
> aBuffer(VALUE_HEADERSIZE
);
1020 RegValueType valueType
;
1021 sal_uInt32 valueSize
;
1023 OString
targetPath(OUStringToOString(sTargetPath
, RTL_TEXTENCODING_UTF8
));
1025 if (!rValue
.readAt(0, aBuffer
.data(), VALUE_HEADERSIZE
, rwBytes
) &&
1026 (rwBytes
== VALUE_HEADERSIZE
))
1028 sal_uInt8 type
= aBuffer
[0];
1029 valueType
= static_cast<RegValueType
>(type
);
1030 readUINT32(aBuffer
.data() + VALUE_TYPEOFFSET
, valueSize
);
1032 if (valueType
== RegValueType::BINARY
)
1034 aBuffer
.resize(valueSize
);
1035 if (!rValue
.readAt(VALUE_HEADEROFFSET
, aBuffer
.data(), valueSize
, rwBytes
) &&
1036 (rwBytes
== valueSize
))
1038 RegistryTypeReader
reader2(aBuffer
.data(), valueSize
);
1040 if ((reader
.getTypeClass() != reader2
.getTypeClass())
1041 || reader2
.getTypeClass() == RT_TYPE_INVALID
)
1045 fprintf(stdout
, "ERROR: values of blop from key \"%s\" has different types.\n",
1046 targetPath
.getStr());
1048 return RegError::MERGE_ERROR
;
1051 if (reader
.getTypeClass() == RT_TYPE_MODULE
)
1053 if (reader
.getFieldCount() > 0 &&
1054 reader2
.getFieldCount() > 0)
1056 mergeModuleValue(rValue
, reader
, reader2
);
1058 return RegError::NO_ERROR
;
1060 if (reader2
.getFieldCount() > 0)
1062 return RegError::NO_ERROR
;
1065 return RegError::MERGE_CONFLICT
;
1071 fprintf(stderr
, "WARNING: value of key \"%s\" already exists.\n",
1072 targetPath
.getStr());
1074 return RegError::MERGE_CONFLICT
;
1080 fprintf(stderr
, "ERROR: values of key \"%s\" contains bad data.\n",
1081 targetPath
.getStr());
1083 return RegError::MERGE_ERROR
;
1089 fprintf(stderr
, "ERROR: values of key \"%s\" has different types.\n",
1090 targetPath
.getStr());
1092 return RegError::MERGE_ERROR
;
1096 return RegError::INVALID_VALUE
;
1100 static sal_uInt32
checkTypeReaders(RegistryTypeReader
const & reader1
,
1101 RegistryTypeReader
const & reader2
,
1102 std::set
< OUString
>& nameSet
)
1105 for (sal_uInt32 i
=0 ; i
< reader1
.getFieldCount(); i
++)
1107 nameSet
.insert(reader1
.getFieldName(i
));
1110 for (sal_uInt32 i
=0 ; i
< reader2
.getFieldCount(); i
++)
1112 if (nameSet
.insert(reader2
.getFieldName(i
)).second
)
1118 RegError
ORegistry::mergeModuleValue(OStoreStream
& rTargetValue
,
1119 RegistryTypeReader
const & reader
,
1120 RegistryTypeReader
const & reader2
)
1122 std::set
< OUString
> nameSet
;
1123 sal_uInt32 count
= checkTypeReaders(reader
, reader2
, nameSet
);
1125 if (count
!= reader
.getFieldCount())
1127 sal_uInt16 index
= 0;
1129 RegistryTypeWriter
writer(reader
.getTypeClass(),
1130 reader
.getTypeName(),
1131 reader
.getSuperTypeName(),
1132 static_cast<sal_uInt16
>(count
));
1134 for (sal_uInt32 i
=0 ; i
< reader
.getFieldCount(); i
++)
1136 writer
.setFieldData(index
,
1137 reader
.getFieldName(i
),
1138 reader
.getFieldType(i
),
1139 reader
.getFieldDoku(i
),
1140 reader
.getFieldFileName(i
),
1141 reader
.getFieldAccess(i
),
1142 reader
.getFieldConstValue(i
));
1145 for (sal_uInt32 i
=0 ; i
< reader2
.getFieldCount(); i
++)
1147 if (nameSet
.find(reader2
.getFieldName(i
)) == nameSet
.end())
1149 writer
.setFieldData(index
,
1150 reader2
.getFieldName(i
),
1151 reader2
.getFieldType(i
),
1152 reader2
.getFieldDoku(i
),
1153 reader2
.getFieldFileName(i
),
1154 reader2
.getFieldAccess(i
),
1155 reader2
.getFieldConstValue(i
));
1160 const sal_uInt8
* pBlop
= writer
.getBlop();
1161 sal_uInt32 aBlopSize
= writer
.getBlopSize();
1163 sal_uInt8 type
= sal_uInt8(RegValueType::BINARY
);
1164 std::vector
<sal_uInt8
> aBuffer(VALUE_HEADERSIZE
+ aBlopSize
);
1166 memcpy(aBuffer
.data(), &type
, 1);
1167 writeUINT32(aBuffer
.data() + VALUE_TYPEOFFSET
, aBlopSize
);
1168 memcpy(aBuffer
.data() + VALUE_HEADEROFFSET
, pBlop
, aBlopSize
);
1171 if (rTargetValue
.writeAt(0, aBuffer
.data(), VALUE_HEADERSIZE
+aBlopSize
, rwBytes
))
1173 return RegError::INVALID_VALUE
;
1176 if (rwBytes
!= VALUE_HEADERSIZE
+aBlopSize
)
1178 return RegError::INVALID_VALUE
;
1181 return RegError::NO_ERROR
;
1184 RegError
ORegistry::loadAndSaveKeys(ORegKey
* pTargetKey
,
1185 ORegKey
* pSourceKey
,
1186 const OUString
& keyName
,
1191 RegError _ret
= RegError::NO_ERROR
;
1192 OUString
sRelPath(pSourceKey
->getName().copy(nCut
));
1195 if(pTargetKey
->getName().getLength() > 1)
1196 sFullPath
+= pTargetKey
->getName();
1197 sFullPath
+= sRelPath
;
1198 if (sRelPath
.getLength() > 1 || sFullPath
.isEmpty())
1201 OUString sFullKeyName
= sFullPath
;
1202 sFullKeyName
+= keyName
;
1204 OStoreDirectory rStoreDir
;
1205 if (rStoreDir
.create(pTargetKey
->getStoreFile(), sFullPath
, keyName
, storeAccessMode::Create
))
1207 return RegError::CREATE_KEY_FAILED
;
1210 if (m_openKeyTable
.count(sFullKeyName
) > 0)
1212 m_openKeyTable
[sFullKeyName
]->setDeleted(false);
1215 ORegKey
* pTmpKey
= nullptr;
1216 _ret
= pSourceKey
->openKey(keyName
, reinterpret_cast<RegKeyHandle
*>(&pTmpKey
));
1217 if (_ret
!= RegError::NO_ERROR
)
1220 OStoreDirectory::iterator iter
;
1221 OStoreDirectory
rTmpStoreDir(pTmpKey
->getStoreDir());
1222 storeError _err
= rTmpStoreDir
.first(iter
);
1224 while (_err
== store_E_None
)
1226 OUString
const sName
= iter
.m_pszName
;
1228 if (iter
.m_nAttrib
& STORE_ATTRIB_ISDIR
)
1230 _ret
= loadAndSaveKeys(pTargetKey
, pTmpKey
,
1231 sName
, nCut
, bWarnings
, bReport
);
1234 _ret
= loadAndSaveValue(pTargetKey
, pTmpKey
,
1235 sName
, nCut
, bWarnings
, bReport
);
1238 if (_ret
== RegError::MERGE_ERROR
)
1240 if (_ret
== RegError::MERGE_CONFLICT
&& bWarnings
)
1243 _err
= rTmpStoreDir
.next(iter
);
1246 pSourceKey
->releaseKey(pTmpKey
);
1250 ORegKey
* ORegistry::getRootKey()
1252 m_openKeyTable
[ROOT
]->acquire();
1253 return m_openKeyTable
[ROOT
];
1256 RegError
ORegistry::dumpRegistry(RegKeyHandle hKey
) const
1258 ORegKey
*pKey
= static_cast<ORegKey
*>(hKey
);
1260 RegError _ret
= RegError::NO_ERROR
;
1261 OStoreDirectory::iterator iter
;
1262 OStoreDirectory
rStoreDir(pKey
->getStoreDir());
1263 storeError _err
= rStoreDir
.first(iter
);
1265 OString
regName(OUStringToOString(getName(), osl_getThreadTextEncoding()));
1266 OString
keyName(OUStringToOString(pKey
->getName(), RTL_TEXTENCODING_UTF8
));
1267 fprintf(stdout
, "Registry \"%s\":\n\n%s\n", regName
.getStr(), keyName
.getStr());
1269 while (_err
== store_E_None
)
1271 sName
= iter
.m_pszName
;
1273 if (iter
.m_nAttrib
& STORE_ATTRIB_ISDIR
)
1275 _ret
= dumpKey(pKey
->getName(), sName
, 1);
1278 _ret
= dumpValue(pKey
->getName(), sName
, 1);
1281 if (_ret
!= RegError::NO_ERROR
)
1286 _err
= rStoreDir
.next(iter
);
1289 return RegError::NO_ERROR
;
1292 RegError
ORegistry::dumpValue(const OUString
& sPath
, const OUString
& sName
, sal_Int16 nSpc
) const
1294 OStoreStream rValue
;
1295 sal_uInt32 valueSize
;
1296 RegValueType valueType
;
1297 OUString
sFullPath(sPath
);
1299 storeAccessMode accessMode
= storeAccessMode::ReadWrite
;
1303 accessMode
= storeAccessMode::ReadOnly
;
1306 for (int i
= 0; i
< nSpc
; i
++) sIndent
+= " ";
1308 if (sFullPath
.getLength() > 1)
1312 if (rValue
.create(m_file
, sFullPath
, sName
, accessMode
))
1314 return RegError::VALUE_NOT_EXISTS
;
1317 std::vector
<sal_uInt8
> aBuffer(VALUE_HEADERSIZE
);
1320 if (rValue
.readAt(0, aBuffer
.data(), VALUE_HEADERSIZE
, rwBytes
))
1322 return RegError::INVALID_VALUE
;
1324 if (rwBytes
!= (VALUE_HEADERSIZE
))
1326 return RegError::INVALID_VALUE
;
1329 sal_uInt8 type
= aBuffer
[0];
1330 valueType
= static_cast<RegValueType
>(type
);
1331 readUINT32(aBuffer
.data() + VALUE_TYPEOFFSET
, valueSize
);
1333 aBuffer
.resize(valueSize
);
1334 if (rValue
.readAt(VALUE_HEADEROFFSET
, aBuffer
.data(), valueSize
, rwBytes
))
1336 return RegError::INVALID_VALUE
;
1338 if (rwBytes
!= valueSize
)
1340 return RegError::INVALID_VALUE
;
1343 const sal_Char
* indent
= sIndent
.getStr();
1346 case RegValueType::NOT_DEFINED
:
1347 fprintf(stdout
, "%sValue: Type = VALUETYPE_NOT_DEFINED\n", indent
);
1349 case RegValueType::LONG
:
1351 fprintf(stdout
, "%sValue: Type = RegValueType::LONG\n", indent
);
1353 stdout
, "%s Size = %lu\n", indent
,
1354 sal::static_int_cast
< unsigned long >(valueSize
));
1355 fprintf(stdout
, "%s Data = ", indent
);
1358 readINT32(aBuffer
.data(), value
);
1359 fprintf(stdout
, "%ld\n", sal::static_int_cast
< long >(value
));
1362 case RegValueType::STRING
:
1364 sal_Char
* value
= static_cast<sal_Char
*>(std::malloc(valueSize
));
1365 readUtf8(aBuffer
.data(), value
, valueSize
);
1366 fprintf(stdout
, "%sValue: Type = RegValueType::STRING\n", indent
);
1368 stdout
, "%s Size = %lu\n", indent
,
1369 sal::static_int_cast
< unsigned long >(valueSize
));
1370 fprintf(stdout
, "%s Data = \"%s\"\n", indent
, value
);
1374 case RegValueType::UNICODE
:
1376 sal_uInt32 size
= (valueSize
/ 2) * sizeof(sal_Unicode
);
1377 fprintf(stdout
, "%sValue: Type = RegValueType::UNICODE\n", indent
);
1379 stdout
, "%s Size = %lu\n", indent
,
1380 sal::static_int_cast
< unsigned long >(valueSize
));
1381 fprintf(stdout
, "%s Data = ", indent
);
1383 std::unique_ptr
<sal_Unicode
[]> value(new sal_Unicode
[size
]);
1384 readString(aBuffer
.data(), value
.get(), size
);
1386 OString uStr
= OUStringToOString(value
.get(), RTL_TEXTENCODING_UTF8
);
1387 fprintf(stdout
, "L\"%s\"\n", uStr
.getStr());
1390 case RegValueType::BINARY
:
1392 fprintf(stdout
, "%sValue: Type = RegValueType::BINARY\n", indent
);
1394 stdout
, "%s Size = %lu\n", indent
,
1395 sal::static_int_cast
< unsigned long >(valueSize
));
1396 fprintf(stdout
, "%s Data = ", indent
);
1398 typereg::Reader(aBuffer
.data(), valueSize
),
1402 case RegValueType::LONGLIST
:
1404 sal_uInt32 offset
= 4; // initial 4 bytes for the size of the array
1407 readUINT32(aBuffer
.data(), len
);
1409 fprintf(stdout
, "%sValue: Type = RegValueType::LONGLIST\n", indent
);
1411 stdout
, "%s Size = %lu\n", indent
,
1412 sal::static_int_cast
< unsigned long >(valueSize
));
1414 stdout
, "%s Len = %lu\n", indent
,
1415 sal::static_int_cast
< unsigned long >(len
));
1416 fprintf(stdout
, "%s Data = ", indent
);
1418 sal_Int32 longValue
;
1419 for (sal_uInt32 i
=0; i
< len
; i
++)
1421 readINT32(aBuffer
.data() + offset
, longValue
);
1424 fprintf(stdout
, "%s ", indent
);
1427 stdout
, "%lu = %ld\n",
1428 sal::static_int_cast
< unsigned long >(i
),
1429 sal::static_int_cast
< long >(longValue
));
1430 offset
+= 4; // 4 Bytes for sal_Int32
1434 case RegValueType::STRINGLIST
:
1436 sal_uInt32 offset
= 4; // initial 4 bytes for the size of the array
1437 sal_uInt32 sLen
= 0;
1440 readUINT32(aBuffer
.data(), len
);
1442 fprintf(stdout
, "%sValue: Type = RegValueType::STRINGLIST\n", indent
);
1444 stdout
, "%s Size = %lu\n", indent
,
1445 sal::static_int_cast
< unsigned long >(valueSize
));
1447 stdout
, "%s Len = %lu\n", indent
,
1448 sal::static_int_cast
< unsigned long >(len
));
1449 fprintf(stdout
, "%s Data = ", indent
);
1451 for (sal_uInt32 i
=0; i
< len
; i
++)
1453 readUINT32(aBuffer
.data() + offset
, sLen
);
1455 offset
+= 4; // 4 bytes (sal_uInt32) for the string size
1457 sal_Char
*pValue
= static_cast<sal_Char
*>(std::malloc(sLen
));
1458 readUtf8(aBuffer
.data() + offset
, pValue
, sLen
);
1461 fprintf(stdout
, "%s ", indent
);
1464 stdout
, "%lu = \"%s\"\n",
1465 sal::static_int_cast
< unsigned long >(i
), pValue
);
1471 case RegValueType::UNICODELIST
:
1473 sal_uInt32 offset
= 4; // initial 4 bytes for the size of the array
1474 sal_uInt32 sLen
= 0;
1477 readUINT32(aBuffer
.data(), len
);
1479 fprintf(stdout
, "%sValue: Type = RegValueType::UNICODELIST\n", indent
);
1481 stdout
, "%s Size = %lu\n", indent
,
1482 sal::static_int_cast
< unsigned long >(valueSize
));
1484 stdout
, "%s Len = %lu\n", indent
,
1485 sal::static_int_cast
< unsigned long >(len
));
1486 fprintf(stdout
, "%s Data = ", indent
);
1489 for (sal_uInt32 i
=0; i
< len
; i
++)
1491 readUINT32(aBuffer
.data() + offset
, sLen
);
1493 offset
+= 4; // 4 bytes (sal_uInt32) for the string size
1495 sal_Unicode
*pValue
= static_cast<sal_Unicode
*>(std::malloc((sLen
/ 2) * sizeof(sal_Unicode
)));
1496 readString(aBuffer
.data() + offset
, pValue
, sLen
);
1499 fprintf(stdout
, "%s ", indent
);
1501 uStr
= OUStringToOString(pValue
, RTL_TEXTENCODING_UTF8
);
1503 stdout
, "%lu = L\"%s\"\n",
1504 sal::static_int_cast
< unsigned long >(i
),
1515 fprintf(stdout
, "\n");
1517 return RegError::NO_ERROR
;
1520 RegError
ORegistry::dumpKey(const OUString
& sPath
, const OUString
& sName
, sal_Int16 nSpace
) const
1522 OStoreDirectory rStoreDir
;
1523 OUString
sFullPath(sPath
);
1525 storeAccessMode accessMode
= storeAccessMode::ReadWrite
;
1526 RegError _ret
= RegError::NO_ERROR
;
1530 accessMode
= storeAccessMode::ReadOnly
;
1533 for (int i
= 0; i
< nSpace
; i
++) sIndent
+= " ";
1535 if (sFullPath
.getLength() > 1)
1538 storeError _err
= rStoreDir
.create(m_file
, sFullPath
, sName
, accessMode
);
1540 if (_err
== store_E_NotExists
)
1541 return RegError::KEY_NOT_EXISTS
;
1542 else if (_err
== store_E_WrongFormat
)
1543 return RegError::INVALID_KEY
;
1545 fprintf(stdout
, "%s/ %s\n", sIndent
.getStr(), OUStringToOString(sName
, RTL_TEXTENCODING_UTF8
).getStr());
1547 OUString
sSubPath(sFullPath
);
1551 OStoreDirectory::iterator iter
;
1553 _err
= rStoreDir
.first(iter
);
1555 while (_err
== store_E_None
)
1557 sSubName
= iter
.m_pszName
;
1559 if (iter
.m_nAttrib
& STORE_ATTRIB_ISDIR
)
1561 _ret
= dumpKey(sSubPath
, sSubName
, nSpace
+2);
1564 _ret
= dumpValue(sSubPath
, sSubName
, nSpace
+2);
1567 if (_ret
!= RegError::NO_ERROR
)
1572 _err
= rStoreDir
.next(iter
);
1575 return RegError::NO_ERROR
;
1578 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */