Compare commits

...

24 Commits
master ... dev

Author SHA1 Message Date
Syping 9379595283 RagePhoto.cpp: remove unused uInt32Buffer 2024-04-22 21:26:16 +02:00
Syping bb2b98e5d8 libragephoto: use ternary operator for flags
index.dox: remove unnecessary whitespaces
2024-04-16 09:30:41 +02:00
Syping 2d59f2acf9 libragephoto: added library flags support
libragephoto: added RAGEPHOTO_FLAG_LEGACY_NULL_RETURN library flag
2024-04-15 12:40:36 +02:00
Syping bc9065e8dc libragephoto: improve documentation 2024-04-09 05:21:36 +02:00
Syping 0fa69667d0 libragephoto: put C++ wrapper in ragephoto_c namespace 2024-04-05 19:46:12 +02:00
Syping eadec52f35 libragephoto: improve serializer 2024-04-05 19:31:14 +02:00
Syping caf885d91c RagePhoto.c: fix off_t type in ragephoto_loadfile 2024-04-05 04:22:43 +02:00
Syping c8c2f65b3a libragephoto: remove offset jumping
- CMakeLists.txt: add RAGEPHOTO_DEBUG option
- RagePhoto.c: fix ragephoto_loadfile and add support for 64-Bit files
- RagePhoto.*: improve correctness
- ragephoto-*viewer: depend on ragephoto 0.6 or newer
2024-04-05 04:17:41 +02:00
Syping 1e160ee170 ragephoto_c: fix header inclusion
- CI: hardcore Emscripten version
2024-03-26 10:21:32 +01:00
Syping 18325f7c05 pyragephoto: add 'with' support 2024-03-25 13:43:10 +01:00
Syping c55b99e23f RagePhoto: dont take reference from save function 2024-03-25 13:15:26 +01:00
Syping 0611a368a6 RagePhoto: remove u8path in saveFile 2024-03-25 13:09:52 +01:00
Syping dd60f8d0b7 libragephoto: add ragephoto_getphotosigns function
- rename libragephoto.py to libragephoto_loader.py
- enforce set CXX standard for WASM build
2024-03-25 13:06:14 +01:00
Syping 1b7c95e735 CI: adjust CXX standards 2024-01-19 08:19:50 +01:00
Syping 1a80125802 CMakeLists and RagePhoto C file access improvements
- CMakeLists.txt: CMAKE_CXX_STANDARD -> RAGEPHOTO_CXX_STANDARD
- RagePhoto.c: Fix potential issues with file access
2024-01-18 19:53:47 +01:00
Syping e1cf6df258 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
2024-01-15 17:05:54 +01:00
Syping b94e66624b RagePhoto.c: add windows.h for RAGEPHOTO_BENCHMARK on Windows 2023-12-24 16:19:09 +01:00
Syping e62fbacea8 RagePhoto.c: add benchmark for Windows 2023-12-23 11:14:01 +01:00
Syping 1b7b3c977b libragephoto: add benchmark to C implementation
CMakeLists.txt: update RAGEPHOTO_BENCHMARK description
RagePhoto.c: add benchmark
RagePhoto.cpp: change benchmark clock from
std::chrono::high_resolution_clock to std::chrono::steady_clock
2023-12-10 07:57:15 +01:00
Syping 6046b7213a libragephoto: use zeroBuffer to improve efficiency 2023-12-06 02:38:13 +01:00
Syping 534d978e47 pyragephoto: ship libragephoto with Python Package 2023-11-16 00:07:43 +01:00
Syping 8b8da1c1ef ragephoto.py: change format variable to photoFormat 2023-11-09 20:35:28 +01:00
Syping 0f1cfe630b libragephoto: add ragephoto Python Package
- separate RagePhoto and RagePhoto-Extract sources
2023-11-09 20:17:37 +01:00
Syping 9a5bcabf8c libragephoto: improve get and serializer reliability
- get functions do not return NULL or nullptr anymore
- serializer checks for valid references to not crash
- tight buffers do not prematurely end writing anymore
- add zeroBuffer function for future nullbyte insertions
- IconvTest.cpp: remove some C-style casts
- RagePhoto.cpp: improve initializer reliability
- RagePhotoA.hpp: throw error when instance could not be allocated
- README.md: minor markdown fix
2023-10-30 14:31:50 +01:00
44 changed files with 1583 additions and 793 deletions

View File

@ -16,7 +16,7 @@ jobs:
sudo apt-get update -qq
sudo apt-get install cmake gcc g++ ninja-build -qq
- name: Configure CMake
run: cmake -B ${{github.workspace}}/build -DCMAKE_CXX_STANDARD=17 -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -GNinja
run: cmake -B ${{github.workspace}}/build -DRAGEPHOTO_CXX_STANDARD=17 -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -GNinja
- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
- name: Install

View File

@ -12,7 +12,9 @@ jobs:
with:
submodules: recursive
- name: Setup emsdk
uses: mymindstorm/setup-emsdk@v11
uses: mymindstorm/setup-emsdk@v14
with:
version: 3.1.47
- name: Install packages
run: |
sudo apt-get update -qq

View File

@ -12,13 +12,15 @@ jobs:
with:
submodules: recursive
- name: Setup emsdk
uses: mymindstorm/setup-emsdk@v11
uses: mymindstorm/setup-emsdk@v14
with:
version: 3.1.47
- name: Install packages
run: |
sudo apt-get update -qq
sudo apt-get install cmake ninja-build -qq
- name: Configure CMake
run: emcmake cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -GNinja
run: emcmake cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DRAGEPHOTO_CXX_STANDARD=11 -GNinja
- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
- name: Upload

View File

@ -21,7 +21,7 @@ jobs:
with:
arch: ${{matrix.arch}}
- name: Configure CMake
run: cmake -B ${{github.workspace}}/build -DCMAKE_CXX_STANDARD=17 -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DRAGEPHOTO_UNICODE=wincvt -GNinja
run: cmake -B ${{github.workspace}}/build -DRAGEPHOTO_CXX_STANDARD=17 -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DRAGEPHOTO_UNICODE=wincvt -GNinja
- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
- name: Install

View File

@ -1,6 +1,6 @@
#[[**************************************************************************
* 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,
* are permitted provided that the following conditions are met:
@ -17,14 +17,13 @@
****************************************************************************]]
cmake_minimum_required(VERSION 3.8)
project(ragephoto VERSION 0.5.1 LANGUAGES C CXX)
project(ragephoto VERSION 0.6.0 LANGUAGES C CXX)
include(GNUInstallDirs)
# RagePhoto CMake includes
include(cmake/cxxstd.cmake)
include(cmake/unicode.cmake)
# RagePhoto Top Level ON
# RagePhoto Top Level
if (${CMAKE_PROJECT_NAME} STREQUAL "ragephoto")
set(RPTL_ON ON)
else()
@ -35,77 +34,77 @@ endif()
option(RAGEPHOTO_C_LIBRARY "Build libragephoto as C library" OFF)
if (RAGEPHOTO_C_LIBRARY)
set(RAGEPHOTO_HEADERS
src/RagePhoto.h
src/RagePhotoA
src/RagePhotoA.hpp
src/RagePhotoB
src/RagePhotoB.hpp
src/RagePhotoLibrary.h
src/RagePhotoTypedefs.h
src/core/RagePhoto.h
src/core/ragephoto_c
src/core/ragephoto_c.hpp
src/core/RagePhoto
src/core/RagePhoto.hpp
src/core/RagePhotoLibrary.h
src/core/RagePhotoTypedefs.h
)
set(RAGEPHOTO_SOURCES
src/RagePhoto.c
src/core/RagePhoto.c
)
else()
set(RAGEPHOTO_HEADERS
src/RagePhoto
src/RagePhoto.hpp
src/RagePhotoB
src/RagePhotoB.hpp
src/RagePhotoLibrary.h
src/RagePhotoTypedefs.h
src/core/ragephoto_cxx
src/core/ragephoto_cxx.hpp
src/core/RagePhoto
src/core/RagePhoto.hpp
src/core/RagePhotoLibrary.h
src/core/RagePhotoTypedefs.h
)
set(RAGEPHOTO_SOURCES
src/RagePhoto.cpp
src/core/RagePhoto.cpp
)
endif()
# RagePhoto Library Type
option(RAGEPHOTO_STATIC "Build libragephoto as static library" OFF)
if (RAGEPHOTO_STATIC)
option(RAGEPHOTO_C_API "Build libragephoto with C API support" OFF)
set(LIBRAGEPHOTO_LIBTYPE LIBRAGEPHOTO_STATIC)
else()
option(RAGEPHOTO_C_API "Build libragephoto with C API support" ON)
set(LIBRAGEPHOTO_LIBTYPE LIBRAGEPHOTO_SHARED)
endif()
# RagePhoto Benchmark
option(RAGEPHOTO_BENCHMARK "Build with libragephoto benchmark (C++ only)" OFF)
if (RAGEPHOTO_BENCHMARK)
list(APPEND LIBRAGEPHOTO_DEFINES
RAGEPHOTO_BENCHMARK
)
endif()
option(RAGEPHOTO_BENCHMARK "Build with libragephoto benchmark" OFF)
# RagePhoto C API
# RagePhoto Debug
option(RAGEPHOTO_DEBUG "Build with libragephoto debug output" OFF)
# RagePhoto API
option(RAGEPHOTO_C_API "Build libragephoto with C API support" ON)
if (RAGEPHOTO_C_LIBRARY)
set(LIBRAGEPHOTO_API LIBRAGEPHOTO_C_ONLY)
else()
if (RAGEPHOTO_C_API)
set(LIBRAGEPHOTO_API LIBRAGEPHOTO_CXX_C)
list(APPEND RAGEPHOTO_HEADERS
src/RagePhoto.h
src/RagePhotoA
src/RagePhotoA.hpp
src/core/RagePhoto.h
src/core/ragephoto_c
src/core/ragephoto_c.hpp
)
else()
set(LIBRAGEPHOTO_API LIBRAGEPHOTO_CXX_ONLY)
endif()
endif()
# RagePhoto C++ Standard
set(RAGEPHOTO_CXX_STANDARD "11" CACHE STRING "libragephoto C++ standard")
# RagePhoto Win32 Shared Resources
if (WIN32)
string(TIMESTAMP ragephoto_BUILD_YEAR "%Y" UTC)
configure_file(src/ragephoto.rc.in "${ragephoto_BINARY_DIR}/resources/ragephoto.rc" @ONLY)
configure_file(src/core/ragephoto.rc.in "${ragephoto_BINARY_DIR}/resources/ragephoto.rc" @ONLY)
list(APPEND RAGEPHOTO_SHARED_RESOURCES
"${ragephoto_BINARY_DIR}/resources/ragephoto.rc"
)
endif()
# RagePhoto Configures + Target + Installs
configure_file(src/ragephoto.pc.in "${ragephoto_BINARY_DIR}/pkgconfig/ragephoto.pc" @ONLY)
configure_file(src/RagePhotoConfig.h.in "${ragephoto_BINARY_DIR}/include/RagePhotoConfig.h" @ONLY)
configure_file(src/core/ragephoto.pc.in "${ragephoto_BINARY_DIR}/pkgconfig/ragephoto.pc" @ONLY)
configure_file(src/core/RagePhotoConfig.h.in "${ragephoto_BINARY_DIR}/include/RagePhotoConfig.h" @ONLY)
list(APPEND RAGEPHOTO_HEADERS
"${ragephoto_BINARY_DIR}/include/RagePhotoConfig.h"
)
@ -119,16 +118,22 @@ else()
SOVERSION "${ragephoto_VERSION}"
)
endif()
set_target_properties(ragephoto PROPERTIES
CXX_STANDARD ${RAGEPHOTO_CXX_STANDARD}
CXX_STANDARD_REQUIRED ON
)
target_compile_definitions(ragephoto PRIVATE
LIBRAGEPHOTO_LIBRARY
${LIBRAGEPHOTO_DEFINES}
$<$<BOOL:${RAGEPHOTO_BENCHMARK}>:RAGEPHOTO_BENCHMARK>
$<$<BOOL:${RAGEPHOTO_DEBUG}>:RAGEPHOTO_DEBUG>
)
if (MSVC AND MSVC_VERSION GREATER_EQUAL 1914 AND NOT RAGEPHOTO_C_LIBRARY)
target_compile_options(ragephoto PRIVATE "/Zc:__cplusplus")
if (MSVC AND MSVC_VERSION GREATER_EQUAL 1914)
target_compile_options(ragephoto PRIVATE $<$<COMPILE_LANGUAGE:CXX>:/Zc:__cplusplus>)
endif()
target_include_directories(ragephoto PUBLIC
"${ragephoto_BINARY_DIR}/include"
"${ragephoto_SOURCE_DIR}/src"
"${ragephoto_SOURCE_DIR}/src/core"
)
install(TARGETS ragephoto
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
@ -167,14 +172,14 @@ option(RAGEPHOTO_EXTRACT "Build libragephoto with ragephoto-extract" ${RPTL_ON})
if (RAGEPHOTO_EXTRACT)
# RagePhoto-Extract Source files
if (RAGEPHOTO_C_API)
set(EXTRACT_SOURCES src/RagePhoto-Extract.c)
set(EXTRACT_SOURCES src/extract/RagePhoto-Extract.c)
else()
set(EXTRACT_SOURCES src/RagePhoto-Extract.cpp)
set(EXTRACT_SOURCES src/extract/RagePhoto-Extract.cpp)
endif()
# RagePhoto-Extract Win32 Shared Resources
if (WIN32)
string(TIMESTAMP ragephoto_BUILD_YEAR "%Y" UTC)
configure_file(src/ragephoto-extract.rc.in "${ragephoto_BINARY_DIR}/resources/ragephoto-extract.rc" @ONLY)
configure_file(src/extract/ragephoto-extract.rc.in "${ragephoto_BINARY_DIR}/resources/ragephoto-extract.rc" @ONLY)
list(APPEND EXTRACT_RESOURCES
"${ragephoto_BINARY_DIR}/resources/ragephoto-extract.rc"
)
@ -183,14 +188,51 @@ if (RAGEPHOTO_EXTRACT)
add_executable(ragephoto-extract ${RAGEPHOTO_HEADERS} ${EXTRACT_SOURCES} ${EXTRACT_RESOURCES})
set_target_properties(ragephoto-extract PROPERTIES
INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}"
CXX_STANDARD ${RAGEPHOTO_CXX_STANDARD}
CXX_STANDARD_REQUIRED ON
)
if (MSVC AND MSVC_VERSION GREATER_EQUAL 1914 AND NOT RAGEPHOTO_C_API)
target_compile_options(ragephoto-extract PRIVATE "/Zc:__cplusplus")
if (MSVC AND MSVC_VERSION GREATER_EQUAL 1914)
target_compile_options(ragephoto-extract PRIVATE $<$<COMPILE_LANGUAGE:CXX>:/Zc:__cplusplus>)
endif()
target_link_libraries(ragephoto-extract PRIVATE ragephoto)
install(TARGETS ragephoto-extract DESTINATION "${CMAKE_INSTALL_BINDIR}")
endif()
# RagePhoto Python Package
option(RAGEPHOTO_PYTHON "Create ragephoto Python Package" OFF)
if (RAGEPHOTO_PYTHON)
# Python Package Library file
if (WIN32)
set(PYRAGEPHOTO_LIBRARY "libragephoto.dll")
else()
set(PYRAGEPHOTO_LIBRARY "libragephoto.so")
endif()
# Generate Python Package Project files
configure_file(src/python/setup.py.in "${ragephoto_BINARY_DIR}/pyragephoto/setup.py" @ONLY)
configure_file(src/python/pyproject.toml.in "${ragephoto_BINARY_DIR}/pyragephoto/pyproject.toml" @ONLY)
configure_file(src/python/__version__.py.in "${ragephoto_BINARY_DIR}/pyragephoto/ragephoto/__version__.py" @ONLY)
# Python Package Source files + Target
set(PYRAGEPHOTO_SOURCES
"src/python/__init__.py"
"src/python/libragephoto_loader.py"
"src/python/ragephoto.py"
)
add_custom_target(pyragephoto SOURCES ${PYRAGEPHOTO_SOURCES})
# Copy Python Package to build directory
file(COPY ${PYRAGEPHOTO_SOURCES} DESTINATION "${ragephoto_BINARY_DIR}/pyragephoto/ragephoto")
# Python Package Bundle Settings
option(RAGEPHOTO_PYTHON_BUNDLE_LIBRARY "Bundle libragephoto with ragephoto Python Package" OFF)
if (RAGEPHOTO_PYTHON_BUNDLE_LIBRARY)
add_custom_command(
TARGET ragephoto
POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy "$<TARGET_FILE:ragephoto>" "${ragephoto_BINARY_DIR}/pyragephoto/ragephoto/${PYRAGEPHOTO_LIBRARY}"
BYPRODUCTS "${ragephoto_BINARY_DIR}/pyragephoto/ragephoto/${PYRAGEPHOTO_LIBRARY}"
VERBATIM
)
endif()
endif()
# CPack Package Generation
if (RPTL_ON)
include(InstallRequiredSystemLibraries)

View File

