blob: 8fb36eeaa49dc8277cef940de7049920d358c28a [file] [log] [blame]
Jooyung Han12a0b702021-08-05 23:20:31 +09001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Provides extension methods Bytes::read<T>(), which calls back ReadFromBytes::read_from_byte()
18
Alice Wangbc4b9a92022-09-16 13:13:18 +000019use anyhow::{ensure, Result};
Jooyung Han12a0b702021-08-05 23:20:31 +090020use bytes::{Buf, Bytes};
21use std::ops::Deref;
22
Jooyung Han5b4c70e2021-08-09 16:36:13 +090023#[derive(Clone, Debug)]
Jooyung Han12a0b702021-08-05 23:20:31 +090024pub struct LengthPrefixed<T> {
25 inner: T,
26}
27
28impl<T> Deref for LengthPrefixed<T> {
29 type Target = T;
30 fn deref(&self) -> &Self::Target {
31 &self.inner
32 }
33}
34
35pub trait BytesExt {
36 fn read<T: ReadFromBytes>(&mut self) -> Result<T>;
37}
38
39impl BytesExt for Bytes {
40 fn read<T: ReadFromBytes>(&mut self) -> Result<T> {
41 T::read_from_bytes(self)
42 }
43}
44
45pub trait ReadFromBytes {
46 fn read_from_bytes(buf: &mut Bytes) -> Result<Self>
47 where
48 Self: Sized;
49}
50
51impl ReadFromBytes for u32 {
52 fn read_from_bytes(buf: &mut Bytes) -> Result<Self> {
53 Ok(buf.get_u32_le())
54 }
55}
56
57impl<T: ReadFromBytes> ReadFromBytes for Vec<T> {
58 fn read_from_bytes(buf: &mut Bytes) -> Result<Self> {
59 let mut result = vec![];
60 while buf.has_remaining() {
61 result.push(buf.read()?);
62 }
63 Ok(result)
64 }
65}
66
67impl<T: ReadFromBytes> ReadFromBytes for LengthPrefixed<T> {
68 fn read_from_bytes(buf: &mut Bytes) -> Result<Self> {
69 let mut inner = read_length_prefixed_slice(buf)?;
70 let inner = inner.read()?;
71 Ok(LengthPrefixed { inner })
72 }
73}
74
75impl ReadFromBytes for Bytes {
76 fn read_from_bytes(buf: &mut Bytes) -> Result<Self> {
77 Ok(buf.slice(..))
78 }
79}
80
81fn read_length_prefixed_slice(buf: &mut Bytes) -> Result<Bytes> {
Alice Wangbc4b9a92022-09-16 13:13:18 +000082 ensure!(
83 buf.remaining() >= 4,
84 "Remaining buffer too short to contain length of length-prefixed field. Remaining: {}",
85 buf.remaining()
86 );
Jooyung Han12a0b702021-08-05 23:20:31 +090087 let len = buf.get_u32_le() as usize;
Alice Wangbc4b9a92022-09-16 13:13:18 +000088 ensure!(
89 buf.remaining() >= len,
90 "length-prefixed field longer than remaining buffer. Field length: {}, remaining: {}",
91 len,
92 buf.remaining()
93 );
Jooyung Han12a0b702021-08-05 23:20:31 +090094 Ok(buf.split_to(len))
95}
Jooyung Hand821a042021-08-10 16:46:43 +090096
97#[cfg(test)]
98mod tests {
Andrew Walbran117cd5e2021-08-13 11:42:13 +000099 use super::*;
Jooyung Hand821a042021-08-10 16:46:43 +0900100 use bytes::{BufMut, BytesMut};
Andrew Walbran117cd5e2021-08-13 11:42:13 +0000101
Jooyung Hand821a042021-08-10 16:46:43 +0900102 #[test]
103 fn test_read_length_prefixed_slice() {
104 let data = b"hello world";
105 let mut b = BytesMut::new();
106 b.put_u32_le(data.len() as u32);
107 b.put_slice(data);
108 let mut slice = b.freeze();
Andrew Walbran117cd5e2021-08-13 11:42:13 +0000109 let res = read_length_prefixed_slice(&mut slice);
Jooyung Hand821a042021-08-10 16:46:43 +0900110 assert!(res.is_ok());
111 assert_eq!(data, res.ok().unwrap().as_ref());
112 }
113}