fidl_codec/
decode.rs

1// Copyright 2019 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use fidl_constants::{ALLOC_PRESENT_U32, ALLOC_PRESENT_U64};
6use nom::bytes::complete::take;
7use nom::combinator::{map, value, verify};
8use nom::multi::count;
9use nom::sequence::{pair, preceded, terminated};
10use nom::{IResult, Parser};
11
12use fidl_data_zx::{DEFAULT_CHANNEL_RIGHTS, ObjType as ObjectType, Rights};
13
14use crate::error::{Error, Result};
15use crate::handle::*;
16use crate::library;
17use crate::transaction::{TransactionHeader, decode_transaction_header};
18use crate::util::*;
19use crate::value::Value;
20
21use std::str;
22
23type DResult<'a, R> = IResult<&'a [u8], R, Error>;
24
25/// This represents an action that will yield a Value when given further bytes to process and
26/// handles to potentially consume. This is how we implement out-of-line data. The initial parse
27/// takes the inline data, and when we're ready, the Defer can be fed the remaining bytes to take
28/// the out of line data.
29enum Defer<'d> {
30    /// This Defer doesn't need any further processing. We can just offer up the value right now.
31    Complete(Value),
32
33    /// This Defer implements the actual deferred processing pattern described.
34    Action(
35        Box<
36            dyn for<'a> FnOnce(
37                    &'a [u8],
38                    &mut Vec<HandleInfo>,
39                    RecursionCounter,
40                ) -> DResult<'a, Value>
41                + 'd,
42        >,
43    ),
44}
45
46impl<'d> Defer<'d> {
47    /// Completes a deferred parse and returns the result.
48    fn complete<'a>(
49        self,
50        data: &'a [u8],
51        handles: &mut Vec<HandleInfo>,
52        counter: RecursionCounter,
53    ) -> DResult<'a, Value> {
54        match self {
55            Defer::Complete(v) => Ok((data, v)),
56            Defer::Action(act) => act(data, handles, counter),
57        }
58    }
59}
60
61impl<'d> From<Value> for Defer<'d> {
62    fn from(v: Value) -> Defer<'d> {
63        Defer::Complete(v)
64    }
65}
66
67fn take_u8(data: &[u8]) -> DResult<'_, u8> {
68    map(take(1usize), |x: &[u8]| x[0]).parse(data)
69}
70
71fn value_u8(data: &[u8]) -> DResult<'_, Value> {
72    map(take_u8, Value::U8).parse(data)
73}
74
75fn value_bool(data: &[u8]) -> DResult<'_, Value> {
76    map(verify(take_u8, |&x| x == 0 || x == 1), |x| Value::Bool(x != 0)).parse(data)
77}
78
79fn take_u16(data: &[u8]) -> DResult<'_, u16> {
80    map(take(2usize), |x: &[u8]| u16::from_le_bytes(x.try_into().unwrap())).parse(data)
81}
82
83fn value_u16(data: &[u8]) -> DResult<'_, Value> {
84    map(take_u16, Value::U16).parse(data)
85}
86
87fn take_u32(data: &[u8]) -> DResult<'_, u32> {
88    map(take(4usize), |x: &[u8]| u32::from_le_bytes(x.try_into().unwrap())).parse(data)
89}
90
91fn value_u32(data: &[u8]) -> DResult<'_, Value> {
92    map(take_u32, Value::U32).parse(data)
93}
94
95fn take_u64(data: &[u8]) -> DResult<'_, u64> {
96    map(take(8usize), |x: &[u8]| u64::from_le_bytes(x.try_into().unwrap())).parse(data)
97}
98
99fn value_u64(data: &[u8]) -> DResult<'_, Value> {
100    map(take_u64, Value::U64).parse(data)
101}
102
103fn take_i8(data: &[u8]) -> DResult<'_, i8> {
104    map(take(1usize), |x: &[u8]| i8::from_le_bytes([x[0]])).parse(data)
105}
106
107fn value_i8(data: &[u8]) -> DResult<'_, Value> {
108    map(take_i8, Value::I8).parse(data)
109}
110
111fn take_i16(data: &[u8]) -> DResult<'_, i16> {
112    map(take(2usize), |x: &[u8]| i16::from_le_bytes(x.try_into().unwrap())).parse(data)
113}
114
115fn value_i16(data: &[u8]) -> DResult<'_, Value> {
116    map(take_i16, Value::I16).parse(data)
117}
118
119fn take_i32(data: &[u8]) -> DResult<'_, i32> {
120    map(take(4usize), |x: &[u8]| i32::from_le_bytes(x.try_into().unwrap())).parse(data)
121}
122
123fn value_i32(data: &[u8]) -> DResult<'_, Value> {
124    map(take_i32, Value::I32).parse(data)
125}
126
127fn take_i64(data: &[u8]) -> DResult<'_, i64> {
128    map(take(8usize), |x: &[u8]| i64::from_le_bytes(x.try_into().unwrap())).parse(data)
129}
130
131fn value_i64(data: &[u8]) -> DResult<'_, Value> {
132    map(take_i64, Value::I64).parse(data)
133}
134
135fn take_f32(data: &[u8]) -> DResult<'_, f32> {
136    map(take(4usize), |x: &[u8]| f32::from_le_bytes(x.try_into().unwrap())).parse(data)
137}
138
139fn value_f32(data: &[u8]) -> DResult<'_, Value> {
140    map(take_f32, Value::F32).parse(data)
141}
142
143fn take_f64(data: &[u8]) -> DResult<'_, f64> {
144    map(take(8usize), |x: &[u8]| f64::from_le_bytes(x.try_into().unwrap())).parse(data)
145}
146
147fn value_f64(data: &[u8]) -> DResult<'_, Value> {
148    map(take_f64, Value::F64).parse(data)
149}
150
151fn transaction_header(data: &[u8]) -> DResult<'_, TransactionHeader> {
152    decode_transaction_header(data)
153        .map(|(a, b)| (b, a))
154        .map_err(|e| Error::DecodeError(format!("Invalid FIDL transaction header ({e:?})")).into())
155}
156
157fn take_padding(amount: usize) -> impl Fn(&[u8]) -> DResult<'_, ()> {
158    move |bytes| value((), verify(take(amount), |x: &[u8]| x.iter().all(|&x| x == 0))).parse(bytes)
159}
160
161fn decode_struct<'s>(
162    ns: &'s library::Namespace,
163    st: &'s library::Struct,
164    nullable: bool,
165) -> impl Fn(&[u8]) -> DResult<'_, Defer<'s>> {
166    move |bytes: &[u8]| {
167        if !nullable {
168            return decode_struct_nonnull(ns, st).parse(bytes);
169        }
170
171        let (bytes, presence) = take_u64(bytes)?;
172
173        if presence == 0 {
174            Ok((bytes, Defer::Complete(Value::Null)))
175        } else if presence != ALLOC_PRESENT_U64 {
176            Err(Error::DecodeError("Bad presence indicator".to_owned()).into())
177        } else {
178            Ok((
179                bytes,
180                Defer::Action(Box::new(
181                    move |bytes: &[u8],
182                          handles: &mut Vec<HandleInfo>,
183                          counter: RecursionCounter| {
184                        let counter = counter.next()?;
185                        let align = alignment_padding_for_size(st.size);
186                        let (bytes, defer) =
187                            terminated(decode_struct_nonnull(ns, st), take_padding(align))
188                                .parse(bytes)?;
189                        defer.complete(bytes, handles, counter)
190                    },
191                )),
192            ))
193        }
194    }
195}
196
197fn decode_struct_nonnull<'s>(
198    ns: &'s library::Namespace,
199    st: &'s library::Struct,
200) -> impl Fn(&[u8]) -> DResult<'_, Defer<'s>> {
201    move |mut bytes: &[u8]| {
202        let mut offset = 0;
203        let mut fields = Vec::new();
204
205        for member in &st.members {
206            let (remaining, result) =
207                preceded(take_padding(member.offset - offset), decode_type(ns, &member.ty))
208                    .parse(bytes)?;
209            fields.push((member.name.clone(), result));
210            bytes = remaining;
211            offset = member.offset + member.ty.inline_size(ns)?;
212        }
213
214        if offset < st.size {
215            let (remaining, _) = take_padding(st.size - offset).parse(bytes)?;
216            bytes = remaining;
217        }
218
219        Ok((
220            bytes,
221            Defer::Action(Box::new(
222                move |mut bytes: &[u8],
223                      handles: &mut Vec<HandleInfo>,
224                      counter: RecursionCounter| {
225                    let mut complete_fields = Vec::new();
226
227                    for (name, defer) in fields {
228                        let (remaining, value) = defer.complete(bytes, handles, counter)?;
229                        bytes = remaining;
230                        complete_fields.push((name, value))
231                    }
232
233                    Ok((bytes, Value::Object(complete_fields)))
234                },
235            )),
236        ))
237    }
238}
239
240fn decode_type<'t>(
241    ns: &'t library::Namespace,
242    ty: &'t library::Type,
243) -> impl Fn(&[u8]) -> DResult<'_, Defer<'t>> {
244    use library::Type;
245    move |b: &[u8]| {
246        match ty {
247            Type::Bool => value_bool(b),
248            Type::U8 => value_u8(b),
249            Type::U16 => value_u16(b),
250            Type::U32 => value_u32(b),
251            Type::U64 => value_u64(b),
252            Type::I8 => value_i8(b),
253            Type::I16 => value_i16(b),
254            Type::I32 => value_i32(b),
255            Type::I64 => value_i64(b),
256            Type::F32 => value_f32(b),
257            Type::F64 => value_f64(b),
258            Type::Array(ty, size) => return decode_array(ns, ty, *size).parse(b),
259            Type::Vector { ty, nullable, element_count } => {
260                return decode_vector(ns, ty, *nullable, *element_count).parse(b);
261            }
262            Type::String { nullable, byte_count } => {
263                return decode_string(*nullable, *byte_count).parse(b);
264            }
265            Type::Identifier { name, nullable } => {
266                return decode_identifier(ns, name, *nullable).parse(b);
267            }
268            Type::Handle { object_type, nullable, rights } => {
269                return decode_handle(*object_type, *nullable, *rights).parse(b);
270            }
271            Type::Endpoint { protocol, rights, nullable, role } => match role {
272                library::EndpointRole::Client => {
273                    return decode_client_end(
274                        protocol.clone(),
275                        *nullable,
276                        rights.or(Some(DEFAULT_CHANNEL_RIGHTS)),
277                    )
278                    .parse(b);
279                }
280                library::EndpointRole::Server => {
281                    return decode_server_end(
282                        protocol.clone(),
283                        *nullable,
284                        rights.or(Some(DEFAULT_CHANNEL_RIGHTS)),
285                    )
286                    .parse(b);
287                }
288            },
289            Type::UnknownString(s) => {
290                Err(Error::LibraryError(format!("Unresolved Type: {}", s)).into())
291            }
292            Type::Unknown(library::TypeInfo { identifier: s, .. }) => {
293                return Err(Error::LibraryError(format!(
294                    "Unresolved Type: {}",
295                    s.as_ref().map_or("<unidentified>", String::as_str)
296                ))
297                .into());
298            }
299            Type::FrameworkError => map(take_u32, |_| Value::Null).parse(b),
300        }
301        .map(|(x, y)| (x, Defer::Complete(y)))
302    }
303}
304
305/// Given a list of defers, complete them all and turn them into a list of complete values.
306fn complete_deferred_list<'a>(
307    bytes: &'a [u8],
308    handles: &mut Vec<HandleInfo>,
309    defers: Vec<Defer<'_>>,
310    counter: RecursionCounter,
311) -> DResult<'a, Value> {
312    let mut bytes = bytes;
313    let mut values = Vec::new();
314
315    for defer in defers {
316        let (next_bytes, value) = defer.complete(bytes, handles, counter)?;
317        bytes = next_bytes;
318        values.push(value)
319    }
320
321    Ok((bytes, Value::List(values)))
322}
323
324fn decode_array<'t>(
325    ns: &'t library::Namespace,
326    ty: &'t library::Type,
327    size: usize,
328) -> impl Fn(&[u8]) -> DResult<'_, Defer<'t>> {
329    move |bytes: &[u8]| {
330        let (bytes, defers) = count(decode_type(ns, ty), size).parse(bytes)?;
331
332        Ok((
333            bytes,
334            Defer::Action(Box::new(
335                move |bytes: &[u8], handles: &mut Vec<HandleInfo>, counter: RecursionCounter| {
336                    complete_deferred_list(bytes, handles, defers, counter)
337                },
338            )),
339        ))
340    }
341}
342
343fn decode_vector<'t>(
344    ns: &'t library::Namespace,
345    ty: &'t library::Type,
346    nullable: bool,
347    element_count: Option<usize>,
348) -> impl Fn(&[u8]) -> DResult<'_, Defer<'t>> {
349    move |bytes: &[u8]| {
350        let (bytes, (size, presence)) = pair(take_u64, take_u64).parse(bytes)?;
351        let size = size as usize;
352        let align = alignment_padding_for_size(size * ty.inline_size(ns)?);
353
354        if presence == 0 {
355            if nullable {
356                if size == 0 {
357                    Ok((bytes, Defer::Complete(Value::Null)))
358                } else {
359                    Err(Error::DecodeError("Absent vector had a size".to_owned()).into())
360                }
361            } else {
362                Err(Error::DecodeError("Missing non-nullable vector".to_owned()).into())
363            }
364        } else if presence != ALLOC_PRESENT_U64 {
365            Err(Error::DecodeError("Bad presence indicator".to_owned()).into())
366        } else if element_count.map(|x| x < size).unwrap_or(false) {
367            Err(Error::DecodeError("Vector too long".to_owned()).into())
368        } else {
369            Ok((
370                bytes,
371                Defer::Action(Box::new(
372                    move |bytes: &[u8],
373                          handles: &mut Vec<HandleInfo>,
374                          counter: RecursionCounter| {
375                        let counter = counter.next()?;
376                        let (bytes, defers) =
377                            terminated(count(decode_type(ns, ty), size), take_padding(align))
378                                .parse(bytes)?;
379
380                        complete_deferred_list(bytes, handles, defers, counter)
381                    },
382                )),
383            ))
384        }
385    }
386}
387
388fn decode_string(
389    nullable: bool,
390    byte_count: Option<usize>,
391) -> impl Fn(&[u8]) -> DResult<'_, Defer<'static>> {
392    move |bytes: &[u8]| {
393        let (bytes, (size, presence)) = pair(take_u64, take_u64).parse(bytes)?;
394        let size = size as usize;
395        let align = alignment_padding_for_size(size);
396
397        if presence == 0 {
398            if nullable {
399                if size == 0 {
400                    Ok((bytes, Defer::Complete(Value::Null)))
401                } else {
402                    Err(Error::DecodeError("Absent string had a size".to_owned()).into())
403                }
404            } else {
405                Err(Error::DecodeError("Missing non-nullable string".to_owned()).into())
406            }
407        } else if presence != ALLOC_PRESENT_U64 {
408            Err(Error::DecodeError("Bad presence indicator".to_owned()).into())
409        } else if byte_count.map(|x| x < size).unwrap_or(false) {
410            Err(Error::DecodeError("String too long".to_owned()).into())
411        } else {
412            Ok((
413                bytes,
414                Defer::Action(Box::new(
415                    move |bytes: &[u8], _: &mut Vec<HandleInfo>, counter: RecursionCounter| {
416                        let _counter = counter.next()?;
417                        let (bytes, data) =
418                            terminated(take(size), take_padding(align)).parse(bytes)?;
419
420                        match str::from_utf8(data) {
421                            Ok(x) => Ok((bytes, Value::String(x.to_owned()))),
422                            Err(x) => Err(Error::Utf8Error(x).into()),
423                        }
424                    },
425                )),
426            ))
427        }
428    }
429}
430
431fn decode_server_end(
432    interface: String,
433    nullable: bool,
434    rights: Option<Rights>,
435) -> impl Fn(&[u8]) -> DResult<'_, Defer<'static>> {
436    decode_handle_with(
437        interface,
438        nullable,
439        &|x, y, z| Value::ServerEnd(x.into(), y, z),
440        Some(ObjectType::Channel),
441        rights,
442    )
443}
444
445fn decode_client_end(
446    interface: String,
447    nullable: bool,
448    rights: Option<Rights>,
449) -> impl Fn(&[u8]) -> DResult<'_, Defer<'static>> {
450    decode_handle_with(
451        interface,
452        nullable,
453        &|x, y, z| Value::ClientEnd(x.into(), y, z),
454        Some(ObjectType::Channel),
455        rights,
456    )
457}
458
459fn decode_handle(
460    handle_type: ObjectType,
461    nullable: bool,
462    rights: Option<Rights>,
463) -> impl Fn(&[u8]) -> DResult<'_, Defer<'static>> {
464    decode_handle_with(handle_type, nullable, &Value::Handle, None, rights)
465}
466
467fn decode_handle_with<T: Clone + 'static>(
468    handle_type: T,
469    nullable: bool,
470    value_builder: &'static (impl Fn(NullableHandle, T, Option<Rights>) -> Value + 'static),
471    constrain_type: Option<ObjectType>,
472    constrain_rights: Option<Rights>,
473) -> impl Fn(&[u8]) -> DResult<'_, Defer<'static>> {
474    move |bytes: &[u8]| {
475        let handle_type = handle_type.clone();
476        let (bytes, presence) = take_u32(bytes)?;
477
478        if presence == 0 {
479            if nullable {
480                Ok((bytes, Defer::Complete(Value::Null)))
481            } else {
482                Err(Error::DecodeError("Missing non-nullable handle".to_owned()).into())
483            }
484        } else if presence != ALLOC_PRESENT_U32 {
485            Err(Error::DecodeError("Bad presence indicator".to_owned()).into())
486        } else {
487            Ok((
488                bytes,
489                Defer::Action(Box::new(
490                    move |bytes: &[u8], handles: &mut Vec<HandleInfo>, _: RecursionCounter| {
491                        if !handles.is_empty() {
492                            if constrain_type.map(|x| x == handles[0].object_type()).unwrap_or(true)
493                            {
494                                let handle_info = handles.remove(0);
495
496                                let decoded_rights = match (handle_info.rights(), constrain_rights) {
497                                    (Rights::SAME_RIGHTS, Some(_)) => Rights::SAME_RIGHTS,
498                                    (handle_rights, Some(Rights::SAME_RIGHTS)) => handle_rights,
499                                    (handle_rights, None) => handle_rights,
500                                    (handle_rights, Some(constrain_rights)) => {
501                                        if handle_rights.contains(constrain_rights) {
502                                            constrain_rights
503                                        } else {
504                                            let mut missing = constrain_rights;
505                                            missing.remove(handle_rights);
506                                            return Err(Error::DecodeError(format!(
507                                                "Insufficient handle rights, need {missing:?}"
508                                            ))
509                                            .into());
510                                        }
511                                    }
512                                };
513
514                                Ok((
515                                    bytes,
516                                    value_builder(
517                                        handle_info.into_handle(),
518                                        handle_type,
519                                        Some(decoded_rights),
520                                    ),
521                                ))
522                            } else {
523                                Err(Error::DecodeError("Wrong handle type".to_owned()).into())
524                            }
525                        } else {
526                            Err(Error::DecodeError("Too few handles".to_owned()).into())
527                        }
528                    },
529                )),
530            ))
531        }
532    }
533}
534
535fn decode_enum<'e>(
536    ns: &'e library::Namespace,
537    en: &'e library::Enum,
538) -> impl Fn(&[u8]) -> DResult<'_, Defer<'e>> {
539    move |bytes: &[u8]| {
540        let (bytes, defer) = decode_type(ns, &en.ty).parse(bytes)?;
541        Ok((
542            bytes,
543            Defer::Action(Box::new(
544                move |bytes: &[u8], handles: &mut Vec<HandleInfo>, counter: RecursionCounter| {
545                    let (bytes, value) = defer.complete(bytes, handles, counter)?;
546
547                    for member in &en.members {
548                        if value == member.value || !en.strict {
549                            return Ok((bytes, Value::Enum(en.name.to_owned(), Box::new(value))));
550                        }
551                    }
552
553                    if en.strict {
554                        Err(Error::DecodeError("Unknown Enum Variant.".to_owned()).into())
555                    } else {
556                        Ok((bytes, Value::Enum(en.name.to_owned(), Box::new(value))))
557                    }
558                },
559            )),
560        ))
561    }
562}
563
564fn decode_bits<'b>(
565    ns: &'b library::Namespace,
566    bits: &'b library::Bits,
567) -> impl Fn(&[u8]) -> DResult<'_, Defer<'b>> {
568    move |bytes: &[u8]| {
569        let (bytes, defer) = decode_type(ns, &bits.ty).parse(bytes)?;
570        Ok((
571            bytes,
572            Defer::Action(Box::new(
573                move |bytes: &[u8], handles: &mut Vec<HandleInfo>, counter: RecursionCounter| {
574                    let (bytes, value) = defer.complete(bytes, handles, counter)?;
575
576                    let data = value.bits().ok_or_else(|| {
577                        Error::LibraryError("Bits with non-integer type.".to_owned())
578                    })?;
579
580                    if bits.strict && data != data & bits.mask {
581                        Err(Error::DecodeError("Invalid value for bits field.".to_owned()).into())
582                    } else {
583                        Ok((bytes, Value::Bits(bits.name.to_owned(), Box::new(value))))
584                    }
585                },
586            )),
587        ))
588    }
589}
590
591/// Contents of an envelope header.
592enum Envelope {
593    Present { bytes: u32, handles: u16 },
594    Inline { bytes: [u8; 4], handles: u16 },
595    Empty,
596}
597
598impl Envelope {
599    fn skip(&self) -> Defer<'static> {
600        let (envelope_bytes, envelope_handles) = match self {
601            Envelope::Present { bytes, handles } => (*bytes, *handles),
602            Envelope::Inline { handles, .. } => (0, *handles),
603            Envelope::Empty => return Defer::Complete(Value::Null),
604        };
605
606        Defer::Action(Box::new(
607            move |bytes: &[u8], handles: &mut Vec<HandleInfo>, counter: RecursionCounter| {
608                let _counter = counter.next()?;
609                if (envelope_bytes & 7u32) != 0 {
610                    return Err(Error::DecodeError("Invalid envelope size".to_owned()).into());
611                }
612                let envelope_bytes = envelope_bytes as usize;
613                let envelope_handles = envelope_handles as usize;
614
615                if envelope_handles > handles.len() {
616                    Err(Error::DecodeError("Insufficient handles for envelope".to_owned()).into())
617                } else if envelope_bytes > bytes.len() {
618                    Err(Error::DecodeError("Insufficient bytes for envelope".to_owned()).into())
619                } else {
620                    *handles = handles.split_off(envelope_handles);
621                    Ok((&bytes[envelope_bytes as usize..], Value::Null))
622                }
623            },
624        ))
625    }
626
627    fn decode_type<'s>(
628        &self,
629        ns: &'s library::Namespace,
630        ty: &'s library::Type,
631    ) -> Result<Defer<'s>> {
632        if let &Envelope::Empty = self {
633            Ok(self.skip())
634        } else if !ty.is_resolved(ns) {
635            Ok(self.skip())
636        } else if let Envelope::Inline { bytes, handles } = self {
637            let (padding, ret) = decode_type(ns, ty).parse(bytes)?;
638            take_padding(padding.len()).parse(padding)?;
639            let expect_handles = *handles as usize;
640            Ok(Defer::Action(Box::new(
641                move |bytes: &[u8], handles: &mut Vec<HandleInfo>, counter: RecursionCounter| {
642                    let handle_count = handles.len();
643                    let v = ret.complete(bytes, handles, counter)?;
644
645                    let handles_used = handle_count - handles.len();
646                    if handles_used != expect_handles {
647                        Err(Error::DecodeError("Wrong number of handles in envelope".to_owned())
648                            .into())
649                    } else {
650                        Ok(v)
651                    }
652                },
653            )))
654        } else if ty.inline_size(ns)? <= 4 {
655            Err(Error::DecodeError("Envelope should be inline".to_owned()))
656        } else {
657            let Envelope::Present { bytes, handles } = self else { unreachable!() };
658            let expect_bytes = *bytes as usize;
659            let expect_handles = *handles as usize;
660            Ok(Defer::Action(Box::new(
661                move |bytes: &[u8], handles: &mut Vec<HandleInfo>, counter: RecursionCounter| {
662                    let counter = counter.next()?;
663                    let bytes_start = bytes.len();
664                    let align = alignment_padding_for_size(ty.inline_size(ns)?);
665                    let (bytes, defer) =
666                        terminated(decode_type(ns, ty), take_padding(align)).parse(bytes)?;
667
668                    let handle_count = handles.len();
669                    let (bytes, value) = defer.complete(bytes, handles, counter)?;
670                    let handles_used = handle_count - handles.len();
671                    let bytes_used = bytes_start - bytes.len();
672                    if handles_used != expect_handles {
673                        Err(Error::DecodeError("Wrong number of handles in envelope".to_owned())
674                            .into())
675                    } else if bytes_used != expect_bytes {
676                        Err(Error::DecodeError("Wrong number of bytes in envelope".to_owned())
677                            .into())
678                    } else {
679                        Ok((bytes, value))
680                    }
681                },
682            )))
683        }
684    }
685
686    fn take<'a>(empty_ok: bool) -> impl Fn(&'a [u8]) -> DResult<'a, Envelope> {
687        move |bytes: &[u8]| {
688            let (bytes, (envelope_bytes, envelope_handles, envelope_flags)) =
689                (take_u32, take_u16, take_u16).parse(bytes)?;
690
691            if envelope_bytes == 0 && envelope_handles == 0 && envelope_flags == 0 {
692                if !empty_ok {
693                    Err(Error::DecodeError("Unexpected empty envelope.".to_owned()).into())
694                } else {
695                    Ok((bytes, Envelope::Empty))
696                }
697            } else if envelope_flags == 0 {
698                Ok((bytes, Envelope::Present { bytes: envelope_bytes, handles: envelope_handles }))
699            } else if envelope_flags == 1 {
700                Ok((
701                    bytes,
702                    Envelope::Inline {
703                        bytes: envelope_bytes.to_le_bytes(),
704                        handles: envelope_handles,
705                    },
706                ))
707            } else {
708                Err(Error::DecodeError("Unknown evelope flags.".to_owned()).into())
709            }
710        }
711    }
712}
713
714fn decode_union<'u>(
715    ns: &'u library::Namespace,
716    union: &'u library::TableOrUnion,
717    nullable: bool,
718) -> impl Fn(&[u8]) -> DResult<'_, Defer<'u>> {
719    move |bytes: &[u8]| {
720        let (bytes, (ordinal, envelope)) = (take_u64, Envelope::take(nullable)).parse(bytes)?;
721
722        match (ordinal, &envelope) {
723            (0, Envelope::Empty) => return Ok((bytes, envelope.skip())),
724            (0, _) => return Err(Error::DecodeError("Invalid Union block.".to_owned()).into()),
725            _ => (),
726        };
727
728        match union.members.get(&ordinal) {
729            None if union.strict => {
730                Err(Error::DecodeError("Invalid Union ordinal.".to_owned()).into())
731            }
732            None => Ok((bytes, envelope.skip())),
733            Some(member) => Ok((
734                bytes,
735                Defer::Action(Box::new(
736                    move |bytes: &[u8],
737                          handles: &mut Vec<HandleInfo>,
738                          counter: RecursionCounter| {
739                        let (bytes, inner) = envelope
740                            .decode_type(ns, &member.ty)?
741                            .complete(bytes, handles, counter)?;
742                        Ok((
743                            bytes,
744                            Value::Union(
745                                union.name.to_owned(),
746                                member.name.to_owned(),
747                                Box::new(inner),
748                            ),
749                        ))
750                    },
751                )),
752            )),
753        }
754    }
755}
756
757fn decode_table<'t>(
758    ns: &'t library::Namespace,
759    table: &'t library::TableOrUnion,
760) -> impl Fn(&[u8]) -> DResult<'_, Defer<'t>> {
761    move |bytes: &[u8]| {
762        let (bytes, (size, data_ptr)) = pair(take_u64, take_u64).parse(bytes)?;
763
764        if data_ptr != ALLOC_PRESENT_U64 {
765            return Err(Error::DecodeError("Bad presence indicator.".to_owned()).into());
766        }
767
768        Ok((
769            bytes,
770            Defer::Action(Box::new(
771                move |bytes: &[u8], handles: &mut Vec<HandleInfo>, counter: RecursionCounter| {
772                    let counter = counter.next()?;
773                    let (mut bytes, envelopes) =
774                        count(Envelope::take(true), size as usize).parse(bytes)?;
775
776                    let mut result = Vec::new();
777                    let mut expect_ord = 1u64;
778                    for envelope in envelopes {
779                        let member = table.members.get(&expect_ord);
780                        expect_ord += 1;
781
782                        let next_bytes = if let Some(member) = member {
783                            let (next_bytes, val) = envelope
784                                .decode_type(ns, &member.ty)?
785                                .complete(bytes, handles, counter)?;
786                            if !matches!(val, Value::Null) {
787                                result.push((member.name.clone(), val));
788                            }
789                            next_bytes
790                        } else {
791                            envelope.skip().complete(bytes, handles, counter)?.0
792                        };
793
794                        bytes = next_bytes;
795                    }
796
797                    Ok((bytes, Value::Object(result)))
798                },
799            )),
800        ))
801    }
802}
803
804fn decode_identifier<'s>(
805    ns: &'s library::Namespace,
806    name: &'s str,
807    nullable: bool,
808) -> impl Fn(&[u8]) -> DResult<'_, Defer<'s>> {
809    move |bytes: &[u8]| match ns.lookup(name)? {
810        library::LookupResult::Bits(b) => decode_bits(ns, b).parse(bytes),
811        library::LookupResult::Enum(e) => decode_enum(ns, e).parse(bytes),
812        library::LookupResult::Struct(s) => decode_struct(ns, s, nullable).parse(bytes),
813        library::LookupResult::Union(u) => decode_union(ns, u, nullable).parse(bytes),
814        library::LookupResult::Table(t) => decode_table(ns, t).parse(bytes),
815        library::LookupResult::Protocol(_) => Err(Error::DecodeError(format!(
816            "Protocol names cannot be used as identifiers: {}",
817            name
818        ))
819        .into()),
820    }
821}
822
823/// Decode a FIDL request or response, depending on the direction header.
824fn decode_message<'a>(
825    ns: &library::Namespace,
826    direction: Direction,
827    bytes: &'a [u8],
828    mut handles: Vec<HandleInfo>,
829) -> Result<(TransactionHeader, Value)> {
830    let (bytes, header) = transaction_header(bytes)?;
831
832    let (_, method) = ns.lookup_method_ordinal(header.ordinal)?;
833
834    let (message, has) = match direction {
835        Direction::Request => (method.request.as_ref(), method.has_request),
836        Direction::Response => (method.response.as_ref(), method.has_response),
837    };
838
839    if let Some(message) = message {
840        let (bytes, defer) = decode_type(ns, message).parse(bytes)?;
841        let (bytes, value) = defer.complete(bytes, &mut handles, RecursionCounter::new())?;
842
843        if !bytes.is_empty() && (bytes.len() >= 8 || bytes.iter().any(|x| *x != 0)) {
844            Err(Error::DecodeError(format!("{} bytes left over.", bytes.len())))
845        } else if !handles.is_empty() {
846            Err(Error::DecodeError(format!("{} handles left over.", handles.len())))
847        } else {
848            Ok((header, value))
849        }
850    } else if !has {
851        Err(Error::DecodeError(format!(
852            "Header indicates method {}, which has no {}.",
853            method.name,
854            direction.to_string()
855        )))
856    } else {
857        Ok((header, Value::Null))
858    }
859}
860
861/// Decode a FIDL request from a byte buffer and a list of handles.
862pub fn decode_request(
863    ns: &library::Namespace,
864    bytes: &[u8],
865    handles: Vec<HandleInfo>,
866) -> Result<(TransactionHeader, Value)> {
867    decode_message(ns, Direction::Request, bytes, handles)
868}
869
870/// Decode a FIDL response from a byte buffer and a list of handles.
871pub fn decode_response(
872    ns: &library::Namespace,
873    bytes: &[u8],
874    handles: Vec<HandleInfo>,
875) -> Result<(TransactionHeader, Value)> {
876    decode_message(ns, Direction::Response, bytes, handles)
877}
878
879/// Decode a FIDL value.
880pub fn decode<'a>(
881    ns: &library::Namespace,
882    ty: &str,
883    bytes: &'a [u8],
884    mut handles: Vec<HandleInfo>,
885) -> Result<Value> {
886    if bytes.len() % 8 != 0 {
887        return Err(Error::DecodeError("Unaligned encoded object".to_owned()));
888    }
889    let (bytes, defer) = decode_identifier(ns, ty, false).parse(bytes)?;
890    let (bytes, value) = defer.complete(bytes, &mut handles, RecursionCounter::new())?;
891    take_padding(bytes.len()).parse(bytes)?;
892
893    if !bytes.is_empty() && (bytes.len() >= 8 || bytes.iter().any(|x| *x != 0)) {
894        Err(Error::DecodeError(format!("{} bytes left over.", bytes.len())))
895    } else if !handles.is_empty() {
896        Err(Error::DecodeError(format!("{} handles left over.", handles.len())))
897    } else {
898        Ok(value)
899    }
900}