-
Notifications
You must be signed in to change notification settings - Fork 2
/
cdfh.c
199 lines (168 loc) · 4.23 KB
/
cdfh.c
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
192
193
194
195
196
197
198
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hw_md.h"
#include "cdfh.h"
//----------------------------------------------------------------------
// SegaCD File Handler - by Chilly Willy
// Inspired by the MikMod READER structure.
//----------------------------------------------------------------------
static uint8_t cd_Eof(CDFileHandle_t *handle)
{
if (!handle)
return 1;
if (handle->pos >= handle->length)
return 1;
return 0;
}
static int32_t cd_Read(CDFileHandle_t *handle, void *ptr, int32_t size)
{
int32_t pos, blk, len, read = 0;
uint8_t *dst = ptr;
if (!handle)
return 0;
while (size != 0)
{
if (handle->Eof(handle))
return read;
pos = handle->pos;
blk = (pos >> 11) + handle->offset;
if (handle->block != blk)
{
read_cd(blk, 1, (void *)0x6800);
handle->block = blk;
}
len = 0x800 - (pos & 0x7FF);
if (len > size)
len = size;
if (len > (handle->length - pos))
len = (handle->length - pos);
memcpy(dst, (char *)0x6800 + (pos & 0x7FF), len);
handle->pos += len;
dst += len;
read += len;
size -= len;
}
return read;
}
static uint8_t cd_Get(CDFileHandle_t *handle)
{
int32_t pos, blk;
if (handle->Eof(handle))
return 0;
pos = handle->pos;
blk = (pos >> 11) + handle->offset;
if (handle->block != blk)
{
read_cd(blk, 1, (void *)0x6800);
handle->block = blk;
}
handle->pos++;
return ((uint8_t*)0x6800)[pos & 0x7FF];
}
static int32_t cd_Seek(CDFileHandle_t *handle, int32_t offset, int32_t whence)
{
int32_t pos;
if (!handle)
return -1;
pos = handle->pos;
switch(whence)
{
case SEEK_CUR:
pos += offset;
break;
case SEEK_SET:
pos = offset;
break;
case SEEK_END:
pos = handle->length - offset - 1;
break;
}
if (pos < 0)
handle->pos = 0;
else if (pos > handle->length)
handle->pos = handle->length;
else
handle->pos = pos;
return handle->pos;
}
static int32_t cd_Tell(CDFileHandle_t *handle)
{
return handle ? handle->pos : 0;
}
CDFileHandle_t *cd_handle_from_offset(int32_t offset, int32_t length)
{
CDFileHandle_t *handle = (CDFileHandle_t*)malloc(sizeof(CDFileHandle_t));
if (handle)
{
handle->Eof = &cd_Eof;
handle->Read = &cd_Read;
handle->Get = &cd_Get;
handle->Seek = &cd_Seek;
handle->Tell = &cd_Tell;
handle->offset = offset;
handle->length = length;
handle->block = -1; // nothing read yet
handle->pos = 0;
}
return handle;
}
CDFileHandle_t *cd_handle_from_name(char *name)
{
int32_t i;
char temp[256];
CDFileHandle_t *handle = (CDFileHandle_t*)malloc(sizeof(CDFileHandle_t));
if (handle)
{
handle->Eof = &cd_Eof;
handle->Read = &cd_Read;
handle->Get = &cd_Get;
handle->Seek = &cd_Seek;
handle->Tell = &cd_Tell;
i = strlen(name);
while (i && (name[i] != '/'))
i--;
if (name[i] == '/')
{
if (i)
{
strncpy(temp, name, i);
temp[i] = 0;
}
else
{
strcpy(temp, "/");
}
if (set_cwd(temp) < 0)
{
// error setting working directory
free(handle);
return NULL;
}
strncpy(temp, &name[i+1], 255);
temp[255] = 0;
}
else
{
strncpy(temp, name, 255);
temp[255] = 0;
}
if (find_dir_entry(temp) < 0)
{
// error finding entry
free(handle);
return NULL;
}
handle->offset = global_vars->DENTRY_OFFSET;
handle->length = global_vars->DENTRY_LENGTH;
handle->block = -1; // nothing read yet
handle->pos = 0;
}
return handle;
}
void delete_cd_handle(CDFileHandle_t *handle)
{
if (handle)
free(handle);
}