1 #ifndef GWROM_H
2 #define GWROM_H
3 
4 #include <stdlib.h>
5 #include <stdint.h>
6 
7 /*---------------------------------------------------------------------------*/
8 /* must be implemented by the user */
9 
10 /*
11 memory allocation routines
12 */
13 void* gwrom_malloc( size_t size );
14 void* gwrom_realloc( void* ptr, size_t size );
15 void  gwrom_free( void* ptr );
16 
17 /*---------------------------------------------------------------------------*/
18 /* compilation config */
19 
20 /* use tar v7 archives */
21 #define GWROM_USE_TAR_V7
22 
23 /* use zlib for decompression */
24 /*#define GWROM_USE_GZIP*/
25 
26 /* use bzip2 for decompression */
27 #define GWROM_USE_BZIP2
28 
29 /*
30 prevents memory reallocation during bzip2 decompression
31 note that in this case gwrom will do two decompression runs, one to evaluate
32 the decompressed size and another to decompress the data to the allocated
33 buffer, but memory will be entirely allocated in one go
34 */
35 #define GWROM_NO_REALLOC 1
36 
37 /* size of the buffer used during decompression */
38 #define GWROM_DECOMP_BUFFER 65536
39 
40 /*---------------------------------------------------------------------------*/
41 /* flags to gwrom_init (1 << 0 to 1 << 15) */
42 
43 /*
44 makes gwrom_init copy the rom into a new buffer, even if it's unecessary (i.e.
45 data wasn't compressed)
46 */
47 #define GWROM_COPY_ALWAYS 1
48 
49 /*---------------------------------------------------------------------------*/
50 
51 /* everything went ok */
52 #define GWROM_OK 0
53 
54 /* invalid rom format */
55 #define GWROM_INVALID_ROM -1
56 
57 /* memory allocation error */
58 #define GWROM_NO_MEMORY -2
59 
60 /* entry not found in the rom */
61 #define GWROM_ENTRY_NOT_FOUND -3
62 
63 /*---------------------------------------------------------------------------*/
64 
65 typedef struct gwrom_t gwrom_t;
66 
67 typedef struct
68 {
69   /* entry name */
70   const char* name;
71   /* pointer to entry data */
72   void*       data;
73   /* entry data size */
74   size_t      size;
75   /* internal gwrom flags for the entry */
76   uint32_t    flags;
77 
78   /* persistent (between gwrom_init and gwrom_destroy) user flags */
79   uint32_t* user_flags;
80 
81   /* available to the user */
82   void** user_data;
83 }
84 gwrom_entry_t;
85 
86 struct gwrom_t
87 {
88   /* rom data */
89   void*    data;
90   /* rom data size */
91   size_t   size;
92   /* internal gwrom flags for the rom */
93   uint32_t flags;
94 
95   /* persistent (between gwrom_init and gwrom_destroy) user flags */
96   uint32_t user_flags;
97 
98   /* available to the user */
99   void* user_data;
100 
101   /* frees all memory allocated to the rom */
102   void (*destroy)( gwrom_t* );
103   /* finds an entry in the rom */
104   int  (*find)( gwrom_entry_t*, gwrom_t*, const char* );
105   /* iterates over all rom entries */
106   void (*iterate)( gwrom_t*, int (*)( gwrom_entry_t*, gwrom_t* ) );
107 };
108 
109 /*---------------------------------------------------------------------------*/
110 /* external api */
111 
112 /*
113 initializes an archive. if flags contain GWROM_COPY_ALWAYS, data will be copied
114 to another buffer. this buffer is automatically freed when gwrom_destroy is
115 called.
116 */
117 int  gwrom_init( gwrom_t* gwrom, void* data, size_t size, uint32_t flags );
118 
119 /* frees all memory allocated for the rom */
120 void gwrom_destroy( gwrom_t* );
121 
122 /* returns a readable descriptions for errors */
123 const char* gwrom_error_message( int error );
124 
125 /* finds an entry in the rom */
126 #define gwrom_find( entry, gwrom, file_name ) ( ( gwrom )->find( entry, gwrom, file_name ) )
127 
128 /* iterates over all rom entries */
129 #define gwrom_iterate( gwrom, callback ) ( ( gwrom )->iterate( gwrom, callback ) )
130 
131 /*---------------------------------------------------------------------------*/
132 
133 #endif /* GWROM_H */
134