| 1 | #include "DST_BitBuffer.h" |
|---|
| 2 | |
|---|
| 3 | #define DHL_FAIL_OUT_OF_RESOURCE 0x0A |
|---|
| 4 | |
|---|
| 5 | struct bitBuffer { |
|---|
| 6 | DS_U8* buffer; |
|---|
| 7 | DS_U32 bufSize; |
|---|
| 8 | DS_U32 bitOffset; |
|---|
| 9 | bool overrunError; |
|---|
| 10 | }bitBuffer; |
|---|
| 11 | |
|---|
| 12 | /****************************************************************************** |
|---|
| 13 | * Local variables declaration |
|---|
| 14 | ******************************************************************************/ |
|---|
| 15 | static const DS_U32 bitMaskTable[] = { |
|---|
| 16 | 0x00000000,0x00000001,0x00000003,0x00000007,0x0000000F,0x0000001F,0x0000003F,0x0000007F, |
|---|
| 17 | 0x000000FF,0x000001FF,0x000003FF,0x000007FF,0x00000FFF,0x00001FFF,0x00003FFF,0x00007FFF, |
|---|
| 18 | 0x0000FFFF,0x0001FFFF,0x0003FFFF,0x0007FFFF,0x000FFFFF,0x001FFFFF,0x003FFFFF,0x007FFFFF, |
|---|
| 19 | 0x00FFFFFF,0x01FFFFFF,0x03FFFFFF,0x07FFFFFF,0x0FFFFFFF,0x1FFFFFFF,0x3FFFFFFF,0x7FFFFFFF, |
|---|
| 20 | 0xFFFFFFFF |
|---|
| 21 | }; |
|---|
| 22 | static const DS_U8 shiftTable1[] = {8,1,2,3,4,5,6,7}; |
|---|
| 23 | static const DS_U8 shiftTable2[] = {0,7,6,5,4,3,2,1}; |
|---|
| 24 | |
|---|
| 25 | /****************************************************************************** |
|---|
| 26 | * Local function prototypes |
|---|
| 27 | ******************************************************************************/ |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | /*========================================================================= |
|---|
| 31 | ErrCode bitBufferCreate (bitBufferPtr_t *instance, DS_U8 *buffer, DS_U32 bufSize) |
|---|
| 32 | |
|---|
| 33 | *instance: bitBuffer instance passed back to caller. |
|---|
| 34 | *buffer: pointer to a byte buffer. |
|---|
| 35 | bufSize: size of the buffer (in bytes). |
|---|
| 36 | |
|---|
| 37 | Creates a bitBuffer. After creation, the bitBufferGetBits() function can |
|---|
| 38 | be called to extract bits. |
|---|
| 39 | =========================================================================*/ |
|---|
| 40 | int bitBufferCreate (bitBufferPtr_t *instance, DS_U8 *buffer, DS_U32 bufSize) |
|---|
| 41 | { |
|---|
| 42 | bitBufferPtr_t bitBufferPtr; |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | /* create bitBuffer structure */ |
|---|
| 46 | bitBufferPtr = (bitBufferPtr_t)DST_OS_MallocDirect(sizeof(bitBuffer_t)); |
|---|
| 47 | if (bitBufferPtr == NULL) { |
|---|
| 48 | return DHL_FAIL_OUT_OF_RESOURCE; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | memset( bitBufferPtr , 0 , sizeof(bitBuffer_t) ); |
|---|
| 52 | |
|---|
| 53 | /* Initialize */ |
|---|
| 54 | bitBufferPtr->buffer = buffer; |
|---|
| 55 | bitBufferPtr->bufSize = bufSize; |
|---|
| 56 | bitBufferPtr->bitOffset = 0; |
|---|
| 57 | |
|---|
| 58 | /* pass instance to user */ |
|---|
| 59 | *instance = bitBufferPtr; |
|---|
| 60 | |
|---|
| 61 | return (0); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | /*========================================================================= |
|---|
| 66 | int bitBufferDestroy (bitBufferPtr_t instance) |
|---|
| 67 | |
|---|
| 68 | instance: bitBuffer instance. |
|---|
| 69 | |
|---|
| 70 | Deletes the bitBuffer. DOES NOT free the buffer passed by the user in the |
|---|
| 71 | function bitBufferCreate()!! |
|---|
| 72 | =========================================================================*/ |
|---|
| 73 | int bitBufferDestroy (bitBufferPtr_t instance) |
|---|
| 74 | { |
|---|
| 75 | //AtiCore_MemFree(instance); |
|---|
| 76 | DST_OS_FreeDirect(instance); |
|---|
| 77 | return (0); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | /*========================================================================= |
|---|
| 82 | DS_U32 bitBufferGetBits (bitBufferPtr_t bitBufferPtr, DS_U8 numberOfBits) |
|---|
| 83 | |
|---|
| 84 | bitBufferPtr: bitBuffer instance. |
|---|
| 85 | numberOfBits: number of bits to be read from bitBuffer. |
|---|
| 86 | |
|---|
| 87 | Extracts the specified number of bits from the bitBuffer. The number of |
|---|
| 88 | bits must be less than or equal to 32. If more bits are requested than |
|---|
| 89 | are present in the bitBuffer, the overrunError flag is set. |
|---|
| 90 | =========================================================================*/ |
|---|
| 91 | DS_U32 bitBufferGetBits (bitBufferPtr_t bitBufferPtr, DS_U8 numberOfBits) |
|---|
| 92 | { |
|---|
| 93 | DS_U32 returnBits = 0; |
|---|
| 94 | DS_U8 bitIndex; |
|---|
| 95 | DS_U8 newBitIndexDiv8,newBitIndexMod8; |
|---|
| 96 | DS_U8 readByteCount; |
|---|
| 97 | DS_U8 *p; |
|---|
| 98 | DS_U8 i; |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | if (numberOfBits > (8*bitBufferPtr->bufSize - bitBufferPtr->bitOffset)) { |
|---|
| 102 | |
|---|
| 103 | bitBufferPtr->overrunError = true; |
|---|
| 104 | bitBufferPtr->bitOffset = 8*bitBufferPtr->bufSize; |
|---|
| 105 | returnBits = 0; |
|---|
| 106 | goto GetBitsReturn; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | bitIndex = bitBufferPtr->bitOffset & 0x07; |
|---|
| 110 | newBitIndexDiv8 = (bitIndex+numberOfBits) >> 3; |
|---|
| 111 | newBitIndexMod8 = (bitIndex+numberOfBits) & 0x07; |
|---|
| 112 | readByteCount = newBitIndexDiv8 + (newBitIndexMod8 ? 1:0); |
|---|
| 113 | |
|---|
| 114 | p = bitBufferPtr->buffer + (bitBufferPtr->bitOffset >> 3); |
|---|
| 115 | |
|---|
| 116 | /* copy first byte */ |
|---|
| 117 | returnBits = *p++; |
|---|
| 118 | if (readByteCount < 2) { |
|---|
| 119 | /* shift bits right */ |
|---|
| 120 | returnBits = returnBits >> shiftTable2[newBitIndexMod8]; |
|---|
| 121 | } |
|---|
| 122 | else { /* more bytes */ |
|---|
| 123 | for (i=0; i < readByteCount-2; i++) { |
|---|
| 124 | /* just copying 'middle' bytes */ |
|---|
| 125 | returnBits = (returnBits << 8) | *p++; |
|---|
| 126 | } |
|---|
| 127 | /* copy last byte */ |
|---|
| 128 | returnBits = (returnBits << shiftTable1[newBitIndexMod8]) | ((*p) >> shiftTable2[newBitIndexMod8]); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | returnBits &= bitMaskTable[numberOfBits]; |
|---|
| 132 | bitBufferPtr->bitOffset += numberOfBits; |
|---|
| 133 | |
|---|
| 134 | GetBitsReturn: |
|---|
| 135 | return (returnBits); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | /*========================================================================= |
|---|
| 140 | void bitBufferSkipBits (bitBufferPtr_t bitBufferPtr, DS_U16 numberOfBits) |
|---|
| 141 | |
|---|
| 142 | bitBufferPtr: bitBuffer instance. |
|---|
| 143 | numberOfBits: number of bits to be skipped. |
|---|
| 144 | |
|---|
| 145 | Skips the specified number of bits in the bitBuffer. This operation is |
|---|
| 146 | faster than extracting bits and can be used when a bit field is ignored. |
|---|
| 147 | =========================================================================*/ |
|---|
| 148 | void bitBufferSkipBits (bitBufferPtr_t bitBufferPtr, DS_U16 numberOfBits) |
|---|
| 149 | { |
|---|
| 150 | bitBufferPtr->bitOffset += numberOfBits; |
|---|
| 151 | if (8*bitBufferPtr->bufSize < bitBufferPtr->bitOffset) { |
|---|
| 152 | bitBufferPtr->overrunError = true; |
|---|
| 153 | } |
|---|
| 154 | return; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | /*========================================================================= |
|---|
| 159 | DS_U8* bitBufferGetBytePointer (INT32 instance) |
|---|
| 160 | |
|---|
| 161 | bitBufferPtr: bitBuffer instance. |
|---|
| 162 | |
|---|
| 163 | Returns a pointer to the current byte offset. |
|---|
| 164 | =========================================================================*/ |
|---|
| 165 | DS_U8* bitBufferGetBytePointer (bitBufferPtr_t bitBufferPtr) |
|---|
| 166 | { |
|---|
| 167 | return (bitBufferPtr->buffer + (bitBufferPtr->bitOffset >> 3)); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | /*========================================================================= |
|---|
| 172 | bool bitBufferCheckError (INT32 instance) |
|---|
| 173 | |
|---|
| 174 | bitBufferPtr_t: bitBuffer instance. |
|---|
| 175 | |
|---|
| 176 | Returns '_TRUE_' if the bitBuffer is in an overrun error state. |
|---|
| 177 | =========================================================================*/ |
|---|
| 178 | bool bitBufferCheckError (bitBufferPtr_t bitBufferPtr) |
|---|
| 179 | { |
|---|
| 180 | return (bitBufferPtr->overrunError); |
|---|
| 181 | } |
|---|