alox-48 0.7.0

ruby marshal data deserializer
Documentation
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
// Copyright (c) 2024 Lily Lyons
//
// 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/.

// These are necessary evils, sadly.
#![allow(clippy::cast_sign_loss)]
#![allow(clippy::cast_possible_wrap)]
#![allow(clippy::cast_lossless)]

use super::{ignored::Ignored, DeserializeSeed, Error, Kind, Result};
use crate::{tag::Tag, BignumRef, Deserialize, Fixnum, FromPrimitive, Sym, Visitor};

/// The alox-48 deserializer.
#[derive(Debug, Clone)]
pub struct Deserializer<'de> {
    pub(crate) cursor: Cursor<'de>,

    objtable: Vec<usize>,
    stack: Vec<usize>,
    is_reading_instance: bool,

    sym_table: Vec<&'de Sym>,
}

#[derive(Debug, Clone)]
pub(crate) struct Cursor<'de> {
    pub(crate) input: &'de [u8],
    pub(crate) position: usize,
}

struct InstanceAccess<'de, 'a> {
    deserializer: &'a mut Deserializer<'de>,
    // used to track undeserialized data
    len: &'a mut usize,
    index: &'a mut usize,
}

struct IvarAccess<'de, 'a> {
    deserializer: &'a mut Deserializer<'de>,
    len: usize,
    index: &'a mut usize,
    state: MapState,
}

struct ArrayAccess<'de, 'a> {
    deserializer: &'a mut Deserializer<'de>,
    len: usize,
    index: &'a mut usize,
}

struct HashAccess<'de, 'a> {
    deserializer: &'a mut Deserializer<'de>,
    len: usize,
    index: &'a mut usize,
    state: MapState,
}

enum MapState {
    Key,
    Value,
}

impl<'de> Cursor<'de> {
    fn new(input: &'de [u8]) -> Self {
        Self { input, position: 0 }
    }

    fn seek(&mut self, position: usize) {
        self.position = position;
    }

    fn peek_byte(&self) -> Result<u8> {
        self.input
            .get(self.position)
            .copied()
            .ok_or(Error { kind: Kind::Eof })
    }

    fn next_byte(&mut self) -> Result<u8> {
        let byte = self.peek_byte()?;
        self.position += 1;
        Ok(byte)
    }

    fn peek_tag(&self) -> Result<Tag> {
        let byte = self.peek_byte()?;
        Tag::from_u8(byte).ok_or(Error {
            kind: Kind::WrongTag(byte),
        })
    }

    fn next_tag(&mut self) -> Result<Tag> {
        let byte = self.next_byte()?;
        Tag::from_u8(byte).ok_or(Error {
            kind: Kind::WrongTag(byte),
        })
    }

    fn next_bytes_dyn(&mut self, length: usize) -> Result<&'de [u8]> {
        let new_position = self
            .position
            .checked_add(length)
            .ok_or(Error { kind: Kind::Eof })?;
        if new_position > self.input.len() {
            return Err(Error { kind: Kind::Eof });
        }

        let ret = &self.input[self.position..new_position];
        self.position = new_position;
        Ok(ret)
    }
}

impl<'de> Deserializer<'de> {
    /// Create a new deserializer with the given input.
    ///
    /// # Errors
    /// Will error if the input has a len < 1.
    ///
    /// Will error if the input has a version number != to 4.8.
    /// The first two bytes of marshal data encode the version number. [major, minor]
    pub fn new(input: &'de [u8]) -> Result<Self> {
        let mut cursor = Cursor::new(input);
        if input.len() < 2 {
            return Err(Error { kind: Kind::Eof });
        }

        let v1 = cursor.next_byte()?;
        let v2 = cursor.next_byte()?;
        if [v1, v2] != [4, 8] {
            return Err(Error {
                kind: Kind::VersionError([v1, v2]),
            });
        }

        Ok(Self {
            cursor,

            objtable: vec![],
            sym_table: vec![],
            is_reading_instance: false,

            stack: vec![],
        })
    }

