Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
GrayLib::cBitArray Class Reference

#include <cBitArray.h>

Inheritance diagram for GrayLib::cBitArray:
GrayLib::cBitArrayStatic Gray::cBits GrayLib::cBigUnsigned GrayLib::cBigInteger

Public Member Functions

bool isHighBlockUseExact () const
 
bool isHighBlockUseValid () const
 
BLOCK_ENUM_t get_BlocksUse () const
 
void UpdateBlocksUse ()
 
void UpdateBlocksUseMax (BLOCK_ENUM_t nBlocksUseMax)
 
void UpdateBlocksUseCap ()
 
BLOCK_tRefBlock (BLOCK_ENUM_t nBlock)
 
 cBitArray ()
 
 cBitArray (BIT_ENUM_t nBits)
 
 cBitArray (BIT_ENUM_t nBits, BLOCK_t uValueMask)
 
 cBitArray (BLOCK_ENUM_t nBlocks, const BLOCK_t *pBlocks)
 
 cBitArray (BLOCK_ENUM_t nBlocks, const BLOCK_t *pBlocks, bool bStatic)
 
 ~cBitArray ()
 
void SetBlocksStatic (BLOCK_ENUM_t nBlocks, const BLOCK_t *pBlocks)
 
void SetBlocksStatic (const cBitArrayStatic &b)
 
HRESULT SetBlockSizeGrow (BLOCK_ENUM_t nBlocksNew)
 
HRESULT SetBlockSizeShrink (BLOCK_ENUM_t nBlocksNew)
 
void SetBlockSizeUse (BLOCK_ENUM_t nBlocksNew)
 
bool IsEqual (const THIS_t &x) const
 
bool IsEqualU (BLOCK_t x) const
 
bool operator== (const THIS_t &x) const
 
bool operator!= (const THIS_t &x) const
 
bool isEmptyBits () const
 
bool IsSet (BIT_ENUM_t nBit) const
 
BIT_ENUM_t get_Lowest1Bit () const
 
BIT_ENUM_t get_Highest1Bit () const
 
BIT_ENUM_t get_Count1Bits () const
 
void SetBit (BIT_ENUM_t nBit)
 
void ClrBit (BIT_ENUM_t nBit)
 
void ModBit (BIT_ENUM_t nBit, bool bVal)
 
void SetTruncateBits (BIT_ENUM_t nBits)
 
HRESULT SetRandomBits (BIT_ENUM_t nBits, IRandomNoise *pRandom=nullptr)
 
HRESULT SetRandomBitsLarge (BIT_ENUM_t nBits, IRandomNoise *pRandom=nullptr)
 
void SetCopyBits (const THIS_t &ref)
 
HRESULT SetCopySecure (const THIS_t &val, bool assign)
 
void SetZeroAll ()
 
void SetNullVal ()
 
size_t get_BinarySize () const
 
HRESULT GetBinaryBE (BYTE *pOut, size_t nSizeOut) const
 
HRESULT GetBinaryBE (cHeapBlock &b) const
 
HRESULT SetBinaryBE (const BYTE *pData, size_t nSize)
 
HRESULT SetBinaryLE (const BYTE *pData, size_t nSize)
 
 UNITTEST_FRIEND (cBitArray)
 
- Public Member Functions inherited from GrayLib::cBitArrayStatic
 cBitArrayStatic () noexcept
 
 cBitArrayStatic (BIT_ENUM_t nBits)
 
 cBitArrayStatic (BIT_ENUM_t nBits, BLOCK_t uValueMask)
 
 cBitArrayStatic (BLOCK_ENUM_t nBlocks, const BLOCK_t *pData)
 
 cBitArrayStatic (BLOCK_ENUM_t nBlocks, const BLOCK_t *pBlocksInit, bool bStatic)
 
 cBitArrayStatic (const THIS_t &ref)
 
 cBitArrayStatic (THIS_t &&ref) noexcept
 
 ~cBitArrayStatic ()
 
bool isNullVal () const noexcept
 
bool isZeroVal () const noexcept
 
BLOCK_ENUM_t get_BlocksCap () const noexcept
 
size_t get_BytesCap () const noexcept
 
BLOCK_t GetBlockInt (BLOCK_ENUM_t i) const
 
const BLOCK_tget_BlockPtrC () const noexcept
 
BLOCK_tget_BlockPtr ()
 
BLOCK_tRefBlockInt (BLOCK_ENUM_t i)
 
