Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.4 kB
2
Indexable
Never
#include <stdio.h>
#include <iostream>
#include <ios>

const char* INPUT = "input";
const char* OUTPUT = "output";

uint32_t getFileSize(auto* f) {
  fseek(f, 0L, SEEK_END);
  uint32_t sz = ftell(f);
  rewind(f);
  return sz;
}


int main() {
  auto in = fopen(INPUT, "rb");
  uint32_t sz = getFileSize(in);

  std::cout << "fsize = " << sz << '\n';

  char* buff = (char*)malloc(sz + 1);
  buff[0] = 0;

  fread(buff, sizeof(char), sz, in);
  fclose(in);

  int pos = 12; // i don't know what in there
  uint32_t tag = 0;
  uint32_t sectionSize = 0;

  do { // iterate until find object3d, 16 - tag and all section data
    pos += sectionSize;
    tag = *(uint32_t*)(buff + pos); //get tag
    std::cout << "found tag " << std::hex << tag << std::dec << '\n';
    pos += 4; // skip tag
    pos += 4; // skip section id
    sectionSize = *(uint32_t*)(buff + pos);
    pos += 4; //skip size 
    std::cout << "section size " << sectionSize << '\n';
  } while (pos + sectionSize + 16 < sz && tag != 0x62212D88);
  pos += sectionSize + 16;
  if (pos >= sz) {
    std::cout << "pos(" << pos << ") > " << sz << '\n';
    return 1;
  }

  std::cout << "write from position " << pos << " " << sectionSize << "bytes\n";
  auto out = fopen(OUTPUT, "wb");
  fwrite(buff + pos, sizeof(char), sectionSize, out);
  return 0;
}