/src/boringssl/crypto/buf/buf.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <openssl/buf.h> |
16 | | |
17 | | #include <string.h> |
18 | | |
19 | | #include <openssl/err.h> |
20 | | #include <openssl/mem.h> |
21 | | |
22 | | #include "../internal.h" |
23 | | |
24 | | |
25 | 1.51M | BUF_MEM *BUF_MEM_new(void) { |
26 | 1.51M | return reinterpret_cast<BUF_MEM *>(OPENSSL_zalloc(sizeof(BUF_MEM))); |
27 | 1.51M | } |
28 | | |
29 | 1.46M | void BUF_MEM_free(BUF_MEM *buf) { |
30 | 1.46M | if (buf == nullptr) { |
31 | 0 | return; |
32 | 0 | } |
33 | 1.46M | OPENSSL_free(buf->data); |
34 | 1.46M | OPENSSL_free(buf); |
35 | 1.46M | } |
36 | | |
37 | 2.90M | int BUF_MEM_reserve(BUF_MEM *buf, size_t cap) { |
38 | 2.90M | if (buf->max >= cap) { |
39 | 1.67M | return 1; |
40 | 1.67M | } |
41 | | |
42 | 1.22M | size_t n = cap + 3; |
43 | 1.22M | if (n < cap) { |
44 | 0 | OPENSSL_PUT_ERROR(BUF, ERR_R_OVERFLOW); |
45 | 0 | return 0; |
46 | 0 | } |
47 | 1.22M | n = n / 3; |
48 | 1.22M | size_t alloc_size = n * 4; |
49 | 1.22M | if (alloc_size / 4 != n) { |
50 | 0 | OPENSSL_PUT_ERROR(BUF, ERR_R_OVERFLOW); |
51 | 0 | return 0; |
52 | 0 | } |
53 | | |
54 | 1.22M | char *new_buf = |
55 | 1.22M | reinterpret_cast<char *>(OPENSSL_realloc(buf->data, alloc_size)); |
56 | 1.22M | if (new_buf == NULL) { |
57 | 0 | return 0; |
58 | 0 | } |
59 | | |
60 | 1.22M | buf->data = new_buf; |
61 | 1.22M | buf->max = alloc_size; |
62 | 1.22M | return 1; |
63 | 1.22M | } |
64 | | |
65 | 681k | size_t BUF_MEM_grow(BUF_MEM *buf, size_t len) { |
66 | 681k | if (!BUF_MEM_reserve(buf, len)) { |
67 | 0 | return 0; |
68 | 0 | } |
69 | 681k | if (buf->length < len) { |
70 | 632k | OPENSSL_memset(&buf->data[buf->length], 0, len - buf->length); |
71 | 632k | } |
72 | 681k | buf->length = len; |
73 | 681k | return len; |
74 | 681k | } |
75 | | |
76 | 5.54k | size_t BUF_MEM_grow_clean(BUF_MEM *buf, size_t len) { |
77 | 5.54k | return BUF_MEM_grow(buf, len); |
78 | 5.54k | } |
79 | | |
80 | 1.99M | int BUF_MEM_append(BUF_MEM *buf, const void *in, size_t len) { |
81 | | // Work around a C language bug. See https://crbug.com/1019588. |
82 | 1.99M | if (len == 0) { |
83 | 16.0k | return 1; |
84 | 16.0k | } |
85 | 1.98M | size_t new_len = buf->length + len; |
86 | 1.98M | if (new_len < len) { |
87 | 0 | OPENSSL_PUT_ERROR(BUF, ERR_R_OVERFLOW); |
88 | 0 | return 0; |
89 | 0 | } |
90 | 1.98M | if (!BUF_MEM_reserve(buf, new_len)) { |
91 | 0 | return 0; |
92 | 0 | } |
93 | 1.98M | OPENSSL_memcpy(buf->data + buf->length, in, len); |
94 | 1.98M | buf->length = new_len; |
95 | 1.98M | return 1; |
96 | 1.98M | } |
97 | | |
98 | 0 | char *BUF_strdup(const char *str) { return OPENSSL_strdup(str); } |
99 | | |
100 | 0 | size_t BUF_strnlen(const char *str, size_t max_len) { |
101 | 0 | return OPENSSL_strnlen(str, max_len); |
102 | 0 | } |
103 | | |
104 | 0 | char *BUF_strndup(const char *str, size_t size) { |
105 | 0 | return OPENSSL_strndup(str, size); |
106 | 0 | } |
107 | | |
108 | 0 | size_t BUF_strlcpy(char *dst, const char *src, size_t dst_size) { |
109 | 0 | return OPENSSL_strlcpy(dst, src, dst_size); |
110 | 0 | } |
111 | | |
112 | 0 | size_t BUF_strlcat(char *dst, const char *src, size_t dst_size) { |
113 | 0 | return OPENSSL_strlcat(dst, src, dst_size); |
114 | 0 | } |
115 | | |
116 | 0 | void *BUF_memdup(const void *data, size_t size) { |
117 | 0 | return OPENSSL_memdup(data, size); |
118 | 0 | } |