1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright Anton Sol
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use std::fmt::{self, Debug};

use super::*;
use crate::utils::none;

// starting from the first pkt_byte, if the type is keypoint, this is the offset for the inner linkpoint pkt
pub(crate) const LINKPOINT_IN_KEYPOINT_OFFSET: usize = size_of::<PointHeader>()
    + size_of::<KeyPointHeader>()
    - size_of::<PointHeader>()
    - size_of::<LinkPointHeader>();

/// Flat [u8] repr of a [Point]
#[repr(C, align(4))]
pub struct PointPtr {
    pub(crate) pkt_header: PointHeader,
    content: [u8],
}
#[repr(C)]
pub struct PointThinPtr(pub(crate) PointHeader);

impl Debug for PointThinPtr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("PointThin").field(&self.as_sized()).finish()
    }
}
impl PointThinPtr {
    //pub(crate) const EMPTY : Self = PointThinPtr(PointHeader::ERROR);
    #[inline(always)]
    pub fn point_header(&self) -> &PointHeader {
        &self.0
    }
    #[inline(always)]
    pub fn as_sized(&self) -> &PointPtr {
        unsafe {
            &*(std::ptr::from_raw_parts(
                self as *const PointThinPtr as *const (),
                self.0.content_size(),
            ))
        }
    }
}

impl fmt::Debug for PointPtr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let PointParts { pkt_header, fields } = self.parts();
        f.debug_struct("PktPtr")
            .field("pkt_header", &pkt_header)
            .field("fields", &fields)
            .finish()
    }
}

impl PointPtr {
    #[inline(always)]
    pub fn thin_point(&self) -> &PointThinPtr {
        unsafe { &*((&self.pkt_header) as *const PointHeader as *const PointThinPtr) }
    }
    /// # Safety
    ///
    /// should be well aligned valid netpktbytes.
    pub unsafe fn from_bytes_unchecked(b: &[u8]) -> &PointPtr {
        PointThinPtr::from_bytes_unchecked(b).as_sized()
    }
    pub fn internal_consitent_length(&self) -> Result<usize, Error> {
        self.thin_point().internal_consitent_length()
    }
    #[inline(always)]
    pub fn pkt_bytes(&self) -> &[u8] {
        unsafe {
            core::slice::from_raw_parts(
                self as *const Self as *const u8,
                self.pkt_header.point_size(),
            )
        }
    }
}
impl PointThinPtr {
    /// # Safety
    ///
    /// This is a unchecked cast, meaning accessing fields is dangerous
    /// to validate, first ensure you have enough bytes to read a PktHeader,
    /// Then call internal_consistent_length and compare the lengths
    /// finally call check_signature to ensure if its an assert pkt that its valid.
    pub unsafe fn from_bytes_unchecked(b: &[u8]) -> &PointThinPtr {
        assert!(b.len() >= size_of::<PointHeader>(), "Never gone work");
        assert!(b.as_ptr().align_offset(4) == 0, "Unaligned cast");
        &*(b.as_ptr() as *const PointThinPtr)
    }
    #[inline(always)]
    fn fixed(&self) -> BarePointFields {
        let mem = &self.content_memory();
        match self.0.point_type {
            PointTypeFlags::DATA_POINT => BarePointFields::DataPoint(mem),
            PointTypeFlags::LINK_POINT => {
                let (linkpoint, tail) = mem.split_at(size_of::<LinkPointHeader>());
                let linkpoint = unsafe { &*(linkpoint.as_ptr() as *const LinkPointHeader) };
                BarePointFields::LinkPoint {
                    lp_header: linkpoint,
                    tail,
                }
            }
            PointTypeFlags::KEY_POINT => {
                let (assert, tail) = mem.split_at(size_of::<KeyPointHeader>());
                let assert = unsafe { &*(assert.as_ptr() as *const KeyPointHeader) };
                BarePointFields::KeyPoint {
                    a_header: assert,
                    tail,
                }
            }
            PointTypeFlags::ERROR_POINT => BarePointFields::Error(mem),
            _ => panic!("Working with invalid packets."),
        }
    }

