Branch data Line data Source code
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 : : #include "utils/s2n_blob.h"
17 : :
18 : : #include <ctype.h>
19 : : #include <string.h>
20 : : #include <sys/param.h>
21 : :
22 : : #include "api/s2n.h"
23 : : #include "error/s2n_errno.h"
24 : : #include "utils/s2n_safety.h"
25 : :
26 : : S2N_RESULT s2n_blob_validate(const struct s2n_blob *b)
27 : 951901428 : {
28 [ + + ][ + - ]: 951901428 : RESULT_ENSURE_REF(b);
29 [ + - ][ + + ]: 951901419 : RESULT_DEBUG_ENSURE(S2N_IMPLIES(b->data == NULL, b->size == 0), S2N_ERR_SAFETY);
[ + + ][ + + ]
30 [ # # ][ - + ]: 951901416 : RESULT_DEBUG_ENSURE(S2N_IMPLIES(b->data == NULL, b->allocated == 0), S2N_ERR_SAFETY);
[ + - ][ + + ]
31 [ # # ][ - + ]: 951901416 : RESULT_DEBUG_ENSURE(S2N_IMPLIES(b->growable == 0, b->allocated == 0), S2N_ERR_SAFETY);
[ + + ][ + - ]
32 [ # # ][ - + ]: 951901416 : RESULT_DEBUG_ENSURE(S2N_IMPLIES(b->growable == 1, b->allocated > 0 || b->size == 0), S2N_ERR_SAFETY);
[ + - ][ # # ]
[ + + ]
33 [ # # ][ - + ]: 951901416 : RESULT_DEBUG_ENSURE(S2N_IMPLIES(b->growable != 0, b->size <= b->allocated), S2N_ERR_SAFETY);
[ + + ][ + - ]
34 [ # # ][ - + ]: 951901416 : RESULT_DEBUG_ENSURE(S2N_MEM_IS_READABLE(b->data, b->allocated), S2N_ERR_SAFETY);
[ + + ][ + - ]
35 [ # # ][ - + ]: 951901416 : RESULT_DEBUG_ENSURE(S2N_MEM_IS_READABLE(b->data, b->size), S2N_ERR_SAFETY);
[ + + ][ + - ]
36 : 951901416 : return S2N_RESULT_OK;
37 : 951901416 : }
38 : :
39 : : /**
40 : : * Initialize a blob to reference some data.
41 : : *
42 : : * `b` will not free `data`. The caller is responsible for making sure that
43 : : * `data` outlives `b`.
44 : : */
45 : : int s2n_blob_init(struct s2n_blob *b, uint8_t *data, uint32_t size)
46 : 47635787 : {
47 [ + + ][ + - ]: 47635787 : POSIX_ENSURE_REF(b);
48 [ # # ][ + + ]: 47635786 : POSIX_ENSURE(S2N_MEM_IS_READABLE(data, size), S2N_ERR_SAFETY);
[ + - ]
49 : 47635786 : *b = (struct s2n_blob){ .data = data, .size = size, .allocated = 0, .growable = 0 };
50 [ - + ][ + + ]: 47635786 : POSIX_POSTCONDITION(s2n_blob_validate(b));
51 : 47635786 : return S2N_SUCCESS;
52 : 47635786 : }
53 : :
54 : : int s2n_blob_zero(struct s2n_blob *b)
55 : 102036302 : {
56 [ + + ][ + + ]: 102036302 : POSIX_PRECONDITION(s2n_blob_validate(b));
57 [ # # ][ - + ]: 102036300 : POSIX_CHECKED_MEMSET(b->data, 0, MAX(b->allocated, b->size));
[ + + ][ + + ]
58 [ - + ][ + + ]: 102036300 : POSIX_POSTCONDITION(s2n_blob_validate(b));
59 : 102036300 : return S2N_SUCCESS;
60 : 102036300 : }
61 : :
62 : : /**
63 : : * Set `slice` to reference some portion of `b`.
64 : : *
65 : : * The caller is responsible for ensuring that the data pointed to by `b` outlives
66 : : * `slice`.
67 : : */
68 : : int s2n_blob_slice(const struct s2n_blob *b, struct s2n_blob *slice, uint32_t offset, uint32_t size)
69 : 4867977 : {
70 [ - + ][ + + ]: 4867977 : POSIX_PRECONDITION(s2n_blob_validate(b));
71 [ - + ][ + + ]: 4867977 : POSIX_PRECONDITION(s2n_blob_validate(slice));
72 : :
73 : 4867977 : uint32_t slice_size = 0;
74 [ - + ]: 4867977 : POSIX_GUARD(s2n_add_overflow(offset, size, &slice_size));
75 [ # # ][ - + ]: 4867977 : POSIX_ENSURE(b->size >= slice_size, S2N_ERR_SIZE_MISMATCH);
76 [ + + ]: 4867977 : slice->data = (b->data) ? (b->data + offset) : NULL;
77 : 4867977 : slice->size = size;
78 : 4867977 : slice->growable = 0;
79 : 4867977 : slice->allocated = 0;
80 : :
81 [ - + ][ + + ]: 4867977 : POSIX_POSTCONDITION(s2n_blob_validate(slice));
82 : 4867977 : return S2N_SUCCESS;
83 : 4867977 : }
84 : :
85 : : int s2n_blob_char_to_lower(struct s2n_blob *b)
86 : 1401 : {
87 [ - + ][ + - ]: 1401 : POSIX_PRECONDITION(s2n_blob_validate(b));
88 [ + + ]: 13739 : for (size_t i = 0; i < b->size; i++) {
89 : 12338 : b->data[i] = tolower(b->data[i]);
90 : 12338 : }
91 [ - + ][ + - ]: 1401 : POSIX_POSTCONDITION(s2n_blob_validate(b));
92 : 1401 : return S2N_SUCCESS;
93 : 1401 : }
|