1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #define LOG_TAG "RdbStoreConfig"
16 #include "rdb_store_config.h"
17 
18 #include <sstream>
19 
20 #include "logger.h"
21 #include "rdb_errno.h"
22 #include "rdb_security_manager.h"
23 #include "string_utils.h"
24 #include "sqlite_global_config.h"
25 
26 namespace OHOS::NativeRdb {
27 using namespace OHOS::Rdb;
28 
RdbStoreConfig(const std::string & name,StorageMode storageMode,bool isReadOnly,const std::vector<uint8_t> & encryptKey,const std::string & journalMode,const std::string & syncMode,const std::string & databaseFileType,SecurityLevel securityLevel,bool isCreateNecessary,bool autoCheck,int journalSize,int pageSize)29 RdbStoreConfig::RdbStoreConfig(const std::string &name, StorageMode storageMode, bool isReadOnly,
30     const std::vector<uint8_t> &encryptKey, const std::string &journalMode, const std::string &syncMode,
31     const std::string &databaseFileType, SecurityLevel securityLevel, bool isCreateNecessary, bool autoCheck,
32     int journalSize, int pageSize)
33     : readOnly_(isReadOnly), isCreateNecessary_(isCreateNecessary), autoCheck_(autoCheck), journalSize_(journalSize),
34       pageSize_(pageSize), securityLevel_(securityLevel), storageMode_(storageMode), path_(name),
35       journalMode_(journalMode), syncMode_(syncMode), databaseFileType(databaseFileType)
36 {
37     name_ = StringUtils::ExtractFileName(name);
38     cryptoParam_.encryptKey_ = encryptKey;
39     walLimitSize_ = GlobalExpr::DB_WAL_DEFAULT_SIZE;
40     checkpointSize_ = GlobalExpr::DB_WAL_WARNING_SIZE;
41     startCheckpointSize_ = GlobalExpr::DB_WAL_SIZE_LIMIT_MIN;
42 }
43 
~RdbStoreConfig()44 RdbStoreConfig::~RdbStoreConfig()
45 {
46     ClearEncryptKey();
47 }
48 
49 /**
50  * Obtains the database name.
51  */
GetName() const52 std::string RdbStoreConfig::GetName() const
53 {
54     return name_;
55 }
56 
57 /**
58  * Obtains the database path.
59  */
GetPath() const60 std::string RdbStoreConfig::GetPath() const
61 {
62     return path_;
63 }
64 
65 /**
66  * Obtains the storage mode.
67  */
GetStorageMode() const68 StorageMode RdbStoreConfig::GetStorageMode() const
69 {
70     return storageMode_;
71 }
72 
73 /**
74  * Obtains the journal mode in this {@code StoreConfig} object.
75  */
GetJournalMode() const76 std::string RdbStoreConfig::GetJournalMode() const
77 {
78     return journalMode_;
79 }
80 
81 /**
82  * Obtains the synchronization mode in this {@code StoreConfig} object.
83  */
GetSyncMode() const84 std::string RdbStoreConfig::GetSyncMode() const
85 {
86     return syncMode_;
87 }
88 
89 /**
90  * Checks whether the database is read-only.
91  */
IsReadOnly() const92 bool RdbStoreConfig::IsReadOnly() const
93 {
94     return readOnly_;
95 }
96 
97 /**
98  * Checks whether the database is memory.
99  */
IsMemoryRdb() const100 bool RdbStoreConfig::IsMemoryRdb() const
101 {
102     return GetStorageMode() == StorageMode::MODE_MEMORY;
103 }
104 
105 /**
106  * Obtains the database file type in this {@code StoreConfig} object.
107  */
GetDatabaseFileType() const108 std::string RdbStoreConfig::GetDatabaseFileType() const
109 {
110     return databaseFileType;
111 }
112 
SetName(std::string name)113 void RdbStoreConfig::SetName(std::string name)
114 {
115     this->name_ = std::move(name);
116 }
117 
118 /**
119  * Sets the journal mode  for the object.
120  */
SetJournalMode(JournalMode journalMode)121 void RdbStoreConfig::SetJournalMode(JournalMode journalMode)
122 {
123     this->journalMode_ = GetJournalModeValue(journalMode);
124 }
125 
SetJournalMode(const std::string & journalMode)126 void RdbStoreConfig::SetJournalMode(const std::string &journalMode)
127 {
128     this->journalMode_ = journalMode;
129 }
130 
SetDatabaseFileType(DatabaseFileType type)131 void RdbStoreConfig::SetDatabaseFileType(DatabaseFileType type)
132 {
133     this->databaseFileType = GetDatabaseFileTypeValue(type);
134 }
135 
136 /**
137  * Sets the path  for the object.
138  */
SetPath(std::string path)139 void RdbStoreConfig::SetPath(std::string path)
140 {
141     this->path_ = std::move(path);
142 }
143 
SetStorageMode(StorageMode storageMode)144 void RdbStoreConfig::SetStorageMode(StorageMode storageMode)
145 {
146     this->storageMode_ = storageMode;
147 }
148 
IsAutoCheck() const149 bool RdbStoreConfig::IsAutoCheck() const
150 {
151     return autoCheck_;
152 }
SetAutoCheck(bool autoCheck)153 void RdbStoreConfig::SetAutoCheck(bool autoCheck)
154 {
155     this->autoCheck_ = autoCheck;
156 }
GetJournalSize() const157 int RdbStoreConfig::GetJournalSize() const
158 {
159     return journalSize_;
160 }
SetJournalSize(int journalSize)161 void RdbStoreConfig::SetJournalSize(int journalSize)
162 {
163     this->journalSize_ = journalSize;
164 }
GetPageSize() const165 int RdbStoreConfig::GetPageSize() const
166 {
167     return pageSize_;
168 }
SetPageSize(int pageSize)169 void RdbStoreConfig::SetPageSize(int pageSize)
170 {
171     this->pageSize_ = pageSize;
172 }
GetEncryptAlgo() const173 EncryptAlgo RdbStoreConfig::GetEncryptAlgo() const
174 {
175     return static_cast<EncryptAlgo>(cryptoParam_.encryptAlgo);
176 }
SetEncryptAlgo(EncryptAlgo encryptAlgo)177 void RdbStoreConfig::SetEncryptAlgo(EncryptAlgo encryptAlgo)
178 {
179     this->cryptoParam_.encryptAlgo = static_cast<int32_t>(encryptAlgo);
180 }
181 
SetReadOnly(bool readOnly)182 void RdbStoreConfig::SetReadOnly(bool readOnly)
183 {
184     this->readOnly_ = readOnly;
185 }
186 
SetDistributedType(DistributedType type)187 int RdbStoreConfig::SetDistributedType(DistributedType type)
188 {
189     if (type != DistributedType::RDB_DEVICE_COLLABORATION) {
190         LOG_ERROR("type is invalid.");
191         return E_ERROR;
192     }
193     distributedType_ = type;
194     return E_OK;
195 }
196 
GetDistributedType() const197 DistributedType RdbStoreConfig::GetDistributedType() const
198 {
199     return distributedType_;
200 }
201 
SetBundleName(const std::string & bundleName)202 int RdbStoreConfig::SetBundleName(const std::string &bundleName)
203 {
204     if (bundleName.empty()) {
205         LOG_ERROR("bundleName is empty.");
206         return E_ERROR;
207     }
208     bundleName_ = bundleName;
209     return E_OK;
210 }
211 
GetBundleName() const212 std::string RdbStoreConfig::GetBundleName() const
213 {
214     return bundleName_;
215 }
216 
SetModuleName(const std::string & moduleName)217 void RdbStoreConfig::SetModuleName(const std::string &moduleName)
218 {
219     moduleName_ = moduleName;
220 }
221 
GetModuleName() const222 std::string RdbStoreConfig::GetModuleName() const
223 {
224     return moduleName_;
225 }
226 
SetServiceName(const std::string & serviceName)227 void RdbStoreConfig::SetServiceName(const std::string &serviceName)
228 {
229     SetBundleName(serviceName);
230 }
231 
SetArea(int32_t area)232 void RdbStoreConfig::SetArea(int32_t area)
233 {
234     area_ = area + 1;
235 }
236 
GetArea() const237 int32_t RdbStoreConfig::GetArea() const
238 {
239     return area_;
240 }
241 
GetJournalModeValue(JournalMode journalMode)242 std::string RdbStoreConfig::GetJournalModeValue(JournalMode journalMode)
243 {
244     std::string value = "";
245 
246     switch (journalMode) {
247         case JournalMode::MODE_DELETE:
248             return "DELETE";
249         case JournalMode::MODE_TRUNCATE:
250             return "TRUNCATE";
251         case JournalMode::MODE_PERSIST:
252             return  "PERSIST";
253         case JournalMode::MODE_MEMORY:
254             return "MEMORY";
255         case JournalMode::MODE_WAL:
256             return "WAL";
257         case JournalMode::MODE_OFF:
258             return "OFF";
259         default:
260             break;
261     }
262     return value;
263 }
264 
GetSyncModeValue(SyncMode syncMode)265 std::string RdbStoreConfig::GetSyncModeValue(SyncMode syncMode)
266 {
267     std::string value = "";
268     switch (syncMode) {
269         case SyncMode::MODE_OFF:
270             return "MODE_OFF";
271         case SyncMode::MODE_NORMAL:
272             return "MODE_NORMAL";
273         case SyncMode::MODE_FULL:
274             return "MODE_FULL";
275         case SyncMode::MODE_EXTRA:
276             return "MODE_EXTRA";
277         default:
278             break;
279     }
280 
281     return value;
282 }
283 
GetDatabaseFileTypeValue(DatabaseFileType databaseFileType)284 std::string RdbStoreConfig::GetDatabaseFileTypeValue(DatabaseFileType databaseFileType)
285 {
286     std::string value = "";
287     switch (databaseFileType) {
288         case DatabaseFileType::NORMAL:
289             return "db";
290         case DatabaseFileType::BACKUP:
291             return "backup";
292         case DatabaseFileType::CORRUPT:
293             return "corrupt";
294         default:
295             break;
296     }
297 
298     return value;
299 }
300 
SetSecurityLevel(SecurityLevel sl)301 void RdbStoreConfig::SetSecurityLevel(SecurityLevel sl)
302 {
303     securityLevel_ = sl;
304 }
305 
GetSecurityLevel() const306 SecurityLevel RdbStoreConfig::GetSecurityLevel() const
307 {
308     return securityLevel_;
309 }
310 
SetEncryptStatus(const bool status)311 void RdbStoreConfig::SetEncryptStatus(const bool status)
312 {
313     this->isEncrypt_ = status;
314 }
315 
IsEncrypt() const316 bool RdbStoreConfig::IsEncrypt() const
317 {
318     return isEncrypt_ || !cryptoParam_.encryptKey_.empty();
319 }
320 
IsCreateNecessary() const321 bool RdbStoreConfig::IsCreateNecessary() const
322 {
323     return isCreateNecessary_;
324 }
325 
SetCreateNecessary(bool isCreateNecessary)326 void RdbStoreConfig::SetCreateNecessary(bool isCreateNecessary)
327 {
328     isCreateNecessary_ = isCreateNecessary;
329 }
330 
GetReadConSize() const331 int RdbStoreConfig::GetReadConSize() const
332 {
333     return readConSize_;
334 }
335 
SetReadConSize(int readConSize)336 void RdbStoreConfig::SetReadConSize(int readConSize)
337 {
338     readConSize_= readConSize;
339 }
340 
SetEncryptKey(const std::vector<uint8_t> & encryptKey)341 void RdbStoreConfig::SetEncryptKey(const std::vector<uint8_t> &encryptKey)
342 {
343     cryptoParam_.encryptKey_.assign(cryptoParam_.encryptKey_.size(), 0);
344     cryptoParam_.encryptKey_ = encryptKey;
345 }
346 
RestoreEncryptKey(const std::vector<uint8_t> & encryptKey) const347 void RdbStoreConfig::RestoreEncryptKey(const std::vector<uint8_t> &encryptKey) const
348 {
349     RdbSecurityManager::GetInstance().RestoreKeyFile(GetPath(), encryptKey);
350     cryptoParam_.encryptKey_.assign(cryptoParam_.encryptKey_.size(), 0);
351     newEncryptKey_.assign(newEncryptKey_.size(), 0);
352     cryptoParam_.encryptKey_ = encryptKey;
353 }
354 
SetNewEncryptKey(const std::vector<uint8_t> newEncryptKey)355 void RdbStoreConfig::SetNewEncryptKey(const std::vector<uint8_t> newEncryptKey)
356 {
357     newEncryptKey_ = newEncryptKey;
358 }
359 
GetEncryptKey() const360 std::vector<uint8_t> RdbStoreConfig::GetEncryptKey() const
361 {
362     return cryptoParam_.encryptKey_;
363 }
364 
ChangeEncryptKey() const365 void RdbStoreConfig::ChangeEncryptKey() const
366 {
367     RdbSecurityManager::GetInstance().ChangeKeyFile(GetPath());
368     if (newEncryptKey_.empty()) {
369         return;
370     }
371     cryptoParam_.encryptKey_.assign(cryptoParam_.encryptKey_.size(), 0);
372     cryptoParam_.encryptKey_.assign(newEncryptKey_.data(), newEncryptKey_.data() + newEncryptKey_.size());
373     newEncryptKey_.assign(newEncryptKey_.size(), 0);
374     newEncryptKey_.resize(0);
375 }
376 
GetNewEncryptKey() const377 std::vector<uint8_t> RdbStoreConfig::GetNewEncryptKey() const
378 {
379     return newEncryptKey_;
380 }
381 
Initialize() const382 int32_t RdbStoreConfig::Initialize() const
383 {
384     return GenerateEncryptedKey();
385 }
386 
GenerateEncryptedKey() const387 int32_t RdbStoreConfig::GenerateEncryptedKey() const
388 {
389     if (!isEncrypt_ || !cryptoParam_.encryptKey_.empty()) {
390         return E_OK;
391     }
392 
393     auto name = bundleName_;
394     if (name.empty()) {
395         name = std::string(path_).substr(0, path_.rfind("/") + 1);
396     }
397     using KeyFileType = RdbSecurityManager::KeyFileType;
398     auto errCode = RdbSecurityManager::GetInstance().Init(name);
399     if (errCode != E_OK) {
400         LOG_ERROR("generate root encrypt key failed, bundleName_:%{public}s", bundleName_.c_str());
401         return errCode;
402     }
403     auto rdbPwd = RdbSecurityManager::GetInstance().GetRdbPassword(path_, KeyFileType::PUB_KEY_FILE);
404     if (rdbPwd.IsValid()) {
405         cryptoParam_.encryptKey_ = std::vector<uint8_t>(rdbPwd.GetData(), rdbPwd.GetData() + rdbPwd.GetSize());
406     }
407     rdbPwd.Clear();
408     if ((rdbPwd.isKeyExpired && autoRekey_) ||
409         RdbSecurityManager::GetInstance().IsKeyFileExists(path_, KeyFileType::PUB_KEY_FILE_NEW_KEY)) {
410         auto rdbNewPwd = RdbSecurityManager::GetInstance().GetRdbPassword(path_, KeyFileType::PUB_KEY_FILE_NEW_KEY);
411         if (rdbNewPwd.IsValid()) {
412             newEncryptKey_ = std::vector<uint8_t>(rdbNewPwd.GetData(), rdbNewPwd.GetData() + rdbNewPwd.GetSize());
413         }
414         rdbPwd.Clear();
415     }
416     if (cryptoParam_.encryptKey_.empty() && newEncryptKey_.empty()) {
417         LOG_WARN("key is inValid, bundleName_:%{public}s", bundleName_.c_str());
418     }
419     return E_OK;
420 }
421 
ClearEncryptKey()422 void RdbStoreConfig::ClearEncryptKey()
423 {
424     cryptoParam_.encryptKey_.assign(cryptoParam_.encryptKey_.size(), 0);
425     newEncryptKey_.assign(newEncryptKey_.size(), 0);
426 }
427 
SetScalarFunction(const std::string & functionName,int argc,ScalarFunction function)428 void RdbStoreConfig::SetScalarFunction(const std::string &functionName, int argc, ScalarFunction function)
429 {
430     customScalarFunctions.try_emplace(functionName, ScalarFunctionInfo(function, argc));
431 }
432 
SetScalarFunctions(const std::map<std::string,ScalarFunctionInfo> functions)433 void RdbStoreConfig::SetScalarFunctions(const std::map<std::string, ScalarFunctionInfo> functions)
434 {
435     customScalarFunctions = functions;
436 }
437 
GetScalarFunctions() const438 std::map<std::string, ScalarFunctionInfo> RdbStoreConfig::GetScalarFunctions() const
439 {
440     return customScalarFunctions;
441 }
442 
SetDataGroupId(const std::string & DataGroupId)443 void RdbStoreConfig::SetDataGroupId(const std::string &DataGroupId)
444 {
445     dataGroupId_ = DataGroupId;
446 }
447 
GetDataGroupId() const448 std::string RdbStoreConfig::GetDataGroupId() const
449 {
450     return dataGroupId_;
451 }
452 
SetAutoClean(bool isAutoClean)453 void RdbStoreConfig::SetAutoClean(bool isAutoClean)
454 {
455     isAutoClean_ = isAutoClean;
456 }
457 
GetAutoClean() const458 bool RdbStoreConfig::GetAutoClean() const
459 {
460     return isAutoClean_;
461 }
462 
SetIsVector(bool isVector)463 void RdbStoreConfig::SetIsVector(bool isVector)
464 {
465     isVector_ = isVector;
466     isVector ? SetDBType(DB_VECTOR) : SetDBType(DB_SQLITE);
467 }
468 
IsVector() const469 bool RdbStoreConfig::IsVector() const
470 {
471     return isVector_;
472 }
473 
SetCustomDir(const std::string & customDir)474 void RdbStoreConfig::SetCustomDir(const std::string &customDir)
475 {
476     customDir_ = customDir;
477 }
478 
GetCustomDir() const479 std::string RdbStoreConfig::GetCustomDir() const
480 {
481     return customDir_;
482 }
483 
SetVisitorDir(const std::string & visitorDir)484 void RdbStoreConfig::SetVisitorDir(const std::string &visitorDir)
485 {
486     visitorDir_ = visitorDir;
487 }
488 
GetVisitorDir() const489 std::string RdbStoreConfig::GetVisitorDir() const
490 {
491     return visitorDir_;
492 }
493 
IsSearchable() const494 bool RdbStoreConfig::IsSearchable() const
495 {
496     return isSearchable_;
497 }
498 
SetSearchable(bool isSearchable)499 void RdbStoreConfig::SetSearchable(bool isSearchable)
500 {
501     isSearchable_ = isSearchable;
502 }
503 
GetWriteTime() const504 int RdbStoreConfig::GetWriteTime() const
505 {
506     return writeTimeout_;
507 }
508 
SetWriteTime(int timeout)509 void RdbStoreConfig::SetWriteTime(int timeout)
510 {
511     writeTimeout_ = std::max(MIN_TIMEOUT, std::min(MAX_TIMEOUT, timeout));
512 }
513 
GetReadTime() const514 int RdbStoreConfig::GetReadTime() const
515 {
516     return readTimeout_;
517 }
518 
SetReadTime(int timeout)519 void RdbStoreConfig::SetReadTime(int timeout)
520 {
521     readTimeout_ = std::max(MIN_TIMEOUT, std::min(MAX_TIMEOUT, timeout));
522 }
523 
SetRoleType(RoleType role)524 void RdbStoreConfig::SetRoleType(RoleType role)
525 {
526     role_ = role;
527 }
528 
GetRoleType() const529 uint32_t RdbStoreConfig::GetRoleType() const
530 {
531     return role_;
532 }
533 
SetDBType(int32_t dbType)534 void RdbStoreConfig::SetDBType(int32_t dbType)
535 {
536     dbType_ = dbType;
537 }
538 
GetDBType() const539 int32_t RdbStoreConfig::GetDBType() const
540 {
541     return dbType_;
542 }
543 
SetAllowRebuild(bool allowRebuild)544 void RdbStoreConfig::SetAllowRebuild(bool allowRebuild)
545 {
546     this->allowRebuilt_ = allowRebuild;
547 }
548 
GetAllowRebuild() const549 bool RdbStoreConfig::GetAllowRebuild() const
550 {
551     return allowRebuilt_;
552 }
553 
SetIntegrityCheck(IntegrityCheck checkType)554 void RdbStoreConfig::SetIntegrityCheck(IntegrityCheck checkType)
555 {
556     checkType_ = checkType;
557 }
558 
GetIntegrityCheck() const559 IntegrityCheck RdbStoreConfig::GetIntegrityCheck() const
560 {
561     return checkType_;
562 }
563 
GetIter() const564 int32_t RdbStoreConfig::GetIter() const
565 {
566     return cryptoParam_.iterNum;
567 }
568 
SetIter(int32_t iter) const569 void RdbStoreConfig::SetIter(int32_t iter) const
570 {
571     cryptoParam_.iterNum = iter;
572 }
573 
SetPluginLibs(const std::vector<std::string> & pluginLibs)574 void RdbStoreConfig::SetPluginLibs(const std::vector<std::string> &pluginLibs)
575 {
576     pluginLibs_ = pluginLibs;
577 }
578 
GetPluginLibs() const579 std::vector<std::string> RdbStoreConfig::GetPluginLibs() const
580 {
581     return pluginLibs_;
582 }
583 
GetHaMode() const584 int32_t RdbStoreConfig::GetHaMode() const
585 {
586     return haMode_;
587 }
588 
SetHaMode(int32_t haMode)589 void RdbStoreConfig::SetHaMode(int32_t haMode)
590 {
591     haMode_ = haMode;
592 }
593 
GetWalLimitSize() const594 ssize_t RdbStoreConfig::GetWalLimitSize() const
595 {
596     return walLimitSize_;
597 }
598 
SetWalLimitSize(ssize_t size)599 void RdbStoreConfig::SetWalLimitSize(ssize_t size)
600 {
601     if (size < GlobalExpr::DB_WAL_DEFAULT_SIZE) {
602         size = GlobalExpr::DB_WAL_DEFAULT_SIZE;
603     }
604     if (size > GlobalExpr::DB_WAL_SIZE_LIMIT_MAX) {
605         size = GlobalExpr::DB_WAL_SIZE_LIMIT_MAX;
606     }
607     walLimitSize_ = size;
608     // '(size >> 1) + (size >> 2)' Size of the WAL file that does not checkpoint within 5 minutes when sqlite_busy
609     checkpointSize_ = (size >> 1) + (size >> 2);
610     // '(size >> 5) + (size >> 7)' Size of the WAL file for starting checkpoint.
611     startCheckpointSize_ = (size >> 5) + (size >> 7);
612 }
613 
GetCheckpointSize() const614 ssize_t RdbStoreConfig::GetCheckpointSize() const
615 {
616     return checkpointSize_;
617 }
618 
GetStartCheckpointSize() const619 ssize_t RdbStoreConfig::GetStartCheckpointSize() const
620 {
621     return startCheckpointSize_;
622 }
623 
EnableRekey(bool enable)624 void RdbStoreConfig::EnableRekey(bool enable)
625 {
626     autoRekey_ = enable;
627 }
628 
SetCryptoParam(RdbStoreConfig::CryptoParam cryptoParam)629 void RdbStoreConfig::SetCryptoParam(RdbStoreConfig::CryptoParam cryptoParam)
630 {
631     cryptoParam_ = cryptoParam;
632 }
633 
GetCryptoParam() const634 RdbStoreConfig::CryptoParam RdbStoreConfig::GetCryptoParam() const
635 {
636     return cryptoParam_;
637 }
638 
639 RdbStoreConfig::CryptoParam::CryptoParam() = default;
640 
~CryptoParam()641 RdbStoreConfig::CryptoParam::~CryptoParam()
642 {
643     encryptKey_.assign(encryptKey_.size(), 0);
644 }
645 
IsValid() const646 bool RdbStoreConfig::CryptoParam::IsValid() const
647 {
648     int32_t count = iterNum;
649     if (count < 0) {
650         return false;
651     }
652 
653     if (encryptAlgo != AES_256_CBC && encryptAlgo != AES_256_GCM) {
654         return false;
655     }
656 
657     if (hmacAlgo < SHA1 || hmacAlgo > SHA512) {
658         return false;
659     }
660 
661     if (kdfAlgo < KDF_SHA1 || kdfAlgo > KDF_SHA512) {
662         return false;
663     }
664 
665     return (cryptoPageSize != 0) && ((cryptoPageSize & DB_INVALID_CRYPTO_PAGE_SIZE_MASK) == 0) &&
666            (cryptoPageSize & (cryptoPageSize - 1)) == 0;
667 }
668 
Format(const RdbStoreConfig & cacheConfig,const RdbStoreConfig & incomingConfig)669 std::string RdbStoreConfig::Format(const RdbStoreConfig &cacheConfig, const RdbStoreConfig &incomingConfig)
670 {
671     std::stringstream oss;
672     oss << " isEncrypt:" << static_cast<int32_t>(cacheConfig.IsEncrypt()) << "->"
673         << static_cast<int32_t>(incomingConfig.IsEncrypt()) << ",";
674     oss << " securityLevel:" << static_cast<int32_t>(cacheConfig.securityLevel_) << "->"
675         << static_cast<int32_t>(incomingConfig.securityLevel_) << ",";
676     oss << " area:" << cacheConfig.area_ << "->" << incomingConfig.area_ << ",";
677     oss << " storageMode:" << static_cast<int32_t>(cacheConfig.storageMode_) << "->"
678         << static_cast<int32_t>(incomingConfig.storageMode_) << ",";
679     oss << " journalMode:" << cacheConfig.journalMode_ << "->" << incomingConfig.journalMode_ << ",";
680     oss << " syncMode:" << cacheConfig.syncMode_ << "->" << incomingConfig.syncMode_ << ",";
681     oss << " databaseFileType:" << cacheConfig.databaseFileType << "->" << incomingConfig.databaseFileType << ",";
682     oss << " journalSize:" << cacheConfig.journalSize_ << "->" << incomingConfig.journalSize_ << ",";
683     oss << " pageSize:" << cacheConfig.pageSize_ << "->" << incomingConfig.pageSize_ << ",";
684     oss << " customDir:" << cacheConfig.customDir_ << "->" << incomingConfig.customDir_ << ",";
685     oss << " haMode:" << cacheConfig.haMode_ << "->" << incomingConfig.haMode_ << ",";
686     oss << " dbType:" << cacheConfig.dbType_ << "->" << incomingConfig.dbType_ << ",";
687 
688     return oss.str();
689 }
690 } // namespace OHOS::NativeRdb
691