use super::openjpeg::*;
use super::malloc::*;
#[repr(C)]
#[derive(Copy, Clone)]
pub struct opj_bio {
pub start: *mut OPJ_BYTE,
pub end: *mut OPJ_BYTE,
pub bp: *mut OPJ_BYTE,
pub buf: OPJ_UINT32,
pub ct: OPJ_UINT32,
}
pub type opj_bio_t = opj_bio;
unsafe fn opj_bio_byteout(mut bio: *mut opj_bio_t) -> OPJ_BOOL {
(*bio).buf = (*bio).buf << 8i32 & 0xffffu32;
(*bio).ct = if (*bio).buf == 0xff00u32 { 7i32 } else { 8i32 } as OPJ_UINT32;
if (*bio).bp as OPJ_SIZE_T >= (*bio).end as OPJ_SIZE_T {
return 0i32;
}
let fresh0 = (*bio).bp;
(*bio).bp = (*bio).bp.offset(1);
*fresh0 = ((*bio).buf >> 8i32) as OPJ_BYTE;
1i32
}
unsafe fn opj_bio_bytein(mut bio: *mut opj_bio_t) -> OPJ_BOOL {
(*bio).buf = (*bio).buf << 8i32 & 0xffffu32;
(*bio).ct = if (*bio).buf == 0xff00u32 { 7i32 } else { 8i32 } as OPJ_UINT32;
if (*bio).bp as OPJ_SIZE_T >= (*bio).end as OPJ_SIZE_T {
return 0i32;
}
let fresh1 = (*bio).bp;
(*bio).bp = (*bio).bp.offset(1);
(*bio).buf |= *fresh1 as core::ffi::c_uint;
1i32
}
unsafe fn opj_bio_getbit(mut bio: *mut opj_bio_t) -> OPJ_UINT32 {
if (*bio).ct == 0u32 {
opj_bio_bytein(bio);
}
(*bio).ct = (*bio).ct.wrapping_sub(1);
(*bio).buf >> (*bio).ct & 1u32
}
#[no_mangle]
pub(crate) unsafe fn opj_bio_create() -> *mut opj_bio_t {
opj_malloc(core::mem::size_of::<opj_bio_t>()) as *mut opj_bio_t
}
#[no_mangle]
pub(crate) unsafe fn opj_bio_destroy(mut bio: *mut opj_bio_t) {
if !bio.is_null() {
opj_free(bio as *mut core::ffi::c_void);
};
}
#[no_mangle]
pub(crate) unsafe fn opj_bio_numbytes(mut bio: *mut opj_bio_t) -> isize {
(*bio).bp.offset_from((*bio).start)
}
#[no_mangle]
pub(crate) unsafe fn opj_bio_init_enc(
mut bio: *mut opj_bio_t,
mut bp: *mut OPJ_BYTE,
mut len: OPJ_UINT32,
) {
(*bio).start = bp;
(*bio).end = bp.offset(len as isize);
(*bio).bp = bp;
(*bio).buf = 0 as OPJ_UINT32;
(*bio).ct = 8 as OPJ_UINT32;
}
#[no_mangle]
pub(crate) unsafe fn opj_bio_init_dec(
mut bio: *mut opj_bio_t,
mut bp: *mut OPJ_BYTE,
mut len: OPJ_UINT32,
) {
(*bio).start = bp;
(*bio).end = bp.offset(len as isize);
(*bio).bp = bp;
(*bio).buf = 0 as OPJ_UINT32;
(*bio).ct = 0 as OPJ_UINT32;
}
#[no_mangle]
pub(crate) unsafe fn opj_bio_putbit(mut bio: *mut opj_bio_t, mut b: OPJ_UINT32) {
if (*bio).ct == 0u32 {
opj_bio_byteout(bio);
}
(*bio).ct -= 1;
(*bio).buf |= b << (*bio).ct;
}
#[no_mangle]
pub(crate) unsafe fn opj_bio_write(mut bio: *mut opj_bio_t, mut v: OPJ_UINT32, mut n: OPJ_UINT32) {
let mut i: OPJ_INT32 = 0;
assert!(n > 0u32 && n <= 32u32);
i = n as OPJ_INT32 - 1i32;
while i >= 0i32 {
opj_bio_putbit(bio, v >> i & 1u32);
i -= 1
}
}
#[no_mangle]
pub(crate) unsafe fn opj_bio_read(mut bio: *mut opj_bio_t, mut n: OPJ_UINT32) -> OPJ_UINT32 {
let mut i: OPJ_INT32 = 0;
let mut v: OPJ_UINT32 = 0;
assert!(n > 0u32);
v = 0u32;
i = n as OPJ_INT32 - 1i32;
while i >= 0i32 {
v |= opj_bio_getbit(bio) << i;
i -= 1
}
v
}
#[no_mangle]
pub(crate) unsafe fn opj_bio_flush(mut bio: *mut opj_bio_t) -> OPJ_BOOL {
if opj_bio_byteout(bio) == 0 {
return 0i32;
}
if (*bio).ct == 7u32 && opj_bio_byteout(bio) == 0 {
return 0i32;
}
1i32
}
#[no_mangle]
pub(crate) unsafe fn opj_bio_inalign(mut bio: *mut opj_bio_t) -> OPJ_BOOL {
if (*bio).buf & 0xffu32 == 0xffu32 && opj_bio_bytein(bio) == 0 {
return 0i32;
}
(*bio).ct = 0 as OPJ_UINT32;
1i32
}