Typo.
[openssl.git] / crypto / buffer / buffer.c
index 9299baba9e2d5de688fca19cfd889da68c012ef8..a6b590c3b2b2e3add87de35d1ad413b50e40537b 100644 (file)
 #include "cryptlib.h"
 #include <openssl/buffer.h>
 
+/* LIMIT_BEFORE_EXPANSION is the maximum n such that (n+3)/3*4 < 2**31. That
+ * function is applied in several functions in this file and this limit ensures
+ * that the result fits in an int. */
+#define LIMIT_BEFORE_EXPANSION 0x5ffffffc
+
 BUF_MEM *BUF_MEM_new(void)
        {
        BUF_MEM *ret;
@@ -89,10 +94,10 @@ void BUF_MEM_free(BUF_MEM *a)
        OPENSSL_free(a);
        }
 
-int BUF_MEM_grow(BUF_MEM *str, int len)
+int BUF_MEM_grow(BUF_MEM *str, size_t len)
        {
        char *ret;
-       unsigned int n;
+       size_t n;
 
        if (str->length >= len)
                {
@@ -105,6 +110,12 @@ int BUF_MEM_grow(BUF_MEM *str, int len)
                str->length=len;
                return(len);
                }
+       /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */
+       if (len > LIMIT_BEFORE_EXPANSION)
+               {
+               BUFerr(BUF_F_BUF_MEM_GROW,ERR_R_MALLOC_FAILURE);
+               return 0;
+               }
        n=(len+3)/3*4;
        if (str->data == NULL)
                ret=OPENSSL_malloc(n);
@@ -125,21 +136,68 @@ int BUF_MEM_grow(BUF_MEM *str, int len)
        return(len);
        }
 
-char *BUF_strdup(const char *str)
+int BUF_MEM_grow_clean(BUF_MEM *str, size_t len)
        {
        char *ret;
-       int n;
-
-       if (str == NULL) return(NULL);
+       size_t n;
 
-       n=strlen(str);
-       ret=OPENSSL_malloc(n+1);
-       if (ret == NULL) 
+       if (str->length >= len)
                {
-               BUFerr(BUF_F_BUF_STRDUP,ERR_R_MALLOC_FAILURE);
-               return(NULL);
+               memset(&str->data[len],0,str->length-len);
+               str->length=len;
+               return(len);
                }
-       memcpy(ret,str,n+1);
-       return(ret);
+       if (str->max >= len)
+               {
+               memset(&str->data[str->length],0,len-str->length);
+               str->length=len;
+               return(len);
+               }
+       /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */
+       if (len > LIMIT_BEFORE_EXPANSION)
+               {
+               BUFerr(BUF_F_BUF_MEM_GROW_CLEAN,ERR_R_MALLOC_FAILURE);
+               return 0;
+               }
+       n=(len+3)/3*4;
+       if (str->data == NULL)
+               ret=OPENSSL_malloc(n);
+       else
+               ret=OPENSSL_realloc_clean(str->data,str->max,n);
+       if (ret == NULL)
+               {
+               BUFerr(BUF_F_BUF_MEM_GROW_CLEAN,ERR_R_MALLOC_FAILURE);
+               len=0;
+               }
+       else
+               {
+               str->data=ret;
+               str->max=n;
+               memset(&str->data[str->length],0,len-str->length);
+               str->length=len;
+               }
+       return(len);
        }
 
+void BUF_reverse(unsigned char *out, unsigned char *in, size_t size)
+       {
+       size_t i;
+       if (in)
+               {
+               out += size - 1;
+               for (i = 0; i < size; i++)
+                       *out-- = *in++;
+               }
+       else
+               {
+               unsigned char *q;
+               char c;
+               q = out + size - 1;
+               for (i = 0; i < size/2; i++)
+                       {
+                       c = *q;
+                       *q-- = *out;
+                       *out++ = c;
+                       }
+               }
+       }