libcoap 4.3.1
str.c
Go to the documentation of this file.
1/* str.c -- strings to be used in the CoAP library
2 *
3 * Copyright (C) 2010,2011 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
22#ifdef WITH_LWIP
23 if (size >= MEMP_LEN_COAPSTRING) {
25 "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
47}
48
49coap_str_const_t *coap_new_str_const(const uint8_t *data, size_t size) {
51 if (!s)
52 return NULL;
53 memcpy (s->s, data, size);
54 s->length = size;
55 return (coap_str_const_t *)s;
56}
57
60}
61
63{
64 static int ofs = 0;
66 if (++ofs == COAP_MAX_STR_CONST_FUNC) ofs = 0;
67 var[ofs].length = strlen(string);
68 var[ofs].s = (const uint8_t *)string;
69 return &var[ofs];
70}
71
73 return (coap_binary_t *)coap_new_string(size);
74}
75
77#if defined(RIOT_VERSION) || defined(WITH_CONTIKI) || defined(WITH_LWIP)
78 /* Unlikely to work as strings will not be large enough */
79 coap_binary_t *new = coap_new_binary(size);
80 if (new) {
81 memcpy(new->s, s->s, s->length);
83 }
84#else /* ! RIOT_VERSION && ! WITH_CONTIKI && ! WITH_LWIP */
86 s,
87 sizeof(coap_binary_t) + size);
88#endif /* ! RIOT_VERSION && ! WITH_CONTIKI && ! WITH_LWIP */
89 if (new) {
90 new->length = size;
91 new->s = ((unsigned char *)new) + sizeof(coap_string_t);
92 }
93 return new;
94}
95
98}
99
100coap_bin_const_t *coap_new_bin_const(const uint8_t *data, size_t size) {
102 if (!s)
103 return NULL;
104 memcpy (s->s, data, size);
105 s->length = size;
106 return (coap_bin_const_t *)s;
107}
108
111}
112
Pulls together all the internal only header files.
#define coap_log_crit(...)
Definition: coap_debug.h:43
#define COAP_MAX_STR_CONST_FUNC
Definition: coap_str.h:162
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
Definition: str.c:109
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
Definition: str.c:58
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
Definition: str.c:72
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: str.c:62
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: str.c:100
coap_binary_t * coap_resize_binary(coap_binary_t *s, size_t size)
Resizes the given coap_binary_t object.
Definition: str.c:76
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
Definition: str.c:96
coap_string_t * coap_new_string(size_t size)
Returns a new string object with at least size+1 bytes storage allocated.
Definition: str.c:20
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: str.c:49
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition: str.c:45
@ COAP_STRING
Definition: mem.h:37
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().
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