1 /*
2 * encode.h -- encoding and decoding of CoAP data types
3 *
4 * Copyright (C) 2010-2012 Olaf Bergmann <bergmann@tzi.org>
5 *
6 * This file is part of the CoAP library libcoap. Please see README for terms
7 * of use.
8 */
9
10 #ifndef COAP_ENCODE_H_
11 #define COAP_ENCODE_H_
12 #define BSD 199103
13 #if (BSD >= 199103) || defined(WITH_CONTIKI) || defined(_WIN32)
14 # include <string.h>
15 #else
16 # include <strings.h>
17 #endif
18
19 #include <stdint.h>
20
21 #define Nn 8 /* duplicate definition of N if built on sky motes */
22 #define ENCODE_HEADER_SIZE 4
23 #define HIBIT (1 << (Nn - 1))
24 #define EMASK ((1 << ENCODE_HEADER_SIZE) - 1)
25 #define MMASK ((1 << Nn) - 1 - EMASK)
26 #define MAX_VALUE ( (1 << Nn) - (1 << ENCODE_HEADER_SIZE) ) * (1 << ((1 << ENCODE_HEADER_SIZE) - 1))
27
28 #define COAP_PSEUDOFP_DECODE_8_4(r) (r < HIBIT ? r : (r & MMASK) << (r & EMASK))
29
30 #ifndef HAVE_FLS
31 /* include this only if fls() is not available */
32 extern int coap_fls(unsigned int i);
33 #else
34 #define coap_fls(i) fls(i)
35 #endif
36
37 #ifndef HAVE_FLSLL
38 /* include this only if flsll() is not available */
39 extern int coap_flsll(long long i);
40 #else
41 #define coap_flsll(i) flsll(i)
42 #endif
43
44 /* ls and s must be integer variables */
45 #define COAP_PSEUDOFP_ENCODE_8_4_DOWN(v,ls) (v < HIBIT ? v : (ls = coap_fls(v) - Nn, (v >> ls) & MMASK) + ls)
46 #define COAP_PSEUDOFP_ENCODE_8_4_UP(v,ls,s) (v < HIBIT ? v : (ls = coap_fls(v) - Nn, (s = (((v + ((1<<ENCODE_HEADER_SIZE<<ls)-1)) >> ls) & MMASK)), s == 0 ? HIBIT + ls + 1 : s + ls))
47
48 /**
49 * Decodes multiple-length byte sequences. @p buf points to an input byte
50 * sequence of length @p length. Returns the up to 4 byte decoded value.
51 *
52 * @param buf The input byte sequence to decode from
53 * @param length The length of the input byte sequence
54 *
55 * @return The decoded value
56 */
57 unsigned int coap_decode_var_bytes(const uint8_t *buf, unsigned int length);
58
59 /**
60 * Decodes multiple-length byte sequences. @p buf points to an input byte
61 * sequence of length @p length. Returns the up to 8 byte decoded value.
62 *
63 * @param buf The input byte sequence to decode from
64 * @param length The length of the input byte sequence
65 *
66 * @return The decoded value
67 */
68 uint64_t coap_decode_var_bytes8(const uint8_t *buf, unsigned int length);
69
70 /**
71 * Encodes multiple-length byte sequences. @p buf points to an output buffer of
72 * sufficient length to store the encoded bytes. @p value is the 4 byte value
73 * to encode.
74 * Returns the number of bytes used to encode @p value or 0 on error.
75 *
76 * @param buf The output buffer to encode into
77 * @param length The output buffer size to encode into (must be sufficient)
78 * @param value The value to encode into the buffer
79 *
80 * @return The number of bytes used to encode @p value or @c 0 on error.
81 */
82 unsigned int coap_encode_var_safe(uint8_t *buf,
83 size_t length,
84 unsigned int value);
85
86 /**
87 * Encodes multiple-length byte sequences. @p buf points to an output buffer of
88 * sufficient length to store the encoded bytes. @p value is the 8 byte value
89 * to encode.
90 * Returns the number of bytes used to encode @p value or 0 on error.
91 *
92 * @param buf The output buffer to encode into
93 * @param length The output buffer size to encode into (must be sufficient)
94 * @param value The value to encode into the buffer
95 *
96 * @return The number of bytes used to encode @p value or @c 0 on error.
97 */
98 unsigned int coap_encode_var_safe8(uint8_t *buf,
99 size_t length,
100 uint64_t value);
101
102 /**
103 * @deprecated Use coap_encode_var_safe() instead.
104 * Provided for backward compatibility. As @p value has a
105 * maximum value of 0xffffffff, and buf is usually defined as an array, it
106 * is unsafe to continue to use this variant if buf[] is less than buf[4].
107 *
108 * For example
109 * char buf[1],oops;
110 * ..
111 * coap_encode_var_bytes(buf, 0xfff);
112 * would cause oops to get overwritten. This error can only be found by code
113 * inspection.
114 * coap_encode_var_safe(buf, sizeof(buf), 0xfff);
115 * would catch this error at run-time and should be used instead.
116 */
117 COAP_STATIC_INLINE COAP_DEPRECATED int
coap_encode_var_bytes(uint8_t * buf,unsigned int value)118 coap_encode_var_bytes(uint8_t *buf, unsigned int value
119 ) {
120 return (int)coap_encode_var_safe(buf, sizeof(value), value);
121 }
122
123 #endif /* COAP_ENCODE_H_ */
124