unicode.cmake improvements, RagePhoto API changes

RagePhoto: remove class from Error enum
RagePhotoA: error() uint32_t -> uint8_t
RagePhoto-Extract: remove Error:: from Error enums
ragephoto-gtkviewer: RagePhoto::Error -> uint8_t, remove Error:: from
Error enum
ragephoto-qtviewer: RagePhoto::Error -> uint8_t, remove Error:: from
Error enum
This commit is contained in:
Syping 2021-12-02 05:55:50 +01:00
parent eed3290633
commit 7326cc26c7
7 changed files with 135 additions and 123 deletions

View file

@ -16,26 +16,34 @@
* responsible for anything with use of the software, you are self responsible. * responsible for anything with use of the software, you are self responsible.
****************************************************************************]] ****************************************************************************]]
# RagePhoto Unicode functionality tests set(RAGEPHOTO_UNICODE "" CACHE STRING "libragephoto Unicode implementation")
message("-- Testing codecvt") if (RAGEPHOTO_UNICODE)
try_run(CODECVT_RUN CODECVT_COMPILE ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR}/tests/CodecvtTest.cpp) string(TOUPPER "UNICODE_${RAGEPHOTO_UNICODE}" UNICODE_DEF)
if (CODECVT_COMPILE AND CODECVT_RUN EQUAL 0) list(APPEND LIBRAGEPHOTO_DEFINES
"${UNICODE_DEF}"
)
message("-- UnicodeCvt - ${RAGEPHOTO_UNICODE}")
else()
# RagePhoto Unicode functionality tests
message("-- Testing codecvt")
try_run(CODECVT_RUN CODECVT_COMPILE ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR}/tests/CodecvtTest.cpp)
if (CODECVT_COMPILE AND CODECVT_RUN EQUAL 0)
set(CODECVT_COMPAT TRUE) set(CODECVT_COMPAT TRUE)
message("-- Testing codecvt - yes") message("-- Testing codecvt - yes")
else() else()
message("-- Testing codecvt - no") message("-- Testing codecvt - no")
endif() endif()
message("-- Testing iconv") message("-- Testing iconv")
try_run(ICONV_RUN ICONV_COMPILE ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR}/tests/IconvTest.cpp) try_run(ICONV_RUN ICONV_COMPILE ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR}/tests/IconvTest.cpp)
if (ICONV_COMPILE AND ICONV_RUN EQUAL 0) if (ICONV_COMPILE AND ICONV_RUN EQUAL 0)
set(ICONV_COMPAT TRUE) set(ICONV_COMPAT TRUE)
message("-- Testing iconv - yes") message("-- Testing iconv - yes")
else() else()
message("-- Testing iconv - no") message("-- Testing iconv - no")
endif() endif()
if (WIN32) if (WIN32)
message("-- Testing wincvt") message("-- Testing wincvt")
try_run(WINCVT_RUN WINCVT_COMPILE ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR}/tests/WincvtTest.cpp) try_run(WINCVT_RUN WINCVT_COMPILE ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR}/tests/WincvtTest.cpp)
if (WINCVT_COMPILE AND WINCVT_RUN EQUAL 0) if (WINCVT_COMPILE AND WINCVT_RUN EQUAL 0)
@ -44,19 +52,23 @@ if (WIN32)
else() else()
message("-- Testing wincvt - no") message("-- Testing wincvt - no")
endif() endif()
endif() endif()
# Unicode implementation for RagePhoto # Unicode implementation for RagePhoto
if (WINCVT_COMPAT) if (WINCVT_COMPAT)
list(APPEND LIBRAGEPHOTO_DEFINES list(APPEND LIBRAGEPHOTO_DEFINES
UNICODE_WINCVT "UNICODE_WINCVT"
) )
elseif (CODECVT_COMPAT) message("-- UnicodeCvt - wincvt")
elseif (CODECVT_COMPAT)
list(APPEND LIBRAGEPHOTO_DEFINES list(APPEND LIBRAGEPHOTO_DEFINES
UNICODE_CODECVT "UNICODE_CODECVT"
) )
elseif (ICONV_COMPAT) message("-- UnicodeCvt - codecvt")
elseif (ICONV_COMPAT)
list(APPEND LIBRAGEPHOTO_DEFINES list(APPEND LIBRAGEPHOTO_DEFINES
UNICODE_ICONV "UNICODE_ICONV"
) )
message("-- UnicodeCvt - iconv")
endif()
endif() endif()

