MCPcopy Create free account
hub / github.com/F-Stack/f-stack / BIT_initDStream

Function BIT_initDStream

freebsd/contrib/zstd/lib/common/bitstream.h:277–323  ·  view source on GitHub ↗

-******************************************************** * 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

Source from the content-addressed store, hash-verified

275 * @return : size of stream (== srcSize), or an errorCode if a problem is detected
276 */
277MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize)
278{
279 if (srcSize < 1) { ZSTD_memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); }
280
281 bitD->start = (const char*)srcBuffer;
282 bitD->limitPtr = bitD->start + sizeof(bitD->bitContainer);
283
284 if (srcSize >= sizeof(bitD->bitContainer)) { /* normal case */
285 bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(bitD->bitContainer);
286 bitD->bitContainer = MEM_readLEST(bitD->ptr);
287 { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
288 bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */
289 if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */ }
290 } else {
291 bitD->ptr = bitD->start;
292 bitD->bitContainer = *(const BYTE*)(bitD->start);
293 switch(srcSize)
294 {
295 case 7: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16);
296 /* fall-through */
297
298 case 6: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24);
299 /* fall-through */
300
301 case 5: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32);
302 /* fall-through */
303
304 case 4: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[3]) << 24;
305 /* fall-through */
306
307 case 3: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[2]) << 16;
308 /* fall-through */
309
310 case 2: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[1]) << 8;
311 /* fall-through */
312
313 default: break;
314 }
315 { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
316 bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0;
317 if (lastByte == 0) return ERROR(corruption_detected); /* endMark not present */
318 }
319 bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize)*8;
320 }
321
322 return srcSize;
323}
324
325MEM_STATIC FORCE_INLINE_ATTR size_t BIT_getUpperBits(size_t bitContainer, U32 const start)
326{

Callers 1

Calls 2

MEM_readLESTFunction · 0.70
BIT_highbit32Function · 0.70

Tested by

no test coverage detected