-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiffr.c
315 lines (261 loc) · 9.12 KB
/
iffr.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include <exec/types.h>
#include <libraries/dos.h>
#include <proto/dos.h>
#include "hdrs/iff.h"
/*------------------------------------------------------------------------*/
#ifndef OFFSET_BEGINNING
#define OFFSET_BEGINNING OFFSET_BEGINING
#endif
/*------------------------------------------------------------------------*/
IFFP OpenRIFF(BPTR file0, GroupContext *new0, ClientFrame *clientFrame)
{
REGISTER BPTR file=file0;
REGISTER GroupContext *new = new0;
IFFP iffp=IFF_OKAY;
new->parent =NULL;
new->clientFrame =clientFrame;
new->file =file;
new->position =0;
new->ckHdr.ckID =new->subtype =NULL_CHUNK;
new->ckHdr.ckSize=new->bytesSoFar=0;
if(file<=0) return(NO_FILE);
Seek(file,0,OFFSET_END);
new->bound=Seek(file,0,OFFSET_CURRENT);
if(new->bound<0) return(DOS_ERROR);
Seek(file,0,OFFSET_BEGINNING);
if(new->bound<sizeof(ChunkHeader))
iffp=NOT_IFF;
return(iffp);
}
IFFP OpenRGroup(GroupContext *parent0, GroupContext *new0)
{
REGISTER GroupContext *parent=parent0;
REGISTER GroupContext *new =new0;
IFFP iffp=IFF_OKAY;
new->parent =parent;
new->clientFrame =parent->clientFrame;
new->file =parent->file;
new->position =parent->position;
new->bound =parent->position+ChunkMoreBytes(parent);
new->ckHdr.ckID =new->subtype =NULL_CHUNK;
new->ckHdr.ckSize=new->bytesSoFar=0;
if(new->bound>parent->bound || IS_ODD(new->bound))
iffp=BAD_IFF;
return(iffp);
}
IFFP CloseRGroup(GroupContext *context)
{
REGISTER LONG position;
if(context->parent!=NULL){
position=context->position;
context->parent->bytesSoFar+=position-context->parent->position;
context->parent->position=position;
}
return(IFF_OKAY);
}
IFFP SkipFwd(GroupContext *context, LONG bytes)
{
IFFP iffp=IFF_OKAY;
if(bytes>0){
if (Seek(context->file, bytes, OFFSET_CURRENT)==-1)
iffp = BAD_IFF;
else context->position += bytes;
}
return(iffp);
}
ID GetChunkHdr(GroupContext *context0)
{
REGISTER GroupContext *context=context0;
REGISTER IFFP iffp;
LONG remaining;
iffp=SkipFwd(context,ChunkMoreBytes(context)+
IS_ODD(context->ckHdr.ckSize));
CheckIFFP();
context->ckHdr.ckID=BAD_IFF;
context->subtype =NULL_CHUNK;
context->bytesSoFar=0;
remaining=context->bound-context->position;
if(remaining==0){
context->ckHdr.ckSize=0;
context->ckHdr.ckID =END_MARK;
}
else if(sizeof(ChunkHeader)>remaining){
context->ckHdr.ckSize = remaining;
}else {
switch(Read(context->file,&context->ckHdr,
sizeof(ChunkHeader))){
case -1: return(context->ckHdr.ckID=DOS_ERROR);
case 0: return(context->ckHdr.ckID=BAD_IFF);
}
if (context->parent==NULL)
switch(context->ckHdr.ckID){
case FORM:
case LIST:
case CAT : break;
default: return(context->ckHdr.ckID=NOT_IFF);
}
context->position+=sizeof(ChunkHeader);
remaining -=sizeof(ChunkHeader);
if(context->ckHdr.ckID<=0)
context->ckHdr.ckID=BAD_IFF;
else if(context->ckHdr.ckSize<0
|| context->ckHdr.ckSize>remaining){
context->ckHdr.ckSize=remaining;
context->ckHdr.ckID =BAD_IFF;
}else switch(context->ckHdr.ckID){
case LIST:
case FORM:
case PROP:
case CAT : {
iffp = IFFReadBytes(context,
(BYTE *)&context->subtype,
sizeof(ID));
if(iffp!=IFF_OKAY)
context->ckHdr.ckID=iffp;
break;
}
}
}
return(context->ckHdr.ckID);
}
IFFP IFFReadBytes(GroupContext *context, BYTE *buffer, LONG nBytes)
{
REGISTER IFFP iffp=IFF_OKAY;
if(nBytes<0)
iffp=CLIENT_ERROR;
else if(nBytes>ChunkMoreBytes(context))
iffp=SHORT_CHUNK;
else if(nBytes>0)
switch(Read(context->file,buffer,nBytes)){
case -1: {
iffp=DOS_ERROR;
break;
}
case 0: {
iffp=BAD_IFF;
break;
}
default: {
context->position +=nBytes;
context->bytesSoFar+=nBytes;
}
}
return(iffp);
}
IFFP SkipGroup(GroupContext *context)
{}
IFFP ReadIFF(BPTR file, ClientFrame *clientFrame)
{
IFFP iffp;
GroupContext context;
iffp=OpenRIFF(file,&context);
context.clientFrame=clientFrame;
if(iffp==IFF_OKAY)
switch(iffp=GetChunkHdr(&context)) {
case FORM: {
iffp=(*clientFrame->getForm)(&context);
break;
}
case LIST: {
iffp=(*clientFrame->getList)(&context);
break;
}
case CAT : {
iffp=(*clientFrame->getCat )(&context);
break;
}
}
CloseRGroup(&context);
if(iffp>0)
iffp = NOT_IFF;
return(iffp);
}
IFFP ReadIList(GroupContext *parent, ClientFrame *clientFrame)
{
GroupContext listContext;
IFFP iffp;
BOOL propOk=TRUE;
iffp=OpenRGroup(parent,&listContext);
CheckIFFP();
if(parent->ckHdr.ckID==CAT)
propOk=FALSE;
else
listContext.clientFrame=clientFrame;
do {
switch(iffp=GetChunkHdr(&listContext)) {
case PROP: {
if(propOk) iffp=(*clientFrame->getProp)
(&listContext);
else iffp=BAD_IFF;
break;
}
case FORM: {
iffp=(*clientFrame->getForm)(&listContext);
break;
}
case LIST: {
iffp=(*clientFrame->getList)(&listContext);
break;
}
case CAT : {
iffp=(*clientFrame->getCat )(&listContext);
break;
}
}
if (listContext.ckHdr.ckID!=PROP)
propOk=FALSE;
} while(iffp==IFF_OKAY);
CloseRGroup(&listContext);
if(iffp>0) iffp=BAD_IFF;
return(iffp==END_MARK ? IFF_OKAY : iffp);
}
IFFP ReadICat(GroupContext *parent)
{
return(ReadIList(parent,NULL));
}
ID GetFChunkHdr(GroupContext *context)
{
REGISTER ID id;
id=GetChunkHdr(context);
if(id==PROP) context->ckHdr.ckID=id=BAD_IFF;
return(id);
}
ID GetF1ChunkHdr(GroupContext *context)
{
REGISTER ID id;
REGISTER ClientFrame *clientFrame=context->clientFrame;
switch(id=GetChunkHdr(context)){
case PROP: {
id=BAD_IFF;
break;
}
case FORM: {
id=(*clientFrame->getForm)(context);
break;
}
case LIST: {
id=(*clientFrame->getList)(context);
break;
}
case CAT : {
id=(*clientFrame->getCat )(context);
break;
}
}
return(context->ckHdr.ckID = id);
}
ID GetPChunkHdr(GroupContext *context)
{
REGISTER ID id;
id=GetChunkHdr(context);
switch(id) {
case LIST:
case FORM:
case PROP:
case CAT : {
id=context->ckHdr.ckID=BAD_IFF;
break;
}
}
return(id);
}