/src/libcoap/src/coap_str.c
Line | Count | Source |
1 | | /* coap_str.c -- strings to be used in the CoAP library |
2 | | * |
3 | | * Copyright (C) 2010,2011,2022-2025 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_libcoap_build.h" |
17 | | |
18 | | #include <stdio.h> |
19 | | |
20 | | coap_string_t * |
21 | 7.24k | coap_new_string(size_t size) { |
22 | 7.24k | coap_string_t *s; |
23 | | #if defined(WITH_LWIP) && MEMP_USE_CUSTOM_POOLS |
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 && MEMP_USE_CUSTOM_POOLS */ |
30 | 7.24k | assert(size+1 != 0); |
31 | 7.24k | s = (coap_string_t *)coap_malloc_type(COAP_STRING, |
32 | 7.24k | sizeof(coap_string_t) + size + 1); |
33 | 7.24k | if (!s) { |
34 | 0 | coap_log_crit("coap_new_string: malloc: failed\n"); |
35 | 0 | return NULL; |
36 | 0 | } |
37 | | |
38 | 7.24k | memset(s, 0, sizeof(coap_string_t)); |
39 | 7.24k | s->s = ((unsigned char *)s) + sizeof(coap_string_t); |
40 | 7.24k | s->s[size] = '\000'; |
41 | 7.24k | s->length = size; |
42 | 7.24k | return s; |
43 | 7.24k | } |
44 | | |
45 | | void |
46 | 14.2k | coap_delete_string(coap_string_t *s) { |
47 | 14.2k | coap_free_type(COAP_STRING, s); |
48 | 14.2k | } |
49 | | |
50 | | coap_str_const_t * |
51 | 0 | coap_new_str_const(const uint8_t *data, size_t size) { |
52 | 0 | coap_string_t *s = coap_new_string(size); |
53 | 0 | if (!s) |
54 | 0 | return NULL; |
55 | 0 | memcpy(s->s, data, size); |
56 | 0 | s->length = size; |
57 | 0 | return (coap_str_const_t *)s; |
58 | 0 | } |
59 | | |
60 | | void |
61 | 0 | coap_delete_str_const(coap_str_const_t *s) { |
62 | 0 | coap_free_type(COAP_STRING, s); |
63 | 0 | } |
64 | | |
65 | | coap_str_const_t * |
66 | 0 | coap_make_str_const(const char *string) { |
67 | 0 | static int ofs = 0; |
68 | 0 | static coap_str_const_t var[COAP_MAX_STR_CONST_FUNC]; |
69 | 0 | if (++ofs == COAP_MAX_STR_CONST_FUNC) |
70 | 0 | ofs = 0; |
71 | 0 | var[ofs].length = strlen(string); |
72 | 0 | var[ofs].s = (const uint8_t *)string; |
73 | 0 | return &var[ofs]; |
74 | 0 | } |
75 | | |
76 | | coap_binary_t * |
77 | 0 | coap_new_binary(size_t size) { |
78 | 0 | return (coap_binary_t *)coap_new_string(size); |
79 | 0 | } |
80 | | |
81 | | coap_binary_t * |
82 | 0 | coap_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 | 0 | coap_binary_t *new = coap_realloc_type(COAP_STRING, |
94 | 0 | s, |
95 | 0 | sizeof(coap_binary_t) + size); |
96 | 0 | #endif /* ! RIOT_VERSION && ! WITH_LWIP */ |
97 | 0 | if (new) { |
98 | 0 | new->length = size; |
99 | 0 | new->s = ((unsigned char *)new) + sizeof(coap_string_t); |
100 | 0 | } |
101 | 0 | return new; |
102 | 0 | } |
103 | | |
104 | | void |
105 | 10.9k | coap_delete_binary(coap_binary_t *s) { |
106 | 10.9k | coap_free_type(COAP_STRING, s); |
107 | 10.9k | } |
108 | | |
109 | | coap_bin_const_t * |
110 | 0 | coap_new_bin_const(const uint8_t *data, size_t size) { |
111 | 0 | coap_string_t *s = coap_new_string(size); |
112 | 0 | if (!s) |
113 | 0 | return NULL; |
114 | 0 | memcpy(s->s, data, size); |
115 | 0 | s->length = size; |
116 | 0 | return (coap_bin_const_t *)s; |
117 | 0 | } |
118 | | |
119 | | void |
120 | 0 | coap_delete_bin_const(coap_bin_const_t *s) { |
121 | 0 | coap_free_type(COAP_STRING, s); |
122 | 0 | } |