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