UINT32 GetU32Int (BLOCK_ENUM_t j) const
 
void SetU32Int (BLOCK_ENUM_t j, UINT32 val32)
 
const THIS_toperator= (const THIS_t &ref)
 
THIS_toperator= (THIS_t &&ref)
 
bool isStatic () const noexcept
 
void SetBlocksStatic (BLOCK_ENUM_t nBlocks, const BLOCK_t *pBlocks)
 
bool isReadOnly () const noexcept
 
BIT_ENUM_t get_BitsCap () const noexcept
 
bool IsValidBit (BIT_ENUM_t nBit) const noexcept
 
bool IsSet (BIT_ENUM_t nBit) const
 
bool isOdd () const
 
void ClrBit (BIT_ENUM_t nBit)
 
void SetBit (BIT_ENUM_t nBit)
 
bool SetBitRet (BIT_ENUM_t nBit)
 
void ModBit (BIT_ENUM_t nBit, bool bVal)
 
void SetNullVal () noexcept
 
void SetZeroAll ()
 
void SetAll1 ()
 
void SetBitsSizeZ (BIT_ENUM_t nBits)
 
void SetInvertBytes (size_t nSize)
 
void SetCopyBits (const THIS_t &ref)
 

Static Public Member Functions

static HRESULT GRAYCALL ReturnError (HRESULT hRes, const char *pszMsg)
 
static HRESULT GRAYCALL SwapSecure (THIS_t &X, THIS_t &Y, bool swap)
 
- Static Public Member Functions inherited from GrayLib::cBitArrayStatic
static BLOCK_ENUM_t GetBlocksFromBits (BIT_ENUM_t nBits) noexcept
 
static BLOCK_ENUM_t GetBlocksFromBytes (size_t nBytes) noexcept
 
static bool IsReasonableBlockQty (BLOCK_ENUM_t nBlocks) noexcept
 
- Static Public Member Functions inherited from Gray::cBits
static constexpr size_t GetSizeBytes (BIT_ENUM_t nBits) noexcept
 
template<typename TYPE >
static constexpr TYPE Mask1 (BIT_ENUM_t nBit) noexcept
 
template<typename TYPE >
static constexpr bool IsMask1 (TYPE nVal) noexcept
 
template<typename TYPE >
static constexpr bool IsSet (TYPE nVal, BIT_ENUM_t nBit) noexcept
 
template<typename TYPE >
static constexpr bool IsClear (TYPE nVal, BIT_ENUM_t nBit) noexcept
 
template<typename TYPE >
static constexpr TYPE SetBit (TYPE nVal, BIT_ENUM_t nBit) noexcept
 
template<typename TYPE >
static constexpr TYPE ClearBit (TYPE nVal, BIT_ENUM_t nBit) noexcept
 
template<typename TYPE >
static constexpr bool HasMask (TYPE nVal, TYPE nMask) noexcept
 
template<typename TYPE >
static BIT_ENUM_t Highest1Bit (TYPE nMask) noexcept
 
template<typename TYPE >
static BIT_ENUM_t Count1Bits (TYPE nMask) noexcept
 
template<typename TYPE >
static TYPE Rotl (TYPE nVal, BIT_ENUM_t nBits) noexcept
 
template<typename TYPE >
static TYPE Rotr (TYPE nVal, BIT_ENUM_t nBits) noexcept
 
template<typename TYPE >
static TYPE Reverse (TYPE nVal) noexcept
 
template<>
BIT_ENUM_t Count1Bits (UINT32 nVal) noexcept
 
template<>
BYTE Reverse (BYTE nVal) noexcept
 
template<>
UINT32 Reverse (UINT32 nVal) noexcept
 
template<>
ULONG Reverse (ULONG nVal) noexcept
 

Protected Member Functions

void put_BlocksUse (BLOCK_ENUM_t nBlocksUse)
 
BLOCK_t GetBlock (BLOCK_ENUM_t nBlock) const
 
void AddBlockGrow (BLOCK_t nVal)
 
void SetBlock (BLOCK_ENUM_t i, BLOCK_t nVal)
 
- Protected Member Functions inherited from GrayLib::cBitArrayStatic
bool IsValidBlockIndex (BLOCK_ENUM_t i) const noexcept
 
void SetBlockSizeReAlloc (BLOCK_ENUM_t nBlocksNew)
 
void SetBlockSizeUninit (BLOCK_ENUM_t nBlocksNew)
 
