From e08b45d9809783a0c0d2535def9398c3b4ff5ac9 Mon Sep 17 00:00:00 2001 From: Syping Date: Sat, 7 Jan 2023 20:43:11 +0100 Subject: [PATCH] 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 --- CMakeLists.txt | 2 +- examples/ragephoto-gtkviewer/src/main.cpp | 2 +- examples/ragephoto-qtviewer/src/main.cpp | 2 +- src/RagePhoto-Extract.c | 2 +- src/RagePhoto-Extract.cpp | 2 +- src/RagePhoto.cpp | 225 ++++++++++++++-------- src/RagePhoto.h | 24 +-- src/RagePhotoA.h | 60 +++++- src/RagePhotoC.h | 2 +- src/RagePhotoData.h | 11 +- 10 files changed, 221 insertions(+), 111 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3889a22..e39df3f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,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) set(CMAKE_INCLUDE_CURRENT_DIR ON) diff --git a/examples/ragephoto-gtkviewer/src/main.cpp b/examples/ragephoto-gtkviewer/src/main.cpp index d89f89d..2d8d8f3 100644 --- a/examples/ragephoto-gtkviewer/src/main.cpp +++ b/examples/ragephoto-gtkviewer/src/main.cpp @@ -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); gdk_pixbuf_loader_close(pixbuf_loader, NULL); 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; } else { diff --git a/examples/ragephoto-qtviewer/src/main.cpp b/examples/ragephoto-qtviewer/src/main.cpp index 92c8846..0680aa4 100644 --- a/examples/ragephoto-qtviewer/src/main.cpp +++ b/examples/ragephoto-qtviewer/src/main.cpp @@ -47,7 +47,7 @@ bool readPhotoFile(const QString &filename, QMainWindow *mainWindow, QLabel *pho const QByteArray photoData = QByteArray::fromRawData(ragePhoto.photoData(), ragePhoto.photoSize()); const QImage image = QImage::fromData(photoData, "JPEG"); 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; } else { diff --git a/src/RagePhoto-Extract.c b/src/RagePhoto-Extract.c index 24579a9..c5a9a20 100644 --- a/src/RagePhoto-Extract.c +++ b/src/RagePhoto-Extract.c @@ -1,6 +1,6 @@ /***************************************************************************** * 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, * are permitted provided that the following conditions are met: diff --git a/src/RagePhoto-Extract.cpp b/src/RagePhoto-Extract.cpp index be57d64..500d8d4 100644 --- a/src/RagePhoto-Extract.cpp +++ b/src/RagePhoto-Extract.cpp @@ -1,6 +1,6 @@ /***************************************************************************** * 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, * are permitted provided that the following conditions are met: diff --git a/src/RagePhoto.cpp b/src/RagePhoto.cpp index 565592c..eeedb63 100644 --- a/src/RagePhoto.cpp +++ b/src/RagePhoto.cpp @@ -68,6 +68,43 @@ inline size_t writeBuffer(const void *input, char *output, size_t *pos, size_t o 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(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(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(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) { return (static_cast(x[3]) << 24 | @@ -88,27 +125,35 @@ inline void uInt32ToCharLE(uint32_t x, char *y) /* BEGIN OF RAGEPHOTO CLASS */ RagePhoto::RagePhoto() { - m_data.photoLoaded = false; m_data.photoData = nullptr; + m_data.description = nullptr; + m_data.json = nullptr; + m_data.header = nullptr; + m_data.title = nullptr; setBufferDefault(); } RagePhoto::~RagePhoto() { 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() { - if (m_data.photoLoaded) { - std::free(m_data.photoData); - m_data.photoData = nullptr; - m_data.photoLoaded = false; - } - m_data.description.clear(); - m_data.json.clear(); - m_data.header.clear(); - m_data.title.clear(); + 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; m_data.error = 0U; m_data.photoFormat = 0UL; m_data.unnamedSum1 = 0UL; @@ -154,37 +199,53 @@ bool RagePhoto::load(const char *data, size_t length) #if defined UNICODE_CODECVT std::wstring_convert,char16_t> convert; - m_data.header = convert.to_bytes(reinterpret_cast(photoHeader)); + const std::string photoHeader_string = convert.to_bytes(reinterpret_cast(photoHeader)); if (convert.converted() == 0) { m_data.error = Error::UnicodeHeaderError; // 5 return false; } + const size_t photoHeader_size = photoHeader_string.size() + 1; + m_data.header = static_cast(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 iconv_t iconv_in = iconv_open("UTF-8", "UTF-16LE"); if (iconv_in == (iconv_t)-1) { m_data.error = Error::UnicodeInitError; // 4 return false; } - char photoHeader_string[256]; + m_data.header = static_cast(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 dst_s = sizeof(photoHeader_string); + size_t dst_s = sizeof(m_data.header); 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); iconv_close(iconv_in); if (ret == static_cast(-1)) { m_data.error = Error::UnicodeHeaderError; // 5 return false; } - m_data.header = std::string(photoHeader_string); #elif defined UNICODE_WINCVT - char photoHeader_string[256]; - const int converted = WideCharToMultiByte(CP_UTF8, 0, reinterpret_cast(photoHeader), -1, photoHeader_string, 256, NULL, NULL); + m_data.header = static_cast(std::malloc(256)); + if (!m_data.header) { + // add MALLOC ERROR INDICATOR + return false; + } + const int converted = WideCharToMultiByte(CP_UTF8, 0, reinterpret_cast(photoHeader), -1, m_data.header, 256, NULL, NULL); if (converted == 0) { + std::free(m_data.header); + m_data.header = nullptr; m_data.error = Error::UnicodeHeaderError; // 5 return false; } - m_data.header = std::string(photoHeader_string); #endif 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 return false; } - m_data.photoLoaded = true; pos = m_data.jsonOffset + headerSize; 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); #endif - char *t_jsonData = static_cast(std::malloc(m_data.jsonBuffer)); - if (!t_jsonData) { + m_data.json = static_cast(std::malloc(m_data.jsonBuffer)); + if (!m_data.json) { m_data.error = Error::JsonMallocError; // 20 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) { - std::free(t_jsonData); + std::free(m_data.json); + m_data.json = nullptr; m_data.error = Error::JsonReadError; // 21 return false; } - m_data.json = std::string(t_jsonData); - std::free(t_jsonData); pos = m_data.titlOffset + headerSize; 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); #endif - char *t_titlData = static_cast(std::malloc(m_data.titlBuffer)); - if (!t_titlData) { + m_data.title = static_cast(std::malloc(m_data.titlBuffer)); + if (!m_data.title) { m_data.error = Error::TitleMallocError; // 25 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) { - std::free(t_titlData); + std::free(m_data.title); + m_data.title = nullptr; m_data.error = Error::TitleReadError; // 26 return false; } - m_data.title = std::string(t_titlData); - std::free(t_titlData); pos = m_data.descOffset + headerSize; 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); #endif - char *t_descData = static_cast(std::malloc(m_data.descBuffer)); - if (!t_descData) { + m_data.description = static_cast(std::malloc(m_data.descBuffer)); + if (!m_data.description) { m_data.error = Error::DescMallocError; // 30 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) { - std::free(t_descData); + std::free(m_data.description); + m_data.description = nullptr; m_data.error = Error::DescReadError; // 31 return false; } - m_data.description = std::string(t_descData); - std::free(t_descData); pos = m_data.endOfFile + headerSize - 4; 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 << "descBuffer: " << m_data.descBuffer << 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 << "jsonOffset: " << m_data.jsonOffset << std::endl; + std::cout << "json: " << m_data.json << std::endl; std::cout << "titlBuffer: " << m_data.titlBuffer << 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 << "setBufferOffsets()" << std::endl; setBufferOffsets(); @@ -469,11 +529,6 @@ bool RagePhoto::load(const char *data, size_t length) return false; #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 return false; } @@ -509,7 +564,7 @@ uint32_t RagePhoto::format() const const std::string RagePhoto::photo() const { - if (m_data.photoLoaded) + if (m_data.photoData) return std::string(m_data.photoData, m_data.photoSize); else return std::string(); @@ -517,36 +572,33 @@ const std::string RagePhoto::photo() const const char* RagePhoto::photoData() const { - if (m_data.photoLoaded) - return m_data.photoData; - else - return nullptr; + return m_data.photoData; } uint32_t RagePhoto::photoSize() const { - if (m_data.photoLoaded) + if (m_data.photoData) return m_data.photoSize; else return 0UL; } -const std::string& RagePhoto::description() const +const char* RagePhoto::description() const { return m_data.description; } -const std::string& RagePhoto::json() const +const char* RagePhoto::json() const { return m_data.json; } -const std::string& RagePhoto::header() const +const char* RagePhoto::header() const { return m_data.header; } -const std::string& RagePhoto::title() const +const char* RagePhoto::title() const { return m_data.title; } @@ -579,12 +631,10 @@ bool RagePhoto::save(char *data, uint32_t photoFormat) m_data.error = Error::UnicodeInitError; // 4 return false; } - char photoHeader_string[256]; - std::memcpy(photoHeader_string, m_data.header.data(), m_data.header.size()); 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); - char *src = photoHeader_string; + char *src = m_data.header; char *dst = photoHeader; const size_t ret = iconv(iconv_in, &src, &src_s, &dst, &dst_s); iconv_close(iconv_in); @@ -595,7 +645,7 @@ bool RagePhoto::save(char *data, uint32_t photoFormat) const size_t photoHeader_size = 256; #elif defined UNICODE_WINCVT char photoHeader[256]{}; - const int converted = MultiByteToWideChar(CP_UTF8, 0, m_data.header.data(), static_cast(m_data.header.size()), reinterpret_cast(photoHeader), 256 / sizeof(wchar_t)); + const int converted = MultiByteToWideChar(CP_UTF8, 0, m_data.header, static_cast(strlen(m_data.header)), reinterpret_cast(photoHeader), 256 / sizeof(wchar_t)); if (converted == 0) { m_data.error = Error::UnicodeHeaderError; // 5 return false; @@ -608,19 +658,19 @@ bool RagePhoto::save(char *data, uint32_t photoFormat) 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) { m_data.error = Error::JsonBufferTight; // 36 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) { m_data.error = Error::TitleBufferTight; // 37 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) { m_data.error = Error::DescBufferTight; // 38 return false; @@ -725,7 +775,7 @@ bool RagePhoto::save(char *data, uint32_t photoFormat) #endif 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++) { writeBuffer("\0", data, &pos, length, 1); } @@ -740,7 +790,7 @@ bool RagePhoto::save(char *data, uint32_t photoFormat) #endif 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++) { writeBuffer("\0", data, &pos, length, 1); } @@ -755,7 +805,7 @@ bool RagePhoto::save(char *data, uint32_t photoFormat) #endif 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++) { writeBuffer("\0", data, &pos, length, 1); } @@ -877,13 +927,17 @@ inline void RagePhoto::setBufferOffsets(RagePhotoData *ragePhotoData) 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) { m_data.descBuffer = bufferSize; setBufferOffsets(); } + m_data.error = Error::NoError; // 255 } void RagePhoto::setFormat(uint32_t photoFormat) @@ -891,29 +945,32 @@ void RagePhoto::setFormat(uint32_t 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; -} - -void RagePhoto::setJson(const std::string &json, uint32_t bufferSize) -{ - m_data.json = json; + if (!writeDataChar(json, &m_data.json)) { + m_data.error = Error::JsonMallocError; // 20 + return; + } if (bufferSize != 0) { m_data.jsonBuffer = bufferSize; 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.error = Error::NoError; // 255 } 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) { char *t_photoData = static_cast(std::realloc(m_data.photoData, size)); if (!t_photoData) { @@ -929,7 +986,6 @@ bool RagePhoto::setPhoto(const char *data, uint32_t size, uint32_t bufferSize) m_data.photoData = static_cast(std::malloc(size)); if (!m_data.photoData) { m_data.error = Error::PhotoMallocError; // 15 - m_data.photoLoaded = false; return false; } 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; } std::memcpy(m_data.photoData, data, size); - m_data.photoLoaded = true; m_data.photoSize = size; } @@ -964,13 +1019,17 @@ bool RagePhoto::setPhoto(const std::string &data, uint32_t bufferSize) return setPhoto(data.data(), static_cast(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) { m_data.titlBuffer = bufferSize; setBufferOffsets(); } + m_data.error = Error::NoError; // 255 } #ifdef LIBRAGEPHOTO_C_API @@ -1026,7 +1085,7 @@ uint32_t ragephoto_format_rdr2() const char* ragephoto_getphotodesc(ragephoto_t instance) { RagePhoto *ragePhoto = static_cast(instance); - return ragePhoto->description().c_str(); + return ragePhoto->description(); } 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) { RagePhoto *ragePhoto = static_cast(instance); - return ragePhoto->json().c_str(); + return ragePhoto->json(); } const char* ragephoto_getphotoheader(ragephoto_t instance) { RagePhoto *ragePhoto = static_cast(instance); - return ragePhoto->header().c_str(); + return ragePhoto->header(); } 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) { RagePhoto *ragePhoto = static_cast(instance); - return ragePhoto->title().c_str(); + return ragePhoto->title(); } size_t ragephoto_getsavesize(ragephoto_t instance) diff --git a/src/RagePhoto.h b/src/RagePhoto.h index 48446bc..cb5cef2 100644 --- a/src/RagePhoto.h +++ b/src/RagePhoto.h @@ -1,6 +1,6 @@ /***************************************************************************** * 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, * are permitted provided that the following conditions are met: @@ -22,15 +22,11 @@ #ifdef __cplusplus #include "libragephoto_global.h" #include "RagePhotoData.h" -#include -#include #include #include #include #include -typedef std::function RagePhotoLoadFunc; - /** * \brief GTA V and RDR 2 Photo Parser. */ @@ -117,10 +113,10 @@ public: const std::string photo() 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. */ - const std::string& description() const; /**< Returns the Photo description. */ - const std::string& json() const; /**< Returns the Photo JSON data. */ - const std::string& header() const; /**< Returns the Photo header. */ - const std::string& title() const; /**< Returns the Photo title. */ + const char* description() const; /**< Returns the Photo description. */ + const char* json() const; /**< Returns the Photo JSON data. */ + const char* header() const; /**< Returns the Photo header. */ + const char* title() const; /**< Returns the Photo title. */ static const char* version(); /**< Returns the library version. */ /** Saves a Photo to a char*. * \param data Photo data @@ -150,11 +146,10 @@ public: static void setBufferDefault(RagePhotoData *ragePhotoData); /**< Sets all cross-format Buffer to default size. */ void setBufferOffsets(); /**< 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 setFormatLoadFunction(uint32_t photoFormat, RagePhotoLoadFunc func); /**< Sets a custom Photo Format load function. */ - void setJson(const std::string &json, uint32_t bufferSize = 0); /**< Sets the Photo JSON data. */ - void setHeader(const std::string &header, uint32_t headerSum); /**< Sets the Photo header. (EXPERT ONLY) */ + void setJson(const char *json, uint32_t bufferSize = 0); /**< Sets the Photo JSON data. */ + void setHeader(const char *header, uint32_t headerSum); /**< Sets the Photo header. (EXPERT ONLY) */ /** Sets the Photo JPEG data. * \param data JPEG data * \param size JPEG data size @@ -166,10 +161,9 @@ public: * \param bufferSize JPEG buffer size */ 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: - std::unordered_map m_loadFuncs; RagePhotoData m_data; }; #else diff --git a/src/RagePhotoA.h b/src/RagePhotoA.h index a1a1b3c..4a04e85 100644 --- a/src/RagePhotoA.h +++ b/src/RagePhotoA.h @@ -1,6 +1,6 @@ /***************************************************************************** * 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, * are permitted provided that the following conditions are met: @@ -33,6 +33,64 @@ class RagePhotoA { 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() { instance = ragephoto_open(); } diff --git a/src/RagePhotoC.h b/src/RagePhotoC.h index 40d58a4..53a8bee 100644 --- a/src/RagePhotoC.h +++ b/src/RagePhotoC.h @@ -1,6 +1,6 @@ /***************************************************************************** * 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, * are permitted provided that the following conditions are met: diff --git a/src/RagePhotoData.h b/src/RagePhotoData.h index 18efc27..2008a6f 100644 --- a/src/RagePhotoData.h +++ b/src/RagePhotoData.h @@ -1,6 +1,6 @@ /***************************************************************************** * 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, * are permitted provided that the following conditions are met: @@ -26,12 +26,11 @@ struct LIBRAGEPHOTO_CXX_EXPORT RagePhotoData { - bool photoLoaded; char* photoData; - std::string description; - std::string json; - std::string header; - std::string title; + char* description; + char* json; + char* header; + char* title; uint8_t error; uint32_t descBuffer; uint32_t descOffset;