upgrade to xpdf 3.00.
[swftools.git] / lib / bitio.c
blob26d89c1f62b3c9949eedda79ab93afaaa4f6a81e
1 /* bitio.c
2 part of swftools
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 */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <memory.h>
25 #include <fcntl.h>
27 #include "../config.h"
29 #ifdef HAVE_ZLIB
30 #include <zlib.h>
31 #define ZLIB_BUFFER_SIZE 16384
32 #endif
33 #include "./bitio.h"
35 /* ---------------------------- null reader ------------------------------- */
37 static int reader_nullread(struct reader_t*r, void* data, int len)
39 memset(data, 0, len);
40 return len;
42 static void reader_nullread_dealloc(struct reader_t*r)
44 memset(r, 0, sizeof(struct reader_t));
46 void reader_init_nullreader(struct reader_t*r)
48 r->read = reader_nullread;
49 r->dealloc = reader_nullread_dealloc;
50 r->internal = 0;
51 r->type = READER_TYPE_NULL;
52 r->mybyte = 0;
53 r->bitpos = 8;
54 r->pos = 0;
56 /* ---------------------------- file reader ------------------------------- */
58 static int reader_fileread(struct reader_t*reader, void* data, int len)
60 int ret = read((int)reader->internal, data, len);
61 if(ret>=0)
62 reader->pos += ret;
63 return ret;
65 void reader_init_filereader(struct reader_t*r, int handle)
67 r->read = reader_fileread;
68 r->internal = (void*)handle;
69 r->type = READER_TYPE_FILE;
70 r->mybyte = 0;
71 r->bitpos = 8;
72 r->pos = 0;
75 /* ---------------------------- mem reader ------------------------------- */
77 struct memread_t
79 unsigned char*data;
80 int length;
82 static int reader_memread(struct reader_t*reader, void* data, int len)
84 struct memread_t*mr = (struct memread_t*)reader->internal;
86 if(mr->length - reader->pos > len) {
87 memcpy(data, &mr->data[reader->pos], len);
88 reader->pos += len;
89 return len;
90 } else {
91 memcpy(data, &mr->data[reader->pos], mr->length - reader->pos);
92 reader->pos = mr->length;
93 return mr->length - reader->pos;
96 static void reader_memread_dealloc(struct reader_t*reader)
98 if(reader->internal)
99 free(reader->internal);
100 memset(reader, 0, sizeof(struct reader_t));
102 void reader_init_memreader(struct reader_t*r, void*newdata, int newlength)
104 struct memread_t*mr = malloc(sizeof(struct memread_t));
105 mr->data = newdata;
106 mr->length = newlength;
107 r->read = reader_memread;
108 r->dealloc = reader_memread_dealloc;
109 r->internal = (void*)mr;
110 r->type = READER_TYPE_MEM;
111 r->mybyte = 0;
112 r->bitpos = 8;
113 r->pos = 0;
116 /* ---------------------------- mem writer ------------------------------- */
118 struct memwrite_t
120 unsigned char*data;
121 int length;
124 static int writer_memwrite_write(struct writer_t*w, void* data, int len)
126 struct memwrite_t*mw = (struct memwrite_t*)w->internal;
127 if(mw->length - w->pos > len) {
128 memcpy(&mw->data[w->pos], data, len);
129 w->pos += len;
130 return len;
131 } else {
132 memcpy(&mw->data[w->pos], data, mw->length - w->pos);
133 w->pos = mw->length;
134 return mw->length - w->pos;
137 static void writer_memwrite_finish(struct writer_t*w)
139 if(w->internal)
140 free(w->internal);
141 w->internal = 0;
143 void writer_init_memwriter(struct writer_t*w, void*data, int len)
145 struct memwrite_t *mr;
146 mr = malloc(sizeof(struct memwrite_t));
147 mr->data = data;
148 mr->length = len;
149 memset(w, 0, sizeof(struct writer_t));
150 w->write = writer_memwrite_write;
151 w->finish = writer_memwrite_finish;
152 w->internal = (void*)mr;
153 w->type = WRITER_TYPE_MEM;
154 w->bitpos = 0;
155 w->mybyte = 0;
156 w->pos = 0;
159 /* ------------------------- growing mem writer ------------------------------- */
161 struct growmemwrite_t
163 unsigned char*data;
164 int length;
166 static int writer_growmemwrite_write(struct writer_t*w, void* data, int len)
168 struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
169 if(!mw->data) {
170 fprintf(stderr, "Illegal write operation: data already given away");
171 exit(1);
173 if(mw->length - w->pos <= len) {
174 unsigned char*newmem;
175 int newlength = mw->length;
176 while(newlength - w->pos < len) {
177 newlength += 4096;
179 newmem = malloc(newlength);
180 memcpy(newmem, mw->data, mw->length);
181 free(mw->data);
182 mw->data = newmem;
183 mw->length = newlength;
185 memcpy(&mw->data[w->pos], data, len);
186 w->pos += len;
187 return len;
189 static void writer_growmemwrite_finish(struct writer_t*w)
191 struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
192 if(mw->data)
193 free(mw->data);
194 mw->data = 0;
195 mw->length = 0;
196 free(w->internal);mw=0;
197 memset(w, 0, sizeof(struct writer_t));
199 void* writer_growmemwrite_getmem(struct writer_t*w)
201 struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
202 void*ret = mw->data;
203 /* remove own reference so that neither write() nor finish() can free it.
204 It's property of the caller now.
206 mw->data = 0;
207 return ret;
209 void writer_init_growingmemwriter(struct writer_t*w)
211 struct memwrite_t *mr;
212 mr = malloc(sizeof(struct memwrite_t));
213 mr->length = 4096;
214 mr->data = malloc(mr->length);
215 memset(w, 0, sizeof(struct writer_t));
216 w->write = writer_growmemwrite_write;
217 w->finish = writer_growmemwrite_finish;
218 w->internal = (void*)mr;
219 w->type = WRITER_TYPE_GROWING_MEM;
220 w->bitpos = 0;
221 w->mybyte = 0;
222 w->pos = 0;
225 /* ---------------------------- file writer ------------------------------- */
227 struct filewrite_t
229 int handle;
230 char free_handle;
233 static int writer_filewrite_write(struct writer_t*w, void* data, int len)
235 struct filewrite_t * fw= (struct filewrite_t*)w->internal;
236 return write(fw->handle, data, len);
238 static void writer_filewrite_finish(struct writer_t*w)
240 struct filewrite_t *mr = (struct filewrite_t*)w->internal;
241 if(mr->free_handle)
242 close(mr->handle);
243 free(w->internal);
244 memset(w, 0, sizeof(struct writer_t));
246 void writer_init_filewriter(struct writer_t*w, int handle)
248 struct filewrite_t *mr = malloc(sizeof(struct filewrite_t));
249 mr->handle = handle;
250 mr->free_handle = 0;
251 memset(w, 0, sizeof(struct writer_t));
252 w->write = writer_filewrite_write;
253 w->finish = writer_filewrite_finish;
254 w->internal = mr;
255 w->type = WRITER_TYPE_FILE;
256 w->bitpos = 0;
257 w->mybyte = 0;
258 w->pos = 0;
260 void writer_init_filewriter2(struct writer_t*w, char*filename)
262 int fi = open("movie.swf",
263 #ifdef O_BINARY
264 O_BINARY|
265 #endif
266 O_WRONLY|O_CREAT|O_TRUNC, 0644);
267 writer_init_filewriter(w, fi);
268 ((struct filewrite_t*)w->internal)->free_handle = 1;
271 /* ---------------------------- null writer ------------------------------- */
273 static int writer_nullwrite_write(struct writer_t*w, void* data, int len)
275 w->pos += len;
276 return len;
278 static void writer_nullwrite_finish(struct writer_t*w)
280 memset(w, 0, sizeof(struct writer_t));
282 void writer_init_nullwriter(struct writer_t*w)
284 memset(w, 0, sizeof(struct writer_t));
285 w->write = writer_nullwrite_write;
286 w->finish = writer_nullwrite_finish;
287 w->internal = 0;
288 w->type = WRITER_TYPE_NULL;
289 w->bitpos = 0;
290 w->mybyte = 0;
291 w->pos = 0;
293 /* ---------------------------- zlibinflate reader -------------------------- */
295 struct zlibinflate_t
297 #ifdef HAVE_ZLIB
298 z_stream zs;
299 struct reader_t*input;
300 unsigned char readbuffer[ZLIB_BUFFER_SIZE];
301 #endif
304 #ifdef HAVE_ZLIB
305 static void zlib_error(int ret, char* msg, z_stream*zs)
307 fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
308 msg,
309 ret,
310 zs->msg?zs->msg:"unknown");
311 perror("errno:");
312 exit(1);
314 #endif
316 static int reader_zlibinflate(struct reader_t*reader, void* data, int len)
318 #ifdef HAVE_ZLIB
319 struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
320 int ret;
321 if(!z) {
322 return 0;
324 if(!len)
325 return 0;
327 z->zs.next_out = data;
328 z->zs.avail_out = len;
330 while(1) {
331 if(!z->zs.avail_in) {
332 z->zs.avail_in = z->input->read(z->input, z->readbuffer, ZLIB_BUFFER_SIZE);
333 z->zs.next_in = z->readbuffer;
335 if(z->zs.avail_in)
336 ret = inflate(&z->zs, Z_NO_FLUSH);
337 else
338 ret = inflate(&z->zs, Z_FINISH);
340 if (ret != Z_OK &&
341 ret != Z_STREAM_END) zlib_error(ret, "bitio:inflate_inflate", &z->zs);
343 if (ret == Z_STREAM_END) {
344 int pos = z->zs.next_out - (Bytef*)data;
345 ret = inflateEnd(&z->zs);
346 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_end", &z->zs);
347 free(reader->internal);
348 reader->internal = 0;
349 reader->pos += pos;
350 return pos;
352 if(!z->zs.avail_out) {
353 break;
356 reader->pos += len;
357 return len;
358 #else
359 fprintf(stderr, "Error: swftools was compiled without zlib support");
360 exit(1);
361 #endif
363 static void reader_zlibinflate_dealloc(struct reader_t*reader)
365 #ifdef HAVE_ZLIB
366 struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
367 /* test whether read() already did basic deallocation */
368 if(reader->internal) {
369 inflateEnd(&z->zs);
370 free(reader->internal);
372 memset(reader, 0, sizeof(struct reader_t));
373 #endif
375 void reader_init_zlibinflate(struct reader_t*r, struct reader_t*input)
377 #ifdef HAVE_ZLIB
378 struct zlibinflate_t*z;
379 int ret;
380 memset(r, 0, sizeof(struct reader_t));
381 z = (struct zlibinflate_t*)malloc(sizeof(struct zlibinflate_t));
382 memset(z, 0, sizeof(struct zlibinflate_t));
383 r->internal = z;
384 r->read = reader_zlibinflate;
385 r->dealloc = reader_zlibinflate_dealloc;
386 r->type = READER_TYPE_ZLIB;
387 r->pos = 0;
388 z->input = input;
389 memset(&z->zs,0,sizeof(z_stream));
390 z->zs.zalloc = Z_NULL;
391 z->zs.zfree = Z_NULL;
392 z->zs.opaque = Z_NULL;
393 ret = inflateInit(&z->zs);
394 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_init", &z->zs);
395 reader_resetbits(r);
396 #else
397 fprintf(stderr, "Error: swftools was compiled without zlib support");
398 #endif
401 /* ---------------------------- zlibdeflate writer -------------------------- */
403 struct zlibdeflate_t
405 #ifdef HAVE_ZLIB
406 z_stream zs;
407 struct writer_t*output;
408 unsigned char writebuffer[ZLIB_BUFFER_SIZE];
409 #endif
412 static int writer_zlibdeflate_write(struct writer_t*writer, void* data, int len)
414 #ifdef HAVE_ZLIB
415 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
416 int ret;
417 if(writer->type != WRITER_TYPE_ZLIB) {
418 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
419 return 0;
421 if(!z) {
422 fprintf(stderr, "zlib not initialized!\n");
423 return 0;
425 if(!len)
426 return 0;
428 z->zs.next_in = data;
429 z->zs.avail_in = len;
431 while(1) {
432 ret = deflate(&z->zs, Z_NO_FLUSH);
434 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
436 if(z->zs.next_out != z->writebuffer) {
437 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
438 z->zs.next_out = z->writebuffer;
439 z->zs.avail_out = ZLIB_BUFFER_SIZE;
442 if(!z->zs.avail_in) {
443 break;
446 writer->pos += len;
447 return len;
448 #else
449 fprintf(stderr, "Error: swftools was compiled without zlib support");
450 exit(1);
451 #endif
453 static void writer_zlibdeflate_finish(struct writer_t*writer)
455 #ifdef HAVE_ZLIB
456 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
457 struct writer_t*output;
458 int ret;
459 if(writer->type != WRITER_TYPE_ZLIB) {
460 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
461 return;
463 if(!z)
464 return;
465 output= z->output;
466 while(1) {
467 ret = deflate(&z->zs, Z_FINISH);
468 if (ret != Z_OK &&
469 ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_finish", &z->zs);
471 if(z->zs.next_out != z->writebuffer) {
472 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
473 z->zs.next_out = z->writebuffer;
474 z->zs.avail_out = ZLIB_BUFFER_SIZE;
477 if (ret == Z_STREAM_END) {
478 break;
482 ret = deflateEnd(&z->zs);
483 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_end", &z->zs);
484 free(writer->internal);
485 memset(writer, 0, sizeof(struct writer_t));
486 //output->finish(output);
487 #else
488 fprintf(stderr, "Error: swftools was compiled without zlib support");
489 exit(1);
490 #endif
492 void writer_init_zlibdeflate(struct writer_t*w, struct writer_t*output)
494 #ifdef HAVE_ZLIB
495 struct zlibdeflate_t*z;
496 int ret;
497 memset(w, 0, sizeof(struct writer_t));
498 z = (struct zlibdeflate_t*)malloc(sizeof(struct zlibdeflate_t));
499 memset(z, 0, sizeof(struct zlibdeflate_t));
500 w->internal = z;
501 w->write = writer_zlibdeflate_write;
502 w->finish = writer_zlibdeflate_finish;
503 w->type = WRITER_TYPE_ZLIB;
504 w->pos = 0;
505 z->output = output;
506 memset(&z->zs,0,sizeof(z_stream));
507 z->zs.zalloc = Z_NULL;
508 z->zs.zfree = Z_NULL;
509 z->zs.opaque = Z_NULL;
510 ret = deflateInit(&z->zs, 9);
511 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_init", &z->zs);
512 w->bitpos = 0;
513 w->mybyte = 0;
514 z->zs.next_out = z->writebuffer;
515 z->zs.avail_out = ZLIB_BUFFER_SIZE;
516 #else
517 fprintf(stderr, "Error: swftools was compiled without zlib support");
518 exit(1);
519 #endif
522 /* ----------------------- bit handling routines -------------------------- */
524 void writer_writebit(struct writer_t*w, int bit)
526 if(w->bitpos==8)
528 w->write(w, &w->mybyte, 1);
529 w->bitpos = 0;
530 w->mybyte = 0;
532 if(bit&1)
533 w->mybyte |= 1 << (7 - w->bitpos);
534 w->bitpos ++;
536 void writer_writebits(struct writer_t*w, unsigned int data, int bits)
538 int t;
539 for(t=0;t<bits;t++)
541 writer_writebit(w, (data >> (bits-t-1))&1);
544 void writer_resetbits(struct writer_t*w)
546 if(w->bitpos)
547 w->write(w, &w->mybyte, 1);
548 w->bitpos = 0;
549 w->mybyte = 0;
552 unsigned int reader_readbit(struct reader_t*r)
554 if(r->bitpos==8)
556 r->bitpos=0;
557 r->read(r, &r->mybyte, 1);
559 return (r->mybyte>>(7-r->bitpos++))&1;
561 unsigned int reader_readbits(struct reader_t*r, int num)
563 int t;
564 int val = 0;
565 for(t=0;t<num;t++)
567 val<<=1;
568 val|=reader_readbit(r);
570 return val;
572 void reader_resetbits(struct reader_t*r)
574 r->mybyte = 0;
575 r->bitpos = 8;