From: Richard Levitte Date: Tue, 29 Apr 2003 22:08:57 +0000 (+0000) Subject: Add BUF_strndup() and BUF_memdup(). Not currently used, but I've code X-Git-Tag: BEN_FIPS_TEST_1~38^2~125 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=1ae0a83bdd37cdbe09d6612b7d50627dbabbe882;p=oweals%2Fopenssl.git Add BUF_strndup() and BUF_memdup(). Not currently used, but I've code that uses them that I'll commit in a few days. --- diff --git a/CHANGES b/CHANGES index 2a926d1917..9a416dea6b 100644 --- a/CHANGES +++ b/CHANGES @@ -4,6 +4,12 @@ Changes between 0.9.7a and 0.9.8 [xx XXX xxxx] + *) Add the functions BUF_strndup() and BUF_memdup(). BUF_strndup() + works like BUF_strdup() but can be used to duplicate a portion of + a string. The copy gets NUL-terminated. BUF_memdup() duplicates + a memory area. + [Richard Levitte] + *) Add the function sk_find_ex() which works like sk_find(), but will return an index to an element even if an exact match couldn't be found. The index is guaranteed to point at the element where the diff --git a/crypto/buffer/buf_err.c b/crypto/buffer/buf_err.c index 6559060784..73702f0f10 100644 --- a/crypto/buffer/buf_err.c +++ b/crypto/buffer/buf_err.c @@ -1,6 +1,6 @@ /* crypto/buffer/buf_err.c */ /* ==================================================================== - * Copyright (c) 1999 The OpenSSL Project. All rights reserved. + * Copyright (c) 1999-2003 The OpenSSL Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -61,15 +61,16 @@ #include #include #include -#include /* To see if OPENSSL_NO_ERR is defined */ /* BEGIN ERROR CODES */ #ifndef OPENSSL_NO_ERR static ERR_STRING_DATA BUF_str_functs[]= { +{ERR_PACK(0,BUF_F_BUF_MEMDUP,0), "BUF_memdup"}, {ERR_PACK(0,BUF_F_BUF_MEM_GROW,0), "BUF_MEM_grow"}, {ERR_PACK(0,BUF_F_BUF_MEM_NEW,0), "BUF_MEM_new"}, {ERR_PACK(0,BUF_F_BUF_STRDUP,0), "BUF_strdup"}, +{ERR_PACK(0,BUF_F_BUF_STRNDUP,0), "BUF_strndup"}, {0,NULL} }; diff --git a/crypto/buffer/buffer.c b/crypto/buffer/buffer.c index d96487e7db..03ebf23a14 100644 --- a/crypto/buffer/buffer.c +++ b/crypto/buffer/buffer.c @@ -163,23 +163,42 @@ int BUF_MEM_grow_clean(BUF_MEM *str, int len) } char *BUF_strdup(const char *str) + { + if (str == NULL) return(NULL); + return BUF_strndup(str, strlen(str)); + } + +char *BUF_strndup(const char *str, size_t siz) { char *ret; - int n; if (str == NULL) return(NULL); - n=strlen(str); - ret=OPENSSL_malloc(n+1); + ret=OPENSSL_malloc(siz+1); if (ret == NULL) { - BUFerr(BUF_F_BUF_STRDUP,ERR_R_MALLOC_FAILURE); + BUFerr(BUF_F_BUF_STRNDUP,ERR_R_MALLOC_FAILURE); return(NULL); } - memcpy(ret,str,n+1); + BUF_strlcpy(ret,str,siz+1); return(ret); } +void *BUF_memdup(const void *data, size_t siz) + { + void *ret; + + if (data == NULL) return(NULL); + + ret=OPENSSL_malloc(siz); + if (ret == NULL) + { + BUFerr(BUF_F_BUF_MEMDUP,ERR_R_MALLOC_FAILURE); + return(NULL); + } + return memcpy(ret, data, siz); + } + size_t BUF_strlcpy(char *dst, const char *src, size_t size) { size_t l = 0; diff --git a/crypto/buffer/buffer.h b/crypto/buffer/buffer.h index 465dc34f3f..164f8aa6ee 100644 --- a/crypto/buffer/buffer.h +++ b/crypto/buffer/buffer.h @@ -78,6 +78,8 @@ void BUF_MEM_free(BUF_MEM *a); int BUF_MEM_grow(BUF_MEM *str, int len); int BUF_MEM_grow_clean(BUF_MEM *str, int len); char * BUF_strdup(const char *str); +char * BUF_strndup(const char *str, size_t siz); +void * BUF_memdup(const void *data, size_t siz); /* safe string functions */ size_t BUF_strlcpy(char *dst,const char *src,size_t siz); @@ -93,9 +95,11 @@ void ERR_load_BUF_strings(void); /* Error codes for the BUF functions. */ /* Function codes. */ +#define BUF_F_BUF_MEMDUP 103 #define BUF_F_BUF_MEM_GROW 100 #define BUF_F_BUF_MEM_NEW 101 #define BUF_F_BUF_STRDUP 102 +#define BUF_F_BUF_STRNDUP 104 /* Reason codes. */