Loading...
Searching...
No Matches
coap_str.c
Go to the documentation of this file.
/* coap_str.c -- strings to be used in the CoAP library
*
* Copyright (C) 2010,2011,2022-2024 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#include "coap3/coap_libcoap_build.h"
#include <stdio.h>
coap_string_t *
coap_new_string(size_t size) {
coap_string_t *s;
#ifdef WITH_LWIP
if (size >= MEMP_LEN_COAPSTRING) {
coap_log_crit("coap_new_string: size too large (%zu +1 > MEMP_LEN_COAPSTRING)\n",
size);
return NULL;
}
#endif /* WITH_LWIP */
assert(size+1 != 0);
s = (coap_string_t *)coap_malloc_type(COAP_STRING,
sizeof(coap_string_t) + size + 1);
if (!s) {
coap_log_crit("coap_new_string: malloc: failed\n");
return NULL;
}
memset(s, 0, sizeof(coap_string_t));
s->s = ((unsigned char *)s) + sizeof(coap_string_t);
s->s[size] = '\000';
s->length = size;
return s;
}
void
coap_delete_string(coap_string_t *s) {
coap_free_type(COAP_STRING, s);
}
coap_str_const_t *
coap_new_str_const(const uint8_t *data, size_t size) {
coap_string_t *s = coap_new_string(size);
if (!s)
return NULL;
memcpy(s->s, data, size);
s->length = size;
return (coap_str_const_t *)s;
}
void
coap_delete_str_const(coap_str_const_t *s) {
coap_free_type(COAP_STRING, s);
}
coap_str_const_t *
coap_make_str_const(const char *string) {
static int ofs = 0;
static coap_str_const_t var[COAP_MAX_STR_CONST_FUNC];
if (++ofs == COAP_MAX_STR_CONST_FUNC)
ofs = 0;
var[ofs].length = strlen(string);
var[ofs].s = (const uint8_t *)string;
return &var[ofs];
}
coap_binary_t *
coap_new_binary(size_t size) {
return (coap_binary_t *)coap_new_string(size);
}
coap_binary_t *
coap_resize_binary(coap_binary_t *s, size_t size) {
#if defined(RIOT_VERSION) || defined(WITH_LWIP)
/* Unlikely to work as strings will not be large enough */
coap_binary_t *new = coap_new_binary(size);
if (new) {
if (s) {
memcpy(new->s, s->s, s->length);
coap_delete_binary(s);
}
}
#else /* ! RIOT_VERSION && ! WITH_LWIP */
coap_binary_t *new = coap_realloc_type(COAP_STRING,
s,
sizeof(coap_binary_t) + size);
#endif /* ! RIOT_VERSION && ! WITH_LWIP */
if (new) {
new->length = size;
new->s = ((unsigned char *)new) + sizeof(coap_string_t);
}
return new;
}
void
coap_delete_binary(coap_binary_t *s) {
coap_free_type(COAP_STRING, s);
}
coap_bin_const_t *
coap_new_bin_const(const uint8_t *data, size_t size) {
coap_string_t *s = coap_new_string(size);
if (!s)
return NULL;
memcpy(s->s, data, size);
s->length = size;
return (coap_bin_const_t *)s;
}
void
coap_delete_bin_const(coap_bin_const_t *s) {
coap_free_type(COAP_STRING, s);
}
coap_libcoap_build.h
Library specific build wrapper for coap_internal.h.
COAP_STRING
@ COAP_STRING
Definition coap_mem.h:39
coap_realloc_type
void * coap_realloc_type(coap_memory_tag_t type, void *p, size_t size)
Reallocates a chunk p of bytes created by coap_malloc_type() or coap_realloc_type() and returns a poi...
coap_malloc_type
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
coap_free_type
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
coap_log_crit
#define coap_log_crit(...)
Definition coap_debug.h:90
COAP_MAX_STR_CONST_FUNC
#define COAP_MAX_STR_CONST_FUNC
Definition coap_str.h:170
coap_delete_bin_const
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
Definition coap_str.c:120
coap_delete_str_const
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
Definition coap_str.c:61
coap_new_binary
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
Definition coap_str.c:77
coap_make_str_const
coap_str_const_t * coap_make_str_const(const char *string)
Take the specified byte array (text) and create a coap_str_const_t *.
Definition coap_str.c:66
coap_new_bin_const
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
Definition coap_str.c:110
coap_resize_binary
coap_binary_t * coap_resize_binary(coap_binary_t *s, size_t size)
Resizes the given coap_binary_t object.
Definition coap_str.c:82
coap_delete_binary
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
Definition coap_str.c:105
coap_new_string
coap_string_t * coap_new_string(size_t size)
Returns a new string object with at least size+1 bytes storage allocated.
Definition coap_str.c:21
coap_new_str_const
coap_str_const_t * coap_new_str_const(const uint8_t *data, size_t size)
Returns a new const string object with at least size+1 bytes storage allocated, and the provided data...
Definition coap_str.c:51
coap_delete_string
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition coap_str.c:46
coap_bin_const_t
CoAP binary data definition with const data.
Definition coap_str.h:64
coap_binary_t
CoAP binary data definition.
Definition coap_str.h:56
coap_binary_t::length
size_t length
length of binary data
Definition coap_str.h:57
coap_binary_t::s
uint8_t * s
binary data
Definition coap_str.h:58
coap_str_const_t
CoAP string data definition with const data.
Definition coap_str.h:46
coap_str_const_t::s
const uint8_t * s
read-only string data
Definition coap_str.h:48
coap_str_const_t::length
size_t length
length of string
Definition coap_str.h:47
coap_string_t
CoAP string data definition.
Definition coap_str.h:38
coap_string_t::s
uint8_t * s
string data
Definition coap_str.h:40
coap_string_t::length
size_t length
length of string
Definition coap_str.h:39