forked from usineur/hode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fileio.cpp
161 lines (141 loc) · 3.24 KB
/
fileio.cpp
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
/*
* Heart of Darkness engine rewrite
* Copyright (C) 2009-2011 Gregory Montoir ([email protected])
*/
#ifndef _MSC_VER
#include <sys/param.h>
#endif // !_MSC_VER
#include "fileio.h"
#include "util.h"
static const bool kCheckSectorFileCrc = false;
#ifdef PSP
static const bool kSeekAbsolutePosition = true;
#else
static const bool kSeekAbsolutePosition = false;
#endif
File::File()
: _fp(0) {
}
File::~File() {
}
void File::setFp(FILE *fp) {
_fp = fp;
}
void File::seekAlign(uint32_t pos) {
fseek(_fp, pos, SEEK_SET);
}
void File::seek(int pos, int whence) {
if (kSeekAbsolutePosition && whence == SEEK_CUR) {
pos += ftell(_fp);
whence = SEEK_SET;
}
fseek(_fp, pos, whence);
}
int File::read(uint8_t *ptr, int size) {
return fread(ptr, 1, size, _fp);
}
uint8_t File::readByte() {
uint8_t buf;
read(&buf, 1);
return buf;
}
uint16_t File::readUint16() {
uint8_t buf[2];
read(buf, 2);
return READ_LE_UINT16(buf);
}
uint32_t File::readUint32() {
uint8_t buf[4];
read(buf, 4);
return READ_LE_UINT32(buf);
}
SectorFile::SectorFile() {
memset(_buf, 0, sizeof(_buf));
_bufPos = 2044;
}
int fioAlignSizeTo2048(int size) {
return ((size + 2043) / 2044) * 2048;
}
uint32_t fioUpdateCRC(uint32_t sum, const uint8_t *buf, uint32_t size) {
assert((size & 3) == 0);
for (uint32_t offset = 0; offset < size; offset += 4) {
sum ^= READ_LE_UINT32(buf + offset);
}
return sum;
}
void SectorFile::refillBuffer(uint8_t *ptr) {
if (ptr) {
static const int kPayloadSize = kFioBufferSize - 4;
const int size = fread(ptr, 1, kPayloadSize, _fp);
assert(size == kPayloadSize);
uint8_t buf[4];
const int count = fread(buf, 1, 4, _fp);
assert(count == 4);
if (kCheckSectorFileCrc) {
const uint32_t crc = fioUpdateCRC(0, ptr, kPayloadSize);
assert(crc == READ_LE_UINT32(buf));
}
} else {
const int size = fread(_buf, 1, kFioBufferSize, _fp);
assert(size == kFioBufferSize);
if (kCheckSectorFileCrc) {
const uint32_t crc = fioUpdateCRC(0, _buf, kFioBufferSize);
assert(crc == 0);
}
_bufPos = 0;
}
}
void SectorFile::seekAlign(uint32_t pos) {
pos += (pos / 2048) * 4;
const long alignPos = pos & ~2047;
if (alignPos != (ftell(_fp) - 2048)) {
fseek(_fp, alignPos, SEEK_SET);
refillBuffer();
}
_bufPos = pos - alignPos;
}
void SectorFile::seek(int pos, int whence) {
if (whence == SEEK_SET) {
assert((pos & 2047) == 0);
_bufPos = 2044;
File::seek(pos, SEEK_SET);
} else {
assert(whence == SEEK_CUR && pos >= 0);
const int bufLen = 2044 - _bufPos;
if (pos < bufLen) {
_bufPos += pos;
} else {
pos -= bufLen;
const int count = (fioAlignSizeTo2048(pos) / 2048) - 1;
if (count > 0) {
const int alignPos = count * 2048;
fseek(_fp, alignPos, SEEK_CUR);
}
refillBuffer();
_bufPos = pos % 2044;
}
}
}
int SectorFile::read(uint8_t *ptr, int size) {
const int bufLen = 2044 - _bufPos;
if (size >= bufLen) {
if (bufLen) {
memcpy(ptr, _buf + _bufPos, bufLen);
ptr += bufLen;
size -= bufLen;
}
const int count = (fioAlignSizeTo2048(size) / 2048) - 1;
for (int i = 0; i < count; ++i) {
refillBuffer(ptr);
ptr += 2044;
size -= 2044;
}
refillBuffer();
}
if (size != 0) {
assert(size <= 2044 - _bufPos);
memcpy(ptr, _buf + _bufPos, size);
_bufPos += size;
}
return 0;
}