• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
coap_new_string(size_t size)15 coap_string_t *coap_new_string(size_t size) {
16   coap_string_t *s;
17 #ifdef WITH_LWIP
18   if (size >= MEMP_LEN_COAPSTRING) {
19     coap_log(LOG_CRIT,
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);
26   s = (coap_string_t *)coap_malloc_type(COAP_STRING,
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 
coap_delete_string(coap_string_t * s)40 void coap_delete_string(coap_string_t *s) {
41   coap_free_type(COAP_STRING, s);
42 }
43 
coap_new_str_const(const uint8_t * data,size_t size)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 
coap_delete_str_const(coap_str_const_t * s)53 void coap_delete_str_const(coap_str_const_t *s) {
54   coap_free_type(COAP_STRING, s);
55 }
56 
coap_make_str_const(const char * string)57 coap_str_const_t *coap_make_str_const(const char *string)
58 {
59   static int ofs = 0;
60   static coap_str_const_t var[COAP_MAX_STR_CONST_FUNC];
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 
coap_new_binary(size_t size)67 coap_binary_t *coap_new_binary(size_t size) {
68   return (coap_binary_t *)coap_new_string(size);
69 }
70 
coap_resize_binary(coap_binary_t * s,size_t size)71 coap_binary_t *coap_resize_binary(coap_binary_t *s, size_t size) {
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);
77     coap_delete_binary(s);
78   }
79 #else /* ! RIOT_VERSION && ! WITH_CONTIKI && ! WITH_LWIP */
80   coap_binary_t *new = coap_realloc_type(COAP_STRING,
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 
coap_delete_binary(coap_binary_t * s)91 void coap_delete_binary(coap_binary_t *s) {
92   coap_free_type(COAP_STRING, s);
93 }
94 
coap_new_bin_const(const uint8_t * data,size_t size)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 
coap_delete_bin_const(coap_bin_const_t * s)104 void coap_delete_bin_const(coap_bin_const_t *s) {
105   coap_free_type(COAP_STRING, s);
106 }
107 
108