| 678 | |
| 679 | #[pyfunction] |
| 680 | fn rlecode_hqx(s: ArgAsciiBuffer) -> PyResult<Vec<u8>> { |
| 681 | const RUN_CHAR: u8 = 0x90; // b'\x90' |
| 682 | s.with_ref(|buffer| { |
| 683 | let len = buffer.len(); |
| 684 | let mut out_data = Vec::<u8>::with_capacity((len * 2) + 2); |
| 685 | |
| 686 | let mut idx = 0; |
| 687 | while idx < len { |
| 688 | let ch = buffer[idx]; |
| 689 | |
| 690 | if ch == RUN_CHAR { |
| 691 | out_data.push(RUN_CHAR); |
| 692 | out_data.push(0); |
| 693 | return Ok(out_data); |
| 694 | } else { |
| 695 | let mut in_end = idx + 1; |
| 696 | while in_end < len && buffer[in_end] == ch && in_end < idx + 255 { |
| 697 | in_end += 1; |
| 698 | } |
| 699 | if in_end - idx > 3 { |
| 700 | out_data.push(ch); |
| 701 | out_data.push(RUN_CHAR); |
| 702 | out_data.push(((in_end - idx) % 256) as u8); |
| 703 | idx = in_end - 1; |
| 704 | } else { |
| 705 | out_data.push(ch); |
| 706 | } |
| 707 | } |
| 708 | idx += 1; |
| 709 | } |
| 710 | Ok(out_data) |
| 711 | }) |
| 712 | } |
| 713 | |
| 714 | #[pyfunction] |
| 715 | fn rledecode_hqx(s: ArgAsciiBuffer) -> PyResult<Vec<u8>> { |