1
0
Fork 0
forked from koniifer/ableos

OOOO MIGHTY CLIPPY, WE SUMMON YOU!!!

This commit is contained in:
Erin 2022-08-10 21:47:33 +02:00 committed by ondra05
parent f2bd9ddf4d
commit a75279df1f
10 changed files with 57 additions and 58 deletions

View file

@ -50,7 +50,7 @@ impl<T> Clone for Synced<T> {
impl<S: SectorSize, V: Volume<u8, S>> Synced<Ext2<S, V>> { impl<S: SectorSize, V: Volume<u8, S>> Synced<Ext2<S, V>> {
/// DOCME: what is this? /// DOCME: what is this?
pub fn new(volume: V) -> Result<Synced<Ext2<S, V>>, Error> { pub fn new(volume: V) -> Result<Synced<Ext2<S, V>>, Error> {
Ext2::new(volume).map(|inner| Synced::with_inner(inner)) Ext2::new(volume).map(Synced::with_inner)
} }
/// Get the root inode. /// Get the root inode.
pub fn root_inode(&self) -> Inode<S, V> { pub fn root_inode(&self) -> Inode<S, V> {
@ -139,7 +139,7 @@ impl<S: SectorSize, V: Volume<u8, S>> Fs for Synced<Ext2<S, V>> {
inner(fs, inode, path, abs_path) inner(fs, inode, path, abs_path)
} }
if abs_path.len() == 0 || abs_path[0] != b'/' { if abs_path.is_empty() || abs_path[0] != b'/' {
return Err(Error::NotAbsolute { return Err(Error::NotAbsolute {
name: String::from_utf8_lossy(abs_path).into_owned(), name: String::from_utf8_lossy(abs_path).into_owned(),
}); });
@ -355,17 +355,17 @@ impl<S: SectorSize, V: Volume<u8, S>> Inode<S, V> {
buf.set_len(total_size); buf.set_len(total_size);
} }
let size = self.read(&mut buf[..]); let size = self.read(&mut buf[..]);
size.and_then(|size| { size.map(|size| {
unsafe { unsafe {
buf.set_len(size); buf.set_len(size);
} }
Ok(size) size
}) })
.or_else(|err| { .map_err(|err| {
unsafe { unsafe {
buf.set_len(0); buf.set_len(0);
} }
Err(err) err
}) })
} }
/// Return blocks on a sector /// Return blocks on a sector
@ -571,7 +571,7 @@ impl<S: SectorSize, V: Volume<u8, S>> File for Inode<S, V> {
buf[offset..end].copy_from_slice(&data[..data_size]); buf[offset..end].copy_from_slice(&data[..data_size]);
offset += data_size; offset += data_size;
} }
Err(err) => return Err(err.into()), Err(err) => return Err(err),
} }
} }
@ -673,9 +673,9 @@ impl<S: SectorSize, V: Volume<u8, S>> Iterator for Directory<S, V> {
self.offset += size as usize; self.offset += size as usize;
Some(Ok(DirectoryEntry { Some(Ok(DirectoryEntry {
name: name, name,
inode: inode as usize, inode: inode as usize,
ty: ty, ty,
})) }))
} }
} }

View file

