Commit 2cc70c02 authored by nagayama15's avatar nagayama15

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

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