1
/* Copyright (C) 2005 Ole André Vadla Ravnås <oleavr@gmail.com>
3
* This library is free software; you can redistribute it and/or
4
* modify it under the terms of the GNU Lesser General Public
5
* License as published by the Free Software Foundation; either
6
* version 2.1 of the License, or (at your option) any later version.
8
* This library is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
* Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public
14
* License along with this library; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
#include "mimic-private.h"
23
* Internal helper-function used to read num_bits
26
guint32 _read_bits(MimCtx *ctx, gint num_bits)
30
if (ctx->cur_chunk_len >= 16) {
31
guchar *input_buf = (guchar *) ctx->data_buffer + ctx->data_index;
36
ctx->cur_chunk = (input_buf[3] << 24) |
37
(input_buf[2] << 16) |
42
ctx->read_odd = FALSE;
44
ctx->cur_chunk = (input_buf[1] << 24) |
45
(input_buf[0] << 16) |
52
ctx->cur_chunk_len -= 16;
55
bits = (ctx->cur_chunk << ctx->cur_chunk_len) >> (32 - num_bits);
56
ctx->cur_chunk_len += num_bits;
64
* Internal helper-function used to write "length"
65
* bits of "bits" to stream.
67
void _write_bits(MimCtx *ctx, guint32 bits, gint length)
69
/* Left-align the bit string within its 32-bit container. */
70
bits <<= (32 - length);
72
/* Append the bit string (one or more of the trailing bits might not fit, but that's ok). */
73
ctx->cur_chunk |= bits >> ctx->cur_chunk_len;
74
ctx->cur_chunk_len += length;
77
if (ctx->cur_chunk_len >= 32) {
79
/* Add the full 32-bit chunk to the stream and update counter. */
80
ctx->chunk_ptr[0] = GUINT32_TO_LE(ctx->cur_chunk);
82
ctx->cur_chunk_len -= 32;
84
/* Add any trailing bits that didn't fit. */
85
ctx->cur_chunk = bits << (length - ctx->cur_chunk_len);