libcoap 4.3.5-develop-19cef11
coap_str.c
Go to the documentation of this file.
1/* coap_str.c -- strings to be used in the CoAP library
2 *
3 * Copyright (C) 2010,2011,2022-2024 Olaf Bergmann <bergmann@tzi.org>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 *
7 * This file is part of the CoAP library libcoap. Please see
8 * README for terms of use.
9 */
10
17
18#include <stdio.h>
19
21coap_new_string(size_t size) {
23#ifdef WITH_LWIP
24 if (size >= MEMP_LEN_COAPSTRING) {
25 coap_log_crit("coap_new_string: size too large (%zu +1 > MEMP_LEN_COAPSTRING)\n",
26 size);
27 return NULL;
28 }
29#endif /* WITH_LWIP */
30 assert(size+1 != 0);
32 sizeof(coap_string_t) + size + 1);
33 if (!s) {
34 coap_log_crit("coap_new_string: malloc: failed\n");
35 return NULL;
36 }
37
38 memset(s, 0, sizeof(coap_string_t));
39 s->s = ((unsigned char *)s) + sizeof(coap_string_t);
40 s->s[size] = '\000';
41 s->length = size;
42 return s;
43}
44
45void
48}
49
51coap_new_str_const(const uint8_t *data, size_t size) {
53 if (!s)
54 return NULL;
55 memcpy(s->s, data, size);
56 s->length = size;
57 return (coap_str_const_t *)s;
58}
59
60void
63}
64
66coap_make_str_const(const char *string) {
67 static int ofs = 0;
69 if (++ofs == COAP_MAX_STR_CONST_FUNC)
70 ofs = 0;
71 var[ofs].length = strlen(string);
72 var[ofs].s = (const uint8_t *)string;
73 return &var[ofs];
74}
75
77coap_new_binary(size_t size) {
78 return (coap_binary_t *)coap_new_string(size);
79}
80
83#if defined(RIOT_VERSION) || defined(WITH_LWIP)
84 /* Unlikely to work as strings will not be large enough */
85 coap_binary_t *new = coap_new_binary(size);
86 if (new) {
87 if (s) {
88 memcpy(new->s, s->s, s->length);
90 }
91 }
92#else /* ! RIOT_VERSION && ! WITH_LWIP */
94 s,
95 sizeof(coap_binary_t) + size);
96#endif /* ! RIOT_VERSION && ! WITH_LWIP */
97 if (new) {
98 new->length = size;
99 new->s = ((unsigned char *)new) + sizeof(coap_string_t);
100 }
101 return new;
102}
103
104void
107}
108
110coap_new_bin_const(const uint8_t *data, size_t size) {
112 if (!s)
113 return NULL;
114 memcpy(s->s, data, size);
115 s->length = size;
116 return (coap_bin_const_t *)s;
117}
118
119void
122}
Library specific build wrapper for coap_internal.h.
@ COAP_STRING
Definition: coap_mem.h:39
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...
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.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
#define coap_log_crit(...)
Definition: coap_debug.h:90
#define COAP_MAX_STR_CONST_FUNC
Definition: coap_str.h:170
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
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_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_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_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_binary_t * coap_resize_binary(coap_binary_t *s, size_t size)
Resizes the given coap_binary_t object.
Definition: coap_str.c:82
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_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
struct coap_string_t coap_string_t
CoAP string data definition.
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
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition: coap_str.c:46
CoAP binary data definition with const data.
Definition: coap_str.h:64
CoAP binary data definition.
Definition: coap_str.h:56
size_t length
length of binary data
Definition: coap_str.h:57
uint8_t * s
binary data
Definition: coap_str.h:58
CoAP string data definition with const data.
Definition: coap_str.h:46
const uint8_t * s
read-only string data
Definition: coap_str.h:48
size_t length
length of string
Definition: coap_str.h:47
CoAP string data definition.
Definition: coap_str.h:38
uint8_t * s
string data
Definition: coap_str.h:40
size_t length
length of string
Definition: coap_str.h:39