low level improvements and data type changes

- RagePhoto: removed setFormatLoadFunction (will be replaced with a
better solution later)
- RagePhoto: code updates for the RagePhotoData changes
- RagePhotoA: have now RagePhoto enum's available
- RagePhotoData: changed description, json, header and title types to
char*
- RagePhotoData: removed photoLoaded in favour of nullptr check
- examples: prepared for the API update
This commit is contained in:
Syping 2023-01-07 20:43:11 +01:00
parent 4b9637e0fd
commit e08b45d980
10 changed files with 221 additions and 111 deletions

View file

@ -17,7 +17,7 @@
****************************************************************************]] ****************************************************************************]]
cmake_minimum_required(VERSION 3.7) cmake_minimum_required(VERSION 3.7)
project(ragephoto VERSION 0.1.0 LANGUAGES CXX) project(ragephoto VERSION 0.2.0 LANGUAGES CXX)
include(GNUInstallDirs) include(GNUInstallDirs)
set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_INCLUDE_CURRENT_DIR ON)

View file

@ -52,7 +52,7 @@ bool readPhotoFile(const std::string &filename, Gtk::Window *win, Gtk::Image *im
GdkPixbuf *pixbuf = gdk_pixbuf_loader_get_pixbuf(pixbuf_loader); GdkPixbuf *pixbuf = gdk_pixbuf_loader_get_pixbuf(pixbuf_loader);
gdk_pixbuf_loader_close(pixbuf_loader, NULL); gdk_pixbuf_loader_close(pixbuf_loader, NULL);
image->set(Glib::wrap(pixbuf)); image->set(Glib::wrap(pixbuf));
win->set_title("RagePhoto GTK Photo Viewer - " + ragePhoto.title()); win->set_title("RagePhoto GTK Photo Viewer - " + std::string(ragePhoto.title()));
return true; return true;
} }
else { else {

View file

@ -47,7 +47,7 @@ bool readPhotoFile(const QString &filename, QMainWindow *mainWindow, QLabel *pho
const QByteArray photoData = QByteArray::fromRawData(ragePhoto.photoData(), ragePhoto.photoSize()); const QByteArray photoData = QByteArray::fromRawData(ragePhoto.photoData(), ragePhoto.photoSize());
const QImage image = QImage::fromData(photoData, "JPEG"); const QImage image = QImage::fromData(photoData, "JPEG");
photoLabel->setPixmap(QPixmap::fromImage(image)); photoLabel->setPixmap(QPixmap::fromImage(image));
mainWindow->setWindowTitle("RagePhoto Qt Photo Viewer - " + QString::fromStdString(ragePhoto.title())); mainWindow->setWindowTitle(QStringLiteral("RagePhoto Qt Photo Viewer - ") + QString::fromUtf8(ragePhoto.title()));
return true; return true;
} }
else { else {

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* libragephoto RAGE Photo Parser * libragephoto RAGE Photo Parser
* Copyright (C) 2021-2022 Syping * Copyright (C) 2021-2023 Syping
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* libragephoto RAGE Photo Parser * libragephoto RAGE Photo Parser
* Copyright (C) 2021 Syping * Copyright (C) 2021-2023 Syping
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:

View file

@ -68,6 +68,43 @@ inline size_t writeBuffer(const void *input, char *output, size_t *pos, size_t o
return writeLen; return writeLen;
} }
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;
}
inline uint32_t charToUInt32LE(char *x) inline uint32_t charToUInt32LE(char *x)
{ {
return (static_cast<unsigned char>(x[3]) << 24 | return (static_cast<unsigned char>(x[3]) << 24 |
@ -88,27 +125,35 @@ inline void uInt32ToCharLE(uint32_t x, char *y)
/* BEGIN OF RAGEPHOTO CLASS */ /* BEGIN OF RAGEPHOTO CLASS */
RagePhoto::RagePhoto() RagePhoto::RagePhoto()
{ {
m_data.photoLoaded = false;
m_data.photoData = nullptr; m_data.photoData = nullptr;
m_data.description = nullptr;
m_data.json = nullptr;
m_data.header = nullptr;
m_data.title = nullptr;
setBufferDefault(); setBufferDefault();
} }
RagePhoto::~RagePhoto() RagePhoto::~RagePhoto()
{ {
std::free(m_data.photoData); std::free(m_data.photoData);
std::free(m_data.description);
std::free(m_data.json);
std::free(m_data.header);
std::free(m_data.title);
} }
void RagePhoto::clear() void RagePhoto::clear()
{ {
if (m_data.photoLoaded) { std::free(m_data.photoData);
std::free(m_data.photoData); m_data.photoData = nullptr;
m_data.photoData = nullptr; std::free(m_data.description);
m_data.photoLoaded = false; m_data.description = nullptr;
} std::free(m_data.json);
m_data.description.clear(); m_data.json = nullptr;
m_data.json.clear(); std::free(m_data.header);
m_data.header.clear(); m_data.header = nullptr;
m_data.title.clear(); std::free(m_data.title);
m_data.title = nullptr;
m_data.error = 0U; m_data.error = 0U;
m_data.photoFormat = 0UL; m_data.photoFormat = 0UL;
m_data.unnamedSum1 = 0UL; m_data.unnamedSum1 = 0UL;
@ -154,37 +199,53 @@ bool RagePhoto::load(const char *data, size_t length)
#if defined UNICODE_CODECVT #if defined UNICODE_CODECVT
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert; std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert;
m_data.header = convert.to_bytes(reinterpret_cast<char16_t*>(photoHeader)); const std::string photoHeader_string = convert.to_bytes(reinterpret_cast<char16_t*>(photoHeader));
if (convert.converted() == 0) { if (convert.converted() == 0) {
m_data.error = Error::UnicodeHeaderError; // 5 m_data.error = Error::UnicodeHeaderError; // 5
return false; return false;
} }
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);
#elif defined UNICODE_ICONV #elif defined UNICODE_ICONV
iconv_t iconv_in = iconv_open("UTF-8", "UTF-16LE"); iconv_t iconv_in = iconv_open("UTF-8", "UTF-16LE");
if (iconv_in == (iconv_t)-1) { if (iconv_in == (iconv_t)-1) {
m_data.error = Error::UnicodeInitError; // 4 m_data.error = Error::UnicodeInitError; // 4
return false; return false;
} }
char photoHeader_string[256]; m_data.header = static_cast<char*>(std::malloc(256));
if (!m_data.header) {
// add MALLOC ERROR INDICATOR
iconv_close(iconv_in);
return false;
}
size_t src_s = sizeof(photoHeader); size_t src_s = sizeof(photoHeader);
size_t dst_s = sizeof(photoHeader_string); size_t dst_s = sizeof(m_data.header);
char *src = photoHeader; char *src = photoHeader;
char *dst = photoHeader_string; char *dst = m_data.header;
const size_t ret = iconv(iconv_in, &src, &src_s, &dst, &dst_s); const size_t ret = iconv(iconv_in, &src, &src_s, &dst, &dst_s);
iconv_close(iconv_in); iconv_close(iconv_in);
if (ret == static_cast<size_t>(-1)) { if (ret == static_cast<size_t>(-1)) {
m_data.error = Error::UnicodeHeaderError; // 5 m_data.error = Error::UnicodeHeaderError; // 5
return false; return false;
} }
m_data.header = std::string(photoHeader_string);
#elif defined UNICODE_WINCVT #elif defined UNICODE_WINCVT
char photoHeader_string[256]; m_data.header = static_cast<char*>(std::malloc(256));
const int converted = WideCharToMultiByte(CP_UTF8, 0, reinterpret_cast<wchar_t*>(photoHeader), -1, photoHeader_string, 256, NULL, NULL); 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);
if (converted == 0) { if (converted == 0) {
std::free(m_data.header);
m_data.header = nullptr;
m_data.error = Error::UnicodeHeaderError; // 5 m_data.error = Error::UnicodeHeaderError; // 5
return false; return false;
} }
m_data.header = std::string(photoHeader_string);
#endif #endif
size = readBuffer(data, uInt32Buffer, &pos, 4, length); size = readBuffer(data, uInt32Buffer, &pos, 4, length);
@ -311,7 +372,6 @@ bool RagePhoto::load(const char *data, size_t length)
m_data.error = Error::PhotoReadError; // 16 m_data.error = Error::PhotoReadError; // 16
return false; return false;
} }
m_data.photoLoaded = true;
pos = m_data.jsonOffset + headerSize; pos = m_data.jsonOffset + headerSize;
size = readBuffer(data, markerBuffer, &pos, 4, length); size = readBuffer(data, markerBuffer, &pos, 4, length);
@ -335,19 +395,18 @@ bool RagePhoto::load(const char *data, size_t length)
m_data.jsonBuffer = charToUInt32LE(uInt32Buffer); m_data.jsonBuffer = charToUInt32LE(uInt32Buffer);
#endif #endif
char *t_jsonData = static_cast<char*>(std::malloc(m_data.jsonBuffer)); m_data.json = static_cast<char*>(std::malloc(m_data.jsonBuffer));
if (!t_jsonData) { if (!m_data.json) {
m_data.error = Error::JsonMallocError; // 20 m_data.error = Error::JsonMallocError; // 20
return false; return false;
} }
size = readBuffer(data, t_jsonData, &pos, m_data.jsonBuffer, length); size = readBuffer(data, m_data.json, &pos, m_data.jsonBuffer, length);
if (size != m_data.jsonBuffer) { if (size != m_data.jsonBuffer) {
std::free(t_jsonData); std::free(m_data.json);
m_data.json = nullptr;
m_data.error = Error::JsonReadError; // 21 m_data.error = Error::JsonReadError; // 21
return false; return false;
} }
m_data.json = std::string(t_jsonData);
std::free(t_jsonData);
pos = m_data.titlOffset + headerSize; pos = m_data.titlOffset + headerSize;
size = readBuffer(data, markerBuffer, &pos, 4, length); size = readBuffer(data, markerBuffer, &pos, 4, length);
@ -371,19 +430,18 @@ bool RagePhoto::load(const char *data, size_t length)
m_data.titlBuffer = charToUInt32LE(uInt32Buffer); m_data.titlBuffer = charToUInt32LE(uInt32Buffer);
#endif #endif
char *t_titlData = static_cast<char*>(std::malloc(m_data.titlBuffer)); m_data.title = static_cast<char*>(std::malloc(m_data.titlBuffer));
if (!t_titlData) { if (!m_data.title) {
m_data.error = Error::TitleMallocError; // 25 m_data.error = Error::TitleMallocError; // 25
return false; return false;
} }
size = readBuffer(data, t_titlData, &pos, m_data.titlBuffer, length); size = readBuffer(data, m_data.title, &pos, m_data.titlBuffer, length);
if (size != m_data.titlBuffer) { if (size != m_data.titlBuffer) {
std::free(t_titlData); std::free(m_data.title);
m_data.title = nullptr;
m_data.error = Error::TitleReadError; // 26 m_data.error = Error::TitleReadError; // 26
return false; return false;
} }
m_data.title = std::string(t_titlData);
std::free(t_titlData);
pos = m_data.descOffset + headerSize; pos = m_data.descOffset + headerSize;
size = readBuffer(data, markerBuffer, &pos, 4, length); size = readBuffer(data, markerBuffer, &pos, 4, length);
@ -407,19 +465,18 @@ bool RagePhoto::load(const char *data, size_t length)
m_data.descBuffer = charToUInt32LE(uInt32Buffer); m_data.descBuffer = charToUInt32LE(uInt32Buffer);
#endif #endif
char *t_descData = static_cast<char*>(std::malloc(m_data.descBuffer)); m_data.description = static_cast<char*>(std::malloc(m_data.descBuffer));
if (!t_descData) { if (!m_data.description) {
m_data.error = Error::DescMallocError; // 30 m_data.error = Error::DescMallocError; // 30
return false; return false;
} }
size = readBuffer(data, t_descData, &pos, m_data.descBuffer, length); size = readBuffer(data, m_data.description, &pos, m_data.descBuffer, length);
if (size != m_data.descBuffer) { if (size != m_data.descBuffer) {
std::free(t_descData); std::free(m_data.description);
m_data.description = nullptr;
m_data.error = Error::DescReadError; // 31 m_data.error = Error::DescReadError; // 31
return false; return false;
} }
m_data.description = std::string(t_descData);
std::free(t_descData);
pos = m_data.endOfFile + headerSize - 4; pos = m_data.endOfFile + headerSize - 4;
size = readBuffer(data, markerBuffer, &pos, 4, length); size = readBuffer(data, markerBuffer, &pos, 4, length);
@ -446,10 +503,13 @@ bool RagePhoto::load(const char *data, size_t length)
std::cout << "photoBuffer: " << m_data.photoBuffer << std::endl; std::cout << "photoBuffer: " << m_data.photoBuffer << std::endl;
std::cout << "descBuffer: " << m_data.descBuffer << std::endl; std::cout << "descBuffer: " << m_data.descBuffer << std::endl;
std::cout << "descOffset: " << m_data.descOffset << std::endl; std::cout << "descOffset: " << m_data.descOffset << std::endl;
std::cout << "description: " << m_data.description << std::endl;
std::cout << "jsonBuffer: " << m_data.jsonBuffer << std::endl; std::cout << "jsonBuffer: " << m_data.jsonBuffer << std::endl;
std::cout << "jsonOffset: " << m_data.jsonOffset << std::endl; std::cout << "jsonOffset: " << m_data.jsonOffset << std::endl;
std::cout << "json: " << m_data.json << std::endl;
std::cout << "titlBuffer: " << m_data.titlBuffer << std::endl; std::cout << "titlBuffer: " << m_data.titlBuffer << std::endl;
std::cout << "titlOffset: " << m_data.titlOffset << std::endl; std::cout << "titlOffset: " << m_data.titlOffset << std::endl;
std::cout << "title: " << m_data.title << std::endl;
std::cout << "eofOffset: " << m_data.endOfFile << std::endl; std::cout << "eofOffset: " << m_data.endOfFile << std::endl;
std::cout << "setBufferOffsets()" << std::endl; std::cout << "setBufferOffsets()" << std::endl;
setBufferOffsets(); setBufferOffsets();
@ -469,11 +529,6 @@ bool RagePhoto::load(const char *data, size_t length)
return false; return false;
#endif #endif
} }
else {
auto it = m_loadFuncs.find(m_data.photoFormat);
if (it != m_loadFuncs.end())
return it->second(data, length, &m_data);
}
m_data.error = Error::IncompatibleFormat; // 2 m_data.error = Error::IncompatibleFormat; // 2
return false; return false;
} }
@ -509,7 +564,7 @@ uint32_t RagePhoto::format() const
const std::string RagePhoto::photo() const const std::string RagePhoto::photo() const
{ {
if (m_data.photoLoaded) if (m_data.photoData)
return std::string(m_data.photoData, m_data.photoSize); return std::string(m_data.photoData, m_data.photoSize);
else else
return std::string(); return std::string();
@ -517,36 +572,33 @@ const std::string RagePhoto::photo() const
const char* RagePhoto::photoData() const const char* RagePhoto::photoData() const
{ {
if (m_data.photoLoaded) return m_data.photoData;
return m_data.photoData;
else
return nullptr;
} }
uint32_t RagePhoto::photoSize() const uint32_t RagePhoto::photoSize() const
{ {
if (m_data.photoLoaded) if (m_data.photoData)
return m_data.photoSize; return m_data.photoSize;
else else
return 0UL; return 0UL;
} }
const std::string& RagePhoto::description() const const char* RagePhoto::description() const
{ {
return m_data.description; return m_data.description;
} }
const std::string& RagePhoto::json() const const char* RagePhoto::json() const
{ {
return m_data.json; return m_data.json;
} }
const std::string& RagePhoto::header() const const char* RagePhoto::header() const
{ {
return m_data.header; return m_data.header;
} }
const std::string& RagePhoto::title() const const char* RagePhoto::title() const
{ {
return m_data.title; return m_data.title;
} }
@ -579,12 +631,10 @@ bool RagePhoto::save(char *data, uint32_t photoFormat)
m_data.error = Error::UnicodeInitError; // 4 m_data.error = Error::UnicodeInitError; // 4
return false; return false;
} }
char photoHeader_string[256];
std::memcpy(photoHeader_string, m_data.header.data(), m_data.header.size());
char photoHeader[256]{}; char photoHeader[256]{};
size_t src_s = m_data.header.size(); size_t src_s = strlen(m_data.header);
size_t dst_s = sizeof(photoHeader); size_t dst_s = sizeof(photoHeader);
char *src = photoHeader_string; char *src = m_data.header;
char *dst = photoHeader; char *dst = photoHeader;
const size_t ret = iconv(iconv_in, &src, &src_s, &dst, &dst_s); const size_t ret = iconv(iconv_in, &src, &src_s, &dst, &dst_s);
iconv_close(iconv_in); iconv_close(iconv_in);
@ -595,7 +645,7 @@ bool RagePhoto::save(char *data, uint32_t photoFormat)
const size_t photoHeader_size = 256; const size_t photoHeader_size = 256;
#elif defined UNICODE_WINCVT #elif defined UNICODE_WINCVT
char photoHeader[256]{}; char photoHeader[256]{};
const int converted = MultiByteToWideChar(CP_UTF8, 0, m_data.header.data(), static_cast<int>(m_data.header.size()), reinterpret_cast<wchar_t*>(photoHeader), 256 / sizeof(wchar_t)); 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));
if (converted == 0) { if (converted == 0) {
m_data.error = Error::UnicodeHeaderError; // 5 m_data.error = Error::UnicodeHeaderError; // 5
return false; return false;
@ -608,19 +658,19 @@ bool RagePhoto::save(char *data, uint32_t photoFormat)
return false; return false;
} }
const size_t jsonString_size = m_data.json.size() + 1; const size_t jsonString_size = strlen(m_data.json) + 1;
if (jsonString_size > m_data.jsonBuffer) { if (jsonString_size > m_data.jsonBuffer) {
m_data.error = Error::JsonBufferTight; // 36 m_data.error = Error::JsonBufferTight; // 36
return false; return false;
} }
const size_t titlString_size = m_data.title.size() + 1; const size_t titlString_size = strlen(m_data.title) + 1;
if (titlString_size > m_data.titlBuffer) { if (titlString_size > m_data.titlBuffer) {
m_data.error = Error::TitleBufferTight; // 37 m_data.error = Error::TitleBufferTight; // 37
return false; return false;
} }
const size_t descString_size = m_data.description.size() + 1; const size_t descString_size = strlen(m_data.description) + 1;
if (descString_size > m_data.descBuffer) { if (descString_size > m_data.descBuffer) {
m_data.error = Error::DescBufferTight; // 38 m_data.error = Error::DescBufferTight; // 38
return false; return false;
@ -725,7 +775,7 @@ bool RagePhoto::save(char *data, uint32_t photoFormat)
#endif #endif
writeBuffer(uInt32Buffer, data, &pos, length, 4); writeBuffer(uInt32Buffer, data, &pos, length, 4);
writeBuffer(m_data.json.c_str(), data, &pos, length, jsonString_size); writeBuffer(m_data.json, data, &pos, length, jsonString_size);
for (size_t i = jsonString_size; i < m_data.jsonBuffer; i++) { for (size_t i = jsonString_size; i < m_data.jsonBuffer; i++) {
writeBuffer("\0", data, &pos, length, 1); writeBuffer("\0", data, &pos, length, 1);
} }
@ -740,7 +790,7 @@ bool RagePhoto::save(char *data, uint32_t photoFormat)
#endif #endif
writeBuffer(uInt32Buffer, data, &pos, length, 4); writeBuffer(uInt32Buffer, data, &pos, length, 4);
writeBuffer(m_data.title.c_str(), data, &pos, length, titlString_size); writeBuffer(m_data.title, data, &pos, length, titlString_size);
for (size_t i = titlString_size; i < m_data.titlBuffer; i++) { for (size_t i = titlString_size; i < m_data.titlBuffer; i++) {
writeBuffer("\0", data, &pos, length, 1); writeBuffer("\0", data, &pos, length, 1);
} }
@ -755,7 +805,7 @@ bool RagePhoto::save(char *data, uint32_t photoFormat)
#endif #endif
writeBuffer(uInt32Buffer, data, &pos, length, 4); writeBuffer(uInt32Buffer, data, &pos, length, 4);
writeBuffer(m_data.description.c_str(), data, &pos, length, descString_size); writeBuffer(m_data.description, data, &pos, length, descString_size);
for (size_t i = descString_size; i < m_data.descBuffer; i++) { for (size_t i = descString_size; i < m_data.descBuffer; i++) {
writeBuffer("\0", data, &pos, length, 1); writeBuffer("\0", data, &pos, length, 1);
} }
@ -877,13 +927,17 @@ inline void RagePhoto::setBufferOffsets(RagePhotoData *ragePhotoData)
ragePhotoData->endOfFile = ragePhotoData->descOffset + ragePhotoData->descBuffer + 12; ragePhotoData->endOfFile = ragePhotoData->descOffset + ragePhotoData->descBuffer + 12;
} }
void RagePhoto::setDescription(const std::string &description, uint32_t bufferSize) void RagePhoto::setDescription(const char *description, uint32_t bufferSize)
{ {
m_data.description = description; if (!writeDataChar(description, &m_data.description)) {
m_data.error = Error::DescMallocError; // 30
return;
}
if (bufferSize != 0) { if (bufferSize != 0) {
m_data.descBuffer = bufferSize; m_data.descBuffer = bufferSize;
setBufferOffsets(); setBufferOffsets();
} }
m_data.error = Error::NoError; // 255
} }
void RagePhoto::setFormat(uint32_t photoFormat) void RagePhoto::setFormat(uint32_t photoFormat)
@ -891,29 +945,32 @@ void RagePhoto::setFormat(uint32_t photoFormat)
m_data.photoFormat = photoFormat; m_data.photoFormat = photoFormat;
} }
void RagePhoto::setFormatLoadFunction(uint32_t photoFormat, RagePhotoLoadFunc func) void RagePhoto::setJson(const char *json, uint32_t bufferSize)
{ {
m_loadFuncs[photoFormat] = func; if (!writeDataChar(json, &m_data.json)) {
} m_data.error = Error::JsonMallocError; // 20
return;
void RagePhoto::setJson(const std::string &json, uint32_t bufferSize) }
{
m_data.json = json;
if (bufferSize != 0) { if (bufferSize != 0) {
m_data.jsonBuffer = bufferSize; m_data.jsonBuffer = bufferSize;
setBufferOffsets(); setBufferOffsets();
} }
m_data.error = Error::NoError; // 255
} }
void RagePhoto::setHeader(const std::string &header, uint32_t headerSum) void RagePhoto::setHeader(const char *header, uint32_t headerSum)
{ {
m_data.header = header; if (!writeDataChar(header, &m_data.header)) {
//m_data.error = Error::HeaderMallocError;
return;
}
m_data.headerSum = headerSum; m_data.headerSum = headerSum;
m_data.error = Error::NoError; // 255
} }
bool RagePhoto::setPhoto(const char *data, uint32_t size, uint32_t bufferSize) bool RagePhoto::setPhoto(const char *data, uint32_t size, uint32_t bufferSize)
{ {
if (m_data.photoLoaded) { if (m_data.photoData) {
if (m_data.photoSize > size) { if (m_data.photoSize > size) {
char *t_photoData = static_cast<char*>(std::realloc(m_data.photoData, size)); char *t_photoData = static_cast<char*>(std::realloc(m_data.photoData, size));
if (!t_photoData) { if (!t_photoData) {
@ -929,7 +986,6 @@ bool RagePhoto::setPhoto(const char *data, uint32_t size, uint32_t bufferSize)
m_data.photoData = static_cast<char*>(std::malloc(size)); m_data.photoData = static_cast<char*>(std::malloc(size));
if (!m_data.photoData) { if (!m_data.photoData) {
m_data.error = Error::PhotoMallocError; // 15 m_data.error = Error::PhotoMallocError; // 15
m_data.photoLoaded = false;
return false; return false;
} }
std::memcpy(m_data.photoData, data, size); std::memcpy(m_data.photoData, data, size);
@ -946,7 +1002,6 @@ bool RagePhoto::setPhoto(const char *data, uint32_t size, uint32_t bufferSize)
return false; return false;
} }
std::memcpy(m_data.photoData, data, size); std::memcpy(m_data.photoData, data, size);
m_data.photoLoaded = true;
m_data.photoSize = size; m_data.photoSize = size;
} }
@ -964,13 +1019,17 @@ bool RagePhoto::setPhoto(const std::string &data, uint32_t bufferSize)
return setPhoto(data.data(), static_cast<uint32_t>(data.size()), bufferSize); return setPhoto(data.data(), static_cast<uint32_t>(data.size()), bufferSize);
} }
void RagePhoto::setTitle(const std::string &title, uint32_t bufferSize) void RagePhoto::setTitle(const char *title, uint32_t bufferSize)
{ {
m_data.title = title; if (!writeDataChar(title, &m_data.title)) {
m_data.error = Error::TitleMallocError; // 25
return;
}
if (bufferSize != 0) { if (bufferSize != 0) {
m_data.titlBuffer = bufferSize; m_data.titlBuffer = bufferSize;
setBufferOffsets(); setBufferOffsets();
} }
m_data.error = Error::NoError; // 255
} }
#ifdef LIBRAGEPHOTO_C_API #ifdef LIBRAGEPHOTO_C_API
@ -1026,7 +1085,7 @@ uint32_t ragephoto_format_rdr2()
const char* ragephoto_getphotodesc(ragephoto_t instance) const char* ragephoto_getphotodesc(ragephoto_t instance)
{ {
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance); RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
return ragePhoto->description().c_str(); return ragePhoto->description();
} }
uint32_t ragephoto_getphotoformat(ragephoto_t instance) uint32_t ragephoto_getphotoformat(ragephoto_t instance)
@ -1044,13 +1103,13 @@ const char* ragephoto_getphotojpeg(ragephoto_t instance)
const char* ragephoto_getphotojson(ragephoto_t instance) const char* ragephoto_getphotojson(ragephoto_t instance)
{ {
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance); RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
return ragePhoto->json().c_str(); return ragePhoto->json();
} }
const char* ragephoto_getphotoheader(ragephoto_t instance) const char* ragephoto_getphotoheader(ragephoto_t instance)
{ {
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance); RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
return ragePhoto->header().c_str(); return ragePhoto->header();
} }
uint32_t ragephoto_getphotosize(ragephoto_t instance) uint32_t ragephoto_getphotosize(ragephoto_t instance)
@ -1062,7 +1121,7 @@ uint32_t ragephoto_getphotosize(ragephoto_t instance)
const char* ragephoto_getphototitle(ragephoto_t instance) const char* ragephoto_getphototitle(ragephoto_t instance)
{ {
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance); RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
return ragePhoto->title().c_str(); return ragePhoto->title();
} }
size_t ragephoto_getsavesize(ragephoto_t instance) size_t ragephoto_getsavesize(ragephoto_t instance)

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* libragephoto RAGE Photo Parser * libragephoto RAGE Photo Parser
* Copyright (C) 2021-2022 Syping * Copyright (C) 2021-2023 Syping
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:
@ -22,15 +22,11 @@
#ifdef __cplusplus #ifdef __cplusplus
#include "libragephoto_global.h" #include "libragephoto_global.h"
#include "RagePhotoData.h" #include "RagePhotoData.h"
#include <unordered_map>
#include <functional>
#include <iostream> #include <iostream>
#include <cstdlib> #include <cstdlib>
#include <cstdint> #include <cstdint>
#include <cstdio> #include <cstdio>
typedef std::function<bool(const char*, size_t, RagePhotoData*)> RagePhotoLoadFunc;
/** /**
* \brief GTA V and RDR 2 Photo Parser. * \brief GTA V and RDR 2 Photo Parser.
*/ */
@ -117,10 +113,10 @@ public:
const std::string photo() const; /**< Returns the Photo JPEG data. */ const std::string photo() const; /**< Returns the Photo JPEG data. */
const char *photoData() const; /**< Returns the Photo JPEG data. */ const char *photoData() const; /**< Returns the Photo JPEG data. */
uint32_t photoSize() const; /**< Returns the Photo JPEG data size. */ uint32_t photoSize() const; /**< Returns the Photo JPEG data size. */
const std::string& description() const; /**< Returns the Photo description. */ const char* description() const; /**< Returns the Photo description. */
const std::string& json() const; /**< Returns the Photo JSON data. */ const char* json() const; /**< Returns the Photo JSON data. */
const std::string& header() const; /**< Returns the Photo header. */ const char* header() const; /**< Returns the Photo header. */
const std::string& title() const; /**< Returns the Photo title. */ const char* title() const; /**< Returns the Photo title. */
static const char* version(); /**< Returns the library version. */ static const char* version(); /**< Returns the library version. */
/** Saves a Photo to a char*. /** Saves a Photo to a char*.
* \param data Photo data * \param data Photo data
@ -150,11 +146,10 @@ public:
static void setBufferDefault(RagePhotoData *ragePhotoData); /**< Sets all cross-format Buffer to default size. */ static void setBufferDefault(RagePhotoData *ragePhotoData); /**< Sets all cross-format Buffer to default size. */
void setBufferOffsets(); /**< Moves all Buffer offsets to correct position. */ void setBufferOffsets(); /**< Moves all Buffer offsets to correct position. */
static void setBufferOffsets(RagePhotoData *ragePhotoData); /**< Moves all Buffer offsets to correct position. */ static void setBufferOffsets(RagePhotoData *ragePhotoData); /**< Moves all Buffer offsets to correct position. */
void setDescription(const std::string &description, uint32_t bufferSize = 0); /**< Sets the Photo description. */ void setDescription(const char *description, uint32_t bufferSize = 0); /**< Sets the Photo description. */
void setFormat(uint32_t photoFormat); /**< Sets the Photo Format (GTA V or RDR 2). */ void setFormat(uint32_t photoFormat); /**< Sets the Photo Format (GTA V or RDR 2). */
void setFormatLoadFunction(uint32_t photoFormat, RagePhotoLoadFunc func); /**< Sets a custom Photo Format load function. */ void setJson(const char *json, uint32_t bufferSize = 0); /**< Sets the Photo JSON data. */
void setJson(const std::string &json, uint32_t bufferSize = 0); /**< Sets the Photo JSON data. */ void setHeader(const char *header, uint32_t headerSum); /**< Sets the Photo header. (EXPERT ONLY) */
void setHeader(const std::string &header, uint32_t headerSum); /**< Sets the Photo header. (EXPERT ONLY) */
/** Sets the Photo JPEG data. /** Sets the Photo JPEG data.
* \param data JPEG data * \param data JPEG data
* \param size JPEG data size * \param size JPEG data size
@ -166,10 +161,9 @@ public:
* \param bufferSize JPEG buffer size * \param bufferSize JPEG buffer size
*/ */
bool setPhoto(const std::string &data, uint32_t bufferSize = 0); bool setPhoto(const std::string &data, uint32_t bufferSize = 0);
void setTitle(const std::string &title, uint32_t bufferSize = 0); /**< Sets the Photo title. */ void setTitle(const char *title, uint32_t bufferSize = 0); /**< Sets the Photo title. */
private: private:
std::unordered_map<uint8_t, RagePhotoLoadFunc> m_loadFuncs;
RagePhotoData m_data; RagePhotoData m_data;
}; };
#else #else

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* libragephoto RAGE Photo Parser * libragephoto RAGE Photo Parser
* Copyright (C) 2021-2022 Syping * Copyright (C) 2021-2023 Syping
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:
@ -33,6 +33,64 @@
class RagePhotoA class RagePhotoA
{ {
public: public:
/** Default sizes */
enum DefaultSize : uint32_t {
DEFAULT_GTA5_PHOTOBUFFER = 524288UL, /**< GTA V default Photo Buffer Size */
DEFAULT_RDR2_PHOTOBUFFER = 1048576UL, /**< RDR 2 default Photo Buffer Size */
DEFAULT_DESCBUFFER = 256UL, /**< Default Description Buffer Size */
DEFAULT_JSONBUFFER = 3072UL, /**< Default JSON Buffer Size */
DEFAULT_TITLBUFFER = 256UL, /**< Default Title Buffer Size */
GTA5_HEADERSIZE = 264UL, /**< GTA V Header Size */
RDR2_HEADERSIZE = 272UL, /**< RDR 2 Header Size */
};
/** Parsing and set errors */
enum Error : uint8_t {
DescBufferTight = 38, /**< Description Buffer is too tight */
DescMallocError = 30, /**< Description Buffer can't be allocated */
DescReadError = 31, /**< Description can't be read successfully */
HeaderBufferTight = 34, /**< Header Buffer is too tight */
IncompatibleFormat = 2, /**< Format is incompatible */
IncompleteChecksum = 6, /**< Header checksum is incomplete */
IncompleteDescBuffer = 29, /**< Description Buffer Size is incomplete */
IncompleteDescMarker = 27, /**< Description Marker is incomplete */
IncompleteDescOffset = 10, /**< Description Offset is incomplete */
IncompleteEOF = 7, /**< End Of File Offset is incomplete */
IncompleteHeader = 3, /**< Header is incomplete */
IncompleteJendMarker = 32, /**< JEND Marker is incomplete */
IncompleteJpegMarker = 11, /**< JPEG Marker is incomplete */
IncompleteJsonBuffer = 19, /**< JSON Buffer Size is incomplete */
IncompleteJsonMarker = 17, /**< JSON Marker incomplete */
IncompleteJsonOffset = 8, /**< JSON Offset incomplete */
IncompletePhotoBuffer = 13, /**< Photo Buffer Size is incomplete */
IncompletePhotoSize = 14, /**< Photo Size is incomplete */
IncompleteTitleBuffer = 24, /**< Title Buffer Size is incomplete */
IncompleteTitleMarker = 22, /**< Title Marker is incomplete */
IncompleteTitleOffset = 9, /**< Title Offset is incomplete */
IncorrectDescMarker = 28, /**< Description Marker is incorrect */
IncorrectJendMarker = 33, /**< JEND Marker is incorrect */
IncorrectJpegMarker = 12, /**< JPEG Marker is incorrect */
IncorrectJsonMarker = 18, /**< JSON Marker is incorrect */
IncorrectTitleMarker = 23, /**< Title Marker is incorrect */
JsonBufferTight = 36, /**< JSON Buffer is too tight */
JsonMallocError = 20, /**< JSON Buffer can't be allocated */
JsonReadError = 21, /**< JSON can't be read successfully */
NoError = 255, /**< Finished without errors */
NoFormatIdentifier = 1, /**< No format detected, empty file */
PhotoBufferTight = 35, /**< Photo Buffer is too tight */
PhotoMallocError = 15, /**< Photo Buffer can't be allocated */
PhotoReadError = 16, /**< Photo can't be read */
TitleBufferTight = 37, /**< Title Buffer is too tight */
TitleMallocError = 25, /**< Title Buffer can't be allocated */
TitleReadError = 26, /**< Title can't be read */
UnicodeHeaderError = 5, /**< Header can't be decoded */
UnicodeInitError = 4, /**< Failed to initialise Unicode decoder */
Uninitialised = 0, /**< Uninitialised, file access failed */
};
/** Photo Formats */
enum PhotoFormat : uint32_t {
GTA5 = 0x01000000UL, /**< GTA V Photo Format */
RDR2 = 0x04000000UL, /**< RDR 2 Photo Format */
};
RagePhotoA() { RagePhotoA() {
instance = ragephoto_open(); instance = ragephoto_open();
} }

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* libragephoto RAGE Photo Parser * libragephoto RAGE Photo Parser
* Copyright (C) 2021-2022 Syping * Copyright (C) 2021-2023 Syping
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* libragephoto RAGE Photo Parser * libragephoto RAGE Photo Parser
* Copyright (C) 2021-2022 Syping * Copyright (C) 2021-2023 Syping
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:
@ -26,12 +26,11 @@
struct LIBRAGEPHOTO_CXX_EXPORT RagePhotoData struct LIBRAGEPHOTO_CXX_EXPORT RagePhotoData
{ {
bool photoLoaded;
char* photoData; char* photoData;
std::string description; char* description;
std::string json; char* json;
std::string header; char* header;
std::string title; char* title;
uint8_t error; uint8_t error;
uint32_t descBuffer; uint32_t descBuffer;
uint32_t descOffset; uint32_t descOffset;