struct, enum and few other changes

RagePhoto: updated comments to the new error codes, update code for the
new RagePhotoData names
RagePhotoData: change names of photoData to jpeg, photoSize to jpegSize
RagePhotoA: copied documentation from RagePhoto
RagePhoto::Error: add error type HeaderMallocError
This commit is contained in:
Syping 2023-01-08 13:35:32 +01:00
parent f99456cb63
commit d7e31ecada
4 changed files with 218 additions and 161 deletions

View file

@ -125,7 +125,7 @@ inline void uInt32ToCharLE(uint32_t x, char *y)
/* BEGIN OF RAGEPHOTO CLASS */ /* BEGIN OF RAGEPHOTO CLASS */
RagePhoto::RagePhoto() RagePhoto::RagePhoto()
{ {
m_data.photoData = nullptr; m_data.jpeg = nullptr;
m_data.description = nullptr; m_data.description = nullptr;
m_data.json = nullptr; m_data.json = nullptr;
m_data.header = nullptr; m_data.header = nullptr;
@ -135,7 +135,7 @@ RagePhoto::RagePhoto()
RagePhoto::~RagePhoto() RagePhoto::~RagePhoto()
{ {
std::free(m_data.photoData); std::free(m_data.jpeg);
std::free(m_data.description); std::free(m_data.description);
std::free(m_data.json); std::free(m_data.json);
std::free(m_data.header); std::free(m_data.header);
@ -144,8 +144,8 @@ RagePhoto::~RagePhoto()
void RagePhoto::clear() void RagePhoto::clear()
{ {
std::free(m_data.photoData); std::free(m_data.jpeg);
m_data.photoData = nullptr; m_data.jpeg = nullptr;
std::free(m_data.description); std::free(m_data.description);
m_data.description = nullptr; m_data.description = nullptr;
std::free(m_data.json); std::free(m_data.json);
@ -201,13 +201,13 @@ bool RagePhoto::load(const char *data, size_t length)
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert; std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert;
const std::string photoHeader_string = 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; // 6
return false; return false;
} }
const size_t photoHeader_size = photoHeader_string.size() + 1; const size_t photoHeader_size = photoHeader_string.size() + 1;
m_data.header = static_cast<char*>(std::malloc(photoHeader_size)); m_data.header = static_cast<char*>(std::malloc(photoHeader_size));
if (!m_data.header) { if (!m_data.header) {
// add MALLOC ERROR INDICATOR m_data.error = Error::HeaderMallocError; // 4
return false; return false;
} }
std::memcpy(m_data.header, photoHeader_string.c_str(), photoHeader_size); std::memcpy(m_data.header, photoHeader_string.c_str(), photoHeader_size);
@ -219,7 +219,7 @@ bool RagePhoto::load(const char *data, size_t length)
} }
m_data.header = static_cast<char*>(std::malloc(256)); m_data.header = static_cast<char*>(std::malloc(256));
if (!m_data.header) { if (!m_data.header) {
// add MALLOC ERROR INDICATOR m_data.error = Error::HeaderMallocError; // 4
iconv_close(iconv_in); iconv_close(iconv_in);
return false; return false;
} }
@ -230,27 +230,27 @@ bool RagePhoto::load(const char *data, size_t length)
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; // 6
return false; return false;
} }
#elif defined UNICODE_WINCVT #elif defined UNICODE_WINCVT
m_data.header = static_cast<char*>(std::malloc(256)); m_data.header = static_cast<char*>(std::malloc(256));
if (!m_data.header) { if (!m_data.header) {
// add MALLOC ERROR INDICATOR m_data.error = Error::HeaderMallocError; // 4
return false; return false;
} }
const int converted = WideCharToMultiByte(CP_UTF8, 0, reinterpret_cast<wchar_t*>(photoHeader), -1, m_data.header, 256, NULL, NULL); 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); std::free(m_data.header);
m_data.header = nullptr; m_data.header = nullptr;
m_data.error = Error::UnicodeHeaderError; // 5 m_data.error = Error::UnicodeHeaderError; // 6
return false; return false;
} }
#endif #endif
size = readBuffer(data, uInt32Buffer, &pos, 4, length); size = readBuffer(data, uInt32Buffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
m_data.error = Error::IncompleteChecksum; // 6 m_data.error = Error::IncompleteChecksum; // 7
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -262,7 +262,7 @@ bool RagePhoto::load(const char *data, size_t length)
if (m_data.photoFormat == PhotoFormat::RDR2) { if (m_data.photoFormat == PhotoFormat::RDR2) {
size = readBuffer(data, uInt32Buffer, &pos, 4, length); size = readBuffer(data, uInt32Buffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
m_data.error = Error::IncompleteChecksum; // 6 m_data.error = Error::IncompleteChecksum; // 7
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -273,7 +273,7 @@ bool RagePhoto::load(const char *data, size_t length)
size = readBuffer(data, uInt32Buffer, &pos, 4, length); size = readBuffer(data, uInt32Buffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
m_data.error = Error::IncompleteChecksum; // 6 m_data.error = Error::IncompleteChecksum; // 7
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -286,7 +286,7 @@ bool RagePhoto::load(const char *data, size_t length)
size = readBuffer(data, uInt32Buffer, &pos, 4, length); size = readBuffer(data, uInt32Buffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
m_data.error = Error::IncompleteEOF; // 7 m_data.error = Error::IncompleteEOF; // 8
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -297,7 +297,7 @@ bool RagePhoto::load(const char *data, size_t length)
size = readBuffer(data, uInt32Buffer, &pos, 4, length); size = readBuffer(data, uInt32Buffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
m_data.error = Error::IncompleteJsonOffset; // 8 m_data.error = Error::IncompleteJsonOffset; // 9
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -307,7 +307,7 @@ bool RagePhoto::load(const char *data, size_t length)
#endif #endif
size = readBuffer(data, uInt32Buffer, &pos, 4, length); size = readBuffer(data, uInt32Buffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
m_data.error = Error::IncompleteTitleOffset; // 9 m_data.error = Error::IncompleteTitleOffset; // 10
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -318,7 +318,7 @@ bool RagePhoto::load(const char *data, size_t length)
size = readBuffer(data, uInt32Buffer, &pos, 4, length); size = readBuffer(data, uInt32Buffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
m_data.error = Error::IncompleteDescOffset; // 10 m_data.error = Error::IncompleteDescOffset; // 11
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -330,17 +330,17 @@ bool RagePhoto::load(const char *data, size_t length)
char markerBuffer[4]; char markerBuffer[4];
size = readBuffer(data, markerBuffer, &pos, 4, length); size = readBuffer(data, markerBuffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
m_data.error = Error::IncompleteJpegMarker; // 11 m_data.error = Error::IncompleteJpegMarker; // 12
return false; return false;
} }
if (strncmp(markerBuffer, "JPEG", 4) != 0) { if (strncmp(markerBuffer, "JPEG", 4) != 0) {
m_data.error = Error::IncorrectJpegMarker; // 12 m_data.error = Error::IncorrectJpegMarker; // 13
return false; return false;
} }
size = readBuffer(data, uInt32Buffer, &pos, 4, length); size = readBuffer(data, uInt32Buffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
m_data.error = Error::IncompletePhotoBuffer; // 13 m_data.error = Error::IncompletePhotoBuffer; // 14
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -351,42 +351,42 @@ bool RagePhoto::load(const char *data, size_t length)
size = readBuffer(data, uInt32Buffer, &pos, 4, length); size = readBuffer(data, uInt32Buffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
m_data.error = Error::IncompletePhotoSize; // 14 m_data.error = Error::IncompletePhotoSize; // 15
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
std::memcpy(&m_data.photoSize, uInt32Buffer, 4); std::memcpy(&m_data.jpegSize, uInt32Buffer, 4);
#else #else
m_data.photoSize = charToUInt32LE(uInt32Buffer); m_data.jpegSize = charToUInt32LE(uInt32Buffer);
#endif #endif
m_data.photoData = static_cast<char*>(std::malloc(m_data.photoSize)); m_data.jpeg = static_cast<char*>(std::malloc(m_data.jpegSize));
if (!m_data.photoData) { if (!m_data.jpeg) {
m_data.error = Error::PhotoMallocError; // 15 m_data.error = Error::PhotoMallocError; // 16
return false; return false;
} }
size = readBuffer(data, m_data.photoData, &pos, m_data.photoSize, length); size = readBuffer(data, m_data.jpeg, &pos, m_data.jpegSize, length);
if (size != m_data.photoSize) { if (size != m_data.jpegSize) {
std::free(m_data.photoData); std::free(m_data.jpeg);
m_data.photoData = nullptr; m_data.jpeg = nullptr;
m_data.error = Error::PhotoReadError; // 16 m_data.error = Error::PhotoReadError; // 17
return false; return false;
} }
pos = m_data.jsonOffset + headerSize; pos = m_data.jsonOffset + headerSize;
size = readBuffer(data, markerBuffer, &pos, 4, length); size = readBuffer(data, markerBuffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
m_data.error = Error::IncompleteJsonMarker; // 17 m_data.error = Error::IncompleteJsonMarker; // 18
return false; return false;
} }
if (strncmp(markerBuffer, "JSON", 4) != 0) { if (strncmp(markerBuffer, "JSON", 4) != 0) {
m_data.error = Error::IncorrectJsonMarker; // 18 m_data.error = Error::IncorrectJsonMarker; // 19
return false; return false;
} }
size = readBuffer(data, uInt32Buffer, &pos, 4, length); size = readBuffer(data, uInt32Buffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
m_data.error = Error::IncompleteJsonBuffer; // 19 m_data.error = Error::IncompleteJsonBuffer; // 20
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -397,31 +397,31 @@ bool RagePhoto::load(const char *data, size_t length)
m_data.json = static_cast<char*>(std::malloc(m_data.jsonBuffer)); m_data.json = static_cast<char*>(std::malloc(m_data.jsonBuffer));
if (!m_data.json) { if (!m_data.json) {
m_data.error = Error::JsonMallocError; // 20 m_data.error = Error::JsonMallocError; // 21
return false; return false;
} }
size = readBuffer(data, m_data.json, &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(m_data.json); std::free(m_data.json);
m_data.json = nullptr; m_data.json = nullptr;
m_data.error = Error::JsonReadError; // 21 m_data.error = Error::JsonReadError; // 22
return false; return false;
} }
pos = m_data.titlOffset + headerSize; pos = m_data.titlOffset + headerSize;
size = readBuffer(data, markerBuffer, &pos, 4, length); size = readBuffer(data, markerBuffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
m_data.error = Error::IncompleteTitleMarker; // 22 m_data.error = Error::IncompleteTitleMarker; // 23
return false; return false;
} }
if (strncmp(markerBuffer, "TITL", 4) != 0) { if (strncmp(markerBuffer, "TITL", 4) != 0) {
m_data.error = Error::IncorrectTitleMarker; // 23 m_data.error = Error::IncorrectTitleMarker; // 24
return false; return false;
} }
size = readBuffer(data, uInt32Buffer, &pos, 4, length); size = readBuffer(data, uInt32Buffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
m_data.error = Error::IncompleteTitleBuffer; // 24 m_data.error = Error::IncompleteTitleBuffer; // 25
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -432,31 +432,31 @@ bool RagePhoto::load(const char *data, size_t length)
m_data.title = static_cast<char*>(std::malloc(m_data.titlBuffer)); m_data.title = static_cast<char*>(std::malloc(m_data.titlBuffer));
if (!m_data.title) { if (!m_data.title) {
m_data.error = Error::TitleMallocError; // 25 m_data.error = Error::TitleMallocError; // 26
return false; return false;
} }
size = readBuffer(data, m_data.title, &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(m_data.title); std::free(m_data.title);
m_data.title = nullptr; m_data.title = nullptr;
m_data.error = Error::TitleReadError; // 26 m_data.error = Error::TitleReadError; // 27
return false; return false;
} }
pos = m_data.descOffset + headerSize; pos = m_data.descOffset + headerSize;
size = readBuffer(data, markerBuffer, &pos, 4, length); size = readBuffer(data, markerBuffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
m_data.error = Error::IncompleteDescMarker; // 27 m_data.error = Error::IncompleteDescMarker; // 28
return false; return false;
} }
if (strncmp(markerBuffer, "DESC", 4) != 0) { if (strncmp(markerBuffer, "DESC", 4) != 0) {
m_data.error = Error::IncorrectDescMarker; // 28 m_data.error = Error::IncorrectDescMarker; // 29
return false; return false;
} }
size = readBuffer(data, uInt32Buffer, &pos, 4, length); size = readBuffer(data, uInt32Buffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
m_data.error = Error::IncompleteDescBuffer; // 29 m_data.error = Error::IncompleteDescBuffer; // 30
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -467,25 +467,25 @@ bool RagePhoto::load(const char *data, size_t length)
m_data.description = static_cast<char*>(std::malloc(m_data.descBuffer)); m_data.description = static_cast<char*>(std::malloc(m_data.descBuffer));
if (!m_data.description) { if (!m_data.description) {
m_data.error = Error::DescMallocError; // 30 m_data.error = Error::DescMallocError; // 31
return false; return false;
} }
size = readBuffer(data, m_data.description, &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(m_data.description); std::free(m_data.description);
m_data.description = nullptr; m_data.description = nullptr;
m_data.error = Error::DescReadError; // 31 m_data.error = Error::DescReadError; // 32
return false; return false;
} }
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);
if (size != 4) { if (size != 4) {
m_data.error = Error::IncompleteJendMarker; // 32 m_data.error = Error::IncompleteJendMarker; // 33
return false; return false;
} }
if (strncmp(markerBuffer, "JEND", 4) != 0) { if (strncmp(markerBuffer, "JEND", 4) != 0) {
m_data.error = Error::IncorrectJendMarker; // 33 m_data.error = Error::IncorrectJendMarker; // 34
return false; return false;
} }
@ -564,21 +564,21 @@ uint32_t RagePhoto::format() const
const std::string RagePhoto::photo() const const std::string RagePhoto::photo() const
{ {
if (m_data.photoData) if (m_data.jpeg)
return std::string(m_data.photoData, m_data.photoSize); return std::string(m_data.jpeg, m_data.jpegSize);
else else
return std::string(); return std::string();
} }
const char* RagePhoto::photoData() const const char* RagePhoto::photoData() const
{ {
return m_data.photoData; return m_data.jpeg;
} }
uint32_t RagePhoto::photoSize() const uint32_t RagePhoto::photoSize() const
{ {
if (m_data.photoData) if (m_data.jpeg)
return m_data.photoSize; return m_data.jpegSize;
else else
return 0UL; return 0UL;
} }
@ -616,12 +616,12 @@ bool RagePhoto::save(char *data, uint32_t photoFormat)
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert; std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert;
std::u16string photoHeader_string = convert.from_bytes(m_data.header); std::u16string photoHeader_string = convert.from_bytes(m_data.header);
if (convert.converted() == 0) { if (convert.converted() == 0) {
m_data.error = Error::UnicodeHeaderError; // 5 m_data.error = Error::UnicodeHeaderError; // 6
return false; return false;
} }
const size_t photoHeader_size = photoHeader_string.size() * 2; const size_t photoHeader_size = photoHeader_string.size() * 2;
if (photoHeader_size > 256) { if (photoHeader_size > 256) {
m_data.error = Error::HeaderBufferTight; // 34 m_data.error = Error::HeaderBufferTight; // 35
return false; return false;
} }
const char *photoHeader = reinterpret_cast<const char*>(photoHeader_string.data()); const char *photoHeader = reinterpret_cast<const char*>(photoHeader_string.data());
@ -639,7 +639,7 @@ bool RagePhoto::save(char *data, uint32_t photoFormat)
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; // 6
return false; return false;
} }
const size_t photoHeader_size = 256; const size_t photoHeader_size = 256;
@ -647,32 +647,32 @@ bool RagePhoto::save(char *data, uint32_t photoFormat)
char photoHeader[256]{}; char photoHeader[256]{};
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)); 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; // 6
return false; return false;
} }
const size_t photoHeader_size = 256; const size_t photoHeader_size = 256;
#endif #endif
if (m_data.photoSize > m_data.photoBuffer) { if (m_data.jpegSize > m_data.photoBuffer) {
m_data.error = Error::PhotoBufferTight; // 35 m_data.error = Error::PhotoBufferTight; // 36
return false; return false;
} }
const size_t jsonString_size = strlen(m_data.json) + 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; // 37
return false; return false;
} }
const size_t titlString_size = strlen(m_data.title) + 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; // 38
return false; return false;
} }
const size_t descString_size = strlen(m_data.description) + 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; // 39
return false; return false;
} }
@ -754,14 +754,14 @@ bool RagePhoto::save(char *data, uint32_t photoFormat)
writeBuffer(uInt32Buffer, data, &pos, length, 4); writeBuffer(uInt32Buffer, data, &pos, length, 4);
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
std::memcpy(uInt32Buffer, &m_data.photoSize, 4); std::memcpy(uInt32Buffer, &m_data.jpegSize, 4);
#else #else
uInt32ToCharLE(m_data.photoSize, uInt32Buffer); uInt32ToCharLE(m_data.jpegSize, uInt32Buffer);
#endif #endif
writeBuffer(uInt32Buffer, data, &pos, length, 4); writeBuffer(uInt32Buffer, data, &pos, length, 4);
writeBuffer(m_data.photoData, data, &pos, length, m_data.photoSize); writeBuffer(m_data.jpeg, data, &pos, length, m_data.jpegSize);
for (size_t i = m_data.photoSize; i < m_data.photoBuffer; i++) { for (size_t i = m_data.jpegSize; i < m_data.photoBuffer; i++) {
writeBuffer("\0", data, &pos, length, 1); writeBuffer("\0", data, &pos, length, 1);
} }
@ -930,7 +930,7 @@ inline void RagePhoto::setBufferOffsets(RagePhotoData *ragePhotoData)
void RagePhoto::setDescription(const char *description, uint32_t bufferSize) void RagePhoto::setDescription(const char *description, uint32_t bufferSize)
{ {
if (!writeDataChar(description, &m_data.description)) { if (!writeDataChar(description, &m_data.description)) {
m_data.error = Error::DescMallocError; // 30 m_data.error = Error::DescMallocError; // 31
return; return;
} }
if (bufferSize != 0) { if (bufferSize != 0) {
@ -948,7 +948,7 @@ void RagePhoto::setFormat(uint32_t photoFormat)
void RagePhoto::setJson(const char *json, uint32_t bufferSize) void RagePhoto::setJson(const char *json, uint32_t bufferSize)
{ {
if (!writeDataChar(json, &m_data.json)) { if (!writeDataChar(json, &m_data.json)) {
m_data.error = Error::JsonMallocError; // 20 m_data.error = Error::JsonMallocError; // 21
return; return;
} }
if (bufferSize != 0) { if (bufferSize != 0) {
@ -970,39 +970,39 @@ void RagePhoto::setHeader(const char *header, uint32_t headerSum)
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.photoData) { if (m_data.jpeg) {
if (m_data.photoSize > size) { if (m_data.jpegSize > size) {
char *t_photoData = static_cast<char*>(std::realloc(m_data.photoData, size)); char *t_photoData = static_cast<char*>(std::realloc(m_data.jpeg, size));
if (!t_photoData) { if (!t_photoData) {
m_data.error = Error::PhotoMallocError; // 15 m_data.error = Error::PhotoMallocError; // 16
return false; return false;
} }
m_data.photoData = t_photoData; m_data.jpeg = t_photoData;
std::memcpy(m_data.photoData, data, size); std::memcpy(m_data.jpeg, data, size);
m_data.photoSize = size; m_data.jpegSize = size;
} }
else if (m_data.photoSize < size) { else if (m_data.jpegSize < size) {
std::free(m_data.photoData); std::free(m_data.jpeg);
m_data.photoData = static_cast<char*>(std::malloc(size)); m_data.jpeg = static_cast<char*>(std::malloc(size));
if (!m_data.photoData) { if (!m_data.jpeg) {
m_data.error = Error::PhotoMallocError; // 15 m_data.error = Error::PhotoMallocError; // 16
return false; return false;
} }
std::memcpy(m_data.photoData, data, size); std::memcpy(m_data.jpeg, data, size);
m_data.photoSize = size; m_data.jpegSize = size;
} }
else { else {
std::memcpy(m_data.photoData, data, size); std::memcpy(m_data.jpeg, data, size);
} }
} }
else { else {
m_data.photoData = static_cast<char*>(std::malloc(size)); m_data.jpeg = static_cast<char*>(std::malloc(size));
if (!m_data.photoData) { if (!m_data.jpeg) {
m_data.error = Error::PhotoMallocError; // 15 m_data.error = Error::PhotoMallocError; // 16
return false; return false;
} }
std::memcpy(m_data.photoData, data, size); std::memcpy(m_data.jpeg, data, size);
m_data.photoSize = size; m_data.jpegSize = size;
} }
if (bufferSize != 0) { if (bufferSize != 0) {
@ -1022,7 +1022,7 @@ bool RagePhoto::setPhoto(const std::string &data, uint32_t bufferSize)
void RagePhoto::setTitle(const char *title, uint32_t bufferSize) void RagePhoto::setTitle(const char *title, uint32_t bufferSize)
{ {
if (!writeDataChar(title, &m_data.title)) { if (!writeDataChar(title, &m_data.title)) {
m_data.error = Error::TitleMallocError; // 25 m_data.error = Error::TitleMallocError; // 26
return; return;
} }
if (bufferSize != 0) { if (bufferSize != 0) {

View file

@ -45,45 +45,46 @@ public:
}; };
/** Parsing and set errors */ /** Parsing and set errors */
enum Error : uint8_t { enum Error : uint8_t {
DescBufferTight = 38, /**< Description Buffer is too tight */ DescBufferTight = 39, /**< Description Buffer is too tight */
DescMallocError = 30, /**< Description Buffer can't be allocated */ DescMallocError = 31, /**< Description Buffer can't be allocated */
DescReadError = 31, /**< Description can't be read successfully */ DescReadError = 32, /**< Description can't be read successfully */
HeaderBufferTight = 34, /**< Header Buffer is too tight */ HeaderBufferTight = 35, /**< Header Buffer is too tight */
HeaderMallocError = 4, /**< Header Buffer can't be allocated */
IncompatibleFormat = 2, /**< Format is incompatible */ IncompatibleFormat = 2, /**< Format is incompatible */
IncompleteChecksum = 6, /**< Header checksum is incomplete */ IncompleteChecksum = 7, /**< Header checksum is incomplete */
IncompleteDescBuffer = 29, /**< Description Buffer Size is incomplete */ IncompleteDescBuffer = 30, /**< Description Buffer Size is incomplete */
IncompleteDescMarker = 27, /**< Description Marker is incomplete */ IncompleteDescMarker = 28, /**< Description Marker is incomplete */
IncompleteDescOffset = 10, /**< Description Offset is incomplete */ IncompleteDescOffset = 11, /**< Description Offset is incomplete */
IncompleteEOF = 7, /**< End Of File Offset is incomplete */ IncompleteEOF = 8, /**< End Of File Offset is incomplete */
IncompleteHeader = 3, /**< Header is incomplete */ IncompleteHeader = 3, /**< Header is incomplete */
IncompleteJendMarker = 32, /**< JEND Marker is incomplete */ IncompleteJendMarker = 33, /**< JEND Marker is incomplete */
IncompleteJpegMarker = 11, /**< JPEG Marker is incomplete */ IncompleteJpegMarker = 12, /**< JPEG Marker is incomplete */
IncompleteJsonBuffer = 19, /**< JSON Buffer Size is incomplete */ IncompleteJsonBuffer = 20, /**< JSON Buffer Size is incomplete */
IncompleteJsonMarker = 17, /**< JSON Marker incomplete */ IncompleteJsonMarker = 18, /**< JSON Marker incomplete */
IncompleteJsonOffset = 8, /**< JSON Offset incomplete */ IncompleteJsonOffset = 9, /**< JSON Offset incomplete */
IncompletePhotoBuffer = 13, /**< Photo Buffer Size is incomplete */ IncompletePhotoBuffer = 14, /**< Photo Buffer Size is incomplete */
IncompletePhotoSize = 14, /**< Photo Size is incomplete */ IncompletePhotoSize = 15, /**< Photo Size is incomplete */
IncompleteTitleBuffer = 24, /**< Title Buffer Size is incomplete */ IncompleteTitleBuffer = 25, /**< Title Buffer Size is incomplete */
IncompleteTitleMarker = 22, /**< Title Marker is incomplete */ IncompleteTitleMarker = 23, /**< Title Marker is incomplete */
IncompleteTitleOffset = 9, /**< Title Offset is incomplete */ IncompleteTitleOffset = 10, /**< Title Offset is incomplete */
IncorrectDescMarker = 28, /**< Description Marker is incorrect */ IncorrectDescMarker = 29, /**< Description Marker is incorrect */
IncorrectJendMarker = 33, /**< JEND Marker is incorrect */ IncorrectJendMarker = 34, /**< JEND Marker is incorrect */
IncorrectJpegMarker = 12, /**< JPEG Marker is incorrect */ IncorrectJpegMarker = 13, /**< JPEG Marker is incorrect */
IncorrectJsonMarker = 18, /**< JSON Marker is incorrect */ IncorrectJsonMarker = 19, /**< JSON Marker is incorrect */
IncorrectTitleMarker = 23, /**< Title Marker is incorrect */ IncorrectTitleMarker = 24, /**< Title Marker is incorrect */
JsonBufferTight = 36, /**< JSON Buffer is too tight */ JsonBufferTight = 37, /**< JSON Buffer is too tight */
JsonMallocError = 20, /**< JSON Buffer can't be allocated */ JsonMallocError = 21, /**< JSON Buffer can't be allocated */
JsonReadError = 21, /**< JSON can't be read successfully */ JsonReadError = 22, /**< JSON can't be read successfully */
NoError = 255, /**< Finished without errors */ NoError = 255, /**< Finished without errors */
NoFormatIdentifier = 1, /**< No format detected, empty file */ NoFormatIdentifier = 1, /**< No format detected, empty file */
PhotoBufferTight = 35, /**< Photo Buffer is too tight */ PhotoBufferTight = 36, /**< Photo Buffer is too tight */
PhotoMallocError = 15, /**< Photo Buffer can't be allocated */ PhotoMallocError = 16, /**< Photo Buffer can't be allocated */
PhotoReadError = 16, /**< Photo can't be read */ PhotoReadError = 17, /**< Photo can't be read */
TitleBufferTight = 37, /**< Title Buffer is too tight */ TitleBufferTight = 38, /**< Title Buffer is too tight */
TitleMallocError = 25, /**< Title Buffer can't be allocated */ TitleMallocError = 26, /**< Title Buffer can't be allocated */
TitleReadError = 26, /**< Title can't be read */ TitleReadError = 27, /**< Title can't be read */
UnicodeHeaderError = 5, /**< Header can't be decoded */ UnicodeInitError = 5, /**< Failed to initialise Unicode decoder */
UnicodeInitError = 4, /**< Failed to initialise Unicode decoder */ UnicodeHeaderError = 6, /**< Header can't be encoded/decoded successfully */
Uninitialised = 0, /**< Uninitialised, file access failed */ Uninitialised = 0, /**< Uninitialised, file access failed */
}; };
/** Photo Formats */ /** Photo Formats */

View file

@ -45,45 +45,46 @@ public:
}; };
/** Parsing and set errors */ /** Parsing and set errors */
enum Error : uint8_t { enum Error : uint8_t {
DescBufferTight = 38, /**< Description Buffer is too tight */ DescBufferTight = 39, /**< Description Buffer is too tight */
DescMallocError = 30, /**< Description Buffer can't be allocated */ DescMallocError = 31, /**< Description Buffer can't be allocated */
DescReadError = 31, /**< Description can't be read successfully */ DescReadError = 32, /**< Description can't be read successfully */
HeaderBufferTight = 34, /**< Header Buffer is too tight */ HeaderBufferTight = 35, /**< Header Buffer is too tight */
HeaderMallocError = 4, /**< Header Buffer can't be allocated */
IncompatibleFormat = 2, /**< Format is incompatible */ IncompatibleFormat = 2, /**< Format is incompatible */
IncompleteChecksum = 6, /**< Header checksum is incomplete */ IncompleteChecksum = 7, /**< Header checksum is incomplete */
IncompleteDescBuffer = 29, /**< Description Buffer Size is incomplete */ IncompleteDescBuffer = 30, /**< Description Buffer Size is incomplete */
IncompleteDescMarker = 27, /**< Description Marker is incomplete */ IncompleteDescMarker = 28, /**< Description Marker is incomplete */
IncompleteDescOffset = 10, /**< Description Offset is incomplete */ IncompleteDescOffset = 11, /**< Description Offset is incomplete */
IncompleteEOF = 7, /**< End Of File Offset is incomplete */ IncompleteEOF = 8, /**< End Of File Offset is incomplete */
IncompleteHeader = 3, /**< Header is incomplete */ IncompleteHeader = 3, /**< Header is incomplete */
IncompleteJendMarker = 32, /**< JEND Marker is incomplete */ IncompleteJendMarker = 33, /**< JEND Marker is incomplete */
IncompleteJpegMarker = 11, /**< JPEG Marker is incomplete */ IncompleteJpegMarker = 12, /**< JPEG Marker is incomplete */
IncompleteJsonBuffer = 19, /**< JSON Buffer Size is incomplete */ IncompleteJsonBuffer = 20, /**< JSON Buffer Size is incomplete */
IncompleteJsonMarker = 17, /**< JSON Marker incomplete */ IncompleteJsonMarker = 18, /**< JSON Marker incomplete */
IncompleteJsonOffset = 8, /**< JSON Offset incomplete */ IncompleteJsonOffset = 9, /**< JSON Offset incomplete */
IncompletePhotoBuffer = 13, /**< Photo Buffer Size is incomplete */ IncompletePhotoBuffer = 14, /**< Photo Buffer Size is incomplete */
IncompletePhotoSize = 14, /**< Photo Size is incomplete */ IncompletePhotoSize = 15, /**< Photo Size is incomplete */
IncompleteTitleBuffer = 24, /**< Title Buffer Size is incomplete */ IncompleteTitleBuffer = 25, /**< Title Buffer Size is incomplete */
IncompleteTitleMarker = 22, /**< Title Marker is incomplete */ IncompleteTitleMarker = 23, /**< Title Marker is incomplete */
IncompleteTitleOffset = 9, /**< Title Offset is incomplete */ IncompleteTitleOffset = 10, /**< Title Offset is incomplete */
IncorrectDescMarker = 28, /**< Description Marker is incorrect */ IncorrectDescMarker = 29, /**< Description Marker is incorrect */
IncorrectJendMarker = 33, /**< JEND Marker is incorrect */ IncorrectJendMarker = 34, /**< JEND Marker is incorrect */
IncorrectJpegMarker = 12, /**< JPEG Marker is incorrect */ IncorrectJpegMarker = 13, /**< JPEG Marker is incorrect */
IncorrectJsonMarker = 18, /**< JSON Marker is incorrect */ IncorrectJsonMarker = 19, /**< JSON Marker is incorrect */
IncorrectTitleMarker = 23, /**< Title Marker is incorrect */ IncorrectTitleMarker = 24, /**< Title Marker is incorrect */
JsonBufferTight = 36, /**< JSON Buffer is too tight */ JsonBufferTight = 37, /**< JSON Buffer is too tight */
JsonMallocError = 20, /**< JSON Buffer can't be allocated */ JsonMallocError = 21, /**< JSON Buffer can't be allocated */
JsonReadError = 21, /**< JSON can't be read successfully */ JsonReadError = 22, /**< JSON can't be read successfully */
NoError = 255, /**< Finished without errors */ NoError = 255, /**< Finished without errors */
NoFormatIdentifier = 1, /**< No format detected, empty file */ NoFormatIdentifier = 1, /**< No format detected, empty file */
PhotoBufferTight = 35, /**< Photo Buffer is too tight */ PhotoBufferTight = 36, /**< Photo Buffer is too tight */
PhotoMallocError = 15, /**< Photo Buffer can't be allocated */ PhotoMallocError = 16, /**< Photo Buffer can't be allocated */
PhotoReadError = 16, /**< Photo can't be read */ PhotoReadError = 17, /**< Photo can't be read */
TitleBufferTight = 37, /**< Title Buffer is too tight */ TitleBufferTight = 38, /**< Title Buffer is too tight */
TitleMallocError = 25, /**< Title Buffer can't be allocated */ TitleMallocError = 26, /**< Title Buffer can't be allocated */
TitleReadError = 26, /**< Title can't be read */ TitleReadError = 27, /**< Title can't be read */
UnicodeHeaderError = 5, /**< Header can't be decoded */ UnicodeInitError = 5, /**< Failed to initialise Unicode decoder */
UnicodeInitError = 4, /**< Failed to initialise Unicode decoder */ UnicodeHeaderError = 6, /**< Header can't be encoded/decoded successfully */
Uninitialised = 0, /**< Uninitialised, file access failed */ Uninitialised = 0, /**< Uninitialised, file access failed */
}; };
/** Photo Formats */ /** Photo Formats */
@ -97,54 +98,86 @@ public:
~RagePhotoA() { ~RagePhotoA() {
ragephoto_close(instance); ragephoto_close(instance);
} }
/** Resets the RagePhoto instance to default values. */
void clear() { void clear() {
ragephoto_clear(instance); ragephoto_clear(instance);
} }
/** Loads a Photo from a const char*.
* \param data Photo data
* \param size Photo data size
*/
bool load(const char *data, size_t size) { bool load(const char *data, size_t size) {
return ragephoto_load(instance, data, size); return ragephoto_load(instance, data, size);
} }
/** Loads a Photo from a std::string.
* \param data Photo data
*/
bool load(const std::string &data) { bool load(const std::string &data) {
return ragephoto_load(instance, data.data(), data.size()); return ragephoto_load(instance, data.data(), data.size());
} }
/** Loads a Photo from a file.
* \param filename File to load
*/
bool loadFile(const char *filename) { bool loadFile(const char *filename) {
return ragephoto_loadfile(instance, filename); return ragephoto_loadfile(instance, filename);
} }
/** Returns the last error occurred. */
uint8_t error() const { uint8_t error() const {
return ragephoto_error(instance); return ragephoto_error(instance);
} }
/** Returns the Photo Format (GTA V or RDR 2). */
uint32_t format() const { uint32_t format() const {
return ragephoto_getphotoformat(instance); return ragephoto_getphotoformat(instance);
} }
/** Returns the Photo JPEG data. */
const std::string photo() const { const std::string photo() const {
return std::string(ragephoto_getphotojpeg(instance), ragephoto_getphotosize(instance)); return std::string(ragephoto_getphotojpeg(instance), ragephoto_getphotosize(instance));
} }
/** Returns the Photo JPEG data. */
const char *photoData() const { const char *photoData() const {
return ragephoto_getphotojpeg(instance); return ragephoto_getphotojpeg(instance);
} }
/** Returns the Photo JPEG data size. */
uint32_t photoSize() const { uint32_t photoSize() const {
return ragephoto_getphotosize(instance); return ragephoto_getphotosize(instance);
} }
/** Returns the Photo description. */
const char* description() const { const char* description() const {
return ragephoto_getphotodesc(instance); return ragephoto_getphotodesc(instance);
} }
/** Returns the Photo JSON data. */
const char* json() const { const char* json() const {
return ragephoto_getphotojson(instance); return ragephoto_getphotojson(instance);
} }
/** Returns the Photo header. */
const char* header() const { const char* header() const {
return ragephoto_getphotoheader(instance); return ragephoto_getphotoheader(instance);
} }
/** Returns the Photo title. */
const char* title() const { const char* title() const {
return ragephoto_getphototitle(instance); return ragephoto_getphototitle(instance);
} }
/** Returns the library version. */
static const char* version() { static const char* version() {
return ragephoto_version(); return ragephoto_version();
} }
/** 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) { bool save(char *data, uint32_t photoFormat) {
return ragephoto_savef(instance, data, photoFormat); return ragephoto_savef(instance, data, photoFormat);
} }
/** Saves a Photo to a char*.
* \param data Photo data
*/
bool save(char *data) { bool save(char *data) {
return ragephoto_save(instance, data); return ragephoto_save(instance, 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) { const std::string save(uint32_t photoFormat, bool *ok = nullptr) {
std::string sdata; std::string sdata;
const size_t size = ragephoto_getsavesizef(instance, photoFormat); const size_t size = ragephoto_getsavesizef(instance, photoFormat);
@ -159,45 +192,68 @@ public:
*ok = saved; *ok = saved;
return sdata; return sdata;
} }
/** Saves a Photo to a std::string.
* \param ok \p true when saved successfully
*/
const std::string save(bool *ok = nullptr) { const std::string save(bool *ok = nullptr) {
return save(ragephoto_getphotoformat(instance), ok); return save(ragephoto_getphotoformat(instance), ok);
} }
/** Saves a Photo to a file. */
bool saveFile(const char *filename, uint32_t photoFormat) { bool saveFile(const char *filename, uint32_t photoFormat) {
return ragephoto_savefilef(instance, filename, photoFormat); return ragephoto_savefilef(instance, filename, photoFormat);
} }
/** Saves a Photo to a file. */
bool saveFile(const char *filename) { bool saveFile(const char *filename) {
return ragephoto_savefile(instance, filename); return ragephoto_savefile(instance, filename);
} }
/** Returns the Photo save file size. */
size_t saveSize(uint32_t photoFormat) { size_t saveSize(uint32_t photoFormat) {
return ragephoto_getsavesizef(instance, photoFormat); return ragephoto_getsavesizef(instance, photoFormat);
} }
/** Returns the Photo save file size. */
size_t saveSize() { size_t saveSize() {
return ragephoto_getsavesize(instance); return ragephoto_getsavesize(instance);
} }
/** Sets all cross-format Buffer to default size. */
void setBufferDefault() { void setBufferDefault() {
ragephoto_setbufferdefault(instance); ragephoto_setbufferdefault(instance);
} }
/** Moves all Buffer offsets to correct position. */
void setBufferOffsets() { void setBufferOffsets() {
ragephoto_setbufferoffsets(instance); ragephoto_setbufferoffsets(instance);
} }
/** Sets the Photo description. */
void setDescription(const char *description, uint32_t bufferSize = 0) { void setDescription(const char *description, uint32_t bufferSize = 0) {
ragephoto_setphotodesc(instance, description, bufferSize); ragephoto_setphotodesc(instance, description, bufferSize);
} }
/** Sets the Photo Format (GTA V or RDR 2). */
void setFormat(uint32_t photoFormat) { void setFormat(uint32_t photoFormat) {
ragephoto_setphotoformat(instance, photoFormat); ragephoto_setphotoformat(instance, photoFormat);
} }
/** Sets the Photo JSON data. */
void setJson(const char *json, uint32_t bufferSize = 0) { void setJson(const char *json, uint32_t bufferSize = 0) {
ragephoto_setphotojson(instance, json, bufferSize); ragephoto_setphotojson(instance, json, bufferSize);
} }
/** Sets the Photo header. (EXPERT ONLY) */
void setHeader(const char *header, uint32_t headerSum) { void setHeader(const char *header, uint32_t headerSum) {
ragephoto_setphotoheader(instance, header, headerSum); ragephoto_setphotoheader(instance, header, headerSum);
} }
/** Sets the Photo JPEG data.
* \param data JPEG data
* \param size JPEG data size
* \param bufferSize JPEG buffer size
*/
bool setPhoto(const char *data, uint32_t size, uint32_t bufferSize = 0) { bool setPhoto(const char *data, uint32_t size, uint32_t bufferSize = 0) {
return ragephoto_setphotojpeg(instance, data, size, bufferSize); return ragephoto_setphotojpeg(instance, data, size, bufferSize);
} }
/** Sets the Photo JPEG data.
* \param data JPEG data
* \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) {
return ragephoto_setphotojpeg(instance, data.data(), static_cast<uint32_t>(data.size()), bufferSize); return ragephoto_setphotojpeg(instance, data.data(), static_cast<uint32_t>(data.size()), bufferSize);
} }
/** Sets the Photo title. */
void setTitle(const char *title, uint32_t bufferSize = 0) { void setTitle(const char *title, uint32_t bufferSize = 0) {
ragephoto_setphototitle(instance, title, bufferSize); ragephoto_setphototitle(instance, title, bufferSize);
} }

View file

@ -22,7 +22,7 @@
#include "stdint.h" #include "stdint.h"
typedef struct RagePhotoData { typedef struct RagePhotoData {
char* photoData; char* jpeg;
char* description; char* description;
char* json; char* json;
char* header; char* header;
@ -32,11 +32,11 @@ typedef struct RagePhotoData {
uint32_t descOffset; uint32_t descOffset;
uint32_t endOfFile; uint32_t endOfFile;
uint32_t headerSum; uint32_t headerSum;
uint32_t jpegSize;
uint32_t jsonBuffer; uint32_t jsonBuffer;
uint32_t jsonOffset; uint32_t jsonOffset;
uint32_t photoBuffer; uint32_t photoBuffer;
uint32_t photoFormat; uint32_t photoFormat;
uint32_t photoSize;
uint32_t titlBuffer; uint32_t titlBuffer;
uint32_t titlOffset; uint32_t titlOffset;
uint32_t unnamedSum1; uint32_t unnamedSum1;