/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-2025 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 | | #if (defined(BSD) && (BSD >= 199103)) || defined(_WIN32) |
21 | | # include <string.h> |
22 | | #else |
23 | | # include <strings.h> |
24 | | #endif |
25 | | |
26 | | #include <stdint.h> |
27 | | |
28 | | #ifdef __cplusplus |
29 | | extern "C" { |
30 | | #endif |
31 | | |
32 | | #ifndef HAVE_FLS |
33 | | /* include this only if fls() is not available */ |
34 | | extern int coap_fls(unsigned int i); |
35 | | #else |
36 | | #define coap_fls(i) fls(i) |
37 | | #endif |
38 | | |
39 | | #ifndef HAVE_FLSLL |
40 | | /* include this only if flsll() is not available */ |
41 | | extern int coap_flsll(long long i); |
42 | | #else |
43 | | #define coap_flsll(i) flsll(i) |
44 | | #endif |
45 | | |
46 | | /** |
47 | | * @ingroup application_api |
48 | | * @defgroup encode Encode / Decode API |
49 | | * API for endoding/decoding CoAP options. |
50 | | * @{ |
51 | | */ |
52 | | |
53 | | /** |
54 | | * Decodes multiple-length byte sequences. @p buf points to an input byte |
55 | | * sequence of length @p length. Returns the up to 4 byte decoded value. |
56 | | * |
57 | | * @param buf The input byte sequence to decode from |
58 | | * @param length The length of the input byte sequence |
59 | | * |
60 | | * @return The decoded value |
61 | | */ |
62 | | unsigned int coap_decode_var_bytes(const uint8_t *buf, size_t length); |
63 | | |
64 | | /** |
65 | | * Decodes multiple-length byte sequences. @p buf points to an input byte |
66 | | * sequence of length @p length. Returns the up to 8 byte decoded value. |
67 | | * |
68 | | * @param buf The input byte sequence to decode from |
69 | | * @param length The length of the input byte sequence |
70 | | * |
71 | | * @return The decoded value |
72 | | */ |
73 | | uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t length); |
74 | | |
75 | | /** |
76 | | * Encodes multiple-length byte sequences. @p buf points to an output buffer of |
77 | | * sufficient length to store the encoded bytes. @p value is the 4 byte value |
78 | | * to encode. |
79 | | * Returns the number of bytes used to encode @p value or 0 on error. |
80 | | * |
81 | | * @param buf The output buffer to encode into |
82 | | * @param length The output buffer size to encode into (must be sufficient) |
83 | | * @param value The value to encode into the buffer |
84 | | * |
85 | | * @return The number of bytes used to encode @p value (which can be 0 |
86 | | * when encoding value of 0) or @c 0 on error. |
87 | | */ |
88 | | unsigned int coap_encode_var_safe(uint8_t *buf, |
89 | | size_t length, |
90 | | unsigned int value); |
91 | | |
92 | | /** |
93 | | * Encodes multiple-length byte sequences. @p buf points to an output buffer of |
94 | | * sufficient length to store the encoded bytes. @p value is the 8 byte value |
95 | | * to encode. |
96 | | * Returns the number of bytes used to encode @p value or 0 on error. |
97 | | * |
98 | | * @param buf The output buffer to encode into |
99 | | * @param length The output buffer size to encode into (must be sufficient) |
100 | | * @param value The value to encode into the buffer |
101 | | * |
102 | | * @return The number of bytes used to encode @p value (which can be 0 |
103 | | * when encoding value of 0) or @c 0 on error. |
104 | | */ |
105 | | unsigned int coap_encode_var_safe8(uint8_t *buf, |
106 | | size_t length, |
107 | | uint64_t value); |
108 | | |
109 | | /** @} */ |
110 | | |
111 | | /** |
112 | | * @deprecated Use coap_encode_var_safe() instead. |
113 | | * Provided for backward compatibility. As @p value has a |
114 | | * maximum value of 0xffffffff, and buf is usually defined as an array, it |
115 | | * is unsafe to continue to use this variant if buf[] is less than buf[4]. |
116 | | * |
117 | | * For example |
118 | | * char buf[1],oops; |
119 | | * .. |
120 | | * coap_encode_var_bytes(buf, 0xfff); |
121 | | * would cause oops to get overwritten. This error can only be found by code |
122 | | * inspection. |
123 | | * coap_encode_var_safe(buf, sizeof(buf), 0xfff); |
124 | | * would catch this error at run-time and should be used instead. |
125 | | */ |
126 | | COAP_STATIC_INLINE COAP_DEPRECATED int |
127 | 0 | coap_encode_var_bytes(uint8_t *buf, unsigned int value) { |
128 | 0 | return (int)coap_encode_var_safe(buf, sizeof(value), value); |
129 | 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_notls.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_sha1.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_threadsafe.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_hashkey.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 |
130 | | |
131 | | #ifdef __cplusplus |
132 | | } |
133 | | #endif |
134 | | |
135 | | #endif /* COAP_ENCODE_H_ */ |