Commit e6723dd1 authored by nagayama15's avatar nagayama15

add: Add bit stream reader

parent c419a4f5
......@@ -16,4 +16,10 @@
(local.get $c)
)
)
(func $inc (param $a i32) (result i32)
(i32.add
(local.get $a)
(i32.const 1)
)
)
)
#ifndef INCLUDE_kyut_BitStreamReader_hpp
#define INCLUDE_kyut_BitStreamReader_hpp
#include <cstdint>
#include <memory>
#include <string_view>
#include <vector>
namespace kyut {
class BitStreamReader {
public:
explicit BitStreamReader(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}) {}
BitStreamReader(const BitStreamReader &) = delete;
BitStreamReader(BitStreamReader &&) = delete;
BitStreamReader &operator=(const BitStreamReader &) = delete;
BitStreamReader &operator=(BitStreamReader &&) = delete;
~BitStreamReader() 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()));
}
[[nodiscard]] std::size_t tell() const noexcept {
return pos_read_;
}
bool readBit() {
if (data_.empty()) {
return false;
}
const auto pos = pos_read_;
pos_read_ = (pos_read_ + 1) % data_.size();
return (data_[pos / 8] >> (pos % 8)) & 1;
}
std::uint64_t read(std::size_t countBits) {
std::uint64_t value = 0;
for (std::size_t i = 0; i < countBits; i++) {
value <<= 1;
value |= readBit();
}
return value;
}
private:
std::vector<std::uint8_t> data_;
std::size_t pos_read_;
};
} // namespace kyut
#endif // INCLUDE_kyut_BitStreamReader_hpp
......@@ -2,21 +2,27 @@
#include <wasm-io.h>
#include "kyut/BitStreamReader.hpp"
int main(int argc, char *argv[]) {
// Parse command line options
if (argc != 2) {
fmt::print(std::cerr, "{} <input file>\n", argv[0]);
if (argc != 3) {
fmt::print(std::cerr, "{} <input file> <watermark>\n", argv[0]);
return 1;
}
const std::string inputFile = argv[1];
const std::string watermark = argv[2];
try {
// Read the input module
wasm::Module module;
wasm::ModuleReader{}.read(inputFile, module);
// Embed watermarks
const auto stream = kyut::BitStreamReader::fromString(watermark);
// Output the result
wasm::ModuleWriter{}.writeText(module, "");
} catch (const wasm::ParseException &e) {
......
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