aboutsummaryrefslogtreecommitdiff
path: root/lpg/libqr/bitstream.c
blob: fd58ea30b16784087c5772577f133093f2060b6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
 * It would perhaps be more sensible just to store the bits
 * as an array of char or similar, but this way is more fun.
 * This is a pretty inefficient implementation, althought I
 * suspect that won't be a problem.
 */

#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <assert.h>

#include "bitstream.h"

#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define MIN(a, b) ((a) > (b) ? (b) : (a))

struct bitstream {
        size_t pos;    /* bits */
        size_t count;  /* bits */
        size_t bufsiz; /* bytes */
        unsigned char * buffer;
};

static size_t bits_to_bytes(size_t bits)
{
        return (bits / CHAR_BIT) + (bits % CHAR_BIT != 0);
}

static int ensure_available(struct bitstream * stream, size_t bits)
{
        size_t need_bits = stream->pos + bits;
        size_t need_bytes = need_bits / CHAR_BIT + ((need_bits % CHAR_BIT) ? 0 : 1);
        size_t newsize;

        if (stream->bufsiz >= need_bytes)
                return 0;

        newsize = MAX(stream->bufsiz, 100);
        while (newsize < need_bytes)
                newsize *= 2;

        return bitstream_resize(stream, newsize);
}

struct bitstream * bitstream_create(void)
{
        struct bitstream * obj;

        obj = malloc(sizeof(*obj));

        if (obj) {
                obj->pos    = 0;
                obj->count  = 0;
                obj->bufsiz = 0;
                obj->buffer = 0;
        }

        return obj;
}

int bitstream_resize(struct bitstream * stream, size_t bits)
{
        size_t newsize;
        void * newbuf;

        newsize = bits_to_bytes(bits);
        newbuf = realloc(stream->buffer, newsize);

        if (newbuf) {
                stream->bufsiz = newsize;
                stream->buffer = newbuf;
        }

        return newbuf ? 0 : -1;
}

void bitstream_destroy(struct bitstream * stream)
{
        free(stream->buffer);
        free(stream);
}

struct bitstream * bitstream_copy(const struct bitstream * src)
{
        struct bitstream * ret;

        ret = bitstream_create();
        if (!ret)
                return 0;

        if (bitstream_resize(ret, src->count) != 0) {
                free(ret);
                return 0;
        }

        ret->pos   = src->pos;
        ret->count = src->count;
        memcpy(ret->buffer, src->buffer, src->bufsiz);

        return ret;
}

void bitstream_seek(struct bitstream * stream, size_t pos)
{
        assert(pos <= stream->count);
        stream->pos = pos;
}

size_t bitstream_tell(const struct bitstream * stream)
{
        return stream->pos;
}

size_t bitstream_remaining(const struct bitstream * stream)
{
        return stream->count - stream->pos;
}

size_t bitstream_size(const struct bitstream * stream)
{
        return stream->count;
}

unsigned int bitstream_read(struct bitstream * stream, size_t bits)
{
        unsigned int result = 0;
        unsigned char * byte;
        size_t bitnum;

        assert(bitstream_remaining(stream) >= bits);

        byte = stream->buffer + (stream->pos / CHAR_BIT);
        bitnum = stream->pos % CHAR_BIT;

        stream->pos += bits;

        while (bits-- > 0) {
                int bit = (*byte >> bitnum++) & 0x1;
                result = (result << 1) | bit;
                if (bitnum == CHAR_BIT) {
                        bitnum = 0;
                        ++byte;
                }                
        }

        return result;
}

void bitstream_unpack(struct bitstream * stream,
                      unsigned int *     result,
                      size_t             count,
                      size_t             bitsize)
{
        assert(bitstream_remaining(stream) >= (count * bitsize));

        while (count--)
                *(result++) = bitstream_read(stream, bitsize);
}

int bitstream_write(struct bitstream * stream,
                    unsigned int       value,
                    size_t             bits)
{
        unsigned char * byte;
        size_t bitnum;

        if (ensure_available(stream, bits) != 0)
                return -1;

        byte = stream->buffer + (stream->pos / CHAR_BIT);
        bitnum = stream->pos % CHAR_BIT;

        stream->pos += bits;
        stream->count = stream->pos; /* truncate */

        while (bits-- > 0) {
                int bit = (value >> bits) & 0x1;
                unsigned char mask = 1 << bitnum++;
                *byte = (*byte & ~mask) | (bit ? mask : 0);
                if (bitnum == CHAR_BIT) {
                        bitnum = 0;
                        ++byte;
                }
        }

        return 0;
}

int bitstream_pack(struct bitstream *   stream,
                   const unsigned int * values,
                   size_t               count,
                   size_t               bitsize)
{
        if (ensure_available(stream, count * bitsize) != 0)
                return -1;

        while (count--)
                bitstream_write(stream, *(values++), bitsize);

        return 0;
}

int bitstream_cat(struct bitstream * dest, const struct bitstream * src)
{
        size_t count = bitstream_size(src);
        size_t srcpos;

        if (ensure_available(dest, count) != 0)
                return -1;

        srcpos = bitstream_tell(src);
        bitstream_seek((struct bitstream *)src, 0);

        /* uint must be at least 16 bits */
        for (; count >= 16; count -= 16)
                bitstream_write(
                        dest,
                        bitstream_read((struct bitstream *)src, 16),
                        16);

        if (count > 0)
                bitstream_write(
                        dest,
                        bitstream_read((struct bitstream *)src, count),
                        count);

        bitstream_seek((struct bitstream *)src, srcpos);

        return 0;
}