Untitled
unknown
c_cpp
3 years ago
2.3 kB
4
Indexable
#include <stdio.h> #include <iostream> #include <ios> uint32_t getFileSize(auto* f) { fseek(f, 0L, SEEK_END); uint32_t sz = ftell(f); rewind(f); return sz; } std::pair<char*, uint32_t> readFile(const char* name) { auto in = fopen(name, "rb"); if (!in) throw std::runtime_error("can\'t read input file"); uint32_t sz = getFileSize(in); std::cout << "fsize = " << sz << '\n'; char* buff = (char*)malloc(sz + 1); buff[0] = 0; uint32_t readed = fread(buff, sizeof(char), sz, in); if (readed != sz) throw std::runtime_error("i\'m a lazy asshole"); fclose(in); return {buff, sz}; }; void writeFile(const char* fileName, const char* buff, uint32_t offset, uint32_t size) { std::cout << "write from position " << std::hex << offset << " " << std::dec << size << "bytes\n"; auto out = fopen(fileName, "wb"); fwrite(buff + offset, sizeof(char), size, out); fclose(out); } std::pair<uint32_t, uint32_t> findObject3d(const char* buff, uint32_t fsize) { 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 sectionSize = *(uint32_t*)(buff + pos + 8); std::cout << "found tag " << std::hex << tag << std::dec << '\n'; std::cout << "section size " << sectionSize << '\n'; pos += 12; // skip tag, section id, size fields } while (pos + sectionSize + 12 < fsize && tag != 0x62212D88); if (pos >= fsize) { throw std::runtime_error("pos > fsize"); } if (pos + sectionSize > fsize) { throw std::runtime_error("section size overlaps file size"); } return {pos, sectionSize}; } std::pair<char*, char*> getFileNames(int c, char** v) { if (c != 3) { std::cout << "usage: " << "./main <input_filename> <output_filename>\n"; exit(0); } return {v[1], v[2]}; }; int main(int argc, char** argv) { auto [input_file, output_file] = getFileNames(argc, argv); auto [buff, sz] = readFile(input_file); auto [pos, sectionSize] = findObject3d(buff, sz); writeFile(output_file, buff, pos, sectionSize); return 0; }
Editor is loading...