Debian Codegraph @

crt/s2n/utils/s2n_blob.h

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #pragma once
17
18 #include <stdbool.h>
19 #include <stdint.h>
20
21 #include "utils/s2n_result.h"
22
23 struct s2n_blob {
24 /* The data for the s2n_blob */
25 uint8_t *data;
26
27 /* The size of the data */
28 uint32_t size;
29
30 /* The amount of memory allocated for this blob (i.e. the amount of memory
31 * which needs to be freed when the blob is cleaned up). If this blob was
32 * created with s2n_blob_init(), this value is 0. If s2n_alloc() was called,
33 * this value will be greater than or equal to size.
34 *
35 * size < allocated implies that an allocated blob is being reused to store
36 * a smaller amount of data.
37 */
38 uint32_t allocated;
39
40 /* An allocated blob (e.g.`s2n_alloc`) is always growable. A "reference"
41 * blob (from `s2n_blob_init`) is never growable.
42 *
43 * This field is necessary to distinguish zero-sized allocated blobs from
44 * zero-sized "reference" blobs. Zero-sized allocated blobs can not be
45 * constructed with s2n_alloc or s2n_realloc, but they are directly initialized
46 * in s2n_free_object.
47 */
48 unsigned growable : 1;
49 };
50
51 bool s2n_blob_is_growable(const struct s2n_blob *b);
52 S2N_RESULT s2n_blob_validate(const struct s2n_blob *b);
53 int S2N_RESULT_MUST_USE s2n_blob_init(struct s2n_blob *b, uint8_t *data, uint32_t size);
54 int s2n_blob_zero(struct s2n_blob *b);
55 int S2N_RESULT_MUST_USE s2n_blob_char_to_lower(struct s2n_blob *b);
56 int S2N_RESULT_MUST_USE s2n_blob_slice(const struct s2n_blob *b, struct s2n_blob *slice, uint32_t offset, uint32_t size);
57
58 #define s2n_stack_blob(name, requested_size, maximum) \
59 size_t name##_requested_size = (requested_size); \
60 uint8_t name##_buf[(maximum)] = { 0 }; \
61 POSIX_ENSURE_LTE(name##_requested_size, (maximum)); \
62 struct s2n_blob name = { 0 }; \
63 POSIX_GUARD(s2n_blob_init(&name, name##_buf, name##_requested_size))
64
65 #define RESULT_STACK_BLOB(name, requested_size, maximum) \
66 size_t name##_requested_size = (requested_size); \
67 uint8_t name##_buf[(maximum)] = { 0 }; \
68 RESULT_ENSURE_LTE(name##_requested_size, (maximum)); \
69 struct s2n_blob name = { 0 }; \
70 RESULT_GUARD_POSIX(s2n_blob_init(&name, name##_buf, name##_requested_size))
71
72 #define S2N_BLOB_LABEL(name, str) \
73 static uint8_t name##_data[] = str; \
74 const struct s2n_blob name = { .data = name##_data, .size = sizeof(name##_data) - 1 };