-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDat2File.cpp
More file actions
191 lines (160 loc) · 4.39 KB
/
Dat2File.cpp
File metadata and controls
191 lines (160 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <cstring>
#include <ttvfs/VFSTools.h>
#include "Dat2ArchiveRef.h"
#include "Dat2File.h"
#include "miniz.h"
namespace ttvfs
{
Dat2File::Dat2File(const char *name, Dat2ArchiveRef *zref)
: File(joinPath(zref->fullname(), name).c_str()),
_pos(0),
_archiveHandle(zref),
_uncompressedData(0),
_isCompressed(false),
_isOpen(false)
{
}
Dat2File::~Dat2File()
{
close();
}
bool Dat2File::open(const char *mode /* = NULL */)
{
if (isopen()) {
return true;
}
if (getIsCompressed()) {
File *file = _archiveHandle->archiveFile.content();
unsigned char compressedData[getPackedSize()];
file->seek(getDataOffset(), SEEK_SET);
file->read(compressedData, getPackedSize());
_uncompressedData = new unsigned char[getUnpackedSize()];
// unpacking
mz_stream zStream;
zStream.total_in = zStream.avail_in = getPackedSize();
zStream.avail_in = getPackedSize();
zStream.next_in = compressedData;
zStream.total_out = zStream.avail_out = getUnpackedSize();
zStream.next_out = _uncompressedData;
zStream.zalloc = 0;
zStream.zfree = 0;
zStream.opaque = 0;
mz_inflateInit(&zStream);
mz_inflate(&zStream, MZ_FINISH);
mz_inflateEnd(&zStream);
}
_isOpen = true;
return true;
}
bool Dat2File::isopen() const
{
return _isOpen;
}
bool Dat2File::iseof() const
{
return _pos >= _unpackedSize;
}
void Dat2File::close()
{
if (isopen()) {
_isOpen = false;
delete[] _uncompressedData;
_uncompressedData = 0;
}
}
bool Dat2File::seek(vfspos pos, int whence)
{
const vfspos end = 0xFFFFFFFF;
switch (whence) {
case SEEK_SET:
if (pos >= end) // zip files have uint32 range only
return false;
_pos = pos;
break;
case SEEK_CUR:
if (_pos + pos >= end)
return false;
_pos += pos;
break;
case SEEK_END:
if (pos >= size())
return false;
_pos = size() - pos;
break;
default:
return false;
}
return true;
}
bool Dat2File::flush()
{
// FIXME: use this to actually write to zip file?
return false;
}
vfspos Dat2File::getpos() const
{
return _pos;
}
size_t Dat2File::read(void *dst, size_t bytes)
{
size_t bytesToRead = bytes;
if (bytes + _pos > size()) {
bytesToRead = size() - _pos;
}
if (_isCompressed) {
unsigned char *startptr = _uncompressedData + _pos;
unsigned char *endptr = _uncompressedData + size();
bytes = std::min<size_t>(endptr - startptr, bytes); // limit in case reading over buffer size
memcpy(dst, startptr, bytes); // binary copy
} else {
File *file = _archiveHandle->archiveFile.content();
file->seek(_dataOffset + _pos, SEEK_SET);
file->read(dst, bytesToRead);
}
_pos += bytesToRead;
return bytesToRead;
}
size_t Dat2File::write(const void *src, size_t bytes)
{
return 0;
}
vfspos Dat2File::size()
{
if (!_archiveHandle->openRead()) {
return npos;
}
return _unpackedSize;
}
void Dat2File::setIsCompressed(bool value)
{
_isCompressed = value;
}
bool Dat2File::getIsCompressed()
{
return _isCompressed;
}
void Dat2File::setDataOffset(unsigned int value)
{
_dataOffset = value;
}
unsigned int Dat2File::getDataOffset()
{
return _dataOffset;
}
void Dat2File::setPackedSize(unsigned int value)
{
_packedSize = value;
}
unsigned int Dat2File::getPackedSize()
{
return _packedSize;
}
void Dat2File::setUnpackedSize(unsigned int value)
{
_unpackedSize = value;
}
unsigned int Dat2File::getUnpackedSize()
{
return _unpackedSize;
}
}