summaryrefslogtreecommitdiff
path: root/cesar/lib/bitstream.h
diff options
context:
space:
mode:
Diffstat (limited to 'cesar/lib/bitstream.h')
-rw-r--r--cesar/lib/bitstream.h202
1 files changed, 202 insertions, 0 deletions
diff --git a/cesar/lib/bitstream.h b/cesar/lib/bitstream.h
new file mode 100644
index 0000000000..63c015b8db
--- /dev/null
+++ b/cesar/lib/bitstream.h
@@ -0,0 +1,202 @@
+#ifndef bitstream_h
+#define bitstream_h
+/* Cesar project {{{
+ *
+ * Copyright (C) 2007 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file bitstream.h
+ * \brief Bit stream access using word access.
+ * \ingroup lib
+ *
+ * This library provides sequential bit stream access functions using word
+ * access, providing bus optimised endian and boundary safe access.
+ */
+
+#define bitsizeof(x) (8*sizeof(x))
+
+typedef enum
+{
+ BITSTREAM_READ,
+ BITSTREAM_WRITE
+} bitstream_type_t;
+
+/** Bit stream sequential read context. */
+typedef struct bitstream
+{
+ /** Beginning address of data structure. */
+ u8 *start;
+ /** Bit stream pointer, read as words. */
+ u32 *stream;
+ /** Number of valid bytes in the stream. */
+ uint nb_bytes;
+ /** Bit offset of current read or write word. */
+ uint bit_offset;
+ /** Input buffer, valid bits are in LSB. */
+ u32 buf;
+ /** Bitstream type of access (Read or Write). */
+ bitstream_type_t type;
+} bitstream_t;
+
+/**
+ * Initialise a bit stream.
+ * \param ctx bit stream context
+ * \param data stream data pointer
+ * \param nb_bytes number of valid bytes in the stream
+ *
+ * Memory pointer doesn't have to be word aligned.
+ */
+void
+bitstream_init (bitstream_t *ctx, void *data, uint nb_bytes,
+ bitstream_type_t type);
+
+/**
+ * Write remaining bits.
+ * \param ctx bit stream context
+ * \return number of written bits if access is write, else 0
+ */
+uint
+bitstream_finalise (bitstream_t *ctx);
+
+uint bitstream_access_unknown_size (void);
+
+/**
+ * Read or write bits from the bit stream.
+ * \param ctx bit stream context
+ * \param value
+ * \param nb_bit number of bits to read (1-64)
+ * \return number of bits read/written
+ *
+ * Access type (read or write) depends on context initialization.
+ */
+#define bitstream_access(ctx, value, nb_bit) \
+ (sizeof(*(value)) == 1 ? bitstream_access_8 ((ctx), (value), (nb_bit)) \
+ : (sizeof(*(value)) == 2 ? bitstream_access_16 ((ctx), (value), (nb_bit)) \
+ : (sizeof(*(value)) == 4 ? bitstream_access_32 ((ctx), (value), (nb_bit)) \
+ : (sizeof(*(value)) == 8 ? bitstream_access_64 ((ctx), (value), (nb_bit)) \
+ : bitstream_access_unknown_size ()))))
+
+/**
+ * Return the number of remaining bits before the read or write limit.
+ * \param ctx bit stream context
+ * \return number of remaining bits
+ *
+ * Using this function, the user can check whether the next read or
+ * write accesses will succeed.
+ */
+uint
+bitstream_available_bits (bitstream_t *ctx);
+
+/**
+ * Read or write the next "nb_bit" from/to the memory pointer
+ * stored into the ctx.
+ * \param ctx bit stream context
+ * \param value pointer to the value
+ * \param nb_bit number of bits to read
+ *
+ * Access type (read or write) depends on context initialization.
+ */
+uint
+bitstream_access_8 (bitstream_t *ctx, void *value, uint nb_bit);
+
+/**
+ * Read or write the next "nb_bit" from/to the memory pointer
+ * stored into the ctx.
+ * \param ctx bit stream context
+ * \param value pointer to the value
+ * \param nb_bit number of bits to read
+ *
+ * Access type (read or write) depends on context initialization.
+ */
+uint
+bitstream_access_16 (bitstream_t *ctx, void *value, uint nb_bit);
+
+/**
+ * Read or write the next "nb_bit" from/to the memory pointer
+ * stored into the ctx.
+ * \param ctx bit stream context
+ * \param value pointer to the value
+ * \param nb_bit number of bits to read
+ *
+ * Access type (read or write) depends on context initialization.
+ */
+uint
+bitstream_access_32 (bitstream_t *ctx, void *value, uint nb_bit);
+
+/**
+ * Read or write the next "nb_bit" from/to the memory pointer
+ * stored into the ctx.
+ * \param ctx bit stream context
+ * \param value pointer to the value
+ * \param nb_bit number of bits to read
+ *
+ * Access type (read or write) depends on context initialization.
+ */
+uint
+bitstream_access_64 (bitstream_t *ctx, void *value, uint nb_bit);
+
+/**
+ * Read "nb_bit" bits from data pointer + bit offset
+ * \param data data base pointer
+ * \param bit_offset bit offset
+ * \param nb_bit number of bits to read
+ * \return bit read data
+ *
+ * Bit number must be <= 32 or use bitstream_direct_read_large
+ */
+uint
+bitstream_direct_read (void *data, uint bit_offset, uint nb_bit);
+
+/**
+ * Read "nb_bit" bits from data pointer + bit offset
+ * \param data data base pointer
+ * \param bit_offset bit offset
+ * \param nb_bit number of bits to read
+ * \return bit read data
+ */
+u64
+bitstream_direct_read_large (u8 *data, uint bit_offset, uint nb_bit);
+
+/**
+ * Write "nb_bit" bits to data pointer + bit offset
+ * \param data data base pointer
+ * \param bit_offset bit offset
+ * \param value bit value to write
+ * \param nb_bit number of bits to write
+ *
+ * Bit number must be <= 32 or use bitstream_direct_write_large
+ */
+void
+bitstream_direct_write (void *data, uint bit_offset, uint value, uint nb_bit);
+
+/**
+ * Write "nb_bit" bits to data pointer + bit offset
+ * \param data data base pointer
+ * \param bit_offset bit offset
+ * \param value bit value to write
+ * \param nb_bit number of bits to write
+*/
+void
+bitstream_direct_write_large (u8 *data, uint bit_offset, u64 value,
+ uint nb_bit);
+
+/**
+ *
+*/
+void*
+bitstream_memcpy (void *dest, void *src, size_t len);
+
+/**
+ * Compare two buffers and return true if the buffers are equals
+ * \param s1 the first buffer to compare.
+ * \param s2 the second buffer to compare.
+ * \param len the length in bytes to compare the buffers.
+ * \return true if equal, false otherwise.
+ */
+bool
+bitstream_memcmp (void *s1, void *s2, size_t len);
+
+#endif /* bitstream_h */