libcoap  4.3.0
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 
11 #include "coap3/coap_internal.h"
12 
13 #include <stdio.h>
14 
16  coap_string_t *s;
17 #ifdef WITH_LWIP
18  if (size >= MEMP_LEN_COAPSTRING) {
20  "coap_new_string: size too large (%zu +1 > MEMP_LEN_COAPSTRING)\n",
21  size);
22  return NULL;
23  }
24 #endif /* WITH_LWIP */
25  assert(size+1 != 0);
27  sizeof(coap_string_t) + size + 1);
28  if ( !s ) {
29  coap_log(LOG_CRIT, "coap_new_string: malloc: failed\n");
30  return NULL;
31  }
32 
33  memset(s, 0, sizeof(coap_string_t));
34  s->s = ((unsigned char *)s) + sizeof(coap_string_t);
35  s->s[size] = '\000';
36  s->length = size;
37  return s;
38 }
39 
42 }
43 
44 coap_str_const_t *coap_new_str_const(const uint8_t *data, size_t size) {
45  coap_string_t *s = coap_new_string(size);
46  if (!s)
47  return NULL;
48  memcpy (s->s, data, size);
49  s->length = size;
50  return (coap_str_const_t *)s;
51 }
52 
55 }
56 
58 {
59  static int ofs = 0;
61  if (++ofs == COAP_MAX_STR_CONST_FUNC) ofs = 0;
62  var[ofs].length = strlen(string);
63  var[ofs].s = (const uint8_t *)string;
64  return &var[ofs];
65 }
66 
68  return (coap_binary_t *)coap_new_string(size);
69 }
70 
72 #if defined(RIOT_VERSION) || defined(WITH_CONTIKI) || defined(WITH_LWIP)
73  /* Unlikely to work as strings will not be large enough */
74  coap_binary_t *new = coap_new_binary(size);
75  if (new) {
76  memcpy(new->s, s->s, s->length);
78  }
79 #else /* ! RIOT_VERSION && ! WITH_CONTIKI && ! WITH_LWIP */
81  s,
82  sizeof(coap_binary_t) + size);
83 #endif /* ! RIOT_VERSION && ! WITH_CONTIKI && ! WITH_LWIP */
84  if (new) {
85  new->length = size;
86  new->s = ((unsigned char *)new) + sizeof(coap_string_t);
87  }
88  return new;
89 }
90 
93 }
94 
95 coap_bin_const_t *coap_new_bin_const(const uint8_t *data, size_t size) {
96  coap_string_t *s = coap_new_string(size);
97  if (!s)
98  return NULL;
99  memcpy (s->s, data, size);
100  s->length = size;
101  return (coap_bin_const_t *)s;
102 }
103 
106 }
107 
Pulls together all the internal only header files.
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:152
@ LOG_CRIT
Critical.
Definition: coap_debug.h:54
#define COAP_MAX_STR_CONST_FUNC
Definition: str.h:156
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:15
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
Definition: str.c:104
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
Definition: str.c:53
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
Definition: str.c:91
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:95
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
Definition: str.c:67
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:44
struct coap_string_t coap_string_t
CoAP string data definition.
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition: str.c:40
coap_binary_t * coap_resize_binary(coap_binary_t *s, size_t size)
Resizes the given coap_binary_t object.
Definition: str.c:71
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:57
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_STRING
Definition: mem.h:32
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_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: str.h:58
CoAP binary data definition.
Definition: str.h:50
size_t length
length of binary data
Definition: str.h:51
uint8_t * s
binary data
Definition: str.h:52
CoAP string data definition with const data.
Definition: str.h:40
const uint8_t * s
read-only string data
Definition: str.h:42
size_t length
length of string
Definition: str.h:41
CoAP string data definition.
Definition: str.h:32
uint8_t * s
string data
Definition: str.h:34
size_t length
length of string
Definition: str.h:33