Guest User

main.cpp

a guest
Dec 7th, 2024
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. /*
  6. Tutoj kod dobyvajet iz .*dat arhivov igry Apocalypse 3000  .*png fajly
  7. */
  8.  
  9. const unsigned char png_signature[] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
  10.  
  11. int find_png_signature(const std::vector<unsigned char>& data, int start) {
  12.     for (int i = start; i < data.size() - sizeof(png_signature); ++i) {
  13.         if (memcmp(&data[i], png_signature, sizeof(png_signature)) == 0) {
  14.             return i;
  15.         }
  16.     }
  17.     return -1;
  18. }
  19.  
  20. int main() {
  21.     std::string dat_file = "intro.dat";
  22.     std::fstream file(dat_file, std::ios::binary | std::ios::in);
  23.  
  24.     if (!file.is_open()) {
  25.         std::cerr << "Error: Could not open file: " << dat_file << std::endl;
  26.         return 1;
  27.     }
  28.  
  29.     file.seekg(0, std::ios::end);
  30.     std::streamsize size = file.tellg();
  31.     file.seekg(0, std::ios::beg);
  32.     std::vector<unsigned char> data(size);
  33.     file.read(reinterpret_cast<char*>(data.data()), size);
  34.  
  35.     int current_position = 0;
  36.     int png_start = find_png_signature(data, current_position);
  37.     int image_count = 0;
  38.  
  39.     while (png_start != -1) {
  40.         std::vector<unsigned char> png_data(data.begin() + png_start, data.end());
  41.  
  42.         std::ofstream output_file("extracted_png_" + std::to_string(image_count) + ".png", std::ios::binary);
  43.         if (!output_file.is_open()) {
  44.             std::cerr << "Error: Could not create output file" << std::endl;
  45.         } else {
  46.             output_file.write(reinterpret_cast<char*>(png_data.data()), png_data.size());
  47.             output_file.close();
  48.             std::cout << "Extracted PNG: extracted_png_" << image_count << ".png" << std::endl;
  49.             image_count++;
  50.         }
  51.  
  52.         current_position = png_start + sizeof(png_signature);
  53.         png_start = find_png_signature(data, current_position);
  54.     }
  55.  
  56.     file.close();
  57.     return 0;
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment