Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <fstream>
- #include <cstring>
- #include <string>
- #include <math.h>
- using namespace std;
- const int HEAD[8] = {-119, 80, 78, 71, 13, 10, 26, 10};
- bool check_png(string file_name)
- {
- ifstream file;
- int length;
- int position = 8;
- cout << "Attempting to open " << file_name << endl;
- file.open(file_name, ios::binary);
- length = file.rdbuf()->pubseekoff(0, ios_base::end);
- file.rdbuf()->pubseekoff(0, ios_base::beg);
- char *array = new char[length];
- file.read(array, length);
- for (int i = 0; i < position; i++)
- {
- if ((int)array[i] != HEAD[i])
- {
- cout << "PNG signature ERROR" << endl;
- break;
- return -1;
- }
- }
- bool idat = false; .
- while (position < length)
- {
- cout << "Position[" << position << "]\t";
- int length_block = 0;
- string name_block = "";
- string crc = "";
- for (int i = 0; i < 4; i++)
- {
- length_block += abs((uint8_t)array[position + i]) * pow(16, 2 * (3 - i));
- }
- position += 4;
- for (int i = 0; i < 4; i++)
- {
- name_block += array[position + i];
- }
- if (position == 12)
- {
- if (name_block != "IHDR")
- {
- cout << " IHDR ERROR" << endl;
- return false;
- }
- }
- if ((int)length_block + 8 > length - position)
- {
- cout << " IEND ERROR" << endl;
- return false;
- }
- cout << " Name[" << name_block << "]\t";
- cout << " CRC[";
- for (int i = length_block + 4; i < length_block + 8; i++)
- {
- cout << dec << (int)array[position + i] << " ";
- }
- cout << "]" << endl;
- if (name_block == "IDAT")
- {
- idat = true;
- }
- }
- if (idat = false)
- {
- cout << endl << "IDAT ERROR (not found)" << endl;
- return false;
- }
- return true;
- }
- int main()
- {
- string filename;
- cout << "Enter path to file: ";
- cin >> filename;
- cout << endl;
- if (check_png(filename)==1)
- {
- cout << "File correct" << endl;
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement