Remove some code for a contributor that we cannot find
[openssl.git] / crypto / buffer / buffer.c
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 <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/buffer.h>
13
14 /*
15  * LIMIT_BEFORE_EXPANSION is the maximum n such that (n+3)/3*4 < 2**31. That
16  * function is applied in several functions in this file and this limit
17  * ensures that the result fits in an int.
18  */
19 #define LIMIT_BEFORE_EXPANSION 0x5ffffffc
20
21 BUF_MEM *BUF_MEM_new_ex(unsigned long flags)
22 {
23     BUF_MEM *ret;
24
25     ret = BUF_MEM_new();
26     if (ret != NULL)
27         ret->flags = flags;
28     return ret;
29 }
30
31 BUF_MEM *BUF_MEM_new(void)
32 {
33     BUF_MEM *ret;
34
35     ret = OPENSSL_zalloc(sizeof(*ret));
36     if (ret == NULL) {
37         BUFerr(BUF_F_BUF_MEM_NEW, ERR_R_MALLOC_FAILURE);
38         return NULL;
39     }
40     return ret;
41 }
42
43 void BUF_MEM_free(BUF_MEM *a)
44 {
45     if (a->data != NULL) {
46         if (a->flags & BUF_MEM_FLAG_SECURE)
47             OPENSSL_secure_clear_free(a->data, a->max);
48         else
49             OPENSSL_clear_free(a->data, a->max);
50     }
51     OPENSSL_free(a);
52 }
53
54 /* Allocate a block of secure memory; copy over old data if there
55  * was any, and then free it. */
56 static char *sec_alloc_realloc(BUF_MEM *str, size_t len)
57 {
58     char *ret;
59
60     ret = OPENSSL_secure_malloc(len);
61     if (str->data != NULL) {
62         if (ret != NULL) {
63             memcpy(ret, str->data, str->length);
64             OPENSSL_secure_clear_free(str->data, str->length);
65             str->data = NULL;
66         }
67     }
68     return ret;
69 }
70
71 size_t BUF_MEM_grow(BUF_MEM *str, size_t len)
72 {
73     char *ret;
74     size_t n;
75
76     if (str->length >= len) {
77         str->length = len;
78         return len;
79     }
80     if (str->max >= len) {
81         if (str->data != NULL)
82             memset(&str->data[str->length], 0, len - str->length);
83         str->length = len;
84         return len;
85     }
86     /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */
87     if (len > LIMIT_BEFORE_EXPANSION) {
88         BUFerr(BUF_F_BUF_MEM_GROW, ERR_R_MALLOC_FAILURE);
89         return 0;
90     }
91     n = (len + 3) / 3 * 4;
92     if ((str->flags & BUF_MEM_FLAG_SECURE))
93         ret = sec_alloc_realloc(str, n);
94     else
95         ret = OPENSSL_realloc(str->data, n);
96     if (ret == NULL) {
97         BUFerr(BUF_F_BUF_MEM_GROW, ERR_R_MALLOC_FAILURE);
98         len = 0;
99     } else {
100         str->data = ret;
101         str->max = n;
102         memset(&str->data[str->length], 0, len - str->length);
103         str->length = len;
104     }
105     return len;
106 }
107
108 size_t BUF_MEM_grow_clean(BUF_MEM *str, size_t len)
109 {
110     char *ret;
111     size_t n;
112
113     if (str->length >= len) {
114         if (str->data != NULL)
115             memset(&str->data[len], 0, str->length - len);
116         str->length = len;
117         return len;
118     }
119     if (str->max >= len) {
120         memset(&str->data[str->length], 0, len - str->length);
121         str->length = len;
122         return len;
123     }
124     /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */
125     if (len > LIMIT_BEFORE_EXPANSION) {
126         BUFerr(BUF_F_BUF_MEM_GROW_CLEAN, ERR_R_MALLOC_FAILURE);
127         return 0;
128     }
129     n = (len + 3) / 3 * 4;
130     if ((str->flags & BUF_MEM_FLAG_SECURE))
131         ret = sec_alloc_realloc(str, n);
132     else
133         ret = OPENSSL_clear_realloc(str->data, str->max, n);
134     if (ret == NULL) {
135         BUFerr(BUF_F_BUF_MEM_GROW_CLEAN, ERR_R_MALLOC_FAILURE);
136         len = 0;
137     } else {
138         str->data = ret;
139         str->max = n;
140         memset(&str->data[str->length], 0, len - str->length);
141         str->length = len;
142     }
143     return len;
144 }
145
146 void BUF_reverse(unsigned char *out, const unsigned char *in, size_t size)
147 {
148     size_t i;
149     if (in) {
150         out += size - 1;
151         for (i = 0; i < size; i++)
152             *out-- = *in++;
153     } else {
154         unsigned char *q;
155         char c;
156         q = out + size - 1;
157         for (i = 0; i < size / 2; i++) {
158             c = *q;
159             *q-- = *out;
160             *out++ = c;
161         }
162     }
163 }