@ -16,10 +16,11 @@ sudo cmake --install libragephoto-build
```
##### Optional CMake flags
`-DCMAKE_CXX_STANDARD=17`
`-DRAGEPHOTO_CXX_STANDARD=17`
`-DRAGEPHOTO_BENCHMARK=ON`
`-DRAGEPHOTO_C_API=OFF`
`-DRAGEPHOTO_C_LIBRARY=ON`
`-DRAGEPHOTO_DEBUG=ON`
`-DRAGEPHOTO_DOC=ON`
`-DRAGEPHOTO_EXAMPLE_GTKVIEWER=ON`
`-DRAGEPHOTO_EXAMPLE_QTVIEWER=ON`

View File

@ -1,21 +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.
****************************************************************************]]
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 11 CACHE STRING "libragephoto C++ standard")
set(CMAKE_CXX_STANDARD_REQUIRED ON)

View File

@ -1,6 +1,6 @@
#[[**************************************************************************
* libragephoto RAGE Photo Parser
* Copyright (C) 2023 Syping
* Copyright (C) 2023-2024 Syping
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
@ -17,8 +17,13 @@
****************************************************************************]]
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.13.0")
add_executable(ragephoto-wasm ${RAGEPHOTO_HEADERS} ${RAGEPHOTO_SOURCES})
add_executable(ragephoto-wasm
${RAGEPHOTO_HEADERS}
${RAGEPHOTO_SOURCES}
)
set_target_properties(ragephoto-wasm PROPERTIES
CXX_STANDARD ${RAGEPHOTO_CXX_STANDARD}
CXX_STANDARD_REQUIRED ON
PREFIX "lib"
OUTPUT_NAME "ragephoto"
)
@ -40,7 +45,7 @@ if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.13.0")
)
target_include_directories(ragephoto-wasm PUBLIC
"${ragephoto_BINARY_DIR}/include"
"${ragephoto_SOURCE_DIR}/src"
"${ragephoto_SOURCE_DIR}/src/core"
)
else()
message(WARNING "A useable WebAssembly build needs at least CMake 3.13.0 or newer")

View File

@ -1,6 +1,6 @@
#[[**************************************************************************
* libragephoto RAGE Photo Parser
* Copyright (C) 2021 Syping
* 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:
@ -24,12 +24,13 @@ if (NOT DOXYGEN_FOUND)
message(FATAL_ERROR "Doxygen is needed to build the documentation.")
endif()
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in" "${CMAKE_CURRENT_BINARY_DIR}/Doxyfile" @ONLY)
add_custom_target(
doc ALL
COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND "${DOXYGEN_EXECUTABLE}" "${CMAKE_CURRENT_BINARY_DIR}/Doxyfile"
COMMENT "Generate API documentation with Doxygen"
SOURCES "build.dox" "index.dox" "usage.dox"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
VERBATIM
)
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html DESTINATION ${CMAKE_INSTALL_DOCDIR})
install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/html" DESTINATION "${CMAKE_INSTALL_DOCDIR}")

View File

@ -1,16 +1,18 @@
PROJECT_NAME = "libragephoto"
PROJECT_NUMBER = "Version: @ragephoto_VERSION@"
INPUT = "@CMAKE_CURRENT_SOURCE_DIR@/index.doc" \
"@CMAKE_CURRENT_SOURCE_DIR@/build.doc" \
"@CMAKE_CURRENT_SOURCE_DIR@/usage.doc" \
"src"
INPUT = "src/core" \
"@CMAKE_CURRENT_SOURCE_DIR@/index.dox" \
"@CMAKE_CURRENT_SOURCE_DIR@/build.dox" \
"@CMAKE_CURRENT_SOURCE_DIR@/usage.dox"
OUTPUT_DIRECTORY = "@CMAKE_CURRENT_BINARY_DIR@"
FULL_PATH_NAMES = NO
EXTRACT_PRIVATE = NO
ENABLE_PREPROCESSING = YES
MACRO_EXPANSION = YES
EXPAND_ONLY_PREDEF = YES
EXPAND_ONLY_PREDEF = NO
PREDEFINED = "__cplusplus=201703L" \
"RAGEPHOTO_CXX_STD=17" \
"@LIBRAGEPHOTO_API@" \
"LIBRAGEPHOTO_C_PUBLIC=" \
"LIBRAGEPHOTO_CXX_PUBLIC="
"LIBRAGEPHOTO_CXX_PUBLIC=" \
"INT32_C(val)=val" \
"UINT32_C(val)=val##U"

View File

@ -17,10 +17,11 @@ To customise your libragephoto build, the following <a href="#flags">Optional CM
<h4 id="flags">Optional CMake flags</h4>
\code{.sh}
-DCMAKE_CXX_STANDARD=17
-DRAGEPHOTO_CXX_STANDARD=17
-DRAGEPHOTO_BENCHMARK=ON
-DRAGEPHOTO_C_API=OFF
-DRAGEPHOTO_C_LIBRARY=ON
-DRAGEPHOTO_DEBUG=ON
-DRAGEPHOTO_DOC=ON
-DRAGEPHOTO_EXAMPLE_GTKVIEWER=ON
-DRAGEPHOTO_EXAMPLE_QTVIEWER=ON

View File

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

View File

@ -2,15 +2,13 @@
<h3 id="api_cxx">C++ API</h3>
<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)
<h4 id="cxx_include">Including RagePhoto</h4>
\code{.cpp}
#include <RagePhoto>
\endcode
Create a RagePhoto object
Initializing a RagePhoto object
\code{.cpp}
RagePhoto ragePhoto;
@ -21,16 +19,14 @@ RagePhoto ragePhoto;
From a file using RagePhoto::loadFile
\code{.cpp}
const char* filename = "PGTA5123456789";
const bool loaded = ragePhoto.loadFile(filename);
const bool loaded = ragePhoto.loadFile("PGTA5123456789");
\endcode
From a file using RagePhoto::load(const std::string&)
\code{.cpp}
// Reading file
const char* filename = "PGTA5123456789";
std::ifstream ifs(filename, std::ios::in | std::ios::binary);
std::ifstream ifs("PGTA5123456789", std::ios::in | std::ios::binary);
if (!ifs.is_open())
return;
std::string sdata(std::istreambuf_iterator<char>{ifs}, {});
@ -46,7 +42,7 @@ From a char* using RagePhoto::load(const char*, size_t)
const bool loaded = ragePhoto.load(data, size);
\endcode
<h4 id="cxx_using">Using a Photo</h4>
<h4 id="cxx_using">Querying Photo data</h4>
\code{.cpp}
// Returns the Photo Format
@ -85,64 +81,21 @@ Saving the JPEG from a Photo
\code{.cpp}
// Example saveJpeg function
bool saveJpeg(RagePhoto* ragePhoto, const std::string& filename) {
bool saveJpeg(RagePhoto &ragePhoto, const std::string &filename) {
std::ofstream ofs(filename, std::ios::out | std::ios::binary | std::ios::trunc);
if (!ofs.is_open())
return false;
ofs << ragePhoto->jpeg();
ofs << ragePhoto.jpeg();
const bool saved = ofs.good();
ofs.close();
return saved;
}
// Using the saveJpeg function
const char* filename = "PGTA5123456789.jpg";
const bool saved = saveJpeg(&ragePhoto, filename);
const bool saved = saveJpeg(ragePhoto, "photo.jpg");
\endcode
Using the JPEG in GTK+ (gtkmm)
\code{.cpp}
// Writing pixbuf loader
GdkPixbufLoader* pixbuf_loader = gdk_pixbuf_loader_new();
gdk_pixbuf_loader_write(pixbuf_loader, reinterpret_cast<const guchar*>(ragePhoto.jpegData()), ragePhoto.jpegSize(), nullptr);
GdkPixbuf* pixbuf = gdk_pixbuf_loader_get_pixbuf(pixbuf_loader);
gdk_pixbuf_loader_close(pixbuf_loader, nullptr);
// Set image
Gtk::Image image;
image.set(Glib::wrap(pixbuf));
\endcode
Using the JPEG in Qt
\code{.cpp}
// Returns the JPEG as QImage
const QImage image = QImage::fromData(QByteArray::fromRawData(ragePhoto.jpegData(), ragePhoto.jpegSize()), "JPEG");
// Loading the JPEG in QImage
QImage image;
const bool loaded = image.loadFromData(QByteArray::fromRawData(ragePhoto.jpegData(), ragePhoto.jpegSize()), "JPEG");
\endcode
Using the JSON in Boost.JSON
\code{.cpp}
boost::json::error_code ec;
const boost::json::value jv = boost::json::parse(ragePhoto.json(), ec);
if (ec)
return;
\endcode
Using the JSON in Qt
\code{.cpp}
const QJsonDocument jd = QJsonDocument::fromJson(ragePhoto.json());
if (jd.isNull())
return;
\endcode
<h4 id="cxx_error">Detect Photo errors</h4>
<h4 id="cxx_error">Querying last error</h4>
\code{.cpp}
const int32_t error = ragePhoto.error();
@ -164,6 +117,110 @@ default:
Available error codes: RagePhoto::Error
<h3 id="api_c">C API</h3>
<h4 id="c_include">Including RagePhoto</h4>
\code{.c}
#include <RagePhoto.h>
\endcode
Initializing a RagePhoto instance
\code{.c}
ragephoto_t instance = ragephoto_open();
\endcode
Destroying a RagePhoto instance
\code{.c}
ragephoto_close(instance);
\endcode
<h4 id="c_loading">Loading a Photo</h4>
From a file using ragephoto_loadfile(ragephoto_t, const char*)
\code{.c}
const bool loaded = ragephoto_loadfile(instance, "PGTA5123456789");
\endcode
From a char* using ragephoto_load(ragephoto_t, const char*, size_t)
\code{.c}
const bool loaded = ragephoto_load(instance, data, size);
\endcode
<h4 id="c_using">Querying Photo data</h4>
\code{.c}
// Returns the Photo Format
const uint32_t format = ragephoto_getphotoformat(instance);
// Returns the JPEG as const char*
const char* jpeg = ragephoto_getphotojpeg(instance);
const uint32_t size = ragephoto_getphotosize(instance);
// Returns the JSON
const char* json = ragephoto_getphotojson(instance);
// Returns the Title
const char* title = ragephoto_getphototitle(instance);
\endcode
Detecting if Photo is from GTA V or RDR 2
\code{.c}
switch (ragephoto_getphotoformat(instance)) {
case RAGEPHOTO_FORMAT_GTA5:
printf("GTA V format detected\n");
break;
case RAGEPHOTO_FORMAT_RDR2:
printf("RDR 2 format detected\n");
break;
default:
printf("Unknown format detected\n");
}
\endcode
Saving the JPEG from a Photo
\code{.c}
// Example saveJpeg function
bool saveJpeg(ragephoto_t instance, const char* filename) {
FILE* file = fopen(filename, "wb");
if (!file)
return false;
const uint32_t jpegSize = ragephoto_getphotosize(instance);
const size_t writeSize = fwrite(ragephoto_getphotojpeg(instance), sizeof(char), jpegSize, file);
fclose(file);
return (jpegSize == writeSize);
}
// Using the saveJpeg function
const bool saved = saveJpeg(instance, "photo.jpg");
\endcode
<h4 id="c_error">Querying last error</h4>
\code{.c}
const int32_t error = ragephoto_error(instance);
switch (error) {
case RAGEPHOTO_ERROR_NOFORMATIDENTIFIER:
printf("No format identifier\n");
break;
case RAGEPHOTO_ERROR_INCOMPATIBLEFORMAT:
printf("Incompatible format\n");
break;
// Detect for more errors here...
case RAGEPHOTO_ERROR_NOERROR:
printf("No error detected\n");
break;
default:
printf("Unknown error detected\n");
}
\endcode
<h3 id="cmake">Including libragephoto in a CMake project</h3>
<h4 id="cmake_pkgconfig">Using PkgConfig</h4>

View File

@ -35,7 +35,7 @@ if (TARGET ragephoto)
set(RAGEPHOTO_LIBRARIES ragephoto)
set(RAGEPHOTO_LIBRARY_DIRS ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
else()
pkg_check_modules(RAGEPHOTO REQUIRED ragephoto)
pkg_check_modules(RAGEPHOTO REQUIRED ragephoto>=0.6)
endif()
add_executable(ragephoto-gtkviewer WIN32 ${GTKVIEWER_SOURCES})

View File

@ -16,7 +16,7 @@
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
#include <RagePhotoB>
#include <RagePhoto>
#include <gtkmm/application.h>
#include <gtkmm/box.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()) {
std::string data(std::istreambuf_iterator<char>{ifs}, {});
ifs.close();
RagePhotoB ragePhoto;
RagePhoto ragePhoto;
const bool loaded = ragePhoto.load(data);
if (!loaded) {
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);
msg.set_title("Open Photo");
msg.run();

View File

@ -41,7 +41,7 @@ if (TARGET ragephoto)
set(RAGEPHOTO_LIBRARY_DIRS ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
else()
find_package(PkgConfig REQUIRED)
pkg_check_modules(RAGEPHOTO REQUIRED ragephoto)
pkg_check_modules(RAGEPHOTO REQUIRED ragephoto>=0.6)
endif()
add_executable(ragephoto-qtviewer WIN32 ${QTVIEWER_SOURCES})

View File

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

View File

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

View File

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

View File

@ -1,143 +0,0 @@
/*****************************************************************************
* libragephoto RAGE Photo Parser
* Copyright (C) 2021-2023 Syping
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 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 RAGEPHOTOTYPEDEFS_H
#define RAGEPHOTOTYPEDEFS_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
/** RagePhoto data struct for storing internal data. */
typedef struct RagePhotoData {
char* jpeg; /**< Pointer for internal JPEG buffer */
char* description; /**< Pointer for internal Description buffer */
char* json; /**< Pointer for internal JSON buffer */
char* header; /**< Pointer for internal Header buffer */
char* title; /**< Pointer for internal Title buffer */
int32_t error; /**< RagePhoto error code */
uint32_t descBuffer; /**< Description buffer length */
uint32_t descOffset; /**< Description buffer offset */
uint32_t endOfFile; /**< End Of File offset */
uint32_t headerSum; /**< Checksum of the header 1 */
uint32_t headerSum2; /**< Checksum of the header 2 (RDR 2 only) */
uint32_t jpegBuffer; /**< JPEG buffer length */
uint32_t jpegSize; /**< Internal JPEG buffer length and size of JPEG */
uint32_t jsonBuffer; /**< JSON buffer length */
uint32_t jsonOffset; /**< JSON buffer offset */
uint32_t photoFormat; /**< Photo file format magic */
uint32_t titlBuffer; /**< Title buffer length */
uint32_t titlOffset; /**< Title buffer offset */
} RagePhotoData;
/** RagePhoto load function typedef. */
typedef bool (*ragephoto_loadfunc_t)(RagePhotoData*, const char*, size_t);
/** RagePhoto save function typedef (char* allocated by caller). */
typedef bool (*ragephoto_savefunc_t)(RagePhotoData*, char*, uint32_t);
/** RagePhoto save function typedef (char* allocated by function). */
typedef bool (*ragephoto_savepfunc_t)(RagePhotoData*, char**, uint32_t);
/** RagePhoto saveSize function typedef. */
typedef size_t (*ragephoto_saveszfunc_t)(RagePhotoData*, uint32_t);
/** RagePhoto format parser struct for registering custom formats. */
typedef struct RagePhotoFormatParser {
uint32_t photoFormat; /**< Photo file format magic */
ragephoto_loadfunc_t funcLoad; /**< Pointer to load function */
ragephoto_savefunc_t funcSave; /**< Pointer to save function */
ragephoto_savepfunc_t funcSavep; /**< Pointer to savep function */
ragephoto_saveszfunc_t funcSaveSz; /**< Pointer to saveSize function */
} RagePhotoFormatParser;
/** RagePhoto instance struct for storing data and format parser pointer. */
typedef struct RagePhotoInstance {
RagePhotoData *data; /**< Pointer for data */
RagePhotoFormatParser *parser; /**< Pointer for format parser */
} RagePhotoInstance;
/* RagePhoto default sizes */
#define RAGEPHOTO_DEFAULT_GTA5_PHOTOBUFFER 524288UL /**< GTA V default Photo Buffer Size */
#define RAGEPHOTO_DEFAULT_RDR2_PHOTOBUFFER 1048576UL /**< RDR 2 default Photo Buffer Size */
#define RAGEPHOTO_DEFAULT_DESCBUFFER 256UL /**< Default Description Buffer Size */
#define RAGEPHOTO_DEFAULT_JSONBUFFER 3072UL /**< Default JSON Buffer Size */
#define RAGEPHOTO_DEFAULT_TITLBUFFER 256UL /**< Default Title Buffer Size */
#define RAGEPHOTO_GTA5_HEADERSIZE 264UL /**< GTA V Header Size */
#define RAGEPHOTO_RDR2_HEADERSIZE 272UL /**< RDR 2 Header Size */
/* RagePhoto error codes */
#define RAGEPHOTO_ERROR_DESCBUFFERTIGHT 39L /**< Description Buffer is too tight */
#define RAGEPHOTO_ERROR_DESCMALLOCERROR 31L /**< Description Buffer can't be allocated */
#define RAGEPHOTO_ERROR_DESCREADERROR 32L /**< Description can't be read successfully */
#define RAGEPHOTO_ERROR_HEADERBUFFERTIGHT 35L /**< Header Buffer is too tight */
#define RAGEPHOTO_ERROR_HEADERMALLOCERROR 4L /**< Header Buffer can't be allocated */
#define RAGEPHOTO_ERROR_INCOMPATIBLEFORMAT 2L /**< Format is incompatible */
#define RAGEPHOTO_ERROR_INCOMPLETECHECKSUM 7L /**< Header checksum is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEDESCBUFFER 30L /**< Description Buffer Size is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEDESCMARKER 28L /**< Description Marker is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEDESCOFFSET 11L /**< Description Offset is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEEOF 8L /**< End Of File Offset is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEHEADER 3L /**< Header is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEJENDMARKER 33L /**< JEND Marker is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEJPEGMARKER 12L /**< JPEG Marker is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEJSONBUFFER 20L /**< JSON Buffer Size is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEJSONMARKER 18L /**< JSON Marker incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEJSONOFFSET 9L /**< JSON Offset incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEPHOTOBUFFER 14L /**< Photo Buffer Size is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEPHOTOSIZE 15L /**< Photo Size is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETETITLEBUFFER 25L /**< Title Buffer Size is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETETITLEMARKER 23L /**< Title Marker is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETETITLEOFFSET 10L /**< Title Offset is incomplete */
#define RAGEPHOTO_ERROR_INCORRECTDESCMARKER 29L /**< Description Marker is incorrect */
#define RAGEPHOTO_ERROR_INCORRECTJENDMARKER 34L /**< JEND Marker is incorrect */
#define RAGEPHOTO_ERROR_INCORRECTJPEGMARKER 13L /**< JPEG Marker is incorrect */
#define RAGEPHOTO_ERROR_INCORRECTJSONMARKER 19L /**< JSON Marker is incorrect */
#define RAGEPHOTO_ERROR_INCORRECTTITLEMARKER 24L /**< Title Marker is incorrect */
#define RAGEPHOTO_ERROR_JSONBUFFERTIGHT 37L /**< JSON Buffer is too tight */
#define RAGEPHOTO_ERROR_JSONMALLOCERROR 21L /**< JSON Buffer can't be allocated */
#define RAGEPHOTO_ERROR_JSONREADERROR 22L /**< JSON can't be read successfully */
#define RAGEPHOTO_ERROR_NOERROR 255L /**< Finished without errors */
#define RAGEPHOTO_ERROR_NOFORMATIDENTIFIER 1L /**< No format detected, empty file */
#define RAGEPHOTO_ERROR_PHOTOBUFFERTIGHT 36L /**< Photo Buffer is too tight */
#define RAGEPHOTO_ERROR_PHOTOMALLOCERROR 16L /**< Photo Buffer can't be allocated */
#define RAGEPHOTO_ERROR_PHOTOREADERROR 17L /**< Photo can't be read */
#define RAGEPHOTO_ERROR_TITLEBUFFERTIGHT 38L /**< Title Buffer is too tight */
#define RAGEPHOTO_ERROR_TITLEMALLOCERROR 26L /**< Title Buffer can't be allocated */
#define RAGEPHOTO_ERROR_TITLEREADERROR 27L /**< Title can't be read */
#define RAGEPHOTO_ERROR_UNICODEINITERROR 5L /**< Failed to initialise Unicode decoder */
#define RAGEPHOTO_ERROR_UNICODEHEADERERROR 6L /**< Header can't be encoded/decoded successfully */
#define RAGEPHOTO_ERROR_UNINITIALISED 0L /**< Uninitialised, file access failed */
/* RagePhoto formats */
#define RAGEPHOTO_FORMAT_GTA5 0x01000000UL /**< GTA V Photo Format */
#define RAGEPHOTO_FORMAT_RDR2 0x04000000UL /**< RDR 2 Photo Format */
/* RagePhoto sign initials */
#define RAGEPHOTO_SIGNINITIAL_GTA5 0xE47AB81CUL /**< GTA V Sign Initial */
#define RAGEPHOTO_SIGNINITIAL_RDR2 0x00FEEB1EUL /**< RDR 2 Sign Initial */
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // RAGEPHOTOTYPEDEFS_H

View File

@ -1,6 +1,6 @@
/*****************************************************************************
* 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,
* are permitted provided that the following conditions are met:
@ -22,6 +22,15 @@
#include <stdio.h>
#include <string.h>
#ifdef RAGEPHOTO_BENCHMARK
#ifdef _WIN32
#include <windows.h>
#include <profileapi.h>
#else
#include <time.h>
#endif
#endif
#if defined(UNICODE_ICONV)
#include <iconv.h>
#elif defined(UNICODE_WINCVT)
@ -29,6 +38,10 @@
#include <stringapiset.h>
#endif
/* RAGEPHOTO LIBRARY GLOBALS */
int libraryflags = 0;
const char* nullchar = "";
/* BEGIN OF STATIC LIBRARY FUNCTIONS */
inline size_t readBuffer(const char *input, void *output, size_t *pos, size_t outputLen, size_t inputLen)
{
@ -56,13 +69,26 @@ inline size_t writeBuffer(const void *input, char *output, size_t *pos, size_t o
return writeLen;
}
inline size_t zeroBuffer(char *output, size_t *pos, size_t outputLen, size_t inputLen)
{
const size_t maxLen = outputLen - *pos;
size_t zeroLen = inputLen;
if (*pos >= outputLen)
return 0;
if (inputLen > maxLen)
zeroLen = maxLen;
memset(&output[*pos], 0, zeroLen);
*pos = *pos + zeroLen;
return zeroLen;
}
inline bool writeDataChar(const char *input, char **output)
{
const size_t src_s = strlen(input) + 1;
if (*output) {
const size_t dst_s = strlen(*output) + 1;
if (dst_s > src_s) {
char *t_output = (char*)(realloc(*output, src_s));
char *t_output = (char*)realloc(*output, src_s);
if (!t_output) {
return false;
}
@ -70,7 +96,7 @@ inline bool writeDataChar(const char *input, char **output)
memcpy(*output, input, src_s);
}
else if (dst_s < src_s) {
char *t_output = (char*)(malloc(src_s));
char *t_output = (char*)malloc(src_s);
if (!t_output) {
return false;
}
@ -83,7 +109,7 @@ inline bool writeDataChar(const char *input, char **output)
}
}
else {
char *t_output = (char*)(malloc(src_s));
char *t_output = (char*)malloc(src_s);
if (!t_output) {
return false;
}
@ -114,12 +140,12 @@ inline uint32_t joaatFromInitial(const char *data, size_t size, uint32_t init_va
uint32_t val = init_val;
for (size_t i = 0; i != size; i++) {
val += data[i];
val += (val << 10);
val ^= (val >> 6);
val += val << 10;
val ^= val >> 6;
}
val += (val << 3);
val ^= (val >> 11);
val += (val << 15);
val += val << 3;
val ^= val >> 11;
val += val << 15;
return val;
}
/* END OF STATIC LIBRARY FUNCTIONS */
@ -127,16 +153,16 @@ inline uint32_t joaatFromInitial(const char *data, size_t size, uint32_t init_va
/* BEGIN OF RAGEPHOTO CLASS */
ragephoto_t ragephoto_open()
{
RagePhotoInstance *instance = (RagePhotoInstance*)(malloc(sizeof(RagePhotoInstance)));
RagePhotoInstance *instance = (RagePhotoInstance*)malloc(sizeof(RagePhotoInstance));
if (!instance)
return NULL;
instance->data = (RagePhotoData*)(malloc(sizeof(RagePhotoData)));
instance->data = (RagePhotoData*)malloc(sizeof(RagePhotoData));
if (!instance->data) {
free(instance);
return NULL;
}
memset(instance->data, 0, sizeof(RagePhotoData));
instance->parser = (RagePhotoFormatParser*)(malloc(sizeof(RagePhotoFormatParser)));
instance->parser = (RagePhotoFormatParser*)malloc(sizeof(RagePhotoFormatParser));
if (!instance->parser) {
free(instance->data);
free(instance);
@ -170,12 +196,12 @@ void ragephoto_addparser(ragephoto_t instance_t, RagePhotoFormatParser *rp_parse
return;
size_t length;
for (length = 0; memcmp(&n_parser, &instance->parser[length], sizeof(RagePhotoFormatParser)); length++);
RagePhotoFormatParser *t_parser = (RagePhotoFormatParser*)(realloc(instance->parser, (length + 2 * sizeof(RagePhotoFormatParser))));
RagePhotoFormatParser *t_parser = (RagePhotoFormatParser*)realloc(instance->parser, (length + 2 * sizeof(RagePhotoFormatParser)));
if (!t_parser)
return;
instance->parser = t_parser;
memcpy(&instance->parser[length], rp_parser, sizeof(RagePhotoFormatParser));
memset(&instance->parser[length+1], 0, sizeof(RagePhotoFormatParser));
memset(&instance->parser[length + 1], 0, sizeof(RagePhotoFormatParser));
}
}
@ -204,6 +230,17 @@ RagePhotoData* ragephoto_getphotodata(ragephoto_t instance_t)
bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser, const char *data, size_t length)
{
#ifdef RAGEPHOTO_BENCHMARK
#ifdef _WIN32
LARGE_INTEGER freq, benchmark_parse_start, benchmark_parse_end;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&benchmark_parse_start);
#else
struct timespec benchmark_parse_start, benchmark_parse_end;
clock_gettime(CLOCK_MONOTONIC, &benchmark_parse_start);
#endif
#endif
// Avoid data conflicts
ragephotodata_clear(rp_data);
@ -235,7 +272,7 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
rp_data->error = RAGEPHOTO_ERROR_UNICODEINITERROR; // 4
return false;
}
rp_data->header = (char*)(malloc(256));
rp_data->header = (char*)malloc(256);
if (!rp_data->header) {
rp_data->error = RAGEPHOTO_ERROR_HEADERMALLOCERROR; // 4
iconv_close(iconv_in);
@ -247,17 +284,17 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
char *dst = rp_data->header;
const size_t ret = iconv(iconv_in, &src, &src_s, &dst, &dst_s);
iconv_close(iconv_in);
if (ret == (size_t)(-1)) {
if (ret == (size_t)-1) {
rp_data->error = RAGEPHOTO_ERROR_UNICODEHEADERERROR; // 6
return false;
}
#elif defined(UNICODE_WINCVT)
rp_data->header = (char*)(malloc(256));
rp_data->header = (char*)malloc(256);
if (!rp_data->header) {
rp_data->error = RAGEPHOTO_ERROR_HEADERMALLOCERROR; // 4
return false;
}
const int converted = WideCharToMultiByte(CP_UTF8, 0, (wchar_t*)(photoHeader), -1, rp_data->header, 256, NULL, NULL);
const int converted = WideCharToMultiByte(CP_UTF8, 0, (wchar_t*)photoHeader, -1, rp_data->header, 256, NULL, NULL);
if (converted == 0) {
free(rp_data->header);
rp_data->header = NULL;
@ -302,7 +339,6 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
rp_data->headerSum2 = charToUInt32LE(uInt32Buffer);
#endif
}
const size_t headerSize = pos;
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
if (size != 4) {
@ -380,7 +416,7 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
rp_data->jpegSize = charToUInt32LE(uInt32Buffer);
#endif
rp_data->jpeg = (char*)(malloc(rp_data->jpegSize));
rp_data->jpeg = (char*)malloc(rp_data->jpegSize);
if (!rp_data->jpeg) {
rp_data->error = RAGEPHOTO_ERROR_PHOTOMALLOCERROR; // 16
return false;
@ -393,7 +429,7 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
return false;
}
pos = rp_data->jsonOffset + headerSize;
pos += rp_data->jpegBuffer - rp_data->jpegSize;
size = readBuffer(data, markerBuffer, &pos, 4, length);
if (size != 4) {
rp_data->error = RAGEPHOTO_ERROR_INCOMPLETEJSONMARKER; // 18
@ -415,7 +451,7 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
rp_data->jsonBuffer = charToUInt32LE(uInt32Buffer);
#endif
rp_data->json = (char*)(malloc(rp_data->jsonBuffer));
rp_data->json = (char*)malloc(rp_data->jsonBuffer);
if (!rp_data->json) {
rp_data->error = RAGEPHOTO_ERROR_JSONMALLOCERROR; // 21
return false;
@ -428,7 +464,6 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
return false;
}
pos = rp_data->titlOffset + headerSize;
size = readBuffer(data, markerBuffer, &pos, 4, length);
if (size != 4) {
rp_data->error = RAGEPHOTO_ERROR_INCOMPLETETITLEMARKER; // 23
@ -450,7 +485,7 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
rp_data->titlBuffer = charToUInt32LE(uInt32Buffer);
#endif
rp_data->title = (char*)(malloc(rp_data->titlBuffer));
rp_data->title = (char*)malloc(rp_data->titlBuffer);
if (!rp_data->title) {
rp_data->error = RAGEPHOTO_ERROR_TITLEMALLOCERROR; // 26
return false;
@ -463,7 +498,6 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
return false;
}
pos = rp_data->descOffset + headerSize;
size = readBuffer(data, markerBuffer, &pos, 4, length);
if (size != 4) {
rp_data->error = RAGEPHOTO_ERROR_INCOMPLETEDESCMARKER; // 28
@ -485,7 +519,7 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
rp_data->descBuffer = charToUInt32LE(uInt32Buffer);
#endif
rp_data->description = (char*)(malloc(rp_data->descBuffer));
rp_data->description = (char*)malloc(rp_data->descBuffer);
if (!rp_data->description) {
rp_data->error = RAGEPHOTO_ERROR_DESCMALLOCERROR; // 31
return false;
@ -498,7 +532,6 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
return false;
}
pos = rp_data->endOfFile + headerSize - 4;
size = readBuffer(data, markerBuffer, &pos, 4, length);
if (size != 4) {
rp_data->error = RAGEPHOTO_ERROR_INCOMPLETEJENDMARKER; // 33
@ -509,30 +542,39 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
return false;
}
#ifdef RAGEPHOTO_BENCHMARK
#ifdef _WIN32
QueryPerformanceCounter(&benchmark_parse_end);
const uint64_t benchmark_ns = (benchmark_parse_end.QuadPart - benchmark_parse_start.QuadPart) * INT64_C(1000000000) / freq.QuadPart;
#else
clock_gettime(CLOCK_MONOTONIC, &benchmark_parse_end);
const uint64_t benchmark_ns = (UINT64_C(1000000000) * benchmark_parse_end.tv_sec + benchmark_parse_end.tv_nsec) -
(UINT64_C(1000000000) * benchmark_parse_start.tv_sec + benchmark_parse_start.tv_nsec);
#endif
printf("Benchmark: %" PRIu64 "ns\n", benchmark_ns);
#endif
#ifdef RAGEPHOTO_DEBUG
const uint32_t jsonOffset = rp_data->jpegBuffer + UINT32_C(28);
const uint32_t titlOffset = jsonOffset + rp_data->jsonBuffer + UINT32_C(8);
const uint32_t descOffset = titlOffset + rp_data->titlBuffer + UINT32_C(8);
const uint32_t endOfFile = descOffset + rp_data->descBuffer + UINT32_C(12);
printf("header: %s\n", rp_data->header);
printf("headerSum: %" PRIu32 "\n", rp_data->headerSum);
printf("headerSum2: %" PRIu32 "\n", rp_data->headerSum2);
printf("photoBuffer: %" PRIu32 "\n", rp_data->jpegBuffer);
printf("descBuffer: %" PRIu32 "\n", rp_data->descBuffer);
printf("descOffset: %" PRIu32 "\n", rp_data->descOffset);
printf("descOffset: %" PRIu32 " (%" PRIu32 ")\n", rp_data->descOffset, descOffset);
printf("description: %s\n", rp_data->description);
printf("jsonBuffer: %" PRIu32 "\n", rp_data->jsonBuffer);
printf("jsonOffset: %" PRIu32 "\n", rp_data->jsonOffset);
printf("jsonOffset: %" PRIu32 " (%" PRIu32 ")\n", rp_data->jsonOffset, jsonOffset);
printf("json: %s\n", rp_data->json);
printf("sign: %" PRIu64 "\n", ragephotodata_getphotosign(rp_data));
printf("titlBuffer: %" PRIu32 "\n", rp_data->titlBuffer);
printf("titlOffset: %" PRIu32 "\n", rp_data->titlOffset);
printf("titlOffset: %" PRIu32 " (%" PRIu32 ")\n", rp_data->titlOffset, titlOffset);
printf("title: %s\n", rp_data->title);
printf("eofOffset: %" PRIu32 "\n", rp_data->endOfFile);
printf("ragephotodata_setbufferoffsets()\n");
ragephotodata_setbufferoffsets(rp_data);
printf("descOffset: %" PRIu32 "\n", rp_data->descOffset);
printf("jsonOffset: %" PRIu32 "\n", rp_data->jsonOffset);
printf("titlOffset: %" PRIu32 "\n", rp_data->titlOffset);
printf("eofOffset: %" PRIu32 "\n", rp_data->endOfFile);
printf("calc size: %zu\n", ragephotodata_getsavesize(rp_data, rp_parser));
printf("real size: %zu\n", length);
printf("eofOffset: %" PRIu32 " (%" PRIu32 ")\n", rp_data->endOfFile, endOfFile);
printf("size: %zu / %zu\n", length, ragephotodata_getsavesize(rp_data, NULL));
#endif
rp_data->error = RAGEPHOTO_ERROR_NOERROR; // 255
@ -565,7 +607,7 @@ bool ragephoto_load(ragephoto_t instance_t, const char *data, size_t size)
bool ragephoto_loadfile(ragephoto_t instance_t, const char *filename)
{
RagePhotoInstance *instance = (RagePhotoInstance*)instance_t;
#ifdef _WIN32
#if defined(_WIN32)
FILE *file = NULL;
fopen_s(&file, filename, "rb");
#else
@ -573,29 +615,47 @@ bool ragephoto_loadfile(ragephoto_t instance_t, const char *filename)
#endif
if (!file)
return false;
const int64_t fseek_end = fseek(file, 0, SEEK_END);
if (fseek_end == -1) {
#if defined(_WIN64)
int fseek_ret = _fseeki64(file, 0, SEEK_END);
#elif (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
int fseek_ret = fseeko(file, 0, SEEK_END);
#else
int fseek_ret = fseek(file, 0, SEEK_END);
#endif
if (fseek_ret == -1) {
fclose(file);
return false;
}
const int64_t fileSize = ftell(file);
#if defined(_WIN64)
const long long fileSize = _ftelli64(file);
#elif (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
const off_t fileSize = ftello(file);
#else
const long fileSize = ftell(file);
#endif
if (fileSize == -1) {
fclose(file);
return false;
}
const int64_t fseek_set = fseek(file, 0, SEEK_SET);
if (fseek_set == -1) {
#if defined(_WIN64)
fseek_ret = _fseeki64(file, 0, SEEK_SET);
#elif (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
fseek_ret = fseeko(file, 0, SEEK_SET);
#else
fseek_ret = fseek(file, 0, SEEK_SET);
#endif
if (fseek_ret == -1) {
fclose(file);
return false;
}
char *data = (char*)(malloc(fileSize));
char *data = (char*)malloc(fileSize);
if (!data) {
fclose(file);
return false;
}
const size_t fileRsize = fread(data, 1, fileSize, file);
const size_t readSize = fread(data, sizeof(char), fileSize, file);
fclose(file);
if (fileSize != fileRsize) {
if (fileSize != readSize) {
free(data);
return false;
}
@ -619,17 +679,20 @@ uint32_t ragephoto_getphotoformat(ragephoto_t instance_t)
const char* ragephoto_getphotojpeg(ragephoto_t instance_t)
{
RagePhotoInstance *instance = (RagePhotoInstance*)instance_t;
return instance->data->jpeg;
if (instance->data->jpeg)
return instance->data->jpeg;
return (libraryflags & RAGEPHOTO_FLAG_LEGACY_NULL_RETURN) ? NULL : nullchar;
}
uint64_t ragephotodata_getphotosignf(RagePhotoData *rp_data, uint32_t photoFormat)
{
if (photoFormat == RAGEPHOTO_FORMAT_GTA5)
return (0x100000000000000ULL | joaatFromInitial(rp_data->jpeg, rp_data->jpegSize, RAGEPHOTO_SIGNINITIAL_GTA5));
else if (photoFormat == RAGEPHOTO_FORMAT_RDR2)
return (0x100000000000000ULL | joaatFromInitial(rp_data->jpeg, rp_data->jpegSize, RAGEPHOTO_SIGNINITIAL_RDR2));
else
return 0;
if (rp_data->jpeg) {
if (photoFormat == RAGEPHOTO_FORMAT_GTA5)
return (UINT64_C(0x100000000000000) | joaatFromInitial(rp_data->jpeg, rp_data->jpegSize, RAGEPHOTO_SIGNINITIAL_GTA5));
else if (photoFormat == RAGEPHOTO_FORMAT_RDR2)
return (UINT64_C(0x100000000000000) | joaatFromInitial(rp_data->jpeg, rp_data->jpegSize, RAGEPHOTO_SIGNINITIAL_RDR2));
}
return 0;
}
uint64_t ragephotodata_getphotosign(RagePhotoData *rp_data)
@ -649,37 +712,54 @@ uint64_t ragephoto_getphotosign(ragephoto_t instance_t)
return ragephotodata_getphotosignf(instance->data, instance->data->photoFormat);
}
void ragephoto_getphotosigns(ragephoto_t instance, char *data, size_t size)
{
snprintf(data, size, "%" PRIu64, ragephoto_getphotosign(instance));
}
void ragephoto_getphotosignsf(ragephoto_t instance, char *data, size_t size, uint32_t photoFormat)
{
snprintf(data, size, "%" PRIu64, ragephoto_getphotosignf(instance, photoFormat));
}
uint32_t ragephoto_getphotosize(ragephoto_t instance_t)
{
RagePhotoInstance *instance = (RagePhotoInstance*)instance_t;
if (instance->data->jpeg)
return instance->data->jpegSize;
else
return 0;
return 0;
}
const char* ragephoto_getphotodesc(ragephoto_t instance_t)
{
RagePhotoInstance *instance = (RagePhotoInstance*)instance_t;
return instance->data->description;
if (instance->data->description)
return instance->data->description;
return (libraryflags & RAGEPHOTO_FLAG_LEGACY_NULL_RETURN) ? NULL : nullchar;
}
const char* ragephoto_getphotojson(ragephoto_t instance_t)
{
RagePhotoInstance *instance = (RagePhotoInstance*)instance_t;
return instance->data->json;
if (instance->data->json)
return instance->data->json;
return (libraryflags & RAGEPHOTO_FLAG_LEGACY_NULL_RETURN) ? NULL : nullchar;
}
const char* ragephoto_getphotoheader(ragephoto_t instance_t)
{
RagePhotoInstance *instance = (RagePhotoInstance*)instance_t;
return instance->data->header;
if (instance->data->header)
return instance->data->header;
return (libraryflags & RAGEPHOTO_FLAG_LEGACY_NULL_RETURN) ? NULL : nullchar;
}
const char* ragephoto_getphototitle(ragephoto_t instance_t)
{
RagePhotoInstance *instance = (RagePhotoInstance*)instance_t;
return instance->data->title;
if (instance->data->title)
return instance->data->title;
return (libraryflags & RAGEPHOTO_FLAG_LEGACY_NULL_RETURN) ? NULL : nullchar;
}
const char* ragephoto_version()
@ -690,6 +770,10 @@ const char* ragephoto_version()
bool ragephotodata_savef(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser, char *data, uint32_t photoFormat)
{
if (photoFormat == RAGEPHOTO_FORMAT_GTA5 || photoFormat == RAGEPHOTO_FORMAT_RDR2) {
if (!rp_data->header) {
rp_data->error = RAGEPHOTO_ERROR_INCOMPLETEHEADER; // 3
return false;
}
#if defined(UNICODE_ICONV) || defined(UNICODE_WINCVT)
#if defined(UNICODE_ICONV)
iconv_t iconv_in = iconv_open("UTF-16LE", "UTF-8");
@ -705,7 +789,7 @@ bool ragephotodata_savef(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parse
char *dst = photoHeader;
const size_t ret = iconv(iconv_in, &src, &src_s, &dst, &dst_s);
iconv_close(iconv_in);
if (ret == (size_t)(-1)) {
if (ret == (size_t)-1) {
rp_data->error = RAGEPHOTO_ERROR_UNICODEHEADERERROR; // 6
return false;
}
@ -713,7 +797,7 @@ bool ragephotodata_savef(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parse
#elif defined(UNICODE_WINCVT)
char photoHeader[256];
memset(&photoHeader, 0, 256);
const int converted = MultiByteToWideChar(CP_UTF8, 0, rp_data->header, (int)(strlen(rp_data->header)), (wchar_t*)(photoHeader), 256 / sizeof(wchar_t));
const int converted = MultiByteToWideChar(CP_UTF8, 0, rp_data->header, (int)strlen(rp_data->header), (wchar_t*)photoHeader, 256 / sizeof(wchar_t));
if (converted == 0) {
rp_data->error = RAGEPHOTO_ERROR_UNICODEHEADERERROR; // 6
return false;
@ -721,161 +805,165 @@ bool ragephotodata_savef(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parse
const size_t photoHeader_size = 256;
#endif
if (rp_data->jpegSize > rp_data->jpegBuffer) {
rp_data->error = RAGEPHOTO_ERROR_PHOTOBUFFERTIGHT; // 36
return false;
}
const size_t jsonString_size = strlen(rp_data->json) + 1;
if (jsonString_size > rp_data->jsonBuffer) {
rp_data->error = RAGEPHOTO_ERROR_JSONBUFFERTIGHT; // 37
return false;
}
const size_t titlString_size = strlen(rp_data->title) + 1;
if (titlString_size > rp_data->titlBuffer) {
rp_data->error = RAGEPHOTO_ERROR_TITLEBUFFERTIGHT; // 38
return false;
}
const size_t descString_size = strlen(rp_data->description) + 1;
if (descString_size > rp_data->descBuffer) {
rp_data->error = RAGEPHOTO_ERROR_DESCBUFFERTIGHT; // 39
return false;
}
const size_t length = ragephotodata_getsavesizef(rp_data, rp_parser, photoFormat);
const size_t length = ragephotodata_getsavesizef(rp_data, NULL, photoFormat);
size_t pos = 0;
char uInt32Buffer[4];
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &photoFormat, 4);
writeBuffer(&photoFormat, data, &pos, length, 4);
#else
uInt32ToCharLE((uint32_t)(photoFormat), uInt32Buffer);
#endif
char uInt32Buffer[4];
uInt32ToCharLE(photoFormat, uInt32Buffer);
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
writeBuffer(photoHeader, data, &pos, length, photoHeader_size);
for (size_t i = photoHeader_size; i < 256; i++) {
writeBuffer("\0", data, &pos, length, 1);
}
zeroBuffer(data, &pos, length, 256 - photoHeader_size);
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->headerSum, 4);
writeBuffer(&rp_data->headerSum, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->headerSum, uInt32Buffer);
#endif
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
if (photoFormat == RAGEPHOTO_FORMAT_RDR2) {
char n_formatCheckBuffer[4];
memset(&n_formatCheckBuffer, 0, 4);
writeBuffer(n_formatCheckBuffer, data, &pos, length, 4);
zeroBuffer(data, &pos, length, 4);
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->headerSum2, 4);
writeBuffer(&rp_data->headerSum2, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->headerSum2, uInt32Buffer);
#endif
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
}
const size_t headerSize = pos;
const uint32_t jsonOffset = rp_data->jpegBuffer + UINT32_C(28);
const uint32_t titlOffset = jsonOffset + rp_data->jsonBuffer + UINT32_C(8);
const uint32_t descOffset = titlOffset + rp_data->titlBuffer + UINT32_C(8);
const uint32_t endOfFile = descOffset + rp_data->descBuffer + UINT32_C(12);
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->endOfFile, 4);
writeBuffer(&endOfFile, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->endOfFile, uInt32Buffer);
#endif
uInt32ToCharLE(endOfFile, uInt32Buffer);
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->jsonOffset, 4);
writeBuffer(&jsonOffset, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->jsonOffset, uInt32Buffer);
#endif
uInt32ToCharLE(jsonOffset, uInt32Buffer);
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->titlOffset, 4);
writeBuffer(&titlOffset, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->titlOffset, uInt32Buffer);
#endif
uInt32ToCharLE(titlOffset, uInt32Buffer);
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->descOffset, 4);
writeBuffer(&descOffset, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->descOffset, uInt32Buffer);
#endif
uInt32ToCharLE(descOffset, uInt32Buffer);
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
writeBuffer("JPEG", data, &pos, length, 4);
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->jpegBuffer, 4);
writeBuffer(&rp_data->jpegBuffer, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->jpegBuffer, uInt32Buffer);
#endif
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->jpegSize, 4);
writeBuffer(&rp_data->jpegSize, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->jpegSize, uInt32Buffer);
#endif
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
writeBuffer(rp_data->jpeg, data, &pos, length, rp_data->jpegSize);
for (size_t i = rp_data->jpegSize; i < rp_data->jpegBuffer; i++) {
writeBuffer("\0", data, &pos, length, 1);
if (rp_data->jpeg) {
if (rp_data->jpegSize > rp_data->jpegBuffer) {
rp_data->error = RAGEPHOTO_ERROR_PHOTOBUFFERTIGHT; // 36
return false;
}
writeBuffer(rp_data->jpeg, data, &pos, length, rp_data->jpegSize);
zeroBuffer(data, &pos, length, rp_data->jpegBuffer - rp_data->jpegSize);
}
else {
zeroBuffer(data, &pos, length, rp_data->jpegBuffer);
}
pos = rp_data->jsonOffset + headerSize;
writeBuffer("JSON", data, &pos, length, 4);
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->jsonBuffer, 4);
writeBuffer(&rp_data->jsonBuffer, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->jsonBuffer, uInt32Buffer);
#endif
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
writeBuffer(rp_data->json, data, &pos, length, jsonString_size);
for (size_t i = jsonString_size; i < rp_data->jsonBuffer; i++) {
writeBuffer("\0", data, &pos, length, 1);
if (rp_data->json) {
const size_t jsonString_size = strlen(rp_data->json) + 1;
if (jsonString_size > rp_data->jsonBuffer) {
rp_data->error = RAGEPHOTO_ERROR_JSONBUFFERTIGHT; // 37
return false;
}
writeBuffer(rp_data->json, data, &pos, length, jsonString_size);
zeroBuffer(data, &pos, length, rp_data->jsonBuffer - jsonString_size);
}
else {
zeroBuffer(data, &pos, length, rp_data->jsonBuffer);
}
pos = rp_data->titlOffset + headerSize;
writeBuffer("TITL", data, &pos, length, 4);
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->titlBuffer, 4);
writeBuffer(&rp_data->titlBuffer, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->titlBuffer, uInt32Buffer);
#endif
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
writeBuffer(rp_data->title, data, &pos, length, titlString_size);
for (size_t i = titlString_size; i < rp_data->titlBuffer; i++) {
writeBuffer("\0", data, &pos, length, 1);
if (rp_data->title) {
const size_t titlString_size = strlen(rp_data->title) + 1;
if (titlString_size > rp_data->titlBuffer) {
rp_data->error = RAGEPHOTO_ERROR_TITLEBUFFERTIGHT; // 38
return false;
}
writeBuffer(rp_data->title, data, &pos, length, titlString_size);
zeroBuffer(data, &pos, length, rp_data->titlBuffer - titlString_size);
}
else {
zeroBuffer(data, &pos, length, rp_data->titlBuffer);
}
pos = rp_data->descOffset + headerSize;
writeBuffer("DESC", data, &pos, length, 4);
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->descBuffer, 4);
writeBuffer(&rp_data->descBuffer, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->descBuffer, uInt32Buffer);
#endif
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
writeBuffer(rp_data->description, data, &pos, length, descString_size);
for (size_t i = descString_size; i < rp_data->descBuffer; i++) {
writeBuffer("\0", data, &pos, length, 1);
if (rp_data->description) {
const size_t descString_size = strlen(rp_data->description) + 1;
if (descString_size > rp_data->descBuffer) {
rp_data->error = RAGEPHOTO_ERROR_DESCBUFFERTIGHT; // 39
return false;
}
writeBuffer(rp_data->description, data, &pos, length, descString_size);
zeroBuffer(data, &pos, length, rp_data->descBuffer - descString_size);
}
else {
zeroBuffer(data, &pos, length, rp_data->descBuffer);
}
pos = rp_data->endOfFile + headerSize - 4;
writeBuffer("JEND", data, &pos, length, 4);
rp_data->error = RAGEPHOTO_ERROR_NOERROR; // 255
@ -921,7 +1009,7 @@ bool ragephoto_savefilef(ragephoto_t instance_t, const char *filename, uint32_t
{
RagePhotoInstance *instance = (RagePhotoInstance*)instance_t;
const size_t fileSize = ragephotodata_getsavesizef(instance->data, instance->parser, photoFormat);
char *data = (char*)(malloc(fileSize));
char *data = (char*)malloc(fileSize);
if (!data)
return false;
if (!ragephotodata_savef(instance->data, instance->parser, data, photoFormat)) {
@ -938,10 +1026,10 @@ bool ragephoto_savefilef(ragephoto_t instance_t, const char *filename, uint32_t
free(data);
return false;
}
const size_t fileWsize = fwrite(data, sizeof(char), fileSize, file);
const size_t writeSize = fwrite(data, sizeof(char), fileSize, file);
fclose(file);
free(data);
return (fileSize == fileWsize);
return (fileSize == writeSize);
}
bool ragephoto_savefile(ragephoto_t instance_t, const char *filename)
@ -953,9 +1041,9 @@ bool ragephoto_savefile(ragephoto_t instance_t, const char *filename)
size_t ragephotodata_getsavesizef(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser, uint32_t photoFormat)
{
if (photoFormat == RAGEPHOTO_FORMAT_GTA5)
return (rp_data->jpegBuffer + rp_data->jsonBuffer + rp_data->titlBuffer + rp_data->descBuffer + RAGEPHOTO_GTA5_HEADERSIZE + 56UL);
return (rp_data->jpegBuffer + rp_data->jsonBuffer + rp_data->titlBuffer + rp_data->descBuffer + RAGEPHOTO_GTA5_HEADERSIZE + UINT32_C(56));
else if (photoFormat == RAGEPHOTO_FORMAT_RDR2)
return (rp_data->jpegBuffer + rp_data->jsonBuffer + rp_data->titlBuffer + rp_data->descBuffer + RAGEPHOTO_RDR2_HEADERSIZE + 56UL);
return (rp_data->jpegBuffer + rp_data->jsonBuffer + rp_data->titlBuffer + rp_data->descBuffer + RAGEPHOTO_RDR2_HEADERSIZE + UINT32_C(56));
else if (rp_parser) {
RagePhotoFormatParser n_parser;
memset(&n_parser, 0, sizeof(RagePhotoFormatParser));
@ -1013,6 +1101,11 @@ void ragephoto_setbufferoffsets(ragephoto_t instance_t)
ragephotodata_setbufferoffsets(instance->data);
}
void ragephoto_setlibraryflag(RagePhotoLibraryFlag flag, bool state)
{
state ? (libraryflags |= flag) : (libraryflags &= ~flag);
}
bool ragephoto_setphotodata(ragephoto_t instance_t, RagePhotoData *rp_data)
{
RagePhotoInstance *instance = (RagePhotoInstance*)instance_t;
@ -1041,7 +1134,7 @@ bool ragephoto_setphotodatac(ragephoto_t instance_t, RagePhotoData *rp_data)
if (rp_data->header) {
const size_t headerSize = strlen(rp_data->header) + 1;
instance->data->header = (char*)(malloc(headerSize));
instance->data->header = (char*)malloc(headerSize);
if (!instance->data->header)
return false;
memcpy(instance->data->header, rp_data->header, headerSize);
@ -1050,7 +1143,7 @@ bool ragephoto_setphotodatac(ragephoto_t instance_t, RagePhotoData *rp_data)
}
if (rp_data->jpeg) {
instance->data->jpeg = (char*)(malloc(rp_data->jpegSize));
instance->data->jpeg = (char*)malloc(rp_data->jpegSize);
if (!instance->data->jpeg)
return false;
memcpy(instance->data->jpeg, rp_data->jpeg, rp_data->jpegSize);
@ -1060,7 +1153,7 @@ bool ragephoto_setphotodatac(ragephoto_t instance_t, RagePhotoData *rp_data)
if (rp_data->json) {
const size_t jsonSize = strlen(rp_data->json) + 1;
instance->data->json = (char*)(malloc(jsonSize));
instance->data->json = (char*)malloc(jsonSize);
if (!instance->data->json)
return false;
memcpy(instance->data->json, rp_data->json, jsonSize);
@ -1069,7 +1162,7 @@ bool ragephoto_setphotodatac(ragephoto_t instance_t, RagePhotoData *rp_data)
if (rp_data->title) {
const size_t titleSize = strlen(rp_data->title) + 1;
instance->data->title = (char*)(malloc(titleSize));
instance->data->title = (char*)malloc(titleSize);
if (!instance->data->title)
return false;
memcpy(instance->data->title, rp_data->title, titleSize);
@ -1078,7 +1171,7 @@ bool ragephoto_setphotodatac(ragephoto_t instance_t, RagePhotoData *rp_data)
if (rp_data->description) {
const size_t descriptionSize = strlen(rp_data->description) + 1;
instance->data->description = (char*)(malloc(descriptionSize));
instance->data->description = (char*)malloc(descriptionSize);
if (!instance->data->description)
return false;
memcpy(instance->data->description, rp_data->description, descriptionSize);
@ -1114,7 +1207,7 @@ bool ragephoto_setphotojpeg(ragephoto_t instance_t, const char *data, uint32_t s
RagePhotoInstance *instance = (RagePhotoInstance*)instance_t;
if (instance->data->jpeg) {
if (instance->data->jpegSize > size) {
char *t_photoData = (char*)(realloc(instance->data->jpeg, size));
char *t_photoData = (char*)realloc(instance->data->jpeg, size);
if (!t_photoData) {
instance->data->error = RAGEPHOTO_ERROR_PHOTOMALLOCERROR; // 16
return false;
@ -1125,7 +1218,7 @@ bool ragephoto_setphotojpeg(ragephoto_t instance_t, const char *data, uint32_t s
}
else if (instance->data->jpegSize < size) {
free(instance->data->jpeg);
instance->data->jpeg = (char*)(malloc(size));
instance->data->jpeg = (char*)malloc(size);
if (!instance->data->jpeg) {
instance->data->error = RAGEPHOTO_ERROR_PHOTOMALLOCERROR; // 16
return false;
@ -1138,7 +1231,7 @@ bool ragephoto_setphotojpeg(ragephoto_t instance_t, const char *data, uint32_t s
}
}
else {
instance->data->jpeg = (char*)(malloc(size));
instance->data->jpeg = (char*)malloc(size);
if (!instance->data->jpeg) {
instance->data->error = RAGEPHOTO_ERROR_PHOTOMALLOCERROR; // 16
return false;

View File

@ -1,6 +1,6 @@
/*****************************************************************************
* 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,
* are permitted provided that the following conditions are met:
@ -16,9 +16,10 @@
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
#include "RagePhoto.hpp"
#include "ragephoto_cxx.hpp"
#ifdef LIBRAGEPHOTO_CXX_C
#include "RagePhoto.h"
#include <cinttypes>
#endif
#include <cstdlib>
@ -27,10 +28,6 @@
#include <iostream>
#include <iterator>
#if (RAGEPHOTO_CXX_STD >= 17) && (__cplusplus >= 201703L)
#include <filesystem>
#endif
#ifdef RAGEPHOTO_BENCHMARK
#include <chrono>
#endif
@ -45,6 +42,13 @@
#include <stringapiset.h>
#endif
/* CLASSIC RAGEPHOTO TYPEDEF */
typedef ragephoto::photo RagePhoto;
/* RAGEPHOTO LIBRARY GLOBALS */
int libraryflags = 0;
const char* nullchar = "";
/* BEGIN OF STATIC LIBRARY FUNCTIONS */
inline size_t readBuffer(const char *input, void *output, size_t *pos, size_t outputLen, size_t inputLen)
{
@ -72,6 +76,19 @@ inline size_t writeBuffer(const void *input, char *output, size_t *pos, size_t o
return writeLen;
}
inline size_t zeroBuffer(char *output, size_t *pos, size_t outputLen, size_t inputLen)
{
const size_t maxLen = outputLen - *pos;
size_t zeroLen = inputLen;
if (*pos >= outputLen)
return 0;
if (inputLen > maxLen)
zeroLen = maxLen;
memset(&output[*pos], 0, zeroLen);
*pos = *pos + zeroLen;
return zeroLen;
}
inline bool writeDataChar(const char *input, char **output)
{
const size_t src_s = strlen(input) + 1;
@ -130,18 +147,18 @@ inline uint32_t joaatFromInitial(const char *data, size_t size, uint32_t init_va
uint32_t val = init_val;
for (size_t i = 0; i != size; i++) {
val += data[i];
val += (val << 10);
val ^= (val >> 6);
val += val << 10;
val ^= val >> 6;
}
val += (val << 3);
val ^= (val >> 11);
val += (val << 15);
val += val << 3;
val ^= val >> 11;
val += val << 15;
return val;
}
/* END OF STATIC LIBRARY FUNCTIONS */
/* BEGIN OF RAGEPHOTO CLASS */
RagePhoto::RagePhoto()
RagePhoto::photo()
{
m_data = static_cast<RagePhotoData*>(malloc(sizeof(RagePhotoData)));
if (!m_data)
@ -154,7 +171,7 @@ RagePhoto::RagePhoto()
setBufferDefault(m_data);
}
RagePhoto::~RagePhoto()
RagePhoto::~photo()
{
free(m_data->jpeg);
free(m_data->description);
@ -178,7 +195,7 @@ void RagePhoto::addParser(RagePhotoFormatParser *rp_parser)
throw std::runtime_error("RagePhotoFormatParser array can't be expanded");
m_parser = t_parser;
memcpy(&m_parser[length], rp_parser, sizeof(RagePhotoFormatParser));
memset(&m_parser[length+1], 0, sizeof(RagePhotoFormatParser));
memset(&m_parser[length + 1], 0, sizeof(RagePhotoFormatParser));
}
}
@ -206,7 +223,7 @@ RagePhotoData* RagePhoto::data()
bool RagePhoto::load(const char *data, size_t length, RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser)
{
#ifdef RAGEPHOTO_BENCHMARK
auto benchmark_parse_start = std::chrono::high_resolution_clock::now();
auto benchmark_parse_start = std::chrono::steady_clock::now();
#endif
// Avoid data conflicts
@ -320,7 +337,6 @@ bool RagePhoto::load(const char *data, size_t length, RagePhotoData *rp_data, Ra
rp_data->headerSum2 = charToUInt32LE(uInt32Buffer);
#endif
}
const size_t headerSize = pos;
size = readBuffer(data, uInt32Buffer, &pos, 4, length);
if (size != 4) {
@ -411,7 +427,7 @@ bool RagePhoto::load(const char *data, size_t length, RagePhotoData *rp_data, Ra
return false;
}
pos = rp_data->jsonOffset + headerSize;
pos += rp_data->jpegBuffer - rp_data->jpegSize;
size = readBuffer(data, markerBuffer, &pos, 4, length);
if (size != 4) {
rp_data->error = Error::IncompleteJsonMarker; // 18
@ -446,7 +462,6 @@ bool RagePhoto::load(const char *data, size_t length, RagePhotoData *rp_data, Ra
return false;
}
pos = rp_data->titlOffset + headerSize;
size = readBuffer(data, markerBuffer, &pos, 4, length);
if (size != 4) {
rp_data->error = Error::IncompleteTitleMarker; // 23
@ -481,7 +496,6 @@ bool RagePhoto::load(const char *data, size_t length, RagePhotoData *rp_data, Ra
return false;
}
pos = rp_data->descOffset + headerSize;
size = readBuffer(data, markerBuffer, &pos, 4, length);
if (size != 4) {
rp_data->error = Error::IncompleteDescMarker; // 28
@ -516,7 +530,6 @@ bool RagePhoto::load(const char *data, size_t length, RagePhotoData *rp_data, Ra
return false;
}
pos = rp_data->endOfFile + headerSize - 4;
size = readBuffer(data, markerBuffer, &pos, 4, length);
if (size != 4) {
rp_data->error = Error::IncompleteJendMarker; // 33
@ -528,35 +541,32 @@ bool RagePhoto::load(const char *data, size_t length, RagePhotoData *rp_data, Ra
}
#ifdef RAGEPHOTO_BENCHMARK
auto benchmark_parse_end = std::chrono::high_resolution_clock::now();
auto benchmark_parse_end = std::chrono::steady_clock::now();
auto benchmark_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(benchmark_parse_end - benchmark_parse_start);
std::cout << "Benchmark: " << benchmark_ns.count() << "ns" << std::endl;
#endif
#ifdef RAGEPHOTO_DEBUG
const uint32_t jsonOffset = rp_data->jpegBuffer + UINT32_C(28);
const uint32_t titlOffset = jsonOffset + rp_data->jsonBuffer + UINT32_C(8);
const uint32_t descOffset = titlOffset + rp_data->titlBuffer + UINT32_C(8);
const uint32_t endOfFile = descOffset + rp_data->descBuffer + UINT32_C(12);
std::cout << "header: " << rp_data->header << std::endl;
std::cout << "headerSum: " << rp_data->headerSum << std::endl;
std::cout << "headerSum2: " << rp_data->headerSum2 << std::endl;
std::cout << "photoBuffer: " << rp_data->jpegBuffer << std::endl;
std::cout << "descBuffer: " << rp_data->descBuffer << std::endl;
std::cout << "descOffset: " << rp_data->descOffset << std::endl;
std::cout << "descOffset: " << rp_data->descOffset << " (" << descOffset << ")" << std::endl;
std::cout << "description: " << rp_data->description << std::endl;
std::cout << "jsonBuffer: " << rp_data->jsonBuffer << std::endl;
std::cout << "jsonOffset: " << rp_data->jsonOffset << std::endl;
std::cout << "jsonOffset: " << rp_data->jsonOffset << " (" << jsonOffset << ")" << std::endl;
std::cout << "json: " << rp_data->json << std::endl;
std::cout << "sign: " << jpegSign(rp_data) << std::endl;
std::cout << "titlBuffer: " << rp_data->titlBuffer << std::endl;
std::cout << "titlOffset: " << rp_data->titlOffset << std::endl;
std::cout << "titlOffset: " << rp_data->titlOffset << " (" << titlOffset << ")" << std::endl;
std::cout << "title: " << rp_data->title << std::endl;
std::cout << "eofOffset: " << rp_data->endOfFile << std::endl;
std::cout << "setBufferOffsets()" << std::endl;
setBufferOffsets(rp_data);
std::cout << "descOffset: " << rp_data->descOffset << std::endl;
std::cout << "jsonOffset: " << rp_data->jsonOffset << std::endl;
std::cout << "titlOffset: " << rp_data->titlOffset << std::endl;
std::cout << "eofOffset: " << rp_data->endOfFile << std::endl;
std::cout << "calc size: " << saveSize(rp_data, rp_parser) << std::endl;
std::cout << "real size: " << length << std::endl;
std::cout << "eofOffset: " << rp_data->endOfFile << " (" << endOfFile << ")" << std::endl;
std::cout << "size: " << length << " (" << saveSize(rp_data, nullptr) << ")" << std::endl;
#endif
rp_data->error = Error::NoError; // 255
@ -591,11 +601,7 @@ bool RagePhoto::load(const std::string &data)
bool RagePhoto::loadFile(const std::string &filename)
{
#if (RAGEPHOTO_CXX_STD >= 17) && (__cplusplus >= 201703L)
std::ifstream ifs(std::filesystem::u8path(filename), std::ios::in | std::ios::binary);
#else
std::ifstream ifs(filename, std::ios::in | std::ios::binary);
#endif
if (ifs.is_open()) {
std::string sdata(std::istreambuf_iterator<char>{ifs}, {});
ifs.close();
@ -621,8 +627,7 @@ const std::string RagePhoto::jpeg() const
{
if (m_data->jpeg)
return std::string(m_data->jpeg, m_data->jpegSize);
else
return std::string();
return std::string();
}
#if (RAGEPHOTO_CXX_STD >= 17) && (__cplusplus >= 201703L)
@ -630,24 +635,26 @@ const std::string_view RagePhoto::jpeg_view() const
{
if (m_data->jpeg)
return std::string_view(m_data->jpeg, m_data->jpegSize);
else
return std::string_view();
return std::string_view();
}
#endif
const char* RagePhoto::jpegData() const
{
return m_data->jpeg;
if (m_data->jpeg)
return m_data->jpeg;
return (libraryflags & RAGEPHOTO_FLAG_LEGACY_NULL_RETURN) ? nullptr : nullchar;
}
uint64_t RagePhoto::jpegSign(uint32_t photoFormat, RagePhotoData *rp_data)
{
if (photoFormat == PhotoFormat::GTA5)
return (0x100000000000000ULL | joaatFromInitial(rp_data->jpeg, rp_data->jpegSize, SignInitials::SIGTA5));
else if (photoFormat == PhotoFormat::RDR2)
return (0x100000000000000ULL | joaatFromInitial(rp_data->jpeg, rp_data->jpegSize, SignInitials::SIRDR2));
else
return 0;
if (rp_data->jpeg) {
if (photoFormat == PhotoFormat::GTA5)
return (UINT64_C(0x100000000000000) | joaatFromInitial(rp_data->jpeg, rp_data->jpegSize, SignInitials::SIGTA5));
else if (photoFormat == PhotoFormat::RDR2)
return (UINT64_C(0x100000000000000) | joaatFromInitial(rp_data->jpeg, rp_data->jpegSize, SignInitials::SIRDR2));
}
return 0;
}
uint64_t RagePhoto::jpegSign(RagePhotoData *rp_data)
@ -669,28 +676,35 @@ uint32_t RagePhoto::jpegSize() const
{
if (m_data->jpeg)
return m_data->jpegSize;
else
return 0;
return 0;
}
const char* RagePhoto::description() const
{
return m_data->description;
}
const char* RagePhoto::json() const
{
return m_data->json;
if (m_data->description)
return m_data->description;
return (libraryflags & RAGEPHOTO_FLAG_LEGACY_NULL_RETURN) ? nullptr : nullchar;
}
const char* RagePhoto::header() const
{
return m_data->header;
if (m_data->header)
return m_data->header;
return (libraryflags & RAGEPHOTO_FLAG_LEGACY_NULL_RETURN) ? nullptr : nullchar;
}
const char* RagePhoto::json() const
{
if (m_data->json)
return m_data->json;
return (libraryflags & RAGEPHOTO_FLAG_LEGACY_NULL_RETURN) ? nullptr : nullchar;
}
const char* RagePhoto::title() const
{
return m_data->title;
if (m_data->title)
return m_data->title;
return (libraryflags & RAGEPHOTO_FLAG_LEGACY_NULL_RETURN) ? nullptr : nullchar;
}
const char* RagePhoto::version()
@ -701,6 +715,10 @@ const char* RagePhoto::version()
bool RagePhoto::save(char *data, uint32_t photoFormat, RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser)
{
if (photoFormat == PhotoFormat::GTA5 || photoFormat == PhotoFormat::RDR2) {
if (!rp_data->header) {
rp_data->error = Error::IncompleteHeader; // 3
return false;
}
#if defined UNICODE_ICONV || defined UNICODE_CODECVT || defined UNICODE_WINCVT
#if defined UNICODE_CODECVT
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert;
@ -709,7 +727,7 @@ bool RagePhoto::save(char *data, uint32_t photoFormat, RagePhotoData *rp_data, R
rp_data->error = Error::UnicodeHeaderError; // 6
return false;
}
const size_t photoHeader_size = photoHeader_string.size() * 2;
const size_t photoHeader_size = photoHeader_string.size() * sizeof(char16_t);
if (photoHeader_size > 256) {
rp_data->error = Error::HeaderBufferTight; // 35
return false;
@ -743,160 +761,165 @@ bool RagePhoto::save(char *data, uint32_t photoFormat, RagePhotoData *rp_data, R
const size_t photoHeader_size = 256;
#endif
if (rp_data->jpegSize > rp_data->jpegBuffer) {
rp_data->error = Error::PhotoBufferTight; // 36
return false;
}
const size_t jsonString_size = strlen(rp_data->json) + 1;
if (jsonString_size > rp_data->jsonBuffer) {
rp_data->error = Error::JsonBufferTight; // 37
return false;
}
const size_t titlString_size = strlen(rp_data->title) + 1;
if (titlString_size > rp_data->titlBuffer) {
rp_data->error = Error::TitleBufferTight; // 38
return false;
}
const size_t descString_size = strlen(rp_data->description) + 1;
if (descString_size > rp_data->descBuffer) {
rp_data->error = Error::DescBufferTight; // 39
return false;
}
const size_t length = saveSize(photoFormat, rp_data, rp_parser);
const size_t length = saveSize(photoFormat, rp_data, nullptr);
size_t pos = 0;
char uInt32Buffer[4];
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &photoFormat, 4);
writeBuffer(&photoFormat, data, &pos, length, 4);
#else
uInt32ToCharLE(static_cast<uint32_t>(photoFormat), uInt32Buffer);
#endif
char uInt32Buffer[4];
uInt32ToCharLE(photoFormat, uInt32Buffer);
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
writeBuffer(photoHeader, data, &pos, length, photoHeader_size);
for (size_t i = photoHeader_size; i < 256; i++) {
writeBuffer("\0", data, &pos, length, 1);
}
zeroBuffer(data, &pos, length, 256 - photoHeader_size);
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->headerSum, 4);
writeBuffer(&rp_data->headerSum, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->headerSum, uInt32Buffer);
#endif
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
if (photoFormat == PhotoFormat::RDR2) {
char n_formatCheckBuffer[4]{};
writeBuffer(n_formatCheckBuffer, data, &pos, length, 4);
zeroBuffer(data, &pos, length, 4);
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->headerSum2, 4);
writeBuffer(&rp_data->headerSum2, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->headerSum2, uInt32Buffer);
#endif
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
}
const size_t headerSize = pos;
const uint32_t jsonOffset = rp_data->jpegBuffer + UINT32_C(28);
const uint32_t titlOffset = jsonOffset + rp_data->jsonBuffer + UINT32_C(8);
const uint32_t descOffset = titlOffset + rp_data->titlBuffer + UINT32_C(8);
const uint32_t endOfFile = descOffset + rp_data->descBuffer + UINT32_C(12);
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->endOfFile, 4);
writeBuffer(&endOfFile, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->endOfFile, uInt32Buffer);
#endif
uInt32ToCharLE(endOfFile, uInt32Buffer);
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->jsonOffset, 4);
writeBuffer(&jsonOffset, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->jsonOffset, uInt32Buffer);
#endif
uInt32ToCharLE(jsonOffset, uInt32Buffer);
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->titlOffset, 4);
writeBuffer(&titlOffset, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->titlOffset, uInt32Buffer);
#endif
uInt32ToCharLE(titlOffset, uInt32Buffer);
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->descOffset, 4);
writeBuffer(&descOffset, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->descOffset, uInt32Buffer);
#endif
uInt32ToCharLE(descOffset, uInt32Buffer);
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
writeBuffer("JPEG", data, &pos, length, 4);
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->jpegBuffer, 4);
writeBuffer(&rp_data->jpegBuffer, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->jpegBuffer, uInt32Buffer);
#endif
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->jpegSize, 4);
writeBuffer(&rp_data->jpegSize, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->jpegSize, uInt32Buffer);
#endif
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
writeBuffer(rp_data->jpeg, data, &pos, length, rp_data->jpegSize);
for (size_t i = rp_data->jpegSize; i < rp_data->jpegBuffer; i++) {
writeBuffer("\0", data, &pos, length, 1);
if (rp_data->jpeg) {
if (rp_data->jpegSize > rp_data->jpegBuffer) {
rp_data->error = Error::PhotoBufferTight; // 36
return false;
}
writeBuffer(rp_data->jpeg, data, &pos, length, rp_data->jpegSize);
zeroBuffer(data, &pos, length, rp_data->jpegBuffer - rp_data->jpegSize);
}
else {
zeroBuffer(data, &pos, length, rp_data->jpegBuffer);
}
pos = rp_data->jsonOffset + headerSize;
writeBuffer("JSON", data, &pos, length, 4);
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->jsonBuffer, 4);
writeBuffer(&rp_data->jsonBuffer, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->jsonBuffer, uInt32Buffer);
#endif
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
writeBuffer(rp_data->json, data, &pos, length, jsonString_size);
for (size_t i = jsonString_size; i < rp_data->jsonBuffer; i++) {
writeBuffer("\0", data, &pos, length, 1);
if (rp_data->json) {
const size_t jsonString_size = strlen(rp_data->json) + 1;
if (jsonString_size > rp_data->jsonBuffer) {
rp_data->error = Error::JsonBufferTight; // 37
return false;
}
writeBuffer(rp_data->json, data, &pos, length, jsonString_size);
zeroBuffer(data, &pos, length, rp_data->jsonBuffer - jsonString_size);
}
else {
zeroBuffer(data, &pos, length, rp_data->jsonBuffer);
}
pos = rp_data->titlOffset + headerSize;
writeBuffer("TITL", data, &pos, length, 4);
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->titlBuffer, 4);
writeBuffer(&rp_data->titlBuffer, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->titlBuffer, uInt32Buffer);
#endif
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
writeBuffer(rp_data->title, data, &pos, length, titlString_size);
for (size_t i = titlString_size; i < rp_data->titlBuffer; i++) {
writeBuffer("\0", data, &pos, length, 1);
if (rp_data->title) {
const size_t titlString_size = strlen(rp_data->title) + 1;
if (titlString_size > rp_data->titlBuffer) {
rp_data->error = Error::TitleBufferTight; // 38
return false;
}
writeBuffer(rp_data->title, data, &pos, length, titlString_size);
zeroBuffer(data, &pos, length, rp_data->titlBuffer - titlString_size);
}
else {
zeroBuffer(data, &pos, length, rp_data->titlBuffer);
}
pos = rp_data->descOffset + headerSize;
writeBuffer("DESC", data, &pos, length, 4);
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(uInt32Buffer, &rp_data->descBuffer, 4);
writeBuffer(&rp_data->descBuffer, data, &pos, length, 4);
#else
uInt32ToCharLE(rp_data->descBuffer, uInt32Buffer);
#endif
writeBuffer(uInt32Buffer, data, &pos, length, 4);
#endif
writeBuffer(rp_data->description, data, &pos, length, descString_size);
for (size_t i = descString_size; i < rp_data->descBuffer; i++) {
writeBuffer("\0", data, &pos, length, 1);
if (rp_data->description) {
const size_t descString_size = strlen(rp_data->description) + 1;
if (descString_size > rp_data->descBuffer) {
rp_data->error = Error::DescBufferTight; // 39
return false;
}
writeBuffer(rp_data->description, data, &pos, length, descString_size);
zeroBuffer(data, &pos, length, rp_data->descBuffer - descString_size);
}
else {
zeroBuffer(data, &pos, length, rp_data->descBuffer);
}
pos = rp_data->endOfFile + headerSize - 4;
writeBuffer("JEND", data, &pos, length, 4);
rp_data->error = Error::NoError; // 255
@ -956,16 +979,12 @@ const std::string RagePhoto::save(bool *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;
const std::string &sdata = save(photoFormat, &ok);
const std::string sdata = save(photoFormat, &ok);
if (ok) {
#if (RAGEPHOTO_CXX_STD >= 17) && (__cplusplus >= 201703L)
std::ofstream ofs(std::filesystem::u8path(filename), std::ios::out | std::ios::binary | std::ios::trunc);
#else
std::ofstream ofs(filename, std::ios::out | std::ios::binary | std::ios::trunc);
#endif
if (!ofs.is_open()) {
m_data->error = Error::Uninitialised; // 0
return false;
@ -983,7 +1002,7 @@ bool RagePhoto::saveFile(const std::string &filename, uint32_t photoFormat)
return false;
}
bool RagePhoto::saveFile(const std::string &filename)
bool RagePhoto::saveFile(const char *filename)
{
return saveFile(filename, m_data->photoFormat);
}
@ -991,9 +1010,9 @@ bool RagePhoto::saveFile(const std::string &filename)
size_t RagePhoto::saveSize(uint32_t photoFormat, RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser)
{
if (photoFormat == PhotoFormat::GTA5)
return (rp_data->jpegBuffer + rp_data->jsonBuffer + rp_data->titlBuffer + rp_data->descBuffer + GTA5_HEADERSIZE + 56UL);
return (rp_data->jpegBuffer + rp_data->jsonBuffer + rp_data->titlBuffer + rp_data->descBuffer + GTA5_HEADERSIZE + UINT32_C(56));
else if (photoFormat == PhotoFormat::RDR2)
return (rp_data->jpegBuffer + rp_data->jsonBuffer + rp_data->titlBuffer + rp_data->descBuffer + RDR2_HEADERSIZE + 56UL);
return (rp_data->jpegBuffer + rp_data->jsonBuffer + rp_data->titlBuffer + rp_data->descBuffer + RDR2_HEADERSIZE + UINT32_C(56));
else if (rp_parser) {
RagePhotoFormatParser n_parser[1]{};
for (size_t i = 0; memcmp(&n_parser[0], &rp_parser[i], sizeof(RagePhotoFormatParser)); i++) {
@ -1135,6 +1154,17 @@ void RagePhoto::setFormat(uint32_t photoFormat)
m_data->photoFormat = photoFormat;
}
void RagePhoto::setHeader(const char *header, uint32_t headerSum, uint32_t headerSum2)
{
if (!writeDataChar(header, &m_data->header)) {
m_data->error = Error::HeaderMallocError; // 4
return;
}
m_data->headerSum = headerSum;
m_data->headerSum2 = headerSum2;
m_data->error = Error::NoError; // 255
}
bool RagePhoto::setJpeg(const char *data, uint32_t size, uint32_t bufferSize)
{
if (m_data->jpeg) {
@ -1199,15 +1229,9 @@ void RagePhoto::setJson(const char *json, uint32_t bufferSize)
m_data->error = Error::NoError; // 255
}
void RagePhoto::setHeader(const char *header, uint32_t headerSum, uint32_t headerSum2)
void RagePhoto::setLibraryFlag(RagePhotoLibraryFlag flag, bool state)
{
if (!writeDataChar(header, &m_data->header)) {
m_data->error = Error::HeaderMallocError; // 4
return;
}
m_data->headerSum = headerSum;
m_data->headerSum2 = headerSum2;
m_data->error = Error::NoError; // 255
state ? (libraryflags |= flag) : (libraryflags &= ~flag);
}
void RagePhoto::setTitle(const char *title, uint32_t bufferSize)
@ -1226,13 +1250,24 @@ void RagePhoto::setTitle(const char *title, uint32_t bufferSize)
#ifdef LIBRAGEPHOTO_CXX_C
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)
{
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)
@ -1337,6 +1372,18 @@ uint64_t ragephoto_getphotosignf(ragephoto_t instance, uint32_t photoFormat)
return ragePhoto->jpegSign(photoFormat);
}
void ragephoto_getphotosigns(ragephoto_t instance, char *data, size_t size)
{
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
snprintf(data, size, "%" PRIu64, ragePhoto->jpegSign());
}
void ragephoto_getphotosignsf(ragephoto_t instance, char *data, size_t size, uint32_t photoFormat)
{
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
snprintf(data, size, "%" PRIu64, ragePhoto->jpegSign(photoFormat));
}
uint64_t ragephotodata_getphotosign(RagePhotoData *rp_data)
{
return RagePhoto::jpegSign(rp_data);
@ -1437,6 +1484,11 @@ void ragephotodata_setbufferoffsets(RagePhotoData *rp_data)
RagePhoto::setBufferOffsets(rp_data);
}
void ragephoto_setlibraryflag(RagePhotoLibraryFlag flag, bool state)
{
RagePhoto::setLibraryFlag(flag, state);
}
bool ragephoto_setphotodata(ragephoto_t instance, RagePhotoData *rp_data)
{
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);

View File

@ -1,6 +1,6 @@
/*****************************************************************************
* 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,
* are permitted provided that the following conditions are met:
@ -16,6 +16,10 @@
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
/** RagePhoto C API header file
* \file RagePhoto.h
*/
#ifndef RAGEPHOTO_H
#define RAGEPHOTO_H
@ -28,33 +32,37 @@
extern "C" {
#endif // __cplusplus
/** C API for RagePhoto.
* \file RagePhoto.h
/** RagePhoto typedef for C instance/C++ object.
* \memberof RagePhotoInstance
*/
/** RagePhoto C instance/C++ class typedef. */
typedef void* ragephoto_t;
/** Opens a \p ragephoto_t instance. */
/** Opens a \p ragephoto_t instance.
* \memberof RagePhotoInstance
*/
LIBRAGEPHOTO_C_PUBLIC ragephoto_t ragephoto_open();
/** Add a custom defined RagePhotoFormatParser.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
* \param rp_parser RagePhotoFormatParser parser to add
* \param rp_parser Parser to add
*/
LIBRAGEPHOTO_C_PUBLIC void ragephoto_addparser(ragephoto_t instance, RagePhotoFormatParser *rp_parser);
/** Resets the RagePhotoData object to default values.
/** Resets the Data object to default values.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
*/
LIBRAGEPHOTO_C_PUBLIC void ragephoto_clear(ragephoto_t instance);
/** Resets the RagePhotoData object to default values.
* \param rp_data RagePhotoData object
/** Resets the Data object to default values.
* \memberof RagePhotoData
* \param rp_data Data object
*/
LIBRAGEPHOTO_C_PUBLIC void ragephotodata_clear(RagePhotoData *rp_data);
/** Loads a Photo from a const char*.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
* \param data Photo data
* \param size Photo data size
@ -62,136 +70,183 @@ LIBRAGEPHOTO_C_PUBLIC void ragephotodata_clear(RagePhotoData *rp_data);
LIBRAGEPHOTO_C_PUBLIC bool ragephoto_load(ragephoto_t instance, const char *data, size_t size);
/** Loads a Photo from a const char*.
* \param rp_data RagePhotoData object
* \param rp_parser RagePhotoFormatParser parser array
* \memberof RagePhotoData
* \param rp_data Data object
* \param rp_parser Parser array
* \param data Photo data
* \param size Photo data size
*/
LIBRAGEPHOTO_C_PUBLIC bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser, const char *data, size_t size);
/** Loads a Photo from a file.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
* \param filename File to load
*/
LIBRAGEPHOTO_C_PUBLIC bool ragephoto_loadfile(ragephoto_t instance, const char *filename);
/** Returns the last error occurred.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
*/
LIBRAGEPHOTO_C_PUBLIC int32_t ragephoto_error(ragephoto_t instance);
/** Returns the GTA V default Photo Buffer Size. */
/** Returns the GTA V default Photo Buffer Size.
* \relates RagePhotoInstance
*/
LIBRAGEPHOTO_C_PUBLIC uint32_t ragephoto_defpbuf_gta5();
/** Returns the RDR 2 default Photo Buffer Size. */
/** Returns the RDR 2 default Photo Buffer Size.
* \relates RagePhotoInstance
*/
LIBRAGEPHOTO_C_PUBLIC uint32_t ragephoto_defpbuf_rdr2();
/** Returns the GTA V Photo Format. */
/** Returns the GTA V Photo Format.
* \relates RagePhotoInstance
*/
LIBRAGEPHOTO_C_PUBLIC uint32_t ragephoto_format_gta5();
/** Returns the RDR 2 Photo Format. */
/** Returns the RDR 2 Photo Format.
* \relates RagePhotoInstance
*/
LIBRAGEPHOTO_C_PUBLIC uint32_t ragephoto_format_rdr2();
/** Returns the internal RagePhotoData object.
/** Returns the internal Data object.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
*/
LIBRAGEPHOTO_C_PUBLIC RagePhotoData* ragephoto_getphotodata(ragephoto_t instance);
/** Returns the Photo description.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
*/
LIBRAGEPHOTO_C_PUBLIC const char* ragephoto_getphotodesc(ragephoto_t instance);
/** Returns the Photo Format (GTA V or RDR 2).
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
*/
LIBRAGEPHOTO_C_PUBLIC uint32_t ragephoto_getphotoformat(ragephoto_t instance);
/** Returns the Photo JPEG data.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
*/
LIBRAGEPHOTO_C_PUBLIC const char* ragephoto_getphotojpeg(ragephoto_t instance);
/** Returns the Photo JSON data.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
*/
LIBRAGEPHOTO_C_PUBLIC const char* ragephoto_getphotojson(ragephoto_t instance);
/** Returns the Photo header.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
*/
LIBRAGEPHOTO_C_PUBLIC const char* ragephoto_getphotoheader(ragephoto_t instance);
/** Returns the Photo JPEG sign.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
*/
LIBRAGEPHOTO_C_PUBLIC uint64_t ragephoto_getphotosign(ragephoto_t instance);
/** Returns the Photo JPEG sign.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
* \param photoFormat Photo Format (GTA V or RDR 2)
*/
LIBRAGEPHOTO_C_PUBLIC uint64_t ragephoto_getphotosignf(ragephoto_t instance, uint32_t photoFormat);
/** Returns the Photo JPEG sign as string.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
* \param data String data
* \param size String size
*/
LIBRAGEPHOTO_C_PUBLIC void ragephoto_getphotosigns(ragephoto_t instance, char *data, size_t size);
/** Returns the Photo JPEG sign as string.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
* \param data String data
* \param size String size
* \param photoFormat Photo Format (GTA V or RDR 2)
*/
LIBRAGEPHOTO_C_PUBLIC void ragephoto_getphotosignsf(ragephoto_t instance, char *data, size_t size, uint32_t photoFormat);
/** Returns the Photo JPEG sign.
* \param rp_data RagePhotoData object
* \memberof RagePhotoData
* \param rp_data Data object
*/
LIBRAGEPHOTO_C_PUBLIC uint64_t ragephotodata_getphotosign(RagePhotoData *rp_data);
/** Returns the Photo JPEG sign.
* \param rp_data RagePhotoData object
* \memberof RagePhotoData
* \param rp_data Data object
* \param photoFormat Photo Format (GTA V or RDR 2)
*/
LIBRAGEPHOTO_C_PUBLIC uint64_t ragephotodata_getphotosignf(RagePhotoData *rp_data, uint32_t photoFormat);
/** Returns the Photo JPEG data size.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
*/
LIBRAGEPHOTO_C_PUBLIC uint32_t ragephoto_getphotosize(ragephoto_t instance);
/** Returns the Photo title.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
*/
LIBRAGEPHOTO_C_PUBLIC const char* ragephoto_getphototitle(ragephoto_t instance);
/** Returns the Photo save file size.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
*/
LIBRAGEPHOTO_C_PUBLIC size_t ragephoto_getsavesize(ragephoto_t instance);
/** Returns the Photo save file size.
* \param rp_data RagePhotoData object
* \param rp_parser RagePhotoFormatParser parser array
* \memberof RagePhotoData
* \param rp_data Data object
* \param rp_parser Parser array
*/
LIBRAGEPHOTO_C_PUBLIC size_t ragephotodata_getsavesize(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser);
/** Returns the Photo save file size.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
* \param photoFormat Photo Format (GTA V or RDR 2)
*/
LIBRAGEPHOTO_C_PUBLIC size_t ragephoto_getsavesizef(ragephoto_t instance, uint32_t photoFormat);
/** Returns the Photo save file size.
* \param rp_data RagePhotoData object
* \param rp_parser RagePhotoFormatParser parser array
* \memberof RagePhotoData
* \param rp_data Data object
* \param rp_parser Parser array
* \param photoFormat Photo Format (GTA V or RDR 2)
*/
LIBRAGEPHOTO_C_PUBLIC size_t ragephotodata_getsavesizef(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser, uint32_t photoFormat);
/** Saves a Photo to a char*.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
* \param data Photo data
*/
LIBRAGEPHOTO_C_PUBLIC bool ragephoto_save(ragephoto_t instance, char *data);
/** Saves a Photo to a char*.
* \param rp_data RagePhotoData object
* \param rp_parser RagePhotoFormatParser parser array
* \memberof RagePhotoData
* \param rp_data Data object
* \param rp_parser Parser array
* \param data Photo data
*/
LIBRAGEPHOTO_C_PUBLIC bool ragephotodata_save(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser, char *data);
/** Saves a Photo to a char*.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
* \param data Photo data
* \param photoFormat Photo Format (GTA V or RDR 2)
@ -199,20 +254,23 @@ LIBRAGEPHOTO_C_PUBLIC bool ragephotodata_save(RagePhotoData *rp_data, RagePhotoF
LIBRAGEPHOTO_C_PUBLIC bool ragephoto_savef(ragephoto_t instance, char *data, uint32_t photoFormat);
/** Saves a Photo to a char*.
* \param rp_data RagePhotoData object
* \param rp_parser RagePhotoFormatParser parser array
* \memberof RagePhotoData
* \param rp_data Data object
* \param rp_parser Parser array
* \param data Photo data
* \param photoFormat Photo Format (GTA V or RDR 2)
*/
LIBRAGEPHOTO_C_PUBLIC bool ragephotodata_savef(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser, char *data, uint32_t photoFormat);
/** Saves a Photo to a file.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
* \param filename File to save
*/
LIBRAGEPHOTO_C_PUBLIC bool ragephoto_savefile(ragephoto_t instance, const char *filename);
/** Saves a Photo to a file.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
* \param filename File to save
* \param photoFormat Photo Format (GTA V or RDR 2)
@ -220,38 +278,52 @@ LIBRAGEPHOTO_C_PUBLIC bool ragephoto_savefile(ragephoto_t instance, const char *
LIBRAGEPHOTO_C_PUBLIC bool ragephoto_savefilef(ragephoto_t instance, const char *filename, uint32_t photoFormat);
/** Sets all cross-format Buffer to default size.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
*/
LIBRAGEPHOTO_C_PUBLIC void ragephoto_setbufferdefault(ragephoto_t instance);
/** Sets all cross-format Buffer to default size.
* \param rp_data RagePhotoData object
* \memberof RagePhotoData
* \param rp_data Data object
*/
LIBRAGEPHOTO_C_PUBLIC void ragephotodata_setbufferdefault(RagePhotoData *rp_data);
/** Moves all Buffer offsets to correct position.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
*/
LIBRAGEPHOTO_C_PUBLIC void ragephoto_setbufferoffsets(ragephoto_t instance);
/** Moves all Buffer offsets to correct position.
* \param rp_data RagePhotoData object
* \memberof RagePhotoData
* \param rp_data Data object
*/
LIBRAGEPHOTO_C_PUBLIC void ragephotodata_setbufferoffsets(RagePhotoData *rp_data);
/** Sets a library flag.
* \relates RagePhotoInstance
* \param flag Library flag
* \param state Flag state
*/
LIBRAGEPHOTO_C_PUBLIC void ragephoto_setlibraryflag(RagePhotoLibraryFlag flag, bool state);
/** Sets the internal RagePhotoData object.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
* \param rp_data RagePhotoData object being set
* \param rp_data Data object used to replace
*/
LIBRAGEPHOTO_C_PUBLIC bool ragephoto_setphotodata(ragephoto_t instance, RagePhotoData *rp_data);
/** Copies RagePhotoData object to internal RagePhotoData object.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
* \param rp_data RagePhotoData object being copied
* \param rp_data Data object used to copy
*/
LIBRAGEPHOTO_C_PUBLIC bool ragephoto_setphotodatac(ragephoto_t instance, RagePhotoData *rp_data);
/** Sets the Photo description.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
* \param description Description
* \param bufferSize Description buffer size
@ -261,12 +333,14 @@ LIBRAGEPHOTO_C_PUBLIC bool ragephoto_setphotodatac(ragephoto_t instance, RagePho
LIBRAGEPHOTO_C_PUBLIC void ragephoto_setphotodesc(ragephoto_t instance, const char *description, uint32_t bufferSize);
/** Sets the Photo Format (GTA V or RDR 2).
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
* \param photoFormat Photo Format (GTA V or RDR 2)
*/
LIBRAGEPHOTO_C_PUBLIC void ragephoto_setphotoformat(ragephoto_t instance, uint32_t photoFormat);
/** Sets the Photo JPEG data.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
* \param data JPEG data
* \param size JPEG data size
@ -277,6 +351,7 @@ LIBRAGEPHOTO_C_PUBLIC void ragephoto_setphotoformat(ragephoto_t instance, uint32
LIBRAGEPHOTO_C_PUBLIC bool ragephoto_setphotojpeg(ragephoto_t instance, const char *data, uint32_t size, uint32_t bufferSize);
/** Sets the Photo JSON data.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
* \param json JSON data
* \param bufferSize JSON data buffer size
@ -285,13 +360,18 @@ LIBRAGEPHOTO_C_PUBLIC bool ragephoto_setphotojpeg(ragephoto_t instance, const ch
*/
LIBRAGEPHOTO_C_PUBLIC void ragephoto_setphotojson(ragephoto_t instance, const char *json, uint32_t bufferSize);
/** Sets the Photo header. */
/** Sets the Photo header.
* \memberof RagePhotoInstance
*/
LIBRAGEPHOTO_C_PUBLIC void ragephoto_setphotoheader(ragephoto_t instance, const char *header, uint32_t headerSum);
/** Sets the Photo header. (RDR 2) */
/** Sets the Photo header. (RDR 2)
* \memberof RagePhotoInstance
*/
LIBRAGEPHOTO_C_PUBLIC void ragephoto_setphotoheader2(ragephoto_t instance, const char *header, uint32_t headerSum, uint32_t headerSum2);
/** Sets the Photo title.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
* \param title Title
* \param bufferSize Title buffer size
@ -301,11 +381,14 @@ LIBRAGEPHOTO_C_PUBLIC void ragephoto_setphotoheader2(ragephoto_t instance, const
LIBRAGEPHOTO_C_PUBLIC void ragephoto_setphototitle(ragephoto_t instance, const char *title, uint32_t bufferSize);
/** Closes a \p ragephoto_t instance.
* \memberof RagePhotoInstance
* \param instance \p ragephoto_t instance
*/
LIBRAGEPHOTO_C_PUBLIC void ragephoto_close(ragephoto_t instance);
/** Returns the library version. */
/** Returns the library version.
* \relates RagePhotoInstance
*/
LIBRAGEPHOTO_C_PUBLIC const char* ragephoto_version();
#ifdef __cplusplus

View File

@ -1,6 +1,6 @@
/*****************************************************************************
* libragephoto RAGE Photo Parser
* Copyright (C) 2023 Syping
* Copyright (C) 2023-2024 Syping
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
@ -16,28 +16,33 @@
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
#ifndef RAGEPHOTOB_HPP
#define RAGEPHOTOB_HPP
#ifndef RAGEPHOTO_HPP
#define RAGEPHOTO_HPP
#ifdef __cplusplus
#include "RagePhotoLibrary.h"
#ifdef LIBRAGEPHOTO_CXX_ONLY
#include "RagePhoto.hpp"
typedef RagePhoto RagePhotoB;
#include "ragephoto_cxx.hpp"
typedef ragephoto::photo RagePhoto;
#elif defined LIBRAGEPHOTO_CXX_C
#ifdef LIBRAGEPHOTO_STATIC
#include "RagePhoto.hpp"
typedef RagePhoto RagePhotoB;
#include "ragephoto_cxx.hpp"
typedef ragephoto::photo RagePhoto;
#else
#include "RagePhotoA.hpp"
typedef RagePhotoA RagePhotoB;
#ifdef LIBRAGEPHOTO_PREFER_NATIVE
#include "ragephoto_cxx.hpp"
typedef ragephoto::photo RagePhoto;
#else
#include "ragephoto_c.hpp"
typedef ragephoto_c::photo RagePhoto;
#endif // LIBRAGEPHOTO_PREFER_NATIVE
#endif // LIBRAGEPHOTO_STATIC
#elif defined LIBRAGEPHOTO_C_ONLY
#include "RagePhotoA.hpp"
typedef RagePhotoA RagePhotoB;
#include "ragephoto_c.hpp"
typedef ragephoto_c::photo RagePhoto;
#else
#error "Could not determine best RagePhoto implementation, libragephoto installation might be corrupt!"
#endif // LIBRAGEPHOTO_CXX_ONLY
#endif // __cplusplus
#endif // RAGEPHOTOB_HPP
#endif // RAGEPHOTO_HPP

View File

@ -1,6 +1,6 @@
/*****************************************************************************
* libragephoto RAGE Photo Parser
* Copyright (C) 2023 Syping
* Copyright (C) 2023-2024 Syping
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
@ -22,7 +22,7 @@
/* CMAKE CONFIG BEGIN */
#define @LIBRAGEPHOTO_API@
#define @LIBRAGEPHOTO_LIBTYPE@
#define RAGEPHOTO_CXX_STD @CMAKE_CXX_STANDARD@
#define RAGEPHOTO_CXX_STD @RAGEPHOTO_CXX_STANDARD@
#define RAGEPHOTO_VERSION "@ragephoto_VERSION@"
#define RAGEPHOTO_VERSION_MAJOR @ragephoto_VERSION_MAJOR@
#define RAGEPHOTO_VERSION_MINOR @ragephoto_VERSION_MINOR@

View File

@ -0,0 +1,150 @@
/*****************************************************************************
* 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 RAGEPHOTOTYPEDEFS_H
#define RAGEPHOTOTYPEDEFS_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
/** RagePhoto data struct for storing internal data. */
typedef struct RagePhotoData {
char* jpeg; /**< Pointer for internal JPEG buffer */
char* description; /**< Pointer for internal Description buffer */
char* json; /**< Pointer for internal JSON buffer */
char* header; /**< Pointer for internal Header buffer */
char* title; /**< Pointer for internal Title buffer */
int32_t error; /**< RagePhoto error code */
uint32_t descBuffer; /**< Description buffer length */
uint32_t descOffset; /**< Description buffer offset */
uint32_t endOfFile; /**< End Of File offset */
uint32_t headerSum; /**< Checksum of the header 1 */
uint32_t headerSum2; /**< Checksum of the header 2 (RDR 2 only) */
uint32_t jpegBuffer; /**< JPEG buffer length */
uint32_t jpegSize; /**< Internal JPEG buffer length and size of JPEG */
uint32_t jsonBuffer; /**< JSON buffer length */
uint32_t jsonOffset; /**< JSON buffer offset */
uint32_t photoFormat; /**< Photo file format magic */
uint32_t titlBuffer; /**< Title buffer length */
uint32_t titlOffset; /**< Title buffer offset */
} RagePhotoData;
/** RagePhoto load function typedef. */
typedef bool (*ragephoto_loadfunc_t)(RagePhotoData*, const char*, size_t);
/** RagePhoto save function typedef (char* allocated by caller). */
typedef bool (*ragephoto_savefunc_t)(RagePhotoData*, char*, uint32_t);
/** RagePhoto save function typedef (char* allocated by function). */
typedef bool (*ragephoto_savepfunc_t)(RagePhotoData*, char**, uint32_t);
/** RagePhoto saveSize function typedef. */
typedef size_t (*ragephoto_saveszfunc_t)(RagePhotoData*, uint32_t);
/** RagePhoto format parser struct for registering custom formats. */
typedef struct RagePhotoFormatParser {
uint32_t photoFormat; /**< Photo file format magic */
ragephoto_loadfunc_t funcLoad; /**< Pointer to load function */
ragephoto_savefunc_t funcSave; /**< Pointer to save function */
ragephoto_savepfunc_t funcSavep; /**< Pointer to savep function */
ragephoto_saveszfunc_t funcSaveSz; /**< Pointer to saveSize function */
} RagePhotoFormatParser;
/** RagePhoto instance struct for storing data and format parser pointer.
* \struct RagePhotoInstance RagePhoto.h
*/
typedef struct RagePhotoInstance {
RagePhotoData *data; /**< Pointer for internal data */
RagePhotoFormatParser *parser; /**< Pointer for internal format parser */
} RagePhotoInstance;
/** RagePhoto library flags. */
typedef enum RagePhotoLibraryFlag {
RAGEPHOTO_FLAG_LEGACY_NULL_RETURN = 1 << 0 /**< Flag to enable legacy NULL return */
} RagePhotoLibraryFlag;
/* RagePhoto default sizes */
#define RAGEPHOTO_DEFAULT_GTA5_PHOTOBUFFER UINT32_C(524288) /**< GTA V default Photo Buffer Size */
#define RAGEPHOTO_DEFAULT_RDR2_PHOTOBUFFER UINT32_C(1048576) /**< RDR 2 default Photo Buffer Size */
#define RAGEPHOTO_DEFAULT_DESCBUFFER UINT32_C(256) /**< Default Description Buffer Size */
#define RAGEPHOTO_DEFAULT_JSONBUFFER UINT32_C(3072) /**< Default JSON Buffer Size */
#define RAGEPHOTO_DEFAULT_TITLBUFFER UINT32_C(256) /**< Default Title Buffer Size */
#define RAGEPHOTO_GTA5_HEADERSIZE UINT32_C(264) /**< GTA V Header Size */
#define RAGEPHOTO_RDR2_HEADERSIZE UINT32_C(272) /**< RDR 2 Header Size */
/* RagePhoto error codes */
#define RAGEPHOTO_ERROR_DESCBUFFERTIGHT INT32_C(39) /**< Description Buffer is too tight */
#define RAGEPHOTO_ERROR_DESCMALLOCERROR INT32_C(31) /**< Description Buffer can't be allocated */
#define RAGEPHOTO_ERROR_DESCREADERROR INT32_C(32) /**< Description can't be read successfully */
#define RAGEPHOTO_ERROR_HEADERBUFFERTIGHT INT32_C(35) /**< Header Buffer is too tight */
#define RAGEPHOTO_ERROR_HEADERMALLOCERROR INT32_C(4) /**< Header Buffer can't be allocated */
#define RAGEPHOTO_ERROR_INCOMPATIBLEFORMAT INT32_C(2) /**< Format is incompatible */
#define RAGEPHOTO_ERROR_INCOMPLETECHECKSUM INT32_C(7) /**< Header checksum is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEDESCBUFFER INT32_C(30) /**< Description Buffer Size is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEDESCMARKER INT32_C(28) /**< Description Marker is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEDESCOFFSET INT32_C(11) /**< Description Offset is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEEOF INT32_C(8) /**< End Of File Offset is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEHEADER INT32_C(3) /**< Header is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEJENDMARKER INT32_C(33) /**< JEND Marker is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEJPEGMARKER INT32_C(12) /**< JPEG Marker is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEJSONBUFFER INT32_C(20) /**< JSON Buffer Size is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEJSONMARKER INT32_C(18) /**< JSON Marker incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEJSONOFFSET INT32_C(9) /**< JSON Offset incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEPHOTOBUFFER INT32_C(14) /**< Photo Buffer Size is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETEPHOTOSIZE INT32_C(15) /**< Photo Size is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETETITLEBUFFER INT32_C(25) /**< Title Buffer Size is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETETITLEMARKER INT32_C(23) /**< Title Marker is incomplete */
#define RAGEPHOTO_ERROR_INCOMPLETETITLEOFFSET INT32_C(10) /**< Title Offset is incomplete */
#define RAGEPHOTO_ERROR_INCORRECTDESCMARKER INT32_C(29) /**< Description Marker is incorrect */
#define RAGEPHOTO_ERROR_INCORRECTJENDMARKER INT32_C(34) /**< JEND Marker is incorrect */
#define RAGEPHOTO_ERROR_INCORRECTJPEGMARKER INT32_C(13) /**< JPEG Marker is incorrect */
#define RAGEPHOTO_ERROR_INCORRECTJSONMARKER INT32_C(19) /**< JSON Marker is incorrect */
#define RAGEPHOTO_ERROR_INCORRECTTITLEMARKER INT32_C(24) /**< Title Marker is incorrect */
#define RAGEPHOTO_ERROR_JSONBUFFERTIGHT INT32_C(37) /**< JSON Buffer is too tight */
#define RAGEPHOTO_ERROR_JSONMALLOCERROR INT32_C(21) /**< JSON Buffer can't be allocated */
#define RAGEPHOTO_ERROR_JSONREADERROR INT32_C(22) /**< JSON can't be read successfully */
#define RAGEPHOTO_ERROR_NOERROR INT32_C(255) /**< Finished without errors */
#define RAGEPHOTO_ERROR_NOFORMATIDENTIFIER INT32_C(1) /**< No format detected, empty file */
#define RAGEPHOTO_ERROR_PHOTOBUFFERTIGHT INT32_C(36) /**< Photo Buffer is too tight */
#define RAGEPHOTO_ERROR_PHOTOMALLOCERROR INT32_C(16) /**< Photo Buffer can't be allocated */
#define RAGEPHOTO_ERROR_PHOTOREADERROR INT32_C(17) /**< Photo can't be read */
#define RAGEPHOTO_ERROR_TITLEBUFFERTIGHT INT32_C(38) /**< Title Buffer is too tight */
#define RAGEPHOTO_ERROR_TITLEMALLOCERROR INT32_C(26) /**< Title Buffer can't be allocated */
#define RAGEPHOTO_ERROR_TITLEREADERROR INT32_C(27) /**< Title can't be read */
#define RAGEPHOTO_ERROR_UNICODEINITERROR INT32_C(5) /**< Failed to initialise Unicode decoder */
#define RAGEPHOTO_ERROR_UNICODEHEADERERROR INT32_C(6) /**< Header can't be encoded/decoded successfully */
#define RAGEPHOTO_ERROR_UNINITIALISED INT32_C(0) /**< Uninitialised, file access failed */
/* RagePhoto formats */
#define RAGEPHOTO_FORMAT_GTA5 UINT32_C(0x01000000) /**< GTA V Photo Format */
#define RAGEPHOTO_FORMAT_RDR2 UINT32_C(0x04000000) /**< RDR 2 Photo Format */
/* RagePhoto sign initials */
#define RAGEPHOTO_SIGNINITIAL_GTA5 UINT32_C(0xE47AB81C) /**< GTA V Sign Initial */
#define RAGEPHOTO_SIGNINITIAL_RDR2 UINT32_C(0x00FEEB1E) /**< RDR 2 Sign Initial */
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // RAGEPHOTOTYPEDEFS_H

1
src/core/ragephoto_c Normal file
View File

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

View File

@ -1,6 +1,6 @@
/*****************************************************************************
* 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,
* are permitted provided that the following conditions are met:
@ -16,88 +16,93 @@
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
#ifndef RAGEPHOTOA_HPP
#define RAGEPHOTOA_HPP
#ifndef RAGEPHOTO_C_HPP
#define RAGEPHOTO_C_HPP
#ifdef __cplusplus
#include "RagePhoto.h"
#include <cstdlib>
#include <iostream>
namespace ragephoto_c {
/**
* \brief GTA V and RDR 2 Photo Parser (C API wrapper).
* \class ragephoto_c::photo RagePhoto.hpp RagePhoto
*/
class RagePhotoA
class 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 */
DEFAULT_GTA5_PHOTOBUFFER = RAGEPHOTO_DEFAULT_GTA5_PHOTOBUFFER, /**< GTA V default Photo Buffer Size */
DEFAULT_RDR2_PHOTOBUFFER = RAGEPHOTO_DEFAULT_RDR2_PHOTOBUFFER, /**< RDR 2 default Photo Buffer Size */
DEFAULT_DESCBUFFER = RAGEPHOTO_DEFAULT_DESCBUFFER, /**< Default Description Buffer Size */
DEFAULT_JSONBUFFER = RAGEPHOTO_DEFAULT_JSONBUFFER, /**< Default JSON Buffer Size */
DEFAULT_TITLBUFFER = RAGEPHOTO_DEFAULT_TITLBUFFER, /**< Default Title Buffer Size */
GTA5_HEADERSIZE = RAGEPHOTO_GTA5_HEADERSIZE, /**< GTA V Header Size */
RDR2_HEADERSIZE = RAGEPHOTO_RDR2_HEADERSIZE, /**< 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 */
DescBufferTight = RAGEPHOTO_ERROR_DESCBUFFERTIGHT, /**< Description Buffer is too tight */
DescMallocError = RAGEPHOTO_ERROR_DESCMALLOCERROR, /**< Description Buffer can't be allocated */
DescReadError = RAGEPHOTO_ERROR_DESCREADERROR, /**< Description can't be read successfully */
HeaderBufferTight = RAGEPHOTO_ERROR_HEADERBUFFERTIGHT, /**< Header Buffer is too tight */
HeaderMallocError = RAGEPHOTO_ERROR_HEADERMALLOCERROR, /**< Header Buffer can't be allocated */
IncompatibleFormat = RAGEPHOTO_ERROR_INCOMPATIBLEFORMAT, /**< Format is incompatible */
IncompleteChecksum = RAGEPHOTO_ERROR_INCOMPLETECHECKSUM, /**< Header checksum is incomplete */
IncompleteDescBuffer = RAGEPHOTO_ERROR_INCOMPLETEDESCBUFFER, /**< Description Buffer Size is incomplete */
IncompleteDescMarker = RAGEPHOTO_ERROR_INCOMPLETEDESCMARKER, /**< Description Marker is incomplete */
IncompleteDescOffset = RAGEPHOTO_ERROR_INCOMPLETEDESCOFFSET, /**< Description Offset is incomplete */
IncompleteEOF = RAGEPHOTO_ERROR_INCOMPLETEEOF, /**< End Of File Offset is incomplete */
IncompleteHeader = RAGEPHOTO_ERROR_INCOMPLETEHEADER, /**< Header is incomplete */
IncompleteJendMarker = RAGEPHOTO_ERROR_INCOMPLETEJENDMARKER, /**< JEND Marker is incomplete */
IncompleteJpegMarker = RAGEPHOTO_ERROR_INCOMPLETEJPEGMARKER, /**< JPEG Marker is incomplete */
IncompleteJsonBuffer = RAGEPHOTO_ERROR_INCOMPLETEJSONBUFFER, /**< JSON Buffer Size is incomplete */
IncompleteJsonMarker = RAGEPHOTO_ERROR_INCOMPLETEJSONMARKER, /**< JSON Marker incomplete */
IncompleteJsonOffset = RAGEPHOTO_ERROR_INCOMPLETEJSONOFFSET, /**< JSON Offset incomplete */
IncompletePhotoBuffer = RAGEPHOTO_ERROR_INCOMPLETEPHOTOBUFFER, /**< Photo Buffer Size is incomplete */
IncompletePhotoSize = RAGEPHOTO_ERROR_INCOMPLETEPHOTOSIZE, /**< Photo Size is incomplete */
IncompleteTitleBuffer = RAGEPHOTO_ERROR_INCOMPLETETITLEBUFFER, /**< Title Buffer Size is incomplete */
IncompleteTitleMarker = RAGEPHOTO_ERROR_INCOMPLETETITLEMARKER, /**< Title Marker is incomplete */
IncompleteTitleOffset = RAGEPHOTO_ERROR_INCOMPLETETITLEOFFSET, /**< Title Offset is incomplete */
IncorrectDescMarker = RAGEPHOTO_ERROR_INCORRECTDESCMARKER, /**< Description Marker is incorrect */
IncorrectJendMarker = RAGEPHOTO_ERROR_INCORRECTJENDMARKER, /**< JEND Marker is incorrect */
IncorrectJpegMarker = RAGEPHOTO_ERROR_INCORRECTJPEGMARKER, /**< JPEG Marker is incorrect */
IncorrectJsonMarker = RAGEPHOTO_ERROR_INCORRECTJSONMARKER, /**< JSON Marker is incorrect */
IncorrectTitleMarker = RAGEPHOTO_ERROR_INCORRECTTITLEMARKER, /**< Title Marker is incorrect */
JsonBufferTight = RAGEPHOTO_ERROR_JSONBUFFERTIGHT, /**< JSON Buffer is too tight */
JsonMallocError = RAGEPHOTO_ERROR_JSONMALLOCERROR, /**< JSON Buffer can't be allocated */
JsonReadError = RAGEPHOTO_ERROR_JSONREADERROR, /**< JSON can't be read successfully */
NoError = RAGEPHOTO_ERROR_NOERROR, /**< Finished without errors */
NoFormatIdentifier = RAGEPHOTO_ERROR_NOFORMATIDENTIFIER, /**< No format detected, empty file */
PhotoBufferTight = RAGEPHOTO_ERROR_PHOTOBUFFERTIGHT, /**< Photo Buffer is too tight */
PhotoMallocError = RAGEPHOTO_ERROR_PHOTOMALLOCERROR, /**< Photo Buffer can't be allocated */
PhotoReadError = RAGEPHOTO_ERROR_PHOTOREADERROR, /**< Photo can't be read */
TitleBufferTight = RAGEPHOTO_ERROR_TITLEBUFFERTIGHT, /**< Title Buffer is too tight */
TitleMallocError = RAGEPHOTO_ERROR_TITLEMALLOCERROR, /**< Title Buffer can't be allocated */
TitleReadError = RAGEPHOTO_ERROR_TITLEREADERROR, /**< Title can't be read */
UnicodeInitError = RAGEPHOTO_ERROR_UNICODEINITERROR, /**< Failed to initialise Unicode decoder */
UnicodeHeaderError = RAGEPHOTO_ERROR_UNICODEHEADERERROR, /**< Header can't be encoded/decoded successfully */
Uninitialised = RAGEPHOTO_ERROR_UNINITIALISED, /**< Uninitialised, file access failed */
};
/** Photo Formats */
enum PhotoFormat : uint32_t {
GTA5 = 0x01000000UL, /**< GTA V Photo Format */
RDR2 = 0x04000000UL, /**< RDR 2 Photo Format */
GTA5 = RAGEPHOTO_FORMAT_GTA5, /**< GTA V Photo Format */
RDR2 = RAGEPHOTO_FORMAT_RDR2, /**< RDR 2 Photo Format */
};
/** Sign Initials */
enum SignInitials : uint32_t {
SIGTA5 = 0xE47AB81CUL, /**< GTA V Sign Initial */
SIRDR2 = 0x00FEEB1EUL, /**< RDR 2 Sign Initial */
SIGTA5 = RAGEPHOTO_SIGNINITIAL_GTA5, /**< GTA V Sign Initial */
SIRDR2 = RAGEPHOTO_SIGNINITIAL_RDR2, /**< RDR 2 Sign Initial */
};
RagePhotoA() {
photo() {
instance = ragephoto_open();
if (!instance)
throw std::runtime_error("ragephoto_t instance can't be allocated");
}
~RagePhotoA() {
~photo() {
ragephoto_close(instance);
}
/** Add a custom defined RagePhotoFormatParser. */
@ -193,14 +198,14 @@ public:
const char* description() const {
return ragephoto_getphotodesc(instance);
}
/** Returns the Photo JSON data. */
const char* json() const {
return ragephoto_getphotojson(instance);
}
/** Returns the Photo header. */
const char* header() const {
return ragephoto_getphotoheader(instance);
}
/** Returns the Photo JSON data. */
const char* json() const {
return ragephoto_getphotojson(instance);
}
/** Returns the Photo title. */
const char* title() const {
return ragephoto_getphototitle(instance);
@ -309,6 +314,10 @@ public:
void setFormat(uint32_t photoFormat) {
ragephoto_setphotoformat(instance, photoFormat);
}
/** Sets the Photo header. */
void setHeader(const char *header, uint32_t headerSum, uint32_t headerSum2 = 0) {
ragephoto_setphotoheader2(instance, header, headerSum, headerSum2);
}
/** Sets the Photo JPEG data.
* \param data JPEG data
* \param size JPEG data size
@ -328,9 +337,9 @@ public:
void setJson(const char *json, uint32_t bufferSize = 0) {
ragephoto_setphotojson(instance, json, bufferSize);
}
/** Sets the Photo header. */
void setHeader(const char *header, uint32_t headerSum, uint32_t headerSum2 = 0) {
ragephoto_setphotoheader2(instance, header, headerSum, headerSum2);
/** Sets a library flag. */
static void setLibraryFlag(RagePhotoLibraryFlag flag, bool state = true) {
ragephoto_setlibraryflag(flag, state);
}
/** Sets the Photo title. */
void setTitle(const char *title, uint32_t bufferSize = 0) {
@ -340,6 +349,8 @@ public:
private:
ragephoto_t instance;
};
} // ragephoto_c
#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"

View File

@ -1,6 +1,6 @@
/*****************************************************************************
* 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,
* are permitted provided that the following conditions are met:
@ -16,8 +16,8 @@
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
#ifndef RAGEPHOTO_HPP
#define RAGEPHOTO_HPP
#ifndef RAGEPHOTO_CXX_HPP
#define RAGEPHOTO_CXX_HPP
#ifdef __cplusplus
#include "RagePhotoLibrary.h"
@ -27,78 +27,81 @@
#include <cstdint>
#include <cstdio>
namespace ragephoto {
/**
* \brief GTA V and RDR 2 Photo Parser.
* \class ragephoto::photo RagePhoto.hpp RagePhoto
*/
class LIBRAGEPHOTO_CXX_PUBLIC RagePhoto
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 */
DEFAULT_GTA5_PHOTOBUFFER = RAGEPHOTO_DEFAULT_GTA5_PHOTOBUFFER, /**< GTA V default Photo Buffer Size */
DEFAULT_RDR2_PHOTOBUFFER = RAGEPHOTO_DEFAULT_RDR2_PHOTOBUFFER, /**< RDR 2 default Photo Buffer Size */
DEFAULT_DESCBUFFER = RAGEPHOTO_DEFAULT_DESCBUFFER, /**< Default Description Buffer Size */
DEFAULT_JSONBUFFER = RAGEPHOTO_DEFAULT_JSONBUFFER, /**< Default JSON Buffer Size */
DEFAULT_TITLBUFFER = RAGEPHOTO_DEFAULT_TITLBUFFER, /**< Default Title Buffer Size */
GTA5_HEADERSIZE = RAGEPHOTO_GTA5_HEADERSIZE, /**< GTA V Header Size */
RDR2_HEADERSIZE = RAGEPHOTO_RDR2_HEADERSIZE, /**< 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 */
DescBufferTight = RAGEPHOTO_ERROR_DESCBUFFERTIGHT, /**< Description Buffer is too tight */
DescMallocError = RAGEPHOTO_ERROR_DESCMALLOCERROR, /**< Description Buffer can't be allocated */
DescReadError = RAGEPHOTO_ERROR_DESCREADERROR, /**< Description can't be read successfully */
HeaderBufferTight = RAGEPHOTO_ERROR_HEADERBUFFERTIGHT, /**< Header Buffer is too tight */
HeaderMallocError = RAGEPHOTO_ERROR_HEADERMALLOCERROR, /**< Header Buffer can't be allocated */
IncompatibleFormat = RAGEPHOTO_ERROR_INCOMPATIBLEFORMAT, /**< Format is incompatible */
IncompleteChecksum = RAGEPHOTO_ERROR_INCOMPLETECHECKSUM, /**< Header checksum is incomplete */
IncompleteDescBuffer = RAGEPHOTO_ERROR_INCOMPLETEDESCBUFFER, /**< Description Buffer Size is incomplete */
IncompleteDescMarker = RAGEPHOTO_ERROR_INCOMPLETEDESCMARKER, /**< Description Marker is incomplete */
IncompleteDescOffset = RAGEPHOTO_ERROR_INCOMPLETEDESCOFFSET, /**< Description Offset is incomplete */
IncompleteEOF = RAGEPHOTO_ERROR_INCOMPLETEEOF, /**< End Of File Offset is incomplete */
IncompleteHeader = RAGEPHOTO_ERROR_INCOMPLETEHEADER, /**< Header is incomplete */
IncompleteJendMarker = RAGEPHOTO_ERROR_INCOMPLETEJENDMARKER, /**< JEND Marker is incomplete */
IncompleteJpegMarker = RAGEPHOTO_ERROR_INCOMPLETEJPEGMARKER, /**< JPEG Marker is incomplete */
IncompleteJsonBuffer = RAGEPHOTO_ERROR_INCOMPLETEJSONBUFFER, /**< JSON Buffer Size is incomplete */
IncompleteJsonMarker = RAGEPHOTO_ERROR_INCOMPLETEJSONMARKER, /**< JSON Marker incomplete */
IncompleteJsonOffset = RAGEPHOTO_ERROR_INCOMPLETEJSONOFFSET, /**< JSON Offset incomplete */
IncompletePhotoBuffer = RAGEPHOTO_ERROR_INCOMPLETEPHOTOBUFFER, /**< Photo Buffer Size is incomplete */
IncompletePhotoSize = RAGEPHOTO_ERROR_INCOMPLETEPHOTOSIZE, /**< Photo Size is incomplete */
IncompleteTitleBuffer = RAGEPHOTO_ERROR_INCOMPLETETITLEBUFFER, /**< Title Buffer Size is incomplete */
IncompleteTitleMarker = RAGEPHOTO_ERROR_INCOMPLETETITLEMARKER, /**< Title Marker is incomplete */
IncompleteTitleOffset = RAGEPHOTO_ERROR_INCOMPLETETITLEOFFSET, /**< Title Offset is incomplete */
IncorrectDescMarker = RAGEPHOTO_ERROR_INCORRECTDESCMARKER, /**< Description Marker is incorrect */
IncorrectJendMarker = RAGEPHOTO_ERROR_INCORRECTJENDMARKER, /**< JEND Marker is incorrect */
IncorrectJpegMarker = RAGEPHOTO_ERROR_INCORRECTJPEGMARKER, /**< JPEG Marker is incorrect */
IncorrectJsonMarker = RAGEPHOTO_ERROR_INCORRECTJSONMARKER, /**< JSON Marker is incorrect */
IncorrectTitleMarker = RAGEPHOTO_ERROR_INCORRECTTITLEMARKER, /**< Title Marker is incorrect */
JsonBufferTight = RAGEPHOTO_ERROR_JSONBUFFERTIGHT, /**< JSON Buffer is too tight */
JsonMallocError = RAGEPHOTO_ERROR_JSONMALLOCERROR, /**< JSON Buffer can't be allocated */
JsonReadError = RAGEPHOTO_ERROR_JSONREADERROR, /**< JSON can't be read successfully */
NoError = RAGEPHOTO_ERROR_NOERROR, /**< Finished without errors */
NoFormatIdentifier = RAGEPHOTO_ERROR_NOFORMATIDENTIFIER, /**< No format detected, empty file */
PhotoBufferTight = RAGEPHOTO_ERROR_PHOTOBUFFERTIGHT, /**< Photo Buffer is too tight */
PhotoMallocError = RAGEPHOTO_ERROR_PHOTOMALLOCERROR, /**< Photo Buffer can't be allocated */
PhotoReadError = RAGEPHOTO_ERROR_PHOTOREADERROR, /**< Photo can't be read */
TitleBufferTight = RAGEPHOTO_ERROR_TITLEBUFFERTIGHT, /**< Title Buffer is too tight */
TitleMallocError = RAGEPHOTO_ERROR_TITLEMALLOCERROR, /**< Title Buffer can't be allocated */
TitleReadError = RAGEPHOTO_ERROR_TITLEREADERROR, /**< Title can't be read */
UnicodeInitError = RAGEPHOTO_ERROR_UNICODEINITERROR, /**< Failed to initialise Unicode decoder */
UnicodeHeaderError = RAGEPHOTO_ERROR_UNICODEHEADERERROR, /**< Header can't be encoded/decoded successfully */
Uninitialised = RAGEPHOTO_ERROR_UNINITIALISED, /**< Uninitialised, file access failed */
};
/** Photo Formats */
enum PhotoFormat : uint32_t {
GTA5 = 0x01000000UL, /**< GTA V Photo Format */
RDR2 = 0x04000000UL, /**< RDR 2 Photo Format */
GTA5 = RAGEPHOTO_FORMAT_GTA5, /**< GTA V Photo Format */
RDR2 = RAGEPHOTO_FORMAT_RDR2, /**< RDR 2 Photo Format */
};
/** Sign Initials */
enum SignInitials : uint32_t {
SIGTA5 = 0xE47AB81CUL, /**< GTA V Sign Initial */
SIRDR2 = 0x00FEEB1EUL, /**< RDR 2 Sign Initial */
SIGTA5 = RAGEPHOTO_SIGNINITIAL_GTA5, /**< GTA V Sign Initial */
SIRDR2 = RAGEPHOTO_SIGNINITIAL_RDR2, /**< RDR 2 Sign Initial */
};
RagePhoto();
~RagePhoto();
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. */
@ -130,8 +133,8 @@ public:
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* json() const; /**< Returns the Photo JSON data. */
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*. */
@ -154,8 +157,8 @@ public:
* \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. */
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. */
@ -167,6 +170,7 @@ public:
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). */
void setHeader(const char *header, uint32_t headerSum, uint32_t headerSum2 = 0); /**< Sets the Photo header. */
/** Sets the Photo JPEG data.
* \param data JPEG data
* \param size JPEG data size
@ -179,13 +183,15 @@ public:
*/
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. */
static void setLibraryFlag(RagePhotoLibraryFlag flag, bool state = true); /**< Sets a library flag. */
void setTitle(const char *title, uint32_t bufferSize = 0); /**< Sets the Photo title. */
private:
RagePhotoData *m_data;
RagePhotoFormatParser *m_parser;
};
} // ragephoto
#endif // __cplusplus
#endif // RAGEPHOTO_HPP
#endif // RAGEPHOTO_CXX_HPP

View File

@ -1,6 +1,6 @@
/*****************************************************************************
* 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,
* 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.
*****************************************************************************/
#include <RagePhotoB>
#include <RagePhoto>
#include <fstream>
int main(int argc, char *argv[])
@ -27,18 +27,18 @@ int main(int argc, char *argv[])
}
// Initialise RagePhoto
RagePhotoB ragePhoto;
RagePhoto ragePhoto;
// Load Photo
const bool loaded = ragePhoto.loadFile(argv[1]);
if (!loaded) {
const int32_t error = ragePhoto.error();
if (error == RagePhotoB::Uninitialised) {
if (error == RagePhoto::Uninitialised) {
std::cout << "Failed to open file: " << argv[1] << std::endl;
return 1;
}
else if (error <= RagePhotoB::PhotoReadError) {
else if (error <= RagePhoto::PhotoReadError) {
std::cout << "Failed to load photo" << std::endl;
return 1;
}
@ -60,9 +60,9 @@ int main(int argc, char *argv[])
}
const uint32_t photoFormat = ragePhoto.format();
if (photoFormat == RagePhotoB::GTA5)
if (photoFormat == RagePhoto::GTA5)
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;
else
std::cout << "Photo successfully exported" << std::endl;

25
src/python/__init__.py Normal file
View File

@ -0,0 +1,25 @@
##############################################################################
# libragephoto for Python
# Copyright (C) 2023-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.
##############################################################################
from .ragephoto import RagePhoto
__all__ = [
"libragephoto_loader", # libragephoto Loader Module
"ragephoto", # RagePhoto Module
"RagePhoto" # RagePhoto API
]

View File

@ -0,0 +1,20 @@
##############################################################################
# libragephoto for Python
# 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.
##############################################################################
VERSION = (@ragephoto_VERSION_MAJOR@, @ragephoto_VERSION_MINOR@, @ragephoto_VERSION_PATCH@)
__version__ = "@ragephoto_VERSION@"

View File

@ -0,0 +1,88 @@
##############################################################################
# libragephoto for Python
# Copyright (C) 2023-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.
##############################################################################
from ctypes import *
from ctypes.util import find_library
from pathlib import Path
from platform import system
if system() == "Windows":
bundle_library_path = Path(__file__).parent.resolve() / "libragephoto.dll"
if bundle_library_path.is_file():
library_path = str(bundle_library_path)
else:
bundle_library_path = Path(__file__).parent.resolve() / "libragephoto.so"
if bundle_library_path.is_file():
library_path = str(bundle_library_path)
else:
library_path = find_library("ragephoto")
if not library_path:
raise ImportError("libragephoto is required.")
libragephoto = cdll.LoadLibrary(library_path)
libragephoto.ragephoto_open.restype = c_void_p
libragephoto.ragephoto_clear.argtypes = [c_void_p]
libragephoto.ragephoto_close.argtypes = [c_void_p]
libragephoto.ragephoto_load.argtypes = [c_void_p, POINTER(c_char), c_size_t]
libragephoto.ragephoto_load.restype = c_bool
libragephoto.ragephoto_loadfile.argtypes = [c_void_p, c_char_p]
libragephoto.ragephoto_loadfile.restype = c_bool
libragephoto.ragephoto_error.argtypes = [c_void_p]
libragephoto.ragephoto_error.restype = c_int32
libragephoto.ragephoto_getphotodesc.argtypes = [c_void_p]
libragephoto.ragephoto_getphotodesc.restype = c_char_p
libragephoto.ragephoto_getphotoformat.argtypes = [c_void_p]
libragephoto.ragephoto_getphotoformat.restype = c_uint32
libragephoto.ragephoto_getphotojpeg.argtypes = [c_void_p]
libragephoto.ragephoto_getphotojpeg.restype = POINTER(c_char)
libragephoto.ragephoto_getphotojson.argtypes = [c_void_p]
libragephoto.ragephoto_getphotojson.restype = c_char_p
libragephoto.ragephoto_getphotoheader.argtypes = [c_void_p]
libragephoto.ragephoto_getphotoheader.restype = c_char_p
libragephoto.ragephoto_getphotosign.argtypes = [c_void_p]
libragephoto.ragephoto_getphotosign.restype = c_uint64
libragephoto.ragephoto_getphotosignf.argtypes = [c_void_p, c_uint32]
libragephoto.ragephoto_getphotosignf.restype = c_uint64
libragephoto.ragephoto_getphotosize.argtypes = [c_void_p]
libragephoto.ragephoto_getphotosize.restype = c_uint32
libragephoto.ragephoto_getphototitle.argtypes = [c_void_p]
libragephoto.ragephoto_getphototitle.restype = c_char_p
libragephoto.ragephoto_getsavesize.argtypes = [c_void_p]
libragephoto.ragephoto_getsavesize.restype = c_size_t
libragephoto.ragephoto_getsavesizef.argtypes = [c_void_p, c_uint32]
libragephoto.ragephoto_getsavesizef.restype = c_size_t
libragephoto.ragephoto_save.argtypes = [c_void_p, POINTER(c_char)]
libragephoto.ragephoto_save.restype = c_bool
libragephoto.ragephoto_savef.argtypes = [c_void_p, POINTER(c_char), c_uint32]
libragephoto.ragephoto_savef.restype = c_bool
libragephoto.ragephoto_savefile.argtypes = [c_void_p, c_char_p]
libragephoto.ragephoto_savefile.restype = c_bool
libragephoto.ragephoto_savefilef.argtypes = [c_void_p, c_char_p, c_uint32]
libragephoto.ragephoto_savefilef.restype = c_bool
libragephoto.ragephoto_setbufferdefault.argtypes = [c_void_p]
libragephoto.ragephoto_setbufferoffsets.argtypes = [c_void_p]
libragephoto.ragephoto_setphotodesc.argtypes = [c_void_p, c_char_p, c_uint32]
libragephoto.ragephoto_setphotoformat.argtypes = [c_void_p, c_uint32]
libragephoto.ragephoto_setphotojpeg.argtypes = [c_void_p, POINTER(c_char), c_uint32, c_uint32]
libragephoto.ragephoto_setphotojpeg.restype = c_bool
libragephoto.ragephoto_setphotojson.argtypes = [c_void_p, c_char_p, c_uint32]
libragephoto.ragephoto_setphotoheader.argtypes = [c_void_p, c_char_p, c_uint32]
libragephoto.ragephoto_setphotoheader2.argtypes = [c_void_p, c_char_p, c_uint32, c_uint32]
libragephoto.ragephoto_setphototitle.argtypes = [c_void_p, c_char_p, c_uint32]
libragephoto.ragephoto_version.restype = c_char_p

View File

@ -0,0 +1,24 @@
[project]
name = "ragephoto"
version = "@ragephoto_VERSION@"
authors = [
{ name = "Syping" },
]
description = "libragephoto for Python"
requires-python = ">=3.6"
classifiers = [
"License :: OSI Approved :: BSD-2-Clause",
]
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"
[project.urls]
"Homepage" = "https://libragephoto.syping.de/"
[tool.setuptools]
include-package-data = true
[tool.setuptools.package-data]
ragephoto = ["*.dll", "*.so"]

251
src/python/ragephoto.py Normal file
View File

@ -0,0 +1,251 @@
##############################################################################
# libragephoto for Python
# Copyright (C) 2023-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.
##############################################################################
from .libragephoto_loader import *
from enum import IntEnum
from json import loads as parseJson
from json import dumps as serializeJson
class RagePhoto:
class DefaultSize(IntEnum):
DEFAULT_GTA5_PHOTOBUFFER = 524288
DEFAULT_RDR2_PHOTOBUFFER = 1048576
DEFAULT_DESCBUFFER = 256
DEFAULT_JSONBUFFER = 3072
DEFAULT_TITLBUFFER = 256
class Error(IntEnum):
DescBufferTight = 39
DescMallocError = 31
DescReadError = 32
HeaderBufferTight = 35
HeaderMallocError = 4
IncompatibleFormat = 2
IncompleteChecksum = 7
IncompleteDescBuffer = 30
IncompleteDescMarker = 28
IncompleteDescOffset = 11
IncompleteEOF = 8
IncompleteHeader = 3
IncompleteJendMarker = 33
IncompleteJpegMarker = 12
IncompleteJsonBuffer = 20
IncompleteJsonMarker = 18
IncompleteJsonOffset = 9
IncompletePhotoBuffer = 14
IncompletePhotoSize = 15
IncompleteTitleBuffer = 25
IncompleteTitleMarker = 23
IncompleteTitleOffset = 10
IncorrectDescMarker = 29
IncorrectJendMarker = 34
IncorrectJpegMarker = 13
IncorrectJsonMarker = 19
IncorrectTitleMarker = 24
JsonBufferTight = 37
JsonMallocError = 21
JsonReadError = 22
NoError = 255
NoFormatIdentifier = 1
PhotoBufferTight = 36
PhotoMallocError = 16
PhotoReadError = 17
TitleBufferTight = 38
TitleMallocError = 26
TitleReadError = 27
UnicodeInitError = 5
UnicodeHeaderError = 6
Uninitialised = 0
class PhotoFormat(IntEnum):
GTA5 = 0x01000000
RDR2 = 0x04000000
def __init__(self):
self.__instance = libragephoto.ragephoto_open()
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
libragephoto.ragephoto_close(self.__instance)
self.__instance = None
def __del__(self):
if self.__instance is not None:
libragephoto.ragephoto_close(self.__instance)
def clear(self):
libragephoto.ragephoto_clear(self.__instance)
def load(self, data):
return libragephoto.ragephoto_load(self.__instance, data, len(data))
def loadFile(self, file):
if isinstance(file, str):
return libragephoto.ragephoto_loadfile(self.__instance, file.encode())
else:
return libragephoto.ragephoto_loadfile(self.__instance, file)
def error(self):
return libragephoto.ragephoto_error(self.__instance)
def description(self):
_desc = libragephoto.ragephoto_getphotodesc(self.__instance)
if _desc:
return _desc
else:
return b""
def format(self):
return libragephoto.ragephoto_getphotoformat(self.__instance)
def jpeg(self):
_jpeg = libragephoto.ragephoto_getphotojpeg(self.__instance)
if _jpeg:
return _jpeg[:self.jpegSize()]
else:
return b""
def jpegSign(self, photoFormat = None):
if photoFormat is None:
return libragephoto.ragephoto_getphotosign(self.__instance)
else:
return libragephoto.ragephoto_getphotosignf(self.__instance, photoFormat)
def jpegSize(self):
return libragephoto.ragephoto_getphotosize(self.__instance)
def json(self):
_json = libragephoto.ragephoto_getphotojson(self.__instance)
if _json:
return _json
else:
return b""
def header(self):
_header = libragephoto.ragephoto_getphotoheader(self.__instance)
if _header:
return _header
else:
return b""
def save(self, photoFormat = None):
_data = bytearray(self.saveSize(photoFormat))
_ptr = (c_char * len(_data)).from_buffer(_data)
if photoFormat is None:
_ret = libragephoto.ragephoto_save(self.__instance, _ptr)
else:
_ret = libragephoto.ragephoto_savef(self.__instance, _ptr, photoFormat)
if _ret:
return _data
else:
return None
def saveFile(self, file, photoFormat = None):
if isinstance(file, str):
_file = file.encode()
else:
_file = file
if photoFormat is None:
return libragephoto.ragephoto_savefile(self.__instance, _file)
else:
return libragephoto.ragephoto_savefilef(self.__instance, _file, photoFormat)
def saveSize(self, photoFormat = None):
if photoFormat is None:
return libragephoto.ragephoto_getsavesize(self.__instance)
else:
return libragephoto.ragephoto_getsavesizef(self.__instance, photoFormat)
def setBufferDefault(self):
return libragephoto.ragephoto_setbufferdefault(self.__instance)
def setBufferOffsets(self):
return libragephoto.ragephoto_setbufferoffsets(self.__instance)
def setDescription(self, desc, buffer = None):
if isinstance(desc, str):
_desc = desc.encode()
else:
_desc = desc
if buffer is None:
libragephoto.ragephoto_setphotodesc(self.__instance, _desc, self.DefaultSize.DEFAULT_DESCBUFFER)
else:
libragephoto.ragephoto_setphotodesc(self.__instance, _desc, buffer)
def setFormat(self, photoFormat):
libragephoto.ragephoto_setphotoformat(self.__instance, photoFormat)
def setJpeg(self, jpeg, buffer = None):
_buffer = 0
if buffer is None:
_format = self.format()
if _format == self.PhotoFormat.GTA5:
_buffer = self.DefaultSize.DEFAULT_GTA5_PHOTOBUFFER
elif _format == self.PhotoFormat.RDR2:
_buffer = self.DefaultSize.DEFAULT_RDR2_PHOTOBUFFER
if _buffer < len(jpeg):
_buffer = len(jpeg)
return libragephoto.ragephoto_setphotojpeg(self.__instance, jpeg, len(jpeg), _buffer)
def setJson(self, json, buffer = None):
if isinstance(json, str):
_json = json.encode()
else:
_json = json
if buffer is None:
libragephoto.ragephoto_setphotojson(self.__instance, _json, self.DefaultSize.DEFAULT_JSONBUFFER)
else:
libragephoto.ragephoto_setphotojson(self.__instance, _json, buffer)
def setHeader(self, header, headerSum1, headerSum2 = 0):
if isinstance(header, str):
_header = header.encode()
else:
_header = header
libragephoto.ragephoto_setphotoheader2(self.__instance, _header, headerSum1, headerSum2)
def setTitle(self, title, buffer = None):
if isinstance(title, str):
_title = title.encode()
else:
_title = title
if buffer is None:
libragephoto.ragephoto_setphototitle(self.__instance, _title, self.DefaultSize.DEFAULT_TITLBUFFER)
else:
libragephoto.ragephoto_setphototitle(self.__instance, _title, buffer)
def title(self):
_title = libragephoto.ragephoto_getphototitle(self.__instance)
if _title:
return _title
else:
return b""
def updateSign(self):
try:
_json = parseJson(self.json())
except JSONDecodeError:
return False
_json["sign"] = self.jpegSign()
self.setJson(serializeJson(_json, separators=(',', ':')))
return True
def version(self):
return libragephoto.ragephoto_version()

34
src/python/setup.py.in Normal file
View File

@ -0,0 +1,34 @@
#!/usr/bin/env python3
##############################################################################
# libragephoto for Python
# 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.
##############################################################################
from setuptools import setup, find_packages
setup(
name="ragephoto",
version="@ragephoto_VERSION@",
author="Syping",
packages=["ragephoto"],
package_data={"ragephoto":["*.dll","*.so"]},
include_package_data=True,
url="https://libragephoto.syping.de/",
description="libragephoto for Python",
classifiers=[
"License :: OSI Approved :: BSD-2-Clause",
]
)

View File

@ -206,7 +206,7 @@ int main(int argc, char *argv[])
// English
src_s = sizeof(photoHeader_english);
dst_s = sizeof(photoString);
char *src_english = (char*)photoHeader_english;
char *src_english = reinterpret_cast<char*>(photoHeader_english);
char *dst_english = photoString;
ret = iconv(iconv_in, &src_english, &src_s, &dst_english, &dst_s);
if (ret == static_cast<size_t>(-1))
@ -217,7 +217,7 @@ int main(int argc, char *argv[])
// Japanese
src_s = sizeof(photoHeader_japanese);
dst_s = sizeof(photoString);
char *src_japanese = (char*)photoHeader_japanese;
char *src_japanese = reinterpret_cast<char*>(photoHeader_japanese);
char *dst_japanese = photoString;
ret = iconv(iconv_in, &src_japanese, &src_s, &dst_japanese, &dst_s);
if (ret == static_cast<size_t>(-1))
@ -228,7 +228,7 @@ int main(int argc, char *argv[])
// Korean
src_s = sizeof(photoHeader_korean);
dst_s = sizeof(photoString);
char *src_korean = (char*)photoHeader_korean;
char *src_korean = reinterpret_cast<char*>(photoHeader_korean);
char *dst_korean = photoString;
ret = iconv(iconv_in, &src_korean, &src_s, &dst_korean, &dst_s);
if (ret == static_cast<size_t>(-1))
@ -239,7 +239,7 @@ int main(int argc, char *argv[])
// Russian
src_s = sizeof(photoHeader_russian);
dst_s = sizeof(photoString);
char *src_russian = (char*)photoHeader_russian;
char *src_russian = reinterpret_cast<char*>(photoHeader_russian);
char *dst_russian = photoString;
ret = iconv(iconv_in, &src_russian, &src_s, &dst_russian, &dst_s);
if (ret == static_cast<size_t>(-1))
@ -250,7 +250,7 @@ int main(int argc, char *argv[])
// Taiwanese
src_s = sizeof(photoHeader_taiwanese);
dst_s = sizeof(photoString);
char *src_taiwanese = (char*)photoHeader_taiwanese;
char *src_taiwanese = reinterpret_cast<char*>(photoHeader_taiwanese);
char *dst_taiwanese = photoString;
ret = iconv(iconv_in, &src_taiwanese, &src_s, &dst_taiwanese, &dst_s);
if (ret == static_cast<size_t>(-1))