libcoap 4.3.2rc2

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-2023 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
16#include "coap3/coap_internal.h"
17
18#include <stdio.h>
19
20 coap_string_t *
21 coap_new_string(size_t size) {
22 coap_string_t *s;
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);
31 s = (coap_string_t *)coap_malloc_type(COAP_STRING,
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
45 void
46 coap_delete_string(coap_string_t *s) {
47 coap_free_type(COAP_STRING, s);
48}
49
50 coap_str_const_t *
51 coap_new_str_const(const uint8_t *data, size_t size) {
52 coap_string_t *s = coap_new_string(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
60 void
61 coap_delete_str_const(coap_str_const_t *s) {
62 coap_free_type(COAP_STRING, s);
63}
64
65 coap_str_const_t *
66 coap_make_str_const(const char *string) {
67 static int ofs = 0;
68 static coap_str_const_t var[COAP_MAX_STR_CONST_FUNC];
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
76 coap_binary_t *
77 coap_new_binary(size_t size) {
78 return (coap_binary_t *)coap_new_string(size);
79}
80
81 coap_binary_t *
82 coap_resize_binary(coap_binary_t *s, size_t size) {
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);
89 coap_delete_binary(s);
90 }
91 }
92#else /* ! RIOT_VERSION && ! WITH_LWIP */
93 coap_binary_t *new = coap_realloc_type(COAP_STRING,
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
104 void
105 coap_delete_binary(coap_binary_t *s) {
106 coap_free_type(COAP_STRING, s);
107}
108
109 coap_bin_const_t *
110 coap_new_bin_const(const uint8_t *data, size_t size) {
111 coap_string_t *s = coap_new_string(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
119 void
120 coap_delete_bin_const(coap_bin_const_t *s) {
121 coap_free_type(COAP_STRING, s);
122}
coap_internal.h
Pulls together all the internal only header files.
COAP_STRING
@ COAP_STRING
Definition: coap_mem.h:38
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:162
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_string_t
struct coap_string_t coap_string_t
CoAP string data definition.
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