@@ -65,6 +65,8 @@ RecursiveMutex cs_db;
6565std::map<std::string, std::weak_ptr<BerkeleyEnvironment>> g_dbenvs GUARDED_BY (cs_db); // !< Map from directory name to db environment.
6666} // namespace
6767
68+ static constexpr auto REVERSE_BYTE_ORDER{std::endian::native == std::endian::little ? 4321 : 1234 };
69+
6870bool WalletDatabaseFileId::operator ==(const WalletDatabaseFileId& rhs) const
6971{
7072 return memcmp (value, &rhs.value , sizeof (value)) == 0 ;
@@ -300,7 +302,11 @@ static Span<const std::byte> SpanFromDbt(const SafeDbt& dbt)
300302}
301303
302304BerkeleyDatabase::BerkeleyDatabase (std::shared_ptr<BerkeleyEnvironment> env, fs::path filename, const DatabaseOptions& options) :
303- WalletDatabase (), env(std::move(env)), m_filename(std::move(filename)), m_max_log_mb(options.max_log_mb)
305+ WalletDatabase (),
306+ env (std::move(env)),
307+ m_byteswap (options.require_format == DatabaseFormat::BERKELEY_SWAP),
308+ m_filename (std::move(filename)),
309+ m_max_log_mb (options.max_log_mb)
304310{
305311 auto inserted = this ->env ->m_databases .emplace (m_filename, std::ref (*this ));
306312 assert (inserted.second );
@@ -389,6 +395,10 @@ void BerkeleyDatabase::Open()
389395 }
390396 }
391397
398+ if (m_byteswap) {
399+ pdb_temp->set_lorder (REVERSE_BYTE_ORDER);
400+ }
401+
392402 ret = pdb_temp->open (nullptr , // Txn pointer
393403 fMockDb ? nullptr : strFile.c_str (), // Filename
394404 fMockDb ? strFile.c_str () : " main" , // Logical db name
@@ -521,6 +531,10 @@ bool BerkeleyDatabase::Rewrite(const char* pszSkip)
521531 BerkeleyBatch db (*this , true );
522532 std::unique_ptr<Db> pdbCopy = std::make_unique<Db>(env->dbenv .get (), 0 );
523533
534+ if (m_byteswap) {
535+ pdbCopy->set_lorder (REVERSE_BYTE_ORDER);
536+ }
537+
524538 int ret = pdbCopy->open (nullptr , // Txn pointer
525539 strFileRes.c_str (), // Filename
526540 " main" , // Logical db name
0 commit comments