LoginSignup
0
0

More than 5 years have passed since last update.

[boost::range]chunkをsliceで分割してheaderをjoinしてみる

Last updated at Posted at 2017-05-21

バイナリをchunkで分割し、そのサイズをヘッダーに付与してみた。
サイズは2byteで表現可能な64KBまで(256 x head[3] + head[2])

評価コード

#include <iostream>
#include <iterator> 
#include <fstream>
#include <sstream>
#include <boost/range/adaptor/sliced.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/range/join.hpp>

int main(){
  std::ifstream input( "1.jpg", std::ios::binary );
  std::vector<char> buffer((
    std::istreambuf_iterator<char>(input)), 
    (std::istreambuf_iterator<char>()));

  size_t chunkSize = 64 * 1024 -1; // 65535
  char head[4] = { 0x02, 0x01 };
  size_t end = 0;
  size_t byteSize = buffer.size();

  while(end < byteSize){
    size_t pos = end;
    end += chunkSize;
    if (end > byteSize)
      end = byteSize;

    int len = end - pos;
    head[2] = len;
    head[3] = int(len / 256);

    std::string str;
    boost::copy(
      boost::join(head, 
        buffer | boost::adaptors::sliced(pos,end)),
      std::back_inserter(str));

    // output test
    std::cout << "binary: ";
    std::stringstream ss;
    for (int x = 0; x < 10; x++)
      ss << std::hex << (int)(unsigned char)str[x];

    std::cout << ss.str();
    ss.str("");
    std::cout << " ... ";

    for (int x = str.size()-10; x < str.size(); x++)
      ss << std::hex << (int)(unsigned char)str[x];

    std::cout << ss.str() << std::endl;
    std::cout << "size: " << str.size() << std::endl;

    // std::ofstream ofs("out.bin", std::ios::binary);
    // ofs.write(&str[0], str.size());
  }
}

出力

binary: 21ffffffd8ffe0010 ... 1eae66bba547bd8836d8
size: 65539
binary: 21558d77b32e3232e1 ... 78d137b3c12e2bc1ffd9
size: 36185
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0