BSL v0.0.0
AMMOS Bundle Protocol Security Library (BSL)
Loading...
Searching...
No Matches
UtilDefs_Data.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 The Johns Hopkins University Applied Physics
3 * Laboratory LLC.
4 *
5 * This file is part of the Bundle Protocol Security Library (BSL).
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * This work was performed for the Jet Propulsion Laboratory, California
18 * Institute of Technology, sponsored by the United States Government under
19 * the prime contract 80NM0018D0004 between the Caltech and NASA under
20 * subcontract 1700763.
21 */
26#include <string.h>
27
28#include <BPSecLib_Private.h>
29
30static void bsl_data_int_reset(BSL_Data_t *data)
31{
32 ASSERT_ARG_NONNULL(data);
33
34 data->owned = false;
35 data->ptr = NULL;
36 data->len = 0;
37 memset(data, 0, sizeof(*data));
38}
39
40static void bsl_data_int_free(BSL_Data_t *data)
41{
42 ASSERT_ARG_NONNULL(data);
43
44 if (data->owned && data->ptr)
45 {
46 BSL_FREE(data->ptr);
47 }
48}
49
51{
52 CHK_ARG_NONNULL(data);
53 bsl_data_int_reset(data);
54 return BSL_SUCCESS;
55}
56
57int BSL_Data_InitBuffer(BSL_Data_t *data, size_t bytelen)
58{
59 CHK_ARG_NONNULL(data);
60 CHK_ARG_EXPR(bytelen > 0);
61
62 bsl_data_int_reset(data);
63 data->ptr = BSL_MALLOC(bytelen);
64 data->len = bytelen;
65 data->owned = true;
66 memset(data->ptr, 0, bytelen);
67
68 CHK_POSTCONDITION(data->ptr != NULL);
69 return BSL_SUCCESS;
70}
71
72int BSL_Data_InitView(BSL_Data_t *data, size_t len, const BSL_DataPtr_t src)
73{
74 CHK_ARG_NONNULL(data);
75 CHK_ARG_EXPR(len > 0);
76 CHK_ARG_NONNULL(src);
77
78 data->owned = false;
79 data->ptr = src;
80 data->len = len;
81 return BSL_SUCCESS;
82}
83
85{
86 ASSERT_ARG_NONNULL(data);
87 ASSERT_ARG_NONNULL(src);
88 *data = *src;
89 bsl_data_int_reset(src);
90}
91
93{
94 CHK_ARG_NONNULL(data);
95 bsl_data_int_free(data);
96 memset(data, 0, sizeof(*data));
97 return BSL_SUCCESS;
98}
99
101{
102 CHK_ARG_NONNULL(data);
103
104 if (len)
105 {
106 if (!data->owned)
107 {
108 data->ptr = NULL;
109 data->owned = true;
110 }
111 int ecode = BSL_Data_Resize(data, len);
112 if (ecode < 0)
113 {
114 BSL_LOG_ERR("Failed to resize data to %lu bytes", len);
115 return ecode;
116 }
117 }
118 else
119 {
120 bsl_data_int_reset(data);
121 }
122
123 if (data->ptr && src)
124 {
125 memcpy(data->ptr, src, len);
126 }
127
128 return BSL_SUCCESS;
129}
130
131int BSL_Data_Resize(BSL_Data_t *data, size_t len)
132{
133 CHK_ARG_NONNULL(data);
134
135 if (len == data->len)
136 {
137 return BSL_SUCCESS;
138 }
139
140 if (len == 0)
141 {
142 bsl_data_int_free(data);
143 bsl_data_int_reset(data);
144 return BSL_SUCCESS;
145 }
146
147 if (!data->owned)
148 {
149 data->ptr = NULL;
150 }
151 BSL_DataPtr_t got = BSL_REALLOC(data->ptr, len);
152 if (UNLIKELY(!got))
153 {
154 bsl_data_int_reset(data);
155 BSL_LOG_ERR("Failed to realloc");
157 }
158 data->owned = true;
159 data->ptr = got;
160 data->len = len;
161 return BSL_SUCCESS;
162}
163
165{
166 CHK_ARG_NONNULL(data);
167 CHK_ARG_EXPR(len > 0);
168 CHK_ARG_NONNULL(src);
169
170 int ecode = 0;
171 if ((ecode = BSL_Data_Resize(data, data->len + len)) < 0)
172 {
173 BSL_LOG_ERR("Failed to resize");
174 return ecode;
175 }
176 memcpy(&data->ptr[data->len - len], src, len);
177 return BSL_SUCCESS;
178}
Single entry-point include file for all of the BPSec Lib (BSL) frontend API.
uint8_t * BSL_DataPtr_t
Data pointer for BSL_Data_t.
const uint8_t * BSL_DataConstPtr_t
Pointer to constant data for BSL_Data_t.
#define UNLIKELY(expr)
Hint to the compiler that the expression is expected to evaluate to false and the associated branch i...
#define BSL_LOG_ERR(...)
This is an overloaded member function, provided for convenience. It differs from the above function o...
@ BSL_ERR_INSUFFICIENT_SPACE
Insufficient space to complete.
@ BSL_SUCCESS
Placeholder for non-error code.
int BSL_Data_Resize(BSL_Data_t *data, size_t len)
Resize the data, copying if necessary.
void BSL_Data_InitMove(BSL_Data_t *data, BSL_Data_t *src)
This is an overloaded member function, provided for convenience. It differs from the above function o...
int BSL_Data_AppendFrom(BSL_Data_t *data, size_t len, BSL_DataConstPtr_t src)
Append an initialized data struct with a given size.
int BSL_Data_Deinit(BSL_Data_t *data)
De-initialize a data struct, freeing if necessary.
int BSL_Data_InitBuffer(BSL_Data_t *data, size_t bytelen)
Initialize with an owned buffer of size bytelen.
int BSL_Data_InitView(BSL_Data_t *data, size_t len, const BSL_DataPtr_t src)
Initialize a data struct as an overlay on optional external data.
int BSL_Data_CopyFrom(BSL_Data_t *data, size_t len, BSL_DataConstPtr_t src)
Set an initialized data struct to a given size.
int BSL_Data_Init(BSL_Data_t *data)
Initialize an empty data struct.
Heap data storage and views.
size_t len
Size of the data buffer.
BSL_DataPtr_t ptr
Pointer to the front of the buffer.
bool owned
True if this data is a copy.