libwuhbutils/include/wuhb_utils/utils.h

226 lines
12 KiB
C
Raw Normal View History

2022-04-15 17:33:16 +02:00
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
2022-09-03 14:19:11 +02:00
typedef enum WUHBUtilsStatus {
2022-04-15 17:33:16 +02:00
WUHB_UTILS_RESULT_SUCCESS = 0,
2022-09-03 14:19:11 +02:00
WUHB_UTILS_RESULT_MODULE_NOT_FOUND = -0x1,
WUHB_UTILS_RESULT_MODULE_MISSING_EXPORT = -0x2,
WUHB_UTILS_RESULT_UNSUPPORTED_VERSION = -0x3,
WUHB_UTILS_RESULT_INVALID_ARGUMENT = -0x10,
WUHB_UTILS_RESULT_NO_MEMORY = -0x11,
WUHB_UTILS_RESULT_MOUNT_NAME_TAKEN = -0x12,
WUHB_UTILS_RESULT_MOUNT_NOT_FOUND = -0x13,
WUHB_UTILS_RESULT_FILE_NOT_FOUND = -0x14,
WUHB_UTILS_RESULT_FILE_HANDLE_NOT_FOUND = -0x15,
WUHB_UTILS_RESULT_MOUNT_FAILED = -0x16,
WUHB_UTILS_RESULT_LIB_UNINITIALIZED = -0x20,
WUHB_UTILS_RESULT_UNSUPPORTED_COMMAND = -0x21,
WUHB_UTILS_RESULT_UNKNOWN_ERROR = -0x100,
} WUHBUtilsStatus;
2022-04-15 17:33:16 +02:00
typedef uint32_t WUHBUtilsVersion;
typedef uint32_t WUHBFileHandle;
2022-09-03 14:19:11 +02:00
#define WUHB_UTILS_MODULE_VERSION_ERROR 0xFFFFFFFF
2022-04-15 17:33:16 +02:00
typedef enum WUHBUtilsApiErrorType {
WUHB_UTILS_API_ERROR_NONE = 0,
WUHB_UTILS_API_ERROR_INVALID_ARG = -1,
WUHB_UTILS_API_ERROR_MOUNT_NAME_TAKEN = -2,
WUHB_UTILS_API_ERROR_MOUNT_NOT_FOUND = -3,
WUHB_UTILS_API_ERROR_FILE_NOT_FOUND = -4,
WUHB_UTILS_API_ERROR_FILE_HANDLE_NOT_FOUND = -5,
WUHB_UTILS_API_ERROR_NO_MEMORY = -6,
WUHB_UTILS_API_ERROR_MOUNT_FAILED = -7,
} WUHBUtilsApiErrorType;
typedef struct {
uint64_t length; // Offset of the file's data.
uint64_t offset; // Length of the file's data.
} WUHBRPXInfo;
typedef enum {
BundleSource_FileDescriptor, /* The POSIX file api be used, use paths like fs:/vol/external01/my.wuhb */
BundleSource_FileDescriptor_CafeOS, /* The native CafeOS file api will be used, use paths like /vol/external01/my.wuhb */
} BundleSource;
/**
* This function has to be called before any other function of this lib (except WUHBUtils_GetVersion) can be used.
*
2022-09-03 14:19:11 +02:00
* @return WUHB_UTILS_RESULT_SUCCESS: The library has been initialized successfully. Other functions can now be used. <br>
* WUHB_UTILS_RESULT_MODULE_NOT_FOUND: The module could not be found. Make sure the module is loaded.<br>
* WUHB_UTILS_RESULT_MODULE_MISSING_EXPORT: The module is missing an expected export.<br>
2022-04-15 17:33:16 +02:00
* WUHB_UTILS_RESULT_UNSUPPORTED_VERSION: The version of the loaded module is not compatible with this version of the lib.
*/
WUHBUtilsStatus WUHBUtils_InitLibrary();
2022-04-15 17:33:16 +02:00
2022-09-03 14:19:11 +02:00
/**
* Deinitializes the WUHBUtils lib
* @return WUHB_UTILS_RESULT_SUCCESS
*/
2022-09-03 13:18:13 +02:00
WUHBUtilsStatus WUHBUtils_DeInitLibrary();
2022-04-15 17:33:16 +02:00
/**
2022-09-03 14:19:11 +02:00
* Retrieves the API Version of the loaded WUHBUtils.<br>
* <br>
* @param outVersion pointer to the variable where the version will be stored.
*
* @return WUHB_UTILS_RESULT_SUCCESS: The API version has been store in the version ptr.<br>
* WUHB_UTILS_RESULT_MODULE_NOT_FOUND: The module could not be found. Make sure the module is loaded.<br>
* WUHB_UTILS_RESULT_MODULE_MISSING_EXPORT: The module is missing an expected export.<br>
* WUHB_UTILS_RESULT_INVALID_ARGUMENT: Invalid version pointer.<br>
* WUHB_UTILS_RESULT_UNKNOWN_ERROR: Retrieving the module version failed.
2022-04-15 17:33:16 +02:00
*/
2022-09-03 14:19:11 +02:00
WUHBUtilsStatus WUHBUtils_GetVersion(WUHBUtilsVersion *outVersion);
2022-04-15 17:33:16 +02:00
/**
2022-09-03 14:19:11 +02:00
* Mounts a given bundle to a given mount path. Use WUHBUtils_UnmountBundle to unmount it.<br>
*<br>
* Caution: the mounted path is only available via the WUHBUtils_FileXXX functions.<br>
2022-04-15 17:33:16 +02:00
*
* @param name path the bundle should be mounted to (e.g. "bundle")
* @param bundle_path path to the bundle file (path may depend on the BundleSource)
* @param source type of source. See BundleSource for more information.
* @param outRes on success the result of the function will be stored here.
2022-09-03 14:19:11 +02:00
* @return WUHB_UTILS_RESULT_SUCCESS: MountBundle has been called successfully. The result has been written to outRes.<br>
* *outRes is >= 0 on success.<br>
* *outRes is < 0 on error.<br>
* WUHB_UTILS_RESULT_LIB_UNINITIALIZED: "WUHBUtils_Init()" was not called before.<br>
* WUHB_UTILS_RESULT_UNSUPPORTED_COMMAND: Command not supported by the currently loaded WUHBUtilsModule version.<br>
* WUHB_UTILS_RESULT_INVALID_ARGUMENT: "name", "path" or "outRes" was NULL<br>
* WUHB_UTILS_RESULT_MOUNT_NAME_TAKEN: The given name has been already taken.<br>
2022-04-15 17:33:16 +02:00
* WUHB_UTILS_RESULT_UNKNOWN_ERROR: Unknown error.
*/
WUHBUtilsStatus WUHBUtils_MountBundle(const char *name, const char *path, BundleSource source, int32_t *outRes);
/**
*
* @param name path the bundle should be unmounted to (e.g. "bundle")
* @param outRes (optional) on success the result of the function will be stored here.
2022-09-03 14:19:11 +02:00
* @return WUHB_UTILS_RESULT_SUCCESS: UnmountBundle has been called successfully. The result has been written to outRes.<br>
* *outRes is >= 0 on success.<br>
* *outRes is < 0 on error.<br>
* WUHB_UTILS_RESULT_LIB_UNINITIALIZED: "WUHBUtils_Init()" was not called before.<br>
* WUHB_UTILS_RESULT_UNSUPPORTED_COMMAND: Command not supported by the currently loaded WUHBUtilsModule version.<br><br>
* WUHB_UTILS_RESULT_INVALID_ARGUMENT: "name", was NULL<br>
* WUHB_UTILS_API_ERROR_MOUNT_NOT_FOUND: "name" was not mounted.<br>
2022-04-15 17:33:16 +02:00
* WUHB_UTILS_RESULT_UNKNOWN_ERROR: Unknown error.
*/
WUHBUtilsStatus WUHBUtils_UnmountBundle(const char *name, int32_t *outRes);
/**
2022-09-03 14:19:11 +02:00
* Opens a file inside a mounted bundle.<br>
* (only read only is supported and is default)<br>
* <br>
* Make sure the bundle is mounted via WUHBUtils_MountBundle.<br>
* <br>
* If a given files does not exists, it's checks for a compressed version<br>
* (at name + ".gz). If a compressed file was found, all file reads will be<br>
* decompressed on the fly.<br>
2022-04-15 17:33:16 +02:00
*
* @param name path to the file that should be opened.
* @param outHandle on success the result of the function will be stored here.
* @return WUHB_UTILS_RESULT_SUCCESS: file has been opened successfully.
* The file handle has been stored in *outHandle
2022-09-03 14:19:11 +02:00
* WUHB_UTILS_RESULT_LIB_UNINITIALIZED: "WUHBUtils_Init()" was not called before.<br>
* WUHB_UTILS_RESULT_UNSUPPORTED_COMMAND: Command not supported by the currently loaded WUHBUtilsModule version.<br><br>
* WUHB_UTILS_RESULT_INVALID_ARGUMENT: "name", was NULL<br>
* WUHB_UTILS_API_ERROR_FILE_NOT_FOUND: file at path "name" was not found.<br>
* WUHB_UTILS_API_ERROR_NO_MEMORY: not enough memory.<br>
2022-04-15 17:33:16 +02:00
* WUHB_UTILS_RESULT_UNKNOWN_ERROR: Unknown error.
*/
WUHBUtilsStatus WUHBUtils_FileOpen(const char *name, WUHBFileHandle *outHandle);
/**
* Reads from a given file.
*
* @param handle File handle to be read from.
* @param buffer buffer where data will be written to.
* Align to 0x40 for best performance
* @param size maximum bytes this function should read into buffer
2022-09-03 14:19:11 +02:00
* @return WUHB_UTILS_RESULT_SUCCESS file read has been called successfully. The result has been written to *outRes.<br>
* On success, the number of bytes read is set to *outRes (zero indicates<br>
* end of file), and the file position is advanced by this number.<br>
* It is not an error if this number is smaller than the number of<br>
* bytes requested.<br>
* On error, *outRes is set to -1.<br>
* WUHB_UTILS_RESULT_LIB_UNINITIALIZED: "WUHBUtils_Init()" was not called before.<br>
* WUHB_UTILS_RESULT_UNSUPPORTED_COMMAND: Command not supported by the currently loaded WUHBUtilsModule version.<br>
* WUHB_UTILS_RESULT_INVALID_ARGUMENT: "buffer" or "outRes" is NULL<br>
* WUHB_UTILS_API_ERROR_FILE_HANDLE_NOT_FOUND: file handle is invalid.<br>
2022-04-15 17:33:16 +02:00
* WUHB_UTILS_RESULT_UNKNOWN_ERROR: Unknown error.
*/
WUHBUtilsStatus WUHBUtils_FileRead(WUHBFileHandle handle, uint8_t *buffer, uint32_t size, int32_t *outRes);
/**
* Closes a given file
*
* example: if(WUHBUtils_FileClose(fileHandle) != WUHB_UTILS_RESULT_SUCCESS) { //error while closing the file }
*
* @param handle File to be closed
2022-09-03 14:19:11 +02:00
* @return WUHB_UTILS_RESULT_SUCCESS file handle has been closed successfully.<br>
* WUHB_UTILS_RESULT_LIB_UNINITIALIZED: "WUHBUtils_Init()" was not called before.<br>
* WUHB_UTILS_RESULT_UNSUPPORTED_COMMAND: Command not supported by the currently loaded WUHBUtilsModule version.<br>
* WUHB_UTILS_API_ERROR_FILE_HANDLE_NOT_FOUND: file handle is invalid.<br>
2022-04-15 17:33:16 +02:00
* WUHB_UTILS_RESULT_UNKNOWN_ERROR: Unknown error.
*/
WUHBUtilsStatus WUHBUtils_FileClose(WUHBFileHandle handle);
/**
* Checks if a given file exists
*
* example: int32_t res; if(WUHBUtils_FileExists("bundle:/meta/meta.ini", &res) == WUHB_UTILS_RESULT_SUCCESS && res) { // file exists}
*
* @param name Paths to be checked
* @param outRes PTR to the int where the file check result will be stored.
2022-09-03 14:19:11 +02:00
* @return WUHB_UTILS_RESULT_SUCCESS file exists check has been called. The result has been written to *outRes. <br>
* WUHB_UTILS_RESULT_LIB_UNINITIALIZED: "WUHBUtils_Init()" was not called before.<br>
* WUHB_UTILS_RESULT_UNSUPPORTED_COMMAND: Command not supported by the currently loaded WUHBUtilsModule version.<br>
* WUHB_UTILS_API_ERROR_INVALID_ARG: "name" or "outRes" is NULL.<br>
* WUHB_UTILS_RESULT_UNKNOWN_ERROR: Unknown error.
2022-04-15 17:33:16 +02:00
*/
WUHBUtilsStatus WUHBUtils_FileExists(const char *name, int32_t *outRes);
/**
2022-09-03 14:19:11 +02:00
* Opens a file, reads it completely and returns the data as new buffer, <br>
* On success the the caller has to call "free()" on the returned buffer after using it.
2022-04-15 17:33:16 +02:00
*
* @param path path to the file.
* @param buffer address where the buffer address will be stored
* @param size address where the size will be stored
2022-09-03 14:19:11 +02:00
* @return WUHB_UTILS_RESULT_SUCCESS file read has been done. The new buffer been written to *outBuf. <br>
* The size of the buffer will be written to *outSize. <br>
2022-04-15 17:33:16 +02:00
* The buffer returned in *outBuf has be cleaned up via "free()"
2022-09-03 14:19:11 +02:00
* WUHB_UTILS_RESULT_LIB_UNINITIALIZED: "WUHBUtils_Init()" was not called before. <br>
* WUHB_UTILS_RESULT_UNSUPPORTED_COMMAND: Command not supported by the currently loaded WUHBUtilsModule version.<br>
* WUHB_UTILS_API_ERROR_INVALID_ARG: "outBuf" or "outSize" is NULL. <br>
* WUHB_UTILS_RESULT_NO_MEMORY: Not enough memory. <br>
* WUHB_UTILS_RESULT_UNKNOWN_ERROR: Unknown error.
2022-04-15 17:33:16 +02:00
*/
WUHBUtilsStatus WUHBUtils_ReadWholeFile(const char *path, uint8_t **outBuf, uint32_t *outSize);
/**
* Gets the offset and size of the /code/ *.rpx inside a WUHB.
*
* @param bundle_path path to the bundle file (path may depend on the BundleSource)
* @param source type of source. See BundleSource for more information.
* @param outFileInfo on success the result file info will be stored he
2022-09-03 14:19:11 +02:00
* @return WUHB_UTILS_RESULT_SUCCESS GetRPXInfo check has been done successfully. <br>
2022-04-15 17:33:16 +02:00
* The result of the check has been written to outFileInfo.
2022-09-03 14:19:11 +02:00
* WUHB_UTILS_RESULT_LIB_UNINITIALIZED: "WUHBUtils_Init()" was not called before. <br>
* WUHB_UTILS_RESULT_UNSUPPORTED_COMMAND: Command not supported by the currently loaded WUHBUtilsModule version.<br>
* WUHB_UTILS_RESULT_INVALID_ARGUMENT: "bundle_path" or "outFileInfo" is NULL. <br>
* WUHB_UTILS_RESULT_MOUNT_FAILED: failed to mount bundle. <br>
* WUHB_UTILS_RESULT_FILE_NOT_FOUND: No .rpx inside [bundle]/code/ found. <br>
* WUHB_UTILS_RESULT_UNKNOWN_ERROR: Unknown error.
2022-04-15 17:33:16 +02:00
*/
WUHBUtilsStatus WUHBUtils_GetRPXInfo(const char *bundle_path, BundleSource source, WUHBRPXInfo *outFileInfo);
#ifdef __cplusplus
} // extern "C"
#endif