65618ccdea8cb2641bf0c51a72c03387c22ac313
[swftools.git] / lib / bitio.h
1 /* bitio.h
2    Generic object-oriented reading/writing.
3
4    Part of the swftools package.
5    
6    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org> 
7  
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
21
22 #include <stdio.h>
23 #include "types.h"
24
25 #ifndef __rfxswf_bitio_h__
26 #define __rfxswf_bitio_h__
27
28 #define READER_TYPE_FILE 1
29 #define READER_TYPE_MEM  2
30 #define READER_TYPE_ZLIB_U 3
31 #define READER_TYPE_ZLIB_C 4
32 #define READER_TYPE_ZLIB READER_TYPE_ZLIB_U
33 #define READER_TYPE_NULL 5
34 #define READER_TYPE_FILE2 6
35
36 #define WRITER_TYPE_FILE 1
37 #define WRITER_TYPE_MEM  2
38 #define WRITER_TYPE_ZLIB_C 3
39 #define WRITER_TYPE_ZLIB_U 4
40 #define WRITER_TYPE_NULL 5
41 #define WRITER_TYPE_GROWING_MEM  6
42 #define WRITER_TYPE_ZLIB WRITER_TYPE_ZLIB_C
43
44 typedef struct _reader
45 {
46     int (*read)(struct _reader*, void*data, int len);
47     void (*dealloc)(struct _reader*);
48
49     void *internal;
50     int type;
51     unsigned char mybyte;
52     unsigned char bitpos;
53     int pos;
54 } reader_t;
55
56 typedef struct _writer
57 {
58     int (*write)(struct _writer*, void*data, int len);
59     void (*flush)(struct _writer*);
60     void (*finish)(struct _writer*);
61
62     void *internal;
63     int type;
64     unsigned char mybyte;
65     unsigned char bitpos;
66     int pos;
67 } writer_t;
68
69 void reader_resetbits(reader_t*r);
70 unsigned int reader_readbit(reader_t*r);
71 unsigned int reader_readbits(reader_t*r, int num);
72
73 U8 reader_readU8(reader_t*r);
74 U16 reader_readU16(reader_t*r);
75 U32 reader_readU32(reader_t*r);
76 float reader_readFloat(reader_t*r);
77 double reader_readDouble(reader_t*r);
78 char*reader_readString(reader_t*r);
79
80 void writer_resetbits(writer_t*w);
81 void writer_writebit(writer_t*w, int bit);
82 void writer_writebits(writer_t*w, unsigned int data, int bits);
83
84 void writer_writeU8(writer_t*w, unsigned char b);
85 void writer_writeU16(writer_t*w, unsigned short v);
86 void writer_writeU32(writer_t*w, unsigned long v);
87 void writer_writeFloat(writer_t*w, float f);
88 void writer_writeDouble(writer_t*w, double f);
89 void writer_writeString(writer_t*w, const char*s);
90
91 /* standard readers / writers */
92
93 void reader_init_filereader(reader_t*r, int handle);
94 void reader_init_filereader2(reader_t*r, const char*filename);
95 void reader_init_zlibinflate(reader_t*r, reader_t*input);
96 void reader_init_memreader(reader_t*r, void*data, int length);
97 void reader_init_nullreader(reader_t*r);
98
99 void writer_init_filewriter(writer_t*w, int handle);
100 void writer_init_filewriter2(writer_t*w, char*filename);
101 void writer_init_zlibdeflate(writer_t*w, writer_t*output);
102 void writer_init_memwriter(writer_t*r, void*data, int length);
103 void writer_init_nullwriter(writer_t*w);
104
105 void writer_init_growingmemwriter(writer_t*r, U32 grow);
106 void* writer_growmemwrite_memptr(writer_t*w, int*len);
107 void* writer_growmemwrite_getmem(writer_t*w);
108 void writer_growmemwrite_reset(writer_t*w);
109
110 #endif //__rfxswf_bitio_h__