1 // Copyright 2017 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 
5 #ifndef LIB_FIDL_CODING_H_
6 #define LIB_FIDL_CODING_H_
7 
8 #include <zircon/compiler.h>
9 #include <zircon/fidl.h>
10 #include <zircon/types.h>
11 
12 __BEGIN_CDECLS
13 
14 // The maximum recursion depth the fidl encoder or decoder will
15 // perform. Each nested aggregate type (structs, unions, arrays, or
16 // vectors) counts as one step in the recursion depth.
17 #define FIDL_RECURSION_DEPTH 32
18 
19 // See https://fuchsia.googlesource.com/docs/+/master/development/languages/fidl/languages/c.md#fidl_encode-fidl_encode_msg
20 zx_status_t fidl_encode(const fidl_type_t* type, void* bytes, uint32_t num_bytes,
21                         zx_handle_t* handles, uint32_t max_handles,
22                         uint32_t* out_actual_handles, const char** out_error_msg);
23 zx_status_t fidl_encode_msg(const fidl_type_t* type, fidl_msg_t* msg,
24                             uint32_t* out_actual_handles, const char** out_error_msg);
25 
26 // See https://fuchsia.googlesource.com/docs/+/master/development/languages/fidl/languages/c.md#fidl_decode-fidl_decode_msg
27 zx_status_t fidl_decode(const fidl_type_t* type, void* bytes, uint32_t num_bytes,
28                         const zx_handle_t* handles, uint32_t num_handles,
29                         const char** error_msg_out);
30 zx_status_t fidl_decode_msg(const fidl_type_t* type, fidl_msg_t* msg,
31                             const char** out_error_msg);
32 
33 // Validates an encoded message against the given |type|.
34 //
35 // The |bytes| are not modified.
36 zx_status_t fidl_validate(const fidl_type_t* type, const void* bytes, uint32_t num_bytes,
37                           uint32_t num_handles, const char** error_msg_out);
38 zx_status_t fidl_validate_msg(const fidl_type_t* type, const fidl_msg_t* msg,
39                               const char** out_error_msg);
40 
41 // Traverses a linearized FIDL message, closing all handles within it.
42 // This function is a no-op on host side.
43 //
44 // Handle values in |bytes| are replaced with ZX_HANDLE_INVALID.
45 zx_status_t fidl_close_handles(const fidl_type_t* type, void* bytes, uint32_t num_bytes,
46                                const char** out_error_msg);
47 zx_status_t fidl_close_handles_msg(const fidl_type_t* type, const fidl_msg_t* msg,
48                                    const char** out_error_msg);
49 
50 // Stores the name of a fidl type into the provided buffer.
51 // Truncates the name if it is too long to fit into the buffer.
52 // Returns the number of characters written into the buffer.
53 //
54 // Note: This function does not write a trailing NUL.
55 size_t fidl_format_type_name(const fidl_type_t* type,
56                              char* buffer, size_t capacity);
57 
58 __END_CDECLS
59 
60 #endif // LIB_FIDL_CODING_H_
61