@ -65,7 +65,7 @@ impl<S: SectorSize> Address<S> {
/// ///
pub fn new(sector: u32, offset: i32) -> Address<S> { pub fn new(sector: u32, offset: i32) -> Address<S> {
let sector = (sector as i32 + (offset >> S::LOG_SIZE)) as u32; let sector = (sector as i32 + (offset >> S::LOG_SIZE)) as u32;
let offset = offset.abs() as u32 & S::OFFSET_MASK; let offset = offset.unsigned_abs() & S::OFFSET_MASK;
unsafe { Address::new_unchecked(sector, offset) } unsafe { Address::new_unchecked(sector, offset) }
} }
/// ///
@ -75,7 +75,7 @@ impl<S: SectorSize> Address<S> {
log_block_size: u32, log_block_size: u32,
) -> Address<S> { ) -> Address<S> {
let block = (block as i32 + (offset >> log_block_size)) as u32; let block = (block as i32 + (offset >> log_block_size)) as u32;
let offset = offset.abs() as u32 & ((1 << log_block_size) - 1); let offset = offset.unsigned_abs() & ((1 << log_block_size) - 1);
let log_diff = log_block_size as i32 - S::LOG_SIZE as i32; let log_diff = log_block_size as i32 - S::LOG_SIZE as i32;
let top_offset = offset >> S::LOG_SIZE; let top_offset = offset >> S::LOG_SIZE;

View file

@ -36,7 +36,7 @@ pub trait Volume<T: Clone, S: SectorSize> {
) -> Result<VolumeSlice<'a, T, S>, Self::Error>; ) -> Result<VolumeSlice<'a, T, S>, Self::Error>;
} }
#[derive(Debug, Clone, PartialEq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct VolumeSlice<'a, T: 'a + Clone, S: SectorSize> { pub struct VolumeSlice<'a, T: 'a + Clone, S: SectorSize> {
inner: Cow<'a, [T]>, inner: Cow<'a, [T]>,
index: Address<S>, index: Address<S>,

View file

