3 implementation of bitio.h.
5 Copyright (C) 2003 Matthias Kramm <kramm@quiss.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
27 #include "../config.h"
31 #define ZLIB_BUFFER_SIZE 16384
35 /* ---------------------------- file reader ------------------------------- */
37 static int reader_fileread(struct reader_t*reader, void* data, int len)
39 int ret = read((int)reader->internal, data, len);
44 void reader_init_filereader(struct reader_t*r, int handle)
46 r->read = reader_fileread;
47 r->internal = (void*)handle;
48 r->type = READER_TYPE_FILE;
54 /* ---------------------------- mem reader ------------------------------- */
61 static int reader_memread(struct reader_t*reader, void* data, int len)
63 struct memread_t*mr = (struct memread_t*)reader->internal;
65 if(mr->length - reader->pos > len) {
66 memcpy(data, &mr->data[reader->pos], len);
70 memcpy(data, &mr->data[reader->pos], mr->length - reader->pos);
71 reader->pos = mr->length;
72 return mr->length - reader->pos;
75 void reader_init_memreader(struct reader_t*r, void*newdata, int newlength)
77 struct memread_t*mr = malloc(sizeof(struct memread_t));
79 mr->length = newlength;
80 r->read = reader_memread;
81 r->internal = (void*)mr;
82 r->type = READER_TYPE_MEM;
88 /* ---------------------------- mem writer ------------------------------- */
96 static int writer_memwrite_write(struct writer_t*w, void* data, int len)
98 struct memread_t*mw = (struct memread_t*)w->internal;
99 if(mw->length - w->pos > len) {
100 memcpy(&mw->data[w->pos], data, len);
104 memcpy(&mw->data[w->pos], data, mw->length - w->pos);
106 return mw->length - w->pos;
109 static void writer_memwrite_finish(struct writer_t*w)
113 void writer_init_memwriter(struct writer_t*w, void*data, int len)
115 struct memwrite_t *mr;
116 mr = malloc(sizeof(struct memwrite_t));
119 memset(w, 0, sizeof(struct writer_t));
120 w->write = writer_memwrite_write;
121 w->finish = writer_memwrite_finish;
122 w->internal = (void*)mr;
123 w->type = WRITER_TYPE_FILE;
129 /* ---------------------------- file writer ------------------------------- */
137 static int writer_filewrite_write(struct writer_t*w, void* data, int len)
139 struct filewrite_t * fw= (struct filewrite_t*)w->internal;
140 return write(fw->handle, data, len);
142 static void writer_filewrite_finish(struct writer_t*w)
144 struct filewrite_t *mr = (struct filewrite_t*)w->internal;
149 void writer_init_filewriter(struct writer_t*w, int handle)
151 struct filewrite_t *mr = malloc(sizeof(struct filewrite_t));
154 memset(w, 0, sizeof(struct writer_t));
155 w->write = writer_filewrite_write;
156 w->finish = writer_filewrite_finish;
158 w->type = WRITER_TYPE_FILE;
163 void writer_init_filewriter2(struct writer_t*w, char*filename)
165 int fi = open("movie.swf",
169 O_WRONLY|O_CREAT|O_TRUNC, 0644);
170 writer_init_filewriter(w, fi);
171 ((struct filewrite_t*)w->internal)->free_handle = 1;
174 /* ---------------------------- null writer ------------------------------- */
176 static int writer_nullwrite_write(struct writer_t*w, void* data, int len)
181 static void writer_nullwrite_finish(struct writer_t*w)
184 void writer_init_nullwriter(struct writer_t*w)
186 memset(w, 0, sizeof(struct writer_t));
187 w->write = writer_nullwrite_write;
188 w->finish = writer_nullwrite_finish;
190 w->type = WRITER_TYPE_NULL;
195 /* ---------------------------- zlibinflate writer -------------------------- */
201 struct reader_t*input;
202 unsigned char readbuffer[ZLIB_BUFFER_SIZE];
207 static void zlib_error(int ret, char* msg, z_stream*zs)
209 fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
212 zs->msg?zs->msg:"unknown");
218 static int reader_zlibinflate(struct reader_t*reader, void* data, int len)
221 struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
229 z->zs.next_out = data;
230 z->zs.avail_out = len;
233 if(!z->zs.avail_in) {
234 z->zs.avail_in = z->input->read(z->input, z->readbuffer, ZLIB_BUFFER_SIZE);
235 z->zs.next_in = z->readbuffer;
238 ret = inflate(&z->zs, Z_NO_FLUSH);
240 ret = inflate(&z->zs, Z_FINISH);
243 ret != Z_STREAM_END) zlib_error(ret, "bitio:inflate_inflate", &z->zs);
245 if (ret == Z_STREAM_END) {
246 int pos = z->zs.next_out - (Bytef*)data;
247 ret = inflateEnd(&z->zs);
248 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_end", &z->zs);
249 free(reader->internal);
250 reader->internal = 0;
254 if(!z->zs.avail_out) {
261 fprintf(stderr, "Error: swftools was compiled without zlib support");
265 void reader_init_zlibinflate(struct reader_t*r, struct reader_t*input)
268 struct zlibinflate_t*z;
270 memset(r, 0, sizeof(struct reader_t));
271 z = (struct zlibinflate_t*)malloc(sizeof(struct zlibinflate_t));
272 memset(z, 0, sizeof(struct zlibinflate_t));
274 r->read = reader_zlibinflate;
275 r->type = READER_TYPE_ZLIB;
278 memset(&z->zs,0,sizeof(z_stream));
279 z->zs.zalloc = Z_NULL;
280 z->zs.zfree = Z_NULL;
281 z->zs.opaque = Z_NULL;
282 ret = inflateInit(&z->zs);
283 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_init", &z->zs);
286 fprintf(stderr, "Error: swftools was compiled without zlib support");
290 /* ---------------------------- zlibdeflate writer -------------------------- */
296 struct writer_t*output;
297 unsigned char writebuffer[ZLIB_BUFFER_SIZE];
301 static int writer_zlibdeflate_write(struct writer_t*writer, void* data, int len)
304 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
306 if(writer->type != WRITER_TYPE_ZLIB) {
307 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
311 fprintf(stderr, "zlib not initialized!\n");
317 z->zs.next_in = data;
318 z->zs.avail_in = len;
321 ret = deflate(&z->zs, Z_NO_FLUSH);
323 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
325 if(z->zs.next_out != z->writebuffer) {
326 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
327 z->zs.next_out = z->writebuffer;
328 z->zs.avail_out = ZLIB_BUFFER_SIZE;
331 if(!z->zs.avail_in) {
338 fprintf(stderr, "Error: swftools was compiled without zlib support");
342 static void writer_zlibdeflate_finish(struct writer_t*writer)
345 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
346 struct writer_t*output;
348 if(writer->type != WRITER_TYPE_ZLIB) {
349 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
356 ret = deflate(&z->zs, Z_FINISH);
358 ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_finish", &z->zs);
360 if(z->zs.next_out != z->writebuffer) {
361 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
362 z->zs.next_out = z->writebuffer;
363 z->zs.avail_out = ZLIB_BUFFER_SIZE;
366 if (ret == Z_STREAM_END) {
371 ret = deflateEnd(&z->zs);
372 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_end", &z->zs);
373 free(writer->internal);
374 writer->internal = 0;
375 //output->finish(output);
377 fprintf(stderr, "Error: swftools was compiled without zlib support");
381 void writer_init_zlibdeflate(struct writer_t*w, struct writer_t*output)
384 struct zlibdeflate_t*z;
386 memset(w, 0, sizeof(struct writer_t));
387 z = (struct zlibdeflate_t*)malloc(sizeof(struct zlibdeflate_t));
388 memset(z, 0, sizeof(struct zlibdeflate_t));
390 w->write = writer_zlibdeflate_write;
391 w->finish = writer_zlibdeflate_finish;
392 w->type = WRITER_TYPE_ZLIB;
395 memset(&z->zs,0,sizeof(z_stream));
396 z->zs.zalloc = Z_NULL;
397 z->zs.zfree = Z_NULL;
398 z->zs.opaque = Z_NULL;
399 ret = deflateInit(&z->zs, 9);
400 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_init", &z->zs);
403 z->zs.next_out = z->writebuffer;
404 z->zs.avail_out = ZLIB_BUFFER_SIZE;
406 fprintf(stderr, "Error: swftools was compiled without zlib support");
411 /* ----------------------- bit handling routines -------------------------- */
413 void writer_writebit(struct writer_t*w, int bit)
417 w->write(w, &w->mybyte, 1);
422 w->mybyte |= 1 << (7 - w->bitpos);
425 void writer_writebits(struct writer_t*w, unsigned int data, int bits)
430 writer_writebit(w, (data >> (bits-t-1))&1);
433 void writer_resetbits(struct writer_t*w)
436 w->write(w, &w->mybyte, 1);
441 unsigned int reader_readbit(struct reader_t*r)
446 r->read(r, &r->mybyte, 1);
448 return (r->mybyte>>(7-r->bitpos++))&1;
450 unsigned int reader_readbits(struct reader_t*r, int num)
457 val|=reader_readbit(r);
461 void reader_resetbits(struct reader_t*r)