2021-08-25 00:30:10 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* libragephoto RAGE Photo Parser
|
|
|
|
* Copyright (C) 2021 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_H
|
|
|
|
#define RAGEPHOTO_H
|
|
|
|
|
|
|
|
#include "libragephoto_global.h"
|
|
|
|
#include <iostream>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdio>
|
|
|
|
|
|
|
|
class LIBRAGEPHOTO_EXPORT RagePhoto
|
|
|
|
{
|
|
|
|
public:
|
2021-10-20 04:03:45 +02:00
|
|
|
/** 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 */
|
|
|
|
};
|
2021-09-14 19:35:46 +02:00
|
|
|
/** Parsing and set errors */
|
2021-08-26 00:22:11 +02:00
|
|
|
enum class Error : uint8_t {
|
2021-09-17 07:22:49 +02:00
|
|
|
DescBufferTight = 38, /**< Description Buffer is too tight */
|
2021-09-15 16:17:51 +02:00
|
|
|
DescMallocError = 30, /**< Description Buffer can't be allocated */
|
|
|
|
DescReadError = 31, /**< Description can't be read successfully */
|
2021-09-17 07:22:49 +02:00
|
|
|
HeaderBufferTight = 34, /**< Header Buffer is too tight */
|
2021-09-15 16:17:51 +02:00
|
|
|
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 */
|
2021-09-17 07:22:49 +02:00
|
|
|
JsonBufferTight = 36, /**< JSON Buffer is too tight */
|
2021-09-15 16:17:51 +02:00
|
|
|
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 */
|
2021-09-17 07:22:49 +02:00
|
|
|
PhotoBufferTight = 35, /**< Photo Buffer is too tight */
|
2021-09-15 16:17:51 +02:00
|
|
|
PhotoMallocError = 15, /**< Photo Buffer can't be allocated */
|
|
|
|
PhotoReadError = 16, /**< Photo can't be read */
|
2021-09-17 07:22:49 +02:00
|
|
|
TitleBufferTight = 37, /**< Title Buffer is too tight */
|
2021-09-15 16:17:51 +02:00
|
|
|
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 */
|
2021-08-26 00:22:11 +02:00
|
|
|
};
|
2021-09-14 19:35:46 +02:00
|
|
|
/** Photo Formats */
|
2021-08-25 00:30:10 +02:00
|
|
|
enum class PhotoFormat : uint32_t {
|
2021-09-14 19:35:46 +02:00
|
|
|
GTA5 = 0x01000000U, /**< GTA V Photo Format */
|
|
|
|
RDR2 = 0x04000000U, /**< RDR 2 Photo Format */
|
|
|
|
Undefined = 0, /**< Undefined Format */
|
2021-08-25 00:30:10 +02:00
|
|
|
};
|
|
|
|
RagePhoto();
|
|
|
|
~RagePhoto();
|
2021-09-14 19:35:46 +02:00
|
|
|
void clear(); /**< Resets the RagePhoto instance to default values. */
|
2021-09-16 05:02:08 +02:00
|
|
|
/** 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);
|
2021-09-14 19:35:46 +02:00
|
|
|
Error error(); /**< Returns the last error occurred. */
|
|
|
|
PhotoFormat format(); /**< Returns the Photo Format (GTA V or RDR 2). */
|
2021-09-16 05:02:08 +02:00
|
|
|
const std::string photo(); /**< Returns the Photo JPEG data. */
|
2021-09-14 19:35:46 +02:00
|
|
|
const char *photoData(); /**< Returns the Photo JPEG data. */
|
2021-09-16 03:07:11 +02:00
|
|
|
uint32_t photoSize(); /**< Returns the Photo JPEG data size. */
|
2021-09-14 19:35:46 +02:00
|
|
|
const std::string description(); /**< Returns the Photo description. */
|
|
|
|
const std::string json(); /**< Returns the Photo JSON data. */
|
|
|
|
const std::string header(); /**< Returns the Photo header. */
|
|
|
|
const std::string title(); /**< Returns the Photo title. */
|
2021-09-17 07:22:49 +02:00
|
|
|
/** Saves a Photo to a char*.
|
|
|
|
* \param data Photo data
|
|
|
|
* \param photoFormat Photo Format (GTA V or RDR 2)
|
|
|
|
*/
|
|
|
|
bool save(char *data, PhotoFormat 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(PhotoFormat 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);
|
|
|
|
size_t saveSize(PhotoFormat photoFormat); /**< Returns the save file size. */
|
|
|
|
size_t saveSize(); /**< Returns the save file size. */
|
2021-09-16 03:07:11 +02:00
|
|
|
void setBufferDefault(); /**< Sets all cross-format Buffer to default size. */
|
2021-09-14 19:35:46 +02:00
|
|
|
void setDescription(const std::string &description, uint32_t bufferSize = 0); /**< Sets the Photo description. */
|
2021-09-16 03:07:11 +02:00
|
|
|
void setFormat(PhotoFormat photoFormat); /**< Sets the Photo Format (GTA V or RDR 2). */
|
2021-09-14 19:35:46 +02:00
|
|
|
void setJson(const std::string &json, uint32_t bufferSize = 0); /**< Sets the Photo JSON data. */
|
2021-09-15 16:17:51 +02:00
|
|
|
void setHeader(const std::string &header, uint32_t headerSum); /**< Sets the Photo header. (expert only) */
|
2021-09-14 19:35:46 +02:00
|
|
|
/** Sets the Photo JPEG data.
|
2021-09-16 03:07:11 +02:00
|
|
|
* \param data JPEG data
|
|
|
|
* \param size JPEG data size
|
|
|
|
* \param bufferSize JPEG buffer size
|
2021-09-14 19:35:46 +02:00
|
|
|
*/
|
2021-09-16 05:02:08 +02:00
|
|
|
bool setPhoto(const char *data, uint32_t size, uint32_t bufferSize = 0);
|
2021-09-14 19:35:46 +02:00
|
|
|
/** Sets the Photo JPEG data.
|
2021-09-16 03:07:11 +02:00
|
|
|
* \param data JPEG data
|
|
|
|
* \param bufferSize JPEG buffer size
|
2021-09-14 19:35:46 +02:00
|
|
|
*/
|
2021-09-16 05:02:08 +02:00
|
|
|
bool setPhoto(const std::string &data, uint32_t bufferSize = 0);
|
2021-09-14 19:35:46 +02:00
|
|
|
void setTitle(const std::string &title, uint32_t bufferSize = 0); /**< Sets the Photo title. */
|
2021-08-25 00:30:10 +02:00
|
|
|
|
2021-08-29 02:35:25 +02:00
|
|
|
protected:
|
2021-09-14 19:35:46 +02:00
|
|
|
inline void moveOffsets();
|
2021-08-26 00:22:11 +02:00
|
|
|
inline size_t readBuffer(const char *input, char *output, size_t *pos, size_t len, size_t inputLen);
|
2021-09-17 07:22:49 +02:00
|
|
|
inline size_t writeBuffer(const char *input, char *output, size_t *pos, size_t len, size_t inputLen);
|
2021-08-25 00:30:10 +02:00
|
|
|
inline uint32_t charToUInt32LE(char *x);
|
|
|
|
inline void uInt32ToCharLE(uint32_t x, char *y);
|
2021-08-26 00:22:11 +02:00
|
|
|
bool p_photoLoaded;
|
2021-08-25 00:30:10 +02:00
|
|
|
char* p_photoData;
|
2021-08-26 00:22:11 +02:00
|
|
|
Error p_error;
|
2021-09-03 18:36:00 +02:00
|
|
|
PhotoFormat p_photoFormat;
|
2021-08-25 00:30:10 +02:00
|
|
|
std::string p_descriptionString;
|
|
|
|
std::string p_jsonString;
|
|
|
|
std::string p_photoString;
|
|
|
|
std::string p_titleString;
|
|
|
|
uint32_t p_descBuffer;
|
|
|
|
uint32_t p_descOffset;
|
|
|
|
uint32_t p_endOfFile;
|
|
|
|
uint32_t p_headerSum;
|
|
|
|
uint32_t p_jsonBuffer;
|
|
|
|
uint32_t p_jsonOffset;
|
|
|
|
uint32_t p_photoBuffer;
|
|
|
|
uint32_t p_photoSize;
|
|
|
|
uint32_t p_titlBuffer;
|
|
|
|
uint32_t p_titlOffset;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // RAGEPHOTO_H
|