@ -135,14 +135,14 @@ fn test_push_number() {
assert_eq!( assert_eq!(
bytes, bytes,
vec![ vec![
0b010__110__10, // 90 0b0101_1010, // 90
0b1__001_1010, // 154 0b1001_1010, // 154
0b1100__1011, // 203 0b1100_1011, // 203
0b0110_1101, // 109 0b0110_1101, // 109
0b01_1001_00, // 100 0b01_1001_00, // 100
0b01__111_001, // 121 0b0111_1001, // 121
0b0_1110_001, // 113 0b0111_0001, // 113
0b1__0000000, // 128 0b1000_0000, // 128
] ]
); );
} }
@ -287,21 +287,21 @@ mod eci_tests {
fn test_9() { fn test_9() {
let mut bits = Bits::new(Version::Normal(1)); let mut bits = Bits::new(Version::Normal(1));
assert_eq!(bits.push_eci_designator(9), Ok(())); assert_eq!(bits.push_eci_designator(9), Ok(()));
assert_eq!(bits.into_bytes(), vec![0b0111__0000, 0b1001__0000]); assert_eq!(bits.into_bytes(), vec![0b0111_0000, 0b1001_0000]);
} }
#[test] #[test]
fn test_899() { fn test_899() {
let mut bits = Bits::new(Version::Normal(1)); let mut bits = Bits::new(Version::Normal(1));
assert_eq!(bits.push_eci_designator(899), Ok(())); assert_eq!(bits.push_eci_designator(899), Ok(()));
assert_eq!(bits.into_bytes(), vec![0b0111__10_00, 0b00111000, 0b0011__0000]); assert_eq!(bits.into_bytes(), vec![0b0111_1000, 0b00111000, 0b0011_0000]);
} }
#[test] #[test]
fn test_999999() { fn test_999999() {
let mut bits = Bits::new(Version::Normal(1)); let mut bits = Bits::new(Version::Normal(1));
assert_eq!(bits.push_eci_designator(999999), Ok(())); assert_eq!(bits.push_eci_designator(999999), Ok(()));
assert_eq!(bits.into_bytes(), vec![0b0111__110_0, 0b11110100, 0b00100011, 0b1111__0000]); assert_eq!(bits.into_bytes(), vec![0b0111_1100, 0b11110100, 0b00100011, 0b1111_0000]);
} }
#[test] #[test]
@ -359,7 +359,7 @@ mod numeric_tests {
assert_eq!(bits.push_numeric_data(b"01234567"), Ok(())); assert_eq!(bits.push_numeric_data(b"01234567"), Ok(()));
assert_eq!( assert_eq!(
bits.into_bytes(), bits.into_bytes(),
vec![0b0001_0000, 0b001000_00, 0b00001100, 0b01010110, 0b01_100001, 0b1__0000000] vec![0b0001_0000, 0b001000_00, 0b00001100, 0b01010110, 0b0110_0001, 0b1000_0000]
); );
} }
@ -374,11 +374,11 @@ mod numeric_tests {
0b010000_00, 0b010000_00,
0b00001100, 0b00001100,
0b01010110, 0b01010110,
0b01_101010, 0b0110_1010,
0b0110_1110, 0b0110_1110,
0b000101_00, 0b000101_00,
0b11101010, 0b11101010,
0b0101__0000, 0b0101_0000,
] ]
); );
} }
@ -390,14 +390,14 @@ mod numeric_tests {
assert_eq!( assert_eq!(
bits.into_bytes(), bits.into_bytes(),
vec![ vec![
0b00_10000_0, 0b0010_0000,
0b00000110, 0b00000110,
0b0_0101011, 0b0010_1011,
0b001_10101, 0b0011_0101,
0b00110_111, 0b0011_0111,
0b0000101_0, 0b0000_1010,
0b01110101, 0b01110101,
0b00101__000, 0b0010_1000,
] ]
); );
} }
@ -467,7 +467,7 @@ mod alphanumeric_tests {
assert_eq!(bits.push_alphanumeric_data(b"AC-42"), Ok(())); assert_eq!(bits.push_alphanumeric_data(b"AC-42"), Ok(()));
assert_eq!( assert_eq!(
bits.into_bytes(), bits.into_bytes(),
vec![0b0010_0000, 0b00101_001, 0b11001110, 0b11100111, 0b001_00001, 0b0__0000000] vec![0b0010_0000, 0b0010_1001, 0b11001110, 0b11100111, 0b0010_0001, 0b0000_0000]
); );
} }
@ -524,7 +524,7 @@ mod byte_tests {
0b1010_1011, 0b1010_1011,
0b1100_1101, 0b1100_1101,
0b1110_1111, 0b1110_1111,
0b0000__0000, 0b0000_0000,
] ]
); );
} }
@ -579,7 +579,7 @@ mod kanji_tests {
fn test_iso_18004_example() { fn test_iso_18004_example() {
let mut bits = Bits::new(Version::Normal(1)); let mut bits = Bits::new(Version::Normal(1));
assert_eq!(bits.push_kanji_data(b"\x93\x5f\xe4\xaa"), Ok(())); assert_eq!(bits.push_kanji_data(b"\x93\x5f\xe4\xaa"), Ok(()));
assert_eq!(bits.into_bytes(), vec![0b1000_0000, 0b0010_0110, 0b11001111, 0b1_1101010, 0b101010__00]); assert_eq!(bits.into_bytes(), vec![0b1000_0000, 0b0010_0110, 0b11001111, 0b1110_1010, 0b1010_1000]);
} }
#[test] #[test]
@ -785,7 +785,7 @@ mod finish_tests {
let mut bits = Bits::new(Version::Micro(1)); let mut bits = Bits::new(Version::Micro(1));
assert_eq!(bits.push_numeric_data(b"99999"), Ok(())); assert_eq!(bits.push_numeric_data(b"99999"), Ok(()));
assert_eq!(bits.push_terminator(EcLevel::L), Ok(())); assert_eq!(bits.push_terminator(EcLevel::L), Ok(()));
assert_eq!(bits.into_bytes(), vec![0b101_11111, 0b00111_110, 0b0011__0000]); assert_eq!(bits.into_bytes(), vec![0b1011_1111, 0b0011_1110, 0b0011_0000]);
} }
#[test] #[test]
@ -793,7 +793,7 @@ mod finish_tests {
let mut bits = Bits::new(Version::Micro(1)); let mut bits = Bits::new(Version::Micro(1));
assert_eq!(bits.push_numeric_data(b"9999"), Ok(())); assert_eq!(bits.push_numeric_data(b"9999"), Ok(()));
assert_eq!(bits.push_terminator(EcLevel::L), Ok(())); assert_eq!(bits.push_terminator(EcLevel::L), Ok(()));
assert_eq!(bits.into_bytes(), vec![0b100_11111, 0b00111_100, 0b1_000__0000]); assert_eq!(bits.into_bytes(), vec![0b1001_1111, 0b0011_1100, 0b1000_0000]);
} }
#[test] #[test]
@ -801,7 +801,7 @@ mod finish_tests {
let mut bits = Bits::new(Version::Micro(1)); let mut bits = Bits::new(Version::Micro(1));
assert_eq!(bits.push_numeric_data(b"999"), Ok(())); assert_eq!(bits.push_numeric_data(b"999"), Ok(()));
assert_eq!(bits.push_terminator(EcLevel::L), Ok(())); assert_eq!(bits.push_terminator(EcLevel::L), Ok(()));
assert_eq!(bits.into_bytes(), vec![0b011_11111, 0b00111_000, 0b0000__0000]); assert_eq!(bits.into_bytes(), vec![0b0111_1111, 0b0011_1000, 0b0000_0000]);
} }
#[test] #[test]
@ -809,7 +809,7 @@ mod finish_tests {
let mut bits = Bits::new(Version::Micro(1)); let mut bits = Bits::new(Version::Micro(1));
assert_eq!(bits.push_numeric_data(b""), Ok(())); assert_eq!(bits.push_numeric_data(b""), Ok(()));
assert_eq!(bits.push_terminator(EcLevel::L), Ok(())); assert_eq!(bits.push_terminator(EcLevel::L), Ok(()));
assert_eq!(bits.into_bytes(), vec![0b000_000_00, 0b11101100, 0]); assert_eq!(bits.into_bytes(), vec![0b0000_0000, 0b11101100, 0]);
} }
} }
@ -882,7 +882,7 @@ mod encode_tests {
#[test] #[test]
fn test_auto_mode_switch() { fn test_auto_mode_switch() {
let res = encode(b"123A", Version::Micro(2), EcLevel::L); let res = encode(b"123A", Version::Micro(2), EcLevel::L);
assert_eq!(res, Ok(vec![0b0_0011_000, 0b1111011_1, 0b001_00101, 0b0_00000__00, 0b11101100])); assert_eq!(res, Ok(vec![0b0001_1000, 0b1111_0111, 0b0010_0101, 0b0000_0000, 0b11101100]));
} }
#[test] #[test]
@ -909,7 +909,7 @@ pub fn encode_auto(data: &[u8], ec_level: EcLevel) -> QrResult<Bits> {
let segments = Parser::new(data).collect::<Vec<Segment>>(); let segments = Parser::new(data).collect::<Vec<Segment>>();
for version in &[Version::Normal(9), Version::Normal(26), Version::Normal(40)] { for version in &[Version::Normal(9), Version::Normal(26), Version::Normal(40)] {
let opt_segments = Optimizer::new(segments.iter().cloned(), *version).collect::<Vec<_>>(); let opt_segments = Optimizer::new(segments.iter().cloned(), *version).collect::<Vec<_>>();
let total_len = total_encoded_len(&*opt_segments, *version); let total_len = total_encoded_len(&opt_segments, *version);
let data_capacity = version.fetch(ec_level, &DATA_LENGTHS).expect("invalid DATA_LENGTHS"); let data_capacity = version.fetch(ec_level, &DATA_LENGTHS).expect("invalid DATA_LENGTHS");
if total_len <= data_capacity { if total_len <= data_capacity {
let min_version = find_min_version(total_len, ec_level); let min_version = find_min_version(total_len, ec_level);

View file

@ -999,7 +999,7 @@ pub fn is_functional(version: Version, width: i16, x: i16, y: i16) -> bool {
true true
} else if a == 1 { } else if a == 1 {
false false
} else if 2 <= a && a <= 6 { } else if (2..=6).contains(&a) {
(width - 7 - x).abs() <= 2 && (width - 7 - y).abs() <= 2 (width - 7 - x).abs() <= 2 && (width - 7 - y).abs() <= 2
} else { } else {
let positions = ALIGNMENT_PATTERN_POSITIONS[(a - 7).as_usize()]; let positions = ALIGNMENT_PATTERN_POSITIONS[(a - 7).as_usize()];
@ -1193,7 +1193,7 @@ impl Iterator for DataModuleIter {
#[cfg(test)] #[cfg(test)]
#[rustfmt::skip] // skip to prevent file becoming too long. #[rustfmt::skip] // skip to prevent file becoming too long.
mod data_iter_tests { mod data_iter_tests {
use alloc::vec::{*,Vec}; use alloc::vec::{Vec};
use crate::canvas::DataModuleIter; use crate::canvas::DataModuleIter;
use crate::types::Version; use crate::types::Version;
@ -1373,7 +1373,7 @@ impl Canvas {
let bits_end = if i == last_word { 4 } else { 0 }; let bits_end = if i == last_word { 4 } else { 0 };
'outside: for j in (bits_end..=7).rev() { 'outside: for j in (bits_end..=7).rev() {
let color = if (*b & (1 << j)) == 0 { Color::Light } else { Color::Dark }; let color = if (*b & (1 << j)) == 0 { Color::Light } else { Color::Dark };
while let Some((x, y)) = coords.next() { for (x, y) in coords.by_ref() {
let r = self.get_mut(x, y); let r = self.get_mut(x, y);
if *r == Module::Empty { if *r == Module::Empty {
*r = Module::Unmasked(color); *r = Module::Unmasked(color);

View file

@ -124,7 +124,7 @@ pub fn construct_codewords(rawbits: &[u8], version: Version, ec_level: EcLevel)
// Generate EC codes. // Generate EC codes.
let ec_bytes = version.fetch(ec_level, &EC_BYTES_PER_BLOCK)?; let ec_bytes = version.fetch(ec_level, &EC_BYTES_PER_BLOCK)?;
let ec_codes = blocks.iter().map(|block| create_error_correction_code(*block, ec_bytes)).collect::<Vec<Vec<u8>>>(); let ec_codes = blocks.iter().map(|block| create_error_correction_code(block, ec_bytes)).collect::<Vec<Vec<u8>>>();
let blocks_vec = interleave(&blocks); let blocks_vec = interleave(&blocks);
let ec_vec = interleave(&ec_codes); let ec_vec = interleave(&ec_codes);

View file

@ -160,10 +160,10 @@ impl QrCode {
pub fn with_bits(bits: bits::Bits, ec_level: EcLevel) -> QrResult<Self> { pub fn with_bits(bits: bits::Bits, ec_level: EcLevel) -> QrResult<Self> {
let version = bits.version(); let version = bits.version();
let data = bits.into_bytes(); let data = bits.into_bytes();
let (encoded_data, ec_data) = ec::construct_codewords(&*data, version, ec_level)?; let (encoded_data, ec_data) = ec::construct_codewords(&data, version, ec_level)?;
let mut canvas = canvas::Canvas::new(version, ec_level); let mut canvas = canvas::Canvas::new(version, ec_level);
canvas.draw_all_functional_patterns(); canvas.draw_all_functional_patterns();
canvas.draw_data(&*encoded_data, &*ec_data); canvas.draw_data(&encoded_data, &ec_data);
let canvas = canvas.apply_best_mask(); let canvas = canvas.apply_best_mask();
Ok(Self { content: canvas.into_colors(), version, ec_level, width: version.width().as_usize() }) Ok(Self { content: canvas.into_colors(), version, ec_level, width: version.width().as_usize() })
} }

View file

@ -1,7 +1,6 @@
//! Find the optimal data mode sequence to encode a piece of data. //! Find the optimal data mode sequence to encode a piece of data.
use crate::types::{Mode, Version}; use crate::types::{Mode, Version};
#[cfg(test)]
use alloc::vec::{self, Vec, *};
use core::slice::Iter; use core::slice::Iter;
@ -156,7 +155,7 @@ impl<'a> Iterator for Parser<'a> {
#[cfg(test)] #[cfg(test)]
mod parse_tests { mod parse_tests {
use alloc::vec::{self, Vec}; use alloc::vec::{Vec};
use crate::optimize::{Parser, Segment}; use crate::optimize::{Parser, Segment};
use crate::types::Mode; use crate::types::Mode;
@ -347,9 +346,9 @@ mod optimize_tests {
use crate::types::{Mode, Version}; use crate::types::{Mode, Version};
fn test_optimization_result(given: Vec<Segment>, expected: Vec<Segment>, version: Version) { fn test_optimization_result(given: Vec<Segment>, expected: Vec<Segment>, version: Version) {
let prev_len = total_encoded_len(&*given, version); let prev_len = total_encoded_len(&given, version);
let opt_segs = Optimizer::new(given.iter().map(|seg| *seg), version).collect::<Vec<_>>(); let opt_segs = Optimizer::new(given.iter().copied(), version).collect::<Vec<_>>();
let new_len = total_encoded_len(&*opt_segs, version); let new_len = total_encoded_len(&opt_segs, version);
if given != opt_segs { if given != opt_segs {
assert!(prev_len > new_len, "{} > {}", prev_len, new_len); assert!(prev_len > new_len, "{} > {}", prev_len, new_len);
} }
@ -357,7 +356,7 @@ mod optimize_tests {
opt_segs == expected, opt_segs == expected,
"Optimization gave something better: {} < {} ({:?})", "Optimization gave something better: {} < {} ({:?})",
new_len, new_len,
total_encoded_len(&*expected, version), total_encoded_len(&expected, version),
opt_segs opt_segs
); );
} }

View file

@ -5,7 +5,7 @@ use alloc::{string::String, vec::Vec};
const CODEPAGE: [&str; 4] = [" ", "\u{2584}", "\u{2580}", "\u{2588}"]; const CODEPAGE: [&str; 4] = [" ", "\u{2584}", "\u{2580}", "\u{2588}"];
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, PartialEq, Eq)]
pub enum Dense1x2 { pub enum Dense1x2 {
Dark, Dark,
Light, Light,

View file

@ -71,7 +71,7 @@ impl Bus {
id, id,
irq, irq,
data_register: Port::new(io_base + 0), data_register: Port::new(io_base),
error_register: PortReadOnly::new(io_base + 1), error_register: PortReadOnly::new(io_base + 1),
features_register: PortWriteOnly::new(io_base + 1), features_register: PortWriteOnly::new(io_base + 1),
sector_count_register: Port::new(io_base + 2), sector_count_register: Port::new(io_base + 2),
@ -82,8 +82,8 @@ impl Bus {
status_register: PortReadOnly::new(io_base + 7), status_register: PortReadOnly::new(io_base + 7),
command_register: PortWriteOnly::new(io_base + 7), command_register: PortWriteOnly::new(io_base + 7),
alternate_status_register: PortReadOnly::new(ctrl_base + 0), alternate_status_register: PortReadOnly::new(ctrl_base),
control_register: PortWriteOnly::new(ctrl_base + 0), control_register: PortWriteOnly::new(ctrl_base),
drive_blockess_register: PortReadOnly::new(ctrl_base + 1), drive_blockess_register: PortReadOnly::new(ctrl_base + 1),
} }
} }
@ -248,7 +248,7 @@ impl Bus {
let data = self.read_data(); let data = self.read_data();
//log!("Read[{:08X}][{:02X}]: 0x{:04X}\n", block, i, data); //log!("Read[{:08X}][{:02X}]: 0x{:04X}\n", block, i, data);
buf[i + 0] = data.get_bits(0..8) as u8; buf[i] = data.get_bits(0..8) as u8;
buf[i + 1] = data.get_bits(8..16) as u8; buf[i + 1] = data.get_bits(8..16) as u8;
} }
} }
@ -274,7 +274,7 @@ impl Bus {
self.write_command(Command::Write); self.write_command(Command::Write);
self.busy_loop(); self.busy_loop();
for i in 0..256 { for i in 0..256 {
let mut data = 0 as u16; let mut data = 0_u16;
data.set_bits(0..8, buf[i * 2] as u16); data.set_bits(0..8, buf[i * 2] as u16);
data.set_bits(8..16, buf[i * 2 + 1] as u16); data.set_bits(8..16, buf[i * 2 + 1] as u16);