    /// Check the header length fields and return the point size. This is the length without the hash.
    #[inline(always)]
    pub fn internal_consitent_length(&self) -> Result<usize, Error> {
        self.point_header().check()?;
        let point_size = self.point_header().point_size();
        match self.fixed() {
            BarePointFields::DataPoint(_) => (),
            BarePointFields::LinkPoint { lp_header, tail: _ } => {
                let isp_offset = lp_header.info.offset_ipath.get();
                let data_offset = lp_header.info.offset_data.get();
                if data_offset as usize > point_size {
                    return Err(Error::DataOffsetIncompatible);
                }
                if isp_offset > data_offset {
                    return Err(Error::DataOffsetIncompatible);
                }
                let link_size = isp_offset
                    .checked_sub((size_of::<PointHeader>() + size_of::<LinkPointHeader>()) as u16)
                    .ok_or(Error::ISPOffsetIncompatible)?;
                if link_size % size_of::<Link>() as u16 != 0 {
                    return Err(Error::IndivisableLinkbytes);
                }
                unsafe { self.unchecked_linkpoint_tail() }
                    .ipath
                    .check_components()?;
            }
            BarePointFields::KeyPoint {
                a_header: assert,
                tail: _,
            } => {
                let inner_linkpoint_size = self.point_header().content_size()
                    - size_of::<KeyPointPadding>()
                    - size_of::<PubKey>()
                    - size_of::<Signature>();
                if inner_linkpoint_size < MIN_LINKPOINT_SIZE {
                    return Err(Error::KeyPointLength);
                }
                if assert.inner_point.point_type != PointTypeFlags::LINK_POINT {
                    return Err(Error::SignedInvalidPkt);
                }
                let inner_lp_bytes = &self.pkt_bytes()[LINKPOINT_IN_KEYPOINT_OFFSET..];
                let linkpoint = unsafe { PointThinPtr::from_bytes_unchecked(inner_lp_bytes) };
                let lp_size = linkpoint.internal_consitent_length()?;
                if inner_linkpoint_size != lp_size + size_of::<LkHash>() {
                    return Err(Error::KeyPointLength);
                }
            }
            BarePointFields::Error(_) => (),
            BarePointFields::Unknown(_) => (),
        };
        Ok(point_size)
    }

    pub fn check_signature(&self) -> Result<(), Error> {
        if let BarePointFields::KeyPoint {
            a_header: assert,
            tail: _,
        } = self.fixed()
        {
            if assert.reserved != KeyPointPadding::default() {
                return Err(Error::ReservedBitsSet);
            }
            let inner_ptr = &assert.inner_point as *const PointHeader;
            let inner_linkpoint: &PointPtr = unsafe {
                &*std::ptr::from_raw_parts(
                    inner_ptr as *const (),
                    assert.inner_point.content_size(),
                )
            };
            let hash = inner_linkpoint.compute_hash();
            if hash != assert.signed.linkpoint_hash {
                return Err(Error::HashMismatch);
            }
            assert
                .signed
                .validate()
                .map_err(|_| Error::InvalidSignature)?;
        };
        Ok(())
    }
    fn self_ptr(&self) -> *const u8 {
        self as *const Self as *const u8
    }
    pub fn pkt_bytes(&self) -> &[u8] {
        unsafe {
            core::slice::from_raw_parts(
                self as *const Self as *const u8,
                self.point_header().point_size(),
            )
        }
    }

    fn content_memory(&self) -> &[u8] {
        &self.pkt_bytes()[size_of::<PointHeader>()..]
    }

