/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-2026 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 | 20.9k | coap_new_string(size_t size) { |
22 | 20.9k | coap_string_t *s; |
23 | | #if defined(WITH_LWIP) && MEMP_USE_CUSTOM_POOLS && ! MEM_USE_POOLS |
24 | | if (size >= MEMP_LEN_COAPSTRING) { |
25 | | coap_log_crit("coap_new_string: size too large (%" PRIuS " +1 > MEMP_LEN_COAPSTRING)\n", |
26 | | size); |
27 | | return NULL; |
28 | | } |
29 | | #endif /* WITH_LWIP && MEMP_USE_CUSTOM_POOLS */ |
30 | | /* Check no overflow (including a 8 byte small headroom) */ |
31 | 20.9k | if (size > SIZE_MAX - sizeof(coap_str_bin_union_t) - 1 - 8) { |
32 | 0 | return NULL; |
33 | 0 | } |
34 | | |
35 | 20.9k | s = (coap_string_t *)coap_malloc_type(COAP_STRING, |
36 | 20.9k | sizeof(coap_str_bin_union_t) + size + 1); |
37 | 20.9k | if (!s) { |
38 | 0 | coap_log_crit("coap_new_string: malloc: failed\n"); |
39 | 0 | return NULL; |
40 | 0 | } |
41 | | |
42 | 20.9k | memset(s, 0, sizeof(coap_str_bin_union_t)); |
43 | 20.9k | s->s = ((unsigned char *)s) + sizeof(coap_str_bin_union_t); |
44 | 20.9k | s->s[size] = '\000'; |
45 | 20.9k | s->length = size; |
46 | 20.9k | return s; |
47 | 20.9k | } |
48 | | |
49 | | void |
50 | 9.18k | coap_delete_string(coap_string_t *s) { |
51 | 9.18k | coap_free_type(COAP_STRING, s); |
52 | 9.18k | } |
53 | | |
54 | | coap_str_const_t * |
55 | 4.92k | coap_new_str_const(const uint8_t *data, size_t size) { |
56 | 4.92k | coap_string_t *s = coap_new_string(size); |
57 | 4.92k | if (!s) |
58 | 0 | return NULL; |
59 | 4.92k | memcpy(s->s, data, size); |
60 | 4.92k | s->length = size; |
61 | 4.92k | return (coap_str_const_t *)s; |
62 | 4.92k | } |
63 | | |
64 | | void |
65 | 9.84k | coap_delete_str_const(coap_str_const_t *s) { |
66 | 9.84k | coap_free_type(COAP_STRING, s); |
67 | 9.84k | } |
68 | | |
69 | | coap_str_const_t * |
70 | 2.46k | coap_make_str_const(const char *string) { |
71 | 2.46k | static int ofs = 0; |
72 | 2.46k | static coap_str_const_t var[COAP_MAX_STR_CONST_FUNC]; |
73 | 2.46k | if (++ofs == COAP_MAX_STR_CONST_FUNC) |
74 | 1.23k | ofs = 0; |
75 | 2.46k | var[ofs].length = strlen(string); |
76 | 2.46k | var[ofs].s = (const uint8_t *)string; |
77 | 2.46k | return &var[ofs]; |
78 | 2.46k | } |
79 | | |
80 | | coap_binary_t * |
81 | 5.80k | coap_new_binary(size_t size) { |
82 | 5.80k | return (coap_binary_t *)coap_new_string(size); |
83 | 5.80k | } |
84 | | |
85 | | coap_binary_t * |
86 | 0 | coap_resize_binary(coap_binary_t *s, size_t size) { |
87 | | /* Check no overflow (including a 8 byte small headroom) */ |
88 | 0 | if (size > SIZE_MAX - sizeof(coap_str_bin_union_t) - 1 - 8) { |
89 | 0 | return NULL; |
90 | 0 | } |
91 | | |
92 | | #if defined(RIOT_VERSION) || defined(WITH_LWIP) |
93 | | /* Unlikely to work as strings will not be large enough */ |
94 | | coap_binary_t *new = coap_new_binary(size); |
95 | | if (new) { |
96 | | if (s) { |
97 | | memcpy(new->s, s->s, s->length); |
98 | | coap_delete_binary(s); |
99 | | } |
100 | | } |
101 | | #else /* ! RIOT_VERSION && ! WITH_LWIP */ |
102 | 0 | coap_binary_t *new = coap_realloc_type(COAP_STRING, |
103 | 0 | s, |
104 | 0 | sizeof(coap_str_bin_union_t) + size); |
105 | 0 | #endif /* ! RIOT_VERSION && ! WITH_LWIP */ |
106 | 0 | if (new) { |
107 | 0 | new->length = size; |
108 | 0 | new->s = ((unsigned char *)new) + sizeof(coap_str_bin_union_t); |
109 | 0 | } |
110 | 0 | return new; |
111 | 0 | } |
112 | | |
113 | | void |
114 | 19.9k | coap_delete_binary(coap_binary_t *s) { |
115 | 19.9k | coap_free_type(COAP_STRING, s); |
116 | 19.9k | } |
117 | | |
118 | | coap_bin_const_t * |
119 | 7.38k | coap_new_bin_const(const uint8_t *data, size_t size) { |
120 | 7.38k | coap_string_t *s = coap_new_string(size); |
121 | 7.38k | if (!s) |
122 | 0 | return NULL; |
123 | 7.38k | if (data) |
124 | 7.38k | memcpy(s->s, data, size); |
125 | 7.38k | s->length = size; |
126 | 7.38k | return (coap_bin_const_t *)s; |
127 | 7.38k | } |
128 | | |
129 | | void |
130 | 50.6k | coap_delete_bin_const(coap_bin_const_t *s) { |
131 | 50.6k | coap_free_type(COAP_STRING, s); |
132 | 50.6k | } |