    /// Deserialize a value from the input.
    pub fn deserialize_value<T>(&mut self) -> Result<T>
    where
        T: Deserialize<'de>,
    {
        T::deserialize(self)
    }

    /// Returns the current position of the deserializer.
    ///
    /// This is useful for debugging.
    pub fn current_position(&self) -> usize {
        self.cursor.position
    }

    /// Returns the data that the deserializer is reading from.
    ///
    /// This is useful for debugging.
    pub fn data(&self) -> &'de [u8] {
        self.cursor.input
    }

    fn read_fixnum(&mut self) -> Result<Fixnum> {
        // The bounds of a Ruby Marshal packed integer are [-(2**30), 2**30 - 1], anything beyond that
        // gets serialized as a bignum.
        //
        // The bounds of an i32 are [-(2**31), 2**31 - 1], so we should be safe.
        let c = self.cursor.next_byte()? as i8;

        Ok(match c {
            0 => 0i16.into(),
            5..=127 => (c - 5).into(),
            -128..=-5 => (c + 5).into(),
            1..=4 => {
                let mut x = 0;

                for i in 0..c {
                    let n = self.cursor.next_byte()? as i32;
                    let n = n << (8 * i);
                    x |= n;
                }

                FromPrimitive::from_i32(x).unwrap()
            }
            -4..=-1 => {
                let mut x = -1;

                for i in 0..-c {
                    let a = !(0xFF << (8 * i)); // wtf is this magic
                    let b = self.cursor.next_byte()? as i32;
                    let b = b << (8 * i);

                    x = (x & a) | b;
                }

                FromPrimitive::from_i32(x).unwrap()
            }
        })
    }

    fn read_usize(&mut self) -> Result<usize> {
        num_traits::ToPrimitive::to_usize(&self.read_fixnum()?).ok_or(Error { kind: Kind::Eof })
    }

    #[allow(clippy::panic_in_result_fn)]
    fn read_float(&mut self) -> Result<f64> {
        let out = self.read_bytes_len()?;

        if let Some(terminator_idx) = out.iter().position(|v| *v == 0) {
            let (str, [0, mantissa @ ..]) = out.split_at(terminator_idx) else {
                unreachable!();
            };
            let float = str::parse::<f64>(&String::from_utf8_lossy(str)).map_err(|err| Error {
                kind: Kind::Message(err.to_string()),
            })?;
            let transmuted = u64::from_ne_bytes(float.to_ne_bytes());
            if mantissa.len() > 4 {
                return Err(Error {
                    kind: Kind::ParseFloatMantissaTooLong,
                });
            }
            let (mantissa, mask) = mantissa.iter().fold((0u64, 0u64), |(acc, mask), v| {
                ((acc << 8) | u64::from(*v), (mask << 8) | 0xFF)
            });

            let transmuted = (transmuted & !mask) | mantissa;
            Ok(f64::from_ne_bytes(transmuted.to_ne_bytes()))
        } else {
            Ok(
                str::parse::<f64>(&String::from_utf8_lossy(out)).map_err(|err| Error {
                    kind: Kind::Message(err.to_string()),
                })?,
            )
        }
    }

    fn read_symbol(&mut self) -> Result<&'de Sym> {
        let out = self.read_str_len()?;

        let sym = Sym::new(out);

        if self.stack.is_empty() {
            self.sym_table.push(sym);
        }
        Ok(sym)
    }

    fn read_symlink(&mut self) -> Result<&'de Sym> {
        let index = self.read_usize()?;

        self.sym_table.get(index).copied().ok_or(Error {
            kind: Kind::UnresolvedSymlink(index),
        })
    }

    // FIXME: FIND BETTER NAME
    fn read_symbol_either(&mut self) -> Result<&'de Sym> {
        match self.cursor.next_tag()? {
            Tag::Symbol => self.read_symbol(),
            Tag::Symlink => self.read_symlink(),
            t => Err(Error {
                kind: Kind::ExpectedSymbol(t),
            }),
        }
    }

    fn register_obj(&mut self) {
        // Only push into the object table if we are reading new input
        // also don't push if we're reading an instance (ruby moment)
        if !self.stack.is_empty() || self.is_reading_instance {
            self.is_reading_instance = false; // since we only want to skip reading the instance data, we can reset this here
            return;
        }
        self.objtable.push(self.cursor.position);
    }

    fn read_bytes_len(&mut self) -> Result<&'de [u8]> {
        let len = self.read_usize()?;
        self.cursor.next_bytes_dyn(len)
    }

    fn read_str_len(&mut self) -> Result<&'de str> {
        let len = self.read_usize()?;
        let bytes = self.cursor.next_bytes_dyn(len)?;

        std::str::from_utf8(bytes).map_err(|e| Error {
            kind: Kind::SymbolInvalidUTF8(e),
        })
    }
}

