/src/libcoap/include/coap3/coap_encode.h
Line | Count | Source |
1 | | /* |
2 | | * encode.h -- encoding and decoding of CoAP data types |
3 | | * |
4 | | * Copyright (C) 2010-2012,2023-2026 Olaf Bergmann <bergmann@tzi.org> |
5 | | * |
6 | | * SPDX-License-Identifier: BSD-2-Clause |
7 | | * |
8 | | * This file is part of the CoAP library libcoap. Please see README for terms |
9 | | * of use. |
10 | | */ |
11 | | |
12 | | /** |
13 | | * @file coap_encode.h |
14 | | * @brief Encoding and decoding of CoAP data types |
15 | | */ |
16 | | |
17 | | #ifndef COAP_ENCODE_H_ |
18 | | #define COAP_ENCODE_H_ |
19 | | |
20 | | #ifdef __cplusplus |
21 | | extern "C" { |
22 | | #endif |
23 | | |
24 | | #ifndef HAVE_FLS |
25 | | /* include this only if fls() is not available */ |
26 | | extern int coap_fls(unsigned int i); |
27 | | #else |
28 | | #define coap_fls(i) fls(i) |
29 | | #endif |
30 | | |
31 | | #ifndef HAVE_FLSLL |
32 | | /* include this only if flsll() is not available */ |
33 | | extern int coap_flsll(long long i); |
34 | | #else |
35 | | #define coap_flsll(i) flsll(i) |
36 | | #endif |
37 | | |
38 | | /** |
39 | | * @ingroup application_api |
40 | | * @defgroup encode Encode / Decode API |
41 | | * API for endoding/decoding CoAP options. |
42 | | * @{ |
43 | | */ |
44 | | |
45 | | /** |
46 | | * Decodes multiple-length byte sequences. @p buf points to an input byte |
47 | | * sequence of length @p length. Returns the up to 4 byte decoded value. |
48 | | * |
49 | | * @param buf The input byte sequence to decode from |
50 | | * @param length The length of the input byte sequence |
51 | | * |
52 | | * @return The decoded value |
53 | | */ |
54 | | unsigned int coap_decode_var_bytes(const uint8_t *buf, size_t length); |
55 | | |
56 | | /** |
57 | | * Decodes multiple-length byte sequences. @p buf points to an input byte |
58 | | * sequence of length @p length. Returns the up to 8 byte decoded value. |
59 | | * |
60 | | * @param buf The input byte sequence to decode from |
61 | | * @param length The length of the input byte sequence |
62 | | * |
63 | | * @return The decoded value |
64 | | */ |
65 | | uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t length); |
66 | | |
67 | | /** |
68 | | * Encodes multiple-length byte sequences. @p buf points to an output buffer of |
69 | | * sufficient length to store the encoded bytes. @p value is the 4 byte value |
70 | | * to encode. |
71 | | * Returns the number of bytes used to encode @p value or 0 on error. |
72 | | * |
73 | | * @param buf The output buffer to encode into |
74 | | * @param length The output buffer size to encode into (must be sufficient) |
75 | | * @param value The value to encode into the buffer |
76 | | * |
77 | | * @return The number of bytes used to encode @p value (which can be 0 |
78 | | * when encoding value of 0) or @c 0 on error. |
79 | | */ |
80 | | unsigned int coap_encode_var_safe(uint8_t *buf, |
81 | | size_t length, |
82 | | unsigned int value); |
83 | | |
84 | | /** |
85 | | * Encodes multiple-length byte sequences. @p buf points to an output buffer of |
86 | | * sufficient length to store the encoded bytes. @p value is the 8 byte value |
87 | | * to encode. |
88 | | * Returns the number of bytes used to encode @p value or 0 on error. |
89 | | * |
90 | | * @param buf The output buffer to encode into |
91 | | * @param length The output buffer size to encode into (must be sufficient) |
92 | | * @param value The value to encode into the buffer |
93 | | * |
94 | | * @return The number of bytes used to encode @p value (which can be 0 |
95 | | * when encoding value of 0) or @c 0 on error. |
96 | | */ |
97 | | unsigned int coap_encode_var_safe8(uint8_t *buf, |
98 | | size_t length, |
99 | | uint64_t value); |
100 | | |
101 | | /** @} */ |
102 | | |
103 | | /** |
104 | | * @deprecated Use coap_encode_var_safe() instead. |
105 | | * Provided for backward compatibility. As @p value has a |
106 | | * maximum value of 0xffffffff, and buf is usually defined as an array, it |
107 | | * is unsafe to continue to use this variant if buf[] is less than buf[4]. |
108 | | * |
109 | | * For example |
110 | | * char buf[1],oops; |
111 | | * .. |
112 | | * coap_encode_var_bytes(buf, 0xfff); |
113 | | * would cause oops to get overwritten. This error can only be found by code |
114 | | * inspection. |
115 | | * coap_encode_var_safe(buf, sizeof(buf), 0xfff); |
116 | | * would catch this error at run-time and should be used instead. |
117 | | */ |
118 | | COAP_STATIC_INLINE COAP_DEPRECATED int |
119 | 0 | coap_encode_var_bytes(uint8_t *buf, unsigned int value) { |
120 | 0 | return (int)coap_encode_var_safe(buf, sizeof(value), value); |
121 | 0 | } Unexecuted instantiation: coap_debug.c:coap_encode_var_bytes Unexecuted instantiation: coap_encode.c:coap_encode_var_bytes Unexecuted instantiation: coap_net.c:coap_encode_var_bytes Unexecuted instantiation: coap_netif.c:coap_encode_var_bytes Unexecuted instantiation: coap_openssl.c:coap_encode_var_bytes Unexecuted instantiation: coap_option.c:coap_encode_var_bytes Unexecuted instantiation: coap_oscore.c:coap_encode_var_bytes Unexecuted instantiation: coap_pdu.c:coap_encode_var_bytes Unexecuted instantiation: coap_proxy.c:coap_encode_var_bytes Unexecuted instantiation: coap_prng.c:coap_encode_var_bytes Unexecuted instantiation: coap_resource.c:coap_encode_var_bytes Unexecuted instantiation: coap_session.c:coap_encode_var_bytes Unexecuted instantiation: coap_str.c:coap_encode_var_bytes Unexecuted instantiation: coap_strm_posix.c:coap_encode_var_bytes Unexecuted instantiation: coap_subscribe.c:coap_encode_var_bytes Unexecuted instantiation: coap_time.c:coap_encode_var_bytes Unexecuted instantiation: coap_uri.c:coap_encode_var_bytes Unexecuted instantiation: coap_ws.c:coap_encode_var_bytes Unexecuted instantiation: oscore.c:coap_encode_var_bytes Unexecuted instantiation: oscore_cbor.c:coap_encode_var_bytes Unexecuted instantiation: oscore_context.c:coap_encode_var_bytes Unexecuted instantiation: oscore_cose.c:coap_encode_var_bytes Unexecuted instantiation: oscore_crypto.c:coap_encode_var_bytes Unexecuted instantiation: coap_address.c:coap_encode_var_bytes Unexecuted instantiation: coap_async.c:coap_encode_var_bytes Unexecuted instantiation: coap_block.c:coap_encode_var_bytes Unexecuted instantiation: coap_cache.c:coap_encode_var_bytes Unexecuted instantiation: coap_dgrm_posix.c:coap_encode_var_bytes Unexecuted instantiation: coap_dtls.c:coap_encode_var_bytes Unexecuted instantiation: coap_io.c:coap_encode_var_bytes Unexecuted instantiation: coap_io_posix.c:coap_encode_var_bytes Unexecuted instantiation: coap_layers.c:coap_encode_var_bytes Unexecuted instantiation: coap_mem.c:coap_encode_var_bytes |
122 | | |
123 | | #ifdef __cplusplus |
124 | | } |
125 | | #endif |
126 | | |
127 | | #endif /* COAP_ENCODE_H_ */ |