Pulsar  3d3a057
archive.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Archive container interface
4  */
5 #pragma once
6 
7 #include <pulsar/types.h>
8 #include <pulsar/archive/archive_internal.h>
10 
11 /// Archive version found in header
12 typedef union {
13  u32 raw;
14  struct {
15  u8 revision;
16  u8 minor;
17  u8 major;
18  };
20 
21 /// Common archive header information
22 typedef struct {
23  PLSR_ArchiveVersion version;
24  u32 fileSize;
25  u16 sectionCount;
27 
28 /// Archive section header information
29 typedef struct {
30  u32 blockSize; ///< Section size
32 
33 /// Archive section
34 typedef struct {
35  u32 offset; ///< Section start offset in archive
36  PLSR_ArchiveSectionInfo info; ///< Cached section information
38 
39 /// Archive section header information
40 typedef struct {
41  u32 count; ///< Table entry/block count
43 
44 /// Archive table (entry list)
45 typedef struct {
46  u32 offset; ///< Table start offset in archive
47  PLSR_ArchiveTableInfo info; ///< Cached table information
49 
50 /// Archive table entry (fixed size entry)
51 typedef struct {
52  u32 offset; ///< Entry start offset in archive
54 
55 /// Archive table block (sized entry)
56 typedef struct {
57  u32 offset; ///< Entry start offset in archive
58  u32 size;
60 
61 /// Archive list information
62 typedef struct {
63  u32 entrySize;
64  u32 count;
66 
67 /// Archive list (fixed size data array)
68 typedef struct {
69  u32 offset; ///< List start offset in archive
70  PLSR_ArchiveListInfo info; ///< Cached list information
72 
73 /// Archive file
74 /** Created by opening a file, or an embedded archive at a specified offset */
75 typedef struct {
76  PLSR_ArchiveFileHandle handle; ///< File handle, closed when all archives with the same handle are closed
77  u32 offset; ///< Start offset of the archive in the file
78 } PLSR_Archive;
79 
80 /// Open from a file at specified path (advanced)
81 PLSR_RC plsrArchiveOpenEx(const char* path, PLSR_Archive* out, bool storePath);
82 
83 /// Open from a file at specified path
84 /** @note Path is not stored by default unless specified using plsrArchiveOpenEx() */
85 NX_INLINE PLSR_RC plsrArchiveOpen(const char* path, PLSR_Archive* out) {
86  return plsrArchiveOpenEx(path, out, false);
87 }
88 
89 /// Open from inside another archive at specified offset
90 PLSR_RC plsrArchiveOpenInside(const PLSR_Archive* ar, u32 offset, PLSR_Archive* out);
91 
92 /// Close archive, releasing resources if applicable
94 
95 /// Construct path relative to the physical archive file
96 /**
97  * - Does not guarantee it exists (but might fail in some cases if it does not)
98  * - Works only if the archive path was stored when opened
99  */
100 NX_INLINE PLSR_RC plsrArchiveRelativePath(const PLSR_Archive* ar, const char* path, char* out, size_t size) {
101  return plsrArchiveFileRelativePath(ar->handle, path, out, size) ? PLSR_RC_OK : PLSR_ResultType_NotFound;
102 }
103 
104 /// Read archive contents at the current position
105 NX_INLINE PLSR_RC plsrArchiveRead(const PLSR_Archive* ar, void* out, size_t size) {
106  return plsrArchiveFileRead(ar->handle, out, size) ? PLSR_RC_OK : PLSR_ResultType_FileRead;
107 }
108 
109 /// Read archive contents at the specified offset (advanced)
110 PLSR_RC plsrArchiveReadAtEx(const PLSR_Archive* ar, u32 offset, void* out, size_t size, bool acceptZero);
111 
112 /// Read archive contents at the specified offset
113 /** @note Offset zero is invalid unless you specify otherwise using plsrArchiveReadAtEx() (start of an archive should always be the header) */
114 NX_INLINE PLSR_RC plsrArchiveReadAt(const PLSR_Archive* ar, u32 offset, void* out, size_t size) {
115  return plsrArchiveReadAtEx(ar, offset, out, size, false);
116 }
117 
118 /// Read a null-terminated string at the specified offset
119 PLSR_RC plsrArchiveReadString(const PLSR_Archive* ar, u32 offset, char* out, size_t size);
120 
121 /// Read archive header
122 /** @param magic Expected header magic */
123 PLSR_RC plsrArchiveReadHeaderInfo(const PLSR_Archive* ar, const char* magic, PLSR_ArchiveHeaderInfo* out);
124 
125 /// Read archive section header
126 /** @param magic Expected section magic */
127 PLSR_RC plsrArchiveReadSectionHeaderInfo(const PLSR_Archive* ar, u32 offset, const char* magic, PLSR_ArchiveSectionInfo* out);
128 
129 /// Read archive table header information
131 
132 /// Read one archive table entry at specified index
133 /** @param id Expected reference identifier */
134 PLSR_RC plsrArchiveReadTableEntry(const PLSR_Archive* ar, const PLSR_ArchiveTable* table, u16 id, u32 index, PLSR_ArchiveTableEntry* out);
135 
136 /// Read one table block at specified index
137 /** @param id Expected reference identifier */
138 PLSR_RC plsrArchiveReadTableBlock(const PLSR_Archive* ar, const PLSR_ArchiveTable* table, u16 id, u32 index, PLSR_ArchiveTableBlock* out);
139 
140 /// Read a number of list entries starting from the specified index
141 /** @note Additionally to checking RC, outReadCount might not match the requested count */
142 PLSR_RC plsrArchiveListReadEntries(const PLSR_Archive* ar, const PLSR_ArchiveList* list, u32 startIndex, u32 count, void* outEntries, u32* outReadCount);
143 
144 /// Read one list entry
145 PLSR_RC plsrArchiveListGetEntry(const PLSR_Archive* ar, const PLSR_ArchiveList* list, u32 index, void* out);
PLSR_RC plsrArchiveOpenEx(const char *path, PLSR_Archive *out, bool storePath)
Open from a file at specified path (advanced)
u32 offset
Entry start offset in archive.
Definition: archive.h:57
Base types.
PLSR_RC plsrArchiveReadString(const PLSR_Archive *ar, u32 offset, char *out, size_t size)
Read a null-terminated string at the specified offset.
NX_INLINE PLSR_RC plsrArchiveOpen(const char *path, PLSR_Archive *out)
Open from a file at specified path.
Definition: archive.h:85
Archive section.
Definition: archive.h:34
Archive section header information.
Definition: archive.h:29
NX_INLINE PLSR_RC plsrArchiveRelativePath(const PLSR_Archive *ar, const char *path, char *out, size_t size)
Construct path relative to the physical archive file.
Definition: archive.h:100
NX_INLINE PLSR_RC plsrArchiveReadAt(const PLSR_Archive *ar, u32 offset, void *out, size_t size)
Read archive contents at the specified offset.
Definition: archive.h:114
u32 offset
Section start offset in archive.
Definition: archive.h:35
Common archive header information.
Definition: archive.h:22
u32 offset
List start offset in archive.
Definition: archive.h:69
Archive list information.
Definition: archive.h:62
PLSR_RC plsrArchiveReadTableEntry(const PLSR_Archive *ar, const PLSR_ArchiveTable *table, u16 id, u32 index, PLSR_ArchiveTableEntry *out)
Read one archive table entry at specified index.
PLSR_RC plsrArchiveOpenInside(const PLSR_Archive *ar, u32 offset, PLSR_Archive *out)
Open from inside another archive at specified offset.
Archive version found in header.
Definition: archive.h:12
u32 count
Table entry/block count.
Definition: archive.h:41
void plsrArchiveClose(PLSR_Archive *ar)
Close archive, releasing resources if applicable.
PLSR_ArchiveSectionInfo info
Cached section information.
Definition: archive.h:36
Archive section header information.
Definition: archive.h:40
File could not be opened or read.
Definition: types.h:79
Shared archive file management.
PLSR_ArchiveFileHandle handle
File handle, closed when all archives with the same handle are closed.
Definition: archive.h:76
Archive table block (sized entry)
Definition: archive.h:56
Definition: archive_file.h:12
Archive file.
Definition: archive.h:75
PLSR_ArchiveTableInfo info
Cached table information.
Definition: archive.h:47
Requested data could not be retrieved.
Definition: types.h:83
Archive list (fixed size data array)
Definition: archive.h:68
u32 offset
Start offset of the archive in the file.
Definition: archive.h:77
u32 offset
Entry start offset in archive.
Definition: archive.h:52
PLSR_RC plsrArchiveReadTableHeaderInfo(const PLSR_Archive *ar, u32 offset, PLSR_ArchiveTableInfo *out)
Read archive table header information.
PLSR_RC plsrArchiveReadAtEx(const PLSR_Archive *ar, u32 offset, void *out, size_t size, bool acceptZero)
Read archive contents at the specified offset (advanced)
PLSR_ArchiveListInfo info
Cached list information.
Definition: archive.h:70
NX_INLINE PLSR_RC plsrArchiveRead(const PLSR_Archive *ar, void *out, size_t size)
Read archive contents at the current position.
Definition: archive.h:105
u32 PLSR_RC
Result code returned by Pulsar functions.
Definition: types.h:73
#define PLSR_RC_OK
Result code returned on success.
Definition: types.h:32
PLSR_RC plsrArchiveListGetEntry(const PLSR_Archive *ar, const PLSR_ArchiveList *list, u32 index, void *out)
Read one list entry.
PLSR_RC plsrArchiveReadHeaderInfo(const PLSR_Archive *ar, const char *magic, PLSR_ArchiveHeaderInfo *out)
Read archive header.
PLSR_RC plsrArchiveReadSectionHeaderInfo(const PLSR_Archive *ar, u32 offset, const char *magic, PLSR_ArchiveSectionInfo *out)
Read archive section header.
u32 offset
Table start offset in archive.
Definition: archive.h:46
Archive table entry (fixed size entry)
Definition: archive.h:51
PLSR_RC plsrArchiveReadTableBlock(const PLSR_Archive *ar, const PLSR_ArchiveTable *table, u16 id, u32 index, PLSR_ArchiveTableBlock *out)
Read one table block at specified index.
u32 blockSize
Section size.
Definition: archive.h:30
PLSR_RC plsrArchiveListReadEntries(const PLSR_Archive *ar, const PLSR_ArchiveList *list, u32 startIndex, u32 count, void *outEntries, u32 *outReadCount)
Read a number of list entries starting from the specified index.
Archive table (entry list)
Definition: archive.h:45