~ubuntu-branches/ubuntu/gutsy/amsn/gutsy

« back to all changes in this revision

Viewing changes to utils/webcamsn/src/bitstring.c

  • Committer: Bazaar Package Importer
  • Author(s): Theodore Karkoulis
  • Date: 2006-01-04 15:26:02 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060104152602-ipe1yg00rl3nlklv
Tags: 0.95-1
New Upstream Release (closes: #345052, #278575).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2005  Ole André Vadla Ravnås <oleavr@gmail.com>
 
2
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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
 
16
 */
 
17
 
 
18
#include "mimic-private.h"
 
19
 
 
20
/*
 
21
 * _read_bits
 
22
 *
 
23
 * Internal helper-function used to read num_bits
 
24
 * from stream.
 
25
 */
 
26
guint32 _read_bits(MimCtx *ctx, gint num_bits)
 
27
{
 
28
    guint32 bits;
 
29
    
 
30
    if (ctx->cur_chunk_len >= 16) {
 
31
        guchar *input_buf = (guchar *) ctx->data_buffer + ctx->data_index;
 
32
 
 
33
        if (!ctx->read_odd) {
 
34
            ctx->read_odd = TRUE;
 
35
 
 
36
            ctx->cur_chunk = (input_buf[3] << 24) |
 
37
                             (input_buf[2] << 16) |
 
38
                             (input_buf[1] <<  8) |
 
39
                              input_buf[0];
 
40
 
 
41
        } else {
 
42
            ctx->read_odd = FALSE;
 
43
            
 
44
            ctx->cur_chunk = (input_buf[1] << 24) |
 
45
                             (input_buf[0] << 16) |
 
46
                             (input_buf[7] <<  8) |
 
47
                              input_buf[6];
 
48
 
 
49
            ctx->data_index += 4;
 
50
        }
 
51
 
 
52
        ctx->cur_chunk_len -= 16;
 
53
    }
 
54
 
 
55
    bits = (ctx->cur_chunk << ctx->cur_chunk_len) >> (32 - num_bits);
 
56
    ctx->cur_chunk_len += num_bits;
 
57
 
 
58
    return bits;
 
59
}
 
60
 
 
61
/*
 
62
 * _write_bits
 
63
 *
 
64
 * Internal helper-function used to write "length"
 
65
 * bits of "bits" to stream.
 
66
 */
 
67
void _write_bits(MimCtx *ctx, guint32 bits, gint length)
 
68
{
 
69
    /* Left-align the bit string within its 32-bit container. */
 
70
    bits <<= (32 - length);
 
71
 
 
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;
 
75
 
 
76
    /* Is it full? */
 
77
        if (ctx->cur_chunk_len >= 32) {
 
78
 
 
79
        /* Add the full 32-bit chunk to the stream and update counter. */
 
80
        ctx->chunk_ptr[0] = GUINT32_TO_LE(ctx->cur_chunk);
 
81
        ctx->chunk_ptr++;
 
82
        ctx->cur_chunk_len -= 32;
 
83
 
 
84
        /* Add any trailing bits that didn't fit. */
 
85
        ctx->cur_chunk = bits << (length - ctx->cur_chunk_len);
 
86
    }
 
87
}
 
88