/*************************************************************************** * Copyright (c) 2003-2008, Broadcom Corporation * All Rights Reserved * Confidential Property of Broadcom Corporation * * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. * * $brcm_Workfile: $ * $brcm_Revision: $ * $brcm_Date: $ * * Module Description: private message parsing api * * Revision History: * * $brcm_Log: $ * * ***************************************************************************/ #include "bstd.h" #include "bdbg.h" #include "getbits.h" /* Return bit_count of bits from the bitstream. bit_count can be anywhere from 0 to 16. The bit_state_t structure has to be reset after 536870911 bytes as bindex is 32 bit value. This is not a practical limitation. */ unsigned int get_bits(size_t bit_count, struct bit_state_t * bs) { unsigned int bits; unsigned char * pbits; size_t data_index; if(0 == bit_count){ return 0; } data_index = bs->bindex >> 3; pbits = &bs->data[data_index]; bits = pbits[0]; bits <<= 8; bits |= pbits[1]; bits <<= 8; bits |= pbits[2]; bits <<= 8; bits <<= bs->bindex & 0x7; bits >>= (32 - bit_count); bs->bindex += bit_count; return bits; } /* Return bit_count of bits from bitstream. bit_count can be anywhere from 0 to 32. This function can be called only when bit field to be returned is aligned on a byte boundary. Most new standards follow this rule. */ unsigned int get_bits_aligned(size_t bit_count, struct bit_state_t * bs) { unsigned int bits; unsigned char * pbits; size_t data_index; if(0 == bit_count){ return 0; } BDBG_ASSERT(0 == (bs->bindex & 0x7)); data_index = bs->bindex >> 3; pbits = &bs->data[data_index]; bits = pbits[0]; bits <<= 8; bits |= pbits[1]; bits <<= 8; bits |= pbits[2]; bits <<= 8; bits |= pbits[3]; bits >>= (32 - bit_count); bs->bindex += bit_count; return bits; }