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 AUTHORS DISCLAIM ALL WARRANTIES |
8 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
9 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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 | fn from(len: LengthMeasurement) -> usize { |
28 | len.len |
29 | } |
30 | } |
31 | |
32 | impl LengthMeasurement { |
33 | pub fn zero() -> Self { |
34 | Self { len: 0 } |
35 | } |
36 | } |
37 | |
38 | impl Accumulator for LengthMeasurement { |
39 | fn write_byte(&mut self, _value: u8) -> Result<(), TooLongError> { |
40 | self.len = self.len.checked_add(1).ok_or_else(err:TooLongError::new)?; |
41 | Ok(()) |
42 | } |
43 | fn write_bytes(&mut self, value: &[u8]) -> Result<(), TooLongError> { |
44 | self.len = self |
45 | .len |
46 | .checked_add(value.len()) |
47 | .ok_or_else(err:TooLongError::new)?; |
48 | Ok(()) |
49 | } |
50 | } |
51 | |
52 | pub(super) struct Writer { |
53 | bytes: Vec<u8>, |
54 | requested_capacity: usize, |
55 | } |
56 | |
57 | impl Writer { |
58 | pub(super) fn with_capacity(capacity: LengthMeasurement) -> Self { |
59 | Self { |
60 | bytes: Vec::with_capacity(capacity.len), |
61 | requested_capacity: capacity.len, |
62 | } |
63 | } |
64 | } |
65 | |
66 | impl From<Writer> for Box<[u8]> { |
67 | fn from(writer: Writer) -> Self { |
68 | assert_eq!(writer.requested_capacity, writer.bytes.len()); |
69 | writer.bytes.into_boxed_slice() |
70 | } |
71 | } |
72 | |
73 | impl Accumulator for Writer { |
74 | fn write_byte(&mut self, value: u8) -> Result<(), TooLongError> { |
75 | self.bytes.push(value); |
76 | Ok(()) |
77 | } |
78 | fn write_bytes(&mut self, value: &[u8]) -> Result<(), TooLongError> { |
79 | self.bytes.extend(iter:value); |
80 | Ok(()) |
81 | } |
82 | } |
83 | |
84 | pub fn write_copy( |
85 | accumulator: &mut dyn Accumulator, |
86 | to_copy: untrusted::Input, |
87 | ) -> Result<(), TooLongError> { |
88 | accumulator.write_bytes(to_copy.as_slice_less_safe()) |
89 | } |
90 | |
91 | pub struct TooLongError(()); |
92 | |
93 | impl TooLongError { |
94 | pub fn new() -> Self { |
95 | Self(()) |
96 | } |
97 | } |
98 | |