libcoap 4.3.5-develop-146e0bb
Loading...
Searching...
No Matches
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-2026 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#if defined(WITH_LWIP) && MEMP_USE_CUSTOM_POOLS
24 if (size >= MEMP_LEN_COAPSTRING) {
25 coap_log_crit("coap_new_string: size too large (%" PRIuS " +1 > MEMP_LEN_COAPSTRING)\n",
26 size);
27 return NULL;
28 }
29#endif /* WITH_LWIP && MEMP_USE_CUSTOM_POOLS */
30 /* Check no overflow (including a 8 byte small headroom) */
31 if (size > SIZE_MAX - sizeof(coap_str_bin_union_t) - 1 - 8) {
32 return NULL;
33 }
34
36 sizeof(coap_str_bin_union_t) + size + 1);
37 if (!s) {
38 coap_log_crit("coap_new_string: malloc: failed\n");
39 return NULL;
40 }
41
42 memset(s, 0, sizeof(coap_str_bin_union_t));
43 s->s = ((unsigned char *)s) + sizeof(coap_str_bin_union_t);
44 s->s[size] = '\000';
45 s->length = size;
46 return s;
47}
48
49void
53
55coap_new_str_const(const uint8_t *data, size_t size) {
57 if (!s)
58 return NULL;
59 memcpy(s->s, data, size);
60 s->length = size;
61 return (coap_str_const_t *)s;
62}
63
64void
68
70coap_make_str_const(const char *string) {
71 static int ofs = 0;
73 if (++ofs == COAP_MAX_STR_CONST_FUNC)
74 ofs = 0;
75 var[ofs].length = strlen(string);
76 var[ofs].s = (const uint8_t *)string;
77 return &var[ofs];
78}
79
81coap_new_binary(size_t size) {
82 return (coap_binary_t *)coap_new_string(size);
83}
84
87 /* Check no overflow (including a 8 byte small headroom) */
88 if (size > SIZE_MAX - sizeof(coap_str_bin_union_t) - 1 - 8) {
89 return NULL;
90 }
91
92#if defined(RIOT_VERSION) || defined(WITH_LWIP)
93 /* Unlikely to work as strings will not be large enough */
94 coap_binary_t *new = coap_new_binary(size);
95 if (new) {
96 if (s) {
97 memcpy(new->s, s->s, s->length);
99 }
100 }
101#else /* ! RIOT_VERSION && ! WITH_LWIP */
103 s,
104 sizeof(coap_str_bin_union_t) + size);
105#endif /* ! RIOT_VERSION && ! WITH_LWIP */
106 if (new) {
107 new->length = size;
108 new->s = ((unsigned char *)new) + sizeof(coap_str_bin_union_t);
109 }
110 return new;
111}
112
113void
117
119coap_new_bin_const(const uint8_t *data, size_t size) {
121 if (!s)
122 return NULL;
123 memcpy(s->s, data, size);
124 s->length = size;
125 return (coap_bin_const_t *)s;
126}
127
128void
#define PRIuS
Library specific build wrapper for coap_internal.h.
@ COAP_STRING
Definition coap_mem.h:33
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 NULL
Definition coap_option.h:30
#define coap_log_crit(...)
Definition coap_debug.h:96
#define COAP_MAX_STR_CONST_FUNC
Definition coap_str.h:181
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:129
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
Definition coap_str.c:65
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:81
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:70
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:119
coap_binary_t * coap_resize_binary(coap_binary_t *s, size_t size)
Resizes the given coap_binary_t object.
Definition coap_str.c:86
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
Definition coap_str.c:114
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_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:55
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition coap_str.c:50
CoAP binary data definition with const data.
Definition coap_str.h:65
CoAP binary data definition.
Definition coap_str.h:57
size_t length
length of binary data
Definition coap_str.h:58
uint8_t * s
binary data
Definition coap_str.h:59
CoAP string data definition with const data.
Definition coap_str.h:47
const uint8_t * s
read-only string data
Definition coap_str.h:49
size_t length
length of string
Definition coap_str.h:48
CoAP string data definition.
Definition coap_str.h:39
uint8_t * s
string data
Definition coap_str.h:41
size_t length
length of string
Definition coap_str.h:40
CoAP union for binary, string etc.
Definition coap_str.h:73