    #[inline(always)]
    fn map_fixed<'a, A: 'a>(
        &'a self,
        onunknown: impl FnOnce(&'a ()) -> A,
        ondata: impl FnOnce(&'a ()) -> A,
        onlinkpoint: impl FnOnce(&'a LinkPointHeader) -> A,
        onassert: impl FnOnce(&'a KeyPointHeader) -> A,
        onerror: impl FnOnce(&'a ()) -> A,
    ) -> A {
        match self.fixed() {
            BarePointFields::DataPoint(_) => ondata(&()),
            BarePointFields::LinkPoint {
                lp_header: linkpoint,
                tail: _,
            } => onlinkpoint(linkpoint),
            BarePointFields::KeyPoint {
                a_header: assert,
                tail: _,
            } => onassert(assert),
            BarePointFields::Error(_) => onerror(&()),
            BarePointFields::Unknown(_) => onunknown(&()),
        }
    }

    #[inline(always)]
    unsafe fn unchecked_assert_tail(&self) -> Tail {
        let inner_linkpoint =
            self.self_ptr().add(LINKPOINT_IN_KEYPOINT_OFFSET) as *const PointThinPtr;
        (*inner_linkpoint).unchecked_linkpoint_tail()
    }
    #[inline(always)]
    unsafe fn unchecked_linkpoint_tail(&self) -> Tail {
        let ptr = self.self_ptr();
        let p = &*{ ptr.add(size_of::<PointHeader>()) as *const LinkPointHeader };
        let start = size_of::<PointHeader>() + size_of::<LinkPointHeader>();
        let is_offset = p.info.offset_ipath.get() as usize;
        let data_offset = p.info.offset_data.get() as usize;
        let size = self.point_header().point_size();
        //assert!(start <= is_offset && is_offset <= data_offset && data_offset <= size , "bad sizes");
        let links: &[Link] = core::slice::from_ptr_range(
            ptr.add(start) as *const Link..ptr.add(is_offset) as *const Link,
        );
        let isp_bytes = core::slice::from_ptr_range(ptr.add(is_offset)..ptr.add(data_offset));
        let data = core::slice::from_ptr_range(ptr.add(data_offset)..ptr.add(size));
        let spath_idx = IPath::from_unchecked(isp_bytes);
        debug_assert!(spath_idx.check_components().is_ok(), "{spath_idx:?}");
        Tail {
            ipath: spath_idx,
            data,
            links,
        }
    }
}

impl Point for PointThinPtr {
    #[inline(always)]
    fn data(&self) -> &[u8] {
        self.parts().data_ptr()
    }
    #[inline(always)]
    fn tail(&self) -> Option<Tail> {
        match self.parts().fields {
            PointFields::LinkPoint(LinkPoint { head: _, tail }) => Some(tail),
            PointFields::KeyPoint(KeyPoint { head: _, tail }) => Some(tail),
            _ => None,
        }
    }
    #[inline(always)]
    fn keypoint_header(&self) -> Option<&KeyPointHeader> {
        self.map_fixed(none, none, none, Some, none)
    }
    #[inline(always)]
    fn linkpoint_header(&self) -> Option<&LinkPointHeader> {
        self.map_fixed(none, none, Some, |a| Some(&a.linkpoint), none)
    }
    #[inline(always)]
    fn parts(&self) -> PointParts {
        let fields = match self.point_header().point_type {
            PointTypeFlags::DATA_POINT => PointFields::DataPoint(self.content_memory()),
            PointTypeFlags::LINK_POINT => {
                let head = unsafe { &*(self.content_memory().as_ptr() as *const LinkPointHeader) };
                let tail = unsafe { self.unchecked_linkpoint_tail() };
                PointFields::LinkPoint(LinkPoint { head: *head, tail })
            }
            PointTypeFlags::KEY_POINT => {
                let head = unsafe { &*(self.content_memory().as_ptr() as *const KeyPointHeader) };
                let tail = unsafe { self.unchecked_assert_tail() };
                PointFields::KeyPoint(KeyPoint { head: *head, tail })
            }
            _ => PointFields::Error(self.content_memory()),
        };
        PointParts {
            pkt_header: *self.point_header(),
            fields,
        }
    }

    #[inline(always)]
    fn point_header_ref(&self) -> &PointHeader {
        self.point_header()
    }

    #[inline(always)]
    fn pkt_segments(&self) -> ByteSegments {
        ByteSegments::from_array([self.pkt_bytes()])
    }
}

impl Point for PointPtr {
    #[inline(always)]
    fn data(&self) -> &[u8] {
        self.thin_point().data()
    }
    #[inline(always)]
    fn tail(&self) -> Option<Tail> {
        self.thin_point().tail()
    }
    #[inline(always)]
    fn keypoint_header(&self) -> Option<&KeyPointHeader> {
        self.thin_point().map_fixed(none, none, none, Some, none)
    }
    #[inline(always)]
    fn linkpoint_header(&self) -> Option<&LinkPointHeader> {
        self.thin_point().linkpoint_header()
    }
    #[inline(always)]
    fn parts(&self) -> PointParts {
        self.thin_point().parts()
    }

    #[inline(always)]
    fn point_header_ref(&self) -> &PointHeader {
        &self.pkt_header
    }

    #[inline(always)]
    fn pkt_segments(&self) -> ByteSegments {
        ByteSegments::from_array([self.pkt_bytes()])
    }
}

#[allow(dead_code)]
#[repr(align(4))]
enum BarePointFields<'o> {
    DataPoint(&'o [u8]),
    LinkPoint {
        lp_header: &'o LinkPointHeader,
        tail: &'o [u8],
    },
    KeyPoint {
        a_header: &'o KeyPointHeader,
        tail: &'o [u8],
    },
    Error(&'o [u8]),
    Unknown(&'o [u8]),
}