binary_cookies/decode/
cookies.rs1use std::ops::Deref;
2
3use oval::Buffer;
4use winnow::{
5 stream::{Offset, Stream},
6 Parser,
7};
8
9use super::{DecodeResult, OffsetSize};
10use crate::{cookie::Cookie, decode::StreamIn, error::Result};
11
12#[derive(Clone)]
13#[derive(Debug)]
14#[derive(Default)]
15#[derive(PartialEq, Eq, PartialOrd, Ord)]
16pub struct CookiesOffsetInPage(pub(crate) Vec<u32>);
17
18impl Deref for CookiesOffsetInPage {
19 type Target = [u32];
20
21 fn deref(&self) -> &Self::Target {
22 &self.0
23 }
24}
25
26#[derive(Clone)]
27#[derive(Debug)]
28#[derive(PartialEq, Eq, PartialOrd, Ord)]
29pub struct CookiesOffset {
30 pub(crate) offset_sizes: Vec<OffsetSize>,
31}
32
33impl CookiesOffset {
34 #[cfg(any(feature = "sync", feature = "tokio"))]
35 pub(crate) fn new(page_offset: u64, page_size: u32, cookies_offset: &[u32]) -> Self {
37 let mut prev_cookie_end = page_size;
38
39 let offset_sizes: Vec<OffsetSize> = cookies_offset
40 .iter()
41 .rev()
42 .map(|&offset_in_page| {
43 let res = OffsetSize {
44 offset: offset_in_page as u64 + page_offset,
45 size: prev_cookie_end - offset_in_page,
46 };
47 prev_cookie_end = offset_in_page;
48 res
49 })
50 .collect();
51
52 Self { offset_sizes }
53 }
54}
55
56#[derive(Clone)]
57#[derive(Debug)]
58pub struct CookieFsm {
59 pub(crate) buffer: Buffer,
60}
61
62impl Default for CookieFsm {
63 fn default() -> Self {
64 Self::new()
65 }
66}
67
68impl CookieFsm {
69 const BUF_SIZE: usize = 4;
71
72 pub fn new() -> Self {
73 let buffer = Buffer::with_capacity(Self::BUF_SIZE);
74 Self { buffer }
75 }
76
77 pub fn with_capacity(size: usize) -> Self {
78 let buffer = Buffer::with_capacity(size);
79 Self { buffer }
80 }
81
82 pub fn process(mut self) -> Result<DecodeResult<Self, (Cookie, Buffer)>> {
83 let mut input: StreamIn = StreamIn::new(self.buffer.data());
84 let start = input.checkpoint();
85
86 let e = match Cookie::decode.parse_next(&mut input) {
87 Ok(o) => {
88 let consumed = input.offset_from(&start);
89 self.buffer.consume(consumed);
90 return Ok(DecodeResult::Done((o, self.buffer)));
91 },
92 Err(e) => e,
93 };
94
95 mode_err! {self, e, self.buffer}
96 }
97}