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 /* ---------------------------- zlibinflate writer -------------------------- */
169 struct reader_t*input;
170 unsigned char readbuffer[ZLIB_BUFFER_SIZE];
175 static void zlib_error(int ret, char* msg, z_stream*zs)
177 fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
180 zs->msg?zs->msg:"unknown");
186 static int reader_zlibinflate(struct reader_t*reader, void* data, int len)
189 struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
197 z->zs.next_out = data;
198 z->zs.avail_out = len;
201 if(!z->zs.avail_in) {
202 z->zs.avail_in = z->input->read(z->input, z->readbuffer, ZLIB_BUFFER_SIZE);
203 z->zs.next_in = z->readbuffer;
206 ret = inflate(&z->zs, Z_NO_FLUSH);
208 ret = inflate(&z->zs, Z_FINISH);
211 ret != Z_STREAM_END) zlib_error(ret, "bitio:inflate_inflate", &z->zs);
213 if (ret == Z_STREAM_END) {
214 int pos = z->zs.next_out - (Bytef*)data;
215 ret = inflateEnd(&z->zs);
216 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_end", &z->zs);
217 free(reader->internal);
218 reader->internal = 0;
222 if(!z->zs.avail_out) {
229 fprintf(stderr, "Error: swftools was compiled without zlib support");
233 void reader_init_zlibinflate(struct reader_t*r, struct reader_t*input)
236 struct zlibinflate_t*z;
238 memset(r, 0, sizeof(struct reader_t));
239 z = (struct zlibinflate_t*)malloc(sizeof(struct zlibinflate_t));
240 memset(z, 0, sizeof(struct zlibinflate_t));
242 r->read = reader_zlibinflate;
243 r->type = READER_TYPE_ZLIB;
246 memset(&z->zs,0,sizeof(z_stream));
247 z->zs.zalloc = Z_NULL;
248 z->zs.zfree = Z_NULL;
249 z->zs.opaque = Z_NULL;
250 ret = inflateInit(&z->zs);
251 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_init", &z->zs);
254 fprintf(stderr, "Error: swftools was compiled without zlib support");
258 /* ---------------------------- zlibdeflate writer -------------------------- */
264 struct writer_t*output;
265 unsigned char writebuffer[ZLIB_BUFFER_SIZE];
269 static int writer_zlibdeflate_write(struct writer_t*writer, void* data, int len)
272 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
275 fprintf(stderr, "zlib not initialized!\n");
281 z->zs.next_in = data;
282 z->zs.avail_in = len;
285 ret = deflate(&z->zs, Z_NO_FLUSH);
287 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
289 if(z->zs.next_out != z->writebuffer) {
290 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
291 z->zs.next_out = z->writebuffer;
292 z->zs.avail_out = ZLIB_BUFFER_SIZE;
295 if(!z->zs.avail_in) {
302 fprintf(stderr, "Error: swftools was compiled without zlib support");
306 static void writer_zlibdeflate_finish(struct writer_t*writer)
309 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
310 struct writer_t*output;
316 ret = deflate(&z->zs, Z_FINISH);
318 ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_finish", &z->zs);
320 if(z->zs.next_out != z->writebuffer) {
321 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
322 z->zs.next_out = z->writebuffer;
323 z->zs.avail_out = ZLIB_BUFFER_SIZE;
326 if (ret == Z_STREAM_END) {
331 ret = deflateEnd(&z->zs);
332 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_end", &z->zs);
333 free(writer->internal);
334 writer->internal = 0;
335 output->finish(output);
337 fprintf(stderr, "Error: swftools was compiled without zlib support");
341 void writer_init_zlibdeflate(struct writer_t*w, struct writer_t*output)
344 struct zlibdeflate_t*z;
346 memset(w, 0, sizeof(struct writer_t));
347 z = (struct zlibdeflate_t*)malloc(sizeof(struct zlibdeflate_t));
348 memset(z, 0, sizeof(struct zlibdeflate_t));
350 w->write = writer_zlibdeflate_write;
351 w->finish = writer_zlibdeflate_finish;
352 w->type = WRITER_TYPE_ZLIB;
355 memset(&z->zs,0,sizeof(z_stream));
356 z->zs.zalloc = Z_NULL;
357 z->zs.zfree = Z_NULL;
358 z->zs.opaque = Z_NULL;
359 ret = deflateInit(&z->zs, 9);
360 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_init", &z->zs);
363 z->zs.next_out = z->writebuffer;
364 z->zs.avail_out = ZLIB_BUFFER_SIZE;
366 fprintf(stderr, "Error: swftools was compiled without zlib support");
371 /* ----------------------- bit handling routines -------------------------- */
373 void writer_writebit(struct writer_t*w, int bit)
377 w->write(w, &w->mybyte, 1);
382 w->mybyte |= 1 << (7 - w->bitpos);
385 void writer_writebits(struct writer_t*w, unsigned int data, int bits)
390 writer_writebit(w, (data >> (bits-t-1))&1);
393 void writer_resetbits(struct writer_t*w)
396 w->write(w, &w->mybyte, 1);
401 unsigned int reader_readbit(struct reader_t*r)
406 r->read(r, &r->mybyte, 1);
408 return (r->mybyte>>(7-r->bitpos++))&1;
410 unsigned int reader_readbits(struct reader_t*r, int num)
417 val|=reader_readbit(r);
421 void reader_resetbits(struct reader_t*r)