-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiff.c
96 lines (72 loc) · 2.48 KB
/
iff.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
/* Nitfol - z-machine interpreter using Glk for output.
Copyright (C) 1999 Evin Robertson
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
The author can be reached at nitfol@deja.com
*/
#include "nitfol.h"
BOOL iffgetchunk(strid_t stream, char *desttype, glui32 *ulength,
glui32 file_size)
{
int i;
glui32 c;
unsigned char length[4];
c = glk_stream_get_position(stream);
if(c & 1) {
glk_get_char_stream(stream); /* Eat padding */
c++;
}
if(glk_get_buffer_stream(stream, desttype, 4) != 4)
return FALSE;
if(glk_get_buffer_stream(stream, (char *) length, 4) != 4)
return FALSE;
*ulength = MSBdecode4(length);
for(i = 0; i < 4; i++)
if(desttype[i] < 0x20 || desttype[i] > 0x7e)
return FALSE;
c += 8;
return ((c + *ulength) <= file_size);
}
BOOL ifffindchunk(strid_t stream, const char *type, glui32 *length, glui32 loc)
{
char t[4];
glui32 file_size;
glk_stream_set_position(stream, 0, seekmode_End);
file_size = glk_stream_get_position(stream);
glk_stream_set_position(stream, loc, seekmode_Start);
*length = 0;
do {
glk_stream_set_position(stream, *length, seekmode_Current);
if(!iffgetchunk(stream, t, length, file_size))
return FALSE;
} while((t[0] != type[0]) || (t[1] != type[1]) ||
(t[2] != type[2]) || (t[3] != type[3]));
return TRUE;
}
void iffputchunk(strid_t stream, const char *type, glui32 ulength)
{
glui32 c;
unsigned char length[4];
c = glk_stream_get_position(stream);
if(c & 1)
glk_put_char_stream(stream, 0); /* Spew padding */
MSBencode4(length, ulength);
w_glk_put_buffer_stream(stream, type, 4);
w_glk_put_buffer_stream(stream, (char *) length, 4);
}
void iffpadend(strid_t stream)
{
glui32 c;
c = glk_stream_get_position(stream);
if(c & 1)
glk_put_char_stream(stream, 0); /* Spew padding */
}