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