This commit is contained in:
parent
8788076d39
commit
1c5c1b4011
59 changed files with 24871 additions and 56 deletions
138
Utils/Libs/GLib/BITS.CPP
Normal file
138
Utils/Libs/GLib/BITS.CPP
Normal file
|
@ -0,0 +1,138 @@
|
|||
//===========================================================================
|
||||
//
|
||||
// BITS.CPP
|
||||
//
|
||||
// Author: Graeme Webb
|
||||
// Created: 3rd August 1996
|
||||
// Project: General C++ Library
|
||||
// Purpose: Bit stream object
|
||||
//
|
||||
// Copyright (c) 1996 Graeme Webb
|
||||
//
|
||||
//===========================================================================
|
||||
// Revision history:
|
||||
//===========================================================================
|
||||
|
||||
#include "gtypes.h"
|
||||
#include "bits.hpp"
|
||||
|
||||
//===========================================================================
|
||||
|
||||
BitStream::BitStream(byte *ptr)
|
||||
{
|
||||
baseptr = (byte *) ptr;
|
||||
currptr = (byte *) ptr;
|
||||
bitpos = 0;
|
||||
mask = 0x01;
|
||||
}
|
||||
|
||||
BitStream::~BitStream()
|
||||
{
|
||||
}
|
||||
|
||||
void BitStream::IncCurPtr(void)
|
||||
{
|
||||
currptr++;
|
||||
}
|
||||
|
||||
|
||||
void BitStream::PutBit(uint bit)
|
||||
{
|
||||
if (mask == 0x01)
|
||||
{
|
||||
*currptr = (bit ? mask : 0);
|
||||
bitpos++;
|
||||
mask <<= 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
*currptr |= (bit ? mask : 0);
|
||||
bitpos++;
|
||||
if (mask == 0x80)
|
||||
{
|
||||
IncCurPtr();
|
||||
mask = 0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
mask <<= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BitStream::PutBits(ulong bits, uint len)
|
||||
{
|
||||
uint i;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
PutBit(bits & 1);
|
||||
bits >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
uint BitStream::GetBit()
|
||||
{
|
||||
uint bit;
|
||||
|
||||
bit = (*currptr & mask) ? 1 : 0;
|
||||
bitpos++;
|
||||
|
||||
if (mask == 0x80)
|
||||
{
|
||||
IncCurPtr();
|
||||
mask = 0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
mask <<= 1;
|
||||
}
|
||||
|
||||
return bit;
|
||||
}
|
||||
|
||||
ulong BitStream::GetBits(uint len)
|
||||
{
|
||||
uint i;
|
||||
ulong bits, mbit;
|
||||
|
||||
bits = 0;
|
||||
mbit = 1;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
if (GetBit())
|
||||
bits |= mbit;
|
||||
mbit <<= 1;
|
||||
}
|
||||
|
||||
return bits;
|
||||
}
|
||||
|
||||
static inline ulong RoundUp(ulong value, uint align)
|
||||
{
|
||||
value += align - 1;
|
||||
return (value - value % align);
|
||||
}
|
||||
|
||||
void BitStream::Align(uint bytes)
|
||||
{
|
||||
ulong bytepos, nextpos;
|
||||
|
||||
bytepos = ByteLength();
|
||||
nextpos = RoundUp(bytepos,bytes);
|
||||
|
||||
currptr = baseptr + bytepos;
|
||||
|
||||
while (bytepos < nextpos)
|
||||
{
|
||||
*currptr=0;
|
||||
IncCurPtr();
|
||||
bytepos++;
|
||||
}
|
||||
|
||||
bitpos = bytepos * 8;
|
||||
mask = 0x01;
|
||||
}
|
||||
|
||||
//===========================================================================
|
Loading…
Add table
Add a link
Reference in a new issue