Wing Commander II
Reconstructed Win32 source (1996)
Loading...
Searching...
No Matches
packet.c
Go to the documentation of this file.
1/*
2 * Packet sections and data file handles.
3 *
4 * Retail object 0x44F010-0x44F1EF (link order; see docs/ORDER.md).
5 */
6#include "game.h"
7
8short OpenDataFileOrDie(const char *path);
9int ReadDataFileAtOffset(unsigned short fd, int offset, unsigned int length,
10 void *data);
11int SeekDataFile(unsigned short fd, int offset, unsigned int origin);
12
13/* Function start: 0x44F010 */
14short OpenPacketSection(const char *filename, short section,
16{
17 unsigned int start;
18 unsigned int length;
19 unsigned int next;
20 int offset;
21 unsigned int directorySize;
22 short method;
23 short entryCount;
24 short handle;
25
26 handle = OpenDataFileOrDie(filename);
27 if (handle != -1) {
28 for (;;) {
29 if (ReadDataFileAtOffset(handle, 0, 4, &length) == 0)
30 goto failed;
31 if (ReadDataFileAtOffset(handle, 4, 4, &directorySize) == 0)
32 goto failed;
33 entryCount = (short)((directorySize >> 2) - 1);
34 if (section >= entryCount) {
36 goto failed;
37 }
38 offset = (section + 1) * 4;
39 if (ReadDataFileAtOffset(handle, offset, 4, &start) == 0)
40 goto failed;
41 method = (short)(start >> 24);
42 start &= 0x00ffffff;
43 out->finalSection = 0;
44 if (entryCount - 1 == section) {
45 next = length;
46 out->finalSection = 1;
47 } else {
48 offset += 4;
49 if (ReadDataFileAtOffset(handle, offset, 4, &next) == 0)
50 goto failed;
51 /* The top byte holds the next section's method flag. */
52 ((unsigned char *)&next)[3] = 0;
53 }
54 length = next - start;
55 if (SeekDataFile(handle, start, 0) == 0)
56 goto failed;
57 out->file = handle;
58 out->sectionCount = entryCount;
59 out->compression = method;
60 out->dataOffset = start;
61 out->position = 0;
62 out->dataSize = length;
63 return 1;
64 }
65 }
66failed:
67 CloseDataFile((unsigned short)handle);
68 return 0;
69}
70
71/* Function start: 0x44F1C7 */
72void CloseDataFileByHandle(unsigned short *handle)
73{
74 CloseDataFile(*handle);
75}
int SeekDataFile(unsigned short fd, int offset, unsigned int origin)
Definition datafile.c:78
int ReadDataFileAtOffset(unsigned short fd, int offset, unsigned int length, void *data)
Definition datafile.c:60
short OpenPacketSection(const char *filename, short section, PacketSectionHandle *handle)
Definition packet.c:14
void CloseDataFileByHandle(unsigned short *handle)
Definition packet.c:72
short OpenDataFileOrDie(const char *path)
Definition datafile.c:13
void CloseDataFile(unsigned short fd)
Definition datafile.c:22
Shared Win32 declarations, types and library interfaces.
short g_nPacketError
Definition datafile.c:10
unsigned int dataOffset
Definition game_data.h:1177
unsigned int dataSize
Definition game_data.h:1179
unsigned int position
Definition game_data.h:1178