This repository has been archived on 2021-06-27. You can view files and clone it, but cannot push or open issues or pull requests.
modetw/src/calibrate.cpp

68 lines
1.6 KiB
C++

#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <iostream>
#include <openssl/md5.h> /* md5sum */
#include "../include/calibrate.hpp"
#include "../include/exceptionhandler.hpp"
unsigned int Calibrate::_i = 0;
Calibrate::Calibrate(const std::string &path, const std::string &md5)
: Generic(std::move(path)), _md5(std::move(md5)) {}
bool Calibrate::CompareMD5(unsigned char *md5) const
{
/*
* Variable initialisation
*/
int file_descript;
unsigned long file_size;
unsigned char* file_buffer;
/* Checks if the file path can be opened. */
file_descript = open(_path.c_str(), O_RDONLY);
if (file_descript < 0)
{
throw ExceptionHandler(INVALID_PATH_ERROR, _path);
}
/* Grabs the size of file */
file_size = GetSizeByFD(file_descript);
/* Generates the buffer */
file_buffer = static_cast<unsigned char*>(mmap(static_cast<caddr_t>(0), file_size, PROT_READ, MAP_SHARED, file_descript, 0));
/* Computes the MD5 checksum to result */
MD5(file_buffer, file_size, md5);
/* Removes fime_buffer and file_size */
munmap(file_buffer, file_size);
MD5_wrapper md5_wrapper(md5);
if (_md5.GetString() != md5_wrapper.GetString())
{
std::cout << "WARNING! " << _md5.GetString() << " != " << md5_wrapper.GetString() << "\n";
return false;
}
return true;
}
/**
* Get the size of the file by its file descriptor
*
* @param file_descript
*/
unsigned long Calibrate::GetSizeByFD(const int fd) const
{
struct stat statbuf;
if (fstat(fd, &statbuf) < 0)
{
exit(EXIT_FAILURE);
}
return statbuf.st_size;
}