BSL v0.0.0
AMMOS Bundle Protocol Security Library (BSL)
Loading...
Searching...
No Matches
UtilDefs_SeqReadWrite.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 */
27#include <math.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31
32#include <BPSecLib_Private.h>
33
35
37{
38 // nothing to do here
39 return BSL_SUCCESS;
40}
41
42int BSL_SeqReader_InitFlat(BSL_SeqReader_t *obj, const uint8_t *buf, size_t bufsize)
43{
44 CHK_ARG_NONNULL(obj);
45 CHK_ARG_NONNULL(buf);
46 // CHK_ARG_EXPR(bufsize > 0);
47
48 obj->cursor = buf;
49 obj->remain = bufsize;
50
51 return BSL_SUCCESS;
52}
53
54int BSL_SeqReader_Get(BSL_SeqReader_t *obj, uint8_t *buf, size_t *bufsize)
55{
56 CHK_ARG_NONNULL(obj);
57 CHK_ARG_NONNULL(buf);
58 CHK_ARG_NONNULL(bufsize);
59 CHK_ARG_EXPR(*bufsize > 0);
60
61 const size_t got = (obj->remain < *bufsize ? obj->remain : *bufsize);
62 if (got > 0)
63 {
64 memcpy(buf, obj->cursor, got);
65 obj->cursor += got;
66 obj->remain -= got;
67 }
68 *bufsize = got;
69 return BSL_SUCCESS;
70}
71
72int BSL_SeqWriter_InitFlat(BSL_SeqWriter_t *obj, uint8_t **buf, size_t *bufsize)
73{
74 CHK_ARG_NONNULL(obj);
75 CHK_ARG_NONNULL(buf);
76 CHK_ARG_NONNULL(bufsize);
77
78 obj->fd = open_memstream((char **)buf, bufsize);
79
80 CHK_POSTCONDITION(obj->fd != NULL);
81 return BSL_SUCCESS;
82}
83
85{
86 CHK_ARG_NONNULL(obj);
87
88 // NOLINTNEXTLINE
89 fclose(obj->fd);
90 obj->fd = NULL;
91
92 return BSL_SUCCESS;
93}
94
95int BSL_SeqWriter_Put(BSL_SeqWriter_t *obj, const uint8_t *buf, size_t *bufsize)
96{
97 CHK_ARG_NONNULL(obj);
98 CHK_ARG_NONNULL(buf);
99 CHK_ARG_NONNULL(bufsize);
100 // CHK_ARG_EXPR(*bufsize > 0);
101
102 const size_t got = fwrite(buf, 1, *bufsize, obj->fd);
103 if (got > 0)
104 {
105 *bufsize = got;
106 }
107 return BSL_SUCCESS;
108}
Single entry-point include file for all of the BPSec Lib (BSL) frontend API.
#define _U_
Mark an unused parameter Within a function definition.
@ BSL_SUCCESS
Placeholder for non-error code.
int BSL_SeqWriter_InitFlat(BSL_SeqWriter_t *obj, uint8_t **buf, size_t *bufsize)
Initialize resources for a sequential writer.
int BSL_SeqWriter_Put(BSL_SeqWriter_t *obj, const uint8_t *buf, size_t *bufsize)
Iterate a sequential writer.
int BSL_SeqWriter_Deinit(BSL_SeqWriter_t *obj)
Release resources from a sequential writer.
int BSL_SeqReader_Get(BSL_SeqReader_t *obj, uint8_t *buf, size_t *bufsize)
Iterate a sequential reader.
int BSL_SeqReader_InitFlat(BSL_SeqReader_t *obj, const uint8_t *buf, size_t bufsize)
Initialize resources for a sequential reader.
int BSL_SeqReader_Deinit(BSL_SeqReader_t *obj)
Release resources from a sequential reader.
Flat buffer data reading and writing.
Definition of a simple flat buffer iterator.
const uint8_t * cursor
Current cursor into available data.
size_t remain
Remaining available buffer.
Definition of a simple flat buffer iterator.
FILE * fd
Memory mapped file.