Skip to content

Commit

Permalink
Simplify the program reader
Browse files Browse the repository at this point in the history
  • Loading branch information
koute committed Dec 11, 2023
1 parent 9fc22cd commit 121cc16
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions crates/polkavm-common/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,6 @@ pub struct ProgramBlob<'a> {
struct Reader<'a> {
blob: &'a [u8],
position: usize,
previous_position: usize,
}

impl<'a> Reader<'a> {
Expand All @@ -1041,24 +1040,22 @@ impl<'a> Reader<'a> {
}

fn read_varint(&mut self) -> Result<u32, ProgramParseError> {
let offset = self.position;
let first_byte = self.read_byte()?;
let (length, value) =
read_varint(&self.blob[self.position..], first_byte).ok_or(ProgramParseError(ProgramParseErrorKind::FailedToReadVarint {
offset: self.previous_position,
}))?;
let (length, value) = read_varint(&self.blob[self.position..], first_byte)
.ok_or(ProgramParseError(ProgramParseErrorKind::FailedToReadVarint { offset }))?;
self.position += length;
Ok(value)
}

fn read_string_with_length(&mut self) -> Result<&'a str, ProgramParseError> {
let offset = self.position;
let length = self.read_varint()?;
let range = self.read_slice_as_range(length)?;
let slice = &self.blob[range];
core::str::from_utf8(slice)
.ok()
.ok_or(ProgramParseError(ProgramParseErrorKind::FailedToReadStringNonUtf {
offset: self.previous_position,
}))
.ok_or(ProgramParseError(ProgramParseErrorKind::FailedToReadStringNonUtf { offset }))
}

fn read_slice_as_range(&mut self, count: u32) -> Result<Range<usize>, ProgramParseError> {
Expand All @@ -1070,7 +1067,8 @@ impl<'a> Reader<'a> {
actual_count: self.blob.len() - self.position,
}));
};
self.previous_position = core::mem::replace(&mut self.position, range.end);

self.position = range.end;
Ok(range)
}

Expand Down Expand Up @@ -1156,7 +1154,6 @@ impl<'a> ProgramBlob<'a> {
let mut reader = Reader {
blob: &program.blob,
position: BLOB_MAGIC.len(),
previous_position: 0,
};

let blob_version = reader.read_byte()?;
Expand Down Expand Up @@ -1208,7 +1205,7 @@ impl<'a> ProgramBlob<'a> {
}

Err(ProgramParseError(ProgramParseErrorKind::UnexpectedSection {
offset: reader.previous_position,
offset: reader.position - 1,
section,
}))
}
Expand Down Expand Up @@ -1242,7 +1239,6 @@ impl<'a> ProgramBlob<'a> {
Reader {
blob: &self.blob[..range.end],
position: range.start,
previous_position: 0,
}
}

Expand Down

0 comments on commit 121cc16

Please sign in to comment.