void SetBlockSizeGrow (BLOCK_ENUM_t nBlocksNew)
 
BLOCK_ENUM_t FindUsedBlocksQty (BLOCK_ENUM_t nBlocks) const
 
BLOCK_ENUM_t FindUsedBlocksQty () const
 

Protected Attributes

BLOCK_ENUM_t m_nBlocksUse
 Qty of blocks that is non zero. ASSUME anything above this is 0 blocks. More...
 

Additional Inherited Members

- Public Types inherited from GrayLib::cBitArrayStatic
typedef UINT32 BLOCK_t
 The biggest unsigned type I can do atomic math on for this architecture. More...
 
typedef INT32 BLOCKI_t
 The biggest signed type I can do atomic math on. More...
 
typedef WORD BLOCKH_t
 half sized BLOCK_t for multiplication overflows. More...
 
typedef unsigned int BLOCK_ENUM_t
 Type for the index of a BLOCK_t in the array. NOT bits BIT_ENUM_t. More...
 
- Static Public Attributes inherited from GrayLib::cBitArrayStatic
static const BLOCK_ENUM_t k_BLOCK_MAX = 8096
 Arbitrary Max reasonable size. More...
 
static const size_t k_SIZE_MAX = k_BLOCK_MAX * sizeof(BLOCK_t)
 
static const BIT_ENUM_t k_BIT_MAX = k_SIZE_MAX * 8
 
static const cBitArrayStatic k_Zero
 
static const BLOCK_t k_OneBlock = 1
 
static const BIT_ENUM_t k_BLOCK_BITS = (BIT_ENUM_t)(sizeof(BLOCK_t) * 8)
 The number of bits in a BLOCK_t, BIT_ENUM_t. More...
 
static const BIT_ENUM_t k_BLOCKH_BITS = (BIT_ENUM_t)(sizeof(BLOCKH_t) * 8)
 The number of bits in a BLOCKH_t, BIT_ENUM_t. More...
 
- Static Public Attributes inherited from Gray::cBits
static const BYTE k_8 = 8
 represent the 8 bits in a byte. BIT_ENUM_t More...
 

Detailed Description

An arbitrary dynamic sized array of boolean values. similar to a bit mask of unlimited size. Actual storage is BLOCK_t aligned.

Note
Compile BLOCK_t to be whatever size is biggest allowed for the platform.
USE_LITTLE_ENDIAN should make no difference for this class.

Constructor & Destructor Documentation

◆ cBitArray() [1/5]

GrayLib::cBitArray::cBitArray ( )
inline

construct empty. nullptr.

◆ cBitArray() [2/5]

GrayLib::cBitArray::cBitArray ( BIT_ENUM_t  nBits)
inlineexplicit

◆ cBitArray() [3/5]

GrayLib::cBitArray::cBitArray ( BIT_ENUM_t  nBits,
BLOCK_t  uValueMask 
)
inline

◆ cBitArray() [4/5]

GrayLib::cBitArray::cBitArray ( BLOCK_ENUM_t  nBlocks,
const BLOCK_t pBlocks 
)
inline

Init NON static

◆ cBitArray() [5/5]

GrayLib::cBitArray::cBitArray ( BLOCK_ENUM_t  nBlocks,
const BLOCK_t pBlocks,
bool  bStatic 
)
inline

Init static value

◆ ~cBitArray()

GrayLib::cBitArray::~cBitArray ( )
inline

Member Function Documentation

◆ AddBlockGrow()

void GrayLib::cBitArray::AddBlockGrow ( BLOCK_t  nVal)
inlineprotected

Create new space at the end of m_nBlocksUse for this block. Overflow.

◆ ClrBit()

void GrayLib::cBitArray::ClrBit ( BIT_ENUM_t  nBit)
inline

clear bit. Never grow.

◆ get_BinarySize()

size_t GrayLib::cBitArray::get_BinarySize ( ) const
inline

Get a block with high to low order bytes = Big Endian or network order.

Returns
the total size in bytes. of non 0 bytes.

◆ get_BlocksUse()

BLOCK_ENUM_t GrayLib::cBitArray::get_BlocksUse ( ) const
inline

Get number of blocks currently declared in use.

◆ get_Count1Bits()

BIT_ENUM_t GrayLib::cBitArray::get_Count1Bits ( ) const
inline

Count all the set bits.

◆ get_Highest1Bit()

BIT_ENUM_t GrayLib::cBitArray::get_Highest1Bit ( ) const

Get the magnitude of significant bits. Quantity of significant bits.