impl<'de> super::DeserializerTrait<'de> for &mut Deserializer<'de> {
    // This is just barely over the limit.
    // It's fine, I swear.
    #[allow(clippy::too_many_lines)]
    fn deserialize<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        if self.cursor.peek_tag()?.is_object_link_referenceable() {
            self.register_obj();
        }

        match self.cursor.next_tag()? {
            Tag::Nil => visitor.visit_nil(),
            Tag::True => visitor.visit_bool(true),
            Tag::False => visitor.visit_bool(false),
            Tag::Fixnum => visitor.visit_fixnum(self.read_fixnum()?),
            Tag::Float => visitor.visit_f64(self.read_float()?),
            Tag::Bignum => {
                let is_negative = self.cursor.next_byte()? == b'-';
                let len = self
                    .read_usize()?
                    .checked_mul(2)
                    .ok_or(Error { kind: Kind::Eof })?;
                let le_bytes = self.cursor.next_bytes_dyn(len)?;
                if let Some(bignum) = BignumRef::from_le_bytes(is_negative, le_bytes) {
                    visitor.visit_bignum(bignum)
                } else {
                    visitor.visit_fixnum(Fixnum::from_le_bytes(is_negative, le_bytes).unwrap())
                }
            }
            Tag::String => {
                let data = self.read_bytes_len()?;
                visitor.visit_string(data)
            }
            Tag::Array => {
                let len = self.read_usize()?;
                let mut index = 0;

                let result = visitor.visit_array(ArrayAccess {
                    deserializer: self,
                    len,
                    index: &mut index,
                })?;

                // Deserialize remaining elements that weren't deserialized
                while index < len {
                    index += 1;
                    Ignored::deserialize(&mut *self)?;
                }

                Ok(result)
            }
            Tag::Hash => {
                let len = self.read_usize()?;
                let mut index = 0;

                let result = visitor.visit_hash(HashAccess {
                    deserializer: self,
                    len,
                    index: &mut index,
                    state: MapState::Value, // we want to enforce getting a key next so we set the state to value
                })?;

                // Deserialize remaining elements that weren't deserialized
                while index < len {
                    index += 1;
                    // Key
                    Ignored::deserialize(&mut *self)?;
                    // Value
                    Ignored::deserialize(&mut *self)?;
                }

                Ok(result)
            }
            Tag::Symbol => visitor.visit_symbol(self.read_symbol()?),
            Tag::Symlink => visitor.visit_symbol(self.read_symlink()?),
            // Instance genuinely baffles me.
            Tag::Instance => {
                self.is_reading_instance = true;

                let mut len = 0;
                let mut index = 0;

                let result = visitor.visit_instance(&mut InstanceAccess {
                    deserializer: &mut *self,
                    len: &mut len,
                    index: &mut index,
                })?;

                while index < len {
                    index += 1;
                    // Ivar
                    self.read_symbol_either()?;
                    // Value
                    Ignored::deserialize(&mut *self)?;
                }

                Ok(result)
            }
            Tag::Object => {
                let class = self.read_symbol_either()?;

                let len = self.read_usize()?;
                let mut index = 0;

                let result = visitor.visit_object(
                    class,
                    IvarAccess {
                        deserializer: self,
                        len,
                        index: &mut index,
                        state: MapState::Value, // we want to enforce getting a key next so we set the state to value
                    },
                )?;

                // Deserialize remaining elements that weren't deserialized
                while index < len {
                    index += 1;
                    // Ivar
                    self.read_symbol_either()?;
                    // Value
                    Ignored::deserialize(&mut *self)?;
                }

                Ok(result)
            }
            Tag::ObjectLink => {
                let index = self.read_usize()?;

                let jump_target = self.objtable.get(index).copied().ok_or(Error {
                    kind: Kind::UnresolvedObjectlink(index),
                })?;

                if self.stack.contains(&self.cursor.position) {
                    return Err(Error {
                        kind: Kind::CircularReference,
                    });
                }

                self.stack.push(self.cursor.position);
                self.cursor.seek(jump_target);

                let result = self.deserialize(visitor);

                self.cursor
                    .seek(self.stack.pop().expect("stack should not empty"));

                result
            }
            Tag::UserDef => {
                let class = self.read_symbol_either()?;
                let data = self.read_bytes_len()?;

                visitor.visit_user_data(class, data)
            }
            // FIXME: this ignores default hash values. we should fix this?
            Tag::HashDefault => {
                let len = self.read_usize()?;
                let mut index = 0;

                let result = visitor.visit_hash(HashAccess {
                    deserializer: self,
                    len,
                    index: &mut index,
                    state: MapState::Value, // we want to enforce getting a key next so we set the state to value
                });

                // Deserialize remaining elements that weren't deserialized
                while index < len {
                    index += 1;
                    // Key
                    Ignored::deserialize(&mut *self)?;
                    // Value
                    Ignored::deserialize(&mut *self)?;
                }

                // Ignore the default value.
                // This should work.
                // Probably.
                // :)
                Ignored::deserialize(&mut *self)?;

                result
            }
            Tag::UserClass => {
                let class = self.read_symbol_either()?;
                visitor.visit_user_class(class, &mut *self)
            }
            Tag::RawRegexp => {
                let regex = self.read_bytes_len()?;
                let flags = self.cursor.next_byte()?;
                visitor.visit_regular_expression(regex, flags)
            }
            Tag::ClassRef => {
                // In my testing this isn't a symbol. How strange!
                let class = self.read_str_len()?;

                visitor.visit_class(Sym::new(class))
            }
            Tag::ModuleRef => {
                let module = self.read_str_len()?;

                visitor.visit_module(Sym::new(module))
            }
            // the ruby docs are wrong about this actually!
            // they say the object comes first, then the module, but actually it's the other way around.
            Tag::Extended => {
                let module = self.read_symbol_either()?;
                visitor.visit_extended(module, &mut *self)
            }
            Tag::UserMarshal => {
                let class = self.read_symbol_either()?;
                visitor.visit_user_marshal(class, &mut *self)
            }
            Tag::Struct => {
                let name = self.read_symbol_either()?;

                let len = self.read_usize()?;
                let mut index = 0;

                let result = visitor.visit_struct(
                    name,
                    IvarAccess {
                        deserializer: self,
                        len,
                        index: &mut index,
                        state: MapState::Value, // we want to enforce getting a key next so we set the state to value
                    },
                )?;

                // Deserialize remaining elements that weren't deserialized
                while index < len {
                    index += 1;
                    // Member
                    self.read_symbol_either()?;
                    // Value
                    Ignored::deserialize(&mut *self)?;
                }

                Ok(result)
            }
            // I'm not sure why this exists. The ruby marshal doc mentions that it's for types from C extensions,
            // But Data is functionally identical to UserMarshal.
            Tag::Data => {
                let class = self.read_symbol_either()?;
                visitor.visit_data(class, &mut *self)
            }
        }
    }

    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
    where
        V: super::traits::VisitorOption<'de>,
    {
        if self.cursor.peek_tag()? == Tag::Nil {
            self.cursor.next_byte()?;
            visitor.visit_none()
        } else {
            visitor.visit_some(self)
        }
    }

    fn deserialize_instance<V>(self, visitor: V) -> Result<V::Value>
    where
        V: super::traits::VisitorInstance<'de>,
    {
        if self.cursor.peek_tag()? == Tag::Instance {
            self.register_obj(); // we need to register the object before we start reading it
            self.is_reading_instance = true; // also need to remember NOT to push into the object table

            self.cursor.next_byte()?;

            let mut len = 0;
            let mut index = 0;

            let result = visitor.visit_instance(&mut InstanceAccess {
                deserializer: &mut *self,
                len: &mut len,
                index: &mut index,
            })?;

            while index < len {
                index += 1;
                // Ivar
                self.read_symbol_either()?;
                // Value
                Ignored::deserialize(&mut *self)?;
            }

            Ok(result)
        } else {
            visitor.visit(self)
        }
    }
}

