source: svn/trunk/newcon3bcm2_21bu/dta/src/app/getbits.c @ 2

Last change on this file since 2 was 2, checked in by phkim, 11 years ago

1.phkim

  1. revision copy newcon3sk r27
  • Property svn:executable set to *
File size: 2.1 KB
Line 
1/***************************************************************************
2 *     Copyright (c) 2003-2008, Broadcom Corporation
3 *     All Rights Reserved
4 *     Confidential Property of Broadcom Corporation
5 *
6 *  THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE
7 *  AGREEMENT  BETWEEN THE USER AND BROADCOM.  YOU HAVE NO RIGHT TO USE OR
8 *  EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT.
9 *
10 * $brcm_Workfile: $
11 * $brcm_Revision: $
12 * $brcm_Date: $
13 *
14 * Module Description: private message parsing api
15 *
16 * Revision History:
17 *
18 * $brcm_Log: $
19 *
20 *
21 ***************************************************************************/
22
23#include "bstd.h"
24#include "bdbg.h"
25
26#include "getbits.h"
27
28/*
29  Return bit_count of bits from the bitstream. bit_count can be anywhere
30  from 0 to 16. The bit_state_t structure has to be reset after 536870911
31  bytes as bindex is 32 bit value. This is not a practical limitation.
32 */
33unsigned int get_bits(size_t bit_count, struct bit_state_t * bs)
34{
35    unsigned int bits;
36    unsigned char * pbits;
37    size_t data_index;
38    if(0 == bit_count){
39        return 0;
40    }
41    data_index = bs->bindex >> 3;
42    pbits = &bs->data[data_index];
43    bits = pbits[0];
44    bits <<= 8;
45    bits |= pbits[1];
46    bits <<= 8;
47    bits |= pbits[2];
48    bits <<= 8;
49    bits <<= bs->bindex & 0x7;
50    bits >>= (32 - bit_count);
51    bs->bindex += bit_count;
52    return bits;
53}
54/* Return bit_count of bits from bitstream. bit_count can be anywhere from 0
55   to 32. This function can be called only when bit field to be returned is
56   aligned on a byte boundary. Most new standards follow this rule.
57 */
58unsigned int get_bits_aligned(size_t bit_count, struct bit_state_t * bs)
59{
60    unsigned int bits;
61    unsigned char * pbits;
62    size_t data_index;
63
64    if(0 == bit_count){
65        return 0;
66    }
67    BDBG_ASSERT(0 == (bs->bindex & 0x7));
68    data_index = bs->bindex >> 3;
69    pbits = &bs->data[data_index];
70    bits = pbits[0];
71    bits <<= 8;
72    bits |= pbits[1];
73    bits <<= 8;
74    bits |= pbits[2];
75    bits <<= 8;
76    bits |= pbits[3];
77    bits >>= (32 - bit_count);
78    bs->bindex += bit_count;
79    return bits;
80}
Note: See TracBrowser for help on using the repository browser.