From afa6ff7c4b24e3daf6152fb8206f1125041be7e1 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Tue, 3 Feb 2015 04:31:10 -0800 Subject: [PATCH 01/33] Revert RocksDB backend settings: This reverts the change that makes RocksDBQuick the default settings for node_db "type=rocksdb". The quick settings can be obtained by setting "type=rocksdbquick". RocksDBQuick settings are implicated in memory over-utilization problems seen recently. --- Builds/VisualStudio2013/RippleD.vcxproj | 3 + .../VisualStudio2013/RippleD.vcxproj.filters | 3 + .../nodestore/backend/RocksDBFactory.cpp | 165 +++---- .../nodestore/backend/RocksDBQuickFactory.cpp | 402 ++++++++++++++++++ src/ripple/unity/nodestore.cpp | 1 + 5 files changed, 499 insertions(+), 75 deletions(-) create mode 100644 src/ripple/nodestore/backend/RocksDBQuickFactory.cpp diff --git a/Builds/VisualStudio2013/RippleD.vcxproj b/Builds/VisualStudio2013/RippleD.vcxproj index 2ff2aa02b3..62dd8d9585 100644 --- a/Builds/VisualStudio2013/RippleD.vcxproj +++ b/Builds/VisualStudio2013/RippleD.vcxproj @@ -2524,6 +2524,9 @@ True + + True + diff --git a/Builds/VisualStudio2013/RippleD.vcxproj.filters b/Builds/VisualStudio2013/RippleD.vcxproj.filters index 7650cebafa..4336f12c44 100644 --- a/Builds/VisualStudio2013/RippleD.vcxproj.filters +++ b/Builds/VisualStudio2013/RippleD.vcxproj.filters @@ -3606,6 +3606,9 @@ ripple\nodestore\backend + + ripple\nodestore\backend + ripple\nodestore diff --git a/src/ripple/nodestore/backend/RocksDBFactory.cpp b/src/ripple/nodestore/backend/RocksDBFactory.cpp index 78bdc95136..5f612c3bdc 100644 --- a/src/ripple/nodestore/backend/RocksDBFactory.cpp +++ b/src/ripple/nodestore/backend/RocksDBFactory.cpp @@ -26,6 +26,7 @@ #include // VFALCO Bad dependency #include #include +#include #include #include #include @@ -85,6 +86,7 @@ public: class RocksDBBackend : public Backend + , public BatchWriter::Callback { private: std::atomic m_deletePath; @@ -92,6 +94,8 @@ private: public: beast::Journal m_journal; size_t const m_keyBytes; + Scheduler& m_scheduler; + BatchWriter m_batch; std::string m_name; std::unique_ptr m_db; @@ -100,75 +104,72 @@ public: : m_deletePath (false) , m_journal (journal) , m_keyBytes (keyBytes) + , m_scheduler (scheduler) + , m_batch (*this, scheduler) , m_name (keyValues ["path"].toStdString ()) { if (m_name.empty()) throw std::runtime_error ("Missing path in RocksDBFactory backend"); - // Defaults - std::uint64_t budget = 512 * 1024 * 1024; // 512MB - std::string style("level"); - std::uint64_t threads=4; - - if (!keyValues["budget"].isEmpty()) - budget = keyValues["budget"].getIntValue(); - - if (!keyValues["style"].isEmpty()) - style = keyValues["style"].toStdString(); - - if (!keyValues["threads"].isEmpty()) - threads = keyValues["threads"].getIntValue(); - - - // Set options rocksdb::Options options; + rocksdb::BlockBasedTableOptions table_options; options.create_if_missing = true; options.env = env; - if (style == "level") - options.OptimizeLevelStyleCompaction(budget); + if (keyValues["cache_mb"].isEmpty()) + { + table_options.block_cache = rocksdb::NewLRUCache (getConfig ().getSize (siHashNodeDBCache) * 1024 * 1024); + } + else + { + table_options.block_cache = rocksdb::NewLRUCache (keyValues["cache_mb"].getIntValue() * 1024L * 1024L); + } - if (style == "universal") - options.OptimizeUniversalStyleCompaction(budget); - - if (style == "point") - options.OptimizeForPointLookup(budget / 1024 / 1024); // In MB - - options.IncreaseParallelism(threads); - - // Allows hash indexes in blocks - options.prefix_extractor.reset(rocksdb::NewNoopTransform()); - - // overrride OptimizeLevelStyleCompaction - options.min_write_buffer_number_to_merge = 1; - - rocksdb::BlockBasedTableOptions table_options; - // Use hash index - table_options.index_type = - rocksdb::BlockBasedTableOptions::kHashSearch; - table_options.filter_policy.reset( - rocksdb::NewBloomFilterPolicy(10)); - options.table_factory.reset( - NewBlockBasedTableFactory(table_options)); - - // Higher values make reads slower - // table_options.block_size = 4096; - - // No point when DatabaseImp has a cache - // table_options.block_cache = - // rocksdb::NewLRUCache(64 * 1024 * 1024); - - options.memtable_factory.reset(rocksdb::NewHashSkipListRepFactory()); - // Alternative: - // options.memtable_factory.reset( - // rocksdb::NewHashCuckooRepFactory(options.write_buffer_size)); + if (keyValues["filter_bits"].isEmpty()) + { + if (getConfig ().NODE_SIZE >= 2) + table_options.filter_policy.reset (rocksdb::NewBloomFilterPolicy (10)); + } + else if (keyValues["filter_bits"].getIntValue() != 0) + { + table_options.filter_policy.reset (rocksdb::NewBloomFilterPolicy (keyValues["filter_bits"].getIntValue())); + } if (! keyValues["open_files"].isEmpty()) { options.max_open_files = keyValues["open_files"].getIntValue(); } - - if (! keyValues["compression"].isEmpty ()) + + if (! keyValues["file_size_mb"].isEmpty()) + { + options.target_file_size_base = 1024 * 1024 * keyValues["file_size_mb"].getIntValue(); + options.max_bytes_for_level_base = 5 * options.target_file_size_base; + options.write_buffer_size = 2 * options.target_file_size_base; + } + + if (! keyValues["file_size_mult"].isEmpty()) + { + options.target_file_size_multiplier = keyValues["file_size_mult"].getIntValue(); + } + + if (! keyValues["bg_threads"].isEmpty()) + { + options.env->SetBackgroundThreads + (keyValues["bg_threads"].getIntValue(), rocksdb::Env::LOW); + } + + if (! keyValues["high_threads"].isEmpty()) + { + auto const highThreads = keyValues["high_threads"].getIntValue(); + options.env->SetBackgroundThreads (highThreads, rocksdb::Env::HIGH); + + // If we have high-priority threads, presumably we want to + // use them for background flushes + if (highThreads > 0) + options.max_background_flushes = highThreads; + } + + if (! keyValues["compression"].isEmpty ()) { if (keyValues["compression"].getIntValue () == 0) { @@ -176,8 +177,25 @@ public: } } - rocksdb::DB* db = nullptr; + if (! keyValues["block_size"].isEmpty ()) + { + table_options.block_size = keyValues["block_size"].getIntValue (); + } + if (! keyValues["universal_compaction"].isEmpty ()) + { + if (keyValues["universal_compaction"].getIntValue () != 0) + { + options.compaction_style = rocksdb:: kCompactionStyleUniversal; + options.min_write_buffer_number_to_merge = 2; + options.max_write_buffer_number = 6; + options.write_buffer_size = 6 * options.target_file_size_base; + } + } + + options.table_factory.reset(NewBlockBasedTableFactory(table_options)); + + rocksdb::DB* db = nullptr; rocksdb::Status status = rocksdb::DB::Open (options, m_name, &db); if (!status.ok () || !db) throw std::runtime_error (std::string("Unable to open/create RocksDB: ") + status.ToString()); @@ -190,12 +208,6 @@ public: close(); } - std::string - getName() - { - return m_name; - } - void close() override { @@ -210,6 +222,12 @@ public: } } + std::string + getName() + { + return m_name; + } + //-------------------------------------------------------------------------- Status @@ -265,32 +283,29 @@ public: void store (NodeObject::ref object) { - storeBatch(Batch{object}); + m_batch.store (object); } void storeBatch (Batch const& batch) { rocksdb::WriteBatch wb; - + EncodedBlob encoded; for (auto const& e : batch) { encoded.prepare (e); - wb.Put( - rocksdb::Slice(reinterpret_cast(encoded.getKey()), - m_keyBytes), - rocksdb::Slice(reinterpret_cast(encoded.getData()), - encoded.getSize())); + wb.Put ( + rocksdb::Slice (reinterpret_cast ( + encoded.getKey ()), m_keyBytes), + rocksdb::Slice (reinterpret_cast ( + encoded.getData ()), encoded.getSize ())); } - rocksdb::WriteOptions options; + rocksdb::WriteOptions const options; - // Crucial to ensure good write speed and non-blocking writes to memtable - options.disableWAL = true; - auto ret = m_db->Write (options, &wb); if (!ret.ok ()) @@ -336,7 +351,7 @@ public: int getWriteLoad () { - return 0; + return m_batch.getWriteLoad (); } void @@ -366,12 +381,12 @@ class RocksDBFactory : public Factory public: RocksDBEnv m_env; - RocksDBFactory() + RocksDBFactory () { Manager::instance().insert(*this); } - ~RocksDBFactory() + ~RocksDBFactory () { Manager::instance().erase(*this); } diff --git a/src/ripple/nodestore/backend/RocksDBQuickFactory.cpp b/src/ripple/nodestore/backend/RocksDBQuickFactory.cpp new file mode 100644 index 0000000000..d60f07dc13 --- /dev/null +++ b/src/ripple/nodestore/backend/RocksDBQuickFactory.cpp @@ -0,0 +1,402 @@ +//------------------------------------------------------------------------------ +/* + This file is part of rippled: https://github.com/ripple/rippled + Copyright (c) 2012, 2013 Ripple Labs Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#include + +#include + +#if RIPPLE_ROCKSDB_AVAILABLE + +#include // VFALCO Bad dependency +#include +#include +#include +#include +#include +#include +#include // + +namespace ripple { +namespace NodeStore { + +class RocksDBQuickEnv : public rocksdb::EnvWrapper +{ +public: + RocksDBQuickEnv () + : EnvWrapper (rocksdb::Env::Default()) + { + } + + struct ThreadParams + { + ThreadParams (void (*f_)(void*), void* a_) + : f (f_) + , a (a_) + { + } + + void (*f)(void*); + void* a; + }; + + static + void + thread_entry (void* ptr) + { + ThreadParams* const p (reinterpret_cast (ptr)); + void (*f)(void*) = p->f; + void* a (p->a); + delete p; + + static std::atomic n; + std::size_t const id (++n); + std::stringstream ss; + ss << "rocksdb #" << id; + beast::Thread::setCurrentThreadName (ss.str()); + + (*f)(a); + } + + void + StartThread (void (*f)(void*), void* a) + { + ThreadParams* const p (new ThreadParams (f, a)); + EnvWrapper::StartThread (&RocksDBQuickEnv::thread_entry, p); + } +}; + +//------------------------------------------------------------------------------ + +class RocksDBQuickBackend + : public Backend +{ +private: + std::atomic m_deletePath; + +public: + beast::Journal m_journal; + size_t const m_keyBytes; + std::string m_name; + std::unique_ptr m_db; + + RocksDBQuickBackend (int keyBytes, Parameters const& keyValues, + Scheduler& scheduler, beast::Journal journal, RocksDBQuickEnv* env) + : m_deletePath (false) + , m_journal (journal) + , m_keyBytes (keyBytes) + , m_name (keyValues ["path"].toStdString ()) + { + if (m_name.empty()) + throw std::runtime_error ("Missing path in RocksDBQuickFactory backend"); + + // Defaults + std::uint64_t budget = 512 * 1024 * 1024; // 512MB + std::string style("level"); + std::uint64_t threads=4; + + if (!keyValues["budget"].isEmpty()) + budget = keyValues["budget"].getIntValue(); + + if (!keyValues["style"].isEmpty()) + style = keyValues["style"].toStdString(); + + if (!keyValues["threads"].isEmpty()) + threads = keyValues["threads"].getIntValue(); + + + // Set options + rocksdb::Options options; + options.create_if_missing = true; + options.env = env; + + if (style == "level") + options.OptimizeLevelStyleCompaction(budget); + + if (style == "universal") + options.OptimizeUniversalStyleCompaction(budget); + + if (style == "point") + options.OptimizeForPointLookup(budget / 1024 / 1024); // In MB + + options.IncreaseParallelism(threads); + + // Allows hash indexes in blocks + options.prefix_extractor.reset(rocksdb::NewNoopTransform()); + + // overrride OptimizeLevelStyleCompaction + options.min_write_buffer_number_to_merge = 1; + + rocksdb::BlockBasedTableOptions table_options; + // Use hash index + table_options.index_type = + rocksdb::BlockBasedTableOptions::kHashSearch; + table_options.filter_policy.reset( + rocksdb::NewBloomFilterPolicy(10)); + options.table_factory.reset( + NewBlockBasedTableFactory(table_options)); + + // Higher values make reads slower + // table_options.block_size = 4096; + + // No point when DatabaseImp has a cache + // table_options.block_cache = + // rocksdb::NewLRUCache(64 * 1024 * 1024); + + options.memtable_factory.reset(rocksdb::NewHashSkipListRepFactory()); + // Alternative: + // options.memtable_factory.reset( + // rocksdb::NewHashCuckooRepFactory(options.write_buffer_size)); + + if (! keyValues["open_files"].isEmpty()) + { + options.max_open_files = keyValues["open_files"].getIntValue(); + } + + if (! keyValues["compression"].isEmpty ()) + { + if (keyValues["compression"].getIntValue () == 0) + { + options.compression = rocksdb::kNoCompression; + } + } + + rocksdb::DB* db = nullptr; + + rocksdb::Status status = rocksdb::DB::Open (options, m_name, &db); + if (!status.ok () || !db) + throw std::runtime_error (std::string("Unable to open/create RocksDBQuick: ") + status.ToString()); + + m_db.reset (db); + } + + ~RocksDBQuickBackend () + { + close(); + } + + std::string + getName() + { + return m_name; + } + + void + close() override + { + if (m_db) + { + m_db.reset(); + if (m_deletePath) + { + boost::filesystem::path dir = m_name; + boost::filesystem::remove_all (dir); + } + } + } + + //-------------------------------------------------------------------------- + + Status + fetch (void const* key, NodeObject::Ptr* pObject) + { + pObject->reset (); + + Status status (ok); + + rocksdb::ReadOptions const options; + rocksdb::Slice const slice (static_cast (key), m_keyBytes); + + std::string string; + + rocksdb::Status getStatus = m_db->Get (options, slice, &string); + + if (getStatus.ok ()) + { + DecodedBlob decoded (key, string.data (), string.size ()); + + if (decoded.wasOk ()) + { + *pObject = decoded.createObject (); + } + else + { + // Decoding failed, probably corrupted! + // + status = dataCorrupt; + } + } + else + { + if (getStatus.IsCorruption ()) + { + status = dataCorrupt; + } + else if (getStatus.IsNotFound ()) + { + status = notFound; + } + else + { + status = Status (customCode + getStatus.code()); + + m_journal.error << getStatus.ToString (); + } + } + + return status; + } + + void + store (NodeObject::ref object) + { + storeBatch(Batch{object}); + } + + void + storeBatch (Batch const& batch) + { + rocksdb::WriteBatch wb; + + EncodedBlob encoded; + + for (auto const& e : batch) + { + encoded.prepare (e); + + wb.Put( + rocksdb::Slice(reinterpret_cast(encoded.getKey()), + m_keyBytes), + rocksdb::Slice(reinterpret_cast(encoded.getData()), + encoded.getSize())); + } + + rocksdb::WriteOptions options; + + // Crucial to ensure good write speed and non-blocking writes to memtable + options.disableWAL = true; + + auto ret = m_db->Write (options, &wb); + + if (!ret.ok ()) + throw std::runtime_error ("storeBatch failed: " + ret.ToString()); + } + + void + for_each (std::function f) + { + rocksdb::ReadOptions const options; + + std::unique_ptr it (m_db->NewIterator (options)); + + for (it->SeekToFirst (); it->Valid (); it->Next ()) + { + if (it->key ().size () == m_keyBytes) + { + DecodedBlob decoded (it->key ().data (), + it->value ().data (), + it->value ().size ()); + + if (decoded.wasOk ()) + { + f (decoded.createObject ()); + } + else + { + // Uh oh, corrupted data! + if (m_journal.fatal) m_journal.fatal << + "Corrupt NodeObject #" << uint256 (it->key ().data ()); + } + } + else + { + // VFALCO NOTE What does it mean to find an + // incorrectly sized key? Corruption? + if (m_journal.fatal) m_journal.fatal << + "Bad key size = " << it->key ().size (); + } + } + } + + int + getWriteLoad () + { + return 0; + } + + void + setDeletePath() override + { + m_deletePath = true; + } + + //-------------------------------------------------------------------------- + + void + writeBatch (Batch const& batch) + { + storeBatch (batch); + } + + void + verify() override + { + } +}; + +//------------------------------------------------------------------------------ + +class RocksDBQuickFactory : public Factory +{ +public: + RocksDBQuickEnv m_env; + + RocksDBQuickFactory() + { + Manager::instance().insert(*this); + } + + ~RocksDBQuickFactory() + { + Manager::instance().erase(*this); + } + + std::string + getName () const + { + return "RocksDBQuick"; + } + + std::unique_ptr + createInstance ( + size_t keyBytes, + Parameters const& keyValues, + Scheduler& scheduler, + beast::Journal journal) + { + return std::make_unique ( + keyBytes, keyValues, scheduler, journal, &m_env); + } +}; + +static RocksDBQuickFactory rocksDBQuickFactory; + +} +} + +#endif diff --git a/src/ripple/unity/nodestore.cpp b/src/ripple/unity/nodestore.cpp index ee032dee3e..46f89876ad 100644 --- a/src/ripple/unity/nodestore.cpp +++ b/src/ripple/unity/nodestore.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include From 6276c55cc9d0fbbdc89adb602887e34f218b858d Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Tue, 3 Feb 2015 04:32:02 -0800 Subject: [PATCH 02/33] Set version to 0.27.0-sp1 --- Builds/rpm/rippled.spec | 2 +- src/ripple/protocol/impl/BuildInfo.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Builds/rpm/rippled.spec b/Builds/rpm/rippled.spec index cf1bee6c07..f555bec7ce 100644 --- a/Builds/rpm/rippled.spec +++ b/Builds/rpm/rippled.spec @@ -1,5 +1,5 @@ Name: rippled -Version: 0.27.0 +Version: 0.27.0-sp1 Release: 1%{?dist} Summary: Ripple peer-to-peer network daemon diff --git a/src/ripple/protocol/impl/BuildInfo.cpp b/src/ripple/protocol/impl/BuildInfo.cpp index 5ee029d49d..71d273413d 100644 --- a/src/ripple/protocol/impl/BuildInfo.cpp +++ b/src/ripple/protocol/impl/BuildInfo.cpp @@ -35,7 +35,7 @@ char const* getRawVersionString () // // The build version number (edit this for each release) // - "0.27.0" + "0.27.0-sp1" // // Must follow the format described here: // From b0fd92cb3fcec7dc4530e78cdd00cbbdbc5cb33d Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Mon, 2 Feb 2015 06:15:21 -0800 Subject: [PATCH 03/33] Fix unsafe iterator dereference in PeerFinder --- src/ripple/peerfinder/impl/Logic.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ripple/peerfinder/impl/Logic.h b/src/ripple/peerfinder/impl/Logic.h index a4c90d3b11..e897ccffc3 100644 --- a/src/ripple/peerfinder/impl/Logic.h +++ b/src/ripple/peerfinder/impl/Logic.h @@ -201,7 +201,7 @@ public: if (result.second) { if (m_journal.debug) m_journal.debug << beast::leftw (18) << - "Logic add fixed" << "'" << name << + "Logic add fixed '" << name << "' at " << remote_address; return; } @@ -220,8 +220,6 @@ public: typename SharedState::Access state (m_state); Slots::iterator const iter (state->slots.find (remoteAddress)); - SlotImp& slot (*iter->second); - if (iter == state->slots.end()) { // The slot disconnected before we finished the check @@ -231,6 +229,7 @@ public: return; } + SlotImp& slot (*iter->second); slot.checked = true; slot.connectivityCheckInProgress = false; From 96c329221079086e935b98cd97679dcbcc2635f7 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Thu, 29 Jan 2015 06:58:25 -0800 Subject: [PATCH 04/33] Add missing include --- src/beast/beast/module/core/containers/ElementComparator.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/beast/beast/module/core/containers/ElementComparator.h b/src/beast/beast/module/core/containers/ElementComparator.h index d856ab879c..b47e330128 100644 --- a/src/beast/beast/module/core/containers/ElementComparator.h +++ b/src/beast/beast/module/core/containers/ElementComparator.h @@ -24,8 +24,9 @@ #ifndef BEAST_ELEMENTCOMPARATOR_H_INCLUDED #define BEAST_ELEMENTCOMPARATOR_H_INCLUDED -namespace beast -{ +#include + +namespace beast { #ifndef DOXYGEN From 3838d222c28c7abe00320c51aa90c5628cb9d932 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sun, 1 Feb 2015 15:49:21 -0800 Subject: [PATCH 05/33] NuDB: limit size of mempool (RIPD-787): Insert now blocks when the size of the memory pool exceeds a predefined threshold. This solves the problem where sustained insertions cause the memory pool to grow without bound. --- src/beast/beast/nudb/detail/win32_file.h | 1 - src/beast/beast/nudb/store.h | 231 ++++++++++------------- 2 files changed, 96 insertions(+), 136 deletions(-) diff --git a/src/beast/beast/nudb/detail/win32_file.h b/src/beast/beast/nudb/detail/win32_file.h index d497fb517f..4d1c31ef85 100644 --- a/src/beast/beast/nudb/detail/win32_file.h +++ b/src/beast/beast/nudb/detail/win32_file.h @@ -394,7 +394,6 @@ template std::pair win32_file<_>::flags (file_mode mode) { -mode = file_mode::write; std::pair result(0, 0); switch (mode) { diff --git a/src/beast/beast/nudb/store.h b/src/beast/beast/nudb/store.h index d5befef302..5530787d45 100644 --- a/src/beast/beast/nudb/store.h +++ b/src/beast/beast/nudb/store.h @@ -20,8 +20,6 @@ #ifndef BEAST_NUDB_STORE_H_INCLUDED #define BEAST_NUDB_STORE_H_INCLUDED -#include - #include #include #include @@ -64,71 +62,9 @@ #include #endif -#ifndef BEAST_NUDB_DEBUG_CHECKS -# ifndef NDEBUG -# define BEAST_NUDB_DEBUG_CHECKS 0 -# else -# define BEAST_NUDB_DEBUG_CHECKS 0 -# endif -#endif - namespace beast { namespace nudb { -namespace detail { - -// Holds state variables of the open database. -template -struct state -{ - File df; - File kf; - File lf; - path_type dp; - path_type kp; - path_type lp; - buffers b; - pool p0; - pool p1; - cache c0; - cache c1; - key_file_header const kh; - - // pool commit high water mark - std::size_t pool_thresh = 0; - - state (state const&) = delete; - state& operator= (state const&) = delete; - - state (File&& df_, File&& kf_, File&& lf_, - path_type const& dp_, path_type const& kp_, - path_type const& lp_, key_file_header const& kh_, - std::size_t arena_alloc_size); -}; - -template -state::state ( - File&& df_, File&& kf_, File&& lf_, - path_type const& dp_, path_type const& kp_, - path_type const& lp_, key_file_header const& kh_, - std::size_t arena_alloc_size) - : df (std::move(df_)) - , kf (std::move(kf_)) - , lf (std::move(lf_)) - , dp (dp_) - , kp (kp_) - , lp (lp_) - , b (kh_.block_size) - , p0 (kh_.key_size, arena_alloc_size) - , p1 (kh_.key_size, arena_alloc_size) - , c0 (kh_.key_size, kh_.block_size) - , c1 (kh_.key_size, kh_.block_size) - , kh (kh_) -{ -} - -} // detail - /* TODO @@ -169,20 +105,51 @@ private: using clock_type = std::chrono::steady_clock; + using shared_lock_type = boost::shared_lock; + using unique_lock_type = boost::unique_lock; + using blockbuf = typename detail::buffers::value_type; + struct state + { + File df; + File kf; + File lf; + path_type dp; + path_type kp; + path_type lp; + detail::buffers b; + detail::pool p0; + detail::pool p1; + detail::cache c0; + detail::cache c1; + detail::key_file_header const kh; + + // pool commit high water mark + std::size_t pool_thresh = 0; + + state (state const&) = delete; + state& operator= (state const&) = delete; + + state (File&& df_, File&& kf_, File&& lf_, + path_type const& dp_, path_type const& kp_, + path_type const& lp_, + detail::key_file_header const& kh_, + std::size_t arena_alloc_size); + }; + bool open_ = false; // VFALCO Make consistency checks optional? //bool safe_ = true; // Do consistency checks + // VFALCO Unfortunately boost::optional doesn't support // move construction so we use unique_ptr instead. - std::unique_ptr < - detail::state> s_; // State of an open database + std::unique_ptr s_; // State of an open database std::size_t frac_; // accumulates load std::size_t thresh_; // split threshold @@ -195,6 +162,13 @@ private: std::thread thread_; std::condition_variable_any cond_; + // These allow insert to block, preventing the pool + // from exceeding a limit. Currently the limit is + // baked in, and can only be reached during sustained + // insertions, such as while importing. + std::size_t commit_limit_ = 1UL * 1024 * 1024 * 1024; + std::condition_variable_any cond_limit_; + std::atomic epb_; // `true` when ep_ set std::exception_ptr ep_; @@ -319,16 +293,10 @@ private: std::size_t buckets, std::size_t modulus, detail::bulk_writer& w); - void - check (std::size_t n, detail::bucket& b, - std::size_t buckets, std::size_t modulus); - detail::bucket load (std::size_t n, detail::cache& c1, detail::cache& c0, void* buf); - bool check(); - void commit(); @@ -338,6 +306,30 @@ private: //------------------------------------------------------------------------------ +template +basic_store::state::state ( + File&& df_, File&& kf_, File&& lf_, + path_type const& dp_, path_type const& kp_, + path_type const& lp_, + detail::key_file_header const& kh_, + std::size_t arena_alloc_size) + : df (std::move(df_)) + , kf (std::move(kf_)) + , lf (std::move(lf_)) + , dp (dp_) + , kp (kp_) + , lp (lp_) + , b (kh_.block_size) + , p0 (kh_.key_size, arena_alloc_size) + , p1 (kh_.key_size, arena_alloc_size) + , c0 (kh_.key_size, kh_.block_size) + , c1 (kh_.key_size, kh_.block_size) + , kh (kh_) +{ +} + +//------------------------------------------------------------------------------ + template basic_store::~basic_store() { @@ -384,7 +376,7 @@ basic_store::open ( verify (dh); verify (kh); verify (dh, kh); - auto s = std::make_unique>( + auto s = std::make_unique( std::move(df), std::move(kf), std::move(lf), dat_path, key_path, log_path, kh, arena_alloc_size); @@ -576,10 +568,22 @@ basic_store::insert (void const* key, { unique_lock_type m (m_); s_->p1.insert (h, key, data, size); - bool const full = + // Did we go over the commit limit? + if (commit_limit_ > 0 && + s_->p1.data_size() >= commit_limit_) + { + // Yes, start a new commit + cond_.notify_all(); + // Wait for pool to shrink + cond_limit_.wait(m, + [this]() { return + s_->p1.data_size() < + commit_limit_; }); + } + bool const notify = s_->p1.data_size() >= s_->pool_thresh; m.unlock(); - if (full) + if (notify) cond_.notify_all(); } return true; @@ -738,11 +742,6 @@ basic_store::load ( auto iter = c1.find(n); if (iter != c1.end()) return iter->second; -#if BEAST_NUDB_DEBUG_CHECKS - if (n >= buckets_) - throw std::logic_error( - "nudb: missing bucket in cache"); -#endif iter = c0.find(n); if (iter != c0.end()) return c1.insert (n, @@ -755,24 +754,6 @@ basic_store::load ( return c1.insert (n, tmp)->second; } -template -void -basic_store::check ( - std::size_t n, detail::bucket& b, - std::size_t buckets, std::size_t modulus) -{ - using namespace detail; - for (std::size_t i = 0; i < b.size(); ++i) - { - auto const e = b[i]; - auto const h = hash( - e.key, s_->kh.key_size, s_->kh.salt); - auto const n1 = bucket_index( - h, buckets, modulus); - assert(n1 == n); - } -} - // Commit the memory pool to disk, then sync. // // Preconditions: @@ -795,6 +776,8 @@ basic_store::commit() unique_lock_type m (m_); if (s_->p1.empty()) return; + if (s_->p1.data_size() >= commit_limit_) + cond_limit_.notify_all(); swap (s_->c1, c1); swap (s_->p0, s_->p1); s_->pool_thresh = std::max( @@ -826,10 +809,6 @@ basic_store::commit() // Write inserted data to the data file for (auto& e : s_->p0) { - #if BEAST_NUDB_DEBUG_CHECKS - assert (e.first.hash == hash( - e.first.key, s_->kh.key_size, s_->kh.salt)); - #endif // VFALCO This could be UB since other // threads are reading other data members // of this object in memory @@ -848,10 +827,6 @@ basic_store::commit() // of original and modified buckets for (auto const e : s_->p0) { - #if BEAST_NUDB_DEBUG_CHECKS - assert (e.first.hash == hash( - e.first.key, s_->kh.key_size, s_->kh.salt)); - #endif // VFALCO Should this be >= or > ? if ((frac_ += 65536) >= thresh_) { @@ -862,35 +837,19 @@ basic_store::commit() auto const n1 = buckets - (modulus / 2); auto const n2 = buckets++; auto b1 = load (n1, c1, s_->c0, buf2.get()); - #if BEAST_NUDB_DEBUG_CHECKS - check(n1, b1, buckets, modulus); - #endif auto b2 = c1.create (n2); // If split spills, the writer is // flushed which can amplify writes. split (b1, b2, tmp, n1, n2, buckets, modulus, w); - #if BEAST_NUDB_DEBUG_CHECKS - check(n1, b1, buckets, modulus); - check(n2, b2, buckets, modulus); - #endif } // insert auto const n = bucket_index( e.first.hash, buckets, modulus); auto b = load (n, c1, s_->c0, buf2.get()); // This can amplify writes if it spills. - #if BEAST_NUDB_DEBUG_CHECKS - check(n, b, buckets, modulus); - #endif maybe_spill (b, w); - #if BEAST_NUDB_DEBUG_CHECKS - check(n, b, buckets, modulus); - #endif b.insert (e.second, e.first.size, e.first.key); - #if BEAST_NUDB_DEBUG_CHECKS - check(n, b, buckets, modulus); - #endif } w.flush(); } @@ -950,27 +909,31 @@ template void basic_store::run() { + auto const pred = + [this]() + { + return + ! open_ || + s_->p1.data_size() >= + s_->pool_thresh || + s_->p1.data_size() >= + commit_limit_; + }; try { while (open_) { - auto when = clock_type::now() + - std::chrono::seconds(1); for(;;) { + using std::chrono::seconds; unique_lock_type m (m_); bool const timeout = - cond_.wait_until (m, when) == - std::cv_status::timeout; + ! cond_.wait_for (m, + seconds(1), pred); if (! open_) break; - if (timeout || - s_->p1.data_size() >= - s_->pool_thresh) - { - m.unlock(); - commit(); - } + m.unlock(); + commit(); // Reclaim some memory if // we get a spare moment. if (timeout) @@ -982,8 +945,6 @@ basic_store::run() s_->c1.shrink_to_fit(); s_->c0.shrink_to_fit(); m.unlock(); - when = clock_type::now() + - std::chrono::seconds(1); } } } From 97126f18b10f620dbcc0bcfba86ac50d985edf23 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sat, 24 Jan 2015 07:08:03 -0800 Subject: [PATCH 06/33] Add /crawl cgi request feature to peer protocol (RIPD-729): This adds support for a cgi /crawl request, issued over HTTPS to the configured peer protocol port. The response to the request is a JSON object containing the node public key, type, and IP address of each directly connected neighbor. The IP address is suppressed unless the neighbor has requested its address to be revealed by adding "Crawl: public" to its HTTP headers. This field is currently set by the peer_private option in the rippled.cfg file. --- src/ripple/overlay/Overlay.h | 6 +++ src/ripple/overlay/README.md | 30 ++++++----- src/ripple/overlay/impl/ConnectAttempt.cpp | 13 ++--- src/ripple/overlay/impl/ConnectAttempt.h | 3 +- src/ripple/overlay/impl/OverlayImpl.cpp | 51 ++++++++++++++++++- src/ripple/overlay/impl/OverlayImpl.h | 7 +++ src/ripple/overlay/impl/PeerImp.cpp | 40 ++++++--------- src/ripple/overlay/impl/PeerImp.h | 23 ++------- src/ripple/peerfinder/Manager.h | 8 +++ src/ripple/peerfinder/impl/Logic.h | 7 +++ src/ripple/peerfinder/impl/Manager.cpp | 6 +++ .../peerfinder/impl/PeerfinderConfig.cpp | 2 +- src/ripple/server/impl/ServerHandlerImp.cpp | 1 + 13 files changed, 129 insertions(+), 68 deletions(-) diff --git a/src/ripple/overlay/Overlay.h b/src/ripple/overlay/Overlay.h index c54a8fa9f9..bb3be5b988 100644 --- a/src/ripple/overlay/Overlay.h +++ b/src/ripple/overlay/Overlay.h @@ -20,6 +20,7 @@ #ifndef RIPPLE_OVERLAY_OVERLAY_H_INCLUDED #define RIPPLE_OVERLAY_OVERLAY_H_INCLUDED +#include #include #include #include @@ -101,6 +102,11 @@ public: std::size_t size () = 0; + /** Returns information reported to the crawl cgi command. */ + virtual + Json::Value + crawl() = 0; + /** Return diagnostics on the status of all peers. @deprecated This is superceded by PropertyStream */ diff --git a/src/ripple/overlay/README.md b/src/ripple/overlay/README.md index fef265257f..a4c831746a 100644 --- a/src/ripple/overlay/README.md +++ b/src/ripple/overlay/README.md @@ -51,9 +51,6 @@ operating in the Client Handler role accepts incoming connections from clients and services them through the JSON-RPC interface. A peer can operate in either the leaf or superpeer roles while also operating as a client handler. - - - ## Handshake To establish a protocol connection, a peer makes an outgoing TLS encrypted @@ -69,8 +66,8 @@ Upgrade: RTXP/1.2, RTXP/1.3 Connection: Upgrade Connect-As: Leaf, Peer Accept-Encoding: identity, zlib, snappy -Protocol-Public-Key: aBRoQibi2jpDofohooFuzZi9nEzKw9Zdfc4ExVNmuXHaJpSPh8uJ -Protocol-Session-Cookie: 71ED064155FFADFA38782C5E0158CB26 +Public-Key: aBRoQibi2jpDofohooFuzZi9nEzKw9Zdfc4ExVNmuXHaJpSPh8uJ +Session-Signature: 71ED064155FFADFA38782C5E0158CB26 ``` Upon receipt of a well-formed HTTP request the remote peer will send back a @@ -84,8 +81,8 @@ Upgrade: RTXP/1.2 Connection: Upgrade Connect-As: Leaf Transfer-Encoding: snappy -Protocol-Public-Key: aBRoQibi2jpDofohooFuzZi9nEzKw9Zdfc4ExVNmuXHaJpSPh8uJ -Protocol-Session-Cookie: 71ED064155FFADFA38782C5E0158CB26 +Public-Key: aBRoQibi2jpDofohooFuzZi9nEzKw9Zdfc4ExVNmuXHaJpSPh8uJ +Session-Signature: 71ED064155FFADFA38782C5E0158CB26 ``` If the remote peer has no available slots, the HTTP status code 503 (Service @@ -163,22 +160,29 @@ Content-Type: application/json of elements specified in the request. If a server does not recognize any of the connection types it must return a HTTP error response. -* `Protocol-Public-Key` +* `Public-Key` - This field value must be present, and contain a Base58 encoded value used + This field value must be present, and contain a base 64 encoded value used as a server public key identifier. -* `Protocol-Session-Proof` +* `Session-Signature` - This field must be present (TODO) + This field must be present. It contains a cryptographic token formed + from the SHA512 hash of the shared data exchanged during SSL handshaking. + For more details see the corresponding source code. -* _User Defined_ +* `Crawl` (optional) + + If present, and the value is "public" then neighbors will report the IP + address to crawler requests. If absent, neighbor's default behavior is to + not report IP addresses. + +* _User Defined_ (Unimplemented) The rippled operator may specify additional, optional fields and values through the configuration. These headers will be transmitted in the corresponding request or response messages. - --- [overlay_network]: http://en.wikipedia.org/wiki/Overlay_network diff --git a/src/ripple/overlay/impl/ConnectAttempt.cpp b/src/ripple/overlay/impl/ConnectAttempt.cpp index 9d9c17b65f..f3d53a0e26 100644 --- a/src/ripple/overlay/impl/ConnectAttempt.cpp +++ b/src/ripple/overlay/impl/ConnectAttempt.cpp @@ -218,7 +218,8 @@ ConnectAttempt::onHandshake (error_code ec) return close(); // makeSharedValue logs beast::http::message req = makeRequest( - remote_endpoint_.address()); + ! overlay_.peerFinder().config().peerPrivate, + remote_endpoint_.address()); auto const hello = buildHello (sharedValue, getApp()); appendHello (req, hello); @@ -509,7 +510,7 @@ ConnectAttempt::onReadBody (error_code ec, //-------------------------------------------------------------------------- beast::http::message -ConnectAttempt::makeRequest ( +ConnectAttempt::makeRequest (bool crawl, boost::asio::ip::address const& remote_address) { beast::http::message m; @@ -521,13 +522,7 @@ ConnectAttempt::makeRequest ( //std::string("RTXP/") + to_string (BuildInfo::getCurrentProtocol())); m.headers.append ("Connection", "Upgrade"); m.headers.append ("Connect-As", "Peer"); - //m.headers.append ("Connect-As", "Leaf, Peer"); - //m.headers.append ("Accept-Encoding", "identity"); - //m.headers.append ("Local-Address", stream_. - //m.headers.append ("X-Try-IPs", "192.168.0.1:51234"); - //m.headers.append ("X-Try-IPs", "208.239.114.74:51234"); - //m.headers.append ("A", "BC"); - //m.headers.append ("Content-Length", "0"); + m.headers.append ("Crawl", crawl ? "public" : "private"); return m; } diff --git a/src/ripple/overlay/impl/ConnectAttempt.h b/src/ripple/overlay/impl/ConnectAttempt.h index 4e39904c44..c88f42538a 100644 --- a/src/ripple/overlay/impl/ConnectAttempt.h +++ b/src/ripple/overlay/impl/ConnectAttempt.h @@ -106,7 +106,8 @@ private: static beast::http::message - makeRequest (boost::asio::ip::address const& remote_address); + makeRequest (bool crawl, + boost::asio::ip::address const& remote_address); template void processResponse (beast::http::message const& m, diff --git a/src/ripple/overlay/impl/OverlayImpl.cpp b/src/ripple/overlay/impl/OverlayImpl.cpp index f94f84d0c8..da1fadb427 100644 --- a/src/ripple/overlay/impl/OverlayImpl.cpp +++ b/src/ripple/overlay/impl/OverlayImpl.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -193,6 +194,8 @@ OverlayImpl::onHandoff (std::unique_ptr && ssl_bundle, beast::Journal journal (sink); Handoff handoff; + if (processRequest(request, handoff)) + return handoff; if (! isPeerUpgrade(request)) return handoff; @@ -461,8 +464,9 @@ OverlayImpl::onPrepare() auto const port = serverHandler_.setup().overlay.port; + config.peerPrivate = getConfig().PEER_PRIVATE; config.wantIncoming = - (! getConfig ().PEER_PRIVATE) && (port != 0); + (! config.peerPrivate) && (port != 0); // if it's a private peer or we are running as standalone // automatic connections would defeat the purpose. config.autoConnect = @@ -604,6 +608,34 @@ OverlayImpl::size() return m_publicKeyMap.size (); } +Json::Value +OverlayImpl::crawl() +{ + Json::Value jv; + auto& av = jv["active"] = Json::Value(Json::arrayValue); + std::lock_guard lock (mutex_); + for (auto const& e : m_publicKeyMap) + { + auto const sp = e.second.lock(); + if (sp) + { + auto& pv = av.append(Json::Value(Json::objectValue)); + pv["type"] = "peer"; + pv["public_key"] = beast::base64_encode( + sp->getNodePublic().getNodePublic().data(), + sp->getNodePublic().getNodePublic().size()); + if (sp->crawl()) + { + if (sp->slot()->inbound()) + pv["ip"] = sp->getRemoteAddress().address().to_string(); + else + pv["ip"] = sp->getRemoteAddress().to_string(); + } + } + } + return jv; +} + // Returns information on verified peers. Json::Value OverlayImpl::json () @@ -611,6 +643,23 @@ OverlayImpl::json () return foreach (get_peer_json()); } +bool +OverlayImpl::processRequest (beast::http::message const& req, + Handoff& handoff) +{ + if (req.url() != "/crawl") + return false; + + beast::http::message resp; + resp.request(false); + resp.status(200); + resp.reason("OK"); + Json::Value v; + v["overlay"] = crawl(); + handoff.response = HTTP::make_JsonWriter(resp, v); + return true; +} + Overlay::PeerSequence OverlayImpl::getActivePeers() { diff --git a/src/ripple/overlay/impl/OverlayImpl.h b/src/ripple/overlay/impl/OverlayImpl.h index df37ed3d9f..6db57fef46 100644 --- a/src/ripple/overlay/impl/OverlayImpl.h +++ b/src/ripple/overlay/impl/OverlayImpl.h @@ -207,9 +207,16 @@ private: std::size_t size() override; + Json::Value + crawl() override; + Json::Value json() override; + bool + processRequest (beast::http::message const& req, + Handoff& handoff); + //-------------------------------------------------------------------------- // diff --git a/src/ripple/overlay/impl/PeerImp.cpp b/src/ripple/overlay/impl/PeerImp.cpp index 754227ff12..a2ffd9d71e 100644 --- a/src/ripple/overlay/impl/PeerImp.cpp +++ b/src/ripple/overlay/impl/PeerImp.cpp @@ -187,6 +187,15 @@ PeerImp::charge (Resource::Charge const& fee) //------------------------------------------------------------------------------ +bool +PeerImp::crawl() const +{ + auto const iter = http_message_.headers.find("Crawl"); + if (iter == http_message_.headers.end()) + return false; + return beast::ci_equal(iter->second, "public"); +} + Json::Value PeerImp::json() { @@ -407,28 +416,6 @@ PeerImp::makePrefix(id_t id) return ss.str(); } -beast::http::message -PeerImp::makeRequest(boost::asio::ip::address const& remote_address) -{ - beast::http::message m; - m.method (beast::http::method_t::http_get); - m.url ("/"); - m.version (1, 1); - m.headers.append ("User-Agent", BuildInfo::getFullVersionString()); - m.headers.append ("Upgrade", "RTXP/1.3"); - //std::string("RTXP/") + to_string (BuildInfo::getCurrentProtocol())); - m.headers.append ("Connection", "Upgrade"); - m.headers.append ("Connect-As", "Peer"); - //m.headers.append ("Connect-As", "Leaf, Peer"); - //m.headers.append ("Accept-Encoding", "identity"); - //m.headers.append ("Local-Address", stream_. - //m.headers.append ("X-Try-IPs", "192.168.0.1:51234"); - //m.headers.append ("X-Try-IPs", "208.239.114.74:51234"); - //m.headers.append ("A", "BC"); - //m.headers.append ("Content-Length", "0"); - return m; -} - void PeerImp::onTimer (error_code const& ec) { @@ -497,7 +484,9 @@ void PeerImp::doAccept() // TODO Apply headers to connection state. - auto resp = makeResponse(http_message_, sharedValue); + auto resp = makeResponse( + ! overlay_.peerFinder().config().peerPrivate, + http_message_, sharedValue); beast::http::write (write_buffer_, resp); auto const protocol = BuildInfo::make_protocol(hello_.protoversion()); @@ -536,8 +525,8 @@ void PeerImp::doAccept() } beast::http::message -PeerImp::makeResponse (beast::http::message const& req, - uint256 const& sharedValue) +PeerImp::makeResponse (bool crawl, + beast::http::message const& req, uint256 const& sharedValue) { beast::http::message resp; resp.request(false); @@ -548,6 +537,7 @@ PeerImp::makeResponse (beast::http::message const& req, resp.headers.append("Upgrade", "RTXP/1.2"); resp.headers.append("Connect-AS", "Peer"); resp.headers.append("Server", BuildInfo::getFullVersionString()); + resp.headers.append ("Crawl", crawl ? "public" : "private"); protocol::TMHello hello = buildHello(sharedValue, getApp()); appendHello(resp, hello); return resp; diff --git a/src/ripple/overlay/impl/PeerImp.h b/src/ripple/overlay/impl/PeerImp.h index 75eee1bccf..4616f1943e 100644 --- a/src/ripple/overlay/impl/PeerImp.h +++ b/src/ripple/overlay/impl/PeerImp.h @@ -109,49 +109,37 @@ private: // the current conditions as closely as possible. beast::IP::Endpoint remote_address_; - // These is up here to prevent warnings about order of initializations + // These are up here to prevent warnings about order of initializations // OverlayImpl& overlay_; bool m_inbound; - State state_; // Current state bool detaching_ = false; - // Node public key of peer. RippleAddress publicKey_; - std::string name_; - uint256 sharedValue_; // The indices of the smallest and largest ledgers this peer has available // LedgerIndex minLedger_ = 0; LedgerIndex maxLedger_ = 0; - uint256 closedLedgerHash_; uint256 previousLedgerHash_; - std::list recentLedgers_; std::list recentTxSets_; mutable std::mutex recentLock_; - protocol::TMStatusChange last_status_; protocol::TMHello hello_; - Resource::Consumer usage_; PeerFinder::Slot::ptr slot_; - beast::asio::streambuf read_buffer_; beast::http::message http_message_; - boost::optional http_parser_; beast::http::body http_body_; beast::asio::streambuf write_buffer_; std::queue send_queue_; bool gracefulClose_ = false; - std::unique_ptr load_event_; - std::unique_ptr validatorsConnection_; //-------------------------------------------------------------------------- @@ -234,6 +222,9 @@ public: return id_; } + bool + crawl() const; + bool cluster() const override { @@ -300,10 +291,6 @@ private: std::string makePrefix(id_t id); - static - beast::http::message - makeRequest (boost::asio::ip::address const& remote_address); - // Called when the timer wait completes void onTimer (boost::system::error_code const& ec); @@ -320,7 +307,7 @@ private: static beast::http::message - makeResponse (beast::http::message const& req, + makeResponse (bool crawl, beast::http::message const& req, uint256 const& sharedValue); void diff --git a/src/ripple/peerfinder/Manager.h b/src/ripple/peerfinder/Manager.h index 8d9cd62fd6..7fada8eac1 100644 --- a/src/ripple/peerfinder/Manager.h +++ b/src/ripple/peerfinder/Manager.h @@ -58,6 +58,9 @@ struct Config */ double outPeers; + /** `true` if we want our IP address kept private. */ + bool peerPrivate = true; + /** `true` if we want to accept incoming connections. */ bool wantIncoming; @@ -136,6 +139,11 @@ public: */ virtual void setConfig (Config const& config) = 0; + /** Returns the configuration for the manager. */ + virtual + Config + config() = 0; + /** Add a peer that should always be connected. This is useful for maintaining a private cluster of peers. The string is the name as specified in the configuration diff --git a/src/ripple/peerfinder/impl/Logic.h b/src/ripple/peerfinder/impl/Logic.h index e897ccffc3..158ac38b73 100644 --- a/src/ripple/peerfinder/impl/Logic.h +++ b/src/ripple/peerfinder/impl/Logic.h @@ -179,6 +179,13 @@ public: state->counts.onConfig (state->config); } + Config + config() + { + typename SharedState::Access state (m_state); + return state->config; + } + void addFixedPeer (std::string const& name, std::vector const& addresses) diff --git a/src/ripple/peerfinder/impl/Manager.cpp b/src/ripple/peerfinder/impl/Manager.cpp index 3ec935b687..e1316ef623 100644 --- a/src/ripple/peerfinder/impl/Manager.cpp +++ b/src/ripple/peerfinder/impl/Manager.cpp @@ -94,6 +94,12 @@ public: m_logic.config (config); } + Config + config() override + { + return m_logic.config(); + } + void addFixedPeer (std::string const& name, std::vector const& addresses) override { diff --git a/src/ripple/peerfinder/impl/PeerfinderConfig.cpp b/src/ripple/peerfinder/impl/PeerfinderConfig.cpp index 4f10d14465..4b98c0ff46 100644 --- a/src/ripple/peerfinder/impl/PeerfinderConfig.cpp +++ b/src/ripple/peerfinder/impl/PeerfinderConfig.cpp @@ -24,7 +24,7 @@ namespace ripple { namespace PeerFinder { -Config::Config () +Config::Config() : maxPeers (Tuning::defaultMaxPeers) , outPeers (calcOutPeers ()) , wantIncoming (true) diff --git a/src/ripple/server/impl/ServerHandlerImp.cpp b/src/ripple/server/impl/ServerHandlerImp.cpp index 42267f41e9..2fa74bb59a 100644 --- a/src/ripple/server/impl/ServerHandlerImp.cpp +++ b/src/ripple/server/impl/ServerHandlerImp.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include From 3ae23b6a54355309ab66f697365230f4b8c63db0 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Tue, 3 Feb 2015 08:51:37 -0800 Subject: [PATCH 07/33] Remove spurious call to fetch in NuDBBackend --- src/ripple/nodestore/backend/NuDBFactory.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/ripple/nodestore/backend/NuDBFactory.cpp b/src/ripple/nodestore/backend/NuDBFactory.cpp index 87c28fdfe1..d653697fe9 100644 --- a/src/ripple/nodestore/backend/NuDBFactory.cpp +++ b/src/ripple/nodestore/backend/NuDBFactory.cpp @@ -190,6 +190,11 @@ public: }; //-------------------------------------------------------------------------- + // + // Version 1 Database + // + // Uncompressed + // Status fetch1 (void const* key, @@ -221,6 +226,11 @@ public: } //-------------------------------------------------------------------------- + // + // Version 2 Database + // + // Snappy compression + // Status fetch2 (void const* key, @@ -270,10 +280,6 @@ public: Status fetch (void const* key, NodeObject::Ptr* pno) { - Buffer b1; - if (! db_.fetch (key, b1)) - return notFound; - switch (db_.appnum()) { case typeOne: return fetch1 (key, pno); From dad460dcfc8466a8e1c59529d490829fcfaeee73 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sun, 8 Feb 2015 19:43:04 -0800 Subject: [PATCH 08/33] Squashed 'src/lz4/' content from commit e25b51d git-subtree-dir: src/lz4 git-subtree-split: e25b51de7b51101e04ceea194dd557fcc23c03ca --- .gitattributes | 10 + .travis.yml | 26 + LZ4_Frame_Format.html | 1 + Makefile | 136 +++ NEWS | 133 +++ README.md | 56 + cmake_unofficial/CMakeLists.txt | 82 ++ examples/HCStreaming_ringBuffer.c | 239 +++++ examples/Makefile | 89 ++ examples/blockStreaming_doubleBuffer.c | 195 ++++ examples/blockStreaming_lineByLine.c | 207 ++++ examples/blockStreaming_ringBuffer.c | 200 ++++ examples/printVersion.c | 13 + images/image00.png | Bin 0 -> 8028 bytes images/image01.png | Bin 0 -> 3121 bytes images/image02.png | Bin 0 -> 6355 bytes images/image03.png | Bin 0 -> 3512 bytes images/image04.png | Bin 0 -> 4089 bytes images/image05.png | Bin 0 -> 4233 bytes images/image06.png | Bin 0 -> 6246 bytes lib/LICENSE | 24 + lib/Makefile | 117 ++ lib/liblz4.pc.in | 14 + lib/lz4.c | 1367 ++++++++++++++++++++++++ lib/lz4.h | 315 ++++++ lib/lz4frame.c | 1330 +++++++++++++++++++++++ lib/lz4frame.h | 252 +++++ lib/lz4frame_static.h | 72 ++ lib/lz4hc.c | 751 +++++++++++++ lib/lz4hc.h | 174 +++ lib/xxhash.c | 935 ++++++++++++++++ lib/xxhash.h | 156 +++ lz4_block_format.txt | 120 +++ programs/COPYING | 339 ++++++ programs/Makefile | 206 ++++ programs/bench.c | 435 ++++++++ programs/bench.h | 42 + programs/datagen.c | 286 +++++ programs/frametest.c | 669 ++++++++++++ programs/fullbench.c | 842 +++++++++++++++ programs/fuzzer.c | 1167 ++++++++++++++++++++ programs/lz4.1 | 88 ++ programs/lz4c.1 | 33 + programs/lz4cat.1 | 32 + programs/lz4cli.c | 501 +++++++++ programs/lz4io.c | 669 ++++++++++++ programs/lz4io.h | 77 ++ 47 files changed, 12400 insertions(+) create mode 100644 .gitattributes create mode 100644 .travis.yml create mode 100644 LZ4_Frame_Format.html create mode 100644 Makefile create mode 100644 NEWS create mode 100644 README.md create mode 100644 cmake_unofficial/CMakeLists.txt create mode 100755 examples/HCStreaming_ringBuffer.c create mode 100644 examples/Makefile create mode 100644 examples/blockStreaming_doubleBuffer.c create mode 100644 examples/blockStreaming_lineByLine.c create mode 100644 examples/blockStreaming_ringBuffer.c create mode 100644 examples/printVersion.c create mode 100755 images/image00.png create mode 100755 images/image01.png create mode 100755 images/image02.png create mode 100755 images/image03.png create mode 100755 images/image04.png create mode 100755 images/image05.png create mode 100755 images/image06.png create mode 100644 lib/LICENSE create mode 100644 lib/Makefile create mode 100644 lib/liblz4.pc.in create mode 100644 lib/lz4.c create mode 100644 lib/lz4.h create mode 100644 lib/lz4frame.c create mode 100644 lib/lz4frame.h create mode 100644 lib/lz4frame_static.h create mode 100644 lib/lz4hc.c create mode 100644 lib/lz4hc.h create mode 100644 lib/xxhash.c create mode 100644 lib/xxhash.h create mode 100644 lz4_block_format.txt create mode 100644 programs/COPYING create mode 100644 programs/Makefile create mode 100644 programs/bench.c create mode 100644 programs/bench.h create mode 100644 programs/datagen.c create mode 100644 programs/frametest.c create mode 100644 programs/fullbench.c create mode 100644 programs/fuzzer.c create mode 100644 programs/lz4.1 create mode 100644 programs/lz4c.1 create mode 100644 programs/lz4cat.1 create mode 100644 programs/lz4cli.c create mode 100644 programs/lz4io.c create mode 100644 programs/lz4io.h diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000..17db2ecd2c --- /dev/null +++ b/.gitattributes @@ -0,0 +1,10 @@ +# Set the default behavior +* text eol=lf + +# Explicitly declare source files +*.c text eol=lf +*.h text eol=lf + +# Denote files that should not be modified. +*.odt binary +*.png binary diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000000..9eff075257 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,26 @@ +language: c +compiler: gcc +script: make test-travis +before_install: + - sudo apt-get update -qq + - sudo apt-get install -qq gcc-multilib + - sudo apt-get install -qq valgrind + +env: + - LZ4_TRAVIS_CI_ENV=travis-install + - LZ4_TRAVIS_CI_ENV=streaming-examples + - LZ4_TRAVIS_CI_ENV=cmake + - LZ4_TRAVIS_CI_ENV=dist + - LZ4_TRAVIS_CI_ENV=test-lz4 + - LZ4_TRAVIS_CI_ENV=test-lz4c + - LZ4_TRAVIS_CI_ENV=test-lz4c32 + - LZ4_TRAVIS_CI_ENV=test-fullbench + - LZ4_TRAVIS_CI_ENV=test-fullbench32 + - LZ4_TRAVIS_CI_ENV=test-fuzzer + - LZ4_TRAVIS_CI_ENV=test-fuzzer32 + - LZ4_TRAVIS_CI_ENV=test-frametest + - LZ4_TRAVIS_CI_ENV=test-frametest32 + - LZ4_TRAVIS_CI_ENV=test-mem + +matrix: + fast_finish: true diff --git a/LZ4_Frame_Format.html b/LZ4_Frame_Format.html new file mode 100644 index 0000000000..bd1304c576 --- /dev/null +++ b/LZ4_Frame_Format.html @@ -0,0 +1 @@ +LZ4 Framing format - stable

LZ4 Framing Format


Notices

Copyright (c) 2013-2014 Yann Collet

Permission is granted to copy and distribute this document for any  purpose and without charge, including translations into other  languages and incorporation into compilations, provided that the copyright notice and this notice are preserved, and that any substantive changes or deletions from the original are clearly marked.

Version

1.4.1

Introduction

The purpose of this document is to define a lossless compressed data format, that is independent of CPU type, operating system, file system and character set, suitable for File compression, Pipe and streaming compression using the LZ4 algorithm : http://code.google.com/p/lz4/

The data can be produced or consumed, even for an arbitrarily long sequentially presented input data stream, using only an a priori bounded amount of intermediate storage, and hence can be used in data communications.  The format uses the LZ4 compression method, and xxHash-32 checksum method, for detection of data corruption.

The data format defined by this specification does not attempt to allow random access to compressed data.

This specification is intended for use by implementers of software to compress data into LZ4 format and/or decompress data from LZ4 format. The text of the specification assumes a basic background in programming at the level of bits and other primitive data representations.

Unless otherwise indicated below, a compliant compressor must produce data sets that conform to all the specifications presented here.

A compliant decompressor must be able to accept and decompress at least one data set that conforms to the specifications presented here; whenever it does not support any parameter, it must produce a non-ambiguous error code and associated error message explaining which parameter value is unsupported (a typical example being an unsupported buffer size).

Distribution of this document is unlimited.


Summary :

Introduction

General structure of LZ4 Framing Format

Frame Descriptor

Data Blocks

Skippable Frames

Legacy format

Appendix


General Structure of LZ4 Framing format

LZ4 Framing Format - General Structure.png

Magic Number

4 Bytes, Little endian format.
Value :
0x184D2204

Frame Descriptor

3 to 15 Bytes, to be detailed in the next part.
Most
significant part of the spec.

Data Blocks

To be detailed later on.
That’s where compressed data is stored.

EndMark

The flow of blocks ends when the last data block has a size of “0”.
The size is expressed as a 32-bits value.

Content Checksum

Content Checksum verify that the full content has been decoded correctly.
The content checksum is the result of
xxh32() hash function digesting the original (decoded) data as input, and a seed of zero.
Content checksum is only present when its
associated flag is set in the framing descriptor. Content Checksum validates the result, that all blocks were fully transmitted in the correct order and without error, and also that the encoding/decoding process itself generated no distortion. Its usage is recommended.

Frame Concatenation

In some circumstances, it may be preferable to append multiple frames, for example in order to add new data to an existing compressed file without re-framing it.

In such case, each frame has its own set of descriptor flags. Each frame is considered independent. The only relation between frames is their sequential order.

The ability to decode multiple concatenated frames within a single stream or file is left outside of this specification. While a logical default behavior could be to decode the frames in their sequential order, this is not a requirement.


Frame Descriptor

LZ4 Framing Format - Frame Descriptor.png

LZ4 Framing Format - Descriptor Flags.png

The descriptor uses a minimum of 3 bytes, and up to 15 bytes depending on optional parameters.
In the picture, bit 7 is highest bit, while bit 0 is lowest.

Version Number :

2-bits field, must be set to “01”.
Any other value cannot be decoded by this
version of the specification.
Other version numbers will use different flag layouts.

Block Independence flag :

If this flag is set to “1”, blocks are independent, and can therefore be decoded independently, in parallel.
If this flag is set to “
0”, each block depends on previous ones for decoding (up to LZ4 window size, which is 64 KB). In this case, it’s necessary to decode all blocks in sequence.

Block dependency improves compression ratio, especially for small blocks. On the other hand, it makes jumps or multi-threaded decoding impossible.

Block checksum flag :

If this flag is set, each data block will be followed by a 4-bytes checksum, calculated by using the xxHash-32 algorithm on the raw (compressed) data block.
The intention is to detect data corruption (storage or transmission errors) immediately, before decoding.
Block ch
ecksum usage is optional.

Content Size flag :

If this flag is set, the original (uncompressed) size of data included within the frame will be present as an 8 bytes unsigned value, little endian format, after the flags.

Recommended value : “0” (not present)

Content checksum flag :

If this flag is set, a content checksum will be appended after the EoS mark.

Recommended value : “1” (content checksum is present)

Preset Dictionary flag :

If this flag is set, a Dict-ID field will be present, just after the descriptor flags and the Content size.

Usual value : “0” (not present)

Block Maximum Size :

This information is intended to help the decoder allocate the right amount of memory.
Size here refers to the original (uncompressed) data size.
Block Maximum Size
is one value among the following table :

The decoder may refuse to allocate block sizes above a (system-specific) size.
Unused values may be used in a future revision of the spec.
A decoder conformant to the current version of the spec is only able to decode blocksizes defined in this spec.

Reserved bits :

Value of reserved bits must be 0 (zero).
Reserved bit might be used in a future version of the specification, to enable any (yet-to-decide) optional feature.
If this happens, a decoder respecting the current version of the specification shall not be able to decode such a frame.

Content Size

This is the original (uncompressed) size.
This information is optional, and only present if the
associated flag is set.
Content size is provided using unsigned 8 Bytes, for a maximum of 16 HexaBytes.
Format is Little endian.
This field has no impact on decoding, it just informs the decoder how much data the frame holds (for example, to display it during decoding process, or for verification purpose). It can be safely skipped by a conformant decoder.

Dictionary ID

Dict-ID is only present if the associated flag is set.
A dictionary is specially useful to compress short input sequences. The compressor can take advantage of the dictionary context to encode the input in a more compact manner. It works as a kind of “known prefix” which is used by both the compressor and the decompressor to “warm-up” reference tables and help compress small data blocks.

Dict-ID is the xxHash-32 checksum of this “known prefix”. Format is Little endian.

The decompressor uses this identifier to determine which dictionary has been used by the compressor. The compressor and the decompressor must use exactly the same dictionary. This document does not specify the contents of predefined dictionaries, since the optimal dictionaries are application specific. Any data format using this feature must precisely define the allowed dictionaries.

Within a single frame, a single dictionary can be defined.
When the frame descriptor defines independent blocks, each block will be initialised with the same dictionary.
If the frame descriptor defines linked blocks, the dictionary will only be used once, at the beginning of the decoding process.

Header Checksum :

One-byte checksum of all descriptor fields, including optional ones when present.
The byte is second byte of
xxh32() : { (xxh32()>>8) & 0xFF } ,
using zero as a seed,
and the full Frame Descriptor as an input (
including optional fields when they are present).
A different checksum indicates an error in the descriptor.


Data Blocks

Block Size

This field uses 4-bytes,  format is little-endian.

The highest bit is “1” if data in the block is uncompressed.

The highest bit is “0” if data in the block is compressed by LZ4.

All other bits give the size, in bytes, of the following data block (the size does not include the checksum if present).

Block Size shall never be larger than Block Maximum Size. Such a thing could happen when the original data is incompressible. In this case, such a data block shall be passed in uncompressed format.

Data

Where the actual data to decode stands. It might be compressed or not, depending on previous field indications.
Uncompressed size of Data can be any size, up to “block maximum size”.
Note that the data block is not necessarily filled : an arbitrary “flush” may happen anytime. Any block can be
“partially filled”.

Block checksum :

Only present if the associated flag is set.
This is a 4-bytes checksum value, in little endian format,
calculated by using the xxHash-32 algorithm
on the raw (undecoded) data block,
and a seed of zero.

The intention is to detect data corruption (storage or transmission errors)
before decoding.

Block checksum is cumulative with Content checksum.


Skippable Frames

LZ4 Framing Format - Skippable Frame.png

Skippable frames allow the integration of user-defined data into a flow of concatenated frames.
Its design is pretty straightforward, with the sole objective to allow the decoder to quickly skip over user-defined data and continue decoding.

For the purpose of facilitating identification, it is discouraged to start a flow of concatenated frames with a skippable frame. If there is a need to start such a flow with some user data encapsulated into a skippable frame, it’s recommended to start will a zero-byte LZ4 frame followed by a skippable frame. This will make it easier for file type identifiers.

 

Magic Number

4 Bytes, Little endian format.
Value :
0x184D2A5X, which means any value from 0x184D2A50 to 0x184D2A5F. All 16 values are valid to identify a skippable frame.

Frame Size 

This is the size, in bytes, of the following User Data (without including the magic number nor the size field itself).
4 Bytes,
Little endian format, unsigned 32-bits.
This means User Data can’t be bigger than (2^32-1) Bytes.

User Data

User Data can be anything. Data will just be skipped by the decoder.


Legacy frame

The Legacy frame format was defined into the initial versions of “LZ4Demo”.
Newer compressors should not use this format anymore, since it is too restrictive.
It is recommended that decompressors shall be able to decode this format during the transition period.

Main properties of legacy format :
- Fixed block size :
8 MB.
- All blocks must be completely filled, except the last one.
- All blocks are always compressed, even when compression is detri
mental.
- The last block is detected either because it is followed by the “EOF” (End of File) mark
, or because it is followed by a known Frame Magic Number.
- No checksum
- Convention is Little endian

Magic Number

4 Bytes, Little endian format.
Value :
0x184C2102

Block Compressed Size

This is the size, in bytes, of the following compressed data block.
4 Bytes,
Little endian format.

Data

Where the actual data stands.
Data is
always compressed, even when compression is detrimental (i.e. larger than original size).


Appendix  

Version changes

1.4.1 : changed wording from “stream” to “frame”

1.4 : added skippable streams, re-added stream checksum

1.3 : modified header checksum

1.2 : reduced choice of “block size”, to postpone decision on “dynamic size of BlockSize Field”.

1.1 : optional fields are now part of the descriptor

1.0 : changed “block size” specification, adding a compressed/uncompressed flag

0.9 : reduced scale of “block maximum size” table

0.8 : removed : high compression flag

0.7 : removed : stream checksum

0.6 : settled : stream size uses 8 bytes, endian convention is little endian

0.5: added copyright notice

0.4 : changed format to Google Doc compatible OpenDocument

\ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000..c486239544 --- /dev/null +++ b/Makefile @@ -0,0 +1,136 @@ +# ################################################################ +# LZ4 - Makefile +# Copyright (C) Yann Collet 2011-2015 +# All rights reserved. +# +# BSD license +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, this +# list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright notice, this +# list of conditions and the following disclaimer in the documentation and/or +# other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# You can contact the author at : +# - LZ4 source repository : http://code.google.com/p/lz4/ +# - LZ4 forum froup : https://groups.google.com/forum/#!forum/lz4c +# ################################################################ + +# Version number +export VERSION=126 +export RELEASE=r$(VERSION) + +DESTDIR?= +PREFIX ?= /usr + +LIBDIR ?= $(PREFIX)/lib +INCLUDEDIR=$(PREFIX)/include +PRGDIR = programs +LZ4DIR = lib +DISTRIBNAME=lz4-$(RELEASE).tar.gz + +TEXT = $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4.h $(LZ4DIR)/lz4hc.c $(LZ4DIR)/lz4hc.h \ + $(LZ4DIR)/lz4frame.c $(LZ4DIR)/lz4frame.h $(LZ4DIR)/lz4frame_static.h \ + $(LZ4DIR)/xxhash.c $(LZ4DIR)/xxhash.h \ + $(LZ4DIR)/liblz4.pc.in $(LZ4DIR)/Makefile $(LZ4DIR)/LICENSE \ + Makefile lz4_block_format.txt LZ4_Frame_Format.html NEWS README.md \ + cmake_unofficial/CMakeLists.txt \ + $(PRGDIR)/fullbench.c $(PRGDIR)/lz4cli.c \ + $(PRGDIR)/datagen.c $(PRGDIR)/fuzzer.c \ + $(PRGDIR)/lz4io.c $(PRGDIR)/lz4io.h \ + $(PRGDIR)/bench.c $(PRGDIR)/bench.h \ + $(PRGDIR)/lz4.1 $(PRGDIR)/lz4c.1 $(PRGDIR)/lz4cat.1 \ + $(PRGDIR)/Makefile $(PRGDIR)/COPYING +NONTEXT = images/image00.png images/image01.png images/image02.png \ + images/image03.png images/image04.png images/image05.png \ + images/image06.png +SOURCES = $(TEXT) $(NONTEXT) + + +# Select test target for Travis CI's Build Matrix +ifneq (,$(filter test-%,$(LZ4_TRAVIS_CI_ENV))) +TRAVIS_TARGET=prg-travis +else +TRAVIS_TARGET=$(LZ4_TRAVIS_CI_ENV) +endif + + +default: lz4programs + +all: + @cd $(LZ4DIR); $(MAKE) -e all + @cd $(PRGDIR); $(MAKE) -e all + +lz4programs: + @cd $(PRGDIR); $(MAKE) -e + +clean: + @rm -f $(DISTRIBNAME) *.sha1 + @cd $(PRGDIR); $(MAKE) clean + @cd $(LZ4DIR); $(MAKE) clean + @cd examples; $(MAKE) clean + @echo Cleaning completed + + +#------------------------------------------------------------------------ +#make install is validated only for Linux, OSX, kFreeBSD and Hurd targets +ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU)) + +install: + @cd $(LZ4DIR); $(MAKE) -e install + @cd $(PRGDIR); $(MAKE) -e install + +uninstall: + @cd $(LZ4DIR); $(MAKE) uninstall + @cd $(PRGDIR); $(MAKE) uninstall + +travis-install: + sudo $(MAKE) install + +dist: clean + @install -dD -m 700 lz4-$(RELEASE)/lib/ + @install -dD -m 700 lz4-$(RELEASE)/programs/ + @install -dD -m 700 lz4-$(RELEASE)/cmake_unofficial/ + @install -dD -m 700 lz4-$(RELEASE)/images/ + @for f in $(TEXT); do \ + tr -d '\r' < $$f > .tmp; \ + install -m 600 .tmp lz4-$(RELEASE)/$$f; \ + done + @rm .tmp + @for f in $(NONTEXT); do \ + install -m 600 $$f lz4-$(RELEASE)/$$f; \ + done + @tar -czf $(DISTRIBNAME) lz4-$(RELEASE)/ + @rm -rf lz4-$(RELEASE) + @sha1sum $(DISTRIBNAME) > $(DISTRIBNAME).sha1 + @echo Distribution $(DISTRIBNAME) built + +test: + @cd $(PRGDIR); $(MAKE) -e test + +test-travis: $(TRAVIS_TARGET) + +cmake: + @cd cmake_unofficial; cmake CMakeLists.txt; $(MAKE) + +streaming-examples: + cd examples; $(MAKE) -e test + +prg-travis: + @cd $(PRGDIR); $(MAKE) -e test-travis + +endif diff --git a/NEWS b/NEWS new file mode 100644 index 0000000000..8aeab1beaf --- /dev/null +++ b/NEWS @@ -0,0 +1,133 @@ +r126: +New : lz4frame API is now integrated into liblz4 +Fixed : GCC 4.9 bug on highest performance settings, reported by Greg Slazinski +Fixed : bug within LZ4 HC streaming mode, reported by James Boyle +Fixed : older compiler don't like nameless unions, reported by Cheyi Lin +Changed : lz4 is C90 compatible +Changed : added -pedantic option, fixed a few mminor warnings + +r125: +Changed : endian and alignment code +Changed : directory structure : new "lib" directory +Updated : lz4io, now uses lz4frame +Improved: slightly improved decoding speed +Fixed : LZ4_compress_limitedOutput(); Special thanks to Christopher Speller ! +Fixed : some alignment warnings under clang +Fixed : deprecated function LZ4_slideInputBufferHC() + +r124: +New : LZ4 HC streaming mode +Fixed : LZ4F_compressBound() using null preferencesPtr +Updated : xxHash to r38 +Updated library number, to 1.4.0 + +r123: +Added : experimental lz4frame API, thanks to Takayuki Matsuoka and Christopher Jackson for testings +Fix : s390x support, thanks to Nobuhiro Iwamatsu +Fix : test mode (-t) no longer requires confirmation, thanks to Thary Nguyen + +r122: +Fix : AIX & AIX64 support (SamG) +Fix : mips 64-bits support (lew van) +Added : Examples directory, using code examples from Takayuki Matsuoka +Updated : Framing specification, to v1.4.1 +Updated : xxHash, to r36 + +r121: +Added : Makefile : install for kFreeBSD and Hurd (Nobuhiro Iwamatsu) +Fix : Makefile : install for OS-X and BSD, thanks to Takayuki Matsuoka + +r120: +Modified : Streaming API, using strong types +Added : LZ4_versionNumber(), thanks to Takayuki Matsuoka +Fix : OS-X : library install name, thanks to Clemens Lang +Updated : Makefile : synchronize library version number with lz4.h, thanks to Takayuki Matsuoka +Updated : Makefile : stricter compilation flags +Added : pkg-config, thanks to Zbigniew Jędrzejewski-Szmek (issue 135) +Makefile : lz4-test only test native binaries, as suggested by Michał Górny (issue 136) +Updated : xxHash to r35 + +r119: +Fix : Issue 134 : extended malicious address space overflow in 32-bits mode for some specific configurations + +r118: +New : LZ4 Streaming API (Fast version), special thanks to Takayuki Matsuoka +New : datagen : parametrable synthetic data generator for tests +Improved : fuzzer, support more test cases, more parameters, ability to jump to specific test +fix : support ppc64le platform (issue 131) +fix : Issue 52 (malicious address space overflow in 32-bits mode when using large custom format) +fix : Makefile : minor issue 130 : header files permissions + +r117: +Added : man pages for lz4c and lz4cat +Added : automated tests on Travis, thanks to Takayuki Matsuoka ! +fix : block-dependency command line (issue 127) +fix : lz4fullbench (issue 128) + +r116: +hotfix (issue 124 & 125) + +r115: +Added : lz4cat utility, installed on POSX systems (issue 118) +OS-X compatible compilation of dynamic library (issue 115) + +r114: +Makefile : library correctly compiled with -O3 switch (issue 114) +Makefile : library compilation compatible with clang +Makefile : library is versioned and linked (issue 119) +lz4.h : no more static inline prototypes (issue 116) +man : improved header/footer (issue 111) +Makefile : Use system default $(CC) & $(MAKE) variables (issue 112) +xxhash : updated to r34 + +r113: +Large decompression speed improvement for GCC 32-bits. Thanks to Valery Croizier ! +LZ4HC : Compression Level is now a programmable parameter (CLI from 4 to 9) +Separated IO routines from command line (lz4io.c) +Version number into lz4.h (suggested by Francesc Alted) + +r112: +quickfix + +r111 : +Makefile : added capability to install libraries +Modified Directory tree, to better separate libraries from programs. + +r110 : +lz4 & lz4hc : added capability to allocate state & stream state with custom allocator (issue 99) +fuzzer & fullbench : updated to test new functions +man : documented -l command (Legacy format, for Linux kernel compression) (issue 102) +cmake : improved version by Mika Attila, building programs and libraries (issue 100) +xxHash : updated to r33 +Makefile : clean also delete local package .tar.gz + +r109 : +lz4.c : corrected issue 98 (LZ4_compress_limitedOutput()) +Makefile : can specify version number from makefile + +r108 : +lz4.c : corrected compression efficiency issue 97 in 64-bits chained mode (-BD) for streams > 4 GB (thanks Roman Strashkin for reporting) + +r107 : +Makefile : support DESTDIR for staged installs. Thanks Jorge Aparicio. +Makefile : make install installs both lz4 and lz4c (Jorge Aparicio) +Makefile : removed -Wno-implicit-declaration compilation switch +lz4cli.c : include for isatty() (Luca Barbato) +lz4.h : introduced LZ4_MAX_INPUT_SIZE constant (Shay Green) +lz4.h : LZ4_compressBound() : unified macro and inline definitions (Shay Green) +lz4.h : LZ4_decompressSafe_partial() : clarify comments (Shay Green) +lz4.c : LZ4_compress() verify input size condition (Shay Green) +bench.c : corrected a bug in free memory size evaluation +cmake : install into bin/ directory (Richard Yao) +cmake : check for just C compiler (Elan Ruusamae) + +r106 : +Makefile : make dist modify text files in the package to respect Unix EoL convention +lz4cli.c : corrected small display bug in HC mode + +r105 : +Makefile : New install script and man page, contributed by Prasad Pandit +lz4cli.c : Minor modifications, for easier extensibility +COPYING : added license file +LZ4_Streaming_Format.odt : modified file name to remove white space characters +Makefile : .exe suffix now properly added only for Windows target diff --git a/README.md b/README.md new file mode 100644 index 0000000000..7ab64cc37c --- /dev/null +++ b/README.md @@ -0,0 +1,56 @@ +LZ4 - Extremely fast compression +================================ + +LZ4 is lossless compression algorithm, providing compression speed at 400 MB/s per core, scalable with multi-cores CPU. It also features an extremely fast decoder, with speed in multiple GB/s per core, typically reaching RAM speed limits on multi-core systems. +A high compression derivative, called LZ4_HC, is also provided. It trades CPU time for compression ratio. + +|Branch |Status | +|------------|---------| +|master | [![Build Status](https://travis-ci.org/Cyan4973/lz4.svg?branch=master)](https://travis-ci.org/Cyan4973/lz4) | +|dev | [![Build Status](https://travis-ci.org/Cyan4973/lz4.svg?branch=dev)](https://travis-ci.org/Cyan4973/lz4) | + +This is an official mirror of LZ4 project, [hosted on Google Code](http://code.google.com/p/lz4/). +The intention is to offer github's capabilities to lz4 users, such as cloning, branch, pull requests or source download. + +The "master" branch will reflect, the status of lz4 at its official homepage. +The "dev" branch is the one where all contributions will be merged. If you plan to propose a patch, please commit into the "dev" branch. Direct commit to "master" are not permitted. +Feature branches will also exist, typically to introduce new requirements, and be temporarily available for testing before merge into "dev" branch. + + +Benchmarks +------------------------- + +The benchmark uses the [Open-Source Benchmark program by m^2 (v0.14.2)](http://encode.ru/threads/1371-Filesystem-benchmark?p=33548&viewfull=1#post33548) compiled with GCC v4.6.1 on Linux Ubuntu 64-bits v11.10, +The reference system uses a Core i5-3340M @2.7GHz. +Benchmark evaluates the compression of reference [Silesia Corpus](http://sun.aei.polsl.pl/~sdeor/index.php?page=silesia) in single-thread mode. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CompressorRatioCompressionDecompression
LZ4 (r101)2.084422 MB/s1820 MB/s
LZO 2.062.106414 MB/s600 MB/s
QuickLZ 1.5.1b62.237373 MB/s420 MB/s
Snappy 1.1.02.091323 MB/s1070 MB/s
LZF2.077270 MB/s570 MB/s
zlib 1.2.8 -12.73065 MB/s280 MB/s
LZ4 HC (r101)2.72025 MB/s2080 MB/s
zlib 1.2.8 -63.09921 MB/s300 MB/s
+ diff --git a/cmake_unofficial/CMakeLists.txt b/cmake_unofficial/CMakeLists.txt new file mode 100644 index 0000000000..bb7ab5f247 --- /dev/null +++ b/cmake_unofficial/CMakeLists.txt @@ -0,0 +1,82 @@ +PROJECT(LZ4 C) +set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "LZ4 compression library") +set(CPACK_PACKAGE_VERSION_MAJOR 1) +set(CPACK_PACKAGE_VERSION_MINOR 5) +set(CPACK_PACKAGE_VERSION_PATCH r126) +set(VERSION_STRING " \"${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}\" ") +include(CPack) + +cmake_minimum_required (VERSION 2.6) +INCLUDE (CheckTypeSize) +check_type_size("void *" SIZEOF_VOID_P) +IF( ${SIZEOF_VOID_P} STREQUAL "8" ) + set (CMAKE_SYSTEM_PROCESSOR "64bit") + MESSAGE( STATUS "64 bit architecture detected size of void * is " ${SIZEOF_VOID_P}) +ENDIF() + +option(BUILD_TOOLS "Build the command line tools" ON) +option(BUILD_LIBS "Build the libraries in addition to the tools" OFF) + +if(UNIX AND BUILD_LIBS) + if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64") + add_definitions(-fPIC) + endif(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64") +endif() + +set(LZ4_DIR ../lib/) +set(PRG_DIR ../programs/) +set(LZ4_SRCS_LIB ${LZ4_DIR}lz4.c ${LZ4_DIR}lz4hc.c ${LZ4_DIR}lz4.h ${LZ4_DIR}lz4hc.h) +set(LZ4_SRCS ${LZ4_DIR}lz4frame.c ${LZ4_DIR}xxhash.c ${PRG_DIR}bench.c ${PRG_DIR}lz4cli.c ${PRG_DIR}lz4io.c) + +if(BUILD_TOOLS AND NOT BUILD_LIBS) + set(LZ4_SRCS ${LZ4_SRCS} ${LZ4_SRCS_LIB}) +endif() + +if(BUILD_TOOLS) + add_executable(lz4 ${LZ4_SRCS}) + set_target_properties(lz4 PROPERTIES COMPILE_DEFINITIONS DISABLE_LZ4C_LEGACY_OPTIONS) + install(TARGETS lz4 RUNTIME DESTINATION "bin/") + add_executable(lz4c ${LZ4_SRCS}) + install(TARGETS lz4c RUNTIME DESTINATION "bin/") +endif() + +if(BUILD_LIBS) + add_library(liblz4 ${LZ4_SRCS_LIB}) + + set_target_properties(liblz4 PROPERTIES + OUTPUT_NAME lz4 + SOVERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}" + ) + + install(TARGETS liblz4 + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib + ) + + install(FILES + ${LZ4_DIR}/lz4.h + ${LZ4_DIR}/lz4hc.h + DESTINATION include + ) + + if(BUILD_TOOLS) + target_link_libraries(lz4 liblz4) + target_link_libraries(lz4c liblz4) + endif() +endif() + + +#warnings + +ADD_DEFINITIONS("-Wall") +ADD_DEFINITIONS("-Wextra") +ADD_DEFINITIONS("-Wundef") +ADD_DEFINITIONS("-Wshadow") +ADD_DEFINITIONS("-Wcast-align") +ADD_DEFINITIONS("-Wstrict-prototypes") +ADD_DEFINITIONS("-std=c99") +ADD_DEFINITIONS("-DLZ4_VERSION=\"${CPACK_PACKAGE_VERSION_PATCH}\"") +INCLUDE_DIRECTORIES (${LZ4_DIR}) + + + diff --git a/examples/HCStreaming_ringBuffer.c b/examples/HCStreaming_ringBuffer.c new file mode 100755 index 0000000000..cfae9d743f --- /dev/null +++ b/examples/HCStreaming_ringBuffer.c @@ -0,0 +1,239 @@ +// LZ4 HC streaming API example : ring buffer +// Based on previous work from Takayuki Matsuoka + + +/************************************** + * Compiler Options + **************************************/ +#ifdef _MSC_VER /* Visual Studio */ +# define _CRT_SECURE_NO_WARNINGS // for MSVC +# define snprintf sprintf_s +#endif + +#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) +#ifdef __GNUC__ +# pragma GCC diagnostic ignored "-Wmissing-braces" /* GCC bug 53119 : doesn't accept { 0 } as initializer (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119) */ +#endif + + +/************************************** + * Includes + **************************************/ +#include "lz4hc.h" +#include "lz4.h" + +#include +#include +#include +#include + +enum { + MESSAGE_MAX_BYTES = 1024, + RING_BUFFER_BYTES = 1024 * 8 + MESSAGE_MAX_BYTES, + DEC_BUFFER_BYTES = RING_BUFFER_BYTES + MESSAGE_MAX_BYTES // Intentionally larger to test unsynchronized ring buffers +}; + + +size_t write_int32(FILE* fp, int32_t i) { + return fwrite(&i, sizeof(i), 1, fp); +} + +size_t write_bin(FILE* fp, const void* array, int arrayBytes) { + return fwrite(array, 1, arrayBytes, fp); +} + +size_t read_int32(FILE* fp, int32_t* i) { + return fread(i, sizeof(*i), 1, fp); +} + +size_t read_bin(FILE* fp, void* array, int arrayBytes) { + return fread(array, 1, arrayBytes, fp); +} + + +void test_compress(FILE* outFp, FILE* inpFp) +{ + LZ4_streamHC_t lz4Stream_body = { 0 }; + LZ4_streamHC_t* lz4Stream = &lz4Stream_body; + + static char inpBuf[RING_BUFFER_BYTES]; + int inpOffset = 0; + + for(;;) + { + // Read random length ([1,MESSAGE_MAX_BYTES]) data to the ring buffer. + char* const inpPtr = &inpBuf[inpOffset]; + const int randomLength = (rand() % MESSAGE_MAX_BYTES) + 1; + const int inpBytes = (int) read_bin(inpFp, inpPtr, randomLength); + if (0 == inpBytes) break; + + { + char cmpBuf[LZ4_COMPRESSBOUND(MESSAGE_MAX_BYTES)]; + const int cmpBytes = LZ4_compressHC_continue(lz4Stream, inpPtr, cmpBuf, inpBytes); + + if(cmpBytes <= 0) break; + write_int32(outFp, cmpBytes); + write_bin(outFp, cmpBuf, cmpBytes); + + inpOffset += inpBytes; + + // Wraparound the ringbuffer offset + if(inpOffset >= RING_BUFFER_BYTES - MESSAGE_MAX_BYTES) + inpOffset = 0; + } + } + + write_int32(outFp, 0); +} + + +void test_decompress(FILE* outFp, FILE* inpFp) +{ + static char decBuf[DEC_BUFFER_BYTES]; + int decOffset = 0; + LZ4_streamDecode_t lz4StreamDecode_body = { 0 }; + LZ4_streamDecode_t* lz4StreamDecode = &lz4StreamDecode_body; + + for(;;) + { + int cmpBytes = 0; + char cmpBuf[LZ4_COMPRESSBOUND(MESSAGE_MAX_BYTES)]; + + { + const size_t r0 = read_int32(inpFp, &cmpBytes); + size_t r1; + if(r0 != 1 || cmpBytes <= 0) + break; + + r1 = read_bin(inpFp, cmpBuf, cmpBytes); + if(r1 != (size_t) cmpBytes) + break; + } + + { + char* const decPtr = &decBuf[decOffset]; + const int decBytes = LZ4_decompress_safe_continue( + lz4StreamDecode, cmpBuf, decPtr, cmpBytes, MESSAGE_MAX_BYTES); + if(decBytes <= 0) + break; + + decOffset += decBytes; + write_bin(outFp, decPtr, decBytes); + + // Wraparound the ringbuffer offset + if(decOffset >= DEC_BUFFER_BYTES - MESSAGE_MAX_BYTES) + decOffset = 0; + } + } +} + + +// Compare 2 files content +// return 0 if identical +// return ByteNb>0 if different +size_t compare(FILE* f0, FILE* f1) +{ + size_t result = 1; + + for (;;) + { + char b0[65536]; + char b1[65536]; + const size_t r0 = fread(b0, 1, sizeof(b0), f0); + const size_t r1 = fread(b1, 1, sizeof(b1), f1); + + if ((r0==0) && (r1==0)) return 0; // success + + if (r0 != r1) + { + size_t smallest = r0; + if (r1 +#include +#include +#include + +enum { + BLOCK_BYTES = 1024 * 8, +// BLOCK_BYTES = 1024 * 64, +}; + + +size_t write_int(FILE* fp, int i) { + return fwrite(&i, sizeof(i), 1, fp); +} + +size_t write_bin(FILE* fp, const void* array, size_t arrayBytes) { + return fwrite(array, 1, arrayBytes, fp); +} + +size_t read_int(FILE* fp, int* i) { + return fread(i, sizeof(*i), 1, fp); +} + +size_t read_bin(FILE* fp, void* array, size_t arrayBytes) { + return fread(array, 1, arrayBytes, fp); +} + + +void test_compress(FILE* outFp, FILE* inpFp) +{ + LZ4_stream_t lz4Stream_body = { 0 }; + LZ4_stream_t* lz4Stream = &lz4Stream_body; + + char inpBuf[2][BLOCK_BYTES]; + int inpBufIndex = 0; + + for(;;) { + char* const inpPtr = inpBuf[inpBufIndex]; + const int inpBytes = (int) read_bin(inpFp, inpPtr, BLOCK_BYTES); + if(0 == inpBytes) { + break; + } + + { + char cmpBuf[LZ4_COMPRESSBOUND(BLOCK_BYTES)]; + const int cmpBytes = LZ4_compress_continue( + lz4Stream, inpPtr, cmpBuf, inpBytes); + if(cmpBytes <= 0) { + break; + } + write_int(outFp, cmpBytes); + write_bin(outFp, cmpBuf, (size_t) cmpBytes); + } + + inpBufIndex = (inpBufIndex + 1) % 2; + } + + write_int(outFp, 0); +} + + +void test_decompress(FILE* outFp, FILE* inpFp) +{ + LZ4_streamDecode_t lz4StreamDecode_body = { 0 }; + LZ4_streamDecode_t* lz4StreamDecode = &lz4StreamDecode_body; + + char decBuf[2][BLOCK_BYTES]; + int decBufIndex = 0; + + for(;;) { + char cmpBuf[LZ4_COMPRESSBOUND(BLOCK_BYTES)]; + int cmpBytes = 0; + + { + const size_t readCount0 = read_int(inpFp, &cmpBytes); + if(readCount0 != 1 || cmpBytes <= 0) { + break; + } + + const size_t readCount1 = read_bin(inpFp, cmpBuf, (size_t) cmpBytes); + if(readCount1 != (size_t) cmpBytes) { + break; + } + } + + { + char* const decPtr = decBuf[decBufIndex]; + const int decBytes = LZ4_decompress_safe_continue( + lz4StreamDecode, cmpBuf, decPtr, cmpBytes, BLOCK_BYTES); + if(decBytes <= 0) { + break; + } + write_bin(outFp, decPtr, (size_t) decBytes); + } + + decBufIndex = (decBufIndex + 1) % 2; + } +} + + +int compare(FILE* fp0, FILE* fp1) +{ + int result = 0; + + while(0 == result) { + char b0[65536]; + char b1[65536]; + const size_t r0 = read_bin(fp0, b0, sizeof(b0)); + const size_t r1 = read_bin(fp1, b1, sizeof(b1)); + + result = (int) r0 - (int) r1; + + if(0 == r0 || 0 == r1) { + break; + } + if(0 == result) { + result = memcmp(b0, b1, r0); + } + } + + return result; +} + + +int main(int argc, char* argv[]) +{ + char inpFilename[256] = { 0 }; + char lz4Filename[256] = { 0 }; + char decFilename[256] = { 0 }; + + if(argc < 2) { + printf("Please specify input filename\n"); + return 0; + } + + snprintf(inpFilename, 256, "%s", argv[1]); + snprintf(lz4Filename, 256, "%s.lz4s-%d", argv[1], BLOCK_BYTES); + snprintf(decFilename, 256, "%s.lz4s-%d.dec", argv[1], BLOCK_BYTES); + + printf("inp = [%s]\n", inpFilename); + printf("lz4 = [%s]\n", lz4Filename); + printf("dec = [%s]\n", decFilename); + + // compress + { + FILE* inpFp = fopen(inpFilename, "rb"); + FILE* outFp = fopen(lz4Filename, "wb"); + + printf("compress : %s -> %s\n", inpFilename, lz4Filename); + test_compress(outFp, inpFp); + printf("compress : done\n"); + + fclose(outFp); + fclose(inpFp); + } + + // decompress + { + FILE* inpFp = fopen(lz4Filename, "rb"); + FILE* outFp = fopen(decFilename, "wb"); + + printf("decompress : %s -> %s\n", lz4Filename, decFilename); + test_decompress(outFp, inpFp); + printf("decompress : done\n"); + + fclose(outFp); + fclose(inpFp); + } + + // verify + { + FILE* inpFp = fopen(inpFilename, "rb"); + FILE* decFp = fopen(decFilename, "rb"); + + printf("verify : %s <-> %s\n", inpFilename, decFilename); + const int cmp = compare(inpFp, decFp); + if(0 == cmp) { + printf("verify : OK\n"); + } else { + printf("verify : NG\n"); + } + + fclose(decFp); + fclose(inpFp); + } + + return 0; +} diff --git a/examples/blockStreaming_lineByLine.c b/examples/blockStreaming_lineByLine.c new file mode 100644 index 0000000000..6d14801b23 --- /dev/null +++ b/examples/blockStreaming_lineByLine.c @@ -0,0 +1,207 @@ +// LZ4 streaming API example : line-by-line logfile compression +// Copyright : Takayuki Matsuoka + + +#define _CRT_SECURE_NO_WARNINGS // for MSVC +#include "lz4.h" + +#include +#include +#include +#include + +static size_t write_uint16(FILE* fp, uint16_t i) +{ + return fwrite(&i, sizeof(i), 1, fp); +} + +static size_t write_bin(FILE* fp, const void* array, int arrayBytes) +{ + return fwrite(array, 1, arrayBytes, fp); +} + +static size_t read_uint16(FILE* fp, uint16_t* i) +{ + return fread(i, sizeof(*i), 1, fp); +} + +static size_t read_bin(FILE* fp, void* array, int arrayBytes) +{ + return fread(array, 1, arrayBytes, fp); +} + + +static void test_compress( + FILE* outFp, + FILE* inpFp, + size_t messageMaxBytes, + size_t ringBufferBytes) +{ + LZ4_stream_t* const lz4Stream = LZ4_createStream(); + char* const cmpBuf = malloc(LZ4_COMPRESSBOUND(messageMaxBytes)); + char* const inpBuf = malloc(ringBufferBytes); + int inpOffset = 0; + + for ( ; ; ) + { + char* const inpPtr = &inpBuf[inpOffset]; + +#if 0 + // Read random length data to the ring buffer. + const int randomLength = (rand() % messageMaxBytes) + 1; + const int inpBytes = (int) read_bin(inpFp, inpPtr, randomLength); + if (0 == inpBytes) break; +#else + // Read line to the ring buffer. + int inpBytes = 0; + if (!fgets(inpPtr, (int) messageMaxBytes, inpFp)) + break; + inpBytes = (int) strlen(inpPtr); +#endif + + { + const int cmpBytes = LZ4_compress_continue( + lz4Stream, inpPtr, cmpBuf, inpBytes); + if (cmpBytes <= 0) break; + write_uint16(outFp, (uint16_t) cmpBytes); + write_bin(outFp, cmpBuf, cmpBytes); + + // Add and wraparound the ringbuffer offset + inpOffset += inpBytes; + if ((size_t)inpOffset >= ringBufferBytes - messageMaxBytes) inpOffset = 0; + } + } + write_uint16(outFp, 0); + + free(inpBuf); + free(cmpBuf); + LZ4_freeStream(lz4Stream); +} + + +static void test_decompress( + FILE* outFp, + FILE* inpFp, + size_t messageMaxBytes, + size_t ringBufferBytes) +{ + LZ4_streamDecode_t* const lz4StreamDecode = LZ4_createStreamDecode(); + char* const cmpBuf = malloc(LZ4_COMPRESSBOUND(messageMaxBytes)); + char* const decBuf = malloc(ringBufferBytes); + int decOffset = 0; + + for ( ; ; ) + { + uint16_t cmpBytes = 0; + + if (read_uint16(inpFp, &cmpBytes) != 1) break; + if (cmpBytes <= 0) break; + if (read_bin(inpFp, cmpBuf, cmpBytes) != cmpBytes) break; + + { + char* const decPtr = &decBuf[decOffset]; + const int decBytes = LZ4_decompress_safe_continue( + lz4StreamDecode, cmpBuf, decPtr, cmpBytes, (int) messageMaxBytes); + if (decBytes <= 0) break; + write_bin(outFp, decPtr, decBytes); + + // Add and wraparound the ringbuffer offset + decOffset += decBytes; + if ((size_t)decOffset >= ringBufferBytes - messageMaxBytes) decOffset = 0; + } + } + + free(decBuf); + free(cmpBuf); + LZ4_freeStreamDecode(lz4StreamDecode); +} + + +static int compare(FILE* f0, FILE* f1) +{ + int result = 0; + const size_t tempBufferBytes = 65536; + char* const b0 = malloc(tempBufferBytes); + char* const b1 = malloc(tempBufferBytes); + + while(0 == result) + { + const size_t r0 = fread(b0, 1, tempBufferBytes, f0); + const size_t r1 = fread(b1, 1, tempBufferBytes, f1); + + result = (int) r0 - (int) r1; + + if (0 == r0 || 0 == r1) break; + if (0 == result) result = memcmp(b0, b1, r0); + } + + free(b1); + free(b0); + return result; +} + + +int main(int argc, char* argv[]) +{ + enum { + MESSAGE_MAX_BYTES = 1024, + RING_BUFFER_BYTES = 1024 * 256 + MESSAGE_MAX_BYTES, + }; + + char inpFilename[256] = { 0 }; + char lz4Filename[256] = { 0 }; + char decFilename[256] = { 0 }; + + if (argc < 2) + { + printf("Please specify input filename\n"); + return 0; + } + + snprintf(inpFilename, 256, "%s", argv[1]); + snprintf(lz4Filename, 256, "%s.lz4s", argv[1]); + snprintf(decFilename, 256, "%s.lz4s.dec", argv[1]); + + printf("inp = [%s]\n", inpFilename); + printf("lz4 = [%s]\n", lz4Filename); + printf("dec = [%s]\n", decFilename); + + // compress + { + FILE* inpFp = fopen(inpFilename, "rb"); + FILE* outFp = fopen(lz4Filename, "wb"); + + test_compress(outFp, inpFp, MESSAGE_MAX_BYTES, RING_BUFFER_BYTES); + + fclose(outFp); + fclose(inpFp); + } + + // decompress + { + FILE* inpFp = fopen(lz4Filename, "rb"); + FILE* outFp = fopen(decFilename, "wb"); + + test_decompress(outFp, inpFp, MESSAGE_MAX_BYTES, RING_BUFFER_BYTES); + + fclose(outFp); + fclose(inpFp); + } + + // verify + { + FILE* inpFp = fopen(inpFilename, "rb"); + FILE* decFp = fopen(decFilename, "rb"); + + const int cmp = compare(inpFp, decFp); + if (0 == cmp) + printf("Verify : OK\n"); + else + printf("Verify : NG\n"); + + fclose(decFp); + fclose(inpFp); + } + + return 0; +} diff --git a/examples/blockStreaming_ringBuffer.c b/examples/blockStreaming_ringBuffer.c new file mode 100644 index 0000000000..beb5e661e0 --- /dev/null +++ b/examples/blockStreaming_ringBuffer.c @@ -0,0 +1,200 @@ +// LZ4 streaming API example : ring buffer +// Based on sample code from Takayuki Matsuoka + + +/************************************** + * Compiler Options + **************************************/ +#ifdef _MSC_VER /* Visual Studio */ +# define _CRT_SECURE_NO_WARNINGS // for MSVC +# define snprintf sprintf_s +#endif +#ifdef __GNUC__ +# pragma GCC diagnostic ignored "-Wmissing-braces" /* GCC bug 53119 : doesn't accept { 0 } as initializer (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119) */ +#endif + + +/************************************** + * Includes + **************************************/ +#include +#include +#include +#include +#include "lz4.h" + + +enum { + MESSAGE_MAX_BYTES = 1024, + RING_BUFFER_BYTES = 1024 * 8 + MESSAGE_MAX_BYTES, + DECODE_RING_BUFFER = RING_BUFFER_BYTES + MESSAGE_MAX_BYTES // Intentionally larger, to test unsynchronized ring buffers +}; + + +size_t write_int32(FILE* fp, int32_t i) { + return fwrite(&i, sizeof(i), 1, fp); +} + +size_t write_bin(FILE* fp, const void* array, int arrayBytes) { + return fwrite(array, 1, arrayBytes, fp); +} + +size_t read_int32(FILE* fp, int32_t* i) { + return fread(i, sizeof(*i), 1, fp); +} + +size_t read_bin(FILE* fp, void* array, int arrayBytes) { + return fread(array, 1, arrayBytes, fp); +} + + +void test_compress(FILE* outFp, FILE* inpFp) +{ + LZ4_stream_t lz4Stream_body = { 0 }; + LZ4_stream_t* lz4Stream = &lz4Stream_body; + + static char inpBuf[RING_BUFFER_BYTES]; + int inpOffset = 0; + + for(;;) { + // Read random length ([1,MESSAGE_MAX_BYTES]) data to the ring buffer. + char* const inpPtr = &inpBuf[inpOffset]; + const int randomLength = (rand() % MESSAGE_MAX_BYTES) + 1; + const int inpBytes = (int) read_bin(inpFp, inpPtr, randomLength); + if (0 == inpBytes) break; + + { + char cmpBuf[LZ4_COMPRESSBOUND(MESSAGE_MAX_BYTES)]; + const int cmpBytes = LZ4_compress_continue(lz4Stream, inpPtr, cmpBuf, inpBytes); + if(cmpBytes <= 0) break; + write_int32(outFp, cmpBytes); + write_bin(outFp, cmpBuf, cmpBytes); + + inpOffset += inpBytes; + + // Wraparound the ringbuffer offset + if(inpOffset >= RING_BUFFER_BYTES - MESSAGE_MAX_BYTES) inpOffset = 0; + } + } + + write_int32(outFp, 0); +} + + +void test_decompress(FILE* outFp, FILE* inpFp) +{ + static char decBuf[DECODE_RING_BUFFER]; + int decOffset = 0; + LZ4_streamDecode_t lz4StreamDecode_body = { 0 }; + LZ4_streamDecode_t* lz4StreamDecode = &lz4StreamDecode_body; + + for(;;) { + int cmpBytes = 0; + char cmpBuf[LZ4_COMPRESSBOUND(MESSAGE_MAX_BYTES)]; + + { + const size_t r0 = read_int32(inpFp, &cmpBytes); + if(r0 != 1 || cmpBytes <= 0) break; + + const size_t r1 = read_bin(inpFp, cmpBuf, cmpBytes); + if(r1 != (size_t) cmpBytes) break; + } + + { + char* const decPtr = &decBuf[decOffset]; + const int decBytes = LZ4_decompress_safe_continue( + lz4StreamDecode, cmpBuf, decPtr, cmpBytes, MESSAGE_MAX_BYTES); + if(decBytes <= 0) break; + decOffset += decBytes; + write_bin(outFp, decPtr, decBytes); + + // Wraparound the ringbuffer offset + if(decOffset >= DECODE_RING_BUFFER - MESSAGE_MAX_BYTES) decOffset = 0; + } + } +} + + +int compare(FILE* f0, FILE* f1) +{ + int result = 0; + + while(0 == result) { + char b0[65536]; + char b1[65536]; + const size_t r0 = fread(b0, 1, sizeof(b0), f0); + const size_t r1 = fread(b1, 1, sizeof(b1), f1); + + result = (int) r0 - (int) r1; + + if(0 == r0 || 0 == r1) { + break; + } + if(0 == result) { + result = memcmp(b0, b1, r0); + } + } + + return result; +} + + +int main(int argc, char** argv) +{ + char inpFilename[256] = { 0 }; + char lz4Filename[256] = { 0 }; + char decFilename[256] = { 0 }; + + if(argc < 2) { + printf("Please specify input filename\n"); + return 0; + } + + snprintf(inpFilename, 256, "%s", argv[1]); + snprintf(lz4Filename, 256, "%s.lz4s-%d", argv[1], 0); + snprintf(decFilename, 256, "%s.lz4s-%d.dec", argv[1], 0); + + printf("inp = [%s]\n", inpFilename); + printf("lz4 = [%s]\n", lz4Filename); + printf("dec = [%s]\n", decFilename); + + // compress + { + FILE* inpFp = fopen(inpFilename, "rb"); + FILE* outFp = fopen(lz4Filename, "wb"); + + test_compress(outFp, inpFp); + + fclose(outFp); + fclose(inpFp); + } + + // decompress + { + FILE* inpFp = fopen(lz4Filename, "rb"); + FILE* outFp = fopen(decFilename, "wb"); + + test_decompress(outFp, inpFp); + + fclose(outFp); + fclose(inpFp); + } + + // verify + { + FILE* inpFp = fopen(inpFilename, "rb"); + FILE* decFp = fopen(decFilename, "rb"); + + const int cmp = compare(inpFp, decFp); + if(0 == cmp) { + printf("Verify : OK\n"); + } else { + printf("Verify : NG\n"); + } + + fclose(decFp); + fclose(inpFp); + } + + return 0; +} diff --git a/examples/printVersion.c b/examples/printVersion.c new file mode 100644 index 0000000000..86071397c8 --- /dev/null +++ b/examples/printVersion.c @@ -0,0 +1,13 @@ +// LZ4 trivial example : print Library version number +// Copyright : Takayuki Matsuoka & Yann Collet + + +#include +#include "lz4.h" + +int main(int argc, char** argv) +{ + (void)argc; (void)argv; + printf("Hello World ! LZ4 Library version = %d\n", LZ4_versionNumber()); + return 0; +} diff --git a/images/image00.png b/images/image00.png new file mode 100755 index 0000000000000000000000000000000000000000..69b4ee623dacab0d736971b73dcf5a4ddd9fc66b GIT binary patch literal 8028 zcmZvBWmHsc)HaMrDIqD+(jZ;ZASvD5DJ?KVr=Y|T(j9_Bhjf>8cXu<4^w0?3_&(3$ z_x^a-y4SsCtvS~|*S^m_d+&4Ya8+elEX-G!NJvOn@8zV_k&pm_h~N6?sEECi=Dr#U ziIV2Ml!T@y@?qv0m}V}qFNR3$`25{-?n^llg=Q0aWcI=+hF*LIb^OT`sxk8nmmjPz z_P!;C8ndrPZ_$$DbK%4iaVZRqE|TNVPN7Uu*VLqvTUg6IoBcWrp7hkW^LZ5)Ko!to zH`b_Cf1q7dtf>z52za_uTzm&x9&4Qx5!};UnReOxuHYGjgdX_M>Hy$*%H1Y3p6mnM zv5Wx7xJduA-VUKGGGtGzWm+Na3;n&;96vC(dVUMM`Y*mCl^#iYPZ38<5C=eo^k;#U zkRnFSW7xgZcRS;^dp0R-wp@^H_qnJ*R zh*{Ji0W4_7i^{#1MM{wUDO4ya?znmGy)w;`=}nH%3lP zt5gfVwO3+?nzl1rn`g~+HDtc6#diaJSVp75Nuu-5g)h7t!&IAauqg}H%tr=P8*Rgl zhe$z}$5^fiPKuubg*kB9agl=bs8x@N!ndijjOsp0wH-*aWN-66RwdSHlp76rPY(M^ z^^`lkE@k5iK`&!5fXaiFo7D1$Sap97xMNk4-7Lc&ejm<)zxlbOPal#fO0~H|(l1=j zsyNORMC;!<%_ir=)Vvl~w1Q=8;xW@Ri~vW7ptpB z{qV5fE?B89F>(f4QlH9!El#VJE_p3>KdU~T$?*#?YH2FbacnPO)BY}esOA=fJr9pJ zX8BsY$kfHlbNOB?lXYBwajx`uy6TH(nS4267%Q(JdM{KVS+}vOd-zJmD zW$L!+h-NP5?mLX80O$AociXLTtHCCm`bNob<_{k75WGqz^*OoED~~KtNKG&@J|Uur2$@l^nwR1$DvJ4weKG@PMN4a068&#< zW$ALrHbEW=LY$1LRb}Fhwg>%1Lmn$`Y5BYgQ)a(p|XK|zL4?k~?oIG$c~3VmXx zqHpq7!sRWVP2Esk+er$xLswTW0np5crN-(ZZ~w^U@a^u&@BIB|k(CNcwklsr`imUG zp5*;*aWT*-XmV_ZMG*Aa@q8GnoPU|t1gdLFjOoM)g@ zo#Ap&Wu7_GKpt(U$8R2JjzVoOkh>i@S=pN1B$DV(WHI5lhhG~>!$v-x%bw1Blr8A4WVf4dSB%FMsjBg z?BlW}kB=`xx$Wzdr|GWreT)Y|yBb`sF!w-?wGmC-^VQRBCgF9Ap@FL(chfv|FKhd* zb2?KTT+{sQbW?cB z3&t~Fi{*{*Rzv%f9-qtQ_??l6@R*WVq9Nm^kOIyUO#OzuD#mGQ>*Ow?KUvgkEmt`S z9Xr0#K?I7psX5AeQ`$>A9uz=4J=IuXCB^G~=Y`|VvjPS&o2%s0mh|n5Z>WDV;>&0x zL!B-cNv3}jGg1%v5ZXr29CI3uCO6_;T8K=Q#IqFXXge zowJj)zlC(Z#fF>+|HR=G-0=Mh_%~ToaqOYw><`}{z4QIgSkOnrJN$Ej>M6<}ntzy% zVu(+qo+F1`|C8Vm`vw9OQZ0e`w=85J$^!S^6ZBI=!2ARIi2z~P1QYxYv>3NLk5=mzf_P=b~BI&G6g8b!~QEb zRJaI`@>|mH|JX$A!vctKaT5*1rTotWsz0EQd~(SDnN4v6;Dp~7Y1)l8BmYO)loNx@ zLz$@B0{=iO0@?jB=)w8j2&*x7K=$Z(K|o`xCwaPR%){vh3-IybH%4up=ZIm8=Zkap z&L8Fvbb)=+Ru7=&70bjNAT2VdRGBso-Y_?U+qtm!SMtBy z;yOGDGN-Qgw@TzY=pAZ*8lpP>Zq%rWNhK@p%+(y^#8!E9$>OeLX!eGL9x^ zz|J8o*-oR4Vn6TGJE?^w&sFyMhvv4Jc;gl-=D+jr|7eyJcyI7S$Ej7W%px04PP*7F zBcc1$^YJ-q6dr`jO23HI>p<#en%N69xZdQxOSZdDm?^YY;7nnX+ZUQ@np`nn4;2NC zo>jnN0+f4HDDxD$PJHYVv+~g3>zy2*)lgAVkaj8aJEt)Siqvf<>O7uiHED8Fi(99D zI1aooR<3a%FLs(M?3a(0H5)ioZGNZOrcM5%ZGYC96Vx3s%CvI*bi%oOFBAsLT!J6X z98Z?)g*iD^8rZ|43$o&`Fntg2YEEl(BSmwZ^Obbw$QM5vnu(U+vY3>Ehu>@!`J*a7 z$hU8X>y#K%C@00>KzGl_rs_xWSNx(@^eem$?Fx<^PHMBKaI$d06Uc;=d`3pu<2YnG zDPzpt;B44y?xE)lyVD9Qev&J!uRS6SeY-sRGE(y=kE;w|iFoan!s7?9T|*5KHE zlQDS=yV_qex#83j+vLHUo6Ay<2-EFkZc&6Q3=z6J!$ke6PblHF%JBx z4Sjqd{WlT}Nl1(zB`DU=k7zHYlXZ$1|xi-9qi zl}FB}V3~Ng`!(Pf>dppbjE_|FV68@hIjc&M{bgf9nd8zCsanLfY}4VYZM7CtdvO_g zDbkL?o4Ui>q6)?Ni!ppNOpAn`pqCD8T~CsnCEXdHdui z#g`Bx&(mi3q1X)cD*D+kij@G<MQ`WJp`DNFTQT-cIi$e|qfcqT~2b38+6#qR$*R18JWW6D+yibL&z9+EhOEH6!Py7Hsm-OB& zk%4Agx@yqdD|C@wY=8ojfUL=gNv_9Z-y~33bhn?$iDp^fQa{W%--FygSUacCSDSr3^$y--w&R)M zf1O+(X8f^CR&0E53SNozrW3l?H({kY&d^-b?Fc$043-qHlsH2r$c88&B8()eTIjL+ zoyK?lC<|#aNFJpdTlG-%t0lZ?_Nxz*N?@5Ra`W4sq&&6?39mUqqKjPYp(syrsiG)J76Ayf;TGD(l;Mf_VY}tlciYjm7k9gB9NYuY{KRsX+7(N zJNad8sYVurkN*p47qtWLIZ=#V&QZ$tv1DoqV`LlSVe}YswO?s}bZ?Nh`P#CO1E18lzncvzts_?6p;D8|1u(kkwE#mQaWmQiHO%RG5m=5 z9Ihps-Kc1^Qh* z#gvD?5`Q}|l`VVjhO(d_+vaLU=vSS&)mSSVw0A{M#EET7K=eQx{uEt&R<4^Ibj*6Y zV+8vNJ;Iu^bMyXr*-hu&ofY7*c47dGhMsFQ=eBu)g3nK)Z}=DJ#K%;aV@m*-WXL2d{E+=Y7NDojWM;@XWPEkq_$m8G^0QD?f} z-ZrFdH563|krNw=*Y~>HA|5TrS!y}`P#Mtn?)C3pVzKjKoq0g@Z+!1zAegCVmqN@< z)*y0pqQvKm&jD0WPn4i}lGu{$__`{l1 zGtMw7ZPU|JmL#78-UrZj_rCBAUaSB6qtdELCqvKlvEzwwM*h7vMFCNNSSlF)h4HFu33EJYvPDl@0@Icrh~`6;mQ8&e)|hGELIQW&XWljSOJ3XE=2MP@FT|Uhp6_|?d*&2CAZ@6#sMl;2&os^H}xyEK7foL>ePIDJ?k=mpD0-yB^zjAAV`=mI_q-K~8tkXcXQl>!8m9bK66tw;4#)VdV8fRfpSm`! z@rFw!W76Y+D}4}yPfPLAi0eGv8`1EQu9L7wDT(xniRb<>)fn3l80*Q;!V9?{m|=M# z(ckst?K3($^qbZ55>975v1d}_eedx)?$c=ZI{1BcDVF; z-#b?6k2={}O$0qUUt>@`v=4ul4Xs#@Oh#$9#7DaCU?&N(gm6B_oq{!9MQ*8Lj?vub zd(LS!nEwA}7%4~%7ty_AMif5zuPHU9$B5LKaB&aA^2W6CMAF`s%tU46yDU{+ zJKTHq7Ki7h&k*-wVB{4N&N4u3?d_**oc_Rqb$%)ilS|~A8~H8ZFv|T}a+Z>4sGdbA z<+03w@wx99*4eL@$FXDnt1*p9>3uZf{KnF<7%313zV*|oC_jHVPXXOwh;V-qOtgW? z7DU6ujuSdaHj(6S=sCAI&IZ~|%uE4xveOrSbyz0`(C=EEfH5!^tigc+cbGoS5#i-A zSc?@_Wk#z{_(iai)DB4ndux3G5u=!dn0j_MSz{~ndm}5DpBK^?Xkx$cZU|qMIWe15 z2^f8@s-5`NEriEuQ-^n=z{i95f*L^2;X!|{l;ftzs{aiFALosPtfLT+=R&d^ccft~ zlecLVDrX9VJhrJsxKlhj#p)p5j%B);)SCvjYD^RREdfFWLYFnywu8hvJ7K3KngZx# zte=_gkzJxT?=xsf>KZg*d#6e(I9)mZzJ!k1#i%HO&nQAbUF@}?N%ao=2mj6nypYEq z-K$hk;ZBr_x?p|p6K7*^_V1PT^z@W~Szpe*h&5@lL_RzJqhCD}r7u+N<-+H{S~i(R zW$uPdBVLoocfE!&aNl_Q7BB61osN4B`wCW*6Jy8=SmaXhcwRW8AgxdAI0gBK{Hhef zIj>~ub4WmVQ*&3G1@ZNzCie2iNP5K*#m53ejSSkp1*?eFi&ch#MaiBL z&*{rB_e5C9s88B96KF(qPt=Q5Qfc$|QK8c|h~Bwc=9k!3Unm$fu&5iuUECL5FG32# zxJ$BfO7ND-ve#mxt|+YW2Sr#Z#OQ$w0)~mN7nVD5&YiKv+BwB``}ynu)IP>g%J$;M zR~6mOc3Atf;ORFed(NI-16RJw$LU`$6#R#WZo9?cRwC+0j9T?K?BX&pzShQmWVNIF z!V|~wYCuM%B(*+c!5+sMi?jZTXbf3C)~Tuz&n!&G#kAN(0_B3#<2GL&ne6O^Umbme zEjGG-nkKEy$3*q;=qeKZU2*!o<<*X(!o#|QrUBQQf%G-r%SiOCELKS?FBc@*DlCQ{Pn6O6_hZ>}ET;wWzl!Wnh7gdO=VAA4{UZO7*x_#AH6oDY zK;fplT8RXVHV|DX>sj-N^*+CGQ`~RylKMh1x%A;KERCY%GrUig-dmx&EzVe<8au42 zjMLwws7R^VH~dgUs4dgch)#%EsT;GLdqOwRq^j|`|N7qiW5LNy)NjYnk?{=cCirhK zP;c8Pwr&YWX*@GA#*URzQ*tGtbo#bcPrr3`%4bQh63N|MT^5el8Io5)TI%)-dzNTC z(xU-}I;}UtpXuFlOSzFfQp0Uk?^vbdFW<;aQg+?OiL3LP=_Jj6A%dUTPst2_Rd;u| zbeJsNQPN4Xa*SvA0B)R0CoUd~l~)a*Rx5pL(Va*e=d{|=LmW*Bf2e{WXiK~2Ou5jN|T@Ejcc#(#v%s6GG! zEMAJP?VBPu5oHOSqioq{;{1X?WRz4?eHT9Z?;ZFah_PT-DC(f{ccJqv*Tp*=2gIDw62g zx_8>uiGtaCWeu@BkFPhV3TB?Kanf2vNLwAcY^r2<``*y9pmhzhZ@+JwBn_Q5inD=! z51SE$X_dyqST2T`#>n4VYJF_baoGvua$t?qshiA~ERsX>FDW^Tadr_aHSmxtvX@E6 z8`2cx+Kx&k(kjpmi{u>AW)`+4Gb7)6>JPxUpWdUkb25L>TO(16%$@A*X780H)9ZaF zzAV&P@7pifkhjJAWQk(?t~WhdCsPWhlk+wU{K7t&!Q?u8vDNp14?@5{&c|Bq%3({_ z8{^tUS{@Ezl$7G{&FmJ9)?g{S8 z%3O`_@(}5znXJAu?^iwV@c>m!%wpl{&O7DBo6RQI4|fiZ*&fqBpP}@=M>==T0IKn$ z2X^dZMCwjZ>o3kjey(1uKb?Cl>2`BUZ!Ol+Pqf+T&(>wsq}W=v{4uulX5Oonjobc- z63fWP4LKu3IjUS@N#rU|2aVoW)=qipIp0Hec+1|>&wUw^tB&(Z zh(D~zXw)dZpq?$pa6-+8+405BN+K{vt?<-cu6dBWt2udYp4<&lQvBkpY8|UejHoCY(H`EN8Y4%=< z{n6rvQ>cQ*@py}L5dSNa-*LEIwB@TX8{l+S|JWDQj_+9Xb5=vmbvoFu8CZB}dr&$);9GYi|p%hv-_~lfLvM$m)prA?Dt06tOX6J^ZINqljO^kPMUbjrQk7Z;R5l z(p^+JUkR%_+*Yt~vif-!lcURwr<^++GdLR9$9nO}`#);EO)(i0%ZkNgnyGcf;#8gS^z+H1QXBrG= z=pnY_8mYDRjljl{bEx_u9k)s z$*^8Kk!o|e1)6iHDsU(0Eq~IRjT~v9A*#@5m9Z-Rx@Z07#nNdas>7$I{nAfgl$O;- zxS*7znFn?Dr}mVmzQ6!I9(jLzQ*dIip6uAKN)^AcT>o`aI)@+Lz(IO zOCvoG%OaC~WUz1KtqnIA9upj)GpN1hNNi+XA$~{!#zG4WN9`Y~uRd=ctT-mq2Wm^u z^iztvpVxlc%`xtF%wHM;X)_--a4ebAlW8$(YOdy36Yu`v_w8SP@>By?83{YtYa|z+ z1y*Y5LCXSAYc^p)HFxl|?%g&wpnXG@;QosZ16}9;HD_>cA<8_di~T!jK#Xbs3;=$G xP5h^Y2X@q;GtSM_ypnDHI{~2LPzF5WI8f@yxy2nZBBtg@@1>Qc%HNrO`hV%Nc{u<8 literal 0 HcmV?d00001 diff --git a/images/image01.png b/images/image01.png new file mode 100755 index 0000000000000000000000000000000000000000..e82d02b10afa022899ee49945240c80b815a6e89 GIT binary patch literal 3121 zcmeHJ`#;lf8^0?h$p{r`4mqr~k|F6ceM1UaBjga0u*KxK5MN3To6>B~Wol!q2;U0p z;8anfoQB1=uOezh&gr1`%=7)_dH#dvhv&N9_xrl8_jTQ`*L~kV-RVv^TZMI7)&T$% z?Corv0ayW-=D}-MNtruxE=3wvL^<19gUXJrW70x~V0F|Afcu&9OMa^XSOuK0u7{*L z04M;^06_l)GikOgq0j(j2G9_Iqy~Tk09XJ#0Kfwi2Vm?0P()x7425D^9Zj6s})_z$*=SFaVxw z_9p{HWSjk2hlgnBi)j!|$}EV+g=j)7rO~~FRb&p5tXhLZ&DHjR35-BA8BWe93tp1U@ zSrJ1bq|J)zBoc0?NGK5fEtE)RMN*PN^6z*4KmE_Wz~VWbJJLhRh1+>X0RU@SUMpI{ z?gU6hHPRsul1mt#6yp~e2*Uk>LP>_9fh28HLnA|zjF0(F0Ibckx3O}KJvmu)np#ZBv-T1U{`&blv8uM!Am8E3SKimk ziNp!IgK^Nu_{MAYW`nM0Q@R-!1$W5Z8BY&0vvsMKh~1GZi!59AR;`(wNQ(~MxQ924 z_&9Om_`zLUCf|f|h=dc!zDDm%pUSei!p99GJwr1&g2ezrI6Ki{fK)d8#tAFi5MyJj z6&}{*Iq6ce@T2o%NLR)n)*cBo@Yw=GokDbZ|A!cKW`UrFODt!!}i## z9p{dRFx{2_-P0Q~2g8=CBs+YTY&omehZ|Tm^J%%ew zk6)*L(;A8phfl3RMbUDO$MCula+~dKF?g|dlq>y@dxM6*Qm#h5dOx$HcK66b0lBw> zHAi*2wUa1^a@Km0H&^HpS@9%?nwQR))ZtfBVB?r3970{ra@Q)BzdN06W5=MtA?L|# zx5FX7UL)^@a!L+_IzK%HGZq9f{ZF$CsL1mWf>RzxgITC`m-5rg*FfucXn5LB>a{)0 zjJKNbCClHJ z-r#Uv!-E?4 z$70vTzip2`jH&%{>yz9?oSJa9y7R+67CHS>%gM{02>R$y8QC5g8mFn(HBfndR7XZp zr8Bc$wTfB9g!}5Oo|KXLH|4d08U4+(xGr25b$61q5veovUyY>b`6z*rv0O;Vci2Mt zBr7j2c>6}NspRR(A`7%Eltc}CATJ2=#4Pse^Z%+rt3HhRef7vd^NWcxd@$eVTxtz5 zP{}Rq7d-cVtGxQgisb7H)1p1*k&@};kQmJy{)avq;xjIc-Yw|w=zo`c0TW&v` zh|=5{UrX&2CXaNkfU1@A`~x5i(6R~htEOQrS4imyV9fG z)--Blh%E{&X1^JNiuH*O`Wy23J#6Q{M9&8~X9!zmYwz3Kulx0hH$1lco8qAIn}nUX zNphOak*2r---Fa>1H;M}q(|oRTjs(n1j>_28!7EtnYuVJ_M}JpSa`%5(@iHEQx*D; zxN395-`IGolWq;lW^gzd3S*{8ag$@#RZdmxtYDa+_$V=*9+;o-y_xhr#zt18L;FMs zU8nw=#^cXrYPDfO0D4J#cdGLfA5C<5drNh75ik=tUqaIYx4c9qIvWh<5>6ssSf_p zLB&0o`~w$tlA0leSM$3{Qi{#k!}8Co?|Qnucy1{}lnPIH=DkXSWPJb7XiQ7NZ9OJB zJ!Y|A-_2H2MRn^V=5ag|xZqs*evWjM3{VsLcKG-UaE? z6#ZBgQrMhOexlvBN`1Xsg+Ylf)bPg-;pjX2dl?TM$$?AyOqJ1hB22pJ+M)|pEjM_$ z-{nIN=)K$b?8^vQ`CC#_LgtXur+V^?C!VVmPFHuj+gf3ky+P*0Tk4nmr(sMpQfjH$ z*L3u1yC(|bLG9F%+eEjDNIPxU#+~A8x@tR%+zL%!7^JG!29N{XDvu|c|CBkq1GlsW zS{mQ0=hqE@NDzV1h{4L_?Nfex6*h2|J!-RZ*;A-hF!EsF+Oo`_zQ;Cwa$` zBv_ry`q{2u?P9e9i9CD#l7Ij?_}Ofxu7ZDve5Ph+8zoIgX7dUwWG}3)1Rq(g>*-nT zAEHcR*2m6Uz{jo!h+VtKn@M122PO>`;tkbY_Tt6ez*Rg?VR_m6{AT$0lKhcARU*T^ z>U7RoZ-{%XNAnd%J?KcAg1qO4X{AeKd%tnfOZd7e>(XlhBsMAzLwNXBY&XT6xa+6r zz8@<{^8MP7Dro(ls#zIVkTV&9X^|(*o>>hg;V|7fkKD)Khpmj&+n}i8+u3$cp1yJ0 zYj@jM)_jvPLR)A85y&Mh*9zdRFX{kUb!$WKq)?9x<>c*7JRPh(OU$}2UpH6TXlQQY=L z2yPMaieq9_R=fBmo|ghsZAyFAOHHWjnI>z;?Rj91T&l{BVYIBHN54^xaDhbhQzJvG zweY4HgPi1@WjFVif1RP{Xn4NCc;EhX-gpe_FJ$nkgE#gC@@q+!t4UqxCt8lyDx^?J zH{wv+{LfBy(^-S5S3NEk8~QH5wOKa@+1Ux_obU0Qjw`DC<$`LxlZ#2mH?~R3?5wQ| n5SE!u-qnRLzJ8VN#7bf00gi55fj8H8X+^yo&EC__Xq5xotDLG%)d`e~zgK}Mpa z=$%ob#gHH*-uS-v)^EK(;jQ)Vx@X_B_ub|0efB>0oD;35qfSSCiy8m`I!z5VeE^^! zlKXL2D9LauMBq93alunxT?MEd0&S3yiw?>UlmVbRk>=Ez3IHepJ?)2ZG93UQ03Zzj zFeteJ02F{g0cjW+l>nep01QT^X#s#W0H6Q~EkMc|kZ^(mT2R0m2B4r4PDTJ=1VD@c zX(I{1NQ%rvzF~e6fWH)AB;kYtP^f>KR5Sp?K&7H#FbrT80a#W7C=7tYL(L+fmX$_Q z(J1oUkO&xt4D+E8l`sq*8ZipPOdA#E8x^*qFc=gbZ&W&JRJxDCPowZ>e!Z>!ANMgZ z07KRYLuHx)S(ZSi86?Y68jk`nWC0AUDFo6IA&tk7wUVV#(-_!9q1IHX^%)*D(d&f4 zV=&YB%#~LBG^S~#6*E1JnLf+f*~gzvx17VXsgKcQ+Qdr4#7h3uPUXZ(>)ABs>X-P7V3DUGDpk)k7r658R&Ru*{k38(5WjtPX0*y*Q77i8-tydGerxSB%)cxN zQCNji|K#-i=LCIir*A{&*52FOsL=hVq5X*CV^^!P+s2(>8QP%^syNdAe4n9e;SB5D zGZt~ea4@?WT|BAY&I;PnoaUE}ERI7z=n+Br$nTq^ZP|De??M(gHgX2X!NYaN7IzBe zhjl!JUI(Ow@4k16q+8;3)(sDbg^p(gS;cQW z$z!;@QQs7MvJaay#D#>lkOk1~{dX-#^Baqh{!X46Eo=|Jx-6z`!6H-!(5Z`5)VTB7 z97cV7))B_u{{_3pak_MW!yz$GJ>&iWf`Zsu(fdWL$+Mpmy@^wj=Tg|!p-%rAGCp=p}H~(d$lLI>-x^)F` zWvluftbVyEPP8_Ujir3A;E_ah=3)H8_!qz(ULuKAMSQBWgJk zco6)J)yR_Ow{htDjfg8O$e~y`%UfEgPu3WCe0qp)IEI{ewV(7~m&{QpHJvA>Yv`8~ zV?WFLlE>_^eS%8av8Li^IbsubErePSjf}eZXcsHmc5U!5yIT5v)I0F_JQ!X>%gmoE zVhTCXi((~)g$mZzKy=7*BLTi{Ym$}RV7d87>*}ETy2Ym2 zvG4r8!w(6XqMw1Q$L3nkE(zx~LWR`EMz6gO%-wubw^39RLP7TIetmR!94VTv6)_3H zJc=quhPMeJj$wF$ButQuYZv;PR3dyceh($rk=X}eroCXxe!~oe096fjlB1W{;IkC) z+U~wN(d}k;mu2x%uv>S&k|B4pvnF04pdsW8x&aB4Nk^Z-Q)awnH#e9Dgw)U#D~cN1 z3R7igamqCV9}BW9yu{i;e-Moj&?(5RPf&W7m& zTrJP3UewCBKKGvOFNWdh2dIrZ!g=R0$shQX;*u<8+3Yc1u5=zhsWQp8w%97$GVQ{Y z^XN)8k5a_5NA6vi?7%@vAliIXAnAD1cO{9~RxxWGHMM>{}fi4?z~n&-*vb3Vgun zawerpnC67Rg;{WAu6C0O?OCHFP#l_^%DY!4hu7Bkr61f5TwX4nw3R`7N}mw?AKx?> z+V0{XIQjapH2?FBaR;~8HCrCxdij6j>$8#-n0g(8!QdPpPb&KB3D$QoxAi&tzwvg&UQ`X<-hU= zZ+i-lt6t}&oIf2ZQRSxnq}YhPSU|5`0z)ei}`@P!%TKoq&^m zD3Kv}|DAy-19A8FQfx`X6+e0Z#@{W2j6{Zp`5?dkA7yEyYh9k94GEd+e;B$U?|+uP z@}nkjn*Rz+PP&NtQOW)$yMQL8Z`G*>k~$b>N2f146OogK-?@mL_n$jiW5HQ;oqmr* zHiHPc=6_P$+5MjEh|F0U>;?Sfdwojp6(WEurm5HCIKhu}ULFnP$aHj)>=aIm7i&t2tE#bNI!v;K(oo%DA)NtZi-|C?e1H;F=&h1yyeMI zTH2@>2Q^$EuIEz{7>V?RYPloYOzix8!cxpgr;mZWk91>JO;GKR&xeP~+@oF36?Gb1 zIf>%eHrNF1zEqX_l)AuTu^e7<%{g)Yh?3JqZzi`dv%bU{sCVOGxh{yoNp|NAT==m4 z&^HXtdeuFIkCdGE6_@h4gPU77g)~JcUy$V*7Ya2PbJMrjR1K$;Y1JxW(}WAp4&5yb z8h2JrO}kZ$)*R8!f##b9(Lop)kY0ve!%W*S_63ks6J>s z(gCHsy!?6UV;v+ftH4K2x|8Bn>#nCy-6}uRVW)L*yg>-;X1&$JL6SMaQz@LO39R;_ z6sMW^L^aAd!b8Spl8q|SR^Z%lQbpaJdjx774Y380piz$+xm}wAJ;uUTG2xHvQ5$^r zB1H$?B2I$jpEX~gY}-x7`7S4HYz3iwi?(tIF7l+kwZ$`M%OXrBr1(8|7R&?2XxLZ?=|Ya>N516OHbhb)&AIgoruY6}C#) zgzkoXm<~7wEkBDLl*o3^(xxjj9a%O0wPySTv$0Hq^ec##D9s{rZ+-ascPUjoNrA9u z9V0u;x73imuDWM-t$YDQLkdC(a^8`-MbGY+H#Dq&t^v&HW-QTt<$XU{7@t}IO zdIX_W`m8dDz-)4-{4W2reqtNmtmu>#S&3Q8#2%(1=xIz8wmc|4`kjZ@?$?{p!xa_y zj5ZJbWgXuSuY&mo4J$m0!JMsL2&YflTNeA?=~_*QAQ-{Jy11hvjucbqln_o4VkP@+ zy_muJkyGMp>9AZ~@A=8S{d*=@28*@(1`6U@4^#Z1pqi<_e&oqfdb69phz*Ww{ri59 zK0%q+mA4^i;ORRD{i*?LezUTTIVK+G?ZNl)1_nHz#Pe0>cV3&EJV}jd&)iX2Dq?)N zY3@k4ESjC4sehwmYIRdtLH8TzjO5L6@FY=F>p0q%x#~jaaU(3Z10I| zOqAjb4+CqtK5}I!<%o?@pb4A5X)Z{(>=2D@G5%12KlbCIs~j3l^!WXdl%h``#_!7O zDDDb*j>1N=K7yQ%%+OppbjO6-kwk6{9Ys?%+?T?lF&xc9sRaSu z_qyt`l8ipL_S9yqW(+HZ5WaH6$eRj&S7#bqq*|2f@b`=J0@h(hmxw(zw96!K%b`#< zUFc$bsciq!2dD-)tiDViv9Y9ponc{{w|A)xk955)JC$DASU4y<9$Tf}S3VwjR!K{U zwAbv3yq9CUSh7$FGE!14ew8f;YV@`V&P7QLN;~tgfVRY@4ai9+|t`E9Ee`#}jA#B_?*t{dqo@bw|^|O#Pd= zk))bW5F4ymG&(M#Ij3<`|H4}9cVj`K-@AK6$pyK`r{&AaNpV4uCd}Y}xQt(5s6d%N z!xk^#Wvh%h+DXHc?5^!EgS#OeUb@T`SajP`4(YiMX~(h=6vi&^Xvlfi;~3ANFqQH6 zRP1S<`*OkehSJ=d|0>IRG9P)VuV0tN$V15PEHeuj{c5PuK4dgAl#!}mX*6S?(EsYw z+@mYWi58KLrdg$Jq~iHsn!~&y)}_%^DYM@iVn`J5I1)cH!9bF9jf!iinr6tBdzUGP z;%eO!UBx|GTuV>Y`6n?G0r1Yw%C80NpHX@^R1!`n_3pxzqP=$cql$u7CC0)4O)O>a z-ND5Dz{V_EMjCl!m%wSOMCHAnTM}n#G(Uo@9g4A=%vIn>9h#`mey`*u;u`8hvH!Rc ziJrTxdT1|59>FZV&Z{{m!6Eq5-r=F9wUBi2bs;4#OAmnDKkN`XECvQ%;@DLQ z8`Yg6(Zl>Pdn{lid3jGyR1<4M#_(LfqCJ2cy?U}+czde8SFtPCLBVhGsiR4;n3$e? zUREDBYqipkIiF+ECa(eSY zgR8-1Dbmrql9=s0P@y*(@n`)>Rl@1{`hP5w)UY1?kc~D(E&*3$S5>klzOo&mcRQ#V z2bo_(ex><=MpnQ5@fT~fTC6K#fSF@P8C!xL;VK%B1 z@km{L`?=QGUiup!a$etk_e!Ajx2WIqbeG7_EGI8+zs(N1kE`|spYN=sh~s|~RS984 zfhl@HXLOJjc_<;El1Wol(IpOl^VAZZEqyoF49+l|=UB|)}M z-*X=7Jgg8>b9RWw^V~~E-<>gwn9d!@$vdJT*q=nP61GjARV6(8naYfqV)=H-4$zlG=jhWQ@iasuu~}V_tWLnOq*PM0m)Fw*76#(I${8 zbI-dxyWWQ{y&KbiU$4Wd!H#yp?j6HQ)-w8_U!`fH7dbc4c~NjtrkUmw-@PBGa9hKu z@&++=^(igcEqo0AC89k`P+*ytxTtV{=Jlg#qsdGimj2Sm>H11x==Z`wx}`1=R`;AF zkQGnE{UcAOWlvq-(-QgxgRZSTAo%uffZbdX#mtC=A3qoJ*-o9YpW9b|$DUnTBK=yX zT2Ho*%nz2)WUl@a;MUvw_guefnk$uuL(cBt&GL=32meI%K}$V)hJ9RcPG3OrX0CFn&j(6Ch+CiQd88H)%<4a885AN?XpQ%V>s0s zms1|f&n(FZH8##D?lHNc$b(=c<6>9{5>^`CphMo6UX z1J$QM67_KP2$Rf*wu>(kNGRB)bj!eRMS(rv6STxYTP_E?nyC*Gx1AkC3U$xYSp8rz zCC@U=OpM(RmQu0%Ct#lsA0i6G>2x<=nfQhycbaN)#{-V7T56TPMWoe=Pnvm^hRC;l zZF)n++eYvd9PFZV`Oku_$FT!$ja@r>;bkP!!+#PO`~Xg%-borV5BPbDStxlQq~#z{ zM35$ohmd?Kxn_&InciO78LlvcJ>NUl)>HUUB=UB!eCy7`wE`BmjlTM`#TkPudj!8k5IW0#nOFCh2f0{u*t^Q(p;8GaK k+vNon2ZO>;?4v&nzf=>Wm~u(!=jX+ma2>Tu6{{Ej1qN7&V*mgE literal 0 HcmV?d00001 diff --git a/images/image03.png b/images/image03.png new file mode 100755 index 0000000000000000000000000000000000000000..efe42f82bd2f177b842099b29cccad8ff278d7b0 GIT binary patch literal 3512 zcmeHJ`8U*U8~<7|##)qpEE!rXVUWgB7@DDwF!qv}vDFZxK@!H2NZ&>%YnCio3N?1+ zA^VbT$czf3GNhDxqL_DjetXY(&w2lW=RWs!-Jkn&U!NbY>s;qPSBit(Nl_s=ApihG zF=&({0Pw)MI8cy}>-Y0AuW%2ZFvpV?K<$XaGWWvoXMWlo0P1cF|MuAh0DOSMDJLr~ z4*(hffYjw00O$f5NG?WjNm~FQ0Ej?cz!nJ*umFGsGzfnvWE6snZ3zG&3SkQ%K_n6c zJdywcjljhqs2f{^1ZhZ+fh|eGmb4K-kU*mmXbb|46V=@YA_1--#{-}dxEl!)z~)#D z+8^DYsGROV28f&~vE|SRGu?q8gO>1Q1EuL_A4ZUt32Tk@h~@0RV(x7?inFg!iX>(W?xUNN?9{XUmxuUav-T z*;jaILbiU~t$M>!uZ+)}JFm`-=Z~)EDxA}^7#&6AY~eOHj4|bTD<$OIbZg#r1|BIV z$W%uUn^Bbw5bo+|C^atD93JTyk5fLsJLMB*Q868oo#X-8-(`puy?TKBX_B0oE?Sw{ z-0>~cc@(!@TqvEpA@+v+@W~P~pxq~{N^r{UY8Y8|{K?Q1k96&|<8g!~QT^rC?NX}IkS^FI}(=X)+ z;vlj^Urf{z_2Zs(Skqg3u7^8B=z$m>^5J~@xHEKXH`^CI?EIPACZ&2y-WNGrxNsqx1| zB6MEDWF%fn6(%?<$S?33@uJ;?&3Fc)-`dvMe1|(iDuhzFr&}N31+UI2?o;VM^*PA|TmDPx#XKoNeDy%lfop?!X&7Am+=aWu zAE^TeqZZN`tu~h=+8M|TqeKrjIUWb(E6XZh!s1SLU0MOoYi^yLXs9rvNZjEiSf#s9QA-3L^m-v&ME$`vaFyCih4i|Ex;>qhlTmCUcnUn z&*uaa%#NTsES#U{M=SA@q&NwWAs^07k)~g4uY8v%?HRKN<@TuA>nYoS5~cnKbXL>j z+q%$>7G(5qvFOcdd+)20oA(#kpu19g{`%;fqnkYasPe0&XXMx2_#vS+#scL_i6*lzU>SojnEw8sOzgZ+}nzEOMH;O0P zu?=???wLiKw!Ryk?tT2R5F#I~=?i1Wn(7e^VTNet_SnFQWG^V;Xwf6ER*X`VbwS0C z=iLcwP^_($Wi_wLn-mkTetUXK`Z3Qk8;riV#Uh95=tXyP)Ov@KoYrL64}N^^z=a#a z)X`RFvD=kG<1cT9-8Iiy%RfAR#_XNvTNw%krKl@$wUc@WFX`s?(Uqykrwqc)#%-fNhoY>L<&IudVs zEG1$*HhPy*Wh9C`A2GgRKmYz~+ZM5c&j>P3oT!7^A7+(Cp;_{GU1d-@!qi>0!8%}< z)!Ec&>ydnn94Dhea+^Ije6swklTqtTwcwi2o6|f|=(*~{7A?n$ z^EIDPhWh8Yf9SO^60I$AUA&!Xuifb(eJ6ZoA~r$l8d|aK`8-_$t!;LhC-&B|SBwZ> zRS#zTp0RWU_7pn5`f#E?XRmRe$wM!TcLGnK6%oF?CyK=<`@k8OEi3m_5Rid1s&)gS zzk>P{A@Op7jM%kw-o>hEn34H^)Xz}yWT4088 zBL*Gh;hd8JAIcN%{sO0)bK)S{$!L76T88%;vrKuceY)`x5YQnm`Fpa_#Dc!=qWwl(KH@o@Z0{Wpk@EGj(SsKQNsz>=ywF>CQ7-+bY9d4qY= zb_mO`LN)5?L{?e$c=xcg5Km{87TY9GKB5<%XW~2ax>*#yHsbQ^anmtJ_U$#$x&;cYjy#4S9T4FzcZzDtC~V(6AX;qQzb4%>((DIcZ zecV9>aI4bcdaXh0NarM5t(ai2+?TGI*>?)PPwHXhUiNV>0R(b1!+T-Jf*L%*!gL|D zj+XXh^40udEC_U}*I<>!8<&)(OuBYrF3Z`e#{S4d2XRErC?m$+S`A+~7l`4Qvv+8S%#9PKQLFwEPI8g6e`j(YTDk+t!5d$W{eE z(>)(B6kgi4S1&rw{~*uP8@B{7KZb{HzK^nOZEz8-AL`IW*URk|miTII6n50eQWNbO z@0vbJ12E)Xx+xZaxrJR(Zzl~N-2BTbGGje_(v|l#u1S?B>o-=uB)l_0N|-q>rAS3gZe4o33G3N~Wa=fR=9iG|-v z!y4(eqldr@=C_+y3it!u9~b;go(TVLbqwy#E^}D48)%=~9V|0WRocI@+QZNE)gH#I z(j|giQ(vExm!NNZ$0W8doeBN8K(<}kc(mRR(Tolo^ZdnWOSzkYJ0A8-LAPgR&DFJc z*7TKw&~(Qbb-OcIOJ(1|Cw}a_Mi)ChbxEmIfy&|2gE)_vPOI_ST#zL-SCd+*?s9Ur z=2U&TA<;ysD%i_ZdDj0K1-J5pv)gf&*m~#1b1$FdM#$4W!GGpZjFla#*5Z8pe*hgH Bduadw literal 0 HcmV?d00001 diff --git a/images/image04.png b/images/image04.png new file mode 100755 index 0000000000000000000000000000000000000000..8e1b40ca0d0200c44d47b0069be632602123c94d GIT binary patch literal 4089 zcmVU2u8iO(GM$T4lw z-e#g}dg{jQBjLGP(>ph*3nC!P0U)d;pDl?1y_C*d9SE$Ccz@6f{zvpIy={L)&r+nJ z_aW2v^COy1JU2O-?CXihLo)n7TjqW~NTrMXgZWWP_jU1)J@v$)ok#9#U1fc!r_nFb zc|A$%Ds-_FHhF}85m*>(GGzPsn7ZsXSZ(z6K3Tpc)7mxd^ZV1vb2N2o>=Ir5z6R5t z*PeFj}Bzu;4h1I>$+@$L*^k7OOwZR=uo$>A#+w$E6u7pc-5HE?|u%lrx8otN$8C}`}iIumfPuz#P{$4auy)z z1C5LBzc?T`R}lhiEQ=7TGkrUSm#itv!#56mX|rw?JnBBq%>0*_?zHkH#zxi6>Jv7rpW}RyP;)Aj`1$v%3xa_~g zIlxDqHo%>ZeDKjZaXoaefV#@g)BBpq2bVUwBAx$LMSzWERRja&aTH}dL-K%ZFDUU4 zy@KY(@Xn(@M_wLnJDNny%qR2s|CdW?oFLCo`-SkhCtXVn@~Gg>b=t3_VrITl-RuET z^3?E78GSmOz+IGwn<=f83Ejs})$cmMgWkFJ0CP0X^861`uOJNJNk_*``U3dMp|R9+q!%X2M2XJ|CU04jb$mgDr8m>WEd__liyJvl<`4TOvq$I-pVX^MC5)8t1of~v>Ot+ah zM86mFtT@G>jL3F+-@uKWc;U$_T8NFD_;Yot>&SS_F1Ck{l^R0l7+)KgPwXd8r|#`o zPCeM80^c~BV~n8pUQ?c}Cz21VAi&Y^Q(|MGGC9#rJ9?`lovG+3$}~~iBz z7}bn(W8rAHiL58Om~x@|oM!lCtSGCFvK#$WMAsar^~IsKit;CNbad9}ujTr>nd8Ax zzdVqRCfj)Fy0W9k5=WJ^Pes4YlmvQA_o;UV)0&hyGki@x z0FdQSCm*Xa0weMU`vc2Se}ASTZc5-rzZH;Q!SMA6zWT1*EsJ7`pAldLq7mQ#Pc&4Vkr7}7{xSk=EdO%vbK4mK zMj#r2e<&x7$9ZD}7y(9r5y(bB7|S*Vc*t-IArxk)0TX0M>8gb5vGS9Ymg-5Ej=Y^gLqx`};h#R<1=}#|aZwWr0dS zHDg(q7B$N04u>hKs~W{u64g{ER#J)zt2s5~?}fScA}R~5UjUJEtj13lwgRMlK(6lf zJX(vUVT!tlrVo&p6#4(mGAU6UEJbn~O_!@}3Tncd>-lxYvSxg06Gzhny=foTj;C3h z_-camGOewiv)sVTYMir%Q8fq}E3K9R8NX@v{26>*z`U@*1DTdg|pU<8ZtX1IM4UPQ&pMD z1`=@2-7K=#wmJ8Jx`K3ha@WY{qnfew=O{09jw671ZKsS!d9(s4wMqXgg5)ZaX4Oow z-j7_wtNp{m3+^LY!esk}3!7ZdMk|pv6-=vUEYW`7O>pS}wPc}qrK2IMemUJ`X^L}! z_cRaKR*zD=n`XT#L=U@G26*%;l4>X`2$G3t*d;}_kZ;fK#b6wO487tRz7A-XFA;>r z%?xn^wV!ZGu0$%l^>VW&82~$Vt=h18#dAK$~ec zTrt35J~s*H-VsdaIG2hq%*%}o(Cg(5bx|h5#GOU)G`A97cc# zd%Ojf_lAKyaENjFq)1>?Q_!oU2=$0dc0B{(i1 zZq6a}jcH?M+iGt|s3zIAZ79h(hrFSKCA|ALzTj!~IYp~r?NpjNh`Sz|*EP~uPGCOY zC7(T|nnNdu8i{OWH%ZkDAjmCQFDWqVlYm^b*CwnY$Yf6<_obVk3}wXk_pxLl1;owS zguW4N%W1&*@Bb$Oukm#d&jy zn{x<#3)`5bSwrpct1C&4eRYdQarI$Mtl1qd)n>3EmmG;iYn{4eeXJfR*li0|nPjqM z$$fEJ2nimh{W)HfBS-}x1ss8Sv=bKHGBkUdxLJ6QIOUW@=$qNbP#>MI9iiHb&vijM zm-41h-RDL?Jy;uY7$GT7_S~$l<%}hHm$?4PDrNDMYi?{Mxz9*Lgcl7l+Q=t6dH{&T zGBsPAz*ByY^@+!=vtTM@D=_bSn=}0|TqsV+;yb;DUUWgq7>Bhh@|5dANoY{l^2QQ%q9t#4(`4=D!}ks*)(||C z1ek7CBnw`d2@n^eQ;%4mc;tQ*E7AWUGTG|nzFiDJ&ha&u%sF)hQS=Iy_zF)ZadRG_ zZ)O|g`l$AH#8q36OObq!FbLti8kk=!8taspD%V4Ex}Ib#$-8`;4^$K7D?sd4a1{U~ZngtNLbM?C-N8stG4;M+rsp#e>}qYSw;fL3S2&>imb^sg)j&ty=~z2r zYB*?0S0?{0=$y6@6SPgAJos4GJan2#W6Ybw^)%128GTqPBftnS0J8R~5nu#bi@^T@FOZjIVWRvt00000NkvXXu0mjf%tpR5{22;fvq7>;Aw0@2ow~%-%D*tl4Y!tVy|SYauKkB>(_`u$85$ zJph36*mHkAPBzlsH+HZGP?)`iF;M?edXdd=T{p5Z0)Q6}1oyqT0e};@jB+$%%K-oi z0B``%0stg?rt-hc?**s|paln@S^yjgAlVH7tN{Q80ImRl0RS8TMF9v`02%;j_ydSo zD1d?k)>?q83V=ZZI2!22^U@X;q2ovxO()kQCdPWH_FH zq)_1aG9-nDq_9xLN))l(rJ@x_p?DRw22{4=Xf)e`4X+{=j>e|Ln=KnLovlO!KtZwz zNkjo@F2Ex+o1k^B01XFJ20&|KwEFJADQpuIWHTO0qp&^Th?N2DL{wc{YCd7d83mZJ3hT`x*Fzd^vn_V4)1N}45Zfz>?d(RZ{EfeX*v6u;Xpg?O(pZ${udNgo zD{Xv(!aA%O@2i_+)r@b@4q3FrLq=WJftzk z`}!tX6O&Vn@eS4?i*g?+>idQP;`v83zoE=p*P5J6O-+8AK=t_g_WAtl zw{c(QgO$A25L>$*F^w0uR#vx)=6!Lr*8{k-X+wt8W|>Qz=buZ`H7NqN=kR`txjs{A^JY}7=38K5c;U}n_z}=){hEtv*b19vaQyLg_t4}&AOlxSCj}iv9bey z1=y9c*?+yRlKbi}2=p!T?b}`xPrC>F-NOlLMX?BW`j%D~XeyI~5L_ed(#+8uQ$ z6!A|FcA7NiRFipf-_IbatPVNH#8^>d-^hh7zX84DUzcMfoI~En$mjUb-wPQPYOJ(A z-nn}(f$N>$hI-dajN_X?Nzb)Z)?!W^2kwvIe%)OVYAbJDk{bp>nVj$M|0E#s$peb6zcP+IGLP^nCEQ!(NuiUG))9-zB|LGy?*c&ZN&Vrp7 z)qy9uVnNN~qT7XL)PRT8=IBqFsT_HM?bW{{@J8om(yrDD;KmsA36ZJiHgs5^T(Y?tvw7o6^=|U^ z80Qzc@28MI1;#})?47wBUauXW@CynmN<#1p<#W6BS(K!eAN$Hwo3tygE{O4?V+}t- zurTY{cBuy))IR-#&wkI{qz>W#)ee`)E$62;FbcbiABVv6>*riCxU25kn1y8peA7Oo zc4rJr6vKGlooKosjPo_Wzc^K+z%Ku!VM2z=F3;Zny{rOyB z>`KzyKn1$T;8$P5u~cgzLQAW)9t$gP&py{pS8E=jIE9G5xYeivRl-@&$YfR*qrn?C z27;l)8?G+cr-L}NyD|MT7>UM`i+^5TFu|`*9gGb3h!&AK(>cPf{FMTFmyvu%up10> zA3sEzl!=1eJ$Jim7`%)bMtaE)QJ7qu@JOQC-Gn-xyt@a{$C`DzE)1b%htDNV>%42m zZ-VAE2lrJtF<`!yrc^=0 zGNG;}L~_hy488i&J^X1qpY3oF$)TN&gpN}!8mf1W_86LcXLe$zzsdfJ-3i6Q8cSw( zU&lqBtvB5EQOhkk(1=#!@%8|Jw@UOnW9d$w=qw}-{N)g@(8s_~1wZ}3!YV&VlXQ$; zbo0z!ekOp*<^zTKKFsDQsr*T4?$z5>+Vo%t0Gl|Ypw#u_loDJaiw5SpBcRTwI+5q7 z6_n(*pYb<3guE-0*ZN@>NIFeQu&Y=%b~6riM8;X8$*J(<;7&L}+efwPrL5D1umzsC z;*uB_NS)(_AUNO8<5$Iw-fQxuAAzP?zSz!}PgHqkY^x7mQmp(XdC>Ej#H+ox>hbnk zk!2Veo>8oVmwJ<0eXK?|%^PKE8Qy?%!CWY_<8t~6FIhI}8-4sJ ztR|zKj4l$*t_>Nt)CqM9;dW5_<2XwC+ty6D7hKt@NB!f(QWuu?WS$vA1w(u1XG6Fg zPAk=Qf87Wf3Q~p5;YNADANfrD3VCAMM<4Ixq`Wr&pPIWZtX|K{uf2c&=FHqG=o zSh)kXvD@Z>Q_~ULgBv_E_OCor3;bwOu=ZyZzVih$W=t!j9u~avhc2QV=eJ#@$ z-w{Yo*`q+$Q0;KVX9;v`-cao>rj4#;y@uX6$LHOY`|l#P?-E9(rsXJ*H^3|S?Z5(> zb7IG_2hEU2kyX2k-BA(TZM7VsFCXyBVPFA$)xl%=RZu0H()gDW@3T{onI{E1RLGND zg7`A2BdC{)Cn_;6<1W&~yCHgwr7_p`Y#*hXuKd)6o_|1oA_;_G5ABe*D zG8m;gDG{&0B4L5>L0yo!Q0$s3`EQwBiDP>@J>*nX^EQelooXJeM$vW@8PMzE18X*R zgja=S(Ll7k@6p-WHDHsYE*jG(s?SBpYvtg*PD?wj|CP8lxU7HSSZ-bKZPU4W)cMAa zY8Qg9*oG0$I}sTd)H5lp1P9@ow8D~QaIfgWIVBSV9a6cYD%#`xqtTIxZ|t|atpzf8 zWu!&GS^dOl(!<)vp<-DokE=wN>|jf2_&;U&VP;upUW*X=)Z-1;{s_3R0~>u8Ewy{n zrXZbx*;KN)ErYU(j2w1scNf}Il&ssLcK5&LQq<6St05!zHhY7M9A;6w$m9T@cU6jf z1Mf<4!iC<$A)kAO@MBV2$l5MA^Y^#BbdP(vXyq46&kS6;{?Dl9i=*`8=$G2CznG4M zo}@@_wz+cpf{w6(>>C7BupX80LPb$e2O^v5dM)`-DCe7&7PH|=B4(u&UeRLz&lGeGZAb&=ZLUx}(?W*gB(9g>HXxW(^TuPRbb`wTC$Z<7agAE>W7FWgb*VA~ zv-0|k3XH|a%@4ypEiS;OxT)JOuVPeq&_6ylcnMtaw{ddvfn7>;U(Vsah3+j)7zkjx zFS}p#f88r~)=3g(K1*Xd0%Nl`bB!&=4@Z(*ZF;# zQHjYBtVJghhxhyKZRp`sJzED|IYf%=A&3YO+e;sy#8P>O#19#ERqOGZnn3 zCfIG|!MyN~?;>?4CQ|F6PnZKupjojy8WA2@cKo-@x^IckmKF0J9exz5XW3jimG7xm zlys}@i0HO1kq;B2)vrkQezM^-JSIarrziMjDhfjnBn+Ag@5^ZyA!4p&-&A?Z87LDFf2YkOG1t-O^H$Lo-Ns4Bas>0z>Bjip~I% z@A!Rx-XHIubKUzo`?}ZJd#}CjwbqH%2WXHHGZAB9VUcNRsu^HmVasFi8$x`{bDT9| z0t<^lL`zNCC=mP9%F2^vBtzcv>TTgIfv}Q+lqx>8%^}tsTc;RTO)3>?Wn7@L^II9n z+n|^gB~3*q_a_OrRByaW2`CRRHwY9`+^qQ0BcUVtX1~Tx{hc4a&gJBRLAF zk8F%}q=uA~xWSAO^ZPdEdflWS60H7>DX=-a5qZ9hjU2NJgoH{$n^j zI&#)KFezU+WG+wzE}9Ond9e@wK0Q)R%e*y^q6h$mZi>yyU zceF_)Hi?+MKV@(<-Vo&7l_=Q1Rk=Cs@JE^Z`LW}u1g}e1oW-cu7nkNZI9$i=NI;Os zZ9_fJMop}l7K91}TZXQ$aRMZJ3ReA2OCAsNOCf7Lcr3p4th!#yC1{e10frL<%B6d( zEo?UNZ?0k#SsvLQDP}W=nmxH zP3b0Z(~79QRpx@Aq@)O3!L+&+R;_Jv(g{7kiUM^Na-x|zd=`S6q8xO=H zl8ZA|oZ1{-AmnjBq5LUsDwch!*gpbXUkO)wg6Qyeh?%8jdX-?d#D)_Q$fNeM<)_-b zW9YByRpfHR`kkpuH2c=blwN=$w8O{ zTRau^)OM+chj6&Nk+hZdr;$PI=jF8-%?4@@yG9k99Hq<7lCaGCr0ra;ms}D%2u3@t z#SOP^wK6Popy{?s@E%!B)v=n2f+RK=%$Pho%4fKxq~kHnufJdegqq)(Wut`GpKe`C z$H)!)J(uf#zLQ$H)KjMA|B@7QhU9q-G75&xZ3FG#}2;D{NCbXF;h^u6Kg!xgt z$fAq0$Sy4Rw-ou!mXgGd-Cjc1P0oxfK}(#MhBw0ARF~Gr-Po=EGtzTt&)1WiqqMd4 zphCX;57Ar7JC@dO3_0Q_@3|z--1nc(d)Gd<5CgF1QwXM1Y-nnPtWWc}r-B3>4U~`|j?0 zT`Mf5_PZA~*GtK$#lxU(dp?TZq{6#0H;?OFX6ElVyf^vrSiNtwTL2nG`o$mQd%1+~ z#nP+4Og<%n2fnZu`m(0a%qW{_S)MkrWI7{N(!6-ae3_>UXwLAV{rv{KUc{d9=|0!R zGK-qrB>c38*$NvF)?Ia>P5axU1iiu#+@0~j`Wfp_zFBa7NN-i=fnZ*{Ze}9*ZSDFW zfOolpqpX0tZDw;q!AyJIUSPw%7`VW>9HF<=Tgrot&SIE4C1Ksu&fq|5CknJmouomS zEw(0oCo^*g612|^wpGm2JobOoLxbH&?{MHpQn=UIe|JNJ^PjuV+bAnhI^kmn6C+@QkxC_)d;cshd-NaH;QmE=S#W0w$HX7ZJUraTM_ODZf5H3A zpEsA~!VH?a2}jY{2^BmNY3Hd1|E8$m@{}(}r3as~8NX2UL-2VItTz73F;Vh`RQ_a7Ti{$N{YkP9n^Q$YFxU z`HE&N2M3`aCk9)|fF*b^v@se=nqq9s0#q1lqH35pN5XIXnFm9Aj#&j1$jytfHXDV( zpsmvJ6bvl~8^+TE-AdIk);cJ$(F*eSR{H;Id7+oBA>B`5tc*Jpm-&6y*VjphbY=fG zg_M+Z^Tr1}=OWMj;3i1s?HTf}!=VU6--lrz194z%%18B0ryyXgw>Ld{zc|RXjl*zG zm^#7@ukK7Nd0%Y63FZ2r5t41~>+q7pa>Pcy*2nd3M?;#nHwRsAi0r;58tDc`f}#2P zWK6rv51~O=S=O2yIwehXV5S#; zPvmf0JhvzNpjXWA4icU8F0zS!Zg^QiA9{KfKffFaPwZ?C?g)L9-h`SuQ;bm+hXg!a z(s`*P%T~BX=pSYusQ5e=-z{}Y9{iF4aj5&=09p%CJ%4fGKa*@La};`H5*AhlU?!3o zWl5MLSd79-UEZ^B{1pwS(tH1)%+fDA9^0j6vU2{qOG@(7a##)0_*qNhZ+tH)HOpuN z)j%^uRhqwEASjt-?PmcZqU3oxV&nXVC8aR_QQ0tIkNNOvmd)@C1r`N@bNMnq8W44g zH1s>8>AJ+RNQ)mmYg*rm4x9_W#vmUr?mbi{IOB>KxV(iWY2T_G0 z*chHdQ0)H2b3}vHmL}Xkpd|pces{txC+Ca9&&(iU@%HNLJ#k-OL|dGN*#n(dtSeD3 zH*%JH+}$$Y%gVV$rXus|mZ(by)Ek(eEPoeq^IFtr^b|DC9aP0#3@x4zft7qRq=^=&*!pkKZLgXq4sW~8baJ)^O z8RPqf8;jBV6Y?%*`qV8Ui?&~mr|RKaqNpzdzOp<7?yo)&o+Pfy+`&8E%==!>;{TTe zUxT1$0FPSh4Z40ZD0GEH>|aPJSoB6U9hbB7yZVhe0to}@jJulYiW9f}PKaj`pMuFa zXdM&W75jlz(?feu;=g|kN!P^wvx0T9gYPxWHnu~5tZc7c(T2T%uzOnnrA>;g(esHm zH)-E6y3lMNz5I1P{Nn3VQHjeDF>d+4FmJyo-eMZ66@H2<6_Y>5aEeR(;H-)(yv7W@A z_gB>YVyj79zc$%CD6IOB*W_&vCwXcP@3?(RniJV`J9ipsOmb4i3lm3kWq&g!W6><9 zxYi0l;y5L_%4rJO2DtsB%tiZE)^5u`M9(mP`@J2sk+m^!ecsmrG`&2hnPGVw|7X;q zKKQ)9cB=2%UZU&Sr{wxWiR+f0zp$LXS8OjLmV539RRvh1S^kni$-@m>_kKLr`Pu){ z-uZQ_CLh5?ap4&)o3ea#Y9U+y>BG6@+G&XWpPoYdS3fd$@FuEc;wbL@i2`{W1UDqr zn8F=2wc-ig6pJ?j^X1fZKhUg!gSquF+KHp;Mp=e*I8?Y5qj6)C(_K{wLhbnnB!OD# z6ce0}>zky2O)MF^4EPH&?cm0+li1^wa*TA2h{T_R zv{SBD3HMiUy21&wj4#@gcoJ;r1BZWjzP;o;3xCBnZp!~PUb@qhs$y*KmSbEt-u;x2 zoE1f!0c}aPT-#8HuC0%lYv&P46B%g*TDtNn91~RUT_J=DHv?8SqKBH#{;e*<(&kgmE~!;~LEmSbISd6=|RBU(hwqjK)B+-+M#a&feb; zQpCPTp!Ev+njFTbb9rLItvjb`M&N#t+aoUDA|6G1`Rx#$A)aiWPtr~bz*9&IcSyaA znf(f4b3|r>AUK!3aUpxA8A4rcYMIt;&Sjm~HhIGG$ws$j{9%)|;sdKa_p}}lPPp6z zx*EybR};J2*8IzK7eS(}kS^o8hGu}ih-d0qcYsEgrPSr8ixd10t>M+TEh&b@vf+QuoBV_bG4SP4oYzY{MdmMwh%7GrK z_G%j=f~`45CP${F7JYkg?Awv*T*5(64ufYXQw^&TU8R3&dxGWahDgpm1nP8wym3n} zGqnNMJ?<=tKNHCK>SeVHRAkh+$fzE-lcH2kT{C{O5P*|6p7x$x@nMHoETmeBt#}Cf z)e++@L=Sg+&%-n!ySwx6GQhyUt&)U+TCtM+V^L-JpG7T;+N|bWk5`awPyNy)zFWHh z4+k9mZt+hg+WioJ>jJ1|e^mdsj-%D!aUFP3y^>@Rc4Q8VW#C~eZwhetot@=3`f>{WJo2}r zUteW5XNX|2s3u@H*OHQ&69xOhdO`b_SQRaF56w5zX22d|Eq$m z2%qTG8-Ty-*vuRWxZl$rsVW|}dry40RE|O9gQ3oodSAZ0bpPQ@-=h+Is$zipmysz2d9w-Sd>HD=Mv%hh zmAAjCl^4Z9sm!PLo1O^k{WaM*1PeEGC~Hx;v(nY|U)mZPn$-y}q%&b5ZMThvvI|CV zQ;#L+Px|;uTOSq0X)705P})lT7I_-3Um}xX)ql%X;re2~0_c6RFDqJQ6EU=6mkHk7OhZ=BpF@7qStlpUs|45G@}aOw0v=9b?Bt}1lHDReC%I%5=tZjB|@ zzaaO04wNUTM-~2iEhw;+{?DR!?rXZ^HP*`)$%AC4A4K3&`XZnBm#X};Y zOEY#%FTOe|E&=RHDY(-M$zaI;=Iw+F`-f!O!6g+bRyolU7w_AUh7!H8<`wD6 zW^H7vGtM~r`7VPt+W^Mqpl91N?%(=mJ-nP3ZxWFW#c#g|^Mde3B^rs^MVxW9r>#pw zfFz^;qHyBA{BG1?np=ATu`f&5b6$6XGFA6~IBWA0LKK^M9Vqe0yR${ixqgxF&NNee z)|}vA!373bG8R&VoA214w&bpP_RYsTMf}8t=NyV|Gfi16Zh7S|D~+kdLkM^s--{ zjuaEg`{NH~USivsSG{zUPqQC43k#%9cd^Ro7sb|d6g|1u_e>vu5=i}0!@qkhDsZxm zM++tj)Lmo%ucM^ka=Sio58qNZp9O;s0?N&0=<=F&yEgvjrJnJfgU@%aXldT`+7uF9 zi(-}7V`6g-gvqEuceX@@H8^eJFt`d(Znx z7p4>Z`Fl;viC9$Ve=9fIYaQA7@oV#C{^+QDU|Mqz!`sGD;7=|iT&K~RDt7BvuLAeu zrz)y;Ywp!sXusBw^eT0Ahs2s4ms5}IQxjotrDTM#h%VRIRG8J*6-b{^K54!Hze{^LMuuyGu30NjM<2qXZeI9w}w z2SG-1(4pj-fkhdh7Tb2`YU>|!xhn`f_)lGt@#$@oMkRcpZV zyDZ%+j$)8`#59i_nk>%Kz~290L}cU@UWcGvi@H6~ zX!K}ZxrldOXl80&b%18mL1wyKSP3~%iF5XYS|152-(vPY_FyD_C*07Hm2djikZ4{G(h4qfEfhb^`#&kl)z*;qi`TEk4tRx{;#EL4KM2`!+~< zam53C4Lu4Zno?!%nBI8(z~ua~$G_6{{bGIb7{%(W!|E>VrQ*;4ojsGQWdG{3+)uhy zryMv_R>>^n=6wQh(&?>5m*1OS?xVRBNf&Puv|lS1g-|+8=qL?(d9f(kkvW^W(I;8N z9i6I#muX>S--#|_*3L!Ib^2UOTQlP}E4)M8|DdcWjCVa&CZA;_0MRy(S=cH&Q0ayG zOdrb3>G65>=}53*{sc5B|X4O zeU~(4Bhj(G8J)C*ELwzoowYDI#G`kq2GgJbU8Px($LQ>`!E9p+ylh@OM7&z$vN`;& z>3Sc;wRm~G9itLFjmJR{sYK7!x4SpAEZ1t>4t5A5s_cek#tsH|@Szrx?ZxNf6$D1| zO4AFdw&_LFp|p1f`biTx=;q@+2HGnRuy&Q%o%gjy@M({%gOn$T&mPkT`o)xm>FSW< zxG4KilE{Im2g7^%_>y9>A}?}2sisXg&^od4JRKuMe@Q`qz$xRpssJsO*XS&S-1#%p zFz@chz#UV)HU>x3UtdYe;@isE{;i4nuxU!dDG;7ZIAonX{p}}5(y+L62)6gtij^?i zq9HJD4t%^qLDbNYiQaJsScOnEbsxU87&E^kxvYhGKR!!H76y=S;4#U0c!)cFOpIyc z(C7b`Pd?&}Pf!29TU%d3m~Djr^_MU$DSDg-tR|CmBzNj$@ + +install: liblz4 liblz4.pc + @install -d -m 755 $(DESTDIR)$(LIBDIR)/pkgconfig/ $(DESTDIR)$(INCLUDEDIR)/ + @install -m 755 liblz4.$(SHARED_EXT_VER) $(DESTDIR)$(LIBDIR)/liblz4.$(SHARED_EXT_VER) + @cp -a liblz4.$(SHARED_EXT_MAJOR) $(DESTDIR)$(LIBDIR) + @cp -a liblz4.$(SHARED_EXT) $(DESTDIR)$(LIBDIR) + @cp -a liblz4.pc $(DESTDIR)$(LIBDIR)/pkgconfig/ + @install -m 644 liblz4.a $(DESTDIR)$(LIBDIR)/liblz4.a + @install -m 644 lz4.h $(DESTDIR)$(INCLUDEDIR)/lz4.h + @install -m 644 lz4hc.h $(DESTDIR)$(INCLUDEDIR)/lz4hc.h + @install -m 644 lz4frame.h $(DESTDIR)$(INCLUDEDIR)/lz4frame.h + @echo lz4 static and shared library installed + +uninstall: + @rm -f $(DESTDIR)$(LIBDIR)/liblz4.$(SHARED_EXT) + @rm -f $(DESTDIR)$(LIBDIR)/liblz4.$(SHARED_EXT_MAJOR) + @rm -f $(DESTDIR)$(LIBDIR)/pkgconfig/liblz4.pc + @[ -x $(DESTDIR)$(LIBDIR)/liblz4.$(SHARED_EXT_VER) ] && rm -f $(DESTDIR)$(LIBDIR)/liblz4.$(SHARED_EXT_VER) + @[ -f $(DESTDIR)$(LIBDIR)/liblz4.a ] && rm -f $(DESTDIR)$(LIBDIR)/liblz4.a + @[ -f $(DESTDIR)$(INCLUDEDIR)/lz4.h ] && rm -f $(DESTDIR)$(INCLUDEDIR)/lz4.h + @[ -f $(DESTDIR)$(INCLUDEDIR)/lz4hc.h ] && rm -f $(DESTDIR)$(INCLUDEDIR)/lz4hc.h + @echo lz4 libraries successfully uninstalled + +endif diff --git a/lib/liblz4.pc.in b/lib/liblz4.pc.in new file mode 100644 index 0000000000..0d05152f6b --- /dev/null +++ b/lib/liblz4.pc.in @@ -0,0 +1,14 @@ +# LZ4 - Fast LZ compression algorithm +# Copyright (C) 2011-2014, Yann Collet. +# BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + +prefix=@PREFIX@ +libdir=@LIBDIR@ +includedir=@INCLUDEDIR@ + +Name: lz4 +Description: fast lossless compression algorithm library +URL: http://code.google.com/p/lz4/ +Version: @VERSION@ +Libs: -L@LIBDIR@ -llz4 +Cflags: -I@INCLUDEDIR@ diff --git a/lib/lz4.c b/lib/lz4.c new file mode 100644 index 0000000000..ed928ced3f --- /dev/null +++ b/lib/lz4.c @@ -0,0 +1,1367 @@ +/* + LZ4 - Fast LZ compression algorithm + Copyright (C) 2011-2015, Yann Collet. + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - LZ4 source repository : http://code.google.com/p/lz4 + - LZ4 source mirror : https://github.com/Cyan4973/lz4 + - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c +*/ + + +/************************************** + Tuning parameters +**************************************/ +/* + * HEAPMODE : + * Select how default compression functions will allocate memory for their hash table, + * in memory stack (0:default, fastest), or in memory heap (1:requires malloc()). + */ +#define HEAPMODE 0 + +/* + * CPU_HAS_EFFICIENT_UNALIGNED_MEMORY_ACCESS : + * By default, the source code expects the compiler to correctly optimize + * 4-bytes and 8-bytes read on architectures able to handle it efficiently. + * This is not always the case. In some circumstances (ARM notably), + * the compiler will issue cautious code even when target is able to correctly handle unaligned memory accesses. + * + * You can force the compiler to use unaligned memory access by uncommenting the line below. + * One of the below scenarios will happen : + * 1 - Your target CPU correctly handle unaligned access, and was not well optimized by compiler (good case). + * You will witness large performance improvements (+50% and up). + * Keep the line uncommented and send a word to upstream (https://groups.google.com/forum/#!forum/lz4c) + * The goal is to automatically detect such situations by adding your target CPU within an exception list. + * 2 - Your target CPU correctly handle unaligned access, and was already already optimized by compiler + * No change will be experienced. + * 3 - Your target CPU inefficiently handle unaligned access. + * You will experience a performance loss. Comment back the line. + * 4 - Your target CPU does not handle unaligned access. + * Program will crash. + * If uncommenting results in better performance (case 1) + * please report your configuration to upstream (https://groups.google.com/forum/#!forum/lz4c) + * An automatic detection macro will be added to match your case within future versions of the library. + */ +/* #define CPU_HAS_EFFICIENT_UNALIGNED_MEMORY_ACCESS 1 */ + + +/************************************** + CPU Feature Detection +**************************************/ +/* + * Automated efficient unaligned memory access detection + * Based on known hardware architectures + * This list will be updated thanks to feedbacks + */ +#if defined(CPU_HAS_EFFICIENT_UNALIGNED_MEMORY_ACCESS) \ + || defined(__ARM_FEATURE_UNALIGNED) \ + || defined(__i386__) || defined(__x86_64__) \ + || defined(_M_IX86) || defined(_M_X64) \ + || defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_8__) \ + || (defined(_M_ARM) && (_M_ARM >= 7)) +# define LZ4_UNALIGNED_ACCESS 1 +#else +# define LZ4_UNALIGNED_ACCESS 0 +#endif + +/* + * LZ4_FORCE_SW_BITCOUNT + * Define this parameter if your target system or compiler does not support hardware bit count + */ +#if defined(_MSC_VER) && defined(_WIN32_WCE) /* Visual Studio for Windows CE does not support Hardware bit count */ +# define LZ4_FORCE_SW_BITCOUNT +#endif + + +/************************************** + Compiler Options +**************************************/ +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */ +/* "restrict" is a known keyword */ +#else +# define restrict /* Disable restrict */ +#endif + +#ifdef _MSC_VER /* Visual Studio */ +# define FORCE_INLINE static __forceinline +# include +# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ +# pragma warning(disable : 4293) /* disable: C4293: too large shift (32-bits) */ +#else +# if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */ +# ifdef __GNUC__ +# define FORCE_INLINE static inline __attribute__((always_inline)) +# else +# define FORCE_INLINE static inline +# endif +# else +# define FORCE_INLINE static +# endif /* __STDC_VERSION__ */ +#endif /* _MSC_VER */ + +#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) + +#if (GCC_VERSION >= 302) || (__INTEL_COMPILER >= 800) || defined(__clang__) +# define expect(expr,value) (__builtin_expect ((expr),(value)) ) +#else +# define expect(expr,value) (expr) +#endif + +#define likely(expr) expect((expr) != 0, 1) +#define unlikely(expr) expect((expr) != 0, 0) + + +/************************************** + Memory routines +**************************************/ +#include /* malloc, calloc, free */ +#define ALLOCATOR(n,s) calloc(n,s) +#define FREEMEM free +#include /* memset, memcpy */ +#define MEM_INIT memset + + +/************************************** + Includes +**************************************/ +#include "lz4.h" + + +/************************************** + Basic Types +**************************************/ +#if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */ +# include + typedef uint8_t BYTE; + typedef uint16_t U16; + typedef uint32_t U32; + typedef int32_t S32; + typedef uint64_t U64; +#else + typedef unsigned char BYTE; + typedef unsigned short U16; + typedef unsigned int U32; + typedef signed int S32; + typedef unsigned long long U64; +#endif + + +/************************************** + Reading and writing into memory +**************************************/ +#define STEPSIZE sizeof(size_t) + +static unsigned LZ4_64bits(void) { return sizeof(void*)==8; } + +static unsigned LZ4_isLittleEndian(void) +{ + const union { U32 i; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */ + return one.c[0]; +} + + +static U16 LZ4_readLE16(const void* memPtr) +{ + if ((LZ4_UNALIGNED_ACCESS) && (LZ4_isLittleEndian())) + return *(U16*)memPtr; + else + { + const BYTE* p = memPtr; + return (U16)((U16)p[0] + (p[1]<<8)); + } +} + +static void LZ4_writeLE16(void* memPtr, U16 value) +{ + if ((LZ4_UNALIGNED_ACCESS) && (LZ4_isLittleEndian())) + { + *(U16*)memPtr = value; + return; + } + else + { + BYTE* p = memPtr; + p[0] = (BYTE) value; + p[1] = (BYTE)(value>>8); + } +} + + +static U16 LZ4_read16(const void* memPtr) +{ + if (LZ4_UNALIGNED_ACCESS) + return *(U16*)memPtr; + else + { + U16 val16; + memcpy(&val16, memPtr, 2); + return val16; + } +} + +static U32 LZ4_read32(const void* memPtr) +{ + if (LZ4_UNALIGNED_ACCESS) + return *(U32*)memPtr; + else + { + U32 val32; + memcpy(&val32, memPtr, 4); + return val32; + } +} + +static U64 LZ4_read64(const void* memPtr) +{ + if (LZ4_UNALIGNED_ACCESS) + return *(U64*)memPtr; + else + { + U64 val64; + memcpy(&val64, memPtr, 8); + return val64; + } +} + +static size_t LZ4_read_ARCH(const void* p) +{ + if (LZ4_64bits()) + return (size_t)LZ4_read64(p); + else + return (size_t)LZ4_read32(p); +} + + +static void LZ4_copy4(void* dstPtr, const void* srcPtr) +{ + if (LZ4_UNALIGNED_ACCESS) + { + *(U32*)dstPtr = *(U32*)srcPtr; + return; + } + memcpy(dstPtr, srcPtr, 4); +} + +static void LZ4_copy8(void* dstPtr, const void* srcPtr) +{ +#if GCC_VERSION!=409 /* disabled on GCC 4.9, as it generates invalid opcode (crash) */ + if (LZ4_UNALIGNED_ACCESS) + { + if (LZ4_64bits()) + *(U64*)dstPtr = *(U64*)srcPtr; + else + ((U32*)dstPtr)[0] = ((U32*)srcPtr)[0], + ((U32*)dstPtr)[1] = ((U32*)srcPtr)[1]; + return; + } +#endif + memcpy(dstPtr, srcPtr, 8); +} + +/* customized version of memcpy, which may overwrite up to 7 bytes beyond dstEnd */ +static void LZ4_wildCopy(void* dstPtr, const void* srcPtr, void* dstEnd) +{ + BYTE* d = dstPtr; + const BYTE* s = srcPtr; + BYTE* e = dstEnd; + do { LZ4_copy8(d,s); d+=8; s+=8; } while (d>3); +# elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT) + return (__builtin_ctzll((U64)val) >> 3); +# else + static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7, 0, 2, 3, 6, 1, 5, 3, 5, 1, 3, 4, 4, 2, 5, 6, 7, 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5, 5, 3, 4, 5, 6, 7, 1, 2, 4, 6, 4, 4, 5, 7, 2, 6, 5, 7, 6, 7, 7 }; + return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58]; +# endif + } + else /* 32 bits */ + { +# if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) + unsigned long r; + _BitScanForward( &r, (U32)val ); + return (int)(r>>3); +# elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT) + return (__builtin_ctz((U32)val) >> 3); +# else + static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0, 3, 2, 2, 1, 3, 2, 0, 1, 3, 3, 1, 2, 2, 2, 2, 0, 3, 1, 2, 0, 1, 0, 1, 1 }; + return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27]; +# endif + } + } + else /* Big Endian CPU */ + { + if (LZ4_64bits()) + { +# if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT) + unsigned long r = 0; + _BitScanReverse64( &r, val ); + return (unsigned)(r>>3); +# elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT) + return (__builtin_clzll(val) >> 3); +# else + unsigned r; + if (!(val>>32)) { r=4; } else { r=0; val>>=32; } + if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; } + r += (!val); + return r; +# endif + } + else /* 32 bits */ + { +# if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) + unsigned long r = 0; + _BitScanReverse( &r, (unsigned long)val ); + return (unsigned)(r>>3); +# elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT) + return (__builtin_clz(val) >> 3); +# else + unsigned r; + if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; } + r += (!val); + return r; +# endif + } + } +} + +static unsigned LZ4_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* pInLimit) +{ + const BYTE* const pStart = pIn; + + while (likely(pIn compression run slower on incompressible data */ + + +/************************************** + Local Utils +**************************************/ +int LZ4_versionNumber (void) { return LZ4_VERSION_NUMBER; } +int LZ4_compressBound(int isize) { return LZ4_COMPRESSBOUND(isize); } + + +/************************************** + Local Structures and types +**************************************/ +typedef struct { + U32 hashTable[HASH_SIZE_U32]; + U32 currentOffset; + U32 initCheck; + const BYTE* dictionary; + const BYTE* bufferStart; + U32 dictSize; +} LZ4_stream_t_internal; + +typedef enum { notLimited = 0, limitedOutput = 1 } limitedOutput_directive; +typedef enum { byPtr, byU32, byU16 } tableType_t; + +typedef enum { noDict = 0, withPrefix64k, usingExtDict } dict_directive; +typedef enum { noDictIssue = 0, dictSmall } dictIssue_directive; + +typedef enum { endOnOutputSize = 0, endOnInputSize = 1 } endCondition_directive; +typedef enum { full = 0, partial = 1 } earlyEnd_directive; + + + +/******************************** + Compression functions +********************************/ + +static U32 LZ4_hashSequence(U32 sequence, tableType_t tableType) +{ + if (tableType == byU16) + return (((sequence) * 2654435761U) >> ((MINMATCH*8)-(LZ4_HASHLOG+1))); + else + return (((sequence) * 2654435761U) >> ((MINMATCH*8)-LZ4_HASHLOG)); +} + +static U32 LZ4_hashPosition(const BYTE* p, tableType_t tableType) { return LZ4_hashSequence(LZ4_read32(p), tableType); } + +static void LZ4_putPositionOnHash(const BYTE* p, U32 h, void* tableBase, tableType_t tableType, const BYTE* srcBase) +{ + switch (tableType) + { + case byPtr: { const BYTE** hashTable = (const BYTE**)tableBase; hashTable[h] = p; return; } + case byU32: { U32* hashTable = (U32*) tableBase; hashTable[h] = (U32)(p-srcBase); return; } + case byU16: { U16* hashTable = (U16*) tableBase; hashTable[h] = (U16)(p-srcBase); return; } + } +} + +static void LZ4_putPosition(const BYTE* p, void* tableBase, tableType_t tableType, const BYTE* srcBase) +{ + U32 h = LZ4_hashPosition(p, tableType); + LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase); +} + +static const BYTE* LZ4_getPositionOnHash(U32 h, void* tableBase, tableType_t tableType, const BYTE* srcBase) +{ + if (tableType == byPtr) { const BYTE** hashTable = (const BYTE**) tableBase; return hashTable[h]; } + if (tableType == byU32) { U32* hashTable = (U32*) tableBase; return hashTable[h] + srcBase; } + { U16* hashTable = (U16*) tableBase; return hashTable[h] + srcBase; } /* default, to ensure a return */ +} + +static const BYTE* LZ4_getPosition(const BYTE* p, void* tableBase, tableType_t tableType, const BYTE* srcBase) +{ + U32 h = LZ4_hashPosition(p, tableType); + return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase); +} + +static int LZ4_compress_generic( + void* ctx, + const char* source, + char* dest, + int inputSize, + int maxOutputSize, + limitedOutput_directive outputLimited, + tableType_t tableType, + dict_directive dict, + dictIssue_directive dictIssue) +{ + LZ4_stream_t_internal* const dictPtr = (LZ4_stream_t_internal*)ctx; + + const BYTE* ip = (const BYTE*) source; + const BYTE* base; + const BYTE* lowLimit; + const BYTE* const lowRefLimit = ip - dictPtr->dictSize; + const BYTE* const dictionary = dictPtr->dictionary; + const BYTE* const dictEnd = dictionary + dictPtr->dictSize; + const size_t dictDelta = dictEnd - (const BYTE*)source; + const BYTE* anchor = (const BYTE*) source; + const BYTE* const iend = ip + inputSize; + const BYTE* const mflimit = iend - MFLIMIT; + const BYTE* const matchlimit = iend - LASTLITERALS; + + BYTE* op = (BYTE*) dest; + BYTE* const olimit = op + maxOutputSize; + + U32 forwardH; + size_t refDelta=0; + + /* Init conditions */ + if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) return 0; /* Unsupported input size, too large (or negative) */ + switch(dict) + { + case noDict: + default: + base = (const BYTE*)source; + lowLimit = (const BYTE*)source; + break; + case withPrefix64k: + base = (const BYTE*)source - dictPtr->currentOffset; + lowLimit = (const BYTE*)source - dictPtr->dictSize; + break; + case usingExtDict: + base = (const BYTE*)source - dictPtr->currentOffset; + lowLimit = (const BYTE*)source; + break; + } + if ((tableType == byU16) && (inputSize>=LZ4_64Klimit)) return 0; /* Size too large (not within 64K limit) */ + if (inputSize> LZ4_skipTrigger; + + if (unlikely(forwardIp > mflimit)) goto _last_literals; + + match = LZ4_getPositionOnHash(h, ctx, tableType, base); + if (dict==usingExtDict) + { + if (match<(const BYTE*)source) + { + refDelta = dictDelta; + lowLimit = dictionary; + } + else + { + refDelta = 0; + lowLimit = (const BYTE*)source; + } + } + forwardH = LZ4_hashPosition(forwardIp, tableType); + LZ4_putPositionOnHash(ip, h, ctx, tableType, base); + + } while ( ((dictIssue==dictSmall) ? (match < lowRefLimit) : 0) + || ((tableType==byU16) ? 0 : (match + MAX_DISTANCE < ip)) + || (LZ4_read32(match+refDelta) != LZ4_read32(ip)) ); + } + + /* Catch up */ + while ((ip>anchor) && (match+refDelta > lowLimit) && (unlikely(ip[-1]==match[refDelta-1]))) { ip--; match--; } + + { + /* Encode Literal length */ + unsigned litLength = (unsigned)(ip - anchor); + token = op++; + if ((outputLimited) && (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength/255) > olimit))) + return 0; /* Check output limit */ + if (litLength>=RUN_MASK) + { + int len = (int)litLength-RUN_MASK; + *token=(RUN_MASK<= 255 ; len-=255) *op++ = 255; + *op++ = (BYTE)len; + } + else *token = (BYTE)(litLength< matchlimit) limit = matchlimit; + matchLength = LZ4_count(ip+MINMATCH, match+MINMATCH, limit); + ip += MINMATCH + matchLength; + if (ip==limit) + { + unsigned more = LZ4_count(ip, (const BYTE*)source, matchlimit); + matchLength += more; + ip += more; + } + } + else + { + matchLength = LZ4_count(ip+MINMATCH, match+MINMATCH, matchlimit); + ip += MINMATCH + matchLength; + } + + if ((outputLimited) && (unlikely(op + (1 + LASTLITERALS) + (matchLength>>8) > olimit))) + return 0; /* Check output limit */ + if (matchLength>=ML_MASK) + { + *token += ML_MASK; + matchLength -= ML_MASK; + for (; matchLength >= 510 ; matchLength-=510) { *op++ = 255; *op++ = 255; } + if (matchLength >= 255) { matchLength-=255; *op++ = 255; } + *op++ = (BYTE)matchLength; + } + else *token += (BYTE)(matchLength); + } + + anchor = ip; + + /* Test end of chunk */ + if (ip > mflimit) break; + + /* Fill table */ + LZ4_putPosition(ip-2, ctx, tableType, base); + + /* Test next position */ + match = LZ4_getPosition(ip, ctx, tableType, base); + if (dict==usingExtDict) + { + if (match<(const BYTE*)source) + { + refDelta = dictDelta; + lowLimit = dictionary; + } + else + { + refDelta = 0; + lowLimit = (const BYTE*)source; + } + } + LZ4_putPosition(ip, ctx, tableType, base); + if ( ((dictIssue==dictSmall) ? (match>=lowRefLimit) : 1) + && (match+MAX_DISTANCE>=ip) + && (LZ4_read32(match+refDelta)==LZ4_read32(ip)) ) + { token=op++; *token=0; goto _next_match; } + + /* Prepare next loop */ + forwardH = LZ4_hashPosition(++ip, tableType); + } + +_last_literals: + /* Encode Last Literals */ + { + int lastRun = (int)(iend - anchor); + if ((outputLimited) && (((char*)op - dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize)) + return 0; /* Check output limit */ + if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<= 255 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; } + else *op++ = (BYTE)(lastRun<= sizeof(LZ4_stream_t_internal)); /* A compilation error here means LZ4_STREAMSIZE is not large enough */ + LZ4_resetStream(lz4s); + return lz4s; +} + +int LZ4_freeStream (LZ4_stream_t* LZ4_stream) +{ + FREEMEM(LZ4_stream); + return (0); +} + + +int LZ4_loadDict (LZ4_stream_t* LZ4_dict, const char* dictionary, int dictSize) +{ + LZ4_stream_t_internal* dict = (LZ4_stream_t_internal*) LZ4_dict; + const BYTE* p = (const BYTE*)dictionary; + const BYTE* const dictEnd = p + dictSize; + const BYTE* base; + + if (dict->initCheck) LZ4_resetStream(LZ4_dict); /* Uninitialized structure detected */ + + if (dictSize < MINMATCH) + { + dict->dictionary = NULL; + dict->dictSize = 0; + return 0; + } + + if (p <= dictEnd - 64 KB) p = dictEnd - 64 KB; + base = p - dict->currentOffset; + dict->dictionary = p; + dict->dictSize = (U32)(dictEnd - p); + dict->currentOffset += dict->dictSize; + + while (p <= dictEnd-MINMATCH) + { + LZ4_putPosition(p, dict, byU32, base); + p+=3; + } + + return dict->dictSize; +} + + +static void LZ4_renormDictT(LZ4_stream_t_internal* LZ4_dict, const BYTE* src) +{ + if ((LZ4_dict->currentOffset > 0x80000000) || + ((size_t)LZ4_dict->currentOffset > (size_t)src)) /* address space overflow */ + { + /* rescale hash table */ + U32 delta = LZ4_dict->currentOffset - 64 KB; + const BYTE* dictEnd = LZ4_dict->dictionary + LZ4_dict->dictSize; + int i; + for (i=0; ihashTable[i] < delta) LZ4_dict->hashTable[i]=0; + else LZ4_dict->hashTable[i] -= delta; + } + LZ4_dict->currentOffset = 64 KB; + if (LZ4_dict->dictSize > 64 KB) LZ4_dict->dictSize = 64 KB; + LZ4_dict->dictionary = dictEnd - LZ4_dict->dictSize; + } +} + + +FORCE_INLINE int LZ4_compress_continue_generic (void* LZ4_stream, const char* source, char* dest, int inputSize, + int maxOutputSize, limitedOutput_directive limit) +{ + LZ4_stream_t_internal* streamPtr = (LZ4_stream_t_internal*)LZ4_stream; + const BYTE* const dictEnd = streamPtr->dictionary + streamPtr->dictSize; + + const BYTE* smallest = (const BYTE*) source; + if (streamPtr->initCheck) return 0; /* Uninitialized structure detected */ + if ((streamPtr->dictSize>0) && (smallest>dictEnd)) smallest = dictEnd; + LZ4_renormDictT(streamPtr, smallest); + + /* Check overlapping input/dictionary space */ + { + const BYTE* sourceEnd = (const BYTE*) source + inputSize; + if ((sourceEnd > streamPtr->dictionary) && (sourceEnd < dictEnd)) + { + streamPtr->dictSize = (U32)(dictEnd - sourceEnd); + if (streamPtr->dictSize > 64 KB) streamPtr->dictSize = 64 KB; + if (streamPtr->dictSize < 4) streamPtr->dictSize = 0; + streamPtr->dictionary = dictEnd - streamPtr->dictSize; + } + } + + /* prefix mode : source data follows dictionary */ + if (dictEnd == (const BYTE*)source) + { + int result; + if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) + result = LZ4_compress_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limit, byU32, withPrefix64k, dictSmall); + else + result = LZ4_compress_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limit, byU32, withPrefix64k, noDictIssue); + streamPtr->dictSize += (U32)inputSize; + streamPtr->currentOffset += (U32)inputSize; + return result; + } + + /* external dictionary mode */ + { + int result; + if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) + result = LZ4_compress_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limit, byU32, usingExtDict, dictSmall); + else + result = LZ4_compress_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limit, byU32, usingExtDict, noDictIssue); + streamPtr->dictionary = (const BYTE*)source; + streamPtr->dictSize = (U32)inputSize; + streamPtr->currentOffset += (U32)inputSize; + return result; + } +} + +int LZ4_compress_continue (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize) +{ + return LZ4_compress_continue_generic(LZ4_stream, source, dest, inputSize, 0, notLimited); +} + +int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize, int maxOutputSize) +{ + return LZ4_compress_continue_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limitedOutput); +} + + +/* Hidden debug function, to force separate dictionary mode */ +int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_dict, const char* source, char* dest, int inputSize) +{ + LZ4_stream_t_internal* streamPtr = (LZ4_stream_t_internal*)LZ4_dict; + int result; + const BYTE* const dictEnd = streamPtr->dictionary + streamPtr->dictSize; + + const BYTE* smallest = dictEnd; + if (smallest > (const BYTE*) source) smallest = (const BYTE*) source; + LZ4_renormDictT((LZ4_stream_t_internal*)LZ4_dict, smallest); + + result = LZ4_compress_generic(LZ4_dict, source, dest, inputSize, 0, notLimited, byU32, usingExtDict, noDictIssue); + + streamPtr->dictionary = (const BYTE*)source; + streamPtr->dictSize = (U32)inputSize; + streamPtr->currentOffset += (U32)inputSize; + + return result; +} + + +int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize) +{ + LZ4_stream_t_internal* dict = (LZ4_stream_t_internal*) LZ4_dict; + const BYTE* previousDictEnd = dict->dictionary + dict->dictSize; + + if ((U32)dictSize > 64 KB) dictSize = 64 KB; /* useless to define a dictionary > 64 KB */ + if ((U32)dictSize > dict->dictSize) dictSize = dict->dictSize; + + memmove(safeBuffer, previousDictEnd - dictSize, dictSize); + + dict->dictionary = (const BYTE*)safeBuffer; + dict->dictSize = (U32)dictSize; + + return dictSize; +} + + + +/**************************** + Decompression functions +****************************/ +/* + * This generic decompression function cover all use cases. + * It shall be instantiated several times, using different sets of directives + * Note that it is essential this generic function is really inlined, + * in order to remove useless branches during compilation optimization. + */ +FORCE_INLINE int LZ4_decompress_generic( + const char* const source, + char* const dest, + int inputSize, + int outputSize, /* If endOnInput==endOnInputSize, this value is the max size of Output Buffer. */ + + int endOnInput, /* endOnOutputSize, endOnInputSize */ + int partialDecoding, /* full, partial */ + int targetOutputSize, /* only used if partialDecoding==partial */ + int dict, /* noDict, withPrefix64k, usingExtDict */ + const BYTE* const lowPrefix, /* == dest if dict == noDict */ + const BYTE* const dictStart, /* only if dict==usingExtDict */ + const size_t dictSize /* note : = 0 if noDict */ + ) +{ + /* Local Variables */ + const BYTE* restrict ip = (const BYTE*) source; + const BYTE* const iend = ip + inputSize; + + BYTE* op = (BYTE*) dest; + BYTE* const oend = op + outputSize; + BYTE* cpy; + BYTE* oexit = op + targetOutputSize; + const BYTE* const lowLimit = lowPrefix - dictSize; + + const BYTE* const dictEnd = (const BYTE*)dictStart + dictSize; + const size_t dec32table[] = {4, 1, 2, 1, 4, 4, 4, 4}; + const size_t dec64table[] = {0, 0, 0, (size_t)-1, 0, 1, 2, 3}; + + const int safeDecode = (endOnInput==endOnInputSize); + const int checkOffset = ((safeDecode) && (dictSize < (int)(64 KB))); + + + /* Special cases */ + if ((partialDecoding) && (oexit> oend-MFLIMIT)) oexit = oend-MFLIMIT; /* targetOutputSize too high => decode everything */ + if ((endOnInput) && (unlikely(outputSize==0))) return ((inputSize==1) && (*ip==0)) ? 0 : -1; /* Empty output buffer */ + if ((!endOnInput) && (unlikely(outputSize==0))) return (*ip==0?1:-1); + + + /* Main Loop */ + while (1) + { + unsigned token; + size_t length; + const BYTE* match; + + /* get literal length */ + token = *ip++; + if ((length=(token>>ML_BITS)) == RUN_MASK) + { + unsigned s; + do + { + s = *ip++; + length += s; + } + while (likely((endOnInput)?ip(partialDecoding?oexit:oend-MFLIMIT)) || (ip+length>iend-(2+1+LASTLITERALS))) ) + || ((!endOnInput) && (cpy>oend-COPYLENGTH))) + { + if (partialDecoding) + { + if (cpy > oend) goto _output_error; /* Error : write attempt beyond end of output buffer */ + if ((endOnInput) && (ip+length > iend)) goto _output_error; /* Error : read attempt beyond end of input buffer */ + } + else + { + if ((!endOnInput) && (cpy != oend)) goto _output_error; /* Error : block decoding must stop exactly there */ + if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) goto _output_error; /* Error : input must be consumed */ + } + memcpy(op, ip, length); + ip += length; + op += length; + break; /* Necessarily EOF, due to parsing restrictions */ + } + LZ4_wildCopy(op, ip, cpy); + ip += length; op = cpy; + + /* get offset */ + match = cpy - LZ4_readLE16(ip); ip+=2; + if ((checkOffset) && (unlikely(match < lowLimit))) goto _output_error; /* Error : offset outside destination buffer */ + + /* get matchlength */ + length = token & ML_MASK; + if (length == ML_MASK) + { + unsigned s; + do + { + if ((endOnInput) && (ip > iend-LASTLITERALS)) goto _output_error; + s = *ip++; + length += s; + } while (s==255); + if ((safeDecode) && unlikely((size_t)(op+length)<(size_t)op)) goto _output_error; /* overflow detection */ + } + length += MINMATCH; + + /* check external dictionary */ + if ((dict==usingExtDict) && (match < lowPrefix)) + { + if (unlikely(op+length > oend-LASTLITERALS)) goto _output_error; /* doesn't respect parsing restriction */ + + if (length <= (size_t)(lowPrefix-match)) + { + /* match can be copied as a single segment from external dictionary */ + match = dictEnd - (lowPrefix-match); + memcpy(op, match, length); + op += length; + } + else + { + /* match encompass external dictionary and current segment */ + size_t copySize = (size_t)(lowPrefix-match); + memcpy(op, dictEnd - copySize, copySize); + op += copySize; + copySize = length - copySize; + if (copySize > (size_t)(op-lowPrefix)) /* overlap within current segment */ + { + BYTE* const endOfMatch = op + copySize; + const BYTE* copyFrom = lowPrefix; + while (op < endOfMatch) *op++ = *copyFrom++; + } + else + { + memcpy(op, lowPrefix, copySize); + op += copySize; + } + } + continue; + } + + /* copy repeated sequence */ + cpy = op + length; + if (unlikely((op-match)<8)) + { + const size_t dec64 = dec64table[op-match]; + op[0] = match[0]; + op[1] = match[1]; + op[2] = match[2]; + op[3] = match[3]; + match += dec32table[op-match]; + LZ4_copy4(op+4, match); + op += 8; match -= dec64; + } else { LZ4_copy8(op, match); op+=8; match+=8; } + + if (unlikely(cpy>oend-12)) + { + if (cpy > oend-LASTLITERALS) goto _output_error; /* Error : last LASTLITERALS bytes must be literals */ + if (op < oend-8) + { + LZ4_wildCopy(op, match, oend-8); + match += (oend-8) - op; + op = oend-8; + } + while (opprefixSize = (size_t) dictSize; + lz4sd->prefixEnd = (BYTE*) dictionary + dictSize; + lz4sd->externalDict = NULL; + lz4sd->extDictSize = 0; + return 1; +} + +/* +*_continue() : + These decoding functions allow decompression of multiple blocks in "streaming" mode. + Previously decoded blocks must still be available at the memory position where they were decoded. + If it's not possible, save the relevant part of decoded data into a safe buffer, + and indicate where it stands using LZ4_setStreamDecode() +*/ +int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxOutputSize) +{ + LZ4_streamDecode_t_internal* lz4sd = (LZ4_streamDecode_t_internal*) LZ4_streamDecode; + int result; + + if (lz4sd->prefixEnd == (BYTE*)dest) + { + result = LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, + endOnInputSize, full, 0, + usingExtDict, lz4sd->prefixEnd - lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize); + if (result <= 0) return result; + lz4sd->prefixSize += result; + lz4sd->prefixEnd += result; + } + else + { + lz4sd->extDictSize = lz4sd->prefixSize; + lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize; + result = LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, + endOnInputSize, full, 0, + usingExtDict, (BYTE*)dest, lz4sd->externalDict, lz4sd->extDictSize); + if (result <= 0) return result; + lz4sd->prefixSize = result; + lz4sd->prefixEnd = (BYTE*)dest + result; + } + + return result; +} + +int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int originalSize) +{ + LZ4_streamDecode_t_internal* lz4sd = (LZ4_streamDecode_t_internal*) LZ4_streamDecode; + int result; + + if (lz4sd->prefixEnd == (BYTE*)dest) + { + result = LZ4_decompress_generic(source, dest, 0, originalSize, + endOnOutputSize, full, 0, + usingExtDict, lz4sd->prefixEnd - lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize); + if (result <= 0) return result; + lz4sd->prefixSize += originalSize; + lz4sd->prefixEnd += originalSize; + } + else + { + lz4sd->extDictSize = lz4sd->prefixSize; + lz4sd->externalDict = (BYTE*)dest - lz4sd->extDictSize; + result = LZ4_decompress_generic(source, dest, 0, originalSize, + endOnOutputSize, full, 0, + usingExtDict, (BYTE*)dest, lz4sd->externalDict, lz4sd->extDictSize); + if (result <= 0) return result; + lz4sd->prefixSize = originalSize; + lz4sd->prefixEnd = (BYTE*)dest + originalSize; + } + + return result; +} + + +/* +Advanced decoding functions : +*_usingDict() : + These decoding functions work the same as "_continue" ones, + the dictionary must be explicitly provided within parameters +*/ + +FORCE_INLINE int LZ4_decompress_usingDict_generic(const char* source, char* dest, int compressedSize, int maxOutputSize, int safe, const char* dictStart, int dictSize) +{ + if (dictSize==0) + return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, noDict, (BYTE*)dest, NULL, 0); + if (dictStart+dictSize == dest) + { + if (dictSize >= (int)(64 KB - 1)) + return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, withPrefix64k, (BYTE*)dest-64 KB, NULL, 0); + return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, noDict, (BYTE*)dest-dictSize, NULL, 0); + } + return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, usingExtDict, (BYTE*)dest, (BYTE*)dictStart, dictSize); +} + +int LZ4_decompress_safe_usingDict(const char* source, char* dest, int compressedSize, int maxOutputSize, const char* dictStart, int dictSize) +{ + return LZ4_decompress_usingDict_generic(source, dest, compressedSize, maxOutputSize, 1, dictStart, dictSize); +} + +int LZ4_decompress_fast_usingDict(const char* source, char* dest, int originalSize, const char* dictStart, int dictSize) +{ + return LZ4_decompress_usingDict_generic(source, dest, 0, originalSize, 0, dictStart, dictSize); +} + +/* debug function */ +int LZ4_decompress_safe_forceExtDict(const char* source, char* dest, int compressedSize, int maxOutputSize, const char* dictStart, int dictSize) +{ + return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, endOnInputSize, full, 0, usingExtDict, (BYTE*)dest, (BYTE*)dictStart, dictSize); +} + + +/*************************************************** + Obsolete Functions +***************************************************/ +/* +These function names are deprecated and should no longer be used. +They are only provided here for compatibility with older user programs. +- LZ4_uncompress is totally equivalent to LZ4_decompress_fast +- LZ4_uncompress_unknownOutputSize is totally equivalent to LZ4_decompress_safe +*/ +int LZ4_uncompress (const char* source, char* dest, int outputSize) { return LZ4_decompress_fast(source, dest, outputSize); } +int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize) { return LZ4_decompress_safe(source, dest, isize, maxOutputSize); } + + +/* Obsolete Streaming functions */ + +int LZ4_sizeofStreamState() { return LZ4_STREAMSIZE; } + +static void LZ4_init(LZ4_stream_t_internal* lz4ds, const BYTE* base) +{ + MEM_INIT(lz4ds, 0, LZ4_STREAMSIZE); + lz4ds->bufferStart = base; +} + +int LZ4_resetStreamState(void* state, const char* inputBuffer) +{ + if ((((size_t)state) & 3) != 0) return 1; /* Error : pointer is not aligned on 4-bytes boundary */ + LZ4_init((LZ4_stream_t_internal*)state, (const BYTE*)inputBuffer); + return 0; +} + +void* LZ4_create (const char* inputBuffer) +{ + void* lz4ds = ALLOCATOR(8, LZ4_STREAMSIZE_U64); + LZ4_init ((LZ4_stream_t_internal*)lz4ds, (const BYTE*)inputBuffer); + return lz4ds; +} + +char* LZ4_slideInputBuffer (void* LZ4_Data) +{ + LZ4_stream_t_internal* ctx = (LZ4_stream_t_internal*)LZ4_Data; + int dictSize = LZ4_saveDict((LZ4_stream_t*)ctx, (char*)ctx->bufferStart, 64 KB); + return (char*)(ctx->bufferStart + dictSize); +} + +/* Obsolete compresson functions using User-allocated state */ + +int LZ4_sizeofState() { return LZ4_STREAMSIZE; } + +int LZ4_compress_withState (void* state, const char* source, char* dest, int inputSize) +{ + if (((size_t)(state)&3) != 0) return 0; /* Error : state is not aligned on 4-bytes boundary */ + MEM_INIT(state, 0, LZ4_STREAMSIZE); + + if (inputSize < LZ4_64Klimit) + return LZ4_compress_generic(state, source, dest, inputSize, 0, notLimited, byU16, noDict, noDictIssue); + else + return LZ4_compress_generic(state, source, dest, inputSize, 0, notLimited, LZ4_64bits() ? byU32 : byPtr, noDict, noDictIssue); +} + +int LZ4_compress_limitedOutput_withState (void* state, const char* source, char* dest, int inputSize, int maxOutputSize) +{ + if (((size_t)(state)&3) != 0) return 0; /* Error : state is not aligned on 4-bytes boundary */ + MEM_INIT(state, 0, LZ4_STREAMSIZE); + + if (inputSize < LZ4_64Klimit) + return LZ4_compress_generic(state, source, dest, inputSize, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue); + else + return LZ4_compress_generic(state, source, dest, inputSize, maxOutputSize, limitedOutput, LZ4_64bits() ? byU32 : byPtr, noDict, noDictIssue); +} + +/* Obsolete streaming decompression functions */ + +int LZ4_decompress_safe_withPrefix64k(const char* source, char* dest, int compressedSize, int maxOutputSize) +{ + return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, endOnInputSize, full, 0, withPrefix64k, (BYTE*)dest - 64 KB, NULL, 64 KB); +} + +int LZ4_decompress_fast_withPrefix64k(const char* source, char* dest, int originalSize) +{ + return LZ4_decompress_generic(source, dest, 0, originalSize, endOnOutputSize, full, 0, withPrefix64k, (BYTE*)dest - 64 KB, NULL, 64 KB); +} + +#endif /* LZ4_COMMONDEFS_ONLY */ + diff --git a/lib/lz4.h b/lib/lz4.h new file mode 100644 index 0000000000..7778caad3a --- /dev/null +++ b/lib/lz4.h @@ -0,0 +1,315 @@ +/* + LZ4 - Fast LZ compression algorithm + Header File + Copyright (C) 2011-2014, Yann Collet. + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - LZ4 source repository : http://code.google.com/p/lz4/ + - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c +*/ +#pragma once + +#if defined (__cplusplus) +extern "C" { +#endif + +/* + * lz4.h provides raw compression format functions, for optimal performance and integration into programs. + * If you need to generate data using an inter-operable format (respecting the framing specification), + * please use lz4frame.h instead. +*/ + +/************************************** + Version +**************************************/ +#define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */ +#define LZ4_VERSION_MINOR 5 /* for new (non-breaking) interface capabilities */ +#define LZ4_VERSION_RELEASE 0 /* for tweaks, bug-fixes, or development */ +#define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE) +int LZ4_versionNumber (void); + +/************************************** + Tuning parameter +**************************************/ +/* + * LZ4_MEMORY_USAGE : + * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) + * Increasing memory usage improves compression ratio + * Reduced memory usage can improve speed, due to cache effect + * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache + */ +#define LZ4_MEMORY_USAGE 14 + + +/************************************** + Simple Functions +**************************************/ + +int LZ4_compress (const char* source, char* dest, int sourceSize); +int LZ4_decompress_safe (const char* source, char* dest, int compressedSize, int maxDecompressedSize); + +/* +LZ4_compress() : + Compresses 'sourceSize' bytes from 'source' into 'dest'. + Destination buffer must be already allocated, + and must be sized to handle worst cases situations (input data not compressible) + Worst case size evaluation is provided by function LZ4_compressBound() + inputSize : Max supported value is LZ4_MAX_INPUT_SIZE + return : the number of bytes written in buffer dest + or 0 if the compression fails + +LZ4_decompress_safe() : + compressedSize : is obviously the source size + maxDecompressedSize : is the size of the destination buffer, which must be already allocated. + return : the number of bytes decompressed into the destination buffer (necessarily <= maxDecompressedSize) + If the destination buffer is not large enough, decoding will stop and output an error code (<0). + If the source stream is detected malformed, the function will stop decoding and return a negative result. + This function is protected against buffer overflow exploits, + and never writes outside of output buffer, nor reads outside of input buffer. + It is also protected against malicious data packets. +*/ + + +/************************************** + Advanced Functions +**************************************/ +#define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */ +#define LZ4_COMPRESSBOUND(isize) ((unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16) + +/* +LZ4_compressBound() : + Provides the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible) + This function is primarily useful for memory allocation purposes (output buffer size). + Macro LZ4_COMPRESSBOUND() is also provided for compilation-time evaluation (stack memory allocation for example). + + isize : is the input size. Max supported value is LZ4_MAX_INPUT_SIZE + return : maximum output size in a "worst case" scenario + or 0, if input size is too large ( > LZ4_MAX_INPUT_SIZE) +*/ +int LZ4_compressBound(int isize); + + +/* +LZ4_compress_limitedOutput() : + Compress 'sourceSize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'. + If it cannot achieve it, compression will stop, and result of the function will be zero. + This saves time and memory on detecting non-compressible (or barely compressible) data. + This function never writes outside of provided output buffer. + + sourceSize : Max supported value is LZ4_MAX_INPUT_VALUE + maxOutputSize : is the size of the destination buffer (which must be already allocated) + return : the number of bytes written in buffer 'dest' + or 0 if compression fails +*/ +int LZ4_compress_limitedOutput (const char* source, char* dest, int sourceSize, int maxOutputSize); + + +/* +LZ4_compress_withState() : + Same compression functions, but using an externally allocated memory space to store compression state. + Use LZ4_sizeofState() to know how much memory must be allocated, + and then, provide it as 'void* state' to compression functions. +*/ +int LZ4_sizeofState(void); +int LZ4_compress_withState (void* state, const char* source, char* dest, int inputSize); +int LZ4_compress_limitedOutput_withState (void* state, const char* source, char* dest, int inputSize, int maxOutputSize); + + +/* +LZ4_decompress_fast() : + originalSize : is the original and therefore uncompressed size + return : the number of bytes read from the source buffer (in other words, the compressed size) + If the source stream is detected malformed, the function will stop decoding and return a negative result. + Destination buffer must be already allocated. Its size must be a minimum of 'originalSize' bytes. + note : This function fully respect memory boundaries for properly formed compressed data. + It is a bit faster than LZ4_decompress_safe(). + However, it does not provide any protection against intentionally modified data stream (malicious input). + Use this function in trusted environment only (data to decode comes from a trusted source). +*/ +int LZ4_decompress_fast (const char* source, char* dest, int originalSize); + + +/* +LZ4_decompress_safe_partial() : + This function decompress a compressed block of size 'compressedSize' at position 'source' + into destination buffer 'dest' of size 'maxDecompressedSize'. + The function tries to stop decompressing operation as soon as 'targetOutputSize' has been reached, + reducing decompression time. + return : the number of bytes decoded in the destination buffer (necessarily <= maxDecompressedSize) + Note : this number can be < 'targetOutputSize' should the compressed block to decode be smaller. + Always control how many bytes were decoded. + If the source stream is detected malformed, the function will stop decoding and return a negative result. + This function never writes outside of output buffer, and never reads outside of input buffer. It is therefore protected against malicious data packets +*/ +int LZ4_decompress_safe_partial (const char* source, char* dest, int compressedSize, int targetOutputSize, int maxDecompressedSize); + + +/*********************************************** + Streaming Compression Functions +***********************************************/ + +#define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4) +#define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(long long)) +/* + * LZ4_stream_t + * information structure to track an LZ4 stream. + * important : init this structure content before first use ! + * note : only allocated directly the structure if you are statically linking LZ4 + * If you are using liblz4 as a DLL, please use below construction methods instead. + */ +typedef struct { long long table[LZ4_STREAMSIZE_U64]; } LZ4_stream_t; + +/* + * LZ4_resetStream + * Use this function to init an allocated LZ4_stream_t structure + */ +void LZ4_resetStream (LZ4_stream_t* LZ4_streamPtr); + +/* + * LZ4_createStream will allocate and initialize an LZ4_stream_t structure + * LZ4_freeStream releases its memory. + * In the context of a DLL (liblz4), please use these methods rather than the static struct. + * They are more future proof, in case of a change of LZ4_stream_t size. + */ +LZ4_stream_t* LZ4_createStream(void); +int LZ4_freeStream (LZ4_stream_t* LZ4_streamPtr); + +/* + * LZ4_loadDict + * Use this function to load a static dictionary into LZ4_stream. + * Any previous data will be forgotten, only 'dictionary' will remain in memory. + * Loading a size of 0 is allowed. + * Return : dictionary size, in bytes (necessarily <= 64 KB) + */ +int LZ4_loadDict (LZ4_stream_t* LZ4_streamPtr, const char* dictionary, int dictSize); + +/* + * LZ4_compress_continue + * Compress data block 'source', using blocks compressed before as dictionary to improve compression ratio + * Previous data blocks are assumed to still be present at their previous location. + */ +int LZ4_compress_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize); + +/* + * LZ4_compress_limitedOutput_continue + * Same as before, but also specify a maximum target compressed size (maxOutputSize) + * If objective cannot be met, compression exits, and returns a zero. + */ +int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize, int maxOutputSize); + +/* + * LZ4_saveDict + * If previously compressed data block is not guaranteed to remain available at its memory location + * save it into a safer place (char* safeBuffer) + * Note : you don't need to call LZ4_loadDict() afterwards, + * dictionary is immediately usable, you can therefore call again LZ4_compress_continue() + * Return : dictionary size in bytes, or 0 if error + * Note : any dictSize > 64 KB will be interpreted as 64KB. + */ +int LZ4_saveDict (LZ4_stream_t* LZ4_streamPtr, char* safeBuffer, int dictSize); + + +/************************************************ + Streaming Decompression Functions +************************************************/ + +#define LZ4_STREAMDECODESIZE_U64 4 +#define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long)) +typedef struct { unsigned long long table[LZ4_STREAMDECODESIZE_U64]; } LZ4_streamDecode_t; +/* + * LZ4_streamDecode_t + * information structure to track an LZ4 stream. + * init this structure content using LZ4_setStreamDecode or memset() before first use ! + * + * In the context of a DLL (liblz4) please prefer usage of construction methods below. + * They are more future proof, in case of a change of LZ4_streamDecode_t size in the future. + * LZ4_createStreamDecode will allocate and initialize an LZ4_streamDecode_t structure + * LZ4_freeStreamDecode releases its memory. + */ +LZ4_streamDecode_t* LZ4_createStreamDecode(void); +int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream); + +/* + * LZ4_setStreamDecode + * Use this function to instruct where to find the dictionary. + * Setting a size of 0 is allowed (same effect as reset). + * Return : 1 if OK, 0 if error + */ +int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize); + +/* +*_continue() : + These decoding functions allow decompression of multiple blocks in "streaming" mode. + Previously decoded blocks *must* remain available at the memory position where they were decoded (up to 64 KB) + If this condition is not possible, save the relevant part of decoded data into a safe buffer, + and indicate where is its new address using LZ4_setStreamDecode() +*/ +int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxDecompressedSize); +int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int originalSize); + + +/* +Advanced decoding functions : +*_usingDict() : + These decoding functions work the same as + a combination of LZ4_setDictDecode() followed by LZ4_decompress_x_continue() + They are stand-alone and don't use nor update an LZ4_streamDecode_t structure. +*/ +int LZ4_decompress_safe_usingDict (const char* source, char* dest, int compressedSize, int maxDecompressedSize, const char* dictStart, int dictSize); +int LZ4_decompress_fast_usingDict (const char* source, char* dest, int originalSize, const char* dictStart, int dictSize); + + + +/************************************** + Obsolete Functions +**************************************/ +/* +Obsolete decompression functions +These function names are deprecated and should no longer be used. +They are only provided here for compatibility with older user programs. +- LZ4_uncompress is the same as LZ4_decompress_fast +- LZ4_uncompress_unknownOutputSize is the same as LZ4_decompress_safe +These function prototypes are now disabled; uncomment them if you really need them. +It is highly recommended to stop using these functions and migrate to newer ones */ +/* int LZ4_uncompress (const char* source, char* dest, int outputSize); */ +/* int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize); */ + + +/* Obsolete streaming functions; use new streaming interface whenever possible */ +void* LZ4_create (const char* inputBuffer); +int LZ4_sizeofStreamState(void); +int LZ4_resetStreamState(void* state, const char* inputBuffer); +char* LZ4_slideInputBuffer (void* state); + +/* Obsolete streaming decoding functions */ +int LZ4_decompress_safe_withPrefix64k (const char* source, char* dest, int compressedSize, int maxOutputSize); +int LZ4_decompress_fast_withPrefix64k (const char* source, char* dest, int originalSize); + + +#if defined (__cplusplus) +} +#endif diff --git a/lib/lz4frame.c b/lib/lz4frame.c new file mode 100644 index 0000000000..5183f221e2 --- /dev/null +++ b/lib/lz4frame.c @@ -0,0 +1,1330 @@ +/* +LZ4 auto-framing library +Copyright (C) 2011-2014, Yann Collet. +BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +* Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +You can contact the author at : +- LZ4 source repository : http://code.google.com/p/lz4/ +- LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c +*/ + +/* LZ4F is a stand-alone API to create LZ4-compressed Frames +* fully conformant to specification v1.4.1. +* All related operations, including memory management, are handled by the library. +* */ + + +/************************************** +Compiler Options +**************************************/ +#ifdef _MSC_VER /* Visual Studio */ +# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ +#endif + +#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) +#ifdef __GNUC__ +# pragma GCC diagnostic ignored "-Wmissing-braces" /* GCC bug 53119 : doesn't accept { 0 } as initializer (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119) */ +# pragma GCC diagnostic ignored "-Wmissing-field-initializers" /* GCC bug 53119 : doesn't accept { 0 } as initializer (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119) */ +#endif + + +/************************************** +Memory routines +**************************************/ +#include /* malloc, calloc, free */ +#define ALLOCATOR(s) calloc(1,s) +#define FREEMEM free +#include /* memset, memcpy, memmove */ +#define MEM_INIT memset + + +/************************************** +Includes +**************************************/ +#include "lz4frame_static.h" +#include "lz4.h" +#include "lz4hc.h" +#include "xxhash.h" + + +/************************************** +Basic Types +**************************************/ +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */ +# include +typedef uint8_t BYTE; +typedef uint16_t U16; +typedef uint32_t U32; +typedef int32_t S32; +typedef uint64_t U64; +#else +typedef unsigned char BYTE; +typedef unsigned short U16; +typedef unsigned int U32; +typedef signed int S32; +typedef unsigned long long U64; +#endif + + +/************************************** +Constants +**************************************/ +#define KB *(1<<10) +#define MB *(1<<20) +#define GB *(1<<30) + +#define _1BIT 0x01 +#define _2BITS 0x03 +#define _3BITS 0x07 +#define _4BITS 0x0F +#define _8BITS 0xFF + +#define LZ4F_MAGICNUMBER 0x184D2204U +#define LZ4F_BLOCKUNCOMPRESSED_FLAG 0x80000000U +#define LZ4F_MAXHEADERFRAME_SIZE 7 +#define LZ4F_BLOCKSIZEID_DEFAULT max64KB + +static const U32 minHClevel = 3; + +/************************************** +Structures and local types +**************************************/ +typedef struct +{ + LZ4F_preferences_t prefs; + U32 version; + U32 cStage; + size_t maxBlockSize; + size_t maxBufferSize; + BYTE* tmpBuff; + BYTE* tmpIn; + size_t tmpInSize; + XXH32_state_t xxh; + void* lz4CtxPtr; + U32 lz4CtxLevel; /* 0: unallocated; 1: LZ4_stream_t; 3: LZ4_streamHC_t */ +} LZ4F_cctx_internal_t; + +typedef struct +{ + LZ4F_frameInfo_t frameInfo; + unsigned version; + unsigned dStage; + size_t maxBlockSize; + size_t maxBufferSize; + const BYTE* srcExpect; + BYTE* tmpIn; + size_t tmpInSize; + size_t tmpInTarget; + BYTE* tmpOutBuffer; + BYTE* dict; + size_t dictSize; + BYTE* tmpOut; + size_t tmpOutSize; + size_t tmpOutStart; + XXH32_state_t xxh; + BYTE header[8]; +} LZ4F_dctx_internal_t; + + +/************************************** +Macros +**************************************/ + + +/************************************** +Error management +**************************************/ +#define LZ4F_GENERATE_STRING(STRING) #STRING, +static const char* LZ4F_errorStrings[] = { LZ4F_LIST_ERRORS(LZ4F_GENERATE_STRING) }; + + +U32 LZ4F_isError(LZ4F_errorCode_t code) +{ + return (code > (LZ4F_errorCode_t)(-ERROR_maxCode)); +} + +const char* LZ4F_getErrorName(LZ4F_errorCode_t code) +{ + static const char* codeError = "Unspecified error code"; + if (LZ4F_isError(code)) return LZ4F_errorStrings[-(int)(code)]; + return codeError; +} + + +/************************************** +Private functions +**************************************/ +static size_t LZ4F_getBlockSize(unsigned blockSizeID) +{ + static const size_t blockSizes[4] = { 64 KB, 256 KB, 1 MB, 4 MB }; + + if (blockSizeID == 0) blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT; + blockSizeID -= 4; + if (blockSizeID > 3) return (size_t)-ERROR_maxBlockSize_invalid; + return blockSizes[blockSizeID]; +} + + +/* unoptimized version; solves endianess & alignment issues */ +static void LZ4F_writeLE32 (BYTE* dstPtr, U32 value32) +{ + dstPtr[0] = (BYTE)value32; + dstPtr[1] = (BYTE)(value32 >> 8); + dstPtr[2] = (BYTE)(value32 >> 16); + dstPtr[3] = (BYTE)(value32 >> 24); +} + +static U32 LZ4F_readLE32 (const BYTE* srcPtr) +{ + U32 value32 = srcPtr[0]; + value32 += (srcPtr[1]<<8); + value32 += (srcPtr[2]<<16); + value32 += (srcPtr[3]<<24); + return value32; +} + + +static BYTE LZ4F_headerChecksum (const BYTE* header, size_t length) +{ + U32 xxh = XXH32(header, (U32)length, 0); + return (BYTE)(xxh >> 8); +} + + +/************************************** +Simple compression functions +**************************************/ +size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr) +{ + LZ4F_preferences_t prefs = { 0 }; + size_t headerSize; + size_t streamSize; + + if (preferencesPtr!=NULL) prefs = *preferencesPtr; + { + blockSizeID_t proposedBSID = max64KB; + size_t maxBlockSize = 64 KB; + while (prefs.frameInfo.blockSizeID > proposedBSID) + { + if (srcSize <= maxBlockSize) + { + prefs.frameInfo.blockSizeID = proposedBSID; + break; + } + proposedBSID++; + maxBlockSize <<= 2; + } + } + prefs.autoFlush = 1; + + headerSize = 7; /* basic header size (no option) including magic number */ + streamSize = LZ4F_compressBound(srcSize, &prefs); + + return headerSize + streamSize; +} + + +/* LZ4F_compressFrame() +* Compress an entire srcBuffer into a valid LZ4 frame, as defined by specification v1.4.1, in a single step. +* The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case. +* You can get the minimum value of dstMaxSize by using LZ4F_compressFrameBound() +* If this condition is not respected, LZ4F_compressFrame() will fail (result is an errorCode) +* The LZ4F_preferences_t structure is optional : you can provide NULL as argument. All preferences will be set to default. +* The result of the function is the number of bytes written into dstBuffer. +* The function outputs an error code if it fails (can be tested using LZ4F_isError()) +*/ +size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_preferences_t* preferencesPtr) +{ + LZ4F_cctx_internal_t cctxI = { 0 }; /* works because no allocation */ + LZ4F_preferences_t prefs = { 0 }; + LZ4F_compressOptions_t options = { 0 }; + LZ4F_errorCode_t errorCode; + BYTE* const dstStart = (BYTE*) dstBuffer; + BYTE* dstPtr = dstStart; + BYTE* const dstEnd = dstStart + dstMaxSize; + + + cctxI.version = LZ4F_VERSION; + cctxI.maxBufferSize = 5 MB; /* mess with real buffer size to prevent allocation; works because autoflush==1 & stableSrc==1 */ + + if (preferencesPtr!=NULL) prefs = *preferencesPtr; + { + blockSizeID_t proposedBSID = max64KB; + size_t maxBlockSize = 64 KB; + while (prefs.frameInfo.blockSizeID > proposedBSID) + { + if (srcSize <= maxBlockSize) + { + prefs.frameInfo.blockSizeID = proposedBSID; + break; + } + proposedBSID++; + maxBlockSize <<= 2; + } + } + prefs.autoFlush = 1; + if (srcSize <= LZ4F_getBlockSize(prefs.frameInfo.blockSizeID)) + prefs.frameInfo.blockMode = blockIndependent; /* no need for linked blocks */ + + options.stableSrc = 1; + + if (dstMaxSize < LZ4F_compressFrameBound(srcSize, &prefs)) + return (size_t)-ERROR_dstMaxSize_tooSmall; + + errorCode = LZ4F_compressBegin(&cctxI, dstBuffer, dstMaxSize, &prefs); /* write header */ + if (LZ4F_isError(errorCode)) return errorCode; + dstPtr += errorCode; /* header size */ + + dstMaxSize -= errorCode; + errorCode = LZ4F_compressUpdate(&cctxI, dstPtr, dstMaxSize, srcBuffer, srcSize, &options); + if (LZ4F_isError(errorCode)) return errorCode; + dstPtr += errorCode; + + errorCode = LZ4F_compressEnd(&cctxI, dstPtr, dstEnd-dstPtr, &options); /* flush last block, and generate suffix */ + if (LZ4F_isError(errorCode)) return errorCode; + dstPtr += errorCode; + + FREEMEM(cctxI.lz4CtxPtr); + + return (dstPtr - dstStart); +} + + +/*********************************** +* Advanced compression functions +* *********************************/ + +/* LZ4F_createCompressionContext() : +* The first thing to do is to create a compressionContext object, which will be used in all compression operations. +* This is achieved using LZ4F_createCompressionContext(), which takes as argument a version and an LZ4F_preferences_t structure. +* The version provided MUST be LZ4F_VERSION. It is intended to track potential version differences between different binaries. +* The function will provide a pointer to an allocated LZ4F_compressionContext_t object. +* If the result LZ4F_errorCode_t is not OK_NoError, there was an error during context creation. +* Object can release its memory using LZ4F_freeCompressionContext(); +*/ +LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_compressionContext_t* LZ4F_compressionContextPtr, unsigned version) +{ + LZ4F_cctx_internal_t* cctxPtr; + + cctxPtr = (LZ4F_cctx_internal_t*)ALLOCATOR(sizeof(LZ4F_cctx_internal_t)); + if (cctxPtr==NULL) return (LZ4F_errorCode_t)(-ERROR_allocation_failed); + + cctxPtr->version = version; + cctxPtr->cStage = 0; /* Next stage : write header */ + + *LZ4F_compressionContextPtr = (LZ4F_compressionContext_t)cctxPtr; + + return OK_NoError; +} + + +LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t LZ4F_compressionContext) +{ + LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)LZ4F_compressionContext; + + FREEMEM(cctxPtr->lz4CtxPtr); + FREEMEM(cctxPtr->tmpBuff); + FREEMEM(LZ4F_compressionContext); + + return OK_NoError; +} + + +/* LZ4F_compressBegin() : +* will write the frame header into dstBuffer. +* dstBuffer must be large enough to accommodate a header (dstMaxSize). Maximum header size is LZ4F_MAXHEADERFRAME_SIZE bytes. +* The result of the function is the number of bytes written into dstBuffer for the header +* or an error code (can be tested using LZ4F_isError()) +*/ +size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_preferences_t* preferencesPtr) +{ + LZ4F_preferences_t prefNull = { 0 }; + LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext; + BYTE* const dstStart = (BYTE*)dstBuffer; + BYTE* dstPtr = dstStart; + BYTE* headerStart; + size_t requiredBuffSize; + + if (dstMaxSize < LZ4F_MAXHEADERFRAME_SIZE) return (size_t)-ERROR_dstMaxSize_tooSmall; + if (cctxPtr->cStage != 0) return (size_t)-ERROR_GENERIC; + if (preferencesPtr == NULL) preferencesPtr = &prefNull; + cctxPtr->prefs = *preferencesPtr; + + /* ctx Management */ + { + U32 targetCtxLevel = cctxPtr->prefs.compressionLevellz4CtxLevel < targetCtxLevel) + { + FREEMEM(cctxPtr->lz4CtxPtr); + if (cctxPtr->prefs.compressionLevellz4CtxPtr = (void*)LZ4_createStream(); + else + cctxPtr->lz4CtxPtr = (void*)LZ4_createStreamHC(); + cctxPtr->lz4CtxLevel = targetCtxLevel; + } + } + + /* Buffer Management */ + if (cctxPtr->prefs.frameInfo.blockSizeID == 0) cctxPtr->prefs.frameInfo.blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT; + cctxPtr->maxBlockSize = LZ4F_getBlockSize(cctxPtr->prefs.frameInfo.blockSizeID); + + requiredBuffSize = cctxPtr->maxBlockSize + ((cctxPtr->prefs.frameInfo.blockMode == blockLinked) * 128 KB); + if (preferencesPtr->autoFlush) + requiredBuffSize = (cctxPtr->prefs.frameInfo.blockMode == blockLinked) * 64 KB; /* just needs dict */ + + if (cctxPtr->maxBufferSize < requiredBuffSize) + { + cctxPtr->maxBufferSize = requiredBuffSize; + FREEMEM(cctxPtr->tmpBuff); + cctxPtr->tmpBuff = (BYTE*)ALLOCATOR(requiredBuffSize); + if (cctxPtr->tmpBuff == NULL) return (size_t)-ERROR_allocation_failed; + } + cctxPtr->tmpIn = cctxPtr->tmpBuff; + cctxPtr->tmpInSize = 0; + XXH32_reset(&(cctxPtr->xxh), 0); + if (cctxPtr->prefs.compressionLevellz4CtxPtr)); + else + LZ4_resetStreamHC((LZ4_streamHC_t*)(cctxPtr->lz4CtxPtr), cctxPtr->prefs.compressionLevel); + + /* Magic Number */ + LZ4F_writeLE32(dstPtr, LZ4F_MAGICNUMBER); + dstPtr += 4; + headerStart = dstPtr; + + /* FLG Byte */ + *dstPtr++ = ((1 & _2BITS) << 6) /* Version('01') */ + + ((cctxPtr->prefs.frameInfo.blockMode & _1BIT ) << 5) /* Block mode */ + + (char)((cctxPtr->prefs.frameInfo.contentChecksumFlag & _1BIT ) << 2); /* Stream checksum */ + /* BD Byte */ + *dstPtr++ = (char)((cctxPtr->prefs.frameInfo.blockSizeID & _3BITS) << 4); + /* CRC Byte */ + *dstPtr++ = LZ4F_headerChecksum(headerStart, 2); + + cctxPtr->cStage = 1; /* header written, wait for data block */ + + return (dstPtr - dstStart); +} + + +/* LZ4F_compressBound() : gives the size of Dst buffer given a srcSize to handle worst case situations. +* The LZ4F_frameInfo_t structure is optional : +* you can provide NULL as argument, all preferences will then be set to default. +* */ +size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr) +{ + const LZ4F_preferences_t prefsNull = { 0 }; + const LZ4F_preferences_t* prefsPtr = (preferencesPtr==NULL) ? &prefsNull : preferencesPtr; + blockSizeID_t bid = prefsPtr->frameInfo.blockSizeID; + size_t blockSize = LZ4F_getBlockSize(bid); + unsigned nbBlocks = (unsigned)(srcSize / blockSize) + 1; + size_t lastBlockSize = prefsPtr->autoFlush ? srcSize % blockSize : blockSize; + size_t blockInfo = 4; /* default, without block CRC option */ + size_t frameEnd = 4 + (prefsPtr->frameInfo.contentChecksumFlag*4); + size_t result = (blockInfo * nbBlocks) + (blockSize * (nbBlocks-1)) + lastBlockSize + frameEnd; + + return result; +} + + +typedef int (*compressFunc_t)(void* ctx, const char* src, char* dst, int srcSize, int dstSize, int level); + +static size_t LZ4F_compressBlock(void* dst, const void* src, size_t srcSize, compressFunc_t compress, void* lz4ctx, int level) +{ + /* compress one block */ + BYTE* cSizePtr = (BYTE*)dst; + U32 cSize; + cSize = (U32)compress(lz4ctx, (const char*)src, (char*)(cSizePtr+4), (int)(srcSize), (int)(srcSize-1), level); + LZ4F_writeLE32(cSizePtr, cSize); + if (cSize == 0) /* compression failed */ + { + cSize = (U32)srcSize; + LZ4F_writeLE32(cSizePtr, cSize + LZ4F_BLOCKUNCOMPRESSED_FLAG); + memcpy(cSizePtr+4, src, srcSize); + } + return cSize + 4; +} + + +static int LZ4F_localLZ4_compress_limitedOutput_withState(void* ctx, const char* src, char* dst, int srcSize, int dstSize, int level) +{ + (void) level; + return LZ4_compress_limitedOutput_withState(ctx, src, dst, srcSize, dstSize); +} + +static int LZ4F_localLZ4_compress_limitedOutput_continue(void* ctx, const char* src, char* dst, int srcSize, int dstSize, int level) +{ + (void) level; + return LZ4_compress_limitedOutput_continue((LZ4_stream_t*)ctx, src, dst, srcSize, dstSize); +} + +static int LZ4F_localLZ4_compressHC_limitedOutput_continue(void* ctx, const char* src, char* dst, int srcSize, int dstSize, int level) +{ + (void) level; + return LZ4_compressHC_limitedOutput_continue((LZ4_streamHC_t*)ctx, src, dst, srcSize, dstSize); +} + +static compressFunc_t LZ4F_selectCompression(blockMode_t blockMode, U32 level) +{ + if (level < minHClevel) + { + if (blockMode == blockIndependent) return LZ4F_localLZ4_compress_limitedOutput_withState; + return LZ4F_localLZ4_compress_limitedOutput_continue; + } + if (blockMode == blockIndependent) return LZ4_compressHC2_limitedOutput_withStateHC; + return LZ4F_localLZ4_compressHC_limitedOutput_continue; +} + +static int LZ4F_localSaveDict(LZ4F_cctx_internal_t* cctxPtr) +{ + if (cctxPtr->prefs.compressionLevel < minHClevel) + return LZ4_saveDict ((LZ4_stream_t*)(cctxPtr->lz4CtxPtr), (char*)(cctxPtr->tmpBuff), 64 KB); + return LZ4_saveDictHC ((LZ4_streamHC_t*)(cctxPtr->lz4CtxPtr), (char*)(cctxPtr->tmpBuff), 64 KB); +} + +typedef enum { notDone, fromTmpBuffer, fromSrcBuffer } LZ4F_lastBlockStatus; + +/* LZ4F_compressUpdate() +* LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary. +* The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case. +* If this condition is not respected, LZ4F_compress() will fail (result is an errorCode) +* You can get the minimum value of dstMaxSize by using LZ4F_compressBound() +* The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. +* The result of the function is the number of bytes written into dstBuffer : it can be zero, meaning input data was just buffered. +* The function outputs an error code if it fails (can be tested using LZ4F_isError()) +*/ +size_t LZ4F_compressUpdate(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptionsPtr) +{ + LZ4F_compressOptions_t cOptionsNull = { 0 }; + LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext; + size_t blockSize = cctxPtr->maxBlockSize; + const BYTE* srcPtr = (const BYTE*)srcBuffer; + const BYTE* const srcEnd = srcPtr + srcSize; + BYTE* const dstStart = (BYTE*)dstBuffer; + BYTE* dstPtr = dstStart; + LZ4F_lastBlockStatus lastBlockCompressed = notDone; + compressFunc_t compress; + + + if (cctxPtr->cStage != 1) return (size_t)-ERROR_GENERIC; + if (dstMaxSize < LZ4F_compressBound(srcSize, &(cctxPtr->prefs))) return (size_t)-ERROR_dstMaxSize_tooSmall; + if (compressOptionsPtr == NULL) compressOptionsPtr = &cOptionsNull; + + /* select compression function */ + compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel); + + /* complete tmp buffer */ + if (cctxPtr->tmpInSize > 0) /* some data already within tmp buffer */ + { + size_t sizeToCopy = blockSize - cctxPtr->tmpInSize; + if (sizeToCopy > srcSize) + { + /* add src to tmpIn buffer */ + memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, srcSize); + srcPtr = srcEnd; + cctxPtr->tmpInSize += srcSize; + /* still needs some CRC */ + } + else + { + /* complete tmpIn block and then compress it */ + lastBlockCompressed = fromTmpBuffer; + memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, sizeToCopy); + srcPtr += sizeToCopy; + + dstPtr += LZ4F_compressBlock(dstPtr, cctxPtr->tmpIn, blockSize, compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel); + + if (cctxPtr->prefs.frameInfo.blockMode==blockLinked) cctxPtr->tmpIn += blockSize; + cctxPtr->tmpInSize = 0; + } + } + + while ((size_t)(srcEnd - srcPtr) >= blockSize) + { + /* compress full block */ + lastBlockCompressed = fromSrcBuffer; + dstPtr += LZ4F_compressBlock(dstPtr, srcPtr, blockSize, compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel); + srcPtr += blockSize; + } + + if ((cctxPtr->prefs.autoFlush) && (srcPtr < srcEnd)) + { + /* compress remaining input < blockSize */ + lastBlockCompressed = fromSrcBuffer; + dstPtr += LZ4F_compressBlock(dstPtr, srcPtr, srcEnd - srcPtr, compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel); + srcPtr = srcEnd; + } + + /* preserve dictionary if necessary */ + if ((cctxPtr->prefs.frameInfo.blockMode==blockLinked) && (lastBlockCompressed==fromSrcBuffer)) + { + if (compressOptionsPtr->stableSrc) + { + cctxPtr->tmpIn = cctxPtr->tmpBuff; + } + else + { + int realDictSize = LZ4F_localSaveDict(cctxPtr); + if (realDictSize==0) return (size_t)-ERROR_GENERIC; + cctxPtr->tmpIn = cctxPtr->tmpBuff + realDictSize; + } + } + + /* keep tmpIn within limits */ + if ((cctxPtr->tmpIn + blockSize) > (cctxPtr->tmpBuff + cctxPtr->maxBufferSize) /* necessarily blockLinked && lastBlockCompressed==fromTmpBuffer */ + && !(cctxPtr->prefs.autoFlush)) + { + LZ4F_localSaveDict(cctxPtr); + cctxPtr->tmpIn = cctxPtr->tmpBuff + 64 KB; + } + + /* some input data left, necessarily < blockSize */ + if (srcPtr < srcEnd) + { + /* fill tmp buffer */ + size_t sizeToCopy = srcEnd - srcPtr; + memcpy(cctxPtr->tmpIn, srcPtr, sizeToCopy); + cctxPtr->tmpInSize = sizeToCopy; + } + + if (cctxPtr->prefs.frameInfo.contentChecksumFlag == contentChecksumEnabled) + XXH32_update(&(cctxPtr->xxh), srcBuffer, (unsigned)srcSize); + + return dstPtr - dstStart; +} + + +/* LZ4F_flush() +* Should you need to create compressed data immediately, without waiting for a block to be filled, +* you can call LZ4_flush(), which will immediately compress any remaining data stored within compressionContext. +* The result of the function is the number of bytes written into dstBuffer +* (it can be zero, this means there was no data left within compressionContext) +* The function outputs an error code if it fails (can be tested using LZ4F_isError()) +* The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. +*/ +size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptionsPtr) +{ + LZ4F_compressOptions_t cOptionsNull = { 0 }; + LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext; + BYTE* const dstStart = (BYTE*)dstBuffer; + BYTE* dstPtr = dstStart; + compressFunc_t compress; + + + if (cctxPtr->tmpInSize == 0) return 0; /* nothing to flush */ + if (cctxPtr->cStage != 1) return (size_t)-ERROR_GENERIC; + if (dstMaxSize < (cctxPtr->tmpInSize + 16)) return (size_t)-ERROR_dstMaxSize_tooSmall; + if (compressOptionsPtr == NULL) compressOptionsPtr = &cOptionsNull; + (void)compressOptionsPtr; /* not yet useful */ + + /* select compression function */ + compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel); + + /* compress tmp buffer */ + dstPtr += LZ4F_compressBlock(dstPtr, cctxPtr->tmpIn, cctxPtr->tmpInSize, compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel); + if (cctxPtr->prefs.frameInfo.blockMode==blockLinked) cctxPtr->tmpIn += cctxPtr->tmpInSize; + cctxPtr->tmpInSize = 0; + + /* keep tmpIn within limits */ + if ((cctxPtr->tmpIn + cctxPtr->maxBlockSize) > (cctxPtr->tmpBuff + cctxPtr->maxBufferSize)) /* necessarily blockLinked */ + { + LZ4F_localSaveDict(cctxPtr); + cctxPtr->tmpIn = cctxPtr->tmpBuff + 64 KB; + } + + return dstPtr - dstStart; +} + + +/* LZ4F_compressEnd() +* When you want to properly finish the compressed frame, just call LZ4F_compressEnd(). +* It will flush whatever data remained within compressionContext (like LZ4_flush()) +* but also properly finalize the frame, with an endMark and a checksum. +* The result of the function is the number of bytes written into dstBuffer (necessarily >= 4 (endMark size)) +* The function outputs an error code if it fails (can be tested using LZ4F_isError()) +* The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. +* compressionContext can then be used again, starting with LZ4F_compressBegin(). The preferences will remain the same. +*/ +size_t LZ4F_compressEnd(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptionsPtr) +{ + LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext; + BYTE* const dstStart = (BYTE*)dstBuffer; + BYTE* dstPtr = dstStart; + size_t errorCode; + + errorCode = LZ4F_flush(compressionContext, dstBuffer, dstMaxSize, compressOptionsPtr); + if (LZ4F_isError(errorCode)) return errorCode; + dstPtr += errorCode; + + LZ4F_writeLE32(dstPtr, 0); + dstPtr+=4; /* endMark */ + + if (cctxPtr->prefs.frameInfo.contentChecksumFlag == contentChecksumEnabled) + { + U32 xxh = XXH32_digest(&(cctxPtr->xxh)); + LZ4F_writeLE32(dstPtr, xxh); + dstPtr+=4; /* content Checksum */ + } + + cctxPtr->cStage = 0; /* state is now re-usable (with identical preferences) */ + + return dstPtr - dstStart; +} + + +/*********************************** +* Decompression functions +* *********************************/ + +/* Resource management */ + +/* LZ4F_createDecompressionContext() : +* The first thing to do is to create a decompressionContext object, which will be used in all decompression operations. +* This is achieved using LZ4F_createDecompressionContext(). +* The function will provide a pointer to a fully allocated and initialized LZ4F_decompressionContext object. +* If the result LZ4F_errorCode_t is not zero, there was an error during context creation. +* Object can release its memory using LZ4F_freeDecompressionContext(); +*/ +LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_compressionContext_t* LZ4F_decompressionContextPtr, unsigned versionNumber) +{ + LZ4F_dctx_internal_t* dctxPtr; + + dctxPtr = ALLOCATOR(sizeof(LZ4F_dctx_internal_t)); + if (dctxPtr==NULL) return (LZ4F_errorCode_t)-ERROR_GENERIC; + + dctxPtr->version = versionNumber; + *LZ4F_decompressionContextPtr = (LZ4F_compressionContext_t)dctxPtr; + return OK_NoError; +} + +LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_compressionContext_t LZ4F_decompressionContext) +{ + LZ4F_dctx_internal_t* dctxPtr = (LZ4F_dctx_internal_t*)LZ4F_decompressionContext; + FREEMEM(dctxPtr->tmpIn); + FREEMEM(dctxPtr->tmpOutBuffer); + FREEMEM(dctxPtr); + return OK_NoError; +} + + +/* Decompression */ + +static size_t LZ4F_decodeHeader(LZ4F_dctx_internal_t* dctxPtr, const BYTE* srcPtr, size_t srcSize) +{ + BYTE FLG, BD, HC; + unsigned version, blockMode, blockChecksumFlag, contentSizeFlag, contentChecksumFlag, dictFlag, blockSizeID; + size_t bufferNeeded; + + /* need to decode header to get frameInfo */ + if (srcSize < 7) return (size_t)-ERROR_GENERIC; /* minimal header size */ + + /* control magic number */ + if (LZ4F_readLE32(srcPtr) != LZ4F_MAGICNUMBER) return (size_t)-ERROR_GENERIC; + srcPtr += 4; + + /* Flags */ + FLG = srcPtr[0]; + version = (FLG>>6)&_2BITS; + blockMode = (FLG>>5) & _1BIT; + blockChecksumFlag = (FLG>>4) & _1BIT; + contentSizeFlag = (FLG>>3) & _1BIT; + contentChecksumFlag = (FLG>>2) & _1BIT; + dictFlag = (FLG>>0) & _1BIT; + BD = srcPtr[1]; + blockSizeID = (BD>>4) & _3BITS; + + /* check */ + HC = LZ4F_headerChecksum(srcPtr, 2); + if (HC != srcPtr[2]) return (size_t)-ERROR_GENERIC; /* Bad header checksum error */ + + /* validate */ + if (version != 1) return (size_t)-ERROR_GENERIC; /* Version Number, only supported value */ + if (blockChecksumFlag != 0) return (size_t)-ERROR_GENERIC; /* Only supported value for the time being */ + if (contentSizeFlag != 0) return (size_t)-ERROR_GENERIC; /* Only supported value for the time being */ + if (((FLG>>1)&_1BIT) != 0) return (size_t)-ERROR_GENERIC; /* Reserved bit */ + if (dictFlag != 0) return (size_t)-ERROR_GENERIC; /* Only supported value for the time being */ + if (((BD>>7)&_1BIT) != 0) return (size_t)-ERROR_GENERIC; /* Reserved bit */ + if (blockSizeID < 4) return (size_t)-ERROR_GENERIC; /* Only supported values for the time being */ + if (((BD>>0)&_4BITS) != 0) return (size_t)-ERROR_GENERIC; /* Reserved bits */ + + /* save */ + dctxPtr->frameInfo.blockMode = blockMode; + dctxPtr->frameInfo.contentChecksumFlag = contentChecksumFlag; + dctxPtr->frameInfo.blockSizeID = blockSizeID; + dctxPtr->maxBlockSize = LZ4F_getBlockSize(blockSizeID); + + /* init */ + if (contentChecksumFlag) XXH32_reset(&(dctxPtr->xxh), 0); + + /* alloc */ + bufferNeeded = dctxPtr->maxBlockSize + ((dctxPtr->frameInfo.blockMode==blockLinked) * 128 KB); + if (bufferNeeded > dctxPtr->maxBufferSize) /* tmp buffers too small */ + { + FREEMEM(dctxPtr->tmpIn); + FREEMEM(dctxPtr->tmpOutBuffer); + dctxPtr->maxBufferSize = bufferNeeded; + dctxPtr->tmpIn = ALLOCATOR(dctxPtr->maxBlockSize); + if (dctxPtr->tmpIn == NULL) return (size_t)-ERROR_GENERIC; + dctxPtr->tmpOutBuffer= ALLOCATOR(dctxPtr->maxBufferSize); + if (dctxPtr->tmpOutBuffer== NULL) return (size_t)-ERROR_GENERIC; + } + dctxPtr->tmpInSize = 0; + dctxPtr->tmpInTarget = 0; + dctxPtr->dict = dctxPtr->tmpOutBuffer; + dctxPtr->dictSize = 0; + dctxPtr->tmpOut = dctxPtr->tmpOutBuffer; + dctxPtr->tmpOutStart = 0; + dctxPtr->tmpOutSize = 0; + + return 7; +} + + +typedef enum { dstage_getHeader=0, dstage_storeHeader, dstage_decodeHeader, + dstage_getCBlockSize, dstage_storeCBlockSize, dstage_decodeCBlockSize, + dstage_copyDirect, + dstage_getCBlock, dstage_storeCBlock, dstage_decodeCBlock, + dstage_decodeCBlock_intoDst, dstage_decodeCBlock_intoTmp, dstage_flushOut, + dstage_getSuffix, dstage_storeSuffix, dstage_checkSuffix +} dStage_t; + + +/* LZ4F_getFrameInfo() +* This function decodes frame header information, such as blockSize. +* It is optional : you could start by calling directly LZ4F_decompress() instead. +* The objective is to extract header information without starting decompression, typically for allocation purposes. +* LZ4F_getFrameInfo() can also be used *after* starting decompression, on a valid LZ4F_decompressionContext_t. +* The number of bytes read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value). +* You are expected to resume decompression from where it stopped (srcBuffer + *srcSizePtr) +* The function result is an hint of the better srcSize to use for next call to LZ4F_decompress, +* or an error code which can be tested using LZ4F_isError(). +*/ +LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionContext, LZ4F_frameInfo_t* frameInfoPtr, const void* srcBuffer, size_t* srcSizePtr) +{ + LZ4F_dctx_internal_t* dctxPtr = (LZ4F_dctx_internal_t*)decompressionContext; + + if (dctxPtr->dStage == dstage_getHeader) + { + LZ4F_errorCode_t errorCode = LZ4F_decodeHeader(dctxPtr, srcBuffer, *srcSizePtr); + if (LZ4F_isError(errorCode)) return errorCode; + *srcSizePtr = errorCode; + *frameInfoPtr = dctxPtr->frameInfo; + dctxPtr->srcExpect = NULL; + dctxPtr->dStage = dstage_getCBlockSize; + return 4; + } + + /* frameInfo already decoded */ + *srcSizePtr = 0; + *frameInfoPtr = dctxPtr->frameInfo; + return 0; +} + + +static int LZ4F_decompress_safe (const char* source, char* dest, int compressedSize, int maxDecompressedSize, const char* dictStart, int dictSize) +{ + (void)dictStart; + (void)dictSize; + return LZ4_decompress_safe (source, dest, compressedSize, maxDecompressedSize); +} + + + +static void LZ4F_updateDict(LZ4F_dctx_internal_t* dctxPtr, const BYTE* dstPtr, size_t dstSize, const BYTE* dstPtr0, unsigned withinTmp) +{ + if (dctxPtr->dictSize==0) + dctxPtr->dict = (BYTE*)dstPtr; /* priority to dictionary continuity */ + + if (dctxPtr->dict + dctxPtr->dictSize == dstPtr) /* dictionary continuity */ + { + dctxPtr->dictSize += dstSize; + return; + } + + if (dstPtr - dstPtr0 + dstSize >= 64 KB) /* dstBuffer large enough to become dictionary */ + { + dctxPtr->dict = (BYTE*)dstPtr0; + dctxPtr->dictSize = dstPtr - dstPtr0 + dstSize; + return; + } + + if ((withinTmp) && (dctxPtr->dict == dctxPtr->tmpOutBuffer)) + { + /* assumption : dctxPtr->dict + dctxPtr->dictSize == dctxPtr->tmpOut + dctxPtr->tmpOutStart */ + dctxPtr->dictSize += dstSize; + return; + } + + if (withinTmp) /* copy relevant dict portion in front of tmpOut within tmpOutBuffer */ + { +#if 0 + size_t savedDictSize = dctxPtr->tmpOut - dctxPtr->tmpOutBuffer; + memcpy(dctxPtr->tmpOutBuffer, dctxPtr->dict + dctxPtr->dictSize - dctxPtr->tmpOutStart- savedDictSize, savedDictSize); + dctxPtr->dict = dctxPtr->tmpOutBuffer; + dctxPtr->dictSize = savedDictSize + dctxPtr->tmpOutStart + dstSize; + return; + +#else + + size_t preserveSize = dctxPtr->tmpOut - dctxPtr->tmpOutBuffer; + size_t copySize = 64 KB - dctxPtr->tmpOutSize; + BYTE* oldDictEnd = dctxPtr->dict + dctxPtr->dictSize - dctxPtr->tmpOutStart; + if (dctxPtr->tmpOutSize > 64 KB) copySize = 0; + if (copySize > preserveSize) copySize = preserveSize; + + memcpy(dctxPtr->tmpOutBuffer + preserveSize - copySize, oldDictEnd - copySize, copySize); + + dctxPtr->dict = dctxPtr->tmpOutBuffer; + dctxPtr->dictSize = preserveSize + dctxPtr->tmpOutStart + dstSize; + return; +#endif + } + + if (dctxPtr->dict == dctxPtr->tmpOutBuffer) /* copy dst into tmp to complete dict */ + { + if (dctxPtr->dictSize + dstSize > dctxPtr->maxBufferSize) /* tmp buffer not large enough */ + { + size_t preserveSize = 64 KB - dstSize; /* note : dstSize < 64 KB */ + memcpy(dctxPtr->dict, dctxPtr->dict + dctxPtr->dictSize - preserveSize, preserveSize); + dctxPtr->dictSize = preserveSize; + } + memcpy(dctxPtr->dict + dctxPtr->dictSize, dstPtr, dstSize); + dctxPtr->dictSize += dstSize; + return; + } + + /* join dict & dest into tmp */ + { + size_t preserveSize = 64 KB - dstSize; /* note : dstSize < 64 KB */ + if (preserveSize > dctxPtr->dictSize) preserveSize = dctxPtr->dictSize; + memcpy(dctxPtr->tmpOutBuffer, dctxPtr->dict + dctxPtr->dictSize - preserveSize, preserveSize); + memcpy(dctxPtr->tmpOutBuffer + preserveSize, dstPtr, dstSize); + dctxPtr->dict = dctxPtr->tmpOutBuffer; + dctxPtr->dictSize = preserveSize + dstSize; + } +} + + + +/* LZ4F_decompress() +* Call this function repetitively to regenerate data compressed within srcBuffer. +* The function will attempt to decode *srcSizePtr from srcBuffer, into dstBuffer of maximum size *dstSizePtr. +* +* The number of bytes regenerated into dstBuffer will be provided within *dstSizePtr (necessarily <= original value). +* +* The number of bytes effectively read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value). +* If the number of bytes read is < number of bytes provided, then the decompression operation is not complete. +* You will have to call it again, continuing from where it stopped. +* +* The function result is an hint of the better srcSize to use for next call to LZ4F_decompress. +* Basically, it's the size of the current (or remaining) compressed block + header of next block. +* Respecting the hint provides some boost to performance, since it allows less buffer shuffling. +* Note that this is just a hint, you can always provide any srcSize you want. +* When a frame is fully decoded, the function result will be 0. +* If decompression failed, function result is an error code which can be tested using LZ4F_isError(). +*/ +size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, + void* dstBuffer, size_t* dstSizePtr, + const void* srcBuffer, size_t* srcSizePtr, + const LZ4F_decompressOptions_t* decompressOptionsPtr) +{ + LZ4F_dctx_internal_t* dctxPtr = (LZ4F_dctx_internal_t*)decompressionContext; + static const LZ4F_decompressOptions_t optionsNull = { 0 }; + const BYTE* const srcStart = (const BYTE*)srcBuffer; + const BYTE* const srcEnd = srcStart + *srcSizePtr; + const BYTE* srcPtr = srcStart; + BYTE* const dstStart = (BYTE*)dstBuffer; + BYTE* const dstEnd = dstStart + *dstSizePtr; + BYTE* dstPtr = dstStart; + const BYTE* selectedIn=NULL; + unsigned doAnotherStage = 1; + size_t nextSrcSizeHint = 1; + + + if (decompressOptionsPtr==NULL) decompressOptionsPtr = &optionsNull; + *srcSizePtr = 0; + *dstSizePtr = 0; + + /* expect to continue decoding src buffer where it left previously */ + if (dctxPtr->srcExpect != NULL) + { + if (srcStart != dctxPtr->srcExpect) return (size_t)-ERROR_GENERIC; + } + + /* programmed as a state machine */ + + while (doAnotherStage) + { + + switch(dctxPtr->dStage) + { + + case dstage_getHeader: + { + if (srcEnd-srcPtr >= 7) + { + selectedIn = srcPtr; + srcPtr += 7; + dctxPtr->dStage = dstage_decodeHeader; + break; + } + dctxPtr->tmpInSize = 0; + dctxPtr->dStage = dstage_storeHeader; + break; + } + + case dstage_storeHeader: + { + size_t sizeToCopy = 7 - dctxPtr->tmpInSize; + if (sizeToCopy > (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr; + memcpy(dctxPtr->header + dctxPtr->tmpInSize, srcPtr, sizeToCopy); + dctxPtr->tmpInSize += sizeToCopy; + srcPtr += sizeToCopy; + if (dctxPtr->tmpInSize < 7) + { + nextSrcSizeHint = (7 - dctxPtr->tmpInSize) + 4; + doAnotherStage = 0; /* no enough src, wait to get some more */ + break; + } + selectedIn = dctxPtr->header; + dctxPtr->dStage = dstage_decodeHeader; + break; + } + + case dstage_decodeHeader: + { + LZ4F_errorCode_t errorCode = LZ4F_decodeHeader(dctxPtr, selectedIn, 7); + if (LZ4F_isError(errorCode)) return errorCode; + dctxPtr->dStage = dstage_getCBlockSize; + break; + } + + case dstage_getCBlockSize: + { + if ((srcEnd - srcPtr) >= 4) + { + selectedIn = srcPtr; + srcPtr += 4; + dctxPtr->dStage = dstage_decodeCBlockSize; + break; + } + /* not enough input to read cBlockSize field */ + dctxPtr->tmpInSize = 0; + dctxPtr->dStage = dstage_storeCBlockSize; + break; + } + + case dstage_storeCBlockSize: + { + size_t sizeToCopy = 4 - dctxPtr->tmpInSize; + if (sizeToCopy > (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr; + memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy); + srcPtr += sizeToCopy; + dctxPtr->tmpInSize += sizeToCopy; + if (dctxPtr->tmpInSize < 4) /* not enough input to get full cBlockSize; wait for more */ + { + nextSrcSizeHint = 4 - dctxPtr->tmpInSize; + doAnotherStage=0; + break; + } + selectedIn = dctxPtr->tmpIn; + dctxPtr->dStage = dstage_decodeCBlockSize; + break; + } + + case dstage_decodeCBlockSize: + { + size_t nextCBlockSize = LZ4F_readLE32(selectedIn) & 0x7FFFFFFFU; + if (nextCBlockSize==0) /* frameEnd signal, no more CBlock */ + { + dctxPtr->dStage = dstage_getSuffix; + break; + } + if (nextCBlockSize > dctxPtr->maxBlockSize) return (size_t)-ERROR_GENERIC; /* invalid cBlockSize */ + dctxPtr->tmpInTarget = nextCBlockSize; + if (LZ4F_readLE32(selectedIn) & LZ4F_BLOCKUNCOMPRESSED_FLAG) + { + dctxPtr->dStage = dstage_copyDirect; + break; + } + dctxPtr->dStage = dstage_getCBlock; + if (dstPtr==dstEnd) + { + nextSrcSizeHint = nextCBlockSize + 4; + doAnotherStage = 0; + } + break; + } + + case dstage_copyDirect: /* uncompressed block */ + { + size_t sizeToCopy = dctxPtr->tmpInTarget; + if ((size_t)(srcEnd-srcPtr) < sizeToCopy) sizeToCopy = srcEnd - srcPtr; /* not enough input to read full block */ + if ((size_t)(dstEnd-dstPtr) < sizeToCopy) sizeToCopy = dstEnd - dstPtr; + memcpy(dstPtr, srcPtr, sizeToCopy); + if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), srcPtr, (U32)sizeToCopy); + + /* dictionary management */ + if (dctxPtr->frameInfo.blockMode==blockLinked) + LZ4F_updateDict(dctxPtr, dstPtr, sizeToCopy, dstStart, 0); + + srcPtr += sizeToCopy; + dstPtr += sizeToCopy; + if (sizeToCopy == dctxPtr->tmpInTarget) /* all copied */ + { + dctxPtr->dStage = dstage_getCBlockSize; + break; + } + dctxPtr->tmpInTarget -= sizeToCopy; /* still need to copy more */ + nextSrcSizeHint = dctxPtr->tmpInTarget + 4; + doAnotherStage = 0; + break; + } + + case dstage_getCBlock: /* entry from dstage_decodeCBlockSize */ + { + if ((size_t)(srcEnd-srcPtr) < dctxPtr->tmpInTarget) + { + dctxPtr->tmpInSize = 0; + dctxPtr->dStage = dstage_storeCBlock; + break; + } + selectedIn = srcPtr; + srcPtr += dctxPtr->tmpInTarget; + dctxPtr->dStage = dstage_decodeCBlock; + break; + } + + case dstage_storeCBlock: + { + size_t sizeToCopy = dctxPtr->tmpInTarget - dctxPtr->tmpInSize; + if (sizeToCopy > (size_t)(srcEnd-srcPtr)) sizeToCopy = srcEnd-srcPtr; + memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy); + dctxPtr->tmpInSize += sizeToCopy; + srcPtr += sizeToCopy; + if (dctxPtr->tmpInSize < dctxPtr->tmpInTarget) /* need more input */ + { + nextSrcSizeHint = (dctxPtr->tmpInTarget - dctxPtr->tmpInSize) + 4; + doAnotherStage=0; + break; + } + selectedIn = dctxPtr->tmpIn; + dctxPtr->dStage = dstage_decodeCBlock; + break; + } + + case dstage_decodeCBlock: + { + if ((size_t)(dstEnd-dstPtr) < dctxPtr->maxBlockSize) /* not enough place into dst : decode into tmpOut */ + dctxPtr->dStage = dstage_decodeCBlock_intoTmp; + else + dctxPtr->dStage = dstage_decodeCBlock_intoDst; + break; + } + + case dstage_decodeCBlock_intoDst: + { + int (*decoder)(const char*, char*, int, int, const char*, int); + int decodedSize; + + if (dctxPtr->frameInfo.blockMode == blockLinked) + decoder = LZ4_decompress_safe_usingDict; + else + decoder = LZ4F_decompress_safe; + + decodedSize = decoder((const char*)selectedIn, (char*)dstPtr, (int)dctxPtr->tmpInTarget, (int)dctxPtr->maxBlockSize, (const char*)dctxPtr->dict, (int)dctxPtr->dictSize); + if (decodedSize < 0) return (size_t)-ERROR_GENERIC; /* decompression failed */ + if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), dstPtr, decodedSize); + + /* dictionary management */ + if (dctxPtr->frameInfo.blockMode==blockLinked) + LZ4F_updateDict(dctxPtr, dstPtr, decodedSize, dstStart, 0); + + dstPtr += decodedSize; + dctxPtr->dStage = dstage_getCBlockSize; + break; + } + + case dstage_decodeCBlock_intoTmp: + { + /* not enough place into dst : decode into tmpOut */ + int (*decoder)(const char*, char*, int, int, const char*, int); + int decodedSize; + + if (dctxPtr->frameInfo.blockMode == blockLinked) + decoder = LZ4_decompress_safe_usingDict; + else + decoder = LZ4F_decompress_safe; + + /* ensure enough place for tmpOut */ + if (dctxPtr->frameInfo.blockMode == blockLinked) + { + if (dctxPtr->dict == dctxPtr->tmpOutBuffer) + { + if (dctxPtr->dictSize > 128 KB) + { + memcpy(dctxPtr->dict, dctxPtr->dict + dctxPtr->dictSize - 64 KB, 64 KB); + dctxPtr->dictSize = 64 KB; + } + dctxPtr->tmpOut = dctxPtr->dict + dctxPtr->dictSize; + } + else /* dict not within tmp */ + { + size_t reservedDictSpace = dctxPtr->dictSize; + if (reservedDictSpace > 64 KB) reservedDictSpace = 64 KB; + dctxPtr->tmpOut = dctxPtr->tmpOutBuffer + reservedDictSpace; + } + } + + /* Decode */ + decodedSize = decoder((const char*)selectedIn, (char*)dctxPtr->tmpOut, (int)dctxPtr->tmpInTarget, (int)dctxPtr->maxBlockSize, (const char*)dctxPtr->dict, (int)dctxPtr->dictSize); + if (decodedSize < 0) return (size_t)-ERROR_decompressionFailed; /* decompression failed */ + if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), dctxPtr->tmpOut, decodedSize); + dctxPtr->tmpOutSize = decodedSize; + dctxPtr->tmpOutStart = 0; + dctxPtr->dStage = dstage_flushOut; + break; + } + + case dstage_flushOut: /* flush decoded data from tmpOut to dstBuffer */ + { + size_t sizeToCopy = dctxPtr->tmpOutSize - dctxPtr->tmpOutStart; + if (sizeToCopy > (size_t)(dstEnd-dstPtr)) sizeToCopy = dstEnd-dstPtr; + memcpy(dstPtr, dctxPtr->tmpOut + dctxPtr->tmpOutStart, sizeToCopy); + + /* dictionary management */ + if (dctxPtr->frameInfo.blockMode==blockLinked) + LZ4F_updateDict(dctxPtr, dstPtr, sizeToCopy, dstStart, 1); + + dctxPtr->tmpOutStart += sizeToCopy; + dstPtr += sizeToCopy; + + /* end of flush ? */ + if (dctxPtr->tmpOutStart == dctxPtr->tmpOutSize) + { + dctxPtr->dStage = dstage_getCBlockSize; + break; + } + nextSrcSizeHint = 4; + doAnotherStage = 0; /* still some data to flush */ + break; + } + + case dstage_getSuffix: + { + size_t suffixSize = dctxPtr->frameInfo.contentChecksumFlag * 4; + if (suffixSize == 0) /* frame completed */ + { + nextSrcSizeHint = 0; + dctxPtr->dStage = dstage_getHeader; + doAnotherStage = 0; + break; + } + if ((srcEnd - srcPtr) >= 4) /* CRC present */ + { + selectedIn = srcPtr; + srcPtr += 4; + dctxPtr->dStage = dstage_checkSuffix; + break; + } + dctxPtr->tmpInSize = 0; + dctxPtr->dStage = dstage_storeSuffix; + break; + } + + case dstage_storeSuffix: + { + size_t sizeToCopy = 4 - dctxPtr->tmpInSize; + if (sizeToCopy > (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr; + memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy); + srcPtr += sizeToCopy; + dctxPtr->tmpInSize += sizeToCopy; + if (dctxPtr->tmpInSize < 4) /* not enough input to read complete suffix */ + { + nextSrcSizeHint = 4 - dctxPtr->tmpInSize; + doAnotherStage=0; + break; + } + selectedIn = dctxPtr->tmpIn; + dctxPtr->dStage = dstage_checkSuffix; + break; + } + + case dstage_checkSuffix: + { + U32 readCRC = LZ4F_readLE32(selectedIn); + U32 resultCRC = XXH32_digest(&(dctxPtr->xxh)); + if (readCRC != resultCRC) return (size_t)-ERROR_checksum_invalid; + nextSrcSizeHint = 0; + dctxPtr->dStage = dstage_getHeader; + doAnotherStage = 0; + break; + } + } + } + + /* preserve dictionary within tmp if necessary */ + if ( (dctxPtr->frameInfo.blockMode==blockLinked) + &&(dctxPtr->dict != dctxPtr->tmpOutBuffer) + &&(!decompressOptionsPtr->stableDst) + &&((unsigned)(dctxPtr->dStage-1) < (unsigned)(dstage_getSuffix-1)) + ) + { + if (dctxPtr->dStage == dstage_flushOut) + { + size_t preserveSize = dctxPtr->tmpOut - dctxPtr->tmpOutBuffer; + size_t copySize = 64 KB - dctxPtr->tmpOutSize; + BYTE* oldDictEnd = dctxPtr->dict + dctxPtr->dictSize - dctxPtr->tmpOutStart; + if (dctxPtr->tmpOutSize > 64 KB) copySize = 0; + if (copySize > preserveSize) copySize = preserveSize; + + memcpy(dctxPtr->tmpOutBuffer + preserveSize - copySize, oldDictEnd - copySize, copySize); + + dctxPtr->dict = dctxPtr->tmpOutBuffer; + dctxPtr->dictSize = preserveSize + dctxPtr->tmpOutStart; + } + else + { + size_t newDictSize = dctxPtr->dictSize; + BYTE* oldDictEnd = dctxPtr->dict + dctxPtr->dictSize; + if ((newDictSize) > 64 KB) newDictSize = 64 KB; + + memcpy(dctxPtr->tmpOutBuffer, oldDictEnd - newDictSize, newDictSize); + + dctxPtr->dict = dctxPtr->tmpOutBuffer; + dctxPtr->dictSize = newDictSize; + dctxPtr->tmpOut = dctxPtr->tmpOutBuffer + newDictSize; + } + } + + if (srcPtrsrcExpect = srcPtr; + else + dctxPtr->srcExpect = NULL; + *srcSizePtr = (srcPtr - srcStart); + *dstSizePtr = (dstPtr - dstStart); + return nextSrcSizeHint; +} diff --git a/lib/lz4frame.h b/lib/lz4frame.h new file mode 100644 index 0000000000..d73e3e2865 --- /dev/null +++ b/lib/lz4frame.h @@ -0,0 +1,252 @@ +/* + LZ4 auto-framing library + Header File + Copyright (C) 2011-2015, Yann Collet. + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - LZ4 source repository : http://code.google.com/p/lz4/ + - LZ4 source mirror : https://github.com/Cyan4973/lz4 + - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c +*/ + +/* LZ4F is a stand-alone API to create LZ4-compressed frames + * fully conformant to specification v1.4.1. + * All related operations, including memory management, are handled by the library. + * You don't need lz4.h when using lz4frame.h. + * */ + +#pragma once + +#if defined (__cplusplus) +extern "C" { +#endif + +/************************************** + Includes +**************************************/ +#include /* size_t */ + + +/************************************** + * Error management + * ************************************/ +typedef size_t LZ4F_errorCode_t; + +unsigned LZ4F_isError(LZ4F_errorCode_t code); +const char* LZ4F_getErrorName(LZ4F_errorCode_t code); /* return error code string; useful for debugging */ + + +/************************************** + * Frame compression types + * ************************************/ +typedef enum { LZ4F_default=0, max64KB=4, max256KB=5, max1MB=6, max4MB=7 } blockSizeID_t; +typedef enum { blockLinked=0, blockIndependent} blockMode_t; +typedef enum { noContentChecksum=0, contentChecksumEnabled } contentChecksum_t; + +typedef struct { + blockSizeID_t blockSizeID; /* max64KB, max256KB, max1MB, max4MB ; 0 == default */ + blockMode_t blockMode; /* blockLinked, blockIndependent ; 0 == default */ + contentChecksum_t contentChecksumFlag; /* noContentChecksum, contentChecksumEnabled ; 0 == default */ + unsigned reserved[5]; +} LZ4F_frameInfo_t; + +typedef struct { + LZ4F_frameInfo_t frameInfo; + unsigned compressionLevel; /* 0 == default (fast mode); values above 16 count as 16 */ + unsigned autoFlush; /* 1 == always flush : reduce need for tmp buffer */ + unsigned reserved[4]; +} LZ4F_preferences_t; + + +/*********************************** + * Simple compression function + * *********************************/ +size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr); + +size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_preferences_t* preferencesPtr); +/* LZ4F_compressFrame() + * Compress an entire srcBuffer into a valid LZ4 frame, as defined by specification v1.4.1. + * The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case. + * You can get the minimum value of dstMaxSize by using LZ4F_compressFrameBound() + * If this condition is not respected, LZ4F_compressFrame() will fail (result is an errorCode) + * The LZ4F_preferences_t structure is optional : you can provide NULL as argument. All preferences will be set to default. + * The result of the function is the number of bytes written into dstBuffer. + * The function outputs an error code if it fails (can be tested using LZ4F_isError()) + */ + + + +/********************************** + * Advanced compression functions + * ********************************/ +typedef void* LZ4F_compressionContext_t; + +typedef struct { + unsigned stableSrc; /* 1 == src content will remain available on future calls to LZ4F_compress(); avoid saving src content within tmp buffer as future dictionary */ + unsigned reserved[3]; +} LZ4F_compressOptions_t; + +/* Resource Management */ + +#define LZ4F_VERSION 100 +LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_compressionContext_t* LZ4F_compressionContextPtr, unsigned version); +LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t LZ4F_compressionContext); +/* LZ4F_createCompressionContext() : + * The first thing to do is to create a compressionContext object, which will be used in all compression operations. + * This is achieved using LZ4F_createCompressionContext(), which takes as argument a version and an LZ4F_preferences_t structure. + * The version provided MUST be LZ4F_VERSION. It is intended to track potential version differences between different binaries. + * The function will provide a pointer to a fully allocated LZ4F_compressionContext_t object. + * If the result LZ4F_errorCode_t is not zero, there was an error during context creation. + * Object can release its memory using LZ4F_freeCompressionContext(); + */ + + +/* Compression */ + +size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_preferences_t* preferencesPtr); +/* LZ4F_compressBegin() : + * will write the frame header into dstBuffer. + * dstBuffer must be large enough to accommodate a header (dstMaxSize). Maximum header size is 19 bytes. + * The LZ4F_preferences_t structure is optional : you can provide NULL as argument, all preferences will then be set to default. + * The result of the function is the number of bytes written into dstBuffer for the header + * or an error code (can be tested using LZ4F_isError()) + */ + +size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr); +/* LZ4F_compressBound() : + * Provides the minimum size of Dst buffer given srcSize to handle worst case situations. + * preferencesPtr is optional : you can provide NULL as argument, all preferences will then be set to default. + * Note that different preferences will produce in different results. + */ + +size_t LZ4F_compressUpdate(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptionsPtr); +/* LZ4F_compressUpdate() + * LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary. + * The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case. + * If this condition is not respected, LZ4F_compress() will fail (result is an errorCode) + * You can get the minimum value of dstMaxSize by using LZ4F_compressBound() + * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. + * The result of the function is the number of bytes written into dstBuffer : it can be zero, meaning input data was just buffered. + * The function outputs an error code if it fails (can be tested using LZ4F_isError()) + */ + +size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptionsPtr); +/* LZ4F_flush() + * Should you need to create compressed data immediately, without waiting for a block to be filled, + * you can call LZ4_flush(), which will immediately compress any remaining data buffered within compressionContext. + * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. + * The result of the function is the number of bytes written into dstBuffer + * (it can be zero, this means there was no data left within compressionContext) + * The function outputs an error code if it fails (can be tested using LZ4F_isError()) + */ + +size_t LZ4F_compressEnd(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptionsPtr); +/* LZ4F_compressEnd() + * When you want to properly finish the compressed frame, just call LZ4F_compressEnd(). + * It will flush whatever data remained within compressionContext (like LZ4_flush()) + * but also properly finalize the frame, with an endMark and a checksum. + * The result of the function is the number of bytes written into dstBuffer (necessarily >= 4 (endMark size)) + * The function outputs an error code if it fails (can be tested using LZ4F_isError()) + * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. + * compressionContext can then be used again, starting with LZ4F_compressBegin(). + */ + + +/*********************************** + * Decompression functions + * *********************************/ + +typedef void* LZ4F_decompressionContext_t; + +typedef struct { + unsigned stableDst; /* guarantee that decompressed data will still be there on next function calls (avoid storage into tmp buffers) */ + unsigned reserved[3]; +} LZ4F_decompressOptions_t; + + +/* Resource management */ + +LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_decompressionContext_t* ctxPtr, unsigned version); +LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_decompressionContext_t ctx); +/* LZ4F_createDecompressionContext() : + * The first thing to do is to create a decompressionContext object, which will be used in all decompression operations. + * This is achieved using LZ4F_createDecompressionContext(). + * The version provided MUST be LZ4F_VERSION. It is intended to track potential version differences between different binaries. + * The function will provide a pointer to a fully allocated and initialized LZ4F_decompressionContext_t object. + * If the result LZ4F_errorCode_t is not OK_NoError, there was an error during context creation. + * Object can release its memory using LZ4F_freeDecompressionContext(); + */ + + +/* Decompression */ + +size_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t ctx, + LZ4F_frameInfo_t* frameInfoPtr, + const void* srcBuffer, size_t* srcSizePtr); +/* LZ4F_getFrameInfo() + * This function decodes frame header information, such as blockSize. + * It is optional : you could start by calling directly LZ4F_decompress() instead. + * The objective is to extract header information without starting decompression, typically for allocation purposes. + * LZ4F_getFrameInfo() can also be used *after* starting decompression, on a valid LZ4F_decompressionContext_t. + * The number of bytes read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value). + * You are expected to resume decompression from where it stopped (srcBuffer + *srcSizePtr) + * The function result is an hint of how many srcSize bytes LZ4F_decompress() expects for next call, + * or an error code which can be tested using LZ4F_isError(). + */ + +size_t LZ4F_decompress(LZ4F_decompressionContext_t ctx, + void* dstBuffer, size_t* dstSizePtr, + const void* srcBuffer, size_t* srcSizePtr, + const LZ4F_decompressOptions_t* optionsPtr); +/* LZ4F_decompress() + * Call this function repetitively to regenerate data compressed within srcBuffer. + * The function will attempt to decode *srcSizePtr bytes from srcBuffer, into dstBuffer of maximum size *dstSizePtr. + * + * The number of bytes regenerated into dstBuffer will be provided within *dstSizePtr (necessarily <= original value). + * + * The number of bytes read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value). + * If number of bytes read is < number of bytes provided, then decompression operation is not completed. + * It typically happens when dstBuffer is not large enough to contain all decoded data. + * LZ4F_decompress() must be called again, starting from where it stopped (srcBuffer + *srcSizePtr) + * The function will check this condition, and refuse to continue if it is not respected. + * + * dstBuffer is supposed to be flushed between each call to the function, since its content will be overwritten. + * dst arguments can be changed at will with each consecutive call to the function. + * + * The function result is an hint of how many srcSize bytes LZ4F_decompress() expects for next call. + * Schematically, it's the size of the current (or remaining) compressed block + header of next block. + * Respecting the hint provides some boost to performance, since it does skip intermediate buffers. + * This is just a hint, you can always provide any srcSize you want. + * When a frame is fully decoded, the function result will be 0. (no more data expected) + * If decompression failed, function result is an error code, which can be tested using LZ4F_isError(). + */ + + + +#if defined (__cplusplus) +} +#endif diff --git a/lib/lz4frame_static.h b/lib/lz4frame_static.h new file mode 100644 index 0000000000..cde81862a5 --- /dev/null +++ b/lib/lz4frame_static.h @@ -0,0 +1,72 @@ +/* + LZ4 auto-framing library + Header File for static linking only + Copyright (C) 2011-2015, Yann Collet. + + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - LZ4 source repository : http://code.google.com/p/lz4/ + - LZ4 source mirror : https://github.com/Cyan4973/lz4 + - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c +*/ + +#pragma once + +#if defined (__cplusplus) +extern "C" { +#endif + +/* lz4frame_static.h should be used solely in the context of static linking. + * */ + + +/************************************** + * Error management + * ************************************/ +#define LZ4F_LIST_ERRORS(ITEM) \ + ITEM(OK_NoError) ITEM(ERROR_GENERIC) \ + ITEM(ERROR_maxBlockSize_invalid) ITEM(ERROR_blockMode_invalid) ITEM(ERROR_contentChecksumFlag_invalid) \ + ITEM(ERROR_compressionLevel_invalid) \ + ITEM(ERROR_allocation_failed) \ + ITEM(ERROR_srcSize_tooLarge) ITEM(ERROR_dstMaxSize_tooSmall) \ + ITEM(ERROR_decompressionFailed) \ + ITEM(ERROR_checksum_invalid) \ + ITEM(ERROR_maxCode) + +#define LZ4F_GENERATE_ENUM(ENUM) ENUM, +typedef enum { LZ4F_LIST_ERRORS(LZ4F_GENERATE_ENUM) } LZ4F_errorCodes; /* enum is exposed, to handle specific errors; compare function result to -enum value */ + + +/************************************** + Includes +**************************************/ +#include "lz4frame.h" + + +#if defined (__cplusplus) +} +#endif diff --git a/lib/lz4hc.c b/lib/lz4hc.c new file mode 100644 index 0000000000..5549969c0c --- /dev/null +++ b/lib/lz4hc.c @@ -0,0 +1,751 @@ +/* +LZ4 HC - High Compression Mode of LZ4 +Copyright (C) 2011-2014, Yann Collet. +BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +* Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +You can contact the author at : +- LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html +- LZ4 source repository : http://code.google.com/p/lz4/ +*/ + + + +/************************************** + Tuning Parameter +**************************************/ +static const int LZ4HC_compressionLevel_default = 8; + + +/************************************** + Includes +**************************************/ +#include "lz4hc.h" + + +/************************************** + Local Compiler Options +**************************************/ +#if defined(__GNUC__) +# pragma GCC diagnostic ignored "-Wunused-function" +#endif + +#if defined (__clang__) +# pragma clang diagnostic ignored "-Wunused-function" +#endif + + +/************************************** + Common LZ4 definition +**************************************/ +#define LZ4_COMMONDEFS_ONLY +#include "lz4.c" + + +/************************************** + Local Constants +**************************************/ +#define DICTIONARY_LOGSIZE 16 +#define MAXD (1<> ((MINMATCH*8)-HASH_LOG)) +#define DELTANEXT(p) chainTable[(size_t)(p) & MAXD_MASK] +#define GETNEXT(p) ((p) - (size_t)DELTANEXT(p)) + +static U32 LZ4HC_hashPtr(const void* ptr) { return HASH_FUNCTION(LZ4_read32(ptr)); } + + + +/************************************** + HC Compression +**************************************/ +static void LZ4HC_init (LZ4HC_Data_Structure* hc4, const BYTE* start) +{ + MEM_INIT((void*)hc4->hashTable, 0, sizeof(hc4->hashTable)); + MEM_INIT(hc4->chainTable, 0xFF, sizeof(hc4->chainTable)); + hc4->nextToUpdate = 64 KB; + hc4->base = start - 64 KB; + hc4->inputBuffer = start; + hc4->end = start; + hc4->dictBase = start - 64 KB; + hc4->dictLimit = 64 KB; + hc4->lowLimit = 64 KB; +} + + +/* Update chains up to ip (excluded) */ +FORCE_INLINE void LZ4HC_Insert (LZ4HC_Data_Structure* hc4, const BYTE* ip) +{ + U16* chainTable = hc4->chainTable; + U32* HashTable = hc4->hashTable; + const BYTE* const base = hc4->base; + const U32 target = (U32)(ip - base); + U32 idx = hc4->nextToUpdate; + + while(idx < target) + { + U32 h = LZ4HC_hashPtr(base+idx); + size_t delta = idx - HashTable[h]; + if (delta>MAX_DISTANCE) delta = MAX_DISTANCE; + chainTable[idx & 0xFFFF] = (U16)delta; + HashTable[h] = idx; + idx++; + } + + hc4->nextToUpdate = target; +} + + +FORCE_INLINE int LZ4HC_InsertAndFindBestMatch (LZ4HC_Data_Structure* hc4, /* Index table will be updated */ + const BYTE* ip, const BYTE* const iLimit, + const BYTE** matchpos, + const int maxNbAttempts) +{ + U16* const chainTable = hc4->chainTable; + U32* const HashTable = hc4->hashTable; + const BYTE* const base = hc4->base; + const BYTE* const dictBase = hc4->dictBase; + const U32 dictLimit = hc4->dictLimit; + const U32 lowLimit = (hc4->lowLimit + 64 KB > (U32)(ip-base)) ? hc4->lowLimit : (U32)(ip - base) - (64 KB - 1); + U32 matchIndex; + const BYTE* match; + int nbAttempts=maxNbAttempts; + size_t ml=0; + + /* HC4 match finder */ + LZ4HC_Insert(hc4, ip); + matchIndex = HashTable[LZ4HC_hashPtr(ip)]; + + while ((matchIndex>=lowLimit) && (nbAttempts)) + { + nbAttempts--; + if (matchIndex >= dictLimit) + { + match = base + matchIndex; + if (*(match+ml) == *(ip+ml) + && (LZ4_read32(match) == LZ4_read32(ip))) + { + size_t mlt = LZ4_count(ip+MINMATCH, match+MINMATCH, iLimit) + MINMATCH; + if (mlt > ml) { ml = mlt; *matchpos = match; } + } + } + else + { + match = dictBase + matchIndex; + if (LZ4_read32(match) == LZ4_read32(ip)) + { + size_t mlt; + const BYTE* vLimit = ip + (dictLimit - matchIndex); + if (vLimit > iLimit) vLimit = iLimit; + mlt = LZ4_count(ip+MINMATCH, match+MINMATCH, vLimit) + MINMATCH; + if ((ip+mlt == vLimit) && (vLimit < iLimit)) + mlt += LZ4_count(ip+mlt, base+dictLimit, iLimit); + if (mlt > ml) { ml = mlt; *matchpos = base + matchIndex; } /* virtual matchpos */ + } + } + matchIndex -= chainTable[matchIndex & 0xFFFF]; + } + + return (int)ml; +} + + +FORCE_INLINE int LZ4HC_InsertAndGetWiderMatch ( + LZ4HC_Data_Structure* hc4, + const BYTE* ip, + const BYTE* iLowLimit, + const BYTE* iHighLimit, + int longest, + const BYTE** matchpos, + const BYTE** startpos, + const int maxNbAttempts) +{ + U16* const chainTable = hc4->chainTable; + U32* const HashTable = hc4->hashTable; + const BYTE* const base = hc4->base; + const U32 dictLimit = hc4->dictLimit; + const U32 lowLimit = (hc4->lowLimit + 64 KB > (U32)(ip-base)) ? hc4->lowLimit : (U32)(ip - base) - (64 KB - 1); + const BYTE* const dictBase = hc4->dictBase; + const BYTE* match; + U32 matchIndex; + int nbAttempts = maxNbAttempts; + int delta = (int)(ip-iLowLimit); + + + /* First Match */ + LZ4HC_Insert(hc4, ip); + matchIndex = HashTable[LZ4HC_hashPtr(ip)]; + + while ((matchIndex>=lowLimit) && (nbAttempts)) + { + nbAttempts--; + if (matchIndex >= dictLimit) + { + match = base + matchIndex; + if (*(iLowLimit + longest) == *(match - delta + longest)) + if (LZ4_read32(match) == LZ4_read32(ip)) + { + const BYTE* startt = ip; + const BYTE* tmpMatch = match; + const BYTE* const matchEnd = ip + MINMATCH + LZ4_count(ip+MINMATCH, match+MINMATCH, iHighLimit); + + while ((startt>iLowLimit) && (tmpMatch > iLowLimit) && (startt[-1] == tmpMatch[-1])) {startt--; tmpMatch--;} + + if ((matchEnd-startt) > longest) + { + longest = (int)(matchEnd-startt); + *matchpos = tmpMatch; + *startpos = startt; + } + } + } + else + { + match = dictBase + matchIndex; + if (LZ4_read32(match) == LZ4_read32(ip)) + { + size_t mlt; + int back=0; + const BYTE* vLimit = ip + (dictLimit - matchIndex); + if (vLimit > iHighLimit) vLimit = iHighLimit; + mlt = LZ4_count(ip+MINMATCH, match+MINMATCH, vLimit) + MINMATCH; + if ((ip+mlt == vLimit) && (vLimit < iHighLimit)) + mlt += LZ4_count(ip+mlt, base+dictLimit, iHighLimit); + while ((ip+back > iLowLimit) && (matchIndex+back > lowLimit) && (ip[back-1] == match[back-1])) back--; + mlt -= back; + if ((int)mlt > longest) { longest = (int)mlt; *matchpos = base + matchIndex + back; *startpos = ip+back; } + } + } + matchIndex -= chainTable[matchIndex & 0xFFFF]; + } + + return longest; +} + + +typedef enum { noLimit = 0, limitedOutput = 1 } limitedOutput_directive; + +#define LZ4HC_DEBUG 0 +#if LZ4HC_DEBUG +static unsigned debug = 0; +#endif + +FORCE_INLINE int LZ4HC_encodeSequence ( + const BYTE** ip, + BYTE** op, + const BYTE** anchor, + int matchLength, + const BYTE* const match, + limitedOutput_directive limitedOutputBuffer, + BYTE* oend) +{ + int length; + BYTE* token; + +#if LZ4HC_DEBUG + if (debug) printf("literal : %u -- match : %u -- offset : %u\n", (U32)(*ip - *anchor), (U32)matchLength, (U32)(*ip-match)); +#endif + + /* Encode Literal length */ + length = (int)(*ip - *anchor); + token = (*op)++; + if ((limitedOutputBuffer) && ((*op + (length>>8) + length + (2 + 1 + LASTLITERALS)) > oend)) return 1; /* Check output limit */ + if (length>=(int)RUN_MASK) { int len; *token=(RUN_MASK< 254 ; len-=255) *(*op)++ = 255; *(*op)++ = (BYTE)len; } + else *token = (BYTE)(length<>8) + (1 + LASTLITERALS) > oend)) return 1; /* Check output limit */ + if (length>=(int)ML_MASK) { *token+=ML_MASK; length-=ML_MASK; for(; length > 509 ; length-=510) { *(*op)++ = 255; *(*op)++ = 255; } if (length > 254) { length-=255; *(*op)++ = 255; } *(*op)++ = (BYTE)length; } + else *token += (BYTE)(length); + + /* Prepare next loop */ + *ip += matchLength; + *anchor = *ip; + + return 0; +} + + +static int LZ4HC_compress_generic ( + void* ctxvoid, + const char* source, + char* dest, + int inputSize, + int maxOutputSize, + int compressionLevel, + limitedOutput_directive limit + ) +{ + LZ4HC_Data_Structure* ctx = (LZ4HC_Data_Structure*) ctxvoid; + const BYTE* ip = (const BYTE*) source; + const BYTE* anchor = ip; + const BYTE* const iend = ip + inputSize; + const BYTE* const mflimit = iend - MFLIMIT; + const BYTE* const matchlimit = (iend - LASTLITERALS); + + BYTE* op = (BYTE*) dest; + BYTE* const oend = op + maxOutputSize; + + unsigned maxNbAttempts; + int ml, ml2, ml3, ml0; + const BYTE* ref=NULL; + const BYTE* start2=NULL; + const BYTE* ref2=NULL; + const BYTE* start3=NULL; + const BYTE* ref3=NULL; + const BYTE* start0; + const BYTE* ref0; + + + /* init */ + if (compressionLevel > g_maxCompressionLevel) compressionLevel = g_maxCompressionLevel; + if (compressionLevel < 1) compressionLevel = LZ4HC_compressionLevel_default; + maxNbAttempts = 1 << (compressionLevel-1); + ctx->end += inputSize; + + ip++; + + /* Main Loop */ + while (ip < mflimit) + { + ml = LZ4HC_InsertAndFindBestMatch (ctx, ip, matchlimit, (&ref), maxNbAttempts); + if (!ml) { ip++; continue; } + + /* saved, in case we would skip too much */ + start0 = ip; + ref0 = ref; + ml0 = ml; + +_Search2: + if (ip+ml < mflimit) + ml2 = LZ4HC_InsertAndGetWiderMatch(ctx, ip + ml - 2, ip + 1, matchlimit, ml, &ref2, &start2, maxNbAttempts); + else ml2 = ml; + + if (ml2 == ml) /* No better match */ + { + if (LZ4HC_encodeSequence(&ip, &op, &anchor, ml, ref, limit, oend)) return 0; + continue; + } + + if (start0 < ip) + { + if (start2 < ip + ml0) /* empirical */ + { + ip = start0; + ref = ref0; + ml = ml0; + } + } + + /* Here, start0==ip */ + if ((start2 - ip) < 3) /* First Match too small : removed */ + { + ml = ml2; + ip = start2; + ref =ref2; + goto _Search2; + } + +_Search3: + /* + * Currently we have : + * ml2 > ml1, and + * ip1+3 <= ip2 (usually < ip1+ml1) + */ + if ((start2 - ip) < OPTIMAL_ML) + { + int correction; + int new_ml = ml; + if (new_ml > OPTIMAL_ML) new_ml = OPTIMAL_ML; + if (ip+new_ml > start2 + ml2 - MINMATCH) new_ml = (int)(start2 - ip) + ml2 - MINMATCH; + correction = new_ml - (int)(start2 - ip); + if (correction > 0) + { + start2 += correction; + ref2 += correction; + ml2 -= correction; + } + } + /* Now, we have start2 = ip+new_ml, with new_ml = min(ml, OPTIMAL_ML=18) */ + + if (start2 + ml2 < mflimit) + ml3 = LZ4HC_InsertAndGetWiderMatch(ctx, start2 + ml2 - 3, start2, matchlimit, ml2, &ref3, &start3, maxNbAttempts); + else ml3 = ml2; + + if (ml3 == ml2) /* No better match : 2 sequences to encode */ + { + /* ip & ref are known; Now for ml */ + if (start2 < ip+ml) ml = (int)(start2 - ip); + /* Now, encode 2 sequences */ + if (LZ4HC_encodeSequence(&ip, &op, &anchor, ml, ref, limit, oend)) return 0; + ip = start2; + if (LZ4HC_encodeSequence(&ip, &op, &anchor, ml2, ref2, limit, oend)) return 0; + continue; + } + + if (start3 < ip+ml+3) /* Not enough space for match 2 : remove it */ + { + if (start3 >= (ip+ml)) /* can write Seq1 immediately ==> Seq2 is removed, so Seq3 becomes Seq1 */ + { + if (start2 < ip+ml) + { + int correction = (int)(ip+ml - start2); + start2 += correction; + ref2 += correction; + ml2 -= correction; + if (ml2 < MINMATCH) + { + start2 = start3; + ref2 = ref3; + ml2 = ml3; + } + } + + if (LZ4HC_encodeSequence(&ip, &op, &anchor, ml, ref, limit, oend)) return 0; + ip = start3; + ref = ref3; + ml = ml3; + + start0 = start2; + ref0 = ref2; + ml0 = ml2; + goto _Search2; + } + + start2 = start3; + ref2 = ref3; + ml2 = ml3; + goto _Search3; + } + + /* + * OK, now we have 3 ascending matches; let's write at least the first one + * ip & ref are known; Now for ml + */ + if (start2 < ip+ml) + { + if ((start2 - ip) < (int)ML_MASK) + { + int correction; + if (ml > OPTIMAL_ML) ml = OPTIMAL_ML; + if (ip + ml > start2 + ml2 - MINMATCH) ml = (int)(start2 - ip) + ml2 - MINMATCH; + correction = ml - (int)(start2 - ip); + if (correction > 0) + { + start2 += correction; + ref2 += correction; + ml2 -= correction; + } + } + else + { + ml = (int)(start2 - ip); + } + } + if (LZ4HC_encodeSequence(&ip, &op, &anchor, ml, ref, limit, oend)) return 0; + + ip = start2; + ref = ref2; + ml = ml2; + + start2 = start3; + ref2 = ref3; + ml2 = ml3; + + goto _Search3; + } + + /* Encode Last Literals */ + { + int lastRun = (int)(iend - anchor); + if ((limit) && (((char*)op - dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize)) return 0; /* Check output limit */ + if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK< 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; } + else *op++ = (BYTE)(lastRun<base = NULL; + ((LZ4HC_Data_Structure*)LZ4_streamHCPtr)->compressionLevel = (unsigned)compressionLevel; +} + +int LZ4_loadDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, const char* dictionary, int dictSize) +{ + LZ4HC_Data_Structure* ctxPtr = (LZ4HC_Data_Structure*) LZ4_streamHCPtr; + if (dictSize > 64 KB) + { + dictionary += dictSize - 64 KB; + dictSize = 64 KB; + } + LZ4HC_init (ctxPtr, (const BYTE*)dictionary); + if (dictSize >= 4) LZ4HC_Insert (ctxPtr, (const BYTE*)dictionary +(dictSize-3)); + ctxPtr->end = (const BYTE*)dictionary + dictSize; + return dictSize; +} + + +/* compression */ + +static void LZ4HC_setExternalDict(LZ4HC_Data_Structure* ctxPtr, const BYTE* newBlock) +{ + if (ctxPtr->end >= ctxPtr->base + 4) + LZ4HC_Insert (ctxPtr, ctxPtr->end-3); /* Referencing remaining dictionary content */ + /* Only one memory segment for extDict, so any previous extDict is lost at this stage */ + ctxPtr->lowLimit = ctxPtr->dictLimit; + ctxPtr->dictLimit = (U32)(ctxPtr->end - ctxPtr->base); + ctxPtr->dictBase = ctxPtr->base; + ctxPtr->base = newBlock - ctxPtr->dictLimit; + ctxPtr->end = newBlock; + ctxPtr->nextToUpdate = ctxPtr->dictLimit; /* match referencing will resume from there */ +} + +static int LZ4_compressHC_continue_generic (LZ4HC_Data_Structure* ctxPtr, + const char* source, char* dest, + int inputSize, int maxOutputSize, limitedOutput_directive limit) +{ + /* auto-init if forgotten */ + if (ctxPtr->base == NULL) + LZ4HC_init (ctxPtr, (const BYTE*) source); + + /* Check overflow */ + if ((size_t)(ctxPtr->end - ctxPtr->base) > 2 GB) + { + size_t dictSize = (size_t)(ctxPtr->end - ctxPtr->base) - ctxPtr->dictLimit; + if (dictSize > 64 KB) dictSize = 64 KB; + + LZ4_loadDictHC((LZ4_streamHC_t*)ctxPtr, (const char*)(ctxPtr->end) - dictSize, (int)dictSize); + } + + /* Check if blocks follow each other */ + if ((const BYTE*)source != ctxPtr->end) LZ4HC_setExternalDict(ctxPtr, (const BYTE*)source); + + /* Check overlapping input/dictionary space */ + { + const BYTE* sourceEnd = (const BYTE*) source + inputSize; + const BYTE* dictBegin = ctxPtr->dictBase + ctxPtr->lowLimit; + const BYTE* dictEnd = ctxPtr->dictBase + ctxPtr->dictLimit; + if ((sourceEnd > dictBegin) && ((BYTE*)source < dictEnd)) + { + if (sourceEnd > dictEnd) sourceEnd = dictEnd; + ctxPtr->lowLimit = (U32)(sourceEnd - ctxPtr->dictBase); + if (ctxPtr->dictLimit - ctxPtr->lowLimit < 4) ctxPtr->lowLimit = ctxPtr->dictLimit; + } + } + + return LZ4HC_compress_generic (ctxPtr, source, dest, inputSize, maxOutputSize, ctxPtr->compressionLevel, limit); +} + +int LZ4_compressHC_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize) +{ + return LZ4_compressHC_continue_generic ((LZ4HC_Data_Structure*)LZ4_streamHCPtr, source, dest, inputSize, 0, noLimit); +} + +int LZ4_compressHC_limitedOutput_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize, int maxOutputSize) +{ + return LZ4_compressHC_continue_generic ((LZ4HC_Data_Structure*)LZ4_streamHCPtr, source, dest, inputSize, maxOutputSize, limitedOutput); +} + + +/* dictionary saving */ + +int LZ4_saveDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, char* safeBuffer, int dictSize) +{ + LZ4HC_Data_Structure* streamPtr = (LZ4HC_Data_Structure*)LZ4_streamHCPtr; + int prefixSize = (int)(streamPtr->end - (streamPtr->base + streamPtr->dictLimit)); + if (dictSize > 64 KB) dictSize = 64 KB; + if (dictSize < 4) dictSize = 0; + if (dictSize > prefixSize) dictSize = prefixSize; + memcpy(safeBuffer, streamPtr->end - dictSize, dictSize); + { + U32 endIndex = (U32)(streamPtr->end - streamPtr->base); + streamPtr->end = (const BYTE*)safeBuffer + dictSize; + streamPtr->base = streamPtr->end - endIndex; + streamPtr->dictLimit = endIndex - dictSize; + streamPtr->lowLimit = endIndex - dictSize; + if (streamPtr->nextToUpdate < streamPtr->dictLimit) streamPtr->nextToUpdate = streamPtr->dictLimit; + } + return dictSize; +} + + +/*********************************** + * Deprecated Functions + ***********************************/ +int LZ4_sizeofStreamStateHC(void) { return LZ4_STREAMHCSIZE; } + +int LZ4_resetStreamStateHC(void* state, const char* inputBuffer) +{ + if ((((size_t)state) & (sizeof(void*)-1)) != 0) return 1; /* Error : pointer is not aligned for pointer (32 or 64 bits) */ + LZ4HC_init((LZ4HC_Data_Structure*)state, (const BYTE*)inputBuffer); + return 0; +} + +void* LZ4_createHC (const char* inputBuffer) +{ + void* hc4 = ALLOCATOR(1, sizeof(LZ4HC_Data_Structure)); + LZ4HC_init ((LZ4HC_Data_Structure*)hc4, (const BYTE*)inputBuffer); + return hc4; +} + +int LZ4_freeHC (void* LZ4HC_Data) +{ + FREEMEM(LZ4HC_Data); + return (0); +} + +/* +int LZ4_compressHC_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize) +{ +return LZ4HC_compress_generic (LZ4HC_Data, source, dest, inputSize, 0, 0, noLimit); +} +int LZ4_compressHC_limitedOutput_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int maxOutputSize) +{ +return LZ4HC_compress_generic (LZ4HC_Data, source, dest, inputSize, maxOutputSize, 0, limitedOutput); +} +*/ + +int LZ4_compressHC2_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int compressionLevel) +{ + return LZ4HC_compress_generic (LZ4HC_Data, source, dest, inputSize, 0, compressionLevel, noLimit); +} + +int LZ4_compressHC2_limitedOutput_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel) +{ + return LZ4HC_compress_generic (LZ4HC_Data, source, dest, inputSize, maxOutputSize, compressionLevel, limitedOutput); +} + +char* LZ4_slideInputBufferHC(void* LZ4HC_Data) +{ + LZ4HC_Data_Structure* hc4 = (LZ4HC_Data_Structure*)LZ4HC_Data; + int dictSize = LZ4_saveDictHC((LZ4_streamHC_t*)LZ4HC_Data, (char*)(hc4->inputBuffer), 64 KB); + return (char*)(hc4->inputBuffer + dictSize); +} diff --git a/lib/lz4hc.h b/lib/lz4hc.h new file mode 100644 index 0000000000..ce813abaa8 --- /dev/null +++ b/lib/lz4hc.h @@ -0,0 +1,174 @@ +/* + LZ4 HC - High Compression Mode of LZ4 + Header File + Copyright (C) 2011-2014, Yann Collet. + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html + - LZ4 source repository : http://code.google.com/p/lz4/ +*/ +#pragma once + + +#if defined (__cplusplus) +extern "C" { +#endif + + +int LZ4_compressHC (const char* source, char* dest, int inputSize); +/* +LZ4_compressHC : + return : the number of bytes in compressed buffer dest + or 0 if compression fails. + note : destination buffer must be already allocated. + To avoid any problem, size it to handle worst cases situations (input data not compressible) + Worst case size evaluation is provided by function LZ4_compressBound() (see "lz4.h") +*/ + +int LZ4_compressHC_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize); +/* +LZ4_compress_limitedOutput() : + Compress 'inputSize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'. + If it cannot achieve it, compression will stop, and result of the function will be zero. + This function never writes outside of provided output buffer. + + inputSize : Max supported value is 1 GB + maxOutputSize : is maximum allowed size into the destination buffer (which must be already allocated) + return : the number of output bytes written in buffer 'dest' + or 0 if compression fails. +*/ + + +int LZ4_compressHC2 (const char* source, char* dest, int inputSize, int compressionLevel); +int LZ4_compressHC2_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel); +/* + Same functions as above, but with programmable 'compressionLevel'. + Recommended values are between 4 and 9, although any value between 0 and 16 will work. + 'compressionLevel'==0 means use default 'compressionLevel' value. + Values above 16 behave the same as 16. + Equivalent variants exist for all other compression functions below. +*/ + +/* Note : + Decompression functions are provided within LZ4 source code (see "lz4.h") (BSD license) +*/ + + +/************************************** + Using an external allocation +**************************************/ +int LZ4_sizeofStateHC(void); +int LZ4_compressHC_withStateHC (void* state, const char* source, char* dest, int inputSize); +int LZ4_compressHC_limitedOutput_withStateHC (void* state, const char* source, char* dest, int inputSize, int maxOutputSize); + +int LZ4_compressHC2_withStateHC (void* state, const char* source, char* dest, int inputSize, int compressionLevel); +int LZ4_compressHC2_limitedOutput_withStateHC(void* state, const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel); + +/* +These functions are provided should you prefer to allocate memory for compression tables with your own allocation methods. +To know how much memory must be allocated for the compression tables, use : +int LZ4_sizeofStateHC(); + +Note that tables must be aligned for pointer (32 or 64 bits), otherwise compression will fail (return code 0). + +The allocated memory can be provided to the compression functions using 'void* state' parameter. +LZ4_compress_withStateHC() and LZ4_compress_limitedOutput_withStateHC() are equivalent to previously described functions. +They just use the externally allocated memory for state instead of allocating their own (on stack, or on heap). +*/ + + + +/************************************** + Experimental Streaming Functions +**************************************/ +#define LZ4_STREAMHCSIZE_U64 32774 +#define LZ4_STREAMHCSIZE (LZ4_STREAMHCSIZE_U64 * sizeof(unsigned long long)) +typedef struct { unsigned long long table[LZ4_STREAMHCSIZE_U64]; } LZ4_streamHC_t; +/* +LZ4_streamHC_t +This structure allows static allocation of LZ4 HC streaming state. +State must then be initialized using LZ4_resetStreamHC() before first use. + +Static allocation should only be used with statically linked library. +If you want to use LZ4 as a DLL, please use construction functions below, which are more future-proof. +*/ + + +LZ4_streamHC_t* LZ4_createStreamHC(void); +int LZ4_freeStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr); +/* +These functions create and release memory for LZ4 HC streaming state. +Newly created states are already initialized. +Existing state space can be re-used anytime using LZ4_resetStreamHC(). +If you use LZ4 as a DLL, please use these functions instead of direct struct allocation, +to avoid size mismatch between different versions. +*/ + +void LZ4_resetStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel); +int LZ4_loadDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, const char* dictionary, int dictSize); + +int LZ4_compressHC_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize); +int LZ4_compressHC_limitedOutput_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize, int maxOutputSize); + +int LZ4_saveDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, char* safeBuffer, int maxDictSize); + +/* +These functions compress data in successive blocks of any size, using previous blocks as dictionary. +One key assumption is that each previous block will remain read-accessible while compressing next block. + +Before starting compression, state must be properly initialized, using LZ4_resetStreamHC(). +A first "fictional block" can then be designated as initial dictionary, using LZ4_loadDictHC() (Optional). + +Then, use LZ4_compressHC_continue() or LZ4_compressHC_limitedOutput_continue() to compress each successive block. +They work like usual LZ4_compressHC() or LZ4_compressHC_limitedOutput(), but use previous memory blocks to improve compression. +Previous memory blocks (including initial dictionary when present) must remain accessible and unmodified during compression. + +If, for any reason, previous data block can't be preserved in memory during next compression block, +you must save it to a safer memory space, +using LZ4_saveDictHC(). +*/ + + + +/************************************** + * Deprecated Streaming Functions + * ************************************/ +/* Note : these streaming functions follows the older model, and should no longer be used */ +void* LZ4_createHC (const char* inputBuffer); +char* LZ4_slideInputBufferHC (void* LZ4HC_Data); +int LZ4_freeHC (void* LZ4HC_Data); + +int LZ4_compressHC2_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int compressionLevel); +int LZ4_compressHC2_limitedOutput_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel); + +int LZ4_sizeofStreamStateHC(void); +int LZ4_resetStreamStateHC(void* state, const char* inputBuffer); + + +#if defined (__cplusplus) +} +#endif diff --git a/lib/xxhash.c b/lib/xxhash.c new file mode 100644 index 0000000000..093564cf40 --- /dev/null +++ b/lib/xxhash.c @@ -0,0 +1,935 @@ +/* +xxHash - Fast Hash algorithm +Copyright (C) 2012-2015, Yann Collet + +BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +* Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +You can contact the author at : +- xxHash source repository : http://code.google.com/p/xxhash/ +- xxHash source mirror : https://github.com/Cyan4973/xxHash +- public discussion board : https://groups.google.com/forum/#!forum/lz4c +*/ + + +/************************************** +* Tuning parameters +***************************************/ +/* Unaligned memory access is automatically enabled for "common" CPU, such as x86. + * For others CPU, the compiler will be more cautious, and insert extra code to ensure aligned access is respected. + * If you know your target CPU supports unaligned memory access, you want to force this option manually to improve performance. + * You can also enable this parameter if you know your input data will always be aligned (boundaries of 4, for U32). + */ +#if defined(__ARM_FEATURE_UNALIGNED) || defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64) +# define XXH_USE_UNALIGNED_ACCESS 1 +#endif + +/* XXH_ACCEPT_NULL_INPUT_POINTER : + * If the input pointer is a null pointer, xxHash default behavior is to trigger a memory access error, since it is a bad pointer. + * When this option is enabled, xxHash output for null input pointers will be the same as a null-length input. + * By default, this option is disabled. To enable it, uncomment below define : + */ +/* #define XXH_ACCEPT_NULL_INPUT_POINTER 1 */ + +/* XXH_FORCE_NATIVE_FORMAT : + * By default, xxHash library provides endian-independant Hash values, based on little-endian convention. + * Results are therefore identical for little-endian and big-endian CPU. + * This comes at a performance cost for big-endian CPU, since some swapping is required to emulate little-endian format. + * Should endian-independance be of no importance for your application, you may set the #define below to 1. + * It will improve speed for Big-endian CPU. + * This option has no impact on Little_Endian CPU. + */ +#define XXH_FORCE_NATIVE_FORMAT 0 + + +/************************************** +* Compiler Specific Options +***************************************/ +#ifdef _MSC_VER /* Visual Studio */ +# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ +# define FORCE_INLINE static __forceinline +#else +# if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ +# ifdef __GNUC__ +# define FORCE_INLINE static inline __attribute__((always_inline)) +# else +# define FORCE_INLINE static inline +# endif +# else +# define FORCE_INLINE static +# endif /* __STDC_VERSION__ */ +#endif + + +/************************************** +* Includes & Memory related functions +***************************************/ +#include "xxhash.h" +/* Modify the local functions below should you wish to use some other memory routines */ +/* for malloc(), free() */ +#include +static void* XXH_malloc(size_t s) { return malloc(s); } +static void XXH_free (void* p) { free(p); } +/* for memcpy() */ +#include +static void* XXH_memcpy(void* dest, const void* src, size_t size) +{ + return memcpy(dest,src,size); +} + + +/************************************** +* Basic Types +***************************************/ +#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ +# include +typedef uint8_t BYTE; +typedef uint16_t U16; +typedef uint32_t U32; +typedef int32_t S32; +typedef uint64_t U64; +#else +typedef unsigned char BYTE; +typedef unsigned short U16; +typedef unsigned int U32; +typedef signed int S32; +typedef unsigned long long U64; +#endif + +#if defined(__GNUC__) && !defined(XXH_USE_UNALIGNED_ACCESS) +# define _PACKED __attribute__ ((packed)) +#else +# define _PACKED +#endif + +#if !defined(XXH_USE_UNALIGNED_ACCESS) && !defined(__GNUC__) +# ifdef __IBMC__ +# pragma pack(1) +# else +# pragma pack(push, 1) +# endif +#endif + +typedef struct _U32_S +{ + U32 v; +} _PACKED U32_S; +typedef struct _U64_S +{ + U64 v; +} _PACKED U64_S; + +#if !defined(XXH_USE_UNALIGNED_ACCESS) && !defined(__GNUC__) +# pragma pack(pop) +#endif + +#define A32(x) (((U32_S *)(x))->v) +#define A64(x) (((U64_S *)(x))->v) + + +/***************************************** +* Compiler-specific Functions and Macros +******************************************/ +#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) + +/* Note : although _rotl exists for minGW (GCC under windows), performance seems poor */ +#if defined(_MSC_VER) +# define XXH_rotl32(x,r) _rotl(x,r) +# define XXH_rotl64(x,r) _rotl64(x,r) +#else +# define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r))) +# define XXH_rotl64(x,r) ((x << r) | (x >> (64 - r))) +#endif + +#if defined(_MSC_VER) /* Visual Studio */ +# define XXH_swap32 _byteswap_ulong +# define XXH_swap64 _byteswap_uint64 +#elif GCC_VERSION >= 403 +# define XXH_swap32 __builtin_bswap32 +# define XXH_swap64 __builtin_bswap64 +#else +static U32 XXH_swap32 (U32 x) +{ + return ((x << 24) & 0xff000000 ) | + ((x << 8) & 0x00ff0000 ) | + ((x >> 8) & 0x0000ff00 ) | + ((x >> 24) & 0x000000ff ); +} +static U64 XXH_swap64 (U64 x) +{ + return ((x << 56) & 0xff00000000000000ULL) | + ((x << 40) & 0x00ff000000000000ULL) | + ((x << 24) & 0x0000ff0000000000ULL) | + ((x << 8) & 0x000000ff00000000ULL) | + ((x >> 8) & 0x00000000ff000000ULL) | + ((x >> 24) & 0x0000000000ff0000ULL) | + ((x >> 40) & 0x000000000000ff00ULL) | + ((x >> 56) & 0x00000000000000ffULL); +} +#endif + + +/************************************** +* Constants +***************************************/ +#define PRIME32_1 2654435761U +#define PRIME32_2 2246822519U +#define PRIME32_3 3266489917U +#define PRIME32_4 668265263U +#define PRIME32_5 374761393U + +#define PRIME64_1 11400714785074694791ULL +#define PRIME64_2 14029467366897019727ULL +#define PRIME64_3 1609587929392839161ULL +#define PRIME64_4 9650029242287828579ULL +#define PRIME64_5 2870177450012600261ULL + + +/*************************************** +* Architecture Macros +****************************************/ +typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess; +#ifndef XXH_CPU_LITTLE_ENDIAN /* XXH_CPU_LITTLE_ENDIAN can be defined externally, for example using a compiler switch */ +static const int one = 1; +# define XXH_CPU_LITTLE_ENDIAN (*(char*)(&one)) +#endif + + +/************************************** +* Macros +***************************************/ +#define XXH_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(!!(c)) }; } /* use only *after* variable declarations */ + + +/**************************** +* Memory reads +*****************************/ +typedef enum { XXH_aligned, XXH_unaligned } XXH_alignment; + +FORCE_INLINE U32 XXH_readLE32_align(const void* ptr, XXH_endianess endian, XXH_alignment align) +{ + if (align==XXH_unaligned) + return endian==XXH_littleEndian ? A32(ptr) : XXH_swap32(A32(ptr)); + else + return endian==XXH_littleEndian ? *(U32*)ptr : XXH_swap32(*(U32*)ptr); +} + +FORCE_INLINE U32 XXH_readLE32(const void* ptr, XXH_endianess endian) +{ + return XXH_readLE32_align(ptr, endian, XXH_unaligned); +} + +FORCE_INLINE U64 XXH_readLE64_align(const void* ptr, XXH_endianess endian, XXH_alignment align) +{ + if (align==XXH_unaligned) + return endian==XXH_littleEndian ? A64(ptr) : XXH_swap64(A64(ptr)); + else + return endian==XXH_littleEndian ? *(U64*)ptr : XXH_swap64(*(U64*)ptr); +} + +FORCE_INLINE U64 XXH_readLE64(const void* ptr, XXH_endianess endian) +{ + return XXH_readLE64_align(ptr, endian, XXH_unaligned); +} + + +/**************************** +* Simple Hash Functions +*****************************/ +FORCE_INLINE U32 XXH32_endian_align(const void* input, size_t len, U32 seed, XXH_endianess endian, XXH_alignment align) +{ + const BYTE* p = (const BYTE*)input; + const BYTE* bEnd = p + len; + U32 h32; +#define XXH_get32bits(p) XXH_readLE32_align(p, endian, align) + +#ifdef XXH_ACCEPT_NULL_INPUT_POINTER + if (p==NULL) + { + len=0; + bEnd=p=(const BYTE*)(size_t)16; + } +#endif + + if (len>=16) + { + const BYTE* const limit = bEnd - 16; + U32 v1 = seed + PRIME32_1 + PRIME32_2; + U32 v2 = seed + PRIME32_2; + U32 v3 = seed + 0; + U32 v4 = seed - PRIME32_1; + + do + { + v1 += XXH_get32bits(p) * PRIME32_2; + v1 = XXH_rotl32(v1, 13); + v1 *= PRIME32_1; + p+=4; + v2 += XXH_get32bits(p) * PRIME32_2; + v2 = XXH_rotl32(v2, 13); + v2 *= PRIME32_1; + p+=4; + v3 += XXH_get32bits(p) * PRIME32_2; + v3 = XXH_rotl32(v3, 13); + v3 *= PRIME32_1; + p+=4; + v4 += XXH_get32bits(p) * PRIME32_2; + v4 = XXH_rotl32(v4, 13); + v4 *= PRIME32_1; + p+=4; + } + while (p<=limit); + + h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18); + } + else + { + h32 = seed + PRIME32_5; + } + + h32 += (U32) len; + + while (p+4<=bEnd) + { + h32 += XXH_get32bits(p) * PRIME32_3; + h32 = XXH_rotl32(h32, 17) * PRIME32_4 ; + p+=4; + } + + while (p> 15; + h32 *= PRIME32_2; + h32 ^= h32 >> 13; + h32 *= PRIME32_3; + h32 ^= h32 >> 16; + + return h32; +} + + +unsigned int XXH32 (const void* input, size_t len, unsigned seed) +{ +#if 0 + /* Simple version, good for code maintenance, but unfortunately slow for small inputs */ + XXH32_state_t state; + XXH32_reset(&state, seed); + XXH32_update(&state, input, len); + return XXH32_digest(&state); +#else + XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; + +# if !defined(XXH_USE_UNALIGNED_ACCESS) + if ((((size_t)input) & 3) == 0) /* Input is aligned, let's leverage the speed advantage */ + { + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned); + else + return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned); + } +# endif + + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned); + else + return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned); +#endif +} + +FORCE_INLINE U64 XXH64_endian_align(const void* input, size_t len, U64 seed, XXH_endianess endian, XXH_alignment align) +{ + const BYTE* p = (const BYTE*)input; + const BYTE* bEnd = p + len; + U64 h64; +#define XXH_get64bits(p) XXH_readLE64_align(p, endian, align) + +#ifdef XXH_ACCEPT_NULL_INPUT_POINTER + if (p==NULL) + { + len=0; + bEnd=p=(const BYTE*)(size_t)32; + } +#endif + + if (len>=32) + { + const BYTE* const limit = bEnd - 32; + U64 v1 = seed + PRIME64_1 + PRIME64_2; + U64 v2 = seed + PRIME64_2; + U64 v3 = seed + 0; + U64 v4 = seed - PRIME64_1; + + do + { + v1 += XXH_get64bits(p) * PRIME64_2; + p+=8; + v1 = XXH_rotl64(v1, 31); + v1 *= PRIME64_1; + v2 += XXH_get64bits(p) * PRIME64_2; + p+=8; + v2 = XXH_rotl64(v2, 31); + v2 *= PRIME64_1; + v3 += XXH_get64bits(p) * PRIME64_2; + p+=8; + v3 = XXH_rotl64(v3, 31); + v3 *= PRIME64_1; + v4 += XXH_get64bits(p) * PRIME64_2; + p+=8; + v4 = XXH_rotl64(v4, 31); + v4 *= PRIME64_1; + } + while (p<=limit); + + h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18); + + v1 *= PRIME64_2; + v1 = XXH_rotl64(v1, 31); + v1 *= PRIME64_1; + h64 ^= v1; + h64 = h64 * PRIME64_1 + PRIME64_4; + + v2 *= PRIME64_2; + v2 = XXH_rotl64(v2, 31); + v2 *= PRIME64_1; + h64 ^= v2; + h64 = h64 * PRIME64_1 + PRIME64_4; + + v3 *= PRIME64_2; + v3 = XXH_rotl64(v3, 31); + v3 *= PRIME64_1; + h64 ^= v3; + h64 = h64 * PRIME64_1 + PRIME64_4; + + v4 *= PRIME64_2; + v4 = XXH_rotl64(v4, 31); + v4 *= PRIME64_1; + h64 ^= v4; + h64 = h64 * PRIME64_1 + PRIME64_4; + } + else + { + h64 = seed + PRIME64_5; + } + + h64 += (U64) len; + + while (p+8<=bEnd) + { + U64 k1 = XXH_get64bits(p); + k1 *= PRIME64_2; + k1 = XXH_rotl64(k1,31); + k1 *= PRIME64_1; + h64 ^= k1; + h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4; + p+=8; + } + + if (p+4<=bEnd) + { + h64 ^= (U64)(XXH_get32bits(p)) * PRIME64_1; + h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3; + p+=4; + } + + while (p> 33; + h64 *= PRIME64_2; + h64 ^= h64 >> 29; + h64 *= PRIME64_3; + h64 ^= h64 >> 32; + + return h64; +} + + +unsigned long long XXH64 (const void* input, size_t len, unsigned long long seed) +{ +#if 0 + /* Simple version, good for code maintenance, but unfortunately slow for small inputs */ + XXH64_state_t state; + XXH64_reset(&state, seed); + XXH64_update(&state, input, len); + return XXH64_digest(&state); +#else + XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; + +# if !defined(XXH_USE_UNALIGNED_ACCESS) + if ((((size_t)input) & 7)==0) /* Input is aligned, let's leverage the speed advantage */ + { + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned); + else + return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned); + } +# endif + + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned); + else + return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned); +#endif +} + +/**************************************************** + * Advanced Hash Functions +****************************************************/ + +/*** Allocation ***/ +typedef struct +{ + U64 total_len; + U32 seed; + U32 v1; + U32 v2; + U32 v3; + U32 v4; + U32 mem32[4]; /* defined as U32 for alignment */ + U32 memsize; +} XXH_istate32_t; + +typedef struct +{ + U64 total_len; + U64 seed; + U64 v1; + U64 v2; + U64 v3; + U64 v4; + U64 mem64[4]; /* defined as U64 for alignment */ + U32 memsize; +} XXH_istate64_t; + + +XXH32_state_t* XXH32_createState(void) +{ + XXH_STATIC_ASSERT(sizeof(XXH32_state_t) >= sizeof(XXH_istate32_t)); /* A compilation error here means XXH32_state_t is not large enough */ + return (XXH32_state_t*)XXH_malloc(sizeof(XXH32_state_t)); +} +XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr) +{ + XXH_free(statePtr); + return XXH_OK; +} + +XXH64_state_t* XXH64_createState(void) +{ + XXH_STATIC_ASSERT(sizeof(XXH64_state_t) >= sizeof(XXH_istate64_t)); /* A compilation error here means XXH64_state_t is not large enough */ + return (XXH64_state_t*)XXH_malloc(sizeof(XXH64_state_t)); +} +XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr) +{ + XXH_free(statePtr); + return XXH_OK; +} + + +/*** Hash feed ***/ + +XXH_errorcode XXH32_reset(XXH32_state_t* state_in, U32 seed) +{ + XXH_istate32_t* state = (XXH_istate32_t*) state_in; + state->seed = seed; + state->v1 = seed + PRIME32_1 + PRIME32_2; + state->v2 = seed + PRIME32_2; + state->v3 = seed + 0; + state->v4 = seed - PRIME32_1; + state->total_len = 0; + state->memsize = 0; + return XXH_OK; +} + +XXH_errorcode XXH64_reset(XXH64_state_t* state_in, unsigned long long seed) +{ + XXH_istate64_t* state = (XXH_istate64_t*) state_in; + state->seed = seed; + state->v1 = seed + PRIME64_1 + PRIME64_2; + state->v2 = seed + PRIME64_2; + state->v3 = seed + 0; + state->v4 = seed - PRIME64_1; + state->total_len = 0; + state->memsize = 0; + return XXH_OK; +} + + +FORCE_INLINE XXH_errorcode XXH32_update_endian (XXH32_state_t* state_in, const void* input, size_t len, XXH_endianess endian) +{ + XXH_istate32_t* state = (XXH_istate32_t *) state_in; + const BYTE* p = (const BYTE*)input; + const BYTE* const bEnd = p + len; + +#ifdef XXH_ACCEPT_NULL_INPUT_POINTER + if (input==NULL) return XXH_ERROR; +#endif + + state->total_len += len; + + if (state->memsize + len < 16) /* fill in tmp buffer */ + { + XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, len); + state->memsize += (U32)len; + return XXH_OK; + } + + if (state->memsize) /* some data left from previous update */ + { + XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, 16-state->memsize); + { + const U32* p32 = state->mem32; + state->v1 += XXH_readLE32(p32, endian) * PRIME32_2; + state->v1 = XXH_rotl32(state->v1, 13); + state->v1 *= PRIME32_1; + p32++; + state->v2 += XXH_readLE32(p32, endian) * PRIME32_2; + state->v2 = XXH_rotl32(state->v2, 13); + state->v2 *= PRIME32_1; + p32++; + state->v3 += XXH_readLE32(p32, endian) * PRIME32_2; + state->v3 = XXH_rotl32(state->v3, 13); + state->v3 *= PRIME32_1; + p32++; + state->v4 += XXH_readLE32(p32, endian) * PRIME32_2; + state->v4 = XXH_rotl32(state->v4, 13); + state->v4 *= PRIME32_1; + p32++; + } + p += 16-state->memsize; + state->memsize = 0; + } + + if (p <= bEnd-16) + { + const BYTE* const limit = bEnd - 16; + U32 v1 = state->v1; + U32 v2 = state->v2; + U32 v3 = state->v3; + U32 v4 = state->v4; + + do + { + v1 += XXH_readLE32(p, endian) * PRIME32_2; + v1 = XXH_rotl32(v1, 13); + v1 *= PRIME32_1; + p+=4; + v2 += XXH_readLE32(p, endian) * PRIME32_2; + v2 = XXH_rotl32(v2, 13); + v2 *= PRIME32_1; + p+=4; + v3 += XXH_readLE32(p, endian) * PRIME32_2; + v3 = XXH_rotl32(v3, 13); + v3 *= PRIME32_1; + p+=4; + v4 += XXH_readLE32(p, endian) * PRIME32_2; + v4 = XXH_rotl32(v4, 13); + v4 *= PRIME32_1; + p+=4; + } + while (p<=limit); + + state->v1 = v1; + state->v2 = v2; + state->v3 = v3; + state->v4 = v4; + } + + if (p < bEnd) + { + XXH_memcpy(state->mem32, p, bEnd-p); + state->memsize = (int)(bEnd-p); + } + + return XXH_OK; +} + +XXH_errorcode XXH32_update (XXH32_state_t* state_in, const void* input, size_t len) +{ + XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; + + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH32_update_endian(state_in, input, len, XXH_littleEndian); + else + return XXH32_update_endian(state_in, input, len, XXH_bigEndian); +} + + + +FORCE_INLINE U32 XXH32_digest_endian (const XXH32_state_t* state_in, XXH_endianess endian) +{ + XXH_istate32_t* state = (XXH_istate32_t*) state_in; + const BYTE * p = (const BYTE*)state->mem32; + BYTE* bEnd = (BYTE*)(state->mem32) + state->memsize; + U32 h32; + + if (state->total_len >= 16) + { + h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18); + } + else + { + h32 = state->seed + PRIME32_5; + } + + h32 += (U32) state->total_len; + + while (p+4<=bEnd) + { + h32 += XXH_readLE32(p, endian) * PRIME32_3; + h32 = XXH_rotl32(h32, 17) * PRIME32_4; + p+=4; + } + + while (p> 15; + h32 *= PRIME32_2; + h32 ^= h32 >> 13; + h32 *= PRIME32_3; + h32 ^= h32 >> 16; + + return h32; +} + + +U32 XXH32_digest (const XXH32_state_t* state_in) +{ + XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; + + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH32_digest_endian(state_in, XXH_littleEndian); + else + return XXH32_digest_endian(state_in, XXH_bigEndian); +} + + +FORCE_INLINE XXH_errorcode XXH64_update_endian (XXH64_state_t* state_in, const void* input, size_t len, XXH_endianess endian) +{ + XXH_istate64_t * state = (XXH_istate64_t *) state_in; + const BYTE* p = (const BYTE*)input; + const BYTE* const bEnd = p + len; + +#ifdef XXH_ACCEPT_NULL_INPUT_POINTER + if (input==NULL) return XXH_ERROR; +#endif + + state->total_len += len; + + if (state->memsize + len < 32) /* fill in tmp buffer */ + { + XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, len); + state->memsize += (U32)len; + return XXH_OK; + } + + if (state->memsize) /* some data left from previous update */ + { + XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, 32-state->memsize); + { + const U64* p64 = state->mem64; + state->v1 += XXH_readLE64(p64, endian) * PRIME64_2; + state->v1 = XXH_rotl64(state->v1, 31); + state->v1 *= PRIME64_1; + p64++; + state->v2 += XXH_readLE64(p64, endian) * PRIME64_2; + state->v2 = XXH_rotl64(state->v2, 31); + state->v2 *= PRIME64_1; + p64++; + state->v3 += XXH_readLE64(p64, endian) * PRIME64_2; + state->v3 = XXH_rotl64(state->v3, 31); + state->v3 *= PRIME64_1; + p64++; + state->v4 += XXH_readLE64(p64, endian) * PRIME64_2; + state->v4 = XXH_rotl64(state->v4, 31); + state->v4 *= PRIME64_1; + p64++; + } + p += 32-state->memsize; + state->memsize = 0; + } + + if (p+32 <= bEnd) + { + const BYTE* const limit = bEnd - 32; + U64 v1 = state->v1; + U64 v2 = state->v2; + U64 v3 = state->v3; + U64 v4 = state->v4; + + do + { + v1 += XXH_readLE64(p, endian) * PRIME64_2; + v1 = XXH_rotl64(v1, 31); + v1 *= PRIME64_1; + p+=8; + v2 += XXH_readLE64(p, endian) * PRIME64_2; + v2 = XXH_rotl64(v2, 31); + v2 *= PRIME64_1; + p+=8; + v3 += XXH_readLE64(p, endian) * PRIME64_2; + v3 = XXH_rotl64(v3, 31); + v3 *= PRIME64_1; + p+=8; + v4 += XXH_readLE64(p, endian) * PRIME64_2; + v4 = XXH_rotl64(v4, 31); + v4 *= PRIME64_1; + p+=8; + } + while (p<=limit); + + state->v1 = v1; + state->v2 = v2; + state->v3 = v3; + state->v4 = v4; + } + + if (p < bEnd) + { + XXH_memcpy(state->mem64, p, bEnd-p); + state->memsize = (int)(bEnd-p); + } + + return XXH_OK; +} + +XXH_errorcode XXH64_update (XXH64_state_t* state_in, const void* input, size_t len) +{ + XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; + + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH64_update_endian(state_in, input, len, XXH_littleEndian); + else + return XXH64_update_endian(state_in, input, len, XXH_bigEndian); +} + + + +FORCE_INLINE U64 XXH64_digest_endian (const XXH64_state_t* state_in, XXH_endianess endian) +{ + XXH_istate64_t * state = (XXH_istate64_t *) state_in; + const BYTE * p = (const BYTE*)state->mem64; + BYTE* bEnd = (BYTE*)state->mem64 + state->memsize; + U64 h64; + + if (state->total_len >= 32) + { + U64 v1 = state->v1; + U64 v2 = state->v2; + U64 v3 = state->v3; + U64 v4 = state->v4; + + h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18); + + v1 *= PRIME64_2; + v1 = XXH_rotl64(v1, 31); + v1 *= PRIME64_1; + h64 ^= v1; + h64 = h64*PRIME64_1 + PRIME64_4; + + v2 *= PRIME64_2; + v2 = XXH_rotl64(v2, 31); + v2 *= PRIME64_1; + h64 ^= v2; + h64 = h64*PRIME64_1 + PRIME64_4; + + v3 *= PRIME64_2; + v3 = XXH_rotl64(v3, 31); + v3 *= PRIME64_1; + h64 ^= v3; + h64 = h64*PRIME64_1 + PRIME64_4; + + v4 *= PRIME64_2; + v4 = XXH_rotl64(v4, 31); + v4 *= PRIME64_1; + h64 ^= v4; + h64 = h64*PRIME64_1 + PRIME64_4; + } + else + { + h64 = state->seed + PRIME64_5; + } + + h64 += (U64) state->total_len; + + while (p+8<=bEnd) + { + U64 k1 = XXH_readLE64(p, endian); + k1 *= PRIME64_2; + k1 = XXH_rotl64(k1,31); + k1 *= PRIME64_1; + h64 ^= k1; + h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4; + p+=8; + } + + if (p+4<=bEnd) + { + h64 ^= (U64)(XXH_readLE32(p, endian)) * PRIME64_1; + h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3; + p+=4; + } + + while (p> 33; + h64 *= PRIME64_2; + h64 ^= h64 >> 29; + h64 *= PRIME64_3; + h64 ^= h64 >> 32; + + return h64; +} + + +unsigned long long XXH64_digest (const XXH64_state_t* state_in) +{ + XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; + + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH64_digest_endian(state_in, XXH_littleEndian); + else + return XXH64_digest_endian(state_in, XXH_bigEndian); +} + + diff --git a/lib/xxhash.h b/lib/xxhash.h new file mode 100644 index 0000000000..55b45015a4 --- /dev/null +++ b/lib/xxhash.h @@ -0,0 +1,156 @@ +/* + xxHash - Extremely Fast Hash algorithm + Header File + Copyright (C) 2012-2014, Yann Collet. + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - xxHash source repository : http://code.google.com/p/xxhash/ +*/ + +/* Notice extracted from xxHash homepage : + +xxHash is an extremely fast Hash algorithm, running at RAM speed limits. +It also successfully passes all tests from the SMHasher suite. + +Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz) + +Name Speed Q.Score Author +xxHash 5.4 GB/s 10 +CrapWow 3.2 GB/s 2 Andrew +MumurHash 3a 2.7 GB/s 10 Austin Appleby +SpookyHash 2.0 GB/s 10 Bob Jenkins +SBox 1.4 GB/s 9 Bret Mulvey +Lookup3 1.2 GB/s 9 Bob Jenkins +SuperFastHash 1.2 GB/s 1 Paul Hsieh +CityHash64 1.05 GB/s 10 Pike & Alakuijala +FNV 0.55 GB/s 5 Fowler, Noll, Vo +CRC32 0.43 GB/s 9 +MD5-32 0.33 GB/s 10 Ronald L. Rivest +SHA1-32 0.28 GB/s 10 + +Q.Score is a measure of quality of the hash function. +It depends on successfully passing SMHasher test set. +10 is a perfect score. +*/ + +#pragma once + +#if defined (__cplusplus) +extern "C" { +#endif + + +/***************************** + Includes +*****************************/ +#include /* size_t */ + + +/***************************** + Type +*****************************/ +typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode; + + + +/***************************** + Simple Hash Functions +*****************************/ + +unsigned int XXH32 (const void* input, size_t length, unsigned seed); +unsigned long long XXH64 (const void* input, size_t length, unsigned long long seed); + +/* +XXH32() : + Calculate the 32-bits hash of sequence "length" bytes stored at memory address "input". + The memory between input & input+length must be valid (allocated and read-accessible). + "seed" can be used to alter the result predictably. + This function successfully passes all SMHasher tests. + Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s +XXH64() : + Calculate the 64-bits hash of sequence of length "len" stored at memory address "input". +*/ + + + +/***************************** + Advanced Hash Functions +*****************************/ +typedef struct { long long ll[ 6]; } XXH32_state_t; +typedef struct { long long ll[11]; } XXH64_state_t; + +/* +These structures allow static allocation of XXH states. +States must then be initialized using XXHnn_reset() before first use. + +If you prefer dynamic allocation, please refer to functions below. +*/ + +XXH32_state_t* XXH32_createState(void); +XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr); + +XXH64_state_t* XXH64_createState(void); +XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr); + +/* +These functions create and release memory for XXH state. +States must then be initialized using XXHnn_reset() before first use. +*/ + + +XXH_errorcode XXH32_reset (XXH32_state_t* statePtr, unsigned seed); +XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* input, size_t length); +unsigned int XXH32_digest (const XXH32_state_t* statePtr); + +XXH_errorcode XXH64_reset (XXH64_state_t* statePtr, unsigned long long seed); +XXH_errorcode XXH64_update (XXH64_state_t* statePtr, const void* input, size_t length); +unsigned long long XXH64_digest (const XXH64_state_t* statePtr); + +/* +These functions calculate the xxHash of an input provided in multiple smaller packets, +as opposed to an input provided as a single block. + +XXH state space must first be allocated, using either static or dynamic method provided above. + +Start a new hash by initializing state with a seed, using XXHnn_reset(). + +Then, feed the hash state by calling XXHnn_update() as many times as necessary. +Obviously, input must be valid, meaning allocated and read accessible. +The function returns an error code, with 0 meaning OK, and any other value meaning there is an error. + +Finally, you can produce a hash anytime, by using XXHnn_digest(). +This function returns the final nn-bits hash. +You can nonetheless continue feeding the hash state with more input, +and therefore get some new hashes, by calling again XXHnn_digest(). + +When you are done, don't forget to free XXH state space, using typically XXHnn_freeState(). +*/ + + +#if defined (__cplusplus) +} +#endif diff --git a/lz4_block_format.txt b/lz4_block_format.txt new file mode 100644 index 0000000000..2c424c56f4 --- /dev/null +++ b/lz4_block_format.txt @@ -0,0 +1,120 @@ +LZ4 Format Description +Last revised: 2012-02-27 +Author : Y. Collet + + + +This small specification intents to provide enough information +to anyone willing to produce LZ4-compatible compressed data blocks +using any programming language. + +LZ4 is an LZ77-type compressor with a fixed, byte-oriented encoding. +The most important design principle behind LZ4 is simplicity. +It helps to create an easy to read and maintain source code. +It also helps later on for optimisations, compactness, and speed. +There is no entropy encoder backend nor framing layer. +The latter is assumed to be handled by other parts of the system. + +This document only describes the format, +not how the LZ4 compressor nor decompressor actually work. +The correctness of the decompressor should not depend +on implementation details of the compressor, and vice versa. + + + +-- Compressed block format -- + +An LZ4 compressed block is composed of sequences. +Schematically, a sequence is a suite of literals, followed by a match copy. + +Each sequence starts with a token. +The token is a one byte value, separated into two 4-bits fields. +Therefore each field ranges from 0 to 15. + + +The first field uses the 4 high-bits of the token. +It provides the length of literals to follow. +(Note : a literal is a not-compressed byte). +If the field value is 0, then there is no literal. +If it is 15, then we need to add some more bytes to indicate the full length. +Each additionnal byte then represent a value from 0 to 255, +which is added to the previous value to produce a total length. +When the byte value is 255, another byte is output. +There can be any number of bytes following the token. There is no "size limit". +(Sidenote this is why a not-compressible input block is expanded by 0.4%). + +Example 1 : A length of 48 will be represented as : +- 15 : value for the 4-bits High field +- 33 : (=48-15) remaining length to reach 48 + +Example 2 : A length of 280 will be represented as : +- 15 : value for the 4-bits High field +- 255 : following byte is maxed, since 280-15 >= 255 +- 10 : (=280 - 15 - 255) ) remaining length to reach 280 + +Example 3 : A length of 15 will be represented as : +- 15 : value for the 4-bits High field +- 0 : (=15-15) yes, the zero must be output + +Following the token and optional length bytes, are the literals themselves. +They are exactly as numerous as previously decoded (length of literals). +It's possible that there are zero literal. + + +Following the literals is the match copy operation. + +It starts by the offset. +This is a 2 bytes value, in little endian format. + +The offset represents the position of the match to be copied from. +1 means "current position - 1 byte". +The maximum offset value is 65535, 65536 cannot be coded. +Note that 0 is an invalid value, not used. + +Then we need to extract the match length. +For this, we use the second token field, the low 4-bits. +Value, obviously, ranges from 0 to 15. +However here, 0 means that the copy operation will be minimal. +The minimum length of a match, called minmatch, is 4. +As a consequence, a 0 value means 4 bytes, and a value of 15 means 19+ bytes. +Similar to literal length, on reaching the highest possible value (15), +we output additional bytes, one at a time, with values ranging from 0 to 255. +They are added to total to provide the final match length. +A 255 value means there is another byte to read and add. +There is no limit to the number of optional bytes that can be output this way. +(This points towards a maximum achievable compression ratio of ~250). + +With the offset and the matchlength, +the decoder can now proceed to copy the data from the already decoded buffer. +On decoding the matchlength, we reach the end of the compressed sequence, +and therefore start another one. + + +-- Parsing restrictions -- + +There are specific parsing rules to respect in order to remain compatible +with assumptions made by the decoder : +1) The last 5 bytes are always literals +2) The last match must start at least 12 bytes before end of block +Consequently, a block with less than 13 bytes cannot be compressed. +These rules are in place to ensure that the decoder +will never read beyond the input buffer, nor write beyond the output buffer. + +Note that the last sequence is also incomplete, +and stops right after literals. + + +-- Additional notes -- + +There is no assumption nor limits to the way the compressor +searches and selects matches within the source data block. +It could be a fast scan, a multi-probe, a full search using BST, +standard hash chains or MMC, well whatever. + +Advanced parsing strategies can also be implemented, such as lazy match, +or full optimal parsing. + +All these trade-off offer distinctive speed/memory/compression advantages. +Whatever the method used by the compressor, its result will be decodable +by any LZ4 decoder if it follows the format specification described above. + diff --git a/programs/COPYING b/programs/COPYING new file mode 100644 index 0000000000..d159169d10 --- /dev/null +++ b/programs/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/programs/Makefile b/programs/Makefile new file mode 100644 index 0000000000..543eb7cd5f --- /dev/null +++ b/programs/Makefile @@ -0,0 +1,206 @@ +# ########################################################################## +# LZ4 programs - Makefile +# Copyright (C) Yann Collet 2011-2015 +# +# GPL v2 License +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# You can contact the author at : +# - LZ4 source repository : http://code.google.com/p/lz4/ +# - LZ4 forum froup : https://groups.google.com/forum/#!forum/lz4c +# ########################################################################## +# lz4 : Command Line Utility, supporting gzip-like arguments +# lz4c : CLU, supporting also legacy lz4demo arguments +# lz4c32: Same as lz4c, but forced to compile in 32-bits mode +# fuzzer : Test tool, to check lz4 integrity on target platform +# fuzzer32: Same as fuzzer, but forced to compile in 32-bits mode +# fullbench : Precisely measure speed for each LZ4 function variant +# fullbench32: Same as fullbench, but forced to compile in 32-bits mode +# ########################################################################## + +RELEASE?= r126 + +DESTDIR?= +PREFIX ?= /usr +CFLAGS ?= -O3 +CFLAGS += -std=c99 -Wall -Wextra -Wundef -Wshadow -Wcast-align -Wstrict-prototypes -pedantic -DLZ4_VERSION=\"$(RELEASE)\" +FLAGS = -I../lib $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) + +BINDIR=$(PREFIX)/bin +MANDIR=$(PREFIX)/share/man/man1 +LZ4DIR=../lib + +TEST_FILES = COPYING +TEST_TARGETS=test-native + + +# Define *.exe as extension for Windows systems +ifneq (,$(filter Windows%,$(OS))) +EXT =.exe +VOID = nul +else +EXT = +VOID = /dev/null +endif + + +# Select test target for Travis CI's Build Matrix +TRAVIS_TARGET=$(LZ4_TRAVIS_CI_ENV) + + +default: lz4 lz4c + +all: lz4 lz4c lz4c32 fullbench fullbench32 fuzzer fuzzer32 frametest frametest32 datagen + +lz4: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/lz4frame.c $(LZ4DIR)/xxhash.c bench.c lz4io.c lz4cli.c + $(CC) $(FLAGS) $^ -o $@$(EXT) + +lz4c : $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/lz4frame.c $(LZ4DIR)/xxhash.c bench.c lz4io.c lz4cli.c + $(CC) $(FLAGS) -DENABLE_LZ4C_LEGACY_OPTIONS $^ -o $@$(EXT) + +lz4c32: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/lz4frame.c $(LZ4DIR)/xxhash.c bench.c lz4io.c lz4cli.c + $(CC) -m32 $(FLAGS) -DENABLE_LZ4C_LEGACY_OPTIONS $^ -o $@$(EXT) + +fullbench : $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/lz4frame.c $(LZ4DIR)/xxhash.c fullbench.c + $(CC) $(FLAGS) $^ -o $@$(EXT) + +fullbench32: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/lz4frame.c $(LZ4DIR)/xxhash.c fullbench.c + $(CC) -m32 $(FLAGS) $^ -o $@$(EXT) + +fuzzer : $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/xxhash.c fuzzer.c + $(CC) $(FLAGS) $^ -o $@$(EXT) + +fuzzer32: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/xxhash.c fuzzer.c + $(CC) -m32 $(FLAGS) $^ -o $@$(EXT) + +frametest: $(LZ4DIR)/lz4frame.c $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/xxhash.c frametest.c + $(CC) $(FLAGS) $^ -o $@$(EXT) + +frametest32: $(LZ4DIR)/lz4frame.c $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/xxhash.c frametest.c + $(CC) -m32 $(FLAGS) $^ -o $@$(EXT) + +datagen : datagen.c + $(CC) $(FLAGS) $^ -o $@$(EXT) + + +clean: + @rm -f core *.o *.test \ + lz4$(EXT) lz4c$(EXT) lz4c32$(EXT) \ + fullbench$(EXT) fullbench32$(EXT) \ + fuzzer$(EXT) fuzzer32$(EXT) \ + frametest$(EXT) frametest32$(EXT) \ + datagen$(EXT) + @echo Cleaning completed + + +#------------------------------------------------------------------------ +#make install is validated only for Linux, OSX, kFreeBSD and Hurd targets +ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU)) + +install: lz4 lz4c + @echo Installing binaries + @install -d -m 755 $(DESTDIR)$(BINDIR)/ $(DESTDIR)$(MANDIR)/ + @install -m 755 lz4$(EXT) $(DESTDIR)$(BINDIR)/lz4$(EXT) + @ln -sf lz4$(EXT) $(DESTDIR)$(BINDIR)/lz4cat + @install -m 755 lz4c$(EXT) $(DESTDIR)$(BINDIR)/lz4c$(EXT) + @echo Installing man pages + @install -m 644 lz4.1 $(DESTDIR)$(MANDIR)/lz4.1 + @install -m 644 lz4c.1 $(DESTDIR)$(MANDIR)/lz4c.1 + @install -m 644 lz4cat.1 $(DESTDIR)$(MANDIR)/lz4cat.1 + @echo lz4 installation completed + +uninstall: + rm -f $(DESTDIR)$(BINDIR)/lz4cat + [ -x $(DESTDIR)$(BINDIR)/lz4$(EXT) ] && rm -f $(DESTDIR)$(BINDIR)/lz4$(EXT) + [ -x $(DESTDIR)$(BINDIR)/lz4c$(EXT) ] && rm -f $(DESTDIR)$(BINDIR)/lz4c$(EXT) + [ -f $(DESTDIR)$(MANDIR)/lz4.1 ] && rm -f $(DESTDIR)$(MANDIR)/lz4.1 + [ -f $(DESTDIR)$(MANDIR)/lz4c.1 ] && rm -f $(DESTDIR)$(MANDIR)/lz4c.1 + [ -f $(DESTDIR)$(MANDIR)/lz4cat.1 ] && rm -f $(DESTDIR)$(MANDIR)/lz4cat.1 + @echo lz4 programs successfully uninstalled + +test: test-lz4 test-lz4c test-frametest test-fullbench test-fuzzer test-mem + +test32: test-lz4c32 test-frametest32 test-fullbench32 test-fuzzer32 test-mem32 + +test-all: test test32 + +test-travis: $(TRAVIS_TARGET) + +test-lz4: lz4 datagen + ./datagen -g16KB | ./lz4 -9 | ./lz4 -vdq > $(VOID) + ./datagen | ./lz4 | ./lz4 -vdq > $(VOID) + ./datagen -g6M -p100 | ./lz4 -9BD | ./lz4 -vdq > $(VOID) + ./datagen -g256MB | ./lz4 -vqB4D | ./lz4 -vdq > $(VOID) + ./datagen -g6GB | ./lz4 -vqB5D | ./lz4 -vdq > $(VOID) +# test frame concatenation with null-length frame + @echo -n > empty.test + @echo hi > nonempty.test + cat nonempty.test empty.test nonempty.test > orig.test + @./lz4 -zq empty.test > empty.lz4.test + @./lz4 -zq nonempty.test > nonempty.lz4.test + cat nonempty.lz4.test empty.lz4.test nonempty.lz4.test > concat.lz4.test + ./lz4 -d concat.lz4.test > result.test + sdiff orig.test result.test + @rm *.test + @echo frame concatenation test completed +# test frame concatenation with null-length frame + + +test-lz4c: lz4c datagen + ./datagen -g256MB | ./lz4c -l -v | ./lz4c -vdq > $(VOID) + +test-lz4c32: lz4 lz4c32 lz4 datagen + ./datagen -g16KB | ./lz4c32 -9 | ./lz4c32 -vdq > $(VOID) + ./datagen -g16KB | ./lz4c32 -9 | ./lz4 -vdq > $(VOID) + ./datagen | ./lz4c32 | ./lz4c32 -vdq > $(VOID) + ./datagen | ./lz4c32 | ./lz4 -vdq > $(VOID) + ./datagen -g256MB | ./lz4c32 -vqB4D | ./lz4c32 -vdq > $(VOID) + ./datagen -g256MB | ./lz4c32 -vqB4D | ./lz4 -vdq > $(VOID) + ./datagen -g6GB | ./lz4c32 -vqB5D | ./lz4c32 -vdq > $(VOID) + +test-fullbench: fullbench + ./fullbench --no-prompt $(TEST_FILES) + +test-fullbench32: fullbench32 + ./fullbench32 --no-prompt $(TEST_FILES) + +test-fuzzer: fuzzer + ./fuzzer + +test-fuzzer32: fuzzer32 + ./fuzzer32 + +test-frametest: frametest + ./frametest + +test-frametest32: frametest32 + ./frametest32 + +test-mem: lz4 datagen fuzzer frametest + ./datagen -g16KB > tmp + valgrind --leak-check=yes ./lz4 -9 -BD -f tmp /dev/null + ./datagen -g16MB > tmp + valgrind --leak-check=yes ./lz4 -9 -B5D -f tmp /dev/null + ./datagen -g256MB > tmp + valgrind --leak-check=yes ./lz4 -B4D -f -vq tmp /dev/null + rm tmp + valgrind --leak-check=yes ./fuzzer -i64 -t1 + valgrind --leak-check=yes ./frametest -i256 + +test-mem32: lz4c32 datagen +# unfortunately, valgrind doesn't seem to work with non-native binary. If someone knows how to do a valgrind-test on a 32-bits exe with a 64-bits system... + +endif diff --git a/programs/bench.c b/programs/bench.c new file mode 100644 index 0000000000..02e56c9240 --- /dev/null +++ b/programs/bench.c @@ -0,0 +1,435 @@ +/* + bench.c - Demo program to benchmark open-source compression algorithm + Copyright (C) Yann Collet 2012-2014 + GPL v2 License + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + You can contact the author at : + - LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html + - LZ4 source repository : http://code.google.com/p/lz4/ +*/ + +/************************************** +* Compiler Options +***************************************/ +/* Disable some Visual warning messages */ +#define _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_DEPRECATE /* VS2005 */ + +/* Unix Large Files support (>4GB) */ +#define _FILE_OFFSET_BITS 64 +#if (defined(__sun__) && (!defined(__LP64__))) /* Sun Solaris 32-bits requires specific definitions */ +# define _LARGEFILE_SOURCE +#elif ! defined(__LP64__) /* No point defining Large file for 64 bit */ +# define _LARGEFILE64_SOURCE +#endif + +/* S_ISREG & gettimeofday() are not supported by MSVC */ +#if defined(_MSC_VER) || defined(_WIN32) +# define BMK_LEGACY_TIMER 1 +#endif + + +/************************************** +* Includes +***************************************/ +#include /* malloc */ +#include /* fprintf, fopen, ftello64 */ +#include /* stat64 */ +#include /* stat64 */ + +/* Use ftime() if gettimeofday() is not available on your target */ +#if defined(BMK_LEGACY_TIMER) +# include /* timeb, ftime */ +#else +# include /* gettimeofday */ +#endif + +#include "lz4.h" +#define COMPRESSOR0 LZ4_compress_local +static int LZ4_compress_local(const char* src, char* dst, int size, int clevel) { (void)clevel; return LZ4_compress(src, dst, size); } +#include "lz4hc.h" +#define COMPRESSOR1 LZ4_compressHC2 +#define DEFAULTCOMPRESSOR COMPRESSOR0 + +#include "xxhash.h" + + +/************************************** +* Compiler specifics +***************************************/ +#if !defined(S_ISREG) +# define S_ISREG(x) (((x) & S_IFMT) == S_IFREG) +#endif + + +/************************************** +* Basic Types +***************************************/ +#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ +# include + typedef uint8_t BYTE; + typedef uint16_t U16; + typedef uint32_t U32; + typedef int32_t S32; + typedef uint64_t U64; +#else + typedef unsigned char BYTE; + typedef unsigned short U16; + typedef unsigned int U32; + typedef signed int S32; + typedef unsigned long long U64; +#endif + + +/************************************** +* Constants +***************************************/ +#define NBLOOPS 3 +#define TIMELOOP 2000 + +#define KB *(1 <<10) +#define MB *(1 <<20) +#define GB *(1U<<30) + +#define MAX_MEM (2 GB - 64 MB) +#define DEFAULT_CHUNKSIZE (4 MB) + + +/************************************** +* Local structures +***************************************/ +struct chunkParameters +{ + U32 id; + char* origBuffer; + char* compressedBuffer; + int origSize; + int compressedSize; +}; + +struct compressionParameters +{ + int (*compressionFunction)(const char*, char*, int, int); + int (*decompressionFunction)(const char*, char*, int); +}; + + +/************************************** +* MACRO +***************************************/ +#define DISPLAY(...) fprintf(stderr, __VA_ARGS__) + + +/************************************** +* Benchmark Parameters +***************************************/ +static int chunkSize = DEFAULT_CHUNKSIZE; +static int nbIterations = NBLOOPS; +static int BMK_pause = 0; + +void BMK_SetBlocksize(int bsize) { chunkSize = bsize; } + +void BMK_SetNbIterations(int nbLoops) +{ + nbIterations = nbLoops; + DISPLAY("- %i iterations -\n", nbIterations); +} + +void BMK_SetPause(void) { BMK_pause = 1; } + + +/********************************************************* +* Private functions +**********************************************************/ + +#if defined(BMK_LEGACY_TIMER) + +static int BMK_GetMilliStart(void) +{ + /* Based on Legacy ftime() + Rolls over every ~ 12.1 days (0x100000/24/60/60) + Use GetMilliSpan to correct for rollover */ + struct timeb tb; + int nCount; + ftime( &tb ); + nCount = (int) (tb.millitm + (tb.time & 0xfffff) * 1000); + return nCount; +} + +#else + +static int BMK_GetMilliStart(void) +{ + /* Based on newer gettimeofday() + Use GetMilliSpan to correct for rollover */ + struct timeval tv; + int nCount; + gettimeofday(&tv, NULL); + nCount = (int) (tv.tv_usec/1000 + (tv.tv_sec & 0xfffff) * 1000); + return nCount; +} + +#endif + + +static int BMK_GetMilliSpan( int nTimeStart ) +{ + int nSpan = BMK_GetMilliStart() - nTimeStart; + if ( nSpan < 0 ) + nSpan += 0x100000 * 1000; + return nSpan; +} + + +static size_t BMK_findMaxMem(U64 requiredMem) +{ + size_t step = 64 MB; + BYTE* testmem=NULL; + + requiredMem = (((requiredMem >> 26) + 1) << 26); + requiredMem += 2*step; + if (requiredMem > MAX_MEM) requiredMem = MAX_MEM; + + while (!testmem) + { + requiredMem -= step; + testmem = (BYTE*) malloc ((size_t)requiredMem); + } + + free (testmem); + return (size_t) (requiredMem - step); +} + + +static U64 BMK_GetFileSize(char* infilename) +{ + int r; +#if defined(_MSC_VER) + struct _stat64 statbuf; + r = _stat64(infilename, &statbuf); +#else + struct stat statbuf; + r = stat(infilename, &statbuf); +#endif + if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */ + return (U64)statbuf.st_size; +} + + +/********************************************************* +* Public function +**********************************************************/ + +int BMK_benchFile(char** fileNamesTable, int nbFiles, int cLevel) +{ + int fileIdx=0; + char* orig_buff; + struct compressionParameters compP; + int cfunctionId; + + U64 totals = 0; + U64 totalz = 0; + double totalc = 0.; + double totald = 0.; + + + /* Init */ + if (cLevel <= 3) cfunctionId = 0; else cfunctionId = 1; + switch (cfunctionId) + { +#ifdef COMPRESSOR0 + case 0 : compP.compressionFunction = COMPRESSOR0; break; +#endif +#ifdef COMPRESSOR1 + case 1 : compP.compressionFunction = COMPRESSOR1; break; +#endif + default : compP.compressionFunction = DEFAULTCOMPRESSOR; + } + compP.decompressionFunction = LZ4_decompress_fast; + + /* Loop for each file */ + while (fileIdx inFileSize) benchedSize = (size_t)inFileSize; + if (benchedSize < inFileSize) + { + DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", inFileName, (int)(benchedSize>>20)); + } + + /* Alloc */ + chunkP = (struct chunkParameters*) malloc(((benchedSize / (size_t)chunkSize)+1) * sizeof(struct chunkParameters)); + orig_buff = (char*)malloc((size_t )benchedSize); + nbChunks = (int) ((int)benchedSize / chunkSize) + 1; + maxCompressedChunkSize = LZ4_compressBound(chunkSize); + compressedBuffSize = nbChunks * maxCompressedChunkSize; + compressedBuffer = (char*)malloc((size_t )compressedBuffSize); + + + if (!orig_buff || !compressedBuffer) + { + DISPLAY("\nError: not enough memory!\n"); + free(orig_buff); + free(compressedBuffer); + free(chunkP); + fclose(inFile); + return 12; + } + + /* Init chunks data */ + { + int i; + size_t remaining = benchedSize; + char* in = orig_buff; + char* out = compressedBuffer; + for (i=0; i chunkSize) { chunkP[i].origSize = chunkSize; remaining -= chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; } + chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize; + chunkP[i].compressedSize = 0; + } + } + + /* Fill input buffer */ + DISPLAY("Loading %s... \r", inFileName); + readSize = fread(orig_buff, 1, benchedSize, inFile); + fclose(inFile); + + if (readSize != benchedSize) + { + DISPLAY("\nError: problem reading file '%s' !! \n", inFileName); + free(orig_buff); + free(compressedBuffer); + free(chunkP); + return 13; + } + + /* Calculating input Checksum */ + crcOrig = XXH32(orig_buff, (unsigned int)benchedSize,0); + + + /* Bench */ + { + int loopNb, chunkNb; + size_t cSize=0; + double fastestC = 100000000., fastestD = 100000000.; + double ratio=0.; + U32 crcCheck=0; + + DISPLAY("\r%79s\r", ""); + for (loopNb = 1; loopNb <= nbIterations; loopNb++) + { + int nbLoops; + int milliTime; + + /* Compression */ + DISPLAY("%1i-%-14.14s : %9i ->\r", loopNb, inFileName, (int)benchedSize); + { size_t i; for (i=0; i %9i (%5.2f%%),%7.1f MB/s\r", loopNb, inFileName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / fastestC / 1000.); + + /* Decompression */ + { size_t i; for (i=0; i %9i (%5.2f%%),%7.1f MB/s ,%7.1f MB/s\r", loopNb, inFileName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / fastestC / 1000., (double)benchedSize / fastestD / 1000.); + + /* CRC Checking */ + crcCheck = XXH32(orig_buff, (unsigned int)benchedSize,0); + if (crcOrig!=crcCheck) { DISPLAY("\n!!! WARNING !!! %14s : Invalid Checksum : %x != %x\n", inFileName, (unsigned)crcOrig, (unsigned)crcCheck); break; } + } + + if (crcOrig==crcCheck) + { + if (ratio<100.) + DISPLAY("%-16.16s : %9i -> %9i (%5.2f%%),%7.1f MB/s ,%7.1f MB/s\n", inFileName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / fastestC / 1000., (double)benchedSize / fastestD / 1000.); + else + DISPLAY("%-16.16s : %9i -> %9i (%5.1f%%),%7.1f MB/s ,%7.1f MB/s \n", inFileName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / fastestC / 1000., (double)benchedSize / fastestD / 1000.); + } + totals += benchedSize; + totalz += cSize; + totalc += fastestC; + totald += fastestD; + } + + free(orig_buff); + free(compressedBuffer); + free(chunkP); + } + + if (nbFiles > 1) + DISPLAY("%-16.16s :%10llu ->%10llu (%5.2f%%), %6.1f MB/s , %6.1f MB/s\n", " TOTAL", (long long unsigned int)totals, (long long unsigned int)totalz, (double)totalz/(double)totals*100., (double)totals/totalc/1000., (double)totals/totald/1000.); + + if (BMK_pause) { DISPLAY("\npress enter...\n"); getchar(); } + + return 0; +} + + + diff --git a/programs/bench.h b/programs/bench.h new file mode 100644 index 0000000000..d42df68b93 --- /dev/null +++ b/programs/bench.h @@ -0,0 +1,42 @@ +/* + bench.h - Demo program to benchmark open-source compression algorithm + Copyright (C) Yann Collet 2012-2014 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + You can contact the author at : + - LZ4 source repository : http://code.google.com/p/lz4/ + - LZ4 public forum : https://group.google.com/forum/#!forum/lz4c +*/ +#pragma once + +#if defined (__cplusplus) +extern "C" { +#endif + + +/* Main function */ +int BMK_benchFile(char** fileNamesTable, int nbFiles, int cLevel); + +/* Set Parameters */ +void BMK_SetBlocksize(int bsize); +void BMK_SetNbIterations(int nbLoops); +void BMK_SetPause(void); + + + +#if defined (__cplusplus) +} +#endif diff --git a/programs/datagen.c b/programs/datagen.c new file mode 100644 index 0000000000..0f07477441 --- /dev/null +++ b/programs/datagen.c @@ -0,0 +1,286 @@ +/* + datagen.c - compressible data generator test tool + Copyright (C) Yann Collet 2012-2015 + + GPL v2 License + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + You can contact the author at : + - LZ4 source repository : http://code.google.com/p/lz4 + - LZ4 source mirror : https://github.com/Cyan4973/lz4 + - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c +*/ + +/************************************** + Remove Visual warning messages +**************************************/ +#define _CRT_SECURE_NO_WARNINGS // fgets + + +/************************************** + Includes +**************************************/ +#include // fgets, sscanf +#include // strcmp + + +/************************************** + Basic Types +**************************************/ +#if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */ +# include + typedef uint8_t BYTE; + typedef uint16_t U16; + typedef uint32_t U32; + typedef int32_t S32; + typedef uint64_t U64; +#else + typedef unsigned char BYTE; + typedef unsigned short U16; + typedef unsigned int U32; + typedef signed int S32; + typedef unsigned long long U64; +#endif + + +/************************************** + Constants +**************************************/ +#ifndef LZ4_VERSION +# define LZ4_VERSION "r125" +#endif + +#define KB *(1 <<10) +#define MB *(1 <<20) +#define GB *(1U<<30) + +#define CDG_SIZE_DEFAULT (64 KB) +#define CDG_SEED_DEFAULT 0 +#define CDG_COMPRESSIBILITY_DEFAULT 50 +#define PRIME1 2654435761U +#define PRIME2 2246822519U + + +/************************************** + Macros +**************************************/ +#define DISPLAY(...) fprintf(stderr, __VA_ARGS__) +#define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); } + + +/************************************** + Local Parameters +**************************************/ +static unsigned no_prompt = 0; +static char* programName; +static unsigned displayLevel = 2; + + +/********************************************************* + functions +*********************************************************/ + +#define CDG_rotl32(x,r) ((x << r) | (x >> (32 - r))) +static unsigned int CDG_rand(U32* src) +{ + U32 rand32 = *src; + rand32 *= PRIME1; + rand32 += PRIME2; + rand32 = CDG_rotl32(rand32, 13); + *src = rand32; + return rand32; +} + + +#define CDG_RAND15BITS ((CDG_rand(seed) >> 3) & 32767) +#define CDG_RANDLENGTH ( ((CDG_rand(seed) >> 7) & 3) ? (CDG_rand(seed) % 14) : (CDG_rand(seed) & 511) + 15) +#define CDG_RANDCHAR (((CDG_rand(seed) >> 9) & 63) + '0') +static void CDG_generate(U64 size, U32* seed, double proba) +{ + BYTE fullbuff[32 KB + 128 KB + 1]; + BYTE* buff = fullbuff + 32 KB; + U64 total=0; + U32 P32 = (U32)(32768 * proba); + U32 pos=1; + U32 genBlockSize = 128 KB; + + // Build initial prefix + fullbuff[0] = CDG_RANDCHAR; + while (pos<32 KB) + { + // Select : Literal (char) or Match (within 32K) + if (CDG_RAND15BITS < P32) + { + // Copy (within 64K) + U32 d; + int ref; + int length = CDG_RANDLENGTH + 4; + U32 offset = CDG_RAND15BITS + 1; + if (offset > pos) offset = pos; + ref = pos - offset; + d = pos + length; + while (pos < d) fullbuff[pos++] = fullbuff[ref++]; + } + else + { + // Literal (noise) + U32 d = pos + CDG_RANDLENGTH; + while (pos < d) fullbuff[pos++] = CDG_RANDCHAR; + } + } + + // Generate compressible data + pos = 0; + while (total < size) + { + if (size-total < 128 KB) genBlockSize = (U32)(size-total); + total += genBlockSize; + buff[genBlockSize] = 0; + pos = 0; + while (pos genBlockSize ) length = genBlockSize - pos; + ref = pos - offset; + d = pos + length; + while (pos < d) buff[pos++] = buff[ref++]; + } + else + { + // Literal (noise) + U32 d; + int length = CDG_RANDLENGTH; + if (pos + length > genBlockSize) length = genBlockSize - pos; + d = pos + length; + while (pos < d) buff[pos++] = CDG_RANDCHAR; + } + } + // output datagen + pos=0; + for (;pos+512<=genBlockSize;pos+=512) + printf("%512.512s", buff+pos); + for (;pos='0') && (*argument<='9')) + { + size *= 10; + size += *argument - '0'; + argument++; + } + if (*argument=='K') { size <<= 10; argument++; } + if (*argument=='M') { size <<= 20; argument++; } + if (*argument=='G') { size <<= 30; argument++; } + if (*argument=='B') { argument++; } + break; + case 's': + argument++; + seed=0; + while ((*argument>='0') && (*argument<='9')) + { + seed *= 10; + seed += *argument - '0'; + argument++; + } + break; + case 'p': + argument++; + proba=0; + while ((*argument>='0') && (*argument<='9')) + { + proba *= 10; + proba += *argument - '0'; + argument++; + } + if (proba<0) proba=0; + if (proba>100) proba=100; + break; + case 'v': + displayLevel = 4; + argument++; + break; + default: ; + } + } + + } + } + + // Get Seed + DISPLAYLEVEL(4, "Data Generator %s \n", LZ4_VERSION); + DISPLAYLEVEL(3, "Seed = %u \n", seed); + if (proba!=CDG_COMPRESSIBILITY_DEFAULT) DISPLAYLEVEL(3, "Compressibility : %i%%\n", proba); + + CDG_generate(size, &seed, ((double)proba) / 100); + + return 0; +} diff --git a/programs/frametest.c b/programs/frametest.c new file mode 100644 index 0000000000..71490a6a00 --- /dev/null +++ b/programs/frametest.c @@ -0,0 +1,669 @@ +/* + frameTest - test tool for lz4frame + Copyright (C) Yann Collet 2014 + GPL v2 License + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + You can contact the author at : + - LZ4 source repository : http://code.google.com/p/lz4/ + - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c +*/ + +/************************************** + Compiler specific +**************************************/ +#define _CRT_SECURE_NO_WARNINGS // fgets +#ifdef _MSC_VER /* Visual Studio */ +# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ +# pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */ +#endif +#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) +#ifdef __GNUC__ +# pragma GCC diagnostic ignored "-Wmissing-braces" /* GCC bug 53119 : doesn't accept { 0 } as initializer (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119) */ +# pragma GCC diagnostic ignored "-Wmissing-field-initializers" /* GCC bug 53119 : doesn't accept { 0 } as initializer (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119) */ +#endif + + +/************************************** + Includes +**************************************/ +#include // free +#include // fgets, sscanf +#include // timeb +#include // strcmp +#include "lz4frame_static.h" +#include "xxhash.h" // XXH64 + + +/************************************** + Basic Types +**************************************/ +#if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */ +# include +typedef uint8_t BYTE; +typedef uint16_t U16; +typedef uint32_t U32; +typedef int32_t S32; +typedef uint64_t U64; +#else +typedef unsigned char BYTE; +typedef unsigned short U16; +typedef unsigned int U32; +typedef signed int S32; +typedef unsigned long long U64; +#endif + + +/************************************** + Constants +**************************************/ +#ifndef LZ4_VERSION +# define LZ4_VERSION "" +#endif + +#define KB *(1U<<10) +#define MB *(1U<<20) +#define GB *(1U<<30) + +static const U32 nbTestsDefault = 256 KB; +#define COMPRESSIBLE_NOISE_LENGTH (2 MB) +#define FUZ_COMPRESSIBILITY_DEFAULT 50 +static const U32 prime1 = 2654435761U; +static const U32 prime2 = 2246822519U; + + + +/************************************** + Macros +**************************************/ +#define DISPLAY(...) fprintf(stderr, __VA_ARGS__) +#define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); } +#define DISPLAYUPDATE(l, ...) if (displayLevel>=l) { \ + if ((FUZ_GetMilliSpan(g_time) > refreshRate) || (displayLevel>=4)) \ + { g_time = FUZ_GetMilliStart(); DISPLAY(__VA_ARGS__); \ + if (displayLevel>=4) fflush(stdout); } } +static const U32 refreshRate = 150; +static U32 g_time = 0; + + +/***************************************** + Local Parameters +*****************************************/ +static U32 no_prompt = 0; +static char* programName; +static U32 displayLevel = 2; +static U32 pause = 0; + + +/********************************************************* + Fuzzer functions +*********************************************************/ +static U32 FUZ_GetMilliStart(void) +{ + struct timeb tb; + U32 nCount; + ftime( &tb ); + nCount = (U32) (((tb.time & 0xFFFFF) * 1000) + tb.millitm); + return nCount; +} + + +static U32 FUZ_GetMilliSpan(U32 nTimeStart) +{ + U32 nCurrent = FUZ_GetMilliStart(); + U32 nSpan = nCurrent - nTimeStart; + if (nTimeStart > nCurrent) + nSpan += 0x100000 * 1000; + return nSpan; +} + + +# define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) +unsigned int FUZ_rand(unsigned int* src) +{ + U32 rand32 = *src; + rand32 *= prime1; + rand32 += prime2; + rand32 = FUZ_rotl32(rand32, 13); + *src = rand32; + return rand32 >> 5; +} + + +#define FUZ_RAND15BITS (FUZ_rand(seed) & 0x7FFF) +#define FUZ_RANDLENGTH ( (FUZ_rand(seed) & 3) ? (FUZ_rand(seed) % 15) : (FUZ_rand(seed) % 510) + 15) +static void FUZ_fillCompressibleNoiseBuffer(void* buffer, unsigned bufferSize, double proba, U32* seed) +{ + BYTE* BBuffer = (BYTE*)buffer; + unsigned pos = 0; + U32 P32 = (U32)(32768 * proba); + + // First Byte + BBuffer[pos++] = (BYTE)(FUZ_rand(seed)); + + while (pos < bufferSize) + { + // Select : Literal (noise) or copy (within 64K) + if (FUZ_RAND15BITS < P32) + { + // Copy (within 64K) + unsigned match, end; + unsigned length = FUZ_RANDLENGTH + 4; + unsigned offset = FUZ_RAND15BITS + 1; + if (offset > pos) offset = pos; + if (pos + length > bufferSize) length = bufferSize - pos; + match = pos - offset; + end = pos + length; + while (pos < end) BBuffer[pos++] = BBuffer[match++]; + } + else + { + // Literal (noise) + unsigned end; + unsigned length = FUZ_RANDLENGTH; + if (pos + length > bufferSize) length = bufferSize - pos; + end = pos + length; + while (pos < end) BBuffer[pos++] = (BYTE)(FUZ_rand(seed) >> 5); + } + } +} + + +static unsigned FUZ_highbit(U32 v32) +{ + unsigned nbBits = 0; + if (v32==0) return 0; + while (v32) + { + v32 >>= 1; + nbBits ++; + } + return nbBits; +} + + +int basicTests(U32 seed, double compressibility) +{ + int testResult = 0; + void* CNBuffer; + void* compressedBuffer; + void* decodedBuffer; + U32 randState = seed; + size_t cSize, testSize; + LZ4F_preferences_t prefs = { 0 }; + LZ4F_decompressionContext_t dCtx; + U64 crcOrig; + + // Create compressible test buffer + CNBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); + compressedBuffer = malloc(LZ4F_compressFrameBound(COMPRESSIBLE_NOISE_LENGTH, NULL)); + decodedBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); + FUZ_fillCompressibleNoiseBuffer(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, compressibility, &randState); + crcOrig = XXH64(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); + + // Trivial tests : one-step frame + testSize = COMPRESSIBLE_NOISE_LENGTH; + DISPLAYLEVEL(3, "Using NULL preferences : \n"); + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, NULL), CNBuffer, testSize, NULL); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAYLEVEL(3, "Decompression test : \n"); + { + size_t decodedBufferSize = COMPRESSIBLE_NOISE_LENGTH; + size_t compressedBufferSize = cSize; + BYTE* op = (BYTE*)decodedBuffer; + BYTE* const oend = (BYTE*)decodedBuffer + COMPRESSIBLE_NOISE_LENGTH; + BYTE* ip = (BYTE*)compressedBuffer; + BYTE* const iend = (BYTE*)compressedBuffer + cSize; + U64 crcDest; + + LZ4F_errorCode_t errorCode = LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION); + if (LZ4F_isError(errorCode)) goto _output_error; + + DISPLAYLEVEL(3, "Single Block : \n"); + errorCode = LZ4F_decompress(dCtx, decodedBuffer, &decodedBufferSize, compressedBuffer, &compressedBufferSize, NULL); + crcDest = XXH64(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); + if (crcDest != crcOrig) goto _output_error; + DISPLAYLEVEL(3, "Regenerated %i bytes \n", (int)decodedBufferSize); + + DISPLAYLEVEL(3, "Byte after byte : \n"); + while (ip < iend) + { + size_t oSize = oend-op; + size_t iSize = 1; + //DISPLAY("%7i \n", (int)(ip-(BYTE*)compressedBuffer)); + errorCode = LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, NULL); + if (LZ4F_isError(errorCode)) goto _output_error; + op += oSize; + ip += iSize; + } + crcDest = XXH64(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); + if (crcDest != crcOrig) goto _output_error; + DISPLAYLEVEL(3, "Regenerated %i bytes \n", (int)decodedBufferSize); + + errorCode = LZ4F_freeDecompressionContext(dCtx); + if (LZ4F_isError(errorCode)) goto _output_error; + } + + DISPLAYLEVEL(3, "Using 64 KB block : \n"); + prefs.frameInfo.blockSizeID = max64KB; + prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAYLEVEL(3, "without checksum : \n"); + prefs.frameInfo.contentChecksumFlag = noContentChecksum; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAYLEVEL(3, "Using 256 KB block : \n"); + prefs.frameInfo.blockSizeID = max256KB; + prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAYLEVEL(3, "Decompression test : \n"); + { + size_t decodedBufferSize = COMPRESSIBLE_NOISE_LENGTH; + unsigned maxBits = FUZ_highbit((U32)decodedBufferSize); + BYTE* op = (BYTE*)decodedBuffer; + BYTE* const oend = (BYTE*)decodedBuffer + COMPRESSIBLE_NOISE_LENGTH; + BYTE* ip = (BYTE*)compressedBuffer; + BYTE* const iend = (BYTE*)compressedBuffer + cSize; + U64 crcDest; + + LZ4F_errorCode_t errorCode = LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION); + if (LZ4F_isError(errorCode)) goto _output_error; + + DISPLAYLEVEL(3, "random segment sizes : \n"); + while (ip < iend) + { + unsigned nbBits = FUZ_rand(&randState) % maxBits; + size_t iSize = (FUZ_rand(&randState) & ((1< (size_t)(iend-ip)) iSize = iend-ip; + //DISPLAY("%7i : + %6i\n", (int)(ip-(BYTE*)compressedBuffer), (int)iSize); + errorCode = LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, NULL); + if (LZ4F_isError(errorCode)) goto _output_error; + op += oSize; + ip += iSize; + } + crcDest = XXH64(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); + if (crcDest != crcOrig) goto _output_error; + DISPLAYLEVEL(3, "Regenerated %i bytes \n", (int)decodedBufferSize); + + errorCode = LZ4F_freeDecompressionContext(dCtx); + if (LZ4F_isError(errorCode)) goto _output_error; + } + + DISPLAYLEVEL(3, "without checksum : \n"); + prefs.frameInfo.contentChecksumFlag = noContentChecksum; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAYLEVEL(3, "Using 1 MB block : \n"); + prefs.frameInfo.blockSizeID = max1MB; + prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAYLEVEL(3, "without checksum : \n"); + prefs.frameInfo.contentChecksumFlag = noContentChecksum; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAYLEVEL(3, "Using 4 MB block : \n"); + prefs.frameInfo.blockSizeID = max4MB; + prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAYLEVEL(3, "without checksum : \n"); + prefs.frameInfo.contentChecksumFlag = noContentChecksum; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAY("Basic tests completed \n"); +_end: + free(CNBuffer); + free(compressedBuffer); + free(decodedBuffer); + return testResult; + +_output_error: + testResult = 1; + DISPLAY("Error detected ! \n"); + goto _end; +} + + +static void locateBuffDiff(const void* buff1, const void* buff2, size_t size, unsigned nonContiguous) +{ + int p=0; + BYTE* b1=(BYTE*)buff1; + BYTE* b2=(BYTE*)buff2; + if (nonContiguous) + { + DISPLAY("Non-contiguous output test (%i bytes)\n", (int)size); + return; + } + while (b1[p]==b2[p]) p++; + DISPLAY("Error at pos %i/%i : %02X != %02X \n", p, (int)size, b1[p], b2[p]); +} + + +static const U32 srcDataLength = 9 MB; /* needs to be > 2x4MB to test large blocks */ + +int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressibility) +{ + unsigned testResult = 0; + unsigned testNb = 0; + void* srcBuffer = NULL; + void* compressedBuffer = NULL; + void* decodedBuffer = NULL; + U32 coreRand = seed; + LZ4F_decompressionContext_t dCtx = NULL; + LZ4F_compressionContext_t cCtx = NULL; + size_t result; + XXH64_state_t xxh64; +# define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ + DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; } + + // Create buffers + result = LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION); + CHECK(LZ4F_isError(result), "Allocation failed (error %i)", (int)result); + result = LZ4F_createCompressionContext(&cCtx, LZ4F_VERSION); + CHECK(LZ4F_isError(result), "Allocation failed (error %i)", (int)result); + srcBuffer = malloc(srcDataLength); + CHECK(srcBuffer==NULL, "srcBuffer Allocation failed"); + compressedBuffer = malloc(LZ4F_compressFrameBound(srcDataLength, NULL)); + CHECK(compressedBuffer==NULL, "compressedBuffer Allocation failed"); + decodedBuffer = malloc(srcDataLength); + CHECK(decodedBuffer==NULL, "decodedBuffer Allocation failed"); + FUZ_fillCompressibleNoiseBuffer(srcBuffer, srcDataLength, compressibility, &coreRand); + + // jump to requested testNb + for (testNb =0; testNb < startTest; testNb++) (void)FUZ_rand(&coreRand); // sync randomizer + + // main fuzzer loop + for ( ; testNb < nbTests; testNb++) + { + U32 randState = coreRand ^ prime1; + unsigned BSId = 4 + (FUZ_rand(&randState) & 3); + unsigned BMId = FUZ_rand(&randState) & 1; + unsigned CCflag = FUZ_rand(&randState) & 1; + unsigned autoflush = (FUZ_rand(&randState) & 7) == 2; + LZ4F_preferences_t prefs = { 0 }; + LZ4F_compressOptions_t cOptions = { 0 }; + LZ4F_decompressOptions_t dOptions = { 0 }; + unsigned nbBits = (FUZ_rand(&randState) % (FUZ_highbit(srcDataLength-1) - 1)) + 1; + size_t srcSize = (FUZ_rand(&randState) & ((1< (size_t)(iend-ip)) iSize = iend-ip; + cOptions.stableSrc = ((FUZ_rand(&randState) & 3) == 1); + + result = LZ4F_compressUpdate(cCtx, op, oSize, ip, iSize, &cOptions); + CHECK(LZ4F_isError(result), "Compression failed (error %i)", (int)result); + op += result; + ip += iSize; + + if (forceFlush) + { + result = LZ4F_flush(cCtx, op, oend-op, &cOptions); + CHECK(LZ4F_isError(result), "Compression failed (error %i)", (int)result); + op += result; + } + } + result = LZ4F_compressEnd(cCtx, op, oend-op, &cOptions); + CHECK(LZ4F_isError(result), "Compression completion failed (error %i)", (int)result); + op += result; + cSize = op-(BYTE*)compressedBuffer; + } + + { + const BYTE* ip = (const BYTE*)compressedBuffer; + const BYTE* const iend = ip + cSize; + BYTE* op = (BYTE*)decodedBuffer; + BYTE* const oend = op + srcDataLength; + unsigned maxBits = FUZ_highbit((U32)cSize); + unsigned nonContiguousDst = (FUZ_rand(&randState) & 3) == 1; + nonContiguousDst += FUZ_rand(&randState) & nonContiguousDst; /* 0=>0; 1=>1,2 */ + XXH64_reset(&xxh64, 1); + while (ip < iend) + { + unsigned nbBitsI = (FUZ_rand(&randState) % (maxBits-1)) + 1; + unsigned nbBitsO = (FUZ_rand(&randState) % (maxBits)) + 1; + size_t iSize = (FUZ_rand(&randState) & ((1< (size_t)(iend-ip)) iSize = iend-ip; + if (oSize > (size_t)(oend-op)) oSize = oend-op; + dOptions.stableDst = FUZ_rand(&randState) & 1; + if (nonContiguousDst==2) dOptions.stableDst = 0; + //if (ip == compressedBuffer+62073) DISPLAY("oSize : %i : pos %i \n", (int)oSize, (int)(op-(BYTE*)decodedBuffer)); + result = LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, &dOptions); + //if (op+oSize >= (BYTE*)decodedBuffer+94727) DISPLAY("iSize : %i : pos %i \n", (int)iSize, (int)(ip-(BYTE*)compressedBuffer)); + //if ((int)result<0) DISPLAY("iSize : %i : pos %i \n", (int)iSize, (int)(ip-(BYTE*)compressedBuffer)); + if (result == (size_t)-ERROR_checksum_invalid) locateBuffDiff((BYTE*)srcBuffer+srcStart, decodedBuffer, srcSize, nonContiguousDst); + CHECK(LZ4F_isError(result), "Decompression failed (error %i:%s)", (int)result, LZ4F_getErrorName((LZ4F_errorCode_t)result)); + XXH64_update(&xxh64, op, (U32)oSize); + op += oSize; + ip += iSize; + op += nonContiguousDst; + if (nonContiguousDst==2) op = decodedBuffer; // overwritten destination + } + CHECK(result != 0, "Frame decompression failed (error %i)", (int)result); + crcDecoded = XXH64_digest(&xxh64); + if (crcDecoded != crcOrig) locateBuffDiff((BYTE*)srcBuffer+srcStart, decodedBuffer, srcSize, nonContiguousDst); + CHECK(crcDecoded != crcOrig, "Decompression corruption"); + } + } + + DISPLAYLEVEL(2, "\rAll tests completed \n"); + +_end: + LZ4F_freeDecompressionContext(dCtx); + LZ4F_freeCompressionContext(cCtx); + free(srcBuffer); + free(compressedBuffer); + free(decodedBuffer); + + if (pause) + { + DISPLAY("press enter to finish \n"); + getchar(); + } + return testResult; + +_output_error: + testResult = 1; + goto _end; +} + + +int FUZ_usage(void) +{ + DISPLAY( "Usage :\n"); + DISPLAY( " %s [args]\n", programName); + DISPLAY( "\n"); + DISPLAY( "Arguments :\n"); + DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault); + DISPLAY( " -s# : Select seed (default:prompt user)\n"); + DISPLAY( " -t# : Select starting test number (default:0)\n"); + DISPLAY( " -p# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT); + DISPLAY( " -v : verbose\n"); + DISPLAY( " -h : display help and exit\n"); + return 0; +} + + +int main(int argc, char** argv) +{ + U32 seed=0; + int seedset=0; + int argNb; + int nbTests = nbTestsDefault; + int testNb = 0; + int proba = FUZ_COMPRESSIBILITY_DEFAULT; + int result=0; + + // Check command line + programName = argv[0]; + for(argNb=1; argNb='0') && (*argument<='9')) + { + nbTests *= 10; + nbTests += *argument - '0'; + argument++; + } + break; + case 's': + argument++; + seed=0; + seedset=1; + while ((*argument>='0') && (*argument<='9')) + { + seed *= 10; + seed += *argument - '0'; + argument++; + } + break; + case 't': + argument++; + testNb=0; + while ((*argument>='0') && (*argument<='9')) + { + testNb *= 10; + testNb += *argument - '0'; + argument++; + } + break; + case 'P': /* compressibility % */ + argument++; + proba=0; + while ((*argument>='0') && (*argument<='9')) + { + proba *= 10; + proba += *argument - '0'; + argument++; + } + if (proba<0) proba=0; + if (proba>100) proba=100; + break; + default: + ; + return FUZ_usage(); + } + } + } + } + + // Get Seed + printf("Starting lz4frame tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), LZ4_VERSION); + + if (!seedset) seed = FUZ_GetMilliStart() % 10000; + printf("Seed = %u\n", seed); + if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) printf("Compressibility : %i%%\n", proba); + + if (nbTests<=0) nbTests=1; + + if (testNb==0) result = basicTests(seed, ((double)proba) / 100); + if (result) return 1; + return fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100); +} diff --git a/programs/fullbench.c b/programs/fullbench.c new file mode 100644 index 0000000000..b7859246a0 --- /dev/null +++ b/programs/fullbench.c @@ -0,0 +1,842 @@ +/* + bench.c - Demo program to benchmark open-source compression algorithm + Copyright (C) Yann Collet 2012-2015 + + GPL v2 License + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + You can contact the author at : + - LZ4 source repository : http://code.google.com/p/lz4 + - LZ4 source mirror : https://github.com/Cyan4973/lz4 + - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c +*/ + +//************************************** +// Compiler Options +//************************************** +// Disable some Visual warning messages +#define _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_DEPRECATE // VS2005 + +// Unix Large Files support (>4GB) +#if (defined(__sun__) && (!defined(__LP64__))) // Sun Solaris 32-bits requires specific definitions +# define _LARGEFILE_SOURCE +# define _FILE_OFFSET_BITS 64 +#elif ! defined(__LP64__) // No point defining Large file for 64 bit +# define _LARGEFILE64_SOURCE +#endif + +// S_ISREG & gettimeofday() are not supported by MSVC +#if defined(_MSC_VER) || defined(_WIN32) +# define BMK_LEGACY_TIMER 1 +#endif + + +//************************************** +// Includes +//************************************** +#include // malloc +#include // fprintf, fopen, ftello64 +#include // stat64 +#include // stat64 +#include // strcmp + +// Use ftime() if gettimeofday() is not available on your target +#if defined(BMK_LEGACY_TIMER) +# include // timeb, ftime +#else +# include // gettimeofday +#endif + +#include "lz4.h" +#include "lz4hc.h" +#include "lz4frame.h" + +#include "xxhash.h" + + +//************************************** +// Compiler Options +//************************************** +// S_ISREG & gettimeofday() are not supported by MSVC +#if !defined(S_ISREG) +# define S_ISREG(x) (((x) & S_IFMT) == S_IFREG) +#endif + +// GCC does not support _rotl outside of Windows +#if !defined(_WIN32) +# define _rotl(x,r) ((x << r) | (x >> (32 - r))) +#endif + + +//************************************** +// Basic Types +//************************************** +#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99 +# include + typedef uint8_t BYTE; + typedef uint16_t U16; + typedef uint32_t U32; + typedef int32_t S32; + typedef uint64_t U64; +#else + typedef unsigned char BYTE; + typedef unsigned short U16; + typedef unsigned int U32; + typedef signed int S32; + typedef unsigned long long U64; +#endif + + +//**************************** +// Constants +//**************************** +#define PROGRAM_DESCRIPTION "LZ4 speed analyzer" +#ifndef LZ4_VERSION +# define LZ4_VERSION "" +#endif +#define AUTHOR "Yann Collet" +#define WELCOME_MESSAGE "*** %s %s %i-bits, by %s (%s) ***\n", PROGRAM_DESCRIPTION, LZ4_VERSION, (int)(sizeof(void*)*8), AUTHOR, __DATE__ + +#define NBLOOPS 6 +#define TIMELOOP 2500 + +#define KNUTH 2654435761U +#define MAX_MEM (1984<<20) +#define DEFAULT_CHUNKSIZE (4<<20) + +#define ALL_COMPRESSORS 0 +#define ALL_DECOMPRESSORS 0 + + +//************************************** +// Local structures +//************************************** +struct chunkParameters +{ + U32 id; + char* origBuffer; + char* compressedBuffer; + int origSize; + int compressedSize; +}; + + +//************************************** +// MACRO +//************************************** +#define DISPLAY(...) fprintf(stderr, __VA_ARGS__) +#define PROGRESS(...) no_prompt ? 0 : DISPLAY(__VA_ARGS__) + + + +//************************************** +// Benchmark Parameters +//************************************** +static int chunkSize = DEFAULT_CHUNKSIZE; +static int nbIterations = NBLOOPS; +static int BMK_pause = 0; +static int compressionTest = 1; +static int decompressionTest = 1; +static int compressionAlgo = ALL_COMPRESSORS; +static int decompressionAlgo = ALL_DECOMPRESSORS; +static int no_prompt = 0; + +void BMK_SetBlocksize(int bsize) +{ + chunkSize = bsize; + DISPLAY("-Using Block Size of %i KB-\n", chunkSize>>10); +} + +void BMK_SetNbIterations(int nbLoops) +{ + nbIterations = nbLoops; + DISPLAY("- %i iterations -\n", nbIterations); +} + +void BMK_SetPause(void) +{ + BMK_pause = 1; +} + +//********************************************************* +// Private functions +//********************************************************* + +#if defined(BMK_LEGACY_TIMER) + +static int BMK_GetMilliStart(void) +{ + // Based on Legacy ftime() + // Rolls over every ~ 12.1 days (0x100000/24/60/60) + // Use GetMilliSpan to correct for rollover + struct timeb tb; + int nCount; + ftime( &tb ); + nCount = (int) (tb.millitm + (tb.time & 0xfffff) * 1000); + return nCount; +} + +#else + +static int BMK_GetMilliStart(void) +{ + // Based on newer gettimeofday() + // Use GetMilliSpan to correct for rollover + struct timeval tv; + int nCount; + gettimeofday(&tv, NULL); + nCount = (int) (tv.tv_usec/1000 + (tv.tv_sec & 0xfffff) * 1000); + return nCount; +} + +#endif + + +static int BMK_GetMilliSpan( int nTimeStart ) +{ + int nSpan = BMK_GetMilliStart() - nTimeStart; + if ( nSpan < 0 ) + nSpan += 0x100000 * 1000; + return nSpan; +} + + +static size_t BMK_findMaxMem(U64 requiredMem) +{ + size_t step = (64U<<20); // 64 MB + BYTE* testmem=NULL; + + requiredMem = (((requiredMem >> 25) + 1) << 26); + if (requiredMem > MAX_MEM) requiredMem = MAX_MEM; + + requiredMem += 2*step; + while (!testmem) + { + requiredMem -= step; + testmem = (BYTE*) malloc ((size_t)requiredMem); + } + + free (testmem); + return (size_t) (requiredMem - step); +} + + +static U64 BMK_GetFileSize(char* infilename) +{ + int r; +#if defined(_MSC_VER) + struct _stat64 statbuf; + r = _stat64(infilename, &statbuf); +#else + struct stat statbuf; + r = stat(infilename, &statbuf); +#endif + if (r || !S_ISREG(statbuf.st_mode)) return 0; // No good... + return (U64)statbuf.st_size; +} + + +/********************************************************* + Benchmark function +*********************************************************/ + +static int local_LZ4_compress_limitedOutput(const char* in, char* out, int inSize) +{ + return LZ4_compress_limitedOutput(in, out, inSize, LZ4_compressBound(inSize)); +} + +static void* stateLZ4; +static int local_LZ4_compress_withState(const char* in, char* out, int inSize) +{ + return LZ4_compress_withState(stateLZ4, in, out, inSize); +} + +static int local_LZ4_compress_limitedOutput_withState(const char* in, char* out, int inSize) +{ + return LZ4_compress_limitedOutput_withState(stateLZ4, in, out, inSize, LZ4_compressBound(inSize)); +} + +static LZ4_stream_t* ctx; +static int local_LZ4_compress_continue(const char* in, char* out, int inSize) +{ + return LZ4_compress_continue(ctx, in, out, inSize); +} + +static int local_LZ4_compress_limitedOutput_continue(const char* in, char* out, int inSize) +{ + return LZ4_compress_limitedOutput_continue(ctx, in, out, inSize, LZ4_compressBound(inSize)); +} + + +LZ4_stream_t LZ4_dict; +static void* local_LZ4_resetDictT(const char* fake) +{ + (void)fake; + memset(&LZ4_dict, 0, sizeof(LZ4_stream_t)); + return NULL; +} + +int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_dict, const char* source, char* dest, int inputSize); +static int local_LZ4_compress_forceDict(const char* in, char* out, int inSize) +{ + return LZ4_compress_forceExtDict(&LZ4_dict, in, out, inSize); +} + + +static void* stateLZ4HC; +static int local_LZ4_compressHC_withStateHC(const char* in, char* out, int inSize) +{ + return LZ4_compressHC_withStateHC(stateLZ4HC, in, out, inSize); +} + +static int local_LZ4_compressHC_limitedOutput_withStateHC(const char* in, char* out, int inSize) +{ + return LZ4_compressHC_limitedOutput_withStateHC(stateLZ4HC, in, out, inSize, LZ4_compressBound(inSize)); +} + +static int local_LZ4_compressHC_limitedOutput(const char* in, char* out, int inSize) +{ + return LZ4_compressHC_limitedOutput(in, out, inSize, LZ4_compressBound(inSize)); +} + +static int local_LZ4_compressHC_continue(const char* in, char* out, int inSize) +{ + return LZ4_compressHC_continue((LZ4_streamHC_t*)ctx, in, out, inSize); +} + +static int local_LZ4_compressHC_limitedOutput_continue(const char* in, char* out, int inSize) +{ + return LZ4_compressHC_limitedOutput_continue((LZ4_streamHC_t*)ctx, in, out, inSize, LZ4_compressBound(inSize)); +} + +static int local_LZ4F_compressFrame(const char* in, char* out, int inSize) +{ + return (int)LZ4F_compressFrame(out, 2*inSize + 16, in, inSize, NULL); +} + +static int local_LZ4_saveDict(const char* in, char* out, int inSize) +{ + (void)in; + return LZ4_saveDict(&LZ4_dict, out, inSize); +} + +LZ4_streamHC_t LZ4_dictHC; +static int local_LZ4_saveDictHC(const char* in, char* out, int inSize) +{ + (void)in; + return LZ4_saveDictHC(&LZ4_dictHC, out, inSize); +} + + +static int local_LZ4_decompress_fast(const char* in, char* out, int inSize, int outSize) +{ + (void)inSize; + LZ4_decompress_fast(in, out, outSize); + return outSize; +} + +static int local_LZ4_decompress_fast_withPrefix64k(const char* in, char* out, int inSize, int outSize) +{ + (void)inSize; + LZ4_decompress_fast_withPrefix64k(in, out, outSize); + return outSize; +} + +static int local_LZ4_decompress_fast_usingDict(const char* in, char* out, int inSize, int outSize) +{ + (void)inSize; + LZ4_decompress_fast_usingDict(in, out, outSize, out - 65536, 65536); + return outSize; +} + +static int local_LZ4_decompress_safe_usingDict(const char* in, char* out, int inSize, int outSize) +{ + (void)inSize; + LZ4_decompress_safe_usingDict(in, out, inSize, outSize, out - 65536, 65536); + return outSize; +} + +extern int LZ4_decompress_safe_forceExtDict(const char* in, char* out, int inSize, int outSize, const char* dict, int dictSize); + +static int local_LZ4_decompress_safe_forceExtDict(const char* in, char* out, int inSize, int outSize) +{ + (void)inSize; + LZ4_decompress_safe_forceExtDict(in, out, inSize, outSize, out - 65536, 65536); + return outSize; +} + +static int local_LZ4_decompress_safe_partial(const char* in, char* out, int inSize, int outSize) +{ + return LZ4_decompress_safe_partial(in, out, inSize, outSize - 5, outSize); +} + +static LZ4F_decompressionContext_t g_dCtx; + +static int local_LZ4F_decompress(const char* in, char* out, int inSize, int outSize) +{ + size_t srcSize = inSize; + size_t dstSize = outSize; + size_t result; + result = LZ4F_decompress(g_dCtx, out, &dstSize, in, &srcSize, NULL); + if (result!=0) { DISPLAY("Error decompressing frame : unfinished frame\n"); exit(8); } + if (srcSize != (size_t)inSize) { DISPLAY("Error decompressing frame : read size incorrect\n"); exit(9); } + return (int)dstSize; +} + + +int fullSpeedBench(char** fileNamesTable, int nbFiles) +{ + int fileIdx=0; + char* orig_buff; +# define NB_COMPRESSION_ALGORITHMS 16 + double totalCTime[NB_COMPRESSION_ALGORITHMS+1] = {0}; + double totalCSize[NB_COMPRESSION_ALGORITHMS+1] = {0}; +# define NB_DECOMPRESSION_ALGORITHMS 9 + double totalDTime[NB_DECOMPRESSION_ALGORITHMS+1] = {0}; + size_t errorCode; + + errorCode = LZ4F_createDecompressionContext(&g_dCtx, LZ4F_VERSION); + if (LZ4F_isError(errorCode)) + { + DISPLAY("dctx allocation issue \n"); + return 10; + } + + // Loop for each file + while (fileIdx inFileSize) benchedSize = (size_t)inFileSize; + if (benchedSize < inFileSize) + { + DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", inFileName, (int)(benchedSize>>20)); + } + + // Alloc + chunkP = (struct chunkParameters*) malloc(((benchedSize / (size_t)chunkSize)+1) * sizeof(struct chunkParameters)); + orig_buff = (char*) malloc((size_t)benchedSize); + nbChunks = (int) (((int)benchedSize + (chunkSize-1))/ chunkSize); + maxCompressedChunkSize = LZ4_compressBound(chunkSize); + compressedBuffSize = nbChunks * maxCompressedChunkSize; + compressed_buff = (char*)malloc((size_t)compressedBuffSize); + + + if(!orig_buff || !compressed_buff) + { + DISPLAY("\nError: not enough memory!\n"); + free(orig_buff); + free(compressed_buff); + free(chunkP); + fclose(inFile); + return 12; + } + + // Fill input buffer + DISPLAY("Loading %s... \r", inFileName); + readSize = fread(orig_buff, 1, benchedSize, inFile); + fclose(inFile); + + if(readSize != benchedSize) + { + DISPLAY("\nError: problem reading file '%s' !! \n", inFileName); + free(orig_buff); + free(compressed_buff); + free(chunkP); + return 13; + } + + // Calculating input Checksum + crcOriginal = XXH32(orig_buff, (unsigned int)benchedSize,0); + + + // Bench + { + int loopNb, nb_loops, chunkNb, cAlgNb, dAlgNb; + size_t cSize=0; + double ratio=0.; + + DISPLAY("\r%79s\r", ""); + DISPLAY(" %s : \n", inFileName); + + // Compression Algorithms + for (cAlgNb=1; (cAlgNb <= NB_COMPRESSION_ALGORITHMS) && (compressionTest); cAlgNb++) + { + const char* compressorName; + int (*compressionFunction)(const char*, char*, int); + void* (*initFunction)(const char*) = NULL; + double bestTime = 100000000.; + + // Init data chunks + { + int i; + size_t remaining = benchedSize; + char* in = orig_buff; + char* out = compressed_buff; + nbChunks = (int) (((int)benchedSize + (chunkSize-1))/ chunkSize); + for (i=0; i chunkSize) { chunkP[i].origSize = chunkSize; remaining -= chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; } + chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize; + chunkP[i].compressedSize = 0; + } + } + + if ((compressionAlgo != ALL_COMPRESSORS) && (compressionAlgo != cAlgNb)) continue; + + switch(cAlgNb) + { + case 1 : compressionFunction = LZ4_compress; compressorName = "LZ4_compress"; break; + case 2 : compressionFunction = local_LZ4_compress_limitedOutput; compressorName = "LZ4_compress_limitedOutput"; break; + case 3 : compressionFunction = local_LZ4_compress_withState; compressorName = "LZ4_compress_withState"; break; + case 4 : compressionFunction = local_LZ4_compress_limitedOutput_withState; compressorName = "LZ4_compress_limitedOutput_withState"; break; + case 5 : compressionFunction = local_LZ4_compress_continue; initFunction = LZ4_create; compressorName = "LZ4_compress_continue"; break; + case 6 : compressionFunction = local_LZ4_compress_limitedOutput_continue; initFunction = LZ4_create; compressorName = "LZ4_compress_limitedOutput_continue"; break; + case 7 : compressionFunction = LZ4_compressHC; compressorName = "LZ4_compressHC"; break; + case 8 : compressionFunction = local_LZ4_compressHC_limitedOutput; compressorName = "LZ4_compressHC_limitedOutput"; break; + case 9 : compressionFunction = local_LZ4_compressHC_withStateHC; compressorName = "LZ4_compressHC_withStateHC"; break; + case 10: compressionFunction = local_LZ4_compressHC_limitedOutput_withStateHC; compressorName = "LZ4_compressHC_limitedOutput_withStateHC"; break; + case 11: compressionFunction = local_LZ4_compressHC_continue; initFunction = LZ4_createHC; compressorName = "LZ4_compressHC_continue"; break; + case 12: compressionFunction = local_LZ4_compressHC_limitedOutput_continue; initFunction = LZ4_createHC; compressorName = "LZ4_compressHC_limitedOutput_continue"; break; + case 13: compressionFunction = local_LZ4_compress_forceDict; initFunction = local_LZ4_resetDictT; compressorName = "LZ4_compress_forceDict"; break; + case 14: compressionFunction = local_LZ4F_compressFrame; compressorName = "LZ4F_compressFrame"; + chunkP[0].origSize = (int)benchedSize; nbChunks=1; + break; + case 15: compressionFunction = local_LZ4_saveDict; compressorName = "LZ4_saveDict"; + LZ4_loadDict(&LZ4_dict, chunkP[0].origBuffer, chunkP[0].origSize); + break; + case 16: compressionFunction = local_LZ4_saveDictHC; compressorName = "LZ4_saveDictHC"; + LZ4_loadDictHC(&LZ4_dictHC, chunkP[0].origBuffer, chunkP[0].origSize); + break; + default : DISPLAY("ERROR ! Bad algorithm Id !! \n"); free(chunkP); return 1; + } + + for (loopNb = 1; loopNb <= nbIterations; loopNb++) + { + double averageTime; + int milliTime; + + PROGRESS("%1i- %-28.28s :%9i ->\r", loopNb, compressorName, (int)benchedSize); + { size_t i; for (i=0; i%9i (%5.2f%%),%7.1f MB/s\r", loopNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000.); + } + + if (ratio<100.) + DISPLAY("%2i-%-28.28s :%9i ->%9i (%5.2f%%),%7.1f MB/s\n", cAlgNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000.); + else + DISPLAY("%2i-%-28.28s :%9i ->%9i (%5.1f%%),%7.1f MB/s\n", cAlgNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000.); + + totalCTime[cAlgNb] += bestTime; + totalCSize[cAlgNb] += cSize; + } + + // Prepare layout for decompression + // Init data chunks + { + int i; + size_t remaining = benchedSize; + char* in = orig_buff; + char* out = compressed_buff; + nbChunks = (int) (((int)benchedSize + (chunkSize-1))/ chunkSize); + for (i=0; i chunkSize) { chunkP[i].origSize = chunkSize; remaining -= chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; } + chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize; + chunkP[i].compressedSize = 0; + } + } + for (chunkNb=0; chunkNb\r", loopNb, dName, (int)benchedSize); + + nb_loops = 0; + milliTime = BMK_GetMilliStart(); + while(BMK_GetMilliStart() == milliTime); + milliTime = BMK_GetMilliStart(); + while(BMK_GetMilliSpan(milliTime) < TIMELOOP) + { + for (chunkNb=0; chunkNb %7.1f MB/s\r", loopNb, dName, (int)benchedSize, (double)benchedSize / bestTime / 1000.); + + // CRC Checking + crcDecoded = XXH32(orig_buff, (int)benchedSize, 0); + if (crcOriginal!=crcDecoded) { DISPLAY("\n!!! WARNING !!! %14s : Invalid Checksum : %x != %x\n", inFileName, (unsigned)crcOriginal, (unsigned)crcDecoded); exit(1); } + } + + DISPLAY("%2i-%-29.29s :%10i -> %7.1f MB/s\n", dAlgNb, dName, (int)benchedSize, (double)benchedSize / bestTime / 1000.); + + totalDTime[dAlgNb] += bestTime; + } + + } + + free(orig_buff); + free(compressed_buff); + free(chunkP); + } + + if (BMK_pause) { printf("press enter...\n"); getchar(); } + + return 0; +} + + +int usage(char* exename) +{ + DISPLAY( "Usage :\n"); + DISPLAY( " %s [arg] file1 file2 ... fileX\n", exename); + DISPLAY( "Arguments :\n"); + DISPLAY( " -c : compression tests only\n"); + DISPLAY( " -d : decompression tests only\n"); + DISPLAY( " -H/-h : Help (this text + advanced options)\n"); + return 0; +} + +int usage_advanced(void) +{ + DISPLAY( "\nAdvanced options :\n"); + DISPLAY( " -c# : test only compression function # [1-%i]\n", NB_COMPRESSION_ALGORITHMS); + DISPLAY( " -d# : test only decompression function # [1-%i]\n", NB_DECOMPRESSION_ALGORITHMS); + DISPLAY( " -i# : iteration loops [1-9](default : %i)\n", NBLOOPS); + DISPLAY( " -B# : Block size [4-7](default : 7)\n"); + return 0; +} + +int badusage(char* exename) +{ + DISPLAY("Wrong parameters\n"); + usage(exename); + return 0; +} + +int main(int argc, char** argv) +{ + int i, + filenamesStart=2; + char* exename=argv[0]; + char* input_filename=0; + + // Welcome message + DISPLAY(WELCOME_MESSAGE); + + if (argc<2) { badusage(exename); return 1; } + + for(i=1; i= '0') && (argument[1]<= '9')) + { + compressionAlgo *= 10; + compressionAlgo += argument[1] - '0'; + argument++; + } + break; + + // Select decompression algorithm only + case 'd': + compressionTest = 0; + while ((argument[1]>= '0') && (argument[1]<= '9')) + { + decompressionAlgo *= 10; + decompressionAlgo += argument[1] - '0'; + argument++; + } + break; + + // Display help on usage + case 'h' : + case 'H': usage(exename); usage_advanced(); return 0; + + // Modify Block Properties + case 'B': + while (argument[1]!=0) + switch(argument[1]) + { + case '4': + case '5': + case '6': + case '7': + { + int B = argument[1] - '0'; + int S = 1 << (8 + 2*B); + BMK_SetBlocksize(S); + argument++; + break; + } + case 'D': argument++; break; + default : goto _exit_blockProperties; + } +_exit_blockProperties: + break; + + // Modify Nb Iterations + case 'i': + if ((argument[1] >='1') && (argument[1] <='9')) + { + int iters = argument[1] - '0'; + BMK_SetNbIterations(iters); + argument++; + } + break; + + // Pause at the end (hidden option) + case 'p': BMK_SetPause(); break; + + // Unknown command + default : badusage(exename); return 1; + } + } + continue; + } + + // first provided filename is input + if (!input_filename) { input_filename=argument; filenamesStart=i; continue; } + + } + + // No input filename ==> Error + if(!input_filename) { badusage(exename); return 1; } + + return fullSpeedBench(argv+filenamesStart, argc-filenamesStart); + +} + diff --git a/programs/fuzzer.c b/programs/fuzzer.c new file mode 100644 index 0000000000..6d3b0776a9 --- /dev/null +++ b/programs/fuzzer.c @@ -0,0 +1,1167 @@ +/* + fuzzer.c - Fuzzer test tool for LZ4 + Copyright (C) Yann Collet 2012-2015 + + GPL v2 License + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + You can contact the author at : + - LZ4 source repository : http://code.google.com/p/lz4 + - LZ4 source mirror : https://github.com/Cyan4973/lz4 + - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c +*/ + +/************************************** +* Remove Visual warning messages +**************************************/ +#ifdef _MSC_VER /* Visual Studio */ +# define _CRT_SECURE_NO_WARNINGS /* fgets */ +# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ +# pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */ +# pragma warning(disable : 4310) /* disable: C4310: constant char value > 127 */ +#endif + + +/************************************** +* Includes +**************************************/ +#include +#include /* fgets, sscanf */ +#include /* timeb */ +#include /* strcmp */ +#include "lz4.h" +#include "lz4hc.h" +#include "xxhash.h" + + +/************************************** +* Basic Types +**************************************/ +#if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */ +# include +typedef uint8_t BYTE; +typedef uint16_t U16; +typedef uint32_t U32; +typedef int32_t S32; +typedef uint64_t U64; +#else +typedef unsigned char BYTE; +typedef unsigned short U16; +typedef unsigned int U32; +typedef signed int S32; +typedef unsigned long long U64; +#endif + + +/************************************** +* Constants +**************************************/ +#ifndef LZ4_VERSION +# define LZ4_VERSION "" +#endif + +#define NB_ATTEMPTS (1<<16) +#define COMPRESSIBLE_NOISE_LENGTH (1 << 21) +#define FUZ_MAX_BLOCK_SIZE (1 << 17) +#define FUZ_MAX_DICT_SIZE (1 << 15) +#define FUZ_COMPRESSIBILITY_DEFAULT 60 +#define PRIME1 2654435761U +#define PRIME2 2246822519U +#define PRIME3 3266489917U + +#define KB *(1U<<10) +#define MB *(1U<<20) +#define GB *(1U<<30) + + +/***************************************** +* Macros +*****************************************/ +#define DISPLAY(...) fprintf(stderr, __VA_ARGS__) +#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } +static int g_displayLevel = 2; +static const U32 g_refreshRate = 250; +static U32 g_time = 0; + + +/********************************************************* + Fuzzer functions +*********************************************************/ +static U32 FUZ_GetMilliStart(void) +{ + struct timeb tb; + U32 nCount; + ftime( &tb ); + nCount = (U32) (((tb.time & 0xFFFFF) * 1000) + tb.millitm); + return nCount; +} + +static U32 FUZ_GetMilliSpan(U32 nTimeStart) +{ + U32 nCurrent = FUZ_GetMilliStart(); + U32 nSpan = nCurrent - nTimeStart; + if (nTimeStart > nCurrent) + nSpan += 0x100000 * 1000; + return nSpan; +} + +static U32 FUZ_rotl32(U32 u32, U32 nbBits) +{ + return ((u32 << nbBits) | (u32 >> (32 - nbBits))); +} + +static U32 FUZ_rand(U32* src) +{ + U32 rand32 = *src; + rand32 *= PRIME1; + rand32 += PRIME2; + rand32 = FUZ_rotl32(rand32, 13); + *src = rand32; + return rand32 >> 3; +} + + +#define FUZ_RAND15BITS ((FUZ_rand(seed) >> 3) & 32767) +#define FUZ_RANDLENGTH ( ((FUZ_rand(seed) >> 7) & 3) ? (FUZ_rand(seed) % 15) : (FUZ_rand(seed) % 510) + 15) +static void FUZ_fillCompressibleNoiseBuffer(void* buffer, size_t bufferSize, double proba, U32* seed) +{ + BYTE* BBuffer = (BYTE*)buffer; + size_t pos = 0; + U32 P32 = (U32)(32768 * proba); + + // First Byte + BBuffer[pos++] = (BYTE)(FUZ_rand(seed)); + + while (pos < bufferSize) + { + // Select : Literal (noise) or copy (within 64K) + if (FUZ_RAND15BITS < P32) + { + // Copy (within 64K) + size_t match, d; + size_t length = FUZ_RANDLENGTH + 4; + size_t offset = FUZ_RAND15BITS + 1; + if (offset > pos) offset = pos; + d = pos + length; + if (d > bufferSize) d = bufferSize; + match = pos - offset; + while (pos < d) BBuffer[pos++] = BBuffer[match++]; + } + else + { + // Literal (noise) + size_t d; + size_t length = FUZ_RANDLENGTH; + d = pos + length; + if (d > bufferSize) d = bufferSize; + while (pos < d) BBuffer[pos++] = (BYTE)(FUZ_rand(seed) >> 5); + } + } +} + + +#define MAX_NB_BUFF_I134 150 +#define BLOCKSIZE_I134 (32 MB) +static int FUZ_AddressOverflow(void) +{ + char* buffers[MAX_NB_BUFF_I134+1] = {0}; + int i, nbBuff=0; + int highAddress = 0; + + printf("Overflow tests : "); + + // Only possible in 32-bits + if (sizeof(void*)==8) + { + printf("64 bits mode : no overflow \n"); + fflush(stdout); + return 0; + } + + buffers[0] = (char*)malloc(BLOCKSIZE_I134); + buffers[1] = (char*)malloc(BLOCKSIZE_I134); + if ((!buffers[0]) || (!buffers[1])) + { + printf("not enough memory for tests \n"); + return 0; + } + for (nbBuff=2; nbBuff < MAX_NB_BUFF_I134; nbBuff++) + { + printf("%3i \b\b\b\b", nbBuff); + buffers[nbBuff] = (char*)malloc(BLOCKSIZE_I134); + //printf("%08X ", (U32)(size_t)(buffers[nbBuff])); + fflush(stdout); + + if (((size_t)buffers[nbBuff] > (size_t)0x80000000) && (!highAddress)) + { + printf("high address detected : "); + fflush(stdout); + highAddress=1; + } + if (buffers[nbBuff]==NULL) goto _endOfTests; + + { + size_t sizeToGenerateOverflow = (size_t)(- ((size_t)buffers[nbBuff-1]) + 512); + int nbOf255 = (int)((sizeToGenerateOverflow / 255) + 1); + char* input = buffers[nbBuff-1]; + char* output = buffers[nbBuff]; + int r; + input[0] = (char)0xF0; // Literal length overflow + input[1] = (char)0xFF; + input[2] = (char)0xFF; + input[3] = (char)0xFF; + for(i = 4; i <= nbOf255+4; i++) input[i] = (char)0xff; + r = LZ4_decompress_safe(input, output, nbOf255+64, BLOCKSIZE_I134); + if (r>0) goto _overflowError; + input[0] = (char)0x1F; // Match length overflow + input[1] = (char)0x01; + input[2] = (char)0x01; + input[3] = (char)0x00; + r = LZ4_decompress_safe(input, output, nbOf255+64, BLOCKSIZE_I134); + if (r>0) goto _overflowError; + + output = buffers[nbBuff-2]; // Reverse in/out pointer order + input[0] = (char)0xF0; // Literal length overflow + input[1] = (char)0xFF; + input[2] = (char)0xFF; + input[3] = (char)0xFF; + r = LZ4_decompress_safe(input, output, nbOf255+64, BLOCKSIZE_I134); + if (r>0) goto _overflowError; + input[0] = (char)0x1F; // Match length overflow + input[1] = (char)0x01; + input[2] = (char)0x01; + input[3] = (char)0x00; + r = LZ4_decompress_safe(input, output, nbOf255+64, BLOCKSIZE_I134); + if (r>0) goto _overflowError; + } + } + + nbBuff++; +_endOfTests: + for (i=0 ; i g_refreshRate) || (g_displayLevel>=3)) + { + g_time = FUZ_GetMilliStart(); + DISPLAY("\r%5u ", testNb); + if (g_displayLevel>=3) fflush(stdout); + } +} + + +static int FUZ_test(U32 seed, const U32 nbCycles, const U32 startCycle, const double compressibility) +{ + unsigned long long bytes = 0; + unsigned long long cbytes = 0; + unsigned long long hcbytes = 0; + unsigned long long ccbytes = 0; + void* CNBuffer; + char* compressedBuffer; + char* decodedBuffer; +# define FUZ_max LZ4_COMPRESSBOUND(LEN) + int ret; + unsigned cycleNb; +# define FUZ_CHECKTEST(cond, ...) if (cond) { printf("Test %u : ", testNb); printf(__VA_ARGS__); \ + printf(" (seed %u, cycle %u) \n", seed, cycleNb); goto _output_error; } +# define FUZ_DISPLAYTEST { testNb++; g_displayLevel<3 ? 0 : printf("%2u\b\b", testNb); if (g_displayLevel==4) fflush(stdout); } + void* stateLZ4 = malloc(LZ4_sizeofState()); + void* stateLZ4HC = malloc(LZ4_sizeofStateHC()); + void* LZ4continue; + LZ4_stream_t LZ4dict; + LZ4_streamHC_t LZ4dictHC; + U32 crcOrig, crcCheck; + U32 coreRandState = seed; + U32 randState = coreRandState ^ PRIME3; + + + // init + memset(&LZ4dict, 0, sizeof(LZ4dict)); + + // Create compressible test buffer + CNBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); + FUZ_fillCompressibleNoiseBuffer(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, compressibility, &randState); + compressedBuffer = (char*)malloc(LZ4_compressBound(FUZ_MAX_BLOCK_SIZE)); + decodedBuffer = (char*)malloc(FUZ_MAX_DICT_SIZE + FUZ_MAX_BLOCK_SIZE); + + // move to startCycle + for (cycleNb = 0; cycleNb < startCycle; cycleNb++) + { + (void)FUZ_rand(&coreRandState); + + if (0) // some problems related to dictionary re-use; in this case, enable this loop + { + int dictSize, blockSize, blockStart; + char* dict; + char* block; + FUZ_displayUpdate(cycleNb); + randState = coreRandState ^ PRIME3; + blockSize = FUZ_rand(&randState) % FUZ_MAX_BLOCK_SIZE; + blockStart = FUZ_rand(&randState) % (COMPRESSIBLE_NOISE_LENGTH - blockSize); + dictSize = FUZ_rand(&randState) % FUZ_MAX_DICT_SIZE; + if (dictSize > blockStart) dictSize = blockStart; + block = ((char*)CNBuffer) + blockStart; + dict = block - dictSize; + LZ4_loadDict(&LZ4dict, dict, dictSize); + LZ4_compress_continue(&LZ4dict, block, compressedBuffer, blockSize); + LZ4_loadDict(&LZ4dict, dict, dictSize); + LZ4_compress_continue(&LZ4dict, block, compressedBuffer, blockSize); + LZ4_loadDict(&LZ4dict, dict, dictSize); + LZ4_compress_continue(&LZ4dict, block, compressedBuffer, blockSize); + } + } + + // Test loop + for (cycleNb = startCycle; cycleNb < nbCycles; cycleNb++) + { + U32 testNb = 0; + char* dict; + char* block; + int dictSize, blockSize, blockStart, compressedSize, HCcompressedSize; + int blockContinueCompressedSize; + + FUZ_displayUpdate(cycleNb); + (void)FUZ_rand(&coreRandState); + randState = coreRandState ^ PRIME3; + + // Select block to test + blockSize = FUZ_rand(&randState) % FUZ_MAX_BLOCK_SIZE; + blockStart = FUZ_rand(&randState) % (COMPRESSIBLE_NOISE_LENGTH - blockSize); + dictSize = FUZ_rand(&randState) % FUZ_MAX_DICT_SIZE; + if (dictSize > blockStart) dictSize = blockStart; + block = ((char*)CNBuffer) + blockStart; + dict = block - dictSize; + + /* Compression tests */ + + // Test compression HC + FUZ_DISPLAYTEST; + ret = LZ4_compressHC(block, compressedBuffer, blockSize); + FUZ_CHECKTEST(ret==0, "LZ4_compressHC() failed"); + HCcompressedSize = ret; + + // Test compression HC using external state + FUZ_DISPLAYTEST; + ret = LZ4_compressHC_withStateHC(stateLZ4HC, block, compressedBuffer, blockSize); + FUZ_CHECKTEST(ret==0, "LZ4_compressHC_withStateHC() failed"); + + // Test compression using external state + FUZ_DISPLAYTEST; + ret = LZ4_compress_withState(stateLZ4, block, compressedBuffer, blockSize); + FUZ_CHECKTEST(ret==0, "LZ4_compress_withState() failed"); + + // Test compression + FUZ_DISPLAYTEST; + ret = LZ4_compress(block, compressedBuffer, blockSize); + FUZ_CHECKTEST(ret==0, "LZ4_compress() failed"); + compressedSize = ret; + + /* Decompression tests */ + + crcOrig = XXH32(block, blockSize, 0); + + // Test decoding with output size being exactly what's necessary => must work + FUZ_DISPLAYTEST; + ret = LZ4_decompress_fast(compressedBuffer, decodedBuffer, blockSize); + FUZ_CHECKTEST(ret<0, "LZ4_decompress_fast failed despite correct space"); + FUZ_CHECKTEST(ret!=compressedSize, "LZ4_decompress_fast failed : did not fully read compressed data"); + crcCheck = XXH32(decodedBuffer, blockSize, 0); + FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_fast corrupted decoded data"); + + // Test decoding with one byte missing => must fail + FUZ_DISPLAYTEST; + decodedBuffer[blockSize-1] = 0; + ret = LZ4_decompress_fast(compressedBuffer, decodedBuffer, blockSize-1); + FUZ_CHECKTEST(ret>=0, "LZ4_decompress_fast should have failed, due to Output Size being too small"); + FUZ_CHECKTEST(decodedBuffer[blockSize-1], "LZ4_decompress_fast overrun specified output buffer"); + + // Test decoding with one byte too much => must fail + FUZ_DISPLAYTEST; + ret = LZ4_decompress_fast(compressedBuffer, decodedBuffer, blockSize+1); + FUZ_CHECKTEST(ret>=0, "LZ4_decompress_fast should have failed, due to Output Size being too large"); + + // Test decoding with output size exactly what's necessary => must work + FUZ_DISPLAYTEST; + decodedBuffer[blockSize] = 0; + ret = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize, blockSize); + FUZ_CHECKTEST(ret<0, "LZ4_decompress_safe failed despite sufficient space"); + FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe did not regenerate original data"); + FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe overrun specified output buffer size"); + crcCheck = XXH32(decodedBuffer, blockSize, 0); + FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe corrupted decoded data"); + + // Test decoding with more than enough output size => must work + FUZ_DISPLAYTEST; + decodedBuffer[blockSize] = 0; + decodedBuffer[blockSize+1] = 0; + ret = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize, blockSize+1); + FUZ_CHECKTEST(ret<0, "LZ4_decompress_safe failed despite amply sufficient space"); + FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe did not regenerate original data"); + //FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe wrote more than (unknown) target size"); // well, is that an issue ? + FUZ_CHECKTEST(decodedBuffer[blockSize+1], "LZ4_decompress_safe overrun specified output buffer size"); + crcCheck = XXH32(decodedBuffer, blockSize, 0); + FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe corrupted decoded data"); + + // Test decoding with output size being one byte too short => must fail + FUZ_DISPLAYTEST; + decodedBuffer[blockSize-1] = 0; + ret = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize, blockSize-1); + FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe should have failed, due to Output Size being one byte too short"); + FUZ_CHECKTEST(decodedBuffer[blockSize-1], "LZ4_decompress_safe overrun specified output buffer size"); + + // Test decoding with output size being 10 bytes too short => must fail + FUZ_DISPLAYTEST; + if (blockSize>10) + { + decodedBuffer[blockSize-10] = 0; + ret = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize, blockSize-10); + FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe should have failed, due to Output Size being 10 bytes too short"); + FUZ_CHECKTEST(decodedBuffer[blockSize-10], "LZ4_decompress_safe overrun specified output buffer size"); + } + + // Test decoding with input size being one byte too short => must fail + FUZ_DISPLAYTEST; + ret = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize-1, blockSize); + FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe should have failed, due to input size being one byte too short (blockSize=%i, ret=%i, compressedSize=%i)", blockSize, ret, compressedSize); + + // Test decoding with input size being one byte too large => must fail + FUZ_DISPLAYTEST; + decodedBuffer[blockSize] = 0; + ret = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize+1, blockSize); + FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe should have failed, due to input size being too large"); + FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe overrun specified output buffer size"); + + // Test partial decoding with target output size being max/2 => must work + FUZ_DISPLAYTEST; + ret = LZ4_decompress_safe_partial(compressedBuffer, decodedBuffer, compressedSize, blockSize/2, blockSize); + FUZ_CHECKTEST(ret<0, "LZ4_decompress_safe_partial failed despite sufficient space"); + + // Test partial decoding with target output size being just below max => must work + FUZ_DISPLAYTEST; + ret = LZ4_decompress_safe_partial(compressedBuffer, decodedBuffer, compressedSize, blockSize-3, blockSize); + FUZ_CHECKTEST(ret<0, "LZ4_decompress_safe_partial failed despite sufficient space"); + + /* Test Compression with limited output size */ + + /* Test compression with output size being exactly what's necessary (should work) */ + FUZ_DISPLAYTEST; + ret = LZ4_compress_limitedOutput(block, compressedBuffer, blockSize, compressedSize); + FUZ_CHECKTEST(ret==0, "LZ4_compress_limitedOutput() failed despite sufficient space"); + + /* Test compression with output size being exactly what's necessary and external state (should work) */ + FUZ_DISPLAYTEST; + ret = LZ4_compress_limitedOutput_withState(stateLZ4, block, compressedBuffer, blockSize, compressedSize); + FUZ_CHECKTEST(ret==0, "LZ4_compress_limitedOutput_withState() failed despite sufficient space"); + + /* Test HC compression with output size being exactly what's necessary (should work) */ + FUZ_DISPLAYTEST; + ret = LZ4_compressHC_limitedOutput(block, compressedBuffer, blockSize, HCcompressedSize); + FUZ_CHECKTEST(ret==0, "LZ4_compressHC_limitedOutput() failed despite sufficient space"); + + /* Test HC compression with output size being exactly what's necessary (should work) */ + FUZ_DISPLAYTEST; + ret = LZ4_compressHC_limitedOutput_withStateHC(stateLZ4HC, block, compressedBuffer, blockSize, HCcompressedSize); + FUZ_CHECKTEST(ret==0, "LZ4_compressHC_limitedOutput_withStateHC() failed despite sufficient space"); + + /* Test compression with missing bytes into output buffer => must fail */ + FUZ_DISPLAYTEST; + { + int missingBytes = (FUZ_rand(&randState) % 0x3F) + 1; + if (missingBytes >= compressedSize) missingBytes = compressedSize-1; + missingBytes += !missingBytes; /* avoid special case missingBytes==0 */ + compressedBuffer[compressedSize-missingBytes] = 0; + ret = LZ4_compress_limitedOutput(block, compressedBuffer, blockSize, compressedSize-missingBytes); + FUZ_CHECKTEST(ret, "LZ4_compress_limitedOutput should have failed (output buffer too small by %i byte)", missingBytes); + FUZ_CHECKTEST(compressedBuffer[compressedSize-missingBytes], "LZ4_compress_limitedOutput overran output buffer ! (%i missingBytes)", missingBytes) + } + + /* Test HC compression with missing bytes into output buffer => must fail */ + FUZ_DISPLAYTEST; + { + int missingBytes = (FUZ_rand(&randState) % 0x3F) + 1; + if (missingBytes >= HCcompressedSize) missingBytes = HCcompressedSize-1; + missingBytes += !missingBytes; /* avoid special case missingBytes==0 */ + compressedBuffer[HCcompressedSize-missingBytes] = 0; + ret = LZ4_compressHC_limitedOutput(block, compressedBuffer, blockSize, HCcompressedSize-missingBytes); + FUZ_CHECKTEST(ret, "LZ4_compressHC_limitedOutput should have failed (output buffer too small by %i byte)", missingBytes); + FUZ_CHECKTEST(compressedBuffer[HCcompressedSize-missingBytes], "LZ4_compressHC_limitedOutput overran output buffer ! (%i missingBytes)", missingBytes) + } + + + /********************/ + /* Dictionary tests */ + /********************/ + + /* Compress using dictionary */ + FUZ_DISPLAYTEST; + LZ4continue = LZ4_create (dict); + LZ4_compress_continue ((LZ4_stream_t*)LZ4continue, dict, compressedBuffer, dictSize); // Just to fill hash tables + blockContinueCompressedSize = LZ4_compress_continue ((LZ4_stream_t*)LZ4continue, block, compressedBuffer, blockSize); + FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_continue failed"); + free (LZ4continue); + + /* Decompress with dictionary as prefix */ + FUZ_DISPLAYTEST; + memcpy(decodedBuffer, dict, dictSize); + ret = LZ4_decompress_fast_withPrefix64k(compressedBuffer, decodedBuffer+dictSize, blockSize); + FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_decompress_fast_withPrefix64k did not read all compressed block input"); + crcCheck = XXH32(decodedBuffer+dictSize, blockSize, 0); + if (crcCheck!=crcOrig) + { + int i=0; + while (block[i]==decodedBuffer[i]) i++; + printf("Wrong Byte at position %i/%i\n", i, blockSize); + + } + FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_fast_withPrefix64k corrupted decoded data (dict %i)", dictSize); + + FUZ_DISPLAYTEST; + ret = LZ4_decompress_safe_withPrefix64k(compressedBuffer, decodedBuffer+dictSize, blockContinueCompressedSize, blockSize); + FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe_withPrefix64k did not regenerate original data"); + crcCheck = XXH32(decodedBuffer+dictSize, blockSize, 0); + FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe_withPrefix64k corrupted decoded data"); + + // Compress using External dictionary + FUZ_DISPLAYTEST; + dict -= (FUZ_rand(&randState) & 0xF) + 1; // Separation, so it is an ExtDict + if (dict < (char*)CNBuffer) dict = (char*)CNBuffer; + LZ4_loadDict(&LZ4dict, dict, dictSize); + blockContinueCompressedSize = LZ4_compress_continue(&LZ4dict, block, compressedBuffer, blockSize); + FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_continue failed"); + + FUZ_DISPLAYTEST; + LZ4_loadDict(&LZ4dict, dict, dictSize); + ret = LZ4_compress_limitedOutput_continue(&LZ4dict, block, compressedBuffer, blockSize, blockContinueCompressedSize-1); + FUZ_CHECKTEST(ret>0, "LZ4_compress_limitedOutput_continue using ExtDict should fail : one missing byte for output buffer"); + + FUZ_DISPLAYTEST; + LZ4_loadDict(&LZ4dict, dict, dictSize); + ret = LZ4_compress_limitedOutput_continue(&LZ4dict, block, compressedBuffer, blockSize, blockContinueCompressedSize); + FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_limitedOutput_compressed size is different (%i != %i)", ret, blockContinueCompressedSize); + FUZ_CHECKTEST(ret<=0, "LZ4_compress_limitedOutput_continue should work : enough size available within output buffer"); + + // Decompress with dictionary as external + FUZ_DISPLAYTEST; + decodedBuffer[blockSize] = 0; + ret = LZ4_decompress_fast_usingDict(compressedBuffer, decodedBuffer, blockSize, dict, dictSize); + FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_decompress_fast_usingDict did not read all compressed block input"); + FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_fast_usingDict overrun specified output buffer size") + crcCheck = XXH32(decodedBuffer, blockSize, 0); + if (crcCheck!=crcOrig) + { + int i=0; + while (block[i]==decodedBuffer[i]) i++; + printf("Wrong Byte at position %i/%i\n", i, blockSize); + } + FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_fast_usingDict corrupted decoded data (dict %i)", dictSize); + + FUZ_DISPLAYTEST; + decodedBuffer[blockSize] = 0; + ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize, dict, dictSize); + FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe_usingDict did not regenerate original data"); + FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe_usingDict overrun specified output buffer size") + crcCheck = XXH32(decodedBuffer, blockSize, 0); + FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe_usingDict corrupted decoded data"); + + FUZ_DISPLAYTEST; + decodedBuffer[blockSize-1] = 0; + ret = LZ4_decompress_fast_usingDict(compressedBuffer, decodedBuffer, blockSize-1, dict, dictSize); + FUZ_CHECKTEST(ret>=0, "LZ4_decompress_fast_withDict should have failed : wrong original size (-1 byte)"); + FUZ_CHECKTEST(decodedBuffer[blockSize-1], "LZ4_decompress_fast_usingDict overrun specified output buffer size"); + + FUZ_DISPLAYTEST; + decodedBuffer[blockSize-1] = 0; + ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize-1, dict, dictSize); + FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe_usingDict should have failed : not enough output size (-1 byte)"); + FUZ_CHECKTEST(decodedBuffer[blockSize-1], "LZ4_decompress_safe_usingDict overrun specified output buffer size"); + + FUZ_DISPLAYTEST; + { + U32 missingBytes = (FUZ_rand(&randState) & 0xF) + 2; + if ((U32)blockSize > missingBytes) + { + decodedBuffer[blockSize-missingBytes] = 0; + ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize-missingBytes, dict, dictSize); + FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe_usingDict should have failed : output buffer too small (-%u byte)", missingBytes); + FUZ_CHECKTEST(decodedBuffer[blockSize-missingBytes], "LZ4_decompress_safe_usingDict overrun specified output buffer size (-%u byte) (blockSize=%i)", missingBytes, blockSize); + } + } + + // Compress HC using External dictionary + FUZ_DISPLAYTEST; + dict -= (FUZ_rand(&randState) & 7); // even bigger separation + if (dict < (char*)CNBuffer) dict = (char*)CNBuffer; + LZ4_resetStreamHC (&LZ4dictHC, FUZ_rand(&randState) & 0x7); + LZ4_loadDictHC(&LZ4dictHC, dict, dictSize); + blockContinueCompressedSize = LZ4_compressHC_continue(&LZ4dictHC, block, compressedBuffer, blockSize); + FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compressHC_continue failed"); + + FUZ_DISPLAYTEST; + LZ4_loadDictHC(&LZ4dictHC, dict, dictSize); + ret = LZ4_compressHC_limitedOutput_continue(&LZ4dictHC, block, compressedBuffer, blockSize, blockContinueCompressedSize-1); + FUZ_CHECKTEST(ret>0, "LZ4_compressHC_limitedOutput_continue using ExtDict should fail : one missing byte for output buffer"); + + FUZ_DISPLAYTEST; + LZ4_loadDictHC(&LZ4dictHC, dict, dictSize); + ret = LZ4_compressHC_limitedOutput_continue(&LZ4dictHC, block, compressedBuffer, blockSize, blockContinueCompressedSize); + FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_limitedOutput_compressed size is different (%i != %i)", ret, blockContinueCompressedSize); + FUZ_CHECKTEST(ret<=0, "LZ4_compress_limitedOutput_continue should work : enough size available within output buffer"); + + FUZ_DISPLAYTEST; + decodedBuffer[blockSize] = 0; + ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize, dict, dictSize); + FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe_usingDict did not regenerate original data"); + FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe_usingDict overrun specified output buffer size") + crcCheck = XXH32(decodedBuffer, blockSize, 0); + if (crcCheck!=crcOrig) + { + int i=0; + while (block[i]==decodedBuffer[i]) i++; + printf("Wrong Byte at position %i/%i\n", i, blockSize); + } + FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe_usingDict corrupted decoded data"); + + + // ***** End of tests *** // + // Fill stats + bytes += blockSize; + cbytes += compressedSize; + hcbytes += HCcompressedSize; + ccbytes += blockContinueCompressedSize; + } + + printf("\r%7u /%7u - ", cycleNb, nbCycles); + printf("all tests completed successfully \n"); + printf("compression ratio: %0.3f%%\n", (double)cbytes/bytes*100); + printf("HC compression ratio: %0.3f%%\n", (double)hcbytes/bytes*100); + printf("ratio with dict: %0.3f%%\n", (double)ccbytes/bytes*100); + + // unalloc + { + int result = 0; +_exit: + free(CNBuffer); + free(compressedBuffer); + free(decodedBuffer); + free(stateLZ4); + free(stateLZ4HC); + return result; + +_output_error: + result = 1; + goto _exit; + } +} + + +#define testInputSize (192 KB) +#define testCompressedSize (128 KB) +#define ringBufferSize (8 KB) + +static void FUZ_unitTests(void) +{ + const unsigned testNb = 0; + const unsigned seed = 0; + const unsigned cycleNb= 0; + char testInput[testInputSize]; + char testCompressed[testCompressedSize]; + char testVerify[testInputSize]; + char ringBuffer[ringBufferSize]; + U32 randState = 1; + + // Init + FUZ_fillCompressibleNoiseBuffer(testInput, testInputSize, 0.50, &randState); + + // 32-bits address space overflow test + FUZ_AddressOverflow(); + + // LZ4 streaming tests + { + LZ4_stream_t* statePtr; + LZ4_stream_t streamingState; + U64 crcOrig; + U64 crcNew; + int result; + + // Allocation test + statePtr = LZ4_createStream(); + FUZ_CHECKTEST(statePtr==NULL, "LZ4_createStream() allocation failed"); + LZ4_freeStream(statePtr); + + // simple compression test + crcOrig = XXH64(testInput, testCompressedSize, 0); + LZ4_resetStream(&streamingState); + result = LZ4_compress_limitedOutput_continue(&streamingState, testInput, testCompressed, testCompressedSize, testCompressedSize-1); + FUZ_CHECKTEST(result==0, "LZ4_compress_limitedOutput_continue() compression failed"); + + result = LZ4_decompress_safe(testCompressed, testVerify, result, testCompressedSize); + FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() decompression failed"); + crcNew = XXH64(testVerify, testCompressedSize, 0); + FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() decompression corruption"); + + // ring buffer test + { + XXH64_state_t xxhOrig; + XXH64_state_t xxhNew; + LZ4_streamDecode_t decodeState; + const U32 maxMessageSizeLog = 10; + const U32 maxMessageSizeMask = (1< ringBufferSize) rNext = 0; + if (dNext + messageSize > dBufferSize) dNext = 0; + } + } + } + + // LZ4 HC streaming tests + { + LZ4_streamHC_t* sp; + LZ4_streamHC_t sHC; + //XXH64_state_t xxh; + U64 crcOrig; + U64 crcNew; + int result; + + // Allocation test + sp = LZ4_createStreamHC(); + FUZ_CHECKTEST(sp==NULL, "LZ4_createStreamHC() allocation failed"); + LZ4_freeStreamHC(sp); + + // simple compression test + crcOrig = XXH64(testInput, testCompressedSize, 0); + LZ4_resetStreamHC(&sHC, 0); + result = LZ4_compressHC_limitedOutput_continue(&sHC, testInput, testCompressed, testCompressedSize, testCompressedSize-1); + FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() compression failed"); + + result = LZ4_decompress_safe(testCompressed, testVerify, result, testCompressedSize); + FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() decompression failed"); + crcNew = XXH64(testVerify, testCompressedSize, 0); + FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() decompression corruption"); + + // simple dictionary compression test + crcOrig = XXH64(testInput + 64 KB, testCompressedSize, 0); + LZ4_resetStreamHC(&sHC, 0); + LZ4_loadDictHC(&sHC, testInput, 64 KB); + result = LZ4_compressHC_limitedOutput_continue(&sHC, testInput + 64 KB, testCompressed, testCompressedSize, testCompressedSize-1); + FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result); + + result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result, testCompressedSize, testInput, 64 KB); + FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() simple dictionary decompression test failed"); + crcNew = XXH64(testVerify, testCompressedSize, 0); + FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() simple dictionary decompression test : corruption"); + + // multiple HC compression test with dictionary + { + int result1, result2; + int segSize = testCompressedSize / 2; + crcOrig = XXH64(testInput + segSize, testCompressedSize, 0); + LZ4_resetStreamHC(&sHC, 0); + LZ4_loadDictHC(&sHC, testInput, segSize); + result1 = LZ4_compressHC_limitedOutput_continue(&sHC, testInput + segSize, testCompressed, segSize, segSize -1); + FUZ_CHECKTEST(result1==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result1); + result2 = LZ4_compressHC_limitedOutput_continue(&sHC, testInput + 2*segSize, testCompressed+result1, segSize, segSize-1); + FUZ_CHECKTEST(result2==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result2); + + result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result1, segSize, testInput, segSize); + FUZ_CHECKTEST(result!=segSize, "LZ4_decompress_safe() dictionary decompression part 1 failed"); + result = LZ4_decompress_safe_usingDict(testCompressed+result1, testVerify+segSize, result2, segSize, testInput, 2*segSize); + FUZ_CHECKTEST(result!=segSize, "LZ4_decompress_safe() dictionary decompression part 2 failed"); + crcNew = XXH64(testVerify, testCompressedSize, 0); + FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() dictionary decompression corruption"); + } + + // remote dictionary HC compression test + crcOrig = XXH64(testInput + 64 KB, testCompressedSize, 0); + LZ4_resetStreamHC(&sHC, 0); + LZ4_loadDictHC(&sHC, testInput, 32 KB); + result = LZ4_compressHC_limitedOutput_continue(&sHC, testInput + 64 KB, testCompressed, testCompressedSize, testCompressedSize-1); + FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() remote dictionary failed : result = %i", result); + + result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result, testCompressedSize, testInput, 32 KB); + FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe_usingDict() decompression failed following remote dictionary HC compression test"); + crcNew = XXH64(testVerify, testCompressedSize, 0); + FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_usingDict() decompression corruption"); + + // multiple HC compression with ext. dictionary + { + XXH64_state_t crcOrigState; + XXH64_state_t crcNewState; + const char* dict = testInput + 3; + int dictSize = (FUZ_rand(&randState) & 8191); + char* dst = testVerify; + + size_t segStart = dictSize + 7; + int segSize = (FUZ_rand(&randState) & 8191); + int segNb = 1; + + LZ4_resetStreamHC(&sHC, 0); + LZ4_loadDictHC(&sHC, dict, dictSize); + + XXH64_reset(&crcOrigState, 0); + XXH64_reset(&crcNewState, 0); + + while (segStart + segSize < testInputSize) + { + XXH64_update(&crcOrigState, testInput + segStart, segSize); + crcOrig = XXH64_digest(&crcOrigState); + result = LZ4_compressHC_limitedOutput_continue(&sHC, testInput + segStart, testCompressed, segSize, LZ4_compressBound(segSize)); + FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result); + + result = LZ4_decompress_safe_usingDict(testCompressed, dst, result, segSize, dict, dictSize); + FUZ_CHECKTEST(result!=segSize, "LZ4_decompress_safe_usingDict() dictionary decompression part %i failed", segNb); + XXH64_update(&crcNewState, dst, segSize); + crcNew = XXH64_digest(&crcNewState); + if (crcOrig!=crcNew) + { + size_t c=0; + while (dst[c] == testInput[segStart+c]) c++; + DISPLAY("Bad decompression at %u / %u \n", (U32)c, (U32)segSize); + } + FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_usingDict() part %i corruption", segNb); + + dict = dst; + //dict = testInput + segStart; + dictSize = segSize; + + dst += segSize + 1; + segNb ++; + + segStart += segSize + (FUZ_rand(&randState) & 0xF) + 1; + segSize = (FUZ_rand(&randState) & 8191); + } + } + + // ring buffer test + { + XXH64_state_t xxhOrig; + XXH64_state_t xxhNew; + LZ4_streamDecode_t decodeState; + const U32 maxMessageSizeLog = 10; + const U32 maxMessageSizeMask = (1< ringBufferSize) rNext = 0; + if (dNext + messageSize > dBufferSize) dNext = 0; + } + } + + // small decoder-side ring buffer test + { + XXH64_state_t xxhOrig; + XXH64_state_t xxhNew; + LZ4_streamDecode_t decodeState; + const U32 maxMessageSizeLog = 10; + const U32 maxMessageSizeMask = (1< dBufferSize) dNext = 0; + } + } + + // long stream test ; Warning : very long test ! + if (1) + { + XXH64_state_t crcOrigState; + XXH64_state_t crcNewState; + const U64 totalTestSize = 6ULL << 30; + U64 totalTestDone = 0; + size_t oldStart = 0; + size_t oldSize = 0; + U32 segNb = 1; + + DISPLAY("Long HC streaming test (%u MB)\n", (U32)(totalTestSize >> 20)); + LZ4_resetStreamHC(&sHC, 0); + + XXH64_reset(&crcOrigState, 0); + XXH64_reset(&crcNewState, 0); + + while (totalTestDone < totalTestSize) + { + size_t testSize = (FUZ_rand(&randState) & 65535) + 1; + size_t testStart = FUZ_rand(&randState) & 65535; + + FUZ_displayUpdate((U32)(totalTestDone >> 20)); + + if (testStart == oldStart + oldSize) // Corner case not covered by this test (LZ4_decompress_safe_usingDict() limitation) + testStart++; + + XXH64_update(&crcOrigState, testInput + testStart, testSize); + crcOrig = XXH64_digest(&crcOrigState); + + result = LZ4_compressHC_limitedOutput_continue(&sHC, testInput + testStart, testCompressed, (int)testSize, LZ4_compressBound((int)testSize)); + FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result); + + result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result, (int)testSize, testInput + oldStart, (int)oldSize); + FUZ_CHECKTEST(result!=(int)testSize, "LZ4_decompress_safe_usingDict() dictionary decompression part %u failed", segNb); + + XXH64_update(&crcNewState, testVerify, testSize); + crcNew = XXH64_digest(&crcNewState); + if (crcOrig!=crcNew) + { + size_t c=0; + while (testVerify[c] == testInput[testStart+c]) c++; + DISPLAY("Bad decompression at %u / %u \n", (U32)c, (U32)testSize); + } + FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_usingDict() part %u corruption", segNb); + + oldStart = testStart; + oldSize = testSize; + totalTestDone += testSize; + + segNb ++; + } + + DISPLAY("\r"); + } + } + + printf("All unit tests completed successfully \n"); + return; +_output_error: + exit(1); +} + + +static int FUZ_usage(char* programName) +{ + DISPLAY( "Usage :\n"); + DISPLAY( " %s [args]\n", programName); + DISPLAY( "\n"); + DISPLAY( "Arguments :\n"); + DISPLAY( " -i# : Nb of tests (default:%i) \n", NB_ATTEMPTS); + DISPLAY( " -s# : Select seed (default:prompt user)\n"); + DISPLAY( " -t# : Select starting test number (default:0)\n"); + DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT); + DISPLAY( " -v : verbose\n"); + DISPLAY( " -p : pause at the end\n"); + DISPLAY( " -h : display help and exit\n"); + return 0; +} + + +int main(int argc, char** argv) +{ + U32 seed=0; + int seedset=0; + int argNb; + int nbTests = NB_ATTEMPTS; + int testNb = 0; + int proba = FUZ_COMPRESSIBILITY_DEFAULT; + int pause = 0; + char* programName = argv[0]; + + // Check command line + for(argNb=1; argNb='0') && (*argument<='9')) + { + nbTests *= 10; + nbTests += *argument - '0'; + argument++; + } + break; + + case 's': + argument++; + seed=0; seedset=1; + while ((*argument>='0') && (*argument<='9')) + { + seed *= 10; + seed += *argument - '0'; + argument++; + } + break; + + case 't': /* select starting test nb */ + argument++; + testNb=0; + while ((*argument>='0') && (*argument<='9')) + { + testNb *= 10; + testNb += *argument - '0'; + argument++; + } + break; + + case 'P': /* change probability */ + argument++; + proba=0; + while ((*argument>='0') && (*argument<='9')) + { + proba *= 10; + proba += *argument - '0'; + argument++; + } + if (proba<0) proba=0; + if (proba>100) proba=100; + break; + default: ; + } + } + } + } + + // Get Seed + printf("Starting LZ4 fuzzer (%i-bits, %s)\n", (int)(sizeof(size_t)*8), LZ4_VERSION); + + if (!seedset) seed = FUZ_GetMilliStart() % 10000; + printf("Seed = %u\n", seed); + if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) printf("Compressibility : %i%%\n", proba); + + if ((seedset==0) && (testNb==0)) FUZ_unitTests(); + + if (nbTests<=0) nbTests=1; + + { + int result = FUZ_test(seed, nbTests, testNb, ((double)proba) / 100); + if (pause) + { + DISPLAY("press enter ... \n"); + getchar(); + } + return result; + } +} diff --git a/programs/lz4.1 b/programs/lz4.1 new file mode 100644 index 0000000000..6ae8d3c1e8 --- /dev/null +++ b/programs/lz4.1 @@ -0,0 +1,88 @@ +\" +\" lz4.1: This is a manual page for 'lz4' program. This file is part of the +\" lz4 project. +\" + +\" No hyphenation +.hy 0 +.nr HY 0 + +.TH lz4 "1" "2014-02-27" "lz4" "User Commands" +.SH NAME +\fBlz4\fR - Extremely fast compression algorithm + +.SH SYNOPSIS +.TP 5 +\fBlz4\fR [\fBOPTIONS\fR] [-|INPUT-FILE] + +.SH DESCRIPTION +.PP +\fBlz4\fR is an extremely fast lossless compression algorithm. It is based on +the \fBLZ77\fR family of compression scheme. At the compression speed of 400 +MB/s per core, \fBlz4\fR is also scalable with multi-core CPUs. It features +an extremely fast decoder, with speed in multiple GB/s per core, typically +reaching the RAM speed limits on multi-core systems. \fBlz4\fR supports +following options + +.SH OPTIONS +.TP +.B \-1 + fast compression (default) +.TP +.B \-9 + high compression +.TP +.B \-d + decompression +.TP +.B \-f + overwrite output without prompting +.TP +.B \-h/\-H + display help/long help and exit +.TP +.B \-V + display Version number and exit +.TP +.B \-v + verbose mode +.TP +.B \-q + suppress warnings; specify twice to suppress errors too +.TP +.B \-c + force write to standard output, even if it is the console +.TP +.B \-t + test compressed file integrity +.TP +.B \-z + force compression +.TP +.B \-l + use Legacy format (useful for Linux Kernel compression) +.TP +.B \-B# + block size [4-7](default : 7) + B4= 64KB ; B5= 256KB ; B6= 1MB ; B7= 4MB +.TP +.B \-BD + block dependency (improve compression ratio) +.TP +.B \-BX + enable block checksum (default:disabled) +.TP +.B \-Sx + disable stream checksum (default:enabled) +.TP +.B \-b + benchmark file(s) +.TP +.B \-i# + iteration loops [1-9](default : 3), benchmark mode only + +.SH BUGS +Report bugs at:- https://code.google.com/p/lz4/ + +.SH AUTHOR +Yann Collet diff --git a/programs/lz4c.1 b/programs/lz4c.1 new file mode 100644 index 0000000000..fed6c8b8bd --- /dev/null +++ b/programs/lz4c.1 @@ -0,0 +1,33 @@ +\" +\" lz4c.1: This is a manual page for 'lz4c' program. This file is part of the +\" lz4 project. +\" + +\" No hyphenation +.hy 0 +.nr HY 0 + +.TH lz4c "1" "2014-04-15" "lz4c" "User Commands" +.SH NAME +\fBlz4\fR - Extremely fast compression algorithm + +.SH SYNOPSIS +.TP 5 +\fBlz4c\fR [\fBOPTIONS\fR] [-|INPUT-FILE] + +.SH DESCRIPTION +.PP +\fBlz4c\fR is the legacy version of \fBlz4\fR. +As such, it supports older supplementary legacy commands. +\fBlz4c\fR is now deprecated. +It is recommended to use \fBlz4\fR instead whenever possible. + +To get a list of commands specific to lz4c, do : +lz4c -h + + +.SH BUGS +Report bugs at:- https://code.google.com/p/lz4/ + +.SH AUTHOR +Yann Collet \ No newline at end of file diff --git a/programs/lz4cat.1 b/programs/lz4cat.1 new file mode 100644 index 0000000000..64ddbc8a45 --- /dev/null +++ b/programs/lz4cat.1 @@ -0,0 +1,32 @@ +\" +\" lz4cat.1: This is a manual page for 'lz4cat' program. This file is part of +\" the lz4 project. +\" + +\" No hyphenation +.hy 0 +.nr HY 0 + +.TH lz4cat "1" "2014-06-20" "lz4cat" "User Commands" +.SH NAME +\fBlz4cat\fR - Utility based on LZ4 + +.SH SYNOPSIS +.TP 5 +\fBlz4cat\fR [\fBOPTIONS\fR] [-|INPUT-FILE] + +.SH DESCRIPTION +.PP +\fBlz4cat\fR is an utility based on \fBlz4\fR, an extremely fast lossless compression algorithm. + +\fBlz4cat\fR decompress input file or stream, redirecting its output to the console. +It is equivalent to \fBlz4 -cd\fR, + +Available options are the same as \fBlz4\fR ones (man lz4). + + +.SH BUGS +Report bugs at:- https://code.google.com/p/lz4/ + +.SH AUTHOR +Yann Collet diff --git a/programs/lz4cli.c b/programs/lz4cli.c new file mode 100644 index 0000000000..0da5dce7a5 --- /dev/null +++ b/programs/lz4cli.c @@ -0,0 +1,501 @@ +/* + LZ4cli - LZ4 Command Line Interface + Copyright (C) Yann Collet 2011-2014 + + GPL v2 License + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + You can contact the author at : + - LZ4 source repository : http://code.google.com/p/lz4/ + - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c +*/ +/* + Note : this is stand-alone program. + It is not part of LZ4 compression library, it is a user program of the LZ4 library. + The license of LZ4 library is BSD. + The license of xxHash library is BSD. + The license of this compression CLI program is GPLv2. +*/ + +/************************************** +* Tuning parameters +***************************************/ +/* ENABLE_LZ4C_LEGACY_OPTIONS : + Control the availability of -c0, -c1 and -hc legacy arguments + Default : Legacy options are disabled */ +/* #define ENABLE_LZ4C_LEGACY_OPTIONS */ + + +/************************************** +* Compiler Options +***************************************/ +/* Disable some Visual warning messages */ +#ifdef _MSC_VER +# define _CRT_SECURE_NO_WARNINGS +# define _CRT_SECURE_NO_DEPRECATE /* VS2005 */ +# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ +#endif + +#define _POSIX_SOURCE 1 /* for fileno() within on unix */ + + +/**************************** +* Includes +*****************************/ +#include /* fprintf, getchar */ +#include /* exit, calloc, free */ +#include /* strcmp, strlen */ +#include "bench.h" /* BMK_benchFile, BMK_SetNbIterations, BMK_SetBlocksize, BMK_SetPause */ +#include "lz4io.h" + + +/**************************** +* OS-specific Includes +*****************************/ +#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) +# include /* _O_BINARY */ +# include /* _setmode, _isatty */ +# ifdef __MINGW32__ + int _fileno(FILE *stream); /* MINGW somehow forgets to include this prototype into */ +# endif +# define SET_BINARY_MODE(file) _setmode(_fileno(file), _O_BINARY) +# define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream)) +#else +# include /* isatty */ +# define SET_BINARY_MODE(file) +# define IS_CONSOLE(stdStream) isatty(fileno(stdStream)) +#endif + + +/***************************** +* Constants +******************************/ +#define COMPRESSOR_NAME "LZ4 command line interface" +#ifndef LZ4_VERSION +# define LZ4_VERSION "r126" +#endif +#define AUTHOR "Yann Collet" +#define WELCOME_MESSAGE "*** %s %i-bits %s, by %s (%s) ***\n", COMPRESSOR_NAME, (int)(sizeof(void*)*8), LZ4_VERSION, AUTHOR, __DATE__ +#define LZ4_EXTENSION ".lz4" +#define LZ4_CAT "lz4cat" + +#define KB *(1U<<10) +#define MB *(1U<<20) +#define GB *(1U<<30) + +#define LZ4_BLOCKSIZEID_DEFAULT 7 + + +/************************************** +* Macros +***************************************/ +#define DISPLAY(...) fprintf(stderr, __VA_ARGS__) +#define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); } +static unsigned displayLevel = 2; /* 0 : no display ; 1: errors ; 2 : + result + interaction + warnings ; 3 : + progression; 4 : + information */ + + +/************************************** +* Local Variables +***************************************/ +static char* programName; + + +/************************************** +* Exceptions +***************************************/ +#define DEBUG 0 +#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); +#define EXM_THROW(error, ...) \ +{ \ + DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \ + DISPLAYLEVEL(1, "Error %i : ", error); \ + DISPLAYLEVEL(1, __VA_ARGS__); \ + DISPLAYLEVEL(1, "\n"); \ + exit(error); \ +} + + +/************************************** +* Version modifiers +***************************************/ +#define EXTENDED_ARGUMENTS +#define EXTENDED_HELP +#define EXTENDED_FORMAT +#define DEFAULT_COMPRESSOR LZ4IO_compressFilename +#define DEFAULT_DECOMPRESSOR LZ4IO_decompressFilename +int LZ4IO_compressFilename_Legacy(char* input_filename, char* output_filename, int compressionlevel); /* hidden function */ + + +/**************************** +* Functions +*****************************/ +static int usage(void) +{ + DISPLAY( "Usage :\n"); + DISPLAY( " %s [arg] [input] [output]\n", programName); + DISPLAY( "\n"); + DISPLAY( "input : a filename\n"); + DISPLAY( " with no FILE, or when FILE is - or %s, read standard input\n", stdinmark); + DISPLAY( "Arguments :\n"); + DISPLAY( " -1 : Fast compression (default) \n"); + DISPLAY( " -9 : High compression \n"); + DISPLAY( " -d : decompression (default for %s extension)\n", LZ4_EXTENSION); + DISPLAY( " -z : force compression\n"); + DISPLAY( " -f : overwrite output without prompting \n"); + DISPLAY( " -h/-H : display help/long help and exit\n"); + return 0; +} + +static int usage_advanced(void) +{ + DISPLAY(WELCOME_MESSAGE); + usage(); + DISPLAY( "\n"); + DISPLAY( "Advanced arguments :\n"); + DISPLAY( " -V : display Version number and exit\n"); + DISPLAY( " -v : verbose mode\n"); + DISPLAY( " -q : suppress warnings; specify twice to suppress errors too\n"); + DISPLAY( " -c : force write to standard output, even if it is the console\n"); + DISPLAY( " -t : test compressed file integrity\n"); + DISPLAY( " -l : compress using Legacy format (Linux kernel compression)\n"); + DISPLAY( " -B# : Block size [4-7](default : 7)\n"); + DISPLAY( " -BD : Block dependency (improve compression ratio)\n"); + /* DISPLAY( " -BX : enable block checksum (default:disabled)\n"); *//* Option currently inactive */ + DISPLAY( " -Sx : disable stream checksum (default:enabled)\n"); + DISPLAY( "Benchmark arguments :\n"); + DISPLAY( " -b : benchmark file(s)\n"); + DISPLAY( " -i# : iteration loops [1-9](default : 3), benchmark mode only\n"); +#if defined(ENABLE_LZ4C_LEGACY_OPTIONS) + DISPLAY( "Legacy arguments :\n"); + DISPLAY( " -c0 : fast compression\n"); + DISPLAY( " -c1 : high compression\n"); + DISPLAY( " -hc : high compression\n"); + DISPLAY( " -y : overwrite output without prompting \n"); + DISPLAY( " -s : suppress warnings \n"); +#endif /* ENABLE_LZ4C_LEGACY_OPTIONS */ + EXTENDED_HELP; + return 0; +} + +static int usage_longhelp(void) +{ + DISPLAY( "\n"); + DISPLAY( "Which values can get [output] ? \n"); + DISPLAY( "[output] : a filename\n"); + DISPLAY( " '%s', or '-' for standard output (pipe mode)\n", stdoutmark); + DISPLAY( " '%s' to discard output (test mode)\n", NULL_OUTPUT); + DISPLAY( "[output] can be left empty. In this case, it receives the following value : \n"); + DISPLAY( " - if stdout is not the console, then [output] = stdout \n"); + DISPLAY( " - if stdout is console : \n"); + DISPLAY( " + if compression selected, output to filename%s \n", LZ4_EXTENSION); + DISPLAY( " + if decompression selected, output to filename without '%s'\n", LZ4_EXTENSION); + DISPLAY( " > if input filename has no '%s' extension : error\n", LZ4_EXTENSION); + DISPLAY( "\n"); + DISPLAY( "Compression levels : \n"); + DISPLAY( "There are technically 2 accessible compression levels.\n"); + DISPLAY( "-0 ... -2 => Fast compression\n"); + DISPLAY( "-3 ... -9 => High compression\n"); + DISPLAY( "\n"); + DISPLAY( "stdin, stdout and the console : \n"); + DISPLAY( "To protect the console from binary flooding (bad argument mistake)\n"); + DISPLAY( "%s will refuse to read from console, or write to console \n", programName); + DISPLAY( "except if '-c' command is specified, to force output to console \n"); + DISPLAY( "\n"); + DISPLAY( "Simple example :\n"); + DISPLAY( "1 : compress 'filename' fast, using default output name 'filename.lz4'\n"); + DISPLAY( " %s filename\n", programName); + DISPLAY( "\n"); + DISPLAY( "Arguments can be appended together, or provided independently. For example :\n"); + DISPLAY( "2 : compress 'filename' in high compression mode, overwrite output if exists\n"); + DISPLAY( " %s -f9 filename \n", programName); + DISPLAY( " is equivalent to :\n"); + DISPLAY( " %s -f -9 filename \n", programName); + DISPLAY( "\n"); + DISPLAY( "%s can be used in 'pure pipe mode', for example :\n", programName); + DISPLAY( "3 : compress data stream from 'generator', send result to 'consumer'\n"); + DISPLAY( " generator | %s | consumer \n", programName); +#if defined(ENABLE_LZ4C_LEGACY_OPTIONS) + DISPLAY( "\n"); + DISPLAY( "Warning :\n"); + DISPLAY( "Legacy arguments take precedence. Therefore : \n"); + DISPLAY( " %s -hc filename\n", programName); + DISPLAY( "means 'compress filename in high compression mode'\n"); + DISPLAY( "It is not equivalent to :\n"); + DISPLAY( " %s -h -c filename\n", programName); + DISPLAY( "which would display help text and exit\n"); +#endif /* ENABLE_LZ4C_LEGACY_OPTIONS */ + return 0; +} + +static int badusage(void) +{ + DISPLAYLEVEL(1, "Incorrect parameters\n"); + if (displayLevel >= 1) usage(); + exit(1); +} + + +static void waitEnter(void) +{ + DISPLAY("Press enter to continue...\n"); + getchar(); +} + + +int main(int argc, char** argv) +{ + int i, + cLevel=0, + decode=0, + bench=0, + filenamesStart=2, + legacy_format=0, + forceStdout=0, + forceCompress=0, + main_pause=0; + char* input_filename=0; + char* output_filename=0; + char* dynNameSpace=0; + char nullOutput[] = NULL_OUTPUT; + char extension[] = LZ4_EXTENSION; + int blockSize; + + /* Init */ + programName = argv[0]; + LZ4IO_setOverwrite(0); + blockSize = LZ4IO_setBlockSizeID(LZ4_BLOCKSIZEID_DEFAULT); + + /* lz4cat behavior */ + if (!strcmp(programName, LZ4_CAT)) { decode=1; forceStdout=1; output_filename=stdoutmark; displayLevel=1; } + + /* command switches */ + for(i=1; i='0') && (*argument<='9')) + { + cLevel = 0; + while ((*argument >= '0') && (*argument <= '9')) + { + cLevel *= 10; + cLevel += *argument - '0'; + argument++; + } + argument--; + continue; + } + + switch(argument[0]) + { + /* Display help */ + case 'V': DISPLAY(WELCOME_MESSAGE); return 0; /* Version */ + case 'h': usage_advanced(); return 0; + case 'H': usage_advanced(); usage_longhelp(); return 0; + + /* Compression (default) */ + case 'z': forceCompress = 1; break; + + /* Use Legacy format (ex : Linux kernel compression) */ + case 'l': legacy_format = 1; blockSize = 8 MB; break; + + /* Decoding */ + case 'd': decode=1; break; + + /* Force stdout, even if stdout==console */ + case 'c': forceStdout=1; output_filename=stdoutmark; displayLevel=1; break; + + /* Test integrity */ + case 't': decode=1; LZ4IO_setOverwrite(1); output_filename=nulmark; break; + + /* Overwrite */ + case 'f': LZ4IO_setOverwrite(1); break; + + /* Verbose mode */ + case 'v': displayLevel=4; break; + + /* Quiet mode */ + case 'q': displayLevel--; break; + + /* keep source file (default anyway, so useless) (for xz/lzma compatibility) */ + case 'k': break; + + /* Modify Block Properties */ + case 'B': + while (argument[1]!=0) + { + int exitBlockProperties=0; + switch(argument[1]) + { + case '4': + case '5': + case '6': + case '7': + { + int B = argument[1] - '0'; + blockSize = LZ4IO_setBlockSizeID(B); + BMK_SetBlocksize(blockSize); + argument++; + break; + } + case 'D': LZ4IO_setBlockMode(LZ4IO_blockLinked); argument++; break; + case 'X': LZ4IO_setBlockChecksumMode(1); argument ++; break; /* currently disables */ + default : exitBlockProperties=1; + } + if (exitBlockProperties) break; + } + break; + + /* Modify Stream properties */ + case 'S': if (argument[1]=='x') { LZ4IO_setStreamChecksumMode(0); argument++; break; } else { badusage(); } + + /* Benchmark */ + case 'b': bench=1; break; + + /* Modify Nb Iterations (benchmark only) */ + case 'i': + if ((argument[1] >='1') && (argument[1] <='9')) + { + int iters = argument[1] - '0'; + BMK_SetNbIterations(iters); + argument++; + } + break; + + /* Pause at the end (hidden option) */ + case 'p': main_pause=1; BMK_SetPause(); break; + + /* Specific commands for customized versions */ + EXTENDED_ARGUMENTS; + + /* Unrecognised command */ + default : badusage(); + } + } + continue; + } + + /* first provided filename is input */ + if (!input_filename) { input_filename=argument; filenamesStart=i; continue; } + + /* second provided filename is output */ + if (!output_filename) + { + output_filename=argument; + if (!strcmp (output_filename, nullOutput)) output_filename = nulmark; + continue; + } + } + + DISPLAYLEVEL(3, WELCOME_MESSAGE); + if (!decode) DISPLAYLEVEL(4, "Blocks size : %i KB\n", blockSize>>10); + + /* No input filename ==> use stdin */ + if(!input_filename) { input_filename=stdinmark; } + + /* Check if input or output are defined as console; trigger an error in this case */ + if (!strcmp(input_filename, stdinmark) && IS_CONSOLE(stdin) ) badusage(); + + /* Check if benchmark is selected */ + if (bench) return BMK_benchFile(argv+filenamesStart, argc-filenamesStart, cLevel); + + /* No output filename ==> try to select one automatically (when possible) */ + while (!output_filename) + { + if (!IS_CONSOLE(stdout)) { output_filename=stdoutmark; break; } /* Default to stdout whenever possible (i.e. not a console) */ + if ((!decode) && !(forceCompress)) /* auto-determine compression or decompression, based on file extension */ + { + size_t l = strlen(input_filename); + if (!strcmp(input_filename+(l-4), LZ4_EXTENSION)) decode=1; + } + if (!decode) /* compression to file */ + { + size_t l = strlen(input_filename); + dynNameSpace = (char*)calloc(1,l+5); + output_filename = dynNameSpace; + strcpy(output_filename, input_filename); + strcpy(output_filename+l, LZ4_EXTENSION); + DISPLAYLEVEL(2, "Compressed filename will be : %s \n", output_filename); + break; + } + /* decompression to file (automatic name will work only if input filename has correct format extension) */ + { + size_t outl; + size_t inl = strlen(input_filename); + dynNameSpace = (char*)calloc(1,inl+1); + output_filename = dynNameSpace; + strcpy(output_filename, input_filename); + outl = inl; + if (inl>4) + while ((outl >= inl-4) && (input_filename[outl] == extension[outl-inl+4])) output_filename[outl--]=0; + if (outl != inl-5) { DISPLAYLEVEL(1, "Cannot determine an output filename\n"); badusage(); } + DISPLAYLEVEL(2, "Decoding file %s \n", output_filename); + } + } + + /* Check if output is defined as console; trigger an error in this case */ + if (!strcmp(output_filename,stdoutmark) && IS_CONSOLE(stdout) && !forceStdout) badusage(); + + /* No warning message in pure pipe mode (stdin + stdout) */ + if (!strcmp(input_filename, stdinmark) && !strcmp(output_filename,stdoutmark) && (displayLevel==2)) displayLevel=1; + + + /* IO Stream/File */ + LZ4IO_setNotificationLevel(displayLevel); + if (decode) DEFAULT_DECOMPRESSOR(input_filename, output_filename); + else + { + /* compression is default action */ + if (legacy_format) + { + DISPLAYLEVEL(3, "! Generating compressed LZ4 using Legacy format (deprecated) ! \n"); + LZ4IO_compressFilename_Legacy(input_filename, output_filename, cLevel); + } + else + { + DEFAULT_COMPRESSOR(input_filename, output_filename, cLevel); + } + } + + if (main_pause) waitEnter(); + free(dynNameSpace); + return 0; +} diff --git a/programs/lz4io.c b/programs/lz4io.c new file mode 100644 index 0000000000..fa1f0f9353 --- /dev/null +++ b/programs/lz4io.c @@ -0,0 +1,669 @@ +/* + LZ4io.c - LZ4 File/Stream Interface + Copyright (C) Yann Collet 2011-2014 + GPL v2 License + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + You can contact the author at : + - LZ4 source repository : http://code.google.com/p/lz4/ + - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c +*/ +/* + Note : this is stand-alone program. + It is not part of LZ4 compression library, it is a user code of the LZ4 library. + - The license of LZ4 library is BSD. + - The license of xxHash library is BSD. + - The license of this source file is GPLv2. +*/ + +/************************************** +* Compiler Options +***************************************/ +#ifdef _MSC_VER /* Visual Studio */ +# define _CRT_SECURE_NO_WARNINGS +# define _CRT_SECURE_NO_DEPRECATE /* VS2005 */ +# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ +#endif + +#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) +#ifdef __GNUC__ +# pragma GCC diagnostic ignored "-Wmissing-braces" /* GCC bug 53119 : doesn't accept { 0 } as initializer (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119) */ +# pragma GCC diagnostic ignored "-Wmissing-field-initializers" /* GCC bug 53119 : doesn't accept { 0 } as initializer (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119) */ +#endif + +#define _LARGE_FILES /* Large file support on 32-bits AIX */ +#define _FILE_OFFSET_BITS 64 /* Large file support on 32-bits unix */ +#define _POSIX_SOURCE 1 /* for fileno() within on unix */ + + +/**************************** +* Includes +*****************************/ +#include /* fprintf, fopen, fread, _fileno, stdin, stdout */ +#include /* malloc, free */ +#include /* strcmp, strlen */ +#include /* clock */ +#include "lz4io.h" +#include "lz4.h" /* still required for legacy format */ +#include "lz4hc.h" /* still required for legacy format */ +#include "lz4frame.h" + + +/**************************** +* OS-specific Includes +*****************************/ +#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) +# include /* _O_BINARY */ +# include /* _setmode, _isatty */ +# ifdef __MINGW32__ + int _fileno(FILE *stream); /* MINGW somehow forgets to include this windows declaration into */ +# endif +# define SET_BINARY_MODE(file) _setmode(_fileno(file), _O_BINARY) +# define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream)) +#else +# include /* isatty */ +# define SET_BINARY_MODE(file) +# define IS_CONSOLE(stdStream) isatty(fileno(stdStream)) +#endif + + +/**************************** +* Constants +*****************************/ +#define KB *(1 <<10) +#define MB *(1 <<20) +#define GB *(1U<<30) + +#define _1BIT 0x01 +#define _2BITS 0x03 +#define _3BITS 0x07 +#define _4BITS 0x0F +#define _8BITS 0xFF + +#define MAGICNUMBER_SIZE 4 +#define LZ4S_MAGICNUMBER 0x184D2204 +#define LZ4S_SKIPPABLE0 0x184D2A50 +#define LZ4S_SKIPPABLEMASK 0xFFFFFFF0 +#define LEGACY_MAGICNUMBER 0x184C2102 + +#define CACHELINE 64 +#define LEGACY_BLOCKSIZE (8 MB) +#define MIN_STREAM_BUFSIZE (192 KB) +#define LZ4S_BLOCKSIZEID_DEFAULT 7 +#define LZ4S_CHECKSUM_SEED 0 +#define LZ4S_EOS 0 +#define LZ4S_MAXHEADERSIZE (MAGICNUMBER_SIZE+2+8+4+1) + + +/************************************** +* Macros +***************************************/ +#define DISPLAY(...) fprintf(stderr, __VA_ARGS__) +#define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); } +#define DISPLAYUPDATE(l, ...) if (displayLevel>=l) { \ + if ((LZ4IO_GetMilliSpan(g_time) > refreshRate) || (displayLevel>=4)) \ + { g_time = clock(); DISPLAY(__VA_ARGS__); \ + if (displayLevel>=4) fflush(stdout); } } +static const unsigned refreshRate = 150; +static clock_t g_time = 0; + + +/************************************** +* Local Parameters +***************************************/ +static int displayLevel = 0; /* 0 : no display ; 1: errors ; 2 : + result + interaction + warnings ; 3 : + progression; 4 : + information */ +static int overwrite = 1; +static int globalBlockSizeId = LZ4S_BLOCKSIZEID_DEFAULT; +static int blockChecksum = 0; +static int streamChecksum = 1; +static int blockIndependence = 1; + +static const int minBlockSizeID = 4; +static const int maxBlockSizeID = 7; + + +/************************************** +* Exceptions +***************************************/ +#define DEBUG 0 +#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); +#define EXM_THROW(error, ...) \ +{ \ + DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \ + DISPLAYLEVEL(1, "Error %i : ", error); \ + DISPLAYLEVEL(1, __VA_ARGS__); \ + DISPLAYLEVEL(1, "\n"); \ + exit(error); \ +} + + +/************************************** +* Version modifiers +***************************************/ +#define EXTENDED_ARGUMENTS +#define EXTENDED_HELP +#define EXTENDED_FORMAT +#define DEFAULT_COMPRESSOR compress_file +#define DEFAULT_DECOMPRESSOR decodeLZ4S + + +/* ************************************************** */ +/* ****************** Parameters ******************** */ +/* ************************************************** */ + +/* Default setting : overwrite = 1; return : overwrite mode (0/1) */ +int LZ4IO_setOverwrite(int yes) +{ + overwrite = (yes!=0); + return overwrite; +} + +/* blockSizeID : valid values : 4-5-6-7 */ +int LZ4IO_setBlockSizeID(int bsid) +{ + static const int blockSizeTable[] = { 64 KB, 256 KB, 1 MB, 4 MB }; + if ((bsid < minBlockSizeID) || (bsid > maxBlockSizeID)) return -1; + globalBlockSizeId = bsid; + return blockSizeTable[globalBlockSizeId-minBlockSizeID]; +} + +int LZ4IO_setBlockMode(LZ4IO_blockMode_t blockMode) +{ + blockIndependence = (blockMode == LZ4IO_blockIndependent); + return blockIndependence; +} + +/* Default setting : no checksum */ +int LZ4IO_setBlockChecksumMode(int xxhash) +{ + blockChecksum = (xxhash != 0); + return blockChecksum; +} + +/* Default setting : checksum enabled */ +int LZ4IO_setStreamChecksumMode(int xxhash) +{ + streamChecksum = (xxhash != 0); + return streamChecksum; +} + +/* Default setting : 0 (no notification) */ +int LZ4IO_setNotificationLevel(int level) +{ + displayLevel = level; + return displayLevel; +} + +static unsigned LZ4IO_GetMilliSpan(clock_t nPrevious) +{ + clock_t nCurrent = clock(); + unsigned nSpan = (unsigned)(((nCurrent - nPrevious) * 1000) / CLOCKS_PER_SEC); + return nSpan; +} + + +/* ************************************************************************ */ +/* ********************** LZ4 File / Pipe compression ********************* */ +/* ************************************************************************ */ + +static int LZ4S_GetBlockSize_FromBlockId (int id) { return (1 << (8 + (2 * id))); } +static int LZ4S_isSkippableMagicNumber(unsigned int magic) { return (magic & LZ4S_SKIPPABLEMASK) == LZ4S_SKIPPABLE0; } + + +static int get_fileHandle(char* input_filename, char* output_filename, FILE** pfinput, FILE** pfoutput) +{ + + if (!strcmp (input_filename, stdinmark)) + { + DISPLAYLEVEL(4,"Using stdin for input\n"); + *pfinput = stdin; + SET_BINARY_MODE(stdin); + } + else + { + *pfinput = fopen(input_filename, "rb"); + } + + if (!strcmp (output_filename, stdoutmark)) + { + DISPLAYLEVEL(4,"Using stdout for output\n"); + *pfoutput = stdout; + SET_BINARY_MODE(stdout); + } + else + { + /* Check if destination file already exists */ + *pfoutput=0; + if (output_filename != nulmark) *pfoutput = fopen( output_filename, "rb" ); + if (*pfoutput!=0) + { + fclose(*pfoutput); + if (!overwrite) + { + char ch; + DISPLAYLEVEL(2, "Warning : %s already exists\n", output_filename); + DISPLAYLEVEL(2, "Overwrite ? (Y/N) : "); + if (displayLevel <= 1) EXM_THROW(11, "Operation aborted : %s already exists", output_filename); /* No interaction possible */ + ch = (char)getchar(); + if ((ch!='Y') && (ch!='y')) EXM_THROW(11, "Operation aborted : %s already exists", output_filename); + } + } + *pfoutput = fopen( output_filename, "wb" ); + } + + if ( *pfinput==0 ) EXM_THROW(12, "Pb opening %s", input_filename); + if ( *pfoutput==0) EXM_THROW(13, "Pb opening %s", output_filename); + + return 0; +} + + + + +/*************************************** + * Legacy Compression + * *************************************/ + +/* unoptimized version; solves endianess & alignment issues */ +static void LZ4IO_writeLE32 (void* p, unsigned value32) +{ + unsigned char* dstPtr = p; + dstPtr[0] = (unsigned char)value32; + dstPtr[1] = (unsigned char)(value32 >> 8); + dstPtr[2] = (unsigned char)(value32 >> 16); + dstPtr[3] = (unsigned char)(value32 >> 24); +} + +/* LZ4IO_compressFilename_Legacy : + * This function is intentionally "hidden" (not published in .h) + * It generates compressed streams using the old 'legacy' format */ +int LZ4IO_compressFilename_Legacy(char* input_filename, char* output_filename, int compressionlevel) +{ + int (*compressionFunction)(const char*, char*, int); + unsigned long long filesize = 0; + unsigned long long compressedfilesize = MAGICNUMBER_SIZE; + char* in_buff; + char* out_buff; + FILE* finput; + FILE* foutput; + clock_t start, end; + size_t sizeCheck; + + + /* Init */ + start = clock(); + if (compressionlevel < 3) compressionFunction = LZ4_compress; else compressionFunction = LZ4_compressHC; + + get_fileHandle(input_filename, output_filename, &finput, &foutput); + if ((displayLevel==2) && (compressionlevel==1)) displayLevel=3; + + /* Allocate Memory */ + in_buff = (char*)malloc(LEGACY_BLOCKSIZE); + out_buff = (char*)malloc(LZ4_compressBound(LEGACY_BLOCKSIZE)); + if (!in_buff || !out_buff) EXM_THROW(21, "Allocation error : not enough memory"); + + /* Write Archive Header */ + LZ4IO_writeLE32(out_buff, LEGACY_MAGICNUMBER); + sizeCheck = fwrite(out_buff, 1, MAGICNUMBER_SIZE, foutput); + if (sizeCheck!=MAGICNUMBER_SIZE) EXM_THROW(22, "Write error : cannot write header"); + + /* Main Loop */ + while (1) + { + unsigned int outSize; + /* Read Block */ + int inSize = (int) fread(in_buff, (size_t)1, (size_t)LEGACY_BLOCKSIZE, finput); + if( inSize<=0 ) break; + filesize += inSize; + + /* Compress Block */ + outSize = compressionFunction(in_buff, out_buff+4, inSize); + compressedfilesize += outSize+4; + DISPLAYUPDATE(3, "\rRead : %i MB ==> %.2f%% ", (int)(filesize>>20), (double)compressedfilesize/filesize*100); + + /* Write Block */ + LZ4IO_writeLE32(out_buff, outSize); + sizeCheck = fwrite(out_buff, 1, outSize+4, foutput); + if (sizeCheck!=(size_t)(outSize+4)) EXM_THROW(23, "Write error : cannot write compressed block"); + } + + /* Status */ + end = clock(); + DISPLAYLEVEL(2, "\r%79s\r", ""); + DISPLAYLEVEL(2,"Compressed %llu bytes into %llu bytes ==> %.2f%%\n", + (unsigned long long) filesize, (unsigned long long) compressedfilesize, (double)compressedfilesize/filesize*100); + { + double seconds = (double)(end - start)/CLOCKS_PER_SEC; + DISPLAYLEVEL(4,"Done in %.2f s ==> %.2f MB/s\n", seconds, (double)filesize / seconds / 1024 / 1024); + } + + /* Close & Free */ + free(in_buff); + free(out_buff); + fclose(finput); + fclose(foutput); + + return 0; +} + + +/*********************************************** + * Compression using Frame format + * ********************************************/ + +int LZ4IO_compressFilename(char* input_filename, char* output_filename, int compressionLevel) +{ + unsigned long long filesize = 0; + unsigned long long compressedfilesize = 0; + char* in_buff; + char* out_buff; + FILE* finput; + FILE* foutput; + clock_t start, end; + int blockSize; + size_t sizeCheck, headerSize, readSize, outBuffSize; + LZ4F_compressionContext_t ctx; + LZ4F_errorCode_t errorCode; + LZ4F_preferences_t prefs = {0}; + + + /* Init */ + start = clock(); + if ((displayLevel==2) && (compressionLevel>=3)) displayLevel=3; + errorCode = LZ4F_createCompressionContext(&ctx, LZ4F_VERSION); + if (LZ4F_isError(errorCode)) EXM_THROW(30, "Allocation error : can't create LZ4F context : %s", LZ4F_getErrorName(errorCode)); + get_fileHandle(input_filename, output_filename, &finput, &foutput); + blockSize = LZ4S_GetBlockSize_FromBlockId (globalBlockSizeId); + + /* Set compression parameters */ + prefs.autoFlush = 1; + prefs.compressionLevel = compressionLevel; + prefs.frameInfo.blockMode = blockIndependence; + prefs.frameInfo.blockSizeID = globalBlockSizeId; + prefs.frameInfo.contentChecksumFlag = streamChecksum; + + /* Allocate Memory */ + in_buff = (char*)malloc(blockSize); + outBuffSize = LZ4F_compressBound(blockSize, &prefs); + out_buff = (char*)malloc(outBuffSize); + if (!in_buff || !out_buff) EXM_THROW(31, "Allocation error : not enough memory"); + + /* Write Archive Header */ + headerSize = LZ4F_compressBegin(ctx, out_buff, outBuffSize, &prefs); + if (LZ4F_isError(headerSize)) EXM_THROW(32, "File header generation failed : %s", LZ4F_getErrorName(headerSize)); + sizeCheck = fwrite(out_buff, 1, headerSize, foutput); + if (sizeCheck!=headerSize) EXM_THROW(33, "Write error : cannot write header"); + compressedfilesize += headerSize; + + /* read first block */ + readSize = fread(in_buff, (size_t)1, (size_t)blockSize, finput); + filesize += readSize; + + /* Main Loop */ + while (readSize>0) + { + size_t outSize; + + /* Compress Block */ + outSize = LZ4F_compressUpdate(ctx, out_buff, outBuffSize, in_buff, readSize, NULL); + if (LZ4F_isError(outSize)) EXM_THROW(34, "Compression failed : %s", LZ4F_getErrorName(outSize)); + compressedfilesize += outSize; + DISPLAYUPDATE(3, "\rRead : %i MB ==> %.2f%% ", (int)(filesize>>20), (double)compressedfilesize/filesize*100); + + /* Write Block */ + sizeCheck = fwrite(out_buff, 1, outSize, foutput); + if (sizeCheck!=outSize) EXM_THROW(35, "Write error : cannot write compressed block"); + + /* Read next block */ + readSize = fread(in_buff, (size_t)1, (size_t)blockSize, finput); + filesize += readSize; + } + + /* End of Stream mark */ + headerSize = LZ4F_compressEnd(ctx, out_buff, outBuffSize, NULL); + if (LZ4F_isError(headerSize)) EXM_THROW(36, "End of file generation failed : %s", LZ4F_getErrorName(headerSize)); + + sizeCheck = fwrite(out_buff, 1, headerSize, foutput); + if (sizeCheck!=headerSize) EXM_THROW(37, "Write error : cannot write end of stream"); + compressedfilesize += headerSize; + + /* Close & Free */ + free(in_buff); + free(out_buff); + fclose(finput); + fclose(foutput); + errorCode = LZ4F_freeCompressionContext(ctx); + if (LZ4F_isError(errorCode)) EXM_THROW(38, "Error : can't free LZ4F context resource : %s", LZ4F_getErrorName(errorCode)); + + /* Final Status */ + end = clock(); + DISPLAYLEVEL(2, "\r%79s\r", ""); + DISPLAYLEVEL(2, "Compressed %llu bytes into %llu bytes ==> %.2f%%\n", + (unsigned long long) filesize, (unsigned long long) compressedfilesize, (double)compressedfilesize/filesize*100); + { + double seconds = (double)(end - start)/CLOCKS_PER_SEC; + DISPLAYLEVEL(4, "Done in %.2f s ==> %.2f MB/s\n", seconds, (double)filesize / seconds / 1024 / 1024); + } + + return 0; +} + + +/* ********************************************************************* */ +/* ********************** LZ4 File / Stream decoding ******************* */ +/* ********************************************************************* */ + +static unsigned LZ4IO_readLE32 (const void* s) +{ + const unsigned char* srcPtr = s; + unsigned value32 = srcPtr[0]; + value32 += (srcPtr[1]<<8); + value32 += (srcPtr[2]<<16); + value32 += (srcPtr[3]<<24); + return value32; +} + +static unsigned long long decodeLegacyStream(FILE* finput, FILE* foutput) +{ + unsigned long long filesize = 0; + char* in_buff; + char* out_buff; + + /* Allocate Memory */ + in_buff = (char*)malloc(LZ4_compressBound(LEGACY_BLOCKSIZE)); + out_buff = (char*)malloc(LEGACY_BLOCKSIZE); + if (!in_buff || !out_buff) EXM_THROW(51, "Allocation error : not enough memory"); + + /* Main Loop */ + while (1) + { + int decodeSize; + size_t sizeCheck; + unsigned int blockSize; + + /* Block Size */ + sizeCheck = fread(in_buff, 1, 4, finput); + if (sizeCheck==0) break; /* Nothing to read : file read is completed */ + blockSize = LZ4IO_readLE32(in_buff); /* Convert to Little Endian */ + if (blockSize > LZ4_COMPRESSBOUND(LEGACY_BLOCKSIZE)) + { /* Cannot read next block : maybe new stream ? */ + fseek(finput, -4, SEEK_CUR); + break; + } + + /* Read Block */ + sizeCheck = fread(in_buff, 1, blockSize, finput); + + /* Decode Block */ + decodeSize = LZ4_decompress_safe(in_buff, out_buff, blockSize, LEGACY_BLOCKSIZE); + if (decodeSize < 0) EXM_THROW(52, "Decoding Failed ! Corrupted input detected !"); + filesize += decodeSize; + + /* Write Block */ + sizeCheck = fwrite(out_buff, 1, decodeSize, foutput); + if (sizeCheck != (size_t)decodeSize) EXM_THROW(53, "Write error : cannot write decoded block into output\n"); + } + + /* Free */ + free(in_buff); + free(out_buff); + + return filesize; +} + + +static unsigned long long decodeLZ4S(FILE* finput, FILE* foutput) +{ + unsigned long long filesize = 0; + char* inBuff; + char* outBuff; +# define HEADERMAX 20 + char headerBuff[HEADERMAX]; + size_t sizeCheck, nextToRead, outBuffSize, inBuffSize; + LZ4F_decompressionContext_t ctx; + LZ4F_errorCode_t errorCode; + LZ4F_frameInfo_t frameInfo; + + /* init */ + errorCode = LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION); + if (LZ4F_isError(errorCode)) EXM_THROW(60, "Allocation error : can't create context : %s", LZ4F_getErrorName(errorCode)); + LZ4IO_writeLE32(headerBuff, LZ4S_MAGICNUMBER); /* regenerated here, as it was already read from finput */ + + /* Decode stream descriptor */ + outBuffSize = 0; inBuffSize = 0; sizeCheck = MAGICNUMBER_SIZE; + nextToRead = LZ4F_decompress(ctx, NULL, &outBuffSize, headerBuff, &sizeCheck, NULL); + if (LZ4F_isError(nextToRead)) EXM_THROW(61, "Decompression error : %s", LZ4F_getErrorName(nextToRead)); + if (nextToRead > HEADERMAX) EXM_THROW(62, "Header too large (%i>%i)", (int)nextToRead, HEADERMAX); + sizeCheck = fread(headerBuff, 1, nextToRead, finput); + if (sizeCheck!=nextToRead) EXM_THROW(63, "Read error "); + nextToRead = LZ4F_decompress(ctx, NULL, &outBuffSize, headerBuff, &sizeCheck, NULL); + errorCode = LZ4F_getFrameInfo(ctx, &frameInfo, NULL, &inBuffSize); + if (LZ4F_isError(errorCode)) EXM_THROW(64, "can't decode frame header : %s", LZ4F_getErrorName(errorCode)); + + /* Allocate Memory */ + outBuffSize = LZ4IO_setBlockSizeID(frameInfo.blockSizeID); + inBuffSize = outBuffSize + 4; + inBuff = (char*)malloc(inBuffSize); + outBuff = (char*)malloc(outBuffSize); + if (!inBuff || !outBuff) EXM_THROW(65, "Allocation error : not enough memory"); + + /* Main Loop */ + while (nextToRead != 0) + { + size_t decodedBytes = outBuffSize; + + /* Read Block */ + sizeCheck = fread(inBuff, 1, nextToRead, finput); + if (sizeCheck!=nextToRead) EXM_THROW(66, "Read error "); + + /* Decode Block */ + errorCode = LZ4F_decompress(ctx, outBuff, &decodedBytes, inBuff, &sizeCheck, NULL); + if (LZ4F_isError(errorCode)) EXM_THROW(67, "Decompression error : %s", LZ4F_getErrorName(errorCode)); + if (sizeCheck!=nextToRead) EXM_THROW(67, "Synchronization error"); + nextToRead = errorCode; + filesize += decodedBytes; + + /* Write Block */ + sizeCheck = fwrite(outBuff, 1, decodedBytes, foutput); + if (sizeCheck != decodedBytes) EXM_THROW(68, "Write error : cannot write decoded block\n"); + } + + /* Free */ + free(inBuff); + free(outBuff); + errorCode = LZ4F_freeDecompressionContext(ctx); + if (LZ4F_isError(errorCode)) EXM_THROW(69, "Error : can't free LZ4F context resource : %s", LZ4F_getErrorName(errorCode)); + + return filesize; +} + + +#define ENDOFSTREAM ((unsigned long long)-1) +static unsigned long long selectDecoder( FILE* finput, FILE* foutput) +{ + unsigned char U32store[MAGICNUMBER_SIZE]; + unsigned magicNumber, size; + int errorNb; + size_t nbReadBytes; + + /* Check Archive Header */ + nbReadBytes = fread(U32store, 1, MAGICNUMBER_SIZE, finput); + if (nbReadBytes==0) return ENDOFSTREAM; /* EOF */ + if (nbReadBytes != MAGICNUMBER_SIZE) EXM_THROW(40, "Unrecognized header : Magic Number unreadable"); + magicNumber = LZ4IO_readLE32(U32store); /* Little Endian format */ + if (LZ4S_isSkippableMagicNumber(magicNumber)) magicNumber = LZ4S_SKIPPABLE0; /* fold skippable magic numbers */ + + switch(magicNumber) + { + case LZ4S_MAGICNUMBER: + return DEFAULT_DECOMPRESSOR(finput, foutput); + case LEGACY_MAGICNUMBER: + DISPLAYLEVEL(4, "Detected : Legacy format \n"); + return decodeLegacyStream(finput, foutput); + case LZ4S_SKIPPABLE0: + DISPLAYLEVEL(4, "Skipping detected skippable area \n"); + nbReadBytes = fread(U32store, 1, 4, finput); + if (nbReadBytes != 4) EXM_THROW(42, "Stream error : skippable size unreadable"); + size = LZ4IO_readLE32(U32store); /* Little Endian format */ + errorNb = fseek(finput, size, SEEK_CUR); + if (errorNb != 0) EXM_THROW(43, "Stream error : cannot skip skippable area"); + return selectDecoder(finput, foutput); + EXTENDED_FORMAT; + default: + if (ftell(finput) == MAGICNUMBER_SIZE) EXM_THROW(44,"Unrecognized header : file cannot be decoded"); /* Wrong magic number at the beginning of 1st stream */ + DISPLAYLEVEL(2, "Stream followed by unrecognized data\n"); + return ENDOFSTREAM; + } +} + + +int LZ4IO_decompressFilename(char* input_filename, char* output_filename) +{ + unsigned long long filesize = 0, decodedSize=0; + FILE* finput; + FILE* foutput; + clock_t start, end; + + + /* Init */ + start = clock(); + get_fileHandle(input_filename, output_filename, &finput, &foutput); + + /* Loop over multiple streams */ + do + { + decodedSize = selectDecoder(finput, foutput); + if (decodedSize != ENDOFSTREAM) + filesize += decodedSize; + } while (decodedSize != ENDOFSTREAM); + + /* Final Status */ + end = clock(); + DISPLAYLEVEL(2, "\r%79s\r", ""); + DISPLAYLEVEL(2, "Successfully decoded %llu bytes \n", filesize); + { + double seconds = (double)(end - start)/CLOCKS_PER_SEC; + DISPLAYLEVEL(4, "Done in %.2f s ==> %.2f MB/s\n", seconds, (double)filesize / seconds / 1024 / 1024); + } + + /* Close */ + fclose(finput); + fclose(foutput); + + /* Error status = OK */ + return 0; +} + diff --git a/programs/lz4io.h b/programs/lz4io.h new file mode 100644 index 0000000000..7869a433ec --- /dev/null +++ b/programs/lz4io.h @@ -0,0 +1,77 @@ +/* + LZ4io.h - LZ4 File/Stream Interface + Copyright (C) Yann Collet 2011-2014 + GPL v2 License + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + You can contact the author at : + - LZ4 source repository : http://code.google.com/p/lz4/ + - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c +*/ +/* + Note : this is stand-alone program. + It is not part of LZ4 compression library, it is a user code of the LZ4 library. + - The license of LZ4 library is BSD. + - The license of xxHash library is BSD. + - The license of this source file is GPLv2. +*/ + + +/* ************************************************** */ +/* Special input/output values */ +/* ************************************************** */ +#define NULL_OUTPUT "null" +static char stdinmark[] = "stdin"; +static char stdoutmark[] = "stdout"; +#ifdef _WIN32 +static char nulmark[] = "nul"; +#else +static char nulmark[] = "/dev/null"; +#endif + + +/* ************************************************** */ +/* ****************** Functions ********************* */ +/* ************************************************** */ + +int LZ4IO_compressFilename (char* input_filename, char* output_filename, int compressionlevel); +int LZ4IO_decompressFilename(char* input_filename, char* output_filename); + + +/* ************************************************** */ +/* ****************** Parameters ******************** */ +/* ************************************************** */ + +/* Default setting : overwrite = 1; + return : overwrite mode (0/1) */ +int LZ4IO_setOverwrite(int yes); + +/* blockSizeID : valid values : 4-5-6-7 + return : -1 if error, blockSize if OK */ +int LZ4IO_setBlockSizeID(int blockSizeID); + +/* Default setting : independent blocks */ +typedef enum { LZ4IO_blockLinked=0, LZ4IO_blockIndependent} LZ4IO_blockMode_t; +int LZ4IO_setBlockMode(LZ4IO_blockMode_t blockMode); + +/* Default setting : no checksum */ +int LZ4IO_setBlockChecksumMode(int xxhash); + +/* Default setting : checksum enabled */ +int LZ4IO_setStreamChecksumMode(int xxhash); + +/* Default setting : 0 (no notification) */ +int LZ4IO_setNotificationLevel(int level); From ba42334d3663b646ef21dc5c60ca55cf4a407690 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Wed, 4 Feb 2015 07:19:57 -0800 Subject: [PATCH 09/33] Add lz4 Conflicts: Builds/VisualStudio2013/RippleD.vcxproj Builds/VisualStudio2013/RippleD.vcxproj.filters --- Builds/VisualStudio2013/RippleD.vcxproj | 12 ++++++++++ .../VisualStudio2013/RippleD.vcxproj.filters | 21 ++++++++++++++++ SConstruct | 1 + src/ripple/unity/lz4.c | 24 +++++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 src/ripple/unity/lz4.c diff --git a/Builds/VisualStudio2013/RippleD.vcxproj b/Builds/VisualStudio2013/RippleD.vcxproj index 62dd8d9585..09959bad90 100644 --- a/Builds/VisualStudio2013/RippleD.vcxproj +++ b/Builds/VisualStudio2013/RippleD.vcxproj @@ -1553,6 +1553,16 @@ True + + True + + + + + True + + + True @@ -3392,6 +3402,8 @@ + + diff --git a/Builds/VisualStudio2013/RippleD.vcxproj.filters b/Builds/VisualStudio2013/RippleD.vcxproj.filters index 4336f12c44..c7b252f70d 100644 --- a/Builds/VisualStudio2013/RippleD.vcxproj.filters +++ b/Builds/VisualStudio2013/RippleD.vcxproj.filters @@ -265,6 +265,12 @@ {A3DA589F-2CB8-7260-66DC-5975D80AB46B} + + {B211F8F1-22D2-47BA-C39E-F9846A844D11} + + + {4F65E5BD-7EC4-113A-4603-B4625F16BC18} + {C535C933-C404-7C0F-2AB9-059F92DE0A80} @@ -2427,6 +2433,18 @@ leveldb\util + + lz4\lib + + + lz4\lib + + + lz4\lib + + + lz4\lib + protobuf\src\google\protobuf\compiler @@ -4641,6 +4659,9 @@ ripple\unity + + ripple\unity + ripple\unity diff --git a/SConstruct b/SConstruct index fdbb7714c3..0aeca1a42d 100644 --- a/SConstruct +++ b/SConstruct @@ -667,6 +667,7 @@ for tu_style in ['classic', 'unity']: object_builder.add_source_files( 'src/beast/beast/unity/hash_unity.cpp', 'src/ripple/unity/beast.cpp', + 'src/ripple/unity/lz4.c', 'src/ripple/unity/protobuf.cpp', 'src/ripple/unity/ripple.proto.cpp', 'src/ripple/unity/resource.cpp', diff --git a/src/ripple/unity/lz4.c b/src/ripple/unity/lz4.c new file mode 100644 index 0000000000..68431daf98 --- /dev/null +++ b/src/ripple/unity/lz4.c @@ -0,0 +1,24 @@ +//------------------------------------------------------------------------------ +/* + This file is part of rippled: https://github.com/ripple/rippled + Copyright (c) 2015 Ripple Labs Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#include +#ifdef _MSC_VER +#include +#endif + From a33d0d4fb615861a538d292bc82ceb78e13f529e Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sat, 7 Feb 2015 13:57:15 -0800 Subject: [PATCH 10/33] Add general delimiter split() to rfc2616 --- src/beast/beast/http/rfc2616.h | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/beast/beast/http/rfc2616.h b/src/beast/beast/http/rfc2616.h index e446542c79..351034c645 100644 --- a/src/beast/beast/http/rfc2616.h +++ b/src/beast/beast/http/rfc2616.h @@ -165,9 +165,11 @@ trim (std::string const& s) http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2 */ template >> + class Result = std::vector< + std::basic_string>, + class Char> Result -split_commas(FwdIt first, FwdIt last) +split(FwdIt first, FwdIt last, Char delim) { Result result; using string = typename Result::value_type; @@ -206,7 +208,7 @@ split_commas(FwdIt first, FwdIt last) e.clear(); } } - else if (*iter == ',') + else if (*iter == delim) { e = trim_right (e); if (! e.empty()) @@ -235,6 +237,15 @@ split_commas(FwdIt first, FwdIt last) return result; } +template >> +Result +split_commas(FwdIt first, FwdIt last) +{ + return split(first, last, ','); +} + template > Result split_commas(std::string const& s) From 0b82b5a0d615b49130c08e0a4eae645a7d29e944 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Tue, 3 Feb 2015 07:46:24 -0800 Subject: [PATCH 11/33] NuDB: Performance improvements (RIPD-793,796): This introduces changes in nudb to improve speed, reduce database size, and enhance correctness. The most significant change is to store hashes rather than entire keys in the key file. The output of the hash function is reduced to 48 bits, and stored directly in buckets. The API is also modified to introduce a Codec parameter allowing for compression and decompression to be supported in the implementation itself rather than callers. THe data file no longer contains a salt, as the salt is applicable only to the key and log files. This allows a data file to have multiple key files with different salt values. To distinguish physical files belonging to the same logical database, a new field UID is introduced. The UID is a 64-bit random value generated once on creation and stored in all three files. Buckets are zero filled to the end of each block, this is a security measure to prevent unintended contents of memory getting stored to disk. NuDB offers the varint integer type, this is identical to the varint described by Google. * Add varint * Add Codec template argument * Add "api" convenience traits * Store hash in buckets * istream can throw short read errors * Support std::uint8_t format in streams * Make file classes part of the public interface * Remove buffers pessimization, replace with buffer * Consolidate creation utility functions to the same header * Zero fill unused areas of buckets on disk * More coverage and improvements to the recover test * Fix file read/write to loop until all bytes processed * Add verify_fast, faster verify for large databases The database version number is incremented to 2; older databases can no longer be opened and should be deleted. --- Builds/VisualStudio2013/RippleD.vcxproj | 23 +- .../VisualStudio2013/RippleD.vcxproj.filters | 30 +- src/beast/beast/nudb.h | 4 +- src/beast/beast/nudb/README.md | 40 +- src/beast/beast/nudb/api.h | 109 ++++ src/beast/beast/nudb/{error.h => common.h} | 76 ++- src/beast/beast/nudb/create.h | 87 ++- src/beast/beast/nudb/detail/arena.h | 1 - src/beast/beast/nudb/detail/bucket.h | 336 +++++------ src/beast/beast/nudb/detail/buffer.h | 99 +++ src/beast/beast/nudb/detail/buffers.h | 147 ----- src/beast/beast/nudb/detail/bulkio.h | 10 +- src/beast/beast/nudb/detail/cache.h | 7 +- src/beast/beast/nudb/detail/config.h | 75 --- src/beast/beast/nudb/detail/field.h | 101 ++-- src/beast/beast/nudb/detail/format.h | 273 ++++++--- src/beast/beast/nudb/detail/gentex.h | 1 - src/beast/beast/nudb/detail/pool.h | 1 - src/beast/beast/nudb/detail/stream.h | 146 ++--- src/beast/beast/nudb/detail/varint.h | 155 +++++ src/beast/beast/nudb/file.h | 5 +- .../beast/nudb/{mode.h => identity_codec.h} | 45 +- src/beast/beast/nudb/nudb.cpp | 3 +- .../beast/nudb/{detail => }/posix_file.h | 51 +- src/beast/beast/nudb/recover.h | 22 +- src/beast/beast/nudb/store.h | 564 ++++++++---------- src/beast/beast/nudb/tests/callgrind_test.cpp | 11 +- src/beast/beast/nudb/tests/common.h | 56 +- src/beast/beast/nudb/tests/fail_file.h | 2 +- src/beast/beast/nudb/tests/recover_test.cpp | 83 ++- src/beast/beast/nudb/tests/store_test.cpp | 18 +- src/beast/beast/nudb/tests/varint_test.cpp | 73 +++ src/beast/beast/nudb/tests/verify_test.cpp | 235 +++++++- src/beast/beast/nudb/verify.h | 331 ++++++++-- src/beast/beast/nudb/visit.h | 21 +- .../beast/nudb/{detail => }/win32_file.h | 81 +-- src/ripple/nodestore/backend/NuDBFactory.cpp | 82 +-- 37 files changed, 2102 insertions(+), 1302 deletions(-) create mode 100644 src/beast/beast/nudb/api.h rename src/beast/beast/nudb/{error.h => common.h} (67%) create mode 100644 src/beast/beast/nudb/detail/buffer.h delete mode 100644 src/beast/beast/nudb/detail/buffers.h delete mode 100644 src/beast/beast/nudb/detail/config.h create mode 100644 src/beast/beast/nudb/detail/varint.h rename src/beast/beast/nudb/{mode.h => identity_codec.h} (59%) rename src/beast/beast/nudb/{detail => }/posix_file.h (88%) create mode 100644 src/beast/beast/nudb/tests/varint_test.cpp rename src/beast/beast/nudb/{detail => }/win32_file.h (85%) diff --git a/Builds/VisualStudio2013/RippleD.vcxproj b/Builds/VisualStudio2013/RippleD.vcxproj index 09959bad90..3f37db1f04 100644 --- a/Builds/VisualStudio2013/RippleD.vcxproj +++ b/Builds/VisualStudio2013/RippleD.vcxproj @@ -809,20 +809,22 @@ + + + + - + - - @@ -831,21 +833,19 @@ - - - - - + - + True + + @@ -865,6 +865,9 @@ True + + True + True @@ -872,6 +875,8 @@ + + diff --git a/Builds/VisualStudio2013/RippleD.vcxproj.filters b/Builds/VisualStudio2013/RippleD.vcxproj.filters index c7b252f70d..2b7bac64a6 100644 --- a/Builds/VisualStudio2013/RippleD.vcxproj.filters +++ b/Builds/VisualStudio2013/RippleD.vcxproj.filters @@ -1479,6 +1479,12 @@ beast + + beast\nudb + + + beast\nudb + beast\nudb @@ -1488,7 +1494,7 @@ beast\nudb\detail - + beast\nudb\detail @@ -1497,9 +1503,6 @@ beast\nudb\detail - - beast\nudb\detail - beast\nudb\detail @@ -1512,27 +1515,24 @@ beast\nudb\detail - - beast\nudb\detail - beast\nudb\detail - + beast\nudb\detail - - beast\nudb - beast\nudb - + beast\nudb beast\nudb + + beast\nudb + beast\nudb @@ -1557,6 +1557,9 @@ beast\nudb\tests + + beast\nudb\tests + beast\nudb\tests @@ -1566,6 +1569,9 @@ beast\nudb + + beast\nudb + beast\random diff --git a/src/beast/beast/nudb.h b/src/beast/beast/nudb.h index 40b1d49e79..e413c336f5 100644 --- a/src/beast/beast/nudb.h +++ b/src/beast/beast/nudb.h @@ -20,10 +20,10 @@ #ifndef BEAST_NUDB_H_INCLUDED #define BEAST_NUDB_H_INCLUDED +#include #include -#include +#include #include -#include #include #include #include diff --git a/src/beast/beast/nudb/README.md b/src/beast/beast/nudb/README.md index 6cb2747bd7..777a98b69f 100644 --- a/src/beast/beast/nudb/README.md +++ b/src/beast/beast/nudb/README.md @@ -167,16 +167,23 @@ fixed-length Bucket Records. char[8] Type The characters "nudb.key" uint16 Version Holds the version number + uint64 UID Unique ID generated on creation uint64 Appnum Application defined constant + uint16 KeySize Key size in bytes + uint64 Salt A random seed uint64 Pepper The salt hashed - uint16 KeySize Key size in bytes uint16 BlockSize Size of a file block in bytes + uint16 LoadFactor Target fraction in 65536ths - uint8[64] Reserved Zeroes + + uint8[56] Reserved Zeroes uint8[] Reserved Zero-pad to block size -The Type identifies the file as belonging to nudb. Salt is +The Type identifies the file as belonging to nudb. The UID is +generated randomly when the database is created, and this value +is stored in the data and log files as well. The UID is used +to determine if files belong to the same database. Salt is generated when the database is created and helps prevent complexity attacks; the salt is prepended to the key material when computing a hash, or used to initialize the state of @@ -197,7 +204,8 @@ bucket, and defines the size of a bucket record. The load factor is the target fraction of bucket occupancy. None of the information in the key file header or the data file -header may be changed after the database is created. +header may be changed after the database is created, including +the Appnum. #### Bucket Record (fixed-length) @@ -209,7 +217,7 @@ header may be changed after the database is created. uint48 Offset Offset in data file of the data uint48 Size The size of the value in bytes - uint8[KeySize] Key The key + uint48 Hash The hash of the key ### Data File @@ -220,14 +228,15 @@ variable-length Value Records and Spill Records. char[8] Type The characters "nudb.dat" uint16 Version Holds the version number + uint64 UID Unique ID generated on creation uint64 Appnum Application defined constant - uint64 Salt A random seed uint16 KeySize Key size in bytes + uint8[64] Reserved Zeroes -Salt contains the same value as the salt in the corresponding -key file. This is placed in the data file so that key and value -files belonging to the same database can be identified. +UID contains the same value as the salt in the corresponding key +file. This is placed in the data file so that key and value files +belonging to the same database can be identified. #### Data Record (variable-length) @@ -244,15 +253,24 @@ files belonging to the same database can be identified. ### Log File The Log file contains the Header followed by zero or more fixed size +log records. Each log record contains a snapshot of a bucket. When a +database is not closed cleanly, the recovery process applies the log +records to the key file, overwriting data that may be only partially +updated with known good information. After the log records are applied, +the data and key files are truncated to the last known good size. -#### Header (44 bytes) +#### Header (62 bytes) char[8] Type The characters "nudb.log" uint16 Version Holds the version number + uint64 UID Unique ID generated on creation uint64 Appnum Application defined constant + uint16 KeySize Key size in bytes + uint64 Salt A random seed. uint64 Pepper The salt hashed - uint16 KeySize Key size in bytes + uint16 BlockSize Size of a file block in bytes + uint64 KeyFileSize Size of key file. uint64 DataFileSize Size of data file. diff --git a/src/beast/beast/nudb/api.h b/src/beast/beast/nudb/api.h new file mode 100644 index 0000000000..65ad74919e --- /dev/null +++ b/src/beast/beast/nudb/api.h @@ -0,0 +1,109 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2014, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef BEAST_NUDB_API_H_INCLUDED +#define BEAST_NUDB_API_H_INCLUDED + +#include +#include +#include +#include +#include +#include + +namespace beast { +namespace nudb { + +// Convenience for consolidating template arguments +// +template < + class Hasher, + class Codec, + class File = native_file, + std::size_t BufferSize = 16 * 1024 * 1024 +> +struct api +{ + using hash_type = Hasher; + using codec_type = Codec; + using file_type = File; + using store = nudb::store; + + static std::size_t const buffer_size = BufferSize; + + template + static + bool + create ( + path_type const& dat_path, + path_type const& key_path, + path_type const& log_path, + std::uint64_t appnum, + std::uint64_t salt, + std::size_t key_size, + std::size_t block_size, + float load_factor, + Args&&... args) + { + return nudb::create( + dat_path, key_path, log_path, + appnum, salt, key_size, block_size, + load_factor, args...); + } + + template + static + bool + recover ( + path_type const& dat_path, + path_type const& key_path, + path_type const& log_path, + Args&&... args) + { + return nudb::recover( + dat_path, key_path, log_path, BufferSize, + args...); + } + + static + verify_info + verify ( + path_type const& dat_path, + path_type const& key_path) + { + return nudb::verify( + dat_path, key_path, BufferSize); + } + + template + static + bool + visit( + path_type const& path, + Function&& f) + { + return nudb::visit( + path, BufferSize, f); + } +}; + +} // nudb +} // beast + +#endif diff --git a/src/beast/beast/nudb/error.h b/src/beast/beast/nudb/common.h similarity index 67% rename from src/beast/beast/nudb/error.h rename to src/beast/beast/nudb/common.h index a6f1915425..12a1345874 100644 --- a/src/beast/beast/nudb/error.h +++ b/src/beast/beast/nudb/common.h @@ -17,32 +17,48 @@ */ //============================================================================== -#ifndef BEAST_NUDB_ERROR_H_INCLUDED -#define BEAST_NUDB_ERROR_H_INCLUDED +#ifndef BEAST_NUDB_COMMON_H_INCLUDED +#define BEAST_NUDB_COMMON_H_INCLUDED -#include -#include #include #include namespace beast { namespace nudb { +// Commonly used types + +enum class file_mode +{ + scan, // read sequential + read, // read random + append, // read random, write append + write // read random, write random +}; + +using path_type = std::string; + // All exceptions thrown by nudb are derived -// from std::exception except for fail_error +// from std::runtime_error except for fail_error + +/** Thrown when a codec fails, e.g. corrupt data. */ +struct codec_error : std::runtime_error +{ + template + explicit + codec_error (String const& s) + : runtime_error(s) + { + } +}; /** Base class for all errors thrown by file classes. */ struct file_error : std::runtime_error { + template explicit - file_error (char const* s) - : std::runtime_error(s) - { - } - - explicit - file_error (std::string const& s) - : std::runtime_error(s) + file_error (String const& s) + : runtime_error(s) { } }; @@ -67,21 +83,24 @@ struct file_short_write_error : file_error } }; +/** Thrown when end of istream reached while reading. */ +struct short_read_error : std::runtime_error +{ + short_read_error() + : std::runtime_error( + "nudb: short read") + { + } +}; + /** Base class for all exceptions thrown by store. */ class store_error : public std::runtime_error { public: + template explicit - store_error (char const* m) - : std::runtime_error( - std::string("nudb: ") + m) - { - } - - explicit - store_error (std::string const& m) - : std::runtime_error( - std::string("nudb: ") + m) + store_error (String const& s) + : runtime_error(s) { } }; @@ -90,15 +109,10 @@ public: class store_corrupt_error : public store_error { public: + template explicit - store_corrupt_error (char const* m) - : store_error (m) - { - } - - explicit - store_corrupt_error (std::string const& m) - : store_error (m) + store_corrupt_error (String const& s) + : store_error(s) { } }; diff --git a/src/beast/beast/nudb/create.h b/src/beast/beast/nudb/create.h index c7ccd37eb0..30f3a700e9 100644 --- a/src/beast/beast/nudb/create.h +++ b/src/beast/beast/nudb/create.h @@ -22,16 +22,49 @@ #include #include -#include #include #include #include +#include #include #include namespace beast { namespace nudb { +namespace detail { + +template +std::uint64_t +make_uid() +{ + std::random_device rng; + std::mt19937_64 gen {rng()}; + std::uniform_int_distribution dist; + return dist(gen); +} + +} + +/** Generate a random salt. */ +template +std::uint64_t +make_salt() +{ + std::random_device rng; + std::mt19937_64 gen {rng()}; + std::uniform_int_distribution dist; + return dist(gen); +} + +/** Returns the best guess at the volume's block size. */ +inline +std::size_t +block_size (path_type const& /*path*/) +{ + return 4096; +} + /** Create a new database. Preconditions: The files must not exist @@ -40,7 +73,12 @@ namespace nudb { @param args Arguments passed to File constructors @return `false` if any file could not be created. */ -template +template < + class Hasher, + class Codec, + class File, + class... Args +> bool create ( path_type const& dat_path, @@ -50,10 +88,10 @@ create ( std::uint64_t salt, std::size_t key_size, std::size_t block_size, - float load_factor) + float load_factor, + Args&&... args) { using namespace detail; - using File = native_file; if (key_size < 1) throw std::domain_error( "invalid key size"); @@ -67,43 +105,41 @@ create ( throw std::domain_error( "nudb: load factor too large"); auto const capacity = - bucket_capacity(key_size, block_size); + bucket_capacity(block_size); if (capacity < 1) throw std::domain_error( "nudb: block size too small"); - File df; - File kf; - File lf; - for(;;) + File df(args...); + File kf(args...); + File lf(args...); + if (df.create( + file_mode::append, dat_path)) { - if (df.create( - file_mode::append, dat_path)) + if (kf.create ( + file_mode::append, key_path)) { - if (kf.create ( - file_mode::append, key_path)) - { - if (lf.create( - file_mode::append, log_path)) - break; - File::erase (dat_path); - } - File::erase (key_path); + if (lf.create( + file_mode::append, log_path)) + goto success; + File::erase (dat_path); } - return false; + File::erase (key_path); } - + return false; +success: dat_file_header dh; dh.version = currentVersion; + dh.uid = make_uid(); dh.appnum = appnum; - dh.salt = salt; dh.key_size = key_size; key_file_header kh; kh.version = currentVersion; + kh.uid = dh.uid; kh.appnum = appnum; + kh.key_size = key_size; kh.salt = salt; kh.pepper = pepper(salt); - kh.key_size = key_size; kh.block_size = block_size; // VFALCO Should it be 65536? // How do we set the min? @@ -113,8 +149,7 @@ create ( write (kf, kh); buffer buf(block_size); std::memset(buf.get(), 0, block_size); - bucket b (key_size, block_size, - buf.get(), empty); + bucket b (block_size, buf.get(), empty); b.write (kf, block_size); // VFALCO Leave log file empty? df.sync(); diff --git a/src/beast/beast/nudb/detail/arena.h b/src/beast/beast/nudb/detail/arena.h index 832a2de669..b6a3f6348e 100644 --- a/src/beast/beast/nudb/detail/arena.h +++ b/src/beast/beast/nudb/detail/arena.h @@ -20,7 +20,6 @@ #ifndef BEAST_NUDB_ARENA_H_INCLUDED #define BEAST_NUDB_ARENA_H_INCLUDED -#include #include #include #include diff --git a/src/beast/beast/nudb/detail/bucket.h b/src/beast/beast/nudb/detail/bucket.h index 1b49b85b24..812a1886b6 100644 --- a/src/beast/beast/nudb/detail/bucket.h +++ b/src/beast/beast/nudb/detail/bucket.h @@ -20,12 +20,11 @@ #ifndef BEAST_NUDB_BUCKET_H_INCLUDED #define BEAST_NUDB_BUCKET_H_INCLUDED -#include +#include #include -#include #include #include -#include +#include #include #include @@ -33,21 +32,7 @@ namespace beast { namespace nudb { namespace detail { -// Key, hash, and bucket calculations: - -// Returns the hash of a key given the salt -// -template -inline -typename Hasher::result_type -hash (void const* key, - std::size_t key_size, std::size_t salt) -{ - Hasher h (salt); - h.append (key, key_size); - return static_cast< - typename Hasher::result_type>(h); -} +// bucket calculations: // Returns bucket index given hash, buckets, and modulus // @@ -62,30 +47,6 @@ bucket_index (std::size_t h, return n; } -// Returns the bucket index of a key -// -template -inline -std::size_t -bucket_index (void const* key, std::size_t key_size, - std::size_t salt, std::size_t buckets, - std::size_t modulus) -{ - return bucket_index (hash - (key, key_size, salt), buckets, modulus); -} - -// Returns the bucket index of a key -// given the key file header -template -inline -std::size_t -bucket_index (void const* key, key_file_header const& kh) -{ - return bucket_index(key, kh.key_size, - kh.salt, kh.buckets, kh.modulus); -} - //------------------------------------------------------------------------------ // Tag for constructing empty buckets @@ -97,9 +58,8 @@ template class bucket_t { private: - std::size_t key_size_; // Size of key in bytes std::size_t block_size_; // Size of a key file block - std::size_t count_; // Current key count + std::size_t size_; // Current key count std::size_t spill_; // Offset of next spill record or 0 std::uint8_t* p_; // Pointer to the bucket blob @@ -108,23 +68,15 @@ public: { std::size_t offset; std::size_t size; - void const* key; + std::size_t hash; }; bucket_t (bucket_t const&) = default; bucket_t& operator= (bucket_t const&) = default; - bucket_t (std::size_t key_size, - std::size_t block_size, void* p); + bucket_t (std::size_t block_size, void* p); - bucket_t (std::size_t key_size, - std::size_t block_size, void* p, empty_t); - - std::size_t - key_size() const - { - return key_size_; - } + bucket_t (std::size_t block_size, void* p, empty_t); std::size_t block_size() const @@ -135,44 +87,46 @@ public: std::size_t compact_size() const { - return detail::compact_size( - key_size_, count_); + return detail::bucket_size(size_); } bool empty() const { - return count_ == 0; + return size_ == 0; } bool full() const { - return count_ >= detail::bucket_capacity( - key_size_, block_size_); + return size_ >= + detail::bucket_capacity(block_size_); } std::size_t size() const { - return count_; + return size_; } // Returns offset of next spill record or 0 + // std::size_t spill() const { return spill_; } - // Clear contents of the bucket - void - clear(); - // Set offset of next spill record + // void spill (std::size_t offset); + // Clear contents of the bucket + // + void + clear(); + // Returns the record for a key // entry without bounds checking. // @@ -185,12 +139,15 @@ public: return at(i); } - std::pair - find (void const* key) const; + // Returns index of entry with prefix + // equal to or greater than the given prefix. + // + std::size_t + lower_bound (std::size_t h) const; void insert (std::size_t offset, - std::size_t size, void const* key); + std::size_t size, std::size_t h); // Erase an element by index // @@ -227,45 +184,31 @@ private: // Update size and spill in the blob void update(); - - std::pair - lower_bound (void const* key) const; }; //------------------------------------------------------------------------------ template -bucket_t<_>::bucket_t (std::size_t key_size, +bucket_t<_>::bucket_t ( std::size_t block_size, void* p) - : key_size_ (key_size) - , block_size_ (block_size) + : block_size_ (block_size) , p_ (reinterpret_cast(p)) { // Bucket Record istream is(p_, block_size); - detail::read(is, count_); // Count + detail::read(is, size_); // Count detail::read(is, spill_); // Spill } template -bucket_t<_>::bucket_t (std::size_t key_size, +bucket_t<_>::bucket_t ( std::size_t block_size, void* p, empty_t) - : key_size_ (key_size) - , block_size_ (block_size) - , count_ (0) + : block_size_ (block_size) + , size_ (0) , spill_ (0) , p_ (reinterpret_cast(p)) { - update(); -} - -template -void -bucket_t<_>::clear() -{ - count_ = 0; - spill_ = 0; - update(); + clear(); } template @@ -276,6 +219,15 @@ bucket_t<_>::spill (std::size_t offset) update(); } +template +void +bucket_t<_>::clear() +{ + size_ = 0; + spill_ = 0; + std::memset(p_, 0, block_size_); +} + template auto bucket_t<_>::at (std::size_t i) const -> @@ -286,7 +238,7 @@ bucket_t<_>::at (std::size_t i) const -> std::size_t const w = field::size + // Offset field::size + // Size - key_size_; // Key + field::size; // Prefix // Bucket Record detail::istream is(p_ + field::size + // Count @@ -297,54 +249,80 @@ bucket_t<_>::at (std::size_t i) const -> is, result.offset); // Offset detail::read( is, result.size); // Size - result.key = is.data(key_size_); // Key + detail::read( + is, result.hash); // Hash return result; } template -auto -bucket_t<_>::find (void const* key) const -> - std::pair +std::size_t +bucket_t<_>::lower_bound ( + std::size_t h) const { - std::pair result; - std::size_t i; - std::tie(i, result.second) = lower_bound(key); - if (result.second) - result.first = at(i); - return result; + // Bucket Entry + auto const w = + field::size + // Offset + field::size + // Size + field::size; // Hash + // Bucket Record + auto const p = p_ + + field::size + // Count + field::size + // Spill + // Bucket Entry + field::size + // Offset + field::size; // Size + std::size_t step; + std::size_t first = 0; + std::size_t count = size_; + while (count > 0) + { + step = count / 2; + auto const i = first + step; + std::size_t h1; + readp(p + i * w, h1); + if (h1 < h) + { + first = i + 1; + count -= step + 1; + } + else + { + count = step; + } + } + return first; } template void bucket_t<_>::insert (std::size_t offset, - std::size_t size, void const* key) + std::size_t size, std::size_t h) { - bool found; - std::size_t i; - std::tie(i, found) = lower_bound(key); - (void)found; - assert(! found); + std::size_t i = lower_bound(h); // Bucket Record auto const p = p_ + - field::size + // Count - field::size; // Spill + field< + std::uint16_t>::size + // Count + field::size; // Spill // Bucket Entry std::size_t const w = - field::size + // Offset - field::size + // Size - key_size_; // Key + field::size + // Offset + field::size + // Size + field::size; // Hash std::memmove ( p + (i + 1) * w, p + i * w, - (count_ - i) * w); - count_++; + (size_ - i) * w); + size_++; update(); // Bucket Entry ostream os (p + i * w, w); - detail::write(os, offset); // Offset - detail::write(os, size); // Size - std::memcpy (os.data(key_size_), - key, key_size_); // Key + detail::write( + os, offset); // Offset + detail::write( + os, size); // Size + detail::write( + os, h); // Prefix } template @@ -353,18 +331,20 @@ bucket_t<_>::erase (std::size_t i) { // Bucket Record auto const p = p_ + - field::size + // Count - field::size; // Spill + field< + std::uint16_t>::size + // Count + field::size; // Spill auto const w = - field::size + // Offset - field::size + // Size - key_size_; // Key - --count_; - if (i != count_) + field::size + // Offset + field::size + // Size + field::size; // Hash + --size_; + if (i < size_) std::memmove( - p + i * w, - p + (i + 1) * w, - (count_ - i) * w); + p + i * w, + p + (i + 1) * w, + (size_ - i) * w); + std::memset(p + size_ * w, 0, w); update(); } @@ -374,17 +354,15 @@ void bucket_t<_>::read (File& f, std::size_t offset) { auto const cap = bucket_capacity ( - key_size_, block_size_); + block_size_); // Excludes padding to block size - f.read (offset, p_, bucket_size( - key_size_, bucket_capacity( - key_size_, block_size_))); + f.read (offset, p_, bucket_size(cap)); istream is(p_, block_size_); detail::read< - std::uint16_t>(is, count_); // Count + std::uint16_t>(is, size_); // Count detail::read< uint48_t>(is, spill_); // Spill - if (count_ > cap) + if (size_ > cap) throw store_corrupt_error( "bad bucket size"); } @@ -399,19 +377,21 @@ bucket_t<_>::read (bulk_reader& r) detail::field::size + detail::field::size); detail::read< - std::uint16_t>(is, count_); // Count - detail::read(is, spill_); // Spill + std::uint16_t>(is, size_); // Count + detail::read( + is, spill_); // Spill update(); // Excludes empty bucket entries - auto const w = count_ * ( - field::size + // Offset - field::size + // Size - key_size_); // Key + auto const w = size_ * ( + field::size + // Offset + field::size + // Size + field::size); // Hash is = r.prepare (w); std::memcpy(p_ + - field::size + // Count - field::size, // Spill - is.data(w), w); // Entries + field< + std::uint16_t>::size + // Count + field::size, // Spill + is.data(w), w); // Entries } template @@ -447,56 +427,40 @@ bucket_t<_>::update() // Bucket Record ostream os(p_, block_size_); detail::write< - std::uint16_t>(os, count_); // Count + std::uint16_t>(os, size_); // Count detail::write< uint48_t>(os, spill_); // Spill } -// bool is true if key matches index -template -std::pair -bucket_t<_>::lower_bound ( - void const* key) const -{ - // Bucket Entry - auto const w = - field::size + // Offset - field::size + // Size - key_size_; // Key - // Bucket Record - auto const p = p_ + - field::size + // Count - field::size + // Spill - // Bucket Entry - field::size + // Offset - field::size; // Size - std::size_t step; - std::size_t first = 0; - std::size_t count = count_; - while (count > 0) - { - step = count / 2; - auto const i = first + step; - auto const c = std::memcmp ( - p + i * w, key, key_size_); - if (c < 0) - { - first = i + 1; - count -= step + 1; - } - else if (c > 0) - { - count = step; - } - else - { - return std::make_pair (i, true); - } - } - return std::make_pair (first, false); -} using bucket = bucket_t<>; +// Spill bucket if full. +// The bucket is cleared after it spills. +// +template +void +maybe_spill(bucket& b, bulk_writer& w) +{ + if (b.full()) + { + // Spill Record + auto const offset = w.offset(); + auto os = w.prepare( + field::size + // Zero + field::size + // Size + b.compact_size()); + write (os, 0); // Zero + write ( + os, b.compact_size()); // Size + auto const spill = + offset + os.size(); + b.write (os); // Bucket + // Update bucket + b.clear(); + b.spill (spill); + } +} + } // detail } // nudb } // beast diff --git a/src/beast/beast/nudb/detail/buffer.h b/src/beast/beast/nudb/detail/buffer.h new file mode 100644 index 0000000000..2c71912ece --- /dev/null +++ b/src/beast/beast/nudb/detail/buffer.h @@ -0,0 +1,99 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2014, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef BEAST_NUDB_DETAIL_BUFFER_H_INCLUDED +#define BEAST_NUDB_DETAIL_BUFFER_H_INCLUDED + +#include +#include +#include + +namespace beast { +namespace nudb { +namespace detail { + +// Simple growable memory buffer +class buffer +{ +private: + std::size_t size_ = 0; + std::unique_ptr buf_; + +public: + ~buffer() = default; + buffer() = default; + buffer (buffer const&) = delete; + buffer& operator= (buffer const&) = delete; + + explicit + buffer (std::size_t n) + : size_ (n) + , buf_ (new std::uint8_t[n]) + { + } + + buffer (buffer&& other) + : size_ (other.size_) + , buf_ (std::move(other.buf_)) + { + other.size_ = 0; + } + + buffer& operator= (buffer&& other) + { + size_ = other.size_; + buf_ = std::move(other.buf_); + other.size_ = 0; + return *this; + } + + std::size_t + size() const + { + return size_; + } + + std::uint8_t* + get() const + { + return buf_.get(); + } + + void + reserve (std::size_t n) + { + if (size_ < n) + buf_.reset (new std::uint8_t[n]); + size_ = n; + } + + // BufferFactory + void* + operator() (std::size_t n) + { + reserve(n); + return buf_.get(); + } +}; + +} // detail +} // nudb +} // beast + +#endif diff --git a/src/beast/beast/nudb/detail/buffers.h b/src/beast/beast/nudb/detail/buffers.h deleted file mode 100644 index 40cdb5af01..0000000000 --- a/src/beast/beast/nudb/detail/buffers.h +++ /dev/null @@ -1,147 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of Beast: https://github.com/vinniefalco/Beast - Copyright 2014, Vinnie Falco - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ -//============================================================================== - -#ifndef BEAST_NUDB_BUFFERS_H_INCLUDED -#define BEAST_NUDB_BUFFERS_H_INCLUDED - -#include -#include -#include -#include - -namespace beast { -namespace nudb { -namespace detail { - -// Thread safe pool of temp buffers, -// to avoid needless calls to malloc. -template -class buffers_t -{ -private: - struct element - { - element* next; - }; - - std::size_t const block_size_; - std::mutex m_; - element* h_ = nullptr; - -public: - class value_type - { - private: - buffers_t& b_; - element* e_; - - public: - value_type (value_type const&) = delete; - value_type& operator= (value_type const&) = delete; - - explicit - value_type (buffers_t& b) - : b_ (b) - , e_ (b.acquire()) - { - } - - ~value_type() - { - b_.release(e_); - } - - std::uint8_t* - get() const - { - return const_cast ( - reinterpret_cast< - std::uint8_t const*>(e_ + 1)); - } - }; - - explicit - buffers_t (std::size_t block_size); - - ~buffers_t(); - -private: - element* - acquire(); - - void - release (element* e); -}; - -template -buffers_t<_>::buffers_t (std::size_t block_size) - : block_size_ (block_size) - , h_ (nullptr) -{ -} - -template -buffers_t<_>::~buffers_t() -{ - for (element* e = h_; e;) - { - element* const next = e->next; - e->~element(); - delete[] reinterpret_cast< - std::uint8_t*>(e); - e = next; - } -} - -template -auto -buffers_t<_>::acquire() -> - element* -{ - { - std::lock_guard m(m_); - element* e = h_; - if (e) - { - h_ = e->next; - return e; - } - } - return ::new( - new std::uint8_t[ - sizeof(element) + block_size_] - ) element; -} - -template -void -buffers_t<_>::release (element* e) -{ - std::lock_guard m(m_); - e->next = h_; - h_ = e; -} - -using buffers = buffers_t<>; - -} // detail -} // nudb -} // beast - -#endif diff --git a/src/beast/beast/nudb/detail/bulkio.h b/src/beast/beast/nudb/detail/bulkio.h index e7ea76a445..4a29f5e1ba 100644 --- a/src/beast/beast/nudb/detail/bulkio.h +++ b/src/beast/beast/nudb/detail/bulkio.h @@ -20,7 +20,7 @@ #ifndef BEAST_NUDB_BULKIO_H_INCLUDED #define BEAST_NUDB_BULKIO_H_INCLUDED -#include +#include #include #include #include @@ -45,10 +45,16 @@ public: bulk_reader (File& f, std::size_t offset, std::size_t last, std::size_t buffer_size); + std::size_t + offset() const + { + return offset_ - avail_; + } + bool eof() const { - return offset_ - avail_ == last_; + return offset() >= last_; } istream diff --git a/src/beast/beast/nudb/detail/cache.h b/src/beast/beast/nudb/detail/cache.h index 7c2a397bda..80072dd672 100644 --- a/src/beast/beast/nudb/detail/cache.h +++ b/src/beast/beast/nudb/detail/cache.h @@ -22,7 +22,6 @@ #include #include -#include #include #include #include @@ -77,8 +76,8 @@ private: operator() (argument_type const& e) const { return std::make_pair(e.first, - bucket (cache_->key_size_, - cache_->block_size_, e.second)); + bucket (cache_->block_size_, + e.second)); } }; @@ -209,7 +208,7 @@ cache_t<_>::create (std::size_t n) { auto const p = arena_.alloc (block_size_); map_.emplace (n, p); - return bucket (key_size_, block_size_, + return bucket (block_size_, p, detail::empty); } diff --git a/src/beast/beast/nudb/detail/config.h b/src/beast/beast/nudb/detail/config.h deleted file mode 100644 index 8d8de463f0..0000000000 --- a/src/beast/beast/nudb/detail/config.h +++ /dev/null @@ -1,75 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of Beast: https://github.com/vinniefalco/Beast - Copyright 2014, Vinnie Falco - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ -//============================================================================== - -#ifndef BEAST_NUDB_CONFIG_H_INCLUDED -#define BEAST_NUDB_CONFIG_H_INCLUDED - -#include - -// Compiles out domain checks -#ifndef BEAST_NUDB_NO_DOMAIN_CHECK -# ifdef NDEBUG -# define BEAST_NUDB_NO_DOMAIN_CHECK 1 -# else -# define BEAST_NUDB_NO_DOMAIN_CHECK 0 -# endif -#endif - -namespace beast { -namespace nudb { - -// xxhasher is the fastest and the best choice -// when keys are already uniformly distributed -using default_hash = xxhasher; - -namespace detail { - -// Returns the closest power of 2 not less than x -template -std::size_t -ceil_pow2 (unsigned long long x) -{ - static const unsigned long long t[6] = { - 0xFFFFFFFF00000000ull, - 0x00000000FFFF0000ull, - 0x000000000000FF00ull, - 0x00000000000000F0ull, - 0x000000000000000Cull, - 0x0000000000000002ull - }; - - int y = (((x & (x - 1)) == 0) ? 0 : 1); - int j = 32; - int i; - - for(i = 0; i < 6; i++) { - int k = (((x & t[i]) == 0) ? 0 : j); - y += k; - x >>= k; - j >>= 1; - } - - return std::size_t(1)< #include #include // for BEAST_CONSTEXPR #include @@ -85,16 +84,26 @@ struct field static std::size_t BEAST_CONSTEXPR max = 0xffffffffffffffff; }; -// read field from istream +// read field from memory + +template ::value>* = nullptr> +void +readp (void const* v, U& u) +{ + std::uint8_t const* p = + reinterpret_cast(v); + u = *p; +} template ::value>* = nullptr> void -read (istream& is, U& u) +readp (void const* v, U& u) { - T t; std::uint8_t const* p = - is.data(field::size); + reinterpret_cast(v); + T t; t = T(*p++)<< 8; t = T(*p ) | t; u = t; @@ -103,25 +112,25 @@ read (istream& is, U& u) template ::value>* = nullptr> void -read (istream& is, U& u) +readp (void const* v, U& u) { - T t; std::uint8_t const* p = - is.data(field::size); - t = (T(*p++)<<16) | t; - t = (T(*p++)<< 8) | t; - t = T(*p ) | t; + reinterpret_cast(v); + std::uint32_t t; + t = std::uint32_t(*p++)<<16; + t = (std::uint32_t(*p++)<< 8) | t; + t = std::uint32_t(*p ) | t; u = t; } template ::value>* = nullptr> void -read (istream& is, U& u) +readp (void const* v, U& u) { - T t; std::uint8_t const* p = - is.data(field::size); + reinterpret_cast(v); + T t; t = T(*p++)<<24; t = (T(*p++)<<16) | t; t = (T(*p++)<< 8) | t; @@ -132,11 +141,11 @@ read (istream& is, U& u) template ::value>* = nullptr> void -read (istream& is, U& u) +readp (void const* v, U& u) { - std::uint64_t t; std::uint8_t const* p = - is.data(field::size); + reinterpret_cast(v); + std::uint64_t t; t = (std::uint64_t(*p++)<<40); t = (std::uint64_t(*p++)<<32) | t; t = (std::uint64_t(*p++)<<24) | t; @@ -149,11 +158,11 @@ read (istream& is, U& u) template ::value>* = nullptr> void -read (istream& is, U& u) +readp (void const* v, U& u) { - T t; std::uint8_t const* p = - is.data(field::size); + reinterpret_cast(v); + T t; t = T(*p++)<<56; t = (T(*p++)<<48) | t; t = (T(*p++)<<40) | t; @@ -165,18 +174,32 @@ read (istream& is, U& u) u = t; } +// read field from istream + +template +void +read (istream& is, U& u) +{ + readp(is.data(field::size), u); +} + // write field to ostream +template ::value>* = nullptr> +void +write (ostream& os, U const& u) +{ + std::uint8_t* p = + os.data(field::size); + *p = u; +} + template ::value>* = nullptr> void write (ostream& os, U const& u) { -#ifndef BEAST_NUDB_NO_DOMAIN_CHECK - if (u > field::max) - throw std::logic_error( - "nudb: field max exceeded"); -#endif T t = u; std::uint8_t* p = os.data(field::size); @@ -184,16 +207,11 @@ write (ostream& os, U const& u) *p = t &0xff; } -template ::value>* = nullptr> void write (ostream& os, U const& u) { -#ifndef BEAST_NUDB_NO_DOMAIN_CHECK - if (u > field::max) - throw std::logic_error( - "nudb: field max exceeded"); -#endif T t = u; std::uint8_t* p = os.data(field::size); @@ -202,16 +220,11 @@ write (ostream& os, U const& u) *p = t &0xff; } -template ::value>* = nullptr> void write (ostream& os, U const& u) { -#ifndef BEAST_NUDB_NO_DOMAIN_CHECK - if (u > field::max) - throw std::logic_error( - "nudb: field max exceeded"); -#endif T t = u; std::uint8_t* p = os.data(field::size); @@ -221,16 +234,11 @@ write (ostream& os, U const& u) *p = t &0xff; } -template ::value>* = nullptr> void write (ostream& os, U const& u) { -#ifndef BEAST_NUDB_NO_DOMAIN_CHECK - if (u > field::max) - throw std::logic_error( - "nudb: field max exceeded"); -#endif std::uint64_t const t = u; std::uint8_t* p = os.data(field::size); @@ -242,16 +250,11 @@ write (ostream& os, U const& u) *p = t &0xff; } -template ::value>* = nullptr> void write (ostream& os, U const& u) { -#ifndef BEAST_NUDB_NO_DOMAIN_CHECK - if (u > field::max) - throw std::logic_error( - "nudb: field max exceeded"); -#endif T t = u; std::uint8_t* p = os.data(field::size); diff --git a/src/beast/beast/nudb/detail/format.h b/src/beast/beast/nudb/detail/format.h index 8e41e3b100..91132bc9da 100644 --- a/src/beast/beast/nudb/detail/format.h +++ b/src/beast/beast/nudb/detail/format.h @@ -20,7 +20,8 @@ #ifndef BEAST_NUDB_FORMAT_H_INCLUDED #define BEAST_NUDB_FORMAT_H_INCLUDED -#include +#include +#include #include #include #include // for BEAST_CONSTEXPR @@ -38,22 +39,23 @@ namespace detail { // Format of the nudb files: -static std::size_t BEAST_CONSTEXPR currentVersion = 1; +static std::size_t BEAST_CONSTEXPR currentVersion = 2; struct dat_file_header { static std::size_t BEAST_CONSTEXPR size = 8 + // Type 2 + // Version + 8 + // UID 8 + // Appnum - 8 + // Salt 2 + // KeySize + 64; // (Reserved) char type[8]; std::size_t version; + std::uint64_t uid; std::uint64_t appnum; - std::uint64_t salt; std::size_t key_size; }; @@ -62,20 +64,25 @@ struct key_file_header static std::size_t BEAST_CONSTEXPR size = 8 + // Type 2 + // Version + 8 + // UID 8 + // Appnum + 2 + // KeySize + 8 + // Salt 8 + // Pepper - 2 + // KeySize 2 + // BlockSize 2 + // LoadFactor - 64; // (Reserved) + + 56; // (Reserved) char type[8]; std::size_t version; + std::uint64_t uid; std::uint64_t appnum; + std::size_t key_size; + std::uint64_t salt; std::uint64_t pepper; - std::size_t key_size; std::size_t block_size; std::size_t load_factor; @@ -91,23 +98,65 @@ struct log_file_header static std::size_t BEAST_CONSTEXPR size = 8 + // Type 2 + // Version + 8 + // UID 8 + // Appnum + 2 + // KeySize + 8 + // Salt 8 + // Pepper - 2 + // KeySize + 2 + // BlockSize + 8 + // KeyFileSize 8; // DataFileSize char type[8]; std::size_t version; + std::uint64_t uid; std::uint64_t appnum; + std::size_t key_size; std::uint64_t salt; std::uint64_t pepper; - std::size_t key_size; + std::size_t block_size; std::size_t key_file_size; std::size_t dat_file_size; }; +// Type used to store hashes in buckets. +// This can be smaller than the output +// of the hash function. +// +using hash_t = uint48_t; + +static_assert(field::size <= + sizeof(std::size_t), ""); + +template +std::size_t +make_hash (std::size_t h); + +template<> +inline +std::size_t +make_hash(std::size_t h) +{ + return (h>>16)&0xffffffffffff; +} + +// Returns the hash of a key given the salt. +// Note: The hash is expressed in hash_t units +// +template +inline +std::size_t +hash (void const* key, + std::size_t key_size, std::size_t salt) +{ + Hasher h (salt); + h.append (key, key_size); + return make_hash(static_cast< + typename Hasher::result_type>(h)); +} + // Computes pepper from salt // template @@ -124,8 +173,7 @@ pepper (std::size_t salt) // template std::size_t -bucket_size (std::size_t key_size, - std::size_t capacity) +bucket_size (std::size_t capacity) { // Bucket Record return @@ -134,33 +182,14 @@ bucket_size (std::size_t key_size, capacity * ( field::size + // Offset field::size + // Size - key_size); // Key + field::size); // Hash } -// Returns the size of a bucket large enough to -// hold size keys of length key_size. -// -inline -std::size_t -compact_size(std::size_t key_size, - std::size_t size) -{ - // Bucket Record - return - field::size + // Size - field::size + // Spill - size * ( - field::size + // Offset - field::size + // Size - key_size); // Key -} - -// Returns: number of keys that fit in a bucket +// Returns the number of entries that fit in a bucket // template std::size_t -bucket_capacity (std::size_t key_size, - std::size_t block_size) +bucket_capacity (std::size_t block_size) { // Bucket Record auto const size = @@ -169,17 +198,18 @@ bucket_capacity (std::size_t key_size, auto const entry_size = field::size + // Offset field::size + // Size - key_size; // Key + field::size; // Hash if (block_size < key_file_header::size || block_size < size) return 0; return (block_size - size) / entry_size; } -// returns the number of bytes occupied by a value record +// Returns the number of bytes occupied by a value record inline std::size_t -data_size (std::size_t size, std::size_t key_size) +value_size (std::size_t size, + std::size_t key_size) { // Data Record return @@ -188,6 +218,34 @@ data_size (std::size_t size, std::size_t key_size) size; // Data } +// Returns the closest power of 2 not less than x +template +std::size_t +ceil_pow2 (unsigned long long x) +{ + static const unsigned long long t[6] = { + 0xFFFFFFFF00000000ull, + 0x00000000FFFF0000ull, + 0x000000000000FF00ull, + 0x00000000000000F0ull, + 0x000000000000000Cull, + 0x0000000000000002ull + }; + + int y = (((x & (x - 1)) == 0) ? 0 : 1); + int j = 32; + int i; + + for(i = 0; i < 6; i++) { + int k = (((x & t[i]) == 0) ? 0 : j); + y += k; + x >>= k; + j >>= 1; + } + + return std::size_t(1)<(is, dh.version); + read(is, dh.uid); read(is, dh.appnum); - read(is, dh.salt); read(is, dh.key_size); - std::array zero; - read (is, zero.data(), zero.size()); + std::array reserved; + read (is, + reserved.data(), reserved.size()); } // Read data file header from file @@ -231,12 +290,13 @@ write (ostream& os, dat_file_header const& dh) { write (os, "nudb.dat", 8); write(os, dh.version); + write(os, dh.uid); write(os, dh.appnum); - write(os, dh.salt); write(os, dh.key_size); - std::array zero; - zero.fill(0); - write (os, zero.data(), zero.size()); + std::array reserved; + reserved.fill(0); + write (os, + reserved.data(), reserved.size()); } // Write data file header to file @@ -259,25 +319,26 @@ read (istream& is, std::size_t file_size, { read(is, kh.type, sizeof(kh.type)); read(is, kh.version); + read(is, kh.uid); read(is, kh.appnum); + read(is, kh.key_size); read(is, kh.salt); read(is, kh.pepper); - read(is, kh.key_size); read(is, kh.block_size); read(is, kh.load_factor); - std::array zero; - read (is, zero.data(), zero.size()); + std::array reserved; + read (is, + reserved.data(), reserved.size()); // VFALCO These need to be checked to handle // when the file size is too small - kh.capacity = bucket_capacity( - kh.key_size, kh.block_size); - kh.bucket_size = bucket_size( - kh.key_size, kh.capacity); + kh.capacity = bucket_capacity(kh.block_size); + kh.bucket_size = bucket_size(kh.capacity); if (file_size > kh.block_size) { // VFALCO This should be handled elsewhere. - // we shouldn't put the computed fields in this header. + // we shouldn't put the computed fields + // in this header. if (kh.block_size > 0) kh.buckets = (file_size - kh.bucket_size) / kh.block_size; @@ -319,15 +380,17 @@ write (ostream& os, key_file_header const& kh) { write (os, "nudb.key", 8); write(os, kh.version); + write(os, kh.uid); write(os, kh.appnum); + write(os, kh.key_size); write(os, kh.salt); write(os, kh.pepper); - write(os, kh.key_size); write(os, kh.block_size); write(os, kh.load_factor); - std::array zero; - zero.fill (0); - write (os, zero.data(), zero.size()); + std::array reserved; + reserved.fill (0); + write (os, + reserved.data(), reserved.size()); } // Write key file header to file @@ -335,12 +398,12 @@ template void write (File& f, key_file_header const& kh) { - buffer buf; - buf.reserve (kh.block_size); if (kh.block_size < key_file_header::size) throw std::logic_error( "nudb: block size too small"); - std::fill(buf.get(), buf.get() + buf.size(), 0); + buffer buf(kh.block_size); + std::fill(buf.get(), + buf.get() + buf.size(), 0); ostream os (buf.get(), buf.size()); write (os, kh); f.write (0, buf.get(), buf.size()); @@ -353,10 +416,12 @@ read (istream& is, log_file_header& lh) { read (is, lh.type, sizeof(lh.type)); read(is, lh.version); + read(is, lh.uid); read(is, lh.appnum); + read(is, lh.key_size); read(is, lh.salt); read(is, lh.pepper); - read(is, lh.key_size); + read(is, lh.block_size); read(is, lh.key_file_size); read(is, lh.dat_file_size); } @@ -381,10 +446,12 @@ write (ostream& os, log_file_header const& lh) { write (os, "nudb.log", 8); write(os, lh.version); + write(os, lh.uid); write(os, lh.appnum); + write(os, lh.key_size); write(os, lh.salt); write(os, lh.pepper); - write(os, lh.key_size); + write(os, lh.block_size); write(os, lh.key_file_size); write(os, lh.dat_file_size); } @@ -401,34 +468,6 @@ write (File& f, log_file_header const& lh) f.write (0, buf.data(), buf.size()); } -template -void -verify (key_file_header const& kh) -{ - std::string const type (kh.type, 8); - if (type != "nudb.key") - throw store_corrupt_error ( - "bad type in key file"); - if (kh.version != currentVersion) - throw store_corrupt_error ( - "bad version in key file"); - if (kh.pepper != pepper(kh.salt)) - throw store_corrupt_error( - "wrong hash function for key file"); - if (kh.key_size < 1) - throw store_corrupt_error ( - "bad key size in key file"); - if (kh.load_factor < 1) - throw store_corrupt_error ( - "bad load factor in key file"); - if (kh.capacity < 1) - throw store_corrupt_error ( - "bad capacity in key file"); - if (kh.buckets < 1) - throw store_corrupt_error ( - "bad key file size"); -} - template void verify (dat_file_header const& dh) @@ -445,6 +484,34 @@ verify (dat_file_header const& dh) "bad key size in data file"); } +template +void +verify (key_file_header const& kh) +{ + std::string const type (kh.type, 8); + if (type != "nudb.key") + throw store_corrupt_error ( + "bad type in key file"); + if (kh.version != currentVersion) + throw store_corrupt_error ( + "bad version in key file"); + if (kh.key_size < 1) + throw store_corrupt_error ( + "bad key size in key file"); + if (kh.pepper != pepper(kh.salt)) + throw store_corrupt_error( + "wrong hash function for key file"); + if (kh.load_factor < 1) + throw store_corrupt_error ( + "bad load factor in key file"); + if (kh.capacity < 1) + throw store_corrupt_error ( + "bad capacity in key file"); + if (kh.buckets < 1) + throw store_corrupt_error ( + "bad key file size"); +} + template void verify (log_file_header const& lh) @@ -470,17 +537,16 @@ void verify (dat_file_header const& dh, key_file_header const& kh) { - verify (dh); verify (kh); - if (kh.salt != dh.salt) + if (kh.uid != dh.uid) throw store_corrupt_error( - "salt mismatch"); - if (kh.key_size != dh.key_size) - throw store_corrupt_error( - "key size mismatch"); + "uid mismatch"); if (kh.appnum != dh.appnum) throw store_corrupt_error( "appnum mismatch"); + if (kh.key_size != dh.key_size) + throw store_corrupt_error( + "key size mismatch"); } template @@ -489,15 +555,24 @@ verify (key_file_header const& kh, log_file_header const& lh) { verify(lh); - if (kh.salt != lh.salt) + if (kh.uid != lh.uid) throw store_corrupt_error ( - "salt mismatch in log file"); + "uid mismatch in log file"); + if (kh.appnum != lh.appnum) + throw store_corrupt_error( + "appnum mismatch in log file"); if (kh.key_size != lh.key_size) throw store_corrupt_error ( "key size mismatch in log file"); - if (kh.appnum != lh.appnum) - throw store_corrupt_error( - "appnum mismatch"); + if (kh.salt != lh.salt) + throw store_corrupt_error ( + "salt mismatch in log file"); + if (kh.pepper != lh.pepper) + throw store_corrupt_error ( + "pepper mismatch in log file"); + if (kh.block_size != lh.block_size) + throw store_corrupt_error ( + "block size mismatch in log file"); } } // detail diff --git a/src/beast/beast/nudb/detail/gentex.h b/src/beast/beast/nudb/detail/gentex.h index bbd21d49d7..5e4d9cda6a 100644 --- a/src/beast/beast/nudb/detail/gentex.h +++ b/src/beast/beast/nudb/detail/gentex.h @@ -20,7 +20,6 @@ #ifndef BEAST_NUDB_GENTEX_H_INCLUDED #define BEAST_NUDB_GENTEX_H_INCLUDED -#include #include #include #include diff --git a/src/beast/beast/nudb/detail/pool.h b/src/beast/beast/nudb/detail/pool.h index b4a80fdaa1..0aeab93b44 100644 --- a/src/beast/beast/nudb/detail/pool.h +++ b/src/beast/beast/nudb/detail/pool.h @@ -22,7 +22,6 @@ #include #include -#include #include #include #include diff --git a/src/beast/beast/nudb/detail/stream.h b/src/beast/beast/nudb/detail/stream.h index 93862ee37d..eb6e930a98 100644 --- a/src/beast/beast/nudb/detail/stream.h +++ b/src/beast/beast/nudb/detail/stream.h @@ -20,8 +20,7 @@ #ifndef BEAST_NUDB_STREAM_H_INCLUDED #define BEAST_NUDB_STREAM_H_INCLUDED -#include -#include +#include #include #include #include @@ -32,114 +31,54 @@ namespace beast { namespace nudb { namespace detail { -// Simple growable memory buffer -class buffer -{ -private: - std::size_t size_ = 0; - std::unique_ptr buf_; - -public: - buffer() = default; - buffer (buffer const&) = delete; - buffer& operator= (buffer const&) = delete; - - explicit - buffer (std::size_t n) - : size_ (n) - , buf_ (new std::uint8_t[n]) - { - } - - buffer (buffer&& other) - : size_ (other.size_) - , buf_ (std::move(other.buf_)) - { - other.size_ = 0; - } - - buffer& operator= (buffer&& other) - { - size_ = other.size_; - buf_ = std::move(other.buf_); - other.size_ = 0; - return *this; - } - - std::size_t - size() const - { - return size_; - } - - std::uint8_t* - get() const - { - return buf_.get(); - } - - void - reserve (std::size_t n) - { - if (size_ < n) - buf_.reset (new std::uint8_t[n]); - size_ = n; - } -}; - -//------------------------------------------------------------------------------ - // Input stream from bytes template class istream_t { private: std::uint8_t const* buf_; -#if ! BEAST_NUDB_NO_DOMAIN_CHECK - std::size_t bytes_; -#endif + std::size_t size_ = 0; public: istream_t (istream_t const&) = default; istream_t& operator= (istream_t const&) = default; - istream_t (void const* data, std::size_t - #if ! BEAST_NUDB_NO_DOMAIN_CHECK - bytes - #endif - ) + istream_t (void const* data, std::size_t size) : buf_(reinterpret_cast< std::uint8_t const*>(data)) - #if ! BEAST_NUDB_NO_DOMAIN_CHECK - , bytes_(bytes) - #endif + , size_(size) { } template istream_t (std::array const& a) : buf_ (a.data()) - #if ! BEAST_NUDB_NO_DOMAIN_CHECK - , bytes_ (a.size()) - #endif + , size_ (a.size()) { } std::uint8_t const* - data (std::size_t bytes) + data (std::size_t bytes); + + std::uint8_t const* + operator()(std::size_t bytes) { - #if ! BEAST_NUDB_NO_DOMAIN_CHECK - if (bytes > bytes_) - throw std::logic_error( - "nudb: istream"); - bytes_ -= bytes; - #endif - auto const data = buf_; - buf_ = buf_ + bytes; - return data; + return data(bytes); } }; +template +std::uint8_t const* +istream_t<_>::data (std::size_t bytes) +{ + if (size_ < bytes) + throw short_read_error(); + auto const data = buf_; + buf_ = buf_ + bytes; + size_ -= bytes; + return data; +} + using istream = istream_t<>; //------------------------------------------------------------------------------ @@ -151,32 +90,19 @@ class ostream_t private: std::uint8_t* buf_; std::size_t size_ = 0; -#if ! BEAST_NUDB_NO_DOMAIN_CHECK - std::size_t bytes_; -#endif public: ostream_t (ostream_t const&) = default; ostream_t& operator= (ostream_t const&) = default; - ostream_t (void* data, std::size_t - #if ! BEAST_NUDB_NO_DOMAIN_CHECK - bytes - #endif - ) + ostream_t (void* data, std::size_t) : buf_ (reinterpret_cast(data)) - #if ! BEAST_NUDB_NO_DOMAIN_CHECK - , bytes_ (bytes) - #endif { } template ostream_t (std::array& a) : buf_ (a.data()) - #if ! BEAST_NUDB_NO_DOMAIN_CHECK - , bytes_ (a.size()) - #endif { } @@ -188,21 +114,25 @@ public: } std::uint8_t* - data (std::size_t bytes) + data (std::size_t bytes); + + std::uint8_t* + operator()(std::size_t bytes) { -#if ! BEAST_NUDB_NO_DOMAIN_CHECK - if (bytes > bytes_) - throw std::logic_error( - "nudb: ostream"); - bytes_ -= bytes; -#endif - auto const data = buf_; - buf_ = buf_ + bytes; - size_ += bytes; - return data; + return data(bytes); } }; +template +std::uint8_t* +ostream_t<_>::data (std::size_t bytes) +{ + auto const data = buf_; + buf_ = buf_ + bytes; + size_ += bytes; + return data; +} + using ostream = ostream_t<>; //------------------------------------------------------------------------------ diff --git a/src/beast/beast/nudb/detail/varint.h b/src/beast/beast/nudb/detail/varint.h new file mode 100644 index 0000000000..98b116da9b --- /dev/null +++ b/src/beast/beast/nudb/detail/varint.h @@ -0,0 +1,155 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2014, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef BEAST_NUDB_VARINT_H_INCLUDED +#define BEAST_NUDB_VARINT_H_INCLUDED + +#include // for BEAST_CONSTEXPR +#include +#include +#include // + +namespace beast { +namespace nudb { +namespace detail { + +// base128 varint format is from +// google protocol buffers: +// https://developers.google.com/protocol-buffers/docs/encoding#varints + +// field tag +struct varint; + +// Metafuncton to return largest +// possible size of T represented as varint. +// T must be unsigned +template ::value> +struct varint_traits; + +template +struct varint_traits +{ + static std::size_t BEAST_CONSTEXPR max = + (8 * sizeof(T) + 6) / 7; +}; + +// Returns: Number of bytes consumed or 0 on error, +// if the buffer was too small or t overflowed. +// +template +std::size_t +read_varint (void const* buf, + std::size_t buflen, std::size_t& t) +{ + t = 0; + std::uint8_t const* p = + reinterpret_cast< + std::uint8_t const*>(buf); + std::size_t n = 0; + while (p[n] & 0x80) + if (++n >= buflen) + return 0; + if (++n > buflen) + return 0; + // Special case for 0 + if (n == 1 && *p == 0) + { + t = 0; + return 1; + } + auto const used = n; + while (n--) + { + auto const d = p[n]; + auto const t0 = t; + t *= 127; + t += d & 0x7f; + if (t <= t0) + return 0; // overflow + } + return used; +} + +template ::value>* = nullptr> +std::size_t +size_varint (T v) +{ + std::size_t n = 0; + do + { + v /= 127; + ++n; + } + while (v != 0); + return n; +} + +template +std::size_t +write_varint (void* p0, std::size_t v) +{ + std::uint8_t* p = reinterpret_cast< + std::uint8_t*>(p0); + do + { + std::uint8_t d = + v % 127; + v /= 127; + if (v != 0) + d |= 0x80; + *p++ = d; + } + while (v != 0); + return p - reinterpret_cast< + std::uint8_t*>(p0); +} + +// input stream + +template ::value>* = nullptr> +void +read (istream& is, std::size_t& u) +{ + auto p0 = is(1); + auto p1 = p0; + while (*p1++ & 0x80) + is(1); + read_varint(p0, p1 - p0, u); +} + +// output stream + +template ::value>* = nullptr> +void +write (ostream& os, std::size_t t) +{ + write_varint(os.data( + size_varint(t)), t); +} + +} // detail +} // nudb +} // beast + +#endif diff --git a/src/beast/beast/nudb/file.h b/src/beast/beast/nudb/file.h index bba66092ca..f04f4ae1fd 100644 --- a/src/beast/beast/nudb/file.h +++ b/src/beast/beast/nudb/file.h @@ -20,9 +20,8 @@ #ifndef BEAST_NUDB_FILE_H_INCLUDED #define BEAST_NUDB_FILE_H_INCLUDED -#include -#include -#include +#include +#include #include namespace beast { diff --git a/src/beast/beast/nudb/mode.h b/src/beast/beast/nudb/identity_codec.h similarity index 59% rename from src/beast/beast/nudb/mode.h rename to src/beast/beast/nudb/identity_codec.h index b4b42e18f1..0846e3002c 100644 --- a/src/beast/beast/nudb/mode.h +++ b/src/beast/beast/nudb/identity_codec.h @@ -17,25 +17,46 @@ */ //============================================================================== -#ifndef BEAST_NUDB_MODE_H_INCLUDED -#define BEAST_NUDB_MODE_H_INCLUDED +#ifndef BEAST_NUDB_IDENTITY_CODEC_H_INCLUDED +#define BEAST_NUDB_IDENTITY_CODEC_H_INCLUDED -#include -#include +#include namespace beast { namespace nudb { -enum class file_mode +/** Codec which maps input directly to output. */ +class identity_codec { - scan, // read sequential - read, // read random - append, // read random, write append - write // read random, write random -}; +public: + template + explicit + identity_codec(Args&&... args) + { + } -// This sort of doesn't belong here -using path_type = std::string; + char const* + name() const + { + return "none"; + } + + template + std::pair + compress (void const* in, + std::size_t in_size, BufferFactory&&) const + { + return std::make_pair(in, in_size); + } + + template + std::pair + decompress (void const* in, + std::size_t in_size, BufferFactory&&) const + { + return std::make_pair(in, in_size); + } +}; } // nudb } // beast diff --git a/src/beast/beast/nudb/nudb.cpp b/src/beast/beast/nudb/nudb.cpp index 9fb8cb182f..1888a95440 100644 --- a/src/beast/beast/nudb/nudb.cpp +++ b/src/beast/beast/nudb/nudb.cpp @@ -17,9 +17,8 @@ */ //============================================================================== -#include - #include #include #include +#include #include diff --git a/src/beast/beast/nudb/detail/posix_file.h b/src/beast/beast/nudb/posix_file.h similarity index 88% rename from src/beast/beast/nudb/detail/posix_file.h rename to src/beast/beast/nudb/posix_file.h index 81061e5d91..541dc54113 100644 --- a/src/beast/beast/nudb/detail/posix_file.h +++ b/src/beast/beast/nudb/posix_file.h @@ -20,11 +20,10 @@ #ifndef BEAST_NUDB_POSIX_FILE_H_INCLUDED #define BEAST_NUDB_POSIX_FILE_H_INCLUDED -#include -#include -#include -#include +#include #include +#include +#include #include #include @@ -265,14 +264,21 @@ void posix_file<_>::read (std::size_t offset, void* buffer, std::size_t bytes) { - auto const n = ::pread ( - fd_, buffer, bytes, offset); - // VFALCO end of file should throw short_read - if (n == -1) - throw file_posix_error( - "pread"); - if (n < bytes) - throw file_short_read_error(); + while(bytes > 0) + { + auto const n = ::pread ( + fd_, buffer, bytes, offset); + // VFALCO end of file should throw short_read + if (n == -1) + throw file_posix_error( + "pread"); + if (n == 0) + throw file_short_read_error(); + offset += n; + bytes -= n; + buffer = reinterpret_cast< + char*>(buffer) + n; + } } template @@ -280,13 +286,20 @@ void posix_file<_>::write (std::size_t offset, void const* buffer, std::size_t bytes) { - auto const n = ::pwrite ( - fd_, buffer, bytes, offset); - if (n == -1) - throw file_posix_error( - "pwrite"); - if (n < bytes) - throw file_short_write_error(); + while(bytes > 0) + { + auto const n = ::pwrite ( + fd_, buffer, bytes, offset); + if (n == -1) + throw file_posix_error( + "pwrite"); + if (n == 0) + throw file_short_write_error(); + offset += n; + bytes -= n; + buffer = reinterpret_cast< + char const*>(buffer) + n; + } } template diff --git a/src/beast/beast/nudb/recover.h b/src/beast/beast/nudb/recover.h index 031fcd764c..cd7847eb1e 100644 --- a/src/beast/beast/nudb/recover.h +++ b/src/beast/beast/nudb/recover.h @@ -20,12 +20,10 @@ #ifndef BEAST_NUDB_RECOVER_H_INCLUDED #define BEAST_NUDB_RECOVER_H_INCLUDED -#include +#include #include -#include #include #include -#include #include #include #include @@ -39,19 +37,22 @@ namespace nudb { any partially committed data. */ template < - class Hasher = default_hash, - class File = native_file> + class Hasher, + class Codec, + class File = native_file, + class... Args> bool recover ( path_type const& dat_path, path_type const& key_path, path_type const& log_path, - std::size_t read_size = 16 * 1024 * 1024) + std::size_t read_size, + Args&&... args) { using namespace detail; - File df; - File lf; - File kf; + File df(args...); + File lf(args...); + File kf(args...); if (! df.open (file_mode::append, dat_path)) return false; if (! kf.open (file_mode::write, key_path)) @@ -96,8 +97,7 @@ recover ( verify(kh, lh); auto const df_size = df.actual_size(); buffer buf(kh.block_size); - bucket b (kh.key_size, - kh.block_size, buf.get()); + bucket b (kh.block_size, buf.get()); bulk_reader r(lf, log_file_header::size, lf_size, read_size); while(! r.eof()) diff --git a/src/beast/beast/nudb/store.h b/src/beast/beast/nudb/store.h index 5530787d45..49b4abbb14 100644 --- a/src/beast/beast/nudb/store.h +++ b/src/beast/beast/nudb/store.h @@ -20,20 +20,15 @@ #ifndef BEAST_NUDB_STORE_H_INCLUDED #define BEAST_NUDB_STORE_H_INCLUDED -#include -#include -#include +#include #include #include -#include +#include #include #include -#include #include #include #include -#include -#include #include #include #include @@ -52,7 +47,6 @@ #include #include // #include -#include #include #include #include @@ -80,15 +74,17 @@ namespace nudb { */ /** A simple key/value database + @tparam Hasher The hash function to use on key + @tparam Codec The codec to apply to value data @tparam File The type of File object to use. - @tparam Hash The hash function to use on key */ -template -class basic_store +template +class store { public: - using file_type = File; using hash_type = Hasher; + using codec_type = Codec; + using file_type = File; private: // requires 64-bit integers or better @@ -112,9 +108,6 @@ private: using unique_lock_type = boost::unique_lock; - using blockbuf = - typename detail::buffers::value_type; - struct state { File df; @@ -123,11 +116,11 @@ private: path_type dp; path_type kp; path_type lp; - detail::buffers b; detail::pool p0; detail::pool p1; detail::cache c0; detail::cache c1; + Codec const codec; detail::key_file_header const kh; // pool commit high water mark @@ -144,8 +137,6 @@ private: }; bool open_ = false; - // VFALCO Make consistency checks optional? - //bool safe_ = true; // Do consistency checks // VFALCO Unfortunately boost::optional doesn't support // move construction so we use unique_ptr instead. @@ -173,9 +164,9 @@ private: std::exception_ptr ep_; public: - basic_store() = default; - basic_store (basic_store const&) = delete; - basic_store& operator= (basic_store const&) = delete; + store() = default; + store (store const&) = delete; + store& operator= (store const&) = delete; /** Destroy the database. @@ -191,7 +182,7 @@ public: Throws: None */ - ~basic_store(); + ~store(); /** Returns `true` if the database is open. */ bool @@ -250,17 +241,17 @@ public: /** Fetch a value. - If key is found, BufferFactory will be called as: - `(void*)()(std::size_t bytes)` + If key is found, Handler will be called as: + `(void)()(void const* data, std::size_t size)` - where bytes is the size of the value, and the returned pointer - points to a buffer of at least bytes size. + where data and size represent the value. If the + key is not found, the handler is not called. - @return `true` if the key exists. + @return `true` if a matching key was found. */ - template + template bool - fetch (void const* key, BufferFactory&& bf); + fetch (void const* key, Handler&& handler); /** Insert a value. @@ -280,12 +271,19 @@ private: std::rethrow_exception(ep_); } - std::pair - find (void const* key, detail::bucket& b); + // Fetch key in loaded bucket b or its spills. + // + template + bool + fetch (std::size_t h, void const* key, + detail::bucket b, Handler&& handler); - void - maybe_spill (detail::bucket& b, - detail::bulk_writer& w); + // Returns `true` if the key exists + // lock is unlocked after the first bucket processed + // + bool + exists (std::size_t h, void const* key, + shared_lock_type* lock, detail::bucket b); void split (detail::bucket& b1, detail::bucket& b2, @@ -306,8 +304,8 @@ private: //------------------------------------------------------------------------------ -template -basic_store::state::state ( +template +store::state::state ( File&& df_, File&& kf_, File&& lf_, path_type const& dp_, path_type const& kp_, path_type const& lp_, @@ -319,7 +317,6 @@ basic_store::state::state ( , dp (dp_) , kp (kp_) , lp (lp_) - , b (kh_.block_size) , p0 (kh_.key_size, arena_alloc_size) , p1 (kh_.key_size, arena_alloc_size) , c0 (kh_.key_size, kh_.block_size) @@ -330,8 +327,8 @@ basic_store::state::state ( //------------------------------------------------------------------------------ -template -basic_store::~basic_store() +template +store::~store() { try { @@ -344,10 +341,10 @@ basic_store::~basic_store() } } -template +template template bool -basic_store::open ( +store::open ( path_type const& dat_path, path_type const& key_path, path_type const& log_path, @@ -358,11 +355,13 @@ basic_store::open ( if (is_open()) throw std::logic_error("nudb: already open"); epb_.store(false); - recover (dat_path, key_path, log_path, - recover_read_size); - File df(std::forward(args)...); - File kf(std::forward(args)...); - File lf(std::forward(args)...); + recover( + dat_path, key_path, log_path, + recover_read_size, + args...); + File df(args...); + File kf(args...); + File lf(args...); if (! df.open (file_mode::append, dat_path)) return false; if (! kf.open (file_mode::write, key_path)) @@ -373,7 +372,7 @@ basic_store::open ( key_file_header kh; read (df, dh); read (kf, kh); - verify (dh); + verify (dh); verify (kh); verify (dh, kh); auto s = std::make_unique( @@ -392,13 +391,13 @@ basic_store::open ( s_ = std::move(s); open_ = true; thread_ = std::thread( - &basic_store::run, this); + &store::run, this); return true; } -template +template void -basic_store::close() +store::close() { if (open_) { @@ -414,229 +413,208 @@ basic_store::close() } } -template -template +template +template bool -basic_store::fetch ( - void const* key, BufferFactory&& bf) +store::fetch ( + void const* key, Handler&& handler) { using namespace detail; rethrow(); - std::size_t offset; - std::size_t size; - blockbuf buf(s_->b); - bucket tmp (s_->kh.key_size, - s_->kh.block_size, buf.get()); + auto const h = hash( + key, s_->kh.key_size, s_->kh.salt); + shared_lock_type m (m_); { - auto const h = hash( - key, s_->kh.key_size, s_->kh.salt); - shared_lock_type m (m_, - boost::defer_lock); - m.lock(); + auto iter = s_->p1.find(key); + if (iter == s_->p1.end()) { - typename pool::iterator iter; - iter = s_->p1.find(key); - if (iter != s_->p1.end()) - { - void* const b = bf( - iter->first.size); - if (b == nullptr) - return false; - std::memcpy (b, - iter->first.data, - iter->first.size); - return true; - } iter = s_->p0.find(key); - if (iter != s_->p0.end()) - { - void* const b = bf( - iter->first.size); - if (b == nullptr) - return false; - std::memcpy (b, - iter->first.data, - iter->first.size); - return true; - } + if (iter == s_->p0.end()) + goto next; } + buffer buf; + auto const result = + s_->codec.decompress( + iter->first.data, + iter->first.size, buf); + handler(result.first, result.second); + return true; + } +next: + auto const n = bucket_index( + h, buckets_, modulus_); + auto const iter = s_->c1.find(n); + if (iter != s_->c1.end()) + return fetch(h, key, + iter->second, handler); + // VFALCO Audit for concurrency + genlock g (g_); + m.unlock(); + buffer buf (s_->kh.block_size); + // VFALCO Constructs with garbage here + bucket b (s_->kh.block_size, + buf.get()); + b.read (s_->kf, + (n + 1) * b.block_size()); + return fetch(h, key, b, handler); +} + +template +bool +store::insert ( + void const* key, void const* data, + std::size_t size) +{ + using namespace detail; + rethrow(); + buffer buf; + // Data Record + if (size > field::max) + throw std::logic_error( + "nudb: size too large"); + auto const h = hash( + key, s_->kh.key_size, s_->kh.salt); + std::lock_guard u (u_); + { + shared_lock_type m (m_); + if (s_->p1.find(key) != s_->p1.end()) + return false; + if (s_->p0.find(key) != s_->p0.end()) + return false; auto const n = bucket_index( h, buckets_, modulus_); auto const iter = s_->c1.find(n); if (iter != s_->c1.end()) { - auto const result = - iter->second.find(key); - if (result.second) - { - offset = result.first.offset; - size = result.first.size; - goto found; - } - // VFALCO Audit for concurrency - auto spill = iter->second.spill(); - m.unlock(); - while (spill) - { - tmp.read(s_->df, spill); - auto const result = tmp.find(key); - if (result.second) - { - offset = result.first.offset; - size = result.first.size; - goto found; - } - spill = tmp.spill(); - } - return false; - } - // VFALCO Audit for concurrency - genlock g (g_); - m.unlock(); - tmp.read (s_->kf, - (n + 1) * tmp.block_size()); - auto const result = find(key, tmp); - if (! result.second) - return false; - offset = result.first.offset; - size = result.first.size; - } -found: - void* const b = bf(size); - if (b == nullptr) - return false; - // Data Record - s_->df.read (offset + - field::size + // Size - s_->kh.key_size, // Key - b, size); - return true; -} - -template -bool -basic_store::insert (void const* key, - void const* data, std::size_t size) -{ - using namespace detail; - rethrow(); -#if ! BEAST_NUDB_NO_DOMAIN_CHECK - if (size > field::max) - throw std::logic_error( - "nudb: size too large"); -#endif - blockbuf buf (s_->b); - bucket tmp (s_->kh.key_size, - s_->kh.block_size, buf.get()); - auto const h = hash( - key, s_->kh.key_size, s_->kh.salt); - std::lock_guard u (u_); - shared_lock_type m (m_, boost::defer_lock); - m.lock(); - if (s_->p1.find(key) != s_->p1.end()) - return false; - if (s_->p0.find(key) != s_->p0.end()) - return false; - auto const n = bucket_index( - h, buckets_, modulus_); - auto const iter = s_->c1.find(n); - if (iter != s_->c1.end()) - { - if (iter->second.find(key).second) - return false; - // VFALCO Audit for concurrency - auto spill = iter->second.spill(); - m.unlock(); - while (spill) - { - tmp.read (s_->df, spill); - if (tmp.find(key).second) + if (exists(h, key, &m, + iter->second)) return false; - spill = tmp.spill(); + // m is now unlocked } - } - else - { - genlock g (g_); - m.unlock(); - // VFALCO Audit for concurrency - tmp.read (s_->kf, - (n + 1) * s_->kh.block_size); - if (find(key, tmp).second) - return false; - } - { - unique_lock_type m (m_); - s_->p1.insert (h, key, data, size); - // Did we go over the commit limit? - if (commit_limit_ > 0 && - s_->p1.data_size() >= commit_limit_) + else { - // Yes, start a new commit - cond_.notify_all(); - // Wait for pool to shrink - cond_limit_.wait(m, - [this]() { return - s_->p1.data_size() < - commit_limit_; }); + // VFALCO Audit for concurrency + genlock g (g_); + m.unlock(); + buf.reserve(s_->kh.block_size); + bucket b (s_->kh.block_size, + buf.get()); + b.read (s_->kf, + (n + 1) * s_->kh.block_size); + if (exists(h, key, nullptr, b)) + return false; } - bool const notify = - s_->p1.data_size() >= s_->pool_thresh; - m.unlock(); - if (notify) - cond_.notify_all(); } + auto const result = + s_->codec.compress(data, size, buf); + // Perform insert + unique_lock_type m (m_); + s_->p1.insert (h, key, + result.first, result.second); + // Did we go over the commit limit? + if (commit_limit_ > 0 && + s_->p1.data_size() >= commit_limit_) + { + // Yes, start a new commit + cond_.notify_all(); + // Wait for pool to shrink + cond_limit_.wait(m, + [this]() { return + s_->p1.data_size() < + commit_limit_; }); + } + bool const notify = + s_->p1.data_size() >= s_->pool_thresh; + m.unlock(); + if (notify) + cond_.notify_all(); return true; } -// Find key in loaded bucket b or its spills. -// -template -std::pair -basic_store::find ( - void const* key, detail::bucket& b) -{ - auto result = b.find(key); - if (result.second) - return result; - auto spill = b.spill(); - while (spill) - { - b.read (s_->df, spill); - result = b.find(key); - if (result.second) - return result; - spill = b.spill(); - } - return result; -} - -// Spill bucket if full -// -template -void -basic_store::maybe_spill( - detail::bucket& b, detail::bulk_writer& w) +template +template +bool +store::fetch ( + std::size_t h, void const* key, + detail::bucket b, Handler&& handler) { using namespace detail; - if (b.full()) + buffer buf0; + buffer buf1; + for(;;) { - // Spill Record - auto const offset = w.offset(); - auto os = w.prepare( - field::size + // Zero - field::size + // Size - b.compact_size()); - write (os, 0); // Zero - write ( - os, b.compact_size()); // Size - auto const spill = - offset + os.size(); - b.write (os); // Bucket - // Update bucket - b.clear(); - b.spill (spill); + for (auto i = b.lower_bound(h); + i < b.size(); ++i) + { + auto const item = b[i]; + if (item.hash != h) + break; + // Data Record + auto const len = + s_->kh.key_size + // Key + item.size; // Value + buf0.reserve(len); + s_->df.read(item.offset + + field::size, // Size + buf0.get(), len); + if (std::memcmp(buf0.get(), key, + s_->kh.key_size) == 0) + { + auto const result = + s_->codec.decompress( + buf0.get() + s_->kh.key_size, + item.size, buf1); + handler(result.first, result.second); + return true; + } + } + auto const spill = b.spill(); + if (! spill) + break; + buf1.reserve(s_->kh.block_size); + b = bucket(s_->kh.block_size, + buf1.get()); + b.read(s_->df, spill); } + return false; +} + +template +bool +store::exists ( + std::size_t h, void const* key, + shared_lock_type* lock, detail::bucket b) +{ + using namespace detail; + buffer buf(s_->kh.key_size + + s_->kh.block_size); + void* pk = buf.get(); + void* pb = buf.get() + s_->kh.key_size; + for(;;) + { + for (auto i = b.lower_bound(h); + i < b.size(); ++i) + { + auto const item = b[i]; + if (item.hash != h) + break; + // Data Record + s_->df.read(item.offset + + field::size, // Size + pk, s_->kh.key_size); // Key + if (std::memcmp(pk, key, + s_->kh.key_size) == 0) + return true; + } + auto spill = b.spill(); + if (lock && lock->owns_lock()) + lock->unlock(); + if (! spill) + break; + b = bucket(s_->kh.block_size, pb); + b.read(s_->df, spill); + } + return false; } // Split the bucket in b1 to b2 @@ -644,9 +622,9 @@ basic_store::maybe_spill( // tmp is used as a temporary buffer // splits are written but not the new buckets // -template +template void -basic_store::split (detail::bucket& b1, +store::split (detail::bucket& b1, detail::bucket& b2, detail::bucket& tmp, std::size_t n1, std::size_t n2, std::size_t buckets, std::size_t modulus, @@ -659,15 +637,13 @@ basic_store::split (detail::bucket& b1, // Split for (std::size_t i = 0; i < b1.size();) { - auto e = b1[i]; - auto const h = hash( - e.key, s_->kh.key_size, s_->kh.salt); + auto const e = b1[i]; auto const n = bucket_index( - h, buckets, modulus); + e.hash, buckets, modulus); assert(n==n1 || n==n2); if (n == n2) { - b2.insert (e.offset, e.size, e.key); + b2.insert (e.offset, e.size, e.hash); b1.erase (i); } else @@ -684,26 +660,27 @@ basic_store::split (detail::bucket& b1, // If any part of the spill record is // in the write buffer then flush first // VFALCO Needs audit - if (spill + bucket_size(s_->kh.key_size, - s_->kh.capacity) > w.offset() - w.size()) + if (spill + bucket_size(s_->kh.capacity) > + w.offset() - w.size()) w.flush(); tmp.read (s_->df, spill); for (std::size_t i = 0; i < tmp.size(); ++i) { - auto e = tmp[i]; - auto const n = bucket_index( - e.key, s_->kh.key_size, s_->kh.salt, - buckets, modulus); + auto const e = tmp[i]; + auto const n = bucket_index( + e.hash, buckets, modulus); assert(n==n1 || n==n2); if (n == n2) { - maybe_spill (b2, w); - b2.insert (e.offset, e.size, e.key); + maybe_spill(b2, w); + b2.insert( + e.offset, e.size, e.hash); } else { - maybe_spill (b1, w); - b1.insert (e.offset, e.size, e.key); + maybe_spill(b1, w); + b1.insert( + e.offset, e.size, e.hash); } } spill = tmp.spill(); @@ -732,9 +709,9 @@ basic_store::split (detail::bucket& b1, // Postconditions: // c1, and c0, and the memory pointed to by buf may be modified // -template +template detail::bucket -basic_store::load ( +store::load ( std::size_t n, detail::cache& c1, detail::cache& c0, void* buf) { @@ -746,8 +723,7 @@ basic_store::load ( if (iter != c0.end()) return c1.insert (n, iter->second)->second; - bucket tmp (s_->kh.key_size, - s_->kh.block_size, buf); + bucket tmp (s_->kh.block_size, buf); tmp.read (s_->kf, (n + 1) * s_->kh.block_size); c0.insert (n, tmp); @@ -760,15 +736,14 @@ basic_store::load ( // // Effects: // -template +template void -basic_store::commit() +store::commit() { using namespace detail; - blockbuf buf1 (s_->b); - blockbuf buf2 (s_->b); - bucket tmp (s_->kh.key_size, - s_->kh.block_size, buf1.get()); + buffer buf1 (s_->kh.block_size); + buffer buf2 (s_->kh.block_size); + bucket tmp (s_->kh.block_size, buf1.get()); // Empty cache put in place temporarily // so we can reuse the memory from s_->c1 cache c1; @@ -788,11 +763,14 @@ basic_store::commit() // Log File Header log_file_header lh; lh.version = currentVersion; // Version + lh.uid = s_->kh.uid; // UID lh.appnum = s_->kh.appnum; // Appnum + lh.key_size = s_->kh.key_size; // Key Size lh.salt = s_->kh.salt; // Salt lh.pepper = pepper( lh.salt); // Pepper - lh.key_size = s_->kh.key_size; // Key Size + lh.block_size = + s_->kh.block_size; // Block Size lh.key_file_size = s_->kf.actual_size(); // Key File Size lh.dat_file_size = @@ -813,7 +791,7 @@ basic_store::commit() // threads are reading other data members // of this object in memory e.second = w.offset(); - auto os = w.prepare (data_size( + auto os = w.prepare (value_size( e.first.size, s_->kh.key_size)); // Data Record write (os, @@ -848,8 +826,9 @@ basic_store::commit() e.first.hash, buckets, modulus); auto b = load (n, c1, s_->c0, buf2.get()); // This can amplify writes if it spills. - maybe_spill (b, w); - b.insert (e.second, e.first.size, e.first.key); + maybe_spill(b, w); + b.insert (e.second, + e.first.size, e.first.hash); } w.flush(); } @@ -905,9 +884,9 @@ basic_store::commit() } } -template +template void -basic_store::run() +store::run() { auto const pred = [this]() @@ -957,29 +936,6 @@ basic_store::run() } } -//------------------------------------------------------------------------------ - -using store = basic_store ; - -/** Generate a random salt. */ -template -std::uint64_t -make_salt() -{ - std::random_device rng; - std::mt19937_64 gen {rng()}; - std::uniform_int_distribution dist; - return dist(gen); -} - -/** Returns the best guess at the volume's block size. */ -inline -std::size_t -block_size(std::string const& /*path*/) -{ - return 4096; -} - } // nudb } // beast diff --git a/src/beast/beast/nudb/tests/callgrind_test.cpp b/src/beast/beast/nudb/tests/callgrind_test.cpp index b7c70296a1..f4a9960aea 100644 --- a/src/beast/beast/nudb/tests/callgrind_test.cpp +++ b/src/beast/beast/nudb/tests/callgrind_test.cpp @@ -17,10 +17,7 @@ */ //============================================================================== -#include -#include #include -#include #include #include #include @@ -44,18 +41,18 @@ public: // with keys not present. void do_test (std::size_t count, - nudb::path_type const& path) + path_type const& path) { auto const dp = path + ".dat"; auto const kp = path + ".key"; auto const lp = path + ".log"; - nudb::create (dp, kp, lp, + test_api::create (dp, kp, lp, appnum, salt, sizeof(nudb::test::key_type), nudb::block_size(path), 0.50); - nudb::store db; + test_api::store db; if (! expect (db.open(dp, kp, lp, arena_alloc_size), "open")) return; @@ -67,7 +64,7 @@ public: expect (db.insert(&v.key, v.data, v.size), "insert"); } - storage s; + Storage s; for (std::size_t i = 0; i < count * 2; ++i) { if (! (i%2)) diff --git a/src/beast/beast/nudb/tests/common.h b/src/beast/beast/nudb/tests/common.h index 6fd0492160..82c6e51432 100644 --- a/src/beast/beast/nudb/tests/common.h +++ b/src/beast/beast/nudb/tests/common.h @@ -17,11 +17,13 @@ */ //============================================================================== -#ifndef BEAST_NUDB_TEST_COMMON_H_INCLUDED -#define BEAST_NUDB_TEST_COMMON_H_INCLUDED +#ifndef BEAST_NUDB_TESTS_COMMON_H_INCLUDED +#define BEAST_NUDB_TESTS_COMMON_H_INCLUDED #include +#include #include +#include #include #include #include @@ -33,9 +35,17 @@ namespace test { using key_type = std::size_t; -using fail_store = nudb::basic_store< - beast::nudb::default_hash, nudb::fail_file < - nudb::native_file>>; +// xxhasher is fast and produces good results +using test_api_base = + nudb::api; + +struct test_api : test_api_base +{ + using fail_store = nudb::store< + typename test_api_base::hash_type, + typename test_api_base::codec_type, + nudb::fail_file >; +}; static std::size_t BEAST_CONSTEXPR arena_alloc_size = 16 * 1024 * 1024; @@ -45,8 +55,8 @@ static std::uint64_t BEAST_CONSTEXPR salt = 42; //------------------------------------------------------------------------------ -// Meets the requirements of BufferFactory -class storage +// Meets the requirements of Handler +class Storage { private: std::size_t size_ = 0; @@ -54,9 +64,9 @@ private: std::unique_ptr buf_; public: - storage() = default; - storage (storage const&) = delete; - storage& operator= (storage const&) = delete; + Storage() = default; + Storage (Storage const&) = delete; + Storage& operator= (Storage const&) = delete; std::size_t size() const @@ -71,15 +81,23 @@ public: } std::uint8_t* - operator()(std::size_t n) + reserve (std::size_t size) { - if (capacity_ < n) + if (capacity_ < size) { - capacity_ = detail::ceil_pow2(n); + capacity_ = detail::ceil_pow2(size); buf_.reset ( new std::uint8_t[capacity_]); } - size_ = n; + size_ = size; + return buf_.get(); + } + + std::uint8_t* + operator()(void const* data, std::size_t size) + { + reserve (size); + std::memcpy(buf_.get(), data, size); return buf_.get(); } }; @@ -134,7 +152,7 @@ private: maxSize = 1250 }; - storage s_; + Storage s_; beast::xor_shift_engine gen_; std::uniform_int_distribution d_size_; @@ -162,7 +180,7 @@ public: value_type v; rngcpy (&v.key, sizeof(v.key), gen_); v.size = d_size_(gen_); - v.data = s_(v.size); + v.data = s_.reserve(v.size); rngcpy (v.data, v.size, gen_); return v; } @@ -205,14 +223,18 @@ print (Log log, log << "actual_load: " << std::fixed << std::setprecision(0) << info.actual_load * 100 << "%"; log << "version: " << num(info.version); - log << "salt: " << std::showbase << std::hex << info.salt; + log << "uid: " << std::showbase << std::hex << info.uid; + log << "appnum: " << info.appnum; log << "key_size: " << num(info.key_size); + log << "salt: " << std::showbase << std::hex << info.salt; + log << "pepper: " << std::showbase << std::hex << info.pepper; log << "block_size: " << num(info.block_size); log << "bucket_size: " << num(info.bucket_size); log << "load_factor: " << std::fixed << std::setprecision(0) << info.load_factor * 100 << "%"; log << "capacity: " << num(info.capacity); log << "buckets: " << num(info.buckets); + log << "key_count: " << num(info.key_count); log << "value_count: " << num(info.value_count); log << "value_bytes: " << num(info.value_bytes); log << "spill_count: " << num(info.spill_count); diff --git a/src/beast/beast/nudb/tests/fail_file.h b/src/beast/beast/nudb/tests/fail_file.h index 401cfe525c..fae4cf1623 100644 --- a/src/beast/beast/nudb/tests/fail_file.h +++ b/src/beast/beast/nudb/tests/fail_file.h @@ -20,7 +20,7 @@ #ifndef BEAST_NUDB_FAIL_FILE_H_INCLUDED #define BEAST_NUDB_FAIL_FILE_H_INCLUDED -#include +#include #include #include #include diff --git a/src/beast/beast/nudb/tests/recover_test.cpp b/src/beast/beast/nudb/tests/recover_test.cpp index 84a603b07b..5700b2e4c0 100644 --- a/src/beast/beast/nudb/tests/recover_test.cpp +++ b/src/beast/beast/nudb/tests/recover_test.cpp @@ -17,10 +17,7 @@ */ //============================================================================== -#include -#include #include -#include #include #include #include @@ -42,17 +39,19 @@ public: // they are there. Uses a fail_file that causes the n-th // I/O to fail, causing an exception. void - do_work (std::size_t n, std::size_t count, - float load_factor, nudb::path_type const& path) + do_work (std::size_t count, float load_factor, + nudb::path_type const& path, fail_counter& c) { auto const dp = path + ".dat"; auto const kp = path + ".key"; auto const lp = path + ".log"; - nudb::fail_counter c(0); - nudb::create (dp, kp, lp, appnum, salt, - sizeof(key_type), block_size(path), - load_factor); - fail_store db; + test_api::file_type::erase (dp); + test_api::file_type::erase (kp); + test_api::file_type::erase (lp); + expect(test_api::create ( + dp, kp, lp, appnum, salt, sizeof(key_type), + block_size(path), load_factor), "create"); + test_api::fail_store db; if (! expect(db.open(dp, kp, lp, arena_alloc_size, c), "open")) { @@ -60,14 +59,14 @@ public: // to report this and terminate the test. } expect (db.appnum() == appnum, "appnum"); - c.reset(n); Sequence seq; for (std::size_t i = 0; i < count; ++i) { auto const v = seq[i]; - db.insert(&v.key, v.data, v.size); + expect(db.insert(&v.key, v.data, v.size), + "insert"); } - storage s; + Storage s; for (std::size_t i = 0; i < count; ++i) { auto const v = seq[i]; @@ -81,26 +80,36 @@ public: break; } db.close(); - #ifndef NDEBUG - print(log, verify(dp, kp)); - verify(dp, kp); - #endif - nudb::native_file::erase (dp); - nudb::native_file::erase (kp); - nudb::native_file::erase (lp); + verify_info info; + try + { + info = test_api::verify(dp, kp); + } + catch(...) + { + print(log, info); + throw; + } + test_api::file_type::erase (dp); + test_api::file_type::erase (kp); + test_api::file_type::erase (lp); } void - do_recover (path_type const& path) + do_recover (path_type const& path, + fail_counter& c) { auto const dp = path + ".dat"; auto const kp = path + ".key"; auto const lp = path + ".log"; - recover(dp, kp, lp); - verify(dp, kp); - nudb::native_file::erase (dp); - nudb::native_file::erase (kp); - nudb::native_file::erase (lp); + recover>(dp, kp, lp, + test_api::buffer_size, c); + test_api::verify(dp, kp); + test_api::file_type::erase (dp); + test_api::file_type::erase (kp); + test_api::file_type::erase (lp); } void @@ -114,12 +123,24 @@ public: { try { - do_work (n, count, load_factor, path); + fail_counter c(n); + do_work (count, load_factor, path, c); break; } catch (nudb::fail_error const&) { - do_recover (path); + } + for (std::size_t m = 1;;++m) + { + fail_counter c(m); + try + { + do_recover (path, c); + break; + } + catch (nudb::fail_error const&) + { + } } } } @@ -131,11 +152,10 @@ public: void run() override { - float lf = 0.75f; + float lf = 0.55f; test_recover (lf, 0); test_recover (lf, 10); test_recover (lf, 100); - test_recover (lf, 1000); } }; @@ -148,7 +168,8 @@ public: run() override { float lf = 0.90f; - test_recover (lf, 100000); + test_recover (lf, 1000); + test_recover (lf, 10000); } }; diff --git a/src/beast/beast/nudb/tests/store_test.cpp b/src/beast/beast/nudb/tests/store_test.cpp index 4a858bbc2b..20a1f6bf0c 100644 --- a/src/beast/beast/nudb/tests/store_test.cpp +++ b/src/beast/beast/nudb/tests/store_test.cpp @@ -18,9 +18,7 @@ //============================================================================== #include -#include #include -#include #include #include #include @@ -55,15 +53,15 @@ public: auto const kp = path + ".key"; auto const lp = path + ".log"; Sequence seq; - nudb::store db; + test_api::store db; try { - expect (nudb::create (dp, kp, lp, appnum, + expect (test_api::create (dp, kp, lp, appnum, salt, sizeof(key_type), block_size, load_factor), "create"); expect (db.open(dp, kp, lp, arena_alloc_size), "open"); - storage s; + Storage s; // insert for (std::size_t i = 0; i < N; ++i) { @@ -102,7 +100,9 @@ public: "insert 2"); } db.close(); - auto const stats = nudb::verify (dp, kp); + //auto const stats = test_api::verify(dp, kp); + auto const stats = verify( + dp, kp, 1 * 1024 * 1024); expect (stats.hist[1] > 0, "no splits"); print (log, stats); } @@ -114,9 +114,9 @@ public: { fail (e.what()); } - expect (native_file::erase(dp)); - expect (native_file::erase(kp)); - expect (! native_file::erase(lp)); + expect (test_api::file_type::erase(dp)); + expect (test_api::file_type::erase(kp)); + expect (! test_api::file_type::erase(lp)); } void diff --git a/src/beast/beast/nudb/tests/varint_test.cpp b/src/beast/beast/nudb/tests/varint_test.cpp new file mode 100644 index 0000000000..9aab351071 --- /dev/null +++ b/src/beast/beast/nudb/tests/varint_test.cpp @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2014, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#include +#include +#include + +namespace beast { +namespace nudb { +namespace tests { + +class varint_test : public unit_test::suite +{ +public: + void + test_varints (std::vector vv) + { + testcase("encode, decode"); + for (auto const v : vv) + { + std::array::max> vi; + auto const n0 = + detail::write_varint( + vi.data(), v); + expect (n0 > 0, "write error"); + std::size_t v1; + auto const n1 = + detail::read_varint( + vi.data(), n0, v1); + expect(n1 == n0, "read error"); + expect(v == v1, "wrong value"); + } + } + + void + run() override + { + test_varints({ + 0, 1, 2, + 126, 127, 128, + 253, 254, 255, + 16127, 16128, 16129, + 0xff, + 0xffff, + 0xffffffff, + 0xffffffffffffUL, + 0xffffffffffffffffUL}); + } +}; + +BEAST_DEFINE_TESTSUITE(varint,nudb,beast); + +} // test +} // nudb +} // beast diff --git a/src/beast/beast/nudb/tests/verify_test.cpp b/src/beast/beast/nudb/tests/verify_test.cpp index a934de7c63..f6f5e71010 100644 --- a/src/beast/beast/nudb/tests/verify_test.cpp +++ b/src/beast/beast/nudb/tests/verify_test.cpp @@ -20,11 +20,213 @@ #include #include #include +#include +#include +#include +#include namespace beast { namespace nudb { namespace test { +namespace detail { + +class save_stream_state +{ + std::ostream& os_; + std::streamsize precision_; + std::ios::fmtflags flags_; + std::ios::char_type fill_; +public: + ~save_stream_state() + { + os_.precision(precision_); + os_.flags(flags_); + os_.fill(fill_); + } + save_stream_state(save_stream_state const&) = delete; + save_stream_state& operator=(save_stream_state const&) = delete; + explicit save_stream_state(std::ostream& os) + : os_(os) + , precision_(os.precision()) + , flags_(os.flags()) + , fill_(os.fill()) + { + } +}; + +template +std::ostream& +pretty_time(std::ostream& os, std::chrono::duration d) +{ + save_stream_state _(os); + using namespace std::chrono; + if (d < microseconds{1}) + { + // use nanoseconds + if (d < nanoseconds{100}) + { + // use floating + using ns = duration; + os << std::fixed << std::setprecision(1) << ns(d).count(); + } + else + { + // use integral + os << std::chrono::duration_cast(d).count(); + } + os << "ns"; + } + else if (d < milliseconds{1}) + { + // use microseconds + if (d < microseconds{100}) + { + // use floating + using ms = duration; + os << std::fixed << std::setprecision(1) << ms(d).count(); + } + else + { + // use integral + os << std::chrono::duration_cast(d).count(); + } + os << "us"; + } + else if (d < seconds{1}) + { + // use milliseconds + if (d < milliseconds{100}) + { + // use floating + using ms = duration; + os << std::fixed << std::setprecision(1) << ms(d).count(); + } + else + { + // use integral + os << std::chrono::duration_cast(d).count(); + } + os << "ms"; + } + else if (d < minutes{1}) + { + // use seconds + if (d < seconds{100}) + { + // use floating + using s = duration; + os << std::fixed << std::setprecision(1) << s(d).count(); + } + else + { + // use integral + os << std::chrono::duration_cast(d).count(); + } + os << "s"; + } + else + { + // use minutes + if (d < minutes{100}) + { + // use floating + using m = duration>; + os << std::fixed << std::setprecision(1) << m(d).count(); + } + else + { + // use integral + os << std::chrono::duration_cast(d).count(); + } + os << "min"; + } + return os; +} + +template +inline +std::string +fmtdur(std::chrono::duration const& d) +{ + std::stringstream ss; + pretty_time(ss, d); + return ss.str(); +} + +} // detail + +//------------------------------------------------------------------------------ + +template +class progress +{ +private: + using clock_type = + beast::basic_seconds_clock< + std::chrono::steady_clock>; + + Log& log_; + clock_type::time_point start_ = clock_type::now(); + clock_type::time_point now_ = clock_type::now(); + clock_type::time_point report_ = clock_type::now(); + std::size_t prev_ = 0; + bool estimate_ = false; + +public: + explicit + progress(Log& log) + : log_(log) + { + } + + void + operator()(std::size_t w, std::size_t w1) + { + using namespace std::chrono; + auto const now = clock_type::now(); + if (now == now_) + return; + now_ = now; + auto const elapsed = now - start_; + if (! estimate_) + { + if (elapsed < seconds(15)) + return; + estimate_ = true; + } + else if (now - report_ < + std::chrono::seconds(60)) + { + return; + } + auto const rate = + elapsed.count() / double(w); + clock_type::duration const remain( + static_cast( + (w1 - w) * rate)); + log_ << + "Remaining: " << detail::fmtdur(remain) << + " (" << w << " of " << w1 << + " in " << detail::fmtdur(elapsed) << + ", " << (w - prev_) << + " in " << detail::fmtdur(now - report_) << + ")"; + report_ = now; + prev_ = w; + } + + void + finish() + { + log_ << + "Total time: " << detail::fmtdur( + clock_type::now() - start_); + } +}; + +//------------------------------------------------------------------------------ + class verify_test : public unit_test::suite { public: @@ -34,12 +236,41 @@ public: { auto const dp = path + ".dat"; auto const kp = path + ".key"; - print(log, verify(dp, kp)); + print(log, test_api::verify(dp, kp)); } void run() override { + testcase(abort_on_fail) << "verify " << arg(); + if (arg().empty()) + return fail("missing unit test argument"); + do_verify(arg()); + pass(); + } +}; + +class verify_fast_test : public unit_test::suite +{ +public: + // Runs verify on the database and reports statistics + void + do_verify (nudb::path_type const& path) + { + auto const dp = path + ".dat"; + auto const kp = path + ".key"; + progress p(log); + // VFALCO HACK 32gb hardcoded! + auto const info = verify_fast< + test_api::hash_type>( + dp, kp, 34359738368, p); + print(log, info); + } + + void + run() override + { + testcase(abort_on_fail) << "verify_fast " << arg(); if (arg().empty()) return fail("missing unit test argument"); do_verify(arg()); @@ -48,8 +279,8 @@ public: }; BEAST_DEFINE_TESTSUITE_MANUAL(verify,nudb,beast); +BEAST_DEFINE_TESTSUITE_MANUAL(verify_fast,nudb,beast); } // test } // nudb } // beast - diff --git a/src/beast/beast/nudb/verify.h b/src/beast/beast/nudb/verify.h index 2bd749f310..78a7d41835 100644 --- a/src/beast/beast/nudb/verify.h +++ b/src/beast/beast/nudb/verify.h @@ -20,12 +20,10 @@ #ifndef BEAST_NUDB_VERIFY_H_INCLUDED #define BEAST_NUDB_VERIFY_H_INCLUDED -#include +#include #include -#include #include #include -#include #include #include #include @@ -39,8 +37,11 @@ struct verify_info { // Configured std::size_t version = 0; // API version - std::size_t salt = 0; // Salt or database ID + std::size_t uid = 0; // UID + std::size_t appnum = 0; // Appnum std::size_t key_size = 0; // Size of a key in bytes + std::size_t salt = 0; // Salt + std::size_t pepper = 0; // Pepper std::size_t block_size = 0; // Block size in bytes float load_factor = 0; // Target bucket fill fraction @@ -82,12 +83,12 @@ struct verify_info Iterates the key and data files, throws store_corrupt_error on broken invariants. */ -template +template verify_info verify ( path_type const& dat_path, path_type const& key_path, - std::size_t read_size = 16 * 1024 * 1024) + std::size_t read_size) { using namespace detail; using File = native_file; @@ -103,12 +104,16 @@ verify ( dat_file_header dh; read (df, dh); read (kf, kh); + verify(dh); verify(dh, kh); verify_info info; info.version = dh.version; - info.salt = dh.salt; + info.uid = dh.uid; + info.appnum = dh.appnum; info.key_size = dh.key_size; + info.salt = kh.salt; + info.pepper = kh.pepper; info.block_size = kh.block_size; info.load_factor = kh.load_factor / 65536.f; info.capacity = kh.capacity; @@ -117,21 +122,28 @@ verify ( info.key_file_size = kf.actual_size(); info.dat_file_size = df.actual_size(); - buffer buf (kh.block_size); - bucket b (kh.key_size, - kh.block_size, buf.get()); + // Data Record + auto const dh_len = + field::size + // Size + kh.key_size; // Key + + std::size_t fetches = 0; // Iterate Data File + buffer buf (kh.block_size + dh_len); + bucket b (kh.block_size, buf.get()); + std::uint8_t* pd = buf.get() + kh.block_size; { bulk_reader r(df, dat_file_header::size, df.actual_size(), read_size); while (! r.eof()) { + auto const offset = r.offset(); // Data Record or Spill Record - std::size_t size; auto is = r.prepare( field::size); // Size + std::size_t size; read(is, size); if (size > 0) { @@ -144,39 +156,49 @@ verify ( std::uint8_t const* const data = is.data(size); (void)data; + auto const h = hash( + key, kh.key_size, kh.salt); // Check bucket and spills try { - b.read (kf, (bucket_index( - key, kh) + 1) * kh.block_size); + auto const n = bucket_index( + h, kh.buckets, kh.modulus); + b.read (kf, (n + 1) * kh.block_size); + ++fetches; } catch (file_short_read_error const&) { throw store_corrupt_error( "short bucket"); } - for(;;) + for (;;) { - if (b.find(key).second) - break; - if (b.spill() != 0) + for (auto i = b.lower_bound(h); + i < b.size(); ++i) { - try - { - b.read (df, b.spill()); - } - catch (file_short_read_error const&) - { - throw store_corrupt_error( - "short spill"); - } + auto const item = b[i]; + if (item.hash != h) + break; + if (item.offset == offset) + goto found; + ++fetches; } - else - { + auto const spill = b.spill(); + if (! spill) throw store_corrupt_error( "orphaned value"); + try + { + b.read (df, spill); + ++fetches; + } + catch (file_short_read_error const&) + { + throw store_corrupt_error( + "short spill"); } } + found: // Update ++info.value_count; info.value_bytes += size; @@ -196,17 +218,12 @@ verify ( field::size + // Zero field::size + // Size b.compact_size(); // Bucket - } } } // Iterate Key File { - // Data Record (header) - buffer buf ( - field::size + // Size - kh.key_size); // Key Size for (std::size_t n = 0; n < kh.buckets; ++n) { std::size_t nspill = 0; @@ -219,8 +236,7 @@ verify ( auto const e = b[i]; try { - df.read (e.offset, - buf.get(), buf.size()); + df.read (e.offset, pd, dh_len); } catch (file_short_read_error const&) { @@ -228,16 +244,19 @@ verify ( "missing value"); } // Data Record - istream is(buf.get(), buf.size()); + istream is(pd, dh_len); std::size_t size; read(is, size); // Size + void const* key = + is.data(kh.key_size); // Key if (size != e.size) throw store_corrupt_error( "wrong size"); - if (std::memcmp(is.data(kh.key_size), - e.key, kh.key_size) != 0) + auto const h = hash(key, + kh.key_size, kh.salt); + if (h != e.hash) throw store_corrupt_error( - "wrong key"); + "wrong hash"); } if (! b.spill()) break; @@ -266,12 +285,242 @@ verify ( float sum = 0; for (int i = 0; i < info.hist.size(); ++i) sum += info.hist[i] * (i + 1); - info.avg_fetch = sum / info.buckets; + //info.avg_fetch = sum / info.buckets; + info.avg_fetch = float(fetches) / info.value_count; info.waste = (info.spill_bytes_tot - info.spill_bytes) / float(info.dat_file_size); info.overhead = float(info.key_file_size + info.dat_file_size) / - (info.value_bytes + info.key_count * info.key_size) - 1; + ( + info.value_bytes + + info.key_count * + (info.key_size + + // Data Record + field::size) // Size + ) - 1; + info.actual_load = info.key_count / float( + info.capacity * info.buckets); + return info; +} + +/** Verify consistency of the key and data files. + Effects: + Opens the key and data files in read-only mode. + Throws file_error if a file can't be opened. + Iterates the key and data files, throws store_corrupt_error + on broken invariants. + This uses a different algorithm that depends on allocating + a lareg buffer. +*/ +template +verify_info +verify_fast ( + path_type const& dat_path, + path_type const& key_path, + std::size_t buffer_size, + Progress&& progress) +{ + using namespace detail; + using File = native_file; + File df; + File kf; + if (! df.open (file_mode::scan, dat_path)) + throw store_corrupt_error( + "no data file"); + if (! kf.open (file_mode::read, key_path)) + throw store_corrupt_error( + "no key file"); + key_file_header kh; + dat_file_header dh; + read (df, dh); + read (kf, kh); + verify(dh); + verify(dh, kh); + + verify_info info; + info.version = dh.version; + info.uid = dh.uid; + info.appnum = dh.appnum; + info.key_size = dh.key_size; + info.salt = kh.salt; + info.pepper = kh.pepper; + info.block_size = kh.block_size; + info.load_factor = kh.load_factor / 65536.f; + info.capacity = kh.capacity; + info.buckets = kh.buckets; + info.bucket_size = kh.bucket_size; + info.key_file_size = kf.actual_size(); + info.dat_file_size = df.actual_size(); + + std::size_t fetches = 0; + + // Counts unverified keys per bucket + std::unique_ptr nkeys( + new std::uint32_t[kh.buckets]); + + // Verify contiguous sequential sections of the + // key file using multiple passes over the data. + // + auto const buckets = std::max(1, + buffer_size / kh.block_size); + buffer buf((buckets + 1) * kh.block_size); + bucket tmp(kh.block_size, buf.get() + + buckets * kh.block_size); + std::size_t const passes = + (kh.buckets + buckets - 1) / buckets; + auto const df_size = df.actual_size(); + std::size_t const work = passes * df_size; + std::size_t npass = 0; + for (std::size_t b0 = 0; b0 < kh.buckets; + b0 += buckets) + { + auto const b1 = std::min( + b0 + buckets, kh.buckets); + // Buffered range is [b0, b1) + auto const bn = b1 - b0; + kf.read((b0 + 1) * kh.block_size, + buf.get(), bn * kh.block_size); + // Count keys in buckets + for (std::size_t i = b0 ; i < b1; ++i) + { + bucket b(kh.block_size, buf.get() + + (i - b0) * kh.block_size); + nkeys[i] = b.size(); + std::size_t nspill = 0; + auto spill = b.spill(); + while (spill != 0) + { + tmp.read(df, spill); + nkeys[i] += tmp.size(); + spill = tmp.spill(); + ++nspill; + ++info.spill_count; + info.spill_bytes += + field::size + // Zero + field::size + // Size + tmp.compact_size(); // SpillBucket + } + if (nspill >= info.hist.size()) + nspill = info.hist.size() - 1; + ++info.hist[nspill]; + info.key_count += nkeys[i]; + } + // Iterate Data File + bulk_reader r(df, + dat_file_header::size, df_size, + 64 * 1024 * 1024); + while (! r.eof()) + { + auto const offset = r.offset(); + progress(npass * df_size + offset, work); + // Data Record or Spill Record + auto is = r.prepare( + field::size); // Size + std::size_t size; + read(is, size); + if (size > 0) + { + // Data Record + is = r.prepare( + kh.key_size + // Key + size); // Data + std::uint8_t const* const key = + is.data(kh.key_size); + std::uint8_t const* const data = + is.data(size); + (void)data; + auto const h = hash( + key, kh.key_size, kh.salt); + auto const n = bucket_index( + h, kh.buckets, kh.modulus); + if (n < b0 || n >= b1) + continue; + // Check bucket and spills + bucket b (kh.block_size, buf.get() + + (n - b0) * kh.block_size); + ++fetches; + for (;;) + { + for (auto i = b.lower_bound(h); + i < b.size(); ++i) + { + auto const item = b[i]; + if (item.hash != h) + break; + if (item.offset == offset) + goto found; + ++fetches; + } + auto const spill = b.spill(); + if (! spill) + throw store_corrupt_error( + "orphaned value"); + b = tmp; + try + { + b.read (df, spill); + ++fetches; + } + catch (file_short_read_error const&) + { + throw store_corrupt_error( + "short spill"); + } + } + found: + // Update + ++info.value_count; + info.value_bytes += size; + if (nkeys[n]-- == 0) + throw store_corrupt_error( + "duplicate value"); + } + else + { + // Spill Record + is = r.prepare( + field::size); + read(is, size); // Size + if (size != kh.bucket_size) + throw store_corrupt_error( + "bad spill size"); + tmp.read(r); // Bucket + if (b0 == 0) + { + ++info.spill_count_tot; + info.spill_bytes_tot += + field::size + // Zero + field::size + // Size + tmp.compact_size(); // Bucket + } + } + } + ++npass; + } + + // Make sure every key in every bucket was visited + for (std::size_t i = 0; + i < kh.buckets; ++i) + if (nkeys[i] != 0) + throw store_corrupt_error( + "orphan value"); + + float sum = 0; + for (int i = 0; i < info.hist.size(); ++i) + sum += info.hist[i] * (i + 1); + //info.avg_fetch = sum / info.buckets; + info.avg_fetch = float(fetches) / info.value_count; + info.waste = (info.spill_bytes_tot - info.spill_bytes) / + float(info.dat_file_size); + info.overhead = + float(info.key_file_size + info.dat_file_size) / + ( + info.value_bytes + + info.key_count * + (info.key_size + + // Data Record + field::size) // Size + ) - 1; info.actual_load = info.key_count / float( info.capacity * info.buckets); return info; diff --git a/src/beast/beast/nudb/visit.h b/src/beast/beast/nudb/visit.h index ed343c9cc4..d0167b5fdc 100644 --- a/src/beast/beast/nudb/visit.h +++ b/src/beast/beast/nudb/visit.h @@ -20,11 +20,10 @@ #ifndef BEAST_NUDB_VISIT_H_INCLUDED #define BEAST_NUDB_VISIT_H_INCLUDED -#include +#include #include -#include +#include #include -#include #include #include #include @@ -44,12 +43,12 @@ namespace nudb { @return `true` if the visit completed This only requires the data file. */ -template +template bool visit( path_type const& path, - Function f, - std::size_t read_size = 16 * 1024 * 1024) + std::size_t read_size, + Function&& f) { using namespace detail; using File = native_file; @@ -57,11 +56,13 @@ visit( df.open (file_mode::scan, path); dat_file_header dh; read (df, dh); - verify (dh); + verify (dh); + Codec codec; // Iterate Data File bulk_reader r( df, dat_file_header::size, df.actual_size(), read_size); + buffer buf; try { while (! r.eof()) @@ -79,10 +80,10 @@ visit( size); // Data std::uint8_t const* const key = is.data(dh.key_size); - std::uint8_t const* const data = - is.data(size); + auto const result = codec.decompress( + is.data(size), size, buf); if (! f(key, dh.key_size, - data, size)) + result.first, result.second)) return false; } else diff --git a/src/beast/beast/nudb/detail/win32_file.h b/src/beast/beast/nudb/win32_file.h similarity index 85% rename from src/beast/beast/nudb/detail/win32_file.h rename to src/beast/beast/nudb/win32_file.h index 4d1c31ef85..dfccb86710 100644 --- a/src/beast/beast/nudb/detail/win32_file.h +++ b/src/beast/beast/nudb/win32_file.h @@ -20,9 +20,7 @@ #ifndef BEAST_NUDB_WIN32_FILE_H_INCLUDED #define BEAST_NUDB_WIN32_FILE_H_INCLUDED -#include -#include -#include +#include #include #include @@ -321,25 +319,32 @@ void win32_file<_>::read (std::size_t offset, void* buffer, std::size_t bytes) { - DWORD bytesRead; - LARGE_INTEGER li; - li.QuadPart = static_cast(offset); - OVERLAPPED ov; - ov.Offset = li.LowPart; - ov.OffsetHigh = li.HighPart; - ov.hEvent = NULL; - BOOL const bSuccess = ::ReadFile( - hf_, buffer, bytes, &bytesRead, &ov); - if (! bSuccess) + while(bytes > 0) { - DWORD const dwError = ::GetLastError(); - if (dwError != ERROR_HANDLE_EOF) - throw file_win32_error( - "read file", dwError); - throw file_short_read_error(); + DWORD bytesRead; + LARGE_INTEGER li; + li.QuadPart = static_cast(offset); + OVERLAPPED ov; + ov.Offset = li.LowPart; + ov.OffsetHigh = li.HighPart; + ov.hEvent = NULL; + BOOL const bSuccess = ::ReadFile( + hf_, buffer, bytes, &bytesRead, &ov); + if (! bSuccess) + { + DWORD const dwError = ::GetLastError(); + if (dwError != ERROR_HANDLE_EOF) + throw file_win32_error( + "read file", dwError); + throw file_short_read_error(); + } + if (bytesRead == 0) + throw file_short_read_error(); + offset += bytesRead; + bytes -= bytesRead; + buffer = reinterpret_cast( + buffer) + bytesRead; } - if (bytesRead != bytes) - throw file_short_read_error(); } template @@ -347,20 +352,28 @@ void win32_file<_>::write (std::size_t offset, void const* buffer, std::size_t bytes) { - LARGE_INTEGER li; - li.QuadPart = static_cast(offset); - OVERLAPPED ov; - ov.Offset = li.LowPart; - ov.OffsetHigh = li.HighPart; - ov.hEvent = NULL; - DWORD bytesWritten; - BOOL const bSuccess = ::WriteFile( - hf_, buffer, bytes, &bytesWritten, &ov); - if (! bSuccess) - throw file_win32_error( - "write file"); - if (bytesWritten != bytes) - throw file_short_write_error(); + while(bytes > 0) + { + LARGE_INTEGER li; + li.QuadPart = static_cast(offset); + OVERLAPPED ov; + ov.Offset = li.LowPart; + ov.OffsetHigh = li.HighPart; + ov.hEvent = NULL; + DWORD bytesWritten; + BOOL const bSuccess = ::WriteFile( + hf_, buffer, bytes, &bytesWritten, &ov); + if (! bSuccess) + throw file_win32_error( + "write file"); + if (bytesWritten == 0) + throw file_short_write_error(); + offset += bytesWritten; + bytes -= bytesWritten; + buffer = reinterpret_cast< + char const*>(buffer) + + bytesWritten; + } } template diff --git a/src/ripple/nodestore/backend/NuDBFactory.cpp b/src/ripple/nodestore/backend/NuDBFactory.cpp index d653697fe9..6c37593d87 100644 --- a/src/ripple/nodestore/backend/NuDBFactory.cpp +++ b/src/ripple/nodestore/backend/NuDBFactory.cpp @@ -24,7 +24,10 @@ #include #include #include +#include // remove asap +#include #include +#include #include #include #include @@ -61,10 +64,13 @@ public: currentType = typeTwo }; + using api = beast::nudb::api< + beast::xxhasher, beast::nudb::identity_codec>; + beast::Journal journal_; size_t const keyBytes_; std::string const name_; - beast::nudb::store db_; + api::store db_; std::atomic deletePath_; Scheduler& scheduler_; @@ -85,7 +91,7 @@ public: auto const kp = (folder / "nudb.key").string (); auto const lp = (folder / "nudb.log").string (); using beast::nudb::make_salt; - beast::nudb::create (dp, kp, lp, + api::create (dp, kp, lp, currentType, make_salt(), keyBytes, beast::nudb::block_size(kp), 0.50); @@ -200,22 +206,24 @@ public: fetch1 (void const* key, std::shared_ptr * pno) { + Status status; pno->reset(); - std::size_t bytes; - std::unique_ptr data; if (! db_.fetch (key, - [&data, &bytes](std::size_t n) + [key, pno, &status](void const* data, std::size_t size) { - bytes = n; - data.reset(new std::uint8_t[bytes]); - return data.get(); + DecodedBlob decoded (key, data, size); + if (! decoded.wasOk ()) + { + status = dataCorrupt; + return; + } + *pno = decoded.createObject(); + status = ok; })) + { return notFound; - DecodedBlob decoded (key, data.get(), bytes); - if (! decoded.wasOk ()) - return dataCorrupt; - *pno = decoded.createObject(); - return ok; + } + return status; } void @@ -236,31 +244,35 @@ public: fetch2 (void const* key, std::shared_ptr * pno) { + Status status; pno->reset(); - std::size_t actual; - std::unique_ptr compressed; if (! db_.fetch (key, - [&](std::size_t n) + [&](void const* data, std::size_t size) { - actual = n; - compressed.reset( - new char[n]); - return compressed.get(); + std::size_t actual; + if (! snappy::GetUncompressedLength( + (char const*)data, size, &actual)) + { + status = dataCorrupt; + return; + } + std::unique_ptr buf (new char[actual]); + snappy::RawUncompress ( + (char const*)data, size, buf.get()); + DecodedBlob decoded (key, buf.get(), actual); + if (! decoded.wasOk ()) + { + status = dataCorrupt; + return; + } + *pno = decoded.createObject(); + status = ok; })) + { return notFound; - std::size_t size; - if (! snappy::GetUncompressedLength( - (char const*)compressed.get(), - actual, &size)) - return dataCorrupt; - std::unique_ptr data (new char[size]); - snappy::RawUncompress (compressed.get(), - actual, data.get()); - DecodedBlob decoded (key, data.get(), size); - if (! decoded.wasOk ()) - return dataCorrupt; - *pno = decoded.createObject(); - return ok; + } + + return status; } void @@ -342,7 +354,7 @@ public: auto const lp = db_.log_path(); auto const appnum = db_.appnum(); db_.close(); - beast::nudb::visit (dp, + api::visit (dp, [&]( void const* key, std::size_t key_bytes, void const* data, std::size_t size) @@ -399,7 +411,7 @@ public: auto const kp = db_.key_path(); auto const lp = db_.log_path(); db_.close(); - beast::nudb::verify (dp, kp); + api::verify (dp, kp); db_.open (dp, kp, lp, arena_alloc_size); } From d1c08889feb915e48baddc14f7efa1a8c0ffa4d9 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sun, 8 Feb 2015 15:11:57 -0800 Subject: [PATCH 12/33] Remove obsolete NodeObject fields: Legacy fields of NodeObject are removed, as they are no longer used and there is a space savings from omitting them: * Remove LedgerIndex --- src/ripple/app/ledger/AccountStateSF.cpp | 6 ++-- src/ripple/app/ledger/AccountStateSF.h | 5 +--- src/ripple/app/ledger/InboundLedger.cpp | 28 +++++++++---------- src/ripple/app/ledger/Ledger.cpp | 2 +- src/ripple/app/ledger/TransactionStateSF.cpp | 4 +-- src/ripple/app/ledger/TransactionStateSF.h | 5 +--- src/ripple/app/node/SqliteFactory.cpp | 19 ++++++------- src/ripple/nodestore/Database.h | 1 - src/ripple/nodestore/NodeObject.h | 7 ----- src/ripple/nodestore/impl/DatabaseImp.h | 10 +++---- .../nodestore/impl/DatabaseRotatingImp.h | 3 +- src/ripple/nodestore/impl/DecodedBlob.cpp | 9 +----- src/ripple/nodestore/impl/DecodedBlob.h | 1 - src/ripple/nodestore/impl/EncodedBlob.cpp | 27 ++++++++---------- src/ripple/nodestore/impl/NodeObject.cpp | 14 +--------- src/ripple/nodestore/tests/Base.test.h | 5 +--- src/ripple/nodestore/tests/Timing.test.cpp | 4 +-- src/ripple/overlay/impl/PeerImp.cpp | 3 +- src/ripple/shamap/impl/SHAMap.cpp | 4 +-- 19 files changed, 53 insertions(+), 104 deletions(-) diff --git a/src/ripple/app/ledger/AccountStateSF.cpp b/src/ripple/app/ledger/AccountStateSF.cpp index 7ef576a1d8..541efa1842 100644 --- a/src/ripple/app/ledger/AccountStateSF.cpp +++ b/src/ripple/app/ledger/AccountStateSF.cpp @@ -27,8 +27,7 @@ namespace ripple { -AccountStateSF::AccountStateSF (std::uint32_t ledgerSeq) - : mLedgerSeq (ledgerSeq) +AccountStateSF::AccountStateSF() { } @@ -41,7 +40,8 @@ void AccountStateSF::gotNode (bool fromFilter, // VFALCO SHAMapSync filters should be passed the SHAMap, the // SHAMap should provide an accessor to get the injected Database, // and this should use that Database instad of getNodeStore - getApp().getNodeStore ().store (hotACCOUNT_NODE, mLedgerSeq, std::move (nodeData), nodeHash); + getApp().getNodeStore ().store ( + hotACCOUNT_NODE, std::move (nodeData), nodeHash); } bool AccountStateSF::haveNode (SHAMapNodeID const& id, diff --git a/src/ripple/app/ledger/AccountStateSF.h b/src/ripple/app/ledger/AccountStateSF.h index 97697325c4..711fcb85a9 100644 --- a/src/ripple/app/ledger/AccountStateSF.h +++ b/src/ripple/app/ledger/AccountStateSF.h @@ -29,7 +29,7 @@ namespace ripple { class AccountStateSF : public SHAMapSyncFilter { public: - explicit AccountStateSF (std::uint32_t ledgerSeq); + AccountStateSF(); // Note that the nodeData is overwritten by this call void gotNode (bool fromFilter, @@ -41,9 +41,6 @@ public: bool haveNode (SHAMapNodeID const& id, uint256 const& nodeHash, Blob& nodeData); - -private: - std::uint32_t mLedgerSeq; }; } // ripple diff --git a/src/ripple/app/ledger/InboundLedger.cpp b/src/ripple/app/ledger/InboundLedger.cpp index 6f5788b76d..3ddab5eba5 100644 --- a/src/ripple/app/ledger/InboundLedger.cpp +++ b/src/ripple/app/ledger/InboundLedger.cpp @@ -142,8 +142,8 @@ bool InboundLedger::tryLocal () if (m_journal.trace) m_journal.trace << "Ledger header found in fetch pack"; mLedger = std::make_shared (data, true); - getApp().getNodeStore ().store (hotLEDGER, - mLedger->getLedgerSeq (), std::move (data), mHash); + getApp().getNodeStore ().store ( + hotLEDGER, std::move (data), mHash); } else { @@ -173,7 +173,7 @@ bool InboundLedger::tryLocal () } else { - TransactionStateSF filter (mLedger->getLedgerSeq ()); + TransactionStateSF filter; if (mLedger->peekTransactionMap ()->fetchRoot ( mLedger->getTransHash (), &filter)) @@ -201,7 +201,7 @@ bool InboundLedger::tryLocal () } else { - AccountStateSF filter (mLedger->getLedgerSeq ()); + AccountStateSF filter; if (mLedger->peekAccountStateMap ()->fetchRoot ( mLedger->getAccountHash (), &filter)) @@ -571,7 +571,7 @@ void InboundLedger::trigger (Peer::ptr const& peer) // VFALCO Why 256? Make this a constant nodeIDs.reserve (256); nodeHashes.reserve (256); - AccountStateSF filter (mSeq); + AccountStateSF filter; // Release the lock while we process the large state map sl.unlock(); @@ -650,7 +650,7 @@ void InboundLedger::trigger (Peer::ptr const& peer) std::vector nodeHashes; nodeIDs.reserve (256); nodeHashes.reserve (256); - TransactionStateSF filter (mSeq); + TransactionStateSF filter; mLedger->peekTransactionMap ()->getMissingNodes ( nodeIDs, nodeHashes, 256, &filter); @@ -805,8 +805,8 @@ bool InboundLedger::takeHeader (std::string const& data) Serializer s (data.size () + 4); s.add32 (HashPrefix::ledgerMaster); s.addRaw (data); - getApp().getNodeStore ().store (hotLEDGER, - mLedger->getLedgerSeq (), std::move (s.modData ()), mHash); + getApp().getNodeStore ().store ( + hotLEDGER, std::move (s.modData ()), mHash); progress (); @@ -842,7 +842,7 @@ bool InboundLedger::takeTxNode (const std::list& nodeIDs, std::list::const_iterator nodeIDit = nodeIDs.begin (); std::list< Blob >::const_iterator nodeDatait = data.begin (); - TransactionStateSF tFilter (mLedger->getLedgerSeq ()); + TransactionStateSF tFilter; while (nodeIDit != nodeIDs.end ()) { @@ -909,7 +909,7 @@ bool InboundLedger::takeAsNode (const std::list& nodeIDs, std::list::const_iterator nodeIDit = nodeIDs.begin (); std::list< Blob >::const_iterator nodeDatait = data.begin (); - AccountStateSF tFilter (mLedger->getLedgerSeq ()); + AccountStateSF tFilter; while (nodeIDit != nodeIDs.end ()) { @@ -973,7 +973,7 @@ bool InboundLedger::takeAsRootNode (Blob const& data, SHAMapAddNode& san) return false; } - AccountStateSF tFilter (mLedger->getLedgerSeq ()); + AccountStateSF tFilter; san += mLedger->peekAccountStateMap ()->addRootNode ( mLedger->getAccountHash (), data, snfWIRE, &tFilter); return san.isGood(); @@ -997,7 +997,7 @@ bool InboundLedger::takeTxRootNode (Blob const& data, SHAMapAddNode& san) return false; } - TransactionStateSF tFilter (mLedger->getLedgerSeq ()); + TransactionStateSF tFilter; san += mLedger->peekTransactionMap ()->addRootNode ( mLedger->getTransHash (), data, snfWIRE, &tFilter); return san.isGood(); @@ -1016,7 +1016,7 @@ std::vector InboundLedger::getNeededHashes () if (!mHaveState) { - AccountStateSF filter (mLedger->getLedgerSeq ()); + AccountStateSF filter; // VFALCO NOTE What's the number 4? for (auto const& h : mLedger->getNeededAccountStateHashes (4, &filter)) { @@ -1027,7 +1027,7 @@ std::vector InboundLedger::getNeededHashes () if (!mHaveTransactions) { - TransactionStateSF filter (mLedger->getLedgerSeq ()); + TransactionStateSF filter; // VFALCO NOTE What's the number 4? for (auto const& h : mLedger->getNeededTransactionHashes (4, &filter)) { diff --git a/src/ripple/app/ledger/Ledger.cpp b/src/ripple/app/ledger/Ledger.cpp index b532ca7bb0..7a16cc3e83 100644 --- a/src/ripple/app/ledger/Ledger.cpp +++ b/src/ripple/app/ledger/Ledger.cpp @@ -688,7 +688,7 @@ bool Ledger::saveValidatedLedger (bool current) s.add32 (HashPrefix::ledgerMaster); addRaw (s); getApp().getNodeStore ().store ( - hotLEDGER, mLedgerSeq, std::move (s.modData ()), mHash); + hotLEDGER, std::move (s.modData ()), mHash); } AcceptedLedger::pointer aLedger; diff --git a/src/ripple/app/ledger/TransactionStateSF.cpp b/src/ripple/app/ledger/TransactionStateSF.cpp index af83add86e..2060de4ff3 100644 --- a/src/ripple/app/ledger/TransactionStateSF.cpp +++ b/src/ripple/app/ledger/TransactionStateSF.cpp @@ -27,8 +27,7 @@ namespace ripple { -TransactionStateSF::TransactionStateSF (std::uint32_t ledgerSeq) - : mLedgerSeq (ledgerSeq) +TransactionStateSF::TransactionStateSF() { } @@ -43,7 +42,6 @@ void TransactionStateSF::gotNode (bool fromFilter, // and this should use that Database instad of getNodeStore getApp().getNodeStore ().store ( (type == SHAMapTreeNode::tnTRANSACTION_NM) ? hotTRANSACTION : hotTRANSACTION_NODE, - mLedgerSeq, std::move (nodeData), nodeHash); } diff --git a/src/ripple/app/ledger/TransactionStateSF.h b/src/ripple/app/ledger/TransactionStateSF.h index b9338ef1ef..f1fb347308 100644 --- a/src/ripple/app/ledger/TransactionStateSF.h +++ b/src/ripple/app/ledger/TransactionStateSF.h @@ -30,7 +30,7 @@ namespace ripple { class TransactionStateSF : public SHAMapSyncFilter { public: - explicit TransactionStateSF (std::uint32_t ledgerSeq); + TransactionStateSF(); // Note that the nodeData is overwritten by this call void gotNode (bool fromFilter, @@ -42,9 +42,6 @@ public: bool haveNode (SHAMapNodeID const& id, uint256 const& nodeHash, Blob& nodeData); - -private: - std::uint32_t mLedgerSeq; }; } // ripple diff --git a/src/ripple/app/node/SqliteFactory.cpp b/src/ripple/app/node/SqliteFactory.cpp index 56be1a26b7..2f85d60a48 100644 --- a/src/ripple/app/node/SqliteFactory.cpp +++ b/src/ripple/app/node/SqliteFactory.cpp @@ -27,6 +27,8 @@ namespace ripple { +// VFALCO NOTE LedgerIndex in CommittedObjects is obsolete + static const char* s_nodeStoreDBInit [] = { "PRAGMA synchronous=NORMAL;", @@ -94,7 +96,7 @@ public: uint256 const hash (uint256::fromVoid (key)); static SqliteStatement pSt (m_db->getDB()->getSqliteDB(), - "SELECT ObjType,LedgerIndex,Object FROM CommittedObjects WHERE Hash = ?;"); + "SELECT ObjType,Object FROM CommittedObjects WHERE Hash = ?;"); pSt.bind (1, to_string (hash)); @@ -102,10 +104,9 @@ public: { // VFALCO NOTE This is unfortunately needed, // the DatabaseCon creates the blob? - Blob data (pSt.getBlob (2)); + Blob data (pSt.getBlob(1)); *pObject = NodeObject::createObject ( getTypeFromString (pSt.peekString (0)), - pSt.getUInt32 (1), std::move(data), hash); } @@ -131,15 +132,13 @@ public: void storeBatch (NodeStore::Batch const& batch) { - // VFALCO TODO Rewrite this to use Beast::db - auto sl (m_db->lock()); static SqliteStatement pStB (m_db->getDB()->getSqliteDB(), "BEGIN TRANSACTION;"); static SqliteStatement pStE (m_db->getDB()->getSqliteDB(), "END TRANSACTION;"); static SqliteStatement pSt (m_db->getDB()->getSqliteDB(), "INSERT OR IGNORE INTO CommittedObjects " - "(Hash,ObjType,LedgerIndex,Object) VALUES (?, ?, ?, ?);"); + "(Hash,ObjType,Object) VALUES (?, ?, ?, ?);"); pStB.step(); pStB.reset(); @@ -163,7 +162,7 @@ public: uint256 hash; static SqliteStatement pSt(m_db->getDB()->getSqliteDB(), - "SELECT ObjType,LedgerIndex,Object,Hash FROM CommittedObjects;"); + "SELECT ObjType,Object,Hash FROM CommittedObjects;"); while (pSt.isRow (pSt.step())) { @@ -171,10 +170,9 @@ public: // VFALCO NOTE This is unfortunately needed, // the DatabaseCon creates the blob? - Blob data (pSt.getBlob (2)); + Blob data (pSt.getBlob (1)); NodeObject::Ptr const object (NodeObject::createObject ( getTypeFromString (pSt.peekString (0)), - pSt.getUInt32 (1), std::move(data), hash)); @@ -207,8 +205,7 @@ public: statement.bind(1, to_string (object->getHash())); statement.bind(2, type); - statement.bind(3, object->getLedgerIndex()); - statement.bindStatic(4, object->getData()); + statement.bindStatic(3, object->getData()); } NodeObjectType getTypeFromString (std::string const& s) diff --git a/src/ripple/nodestore/Database.h b/src/ripple/nodestore/Database.h index 0e60c1be6e..255672352e 100644 --- a/src/ripple/nodestore/Database.h +++ b/src/ripple/nodestore/Database.h @@ -106,7 +106,6 @@ public: @return `true` if the object was stored? */ virtual void store (NodeObjectType type, - std::uint32_t ledgerIndex, Blob&& data, uint256 const& hash) = 0; diff --git a/src/ripple/nodestore/NodeObject.h b/src/ripple/nodestore/NodeObject.h index 50b35adbec..e4135966b5 100644 --- a/src/ripple/nodestore/NodeObject.h +++ b/src/ripple/nodestore/NodeObject.h @@ -77,7 +77,6 @@ private: public: // This constructor is private, use createObject instead. NodeObject (NodeObjectType type, - LedgerIndex ledgerIndex, Blob&& data, uint256 const& hash, PrivateAccess); @@ -94,7 +93,6 @@ public: @param hash The 256-bit hash of the payload data. */ static Ptr createObject (NodeObjectType type, - LedgerIndex ledgerIndex, Blob&& data, uint256 const& hash); @@ -106,10 +104,6 @@ public: */ uint256 const& getHash () const; - /** Retrieve the ledger index in which this object appears. - */ - LedgerIndex getLedgerIndex() const; - /** Retrieve the binary data. */ Blob const& getData () const; @@ -136,7 +130,6 @@ public: private: NodeObjectType mType; uint256 mHash; - LedgerIndex mLedgerIndex; Blob mData; }; diff --git a/src/ripple/nodestore/impl/DatabaseImp.h b/src/ripple/nodestore/impl/DatabaseImp.h index 7775f9f12f..2423bd77d9 100644 --- a/src/ripple/nodestore/impl/DatabaseImp.h +++ b/src/ripple/nodestore/impl/DatabaseImp.h @@ -306,21 +306,19 @@ public: //------------------------------------------------------------------------------ void store (NodeObjectType type, - std::uint32_t index, Blob&& data, - uint256 const& hash) + uint256 const& hash) override { - storeInternal (type, index, std::move(data), hash, *m_backend.get()); + storeInternal (type, std::move(data), hash, *m_backend.get()); } void storeInternal (NodeObjectType type, - std::uint32_t index, Blob&& data, uint256 const& hash, Backend& backend) { - NodeObject::Ptr object = NodeObject::createObject(type, index, - std::move(data), hash); + NodeObject::Ptr object = NodeObject::createObject( + type, std::move(data), hash); #if RIPPLE_VERIFY_NODEOBJECT_KEYS assert (hash == Serializer::getSHA512Half (data)); diff --git a/src/ripple/nodestore/impl/DatabaseRotatingImp.h b/src/ripple/nodestore/impl/DatabaseRotatingImp.h index 493b84a54d..ebcf123f23 100644 --- a/src/ripple/nodestore/impl/DatabaseRotatingImp.h +++ b/src/ripple/nodestore/impl/DatabaseRotatingImp.h @@ -110,11 +110,10 @@ public: } void store (NodeObjectType type, - std::uint32_t index, Blob&& data, uint256 const& hash) override { - storeInternal (type, index, std::move(data), hash, + storeInternal (type, std::move(data), hash, *getWritableBackend()); } diff --git a/src/ripple/nodestore/impl/DecodedBlob.cpp b/src/ripple/nodestore/impl/DecodedBlob.cpp index 36b8a0aade..e16268ef02 100644 --- a/src/ripple/nodestore/impl/DecodedBlob.cpp +++ b/src/ripple/nodestore/impl/DecodedBlob.cpp @@ -40,17 +40,10 @@ DecodedBlob::DecodedBlob (void const* key, void const* value, int valueBytes) m_success = false; m_key = key; // VFALCO NOTE Ledger indexes should have started at 1 - m_ledgerIndex = LedgerIndex (-1); m_objectType = hotUNKNOWN; m_objectData = nullptr; m_dataBytes = std::max (0, valueBytes - 9); - if (valueBytes > 4) - { - LedgerIndex const* index = static_cast (value); - m_ledgerIndex = beast::ByteOrder::swapIfLittleEndian (*index); - } - // VFALCO NOTE What about bytes 4 through 7 inclusive? if (valueBytes > 8) @@ -90,7 +83,7 @@ NodeObject::Ptr DecodedBlob::createObject () Blob data(m_objectData, m_objectData + m_dataBytes); object = NodeObject::createObject ( - m_objectType, m_ledgerIndex, std::move(data), uint256::fromVoid(m_key)); + m_objectType, std::move(data), uint256::fromVoid(m_key)); } return object; diff --git a/src/ripple/nodestore/impl/DecodedBlob.h b/src/ripple/nodestore/impl/DecodedBlob.h index b46ad15bf9..2f692c442c 100644 --- a/src/ripple/nodestore/impl/DecodedBlob.h +++ b/src/ripple/nodestore/impl/DecodedBlob.h @@ -51,7 +51,6 @@ private: bool m_success; void const* m_key; - LedgerIndex m_ledgerIndex; NodeObjectType m_objectType; unsigned char const* m_objectData; int m_dataBytes; diff --git a/src/ripple/nodestore/impl/EncodedBlob.cpp b/src/ripple/nodestore/impl/EncodedBlob.cpp index db9fbfa4b6..67cf26f1e9 100644 --- a/src/ripple/nodestore/impl/EncodedBlob.cpp +++ b/src/ripple/nodestore/impl/EncodedBlob.cpp @@ -27,30 +27,27 @@ namespace NodeStore { void EncodedBlob::prepare (NodeObject::Ptr const& object) { - m_key = object->getHash ().begin (); + m_key = object->getHash().begin (); // This is how many bytes we need in the flat data m_size = object->getData ().size () + 9; - m_data.ensureSize (m_size); - - // These sizes must be the same! - static_assert (sizeof (std::uint32_t) == sizeof (object->getLedgerIndex ()), - "Ledger Indices must be exactly 32-bits long."); + m_data.ensureSize(m_size); { - std::uint32_t* buf = static_cast (m_data.getData ()); - - buf [0] = beast::ByteOrder::swapIfLittleEndian (object->getLedgerIndex ()); - buf [1] = beast::ByteOrder::swapIfLittleEndian (object->getLedgerIndex ()); + // these 8 bytes are unused + std::uint64_t* buf = static_cast < + std::uint64_t*>(m_data.getData ()); + *buf = 0; } { - unsigned char* buf = static_cast (m_data.getData ()); - - buf [8] = static_cast (object->getType ()); - - memcpy (&buf [9], object->getData ().data (), object->getData ().size ()); + unsigned char* buf = static_cast < + unsigned char*> (m_data.getData ()); + buf [8] = static_cast < + unsigned char> (object->getType ()); + memcpy (&buf [9], object->getData ().data(), + object->getData ().size()); } } diff --git a/src/ripple/nodestore/impl/NodeObject.cpp b/src/ripple/nodestore/impl/NodeObject.cpp index d760c4a551..97a1efa481 100644 --- a/src/ripple/nodestore/impl/NodeObject.cpp +++ b/src/ripple/nodestore/impl/NodeObject.cpp @@ -27,13 +27,11 @@ namespace ripple { NodeObject::NodeObject ( NodeObjectType type, - LedgerIndex ledgerIndex, Blob&& data, uint256 const& hash, PrivateAccess) : mType (type) , mHash (hash) - , mLedgerIndex (ledgerIndex) { mData = std::move (data); } @@ -41,12 +39,11 @@ NodeObject::NodeObject ( NodeObject::Ptr NodeObject::createObject ( NodeObjectType type, - LedgerIndex ledgerIndex, Blob&& data, uint256 const& hash) { return std::make_shared ( - type, ledgerIndex, std::move (data), hash, PrivateAccess ()); + type, std::move (data), hash, PrivateAccess ()); } NodeObjectType @@ -61,12 +58,6 @@ NodeObject::getHash () const return mHash; } -LedgerIndex -NodeObject::getLedgerIndex () const -{ - return mLedgerIndex; -} - Blob const& NodeObject::getData () const { @@ -82,9 +73,6 @@ NodeObject::isCloneOf (NodeObject::Ptr const& other) const if (mHash != other->mHash) return false; - if (mLedgerIndex != other->mLedgerIndex) - return false; - if (mData != other->mData) return false; diff --git a/src/ripple/nodestore/tests/Base.test.h b/src/ripple/nodestore/tests/Base.test.h index e74acac8a8..5df0fa933a 100644 --- a/src/ripple/nodestore/tests/Base.test.h +++ b/src/ripple/nodestore/tests/Base.test.h @@ -66,8 +66,6 @@ public: break; }; - LedgerIndex ledgerIndex = 1 + r.nextInt (1024 * 1024); - uint256 hash; r.fillBitsRandomly (hash.begin (), hash.size ()); @@ -77,7 +75,7 @@ public: r.fillBitsRandomly (data.data (), payloadBytes); - return NodeObject::createObject(type, ledgerIndex, std::move(data), hash); + return NodeObject::createObject(type, std::move(data), hash); } private: @@ -176,7 +174,6 @@ public: Blob data (object->getData ()); db.store (object->getType (), - object->getLedgerIndex (), std::move (data), object->getHash ()); } diff --git a/src/ripple/nodestore/tests/Timing.test.cpp b/src/ripple/nodestore/tests/Timing.test.cpp index 644a13b2fa..505575c654 100644 --- a/src/ripple/nodestore/tests/Timing.test.cpp +++ b/src/ripple/nodestore/tests/Timing.test.cpp @@ -84,7 +84,6 @@ private: beast::xor_shift_engine gen_; std::uint8_t prefix_; - std::uniform_int_distribution d_seq_; std::uniform_int_distribution d_type_; std::uniform_int_distribution d_size_; @@ -92,7 +91,6 @@ public: explicit Sequence(std::uint8_t prefix) : prefix_ (prefix) - , d_seq_ (minLedger, maxLedger) , d_type_ (hotLEDGER, hotTRANSACTION_NODE) , d_size_ (minSize, maxSize) { @@ -122,7 +120,7 @@ public: rngcpy (&value[0], value.size(), gen_); return NodeObject::createObject ( static_cast(d_type_(gen_)), - d_seq_(gen_), std::move(value), key); + std::move(value), key); } // returns a batch of NodeObjects starting at n diff --git a/src/ripple/overlay/impl/PeerImp.cpp b/src/ripple/overlay/impl/PeerImp.cpp index a2ffd9d71e..fea6818fd7 100644 --- a/src/ripple/overlay/impl/PeerImp.cpp +++ b/src/ripple/overlay/impl/PeerImp.cpp @@ -1401,8 +1401,7 @@ PeerImp::onMessage (std::shared_ptr const& m) if (obj.has_nodeid ()) newObj.set_index (obj.nodeid ()); - if (!reply.has_seq () && (hObj->getLedgerIndex () != 0)) - reply.set_seq (hObj->getLedgerIndex ()); + // VFALCO NOTE "seq" in the message is obsolete } } } diff --git a/src/ripple/shamap/impl/SHAMap.cpp b/src/ripple/shamap/impl/SHAMap.cpp index 0f36f8d321..ce719f4ba7 100644 --- a/src/ripple/shamap/impl/SHAMap.cpp +++ b/src/ripple/shamap/impl/SHAMap.cpp @@ -943,8 +943,8 @@ void SHAMap::writeNode ( Serializer s; node->addRaw (s, snfPREFIX); - db_.store (t, seq, - std::move (s.modData ()), node->getNodeHash ()); + db_.store (t, std::move (s.modData()), + node->getNodeHash ()); } // We can't modify an inner node someone else might have a From e825433a380024a61c7348c2e69bcdc6876c9d4f Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Fri, 6 Feb 2015 11:32:11 -0800 Subject: [PATCH 13/33] NuDB: Use nodeobject codec in Backend (RIPD-793): This adds codecs for snappy and lz4, and a new nodeobject codec. The nodeobject codec provides a highly efficient custom compression scheme for inner nodes, which make up the majority of nodestore databases. Non inner node objects are compressed using lz4. The NuDB backend is modified to use the nodeobject codec. This change is not backward compatible - older NuDB databases cannot be opened or imported. --- Builds/VisualStudio2013/RippleD.vcxproj | 2 + .../VisualStudio2013/RippleD.vcxproj.filters | 3 + src/ripple/nodestore/backend/NuDBFactory.cpp | 205 +------ src/ripple/nodestore/impl/DecodedBlob.cpp | 2 +- src/ripple/nodestore/impl/codec.h | 514 ++++++++++++++++++ 5 files changed, 532 insertions(+), 194 deletions(-) create mode 100644 src/ripple/nodestore/impl/codec.h diff --git a/Builds/VisualStudio2013/RippleD.vcxproj b/Builds/VisualStudio2013/RippleD.vcxproj index 3f37db1f04..4be04dbfd9 100644 --- a/Builds/VisualStudio2013/RippleD.vcxproj +++ b/Builds/VisualStudio2013/RippleD.vcxproj @@ -2555,6 +2555,8 @@ + + diff --git a/Builds/VisualStudio2013/RippleD.vcxproj.filters b/Builds/VisualStudio2013/RippleD.vcxproj.filters index 2b7bac64a6..29dcf94c24 100644 --- a/Builds/VisualStudio2013/RippleD.vcxproj.filters +++ b/Builds/VisualStudio2013/RippleD.vcxproj.filters @@ -3651,6 +3651,9 @@ ripple\nodestore\impl + + ripple\nodestore\impl + ripple\nodestore\impl diff --git a/src/ripple/nodestore/backend/NuDBFactory.cpp b/src/ripple/nodestore/backend/NuDBFactory.cpp index 6c37593d87..3d7c071177 100644 --- a/src/ripple/nodestore/backend/NuDBFactory.cpp +++ b/src/ripple/nodestore/backend/NuDBFactory.cpp @@ -21,14 +21,14 @@ #include #include +#include #include #include #include -#include // remove asap +#include #include #include #include -#include #include #include #include @@ -50,22 +50,11 @@ public: // distribution of data sizes. arena_alloc_size = 16 * 1024 * 1024, - // Version 1 - // No compression - // - typeOne = 1, - - // Version 2 - // Snappy compression - typeTwo = 2, - - - - currentType = typeTwo + currentType = 1 }; using api = beast::nudb::api< - beast::xxhasher, beast::nudb::identity_codec>; + beast::xxhasher, nodeobject_codec>; beast::Journal journal_; size_t const keyBytes_; @@ -137,74 +126,8 @@ public: } } - //-------------------------------------------------------------------------- - - class Buffer - { - private: - std::size_t size_ = 0; - std::size_t capacity_ = 0; - std::unique_ptr buf_; - - public: - Buffer() = default; - Buffer (Buffer const&) = delete; - Buffer& operator= (Buffer const&) = delete; - - explicit - Buffer (std::size_t n) - { - resize (n); - } - - std::size_t - size() const - { - return size_; - } - - std::size_t - capacity() const - { - return capacity_; - } - - void* - get() - { - return buf_.get(); - } - - void - resize (std::size_t n) - { - if (capacity_ < n) - { - capacity_ = beast::nudb::detail::ceil_pow2(n); - buf_.reset (new std::uint8_t[capacity_]); - } - size_ = n; - } - - // Meet the requirements of BufferFactory - void* - operator() (std::size_t n) - { - resize(n); - return get(); - } - }; - - //-------------------------------------------------------------------------- - // - // Version 1 Database - // - // Uncompressed - // - Status - fetch1 (void const* key, - std::shared_ptr * pno) + fetch (void const* key, NodeObject::Ptr* pno) { Status status; pno->reset(); @@ -226,94 +149,13 @@ public: return status; } - void - insert1 (void const* key, void const* data, - std::size_t size) - { - db_.insert (key, data, size); - } - - //-------------------------------------------------------------------------- - // - // Version 2 Database - // - // Snappy compression - // - - Status - fetch2 (void const* key, - std::shared_ptr * pno) - { - Status status; - pno->reset(); - if (! db_.fetch (key, - [&](void const* data, std::size_t size) - { - std::size_t actual; - if (! snappy::GetUncompressedLength( - (char const*)data, size, &actual)) - { - status = dataCorrupt; - return; - } - std::unique_ptr buf (new char[actual]); - snappy::RawUncompress ( - (char const*)data, size, buf.get()); - DecodedBlob decoded (key, buf.get(), actual); - if (! decoded.wasOk ()) - { - status = dataCorrupt; - return; - } - *pno = decoded.createObject(); - status = ok; - })) - { - return notFound; - } - - return status; - } - - void - insert2 (void const* key, void const* data, - std::size_t size) - { - std::unique_ptr buf ( - new char[snappy::MaxCompressedLength(size)]); - std::size_t actual; - snappy::RawCompress ((char const*)data, size, - buf.get(), &actual); - db_.insert (key, buf.get(), actual); - } - - //-------------------------------------------------------------------------- - - Status - fetch (void const* key, NodeObject::Ptr* pno) - { - switch (db_.appnum()) - { - case typeOne: return fetch1 (key, pno); - case typeTwo: return fetch2 (key, pno); - } - throw std::runtime_error( - "nodestore: unknown appnum"); - return notFound; - } - void do_insert (std::shared_ptr const& no) { EncodedBlob e; e.prepare (no); - switch (db_.appnum()) - { - case typeOne: return insert1 (e.getKey(), e.getData(), e.getSize()); - case typeTwo: return insert2 (e.getKey(), e.getData(), e.getSize()); - } - throw std::runtime_error( - "nodestore: unknown appnum"); + db_.insert (e.getKey(), + e.getData(), e.getSize()); } void @@ -352,40 +194,17 @@ public: auto const dp = db_.dat_path(); auto const kp = db_.key_path(); auto const lp = db_.log_path(); - auto const appnum = db_.appnum(); + //auto const appnum = db_.appnum(); db_.close(); api::visit (dp, [&]( void const* key, std::size_t key_bytes, void const* data, std::size_t size) { - switch (appnum) - { - case typeOne: - { - DecodedBlob decoded (key, data, size); - if (! decoded.wasOk ()) - return false; - f (decoded.createObject()); - break; - } - case typeTwo: - { - std::size_t actual; - if (! snappy::GetUncompressedLength( - (char const*)data, size, &actual)) - return false; - std::unique_ptr buf (new char[actual]); - if (! snappy::RawUncompress ((char const*)data, - size, buf.get())) - return false; - DecodedBlob decoded (key, buf.get(), actual); - if (! decoded.wasOk ()) - return false; - f (decoded.createObject()); - break; - } - } + DecodedBlob decoded (key, data, size); + if (! decoded.wasOk ()) + return false; + f (decoded.createObject()); return true; }); db_.open (dp, kp, lp, diff --git a/src/ripple/nodestore/impl/DecodedBlob.cpp b/src/ripple/nodestore/impl/DecodedBlob.cpp index e16268ef02..8fbb914c8c 100644 --- a/src/ripple/nodestore/impl/DecodedBlob.cpp +++ b/src/ripple/nodestore/impl/DecodedBlob.cpp @@ -58,10 +58,10 @@ DecodedBlob::DecodedBlob (void const* key, void const* value, int valueBytes) switch (m_objectType) { - case hotUNKNOWN: default: break; + case hotUNKNOWN: case hotLEDGER: case hotTRANSACTION: case hotACCOUNT_NODE: diff --git a/src/ripple/nodestore/impl/codec.h b/src/ripple/nodestore/impl/codec.h new file mode 100644 index 0000000000..2db493b92b --- /dev/null +++ b/src/ripple/nodestore/impl/codec.h @@ -0,0 +1,514 @@ +//------------------------------------------------------------------------------ +/* + This file is part of rippled: https://github.com/ripple/rippled + Copyright (c) 2012, 2013 Ripple Labs Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef RIPPLE_NODESTORE_CODEC_H_INCLUDED +#define RIPPLE_NODESTORE_CODEC_H_INCLUDED + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace ripple { +namespace NodeStore { + +namespace detail { + +template +std::pair +snappy_compress (void const* in, + std::size_t in_size, BufferFactory&& bf) +{ + std::pair result; + auto const out_max = + snappy::MaxCompressedLength(in_size); + void* const out = bf(out_max); + result.first = out; + snappy::RawCompress( + reinterpret_cast(in), + in_size, reinterpret_cast(out), + &result.second); + return result; +} + +template +std::pair +snappy_decompress (void const* in, + std::size_t in_size, BufferFactory&& bf) +{ + std::pair result; + if (! snappy::GetUncompressedLength( + reinterpret_cast(in), + in_size, &result.second)) + throw beast::nudb::codec_error( + "snappy decompress"); + void* const out = bf(result.second); + result.first = out; + if (! snappy::RawUncompress( + reinterpret_cast(in), in_size, + reinterpret_cast(out))) + throw beast::nudb::codec_error( + "snappy decompress"); + return result; +} + +template +std::pair +lz4_decompress (void const* in, + std::size_t in_size, BufferFactory&& bf) +{ + using beast::nudb::codec_error; + using namespace beast::nudb::detail; + std::pair result; + std::uint8_t const* p = reinterpret_cast< + std::uint8_t const*>(in); + auto const n = read_varint( + p, in_size, result.second); + if (n == 0) + throw codec_error( + "lz4 decompress"); + void* const out = bf(result.second); + result.first = out; + if (LZ4_decompress_fast( + reinterpret_cast(in) + n, + reinterpret_cast(out), + result.second) + n != in_size) + throw codec_error( + "lz4 decompress"); + return result; +} + +template +std::pair +lz4_compress (void const* in, + std::size_t in_size, BufferFactory&& bf) +{ + using beast::nudb::codec_error; + using namespace beast::nudb::detail; + std::pair result; + std::array::max> vi; + auto const n = write_varint( + vi.data(), in_size); + auto const out_max = + LZ4_compressBound(in_size); + std::uint8_t* out = reinterpret_cast< + std::uint8_t*>(bf(n + out_max)); + result.first = out; + std::memcpy(out, vi.data(), n); + auto const out_size = LZ4_compress( + reinterpret_cast(in), + reinterpret_cast(out + n), + in_size); + if (out_size == 0) + throw codec_error( + "lz4 compress"); + result.second = n + out_size; + return result; +} + +//------------------------------------------------------------------------------ + +/* + object types: + + 0 = Uncompressed + 1 = lz4 compressed + 2 = inner node compressed + 3 = full inner node +*/ + +template +std::pair +nodeobject_decompress (void const* in, + std::size_t in_size, BufferFactory&& bf) +{ + using beast::nudb::codec_error; + using namespace beast::nudb::detail; + + std::uint8_t const* p = reinterpret_cast< + std::uint8_t const*>(in); + std::size_t type; + auto const vn = read_varint( + p, in_size, type); + if (vn == 0) + throw codec_error( + "nodeobject decompress"); + p += vn; + in_size -= vn; + + std::pair result; + switch(type) + { + case 0: // uncompressed + { + result.first = p; + result.second = in_size; + break; + } + case 1: // lz4 + { + result = lz4_decompress( + p, in_size, bf); + break; + } + case 2: // inner node + { + auto const hs = + field::size; // Mask + if (in_size < hs + 32) + throw codec_error( + "nodeobject codec: short inner node"); + istream is(p, in_size); + std::uint16_t mask; + read(is, mask); // Mask + in_size -= hs; + result.second = 525; + void* const out = bf(result.second); + result.first = out; + ostream os(out, result.second); + write(os, 0); + write(os, 0); + write (os, hotUNKNOWN); + write(os, HashPrefix::innerNode); + if (mask == 0) + throw codec_error( + "nodeobject codec: empty inner node"); + std::uint16_t bit = 0x8000; + for (int i = 16; i--; bit >>= 1) + { + if (mask & bit) + { + if (in_size < 32) + throw codec_error( + "nodeobject codec: short inner node"); + std::memcpy(os.data(32), is(32), 32); + in_size -= 32; + } + else + { + std::memset(os.data(32), 0, 32); + } + } + if (in_size > 0) + throw codec_error( + "nodeobject codec: long inner node"); + break; + } + case 3: // full inner node + { + if (in_size != 16 * 32) // hashes + throw codec_error( + "nodeobject codec: short full inner node"); + istream is(p, in_size); + result.second = 525; + void* const out = bf(result.second); + result.first = out; + ostream os(out, result.second); + write(os, 0); + write(os, 0); + write (os, hotUNKNOWN); + write(os, HashPrefix::innerNode); + write(os, is(512), 512); + break; + } + default: + throw codec_error( + "nodeobject codec: bad type=" + + std::to_string(type)); + }; + return result; +} + +template +void const* +zero32() +{ + static std::array v = + [] + { + std::array v; + v.fill(0); + return v; + }(); + return v.data(); +} + +template +std::pair +nodeobject_compress (void const* in, + std::size_t in_size, BufferFactory&& bf) +{ + using beast::nudb::codec_error; + using namespace beast::nudb::detail; + + std::size_t type = 1; + // Check for inner node + if (in_size == 525) + { + istream is(in, in_size); + std::uint32_t index; + std::uint32_t unused; + std::uint8_t kind; + std::uint32_t prefix; + read(is, index); + read(is, unused); + read (is, kind); + read(is, prefix); + if (prefix == HashPrefix::innerNode) + { + std::size_t n = 0; + std::uint16_t mask = 0; + std::array< + std::uint8_t, 512> vh; + for (unsigned bit = 0x8000; + bit; bit >>= 1) + { + void const* const h = is(32); + if (std::memcmp( + h, zero32(), 32) == 0) + continue; + std::memcpy( + vh.data() + 32 * n, h, 32); + mask |= bit; + ++n; + } + std::pair result; + if (n < 16) + { + // 2 = inner node compressed + auto const type = 2U; + auto const vs = size_varint(type); + result.second = + vs + + field::size + // mask + n * 32; // hashes + std::uint8_t* out = reinterpret_cast< + std::uint8_t*>(bf(result.second)); + result.first = out; + ostream os(out, result.second); + write(os, type); + write(os, mask); + write(os, vh.data(), n * 32); + return result; + } + // 3 = full inner node + auto const type = 3U; + auto const vs = size_varint(type); + result.second = + vs + + n * 32; // hashes + std::uint8_t* out = reinterpret_cast< + std::uint8_t*>(bf(result.second)); + result.first = out; + ostream os(out, result.second); + write(os, type); + write(os, vh.data(), n * 32); + return result; + } + } + + std::array::max> vi; + auto const vn = write_varint( + vi.data(), type); + std::pair result; + switch(type) + { + case 0: // uncompressed + { + result.second = vn + in_size; + std::uint8_t* p = reinterpret_cast< + std::uint8_t*>(bf(result.second)); + result.first = p; + std::memcpy(p, vi.data(), vn); + std::memcpy(p + vn, in, in_size); + break; + } + case 1: // lz4 + { + std::uint8_t* p; + auto const lzr = lz4_compress( + in, in_size, [&p, &vn, &bf] + (std::size_t n) + { + p = reinterpret_cast< + std::uint8_t*>( + bf(vn + n)); + return p + vn; + }); + std::memcpy(p, vi.data(), vn); + result.first = p; + result.second = vn + lzr.second; + break; + } + default: + throw std::logic_error( + "nodeobject codec: unknown=" + + std::to_string(type)); + }; + return result; +} + +} // detail + +// Modifies an inner node to erase the ledger +// sequence and type information so the codec +// verification can pass. +// +template +void +filter_inner (void* in, std::size_t in_size) +{ + using beast::nudb::codec_error; + using namespace beast::nudb::detail; + + // Check for inner node + if (in_size == 525) + { + istream is(in, in_size); + std::uint32_t index; + std::uint32_t unused; + std::uint8_t kind; + std::uint32_t prefix; + read(is, index); + read(is, unused); + read (is, kind); + read(is, prefix); + if (prefix == HashPrefix::innerNode) + { + ostream os(in, 9); + write(os, 0); + write(os, 0); + write (os, hotUNKNOWN); + } + } +} + +//------------------------------------------------------------------------------ + +class snappy_codec +{ +public: + template + explicit + snappy_codec(Args&&... args) + { + } + + char const* + name() const + { + return "snappy"; + } + + template + std::pair + compress (void const* in, + std::size_t in_size, BufferFactory&& bf) const + { + return snappy_compress(in, in_size, bf); + } + + template + std::pair + decompress (void const* in, + std::size_t in_size, BufferFactory&& bf) const + { + return snappy_decompress(in, in_size, bf); + } +}; + +class lz4_codec +{ +public: + template + explicit + lz4_codec(Args&&... args) + { + } + + char const* + name() const + { + return "lz4"; + } + + template + std::pair + decompress (void const* in, + std::size_t in_size, BufferFactory&& bf) const + { + return lz4_compress(in, in_size, bf); + } + + template + std::pair + compress (void const* in, + std::size_t in_size, BufferFactory&& bf) const + { + return lz4_compress(in, in_size, bf); + } +}; + +class nodeobject_codec +{ +public: + template + explicit + nodeobject_codec(Args&&... args) + { + } + + char const* + name() const + { + return "nodeobject"; + } + + template + std::pair + decompress (void const* in, + std::size_t in_size, BufferFactory&& bf) const + { + return detail::nodeobject_decompress( + in, in_size, bf); + } + + template + std::pair + compress (void const* in, + std::size_t in_size, BufferFactory&& bf) const + { + return detail::nodeobject_compress( + in, in_size, bf); + } +}; + +} // NodeStore +} // ripple + +#endif From a25508b98d96355b8f47a6b6ea2452ec7a40527e Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sat, 7 Feb 2015 07:01:13 -0800 Subject: [PATCH 14/33] Add RocksDB to nudb import tool (RIPD-781,785): This custom tool is specifically designed for very fast import of RocksDB nodestore databases into NuDB. --- Builds/VisualStudio2013/RippleD.vcxproj | 3 + .../VisualStudio2013/RippleD.vcxproj.filters | 3 + src/ripple/nodestore/tests/import_test.cpp | 1033 +++++++++++++++++ src/ripple/unity/nodestore.cpp | 2 + 4 files changed, 1041 insertions(+) create mode 100644 src/ripple/nodestore/tests/import_test.cpp diff --git a/Builds/VisualStudio2013/RippleD.vcxproj b/Builds/VisualStudio2013/RippleD.vcxproj index 4be04dbfd9..a88ddd4951 100644 --- a/Builds/VisualStudio2013/RippleD.vcxproj +++ b/Builds/VisualStudio2013/RippleD.vcxproj @@ -2606,6 +2606,9 @@ True + + True + True diff --git a/Builds/VisualStudio2013/RippleD.vcxproj.filters b/Builds/VisualStudio2013/RippleD.vcxproj.filters index 29dcf94c24..abf3bc1f20 100644 --- a/Builds/VisualStudio2013/RippleD.vcxproj.filters +++ b/Builds/VisualStudio2013/RippleD.vcxproj.filters @@ -3714,6 +3714,9 @@ ripple\nodestore\tests + + ripple\nodestore\tests + ripple\nodestore\tests diff --git a/src/ripple/nodestore/tests/import_test.cpp b/src/ripple/nodestore/tests/import_test.cpp new file mode 100644 index 0000000000..979eace298 --- /dev/null +++ b/src/ripple/nodestore/tests/import_test.cpp @@ -0,0 +1,1033 @@ +//------------------------------------------------------------------------------ +/* + This file is part of rippled: https://github.com/ripple/rippled + Copyright (c) 2012, 2013 Ripple Labs Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#if ! RIPPLE_ROCKSDB_AVAILABLE +# include +#endif + +/* + +Math: + +1000 gb dat file +170 gb key file +capacity 113 keys/bucket + +normal: +1,000gb data file read +19,210gb key file read (113 * 170) +19,210gb key file write + +multi(32gb): +6 passes (170/32) +6,000gb data file read +170gb key file write + + +*/ + +namespace ripple { +namespace NodeStore { + +namespace detail { + +class save_stream_state +{ + std::ostream& os_; + std::streamsize precision_; + std::ios::fmtflags flags_; + std::ios::char_type fill_; +public: + ~save_stream_state() + { + os_.precision(precision_); + os_.flags(flags_); + os_.fill(fill_); + } + save_stream_state(save_stream_state const&) = delete; + save_stream_state& operator=(save_stream_state const&) = delete; + explicit save_stream_state(std::ostream& os) + : os_(os) + , precision_(os.precision()) + , flags_(os.flags()) + , fill_(os.fill()) + { + } +}; + +template +std::ostream& +pretty_time(std::ostream& os, std::chrono::duration d) +{ + save_stream_state _(os); + using namespace std::chrono; + if (d < microseconds{1}) + { + // use nanoseconds + if (d < nanoseconds{100}) + { + // use floating + using ns = duration; + os << std::fixed << std::setprecision(1) << ns(d).count(); + } + else + { + // use integral + os << std::chrono::round(d).count(); + } + os << "ns"; + } + else if (d < milliseconds{1}) + { + // use microseconds + if (d < microseconds{100}) + { + // use floating + using ms = duration; + os << std::fixed << std::setprecision(1) << ms(d).count(); + } + else + { + // use integral + os << std::chrono::round(d).count(); + } + os << "us"; + } + else if (d < seconds{1}) + { + // use milliseconds + if (d < milliseconds{100}) + { + // use floating + using ms = duration; + os << std::fixed << std::setprecision(1) << ms(d).count(); + } + else + { + // use integral + os << std::chrono::round(d).count(); + } + os << "ms"; + } + else if (d < minutes{1}) + { + // use seconds + if (d < seconds{100}) + { + // use floating + using s = duration; + os << std::fixed << std::setprecision(1) << s(d).count(); + } + else + { + // use integral + os << std::chrono::round(d).count(); + } + os << "s"; + } + else + { + // use minutes + if (d < minutes{100}) + { + // use floating + using m = duration>; + os << std::fixed << std::setprecision(1) << m(d).count(); + } + else + { + // use integral + os << std::chrono::round(d).count(); + } + os << "min"; + } + return os; +} + +template +inline +std::string +fmtdur(std::chrono::duration const& d) +{ + std::stringstream ss; + pretty_time(ss, d); + return ss.str(); +} + +} // detail + +//------------------------------------------------------------------------------ + +class progress +{ +private: + using clock_type = + beast::basic_seconds_clock< + std::chrono::steady_clock>; + + std::size_t const work_; + clock_type::time_point start_ = clock_type::now(); + clock_type::time_point now_ = clock_type::now(); + clock_type::time_point report_ = clock_type::now(); + std::size_t prev_ = 0; + bool estimate_ = false; + +public: + explicit + progress(std::size_t work) + : work_(work) + { + } + + template + void + operator()(Log& log, std::size_t work) + { + using namespace std::chrono; + auto const now = clock_type::now(); + if (now == now_) + return; + now_ = now; + auto const elapsed = now - start_; + if (! estimate_) + { + if (elapsed < seconds(15)) + return; + estimate_ = true; + } + else if (now - report_ < + std::chrono::seconds(60)) + { + return; + } + auto const rate = + elapsed.count() / double(work); + clock_type::duration const remain( + static_cast( + (work_ - work) * rate)); + log << + "Remaining: " << detail::fmtdur(remain) << + " (" << work << " of " << work_ << + " in " << detail::fmtdur(elapsed) << + ", " << (work - prev_) << + " in " << detail::fmtdur(now - report_) << + ")"; + report_ = now; + prev_ = work; + } + + template + void + finish(Log& log) + { + log << + "Total time: " << detail::fmtdur( + clock_type::now() - start_); + } +}; + +std::map +parse_args(std::string const& s) +{ + // '=' + static boost::regex const re1 ( + "^" // start of line + "(?:\\s*)" // whitespace (optonal) + "([a-zA-Z][_a-zA-Z0-9]*)" // + "(?:\\s*)" // whitespace (optional) + "(?:=)" // '=' + "(?:\\s*)" // whitespace (optional) + "(.*\\S+)" // + "(?:\\s*)" // whitespace (optional) + , boost::regex_constants::optimize + ); + std::map map; + auto const v = beast::rfc2616::split( + s.begin(), s.end(), ','); + for (auto const& kv : v) + { + boost::smatch m; + if (! boost::regex_match (kv, m, re1)) + throw std::runtime_error( + "invalid parameter " + kv); + auto const result = + map.emplace(m[1], m[2]); + if (! result.second) + throw std::runtime_error( + "duplicate parameter " + m[1]); + } + return map; +} + +//------------------------------------------------------------------------------ + +class import_test : public beast::unit_test::suite +{ +public: + void + run() override + { + testcase(abort_on_fail) << arg(); + + using namespace beast::nudb; + using namespace beast::nudb::detail; + + pass(); + auto const args = parse_args(arg()); + bool usage = args.empty(); + + if (! usage && + args.find("from") == args.end()) + { + log << + "Missing parameter: from"; + usage = true; + } + if (! usage && + args.find("to") == args.end()) + { + log << + "Missing parameter: to"; + usage = true; + } + if (! usage && + args.find("buffer") == args.end()) + { + log << + "Missing parameter: buffer"; + usage = true; + } + + if (usage) + { + log << + "Usage:\n" << + "--unittest-arg=from=,to=,buffer=\n" << + "from: RocksDB database to import from\n" << + "to: NuDB database to import to\n" << + "buffer: Buffer size (bigger is faster)\n" << + "NuDB database must not already exist."; + return; + } + + // This controls the size of the bucket buffer. + // For a 1TB data file, a 32GB bucket buffer is suggested. + // The larger the buffer, the faster the import. + // + std::size_t const buffer_size = + std::stoull(args.at("buffer")); + auto const from_path = args.at("from"); + auto const to_path = args.at("to"); + + using hash_type = beast::xxhasher; + using codec_type = nodeobject_codec; + auto const bulk_size = 64 * 1024 * 1024; + float const load_factor = 0.5; + + auto const dp = to_path + ".dat"; + auto const kp = to_path + ".key"; + + auto const start = + std::chrono::steady_clock::now(); + + log << + "from: " << from_path << "\n" + "to: " << to_path << "\n" + "buffer: " << buffer_size; + + #if RIPPLE_ROCKSDB_AVAILABLE + std::unique_ptr db; + { + rocksdb::Options options; + options.create_if_missing = false; + options.max_open_files = 2000; // 5000? + rocksdb::DB* pdb = nullptr; + rocksdb::Status status = + rocksdb::DB::OpenForReadOnly( + options, from_path, &pdb); + if (! status.ok () || ! pdb) + throw std::runtime_error ( + "Can't open '" + from_path + "': " + + status.ToString()); + db.reset(pdb); + } + #else + std::unique_ptr db; + { + leveldb::Options options; + options.create_if_missing = false; + options.max_open_files = 2000; // 5000? + leveldb::DB* pdb = nullptr; + leveldb::Status status = + leveldb::DB::Open (options, from_path, &pdb); + if (! status.ok () || ! pdb) + throw std::runtime_error ( + "Can't open '" + from_path + "': " + + status.ToString()); + db.reset(pdb); + } + #endif + // Create data file with values + std::size_t nitems = 0; + std::size_t nbytes = 0; + dat_file_header dh; + dh.version = currentVersion; + dh.uid = make_uid(); + dh.appnum = 1; + dh.key_size = 32; + + native_file df; + df.create(file_mode::append, dp); + bulk_writer dw( + df, 0, bulk_size); + { + { + auto os = dw.prepare(dat_file_header::size); + write(os, dh); + } + #if RIPPLE_ROCKSDB_AVAILABLE + rocksdb::ReadOptions options; + options.verify_checksums = false; + options.fill_cache = false; + std::unique_ptr it( + db->NewIterator(options)); + #else + leveldb::ReadOptions options; + options.verify_checksums = false; + options.fill_cache = false; + std::unique_ptr it( + db->NewIterator(options)); + #endif + + buffer buf; + codec_type codec; + for (it->SeekToFirst (); it->Valid (); it->Next()) + { + if (it->key().size() != 32) + throw std::runtime_error( + "Unexpected key size " + + std::to_string(it->key().size())); + void const* const key = it->key().data(); + void const* const data = it->value().data(); + auto const size = it->value().size(); + std::unique_ptr clean( + new char[size]); + std::memcpy(clean.get(), data, size); + filter_inner(clean.get(), size); + auto const out = codec.compress( + clean.get(), size, buf); + // Verify codec correctness + { + buffer buf2; + auto const check = codec.decompress( + out.first, out.second, buf2); + expect(check.second == size, + "codec size error"); + expect(std::memcmp( + check.first, clean.get(), size) == 0, + "codec data error"); + } + // Data Record + auto os = dw.prepare( + field::size + // Size + 32 + // Key + out.second); + write(os, out.second); + std::memcpy(os.data(32), key, 32); + std::memcpy(os.data(out.second), + out.first, out.second); + ++nitems; + nbytes += size; + } + dw.flush(); + } + db.reset(); + log << + "Import data: " << detail::fmtdur( + std::chrono::steady_clock::now() - start); + auto const df_size = + df.actual_size(); + // Create key file + key_file_header kh; + kh.version = currentVersion; + kh.uid = dh.uid; + kh.appnum = dh.appnum; + kh.key_size = 32; + kh.salt = make_salt(); + kh.pepper = pepper(kh.salt); + kh.block_size = block_size(kp); + kh.load_factor = std::min( + 65536.0 * load_factor, 65535); + kh.buckets = std::ceil(nitems / (bucket_capacity( + kh.block_size) * load_factor)); + kh.modulus = ceil_pow2(kh.buckets); + native_file kf; + kf.create(file_mode::append, kp); + buffer buf(kh.block_size); + { + std::memset(buf.get(), 0, kh.block_size); + ostream os(buf.get(), kh.block_size); + write(os, kh); + kf.write(0, buf.get(), kh.block_size); + } + // Build contiguous sequential sections of the + // key file using multiple passes over the data. + // + auto const buckets = std::max(1, + buffer_size / kh.block_size); + buf.reserve(buckets * kh.block_size); + auto const passes = + (kh.buckets + buckets - 1) / buckets; + log << + "items: " << nitems << "\n" + "buckets: " << kh.buckets << "\n" + "data: " << df_size << "\n" + "passes: " << passes; + progress p(df_size * passes); + std::size_t npass = 0; + for (std::size_t b0 = 0; b0 < kh.buckets; + b0 += buckets) + { + auto const b1 = std::min( + b0 + buckets, kh.buckets); + // Buffered range is [b0, b1) + auto const bn = b1 - b0; + // Create empty buckets + for (std::size_t i = 0; i < bn; ++i) + { + bucket b(kh.block_size, + buf.get() + i * kh.block_size, + empty); + } + // Insert all keys into buckets + // Iterate Data File + bulk_reader r( + df, dat_file_header::size, + df_size, bulk_size); + while (! r.eof()) + { + auto const offset = r.offset(); + // Data Record or Spill Record + std::size_t size; + auto is = r.prepare( + field::size); // Size + read(is, size); + if (size > 0) + { + // Data Record + is = r.prepare( + dh.key_size + // Key + size); // Data + std::uint8_t const* const key = + is.data(dh.key_size); + auto const h = hash( + key, kh.key_size, kh.salt); + auto const n = bucket_index( + h, kh.buckets, kh.modulus); + p(log, + npass * df_size + r.offset()); + if (n < b0 || n >= b1) + continue; + bucket b(kh.block_size, buf.get() + + (n - b0) * kh.block_size); + maybe_spill(b, dw); + b.insert(offset, size, h); + } + else + { + // VFALCO Should never get here + // Spill Record + is = r.prepare( + field::size); + read(is, size); // Size + r.prepare(size); // skip + } + } + kf.write((b0 + 1) * kh.block_size, + buf.get(), bn * kh.block_size); + ++npass; + } + dw.flush(); + p.finish(log); + } +}; + +BEAST_DEFINE_TESTSUITE_MANUAL(import,NodeStore,ripple); + +//------------------------------------------------------------------------------ + +class rekey_test : public beast::unit_test::suite +{ +public: + void + run() override + { + testcase(abort_on_fail) << arg(); + + using namespace beast::nudb; + using namespace beast::nudb::detail; + + pass(); + auto const args = parse_args(arg()); + bool usage = args.empty(); + + if (! usage && + args.find("path") == args.end()) + { + log << + "Missing parameter: path"; + usage = true; + } + if (! usage && + args.find("items") == args.end()) + { + log << + "Missing parameter: items"; + usage = true; + } + if (! usage && + args.find("buffer") == args.end()) + { + log << + "Missing parameter: buffer"; + usage = true; + } + + if (usage) + { + log << + "Usage:\n" << + "--unittest-arg=path=,items=,buffer=\n" << + "path: NuDB path to rekey (without the .dat)\n" << + "items: Number of items in the .dat file\n" << + "buffer: Buffer size (bigger is faster)\n" << + "NuDB key file must not already exist."; + return; + } + + std::size_t const buffer_size = + std::stoull(args.at("buffer")); + auto const path = args.at("path"); + std::size_t const items = + std::stoull(args.at("items")); + + using hash_type = beast::xxhasher; + auto const bulk_size = 64 * 1024 * 1024; + float const load_factor = 0.5; + + auto const dp = path + ".dat"; + auto const kp = path + ".key"; + + auto const start = + std::chrono::steady_clock::now(); + + log << + "path: " << path << "\n" + "items: " << items << "\n" + "buffer: " << buffer_size; + + // Create data file with values + native_file df; + df.open(file_mode::append, dp); + dat_file_header dh; + read(df, dh); + auto const df_size = df.actual_size(); + bulk_writer dw( + df, df_size, bulk_size); + + // Create key file + key_file_header kh; + kh.version = currentVersion; + kh.uid = dh.uid; + kh.appnum = dh.appnum; + kh.key_size = 32; + kh.salt = make_salt(); + kh.pepper = pepper(kh.salt); + kh.block_size = block_size(kp); + kh.load_factor = std::min( + 65536.0 * load_factor, 65535); + kh.buckets = std::ceil(items / (bucket_capacity( + kh.block_size) * load_factor)); + kh.modulus = ceil_pow2(kh.buckets); + native_file kf; + kf.create(file_mode::append, kp); + buffer buf(kh.block_size); + { + std::memset(buf.get(), 0, kh.block_size); + ostream os(buf.get(), kh.block_size); + write(os, kh); + kf.write(0, buf.get(), kh.block_size); + } + // Build contiguous sequential sections of the + // key file using multiple passes over the data. + // + auto const buckets = std::max(1, + buffer_size / kh.block_size); + buf.reserve(buckets * kh.block_size); + auto const passes = + (kh.buckets + buckets - 1) / buckets; + log << + "buckets: " << kh.buckets << "\n" + "data: " << df_size << "\n" + "passes: " << passes; + progress p(df_size * passes); + std::size_t npass = 0; + for (std::size_t b0 = 0; b0 < kh.buckets; + b0 += buckets) + { + auto const b1 = std::min( + b0 + buckets, kh.buckets); + // Buffered range is [b0, b1) + auto const bn = b1 - b0; + // Create empty buckets + for (std::size_t i = 0; i < bn; ++i) + { + bucket b(kh.block_size, + buf.get() + i * kh.block_size, + empty); + } + // Insert all keys into buckets + // Iterate Data File + bulk_reader r( + df, dat_file_header::size, + df_size, bulk_size); + while (! r.eof()) + { + auto const offset = r.offset(); + // Data Record or Spill Record + std::size_t size; + auto is = r.prepare( + field::size); // Size + read(is, size); + if (size > 0) + { + // Data Record + is = r.prepare( + dh.key_size + // Key + size); // Data + std::uint8_t const* const key = + is.data(dh.key_size); + auto const h = hash( + key, dh.key_size, kh.salt); + auto const n = bucket_index( + h, kh.buckets, kh.modulus); + p(log, + npass * df_size + r.offset()); + if (n < b0 || n >= b1) + continue; + bucket b(kh.block_size, buf.get() + + (n - b0) * kh.block_size); + maybe_spill(b, dw); + b.insert(offset, size, h); + } + else + { + // VFALCO Should never get here + // Spill Record + is = r.prepare( + field::size); + read(is, size); // Size + r.prepare(size); // skip + } + } + kf.write((b0 + 1) * kh.block_size, + buf.get(), bn * kh.block_size); + ++npass; + } + dw.flush(); + p.finish(log); + } +}; + +BEAST_DEFINE_TESTSUITE_MANUAL(rekey,NodeStore,ripple); + +//------------------------------------------------------------------------------ + +namespace legacy { + +using namespace beast::nudb; +using namespace beast::nudb::detail; + +struct dat_file_header +{ + static std::size_t BEAST_CONSTEXPR size = + 8 + // Type + 2 + // Version + 8 + // Appnum + 8 + // Salt + 2 + // KeySize + 64; // (Reserved) + + char type[8]; + std::size_t version; + std::uint64_t appnum; + std::uint64_t salt; + std::size_t key_size; +}; + +struct key_file_header +{ + static std::size_t BEAST_CONSTEXPR size = + 8 + // Type + 2 + // Version + 8 + // Appnum + 8 + // Salt + 8 + // Pepper + 2 + // KeySize + 2 + // BlockSize + 2 + // LoadFactor + 64; // (Reserved) + + char type[8]; + std::size_t version; + std::uint64_t appnum; + std::uint64_t salt; + std::uint64_t pepper; + std::size_t key_size; + std::size_t block_size; + std::size_t load_factor; + + // Computed values + std::size_t capacity; + std::size_t bucket_size; + std::size_t buckets; + std::size_t modulus; +}; + +// Read data file header from stream +template +void +read (istream& is, dat_file_header& dh) +{ + read (is, dh.type, sizeof(dh.type)); + read(is, dh.version); + read(is, dh.appnum); + read(is, dh.salt); + read(is, dh.key_size); + std::array zero; + read (is, zero.data(), zero.size()); +} + +// Read data file header from file +template +void +read (File& f, dat_file_header& dh) +{ + std::array buf; + try + { + f.read(0, buf.data(), buf.size()); + } + catch (file_short_read_error const&) + { + throw store_corrupt_error( + "short data file header"); + } + istream is(buf); + read (is, dh); +} + +// Read key file header from stream +template +void +read (istream& is, std::size_t file_size, + key_file_header& kh) +{ + read(is, kh.type, sizeof(kh.type)); + read(is, kh.version); + read(is, kh.appnum); + read(is, kh.salt); + read(is, kh.pepper); + read(is, kh.key_size); + read(is, kh.block_size); + read(is, kh.load_factor); + std::array zero; + read (is, zero.data(), zero.size()); + + // VFALCO These need to be checked to handle + // when the file size is too small + kh.capacity = bucket_capacity(kh.block_size); + kh.bucket_size = bucket_size(kh.capacity); + if (file_size > kh.block_size) + { + // VFALCO This should be handled elsewhere. + // we shouldn't put the computed fields in this header. + if (kh.block_size > 0) + kh.buckets = (file_size - kh.bucket_size) + / kh.block_size; + else + // VFALCO Corruption or logic error + kh.buckets = 0; + } + else + { + kh.buckets = 0; + } + kh.modulus = ceil_pow2(kh.buckets); +} + +// Read key file header from file +template +void +read (File& f, key_file_header& kh) +{ + std::array buf; + try + { + f.read(0, buf.data(), buf.size()); + } + catch (file_short_read_error const&) + { + throw store_corrupt_error( + "short key file header"); + } + istream is(buf); + read (is, f.actual_size(), kh); +} + +} // detail + +class update_test : public beast::unit_test::suite +{ +public: + void + run() override + { + testcase(abort_on_fail) << arg(); + + using namespace beast::nudb; + using namespace beast::nudb::detail; + + pass(); + auto const args = parse_args(arg()); + bool usage = args.empty(); + + if (! usage && + args.find("path") == args.end()) + { + log << + "Missing parameter: path"; + usage = true; + } + + if (usage) + { + log << + "Usage:\n" << + "--unittest-arg=path=\n" << + "path: NuDB path to update (without extensions)"; + return; + } + + auto const path = args.at("path"); + + using hash_type = beast::xxhasher; + + auto const dp = path + ".dat"; + auto const kp = path + ".key"; + + auto const start = + std::chrono::steady_clock::now(); + + log << + "path: " << path; + + native_file df; + native_file kf; + df.open(file_mode::write, dp); + kf.open(file_mode::write, kp); + legacy::dat_file_header dh0; + legacy::key_file_header kh0; + read(df, dh0); + read(kf, kh0); + + dat_file_header dh; + std::memcpy(dh.type, "nudb.dat", 8); + dh.version = dh0.version;; + dh.uid = make_uid(); + dh.appnum = dh0.appnum; + dh.key_size = dh0.key_size; + + key_file_header kh; + std::memcpy(kh.type, "nudb.key", 8); + kh.version = dh.version; + kh.uid = dh.uid; + kh.appnum = dh.appnum; + kh.key_size = dh.key_size; + kh.salt = kh0.salt; + kh.pepper = kh0.pepper; + kh.block_size = kh0.block_size; + kh.load_factor = kh0.load_factor; + + // VFALCO These need to be checked to handle + // when the file size is too small + kh.capacity = bucket_capacity(kh.block_size); + kh.bucket_size = bucket_size(kh.capacity); + auto const kf_size = kf.actual_size(); + if (kf_size > kh.block_size) + { + // VFALCO This should be handled elsewhere. + // we shouldn't put the computed fields + // in this header. + if (kh.block_size > 0) + kh.buckets = (kf_size - kh.bucket_size) + / kh.block_size; + else + // VFALCO Corruption or logic error + kh.buckets = 0; + } + else + { + kh.buckets = 0; + } + kh.modulus = ceil_pow2(kh.buckets); + verify(dh); + verify(dh, kh); + write(df, dh); + write(kf, kh); + } +}; + +BEAST_DEFINE_TESTSUITE_MANUAL(update,NodeStore,ripple); + +} +} + +//--conf=D:\\config2\\rippled.cfg --unittest=ripple.NodeStore.rekey --unittest-arg=path=D:\config2\nudb,items=470772,buffer=67108864 diff --git a/src/ripple/unity/nodestore.cpp b/src/ripple/unity/nodestore.cpp index 46f89876ad..881a632191 100644 --- a/src/ripple/unity/nodestore.cpp +++ b/src/ripple/unity/nodestore.cpp @@ -41,4 +41,6 @@ #include #include #include +#include #include + From 0f94e2c0c320938243e8483af0b5c1356aa4dcbc Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sun, 8 Feb 2015 19:52:56 -0800 Subject: [PATCH 15/33] Set version to 0.27.1-rc1 --- Builds/rpm/rippled.spec | 2 +- src/ripple/protocol/impl/BuildInfo.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Builds/rpm/rippled.spec b/Builds/rpm/rippled.spec index f555bec7ce..d0808d945b 100644 --- a/Builds/rpm/rippled.spec +++ b/Builds/rpm/rippled.spec @@ -1,5 +1,5 @@ Name: rippled -Version: 0.27.0-sp1 +Version: 0.27.1-rc1 Release: 1%{?dist} Summary: Ripple peer-to-peer network daemon diff --git a/src/ripple/protocol/impl/BuildInfo.cpp b/src/ripple/protocol/impl/BuildInfo.cpp index 71d273413d..66c68148b5 100644 --- a/src/ripple/protocol/impl/BuildInfo.cpp +++ b/src/ripple/protocol/impl/BuildInfo.cpp @@ -35,7 +35,7 @@ char const* getRawVersionString () // // The build version number (edit this for each release) // - "0.27.0-sp1" + "0.27.1-rc1" // // Must follow the format described here: // From 8eb05d0950d539a261251f52c108b92528e5ae36 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Wed, 11 Feb 2015 20:52:49 -0800 Subject: [PATCH 16/33] Disable Overlay socket handoffs --- src/ripple/overlay/impl/OverlayImpl.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/ripple/overlay/impl/OverlayImpl.cpp b/src/ripple/overlay/impl/OverlayImpl.cpp index da1fadb427..f84365e946 100644 --- a/src/ripple/overlay/impl/OverlayImpl.cpp +++ b/src/ripple/overlay/impl/OverlayImpl.cpp @@ -194,10 +194,16 @@ OverlayImpl::onHandoff (std::unique_ptr && ssl_bundle, beast::Journal journal (sink); Handoff handoff; + +#if 1 + // VFALCO Disable handoffs + return handoff; +#else if (processRequest(request, handoff)) return handoff; if (! isPeerUpgrade(request)) return handoff; +#endif handoff.moved = true; From c20392ca8087dea431693ba701c2f285326d9ed8 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Wed, 11 Feb 2015 20:53:14 -0800 Subject: [PATCH 17/33] Set version to 0.27.1-rc2 --- Builds/rpm/rippled.spec | 2 +- src/ripple/protocol/impl/BuildInfo.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Builds/rpm/rippled.spec b/Builds/rpm/rippled.spec index d0808d945b..a672964dcf 100644 --- a/Builds/rpm/rippled.spec +++ b/Builds/rpm/rippled.spec @@ -1,5 +1,5 @@ Name: rippled -Version: 0.27.1-rc1 +Version: 0.27.1-rc2 Release: 1%{?dist} Summary: Ripple peer-to-peer network daemon diff --git a/src/ripple/protocol/impl/BuildInfo.cpp b/src/ripple/protocol/impl/BuildInfo.cpp index 66c68148b5..850ae8743c 100644 --- a/src/ripple/protocol/impl/BuildInfo.cpp +++ b/src/ripple/protocol/impl/BuildInfo.cpp @@ -35,7 +35,7 @@ char const* getRawVersionString () // // The build version number (edit this for each release) // - "0.27.1-rc1" + "0.27.1-rc2" // // Must follow the format described here: // From ba710bee8674fb880da0229777ab0cda4ac8110f Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Thu, 12 Feb 2015 09:21:07 -0800 Subject: [PATCH 18/33] Reject invalid requests on peer port sooner. --- src/ripple/server/impl/ServerHandlerImp.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/ripple/server/impl/ServerHandlerImp.cpp b/src/ripple/server/impl/ServerHandlerImp.cpp index 2fa74bb59a..07af7bd49f 100644 --- a/src/ripple/server/impl/ServerHandlerImp.cpp +++ b/src/ripple/server/impl/ServerHandlerImp.cpp @@ -184,6 +184,15 @@ void runCoroutine (RPC::Coroutine coroutine, JobQueue& jobQueue) void ServerHandlerImp::onRequest (HTTP::Session& session) { + // Make sure RPC is enabled on the port + if (session.port().protocol.count("http") == 0 && + session.port().protocol.count("https") == 0) + { + HTTPReply (403, "Forbidden", makeOutput (session)); + session.close (true); + return; + } + // Check user/password authorization if (! authorized (session.port(), build_map(session.request().headers))) From c24732ed4e06c0987f0d65e56e75181e571466c0 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Thu, 12 Feb 2015 12:36:18 -0800 Subject: [PATCH 19/33] Fix streambuf bug: The buffers_type::iterator could hold a pointer to a buffers_type that was destroyed. This changes buffers_type::iterator to point to the original streambuf instead, which always outlives the iterator. --- src/beast/beast/asio/streambuf.h | 54 ++++++++++++++++---------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/beast/beast/asio/streambuf.h b/src/beast/beast/asio/streambuf.h index 2e4a56c9da..7988838e5d 100644 --- a/src/beast/beast/asio/streambuf.h +++ b/src/beast/beast/asio/streambuf.h @@ -231,16 +231,13 @@ private: using argument_type = element; using result_type = value_type; - const_buffers_type const* buffers_; + basic_streambuf const* streambuf_ = nullptr; - transform() - : buffers_ (nullptr) - { - } + transform() = default; explicit - transform (const_buffers_type const& buffers) - : buffers_ (&buffers) + transform (basic_streambuf const& streambuf) + : streambuf_ (&streambuf) { } @@ -255,11 +252,15 @@ public: transform, typename list_type::const_iterator, value_type, value_type>; + const_buffers_type() = default; + const_buffers_type (const_buffers_type const&) = default; + const_buffers_type& operator= (const_buffers_type const&) = default; + const_iterator begin() const { return const_iterator (streambuf_->list_.begin(), - transform(*this)); + transform(*streambuf_)); } const_iterator @@ -267,7 +268,7 @@ public: { return const_iterator (streambuf_->out_ == streambuf_->list_.end() ? streambuf_->list_.end() : - std::next(streambuf_->out_), transform(*this)); + std::next(streambuf_->out_), transform(*streambuf_)); } private: @@ -290,12 +291,11 @@ basic_streambuf::const_buffers_type:: transform::operator() (element const& e) const -> value_type const { - basic_streambuf const& streambuf = *buffers_->streambuf_; return value_type (e.data(), - (streambuf.out_ == streambuf.list_.end() || - &e != &*streambuf.out_) ? e.size() : streambuf.out_pos_) + - (&e == &*streambuf.list_.begin() ? - streambuf.in_pos_ : 0); + (streambuf_->out_ == streambuf_->list_.end() || + &e != &*streambuf_->out_) ? e.size() : streambuf_->out_pos_) + + (&e == &*streambuf_->list_.begin() ? + streambuf_->in_pos_ : 0); } //------------------------------------------------------------------------------ @@ -312,16 +312,13 @@ private: using argument_type = element; using result_type = value_type; - mutable_buffers_type const* buffers_; + basic_streambuf const* streambuf_ = nullptr; - transform() - : buffers_ (nullptr) - { - } + transform() = default; explicit - transform (mutable_buffers_type const& buffers) - : buffers_ (&buffers) + transform (basic_streambuf const& streambuf) + : streambuf_ (&streambuf) { } @@ -336,18 +333,22 @@ public: transform, typename list_type::const_iterator, value_type, value_type>; + mutable_buffers_type() = default; + mutable_buffers_type (mutable_buffers_type const&) = default; + mutable_buffers_type& operator= (mutable_buffers_type const&) = default; + const_iterator begin() const { return const_iterator (streambuf_->out_, - transform(*this)); + transform(*streambuf_)); } const_iterator end() const { return const_iterator (streambuf_->list_.end(), - transform(*this)); + transform(*streambuf_)); } private: @@ -368,10 +369,9 @@ basic_streambuf::mutable_buffers_type:: transform::operator() (element const& e) const -> value_type const { - basic_streambuf const& streambuf = *buffers_->streambuf_; - return value_type (e.data(), &e == &*std::prev(streambuf.list_.end()) ? - streambuf.out_end_ : e.size()) + (&e == &*streambuf.out_ ? - streambuf.out_pos_ : 0); + return value_type (e.data(), &e == &*std::prev(streambuf_->list_.end()) ? + streambuf_->out_end_ : e.size()) + (&e == &*streambuf_->out_ ? + streambuf_->out_pos_ : 0); } //------------------------------------------------------------------------------ From 5dc064e9717fa492301a52d4adc09d33eab0c41d Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Thu, 12 Feb 2015 12:43:54 -0800 Subject: [PATCH 20/33] Revert "Disable Overlay socket handoffs" This reverts commit 8eb05d0950d539a261251f52c108b92528e5ae36. --- src/ripple/overlay/impl/OverlayImpl.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/ripple/overlay/impl/OverlayImpl.cpp b/src/ripple/overlay/impl/OverlayImpl.cpp index f84365e946..da1fadb427 100644 --- a/src/ripple/overlay/impl/OverlayImpl.cpp +++ b/src/ripple/overlay/impl/OverlayImpl.cpp @@ -194,16 +194,10 @@ OverlayImpl::onHandoff (std::unique_ptr && ssl_bundle, beast::Journal journal (sink); Handoff handoff; - -#if 1 - // VFALCO Disable handoffs - return handoff; -#else if (processRequest(request, handoff)) return handoff; if (! isPeerUpgrade(request)) return handoff; -#endif handoff.moved = true; From ac64731d55dda1650fa6939c6b22bc6a1cc9bfd9 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Thu, 12 Feb 2015 12:44:55 -0800 Subject: [PATCH 21/33] Set version to 0.27.1-rc3 --- Builds/rpm/rippled.spec | 2 +- src/ripple/protocol/impl/BuildInfo.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Builds/rpm/rippled.spec b/Builds/rpm/rippled.spec index a672964dcf..dcdb9e01a9 100644 --- a/Builds/rpm/rippled.spec +++ b/Builds/rpm/rippled.spec @@ -1,5 +1,5 @@ Name: rippled -Version: 0.27.1-rc2 +Version: 0.27.1-rc3 Release: 1%{?dist} Summary: Ripple peer-to-peer network daemon diff --git a/src/ripple/protocol/impl/BuildInfo.cpp b/src/ripple/protocol/impl/BuildInfo.cpp index 850ae8743c..0b5c217261 100644 --- a/src/ripple/protocol/impl/BuildInfo.cpp +++ b/src/ripple/protocol/impl/BuildInfo.cpp @@ -35,7 +35,7 @@ char const* getRawVersionString () // // The build version number (edit this for each release) // - "0.27.1-rc2" + "0.27.1-rc3" // // Must follow the format described here: // From 95973ba3e8b0bd28eeaa034da8b806faaf498d8a Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Tue, 24 Feb 2015 13:31:13 -0800 Subject: [PATCH 22/33] Set version to 0.27.1 --- Builds/rpm/rippled.spec | 2 +- src/ripple/protocol/impl/BuildInfo.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Builds/rpm/rippled.spec b/Builds/rpm/rippled.spec index dcdb9e01a9..8dbb3c8a2a 100644 --- a/Builds/rpm/rippled.spec +++ b/Builds/rpm/rippled.spec @@ -1,5 +1,5 @@ Name: rippled -Version: 0.27.1-rc3 +Version: 0.27.1 Release: 1%{?dist} Summary: Ripple peer-to-peer network daemon diff --git a/src/ripple/protocol/impl/BuildInfo.cpp b/src/ripple/protocol/impl/BuildInfo.cpp index 0b5c217261..6865133561 100644 --- a/src/ripple/protocol/impl/BuildInfo.cpp +++ b/src/ripple/protocol/impl/BuildInfo.cpp @@ -35,7 +35,7 @@ char const* getRawVersionString () // // The build version number (edit this for each release) // - "0.27.1-rc3" + "0.27.1" // // Must follow the format described here: // From 0b4553506106313b4f7d76c29c73ce441f53966b Mon Sep 17 00:00:00 2001 From: Nik Bougalis Date: Sat, 28 Feb 2015 13:28:54 -0800 Subject: [PATCH 23/33] Calculate deep offer quality --- src/ripple/app/book/BookTip.h | 5 +++-- src/ripple/app/book/impl/BookTip.cpp | 15 +++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/ripple/app/book/BookTip.h b/src/ripple/app/book/BookTip.h index af949b010a..36e70835c9 100644 --- a/src/ripple/app/book/BookTip.h +++ b/src/ripple/app/book/BookTip.h @@ -44,6 +44,7 @@ private: uint256 m_dir; uint256 m_index; SLE::pointer m_entry; + Quality m_quality; LedgerView& view() const noexcept @@ -67,10 +68,10 @@ public: return m_index; } - Quality const + Quality const& quality() const noexcept { - return Quality (getQuality (m_dir)); + return m_quality; } SLE::pointer const& diff --git a/src/ripple/app/book/impl/BookTip.cpp b/src/ripple/app/book/impl/BookTip.cpp index 6750d3f1f2..25874f6a19 100644 --- a/src/ripple/app/book/impl/BookTip.cpp +++ b/src/ripple/app/book/impl/BookTip.cpp @@ -28,6 +28,7 @@ BookTip::BookTip (LedgerView& view, BookRef book) , m_valid (false) , m_book (getBookBase (book)) , m_end (getQualityNext (m_book)) + , m_quality () { } @@ -46,31 +47,33 @@ BookTip::step () for(;;) { // See if there's an entry at or worse than current quality. - auto const page ( - view().getNextLedgerIndex (m_book, m_end)); + auto const first_page (view().getNextLedgerIndex (m_book, m_end)); - if (page.isZero()) + if (first_page.isZero()) return false; unsigned int di (0); SLE::pointer dir; - if (view().dirFirst (page, dir, di, m_index)) + + if (view().dirFirst (first_page, dir, di, m_index)) { m_dir = dir->getIndex(); m_entry = view().entryCache (ltOFFER, m_index); + m_quality = Quality (getQuality (first_page)); m_valid = true; // Next query should start before this directory - m_book = page; + m_book = first_page; // The quality immediately before the next quality --m_book; break; } + // There should never be an empty directory but just in case, // we handle that case by advancing to the next directory. - m_book = page; + m_book = first_page; } return true; From 9cc8eec773e8afc9c12a6aab4982deda80495cf1 Mon Sep 17 00:00:00 2001 From: Nik Bougalis Date: Sun, 1 Mar 2015 14:56:44 -0800 Subject: [PATCH 24/33] Set version to 0.27.2 --- Builds/rpm/rippled.spec | 2 +- src/ripple/protocol/impl/BuildInfo.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Builds/rpm/rippled.spec b/Builds/rpm/rippled.spec index 8dbb3c8a2a..8ae5f5c38c 100644 --- a/Builds/rpm/rippled.spec +++ b/Builds/rpm/rippled.spec @@ -1,5 +1,5 @@ Name: rippled -Version: 0.27.1 +Version: 0.27.2 Release: 1%{?dist} Summary: Ripple peer-to-peer network daemon diff --git a/src/ripple/protocol/impl/BuildInfo.cpp b/src/ripple/protocol/impl/BuildInfo.cpp index 6865133561..74b46a9512 100644 --- a/src/ripple/protocol/impl/BuildInfo.cpp +++ b/src/ripple/protocol/impl/BuildInfo.cpp @@ -35,7 +35,7 @@ char const* getRawVersionString () // // The build version number (edit this for each release) // - "0.27.1" + "0.27.2" // // Must follow the format described here: // From e9381ddeb202fd815f97e1a0dd2344503e728c06 Mon Sep 17 00:00:00 2001 From: David Schwartz Date: Mon, 9 Mar 2015 13:43:58 -0700 Subject: [PATCH 25/33] Add "Default Ripple" account flag and associated logic: AccountSet set/clear, asfDefaultRipple = 8 AccountRoot flag, lsfDefaultRipple = 0x00800000 In trustCreate, set no ripple flag if appropriate. If an account does not have the default ripple flag set, new ripple lines created as a result of its offers being taken or people creating trust lines to it have no ripple set by that account's side automatically Trust lines can be deleted if the no ripple flag matches its default setting based on the account's default ripple setting. Fix default no-rippling in integration tests. --- package.json | 7 +- src/ripple/app/ledger/LedgerEntrySet.cpp | 16 ++- src/ripple/app/transactors/SetAccount.cpp | 9 ++ src/ripple/app/transactors/SetTrust.cpp | 6 +- src/ripple/protocol/LedgerFormats.h | 1 + src/ripple/protocol/TxFlags.h | 1 + test/freeze-test.coffee | 2 +- test/ledger-state.coffee | 10 +- test/no-ripple-test.js | 89 ++++++++++++ test/offer-test.js | 29 ++-- test/orderbook-test.js | 168 +++++++++++----------- test/robust-transaction-test.js | 10 +- test/testutils.js | 63 ++++++-- 13 files changed, 286 insertions(+), 125 deletions(-) diff --git a/package.json b/package.json index 329eb8713a..b644c415a1 100644 --- a/package.json +++ b/package.json @@ -2,13 +2,10 @@ "name": "rippled", "version": "0.0.1", "description": "Rippled Server", - "private": true, - "directories": { "test": "test" }, - "dependencies": { "ripple-lib": "0.8.2", "async": "~0.2.9", @@ -17,18 +14,16 @@ "deep-equal": "0.0.0" }, "devDependencies": { + "assert-diff": "^1.0.1", "coffee-script": "~1.6.3", "mocha": "~1.13.0" }, - "scripts": { "test": "mocha test/websocket-test.js test/server-test.js test/*-test.{js,coffee}" }, - "repository": { "type": "git", "url": "git://github.com/ripple/rippled.git" }, - "readmeFilename": "README.md" } diff --git a/src/ripple/app/ledger/LedgerEntrySet.cpp b/src/ripple/app/ledger/LedgerEntrySet.cpp index 61ac908636..a93b92d113 100644 --- a/src/ripple/app/ledger/LedgerEntrySet.cpp +++ b/src/ripple/app/ledger/LedgerEntrySet.cpp @@ -1342,6 +1342,12 @@ TER LedgerEntrySet::trustCreate ( const bool bSetDst = saLimit.getIssuer () == uDstAccountID; const bool bSetHigh = bSrcHigh ^ bSetDst; + assert (sleAccount->getFieldAccount160 (sfAccount) == + (bSetHigh ? uHighAccountID : uLowAccountID)); + SLE::pointer slePeer = entryCache (ltACCOUNT_ROOT, + getAccountRootIndex (bSetHigh ? uLowAccountID : uHighAccountID)); + assert (slePeer); + // Remember deletion hints. sleRippleState->setFieldU64 (sfLowNode, uLowNode); sleRippleState->setFieldU64 (sfHighNode, uHighNode); @@ -1376,6 +1382,12 @@ TER LedgerEntrySet::trustCreate ( uFlags |= (!bSetHigh ? lsfLowFreeze : lsfHighFreeze); } + if ((slePeer->getFlags() & lsfDefaultRipple) == 0) + { + // The other side's default is no rippling + uFlags |= (bSetHigh ? lsfLowNoRipple : lsfHighNoRipple); + } + sleRippleState->setFieldU32 (sfFlags, uFlags); incrementOwnerCount (sleAccount); @@ -1507,7 +1519,9 @@ TER LedgerEntrySet::rippleCredit ( // Sender is zero or negative. && (uFlags & (!bSenderHigh ? lsfLowReserve : lsfHighReserve)) // Sender reserve is set. - && !(uFlags & (!bSenderHigh ? lsfLowNoRipple : lsfHighNoRipple)) + && static_cast (uFlags & (!bSenderHigh ? lsfLowNoRipple : lsfHighNoRipple)) != + static_cast (entryCache (ltACCOUNT_ROOT, + getAccountRootIndex (uSenderID))->getFlags() & lsfDefaultRipple) && !(uFlags & (!bSenderHigh ? lsfLowFreeze : lsfHighFreeze)) && !sleRippleState->getFieldAmount ( !bSenderHigh ? sfLowLimit : sfHighLimit) diff --git a/src/ripple/app/transactors/SetAccount.cpp b/src/ripple/app/transactors/SetAccount.cpp index 85337a85ee..926f4b93f9 100644 --- a/src/ripple/app/transactors/SetAccount.cpp +++ b/src/ripple/app/transactors/SetAccount.cpp @@ -168,6 +168,15 @@ public: uFlagsOut &= ~lsfDisableMaster; } + if (uSetFlag == asfDefaultRipple) + { + uFlagsOut |= lsfDefaultRipple; + } + else if (uClearFlag == asfDefaultRipple) + { + uFlagsOut &= ~lsfDefaultRipple; + } + if (uSetFlag == asfNoFreeze) { m_journal.trace << "Set NoFreeze flag"; diff --git a/src/ripple/app/transactors/SetTrust.cpp b/src/ripple/app/transactors/SetTrust.cpp index bb751082bf..e042472f94 100644 --- a/src/ripple/app/transactors/SetTrust.cpp +++ b/src/ripple/app/transactors/SetTrust.cpp @@ -288,15 +288,17 @@ public: if (QUALITY_ONE == uHighQualityOut) uHighQualityOut = 0; + bool const bLowDefRipple = sleLowAccount->getFlags() & lsfDefaultRipple; + bool const bHighDefRipple = sleHighAccount->getFlags() & lsfDefaultRipple; bool const bLowReserveSet = uLowQualityIn || uLowQualityOut || - (uFlagsOut & lsfLowNoRipple) || + ((uFlagsOut & lsfLowNoRipple) == 0) != bLowDefRipple || (uFlagsOut & lsfLowFreeze) || saLowLimit || saLowBalance > zero; bool const bLowReserveClear = !bLowReserveSet; bool const bHighReserveSet = uHighQualityIn || uHighQualityOut || - (uFlagsOut & lsfHighNoRipple) || + ((uFlagsOut & lsfHighNoRipple) == 0) != bHighDefRipple || (uFlagsOut & lsfHighFreeze) || saHighLimit || saHighBalance > zero; bool const bHighReserveClear = !bHighReserveSet; diff --git a/src/ripple/protocol/LedgerFormats.h b/src/ripple/protocol/LedgerFormats.h index 286d800575..d3398bb99b 100644 --- a/src/ripple/protocol/LedgerFormats.h +++ b/src/ripple/protocol/LedgerFormats.h @@ -111,6 +111,7 @@ enum LedgerSpecificFlags lsfDisableMaster = 0x00100000, // True, force regular key lsfNoFreeze = 0x00200000, // True, cannot freeze ripple states lsfGlobalFreeze = 0x00400000, // True, all assets frozen + lsfDefaultRipple = 0x00800000, // True, trust lines allow rippling by default // ltOFFER lsfPassive = 0x00010000, diff --git a/src/ripple/protocol/TxFlags.h b/src/ripple/protocol/TxFlags.h index d343878d84..0161ca99a6 100644 --- a/src/ripple/protocol/TxFlags.h +++ b/src/ripple/protocol/TxFlags.h @@ -65,6 +65,7 @@ const std::uint32_t asfDisableMaster = 4; const std::uint32_t asfAccountTxnID = 5; const std::uint32_t asfNoFreeze = 6; const std::uint32_t asfGlobalFreeze = 7; +const std::uint32_t asfDefaultRipple = 8; // OfferCreate flags: const std::uint32_t tfPassive = 0x00010000; diff --git a/test/freeze-test.coffee b/test/freeze-test.coffee index 1b8824f30e..a2d9235bbb 100644 --- a/test/freeze-test.coffee +++ b/test/freeze-test.coffee @@ -847,4 +847,4 @@ execute_if_enabled (suite, enforced) -> remote.request_account_offers args, (err, res) -> assert res.offers.length == 0 - done() \ No newline at end of file + done() diff --git a/test/ledger-state.coffee b/test/ledger-state.coffee index b57f6f9d88..30c3eb57c1 100644 --- a/test/ledger-state.coffee +++ b/test/ledger-state.coffee @@ -497,8 +497,9 @@ exports.LedgerState = class LedgerState add_transaction_fees: -> extra_fees = {} + account_sets = ([k] for k,ac of @accounts) fee = Amount.from_json(@remote.fee_cushion * 10) - for list in [@trusts, @iou_payments, @offers] + for list in [@trusts, @iou_payments, @offers, account_sets] for [src, args...] in list extra = extra_fees[src] extra = if extra? then extra.add(fee) else fee @@ -532,6 +533,13 @@ exports.LedgerState = class LedgerState LOG("Account `#{src}` creating account `#{dest}` by "+ "making payment of #{amt.to_text_full()}") ), cb) + (cb) -> + reqs.transactor( + Transaction::account_set, + accounts_apply_arguments, + ((account, tx) -> + tx.tx_json.SetFlag = 8 + ), cb) (cb) -> reqs.transactor( Transaction::ripple_line_set, diff --git a/test/no-ripple-test.js b/test/no-ripple-test.js index ea116f635f..32146c65db 100644 --- a/test/no-ripple-test.js +++ b/test/no-ripple-test.js @@ -326,3 +326,92 @@ suite('NoRipple', function() { }); }); }); + +suite('Default ripple', function() { + var $ = { }; + + setup(function(done) { + testutils.build_setup().call($, done); + }); + + teardown(function(done) { + testutils.build_teardown().call($, done); + }); + + test('Set default ripple on account, check new trustline', function(done) { + var steps = [ + function (callback) { + testutils.create_accounts( + $.remote, + 'root', + '10000.0', + [ 'alice', 'bob' ], + { default_rippling: false }, + callback); + }, + function (callback) { + var tx = $.remote.createTransaction('AccountSet', { + account: 'bob', + set_flag: 8 + }); + testutils.submit_transaction(tx, callback); + }, + function (callback) { + var tx = $.remote.createTransaction('TrustSet', { + account: 'root', + limit: '100/USD/alice' + }); + testutils.submit_transaction(tx, callback); + }, + function (callback) { + var tx = $.remote.createTransaction('TrustSet', { + account: 'root', + limit: '100/USD/bob' + }); + testutils.submit_transaction(tx, callback); + }, + function (callback) { + $.remote.requestAccountLines({ account: 'root', peer: 'alice' }, function(err, m) { + assert.ifError(err); + assert(Array.isArray(m.lines)); + assert(m.lines[0].no_ripple_peer, + 'Trustline should have no_ripple_peer set'); + callback(); + }); + }, + function (callback) { + $.remote.requestAccountLines({ account: 'alice', peer: 'root' }, function(err, m) { + assert.ifError(err); + assert(Array.isArray(m.lines)); + assert(m.lines[0].no_ripple, + 'Trustline should have no_ripple set'); + callback(); + }); + }, + function (callback) { + $.remote.requestAccountLines({ account: 'root', peer: 'bob' }, function(err, m) { + assert.ifError(err); + assert(Array.isArray(m.lines)); + assert(!m.lines[0].no_ripple, + 'Trustline should not have no_ripple set'); + callback(); + }); + }, + function (callback) { + $.remote.requestAccountLines({ account: 'bob', peer: 'root' }, function(err, m) { + assert.ifError(err); + assert(Array.isArray(m.lines)); + assert(!m.lines[0].no_ripple_peer, + 'Trustline should not have no_ripple_peer set'); + callback(); + }); + } + ] + + async.series(steps, function(error) { + assert(!error, error); + done(); + }); + }); + +}); diff --git a/test/offer-test.js b/test/offer-test.js index 97a5f99c06..aaec3c1848 100644 --- a/test/offer-test.js +++ b/test/offer-test.js @@ -1909,7 +1909,7 @@ suite("Client Issue #535", function() { var starting_xrp = $.amount_for({ ledger_entries: 1, default_transactions: 2, - extra: '100.0' + extra: '100.1' }); testutils.create_accounts($.remote, "root", starting_xrp, ["alice", "bob", "mtgox"], callback); @@ -1938,12 +1938,16 @@ suite("Client Issue #535", function() { $.remote.transaction() .offer_create("alice", "100/XTS/mtgox", "100/XXX/mtgox") - .on('submitted', function (m) { - // console.log("proposed: offer_create: %s", json.stringify(m)); - callback(m.engine_result !== 'tesSUCCESS'); + .on('submitted', function(m) { + if (m.engine_result === 'tesSUCCESS') { + callback(); + } else { + // console.log("proposed: %s", JSON.stringify(m, undefined, 2)); + callback(m); + } - seq_carol = m.tx_json.sequence; - }) + seq_carol = m.tx_json.sequence; + }) .submit(); }, function (callback) { @@ -1982,13 +1986,12 @@ suite("Client Issue #535", function() { }, callback); }, - ], function (error) { - if (error) - //console.log("result: %s: error=%s", self.what, error); - assert(!error, self.what); - - done(); - }); + ], function (error) { + if (error) + //console.log("result: %s: error=%s", self.what, error); + assert(!error, self.what); + done(); + }); }); }); // vim:sw=2:sts=2:ts=8:et diff --git a/test/orderbook-test.js b/test/orderbook-test.js index 4e62eadcc9..cd0086da8d 100644 --- a/test/orderbook-test.js +++ b/test/orderbook-test.js @@ -1,115 +1,115 @@ var async = require('async'); -var assert = require('assert'); +var assert = require('assert-diff'); var Account = require('ripple-lib').UInt160; var Remote = require('ripple-lib').Remote; var Transaction = require('ripple-lib').Transaction; var testutils = require('./testutils'); var config = testutils.init_config(); - + suite('Order Book', function() { var $ = { }; - + setup(function(done) { testutils.build_setup().call($, done); }); - + teardown(function(done) { testutils.build_teardown().call($, done); }); - + test('Track offers', function (done) { var self = this; - + var steps = [ function(callback) { - self.what = 'Create accounts'; + self.what = 'Create accounts'; - testutils.create_accounts( - $.remote, - 'root', - '20000.0', - [ 'mtgox', 'alice', 'bob' ], - callback - ); + testutils.create_accounts( + $.remote, + 'root', + '20000.0', + [ 'mtgox', 'alice', 'bob' ], + callback + ); }, - + function waitLedgers(callback) { self.what = 'Wait ledger'; $.remote.once('ledger_closed', function() { callback(); }); - + $.remote.ledger_accept(); }, - + function verifyBalance(callback) { self.what = 'Verify balance'; testutils.verify_balance( $.remote, [ 'mtgox', 'alice', 'bob' ], - '20000000000', + '19999999988', callback ); }, - + function (callback) { self.what = 'Set transfer rate'; - + var tx = $.remote.transaction('AccountSet', { account: 'mtgox' }); - + tx.transferRate(1.1 * 1e9); - + tx.submit(function(err, m) { assert.ifError(err); assert.strictEqual(m.engine_result, 'tesSUCCESS'); callback(); }); - + testutils.ledger_wait($.remote, tx); }, - + function (callback) { self.what = 'Set limits'; - + testutils.credit_limits($.remote, { 'alice' : '1000/USD/mtgox', 'bob' : '1000/USD/mtgox' }, callback); }, - + function (callback) { self.what = 'Distribute funds'; - + testutils.payments($.remote, { 'mtgox' : [ '100/USD/alice', '50/USD/bob' ] }, callback); }, - + function (callback) { self.what = 'Create offer'; - + // get 4000/XRP pay 10/USD : offer pays 10 USD for 4000 XRP var tx = $.remote.transaction('OfferCreate', { account: 'alice', taker_pays: '4000', taker_gets: '10/USD/mtgox' }); - + tx.submit(function(err, m) { assert.ifError(err); assert.strictEqual(m.engine_result, 'tesSUCCESS'); callback(); }); - + testutils.ledger_wait($.remote, tx); }, - + function (callback) { self.what = 'Create order book'; @@ -118,60 +118,60 @@ suite('Order Book', function() { issuer_gets: Account.json_rewrite('mtgox'), currency_gets: 'USD' }); - + ob.on('model', function(){}); ob.getOffers(function(err, offers) { assert.ifError(err); //console.log('OFFERS', offers); - + var expected = [ - { Account: 'rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn', - BookDirectory: 'AE0A97F385FFE42E3096BA3F98A0173090FE66A3C2482FE0570E35FA931A0000', - BookNode: '0000000000000000', - Flags: 0, - LedgerEntryType: 'Offer', - OwnerNode: '0000000000000000', - PreviousTxnID: offers[0].PreviousTxnID, - PreviousTxnLgrSeq: offers[0].PreviousTxnLgrSeq, - Sequence: 2, - TakerGets: { currency: 'USD', - issuer: 'rGihwhaqU8g7ahwAvTq6iX5rvsfcbgZw6v', - value: '10' - }, - TakerPays: '4000', - index: 'CD6AE78EE0A5438978501A0404D9093597F57B705D566B5070D58BD48F98468C', - owner_funds: '100', - quality: '400', - is_fully_funded: true, - taker_gets_funded: '10', - taker_pays_funded: '4000' } + { Account: 'rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn', + BookDirectory: 'AE0A97F385FFE42E3096BA3F98A0173090FE66A3C2482FE0570E35FA931A0000', + BookNode: '0000000000000000', + Flags: 0, + LedgerEntryType: 'Offer', + OwnerNode: '0000000000000000', + PreviousTxnID: offers[0].PreviousTxnID, + PreviousTxnLgrSeq: offers[0].PreviousTxnLgrSeq, + Sequence: 3, + TakerGets: { currency: 'USD', + issuer: 'rGihwhaqU8g7ahwAvTq6iX5rvsfcbgZw6v', + value: '10' + }, + TakerPays: '4000', + index: '2A432F386EF28151AF60885CE201CC9331FF494A163D40531A9D253C97E81D61', + owner_funds: '100', + quality: '400', + is_fully_funded: true, + taker_gets_funded: '10', + taker_pays_funded: '4000' } ] assert.deepEqual(offers, expected); - + callback(null, ob); }); }, function (ob, callback) { self.what = 'Create offer'; - + // get 5/USD pay 2000/XRP: offer pays 2000 XRP for 5 USD var tx = $.remote.transaction('OfferCreate', { account: 'bob', taker_pays: '5/USD/mtgox', taker_gets: '2000', }); - - tx.submit(function(err, m) { - assert.ifError(err); - assert.strictEqual(m.engine_result, 'tesSUCCESS'); - callback(null, ob); - }); - - testutils.ledger_wait($.remote, tx); + + tx.submit(function(err, m) { + assert.ifError(err); + assert.strictEqual(m.engine_result, 'tesSUCCESS'); + callback(null, ob); + }); + + testutils.ledger_wait($.remote, tx); }, - + function (ob, callback) { self.what = 'Check order book tracking'; @@ -180,21 +180,21 @@ suite('Order Book', function() { //console.log('OFFERS', offers); var expected = [ - { Account: 'rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn', - BookDirectory: 'AE0A97F385FFE42E3096BA3F98A0173090FE66A3C2482FE0570E35FA931A0000', - BookNode: '0000000000000000', - Flags: 0, - LedgerEntryType: 'Offer', - OwnerNode: '0000000000000000', - PreviousTxnID: offers[0].PreviousTxnID, - PreviousTxnLgrSeq: offers[0].PreviousTxnLgrSeq, - Sequence: 2, - TakerGets: - { currency: 'USD', - issuer: 'rGihwhaqU8g7ahwAvTq6iX5rvsfcbgZw6v', - value: '5' }, + { Account: 'rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn', + BookDirectory: 'AE0A97F385FFE42E3096BA3F98A0173090FE66A3C2482FE0570E35FA931A0000', + BookNode: '0000000000000000', + Flags: 0, + LedgerEntryType: 'Offer', + OwnerNode: '0000000000000000', + PreviousTxnID: offers[0].PreviousTxnID, + PreviousTxnLgrSeq: offers[0].PreviousTxnLgrSeq, + Sequence: 3, + TakerGets: + { currency: 'USD', + issuer: 'rGihwhaqU8g7ahwAvTq6iX5rvsfcbgZw6v', + value: '5' }, TakerPays: '2000', - index: 'CD6AE78EE0A5438978501A0404D9093597F57B705D566B5070D58BD48F98468C', + index: '2A432F386EF28151AF60885CE201CC9331FF494A163D40531A9D253C97E81D61', owner_funds: '94.5', quality: '400', is_fully_funded: true, @@ -207,10 +207,10 @@ suite('Order Book', function() { }); }, ]; - - async.waterfall(steps, function (error) { - assert(!error, self.what + ': ' + error); - done(); - }); + + async.waterfall(steps, function (error) { + assert(!error, self.what + ': ' + error); + done(); + }); }); }); diff --git a/test/robust-transaction-test.js b/test/robust-transaction-test.js index f5814d99df..383eaaf7e4 100644 --- a/test/robust-transaction-test.js +++ b/test/robust-transaction-test.js @@ -133,7 +133,7 @@ make_suite('Robust transaction submission', function() { function verifyBalance(callback) { self.what = 'Verify balance'; - testutils.verify_balance($.remote, 'bob', '20001000000', callback); + testutils.verify_balance($.remote, 'bob', '20000999988', callback); } ] @@ -218,7 +218,7 @@ make_suite('Robust transaction submission', function() { function verifyBalance(callback) { self.what = 'Verify balance'; - testutils.verify_balance($.remote, 'alice', '20001000000', callback); + testutils.verify_balance($.remote, 'alice', '20000999988', callback); } ] @@ -249,7 +249,7 @@ make_suite('Robust transaction submission', function() { function verifyBalance(callback) { self.what = 'Verify balance'; - testutils.verify_balance($.remote, 'alice', '20000000000', callback); + testutils.verify_balance($.remote, 'alice', '19999999988', callback); }, function submitTransaction(callback) { @@ -317,7 +317,7 @@ make_suite('Robust transaction submission', function() { function verifyBalance(callback) { self.what = 'Verify balance'; - testutils.verify_balance($.remote, 'alice', '20001000000', callback); + testutils.verify_balance($.remote, 'alice', '20000999988', callback); } ] @@ -381,7 +381,7 @@ make_suite('Robust transaction submission', function() { function verifyBalance(callback) { self.what = 'Verify balance'; - testutils.verify_balance($.remote, 'alice', '20001000000', callback); + testutils.verify_balance($.remote, 'alice', '20000999988', callback); } ] diff --git a/test/testutils.js b/test/testutils.js index 671ace017c..04931bbd3f 100644 --- a/test/testutils.js +++ b/test/testutils.js @@ -215,6 +215,18 @@ function account_dump(remote, account, callback) { // construct a json result }; +function set_account_flag(remote, account, options, callback) { + if (typeof options === 'number') { + options = { set_flag: options }; + } + + var tx = remote.createTransaction('AccountSet', extend({ + account: account + }, options)); + + submit_transaction(tx, callback); +} + exports.fund_account = fund_account = function(remote, src, account, amount, callback) { @@ -228,7 +240,7 @@ function(remote, src, account, amount, callback) { tx.once('proposed', function (result) { //console.log('proposed: %s', JSON.stringify(result)); - callback(result.engine_result === 'tesSUCCESS' ? null : new Error()); + callback(result.engine_result === 'tesSUCCESS' ? null : result); }); tx.once('error', function (result) { @@ -241,7 +253,14 @@ function(remote, src, account, amount, callback) { exports.create_account = create_account = -function(remote, src, account, amount, callback) { +function(remote, src, account, amount, options, callback) { + if (typeof options === 'function') { + callback = options; + options = {}; + } + + options = extend({default_rippling: true}, options); + // Before creating the account, check if it exists in the ledger. // If it does, regardless of the balance, fail the test, because // the ledger is not in the expected state. @@ -253,25 +272,39 @@ function(remote, src, account, amount, callback) { }); info.once('error', function(result) { - if (result.error === "remoteError" && result.remote.error === "actNotFound") { - // rippled indicated the account does not exist. Create it by funding it. - fund_account(remote, src, account, amount, callback); - } else { - // Some other error occurred. Pass it up to the callback. - callback(result); + var isNotFoundError = result.error === 'remoteError' + && result.remote.error === 'actNotFound'; + + if (!isNotFoundError) { + return callback(result); } + + // rippled indicated the account does not exist. Create it by funding it. + fund_account(remote, src, account, amount, function(err) { + if (err) { + callback(err); + } else if (!options.default_rippling) { + callback(); + } else { + // Set default rippling on trustlines for account + set_account_flag(remote, account, 8, callback); + } + }); }); info.request(); } -function create_accounts(remote, src, amount, accounts, callback) { - assert.strictEqual(arguments.length, 5); +function create_accounts(remote, src, amount, accounts, options, callback) { + if (typeof options === 'function') { + callback = options; + options = {}; + } remote.set_account_seq(src, 1); async.forEach(accounts, function (account, callback) { - create_account(remote, src, account, amount, callback); + create_account(remote, src, account, amount, options, callback); }, callback); }; @@ -568,13 +601,18 @@ function ledger_wait(remote, tx) { ;(function nextLedger() { remote.once('ledger_closed', function() { if (!tx.finalized) { - setTimeout(nextLedger, isTravis ? 400 : 100); + setTimeout(nextLedger, isTravis ? 200 : 50); } }); remote.ledger_accept(); })(); }; +function submit_transaction(tx, callback) { + tx.submit(callback); + ledger_wait(tx.remote, tx); +} + exports.account_dump = account_dump; exports.build_setup = build_setup; exports.build_teardown = build_teardown; @@ -595,6 +633,7 @@ exports.verify_offer_not_found = verify_offer_not_found; exports.verify_owner_count = verify_owner_count; exports.verify_owner_counts = verify_owner_counts; exports.ledger_wait = ledger_wait; +exports.submit_transaction = submit_transaction; process.on('uncaughtException', function() { Object.keys(server).forEach(function(host) { From 70c2854f7c8a28801a7ebc81dd62bf0d068188f0 Mon Sep 17 00:00:00 2001 From: Nik Bougalis Date: Tue, 10 Mar 2015 14:06:33 -0700 Subject: [PATCH 26/33] Set version to 0.27.3 --- Builds/rpm/rippled.spec | 2 +- src/ripple/protocol/impl/BuildInfo.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Builds/rpm/rippled.spec b/Builds/rpm/rippled.spec index 8ae5f5c38c..3557932eec 100644 --- a/Builds/rpm/rippled.spec +++ b/Builds/rpm/rippled.spec @@ -1,5 +1,5 @@ Name: rippled -Version: 0.27.2 +Version: 0.27.3 Release: 1%{?dist} Summary: Ripple peer-to-peer network daemon diff --git a/src/ripple/protocol/impl/BuildInfo.cpp b/src/ripple/protocol/impl/BuildInfo.cpp index 74b46a9512..e6570f93d5 100644 --- a/src/ripple/protocol/impl/BuildInfo.cpp +++ b/src/ripple/protocol/impl/BuildInfo.cpp @@ -35,7 +35,7 @@ char const* getRawVersionString () // // The build version number (edit this for each release) // - "0.27.2" + "0.27.3" // // Must follow the format described here: // From ed66b951c6615ae06c85776ed59a53f89dcccdf8 Mon Sep 17 00:00:00 2001 From: Nicholas Dudfield Date: Wed, 11 Mar 2015 12:19:24 +0700 Subject: [PATCH 27/33] Fix testutils.create_accounts * Don't call ledger_wait inside parallel async loop --- test/testutils.js | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/test/testutils.js b/test/testutils.js index 04931bbd3f..0e01246129 100644 --- a/test/testutils.js +++ b/test/testutils.js @@ -213,7 +213,7 @@ function account_dump(remote, account, callback) { // get closed ledger hash // get account root // construct a json result -}; +} function set_account_flag(remote, account, options, callback) { if (typeof options === 'number') { @@ -224,7 +224,9 @@ function set_account_flag(remote, account, options, callback) { account: account }, options)); - submit_transaction(tx, callback); + // submit_transaction(tx, callback); + tx.submit(); + tx.once('proposed', function(){callback();}); } exports.fund_account = @@ -259,7 +261,6 @@ function(remote, src, account, amount, options, callback) { options = {}; } - options = extend({default_rippling: true}, options); // Before creating the account, check if it exists in the ledger. // If it does, regardless of the balance, fail the test, because @@ -272,28 +273,19 @@ function(remote, src, account, amount, options, callback) { }); info.once('error', function(result) { - var isNotFoundError = result.error === 'remoteError' - && result.remote.error === 'actNotFound'; + var isNotFoundError = result.error === 'remoteError' && + result.remote.error === 'actNotFound'; if (!isNotFoundError) { return callback(result); } // rippled indicated the account does not exist. Create it by funding it. - fund_account(remote, src, account, amount, function(err) { - if (err) { - callback(err); - } else if (!options.default_rippling) { - callback(); - } else { - // Set default rippling on trustlines for account - set_account_flag(remote, account, 8, callback); - } - }); + fund_account(remote, src, account, amount, callback); }); info.request(); -} +}; function create_accounts(remote, src, amount, accounts, options, callback) { if (typeof options === 'function') { @@ -305,8 +297,24 @@ function create_accounts(remote, src, amount, accounts, options, callback) { async.forEach(accounts, function (account, callback) { create_account(remote, src, account, amount, options, callback); - }, callback); -}; + }, function(){ + options = extend({default_rippling: true}, options); + // If we don't want to set default rippling, then bail, otherwise + if (!options.default_rippling) { + return callback(); + } + + // close the ledger, so all the accounts always exist, then + remote.ledger_accept(function(){ + // set the default rippling flag, on all the accounts + async.forEach(accounts, function(account, callback){ + set_account_flag(remote, account, 8, callback); + }, function(){ + remote.ledger_accept(function(){callback();}); + }); + }); + }); +} function credit_limit(remote, src, amount, callback) { assert.strictEqual(arguments.length, 4); From 232693419a2c9a8276a0fae991f688f6f01a3add Mon Sep 17 00:00:00 2001 From: Nik Bougalis Date: Wed, 11 Mar 2015 10:26:39 -0700 Subject: [PATCH 28/33] Set version to 0.27.3-sp1 --- Builds/rpm/rippled.spec | 2 +- src/ripple/protocol/impl/BuildInfo.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Builds/rpm/rippled.spec b/Builds/rpm/rippled.spec index 3557932eec..e6912f4470 100644 --- a/Builds/rpm/rippled.spec +++ b/Builds/rpm/rippled.spec @@ -1,5 +1,5 @@ Name: rippled -Version: 0.27.3 +Version: 0.27.3-sp1 Release: 1%{?dist} Summary: Ripple peer-to-peer network daemon diff --git a/src/ripple/protocol/impl/BuildInfo.cpp b/src/ripple/protocol/impl/BuildInfo.cpp index e6570f93d5..074e36e11f 100644 --- a/src/ripple/protocol/impl/BuildInfo.cpp +++ b/src/ripple/protocol/impl/BuildInfo.cpp @@ -35,7 +35,7 @@ char const* getRawVersionString () // // The build version number (edit this for each release) // - "0.27.3" + "0.27.3-sp1" // // Must follow the format described here: // From f1bc662a2431af48d4edf7fb7edc7dbb5e94fdd2 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Wed, 11 Mar 2015 16:03:25 -0700 Subject: [PATCH 29/33] Add noripple_check RPC command To help gateways make the changes needed to adjust to the "default ripple" flag, we've added the "noripple_check" RPC command. This command tells gateways what they need to do to set this flag and fix any trust lines created before they set the flag. Once your server is running and synchronized, you can run the tool from the command line with a command like: rippled json noripple_check ' { "account" : "", "role" : "gateway", "transactions" : "true" }' The server will respond with a list of "problems" that it sees with the configuration of the account and its trust lines. It will also return a "transactions" array suggesting the transactions needed to fix the problems it found. --- src/ripple/rpc/handlers/Handlers.h | 1 + src/ripple/rpc/handlers/NoRippleCheck.cpp | 186 ++++++++++++++++++++++ src/ripple/rpc/impl/Handler.cpp | 1 + src/ripple/unity/rpcx.cpp | 1 + 4 files changed, 189 insertions(+) create mode 100644 src/ripple/rpc/handlers/NoRippleCheck.cpp diff --git a/src/ripple/rpc/handlers/Handlers.h b/src/ripple/rpc/handlers/Handlers.h index c39ebd920c..40a489359e 100644 --- a/src/ripple/rpc/handlers/Handlers.h +++ b/src/ripple/rpc/handlers/Handlers.h @@ -48,6 +48,7 @@ Json::Value doLedgerHeader (RPC::Context&); Json::Value doLedgerRequest (RPC::Context&); Json::Value doLogLevel (RPC::Context&); Json::Value doLogRotate (RPC::Context&); +Json::Value doNoRippleCheck (RPC::Context&); Json::Value doOwnerInfo (RPC::Context&); Json::Value doPathFind (RPC::Context&); Json::Value doPeers (RPC::Context&); diff --git a/src/ripple/rpc/handlers/NoRippleCheck.cpp b/src/ripple/rpc/handlers/NoRippleCheck.cpp new file mode 100644 index 0000000000..5deca19723 --- /dev/null +++ b/src/ripple/rpc/handlers/NoRippleCheck.cpp @@ -0,0 +1,186 @@ +//------------------------------------------------------------------------------ +/* + This file is part of rippled: https://github.com/ripple/rippled + Copyright (c) 2012-2014 Ripple Labs Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#include +#include +#include +#include + +namespace ripple { + +static void fillTransaction ( + Json::Value& txArray, + RippleAddress const& account, + std::uint32_t& sequence, + Ledger::ref ledger) +{ + txArray["Sequence"] = Json::UInt (sequence++); + txArray["Account"] = account.humanAccountID (); + txArray["Fee"] = Json::UInt (ledger->scaleFeeLoad (10, false)); +} + +// { +// account: | +// account_index: // optional, defaults to 0. +// ledger_hash : +// ledger_index : +// limit: integer // optional, number of problems +// role: gateway|user // account role to assume +// transactions: true // optional, reccommend transactions +// } +Json::Value doNoRippleCheck (RPC::Context& context) +{ + auto const& params (context.params); + if (! params.isMember (jss::account)) + return RPC::missing_field_error ("account"); + + if (! params.isMember ("role")) + return RPC::missing_field_error ("role"); + bool roleGateway = false; + { + std::string const role = params["role"].asString(); + if (role == "gateway") + roleGateway = true; + else if (role != "user") + return RPC::invalid_field_message ("role"); + } + + unsigned int limit = 300; + if (params.isMember (jss::limit)) + { + auto const& jvLimit (params[jss::limit]); + if (! jvLimit.isIntegral ()) + return RPC::expected_field_error ("limit", "unsigned integer"); + limit = jvLimit.isUInt () ? jvLimit.asUInt () : + std::max (0, jvLimit.asInt ()); + if (context.role != Role::ADMIN) + { + limit = std::max (RPC::Tuning::minLinesPerRequest, + std::min (limit, RPC::Tuning::maxLinesPerRequest)); + } + } + + bool transactions = false; + if (params.isMember (jss::transactions)) + transactions = params["transactions"].asBool(); + + Ledger::pointer ledger; + Json::Value result (RPC::lookupLedger (params, ledger, context.netOps)); + if (! ledger) + return result; + + Json::Value dummy; + Json::Value& jvTransactions = + transactions ? (result[jss::transactions] = Json::arrayValue) : dummy; + + std::string strIdent (params[jss::account].asString ()); + bool bIndex (params.isMember (jss::account_index)); + int iIndex (bIndex ? params[jss::account_index].asUInt () : 0); + RippleAddress rippleAddress; + + Json::Value const jv (RPC::accountFromString (ledger, rippleAddress, bIndex, + strIdent, iIndex, false, context.netOps)); + if (! jv.empty ()) + { + for (Json::Value::const_iterator it (jv.begin ()); it != jv.end (); ++it) + result[it.memberName ()] = it.key (); + + return result; + } + + AccountState::pointer accountState = ledger->getAccountState (rippleAddress); + if (! accountState) + return rpcError (rpcACT_NOT_FOUND); + + std::uint32_t seq = accountState->peekSLE().getFieldU32 (sfSequence); + + Json::Value& problems = (result["problems"] = Json::arrayValue); + + bool bDefaultRipple = accountState->peekSLE().getFieldU32 (sfFlags) & lsfDefaultRipple; + + if (bDefaultRipple & ! roleGateway) + { + problems.append ("You appear to have set your default ripple flag even though you " + "are not a gateway. This is not recommended unless you are experimenting"); + } + else if (roleGateway & ! bDefaultRipple) + { + problems.append ("You should immediately set your default ripple flag"); + if (transactions) + { + Json::Value& tx = jvTransactions.append (Json::objectValue); + tx["TransactionType"] = "AccountSet"; + tx["SetFlag"] = 8; + fillTransaction (tx, rippleAddress, seq, ledger); + } + } + + auto const accountID = rippleAddress.getAccountID (); + + ledger->visitAccountItems (accountID, uint256(), 0, limit, + [&](SLE::ref ownedItem) + { + if (ownedItem->getType() == ltRIPPLE_STATE) + { + bool const bLow = accountID == ownedItem->getFieldAmount(sfLowLimit).getIssuer(); + + bool const bNoRipple = ownedItem->getFieldU32(sfFlags) & + (bLow ? lsfLowNoRipple : lsfHighNoRipple); + + std::string problem; + bool needFix = false; + if (bNoRipple & roleGateway) + { + problem = "You should clear the no ripple flag on your "; + needFix = true; + } + else if (! roleGateway & ! bNoRipple) + { + problem = "You should probably set the no ripple flag on your "; + needFix = true; + } + if (needFix) + { + Account peer = + ownedItem->getFieldAmount (bLow ? sfHighLimit : sfLowLimit).getIssuer(); + STAmount peerLimit = ownedItem->getFieldAmount (bLow ? sfHighLimit : sfLowLimit); + problem += to_string (peerLimit.getCurrency()); + problem += " line to "; + problem += to_string (peerLimit.getIssuer()); + problems.append (problem); + + STAmount limitAmount (ownedItem->getFieldAmount (bLow ? sfLowLimit : sfHighLimit)); + limitAmount.setIssuer (peer); + + Json::Value& tx = jvTransactions.append (Json::objectValue); + tx["TransactionType"] = "TrustSet"; + tx["LimitAmount"] = limitAmount.getJson (0); + tx["Flags"] = bNoRipple ? tfClearNoRipple : tfSetNoRipple; + fillTransaction(tx, rippleAddress, seq, ledger); + + return true; + } + } + return false; + }); + + return result; +} + +} // ripple diff --git a/src/ripple/rpc/impl/Handler.cpp b/src/ripple/rpc/impl/Handler.cpp index 58f2fae8d6..c9a1c162c4 100644 --- a/src/ripple/rpc/impl/Handler.cpp +++ b/src/ripple/rpc/impl/Handler.cpp @@ -118,6 +118,7 @@ HandlerTable HANDLERS({ { "ledger_request", byRef (&doLedgerRequest), Role::ADMIN, NO_CONDITION }, { "log_level", byRef (&doLogLevel), Role::ADMIN, NO_CONDITION }, { "logrotate", byRef (&doLogRotate), Role::ADMIN, NO_CONDITION }, + { "noripple_check", byRef (&doNoRippleCheck), Role::USER, NEEDS_CURRENT_LEDGER }, { "owner_info", byRef (&doOwnerInfo), Role::USER, NEEDS_CURRENT_LEDGER }, { "peers", byRef (&doPeers), Role::ADMIN, NO_CONDITION }, { "path_find", byRef (&doPathFind), Role::USER, NEEDS_CURRENT_LEDGER }, diff --git a/src/ripple/unity/rpcx.cpp b/src/ripple/unity/rpcx.cpp index e05e0a386e..d448170565 100644 --- a/src/ripple/unity/rpcx.cpp +++ b/src/ripple/unity/rpcx.cpp @@ -64,6 +64,7 @@ #include #include #include +#include #include #include #include From f999839e599e131ed624330ad0ce85bb995f02d3 Mon Sep 17 00:00:00 2001 From: Nik Bougalis Date: Thu, 12 Mar 2015 13:37:47 -0700 Subject: [PATCH 30/33] Set version to 0.27.3-sp2 --- Builds/rpm/rippled.spec | 2 +- src/ripple/protocol/impl/BuildInfo.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Builds/rpm/rippled.spec b/Builds/rpm/rippled.spec index e6912f4470..6eac7a4b12 100644 --- a/Builds/rpm/rippled.spec +++ b/Builds/rpm/rippled.spec @@ -1,5 +1,5 @@ Name: rippled -Version: 0.27.3-sp1 +Version: 0.27.3-sp2 Release: 1%{?dist} Summary: Ripple peer-to-peer network daemon diff --git a/src/ripple/protocol/impl/BuildInfo.cpp b/src/ripple/protocol/impl/BuildInfo.cpp index 074e36e11f..629e3d8735 100644 --- a/src/ripple/protocol/impl/BuildInfo.cpp +++ b/src/ripple/protocol/impl/BuildInfo.cpp @@ -35,7 +35,7 @@ char const* getRawVersionString () // // The build version number (edit this for each release) // - "0.27.3-sp1" + "0.27.3-sp2" // // Must follow the format described here: // From 984f66e0835e13fc732e73339473bb9f3615e111 Mon Sep 17 00:00:00 2001 From: Mark Travis Date: Wed, 11 Feb 2015 13:18:29 -0800 Subject: [PATCH 31/33] Don't VACUUM SQLite databases on startup with online delete enabled. --- src/ripple/app/main/Application.cpp | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/ripple/app/main/Application.cpp b/src/ripple/app/main/Application.cpp index 1449fee87c..328e322ab3 100644 --- a/src/ripple/app/main/Application.cpp +++ b/src/ripple/app/main/Application.cpp @@ -561,20 +561,6 @@ public: mWalletDB = std::make_unique (setup, "wallet.db", WalletDBInit, WalletDBCount); - if (setup.onlineDelete && mTxnDB && mLedgerDB) - { - { - std::lock_guard lock ( - mTxnDB->peekMutex()); - mTxnDB->getDB()->executeSQL ("VACUUM;"); - } - { - std::lock_guard lock ( - mLedgerDB->peekMutex()); - mLedgerDB->getDB()->executeSQL ("VACUUM;"); - } - } - return mRpcDB.get() != nullptr && mTxnDB.get () != nullptr && From 79417ac59a037bc39a11a6a53512db02e9a1b858 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Tue, 3 Mar 2015 14:01:38 -0800 Subject: [PATCH 32/33] Limit passes in the payment engine to prevent endless looping: This adds a limit of 1,000 passes to the payment engine. It protects against possible cases where the execution of a pass fails to exhaust the liquidity that made the pass possible or cases where two passes alternate providing liquidity for each other. --- src/ripple/app/paths/RippleCalc.cpp | 14 +++++++++++++- src/ripple/app/paths/Tuning.h | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/ripple/app/paths/RippleCalc.cpp b/src/ripple/app/paths/RippleCalc.cpp index 0c2e8e71d3..446521539f 100644 --- a/src/ripple/app/paths/RippleCalc.cpp +++ b/src/ripple/app/paths/RippleCalc.cpp @@ -18,6 +18,7 @@ //============================================================================== #include +#include #include #include #include @@ -295,11 +296,12 @@ TER RippleCalc::rippleCalculate () } } + ++iPass; if (ShouldLog (lsDEBUG, RippleCalc)) { WriteLog (lsDEBUG, RippleCalc) << "rippleCalc: Summary:" - << " Pass: " << ++iPass + << " Pass: " << iPass << " Dry: " << iDry << " Paths: " << pathStateList_.size (); for (auto pathState: pathStateList_) @@ -374,6 +376,16 @@ TER RippleCalc::rippleCalculate () mumSource_.insert ( pathState->reverse().begin (), pathState->reverse().end ()); + if (iPass >= PAYMENT_MAX_LOOPS) + { + // This payment is taking too many passes + + WriteLog (lsERROR, RippleCalc) + << "rippleCalc: pass limit"; + + resultCode = telFAILED_PROCESSING; + } + } else if (!inputFlags.partialPaymentAllowed) { diff --git a/src/ripple/app/paths/Tuning.h b/src/ripple/app/paths/Tuning.h index d786502df5..d498459ad4 100644 --- a/src/ripple/app/paths/Tuning.h +++ b/src/ripple/app/paths/Tuning.h @@ -24,6 +24,7 @@ namespace ripple { int const CALC_NODE_DELIVER_MAX_LOOPS = 40; int const NODE_ADVANCE_MAX_LOOPS = 100; +int const PAYMENT_MAX_LOOPS = 1000; int const PATHFINDER_HIGH_PRIORITY = 100000; int const PATHFINDER_MAX_PATHS = 50; int const PATHFINDER_MAX_COMPLETE_PATHS = 1000; From 92812fe7239ffa3ba91649b2ece1e892b866ec2a Mon Sep 17 00:00:00 2001 From: Nik Bougalis Date: Wed, 11 Mar 2015 11:26:44 -0700 Subject: [PATCH 33/33] Set version to 0.27.4 --- Builds/rpm/rippled.spec | 2 +- src/ripple/protocol/impl/BuildInfo.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Builds/rpm/rippled.spec b/Builds/rpm/rippled.spec index 6eac7a4b12..17e1bf7778 100644 --- a/Builds/rpm/rippled.spec +++ b/Builds/rpm/rippled.spec @@ -1,5 +1,5 @@ Name: rippled -Version: 0.27.3-sp2 +Version: 0.27.4 Release: 1%{?dist} Summary: Ripple peer-to-peer network daemon diff --git a/src/ripple/protocol/impl/BuildInfo.cpp b/src/ripple/protocol/impl/BuildInfo.cpp index 629e3d8735..688d0bf56f 100644 --- a/src/ripple/protocol/impl/BuildInfo.cpp +++ b/src/ripple/protocol/impl/BuildInfo.cpp @@ -35,7 +35,7 @@ char const* getRawVersionString () // // The build version number (edit this for each release) // - "0.27.3-sp2" + "0.27.4" // // Must follow the format described here: //