Commit 7dd3140d authored by nagayama15's avatar nagayama15

Rename stream reader

parent 629cc5d1
#ifndef INCLUDE_kyut_BitStreamReader_hpp
#define INCLUDE_kyut_BitStreamReader_hpp
#ifndef INCLUDE_kyut_CircularBitStreamReader_hpp
#define INCLUDE_kyut_CircularBitStreamReader_hpp
#include <cstdint>
#include <memory>
......@@ -7,27 +7,28 @@
#include <vector>
namespace kyut {
class BitStreamReader {
class CircularBitStreamReader {
public:
explicit BitStreamReader(std::vector<std::uint8_t> data)
explicit CircularBitStreamReader(std::vector<std::uint8_t> data)
: data_(std::move(data))
, pos_read_(0) {}
template <typename Iterator>
explicit BitStreamReader(Iterator begin, Iterator end)
: BitStreamReader({begin, end}) {}
explicit CircularBitStreamReader(Iterator begin, Iterator end)
: CircularBitStreamReader({begin, end}) {}
BitStreamReader(const BitStreamReader &) = delete;
BitStreamReader(BitStreamReader &&) = delete;
CircularBitStreamReader(const CircularBitStreamReader &) = delete;
CircularBitStreamReader(CircularBitStreamReader &&) = delete;
BitStreamReader &operator=(const BitStreamReader &) = delete;
BitStreamReader &operator=(BitStreamReader &&) = delete;
CircularBitStreamReader &operator=(const CircularBitStreamReader &) = delete;
CircularBitStreamReader &operator=(CircularBitStreamReader &&) = delete;
~BitStreamReader() noexcept = default;
~CircularBitStreamReader() noexcept = default;
static std::unique_ptr<BitStreamReader> fromString(std::string_view s) {
return std::make_unique<BitStreamReader>(reinterpret_cast<const std::uint8_t *>(s.data()),
reinterpret_cast<const std::uint8_t *>(s.data() + s.size()));
static std::unique_ptr<CircularBitStreamReader> fromString(std::string_view s) {
return std::make_unique<CircularBitStreamReader>(
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 {
......@@ -62,4 +63,4 @@ namespace kyut {
};
} // namespace kyut
#endif // INCLUDE_kyut_BitStreamReader_hpp
#endif // INCLUDE_kyut_CircularBitStreamReader_hpp
......@@ -5,7 +5,7 @@
#include <cassert>
#include <iostream>
#include "../BitStreamReader.hpp"
#include "../CircularBitStreamReader.hpp"
namespace kyut::watermarker {
namespace {
......@@ -23,7 +23,7 @@ namespace kyut::watermarker {
}
} // 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");
// Number of bits embedded in the module
......
......@@ -4,11 +4,11 @@
#include <wasm.h>
namespace kyut {
class BitStreamReader;
class CircularBitStreamReader;
}
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
......@@ -2,7 +2,7 @@
#include <wasm-io.h>
#include "kyut/BitStreamReader.hpp"
#include "kyut/CircularBitStreamReader.hpp"
#include "kyut/watermarker/FunctionOrderingWatermarker.hpp"
int main(int argc, char *argv[]) {
......@@ -22,7 +22,7 @@ int main(int argc, char *argv[]) {
wasm::ModuleReader{}.read(inputFile, module);
// Embed watermarks
const auto stream = kyut::BitStreamReader::fromString(watermark);
const auto stream = kyut::CircularBitStreamReader::fromString(watermark);
kyut::watermarker::embedFunctionOrdering(module, *stream, 10);
......
#include <kyut/BitStreamReader.hpp>
#include <kyut/CircularBitStreamReader.hpp>
#include <boost/test/unit_test.hpp>
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) {
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
BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{0});
......@@ -41,7 +41,7 @@ BOOST_AUTO_TEST_CASE(read_bit) {
BOOST_AUTO_TEST_CASE(read) {
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.read(4), std::uint64_t{0x8});
......
......@@ -4,7 +4,7 @@
#include <wasm-io.h>
#include <kyut/BitStreamReader.hpp>
#include <kyut/CircularBitStreamReader.hpp>
BOOST_AUTO_TEST_SUITE(kyut)
......@@ -24,7 +24,7 @@ BOOST_AUTO_TEST_CASE(function_ordering_watermarker) {
// Embed 0b00
{
BitStreamReader s{{0b0000'0000}};
CircularBitStreamReader s{{0b0000'0000}};
const auto numBitsEmbedded = embedFunctionOrdering(module, s, 10);
BOOST_REQUIRE_EQUAL(numBitsEmbedded, std::size_t{2});
......@@ -39,7 +39,7 @@ BOOST_AUTO_TEST_CASE(function_ordering_watermarker) {
// Embed 0b01
{
BitStreamReader s{{0b0100'0000}};
CircularBitStreamReader s{{0b0100'0000}};
const auto numBitsEmbedded = embedFunctionOrdering(module, s, 10);
BOOST_REQUIRE_EQUAL(numBitsEmbedded, std::size_t{2});
......@@ -54,7 +54,7 @@ BOOST_AUTO_TEST_CASE(function_ordering_watermarker) {
// Embed 0b10
{
BitStreamReader s{{0b1000'0000}};
CircularBitStreamReader s{{0b1000'0000}};
const auto numBitsEmbedded = embedFunctionOrdering(module, s, 10);
BOOST_REQUIRE_EQUAL(numBitsEmbedded, std::size_t{2});
......@@ -69,7 +69,7 @@ BOOST_AUTO_TEST_CASE(function_ordering_watermarker) {
// Embed 0b11
{
BitStreamReader s{{0b1100'0000}};
CircularBitStreamReader s{{0b1100'0000}};
const auto numBitsEmbedded = embedFunctionOrdering(module, s, 10);
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