source: libpacketdump/parser/bitbuffer.c @ 701e164

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 701e164 was 701e164, checked in by Perry Lorier <perry@…>, 16 years ago

Add missing bitbuffer files

  • Property mode set to 100644
File size: 1.4 KB
Line 
1#include <inttypes.h>
2#include "parser.h"
3#include <stdio.h>
4
5uint16_t bits;
6/* "the largest possible type the compiler supports" */
7bitbuffer_t buffer;
8
9bitbuffer_t getbit(void **packet, int *packlen, uint64_t numbits)
10{
11    bitbuffer_t ret;
12    bitbuffer_t mask;
13
14    /* While the buffer is not filled up and there is still
15     * data in the packet to read, read a byte...
16     *
17     * The buffer gets filled from right to left
18     */
19    while(bits < numbits && *packlen > 0)
20    {
21        uint8_t byte;
22        /* read in one byte from the packet */
23        byte=(*((bitbuffer_t*)*packet))&0xff;
24        buffer |= (bitbuffer_t)byte << (sizeof(bitbuffer_t)*8-(bits+sizeof(byte)*8));
25        /* update the position within the packet */
26        *packet = ((char*)*packet) + 1;
27
28        bits += sizeof(byte)*8;
29        *packlen -= 1;
30    }
31
32    /* our return value is the first <numbits> of the buffer */
33    mask = ~((1ULL<<((sizeof(bitbuffer_t)*8-numbits)))-1);
34    ret = buffer & mask;
35    ret >>=(sizeof(bitbuffer_t)*8-numbits);
36   
37    /* remove the bits that are being returned from out buffer */
38    buffer <<= numbits;
39
40    /* and update our position inside this buffer */
41    bits -= numbits;
42
43    return ret;
44}
45
46#ifdef TEST
47#include <stdio.h>
48int main(void)
49{
50        unsigned char mybuffer[] = { 0x01, 0x82, 0x03, 0x04, 0x05, 0x06 };
51        void *buf = mybuffer;
52        int len=sizeof(buffer);
53        printf("8bits=%"PRIx64"\n",getbit(&buf,&len,8));
54        printf("2bits=%"PRIx64"\n",getbit(&buf,&len,2));
55        return 0;
56}
57#endif
Note: See TracBrowser for help on using the repository browser.