summaryrefslogtreecommitdiff
path: root/PLUGINS/src/dvbhddevice/bitbuffer.c
blob: 244b85e19814e50ed57c01bed9dbd8f3a0dfc1d6 (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
/*
 * bitbuffer.c: TODO(short description)
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id: bitbuffer.c 1.1 2009/12/29 14:29:20 kls Exp $
 */

#include "bitbuffer.h"
#include <stdlib.h>

cBitBuffer::cBitBuffer(uint32_t MaxLength)
{
    mData = NULL;
    mMaxLength = 0;
    mBitPos = 0;

    if (MaxLength <= 0x10000)
    {
        mData = new uint8_t[MaxLength];
        if (mData)
        {
            mMaxLength = MaxLength * 8;
        }
    }
}

cBitBuffer::~cBitBuffer(void)
{
    if (mData)
        delete[] mData;
}

uint8_t * cBitBuffer::GetData(void)
{
    return mData;
}

uint32_t cBitBuffer::GetMaxLength(void)
{
    return mMaxLength / 8;
}

uint32_t cBitBuffer::GetBits(int NumBits)
{
    return 0;
}

void cBitBuffer::SetBits(int NumBits, uint32_t Data)
{
    uint32_t nextBitPos;
    uint32_t bytePos;
    uint32_t bitsInByte;
    int shift;

    if (NumBits <= 0 || NumBits > 32)
        return;

    nextBitPos = mBitPos + NumBits;

    if (nextBitPos > mMaxLength)
        return;

    bytePos = mBitPos / 8;
    bitsInByte = mBitPos % 8;

    mData[bytePos] &= (uint8_t) (0xFF << (8 - bitsInByte));
    shift = NumBits - (8 - bitsInByte);
    if (shift > 0)
        mData[bytePos] |= (uint8_t) (Data >> shift);
    else
        mData[bytePos] |= (uint8_t) (Data << (-shift));
    NumBits -= 8 - bitsInByte;
    bytePos++;
    while (NumBits > 0)
    {
        shift = NumBits - 8;
        if (shift > 0)
            mData[bytePos] = (uint8_t) (Data >> shift);
        else
            mData[bytePos] = (uint8_t) (Data << (-shift));
        NumBits -= 8;
        bytePos++;
    }
    mBitPos = nextBitPos;
}

uint32_t cBitBuffer::GetByteLength(void)
{
    return (mBitPos + 7) / 8;
}

void cBitBuffer::SetDataByte(uint32_t Position, uint8_t Data)
{
    if (Position < mMaxLength)
        mData[Position] = Data;
}