View file

@ -39,8 +39,8 @@ bool readPhotoFile(const std::string &filename, Gtk::Window *win, Gtk::Image *im
RagePhoto ragePhoto; RagePhoto ragePhoto;
const bool loaded = ragePhoto.load(data); const bool loaded = ragePhoto.load(data);
if (!loaded) { if (!loaded) {
const RagePhoto::Error error = ragePhoto.error(); const uint8_t error = ragePhoto.error();
if (error <= RagePhoto::Error::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

@ -38,8 +38,8 @@ bool readPhotoFile(const QString &filename, QMainWindow *mainWindow, QLabel *pho
RagePhoto 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 RagePhoto::Error error = ragePhoto.error(); const uint8_t error = ragePhoto.error();
if (error <= RagePhoto::Error::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

@ -33,11 +33,11 @@ int main(int argc, char *argv[])
const bool loaded = ragePhoto.loadFile(argv[1]); const bool loaded = ragePhoto.loadFile(argv[1]);
if (!loaded) { if (!loaded) {
if (ragePhoto.error() == RagePhoto::Error::Uninitialised) { if (ragePhoto.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 (ragePhoto.error() <= RagePhoto::Error::PhotoReadError) { else if (ragePhoto.error() <= RagePhoto::PhotoReadError) {
std::cout << "Failed to load photo" << std::endl; std::cout << "Failed to load photo" << std::endl;
return 1; return 1;
} }

View file

@ -21,11 +21,11 @@
#include "RagePhotoC.h" #include "RagePhotoC.h"
#endif #endif
#include <cstdlib>
#include <cstring>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <iterator> #include <iterator>
#include <cstdlib>
#include <cstring>
#ifdef RAGEPHOTO_BENCHMARK #ifdef RAGEPHOTO_BENCHMARK
#include <chrono> #include <chrono>
@ -129,7 +129,7 @@ bool RagePhoto::load(const char *data, size_t length)
char uInt32Buffer[4]; char uInt32Buffer[4];
size_t size = readBuffer(data, uInt32Buffer, &pos, 4, length); size_t size = readBuffer(data, uInt32Buffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
m_data.error = static_cast<uint8_t>(Error::NoFormatIdentifier); // 1 m_data.error = Error::NoFormatIdentifier; // 1
return false; return false;
} }
@ -143,7 +143,7 @@ bool RagePhoto::load(const char *data, size_t length)
char photoHeader[256]; char photoHeader[256];
size = readBuffer(data, photoHeader, &pos, 256, length); size = readBuffer(data, photoHeader, &pos, 256, length);
if (size != 256) { if (size != 256) {
m_data.error = static_cast<uint8_t>(Error::IncompleteHeader); // 3 m_data.error = Error::IncompleteHeader; // 3
return false; return false;
} }
@ -151,13 +151,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;
m_data.header = convert.to_bytes(reinterpret_cast<char16_t*>(photoHeader)); m_data.header = convert.to_bytes(reinterpret_cast<char16_t*>(photoHeader));
if (convert.converted() == 0) { if (convert.converted() == 0) {
m_data.error = static_cast<uint8_t>(Error::UnicodeHeaderError); // 5 m_data.error = Error::UnicodeHeaderError; // 5
return false; return false;
} }
#elif defined UNICODE_ICONV #elif defined UNICODE_ICONV
iconv_t iconv_in = iconv_open("UTF-8", "UTF-16LE"); iconv_t iconv_in = iconv_open("UTF-8", "UTF-16LE");
if (iconv_in == (iconv_t)-1) { if (iconv_in == (iconv_t)-1) {
m_data.error = static_cast<uint8_t>(Error::UnicodeInitError); // 4 m_data.error = Error::UnicodeInitError; // 4
return false; return false;
} }
char photoHeader_string[256]; char photoHeader_string[256];
@ -168,7 +168,7 @@ 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 = static_cast<uint8_t>(Error::UnicodeHeaderError); // 5 m_data.error = Error::UnicodeHeaderError; // 5
return false; return false;
} }
m_data.header = std::string(photoHeader_string); m_data.header = std::string(photoHeader_string);
@ -176,7 +176,7 @@ bool RagePhoto::load(const char *data, size_t length)
char photoHeader_string[256]; char photoHeader_string[256];
const int converted = WideCharToMultiByte(CP_UTF8, 0, reinterpret_cast<wchar_t*>(photoHeader), -1, photoHeader_string, 256, NULL, NULL); const int converted = WideCharToMultiByte(CP_UTF8, 0, reinterpret_cast<wchar_t*>(photoHeader), -1, photoHeader_string, 256, NULL, NULL);
if (converted == 0) { if (converted == 0) {
m_data.error = static_cast<uint8_t>(Error::UnicodeHeaderError); // 5 m_data.error = Error::UnicodeHeaderError; // 5
return false; return false;
} }
m_data.header = std::string(photoHeader_string); m_data.header = std::string(photoHeader_string);
@ -184,7 +184,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 = static_cast<uint8_t>(Error::IncompleteChecksum); // 6 m_data.error = Error::IncompleteChecksum; // 6
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -196,7 +196,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 = static_cast<uint8_t>(Error::IncompleteChecksum); // 6 m_data.error = Error::IncompleteChecksum; // 6
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -207,7 +207,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 = static_cast<uint8_t>(Error::IncompleteChecksum); // 6 m_data.error = Error::IncompleteChecksum; // 6
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -220,7 +220,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 = static_cast<uint8_t>(Error::IncompleteEOF); // 7 m_data.error = Error::IncompleteEOF; // 7
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -231,7 +231,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 = static_cast<uint8_t>(Error::IncompleteJsonOffset); // 8 m_data.error = Error::IncompleteJsonOffset; // 8
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -241,7 +241,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 = static_cast<uint8_t>(Error::IncompleteTitleOffset); // 9 m_data.error = Error::IncompleteTitleOffset; // 9
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -252,7 +252,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 = static_cast<uint8_t>(Error::IncompleteDescOffset); // 10 m_data.error = Error::IncompleteDescOffset; // 10
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -264,17 +264,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 = static_cast<uint8_t>(Error::IncompleteJpegMarker); // 11 m_data.error = Error::IncompleteJpegMarker; // 11
return false; return false;
} }
if (strncmp(markerBuffer, "JPEG", 4) != 0) { if (strncmp(markerBuffer, "JPEG", 4) != 0) {
m_data.error = static_cast<uint8_t>(Error::IncorrectJpegMarker); // 12 m_data.error = Error::IncorrectJpegMarker; // 12
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 = static_cast<uint8_t>(Error::IncompletePhotoBuffer); // 13 m_data.error = Error::IncompletePhotoBuffer; // 13
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -285,7 +285,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 = static_cast<uint8_t>(Error::IncompletePhotoSize); // 14 m_data.error = Error::IncompletePhotoSize; // 14
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -296,14 +296,14 @@ bool RagePhoto::load(const char *data, size_t length)
m_data.photoData = static_cast<char*>(std::malloc(m_data.photoSize)); m_data.photoData = static_cast<char*>(std::malloc(m_data.photoSize));
if (!m_data.photoData) { if (!m_data.photoData) {
m_data.error = static_cast<uint8_t>(Error::PhotoMallocError); // 15 m_data.error = Error::PhotoMallocError; // 15
return false; return false;
} }
size = readBuffer(data, m_data.photoData, &pos, m_data.photoSize, length); size = readBuffer(data, m_data.photoData, &pos, m_data.photoSize, length);
if (size != m_data.photoSize) { if (size != m_data.photoSize) {
std::free(m_data.photoData); std::free(m_data.photoData);
m_data.photoData = nullptr; m_data.photoData = nullptr;
m_data.error = static_cast<uint8_t>(Error::PhotoReadError); // 16 m_data.error = Error::PhotoReadError; // 16
return false; return false;
} }
m_data.photoLoaded = true; m_data.photoLoaded = true;
@ -311,17 +311,17 @@ bool RagePhoto::load(const char *data, size_t length)
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 = static_cast<uint8_t>(Error::IncompleteJsonMarker); // 17 m_data.error = Error::IncompleteJsonMarker; // 17
return false; return false;
} }
if (strncmp(markerBuffer, "JSON", 4) != 0) { if (strncmp(markerBuffer, "JSON", 4) != 0) {
m_data.error = static_cast<uint8_t>(Error::IncorrectJsonMarker); // 18 m_data.error = Error::IncorrectJsonMarker; // 18
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 = static_cast<uint8_t>(Error::IncompleteJsonBuffer); // 19 m_data.error = Error::IncompleteJsonBuffer; // 19
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -332,13 +332,13 @@ bool RagePhoto::load(const char *data, size_t length)
char *t_jsonData = static_cast<char*>(std::malloc(m_data.jsonBuffer)); char *t_jsonData = static_cast<char*>(std::malloc(m_data.jsonBuffer));
if (!t_jsonData) { if (!t_jsonData) {
m_data.error = static_cast<uint8_t>(Error::JsonMallocError); // 20 m_data.error = Error::JsonMallocError; // 20
return false; return false;
} }
size = readBuffer(data, t_jsonData, &pos, m_data.jsonBuffer, length); size = readBuffer(data, t_jsonData, &pos, m_data.jsonBuffer, length);
if (size != m_data.jsonBuffer) { if (size != m_data.jsonBuffer) {
std::free(t_jsonData); std::free(t_jsonData);
m_data.error = static_cast<uint8_t>(Error::JsonReadError); // 21 m_data.error = Error::JsonReadError; // 21
return false; return false;
} }
m_data.json = std::string(t_jsonData); m_data.json = std::string(t_jsonData);
@ -347,17 +347,17 @@ bool RagePhoto::load(const char *data, size_t length)
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 = static_cast<uint8_t>(Error::IncompleteTitleMarker); // 22 m_data.error = Error::IncompleteTitleMarker; // 22
return false; return false;
} }
if (strncmp(markerBuffer, "TITL", 4) != 0) { if (strncmp(markerBuffer, "TITL", 4) != 0) {
m_data.error = static_cast<uint8_t>(Error::IncorrectTitleMarker); // 23 m_data.error = Error::IncorrectTitleMarker; // 23
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 = static_cast<uint8_t>(Error::IncompleteTitleBuffer); // 24 m_data.error = Error::IncompleteTitleBuffer; // 24
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -368,13 +368,13 @@ bool RagePhoto::load(const char *data, size_t length)
char *t_titlData = static_cast<char*>(std::malloc(m_data.titlBuffer)); char *t_titlData = static_cast<char*>(std::malloc(m_data.titlBuffer));
if (!t_titlData) { if (!t_titlData) {
m_data.error = static_cast<uint8_t>(Error::TitleMallocError); // 25 m_data.error = Error::TitleMallocError; // 25
return false; return false;
} }
size = readBuffer(data, t_titlData, &pos, m_data.titlBuffer, length); size = readBuffer(data, t_titlData, &pos, m_data.titlBuffer, length);
if (size != m_data.titlBuffer) { if (size != m_data.titlBuffer) {
std::free(t_titlData); std::free(t_titlData);
m_data.error = static_cast<uint8_t>(Error::TitleReadError); // 26 m_data.error = Error::TitleReadError; // 26
return false; return false;
} }
m_data.title = std::string(t_titlData); m_data.title = std::string(t_titlData);
@ -383,17 +383,17 @@ bool RagePhoto::load(const char *data, size_t length)
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 = static_cast<uint8_t>(Error::IncompleteDescMarker); // 27 m_data.error = Error::IncompleteDescMarker; // 27
return false; return false;
} }
if (strncmp(markerBuffer, "DESC", 4) != 0) { if (strncmp(markerBuffer, "DESC", 4) != 0) {
m_data.error = static_cast<uint8_t>(Error::IncorrectDescMarker); // 28 m_data.error = Error::IncorrectDescMarker; // 28
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 = static_cast<uint8_t>(Error::IncompleteDescBuffer); // 29 m_data.error = Error::IncompleteDescBuffer; // 29
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -404,13 +404,13 @@ bool RagePhoto::load(const char *data, size_t length)
char *t_descData = static_cast<char*>(std::malloc(m_data.descBuffer)); char *t_descData = static_cast<char*>(std::malloc(m_data.descBuffer));
if (!t_descData) { if (!t_descData) {
m_data.error = static_cast<uint8_t>(Error::DescMallocError); // 30 m_data.error = Error::DescMallocError; // 30
return false; return false;
} }
size = readBuffer(data, t_descData, &pos, m_data.descBuffer, length); size = readBuffer(data, t_descData, &pos, m_data.descBuffer, length);
if (size != m_data.descBuffer) { if (size != m_data.descBuffer) {
std::free(t_descData); std::free(t_descData);
m_data.error = static_cast<uint8_t>(Error::DescReadError); // 31 m_data.error = Error::DescReadError; // 31
return false; return false;
} }
m_data.description = std::string(t_descData); m_data.description = std::string(t_descData);
@ -419,11 +419,11 @@ bool RagePhoto::load(const char *data, size_t length)
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 = static_cast<uint8_t>(Error::IncompleteJendMarker); // 32 m_data.error = Error::IncompleteJendMarker; // 32
return false; return false;
} }
if (strncmp(markerBuffer, "JEND", 4) != 0) { if (strncmp(markerBuffer, "JEND", 4) != 0) {
m_data.error = static_cast<uint8_t>(Error::IncorrectJendMarker); // 33 m_data.error = Error::IncorrectJendMarker; // 33
return false; return false;
} }
@ -456,11 +456,11 @@ bool RagePhoto::load(const char *data, size_t length)
std::cout << "real size: " << length << std::endl; std::cout << "real size: " << length << std::endl;
#endif #endif
m_data.error = static_cast<uint8_t>(Error::NoError); // 255 m_data.error = Error::NoError; // 255
return true; return true;
#else #else
std::cout << "UTF-16LE decoding support missing" << std::endl; std::cout << "UTF-16LE decoding support missing" << std::endl;
m_data.error = static_cast<uint8_t>(Error::UnicodeInitError); // 4 m_data.error = Error::UnicodeInitError; // 4
return false; return false;
#endif #endif
} }
@ -469,7 +469,7 @@ bool RagePhoto::load(const char *data, size_t length)
if (it != m_loadFuncs.end()) if (it != m_loadFuncs.end())
return it->second(data, length, &m_data); return it->second(data, length, &m_data);
} }
m_data.error = static_cast<uint8_t>(Error::IncompatibleFormat); // 2 m_data.error = Error::IncompatibleFormat; // 2
return false; return false;
} }
@ -487,14 +487,14 @@ bool RagePhoto::loadFile(const std::string &filename)
return load(sdata); return load(sdata);
} }
else { else {
m_data.error = static_cast<uint8_t>(Error::Uninitialised); // 0 m_data.error = Error::Uninitialised; // 0
return false; return false;
} }
} }
RagePhoto::Error RagePhoto::error() const uint8_t RagePhoto::error() const
{ {
return static_cast<Error>(m_data.error); return m_data.error;
} }
uint32_t RagePhoto::format() const uint32_t RagePhoto::format() const
@ -559,19 +559,19 @@ 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 = static_cast<uint8_t>(Error::UnicodeHeaderError); // 5 m_data.error = Error::UnicodeHeaderError; // 5
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 = static_cast<uint8_t>(Error::HeaderBufferTight); // 34 m_data.error = Error::HeaderBufferTight; // 34
return false; return false;
} }
const char *photoHeader = reinterpret_cast<const char*>(photoHeader_string.data()); const char *photoHeader = reinterpret_cast<const char*>(photoHeader_string.data());
#elif defined UNICODE_ICONV #elif defined UNICODE_ICONV
iconv_t iconv_in = iconv_open("UTF-16LE", "UTF-8"); iconv_t iconv_in = iconv_open("UTF-16LE", "UTF-8");
if (iconv_in == (iconv_t)-1) { if (iconv_in == (iconv_t)-1) {
m_data.error = static_cast<uint8_t>(Error::UnicodeInitError); // 4 m_data.error = Error::UnicodeInitError; // 4
return false; return false;
} }
char photoHeader_string[256]; char photoHeader_string[256];
@ -584,40 +584,40 @@ 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 = static_cast<uint8_t>(Error::UnicodeHeaderError); // 5 m_data.error = Error::UnicodeHeaderError; // 5
return false; return false;
} }
const size_t photoHeader_size = 256; const size_t photoHeader_size = 256;
#elif defined UNICODE_WINCVT #elif defined UNICODE_WINCVT
char photoHeader[256]{}; char photoHeader[256]{};
const int converted = MultiByteToWideChar(CP_UTF8, 0, m_data.header.data(), m_data.header.size(), reinterpret_cast<wchar_t*>(photoHeader), 256 / sizeof(wchar_t)); const int converted = MultiByteToWideChar(CP_UTF8, 0, m_data.header.data(), static_cast<int>(m_data.header.size()), reinterpret_cast<wchar_t*>(photoHeader), 256 / sizeof(wchar_t));
if (converted == 0) { if (converted == 0) {
m_data.error = static_cast<uint8_t>(Error::UnicodeHeaderError); // 5 m_data.error = Error::UnicodeHeaderError; // 5
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.photoSize > m_data.photoBuffer) {
m_data.error = static_cast<uint8_t>(Error::PhotoBufferTight); // 35 m_data.error = Error::PhotoBufferTight; // 35
return false; return false;
} }
const size_t jsonString_size = m_data.json.size() + 1; const size_t jsonString_size = m_data.json.size() + 1;
if (jsonString_size > m_data.jsonBuffer) { if (jsonString_size > m_data.jsonBuffer) {
m_data.error = static_cast<uint8_t>(Error::JsonBufferTight); // 36 m_data.error = Error::JsonBufferTight; // 36
return false; return false;
} }
const size_t titlString_size = m_data.title.size() + 1; const size_t titlString_size = m_data.title.size() + 1;
if (titlString_size > m_data.titlBuffer) { if (titlString_size > m_data.titlBuffer) {
m_data.error = static_cast<uint8_t>(Error::TitleBufferTight); // 37 m_data.error = Error::TitleBufferTight; // 37
return false; return false;
} }
const size_t descString_size = m_data.description.size() + 1; const size_t descString_size = m_data.description.size() + 1;
if (descString_size > m_data.descBuffer) { if (descString_size > m_data.descBuffer) {
m_data.error = static_cast<uint8_t>(Error::DescBufferTight); // 38 m_data.error = Error::DescBufferTight; // 38
return false; return false;
} }
@ -758,16 +758,16 @@ bool RagePhoto::save(char *data, uint32_t photoFormat)
pos = m_data.endOfFile + headerSize - 4; pos = m_data.endOfFile + headerSize - 4;
writeBuffer("JEND", data, &pos, length, 4); writeBuffer("JEND", data, &pos, length, 4);
m_data.error = static_cast<uint8_t>(Error::NoError); // 255 m_data.error = Error::NoError; // 255
return true; return true;
#else #else
std::cout << "UTF-16LE encoding support missing" << std::endl; std::cout << "UTF-16LE encoding support missing" << std::endl;
m_data.error = static_cast<uint8_t>(Error::UnicodeInitError); // 4 m_data.error = Error::UnicodeInitError; // 4
return false; return false;
#endif #endif
} }
m_data.error = static_cast<uint8_t>(Error::IncompatibleFormat); // 2 m_data.error = Error::IncompatibleFormat; // 2
return false; return false;
} }
@ -778,18 +778,18 @@ bool RagePhoto::save(char *data)
const std::string RagePhoto::save(uint32_t photoFormat, bool *ok) const std::string RagePhoto::save(uint32_t photoFormat, bool *ok)
{ {
std::string data; std::string sdata;
const size_t size = saveSize(photoFormat); const size_t size = saveSize(photoFormat);
if (size == 0) { if (size == 0) {
if (ok) if (ok)
*ok = false; *ok = false;
return data; return sdata;
} }
data.resize(size); sdata.resize(size);
const bool saved = save(&data[0], photoFormat); const bool saved = save(&sdata[0], photoFormat);
if (ok) if (ok)
*ok = saved; *ok = saved;
return data; return sdata;
} }
const std::string RagePhoto::save(bool *ok) const std::string RagePhoto::save(bool *ok)
@ -800,11 +800,11 @@ const std::string RagePhoto::save(bool *ok)
bool RagePhoto::saveFile(const std::string &filename, uint32_t photoFormat) bool RagePhoto::saveFile(const std::string &filename, uint32_t photoFormat)
{ {
bool ok; bool ok;
const std::string sdata = save(photoFormat, &ok); const std::string &sdata = save(photoFormat, &ok);
if (ok) { if (ok) {
std::ofstream ofs(filename, std::ios::out | std::ios::binary | std::ios::trunc); std::ofstream ofs(filename, std::ios::out | std::ios::binary | std::ios::trunc);
if (!ofs.is_open()) { if (!ofs.is_open()) {
m_data.error = static_cast<uint8_t>(Error::Uninitialised); // 0 m_data.error = Error::Uninitialised; // 0
return false; return false;
} }
ofs << sdata; ofs << sdata;
@ -912,7 +912,7 @@ bool RagePhoto::setPhoto(const char *data, uint32_t size, uint32_t bufferSize)
if (m_data.photoSize > size) { if (m_data.photoSize > size) {
char *t_photoData = static_cast<char*>(std::realloc(m_data.photoData, size)); char *t_photoData = static_cast<char*>(std::realloc(m_data.photoData, size));
if (!t_photoData) { if (!t_photoData) {
m_data.error = static_cast<uint8_t>(Error::PhotoMallocError); // 15 m_data.error = Error::PhotoMallocError; // 15
return false; return false;
} }
m_data.photoData = t_photoData; m_data.photoData = t_photoData;
@ -923,7 +923,7 @@ bool RagePhoto::setPhoto(const char *data, uint32_t size, uint32_t bufferSize)
std::free(m_data.photoData); std::free(m_data.photoData);
m_data.photoData = static_cast<char*>(std::malloc(size)); m_data.photoData = static_cast<char*>(std::malloc(size));
if (!m_data.photoData) { if (!m_data.photoData) {
m_data.error = static_cast<uint8_t>(Error::PhotoMallocError); // 15 m_data.error = Error::PhotoMallocError; // 15
m_data.photoLoaded = false; m_data.photoLoaded = false;
return false; return false;
} }
@ -937,7 +937,7 @@ bool RagePhoto::setPhoto(const char *data, uint32_t size, uint32_t bufferSize)
else { else {
m_data.photoData = static_cast<char*>(std::malloc(size)); m_data.photoData = static_cast<char*>(std::malloc(size));
if (!m_data.photoData) { if (!m_data.photoData) {
m_data.error = static_cast<uint8_t>(Error::PhotoMallocError); // 15 m_data.error = Error::PhotoMallocError; // 15
return false; return false;
} }
std::memcpy(m_data.photoData, data, size); std::memcpy(m_data.photoData, data, size);
@ -950,7 +950,7 @@ bool RagePhoto::setPhoto(const char *data, uint32_t size, uint32_t bufferSize)
setBufferOffsets(); setBufferOffsets();
} }
m_data.error = static_cast<uint8_t>(Error::NoError); // 255 m_data.error = Error::NoError; // 255
return true; return true;
} }
@ -995,7 +995,7 @@ int ragephoto_loadfile(ragephoto_t instance, const char *filename)
uint8_t ragephoto_error(ragephoto_t instance) uint8_t ragephoto_error(ragephoto_t instance)
{ {
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance); RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
return static_cast<uint8_t>(ragePhoto->error()); return ragePhoto->error();
} }
uint32_t ragephoto_defpbuf_gta5() uint32_t ragephoto_defpbuf_gta5()

View file

@ -48,7 +48,7 @@ public:
RDR2_HEADERSIZE = 272UL, /**< RDR 2 Header Size */ RDR2_HEADERSIZE = 272UL, /**< RDR 2 Header Size */
}; };
/** Parsing and set errors */ /** Parsing and set errors */
enum class Error : uint8_t { enum Error : uint8_t {
DescBufferTight = 38, /**< Description Buffer is too tight */ DescBufferTight = 38, /**< Description Buffer is too tight */
DescMallocError = 30, /**< Description Buffer can't be allocated */ DescMallocError = 30, /**< Description Buffer can't be allocated */
DescReadError = 31, /**< Description can't be read successfully */ DescReadError = 31, /**< Description can't be read successfully */
@ -111,7 +111,7 @@ public:
* \param filename File to load * \param filename File to load
*/ */
bool loadFile(const std::string &filename); bool loadFile(const std::string &filename);
Error error() const; /**< Returns the last error occurred. */ uint8_t error() const; /**< Returns the last error occurred. */
uint32_t format() const; /**< Returns the Photo Format (GTA V or RDR 2). */ uint32_t format() const; /**< Returns the Photo Format (GTA V or RDR 2). */
const std::string photo() const; /**< Returns the Photo JPEG data. */ const std::string photo() const; /**< Returns the Photo JPEG data. */
const char *photoData() const; /**< Returns the Photo JPEG data. */ const char *photoData() const; /**< Returns the Photo JPEG data. */

View file

@ -51,7 +51,7 @@ public:
bool loadFile(const char *filename) { bool loadFile(const char *filename) {
return ragephoto_loadfile(instance, filename); return ragephoto_loadfile(instance, filename);
} }
uint32_t error() const { uint8_t error() const {
return ragephoto_error(instance); return ragephoto_error(instance);
} }
uint32_t format() const { uint32_t format() const {
@ -88,18 +88,18 @@ public:
return ragephoto_save(instance, data); return ragephoto_save(instance, data);
} }
const std::string save(uint32_t photoFormat, bool *ok = nullptr) { const std::string save(uint32_t photoFormat, bool *ok = nullptr) {
std::string data; std::string sdata;
const size_t size = ragephoto_getsavesizef(instance, photoFormat); const size_t size = ragephoto_getsavesizef(instance, photoFormat);
if (size == 0) { if (size == 0) {
if (ok) if (ok)
*ok = false; *ok = false;
return data; return sdata;
} }
data.resize(size); sdata.resize(size);
const bool saved = ragephoto_savef(instance, &data[0], photoFormat); const bool saved = ragephoto_savef(instance, &sdata[0], photoFormat);
if (ok) if (ok)
*ok = saved; *ok = saved;
return data; return sdata;
} }
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);