Skip to main content

rustorb_cdr/
value.rs

1//! Valuetype tags, sharing and chunking (CORBA 3.4 part 2, 9.3.4).
2//!
3//! A value is written as a value tag (`0x7fffff00` plus flags), an
4//! optional codebase URL, optional repository ids and its state. `null`
5//! is the tag `0`, and a value written before in the same stream is the
6//! tag `0xffffffff` followed by the offset back to its value tag.
7//!
8//! A chunked value splits its state into chunks, each a positive length
9//! followed by that many octets, and ends with an end tag: the negated
10//! nesting depth. A reader that does not know a truncatable value's type
11//! skips the chunks and nested values it cannot read, up to the end tag.
12//!
13//! Positions are buffer indexes, so a message joined from fragments keeps
14//! its indirections even when the stream offset restarts.
15
16use std::collections::HashMap;
17
18use crate::{Decoder, Encoder, Error, Result};
19
20const VALUE_TAG: u32 = 0x7fff_ff00;
21const CODEBASE: u32 = 0x01;
22const SINGLE_ID: u32 = 0x02;
23const ID_LIST: u32 = 0x06;
24const CHUNKED: u32 = 0x08;
25const INDIRECTION: u32 = 0xffff_ffff;
26
27/// What [`Decoder::read_value_header`] found where a value was expected.
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub enum ValueHeader {
30    /// A `null` value.
31    Null,
32    /// A value read earlier in the stream, by its slot.
33    Indirection(usize),
34    /// A new value, whose state follows. The caller reads the state and
35    /// then calls [`Decoder::end_value`].
36    Value {
37        /// The number of this value in the stream, counting from 0, which
38        /// later indirections refer to.
39        slot: usize,
40        /// The repository ids sent, most derived first; empty when the
41        /// sender left the type to the receiver.
42        ids: Vec<String>,
43        /// Whether the state is chunked.
44        chunked: bool,
45    },
46}
47
48/// Writing state kept in an [`Encoder`] once a value has been written.
49#[derive(Debug, Clone, Default)]
50pub(crate) struct WriteState {
51    /// Buffer index of the value tag of each value written, by key.
52    written: HashMap<u64, usize>,
53    /// Whether each value being written is chunked, outermost first.
54    frames: Vec<bool>,
55    /// Buffer index of the length of the open chunk.
56    chunk: Option<usize>,
57}
58
59/// Reading state kept in a [`Decoder`] once a value has been read.
60#[derive(Debug, Clone, Default)]
61pub(crate) struct ReadState {
62    /// Slot of each value tag read, by buffer index.
63    tags: HashMap<usize, usize>,
64    /// Repository ids and codebase URLs read, by buffer index of their
65    /// length, for indirections to them.
66    strings: HashMap<usize, String>,
67    /// Lists of repository ids read, by buffer index of their count.
68    lists: HashMap<usize, Vec<String>>,
69    /// Values read so far.
70    slots: usize,
71    /// The values being read, outermost first.
72    frames: Vec<Frame>,
73    /// Buffer index of the end of the chunk being read.
74    chunk_end: Option<usize>,
75    /// The depth of the values an end tag has already ended, or `0`.
76    ended: usize,
77}
78
79#[derive(Debug, Clone, Copy)]
80struct Frame {
81    chunked: bool,
82    /// The nesting depth, from 1.
83    depth: usize,
84}
85
86impl Encoder {
87    /// The number of values written so far (not counting indirections):
88    /// the slot a reader gives the next new value.
89    pub fn value_count(&self) -> usize {
90        self.values.as_ref().map_or(0, |s| s.written.len())
91    }
92
93    /// Writes a `null` value.
94    pub fn write_null_value(&mut self) {
95        self.write_u32(0);
96    }
97
98    /// Starts writing a value.
99    ///
100    /// `key` identifies the value: if a value with the same key was
101    /// written before in this stream, an indirection to it is written and
102    /// `false` is returned. Otherwise the value tag and `ids` (repository
103    /// ids, most derived first; none leaves the type to the receiver) are
104    /// written and `true` is returned: the caller writes the state and
105    /// then calls [`Encoder::end_value`].
106    ///
107    /// A value nested in a chunked value is chunked as well.
108    pub fn begin_value(&mut self, key: u64, ids: &[&str], chunked: bool) -> Result<bool> {
109        // Chunk lengths and indirections depend on stream offsets.
110        self.pin();
111        let state = self.values.get_or_insert_with(Default::default);
112        if let Some(&tag_at) = state.written.get(&key) {
113            self.write_u32(INDIRECTION);
114            let at = self.buf.len();
115            let offset = i32::try_from(tag_at as i64 - at as i64)
116                .map_err(|_| Error::LengthOverflow(at - tag_at))?;
117            self.write_i32(offset);
118            return Ok(false);
119        }
120        let chunked = chunked || state.frames.last() == Some(&true);
121        self.close_chunk();
122        let mut tag = VALUE_TAG;
123        match ids.len() {
124            0 => {}
125            1 => tag |= SINGLE_ID,
126            _ => tag |= ID_LIST,
127        }
128        if chunked {
129            tag |= CHUNKED;
130        }
131        self.align(4);
132        let tag_at = self.buf.len();
133        self.write_u32(tag);
134        if ids.len() > 1 {
135            self.write_len(ids.len())?;
136        }
137        for id in ids {
138            self.write_string(id)?;
139        }
140        let state = self.values.as_mut().expect("made above");
141        state.written.insert(key, tag_at);
142        state.frames.push(chunked);
143        if chunked {
144            self.open_chunk();
145        }
146        Ok(true)
147    }
148
149    /// Ends the value started by the last [`Encoder::begin_value`] that
150    /// returned `true`.
151    pub fn end_value(&mut self) -> Result<()> {
152        let state = self
153            .values
154            .as_mut()
155            .ok_or(Error::InvalidValue("end_value without begin_value"))?;
156        let depth = state.frames.len();
157        let chunked = state
158            .frames
159            .pop()
160            .ok_or(Error::InvalidValue("end_value without begin_value"))?;
161        let outer_chunked = state.frames.last() == Some(&true);
162        if chunked {
163            self.close_chunk();
164            let depth = i32::try_from(depth).map_err(|_| Error::LengthOverflow(depth))?;
165            self.write_i32(-depth);
166            if outer_chunked {
167                self.open_chunk();
168            }
169        }
170        Ok(())
171    }
172
173    /// Starts a chunk, whose length is filled in when it is closed.
174    fn open_chunk(&mut self) {
175        self.align(4);
176        let at = self.buf.len();
177        self.write_u32(0);
178        if let Some(state) = self.values.as_mut() {
179            state.chunk = Some(at);
180        }
181    }
182
183    /// Fills in the length of the open chunk, or drops it if it is empty.
184    fn close_chunk(&mut self) {
185        let Some(at) = self.values.as_mut().and_then(|s| s.chunk.take()) else {
186            return;
187        };
188        let len = self.buf.len() - (at + 4);
189        if len == 0 {
190            self.buf.truncate(at);
191        } else {
192            let len = len as u32;
193            let bytes = match self.order {
194                crate::ByteOrder::Big => len.to_be_bytes(),
195                crate::ByteOrder::Little => len.to_le_bytes(),
196            };
197            self.buf[at..at + 4].copy_from_slice(&bytes);
198        }
199    }
200}
201
202impl Decoder<'_> {
203    /// Reads what stands where a value is expected: `null`, an indirection
204    /// to a value read before, or the header of a new value.
205    ///
206    /// After [`ValueHeader::Value`], the caller reads the state and then
207    /// calls [`Decoder::end_value`].
208    pub fn read_value_header(&mut self) -> Result<ValueHeader> {
209        let state = self.values.get_or_insert_with(Default::default);
210        let outer_chunked = state.frames.last().is_some_and(|f| f.chunked);
211        let (at, tag, in_chunk) = self.read_value_tag(outer_chunked)?;
212        match tag {
213            0 => Ok(ValueHeader::Null),
214            INDIRECTION => {
215                let offset_at = self.pos;
216                let offset = if in_chunk {
217                    self.read_i32()?
218                } else {
219                    self.read_u32_raw()? as i32
220                };
221                let target = offset_at as i64 + i64::from(offset);
222                let slot = usize::try_from(target)
223                    .ok()
224                    .and_then(|target| self.state().tags.get(&target).copied())
225                    .ok_or(Error::InvalidValue("indirection to no value"))?;
226                Ok(ValueHeader::Indirection(slot))
227            }
228            tag if tag & 0xffff_fff0 == VALUE_TAG => {
229                if in_chunk {
230                    return Err(Error::InvalidValue("value tag inside a chunk"));
231                }
232                let chunked = tag & CHUNKED != 0;
233                if outer_chunked && !chunked {
234                    return Err(Error::InvalidValue("unchunked value in a chunked value"));
235                }
236                if tag & CODEBASE != 0 {
237                    self.read_shared_string()?;
238                }
239                let ids = match tag & ID_LIST {
240                    0 => Vec::new(),
241                    SINGLE_ID => vec![self.read_shared_string()?],
242                    ID_LIST => self.read_id_list()?,
243                    _ => return Err(Error::InvalidValue("invalid value tag")),
244                };
245                let state = self.state();
246                let slot = state.slots;
247                state.slots += 1;
248                state.tags.insert(at, slot);
249                let depth = state.frames.len() + 1;
250                state.frames.push(Frame { chunked, depth });
251                state.chunk_end = None;
252                Ok(ValueHeader::Value { slot, ids, chunked })
253            }
254            _ => Err(Error::InvalidValue("invalid value tag")),
255        }
256    }
257
258    /// Ends the value whose header was read last and is not yet ended.
259    ///
260    /// For a chunked value, the rest of its state (chunks and nested
261    /// values a truncating reader did not read) is skipped up to its end
262    /// tag.
263    pub fn end_value(&mut self) -> Result<()> {
264        let frame = *self
265            .values
266            .as_ref()
267            .and_then(|s| s.frames.last())
268            .ok_or(Error::InvalidValue("end_value without a value"))?;
269        if frame.chunked {
270            let ended = self.state().ended;
271            if ended == 0 || ended > frame.depth {
272                self.skip_to_end_tag(frame.depth)?;
273            }
274        }
275        let state = self.state();
276        state.frames.pop();
277        state.chunk_end = None;
278        if state.frames.last().is_none_or(|f| f.depth < state.ended) {
279            state.ended = 0;
280        }
281        Ok(())
282    }
283
284    /// Skips the rest of a chunked value at `depth` and reads its end tag.
285    fn skip_to_end_tag(&mut self, depth: usize) -> Result<()> {
286        loop {
287            if let Some(end) = self.state().chunk_end.take() {
288                if end > self.pos {
289                    self.pos = end;
290                }
291            }
292            self.align_raw(4)?;
293            let at = self.pos;
294            let tag = self.read_u32_raw()?;
295            match tag {
296                // A null value between chunks.
297                0 => {}
298                tag if tag >= 0x8000_0000 => {
299                    // An end tag ends this value and the values nested in
300                    // it; some writers count only chunked values, so a
301                    // deeper tag still ends this one.
302                    let ended = (tag as i32).unsigned_abs() as usize;
303                    self.state().ended = ended.min(depth);
304                    return Ok(());
305                }
306                tag if tag < VALUE_TAG => self.enter_chunk(tag)?,
307                _ => {
308                    self.pos = at;
309                    if let ValueHeader::Value { .. } = self.read_value_header()? {
310                        self.end_value()?;
311                    }
312                }
313            }
314        }
315    }
316
317    /// Reads a value tag, entering chunks before it. Returns the buffer
318    /// index of the tag, the tag, and whether it was inside a chunk.
319    fn read_value_tag(&mut self, outer_chunked: bool) -> Result<(usize, u32, bool)> {
320        loop {
321            if outer_chunked {
322                if let Some(end) = self.chunk_end() {
323                    let rem = self.position() % 4;
324                    if rem != 0 {
325                        let pad = (4 - rem).min(end - self.pos);
326                        self.read_octets_raw(pad)?;
327                    }
328                    if self.pos < end {
329                        self.check_in_chunk(4)?;
330                        let at = self.pos;
331                        return Ok((at, self.read_u32_raw()?, true));
332                    }
333                    self.state().chunk_end = None;
334                }
335            }
336            self.align_raw(4)?;
337            let at = self.pos;
338            let tag = self.read_u32_raw()?;
339            if outer_chunked && tag != 0 && tag < VALUE_TAG {
340                self.enter_chunk(tag)?;
341                continue;
342            }
343            return Ok((at, tag, false));
344        }
345    }
346
347    /// Starts a chunk of `len` octets at the current position.
348    fn enter_chunk(&mut self, len: u32) -> Result<()> {
349        let remaining = self.remaining();
350        if len as usize > remaining {
351            return Err(Error::LengthTooLarge { len, remaining });
352        }
353        let end = self.pos + len as usize;
354        self.state().chunk_end = Some(end);
355        Ok(())
356    }
357
358    /// Reads a repository id or codebase URL, which may be an indirection
359    /// to the same string read before.
360    fn read_shared_string(&mut self) -> Result<String> {
361        self.align_raw(4)?;
362        let at = self.pos;
363        let len = self.read_u32_raw()?;
364        if len == INDIRECTION {
365            let target = self.read_offset()?;
366            return self
367                .state()
368                .strings
369                .get(&target)
370                .cloned()
371                .ok_or(Error::InvalidValue("indirection to no repository id"));
372        }
373        let remaining = self.remaining();
374        if len as usize > remaining {
375            return Err(Error::LengthTooLarge { len, remaining });
376        }
377        let bytes = self.read_octets_raw(len as usize)?;
378        let bytes = match bytes.split_last() {
379            None => bytes,
380            Some((0, rest)) => rest,
381            Some(_) => return Err(Error::StringNotTerminated),
382        };
383        let s = String::from_utf8(bytes.to_vec()).map_err(|_| Error::InvalidUtf8)?;
384        self.state().strings.insert(at, s.clone());
385        Ok(s)
386    }
387
388    /// Reads a list of repository ids, which may be an indirection to the
389    /// same list read before.
390    fn read_id_list(&mut self) -> Result<Vec<String>> {
391        self.align_raw(4)?;
392        let at = self.pos;
393        let count = self.read_u32_raw()?;
394        if count == INDIRECTION {
395            let target = self.read_offset()?;
396            return self
397                .state()
398                .lists
399                .get(&target)
400                .cloned()
401                .ok_or(Error::InvalidValue("indirection to no repository id list"));
402        }
403        let remaining = self.remaining();
404        if (count as usize).saturating_mul(4) > remaining {
405            return Err(Error::LengthTooLarge {
406                len: count,
407                remaining,
408            });
409        }
410        let ids = (0..count)
411            .map(|_| self.read_shared_string())
412            .collect::<Result<Vec<_>>>()?;
413        self.state().lists.insert(at, ids.clone());
414        Ok(ids)
415    }
416
417    /// Reads an indirection offset outside chunks and returns the buffer
418    /// index it points to.
419    fn read_offset(&mut self) -> Result<usize> {
420        let at = self.pos;
421        let offset = self.read_u32_raw()? as i32;
422        usize::try_from(at as i64 + i64::from(offset))
423            .map_err(|_| Error::InvalidValue("indirection before the stream"))
424    }
425
426    /// Before data is read in a chunked value: if no chunk is open, or the
427    /// open one ended, reads the length of the next chunk.
428    pub(crate) fn chunk_boundary(&mut self) -> Result<()> {
429        if !self.in_chunked_value() {
430            return Ok(());
431        }
432        loop {
433            if self.chunk_end().is_some_and(|end| self.pos < end) {
434                return Ok(());
435            }
436            let len = self.read_u32_raw()?;
437            if len >= VALUE_TAG {
438                return Err(Error::InvalidValue("chunk expected"));
439            }
440            self.enter_chunk(len)?;
441        }
442    }
443
444    /// Fails if `n` octets do not fit in the open chunk.
445    pub(crate) fn check_in_chunk(&self, n: usize) -> Result<()> {
446        match self.chunk_end() {
447            Some(end) if self.pos + n > end => Err(Error::InvalidValue("data crosses a chunk end")),
448            _ => Ok(()),
449        }
450    }
451
452    /// The buffer index where the open chunk ends, inside a chunked value.
453    pub(crate) fn chunk_end(&self) -> Option<usize> {
454        if self.in_chunked_value() {
455            self.values.as_ref().and_then(|s| s.chunk_end)
456        } else {
457            None
458        }
459    }
460
461    fn in_chunked_value(&self) -> bool {
462        self.values
463            .as_ref()
464            .and_then(|s| s.frames.last())
465            .is_some_and(|f| f.chunked)
466    }
467
468    fn state(&mut self) -> &mut ReadState {
469        self.values.get_or_insert_with(Default::default)
470    }
471}
472
473#[cfg(test)]
474mod tests {
475    use super::*;
476    use crate::{ByteOrder, Encoding};
477
478    fn encoder(order: ByteOrder) -> Encoder {
479        Encoder::new(order, Encoding::Giop1_2)
480    }
481
482    fn decoder(bytes: &[u8], order: ByteOrder) -> Decoder<'_> {
483        Decoder::new(bytes, order, Encoding::Giop1_2)
484    }
485
486    #[test]
487    fn plain_value_and_sharing() {
488        for order in [ByteOrder::Big, ByteOrder::Little] {
489            let mut enc = encoder(order);
490            enc.write_octet(7);
491            assert!(enc.begin_value(1, &["IDL:V:1.0"], false).unwrap());
492            enc.write_i32(42);
493            enc.write_f64(1.5);
494            enc.end_value().unwrap();
495            assert!(!enc.begin_value(1, &["IDL:V:1.0"], false).unwrap());
496            enc.write_null_value();
497            enc.write_u16(9);
498            let bytes = enc.into_bytes();
499            assert_eq!(&bytes[4..8], &tag_bytes(0x7fffff02, order));
500
501            let mut dec = decoder(&bytes, order);
502            assert_eq!(dec.read_octet().unwrap(), 7);
503            assert_eq!(
504                dec.read_value_header().unwrap(),
505                ValueHeader::Value {
506                    slot: 0,
507                    ids: vec!["IDL:V:1.0".into()],
508                    chunked: false
509                }
510            );
511            assert_eq!(dec.read_i32().unwrap(), 42);
512            assert_eq!(dec.read_f64().unwrap(), 1.5);
513            dec.end_value().unwrap();
514            assert_eq!(
515                dec.read_value_header().unwrap(),
516                ValueHeader::Indirection(0)
517            );
518            assert_eq!(dec.read_value_header().unwrap(), ValueHeader::Null);
519            assert_eq!(dec.read_u16().unwrap(), 9);
520            assert!(dec.is_empty());
521        }
522    }
523
524    fn tag_bytes(tag: u32, order: ByteOrder) -> [u8; 4] {
525        match order {
526            ByteOrder::Big => tag.to_be_bytes(),
527            ByteOrder::Little => tag.to_le_bytes(),
528        }
529    }
530
531    #[test]
532    fn chunked_values_nest() {
533        let order = ByteOrder::Big;
534        let mut enc = encoder(order);
535        assert!(
536            enc.begin_value(1, &["IDL:D:1.0", "IDL:B:1.0"], true)
537                .unwrap()
538        );
539        enc.write_i32(1);
540        // An unchunked request inside a chunked value is chunked.
541        assert!(enc.begin_value(2, &["IDL:N:1.0"], false).unwrap());
542        enc.write_f64(2.0);
543        enc.write_null_value();
544        assert!(!enc.begin_value(1, &[], false).unwrap());
545        enc.end_value().unwrap();
546        enc.write_octet(3);
547        enc.end_value().unwrap();
548        enc.write_i32(4);
549        let bytes = enc.into_bytes();
550
551        let mut dec = decoder(&bytes, order);
552        assert_eq!(
553            dec.read_value_header().unwrap(),
554            ValueHeader::Value {
555                slot: 0,
556                ids: vec!["IDL:D:1.0".into(), "IDL:B:1.0".into()],
557                chunked: true
558            }
559        );
560        assert_eq!(dec.read_i32().unwrap(), 1);
561        assert_eq!(
562            dec.read_value_header().unwrap(),
563            ValueHeader::Value {
564                slot: 1,
565                ids: vec!["IDL:N:1.0".into()],
566                chunked: true
567            }
568        );
569        assert_eq!(dec.read_f64().unwrap(), 2.0);
570        assert_eq!(dec.read_value_header().unwrap(), ValueHeader::Null);
571        assert_eq!(
572            dec.read_value_header().unwrap(),
573            ValueHeader::Indirection(0)
574        );
575        dec.end_value().unwrap();
576        assert_eq!(dec.read_octet().unwrap(), 3);
577        dec.end_value().unwrap();
578        assert_eq!(dec.read_i32().unwrap(), 4);
579        assert!(dec.is_empty());
580    }
581
582    #[test]
583    fn truncation_skips_unread_state() {
584        let order = ByteOrder::Little;
585        let mut enc = encoder(order);
586        assert!(
587            enc.begin_value(1, &["IDL:D:1.0", "IDL:B:1.0"], true)
588                .unwrap()
589        );
590        enc.write_i32(1);
591        // State of the derived type, with a nested value, that a reader
592        // knowing only the base type skips.
593        enc.write_string("derived").unwrap();
594        assert!(enc.begin_value(2, &["IDL:N:1.0"], false).unwrap());
595        enc.write_f64(2.0);
596        enc.end_value().unwrap();
597        enc.write_octet(5);
598        enc.end_value().unwrap();
599        enc.write_i32(4);
600        let bytes = enc.into_bytes();
601
602        let mut dec = decoder(&bytes, order);
603        assert!(matches!(
604            dec.read_value_header().unwrap(),
605            ValueHeader::Value { chunked: true, .. }
606        ));
607        assert_eq!(dec.read_i32().unwrap(), 1);
608        dec.end_value().unwrap();
609        assert_eq!(dec.read_i32().unwrap(), 4);
610        assert!(dec.is_empty());
611    }
612
613    /// A stream written by hand: data split across chunks in the middle
614    /// of padding, a merged end tag, and repository ids by indirection.
615    #[test]
616    fn reads_other_writers_forms() {
617        let mut b: Vec<u8> = Vec::new();
618        let long = |b: &mut Vec<u8>, v: u32| b.extend_from_slice(&v.to_be_bytes());
619        // Outer chunked value with one id.
620        long(&mut b, 0x7fffff0a);
621        long(&mut b, 4);
622        b.extend_from_slice(b"IDL\0");
623        let id_at = 4;
624        // A chunk holding an octet only.
625        long(&mut b, 1);
626        b.push(1);
627        b.extend_from_slice(&[0; 3]);
628        // The next chunk holds the long that follows the octet.
629        long(&mut b, 4);
630        long(&mut b, 2);
631        // Nested chunked value whose id is an indirection.
632        long(&mut b, 0x7fffff0a);
633        long(&mut b, 0xffffffff);
634        let at = b.len();
635        long(&mut b, (id_at as i64 - at as i64) as i32 as u32);
636        long(&mut b, 2);
637        b.extend_from_slice(&[0x12, 0x34]);
638        // One end tag ends both values.
639        b.extend_from_slice(&[0; 2]);
640        long(&mut b, -1i32 as u32);
641        long(&mut b, 9);
642
643        let mut dec = decoder(&b, ByteOrder::Big);
644        assert!(matches!(
645            dec.read_value_header().unwrap(),
646            ValueHeader::Value { ref ids, .. } if ids == &["IDL"]
647        ));
648        assert_eq!(dec.read_octet().unwrap(), 1);
649        assert_eq!(dec.read_i32().unwrap(), 2);
650        assert!(matches!(
651            dec.read_value_header().unwrap(),
652            ValueHeader::Value { slot: 1, ref ids, .. } if ids == &["IDL"]
653        ));
654        assert_eq!(dec.read_u16().unwrap(), 0x1234);
655        dec.end_value().unwrap();
656        dec.end_value().unwrap();
657        assert_eq!(dec.read_u32().unwrap(), 9);
658        assert!(dec.is_empty());
659    }
660
661    #[test]
662    fn rejects_bad_streams() {
663        let mut dec = decoder(&[0x7f, 0xff, 0xff, 0x10], ByteOrder::Big);
664        assert!(dec.read_value_header().is_err());
665        let mut dec = decoder(&[0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0], ByteOrder::Big);
666        assert!(dec.read_value_header().is_err());
667        // Data past the end of the last chunk.
668        let mut b = Vec::new();
669        b.extend_from_slice(&0x7fffff08u32.to_be_bytes());
670        b.extend_from_slice(&2u32.to_be_bytes());
671        b.extend_from_slice(&[0, 0]);
672        b.extend_from_slice(&[0, 0]);
673        b.extend_from_slice(&(-1i32).to_be_bytes());
674        let mut dec = decoder(&b, ByteOrder::Big);
675        dec.read_value_header().unwrap();
676        assert!(dec.read_i32().is_err());
677        let mut enc = encoder(ByteOrder::Big);
678        assert!(enc.end_value().is_err());
679    }
680}