impl<'de, 'a> super::InstanceAccess<'de> for &'a mut InstanceAccess<'de, 'a> {
    type IvarAccess = IvarAccess<'de, 'a>;

    fn value_seed<V>(self, seed: V) -> Result<(V::Value, Self::IvarAccess)>
    where
        V: DeserializeSeed<'de>,
    {
        let result = seed.deserialize(&mut *self.deserializer)?;

        let len = self.deserializer.read_usize()?;
        *self.len = len;

        Ok((
            result,
            IvarAccess {
                deserializer: &mut *self.deserializer,
                len,
                index: self.index,
                state: MapState::Value, // we want to enforce getting a key next so we set the state to value
            },
        ))
    }
}

impl<'de> super::IvarAccess<'de> for IvarAccess<'de, '_> {
    fn next_ivar(&mut self) -> Result<Option<&'de Sym>> {
        if *self.index >= self.len {
            return Ok(None);
        }

        match self.state {
            MapState::Key => {
                return Err(Error {
                    kind: Kind::KeyAfterKey,
                })
            }
            MapState::Value => self.state = MapState::Key,
        }

        *self.index += 1;

        self.deserializer.read_symbol_either().map(Some)
    }

    fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
    where
        V: DeserializeSeed<'de>,
    {
        match self.state {
            MapState::Value => {
                return Err(Error {
                    kind: Kind::ValueAfterValue,
                })
            }
            MapState::Key => self.state = MapState::Value,
        }

        seed.deserialize(&mut *self.deserializer)
    }

    fn len(&self) -> usize {
        self.len
    }

    fn index(&self) -> usize {
        *self.index
    }
}

