1193645Ssimon/* crypto/buffer/buf_str.c */ 2193645Ssimon/* ==================================================================== 3193645Ssimon * Copyright (c) 2007 The OpenSSL Project. All rights reserved. 4193645Ssimon * 5193645Ssimon * Redistribution and use in source and binary forms, with or without 6193645Ssimon * modification, are permitted provided that the following conditions 7193645Ssimon * are met: 8193645Ssimon * 9193645Ssimon * 1. Redistributions of source code must retain the above copyright 10296465Sdelphij * notice, this list of conditions and the following disclaimer. 11193645Ssimon * 12193645Ssimon * 2. Redistributions in binary form must reproduce the above copyright 13193645Ssimon * notice, this list of conditions and the following disclaimer in 14193645Ssimon * the documentation and/or other materials provided with the 15193645Ssimon * distribution. 16193645Ssimon * 17193645Ssimon * 3. All advertising materials mentioning features or use of this 18193645Ssimon * software must display the following acknowledgment: 19193645Ssimon * "This product includes software developed by the OpenSSL Project 20193645Ssimon * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 21193645Ssimon * 22193645Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 23193645Ssimon * endorse or promote products derived from this software without 24193645Ssimon * prior written permission. For written permission, please contact 25193645Ssimon * licensing@OpenSSL.org. 26193645Ssimon * 27193645Ssimon * 5. Products derived from this software may not be called "OpenSSL" 28193645Ssimon * nor may "OpenSSL" appear in their names without prior written 29193645Ssimon * permission of the OpenSSL Project. 30193645Ssimon * 31193645Ssimon * 6. Redistributions of any form whatsoever must retain the following 32193645Ssimon * acknowledgment: 33193645Ssimon * "This product includes software developed by the OpenSSL Project 34193645Ssimon * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 35193645Ssimon * 36193645Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 37193645Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 38193645Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 39193645Ssimon * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 40193645Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 41193645Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 42193645Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 43193645Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 44193645Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 45193645Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 46193645Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 47193645Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE. 48193645Ssimon * ==================================================================== 49193645Ssimon * 50193645Ssimon * This product includes cryptographic software written by Eric Young 51193645Ssimon * (eay@cryptsoft.com). This product includes software written by Tim 52193645Ssimon * Hudson (tjh@cryptsoft.com). 53193645Ssimon * 54193645Ssimon */ 55193645Ssimon 56193645Ssimon#include <stdio.h> 57193645Ssimon#include "cryptlib.h" 58193645Ssimon#include <openssl/buffer.h> 59193645Ssimon 60193645Ssimonchar *BUF_strdup(const char *str) 61296465Sdelphij{ 62296465Sdelphij if (str == NULL) 63296465Sdelphij return (NULL); 64296465Sdelphij return BUF_strndup(str, strlen(str)); 65296465Sdelphij} 66193645Ssimon 67193645Ssimonchar *BUF_strndup(const char *str, size_t siz) 68296465Sdelphij{ 69296465Sdelphij char *ret; 70193645Ssimon 71296465Sdelphij if (str == NULL) 72296465Sdelphij return (NULL); 73193645Ssimon 74296465Sdelphij ret = OPENSSL_malloc(siz + 1); 75296465Sdelphij if (ret == NULL) { 76296465Sdelphij BUFerr(BUF_F_BUF_STRNDUP, ERR_R_MALLOC_FAILURE); 77296465Sdelphij return (NULL); 78296465Sdelphij } 79296465Sdelphij BUF_strlcpy(ret, str, siz + 1); 80296465Sdelphij return (ret); 81296465Sdelphij} 82193645Ssimon 83193645Ssimonvoid *BUF_memdup(const void *data, size_t siz) 84296465Sdelphij{ 85296465Sdelphij void *ret; 86193645Ssimon 87296465Sdelphij if (data == NULL) 88296465Sdelphij return (NULL); 89193645Ssimon 90296465Sdelphij ret = OPENSSL_malloc(siz); 91296465Sdelphij if (ret == NULL) { 92296465Sdelphij BUFerr(BUF_F_BUF_MEMDUP, ERR_R_MALLOC_FAILURE); 93296465Sdelphij return (NULL); 94296465Sdelphij } 95296465Sdelphij return memcpy(ret, data, siz); 96296465Sdelphij} 97193645Ssimon 98193645Ssimonsize_t BUF_strlcpy(char *dst, const char *src, size_t size) 99296465Sdelphij{ 100296465Sdelphij size_t l = 0; 101296465Sdelphij for (; size > 1 && *src; size--) { 102296465Sdelphij *dst++ = *src++; 103296465Sdelphij l++; 104296465Sdelphij } 105296465Sdelphij if (size) 106296465Sdelphij *dst = '\0'; 107296465Sdelphij return l + strlen(src); 108296465Sdelphij} 109193645Ssimon 110193645Ssimonsize_t BUF_strlcat(char *dst, const char *src, size_t size) 111296465Sdelphij{ 112296465Sdelphij size_t l = 0; 113296465Sdelphij for (; size > 0 && *dst; size--, dst++) 114296465Sdelphij l++; 115296465Sdelphij return l + BUF_strlcpy(dst, src, size); 116296465Sdelphij} 117