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 : 947924919 : {
28 [ + + ][ + - ]: 947924919 : RESULT_ENSURE_REF(b);
29 [ + - ][ + + ]: 947924910 : RESULT_DEBUG_ENSURE(S2N_IMPLIES(b->data == NULL, b->size == 0), S2N_ERR_SAFETY);
[ + + ][ + + ]
30 [ # # ][ - + ]: 947924907 : RESULT_DEBUG_ENSURE(S2N_IMPLIES(b->data == NULL, b->allocated == 0), S2N_ERR_SAFETY);
[ + - ][ + + ]
31 [ # # ][ - + ]: 947924907 : RESULT_DEBUG_ENSURE(S2N_IMPLIES(b->growable == 0, b->allocated == 0), S2N_ERR_SAFETY);
[ + + ][ + - ]
32 [ # # ][ - + ]: 947924907 : RESULT_DEBUG_ENSURE(S2N_IMPLIES(b->growable == 1, b->allocated > 0 || b->size == 0), S2N_ERR_SAFETY);
[ + - ][ # # ]
[ + + ]
33 [ # # ][ - + ]: 947924907 : RESULT_DEBUG_ENSURE(S2N_IMPLIES(b->growable != 0, b->size <= b->allocated), S2N_ERR_SAFETY);
[ + + ][ + - ]
34 [ # # ][ - + ]: 947924907 : RESULT_DEBUG_ENSURE(S2N_MEM_IS_READABLE(b->data, b->allocated), S2N_ERR_SAFETY);
[ + + ][ + - ]
35 [ # # ][ - + ]: 947924907 : RESULT_DEBUG_ENSURE(S2N_MEM_IS_READABLE(b->data, b->size), S2N_ERR_SAFETY);
[ + + ][ + - ]
36 : 947924907 : return S2N_RESULT_OK;
37 : 947924907 : }
38 : :
39 : : int s2n_blob_init(struct s2n_blob *b, uint8_t *data, uint32_t size)
40 : 47541495 : {
41 [ + + ][ + - ]: 47541495 : POSIX_ENSURE_REF(b);
42 [ # # ][ + + ]: 47541494 : POSIX_ENSURE(S2N_MEM_IS_READABLE(data, size), S2N_ERR_SAFETY);
[ + - ]
43 : 47541494 : *b = (struct s2n_blob){ .data = data, .size = size, .allocated = 0, .growable = 0 };
44 [ - + ][ + + ]: 47541494 : POSIX_POSTCONDITION(s2n_blob_validate(b));
45 : 47541494 : return S2N_SUCCESS;
46 : 47541494 : }
47 : :
48 : : int s2n_blob_zero(struct s2n_blob *b)
49 : 101922081 : {
50 [ + + ][ + + ]: 101922081 : POSIX_PRECONDITION(s2n_blob_validate(b));
51 [ # # ][ - + ]: 101922079 : POSIX_CHECKED_MEMSET(b->data, 0, MAX(b->allocated, b->size));
[ + + ][ + + ]
52 [ - + ][ + + ]: 101922079 : POSIX_POSTCONDITION(s2n_blob_validate(b));
53 : 101922079 : return S2N_SUCCESS;
54 : 101922079 : }
55 : :
56 : : int s2n_blob_slice(const struct s2n_blob *b, struct s2n_blob *slice, uint32_t offset, uint32_t size)
57 : 4862315 : {
58 [ - + ][ + + ]: 4862315 : POSIX_PRECONDITION(s2n_blob_validate(b));
59 [ - + ][ + + ]: 4862315 : POSIX_PRECONDITION(s2n_blob_validate(slice));
60 : :
61 : 4862315 : uint32_t slice_size = 0;
62 [ - + ]: 4862315 : POSIX_GUARD(s2n_add_overflow(offset, size, &slice_size));
63 [ # # ][ - + ]: 4862315 : POSIX_ENSURE(b->size >= slice_size, S2N_ERR_SIZE_MISMATCH);
64 [ + + ]: 4862359 : slice->data = (b->data) ? (b->data + offset) : NULL;
65 : 4862315 : slice->size = size;
66 : 4862315 : slice->growable = 0;
67 : 4862315 : slice->allocated = 0;
68 : :
69 [ - + ][ + + ]: 4862315 : POSIX_POSTCONDITION(s2n_blob_validate(slice));
70 : 4862315 : return S2N_SUCCESS;
71 : 4862315 : }
72 : :
73 : : int s2n_blob_char_to_lower(struct s2n_blob *b)
74 : 1223 : {
75 [ - + ][ + - ]: 1223 : POSIX_PRECONDITION(s2n_blob_validate(b));
76 [ + + ]: 12364 : for (size_t i = 0; i < b->size; i++) {
77 : 11141 : b->data[i] = tolower(b->data[i]);
78 : 11141 : }
79 [ - + ][ + - ]: 1223 : POSIX_POSTCONDITION(s2n_blob_validate(b));
80 : 1223 : return S2N_SUCCESS;
81 : 1223 : }
|