libragephoto: add C++ namespaces (API change)

- RagePhoto::saveFile() -> use const char* instead of std::string
- RagePhoto C++ -> C API: try catch for throwable functions (to match C
impl. behaviour)
- Rename header files and classes
This commit is contained in:
Syping 2024-01-15 17:05:54 +01:00
parent b94e66624b
commit e1cf6df258
14 changed files with 275 additions and 249 deletions

View file

@ -36,10 +36,10 @@ option(RAGEPHOTO_C_LIBRARY "Build libragephoto as C library" OFF)
if (RAGEPHOTO_C_LIBRARY) if (RAGEPHOTO_C_LIBRARY)
set(RAGEPHOTO_HEADERS set(RAGEPHOTO_HEADERS
src/core/RagePhoto.h src/core/RagePhoto.h
src/core/RagePhotoA src/core/ragephoto_c
src/core/RagePhotoA.hpp src/core/ragephoto_c.hpp
src/core/RagePhotoB src/core/RagePhoto
src/core/RagePhotoB.hpp src/core/RagePhoto.hpp
src/core/RagePhotoLibrary.h src/core/RagePhotoLibrary.h
src/core/RagePhotoTypedefs.h src/core/RagePhotoTypedefs.h
) )
@ -48,10 +48,10 @@ if (RAGEPHOTO_C_LIBRARY)
) )
else() else()
set(RAGEPHOTO_HEADERS set(RAGEPHOTO_HEADERS
src/core/ragephoto_cxx
src/core/ragephoto_cxx.hpp
src/core/RagePhoto src/core/RagePhoto
src/core/RagePhoto.hpp src/core/RagePhoto.hpp
src/core/RagePhotoB
src/core/RagePhotoB.hpp
src/core/RagePhotoLibrary.h src/core/RagePhotoLibrary.h
src/core/RagePhotoTypedefs.h src/core/RagePhotoTypedefs.h
) )
@ -80,8 +80,8 @@ else()
set(LIBRAGEPHOTO_API LIBRAGEPHOTO_CXX_C) set(LIBRAGEPHOTO_API LIBRAGEPHOTO_CXX_C)
list(APPEND RAGEPHOTO_HEADERS list(APPEND RAGEPHOTO_HEADERS
src/core/RagePhoto.h src/core/RagePhoto.h
src/core/RagePhotoA src/core/ragephoto_c
src/core/RagePhotoA.hpp src/core/ragephoto_c.hpp
) )
else() else()
set(LIBRAGEPHOTO_API LIBRAGEPHOTO_CXX_ONLY) set(LIBRAGEPHOTO_API LIBRAGEPHOTO_CXX_ONLY)

View file

@ -12,8 +12,8 @@
\subpage Usage "Using libragephoto" \subpage Usage "Using libragephoto"
<b>Reference</b> <b>Reference</b>
RagePhoto (C++ API) ragephoto::cxx_abi::photo (C++ API)
RagePhotoA (C++ API based on C API) ragephoto::c_abi::photo (C++ API based on C API)
RagePhoto.h (C API) RagePhoto.h (C API)
RagePhotoData (Data Object Struct) RagePhotoData (Data Object Struct)
RagePhotoFormatParser (Custom Format Parser Struct) RagePhotoFormatParser (Custom Format Parser Struct)

View file

@ -4,7 +4,7 @@
<h4 id="cxx_include_usage">Including and using RagePhoto</h4> <h4 id="cxx_include_usage">Including and using RagePhoto</h4>
Include RagePhoto (C++ native), RagePhotoA (C API wrapper) or \link RagePhotoB.hpp RagePhotoB \endlink (best implementation) Include RagePhoto
\code{.cpp} \code{.cpp}
#include <RagePhoto> #include <RagePhoto>

View file