Returns
the bit position of the highest set bit. 1 based. e.g. zero if the number is zero, 1 bit if value of 1, 4 if the number is 9

◆ get_Lowest1Bit()

BIT_ENUM_t GrayLib::cBitArray::get_Lowest1Bit ( ) const
Returns
the number of least significant 0 bits

◆ GetBinaryBE() [1/2]

HRESULT GrayLib::cBitArray::GetBinaryBE ( BYTE *  pOut,
size_t  nSizeOut 
) const

Get a block with high to low order bytes = Big Endian or network order. for RSA Certificates (x509,ASN)? Export X into unsigned binary data, big endian

◆ GetBinaryBE() [2/2]

HRESULT GrayLib::cBitArray::GetBinaryBE ( cHeapBlock b) const

Get a block with high to low order bytes = Big Endian or network order. for RSA Certificates (x509,ASN)? Export X into unsigned binary data, big endian

◆ GetBlock()

BLOCK_t GrayLib::cBitArray::GetBlock ( BLOCK_ENUM_t  nBlock) const
inlineprotected

Out of range blocks are considered to be 0.

◆ isEmptyBits()

bool GrayLib::cBitArray::isEmptyBits ( ) const
inline

ASSUME: used length is zero = the number is zero. and visa versa. includes isNullVal

◆ IsEqual()

bool GrayLib::cBitArray::IsEqual ( const THIS_t x) const
inline

◆ IsEqualU()

bool GrayLib::cBitArray::IsEqualU ( BLOCK_t  x) const
inline

Equal to a single block?

◆ isHighBlockUseExact()

bool GrayLib::cBitArray::isHighBlockUseExact ( ) const
inline

Is m_nBlocksUse set exactly correctly? should ALWAYS be true. _DEBUG usage only.

◆ isHighBlockUseValid()

bool GrayLib::cBitArray::isHighBlockUseValid ( ) const
inline

Is m_nBlocksUse generally valid? Math will be correct but not most efficient. should ALWAYS be true. _DEBUG usage only.

◆ IsSet()

bool GrayLib::cBitArray::IsSet ( BIT_ENUM_t  nBit) const
inline

Gets the state of the enumerated bit. which has value 2^nBit. Bits beyond m_nBlocksUse are considered to be 0.

◆ ModBit()

void GrayLib::cBitArray::ModBit ( BIT_ENUM_t  nBit,
bool  bVal 
)
inline

Sets the state of bit to bVal. 0 or 1. The m_nBlocksUse may shrink/grows as necessary.

◆ operator!=()

bool GrayLib::cBitArray::operator!= ( const THIS_t x) const
inline

◆ operator==()

bool GrayLib::cBitArray::operator== ( const THIS_t x) const
inline

◆ put_BlocksUse()

void GrayLib::cBitArray::put_BlocksUse ( BLOCK_ENUM_t  nBlocksUse)
inlineprotected

Set this to the value we know it should be.

◆ RefBlock()

BLOCK_t& GrayLib::cBitArray::RefBlock ( BLOCK_ENUM_t  nBlock)
inline

I am about to use/set this block. grow if necessary.

Note
ASSUME UpdateBlocksUse() will be called after this.

◆ ReturnError()

HRESULT GRAYCALL GrayLib::cBitArray::ReturnError ( HRESULT  hRes,
const char *  pszMsg 
)
static

Put break point here to trap errors. Something went wrong. Optionally throw an exception.

◆ SetBinaryBE()

HRESULT GrayLib::cBitArray::SetBinaryBE ( const BYTE *  pData,
size_t  nSize 
)

Set a value from a big-endian byte stream. high to low order. for RSA Certificates ? Import from unsigned binary data, big endian nSize = 0 = allowed.

◆ SetBinaryLE()

HRESULT GrayLib::cBitArray::SetBinaryLE ( const BYTE *  pData,
size_t  nSize 
)

◆ SetBit()

void GrayLib::cBitArray::SetBit ( BIT_ENUM_t  nBit)
inline

set bit, grow if necessary

◆ SetBlock()

void GrayLib::cBitArray::SetBlock ( BLOCK_ENUM_t  i,
BLOCK_t  nVal 
)
inlineprotected

◆ SetBlockSizeGrow()

HRESULT GrayLib::cBitArray::SetBlockSizeGrow ( BLOCK_ENUM_t  nBlocksNew)

make room for a nBlocksNew if it doesn't exist. Grow. I am about to set this block.

