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