base/data/m_bit_parser.c
/* Return number of bytes written. */
Return number of bytes written.
Return number of bytes written .
static size_t peek_next_bytes(M_bit_parser_t *bparser, M_uint8 *dest, size_t destlen, size_t nbits, M_bool strict_pad)
{
	size_t         byteidx;
	size_t         bitskip;
	size_t         nbytes;
	const M_uint8 *src;
	if (nbits == 0) {
		return 0;
	}
	if (bparser->offset + nbits > bparser->nbits) {
		return 0;
	}
	nbytes  = (nbits + 7) / 8;
	byteidx = bparser->offset / 8;
	bitskip = bparser->offset % 8; 
	src     = bparser->bytes + byteidx;
	if (destlen < nbytes) {
		return 0;
	}
	if (bitskip == 0) {
		M_mem_copy(dest, src, nbytes);
	} else {
		M_uint8 invskip = (M_uint8)(8 - bitskip); 
		size_t  i;
		for (i=0; i<nbytes; i++) {
			dest[i] = (M_uint8)((src[i] << bitskip) | (src[i + 1] >> invskip));
		}
	}
	if (strict_pad) {
		size_t pad = nbits % 8;
		if (pad > 0) {
			pad = 8 - pad;
			dest[nbytes - 1] = (M_uint8)(dest[nbytes - 1] & ~(M_uint8)((1 << pad) - 1));
		}
	}
	return nbytes;
}
static size_t peek_next_bytes ( M_bit_parser_t * bparser , M_uint8 * dest , size_t destlen , size_t nbits , M_bool strict_pad ) { size_t byteidx ; size_t bitskip ; size_t nbytes ; const M_uint8 * src ; if ( nbits == 0 ) { return 0 ; } if ( bparser -> offset + nbits > bparser -> nbits ) { return 0 ; } nbytes = ( nbits + 7 ) / 8 ; byteidx = bparser -> offset / 8 ; bitskip = bparser -> offset % 8 ; src = bparser -> bytes + byteidx ; if ( destlen < nbytes ) { return 0 ; } if ( bitskip == 0 ) { M_mem_copy ( dest , src , nbytes ) ; } else { M_uint8 invskip = ( M_uint8 ) ( 8 - bitskip ) ; size_t i ; for ( i = 0 ; i < nbytes ; i ++ ) { dest [ i ] = ( M_uint8 ) ( ( src [ i ] << bitskip ) | ( src [ i + 1 ] >> invskip ) ) ; } } if ( strict_pad ) { size_t pad = nbits % 8 ; if ( pad > 0 ) { pad = 8 - pad ; dest [ nbytes - 1 ] = ( M_uint8 ) ( dest [ nbytes - 1 ] & ~ ( M_uint8 ) ( ( 1 << pad ) - 1 ) ) ; } } return nbytes ; }
Return number of bytes written.
Return number of bytes written .
/* number of bits to skip past on left side of first byte of source. */ /* If the data we're reading starts on a byte boundary, copy the data over byte-wise. */ /* number of bits to skip from right side of next byte */ /* Make sure any extra padding bits on end of last byte are set to zero. */