Skip to content

Commit

Permalink
struct GetBits: Adjust methods to make fields private (#631)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkysen authored Jan 4, 2024
2 parents 01464ba + 4dc129c commit f07d467
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
34 changes: 20 additions & 14 deletions src/getbits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,29 @@ use std::ffi::c_uint;

#[repr(C)]
pub struct GetBits {
pub state: u64,
pub bits_left: c_int,
pub error: c_int,
pub ptr: *const u8,
pub ptr_start: *const u8,
pub ptr_end: *const u8,
state: u64,
bits_left: c_int,
error: c_int,
ptr: *const u8,
ptr_start: *const u8,
ptr_end: *const u8,
}

impl GetBits {
pub unsafe fn init(&mut self, data: *const u8, sz: usize) {
pub const unsafe fn new(data: *const u8, sz: usize) -> Self {
assert!(sz != 0);
self.ptr_start = data;
self.ptr = self.ptr_start;
self.ptr_end = self.ptr_start.add(sz);
self.state = 0;
self.bits_left = 0;
self.error = 0;
Self {
ptr_start: data,
ptr: data,
ptr_end: data.add(sz),
state: 0,
bits_left: 0,
error: 0,
}
}

pub const fn has_error(&self) -> c_int {
self.error
}

pub unsafe fn get_bit(&mut self) -> c_uint {
Expand Down Expand Up @@ -173,7 +179,7 @@ impl GetBits {
}

#[inline]
pub unsafe fn pos(&self) -> c_uint {
pub const unsafe fn pos(&self) -> c_uint {
self.ptr.offset_from(self.ptr_start) as c_uint * 8 - self.bits_left as c_uint
}
}
31 changes: 11 additions & 20 deletions src/obu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,41 +122,41 @@ use std::mem::MaybeUninit;
struct Debug {
enabled: bool,
name: &'static str,
init_ptr: *const u8,
start: c_uint,
}

impl Debug {
pub const fn new(enabled: bool, name: &'static str, gb: &GetBits) -> Self {
pub const unsafe fn new(enabled: bool, name: &'static str, gb: &GetBits) -> Self {
Self {
enabled,
name,
init_ptr: gb.ptr,
start: gb.pos(),
}
}

const fn named(&self, name: &'static str) -> Self {
let &Self {
enabled,
name: _,
init_ptr,
start,
} = self;
Self {
enabled,
name,
init_ptr,
start,
}
}

pub unsafe fn log(&self, gb: &GetBits, msg: fmt::Arguments) {
let &Self {
enabled,
name,
init_ptr,
start,
} = self;
if !enabled {
return;
}
let offset = gb.ptr.offset_from(init_ptr) * 8 - gb.bits_left as isize;
let offset = gb.pos() - start;
println!("{name}: {msg} [off={offset}]");
}

Expand Down Expand Up @@ -2109,7 +2109,7 @@ unsafe fn check_for_overrun(
obu_len: c_uint,
) -> c_int {
// Make sure we haven't actually read past the end of the `gb` buffer
if gb.error != 0 {
if gb.has_error() != 0 {
writeln!(c.logger, "Overrun in OBU bit buffer");
return 1;
}
Expand Down Expand Up @@ -2154,16 +2154,7 @@ unsafe fn parse_obus(
len + init_byte_pos
}

let mut gb = GetBits {
state: 0,
bits_left: 0,
error: 0,
ptr: 0 as *const u8,
ptr_start: 0 as *const u8,
ptr_end: 0 as *const u8,
};

gb.init(r#in.data, r#in.sz);
let mut gb = GetBits::new(r#in.data, r#in.sz);

// obu header
gb.get_bit(); // obu_forbidden_bit
Expand All @@ -2186,7 +2177,7 @@ unsafe fn parse_obus(
} else {
r#in.sz as c_uint - 1 - has_extension as c_uint
};
if gb.error != 0 {
if gb.has_error() != 0 {
return Err(EINVAL);
}

Expand Down Expand Up @@ -2408,7 +2399,7 @@ unsafe fn parse_obus(
// obu metadata type field
let meta_type = gb.get_uleb128() as ObuMetaType;
let meta_type_len = ((gb.pos() - init_bit_pos) >> 3) as c_int;
if gb.error != 0 {
if gb.has_error() != 0 {
return Err(EINVAL);
}

Expand Down

0 comments on commit f07d467

Please sign in to comment.