Commit 7dd3140d authored by nagayama15's avatar nagayama15

Rename stream reader

parent 629cc5d1
#ifndef INCLUDE_kyut_BitStreamReader_hpp #ifndef INCLUDE_kyut_CircularBitStreamReader_hpp
#define INCLUDE_kyut_BitStreamReader_hpp #define INCLUDE_kyut_CircularBitStreamReader_hpp
#include <cstdint> #include <cstdint>
#include <memory> #include <memory>
...@@ -7,27 +7,28 @@ ...@@ -7,27 +7,28 @@
#include <vector> #include <vector>
namespace kyut { namespace kyut {
class BitStreamReader { class CircularBitStreamReader {
public: public:
explicit BitStreamReader(std::vector<std::uint8_t> data) explicit CircularBitStreamReader(std::vector<std::uint8_t> data)
: data_(std::move(data)) : data_(std::move(data))
, pos_read_(0) {} , pos_read_(0) {}
template <typename Iterator> template <typename Iterator>
explicit BitStreamReader(Iterator begin, Iterator end) explicit CircularBitStreamReader(Iterator begin, Iterator end)
: BitStreamReader({begin, end}) {} : CircularBitStreamReader({begin, end}) {}
BitStreamReader(const BitStreamReader &) = delete; CircularBitStreamReader(const CircularBitStreamReader &) = delete;
BitStreamReader(BitStreamReader &&) = delete; CircularBitStreamReader(CircularBitStreamReader &&) = delete;
BitStreamReader &operator=(const BitStreamReader &) = delete; CircularBitStreamReader &operator=(const CircularBitStreamReader &) = delete;
BitStreamReader &operator=(BitStreamReader &&) = delete; CircularBitStreamReader &operator=(CircularBitStreamReader &&) = delete;
~BitStreamReader() noexcept = default; ~CircularBitStreamReader() noexcept = default;
static std::unique_ptr<BitStreamReader> fromString(std::string_view s) { static std::unique_ptr<CircularBitStreamReader> fromString(std::string_view s) {
return std::make_unique<BitStreamReader>(reinterpret_cast<const std::uint8_t *>(s.data()), return std::make_unique<CircularBitStreamReader>(
reinterpret_cast<const std::uint8_t *>(s.data() + s.size())); reinterpret_cast<const std::uint8_t *>(s.data()),
reinterpret_cast<const std::uint8_t *>(s.data() + s.size()));
} }
[[nodiscard]] std::size_t tell() const noexcept { [[nodiscard]] std::size_t tell() const noexcept {
...@@ -62,4 +63,4 @@ namespace kyut { ...@@ -62,4 +63,4 @@ namespace kyut {
}; };
} // namespace kyut } // namespace kyut
#endif // INCLUDE_kyut_BitStreamReader_hpp #endif // INCLUDE_kyut_CircularBitStreamReader_hpp
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#include <cassert> #include <cassert>
#include <iostream> #include <iostream>
#include "../BitStreamReader.hpp" #include "../CircularBitStreamReader.hpp"
namespace kyut::watermarker { namespace kyut::watermarker {
namespace { namespace {
...@@ -23,7 +23,7 @@ namespace kyut::watermarker { ...@@ -23,7 +23,7 @@ namespace kyut::watermarker {
} }
} // namespace } // namespace
std::size_t embedFunctionOrdering(wasm::Module &module, BitStreamReader &stream, std::size_t divisions) { std::size_t embedFunctionOrdering(wasm::Module &module, CircularBitStreamReader &stream, std::size_t divisions) {
assert(2 <= divisions && divisions < 21 && "because 21! > 2^64"); assert(2 <= divisions && divisions < 21 && "because 21! > 2^64");
// Number of bits embedded in the module // Number of bits embedded in the module
......
...@@ -4,11 +4,11 @@ ...@@ -4,11 +4,11 @@
#include <wasm.h> #include <wasm.h>
namespace kyut { namespace kyut {
class BitStreamReader; class CircularBitStreamReader;
} }
namespace kyut::watermarker { namespace kyut::watermarker {
std::size_t embedFunctionOrdering(wasm::Module &module, BitStreamReader &stream, std::size_t divisions); std::size_t embedFunctionOrdering(wasm::Module &module, CircularBitStreamReader &stream, std::size_t divisions);
} }
#endif // INCLUDE_kyut_watermark_FunctionOrderingWatermarker_hpp #endif // INCLUDE_kyut_watermark_FunctionOrderingWatermarker_hpp
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#include <wasm-io.h> #include <wasm-io.h>
#include "kyut/BitStreamReader.hpp" #include "kyut/CircularBitStreamReader.hpp"
#include "kyut/watermarker/FunctionOrderingWatermarker.hpp" #include "kyut/watermarker/FunctionOrderingWatermarker.hpp"
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
...@@ -22,7 +22,7 @@ int main(int argc, char *argv[]) { ...@@ -22,7 +22,7 @@ int main(int argc, char *argv[]) {
wasm::ModuleReader{}.read(inputFile, module); wasm::ModuleReader{}.read(inputFile, module);
// Embed watermarks // Embed watermarks
const auto stream = kyut::BitStreamReader::fromString(watermark); const auto stream = kyut::CircularBitStreamReader::fromString(watermark);
kyut::watermarker::embedFunctionOrdering(module, *stream, 10); kyut::watermarker::embedFunctionOrdering(module, *stream, 10);
......
#include <kyut/BitStreamReader.hpp> #include <kyut/CircularBitStreamReader.hpp>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(kyut) BOOST_AUTO_TEST_SUITE(kyut)
BOOST_AUTO_TEST_SUITE(bit_stream_reader) BOOST_AUTO_TEST_SUITE(circular_bit_stream_reader)
BOOST_AUTO_TEST_CASE(read_bit) { BOOST_AUTO_TEST_CASE(read_bit) {
constexpr std::uint8_t data[1] = {0x89}; constexpr std::uint8_t data[1] = {0x89};
BitStreamReader reader{std::begin(data), std::end(data)}; CircularBitStreamReader reader{std::begin(data), std::end(data)};
// 0x89 == 0b1000'1001 // 0x89 == 0b1000'1001
BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{0}); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{0});
...@@ -41,7 +41,7 @@ BOOST_AUTO_TEST_CASE(read_bit) { ...@@ -41,7 +41,7 @@ BOOST_AUTO_TEST_CASE(read_bit) {
BOOST_AUTO_TEST_CASE(read) { BOOST_AUTO_TEST_CASE(read) {
constexpr std::uint8_t data[4] = {0x89, 0xab, 0xcd, 0xef}; constexpr std::uint8_t data[4] = {0x89, 0xab, 0xcd, 0xef};
BitStreamReader reader{std::begin(data), std::end(data)}; CircularBitStreamReader reader{std::begin(data), std::end(data)};
BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{0}); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{0});
BOOST_REQUIRE_EQUAL(reader.read(4), std::uint64_t{0x8}); BOOST_REQUIRE_EQUAL(reader.read(4), std::uint64_t{0x8});
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
#include <wasm-io.h> #include <wasm-io.h>
#include <kyut/BitStreamReader.hpp> #include <kyut/CircularBitStreamReader.hpp>
BOOST_AUTO_TEST_SUITE(kyut) BOOST_AUTO_TEST_SUITE(kyut)
...@@ -24,7 +24,7 @@ BOOST_AUTO_TEST_CASE(function_ordering_watermarker) { ...@@ -24,7 +24,7 @@ BOOST_AUTO_TEST_CASE(function_ordering_watermarker) {
// Embed 0b00 // Embed 0b00
{ {
BitStreamReader s{{0b0000'0000}}; CircularBitStreamReader s{{0b0000'0000}};
const auto numBitsEmbedded = embedFunctionOrdering(module, s, 10); const auto numBitsEmbedded = embedFunctionOrdering(module, s, 10);
BOOST_REQUIRE_EQUAL(numBitsEmbedded, std::size_t{2}); BOOST_REQUIRE_EQUAL(numBitsEmbedded, std::size_t{2});
...@@ -39,7 +39,7 @@ BOOST_AUTO_TEST_CASE(function_ordering_watermarker) { ...@@ -39,7 +39,7 @@ BOOST_AUTO_TEST_CASE(function_ordering_watermarker) {
// Embed 0b01 // Embed 0b01
{ {
BitStreamReader s{{0b0100'0000}}; CircularBitStreamReader s{{0b0100'0000}};
const auto numBitsEmbedded = embedFunctionOrdering(module, s, 10); const auto numBitsEmbedded = embedFunctionOrdering(module, s, 10);
BOOST_REQUIRE_EQUAL(numBitsEmbedded, std::size_t{2}); BOOST_REQUIRE_EQUAL(numBitsEmbedded, std::size_t{2});
...@@ -54,7 +54,7 @@ BOOST_AUTO_TEST_CASE(function_ordering_watermarker) { ...@@ -54,7 +54,7 @@ BOOST_AUTO_TEST_CASE(function_ordering_watermarker) {
// Embed 0b10 // Embed 0b10
{ {
BitStreamReader s{{0b1000'0000}}; CircularBitStreamReader s{{0b1000'0000}};
const auto numBitsEmbedded = embedFunctionOrdering(module, s, 10); const auto numBitsEmbedded = embedFunctionOrdering(module, s, 10);
BOOST_REQUIRE_EQUAL(numBitsEmbedded, std::size_t{2}); BOOST_REQUIRE_EQUAL(numBitsEmbedded, std::size_t{2});
...@@ -69,7 +69,7 @@ BOOST_AUTO_TEST_CASE(function_ordering_watermarker) { ...@@ -69,7 +69,7 @@ BOOST_AUTO_TEST_CASE(function_ordering_watermarker) {
// Embed 0b11 // Embed 0b11
{ {
BitStreamReader s{{0b1100'0000}}; CircularBitStreamReader s{{0b1100'0000}};
const auto numBitsEmbedded = embedFunctionOrdering(module, s, 10); const auto numBitsEmbedded = embedFunctionOrdering(module, s, 10);
BOOST_REQUIRE_EQUAL(numBitsEmbedded, std::size_t{2}); BOOST_REQUIRE_EQUAL(numBitsEmbedded, std::size_t{2});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment