Skip to content

Commit

Permalink
DER writer: Avoid as conversions when serializing tag & length.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Feb 19, 2024
1 parent 1c8ec2d commit e1730e5
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/io/der_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub(crate) fn write_all(tag: Tag, write_value: &dyn Fn(&mut dyn Accumulator)) ->
output.into()
}

#[allow(clippy::cast_possible_truncation)]
fn write_tlv<F>(output: &mut dyn Accumulator, tag: Tag, write_value: F)
where
F: Fn(&mut dyn Accumulator),
Expand All @@ -49,20 +48,18 @@ where
write_value(&mut length);
length.into()
};
let length: u16 = length.try_into().unwrap();

output.write_byte(tag as u8);
if length < 0x80 {
output.write_byte(length as u8);
} else if length < 0x1_00 {
output.write_byte(0x81);
output.write_byte(length as u8);
} else if length < 0x1_00_00 {
output.write_byte(tag.into());

let [lo, hi] = length.to_le_bytes();
if length >= 0x1_00 {
output.write_byte(0x82);
output.write_byte((length / 0x1_00) as u8);
output.write_byte(length as u8);
} else {
unreachable!();
};
output.write_byte(hi);
} else if length >= 0x80 {
output.write_byte(0x81);
}
output.write_byte(lo);

write_value(output);
}

0 comments on commit e1730e5

Please sign in to comment.