-******************************************************** * bitStream decoding **********************************************************/ ! BIT_initDStream() : * Initialize a BIT_DStream_t. * `bitD` : a pointer to an already allocated BIT_DStream_t structure. * `srcSize` must be the *exact* size of the bitStream, in bytes. * @return : size of stream (== srcSize), or an errorCode if a proble
| 1566 | * @return : size of stream (== srcSize), or an errorCode if a problem is detected |
| 1567 | */ |
| 1568 | MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize) |
| 1569 | { |
| 1570 | if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); } |
| 1571 | |
| 1572 | bitD->start = (const char*)srcBuffer; |
| 1573 | bitD->limitPtr = bitD->start + sizeof(bitD->bitContainer); |
| 1574 | |
| 1575 | if (srcSize >= sizeof(bitD->bitContainer)) { /* normal case */ |
| 1576 | bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(bitD->bitContainer); |
| 1577 | bitD->bitContainer = MEM_readLEST(bitD->ptr); |
| 1578 | { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1]; |
| 1579 | bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */ |
| 1580 | if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */ } |
| 1581 | } else { |
| 1582 | bitD->ptr = bitD->start; |
| 1583 | bitD->bitContainer = *(const BYTE*)(bitD->start); |
| 1584 | switch(srcSize) |
| 1585 | { |
| 1586 | case 7: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16); |
| 1587 | /* fall-through */ |
| 1588 | |
| 1589 | case 6: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24); |
| 1590 | /* fall-through */ |
| 1591 | |
| 1592 | case 5: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32); |
| 1593 | /* fall-through */ |
| 1594 | |
| 1595 | case 4: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[3]) << 24; |
| 1596 | /* fall-through */ |
| 1597 | |
| 1598 | case 3: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[2]) << 16; |
| 1599 | /* fall-through */ |
| 1600 | |
| 1601 | case 2: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[1]) << 8; |
| 1602 | /* fall-through */ |
| 1603 | |
| 1604 | default: break; |
| 1605 | } |
| 1606 | { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1]; |
| 1607 | bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; |
| 1608 | if (lastByte == 0) return ERROR(corruption_detected); /* endMark not present */ |
| 1609 | } |
| 1610 | bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize)*8; |
| 1611 | } |
| 1612 | |
| 1613 | return srcSize; |
| 1614 | } |
| 1615 | |
| 1616 | MEM_STATIC size_t BIT_getUpperBits(size_t bitContainer, U32 const start) |
| 1617 | { |
no test coverage detected