2 implementation of bitio.h.
4 Part of the swftools package.
6 Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
8 This file is distributed under the GPL, see file COPYING for details */
16 #include "../config.h"
20 #define ZLIB_BUFFER_SIZE 16384
24 /* ---------------------------- file reader ------------------------------- */
26 static int reader_fileread(struct reader_t*reader, void* data, int len)
28 int ret = read((int)reader->internal, data, len);
33 void reader_init_filereader(struct reader_t*r, int handle)
35 r->read = reader_fileread;
36 r->internal = (void*)handle;
37 r->type = READER_TYPE_FILE;
43 /* ---------------------------- mem reader ------------------------------- */
50 static int reader_memread(struct reader_t*reader, void* data, int len)
52 struct memread_t*mr = (struct memread_t*)reader->internal;
54 if(mr->length - reader->pos > len) {
55 memcpy(data, &mr->data[reader->pos], len);
59 memcpy(data, &mr->data[reader->pos], mr->length - reader->pos);
60 reader->pos = mr->length;
61 return mr->length - reader->pos;
64 void reader_init_memreader(struct reader_t*r, void*newdata, int newlength)
66 struct memread_t*mr = malloc(sizeof(struct memread_t));
68 mr->length = newlength;
69 r->read = reader_memread;
70 r->internal = (void*)mr;
71 r->type = READER_TYPE_MEM;
77 /* ---------------------------- mem writer ------------------------------- */
85 static int writer_memwrite_write(struct writer_t*w, void* data, int len)
87 struct memread_t*mw = (struct memread_t*)w->internal;
88 if(mw->length - w->pos > len) {
89 memcpy(&mw->data[w->pos], data, len);
93 memcpy(&mw->data[w->pos], data, mw->length - w->pos);
95 return mw->length - w->pos;
98 static void writer_memwrite_finish(struct writer_t*w)
102 void writer_init_memwriter(struct writer_t*w, void*data, int len)
104 struct memwrite_t *mr;
105 mr = malloc(sizeof(struct memwrite_t));
108 memset(w, 0, sizeof(struct writer_t));
109 w->write = writer_memwrite_write;
110 w->finish = writer_memwrite_finish;
111 w->internal = (void*)mr;
112 w->type = WRITER_TYPE_FILE;
118 /* ---------------------------- file writer ------------------------------- */
126 static int writer_filewrite_write(struct writer_t*w, void* data, int len)
128 struct filewrite_t * fw= (struct filewrite_t*)w->internal;
129 return write(fw->handle, data, len);
131 static void writer_filewrite_finish(struct writer_t*w)
133 struct filewrite_t *mr = (struct filewrite_t*)w->internal;
138 void writer_init_filewriter(struct writer_t*w, int handle)
140 struct filewrite_t *mr = malloc(sizeof(struct filewrite_t));
143 memset(w, 0, sizeof(struct writer_t));
144 w->write = writer_filewrite_write;
145 w->finish = writer_filewrite_finish;
147 w->type = WRITER_TYPE_FILE;
152 void writer_init_filewriter2(struct writer_t*w, char*filename)
154 int fi = open("movie.swf",
158 O_WRONLY|O_CREAT|O_TRUNC, 0644);
159 writer_init_filewriter(w, fi);
160 ((struct filewrite_t*)w->internal)->free_handle = 1;
163 /* ---------------------------- null writer ------------------------------- */
165 static int writer_nullwrite_write(struct writer_t*w, void* data, int len)
169 static void writer_nullwrite_finish(struct writer_t*w)
172 void writer_init_nullwriter(struct writer_t*w)
174 memset(w, 0, sizeof(struct writer_t));
175 w->write = writer_nullwrite_write;
176 w->finish = writer_nullwrite_finish;
178 w->type = WRITER_TYPE_NULL;
183 /* ---------------------------- zlibinflate writer -------------------------- */
189 struct reader_t*input;
190 unsigned char readbuffer[ZLIB_BUFFER_SIZE];
195 static void zlib_error(int ret, char* msg, z_stream*zs)
197 fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
200 zs->msg?zs->msg:"unknown");
206 static int reader_zlibinflate(struct reader_t*reader, void* data, int len)
209 struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
217 z->zs.next_out = data;
218 z->zs.avail_out = len;
221 if(!z->zs.avail_in) {
222 z->zs.avail_in = z->input->read(z->input, z->readbuffer, ZLIB_BUFFER_SIZE);
223 z->zs.next_in = z->readbuffer;
226 ret = inflate(&z->zs, Z_NO_FLUSH);
228 ret = inflate(&z->zs, Z_FINISH);
231 ret != Z_STREAM_END) zlib_error(ret, "bitio:inflate_inflate", &z->zs);
233 if (ret == Z_STREAM_END) {
234 int pos = z->zs.next_out - (Bytef*)data;
235 ret = inflateEnd(&z->zs);
236 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_end", &z->zs);
237 free(reader->internal);
238 reader->internal = 0;
242 if(!z->zs.avail_out) {
249 fprintf(stderr, "Error: swftools was compiled without zlib support");
253 void reader_init_zlibinflate(struct reader_t*r, struct reader_t*input)
256 struct zlibinflate_t*z;
258 memset(r, 0, sizeof(struct reader_t));
259 z = (struct zlibinflate_t*)malloc(sizeof(struct zlibinflate_t));
260 memset(z, 0, sizeof(struct zlibinflate_t));
262 r->read = reader_zlibinflate;
263 r->type = READER_TYPE_ZLIB;
266 memset(&z->zs,0,sizeof(z_stream));
267 z->zs.zalloc = Z_NULL;
268 z->zs.zfree = Z_NULL;
269 z->zs.opaque = Z_NULL;
270 ret = inflateInit(&z->zs);
271 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_init", &z->zs);
274 fprintf(stderr, "Error: swftools was compiled without zlib support");
278 /* ---------------------------- zlibdeflate writer -------------------------- */
284 struct writer_t*output;
285 unsigned char writebuffer[ZLIB_BUFFER_SIZE];
289 static int writer_zlibdeflate_write(struct writer_t*writer, void* data, int len)
292 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
295 fprintf(stderr, "zlib not initialized!\n");
301 z->zs.next_in = data;
302 z->zs.avail_in = len;
305 ret = deflate(&z->zs, Z_NO_FLUSH);
307 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
309 if(z->zs.next_out != z->writebuffer) {
310 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
311 z->zs.next_out = z->writebuffer;
312 z->zs.avail_out = ZLIB_BUFFER_SIZE;
315 if(!z->zs.avail_in) {
322 fprintf(stderr, "Error: swftools was compiled without zlib support");
326 static void writer_zlibdeflate_finish(struct writer_t*writer)
329 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
330 struct writer_t*output;
336 ret = deflate(&z->zs, Z_FINISH);
338 ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_finish", &z->zs);
340 if(z->zs.next_out != z->writebuffer) {
341 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
342 z->zs.next_out = z->writebuffer;
343 z->zs.avail_out = ZLIB_BUFFER_SIZE;
346 if (ret == Z_STREAM_END) {
351 ret = deflateEnd(&z->zs);
352 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_end", &z->zs);
353 free(writer->internal);
354 writer->internal = 0;
355 output->finish(output);
357 fprintf(stderr, "Error: swftools was compiled without zlib support");
361 void writer_init_zlibdeflate(struct writer_t*w, struct writer_t*output)
364 struct zlibdeflate_t*z;
366 memset(w, 0, sizeof(struct writer_t));
367 z = (struct zlibdeflate_t*)malloc(sizeof(struct zlibdeflate_t));
368 memset(z, 0, sizeof(struct zlibdeflate_t));
370 w->write = writer_zlibdeflate_write;
371 w->finish = writer_zlibdeflate_finish;
372 w->type = WRITER_TYPE_ZLIB;
375 memset(&z->zs,0,sizeof(z_stream));
376 z->zs.zalloc = Z_NULL;
377 z->zs.zfree = Z_NULL;
378 z->zs.opaque = Z_NULL;
379 ret = deflateInit(&z->zs, 9);
380 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_init", &z->zs);
383 z->zs.next_out = z->writebuffer;
384 z->zs.avail_out = ZLIB_BUFFER_SIZE;
386 fprintf(stderr, "Error: swftools was compiled without zlib support");
391 /* ----------------------- bit handling routines -------------------------- */
393 void writer_writebit(struct writer_t*w, int bit)
397 w->write(w, &w->mybyte, 1);
402 w->mybyte |= 1 << (7 - w->bitpos);
405 void writer_writebits(struct writer_t*w, unsigned int data, int bits)
410 writer_writebit(w, (data >> (bits-t-1))&1);
413 void writer_resetbits(struct writer_t*w)
416 w->write(w, &w->mybyte, 1);
421 unsigned int reader_readbit(struct reader_t*r)
426 r->read(r, &r->mybyte, 1);
428 return (r->mybyte>>(7-r->bitpos++))&1;
430 unsigned int reader_readbits(struct reader_t*r, int num)
437 val|=reader_readbit(r);
441 void reader_resetbits(struct reader_t*r)