2 * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project
5 /* ====================================================================
6 * Copyright (c) 2003 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@openssl.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
62 #include <openssl/crypto.h>
63 #include "internal/cryptlib.h"
64 #include "internal/o_str.h"
66 #if !defined(OPENSSL_IMPLEMENTS_strncasecmp) && \
67 !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_WINCE) && \
68 !defined(NETWARE_CLIB)
72 int OPENSSL_strncasecmp(const char *str1, const char *str2, size_t n)
74 #if defined(OPENSSL_IMPLEMENTS_strncasecmp)
75 while (*str1 && *str2 && n) {
76 int res = toupper(*str1) - toupper(*str2);
78 return res < 0 ? -1 : 1;
92 * Recursion hazard warning! Whenever strncasecmp is #defined as
93 * OPENSSL_strncasecmp, OPENSSL_IMPLEMENTS_strncasecmp must be defined as
96 return strncasecmp(str1, str2, n);
100 int OPENSSL_strcasecmp(const char *str1, const char *str2)
102 #if defined(OPENSSL_IMPLEMENTS_strncasecmp)
103 return OPENSSL_strncasecmp(str1, str2, (size_t)-1);
105 return strcasecmp(str1, str2);
109 int OPENSSL_memcmp(const void *v1, const void *v2, size_t n)
111 const unsigned char *c1 = v1, *c2 = v2;
114 while (n && (ret = *c1 - *c2) == 0)
120 char *CRYPTO_strdup(const char *str, const char* file, int line)
127 size = strlen(str) + 1;
128 ret = CRYPTO_malloc(size, file, line);
130 memcpy(ret, str, size);
134 char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line)
142 maxlen = OPENSSL_strnlen(str, s);
144 ret = CRYPTO_malloc(maxlen + 1, file, line);
146 memcpy(ret, str, maxlen);
152 void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line)
156 if (data == NULL || siz >= INT_MAX)
159 ret = CRYPTO_malloc(siz, file, line);
161 CRYPTOerr(CRYPTO_F_CRYPTO_MEMDUP, ERR_R_MALLOC_FAILURE);
164 return memcpy(ret, data, siz);
167 size_t OPENSSL_strnlen(const char *str, size_t maxlen)
171 for (p = str; maxlen-- != 0 && *p != '\0'; ++p) ;
176 size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)
179 for (; size > 1 && *src; size--) {
185 return l + strlen(src);
188 size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)
191 for (; size > 0 && *dst; size--, dst++)
193 return l + OPENSSL_strlcpy(dst, src, size);