-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswf_file.c
222 lines (170 loc) · 4.92 KB
/
swf_file.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
/*
* Copyright (C) 2001-2 Ben Evans <[email protected]>
* 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-1307, USA.
*
* $Id: swf_file.c,v 1.1 2002/05/31 10:54:27 muttley Exp $
*/
#include "swf_types.h"
#include "swf_parse.h"
#include "swf_file.h"
#include "swf_movie.h"
#include "swf_destroy.h"
#include "swf_serialise.h"
#include <stdio.h>
void
swf_movie_initbits(swf_movie * movie) {
movie->buffer->bitpos = 0;
movie->buffer->bitbuf = 0;
}
void
swf_movie_seek (swf_movie * context, int pos)
{
fseek(context->file, pos, SEEK_SET);
return;
}
/*
* Get an 8-bit byte from the stream, and move the
* parse head on by one.
*/
void
swf_movie_put_byte(swf_movie * context, int * error, SWF_U8 byte)
{
swf_movie_initbits(context);
/* FIXME: Need a write error code */
if (fwrite(&byte, 1, 1, context->file) != 1) {
*error = SWF_ETooManyFriends;
return;
}
context->filepos++;
return;
}
/*
* Ensure the parse head is byte-aligned and read the specified
* number of bytes from the stream.
*/
void
swf_movie_put_bytes (swf_movie * context, int * error, int nbytes, SWF_U8 * bytes)
{
swf_movie_initbits(context);
/* FIXME: Need a write error code */
if( fwrite(bytes, 1, nbytes, context->file) != nbytes){
*error = SWF_ETooManyFriends;
return;
}
context->filepos += nbytes;
return;
}
/*
* Put n bits from the stream.
*/
/* The problem here is... we can't finish our flush until
* we're sure we've got all the adjacent bit fields (and we may
* have SB's and UB's next to each other, as well....
* So, we have to separate out the flush function.
*/
void
swf_movie_flush_bits (swf_movie * context)
{
SWF_U32 bitbuf;
SWF_U8 bitpos, bt;
int error=0;
bitbuf = context->buffer->bitbuf;
bitpos = context->buffer->bitpos;
swf_movie_initbits(context);
/* FIXME: error handling... */
bt = bitbuf >> 24;
swf_movie_put_byte(context, &error, bt);
if (bitpos <= 8) { return; }
bt = (bitbuf << 8) >> 24;
swf_movie_put_byte(context, &error, bt);
if (bitpos <= 16) { return; }
bt = (bitbuf << 16) >> 24;
swf_movie_put_byte(context, &error, bt);
if (bitpos <= 24) { return; }
bt = (bitbuf << 24) >> 24;
swf_movie_put_byte(context, &error, bt);
}
/* FIXME: error handling... */
void
swf_movie_put_bits (swf_movie * context, SWF_U8 n, SWF_U32 bits)
{
SWF_S32 i;
SWF_U32 bitbuf;
SWF_U8 bt;
int error=0;
/* We take in the bits right-flushed. */
i = context->buffer->bitpos;
if (i + n > 32) {
/* We need to flush the buffer and leave the residue */
bitbuf = context->buffer->bitbuf;
bitbuf += bits >> (i + n - 32);
bt = bitbuf >> 24;
swf_movie_put_byte(context, &error, bt);
bt = (bitbuf << 8) >> 24;
swf_movie_put_byte(context, &error, bt);
bt = (bitbuf << 16) >> 24;
swf_movie_put_byte(context, &error, bt);
bt = (bitbuf << 24) >> 24;
swf_movie_put_byte(context, &error, bt);
swf_movie_initbits(context);
context->buffer->bitbuf = bits << (64 - (i + n));
context->buffer->bitpos = i + n - 32;
} else {
/* Can coexist in the same bitbuf */
context->buffer->bitbuf |= ((bits << (32 - n) ) >> i);
context->buffer->bitpos = i + n;
}
}
/*
* Put n bits to the stream with sign extension.
*/
void
swf_movie_put_sbits (swf_movie * context, SWF_U8 n, SWF_S32 bits)
{
int i;
i = context->buffer->bitpos;
if (bits < 0) {
bits += (1L << (n));
}
swf_movie_put_bits(context, n, bits);
}
/*
* Put a 16-bit word to the stream, and move the
* parse head on by two.
*/
void
swf_movie_put_word(swf_movie * context, int * error, SWF_U16 word)
{
swf_movie_initbits(context);
swf_movie_put_byte(context, error, (SWF_U8)((word << 8) >> 8));
swf_movie_put_byte(context, error, (SWF_U8)(word >> 8));
}
/*
* Put a 32-bit dword to the stream, and move the
* parse head on by four.
*/
void
swf_movie_put_dword(swf_movie * context, int * error, SWF_U32 dword)
{
swf_movie_initbits(context);
swf_movie_put_byte(context, error, (SWF_U8)((dword << 24) >> 24));
swf_movie_put_byte(context, error, (SWF_U8)((dword << 16) >> 24));
swf_movie_put_byte(context, error, (SWF_U8)((dword << 8) >> 24));
swf_movie_put_byte(context, error, (SWF_U8)(dword >> 24));
}
void
swf_movie_put_string(swf_movie * context, int * error, char * mystring)
{
}