add error function, README and CI
This commit is contained in:
parent
93b81484a6
commit
63c0977ed6
7 changed files with 340 additions and 106 deletions
|
@ -22,48 +22,81 @@
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc != 3) {
|
||||
std::cout << "Usage: " << argv[0] << " [snapmatic] [jpegout]" << std::endl;
|
||||
std::cout << "Usage: " << argv[0] << " [photo] [jpegout]" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Initialise RagePhoto
|
||||
RagePhoto ragePhoto;
|
||||
// Make it crash when RagePhoto have a bug which make the deinitialisation to fail
|
||||
{
|
||||
// Initialise RagePhoto
|
||||
RagePhoto ragePhoto;
|
||||
|
||||
// Read file
|
||||
FILE *file = fopen(argv[1], "rb");
|
||||
if (!file)
|
||||
return -1;
|
||||
const int fseek_end_value = fseek(file, 0, SEEK_END);
|
||||
if (fseek_end_value == -1) {
|
||||
// Read file
|
||||
FILE *file = fopen(argv[1], "rb");
|
||||
if (!file) {
|
||||
std::cout << "Failed to open import file" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
const int fseek_end_value = fseek(file, 0, SEEK_END);
|
||||
if (fseek_end_value == -1) {
|
||||
fclose(file);
|
||||
std::cout << "Failed to read file" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
const size_t file_size = ftell(file);
|
||||
if (file_size == -1) {
|
||||
fclose(file);
|
||||
std::cout << "Failed to read file" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
const int fseek_set_value = fseek(file, 0, SEEK_SET);
|
||||
if (fseek_set_value == -1) {
|
||||
fclose(file);
|
||||
std::cout << "Failed to read file" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
char *data = static_cast<char*>(malloc(file_size));
|
||||
const size_t file_rsize = fread(data, 1, file_size, file);
|
||||
if (file_size != file_rsize) {
|
||||
fclose(file);
|
||||
std::cout << "Failed to read file" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
fclose(file);
|
||||
return -1;
|
||||
}
|
||||
size_t file_size = ftell(file);
|
||||
if (file_size == -1) {
|
||||
fclose(file);
|
||||
return -1;
|
||||
}
|
||||
const int fseek_set_value = fseek(file, 0, SEEK_SET);
|
||||
if (fseek_set_value == -1) {
|
||||
fclose(file);
|
||||
return -1;
|
||||
}
|
||||
char *data = static_cast<char*>(malloc(file_size));
|
||||
const size_t file_rsize = fread(data, 1, file_size, file);
|
||||
if (file_size != file_rsize) {
|
||||
fclose(file);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ragePhoto.load(data, file_size);
|
||||
fclose(file);
|
||||
// Load Photo
|
||||
const bool loaded = ragePhoto.load(data, file_size);
|
||||
|
||||
// Write jpeg
|
||||
file = fopen(argv[2], "wb");
|
||||
if (!file)
|
||||
return -1;
|
||||
fwrite(ragePhoto.photoData(), sizeof(char), ragePhoto.photoSize(), file);
|
||||
fclose(file);
|
||||
// Deinitialise data after Photo loaded
|
||||
free(data);
|
||||
|
||||
if (!loaded) {
|
||||
const RagePhoto::Error error = ragePhoto.error();
|
||||
if (error <= RagePhoto::Error::PhotoReadError) {
|
||||
std::cout << "Failed to load photo" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Write jpeg
|
||||
file = fopen(argv[2], "wb");
|
||||
if (!file) {
|
||||
std::cout << "Failed to open export file" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
const size_t written = fwrite(ragePhoto.photoData(), sizeof(char), ragePhoto.photoSize(), file);
|
||||
fclose(file);
|
||||
|
||||
if (written != ragePhoto.photoSize()) {
|
||||
std::cout << "Failed to write file" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::cout << "Photo successfully exported" << std::endl;
|
||||
|
||||
// Clear RagePhoto (provocate crash when pointer leak)
|
||||
ragePhoto.clear();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -31,7 +31,9 @@
|
|||
|
||||
RagePhoto::RagePhoto()
|
||||
{
|
||||
p_photoLoaded = false;
|
||||
p_photoData = nullptr;
|
||||
p_error = Error::Uninitialised;
|
||||
}
|
||||
|
||||
RagePhoto::~RagePhoto()
|
||||
|
@ -39,29 +41,52 @@ RagePhoto::~RagePhoto()
|
|||
free(p_photoData);
|
||||
}
|
||||
|
||||
void RagePhoto::clear()
|
||||
{
|
||||
if (p_photoLoaded) {
|
||||
free(p_photoData);
|
||||
p_photoData = nullptr;
|
||||
p_photoLoaded = false;
|
||||
}
|
||||
p_descriptionString.clear();
|
||||
p_jsonString.clear();
|
||||
p_photoString.clear();
|
||||
p_titleString.clear();
|
||||
p_error = Error::Uninitialised;
|
||||
}
|
||||
|
||||
bool RagePhoto::load(const char *data, size_t length)
|
||||
{
|
||||
#ifdef RAGEPHOTO_BENCHMARK
|
||||
auto benchmark_parse_start = std::chrono::high_resolution_clock::now();
|
||||
#endif
|
||||
|
||||
// Avoid data conflicts
|
||||
clear();
|
||||
|
||||
size_t pos = 0;
|
||||
char uInt32Buffer[4];
|
||||
size_t size = bRead(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4)
|
||||
size_t size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4) {
|
||||
p_error = Error::NoFormatIdentifier; // 1
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t format = charToUInt32LE(uInt32Buffer);
|
||||
if (format == static_cast<uint32_t>(PhotoFormat::GTA5)) {
|
||||
char photoHeader[256];
|
||||
size = bRead(data, photoHeader, &pos, 256, length);
|
||||
if (size != 256)
|
||||
size = readBuffer(data, photoHeader, &pos, 256, length);
|
||||
if (size != 256) {
|
||||
p_error = Error::IncompleteHeader; // 3
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef USE_ICONV
|
||||
iconv_t iconv_in = iconv_open("UTF-8", "UTF-16LE");
|
||||
if (iconv_in == (iconv_t)-1)
|
||||
if (iconv_in == (iconv_t)-1) {
|
||||
p_error = Error::UnicodeInitError; // 4
|
||||
return false;
|
||||
}
|
||||
char photoString[256];
|
||||
size_t src_s = sizeof(photoHeader);
|
||||
size_t dst_s = sizeof(photoString);
|
||||
|
@ -69,138 +94,193 @@ bool RagePhoto::load(const char *data, size_t length)
|
|||
char *dst = photoString;
|
||||
size_t ret = iconv(iconv_in, &src, &src_s, &dst, &dst_s);
|
||||
iconv_close(iconv_in);
|
||||
if (ret == static_cast<size_t>(-1))
|
||||
if (ret == static_cast<size_t>(-1)) {
|
||||
p_error = Error::UnicodeHeaderError; // 5
|
||||
return false;
|
||||
}
|
||||
p_photoString = std::string(photoString);
|
||||
#endif
|
||||
|
||||
size = bRead(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4)
|
||||
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4) {
|
||||
p_error = Error::IncompleteChecksum; // 6
|
||||
return false;
|
||||
}
|
||||
p_headerSum = charToUInt32LE(uInt32Buffer);
|
||||
|
||||
size = bRead(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4)
|
||||
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4) {
|
||||
p_error = Error::IncompleteEOF; // 7
|
||||
return false;
|
||||
}
|
||||
p_endOfFile = charToUInt32LE(uInt32Buffer);
|
||||
|
||||
size = bRead(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4)
|
||||
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4) {
|
||||
p_error = Error::IncompleteJsonOffset; // 8
|
||||
return false;
|
||||
}
|
||||
p_jsonOffset = charToUInt32LE(uInt32Buffer);
|
||||
|
||||
size = bRead(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4)
|
||||
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4) {
|
||||
p_error = Error::IncompleteTitleOffset; // 9
|
||||
return false;
|
||||
}
|
||||
p_titlOffset = charToUInt32LE(uInt32Buffer);
|
||||
|
||||
size = bRead(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4)
|
||||
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4) {
|
||||
p_error = Error::IncompleteDescOffset; // 10
|
||||
return false;
|
||||
}
|
||||
p_descOffset = charToUInt32LE(uInt32Buffer);
|
||||
|
||||
char markerBuffer[4];
|
||||
size = bRead(data, markerBuffer, &pos, 4, length);
|
||||
if (size != 4)
|
||||
size = readBuffer(data, markerBuffer, &pos, 4, length);
|
||||
if (size != 4) {
|
||||
p_error = Error::IncompleteJpegMarker; // 11
|
||||
return false;
|
||||
if (strncmp(markerBuffer, "JPEG", 4) != 0)
|
||||
}
|
||||
if (strncmp(markerBuffer, "JPEG", 4) != 0) {
|
||||
p_error = Error::IncorrectJpegMarker; // 12
|
||||
return false;
|
||||
}
|
||||
|
||||
size = bRead(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4)
|
||||
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4) {
|
||||
p_error = Error::IncompletePhotoBuffer; // 13
|
||||
return false;
|
||||
}
|
||||
p_photoBuffer = charToUInt32LE(uInt32Buffer);
|
||||
|
||||
size = bRead(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4)
|
||||
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4) {
|
||||
p_error = Error::IncompletePhotoSize; // 14
|
||||
return false;
|
||||
}
|
||||
p_photoSize = charToUInt32LE(uInt32Buffer);
|
||||
|
||||
p_photoData = static_cast<char*>(malloc(p_photoSize));
|
||||
if (!p_photoData) {
|
||||
p_error = Error::PhotoMallocError; // 15
|
||||
return false;
|
||||
}
|
||||
size = bRead(data, p_photoData, &pos, p_photoSize, length);
|
||||
size = readBuffer(data, p_photoData, &pos, p_photoSize, length);
|
||||
if (size != p_photoSize) {
|
||||
free(p_photoData);
|
||||
p_photoData = nullptr;
|
||||
p_error = Error::PhotoReadError; // 16
|
||||
return false;
|
||||
}
|
||||
p_photoLoaded = true;
|
||||
|
||||
pos = p_jsonOffset + 264;
|
||||
size = readBuffer(data, markerBuffer, &pos, 4, length);
|
||||
if (size != 4) {
|
||||
p_error = Error::IncompleteJsonMarker; // 17
|
||||
return false;
|
||||
}
|
||||
if (strncmp(markerBuffer, "JSON", 4) != 0) {
|
||||
p_error = Error::IncorrectJsonMarker; // 18
|
||||
return false;
|
||||
}
|
||||
|
||||
pos = p_jsonOffset + 264;
|
||||
size = bRead(data, markerBuffer, &pos, 4, length);
|
||||
if (size != 4)
|
||||
return false;
|
||||
if (strncmp(markerBuffer, "JSON", 4) != 0)
|
||||
return false;
|
||||
|
||||
size = bRead(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4)
|
||||
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4) {
|
||||
p_error = Error::IncompleteJsonBuffer; // 19
|
||||
return false;
|
||||
}
|
||||
p_jsonBuffer = charToUInt32LE(uInt32Buffer);
|
||||
|
||||
char *t_jsonData = static_cast<char*>(malloc(p_jsonBuffer));
|
||||
if (!t_jsonData)
|
||||
if (!t_jsonData) {
|
||||
p_error = Error::JsonMallocError; // 20
|
||||
return false;
|
||||
size = bRead(data, t_jsonData, &pos, p_jsonBuffer, length);
|
||||
}
|
||||
size = readBuffer(data, t_jsonData, &pos, p_jsonBuffer, length);
|
||||
if (size != p_jsonBuffer) {
|
||||
free(t_jsonData);
|
||||
p_error = Error::JsonReadError; // 21
|
||||
return false;
|
||||
}
|
||||
p_jsonString = std::string(t_jsonData);
|
||||
free(t_jsonData);
|
||||
|
||||
pos = p_titlOffset + 264;
|
||||
size = bRead(data, markerBuffer, &pos, 4, length);
|
||||
if (size != 4)
|
||||
size = readBuffer(data, markerBuffer, &pos, 4, length);
|
||||
if (size != 4) {
|
||||
p_error = Error::IncompleteTitleMarker; // 22
|
||||
return false;
|
||||
if (strncmp(markerBuffer, "TITL", 4) != 0)
|
||||
}
|
||||
if (strncmp(markerBuffer, "TITL", 4) != 0) {
|
||||
p_error = Error::IncorrectTitleMarker; // 23
|
||||
return false;
|
||||
}
|
||||
|
||||
size = bRead(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4)
|
||||
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4) {
|
||||
p_error = Error::IncompleteTitleBuffer; // 24
|
||||
return false;
|
||||
}
|
||||
p_titlBuffer = charToUInt32LE(uInt32Buffer);
|
||||
|
||||
char *t_titlData = static_cast<char*>(malloc(p_titlBuffer));
|
||||
if (!t_titlData)
|
||||
if (!t_titlData) {
|
||||
p_error = Error::TitleMallocError; // 25
|
||||
return false;
|
||||
size = bRead(data, t_titlData, &pos, p_titlBuffer, length);
|
||||
}
|
||||
size = readBuffer(data, t_titlData, &pos, p_titlBuffer, length);
|
||||
if (size != p_titlBuffer) {
|
||||
free(t_titlData);
|
||||
p_error = Error::TitleReadError; // 26
|
||||
return false;
|
||||
}
|
||||
p_titleString = std::string(t_titlData);
|
||||
free(t_titlData);
|
||||
|
||||
pos = p_descOffset + 264;
|
||||
size = bRead(data, markerBuffer, &pos, 4, length);
|
||||
if (size != 4)
|
||||
size = readBuffer(data, markerBuffer, &pos, 4, length);
|
||||
if (size != 4) {
|
||||
p_error = Error::IncompleteDescMarker; // 27
|
||||
return false;
|
||||
if (strncmp(markerBuffer, "DESC", 4) != 0)
|
||||
}
|
||||
if (strncmp(markerBuffer, "DESC", 4) != 0) {
|
||||
p_error = Error::IncorrectDescMarker; // 28
|
||||
return false;
|
||||
}
|
||||
|
||||
size = bRead(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4)
|
||||
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
||||
if (size != 4) {
|
||||
p_error = Error::IncompleteDescBuffer; // 29
|
||||
return false;
|
||||
}
|
||||
p_descBuffer = charToUInt32LE(uInt32Buffer);
|
||||
|
||||
char *t_descData = static_cast<char*>(malloc(p_descBuffer));
|
||||
if (!t_descData)
|
||||
if (!t_descData) {
|
||||
p_error = Error::DescMallocError; // 30
|
||||
return false;
|
||||
size = bRead(data, t_descData, &pos, p_descBuffer, length);
|
||||
}
|
||||
size = readBuffer(data, t_descData, &pos, p_descBuffer, length);
|
||||
if (size != p_descBuffer) {
|
||||
free(t_descData);
|
||||
p_error = Error::DescReadError; // 31
|
||||
return false;
|
||||
}
|
||||
p_descriptionString = std::string(t_descData);
|
||||
free(t_descData);
|
||||
|
||||
pos = p_endOfFile + 260;
|
||||
size = bRead(data, markerBuffer, &pos, 4, length);
|
||||
if (size != 4)
|
||||
size = readBuffer(data, markerBuffer, &pos, 4, length);
|
||||
if (size != 4) {
|
||||
p_error = Error::IncompleteJendMarker; // 32
|
||||
return false;
|
||||
if (strncmp(markerBuffer, "JEND", 4) != 0)
|
||||
}
|
||||
if (strncmp(markerBuffer, "JEND", 4) != 0) {
|
||||
p_error = Error::IncorrectJendMarker; // 33
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef RAGEPHOTO_BENCHMARK
|
||||
auto benchmark_parse_end = std::chrono::high_resolution_clock::now();
|
||||
|
@ -208,19 +288,32 @@ bool RagePhoto::load(const char *data, size_t length)
|
|||
std::cout << "Benchmark: " << benchmark_ns.count() << "ns" << std::endl;
|
||||
#endif
|
||||
|
||||
p_error = Error::NoError; // 255
|
||||
return true;
|
||||
}
|
||||
p_error = Error::IncompatibleFormat; // 2
|
||||
return false;
|
||||
}
|
||||
|
||||
RagePhoto::Error RagePhoto::error()
|
||||
{
|
||||
return p_error;
|
||||
}
|
||||
|
||||
const char* RagePhoto::photoData()
|
||||
{
|
||||
return p_photoData;
|
||||
if (p_photoLoaded)
|
||||
return p_photoData;
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const uint32_t RagePhoto::photoSize()
|
||||
{
|
||||
return p_photoSize;
|
||||
if (p_photoLoaded)
|
||||
return p_photoSize;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
const std::string RagePhoto::description()
|
||||
|
@ -243,7 +336,7 @@ const std::string RagePhoto::title()
|
|||
return p_titleString;
|
||||
}
|
||||
|
||||
size_t RagePhoto::bRead(const char *input, char *output, size_t *pos, size_t len)
|
||||
size_t RagePhoto::readBuffer(const char *input, char *output, size_t *pos, size_t len)
|
||||
{
|
||||
#ifdef READ_USE_FOR
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
|
@ -256,7 +349,7 @@ size_t RagePhoto::bRead(const char *input, char *output, size_t *pos, size_t len
|
|||
return len;
|
||||
}
|
||||
|
||||
size_t RagePhoto::bRead(const char *input, char *output, size_t *pos, size_t len, size_t inputLen)
|
||||
size_t RagePhoto::readBuffer(const char *input, char *output, size_t *pos, size_t len, size_t inputLen)
|
||||
{
|
||||
size_t readLen = 0;
|
||||
if (*pos >= inputLen)
|
||||
|
|
|
@ -27,14 +27,52 @@
|
|||
class LIBRAGEPHOTO_EXPORT RagePhoto
|
||||
{
|
||||
public:
|
||||
enum class Error : uint8_t {
|
||||
DescMallocError = 30,
|
||||
DescReadError = 31,
|
||||
IncompatibleFormat = 2,
|
||||
IncompleteChecksum = 6,
|
||||
IncompleteDescBuffer = 29,
|
||||
IncompleteDescMarker = 27,
|
||||
IncompleteDescOffset = 10,
|
||||
IncompleteEOF = 7,
|
||||
IncompleteHeader = 3,
|
||||
IncompleteJendMarker = 32,
|
||||
IncompleteJpegMarker = 11,
|
||||
IncompleteJsonBuffer = 19,
|
||||
IncompleteJsonMarker = 17,
|
||||
IncompleteJsonOffset = 8,
|
||||
IncompletePhotoBuffer = 13,
|
||||
IncompletePhotoSize = 14,
|
||||
IncompleteTitleBuffer = 24,
|
||||
IncompleteTitleMarker = 22,
|
||||
IncompleteTitleOffset = 9,
|
||||
IncorrectDescMarker = 28,
|
||||
IncorrectJendMarker = 33,
|
||||
IncorrectJpegMarker = 12,
|
||||
IncorrectJsonMarker = 18,
|
||||
IncorrectTitleMarker = 23,
|
||||
JsonMallocError = 20,
|
||||
JsonReadError = 21,
|
||||
NoError = 255,
|
||||
NoFormatIdentifier = 1,
|
||||
PhotoMallocError = 15,
|
||||
PhotoReadError = 16,
|
||||
TitleMallocError = 25,
|
||||
TitleReadError = 26,
|
||||
UnicodeHeaderError = 5,
|
||||
UnicodeInitError = 4,
|
||||
Uninitialised = 0,
|
||||
};
|
||||
enum class PhotoFormat : uint32_t {
|
||||
GTA5 = 0x01000000U,
|
||||
RDR2 = 0x04000000U,
|
||||
Undefined = 0,
|
||||
};
|
||||
RagePhoto();
|
||||
~RagePhoto();
|
||||
void clear();
|
||||
bool load(const char *data, size_t length);
|
||||
Error error();
|
||||
const char *photoData();
|
||||
const uint32_t photoSize();
|
||||
const std::string description();
|
||||
|
@ -43,13 +81,15 @@ public:
|
|||
const std::string title();
|
||||
|
||||
private:
|
||||
inline size_t bRead(const char *input, char *output, size_t *pos, size_t len);
|
||||
inline size_t bRead(const char *input, char *output, size_t *pos, size_t len, size_t inputLen);
|
||||
inline size_t readBuffer(const char *input, char *output, size_t *pos, size_t len);
|
||||
inline size_t readBuffer(const char *input, char *output, size_t *pos, size_t len, size_t inputLen);
|
||||
inline uint32_t charToUInt32BE(char *x);
|
||||
inline uint32_t charToUInt32LE(char *x);
|
||||
inline void uInt32ToCharBE(uint32_t x, char *y);
|
||||
inline void uInt32ToCharLE(uint32_t x, char *y);
|
||||
bool p_photoLoaded;
|
||||
char* p_photoData;
|
||||
Error p_error;
|
||||
std::string p_descriptionString;
|
||||
std::string p_jsonString;
|
||||
std::string p_photoString;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue