1 /*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <openssl/buf.h>
11
12 #include <string.h>
13
14 #include <openssl/err.h>
15 #include <openssl/mem.h>
16
17 #include "../internal.h"
18
19
BUF_MEM_new(void)20 BUF_MEM *BUF_MEM_new(void) {
21 return reinterpret_cast<BUF_MEM *>(OPENSSL_zalloc(sizeof(BUF_MEM)));
22 }
23
BUF_MEM_free(BUF_MEM * buf)24 void BUF_MEM_free(BUF_MEM *buf) {
25 if (buf == nullptr) {
26 return;
27 }
28 OPENSSL_free(buf->data);
29 OPENSSL_free(buf);
30 }
31
BUF_MEM_reserve(BUF_MEM * buf,size_t cap)32 int BUF_MEM_reserve(BUF_MEM *buf, size_t cap) {
33 if (buf->max >= cap) {
34 return 1;
35 }
36
37 size_t n = cap + 3;
38 if (n < cap) {
39 OPENSSL_PUT_ERROR(BUF, ERR_R_OVERFLOW);
40 return 0;
41 }
42 n = n / 3;
43 size_t alloc_size = n * 4;
44 if (alloc_size / 4 != n) {
45 OPENSSL_PUT_ERROR(BUF, ERR_R_OVERFLOW);
46 return 0;
47 }
48
49 char *new_buf =
50 reinterpret_cast<char *>(OPENSSL_realloc(buf->data, alloc_size));
51 if (new_buf == NULL) {
52 return 0;
53 }
54
55 buf->data = new_buf;
56 buf->max = alloc_size;
57 return 1;
58 }
59
BUF_MEM_grow(BUF_MEM * buf,size_t len)60 size_t BUF_MEM_grow(BUF_MEM *buf, size_t len) {
61 if (!BUF_MEM_reserve(buf, len)) {
62 return 0;
63 }
64 if (buf->length < len) {
65 OPENSSL_memset(&buf->data[buf->length], 0, len - buf->length);
66 }
67 buf->length = len;
68 return len;
69 }
70
BUF_MEM_grow_clean(BUF_MEM * buf,size_t len)71 size_t BUF_MEM_grow_clean(BUF_MEM *buf, size_t len) {
72 return BUF_MEM_grow(buf, len);
73 }
74
BUF_MEM_append(BUF_MEM * buf,const void * in,size_t len)75 int BUF_MEM_append(BUF_MEM *buf, const void *in, size_t len) {
76 // Work around a C language bug. See https://crbug.com/1019588.
77 if (len == 0) {
78 return 1;
79 }
80 size_t new_len = buf->length + len;
81 if (new_len < len) {
82 OPENSSL_PUT_ERROR(BUF, ERR_R_OVERFLOW);
83 return 0;
84 }
85 if (!BUF_MEM_reserve(buf, new_len)) {
86 return 0;
87 }
88 OPENSSL_memcpy(buf->data + buf->length, in, len);
89 buf->length = new_len;
90 return 1;
91 }
92
BUF_strdup(const char * str)93 char *BUF_strdup(const char *str) { return OPENSSL_strdup(str); }
94
BUF_strnlen(const char * str,size_t max_len)95 size_t BUF_strnlen(const char *str, size_t max_len) {
96 return OPENSSL_strnlen(str, max_len);
97 }
98
BUF_strndup(const char * str,size_t size)99 char *BUF_strndup(const char *str, size_t size) {
100 return OPENSSL_strndup(str, size);
101 }
102
BUF_strlcpy(char * dst,const char * src,size_t dst_size)103 size_t BUF_strlcpy(char *dst, const char *src, size_t dst_size) {
104 return OPENSSL_strlcpy(dst, src, dst_size);
105 }
106
BUF_strlcat(char * dst,const char * src,size_t dst_size)107 size_t BUF_strlcat(char *dst, const char *src, size_t dst_size) {
108 return OPENSSL_strlcat(dst, src, dst_size);
109 }
110
BUF_memdup(const void * data,size_t size)111 void *BUF_memdup(const void *data, size_t size) {
112 return OPENSSL_memdup(data, size);
113 }
114