2021-08-25 00:30:10 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* libragephoto RAGE Photo Parser
|
2023-01-07 21:17:49 +01:00
|
|
|
* Copyright (C) 2021-2023 Syping
|
2021-08-25 00:30:10 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* This software is provided as-is, no warranties are given to you, we are not
|
|
|
|
* responsible for anything with use of the software, you are self responsible.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "RagePhoto.h"
|
2021-11-13 04:41:01 +01:00
|
|
|
#ifdef LIBRAGEPHOTO_C_API
|
|
|
|
#include "RagePhotoC.h"
|
|
|
|
#endif
|
|
|
|
|
2021-12-02 05:55:50 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2021-11-02 10:16:56 +01:00
|
|
|
#include <fstream>
|
2021-08-25 00:30:10 +02:00
|
|
|
#include <iostream>
|
2021-11-02 10:16:56 +01:00
|
|
|
#include <iterator>
|
2021-08-25 00:30:10 +02:00
|
|
|
|
|
|
|
#ifdef RAGEPHOTO_BENCHMARK
|
|
|
|
#include <chrono>
|
|
|
|
#endif
|
|
|
|
|
2021-11-07 20:32:06 +01:00
|
|
|
#if defined UNICODE_CODECVT
|
2021-08-27 01:47:09 +02:00
|
|
|
#include <codecvt>
|
|
|
|
#include <locale>
|
2021-11-07 20:32:06 +01:00
|
|
|
#elif defined UNICODE_ICONV
|
2021-08-27 01:47:09 +02:00
|
|
|
#include <iconv.h>
|
2021-11-07 20:32:06 +01:00
|
|
|
#elif defined UNICODE_WINCVT
|
2021-11-12 07:10:36 +01:00
|
|
|
#include <windows.h>
|
2021-11-07 20:32:06 +01:00
|
|
|
#include <stringapiset.h>
|
2021-08-25 00:30:10 +02:00
|
|
|
#endif
|
|
|
|
|
2021-11-18 18:00:09 +01:00
|
|
|
/* BEGIN OF STATIC LIBRARY FUNCTIONS */
|
|
|
|
inline size_t readBuffer(const char *input, void *output, size_t *pos, size_t outputLen, size_t inputLen)
|
|
|
|
{
|
|
|
|
size_t readLen = 0;
|
|
|
|
if (*pos >= inputLen)
|
|
|
|
return 0;
|
|
|
|
readLen = inputLen - *pos;
|
|
|
|
if (readLen > outputLen)
|
|
|
|
readLen = outputLen;
|
|
|
|
std::memcpy(output, &input[*pos], readLen);
|
|
|
|
*pos = *pos + readLen;
|
|
|
|
return readLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t writeBuffer(const void *input, char *output, size_t *pos, size_t outputLen, size_t inputLen)
|
|
|
|
{
|
|
|
|
const size_t maxLen = outputLen - *pos;
|
|
|
|
size_t writeLen = inputLen;
|
|
|
|
if (*pos >= outputLen)
|
|
|
|
return 0;
|
|
|
|
if (inputLen > maxLen)
|
|
|
|
writeLen = maxLen;
|
|
|
|
std::memcpy(&output[*pos], input, writeLen);
|
|
|
|
*pos = *pos + writeLen;
|
|
|
|
return writeLen;
|
|
|
|
}
|
|
|
|
|
2023-01-07 20:43:11 +01:00
|
|
|
inline bool writeDataChar(const char *input, char **output)
|
|
|
|
{
|
|
|
|
const size_t src_s = strlen(input) + 1;
|
|
|
|
if (*output) {
|
|
|
|
const size_t dst_s = strlen(*output) + 1;
|
|
|
|
if (dst_s > src_s) {
|
|
|
|
char *t_output = static_cast<char*>(std::realloc(*output, src_s));
|
|
|
|
if (!t_output) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*output = t_output;
|
|
|
|
std::memcpy(*output, input, src_s);
|
|
|
|
}
|
|
|
|
else if (dst_s < src_s) {
|
|
|
|
char *t_output = static_cast<char*>(std::malloc(src_s));
|
|
|
|
if (!t_output) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::free(*output);
|
|
|
|
*output = t_output;
|
|
|
|
std::memcpy(*output, input, src_s);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
std::memcpy(*output, input, src_s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char *t_output = static_cast<char*>(std::malloc(src_s));
|
|
|
|
if (!t_output) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*output = t_output;
|
|
|
|
std::memcpy(*output, input, src_s);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-18 18:00:09 +01:00
|
|
|
inline uint32_t charToUInt32LE(char *x)
|
|
|
|
{
|
|
|
|
return (static_cast<unsigned char>(x[3]) << 24 |
|
|
|
|
static_cast<unsigned char>(x[2]) << 16 |
|
|
|
|
static_cast<unsigned char>(x[1]) << 8 |
|
|
|
|
static_cast<unsigned char>(x[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void uInt32ToCharLE(uint32_t x, char *y)
|
|
|
|
{
|
|
|
|
y[0] = x;
|
|
|
|
y[1] = x >> 8;
|
|
|
|
y[2] = x >> 16;
|
|
|
|
y[3] = x >> 24;
|
|
|
|
}
|
|
|
|
/* END OF STATIC LIBRARY FUNCTIONS */
|
|
|
|
|
|
|
|
/* BEGIN OF RAGEPHOTO CLASS */
|
2021-08-25 00:30:10 +02:00
|
|
|
RagePhoto::RagePhoto()
|
|
|
|
{
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.photoData = nullptr;
|
2023-01-07 20:43:11 +01:00
|
|
|
m_data.description = nullptr;
|
|
|
|
m_data.json = nullptr;
|
|
|
|
m_data.header = nullptr;
|
|
|
|
m_data.title = nullptr;
|
2021-09-16 03:07:11 +02:00
|
|
|
setBufferDefault();
|
2021-08-25 00:30:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
RagePhoto::~RagePhoto()
|
|
|
|
{
|
2021-11-18 17:33:20 +01:00
|
|
|
std::free(m_data.photoData);
|
2023-01-07 20:43:11 +01:00
|
|
|
std::free(m_data.description);
|
|
|
|
std::free(m_data.json);
|
|
|
|
std::free(m_data.header);
|
|
|
|
std::free(m_data.title);
|
2021-08-25 00:30:10 +02:00
|
|
|
}
|
|
|
|
|
2021-08-26 00:22:11 +02:00
|
|
|
void RagePhoto::clear()
|
|
|
|
{
|
2023-01-07 20:43:11 +01:00
|
|
|
std::free(m_data.photoData);
|
|
|
|
m_data.photoData = nullptr;
|
|
|
|
std::free(m_data.description);
|
|
|
|
m_data.description = nullptr;
|
|
|
|
std::free(m_data.json);
|
|
|
|
m_data.json = nullptr;
|
|
|
|
std::free(m_data.header);
|
|
|
|
m_data.header = nullptr;
|
|
|
|
std::free(m_data.title);
|
|
|
|
m_data.title = nullptr;
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.error = 0U;
|
|
|
|
m_data.photoFormat = 0UL;
|
2021-11-22 14:01:06 +01:00
|
|
|
m_data.unnamedSum1 = 0UL;
|
|
|
|
m_data.unnamedSum2 = 0UL;
|
2021-09-16 03:07:11 +02:00
|
|
|
setBufferDefault();
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
|
|
|
|
2021-12-14 03:15:52 +01:00
|
|
|
RagePhotoData* RagePhoto::data()
|
|
|
|
{
|
|
|
|
return &m_data;
|
|
|
|
}
|
|
|
|
|
2021-08-25 00:30:10 +02:00
|
|
|
bool RagePhoto::load(const char *data, size_t length)
|
|
|
|
{
|
|
|
|
#ifdef RAGEPHOTO_BENCHMARK
|
|
|
|
auto benchmark_parse_start = std::chrono::high_resolution_clock::now();
|
|
|
|
#endif
|
|
|
|
|
2021-08-26 00:22:11 +02:00
|
|
|
// Avoid data conflicts
|
|
|
|
clear();
|
|
|
|
|
2021-08-25 00:30:10 +02:00
|
|
|
size_t pos = 0;
|
|
|
|
char uInt32Buffer[4];
|
2021-08-26 00:22:11 +02:00
|
|
|
size_t size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
|
|
|
if (size != 4) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::NoFormatIdentifier; // 1
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2021-08-25 00:30:10 +02:00
|
|
|
|
2021-08-29 02:35:25 +02:00
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(&m_data.photoFormat, uInt32Buffer, 4);
|
2021-08-29 02:35:25 +02:00
|
|
|
#else
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.photoFormat = charToUInt32LE(uInt32Buffer);
|
2021-08-29 02:35:25 +02:00
|
|
|
#endif
|
2021-10-28 03:28:36 +02:00
|
|
|
if (m_data.photoFormat == PhotoFormat::GTA5 || m_data.photoFormat == PhotoFormat::RDR2) {
|
2021-11-07 20:32:06 +01:00
|
|
|
#if defined UNICODE_ICONV || defined UNICODE_CODECVT || defined UNICODE_WINCVT
|
2021-08-25 00:30:10 +02:00
|
|
|
char photoHeader[256];
|
2021-08-26 00:22:11 +02:00
|
|
|
size = readBuffer(data, photoHeader, &pos, 256, length);
|
|
|
|
if (size != 256) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncompleteHeader; // 3
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2021-08-25 00:30:10 +02:00
|
|
|
|
2021-11-07 20:32:06 +01:00
|
|
|
#if defined UNICODE_CODECVT
|
2021-10-24 05:03:14 +02:00
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert;
|
2023-01-07 20:43:11 +01:00
|
|
|
const std::string photoHeader_string = convert.to_bytes(reinterpret_cast<char16_t*>(photoHeader));
|
2021-11-06 20:17:28 +01:00
|
|
|
if (convert.converted() == 0) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::UnicodeHeaderError; // 5
|
2021-11-06 20:17:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
2023-01-07 20:43:11 +01:00
|
|
|
const size_t photoHeader_size = photoHeader_string.size() + 1;
|
|
|
|
m_data.header = static_cast<char*>(std::malloc(photoHeader_size));
|
|
|
|
if (!m_data.header) {
|
|
|
|
// add MALLOC ERROR INDICATOR
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::memcpy(m_data.header, photoHeader_string.c_str(), photoHeader_size);
|
2021-11-07 20:32:06 +01:00
|
|
|
#elif defined UNICODE_ICONV
|
2021-08-25 00:30:10 +02:00
|
|
|
iconv_t iconv_in = iconv_open("UTF-8", "UTF-16LE");
|
2021-08-26 00:22:11 +02:00
|
|
|
if (iconv_in == (iconv_t)-1) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::UnicodeInitError; // 4
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2023-01-07 20:43:11 +01:00
|
|
|
m_data.header = static_cast<char*>(std::malloc(256));
|
|
|
|
if (!m_data.header) {
|
|
|
|
// add MALLOC ERROR INDICATOR
|
|
|
|
iconv_close(iconv_in);
|
|
|
|
return false;
|
|
|
|
}
|
2021-08-25 00:30:10 +02:00
|
|
|
size_t src_s = sizeof(photoHeader);
|
2023-01-07 21:17:49 +01:00
|
|
|
size_t dst_s = 256;
|
2021-08-25 00:30:10 +02:00
|
|
|
char *src = photoHeader;
|
2023-01-07 20:43:11 +01:00
|
|
|
char *dst = m_data.header;
|
2021-10-20 08:49:35 +02:00
|
|
|
const size_t ret = iconv(iconv_in, &src, &src_s, &dst, &dst_s);
|
2021-08-25 00:30:10 +02:00
|
|
|
iconv_close(iconv_in);
|
2021-08-26 00:22:11 +02:00
|
|
|
if (ret == static_cast<size_t>(-1)) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::UnicodeHeaderError; // 5
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2021-11-07 20:32:06 +01:00
|
|
|
#elif defined UNICODE_WINCVT
|
2023-01-07 20:43:11 +01:00
|
|
|
m_data.header = static_cast<char*>(std::malloc(256));
|
|
|
|
if (!m_data.header) {
|
|
|
|
// add MALLOC ERROR INDICATOR
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const int converted = WideCharToMultiByte(CP_UTF8, 0, reinterpret_cast<wchar_t*>(photoHeader), -1, m_data.header, 256, NULL, NULL);
|
2021-11-07 20:32:06 +01:00
|
|
|
if (converted == 0) {
|
2023-01-07 20:43:11 +01:00
|
|
|
std::free(m_data.header);
|
|
|
|
m_data.header = nullptr;
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::UnicodeHeaderError; // 5
|
2021-11-07 20:32:06 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-08-25 00:30:10 +02:00
|
|
|
#endif
|
|
|
|
|
2021-08-26 00:22:11 +02:00
|
|
|
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
|
|
|
if (size != 4) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncompleteChecksum; // 6
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2021-08-29 02:35:25 +02:00
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(&m_data.headerSum, uInt32Buffer, 4);
|
2021-08-29 02:35:25 +02:00
|
|
|
#else
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.headerSum = charToUInt32LE(uInt32Buffer);
|
2021-08-29 02:35:25 +02:00
|
|
|
#endif
|
2021-08-25 00:30:10 +02:00
|
|
|
|
2021-11-22 14:01:06 +01:00
|
|
|
if (m_data.photoFormat == PhotoFormat::RDR2) {
|
|
|
|
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
|
|
|
if (size != 4) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncompleteChecksum; // 6
|
2021-11-22 14:01:06 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
|
|
std::memcpy(&m_data.unnamedSum1, uInt32Buffer, 4);
|
|
|
|
#else
|
|
|
|
m_data.unnamedSum1 = charToUInt32LE(uInt32Buffer);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
|
|
|
if (size != 4) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncompleteChecksum; // 6
|
2021-11-22 14:01:06 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
|
|
std::memcpy(&m_data.unnamedSum2, uInt32Buffer, 4);
|
|
|
|
#else
|
|
|
|
m_data.unnamedSum2 = charToUInt32LE(uInt32Buffer);
|
|
|
|
#endif
|
|
|
|
}
|
2021-09-22 05:14:04 +02:00
|
|
|
const size_t headerSize = pos;
|
2021-09-03 15:21:02 +02:00
|
|
|
|
2021-08-26 00:22:11 +02:00
|
|
|
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
|
|
|
if (size != 4) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncompleteEOF; // 7
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2021-08-29 02:35:25 +02:00
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(&m_data.endOfFile, uInt32Buffer, 4);
|
2021-08-29 02:35:25 +02:00
|
|
|
#else
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.endOfFile = charToUInt32LE(uInt32Buffer);
|
2021-08-29 02:35:25 +02:00
|
|
|
#endif
|
2021-08-25 00:30:10 +02:00
|
|
|
|
2021-08-26 00:22:11 +02:00
|
|
|
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
|
|
|
if (size != 4) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncompleteJsonOffset; // 8
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2021-08-29 02:35:25 +02:00
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(&m_data.jsonOffset, uInt32Buffer, 4);
|
2021-08-29 02:35:25 +02:00
|
|
|
#else
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.jsonOffset = charToUInt32LE(uInt32Buffer);
|
2021-08-29 02:35:25 +02:00
|
|
|
#endif
|
2021-08-26 00:22:11 +02:00
|
|
|
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
|
|
|
if (size != 4) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncompleteTitleOffset; // 9
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2021-08-29 02:35:25 +02:00
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(&m_data.titlOffset, uInt32Buffer, 4);
|
2021-08-29 02:35:25 +02:00
|
|
|
#else
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.titlOffset = charToUInt32LE(uInt32Buffer);
|
2021-08-29 02:35:25 +02:00
|
|
|
#endif
|
2021-08-25 00:30:10 +02:00
|
|
|
|
2021-08-26 00:22:11 +02:00
|
|
|
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
|
|
|
if (size != 4) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncompleteDescOffset; // 10
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2021-08-29 02:35:25 +02:00
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(&m_data.descOffset, uInt32Buffer, 4);
|
2021-08-29 02:35:25 +02:00
|
|
|
#else
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.descOffset = charToUInt32LE(uInt32Buffer);
|
2021-08-29 02:35:25 +02:00
|
|
|
#endif
|
2021-08-25 00:30:10 +02:00
|
|
|
|
|
|
|
char markerBuffer[4];
|
2021-08-26 00:22:11 +02:00
|
|
|
size = readBuffer(data, markerBuffer, &pos, 4, length);
|
|
|
|
if (size != 4) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncompleteJpegMarker; // 11
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
|
|
|
if (strncmp(markerBuffer, "JPEG", 4) != 0) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncorrectJpegMarker; // 12
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2021-08-25 00:30:10 +02:00
|
|
|
|
2021-08-26 00:22:11 +02:00
|
|
|
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
|
|
|
if (size != 4) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncompletePhotoBuffer; // 13
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2021-08-29 02:35:25 +02:00
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(&m_data.photoBuffer, uInt32Buffer, 4);
|
2021-08-29 02:35:25 +02:00
|
|
|
#else
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.photoBuffer = charToUInt32LE(uInt32Buffer);
|
2021-08-29 02:35:25 +02:00
|
|
|
#endif
|
2021-08-25 00:30:10 +02:00
|
|
|
|
2021-08-26 00:22:11 +02:00
|
|
|
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
|
|
|
if (size != 4) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncompletePhotoSize; // 14
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2021-08-29 02:35:25 +02:00
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(&m_data.photoSize, uInt32Buffer, 4);
|
2021-08-29 02:35:25 +02:00
|
|
|
#else
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.photoSize = charToUInt32LE(uInt32Buffer);
|
2021-08-29 02:35:25 +02:00
|
|
|
#endif
|
2021-08-25 00:30:10 +02:00
|
|
|
|
2021-11-18 17:33:20 +01:00
|
|
|
m_data.photoData = static_cast<char*>(std::malloc(m_data.photoSize));
|
2021-10-28 03:28:36 +02:00
|
|
|
if (!m_data.photoData) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::PhotoMallocError; // 15
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-10-28 03:28:36 +02:00
|
|
|
size = readBuffer(data, m_data.photoData, &pos, m_data.photoSize, length);
|
|
|
|
if (size != m_data.photoSize) {
|
2021-11-18 17:33:20 +01:00
|
|
|
std::free(m_data.photoData);
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.photoData = nullptr;
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::PhotoReadError; // 16
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-28 03:28:36 +02:00
|
|
|
pos = m_data.jsonOffset + headerSize;
|
2021-08-26 00:22:11 +02:00
|
|
|
size = readBuffer(data, markerBuffer, &pos, 4, length);
|
|
|
|
if (size != 4) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncompleteJsonMarker; // 17
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
|
|
|
if (strncmp(markerBuffer, "JSON", 4) != 0) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncorrectJsonMarker; // 18
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2021-08-25 00:30:10 +02:00
|
|
|
|
2021-08-26 00:22:11 +02:00
|
|
|
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
|
|
|
if (size != 4) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncompleteJsonBuffer; // 19
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2021-08-29 02:35:25 +02:00
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(&m_data.jsonBuffer, uInt32Buffer, 4);
|
2021-08-29 02:35:25 +02:00
|
|
|
#else
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.jsonBuffer = charToUInt32LE(uInt32Buffer);
|
2021-08-29 02:35:25 +02:00
|
|
|
#endif
|
2021-08-25 00:30:10 +02:00
|
|
|
|
2023-01-07 20:43:11 +01:00
|
|
|
m_data.json = static_cast<char*>(std::malloc(m_data.jsonBuffer));
|
|
|
|
if (!m_data.json) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::JsonMallocError; // 20
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2023-01-07 20:43:11 +01:00
|
|
|
size = readBuffer(data, m_data.json, &pos, m_data.jsonBuffer, length);
|
2021-10-28 03:28:36 +02:00
|
|
|
if (size != m_data.jsonBuffer) {
|
2023-01-07 20:43:11 +01:00
|
|
|
std::free(m_data.json);
|
|
|
|
m_data.json = nullptr;
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::JsonReadError; // 21
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-28 03:28:36 +02:00
|
|
|
pos = m_data.titlOffset + headerSize;
|
2021-08-26 00:22:11 +02:00
|
|
|
size = readBuffer(data, markerBuffer, &pos, 4, length);
|
|
|
|
if (size != 4) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncompleteTitleMarker; // 22
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
|
|
|
if (strncmp(markerBuffer, "TITL", 4) != 0) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncorrectTitleMarker; // 23
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2021-08-25 00:30:10 +02:00
|
|
|
|
2021-08-26 00:22:11 +02:00
|
|
|
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
|
|
|
if (size != 4) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncompleteTitleBuffer; // 24
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2021-08-29 02:35:25 +02:00
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(&m_data.titlBuffer, uInt32Buffer, 4);
|
2021-08-29 02:35:25 +02:00
|
|
|
#else
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.titlBuffer = charToUInt32LE(uInt32Buffer);
|
2021-08-29 02:35:25 +02:00
|
|
|
#endif
|
2021-08-25 00:30:10 +02:00
|
|
|
|
2023-01-07 20:43:11 +01:00
|
|
|
m_data.title = static_cast<char*>(std::malloc(m_data.titlBuffer));
|
|
|
|
if (!m_data.title) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::TitleMallocError; // 25
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2023-01-07 20:43:11 +01:00
|
|
|
size = readBuffer(data, m_data.title, &pos, m_data.titlBuffer, length);
|
2021-10-28 03:28:36 +02:00
|
|
|
if (size != m_data.titlBuffer) {
|
2023-01-07 20:43:11 +01:00
|
|
|
std::free(m_data.title);
|
|
|
|
m_data.title = nullptr;
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::TitleReadError; // 26
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-28 03:28:36 +02:00
|
|
|
pos = m_data.descOffset + headerSize;
|
2021-08-26 00:22:11 +02:00
|
|
|
size = readBuffer(data, markerBuffer, &pos, 4, length);
|
|
|
|
if (size != 4) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncompleteDescMarker; // 27
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
|
|
|
if (strncmp(markerBuffer, "DESC", 4) != 0) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncorrectDescMarker; // 28
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2021-08-25 00:30:10 +02:00
|
|
|
|
2021-08-26 00:22:11 +02:00
|
|
|
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
|
|
|
|
if (size != 4) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncompleteDescBuffer; // 29
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2021-08-29 02:35:25 +02:00
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(&m_data.descBuffer, uInt32Buffer, 4);
|
2021-08-29 02:35:25 +02:00
|
|
|
#else
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.descBuffer = charToUInt32LE(uInt32Buffer);
|
2021-08-29 02:35:25 +02:00
|
|
|
#endif
|
2021-08-25 00:30:10 +02:00
|
|
|
|
2023-01-07 20:43:11 +01:00
|
|
|
m_data.description = static_cast<char*>(std::malloc(m_data.descBuffer));
|
|
|
|
if (!m_data.description) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::DescMallocError; // 30
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2023-01-07 20:43:11 +01:00
|
|
|
size = readBuffer(data, m_data.description, &pos, m_data.descBuffer, length);
|
2021-10-28 03:28:36 +02:00
|
|
|
if (size != m_data.descBuffer) {
|
2023-01-07 20:43:11 +01:00
|
|
|
std::free(m_data.description);
|
|
|
|
m_data.description = nullptr;
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::DescReadError; // 31
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-28 03:28:36 +02:00
|
|
|
pos = m_data.endOfFile + headerSize - 4;
|
2021-08-26 00:22:11 +02:00
|
|
|
size = readBuffer(data, markerBuffer, &pos, 4, length);
|
|
|
|
if (size != 4) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncompleteJendMarker; // 32
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
|
|
|
if (strncmp(markerBuffer, "JEND", 4) != 0) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncorrectJendMarker; // 33
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
2021-08-25 00:30:10 +02:00
|
|
|
|
|
|
|
#ifdef RAGEPHOTO_BENCHMARK
|
|
|
|
auto benchmark_parse_end = std::chrono::high_resolution_clock::now();
|
|
|
|
auto benchmark_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(benchmark_parse_end - benchmark_parse_start);
|
|
|
|
std::cout << "Benchmark: " << benchmark_ns.count() << "ns" << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2021-09-16 03:07:11 +02:00
|
|
|
#ifdef RAGEPHOTO_DEBUG
|
2021-10-28 03:28:36 +02:00
|
|
|
std::cout << "header: " << m_data.header << std::endl;
|
|
|
|
std::cout << "headerSum: " << m_data.headerSum << std::endl;
|
2021-11-22 14:01:06 +01:00
|
|
|
std::cout << "unnamedSum1: " << m_data.unnamedSum1 << std::endl;
|
|
|
|
std::cout << "unnamedSum2: " << m_data.unnamedSum2 << std::endl;
|
2021-10-28 03:28:36 +02:00
|
|
|
std::cout << "photoBuffer: " << m_data.photoBuffer << std::endl;
|
|
|
|
std::cout << "descBuffer: " << m_data.descBuffer << std::endl;
|
|
|
|
std::cout << "descOffset: " << m_data.descOffset << std::endl;
|
2023-01-07 20:43:11 +01:00
|
|
|
std::cout << "description: " << m_data.description << std::endl;
|
2021-10-28 03:28:36 +02:00
|
|
|
std::cout << "jsonBuffer: " << m_data.jsonBuffer << std::endl;
|
|
|
|
std::cout << "jsonOffset: " << m_data.jsonOffset << std::endl;
|
2023-01-07 20:43:11 +01:00
|
|
|
std::cout << "json: " << m_data.json << std::endl;
|
2021-10-28 03:28:36 +02:00
|
|
|
std::cout << "titlBuffer: " << m_data.titlBuffer << std::endl;
|
|
|
|
std::cout << "titlOffset: " << m_data.titlOffset << std::endl;
|
2023-01-07 20:43:11 +01:00
|
|
|
std::cout << "title: " << m_data.title << std::endl;
|
2021-10-28 03:28:36 +02:00
|
|
|
std::cout << "eofOffset: " << m_data.endOfFile << std::endl;
|
2021-10-29 08:38:26 +02:00
|
|
|
std::cout << "setBufferOffsets()" << std::endl;
|
|
|
|
setBufferOffsets();
|
2021-10-28 03:28:36 +02:00
|
|
|
std::cout << "descOffset: " << m_data.descOffset << std::endl;
|
|
|
|
std::cout << "jsonOffset: " << m_data.jsonOffset << std::endl;
|
|
|
|
std::cout << "titlOffset: " << m_data.titlOffset << std::endl;
|
|
|
|
std::cout << "eofOffset: " << m_data.endOfFile << std::endl;
|
2021-09-16 05:02:08 +02:00
|
|
|
std::cout << "calc size: " << saveSize() << std::endl;
|
|
|
|
std::cout << "real size: " << length << std::endl;
|
2021-09-16 03:07:11 +02:00
|
|
|
#endif
|
|
|
|
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::NoError; // 255
|
2021-08-25 00:30:10 +02:00
|
|
|
return true;
|
2021-10-20 08:49:35 +02:00
|
|
|
#else
|
|
|
|
std::cout << "UTF-16LE decoding support missing" << std::endl;
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::UnicodeInitError; // 4
|
2021-10-20 08:49:35 +02:00
|
|
|
return false;
|
|
|
|
#endif
|
2021-08-25 00:30:10 +02:00
|
|
|
}
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncompatibleFormat; // 2
|
2021-08-25 00:30:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-03 17:59:54 +02:00
|
|
|
bool RagePhoto::load(const std::string &data)
|
|
|
|
{
|
|
|
|
return load(data.data(), data.size());
|
|
|
|
}
|
|
|
|
|
2021-11-02 10:16:56 +01:00
|
|
|
bool RagePhoto::loadFile(const std::string &filename)
|
|
|
|
{
|
|
|
|
std::ifstream ifs(filename, std::ios::in | std::ios::binary);
|
|
|
|
if (ifs.is_open()) {
|
|
|
|
std::string sdata(std::istreambuf_iterator<char>{ifs}, {});
|
|
|
|
ifs.close();
|
|
|
|
return load(sdata);
|
|
|
|
}
|
|
|
|
else {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::Uninitialised; // 0
|
2021-11-02 10:16:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-02 05:55:50 +01:00
|
|
|
uint8_t RagePhoto::error() const
|
2021-08-26 00:22:11 +02:00
|
|
|
{
|
2021-12-02 05:55:50 +01:00
|
|
|
return m_data.error;
|
2021-08-26 00:22:11 +02:00
|
|
|
}
|
|
|
|
|
2021-10-30 17:40:58 +02:00
|
|
|
uint32_t RagePhoto::format() const
|
2021-09-03 18:36:00 +02:00
|
|
|
{
|
2021-10-28 03:28:36 +02:00
|
|
|
return m_data.photoFormat;
|
2021-09-03 18:36:00 +02:00
|
|
|
}
|
|
|
|
|
2021-10-30 17:40:58 +02:00
|
|
|
const std::string RagePhoto::photo() const
|
2021-09-16 05:02:08 +02:00
|
|
|
{
|
2023-01-07 20:43:11 +01:00
|
|
|
if (m_data.photoData)
|
2021-10-28 03:28:36 +02:00
|
|
|
return std::string(m_data.photoData, m_data.photoSize);
|
2021-09-16 05:02:08 +02:00
|
|
|
else
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
2021-10-30 17:40:58 +02:00
|
|
|
const char* RagePhoto::photoData() const
|
2021-08-25 00:30:10 +02:00
|
|
|
{
|
2023-01-07 20:43:11 +01:00
|
|
|
return m_data.photoData;
|
2021-08-25 00:30:10 +02:00
|
|
|
}
|
|
|
|
|
2021-10-30 17:40:58 +02:00
|
|
|
uint32_t RagePhoto::photoSize() const
|
2021-08-25 00:30:10 +02:00
|
|
|
{
|
2023-01-07 20:43:11 +01:00
|
|
|
if (m_data.photoData)
|
2021-10-28 03:28:36 +02:00
|
|
|
return m_data.photoSize;
|
2021-08-26 00:22:11 +02:00
|
|
|
else
|
2021-09-17 07:22:49 +02:00
|
|
|
return 0UL;
|
2021-08-25 00:30:10 +02:00
|
|
|
}
|
|
|
|
|
2023-01-07 20:43:11 +01:00
|
|
|
const char* RagePhoto::description() const
|
2021-08-25 00:30:10 +02:00
|
|
|
{
|
2021-10-28 03:28:36 +02:00
|
|
|
return m_data.description;
|
2021-08-25 00:30:10 +02:00
|
|
|
}
|
|
|
|
|
2023-01-07 20:43:11 +01:00
|
|
|
const char* RagePhoto::json() const
|
2021-08-25 00:30:10 +02:00
|
|
|
{
|
2021-10-28 03:28:36 +02:00
|
|
|
return m_data.json;
|
2021-08-25 00:30:10 +02:00
|
|
|
}
|
|
|
|
|
2023-01-07 20:43:11 +01:00
|
|
|
const char* RagePhoto::header() const
|
2021-08-25 00:30:10 +02:00
|
|
|
{
|
2021-10-28 03:28:36 +02:00
|
|
|
return m_data.header;
|
2021-08-25 00:30:10 +02:00
|
|
|
}
|
|
|
|
|
2023-01-07 20:43:11 +01:00
|
|
|
const char* RagePhoto::title() const
|
2021-08-25 00:30:10 +02:00
|
|
|
{
|
2021-10-28 03:28:36 +02:00
|
|
|
return m_data.title;
|
2021-08-25 00:30:10 +02:00
|
|
|
}
|
|
|
|
|
2021-11-03 10:54:35 +01:00
|
|
|
const char* RagePhoto::version()
|
|
|
|
{
|
|
|
|
return RAGEPHOTO_VERSION;
|
|
|
|
}
|
|
|
|
|
2021-10-28 03:28:36 +02:00
|
|
|
bool RagePhoto::save(char *data, uint32_t photoFormat)
|
2021-09-17 07:22:49 +02:00
|
|
|
{
|
|
|
|
if (photoFormat == PhotoFormat::GTA5 || photoFormat == PhotoFormat::RDR2) {
|
2021-11-07 20:32:06 +01:00
|
|
|
#if defined UNICODE_ICONV || defined UNICODE_CODECVT || defined UNICODE_WINCVT
|
|
|
|
#if defined UNICODE_CODECVT
|
2021-10-24 05:03:14 +02:00
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert;
|
2021-10-28 03:28:36 +02:00
|
|
|
std::u16string photoHeader_string = convert.from_bytes(m_data.header);
|
2021-11-06 20:17:28 +01:00
|
|
|
if (convert.converted() == 0) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::UnicodeHeaderError; // 5
|
2021-11-06 20:17:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-10-28 03:28:36 +02:00
|
|
|
const size_t photoHeader_size = photoHeader_string.size() * 2;
|
2021-10-20 08:49:35 +02:00
|
|
|
if (photoHeader_size > 256) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::HeaderBufferTight; // 34
|
2021-09-17 07:22:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-11-06 20:17:28 +01:00
|
|
|
const char *photoHeader = reinterpret_cast<const char*>(photoHeader_string.data());
|
2021-11-07 20:32:06 +01:00
|
|
|
#elif defined UNICODE_ICONV
|
2021-10-20 08:49:35 +02:00
|
|
|
iconv_t iconv_in = iconv_open("UTF-16LE", "UTF-8");
|
|
|
|
if (iconv_in == (iconv_t)-1) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::UnicodeInitError; // 4
|
2021-10-20 08:49:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-11-06 20:17:28 +01:00
|
|
|
char photoHeader[256]{};
|
2023-01-07 20:43:11 +01:00
|
|
|
size_t src_s = strlen(m_data.header);
|
2021-10-20 08:49:35 +02:00
|
|
|
size_t dst_s = sizeof(photoHeader);
|
2023-01-07 20:43:11 +01:00
|
|
|
char *src = m_data.header;
|
2021-10-20 08:49:35 +02:00
|
|
|
char *dst = photoHeader;
|
|
|
|
const size_t ret = iconv(iconv_in, &src, &src_s, &dst, &dst_s);
|
2021-10-20 08:53:55 +02:00
|
|
|
iconv_close(iconv_in);
|
2021-10-20 08:49:35 +02:00
|
|
|
if (ret == static_cast<size_t>(-1)) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::UnicodeHeaderError; // 5
|
2021-10-20 08:49:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-11-06 20:17:28 +01:00
|
|
|
const size_t photoHeader_size = 256;
|
2021-11-07 20:32:06 +01:00
|
|
|
#elif defined UNICODE_WINCVT
|
|
|
|
char photoHeader[256]{};
|
2023-01-07 20:43:11 +01:00
|
|
|
const int converted = MultiByteToWideChar(CP_UTF8, 0, m_data.header, static_cast<int>(strlen(m_data.header)), reinterpret_cast<wchar_t*>(photoHeader), 256 / sizeof(wchar_t));
|
2021-11-07 20:32:06 +01:00
|
|
|
if (converted == 0) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::UnicodeHeaderError; // 5
|
2021-11-07 20:32:06 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const size_t photoHeader_size = 256;
|
2021-09-17 07:22:49 +02:00
|
|
|
#endif
|
|
|
|
|
2021-10-28 03:28:36 +02:00
|
|
|
if (m_data.photoSize > m_data.photoBuffer) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::PhotoBufferTight; // 35
|
2021-09-17 07:22:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-07 20:43:11 +01:00
|
|
|
const size_t jsonString_size = strlen(m_data.json) + 1;
|
2021-10-28 03:28:36 +02:00
|
|
|
if (jsonString_size > m_data.jsonBuffer) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::JsonBufferTight; // 36
|
2021-09-17 07:22:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-07 20:43:11 +01:00
|
|
|
const size_t titlString_size = strlen(m_data.title) + 1;
|
2021-10-28 03:28:36 +02:00
|
|
|
if (titlString_size > m_data.titlBuffer) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::TitleBufferTight; // 37
|
2021-09-17 07:22:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-07 20:43:11 +01:00
|
|
|
const size_t descString_size = strlen(m_data.description) + 1;
|
2021-10-28 03:28:36 +02:00
|
|
|
if (descString_size > m_data.descBuffer) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::DescBufferTight; // 38
|
2021-09-17 07:22:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-22 05:14:04 +02:00
|
|
|
const size_t length = saveSize(photoFormat);
|
2021-09-17 07:22:49 +02:00
|
|
|
size_t pos = 0;
|
|
|
|
char uInt32Buffer[4];
|
|
|
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(uInt32Buffer, &photoFormat, 4);
|
2021-09-17 07:22:49 +02:00
|
|
|
#else
|
|
|
|
uInt32ToCharLE(static_cast<uint32_t>(photoFormat), uInt32Buffer);
|
|
|
|
#endif
|
|
|
|
writeBuffer(uInt32Buffer, data, &pos, length, 4);
|
|
|
|
|
2021-10-20 08:49:35 +02:00
|
|
|
writeBuffer(photoHeader, data, &pos, length, photoHeader_size);
|
|
|
|
for (size_t i = photoHeader_size; i < 256; i++) {
|
2021-09-17 07:22:49 +02:00
|
|
|
writeBuffer("\0", data, &pos, length, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(uInt32Buffer, &m_data.headerSum, 4);
|
2021-09-17 07:22:49 +02:00
|
|
|
#else
|
2021-10-28 03:28:36 +02:00
|
|
|
uInt32ToCharLE(m_data.headerSum, uInt32Buffer);
|
2021-09-17 07:22:49 +02:00
|
|
|
#endif
|
|
|
|
writeBuffer(uInt32Buffer, data, &pos, length, 4);
|
|
|
|
|
|
|
|
if (photoFormat == PhotoFormat::RDR2) {
|
2021-11-22 14:01:06 +01:00
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
|
|
std::memcpy(uInt32Buffer, &m_data.unnamedSum1, 4);
|
|
|
|
#else
|
|
|
|
uInt32ToCharLE(m_data.unnamedSum1, uInt32Buffer);
|
|
|
|
#endif
|
|
|
|
writeBuffer(uInt32Buffer, data, &pos, length, 4);
|
|
|
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
|
|
std::memcpy(uInt32Buffer, &m_data.unnamedSum2, 4);
|
|
|
|
#else
|
|
|
|
uInt32ToCharLE(m_data.unnamedSum2, uInt32Buffer);
|
|
|
|
#endif
|
|
|
|
writeBuffer(uInt32Buffer, data, &pos, length, 4);
|
2021-09-17 07:22:49 +02:00
|
|
|
}
|
2021-09-22 05:14:04 +02:00
|
|
|
const size_t headerSize = pos;
|
2021-09-17 07:22:49 +02:00
|
|
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(uInt32Buffer, &m_data.endOfFile, 4);
|
2021-09-17 07:22:49 +02:00
|
|
|
#else
|
2021-10-28 03:28:36 +02:00
|
|
|
uInt32ToCharLE(m_data.endOfFile, uInt32Buffer);
|
2021-09-17 07:22:49 +02:00
|
|
|
#endif
|
|
|
|
writeBuffer(uInt32Buffer, data, &pos, length, 4);
|
|
|
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(uInt32Buffer, &m_data.jsonOffset, 4);
|
2021-09-17 07:22:49 +02:00
|
|
|
#else
|
2021-10-28 03:28:36 +02:00
|
|
|
uInt32ToCharLE(m_data.jsonOffset, uInt32Buffer);
|
2021-09-17 07:22:49 +02:00
|
|
|
#endif
|
|
|
|
writeBuffer(uInt32Buffer, data, &pos, length, 4);
|
|
|
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(uInt32Buffer, &m_data.titlOffset, 4);
|
2021-09-17 07:22:49 +02:00
|
|
|
#else
|
2021-10-28 03:28:36 +02:00
|
|
|
uInt32ToCharLE(m_data.titlOffset, uInt32Buffer);
|
2021-09-17 07:22:49 +02:00
|
|
|
#endif
|
|
|
|
writeBuffer(uInt32Buffer, data, &pos, length, 4);
|
|
|
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(uInt32Buffer, &m_data.descOffset, 4);
|
2021-09-17 07:22:49 +02:00
|
|
|
#else
|
2021-10-28 03:28:36 +02:00
|
|
|
uInt32ToCharLE(m_data.descOffset, uInt32Buffer);
|
2021-09-17 07:22:49 +02:00
|
|
|
#endif
|
|
|
|
writeBuffer(uInt32Buffer, data, &pos, length, 4);
|
|
|
|
|
|
|
|
writeBuffer("JPEG", data, &pos, length, 4);
|
|
|
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(uInt32Buffer, &m_data.photoBuffer, 4);
|
2021-09-17 07:22:49 +02:00
|
|
|
#else
|
2021-10-28 03:28:36 +02:00
|
|
|
uInt32ToCharLE(m_data.photoBuffer, uInt32Buffer);
|
2021-09-17 07:22:49 +02:00
|
|
|
#endif
|
|
|
|
writeBuffer(uInt32Buffer, data, &pos, length, 4);
|
|
|
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(uInt32Buffer, &m_data.photoSize, 4);
|
2021-09-17 07:22:49 +02:00
|
|
|
#else
|
2021-10-28 03:28:36 +02:00
|
|
|
uInt32ToCharLE(m_data.photoSize, uInt32Buffer);
|
2021-09-17 07:22:49 +02:00
|
|
|
#endif
|
|
|
|
writeBuffer(uInt32Buffer, data, &pos, length, 4);
|
|
|
|
|
2021-10-28 03:28:36 +02:00
|
|
|
writeBuffer(m_data.photoData, data, &pos, length, m_data.photoSize);
|
|
|
|
for (size_t i = m_data.photoSize; i < m_data.photoBuffer; i++) {
|
2021-09-17 07:22:49 +02:00
|
|
|
writeBuffer("\0", data, &pos, length, 1);
|
|
|
|
}
|
|
|
|
|
2021-10-28 03:28:36 +02:00
|
|
|
pos = m_data.jsonOffset + headerSize;
|
2021-09-17 07:22:49 +02:00
|
|
|
writeBuffer("JSON", data, &pos, length, 4);
|
|
|
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(uInt32Buffer, &m_data.jsonBuffer, 4);
|
2021-09-17 07:22:49 +02:00
|
|
|
#else
|
2021-10-28 03:28:36 +02:00
|
|
|
uInt32ToCharLE(m_data.jsonBuffer, uInt32Buffer);
|
2021-09-17 07:22:49 +02:00
|
|
|
#endif
|
|
|
|
writeBuffer(uInt32Buffer, data, &pos, length, 4);
|
|
|
|
|
2023-01-07 20:43:11 +01:00
|
|
|
writeBuffer(m_data.json, data, &pos, length, jsonString_size);
|
2021-10-28 03:28:36 +02:00
|
|
|
for (size_t i = jsonString_size; i < m_data.jsonBuffer; i++) {
|
2021-09-17 07:22:49 +02:00
|
|
|
writeBuffer("\0", data, &pos, length, 1);
|
|
|
|
}
|
|
|
|
|
2021-10-28 03:28:36 +02:00
|
|
|
pos = m_data.titlOffset + headerSize;
|
2021-09-17 07:22:49 +02:00
|
|
|
writeBuffer("TITL", data, &pos, length, 4);
|
|
|
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(uInt32Buffer, &m_data.titlBuffer, 4);
|
2021-09-17 07:22:49 +02:00
|
|
|
#else
|
2021-10-28 03:28:36 +02:00
|
|
|
uInt32ToCharLE(m_data.titlBuffer, uInt32Buffer);
|
2021-09-17 07:22:49 +02:00
|
|
|
#endif
|
|
|
|
writeBuffer(uInt32Buffer, data, &pos, length, 4);
|
|
|
|
|
2023-01-07 20:43:11 +01:00
|
|
|
writeBuffer(m_data.title, data, &pos, length, titlString_size);
|
2021-10-28 03:28:36 +02:00
|
|
|
for (size_t i = titlString_size; i < m_data.titlBuffer; i++) {
|
2021-09-17 07:22:49 +02:00
|
|
|
writeBuffer("\0", data, &pos, length, 1);
|
|
|
|
}
|
|
|
|
|
2021-10-28 03:28:36 +02:00
|
|
|
pos = m_data.descOffset + headerSize;
|
2021-09-17 07:22:49 +02:00
|
|
|
writeBuffer("DESC", data, &pos, length, 4);
|
|
|
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(uInt32Buffer, &m_data.descBuffer, 4);
|
2021-09-17 07:22:49 +02:00
|
|
|
#else
|
2021-10-28 03:28:36 +02:00
|
|
|
uInt32ToCharLE(m_data.descBuffer, uInt32Buffer);
|
2021-09-17 07:22:49 +02:00
|
|
|
#endif
|
|
|
|
writeBuffer(uInt32Buffer, data, &pos, length, 4);
|
|
|
|
|
2023-01-07 20:43:11 +01:00
|
|
|
writeBuffer(m_data.description, data, &pos, length, descString_size);
|
2021-10-28 03:28:36 +02:00
|
|
|
for (size_t i = descString_size; i < m_data.descBuffer; i++) {
|
2021-09-17 07:22:49 +02:00
|
|
|
writeBuffer("\0", data, &pos, length, 1);
|
|
|
|
}
|
|
|
|
|
2021-10-28 03:28:36 +02:00
|
|
|
pos = m_data.endOfFile + headerSize - 4;
|
2021-09-17 07:22:49 +02:00
|
|
|
writeBuffer("JEND", data, &pos, length, 4);
|
|
|
|
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::NoError; // 255
|
2021-09-17 07:22:49 +02:00
|
|
|
return true;
|
2021-10-20 08:49:35 +02:00
|
|
|
#else
|
|
|
|
std::cout << "UTF-16LE encoding support missing" << std::endl;
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::UnicodeInitError; // 4
|
2021-10-20 08:49:35 +02:00
|
|
|
return false;
|
|
|
|
#endif
|
2021-09-17 07:22:49 +02:00
|
|
|
}
|
|
|
|
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::IncompatibleFormat; // 2
|
2021-09-17 07:22:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RagePhoto::save(char *data)
|
|
|
|
{
|
2021-10-28 03:28:36 +02:00
|
|
|
return save(data, m_data.photoFormat);
|
2021-09-17 07:22:49 +02:00
|
|
|
}
|
|
|
|
|
2021-10-28 03:28:36 +02:00
|
|
|
const std::string RagePhoto::save(uint32_t photoFormat, bool *ok)
|
2021-09-17 07:22:49 +02:00
|
|
|
{
|
2021-12-02 05:55:50 +01:00
|
|
|
std::string sdata;
|
2021-09-22 05:14:04 +02:00
|
|
|
const size_t size = saveSize(photoFormat);
|
2021-09-17 07:22:49 +02:00
|
|
|
if (size == 0) {
|
|
|
|
if (ok)
|
|
|
|
*ok = false;
|
2021-12-02 05:55:50 +01:00
|
|
|
return sdata;
|
2021-09-17 07:22:49 +02:00
|
|
|
}
|
2021-12-02 05:55:50 +01:00
|
|
|
sdata.resize(size);
|
|
|
|
const bool saved = save(&sdata[0], photoFormat);
|
2021-09-17 07:22:49 +02:00
|
|
|
if (ok)
|
|
|
|
*ok = saved;
|
2021-12-02 05:55:50 +01:00
|
|
|
return sdata;
|
2021-09-17 07:22:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::string RagePhoto::save(bool *ok)
|
|
|
|
{
|
2021-10-28 03:28:36 +02:00
|
|
|
return save(m_data.photoFormat, ok);
|
2021-09-17 07:22:49 +02:00
|
|
|
}
|
|
|
|
|
2021-11-02 10:16:56 +01:00
|
|
|
bool RagePhoto::saveFile(const std::string &filename, uint32_t photoFormat)
|
|
|
|
{
|
|
|
|
bool ok;
|
2021-12-02 05:55:50 +01:00
|
|
|
const std::string &sdata = save(photoFormat, &ok);
|
2021-11-02 10:16:56 +01:00
|
|
|
if (ok) {
|
|
|
|
std::ofstream ofs(filename, std::ios::out | std::ios::binary | std::ios::trunc);
|
|
|
|
if (!ofs.is_open()) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::Uninitialised; // 0
|
2021-11-02 10:16:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ofs << sdata;
|
2021-11-06 20:17:28 +01:00
|
|
|
ok = ofs.good();
|
2021-11-02 10:16:56 +01:00
|
|
|
ofs.close();
|
2021-11-06 20:17:28 +01:00
|
|
|
return ok;
|
2021-11-02 10:16:56 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RagePhoto::saveFile(const std::string &filename)
|
|
|
|
{
|
|
|
|
return saveFile(filename, m_data.photoFormat);
|
|
|
|
}
|
|
|
|
|
2021-10-29 08:38:26 +02:00
|
|
|
inline size_t RagePhoto::saveSize(RagePhotoData *ragePhotoData, uint32_t photoFormat)
|
2021-09-16 05:02:08 +02:00
|
|
|
{
|
|
|
|
if (photoFormat == PhotoFormat::GTA5)
|
2021-10-29 08:38:26 +02:00
|
|
|
return (ragePhotoData->photoBuffer + ragePhotoData->jsonBuffer + ragePhotoData->titlBuffer + ragePhotoData->descBuffer + GTA5_HEADERSIZE + 56UL);
|
2021-09-16 05:02:08 +02:00
|
|
|
else if (photoFormat == PhotoFormat::RDR2)
|
2021-10-29 08:38:26 +02:00
|
|
|
return (ragePhotoData->photoBuffer + ragePhotoData->jsonBuffer + ragePhotoData->titlBuffer + ragePhotoData->descBuffer + RDR2_HEADERSIZE + 56UL);
|
2021-09-16 05:02:08 +02:00
|
|
|
else
|
2021-09-17 07:22:49 +02:00
|
|
|
return 0;
|
2021-09-16 05:02:08 +02:00
|
|
|
}
|
|
|
|
|
2021-10-29 08:38:26 +02:00
|
|
|
inline size_t RagePhoto::saveSize(RagePhotoData *ragePhotoData)
|
|
|
|
{
|
|
|
|
return saveSize(ragePhotoData, ragePhotoData->photoFormat);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t RagePhoto::saveSize(uint32_t photoFormat)
|
|
|
|
{
|
|
|
|
return saveSize(&m_data, photoFormat);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t RagePhoto::saveSize()
|
2021-09-16 05:02:08 +02:00
|
|
|
{
|
2021-10-28 03:28:36 +02:00
|
|
|
return saveSize(m_data.photoFormat);
|
2021-09-16 05:02:08 +02:00
|
|
|
}
|
|
|
|
|
2021-10-29 08:38:26 +02:00
|
|
|
inline void RagePhoto::setBufferDefault()
|
|
|
|
{
|
|
|
|
setBufferDefault(&m_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void RagePhoto::setBufferDefault(RagePhotoData *ragePhotoData)
|
2021-09-16 03:07:11 +02:00
|
|
|
{
|
2021-10-29 08:38:26 +02:00
|
|
|
ragePhotoData->descBuffer = DEFAULT_DESCBUFFER;
|
|
|
|
ragePhotoData->jsonBuffer = DEFAULT_JSONBUFFER;
|
|
|
|
ragePhotoData->titlBuffer = DEFAULT_TITLBUFFER;
|
|
|
|
setBufferOffsets(ragePhotoData);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void RagePhoto::setBufferOffsets()
|
|
|
|
{
|
|
|
|
setBufferOffsets(&m_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void RagePhoto::setBufferOffsets(RagePhotoData *ragePhotoData)
|
|
|
|
{
|
|
|
|
ragePhotoData->jsonOffset = ragePhotoData->photoBuffer + 28;
|
|
|
|
ragePhotoData->titlOffset = ragePhotoData->jsonOffset + ragePhotoData->jsonBuffer + 8;
|
|
|
|
ragePhotoData->descOffset = ragePhotoData->titlOffset + ragePhotoData->titlBuffer + 8;
|
|
|
|
ragePhotoData->endOfFile = ragePhotoData->descOffset + ragePhotoData->descBuffer + 12;
|
2021-09-16 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
2023-01-07 20:43:11 +01:00
|
|
|
void RagePhoto::setDescription(const char *description, uint32_t bufferSize)
|
2021-09-14 19:35:46 +02:00
|
|
|
{
|
2023-01-07 20:43:11 +01:00
|
|
|
if (!writeDataChar(description, &m_data.description)) {
|
|
|
|
m_data.error = Error::DescMallocError; // 30
|
|
|
|
return;
|
|
|
|
}
|
2021-09-14 19:35:46 +02:00
|
|
|
if (bufferSize != 0) {
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.descBuffer = bufferSize;
|
2021-10-29 08:38:26 +02:00
|
|
|
setBufferOffsets();
|
2021-09-14 19:35:46 +02:00
|
|
|
}
|
2023-01-07 20:43:11 +01:00
|
|
|
m_data.error = Error::NoError; // 255
|
2021-09-14 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2021-10-28 03:28:36 +02:00
|
|
|
void RagePhoto::setFormat(uint32_t photoFormat)
|
|
|
|
{
|
|
|
|
m_data.photoFormat = photoFormat;
|
|
|
|
}
|
|
|
|
|
2023-01-07 20:43:11 +01:00
|
|
|
void RagePhoto::setJson(const char *json, uint32_t bufferSize)
|
2021-09-16 03:07:11 +02:00
|
|
|
{
|
2023-01-07 20:43:11 +01:00
|
|
|
if (!writeDataChar(json, &m_data.json)) {
|
|
|
|
m_data.error = Error::JsonMallocError; // 20
|
|
|
|
return;
|
|
|
|
}
|
2021-09-14 19:35:46 +02:00
|
|
|
if (bufferSize != 0) {
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.jsonBuffer = bufferSize;
|
2021-10-29 08:38:26 +02:00
|
|
|
setBufferOffsets();
|
2021-09-14 19:35:46 +02:00
|
|
|
}
|
2023-01-07 20:43:11 +01:00
|
|
|
m_data.error = Error::NoError; // 255
|
2021-09-14 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2023-01-07 20:43:11 +01:00
|
|
|
void RagePhoto::setHeader(const char *header, uint32_t headerSum)
|
2021-09-14 19:35:46 +02:00
|
|
|
{
|
2023-01-07 20:43:11 +01:00
|
|
|
if (!writeDataChar(header, &m_data.header)) {
|
|
|
|
//m_data.error = Error::HeaderMallocError;
|
|
|
|
return;
|
|
|
|
}
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.headerSum = headerSum;
|
2023-01-07 20:43:11 +01:00
|
|
|
m_data.error = Error::NoError; // 255
|
2021-09-14 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2021-09-16 05:02:08 +02:00
|
|
|
bool RagePhoto::setPhoto(const char *data, uint32_t size, uint32_t bufferSize)
|
2021-09-14 19:35:46 +02:00
|
|
|
{
|
2023-01-07 20:43:11 +01:00
|
|
|
if (m_data.photoData) {
|
2021-10-28 03:28:36 +02:00
|
|
|
if (m_data.photoSize > size) {
|
2021-11-18 17:33:20 +01:00
|
|
|
char *t_photoData = static_cast<char*>(std::realloc(m_data.photoData, size));
|
2021-09-14 19:35:46 +02:00
|
|
|
if (!t_photoData) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::PhotoMallocError; // 15
|
2021-09-14 19:35:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.photoData = t_photoData;
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(m_data.photoData, data, size);
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.photoSize = size;
|
|
|
|
}
|
|
|
|
else if (m_data.photoSize < size) {
|
2021-11-18 17:33:20 +01:00
|
|
|
std::free(m_data.photoData);
|
|
|
|
m_data.photoData = static_cast<char*>(std::malloc(size));
|
2021-10-28 03:28:36 +02:00
|
|
|
if (!m_data.photoData) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::PhotoMallocError; // 15
|
2021-09-14 19:35:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(m_data.photoData, data, size);
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.photoSize = size;
|
2021-09-14 19:35:46 +02:00
|
|
|
}
|
|
|
|
else {
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(m_data.photoData, data, size);
|
2021-09-14 19:35:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2021-11-18 17:42:25 +01:00
|
|
|
m_data.photoData = static_cast<char*>(std::malloc(size));
|
2021-10-28 03:28:36 +02:00
|
|
|
if (!m_data.photoData) {
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::PhotoMallocError; // 15
|
2021-09-14 19:35:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-11-18 17:33:20 +01:00
|
|
|
std::memcpy(m_data.photoData, data, size);
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.photoSize = size;
|
2021-09-14 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bufferSize != 0) {
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.photoBuffer = bufferSize;
|
2021-10-29 08:38:26 +02:00
|
|
|
setBufferOffsets();
|
2021-09-14 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2021-12-02 05:55:50 +01:00
|
|
|
m_data.error = Error::NoError; // 255
|
2021-09-14 19:35:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-16 05:02:08 +02:00
|
|
|
bool RagePhoto::setPhoto(const std::string &data, uint32_t bufferSize)
|
2021-09-14 19:35:46 +02:00
|
|
|
{
|
2021-09-22 05:14:04 +02:00
|
|
|
return setPhoto(data.data(), static_cast<uint32_t>(data.size()), bufferSize);
|
2021-09-14 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2023-01-07 20:43:11 +01:00
|
|
|
void RagePhoto::setTitle(const char *title, uint32_t bufferSize)
|
2021-09-14 19:35:46 +02:00
|
|
|
{
|
2023-01-07 20:43:11 +01:00
|
|
|
if (!writeDataChar(title, &m_data.title)) {
|
|
|
|
m_data.error = Error::TitleMallocError; // 25
|
|
|
|
return;
|
|
|
|
}
|
2021-09-14 19:35:46 +02:00
|
|
|
if (bufferSize != 0) {
|
2021-10-28 03:28:36 +02:00
|
|
|
m_data.titlBuffer = bufferSize;
|
2021-10-29 08:38:26 +02:00
|
|
|
setBufferOffsets();
|
2021-09-14 19:35:46 +02:00
|
|
|
}
|
2023-01-07 20:43:11 +01:00
|
|
|
m_data.error = Error::NoError; // 255
|
2021-09-14 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2021-10-29 12:03:01 +02:00
|
|
|
#ifdef LIBRAGEPHOTO_C_API
|
2021-10-29 11:52:59 +02:00
|
|
|
ragephoto_t ragephoto_open()
|
|
|
|
{
|
2021-10-30 17:40:58 +02:00
|
|
|
return static_cast<ragephoto_t>(new RagePhoto);
|
2021-10-29 11:52:59 +02:00
|
|
|
}
|
|
|
|
|
2021-11-05 16:20:43 +01:00
|
|
|
void ragephoto_clear(ragephoto_t instance)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
|
|
|
ragePhoto->clear();
|
|
|
|
}
|
|
|
|
|
2021-10-29 12:33:43 +02:00
|
|
|
int ragephoto_load(ragephoto_t instance, const char *data, size_t size)
|
2021-10-29 11:52:59 +02:00
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
|
|
|
return ragePhoto->load(data, size);
|
|
|
|
}
|
|
|
|
|
2021-11-03 10:54:35 +01:00
|
|
|
int ragephoto_loadfile(ragephoto_t instance, const char *filename)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
|
|
|
return ragePhoto->loadFile(filename);
|
|
|
|
}
|
|
|
|
|
2021-10-30 17:40:58 +02:00
|
|
|
uint8_t ragephoto_error(ragephoto_t instance)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
2021-12-02 05:55:50 +01:00
|
|
|
return ragePhoto->error();
|
2021-10-30 17:40:58 +02:00
|
|
|
}
|
|
|
|
|
2021-11-02 09:26:56 +01:00
|
|
|
uint32_t ragephoto_defpbuf_gta5()
|
2021-10-29 11:52:59 +02:00
|
|
|
{
|
2021-11-02 09:26:56 +01:00
|
|
|
return RagePhoto::DEFAULT_GTA5_PHOTOBUFFER;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ragephoto_defpbuf_rdr2()
|
|
|
|
{
|
|
|
|
return RagePhoto::DEFAULT_RDR2_PHOTOBUFFER;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ragephoto_format_gta5()
|
|
|
|
{
|
|
|
|
return RagePhoto::GTA5;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ragephoto_format_rdr2()
|
|
|
|
{
|
|
|
|
return RagePhoto::RDR2;
|
2021-10-29 11:52:59 +02:00
|
|
|
}
|
|
|
|
|
2021-10-30 17:40:58 +02:00
|
|
|
const char* ragephoto_getphotodesc(ragephoto_t instance)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
2023-01-07 20:43:11 +01:00
|
|
|
return ragePhoto->description();
|
2021-10-30 17:40:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ragephoto_getphotoformat(ragephoto_t instance)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
|
|
|
return ragePhoto->format();
|
|
|
|
}
|
|
|
|
|
2021-11-02 09:26:56 +01:00
|
|
|
const char* ragephoto_getphotojpeg(ragephoto_t instance)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
|
|
|
return ragePhoto->photoData();
|
|
|
|
}
|
|
|
|
|
2021-10-30 17:40:58 +02:00
|
|
|
const char* ragephoto_getphotojson(ragephoto_t instance)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
2023-01-07 20:43:11 +01:00
|
|
|
return ragePhoto->json();
|
2021-11-02 09:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* ragephoto_getphotoheader(ragephoto_t instance)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
2023-01-07 20:43:11 +01:00
|
|
|
return ragePhoto->header();
|
2021-10-30 17:40:58 +02:00
|
|
|
}
|
|
|
|
|
2021-10-29 11:52:59 +02:00
|
|
|
uint32_t ragephoto_getphotosize(ragephoto_t instance)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
|
|
|
return ragePhoto->photoSize();
|
|
|
|
}
|
|
|
|
|
2021-10-30 17:40:58 +02:00
|
|
|
const char* ragephoto_getphototitle(ragephoto_t instance)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
2023-01-07 20:43:11 +01:00
|
|
|
return ragePhoto->title();
|
2021-11-02 09:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t ragephoto_getsavesize(ragephoto_t instance)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
|
|
|
return ragePhoto->saveSize();
|
|
|
|
}
|
|
|
|
|
2021-11-04 13:21:18 +01:00
|
|
|
size_t ragephoto_getsavesizef(ragephoto_t instance, uint32_t photoFormat)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
|
|
|
return ragePhoto->saveSize(photoFormat);
|
|
|
|
}
|
|
|
|
|
2021-11-02 09:26:56 +01:00
|
|
|
int ragephoto_save(ragephoto_t instance, char *data)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
|
|
|
return ragePhoto->save(data);
|
|
|
|
}
|
|
|
|
|
2021-11-04 13:21:18 +01:00
|
|
|
int ragephoto_savef(ragephoto_t instance, char *data, uint32_t photoFormat)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
|
|
|
return ragePhoto->save(data, photoFormat);
|
|
|
|
}
|
|
|
|
|
2021-11-03 10:54:35 +01:00
|
|
|
int ragephoto_savefile(ragephoto_t instance, const char *filename)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
|
|
|
return ragePhoto->saveFile(filename);
|
|
|
|
}
|
|
|
|
|
2021-11-04 13:21:18 +01:00
|
|
|
int ragephoto_savefilef(ragephoto_t instance, const char *filename, uint32_t photoFormat)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
|
|
|
return ragePhoto->saveFile(filename, photoFormat);
|
|
|
|
}
|
|
|
|
|
2021-11-02 09:26:56 +01:00
|
|
|
void ragephoto_setbufferdefault(ragephoto_t instance)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
|
|
|
ragePhoto->setBufferDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ragephoto_setbufferoffsets(ragephoto_t instance)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
|
|
|
ragePhoto->setBufferOffsets();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ragephoto_setphotodesc(ragephoto_t instance, const char *description, uint32_t bufferSize)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
|
|
|
ragePhoto->setDescription(description, bufferSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ragephoto_setphotoformat(ragephoto_t instance, uint32_t photoFormat)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
|
|
|
ragePhoto->setFormat(photoFormat);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ragephoto_setphotojpeg(ragephoto_t instance, const char *data, uint32_t size, uint32_t bufferSize)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
|
|
|
return ragePhoto->setPhoto(data, size, bufferSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ragephoto_setphotojson(ragephoto_t instance, const char *json, uint32_t bufferSize)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
|
|
|
ragePhoto->setJson(json, bufferSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ragephoto_setphotoheader(ragephoto_t instance, const char *header, uint32_t headerSum)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
|
|
|
ragePhoto->setHeader(header, headerSum);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ragephoto_setphototitle(ragephoto_t instance, const char *title, uint32_t bufferSize)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
|
|
|
ragePhoto->setTitle(title, bufferSize);
|
2021-10-30 17:40:58 +02:00
|
|
|
}
|
|
|
|
|
2021-10-29 11:52:59 +02:00
|
|
|
void ragephoto_close(ragephoto_t instance)
|
|
|
|
{
|
|
|
|
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
|
|
|
|
delete ragePhoto;
|
|
|
|
}
|
2021-11-03 10:54:35 +01:00
|
|
|
|
|
|
|
const char* ragephoto_version()
|
|
|
|
{
|
|
|
|
return RAGEPHOTO_VERSION;
|
|
|
|
}
|
2021-10-29 12:03:01 +02:00
|
|
|
#endif
|
2021-11-18 18:00:09 +01:00
|
|
|
/* END OF RAGEPHOTO CLASS */
|