1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
11 * Bit streams, used to read data at the bit level rather than byte level.
16 #ifndef SQUIRRELJME_BITSTREAM_H
17 #define SQUIRRELJME_BITSTREAM_H
19 #include "sjme/stdTypes.h"
20 #include "sjme/closeable.h"
21 #include "sjme/stream.h"
25 #ifndef SJME_CXX_IS_EXTERNED
26 #define SJME_CXX_IS_EXTERNED
27 #define SJME_CXX_SQUIRRELJME_BITSTREAM_H
30 #endif /* #ifdef SJME_CXX_IS_EXTERNED */
31 #endif /* #ifdef __cplusplus */
33 /*--------------------------------------------------------------------------*/
36 * Order for when reading or writing multiple bits.
40 typedef enum sjme_bitStream_order
42 /** Least significant bit. */
45 /** Most significant bit. */
47 } sjme_bitStream_order
;
50 * Bit stream state structure, generic.
54 typedef struct sjme_bitStream_base
56 /** Closeable data, as bit streams may be closed. */
57 sjme_closeableBase closeable
;
59 /** The closeable to close, if forwarded, optional. */
60 sjme_closeable forwardClose
;
62 /** Pointer to pass to the function as data. */
63 sjme_pointer funcData
;
65 /** The number of bits read/written. */
66 sjme_juint streamCount
;
68 /** The current bit queue. */
71 /** The amount of bits in the buffer. */
77 /** The overflow count. */
79 } sjme_bitStream_base
;
82 * Base bitstream type.
86 typedef sjme_bitStream_base
* sjme_bitStream
;
88 /** Cast to a bit stream base. */
89 #define SJME_AS_BITSTREAM(x) ((sjme_bitStream)(x))
92 * Input bit stream source.
96 typedef struct sjme_bitStream_inputBase sjme_bitStream_inputBase
;
99 * Input bit stream source.
103 typedef struct sjme_bitStream_inputBase
* sjme_bitStream_input
;
106 * Output bit stream destination.
110 typedef struct sjme_bitStream_outputBase sjme_bitStream_outputBase
;
113 * Output bit stream destination.
117 typedef struct sjme_bitStream_outputBase
* sjme_bitStream_output
;
120 * Reads a single byte from the input.
122 * @param inStream The bit stream.
123 * @param functionData The optional data passed to this function.
124 * @param readCount The resultant read count, negative value means EOF.
125 * @param outBuf The buffer of read bytes.
126 * @param length The number of bytes to read.
127 * @return On any resultant error, if any.
130 typedef sjme_errorCode (*sjme_bitStream_inputReadByteFunc
)(
131 sjme_attrInNotNull sjme_bitStream_input inStream
,
132 sjme_attrInNullable sjme_pointer functionData
,
133 sjme_attrOutNotNull sjme_jint
* readCount
,
134 sjme_attrOutNotNullBuf(length
) sjme_pointer outBuf
,
135 sjme_attrInPositiveNonZero sjme_jint length
);
138 * Writes a single byte to the output.
140 * @param outStream The bit stream.
141 * @param functionData The optional data passed to this function.
142 * @param writeBuf The bytes to write.
143 * @param length The number of bytes to write.
144 * @return On any resultant error, if any.
147 typedef sjme_errorCode (*sjme_bitStream_outputWriteByteFunc
)(
148 sjme_attrInNotNull sjme_bitStream_output outStream
,
149 sjme_attrInNullable sjme_pointer functionData
,
150 sjme_attrInNotNullBuf(length
) sjme_buffer writeBuf
,
151 sjme_attrInPositiveNonZero sjme_jint length
);
153 struct sjme_bitStream_inputBase
155 /** Base bit stream data. */
156 sjme_bitStream_base base
;
158 /** The read function. */
159 sjme_bitStream_inputReadByteFunc readFunc
;
162 sjme_jboolean eofHit
;
165 struct sjme_bitStream_outputBase
167 /** Base bit stream data. */
168 sjme_bitStream_base base
;
170 /** The write function. */
171 sjme_bitStream_outputWriteByteFunc writeFunc
;
175 * Returns the number of bits that are ready.
177 * @param ofStream The stream to get the ready count for.
178 * @param readyBits The number of ready bits.
179 * @return Any resultant error, if any.
182 sjme_errorCode
sjme_bitStream_bitsReady(
183 sjme_attrInNotNull sjme_bitStream ofStream
,
184 sjme_attrOutNotNull sjme_jint
* readyBits
);
187 * Skips the given number of bits until the input bit stream is aligned with
188 * the given alignment, that is the modulo of it is zero.
190 * @param inStream The input bit stream.
191 * @param alignBit The bit to align to.
192 * @param outSkipped The number of bits that were skipped, optional.
193 * @return On any resultant error, if any.
196 sjme_errorCode
sjme_bitStream_inputAlign(
197 sjme_attrInNotNull sjme_bitStream_input inStream
,
198 sjme_attrInRange(2, 32) sjme_jint alignBit
,
199 sjme_attrOutNullable sjme_jint
* outSkipped
);
202 * Opens an input bit stream.
204 * @param allocPool The pool to allocate within.
205 * @param resultStream The resultant stream.
206 * @param readFunc The read function to use.
207 * @param readFuncData The optional data to pass to the function.
208 * @param forwardClose The closeable to close, if closed.
209 * @return On any resultant error, if any.
212 sjme_errorCode
sjme_bitStream_inputOpen(
213 sjme_attrInNotNull sjme_alloc_pool allocPool
,
214 sjme_attrOutNotNull sjme_bitStream_input
* resultStream
,
215 sjme_attrInNotNull sjme_bitStream_inputReadByteFunc readFunc
,
216 sjme_attrInNullable sjme_pointer readFuncData
,
217 sjme_attrInNullable sjme_closeable forwardClose
);
220 * Opens a bit stream from a memory location.
222 * @param allocPool The pool to allocate within.
223 * @param resultStream The resultant stream.
224 * @param base The base memory address.
225 * @param length The length of the memory block.
226 * @return Any resultant error, if any.
229 sjme_errorCode
sjme_bitStream_inputOpenMemory(
230 sjme_attrInNotNull sjme_alloc_pool allocPool
,
231 sjme_attrOutNotNull sjme_bitStream_input
* resultStream
,
232 sjme_attrInNotNull sjme_cpointer base
,
233 sjme_attrInPositive sjme_jint length
);
236 * Opens a bit stream which reads from the given @c sjme_stream_input .
238 * @param allocPool The pool to allocate within.
239 * @param resultStream The resultant stream.
240 * @param inputStream The stream to read byte data from.
241 * @param forwardClose Should close be forwarded to the given stream?
242 * @return On any resultant error, if any.
245 sjme_errorCode
sjme_bitStream_inputOpenStream(
246 sjme_attrInNotNull sjme_alloc_pool allocPool
,
247 sjme_attrOutNotNull sjme_bitStream_input
* resultStream
,
248 sjme_attrInNotNull sjme_stream_input inputStream
,
249 sjme_attrInValue sjme_jboolean forwardClose
);
252 * Reads bits from the input source.
254 * @param inStream The stream to read bits from.
255 * @param bitOrder The order of the bit read.
256 * @param outValue The resultant value.
257 * @param bitCount The number of bits to read.
258 * @return Any resultant error, if any.
261 sjme_errorCode
sjme_bitStream_inputRead(
262 sjme_attrInNotNull sjme_bitStream_input inStream
,
263 sjme_attrInValue sjme_bitStream_order bitOrder
,
264 sjme_attrOutNotNull sjme_juint
* outValue
,
265 sjme_attrInPositiveNonZero sjme_jint bitCount
);
268 * Opens an output bit stream.
270 * @param allocPool The pool to allocate within.
271 * @param resultStream The resultant stream.
272 * @param writeFunc The write function to use.
273 * @param writeFuncData The optional data to pass to the function.
274 * @param forwardClose The closeable to close, if closed.
275 * @return On any resultant error, if any.
278 sjme_errorCode
sjme_bitStream_outputOpen(
279 sjme_attrInNotNull sjme_alloc_pool allocPool
,
280 sjme_attrOutNotNull sjme_bitStream_output
* resultStream
,
281 sjme_attrInNotNull sjme_bitStream_outputWriteByteFunc writeFunc
,
282 sjme_attrInNullable sjme_pointer writeFuncData
,
283 sjme_attrInNullable sjme_closeable forwardClose
);
286 * Opens an output bit stream which writes to the given output stream.
288 * @param allocPool The pool to allocate within.
289 * @param resultStream The resultant output bit stream.
290 * @param outputStream The stream to write.
291 * @param forwardClose If this is closed, should @c outputStream be closed?
292 * @return Any resultant error, if any.
295 sjme_errorCode
sjme_bitStream_outputOpenStream(
296 sjme_attrInNotNull sjme_alloc_pool allocPool
,
297 sjme_attrOutNotNull sjme_bitStream_output
* resultStream
,
298 sjme_attrInNotNull sjme_stream_output outputStream
,
299 sjme_attrInValue sjme_jboolean forwardClose
);
302 * Writes bits to the output destination.
304 * @param outStream The stream to write bits to.
305 * @param bitOrder The order of the bit write.
306 * @param inValue The value to write.
307 * @param bitCount The number of bits to write.
308 * @return Any resultant error, if any.
311 sjme_errorCode
sjme_bitStream_outputWrite(
312 sjme_attrInNotNull sjme_bitStream_output outStream
,
313 sjme_attrInValue sjme_bitStream_order bitOrder
,
314 sjme_attrInValue sjme_juint outValue
,
315 sjme_attrInPositiveNonZero sjme_jint bitCount
);
317 /*--------------------------------------------------------------------------*/
321 #ifdef SJME_CXX_SQUIRRELJME_BITSTREAM_H
323 #undef SJME_CXX_SQUIRRELJME_BITSTREAM_H
324 #undef SJME_CXX_IS_EXTERNED
325 #endif /* #ifdef SJME_CXX_SQUIRRELJME_BITSTREAM_H */
326 #endif /* #ifdef __cplusplus */
328 #endif /* SQUIRRELJME_BITSTREAM_H */