Skip to main content

arrow_avro/reader/
cursor.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5use crate::errors::AvroError;
6use crate::reader::vlq;
7
8/// A wrapper around a byte slice, providing low-level decoding for Avro
9///
10/// <https://avro.apache.org/docs/1.11.1/specification/#encodings>
11#[derive(Debug)]
12pub(crate) struct AvroCursor<'a> {
13    buf: &'a [u8],
14    start_len: usize,
15}
16
17impl<'a> AvroCursor<'a> {
18    pub(crate) fn new(buf: &'a [u8]) -> Self {
19        Self {
20            buf,
21            start_len: buf.len(),
22        }
23    }
24
25    /// Returns the current cursor position
26    #[inline]
27    pub(crate) fn position(&self) -> usize {
28        self.start_len - self.buf.len()
29    }
30
31    /// Read a single `u8`
32    #[inline]
33    pub(crate) fn get_u8(&mut self) -> Result<u8, AvroError> {
34        match self.buf.first().copied() {
35            Some(x) => {
36                self.buf = &self.buf[1..];
37                Ok(x)
38            }
39            None => Err(AvroError::EOF("Unexpected EOF".to_string())),
40        }
41    }
42
43    #[inline]
44    pub(crate) fn get_bool(&mut self) -> Result<bool, AvroError> {
45        Ok(self.get_u8()? != 0)
46    }
47
48    pub(crate) fn read_vlq(&mut self) -> Result<u64, AvroError> {
49        let (val, offset) = vlq::read_varint(self.buf)
50            .ok_or_else(|| AvroError::ParseError("bad varint".to_string()))?;
51        self.buf = &self.buf[offset..];
52        Ok(val)
53    }
54
55    #[inline]
56    pub(crate) fn get_int(&mut self) -> Result<i32, AvroError> {
57        let varint = self.read_vlq()?;
58        let val: u32 = varint
59            .try_into()
60            .map_err(|_| AvroError::ParseError("varint overflow".to_string()))?;
61        Ok((val >> 1) as i32 ^ -((val & 1) as i32))
62    }
63
64    #[inline]
65    pub(crate) fn get_long(&mut self) -> Result<i64, AvroError> {
66        let val = self.read_vlq()?;
67        Ok((val >> 1) as i64 ^ -((val & 1) as i64))
68    }
69
70    pub(crate) fn get_bytes(&mut self) -> Result<&'a [u8], AvroError> {
71        let len: usize = self
72            .get_long()?
73            .try_into()
74            .map_err(|_| AvroError::ParseError("offset overflow reading avro bytes".to_string()))?;
75
76        if self.buf.len() < len {
77            return Err(AvroError::EOF("Unexpected EOF reading bytes".to_string()));
78        }
79        let ret = &self.buf[..len];
80        self.buf = &self.buf[len..];
81        Ok(ret)
82    }
83
84    #[inline]
85    pub(crate) fn get_float(&mut self) -> Result<f32, AvroError> {
86        if self.buf.len() < 4 {
87            return Err(AvroError::EOF("Unexpected EOF reading float".to_string()));
88        }
89        let ret = f32::from_le_bytes(self.buf[..4].try_into().unwrap());
90        self.buf = &self.buf[4..];
91        Ok(ret)
92    }
93
94    #[inline]
95    pub(crate) fn get_double(&mut self) -> Result<f64, AvroError> {
96        if self.buf.len() < 8 {
97            return Err(AvroError::EOF("Unexpected EOF reading double".to_string()));
98        }
99        let ret = f64::from_le_bytes(self.buf[..8].try_into().unwrap());
100        self.buf = &self.buf[8..];
101        Ok(ret)
102    }
103
104    /// Read exactly `n` bytes from the buffer (e.g. for Avro `fixed`).
105    pub(crate) fn get_fixed(&mut self, n: usize) -> Result<&'a [u8], AvroError> {
106        if self.buf.len() < n {
107            return Err(AvroError::EOF("Unexpected EOF reading fixed".to_string()));
108        }
109        let ret = &self.buf[..n];
110        self.buf = &self.buf[n..];
111        Ok(ret)
112    }
113
114    pub(crate) fn skip_int(&mut self) -> Result<(), AvroError> {
115        let offset = vlq::skip_varint(self.buf)
116            .ok_or_else(|| AvroError::ParseError("bad varint".to_string()))?;
117        // Check if the skipped encoded value would fail a conversion to i32;
118        // skip_varint only cares about fitting in a 64-bit value.
119        match offset {
120            ..5 => {}
121            5 if self.buf[4] < 0x10 => {}
122            _ => return Err(AvroError::ParseError("varint overflow".to_owned())),
123        }
124        self.buf = &self.buf[offset..];
125        Ok(())
126    }
127
128    pub(crate) fn skip_long(&mut self) -> Result<(), AvroError> {
129        let offset = vlq::skip_varint(self.buf)
130            .ok_or_else(|| AvroError::ParseError("bad varint".to_string()))?;
131        // skip_varint invalidates encodings that are out of range for i64,
132        // so we are good.
133        self.buf = &self.buf[offset..];
134        Ok(())
135    }
136}