Debian Codegraph @

src/cdogs/cwolfmap/wad/wad.h

raw symbols SCIP
1 /*****************************************************************************
2 * Copyright (c) 2018 Matt Tropiano
3 * All rights reserved. This source and the accompanying materials
4 * are made available under the terms of the GNU Lesser Public License v2.1
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
7 *****************************************************************************/
8
9 #ifndef __WAD_H__
10 #define __WAD_H__
11
12 #include <stdint.h>
13 #include <string.h>
14 #include <stdio.h>
15
16 // Important defines.
17 #define WADTYPE_IWAD 0x44415749
18 #define WADTYPE_PWAD 0x44415750
19
20 #define WADBUFFER_INITSIZE (1024 * 32)
21 #define WADENTRIES_INITSIZE 16
22
23 /**
24 * A WAD header structure (the start of all WAD files).
25 */
26 typedef struct {
27
28 /** The magic number ("IWAD" or "PWAD"). */
29 uint32_t type;
30 /** The amount of entries in the WAD. */
31 int32_t entry_count;
32 /** The byte offset into the WAD for the entry list. */
33 int32_t entry_list_offset;
34
35 } wadheader_t;
36
37 /**
38 * A WAD entry.
39 */
40 typedef struct {
41
42 /** Entry content offset into file. */
43 uint32_t offset;
44 /** Entry content length. */
45 int32_t length;
46 /** Entry name. */
47 unsigned char name[8]; // not null-terminated if all 8 are used!
48
49 } wadentry_t;
50
51 /**
52 * WAD implementation type.
53 * This determines how data is loaded and manipulated and what functions to call.
54 */
55 typedef enum {
56
57 /** Unknown type - not initialized. */
58 WI_UNKNOWN,
59 /** Shallow entry map - preview only (no deep read, no write). */
60 WI_MAP,
61 /** Content and entries stored in memory. */
62 WI_BUFFER,
63 /** Content and entries read from open file (random access). */
64 WI_FILE,
65
66 } wadimpl_t;
67
68 /**
69 * A WAD abstraction.
70 */
71 typedef struct {
72
73 /** Implementation type. */
74 wadimpl_t type;
75
76 /** Handle union. */
77 union {
78
79 /** If WI_FILE. */
80 FILE *file;
81 /** If WI_BUFFER. */
82 unsigned char *buffer;
83
84 } handle;
85
86 /** WAD header. */
87 wadheader_t header;
88 /** WAD entry list. */
89 wadentry_t **entries;
90
91 /** WAD entry list current capacity. */
92 int entries_capacity;
93 /** WAD buffer size (if buffer implementation). */
94 int buffer_size;
95 /** WAD buffer capacity (if buffer implementation). */
96 int buffer_capacity;
97
98 } wad_t;
99
100 /**
101 * A simple iterator abstraction for WADs.
102 */
103 typedef struct {
104
105 /** Pointer to WAD. */
106 wad_t *wad;
107 /** Pointer to current entry. */
108 wadentry_t *entry;
109 /** Next index. */
110 int next;
111 /** Total count. */
112 int count;
113
114 } waditerator_t;
115
116 // ================ Common WAD Functions ====================
117
118 /**
119 * Opens an existing WAD file for random access.
120 * @param filename the file name to open.
121 * @return a newly-allocated wad_t (file implementation), or NULL on error.
122 */
123 wad_t *WAD_Open(const char *filename);
124
125 /**
126 * Creates a new WAD file for random access.
127 * The file is created at the specified path.
128 * @param filename the file name to create.
129 * @return a newly-allocated wad_t (file implementation), or NULL on error.
130 */
131 wad_t* WAD_Create(char *filename);
132
133 /**
134 * Opens an existing WAD file, but only skims for entry data.
135 * The file is not left open - no handle is kept.
136 * @param filename the file name to open.
137 * @return a newly-allocated wad_t (mapping implementation), or NULL on error.
138 */
139 wad_t* WAD_OpenMap(char *filename);
140
141 /**
142 * Creates a WAD buffer in memory by loading the contents of an existing WAD file completely into memory.
143 * The file is not left open - no handle is kept.
144 * @param filename the file name to open.
145 * @return a newly-allocated wad_t (mapping implementation), or NULL on error.
146 */
147 wad_t* WAD_OpenBuffer(char *filename);
148
149 /**
150 * Creates a WAD buffer in memory with a default initial content buffer size WADBUFFER_INITSIZE.
151 * WARNING: Buffers must be saved to disk, or they are not persisted anywhere!
152 * @return a newly-allocated wad_t (buffer implementation), or NULL on error.
153 */
154 wad_t* WAD_CreateBuffer(void);
155
156 /**
157 * Creates a WAD buffer in memory with a specific initial content buffer size.
158 * @param size the initial size, in bytes.
159 * @return a newly-allocated wad_t (mapping implementation), or NULL on error.
160 */
161 wad_t* WAD_CreateBufferInit(int size);
162
163 /**
164 * Gets the amount of entries that this WAD contains.
165 * @param wad the pointer to the open WAD.
166 * @return the amount of entries or -1 on error.
167 */
168 #define WAD_EntryCount(w) ((int)((w)->header.entry_count))
169
170 /**
171 * Commits changes to WAD entries that would not ordinarily be
172 * detected by other functions that manipulate entries.
173 * Most WAD_* functions call this - you only need to call this
174 * if you edit the entry list pointers or their values explicitly.
175 * @param wad the pointer to the open WAD.
176 * @return 0 if committed properly, nonzero on error.
177 */
178 int WAD_CommitEntries(wad_t *wad);
179
180 /**
181 * Gets a WAD entry at a particular index.
182 * @param wad the pointer to the open WAD.
183 * @param index the entry index to get.
184 * @return a valid pointer, or NULL if no corresponding index.
185 */
186 wadentry_t* WAD_GetEntry(const wad_t *wad, int index);
187
188 /**
189 * Gets the first WAD entry by a particular name.
190 * Names are case-sensitive, and only the first 8 characters are compared.
191 * @param wad the pointer to the open WAD.
192 * @param name the entry name.
193 * @return a valid pointer, or NULL if no corresponding entry.
194 */
195 wadentry_t* WAD_GetEntryByName(wad_t *wad, const char *name);
196
197 /**
198 * Gets the first WAD entry by a particular name, from a starting index.
199 * Names are case-sensitive, and only the first 8 characters are compared.
200 * @param wad the pointer to the open WAD.
201 * @param name the entry name.
202 * @param start the index to start from.
203 * @return a valid pointer, or NULL if no corresponding entry.
204 */
205 wadentry_t* WAD_GetEntryByNameOffset(wad_t *wad, const char *name, int start);
206
207 /**
208 * Gets the first WAD entry by a particular name, nth instance.
209 * Names are case-sensitive, and only the first 8 characters are compared.
210 * @param wad the pointer to the open WAD.
211 * @param name the entry name.
212 * @param nth the nth entry to retrieve (0 or 1 is the first, 2 is second, etc.).
213 * @return a valid pointer, or NULL if no corresponding entry.
214 */
215 wadentry_t* WAD_GetEntryByNameNth(wad_t *wad, const char *name, int nth);
216
217 /**
218 * Gets the first WAD entry by a particular name, nth instance, starting from an entry index.
219 * Names are case-sensitive, and only the first 8 characters are compared.
220 * @param wad the pointer to the open WAD.
221 * @param name the entry name.
222 * @param start the index to start from.
223 * @param nth the nth entry to retrieve (0 or 1 is the first, 2 is second, etc.).
224 * @return a valid pointer, or NULL if no corresponding entry.
225 */
226 wadentry_t* WAD_GetEntryByNameOffsetNth(wad_t *wad, const char *name, int start, int nth);
227
228 /**
229 * Gets the last WAD entry by a particular name (backwards search).
230 * Names are case-sensitive, and only the first 8 characters are compared.
231 * @param wad the pointer to the open WAD.
232 * @param name the entry name.
233 * @return a valid pointer, or NULL if no corresponding entry.
234 */
235 wadentry_t* WAD_GetLastEntryByName(wad_t *wad, const char *name);
236
237 /**
238 * Counts how many entries have this name.
239 * Names are case-sensitive, and only the first 8 characters are compared.
240 * @param wad the pointer to the open WAD.
241 * @param name the entry name.
242 * @return the amount of entries.
243 */
244 int WAD_GetEntryCount(wad_t *wad, const char *name);
245
246 /**
247 * Gets the index of a particular entry.
248 * Names are case-sensitive, and only the first 8 characters are compared.
249 * @param wad the pointer to the open WAD.
250 * @param name the entry name.
251 * @return the matching index or -1 if not found.
252 */
253 int WAD_GetEntryIndex(wad_t *wad, const char *name);
254
255 /**
256 * Gets the index of a particular entry, from a starting index.
257 * Names are case-sensitive, and only the first 8 characters are compared.
258 * @param wad the pointer to the open WAD.
259 * @param name the entry name.
260 * @param start the index to start from.
261 * @return the matching index or -1 if not found.
262 */
263 int WAD_GetEntryIndexOffset(wad_t *wad, const char *name, int start);
264
265 /**
266 * Gets the indices of all matching lumps by name.
267 * Names are case-sensitive, and only the first 8 characters are compared.
268 * @param wad the pointer to the open WAD.
269 * @param name the entry name.
270 * @param out the output array for the indices.
271 * @param max the maximum amount of indices to add.
272 * @return the amount of indices returned.
273 */
274 int WAD_GetEntryIndices(wad_t *wad, const char *name, int *out, int max);
275
276 /**
277 * Gets the indices of all matching lumps by name.
278 * Names are case-sensitive, and only the first 8 characters are compared.
279 * @param wad the pointer to the open WAD.
280 * @param name the entry name.
281 * @param start the index to start from.
282 * @param out the output array for the indices.
283 * @param max the maximum amount of indices to add.
284 * @return the amount of indices returned.
285 */
286 int WAD_GetEntryIndicesOffset(wad_t *wad, const char *name, int start, int *out, int max);
287
288 /**
289 * Gets the last index of a particular entry.
290 * Names are case-sensitive, and only the first 8 characters are compared.
291 * @param wad the pointer to the open WAD.
292 * @param name the entry name.
293 * @return the matching index or -1 if not found.
294 */
295 int WAD_GetEntryLastIndex(wad_t *wad, const char *name);
296
297 /**
298 * Creates a new WAD entry at the end of the WAD.
299 * Bad characters in names are coerced into valid characters.
300 * @param wad the pointer to the open WAD.
301 * @param name the entry name.
302 * @return a pointer to the created entry.
303 */
304 wadentry_t* WAD_CreateEntry(wad_t *wad, const char *name);
305
306 /**
307 * Creates a new WAD entry at a specific index.
308 * The rest of the entries after the index are shifted down a position.
309 * Bad characters in names are coerced into valid characters.
310 * @param wad the pointer to the open WAD.
311 * @param name the entry name.
312 * @param index the index position (0-based) to add the entry at.
313 * @return a pointer to the created entry.
314 */
315 wadentry_t* WAD_CreateEntryAt(wad_t *wad, const char *name, int index);
316
317 /**
318 * Adds a new WAD entry (with content) at the end of the WAD.
319 * Content is appended to the end of the content blob.
320 * Bad characters in names are coerced into valid characters.
321 * @param wad the pointer to the open WAD.
322 * @param name the entry name.
323 * @param buffer the pointer to the data to write.
324 * @param buffer_size the amount of data in bytes to write.
325 * @return a pointer to the created entry.
326 */
327 wadentry_t* WAD_AddEntry(wad_t *wad, const char *name, unsigned char *buffer, size_t buffer_size);
328
329 /**
330 * Adds a new WAD entry (with content) at a specific index.
331 * The rest of the entries after the index are shifted down a position.
332 * Content is appended to the end of the content blob.
333 * Bad characters in names are coerced into valid characters.
334 * @param wad the pointer to the open WAD.
335 * @param name the entry name.
336 * @param index the index position (0-based) to add the entry at.
337 * @param buffer the pointer to the data to write.
338 * @param buffer_size the amount of data in bytes to write.
339 * @return a pointer to the created entry.
340 */
341 wadentry_t* WAD_AddEntryAt(wad_t *wad, const char *name, int index, unsigned char *buffer, size_t buffer_size);
342
343 /**
344 * Adds a new WAD entry (with content) at the end of the WAD, from a FILE.
345 * The FILE's position is not adjusted - it is read from its current position to the end.
346 * Content is appended to the end of the content blob.
347 * Bad characters in names are coerced into valid characters.
348 * @param wad the pointer to the open WAD.
349 * @param name the entry name.
350 * @param stream the input stream.
351 * @return a pointer to the created entry.
352 */
353 wadentry_t* WAD_AddEntryData(wad_t *wad, const char *name, FILE *stream);
354
355 /**
356 * Adds a new WAD entry (with content) at a specific index.
357 * The FILE's position is not adjusted - it is read from its current position to the end.
358 * The rest of the entries after the index are shifted down a position.
359 * Content is appended to the end of the content blob.
360 * Bad characters in names are coerced into valid characters.
361 * @param wad the pointer to the open WAD.
362 * @param name the entry name.
363 * @param index the index position (0-based) to add the entry at.
364 * @param stream the input stream.
365 * @return a pointer to the created entry.
366 */
367 wadentry_t* WAD_AddEntryDataAt(wad_t *wad, const char *name, int index, FILE *stream);
368
369 /**
370 * Adds a new WAD entry to the end of the entry list with a predefined length and offset.
371 * The offset provided should be set with the knowledge that the data offset in a WAD
372 * starts at 12 bytes in (if the content is at the 42nd byte of the content block, offset is 54).
373 * The offset and length provided are NOT bounds-checked! Use this function at your own risk!
374 * @param wad the pointer to the open WAD.
375 * @param name the entry name.
376 * @param length the length of the entry in bytes.
377 * @param offset the offset into the WAD in bytes (content starts at 12).
378 * @return a pointer to the created entry.
379 */
380 wadentry_t* WAD_AddExplicitEntry(wad_t *wad, const char *name, size_t length, int offset);
381
382 /**
383 * Adds a new WAD entry with a predefined length and offset.
384 * The offset provided should be set with the knowledge that the data offset in a WAD
385 * starts at 12 bytes in.
386 * The offset and length provided are NOT bounds-checked! Use this function at your own risk!
387 * @param wad the pointer to the open WAD.
388 * @param name the entry name.
389 * @param index the index position (0-based) to add the entry at.
390 * @param length the length of the entry in bytes.
391 * @param offset the offset into the WAD in bytes (content starts at 12).
392 * @return a pointer to the created entry.
393 */
394 wadentry_t* WAD_AddExplicitEntryAt(wad_t *wad, const char *name, int index, size_t length, int offset);
395
396 /**
397 * Adds a new WAD entry to the end of the entry list with a zero length.
398 * Equivalent to WAD_AddExplicitEntry(wad, name, 0, 0).
399 * @param wad the pointer to the open WAD.
400 * @param name the entry name.
401 * @return a pointer to the created entry.
402 */
403 wadentry_t* WAD_AddMarkerEntry(wad_t *wad, const char *name);
404
405 /**
406 * Adds a new WAD entry with a zero length.
407 * Equivalent to WAD_AddExplicitEntryAt(wad, name, index, 0, 0).
408 * @param wad the pointer to the open WAD.
409 * @param name the entry name.
410 * @param index the index position (0-based) to add the entry at.
411 * @return a pointer to the created entry.
412 */
413 wadentry_t* WAD_AddMarkerEntryAt(wad_t *wad, const char *name, int index);
414
415 /**
416 * Removes an entry from the WAD (but not its content).
417 * The rest of the entries after the index are shifted up a position.
418 * @param wad the pointer to the open WAD.
419 * @param index the index position (0-based) to remove the entry from.
420 * @return 0 if successful, nonzero if not.
421 */
422 int WAD_RemoveEntryAt(wad_t *wad, int index);
423
424 /**
425 * Removes an entry from the WAD (but not its content).
426 * The remaining entries are shifted up into their new positions to fill the gaps.
427 * @param wad the pointer to the open WAD.
428 * @param indices the array of indices to remove.
429 * @param count the length of the array.
430 * @return 0 if successful, nonzero if not.
431 */
432 int WAD_RemoveEntriesAt(wad_t *wad, int *indices, int count);
433
434 /**
435 * Removes a set of entries from the WAD (but not their contents).
436 * The remaining entries are shifted up into their new positions to fill the gaps.
437 * @param wad the pointer to the open WAD.
438 * @param start the starting index (0-based, inclusive) to remove entries from.
439 * @param count the amount of entries to remove from the start.
440 * @return 0 if successful, nonzero if not.
441 */
442 int WAD_RemoveEntryRange(wad_t *wad, int start, int count);
443
444 /**
445 * Swaps the index of an entry with an entry at another index.
446 * @param wad the pointer to the open WAD.
447 * @param a the first index (0-based, inclusive).
448 * @param b the second index (0-based, inclusive).
449 * @return 0 if successful, nonzero if not (indexes are out of bounds).
450 */
451 int WAD_SwapEntry(wad_t *wad, int a, int b);
452
453 /**
454 * Moves an entry to another index.
455 * The entry that would be overwritten at the destination is instead displaced to after the destination.
456 * @param wad the pointer to the open WAD.
457 * @param start the starting index (0-based, inclusive) to remove entries from.
458 * @param destination the destination index (0-based, inclusive).
459 * @return 0 if successful, nonzero if not (indexes are out of bounds).
460 */
461 int WAD_ShiftEntry(wad_t *wad, int start, int destination);
462
463 /**
464 * Moves a set of entries to another index.
465 * The entries that would be overwritten at the destination are instead displaced later in the WAD right after the moved entries.
466 * @param wad the pointer to the open WAD.
467 * @param start the starting index (0-based, inclusive) to remove entries from.
468 * @param count the amount of entries to move from the start.
469 * @param destination the destination index (0-based, inclusive).
470 * @return 0 if successful, nonzero if not (indexes are out of bounds).
471 */
472 int WAD_ShiftEntries(wad_t *wad, int start, int count, int destination);
473
474 /**
475 * Gets the content of an entry from a WAD.
476 * The destination must be large enough to accomodate the data.
477 * This ensures a single contiguous read of the data.
478 * @param wad the pointer to the open WAD.
479 * @param entry the entry to use for length and offset.
480 * @param destination the destination buffer.
481 * @return the amount of bytes read, or -1 on a read error.
482 */
483 int WAD_GetEntryData(wad_t *wad, wadentry_t *entry, unsigned char *destination);
484
485 /**
486 * Gets the content of an entry from a WAD.
487 * The destination must be large enough to accomodate the data.
488 * @param wad the pointer to the open WAD.
489 * @param entry the entry to use for length and offset.
490 * @param destination the destination buffer.
491 * @param size the size of a single element in bytes.
492 * @param count the amount of elements to read.
493 * @return the amount of elements read, or -1 on a read error.
494 */
495 int WAD_ReadEntryData(wad_t *wad, wadentry_t *entry, void *destination, size_t size, size_t count);
496
497 /**
498 * Closes an open WAD, performs flushing operations on it if necessary,
499 * then frees it from memory. The pointer provided is then invalid.
500 * @param wad the pointer to the open WAD.
501 * @return 0 if closed properly and wad was freed, nonzero on error.
502 */
503 int WAD_Close(wad_t *wad);
504
505 /**
506 * Creates a WAD iterator.
507 * NOTE: Must have WAD_IteratorNext called on it to get the first entry!
508 * @param wad the pointer to the open WAD.
509 * @param start the starting index into the entry list.
510 * @return the amount of entries or -1 on error.
511 */
512 waditerator_t* WAD_IteratorCreate(wad_t *wad, int start);
513
514 /**
515 * Resets a WAD iterator.
516 * NOTE: Must have WAD_IteratorNext called on it to get the first entry!
517 * @param iter pointer to the iterator.
518 * @param start the starting index into the entry list.
519 * @return the amount of entries or -1 on error.
520 */
521 void WAD_IteratorReset(waditerator_t *iter, int start);
522
523 /**
524 * Advances a WAD iterator to the next entry.
525 * @param iter pointer to the iterator.
526 * @return a pointer to the next entry.
527 */
528 wadentry_t* WAD_IteratorNext(waditerator_t *iter);
529
530 /**
531 * Frees a WAD iterator.
532 * @param iter pointer to the iterator.
533 */
534 void WAD_IteratorClose(waditerator_t *iter);
535
536 #endif