@ -16,7 +16,7 @@
* responsible for anything with use of the software, you are self responsible. * responsible for anything with use of the software, you are self responsible.
*****************************************************************************/ *****************************************************************************/
#include <RagePhotoB> #include <RagePhoto>
#include <gtkmm/application.h> #include <gtkmm/application.h>
#include <gtkmm/box.h> #include <gtkmm/box.h>
#include <gtkmm/button.h> #include <gtkmm/button.h>
@ -36,11 +36,11 @@ bool readPhotoFile(const std::string &filename, Gtk::Window *win, Gtk::Image *im
if (ifs.is_open()) { if (ifs.is_open()) {
std::string data(std::istreambuf_iterator<char>{ifs}, {}); std::string data(std::istreambuf_iterator<char>{ifs}, {});
ifs.close(); ifs.close();
RagePhotoB ragePhoto; RagePhoto ragePhoto;
const bool loaded = ragePhoto.load(data); const bool loaded = ragePhoto.load(data);
if (!loaded) { if (!loaded) {
const int32_t error = ragePhoto.error(); const int32_t error = ragePhoto.error();
if (error <= RagePhotoB::PhotoReadError) { if (error <= RagePhoto::PhotoReadError) {
Gtk::MessageDialog msg(*win, "Failed to read photo: " + filename, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true); Gtk::MessageDialog msg(*win, "Failed to read photo: " + filename, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
msg.set_title("Open Photo"); msg.set_title("Open Photo");
msg.run(); msg.run();

View file

@ -16,7 +16,7 @@
* responsible for anything with use of the software, you are self responsible. * responsible for anything with use of the software, you are self responsible.
*****************************************************************************/ *****************************************************************************/
#include <RagePhotoB> #include <RagePhoto>
#include <QApplication> #include <QApplication>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QVBoxLayout> #include <QVBoxLayout>
@ -35,11 +35,11 @@ bool readPhotoFile(const QString &filename, QMainWindow *mainWindow, QLabel *pho
if (file.open(QIODevice::ReadOnly)) { if (file.open(QIODevice::ReadOnly)) {
const QByteArray fileData = file.readAll(); const QByteArray fileData = file.readAll();
file.close(); file.close();
RagePhotoB ragePhoto; RagePhoto ragePhoto;
const bool loaded = ragePhoto.load(fileData.data(), static_cast<size_t>(fileData.size())); const bool loaded = ragePhoto.load(fileData.data(), static_cast<size_t>(fileData.size()));
if (!loaded) { if (!loaded) {
const int32_t error = ragePhoto.error(); const int32_t error = ragePhoto.error();
if (error <= RagePhotoB::PhotoReadError) { if (error <= RagePhoto::PhotoReadError) {
QMessageBox::warning(mainWindow, "Open Photo", "Failed to read photo: " + filename); QMessageBox::warning(mainWindow, "Open Photo", "Failed to read photo: " + filename);
return false; return false;
} }

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* libragephoto RAGE Photo Parser * libragephoto RAGE Photo Parser
* Copyright (C) 2021-2023 Syping * Copyright (C) 2021-2024 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:
@ -16,7 +16,7 @@
* responsible for anything with use of the software, you are self responsible. * responsible for anything with use of the software, you are self responsible.
*****************************************************************************/ *****************************************************************************/
#include "RagePhoto.hpp" #include "ragephoto_cxx.hpp"
#ifdef LIBRAGEPHOTO_CXX_C #ifdef LIBRAGEPHOTO_CXX_C
#include "RagePhoto.h" #include "RagePhoto.h"
#endif #endif
@ -45,6 +45,9 @@
#include <stringapiset.h> #include <stringapiset.h>
#endif #endif
/* CLASSIC RAGEPHOTO TYPEDEF */
typedef ragephoto::cxx_abi::photo RagePhoto;
/* BEGIN OF STATIC LIBRARY FUNCTIONS */ /* BEGIN OF STATIC LIBRARY FUNCTIONS */
inline size_t readBuffer(const char *input, void *output, size_t *pos, size_t outputLen, size_t inputLen) inline size_t readBuffer(const char *input, void *output, size_t *pos, size_t outputLen, size_t inputLen)
{ {
@ -154,7 +157,7 @@ inline uint32_t joaatFromInitial(const char *data, size_t size, uint32_t init_va
/* END OF STATIC LIBRARY FUNCTIONS */ /* END OF STATIC LIBRARY FUNCTIONS */
/* BEGIN OF RAGEPHOTO CLASS */ /* BEGIN OF RAGEPHOTO CLASS */
RagePhoto::RagePhoto() RagePhoto::photo()
{ {
m_data = static_cast<RagePhotoData*>(malloc(sizeof(RagePhotoData))); m_data = static_cast<RagePhotoData*>(malloc(sizeof(RagePhotoData)));
if (!m_data) if (!m_data)
@ -167,7 +170,7 @@ RagePhoto::RagePhoto()
setBufferDefault(m_data); setBufferDefault(m_data);
} }
RagePhoto::~RagePhoto() RagePhoto::~photo()
{ {
free(m_data->jpeg); free(m_data->jpeg);
free(m_data->description); free(m_data->description);
@ -987,7 +990,7 @@ const std::string RagePhoto::save(bool *ok)
return save(m_data->photoFormat, ok); return save(m_data->photoFormat, ok);
} }
bool RagePhoto::saveFile(const std::string &filename, uint32_t photoFormat) bool RagePhoto::saveFile(const char *filename, uint32_t photoFormat)
{ {
bool ok; bool ok;
const std::string &sdata = save(photoFormat, &ok); const std::string &sdata = save(photoFormat, &ok);
@ -1014,7 +1017,7 @@ bool RagePhoto::saveFile(const std::string &filename, uint32_t photoFormat)
return false; return false;
} }
bool RagePhoto::saveFile(const std::string &filename) bool RagePhoto::saveFile(const char *filename)
{ {
return saveFile(filename, m_data->photoFormat); return saveFile(filename, m_data->photoFormat);
} }
@ -1257,13 +1260,24 @@ void RagePhoto::setTitle(const char *title, uint32_t bufferSize)
#ifdef LIBRAGEPHOTO_CXX_C #ifdef LIBRAGEPHOTO_CXX_C
ragephoto_t ragephoto_open() ragephoto_t ragephoto_open()
{ {
return static_cast<ragephoto_t>(new RagePhoto); try {
return static_cast<ragephoto_t>(new RagePhoto);
}
catch (const std::exception &exception) {
std::cerr << "[libragephoto] Exception thrown at ragephoto_open: " << exception.what() << std::endl;
return nullptr;
}
} }
void ragephoto_addparser(ragephoto_t instance, RagePhotoFormatParser *rp_parser) void ragephoto_addparser(ragephoto_t instance, RagePhotoFormatParser *rp_parser)
{ {
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance); RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
ragePhoto->addParser(rp_parser); try {
ragePhoto->addParser(rp_parser);
}
catch (const std::exception &exception) {
std::cerr << "[libragephoto] Exception thrown at ragephoto_addparser: " << exception.what() << std::endl;
}
} }
void ragephoto_clear(ragephoto_t instance) void ragephoto_clear(ragephoto_t instance)

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* libragephoto RAGE Photo Parser * libragephoto RAGE Photo Parser
* Copyright (C) 2021-2023 Syping * Copyright (C) 2023-2024 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:
@ -21,171 +21,23 @@
#ifdef __cplusplus #ifdef __cplusplus
#include "RagePhotoLibrary.h" #include "RagePhotoLibrary.h"
#include "RagePhotoTypedefs.h" #ifdef LIBRAGEPHOTO_CXX_ONLY
#include <iostream> #include "ragephoto_cxx.hpp"
#include <cstdlib> typedef ragephoto::cxx_abi::photo RagePhoto;
#include <cstdint> #elif defined LIBRAGEPHOTO_CXX_C
#include <cstdio> #ifdef LIBRAGEPHOTO_STATIC
#include "ragephoto_cxx.hpp"
/** typedef ragephoto::cxx_abi::photo RagePhoto;
* \brief GTA V and RDR 2 Photo Parser. #else
*/ #include "ragephoto_c.hpp"
class LIBRAGEPHOTO_CXX_PUBLIC RagePhoto typedef ragephoto::c_abi::photo RagePhoto;
{ #endif // LIBRAGEPHOTO_STATIC
public: #elif defined LIBRAGEPHOTO_C_ONLY
/** Default sizes */ #include "ragephoto_c.hpp"
enum DefaultSize : uint32_t { typedef ragephoto::c_abi::photo RagePhoto;
DEFAULT_GTA5_PHOTOBUFFER = 524288UL, /**< GTA V default Photo Buffer Size */ #else
DEFAULT_RDR2_PHOTOBUFFER = 1048576UL, /**< RDR 2 default Photo Buffer Size */ #error "Could not determine best RagePhoto implementation, libragephoto installation might be corrupt!"
DEFAULT_DESCBUFFER = 256UL, /**< Default Description Buffer Size */ #endif // LIBRAGEPHOTO_CXX_ONLY
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 : int32_t {
DescBufferTight = 39L, /**< Description Buffer is too tight */
DescMallocError = 31L, /**< Description Buffer can't be allocated */
DescReadError = 32L, /**< Description can't be read successfully */
HeaderBufferTight = 35L, /**< Header Buffer is too tight */
HeaderMallocError = 4L, /**< Header Buffer can't be allocated */
IncompatibleFormat = 2L, /**< Format is incompatible */
IncompleteChecksum = 7L, /**< Header checksum is incomplete */
IncompleteDescBuffer = 30L, /**< Description Buffer Size is incomplete */
IncompleteDescMarker = 28L, /**< Description Marker is incomplete */
IncompleteDescOffset = 11L, /**< Description Offset is incomplete */
IncompleteEOF = 8L, /**< End Of File Offset is incomplete */
IncompleteHeader = 3L, /**< Header is incomplete */
IncompleteJendMarker = 33L, /**< JEND Marker is incomplete */
IncompleteJpegMarker = 12L, /**< JPEG Marker is incomplete */
IncompleteJsonBuffer = 20L, /**< JSON Buffer Size is incomplete */
IncompleteJsonMarker = 18L, /**< JSON Marker incomplete */
IncompleteJsonOffset = 9L, /**< JSON Offset incomplete */
IncompletePhotoBuffer = 14L, /**< Photo Buffer Size is incomplete */
IncompletePhotoSize = 15L, /**< Photo Size is incomplete */
IncompleteTitleBuffer = 25L, /**< Title Buffer Size is incomplete */
IncompleteTitleMarker = 23L, /**< Title Marker is incomplete */
IncompleteTitleOffset = 10L, /**< Title Offset is incomplete */
IncorrectDescMarker = 29L, /**< Description Marker is incorrect */
IncorrectJendMarker = 34L, /**< JEND Marker is incorrect */
IncorrectJpegMarker = 13L, /**< JPEG Marker is incorrect */
IncorrectJsonMarker = 19L, /**< JSON Marker is incorrect */
IncorrectTitleMarker = 24L, /**< Title Marker is incorrect */
JsonBufferTight = 37L, /**< JSON Buffer is too tight */
JsonMallocError = 21L, /**< JSON Buffer can't be allocated */
JsonReadError = 22L, /**< JSON can't be read successfully */
NoError = 255L, /**< Finished without errors */
NoFormatIdentifier = 1L, /**< No format detected, empty file */
PhotoBufferTight = 36L, /**< Photo Buffer is too tight */
PhotoMallocError = 16L, /**< Photo Buffer can't be allocated */
PhotoReadError = 17L, /**< Photo can't be read */
TitleBufferTight = 38L, /**< Title Buffer is too tight */
TitleMallocError = 26L, /**< Title Buffer can't be allocated */
TitleReadError = 27L, /**< Title can't be read */
UnicodeInitError = 5L, /**< Failed to initialise Unicode decoder */
UnicodeHeaderError = 6L, /**< Header can't be encoded/decoded successfully */
Uninitialised = 0L, /**< Uninitialised, file access failed */
};
/** Photo Formats */
enum PhotoFormat : uint32_t {
GTA5 = 0x01000000UL, /**< GTA V Photo Format */
RDR2 = 0x04000000UL, /**< RDR 2 Photo Format */
};
/** Sign Initials */
enum SignInitials : uint32_t {
SIGTA5 = 0xE47AB81CUL, /**< GTA V Sign Initial */
SIRDR2 = 0x00FEEB1EUL, /**< RDR 2 Sign Initial */
};
RagePhoto();
~RagePhoto();
void addParser(RagePhotoFormatParser *rp_parser); /**< Add a custom defined RagePhotoFormatParser. */
static void clear(RagePhotoData *rp_data); /**< Resets the RagePhotoData object to default values. */
void clear(); /**< Resets the RagePhotoData object to default values. */
RagePhotoData* data(); /**< Returns the internal RagePhotoData object. */
static bool load(const char *data, size_t size, RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser); /**< Loads a Photo from a const char*. */
/** Loads a Photo from a const char*.
* \param data Photo data
* \param size Photo data size
*/
bool load(const char *data, size_t size);
/** Loads a Photo from a std::string.
* \param data Photo data
*/
bool load(const std::string &data);
/** Loads a Photo from a file.
* \param filename File to load
*/
bool loadFile(const std::string &filename);
int32_t error() const; /**< Returns the last error occurred. */
uint32_t format() const; /**< Returns the Photo Format (GTA V or RDR 2). */
const std::string jpeg() const; /**< Returns the Photo JPEG data. */
#if (RAGEPHOTO_CXX_STD >= 17) && (__cplusplus >= 201703L)
const std::string_view jpeg_view() const; /**< Returns the Photo JPEG data. */
#endif
const char* jpegData() const; /**< Returns the Photo JPEG data. */
static uint64_t jpegSign(uint32_t photoFormat, RagePhotoData *rp_data); /**< Returns the Photo JPEG sign. */
static uint64_t jpegSign(RagePhotoData *rp_data); /**< Returns the Photo JPEG sign. */
uint64_t jpegSign(uint32_t photoFormat) const; /**< Returns the Photo JPEG sign. */
uint64_t jpegSign() const; /**< Returns the Photo JPEG sign. */
uint32_t jpegSize() const; /**< Returns the Photo JPEG data size. */
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. */
static bool save(char *data, uint32_t photoFormat, RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser); /**< Saves a Photo to a char*. */
static bool save(char *data, RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser); /**< Saves a Photo to a char*. */
/** Saves a Photo to a char*.
* \param data Photo data
* \param photoFormat Photo Format (GTA V or RDR 2)
*/
bool save(char *data, uint32_t photoFormat);
/** Saves a Photo to a char*.
* \param data Photo data
*/
bool save(char *data);
/** Saves a Photo to a std::string.
* \param photoFormat Photo Format (GTA V or RDR 2)
* \param ok \p true when saved successfully
*/
const std::string save(uint32_t photoFormat, bool *ok = nullptr);
/** Saves a Photo to a std::string.
* \param ok \p true when saved successfully
*/
const std::string save(bool *ok = nullptr);
bool saveFile(const std::string &filename, uint32_t photoFormat); /**< Saves a Photo to a file. */
bool saveFile(const std::string &filename); /**< Saves a Photo to a file. */
static size_t saveSize(uint32_t photoFormat, RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser); /**< Returns the Photo save file size. */
static size_t saveSize(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser); /**< Returns the Photo save file size. */
size_t saveSize(uint32_t photoFormat); /**< Returns the Photo save file size. */
size_t saveSize(); /**< Returns the Photo save file size. */
static void setBufferDefault(RagePhotoData *rp_data); /**< Sets all cross-format Buffer to default size. */
void setBufferDefault(); /**< Sets all cross-format Buffer to default size. */
static void setBufferOffsets(RagePhotoData *rp_data); /**< Moves all Buffer offsets to correct position. */
void setBufferOffsets(); /**< Moves all Buffer offsets to correct position. */
bool setData(RagePhotoData *rp_data, bool takeCopy = true); /**< Sets the internal RagePhotoData object. */
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). */
/** Sets the Photo JPEG data.
* \param data JPEG data
* \param size JPEG data size
* \param bufferSize JPEG buffer size
*/
bool setJpeg(const char *data, uint32_t size, uint32_t bufferSize = 0);
/** Sets the Photo JPEG data.
* \param data JPEG data
* \param bufferSize JPEG buffer size
*/
bool setJpeg(const std::string &data, uint32_t bufferSize = 0);
void setJson(const char *json, uint32_t bufferSize = 0); /**< Sets the Photo JSON data. */
void setHeader(const char *header, uint32_t headerSum, uint32_t headerSum2 = 0); /**< Sets the Photo header. */
void setTitle(const char *title, uint32_t bufferSize = 0); /**< Sets the Photo title. */
private:
RagePhotoData *m_data;
RagePhotoFormatParser *m_parser;
};
#endif // __cplusplus #endif // __cplusplus
#endif // RAGEPHOTO_HPP #endif // RAGEPHOTO_HPP

View file

@ -1 +0,0 @@
#include "RagePhotoB.hpp"

View file

@ -1,43 +0,0 @@
/*****************************************************************************
* libragephoto RAGE Photo Parser
* Copyright (C) 2023 Syping
*
* 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.
*****************************************************************************/
#ifndef RAGEPHOTOB_HPP
#define RAGEPHOTOB_HPP
#ifdef __cplusplus
#include "RagePhotoLibrary.h"
#ifdef LIBRAGEPHOTO_CXX_ONLY
#include "RagePhoto.hpp"
typedef RagePhoto RagePhotoB;
#elif defined LIBRAGEPHOTO_CXX_C
#ifdef LIBRAGEPHOTO_STATIC
#include "RagePhoto.hpp"
typedef RagePhoto RagePhotoB;
#else
#include "RagePhotoA.hpp"
typedef RagePhotoA RagePhotoB;
#endif // LIBRAGEPHOTO_STATIC
#elif defined LIBRAGEPHOTO_C_ONLY
#include "RagePhotoA.hpp"
typedef RagePhotoA RagePhotoB;
#else
#error "Could not determine best RagePhoto implementation, libragephoto installation might be corrupt!"
#endif // LIBRAGEPHOTO_CXX_ONLY
#endif // __cplusplus
#endif // RAGEPHOTOB_HPP

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* libragephoto RAGE Photo Parser * libragephoto RAGE Photo Parser
* Copyright (C) 2021-2023 Syping * Copyright (C) 2021-2024 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:
@ -16,18 +16,21 @@
* responsible for anything with use of the software, you are self responsible. * responsible for anything with use of the software, you are self responsible.
*****************************************************************************/ *****************************************************************************/
#ifndef RAGEPHOTOA_HPP #ifndef RAGEPHOTO_C_HPP
#define RAGEPHOTOA_HPP #define RAGEPHOTO_C_HPP
#ifdef __cplusplus #ifdef __cplusplus
#include "RagePhoto.h" #include "RagePhoto.h"
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
namespace ragephoto {
namespace c_abi {
/** /**
* \brief GTA V and RDR 2 Photo Parser (C API wrapper). * \brief GTA V and RDR 2 Photo Parser (C API wrapper).
*/ */
class RagePhotoA class photo
{ {
public: public:
/** Default sizes */ /** Default sizes */
@ -94,12 +97,12 @@ public:
SIGTA5 = 0xE47AB81CUL, /**< GTA V Sign Initial */ SIGTA5 = 0xE47AB81CUL, /**< GTA V Sign Initial */
SIRDR2 = 0x00FEEB1EUL, /**< RDR 2 Sign Initial */ SIRDR2 = 0x00FEEB1EUL, /**< RDR 2 Sign Initial */
}; };
RagePhotoA() { photo() {
instance = ragephoto_open(); instance = ragephoto_open();
if (!instance) if (!instance)
throw std::runtime_error("ragephoto_t instance can't be allocated"); throw std::runtime_error("ragephoto_t instance can't be allocated");
} }
~RagePhotoA() { ~photo() {
ragephoto_close(instance); ragephoto_close(instance);
} }
/** Add a custom defined RagePhotoFormatParser. */ /** Add a custom defined RagePhotoFormatParser. */
@ -342,6 +345,9 @@ public:
private: private:
ragephoto_t instance; ragephoto_t instance;
}; };
} // c_abi
} // ragephoto
#endif // __cplusplus #endif // __cplusplus
#endif // RAGEPHOTOA_HPP #endif // RAGEPHOTO_C_HPP

1
src/core/ragephoto_cxx Normal file
View file

@ -0,0 +1 @@
#include "ragephoto_cxx.hpp"

197
src/core/ragephoto_cxx.hpp Normal file
View file

@ -0,0 +1,197 @@
/*****************************************************************************
* libragephoto RAGE Photo Parser
* Copyright (C) 2021-2024 Syping
*
* 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.
*****************************************************************************/
#ifndef RAGEPHOTO_CXX_HPP
#define RAGEPHOTO_CXX_HPP
#ifdef __cplusplus
#include "RagePhotoLibrary.h"
#include "RagePhotoTypedefs.h"
#include <iostream>
#include <cstdlib>
#include <cstdint>
#include <cstdio>
namespace ragephoto {
namespace cxx_abi {
/**
* \brief GTA V and RDR 2 Photo Parser.
*/
class LIBRAGEPHOTO_CXX_PUBLIC photo
{
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 : int32_t {
DescBufferTight = 39L, /**< Description Buffer is too tight */
DescMallocError = 31L, /**< Description Buffer can't be allocated */
DescReadError = 32L, /**< Description can't be read successfully */
HeaderBufferTight = 35L, /**< Header Buffer is too tight */
HeaderMallocError = 4L, /**< Header Buffer can't be allocated */
IncompatibleFormat = 2L, /**< Format is incompatible */
IncompleteChecksum = 7L, /**< Header checksum is incomplete */
IncompleteDescBuffer = 30L, /**< Description Buffer Size is incomplete */
IncompleteDescMarker = 28L, /**< Description Marker is incomplete */
IncompleteDescOffset = 11L, /**< Description Offset is incomplete */
IncompleteEOF = 8L, /**< End Of File Offset is incomplete */
IncompleteHeader = 3L, /**< Header is incomplete */
IncompleteJendMarker = 33L, /**< JEND Marker is incomplete */
IncompleteJpegMarker = 12L, /**< JPEG Marker is incomplete */
IncompleteJsonBuffer = 20L, /**< JSON Buffer Size is incomplete */
IncompleteJsonMarker = 18L, /**< JSON Marker incomplete */
IncompleteJsonOffset = 9L, /**< JSON Offset incomplete */
IncompletePhotoBuffer = 14L, /**< Photo Buffer Size is incomplete */
IncompletePhotoSize = 15L, /**< Photo Size is incomplete */
IncompleteTitleBuffer = 25L, /**< Title Buffer Size is incomplete */
IncompleteTitleMarker = 23L, /**< Title Marker is incomplete */
IncompleteTitleOffset = 10L, /**< Title Offset is incomplete */
IncorrectDescMarker = 29L, /**< Description Marker is incorrect */
IncorrectJendMarker = 34L, /**< JEND Marker is incorrect */
IncorrectJpegMarker = 13L, /**< JPEG Marker is incorrect */
IncorrectJsonMarker = 19L, /**< JSON Marker is incorrect */
IncorrectTitleMarker = 24L, /**< Title Marker is incorrect */
JsonBufferTight = 37L, /**< JSON Buffer is too tight */
JsonMallocError = 21L, /**< JSON Buffer can't be allocated */
JsonReadError = 22L, /**< JSON can't be read successfully */
NoError = 255L, /**< Finished without errors */
NoFormatIdentifier = 1L, /**< No format detected, empty file */
PhotoBufferTight = 36L, /**< Photo Buffer is too tight */
PhotoMallocError = 16L, /**< Photo Buffer can't be allocated */
PhotoReadError = 17L, /**< Photo can't be read */
TitleBufferTight = 38L, /**< Title Buffer is too tight */
TitleMallocError = 26L, /**< Title Buffer can't be allocated */
TitleReadError = 27L, /**< Title can't be read */
UnicodeInitError = 5L, /**< Failed to initialise Unicode decoder */
UnicodeHeaderError = 6L, /**< Header can't be encoded/decoded successfully */
Uninitialised = 0L, /**< Uninitialised, file access failed */
};
/** Photo Formats */
enum PhotoFormat : uint32_t {
GTA5 = 0x01000000UL, /**< GTA V Photo Format */
RDR2 = 0x04000000UL, /**< RDR 2 Photo Format */
};
/** Sign Initials */
enum SignInitials : uint32_t {
SIGTA5 = 0xE47AB81CUL, /**< GTA V Sign Initial */
SIRDR2 = 0x00FEEB1EUL, /**< RDR 2 Sign Initial */
};
photo();
~photo();
void addParser(RagePhotoFormatParser *rp_parser); /**< Add a custom defined RagePhotoFormatParser. */
static void clear(RagePhotoData *rp_data); /**< Resets the RagePhotoData object to default values. */
void clear(); /**< Resets the RagePhotoData object to default values. */
RagePhotoData* data(); /**< Returns the internal RagePhotoData object. */
static bool load(const char *data, size_t size, RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser); /**< Loads a Photo from a const char*. */
/** Loads a Photo from a const char*.
* \param data Photo data
* \param size Photo data size
*/
bool load(const char *data, size_t size);
/** Loads a Photo from a std::string.
* \param data Photo data
*/
bool load(const std::string &data);
/** Loads a Photo from a file.
* \param filename File to load
*/
bool loadFile(const std::string &filename);
int32_t error() const; /**< Returns the last error occurred. */
uint32_t format() const; /**< Returns the Photo Format (GTA V or RDR 2). */
const std::string jpeg() const; /**< Returns the Photo JPEG data. */
#if (RAGEPHOTO_CXX_STD >= 17) && (__cplusplus >= 201703L)
const std::string_view jpeg_view() const; /**< Returns the Photo JPEG data. */
#endif
const char* jpegData() const; /**< Returns the Photo JPEG data. */
static uint64_t jpegSign(uint32_t photoFormat, RagePhotoData *rp_data); /**< Returns the Photo JPEG sign. */
static uint64_t jpegSign(RagePhotoData *rp_data); /**< Returns the Photo JPEG sign. */
uint64_t jpegSign(uint32_t photoFormat) const; /**< Returns the Photo JPEG sign. */
uint64_t jpegSign() const; /**< Returns the Photo JPEG sign. */
uint32_t jpegSize() const; /**< Returns the Photo JPEG data size. */
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. */
static bool save(char *data, uint32_t photoFormat, RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser); /**< Saves a Photo to a char*. */
static bool save(char *data, RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser); /**< Saves a Photo to a char*. */
/** Saves a Photo to a char*.
* \param data Photo data
* \param photoFormat Photo Format (GTA V or RDR 2)
*/
bool save(char *data, uint32_t photoFormat);
/** Saves a Photo to a char*.
* \param data Photo data
*/
bool save(char *data);
/** Saves a Photo to a std::string.
* \param photoFormat Photo Format (GTA V or RDR 2)
* \param ok \p true when saved successfully
*/
const std::string save(uint32_t photoFormat, bool *ok = nullptr);
/** Saves a Photo to a std::string.
* \param ok \p true when saved successfully
*/
const std::string save(bool *ok = nullptr);
bool saveFile(const char *filename, uint32_t photoFormat); /**< Saves a Photo to a file. */
bool saveFile(const char *filename); /**< Saves a Photo to a file. */
static size_t saveSize(uint32_t photoFormat, RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser); /**< Returns the Photo save file size. */
static size_t saveSize(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser); /**< Returns the Photo save file size. */
size_t saveSize(uint32_t photoFormat); /**< Returns the Photo save file size. */
size_t saveSize(); /**< Returns the Photo save file size. */
static void setBufferDefault(RagePhotoData *rp_data); /**< Sets all cross-format Buffer to default size. */
void setBufferDefault(); /**< Sets all cross-format Buffer to default size. */
static void setBufferOffsets(RagePhotoData *rp_data); /**< Moves all Buffer offsets to correct position. */
void setBufferOffsets(); /**< Moves all Buffer offsets to correct position. */
bool setData(RagePhotoData *rp_data, bool takeCopy = true); /**< Sets the internal RagePhotoData object. */
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). */
/** Sets the Photo JPEG data.
* \param data JPEG data
* \param size JPEG data size
* \param bufferSize JPEG buffer size
*/
bool setJpeg(const char *data, uint32_t size, uint32_t bufferSize = 0);
/** Sets the Photo JPEG data.
* \param data JPEG data
* \param bufferSize JPEG buffer size
*/
bool setJpeg(const std::string &data, uint32_t bufferSize = 0);
void setJson(const char *json, uint32_t bufferSize = 0); /**< Sets the Photo JSON data. */
void setHeader(const char *header, uint32_t headerSum, uint32_t headerSum2 = 0); /**< Sets the Photo header. */
void setTitle(const char *title, uint32_t bufferSize = 0); /**< Sets the Photo title. */
private:
RagePhotoData *m_data;
RagePhotoFormatParser *m_parser;
};
} // cxx_abi
} // ragephoto
#endif // __cplusplus
#endif // RAGEPHOTO_CXX_HPP

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* libragephoto RAGE Photo Parser * libragephoto RAGE Photo Parser
* Copyright (C) 2021-2023 Syping * Copyright (C) 2021-2024 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:
@ -16,7 +16,7 @@
* responsible for anything with use of the software, you are self responsible. * responsible for anything with use of the software, you are self responsible.
*****************************************************************************/ *****************************************************************************/
#include <RagePhotoB> #include <RagePhoto>
#include <fstream> #include <fstream>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -27,18 +27,18 @@ int main(int argc, char *argv[])
} }
// Initialise RagePhoto // Initialise RagePhoto
RagePhotoB ragePhoto; RagePhoto ragePhoto;
// Load Photo // Load Photo
const bool loaded = ragePhoto.loadFile(argv[1]); const bool loaded = ragePhoto.loadFile(argv[1]);
if (!loaded) { if (!loaded) {
const int32_t error = ragePhoto.error(); const int32_t error = ragePhoto.error();
if (error == RagePhotoB::Uninitialised) { if (error == RagePhoto::Uninitialised) {
std::cout << "Failed to open file: " << argv[1] << std::endl; std::cout << "Failed to open file: " << argv[1] << std::endl;
return 1; return 1;
} }
else if (error <= RagePhotoB::PhotoReadError) { else if (error <= RagePhoto::PhotoReadError) {
std::cout << "Failed to load photo" << std::endl; std::cout << "Failed to load photo" << std::endl;
return 1; return 1;
} }
@ -60,9 +60,9 @@ int main(int argc, char *argv[])
} }
const uint32_t photoFormat = ragePhoto.format(); const uint32_t photoFormat = ragePhoto.format();
if (photoFormat == RagePhotoB::GTA5) if (photoFormat == RagePhoto::GTA5)
std::cout << "GTA V Photo successfully exported" << std::endl; std::cout << "GTA V Photo successfully exported" << std::endl;
else if (photoFormat == RagePhotoB::RDR2) else if (photoFormat == RagePhoto::RDR2)
std::cout << "RDR 2 Photo successfully exported" << std::endl; std::cout << "RDR 2 Photo successfully exported" << std::endl;
else else
std::cout << "Photo successfully exported" << std::endl; std::cout << "Photo successfully exported" << std::endl;