Commit 2cc70c02 authored by nagayama15's avatar nagayama15

任意の長さの文字列を透かしとして埋め込めるように機能拡張

parent 5637f0cb
......@@ -4,6 +4,7 @@
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string_view>
#include <vector>
......@@ -12,9 +13,9 @@ namespace nykk
class CircularBitStream
{
public:
static CircularBitStream from_string(std::string_view s)
static std::unique_ptr<CircularBitStream> from_string(std::string_view s)
{
return CircularBitStream { reinterpret_cast<const std::byte*>(s.data()), s.size() };
return std::make_unique<CircularBitStream>(reinterpret_cast<const std::byte*>(s.data()), s.size());
}
explicit CircularBitStream(const std::byte* pointer, std::size_t length)
......@@ -24,12 +25,12 @@ namespace nykk
assert(pointer != nullptr || length == 0);
}
// Uncopyable, movable.
// Uncopyable, unmovable.
CircularBitStream(const CircularBitStream&) =delete;
CircularBitStream(CircularBitStream&&) =default;
CircularBitStream(CircularBitStream&&) =delete;
CircularBitStream& operator=(const CircularBitStream&) =delete;
CircularBitStream& operator=(CircularBitStream&&) =default;
CircularBitStream& operator=(CircularBitStream&&) =delete;
~CircularBitStream() =default;
......
......@@ -3,6 +3,7 @@
#include <llvm/Pass.h>
#include <llvm/Support/raw_ostream.h>
#include "../CircularBitStream.hpp"
#include "../PermutationTable.hpp"
#include "Opts.hpp"
......@@ -65,8 +66,8 @@ namespace
explicit BlockWatermarkPass()
: FunctionPass(ID)
, module_name_()
, bit_stream_()
, perm_table_()
, bit_pos_(0)
{
}
......@@ -89,8 +90,8 @@ namespace
bool doInitialization(llvm::Module& module) override
{
module_name_ = module.getName();
bit_stream_ = nykk::CircularBitStream::from_string(nykk::pass::watermark_opt);
perm_table_ = nykk::create_permutation_table(partition_opt.getValue().value);
bit_pos_ = 0;
llvm::errs() << "func" << ", " << "blocks" << ", " << "bits" << "\n";
......@@ -144,12 +145,10 @@ namespace
std::size_t num_embedded_bits = 0;
std::size_t block_index = 1; // Without entry block.
const auto bit_mask = (1 << possible_embedding_bits[partition]) - 1;
for (; block_index + partition <= blocks.size(); block_index += partition)
{
// Part of watermark to embed.
const auto data = (nykk::pass::watermark_opt >> bit_pos_) & bit_mask;
const auto data = bit_stream_->read(possible_embedding_bits[partition]);
// Shuffles each `partition` blocks.
for (std::size_t i = 0; i < partition; i++)
......@@ -161,8 +160,6 @@ namespace
}
num_embedded_bits += possible_embedding_bits[partition];
bit_pos_ += possible_embedding_bits[partition];
bit_pos_ %= sizeof(std::uint32_t) * 8;
}
// Inserts rest blocks.
......@@ -179,8 +176,8 @@ namespace
private:
std::string module_name_;
std::unique_ptr<nykk::CircularBitStream> bit_stream_;
std::vector<std::vector<std::uint8_t>> perm_table_;
std::uint32_t bit_pos_;
};
char BlockWatermarkPass::ID;
......
......@@ -5,7 +5,7 @@
namespace nykk::pass
{
const llvm::cl::opt<int> watermark_opt
const llvm::cl::opt<std::string> watermark_opt
{
"watermark",
llvm::cl::desc("Watermark (32bit)"),
......
......@@ -7,22 +7,22 @@ inline void run_test_circular_bit_stream()
{
auto s = nykk::CircularBitStream::from_string(u8"\xab\xcd\xef\x98");
assert(s.size_bytes() == 4);
assert(s.size_bits() == 32);
assert(s->size_bytes() == 4);
assert(s->size_bits() == 32);
assert(s.read_bit() == true);
assert(s.read_bit() == true);
assert(s.read_bit() == false);
assert(s.pos_bit() == 3);
assert(s->read_bit() == true);
assert(s->read_bit() == true);
assert(s->read_bit() == false);
assert(s->pos_bit() == 3);
assert(s.read(17) == 0x1f9b5);
assert(s.pos_bit() == 20);
assert(s->read(17) == 0x1f9b5);
assert(s->pos_bit() == 20);
assert(s.read(16) == 0xb98e);
assert(s.pos_bit() == 4);
assert(s->read(16) == 0xb98e);
assert(s->pos_bit() == 4);
assert(s.read(64) == 0xb98efcdab98efcda);
assert(s.pos_bit() == 4);
assert(s->read(64) == 0xb98efcdab98efcda);
assert(s->pos_bit() == 4);
}
#endif
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