aboutsummaryrefslogtreecommitdiff
path: root/lpg/libqr/rs-encode.c
blob: ec99c6bf9dbd0b0e606ccf17d484a1b13fd5260d (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
#include <assert.h>
#include <stdlib.h>
#include "bitstream.h"
#include "rs.h"

struct bitstream * rs_generate_words(int k, const int * coeff, struct bitstream * data)
{
        struct bitstream * ec = 0;
        unsigned int * b = 0;
        size_t n, i, dlen;

        dlen = bitstream_size(data);
        assert(dlen % 8 == 0);
        dlen /= 8;

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

        if (bitstream_resize(ec, k * 8) != 0)
                goto fail;

        b = calloc(k, sizeof(*b));
        if (!b)
                goto fail;

        /* First, prepare the registers (b) with data bits. Note that
         * the registers are in reverse of the normal order
         */
        bitstream_seek(data, 0);
        for (n = 0; n < dlen; ++n) {
                unsigned int x = b[0] + bitstream_read(data, 8);
                for (i = 0; i < k-1; ++i)
                        b[i] = b[i+1] + coeff[(k-1) - i] * x;
                b[k-1] = coeff[0] * x;
        }

        /* Read off the registers */
        bitstream_pack(ec, b, k, 8);

        free(b);
        return ec;
fail:
        bitstream_destroy(ec);
        return 0;
}