impl<'de> super::ArrayAccess<'de> for ArrayAccess<'de, '_> {
    fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
    where
        T: DeserializeSeed<'de>,
    {
        if *self.index >= self.len {
            return Ok(None);
        }
        *self.index += 1;

        seed.deserialize(&mut *self.deserializer).map(Some)
    }

    fn len(&self) -> usize {
        self.len
    }

    fn index(&self) -> usize {
        *self.index
    }
}

impl<'de> super::HashAccess<'de> for HashAccess<'de, '_> {
    fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
    where
        K: DeserializeSeed<'de>,
    {
        if *self.index >= self.len {
            return Ok(None);
        }

        match self.state {
            MapState::Key => {
                return Err(Error {
                    kind: Kind::KeyAfterKey,
                })
            }
            MapState::Value => self.state = MapState::Key,
        }

        *self.index += 1;

        seed.deserialize(&mut *self.deserializer).map(Some)
    }

    fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
    where
        V: DeserializeSeed<'de>,
    {
        match self.state {
            MapState::Value => {
                return Err(Error {
                    kind: Kind::ValueAfterValue,
                })
            }
            MapState::Key => self.state = MapState::Value,
        }

        seed.deserialize(&mut *self.deserializer)
    }

    fn len(&self) -> usize {
        self.len
    }

    fn index(&self) -> usize {
        *self.index
    }
}