Note
Must call UpdateBlocksUse() after this.

◆ SetBlockSizeShrink()

HRESULT GrayLib::cBitArray::SetBlockSizeShrink ( BLOCK_ENUM_t  nBlocksNew)

Resize down, keeping at least the specified number of blocks Resize down as much as possible,

  • nBlocks The minimum number of blocks to keep
    Returns
    0 if successful, E_OUTOFMEMORY if memory allocation failed

◆ SetBlockSizeUse()

void GrayLib::cBitArray::SetBlockSizeUse ( BLOCK_ENUM_t  nBlocksNew)

◆ SetBlocksStatic() [1/2]

void GrayLib::cBitArray::SetBlocksStatic ( BLOCK_ENUM_t  nBlocks,
const BLOCK_t pBlocks 
)

◆ SetBlocksStatic() [2/2]

void GrayLib::cBitArray::SetBlocksStatic ( const cBitArrayStatic b)

◆ SetCopyBits()

void GrayLib::cBitArray::SetCopyBits ( const THIS_t ref)

◆ SetCopySecure()

HRESULT GrayLib::cBitArray::SetCopySecure ( const THIS_t val,
bool  assign 
)

Secure conditional assignment X = Y if assign is 1 Conditionally assign X = Y, without leaking information about whether the assignment was made or not. (Leaking information about the respective sizes of X and Y is OK however.) make sure assign is 0 or 1 in a time-constant manner

  • Y = Value to be assigned
  • bAssign = 1: perform the assignment, 0: keep X's original value
Returns
0 if successful, E_OUTOFMEMORY if memory allocation failed,
Note
This function is equivalent to if( assign ) SetCopyInt( X, Y ); except that it avoids leaking any information about whether the assignment was done or not (the above code may leak information through branch prediction and/or memory access patterns analysis).

◆ SetNullVal()

void GrayLib::cBitArray::SetNullVal ( )
inline

◆ SetRandomBits()

HRESULT GrayLib::cBitArray::SetRandomBits ( BIT_ENUM_t  nBits,
IRandomNoise pRandom = nullptr 
)

Fill the cBitArray with random bits. Use pRandom as random number source (for testing)

◆ SetRandomBitsLarge()

HRESULT GrayLib::cBitArray::SetRandomBitsLarge ( BIT_ENUM_t  nBits,
IRandomNoise pRandom = nullptr 
)
inline

MUST be large random number. set highest bit.

◆ SetTruncateBits()

void GrayLib::cBitArray::SetTruncateBits ( BIT_ENUM_t  nBits)

◆ SetZeroAll()

void GrayLib::cBitArray::SetZeroAll ( )
inline

◆ SwapSecure()

HRESULT GRAYCALL GrayLib::cBitArray::SwapSecure ( THIS_t X,
THIS_t Y,
bool  swap 
)
static

Secure conditional swap "X <-> Y" if swap is 1 Used for only for ECP_MONTGOMERY

  • X = First cBigInt value
  • Y = Second cBigInt value
  • assign = 1: perform the swap, 0: keep X and Y's original values
Returns
0 if successful, E_OUTOFMEMORY if memory allocation failed,
Note
This function is equivalent to { if( assign ) cMem::Swap( X, Y ); } except that it avoids leaking any information about whether the assignment was done or not (the above code may leak information through branch prediction and/or memory access patterns analysis). Here it is NOT OK to simply swap the pointers

◆ UNITTEST_FRIEND()

GrayLib::cBitArray::UNITTEST_FRIEND ( cBitArray  )

◆ UpdateBlocksUse()

void GrayLib::cBitArray::UpdateBlocksUse ( )
inline

Decreases m_nBlocksUse to eliminate any leading (high) zero blocks. MUST be <= m_nBlocksUse

◆ UpdateBlocksUseCap()

void GrayLib::cBitArray::UpdateBlocksUseCap ( )
inline

Recalculate the value after an unknown change.

◆ UpdateBlocksUseMax()

void GrayLib::cBitArray::UpdateBlocksUseMax ( BLOCK_ENUM_t  nBlocksUseMax)
inline

Set m_nBlocksUse correctly assuming current value <= nBlocksUseMax

Member Data Documentation

◆ m_nBlocksUse

BLOCK_ENUM_t GrayLib::cBitArray::m_nBlocksUse
protected

Qty of blocks that is non zero. ASSUME anything above this is 0 blocks.


The documentation for this class was generated from the following files: