提交 1ae0a83b 编写于 作者: R Richard Levitte

Add BUF_strndup() and BUF_memdup(). Not currently used, but I've code

that uses them that I'll commit in a few days.
上级 7ae46c67
...@@ -4,6 +4,12 @@ ...@@ -4,6 +4,12 @@
Changes between 0.9.7a and 0.9.8 [xx XXX xxxx] 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 *) 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 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 found. The index is guaranteed to point at the element where the
......
/* crypto/buffer/buf_err.c */ /* 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 * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
...@@ -61,15 +61,16 @@ ...@@ -61,15 +61,16 @@
#include <stdio.h> #include <stdio.h>
#include <openssl/err.h> #include <openssl/err.h>
#include <openssl/buffer.h> #include <openssl/buffer.h>
#include <openssl/opensslconf.h> /* To see if OPENSSL_NO_ERR is defined */
/* BEGIN ERROR CODES */ /* BEGIN ERROR CODES */
#ifndef OPENSSL_NO_ERR #ifndef OPENSSL_NO_ERR
static ERR_STRING_DATA BUF_str_functs[]= 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_GROW,0), "BUF_MEM_grow"},
{ERR_PACK(0,BUF_F_BUF_MEM_NEW,0), "BUF_MEM_new"}, {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_STRDUP,0), "BUF_strdup"},
{ERR_PACK(0,BUF_F_BUF_STRNDUP,0), "BUF_strndup"},
{0,NULL} {0,NULL}
}; };
......
...@@ -163,23 +163,42 @@ int BUF_MEM_grow_clean(BUF_MEM *str, int len) ...@@ -163,23 +163,42 @@ int BUF_MEM_grow_clean(BUF_MEM *str, int len)
} }
char *BUF_strdup(const char *str) 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; char *ret;
int n;
if (str == NULL) return(NULL); if (str == NULL) return(NULL);
n=strlen(str); ret=OPENSSL_malloc(siz+1);
ret=OPENSSL_malloc(n+1);
if (ret == NULL) if (ret == NULL)
{ {
BUFerr(BUF_F_BUF_STRDUP,ERR_R_MALLOC_FAILURE); BUFerr(BUF_F_BUF_STRNDUP,ERR_R_MALLOC_FAILURE);
return(NULL); return(NULL);
} }
memcpy(ret,str,n+1); BUF_strlcpy(ret,str,siz+1);
return(ret); 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 BUF_strlcpy(char *dst, const char *src, size_t size)
{ {
size_t l = 0; size_t l = 0;
......
...@@ -78,6 +78,8 @@ void BUF_MEM_free(BUF_MEM *a); ...@@ -78,6 +78,8 @@ void BUF_MEM_free(BUF_MEM *a);
int BUF_MEM_grow(BUF_MEM *str, int len); int BUF_MEM_grow(BUF_MEM *str, int len);
int BUF_MEM_grow_clean(BUF_MEM *str, int len); int BUF_MEM_grow_clean(BUF_MEM *str, int len);
char * BUF_strdup(const char *str); 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 */ /* safe string functions */
size_t BUF_strlcpy(char *dst,const char *src,size_t siz); size_t BUF_strlcpy(char *dst,const char *src,size_t siz);
...@@ -93,9 +95,11 @@ void ERR_load_BUF_strings(void); ...@@ -93,9 +95,11 @@ void ERR_load_BUF_strings(void);
/* Error codes for the BUF functions. */ /* Error codes for the BUF functions. */
/* Function codes. */ /* Function codes. */
#define BUF_F_BUF_MEMDUP 103
#define BUF_F_BUF_MEM_GROW 100 #define BUF_F_BUF_MEM_GROW 100
#define BUF_F_BUF_MEM_NEW 101 #define BUF_F_BUF_MEM_NEW 101
#define BUF_F_BUF_STRDUP 102 #define BUF_F_BUF_STRDUP 102
#define BUF_F_BUF_STRNDUP 104
/* Reason codes. */ /* Reason codes. */
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册