Skip to content

add fn Binascii rlecode_hqx rledecode_hqx #3809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Lib/test/test_binascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,6 @@ def test_hqx(self):
res = binascii.rledecode_hqx(b)
self.assertEqual(res, self.rawdata)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_rle(self):
# test repetition with a repetition longer than the limit of 255
data = (b'a' * 100 + b'b' + b'c' * 300)
Expand Down
67 changes: 67 additions & 0 deletions stdlib/src/binascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,73 @@ mod decl {
Ok((*c - 0x20) & 0x3f)
}

#[pyfunction]
fn rlecode_hqx(s: ArgAsciiBuffer) -> PyResult<Vec<u8>> {
const RUNCHAR: u8 = 0x90; // b'\x90'
s.with_ref(|buffer| {
let len = buffer.len();
let mut out_data = Vec::<u8>::with_capacity((len * 2) + 2);

let mut idx = 0;
while idx < len {
let ch = buffer[idx];

if ch == RUNCHAR {
out_data.push(RUNCHAR);
out_data.push(0);
return Ok(out_data);
} else {
let mut inend = idx + 1;
while inend < len && buffer[inend] == ch && inend < idx + 255 {
inend += 1;
}
if inend - idx > 3 {
out_data.push(ch);
out_data.push(RUNCHAR);
out_data.push(((inend - idx) % 256) as u8);
idx = inend - 1;
} else {
out_data.push(ch);
}
}
idx += 1;
}
Ok(out_data)
})
}

#[pyfunction]
fn rledecode_hqx(s: ArgAsciiBuffer) -> PyResult<Vec<u8>> {
const RUNCHAR: u8 = 0x90; //b'\x90'
s.with_ref(|buffer| {
let len = buffer.len();
let mut out_data = Vec::<u8>::with_capacity(len);
let mut idx = 0;

out_data.push(buffer[idx]);
idx += 1;

while idx < len {
if buffer[idx] == RUNCHAR {
if buffer[idx + 1] == 0 {
out_data.push(RUNCHAR);
} else {
let ch = buffer[idx - 1];
let range = buffer[idx + 1];
idx += 1;
for _ in 1..range {
out_data.push(ch);
}
}
} else {
out_data.push(buffer[idx]);
}
idx += 1;
}
Ok(out_data)
})
}

#[pyfunction]
fn a2b_uu(s: ArgAsciiBuffer, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
s.with_ref(|b| {
Expand Down