Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cstring>
  5. #include <string>
  6. #include <math.h>
  7.  
  8. using namespace std;
  9.  
  10. const int HEAD[8] = {-119, 80, 78, 71, 13, 10, 26, 10};
  11.  
  12. bool check_png(string file_name)
  13. {
  14.     ifstream file;
  15.     int length;
  16.     int position = 8;
  17.  
  18.     cout << "Attempting to open " << file_name << endl;
  19.  
  20.     file.open(file_name, ios::binary);
  21.     length = file.rdbuf()->pubseekoff(0, ios_base::end);  
  22.     file.rdbuf()->pubseekoff(0, ios_base::beg);
  23.  
  24.     char *array = new char[length];
  25.  
  26.     file.read(array, length);
  27.  
  28.     for (int i = 0; i < position; i++)
  29.     {
  30.         if ((int)array[i] != HEAD[i])
  31.         {
  32.            
  33.             cout << "PNG signature ERROR" << endl;
  34.             break;
  35.             return -1;
  36.         }
  37.     }
  38.    
  39.     bool idat = false; .
  40.  
  41.  
  42.     while (position < length)
  43.    
  44.     {
  45.         cout << "Position[" << position << "]\t";
  46.  
  47.         int length_block = 0;
  48.         string name_block = "";
  49.         string crc = "";
  50.  
  51.         for (int i = 0; i < 4; i++)
  52.         {
  53.             length_block += abs((uint8_t)array[position + i]) * pow(16, 2 * (3 - i));
  54.         }
  55.  
  56.         position += 4;
  57.  
  58.         for (int i = 0; i < 4; i++)
  59.         {
  60.             name_block += array[position + i];
  61.         }
  62.         if (position == 12)
  63.         {
  64.             if (name_block != "IHDR")
  65.             {
  66.                 cout << " IHDR ERROR" << endl;
  67.                 return false;
  68.             }
  69.         }
  70.         if ((int)length_block + 8 > length - position)
  71.         {
  72.             cout << " IEND ERROR" << endl;
  73.             return false;
  74.         }
  75.         cout << " Name[" << name_block << "]\t";
  76.         cout << " CRC[";
  77.         for (int i = length_block + 4; i < length_block + 8; i++)
  78.         {
  79.            
  80.             cout << dec << (int)array[position + i] << " ";
  81.         }
  82.         cout << "]" << endl;
  83.  
  84.  
  85.         if (name_block == "IDAT")
  86.         {
  87.             idat = true;
  88.         }  
  89.  
  90.     }
  91.  
  92.     if (idat = false)
  93.     {
  94.         cout << endl << "IDAT ERROR (not found)" << endl;
  95.         return false;
  96.     }
  97.  
  98.     return true;
  99. }
  100.  
  101. int main()
  102. {
  103.         string filename;
  104.         cout << "Enter path to file: ";
  105.         cin >> filename;
  106.         cout << endl;
  107.  
  108.         if (check_png(filename)==1)
  109.         {
  110.             cout << "File correct" << endl;
  111.         }
  112.  
  113.     system("pause");
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement