xref: /third_party/libcoap/src/coap_str.c (revision c87c5fba)
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
11/**
12 * @file coap_str.c
13 * @brief String handling functions
14 */
15
16#include "coap3/coap_internal.h"
17
18#include <stdio.h>
19
20coap_string_t *
21coap_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
45void
46coap_delete_string(coap_string_t *s) {
47  coap_free_type(COAP_STRING, s);
48}
49
50coap_str_const_t *
51coap_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
60void
61coap_delete_str_const(coap_str_const_t *s) {
62  coap_free_type(COAP_STRING, s);
63}
64
65coap_str_const_t *
66coap_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
76coap_binary_t *
77coap_new_binary(size_t size) {
78  return (coap_binary_t *)coap_new_string(size);
79}
80
81coap_binary_t *
82coap_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
104void
105coap_delete_binary(coap_binary_t *s) {
106  coap_free_type(COAP_STRING, s);
107}
108
109coap_bin_const_t *
110coap_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
119void
120coap_delete_bin_const(coap_bin_const_t *s) {
121  coap_free_type(COAP_STRING, s);
122}
123