/rust/registry/src/index.crates.io-6f17d22bba15001f/ring-0.17.14/src/io/writer.rs
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2018 Brian Smith. |
2 | | // |
3 | | // Permission to use, copy, modify, and/or distribute this software for any |
4 | | // purpose with or without fee is hereby granted, provided that the above |
5 | | // copyright notice and this permission notice appear in all copies. |
6 | | // |
7 | | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
8 | | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
9 | | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
10 | | // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
11 | | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
12 | | // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
13 | | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
14 | | |
15 | | use alloc::{boxed::Box, vec::Vec}; |
16 | | |
17 | | pub trait Accumulator { |
18 | | fn write_byte(&mut self, value: u8) -> Result<(), TooLongError>; |
19 | | fn write_bytes(&mut self, value: &[u8]) -> Result<(), TooLongError>; |
20 | | } |
21 | | |
22 | | pub(super) struct LengthMeasurement { |
23 | | len: usize, |
24 | | } |
25 | | |
26 | | impl From<LengthMeasurement> for usize { |
27 | 0 | fn from(len: LengthMeasurement) -> usize { |
28 | 0 | len.len |
29 | 0 | } |
30 | | } |
31 | | |
32 | | impl LengthMeasurement { |
33 | 0 | pub fn zero() -> Self { |
34 | 0 | Self { len: 0 } |
35 | 0 | } |
36 | | } |
37 | | |
38 | | impl Accumulator for LengthMeasurement { |
39 | 0 | fn write_byte(&mut self, _value: u8) -> Result<(), TooLongError> { |
40 | 0 | self.len = self.len.checked_add(1).ok_or_else(TooLongError::new)?; |
41 | 0 | Ok(()) |
42 | 0 | } |
43 | 0 | fn write_bytes(&mut self, value: &[u8]) -> Result<(), TooLongError> { |
44 | 0 | self.len = self |
45 | 0 | .len |
46 | 0 | .checked_add(value.len()) |
47 | 0 | .ok_or_else(TooLongError::new)?; |
48 | 0 | Ok(()) |
49 | 0 | } |
50 | | } |
51 | | |
52 | | pub(super) struct Writer { |
53 | | bytes: Vec<u8>, |
54 | | requested_capacity: usize, |
55 | | } |
56 | | |
57 | | impl Writer { |
58 | 0 | pub(super) fn with_capacity(capacity: LengthMeasurement) -> Self { |
59 | 0 | Self { |
60 | 0 | bytes: Vec::with_capacity(capacity.len), |
61 | 0 | requested_capacity: capacity.len, |
62 | 0 | } |
63 | 0 | } |
64 | | } |
65 | | |
66 | | impl From<Writer> for Box<[u8]> { |
67 | 0 | fn from(writer: Writer) -> Self { |
68 | 0 | assert_eq!(writer.requested_capacity, writer.bytes.len()); |
69 | 0 | writer.bytes.into_boxed_slice() |
70 | 0 | } |
71 | | } |
72 | | |
73 | | impl Accumulator for Writer { |
74 | 0 | fn write_byte(&mut self, value: u8) -> Result<(), TooLongError> { |
75 | 0 | self.bytes.push(value); |
76 | 0 | Ok(()) |
77 | 0 | } |
78 | 0 | fn write_bytes(&mut self, value: &[u8]) -> Result<(), TooLongError> { |
79 | 0 | self.bytes.extend(value); |
80 | 0 | Ok(()) |
81 | 0 | } |
82 | | } |
83 | | |
84 | 0 | pub fn write_copy( |
85 | 0 | accumulator: &mut dyn Accumulator, |
86 | 0 | to_copy: untrusted::Input, |
87 | 0 | ) -> Result<(), TooLongError> { |
88 | 0 | accumulator.write_bytes(to_copy.as_slice_less_safe()) |
89 | 0 | } |
90 | | |
91 | | pub struct TooLongError(()); |
92 | | |
93 | | impl TooLongError { |
94 | 0 | pub fn new() -> Self { |
95 | 0 | Self(()) |
96 | 0 | } |
97 | | } |