]>
Commit | Line | Data |
---|---|---|
0f113f3e MC |
1 | /* |
2 | * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project | |
3 | * 2003. | |
e6fa67fa RL |
4 | */ |
5 | /* ==================================================================== | |
6 | * Copyright (c) 2003 The OpenSSL Project. All rights reserved. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 13 | * notice, this list of conditions and the following disclaimer. |
e6fa67fa RL |
14 | * |
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 | |
18 | * distribution. | |
19 | * | |
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/)" | |
24 | * | |
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. | |
29 | * | |
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. | |
33 | * | |
34 | * 6. Redistributions of any form whatsoever must retain the following | |
35 | * acknowledgment: | |
36 | * "This product includes software developed by the OpenSSL Project | |
37 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | |
38 | * | |
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 | * ==================================================================== | |
52 | * | |
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). | |
56 | * | |
57 | */ | |
58 | ||
8ee18dd5 | 59 | #include <ctype.h> |
7644a9ae | 60 | #include <limits.h> |
3b4de6e4 | 61 | #include <e_os.h> |
7644a9ae RS |
62 | #include <openssl/crypto.h> |
63 | #include "internal/cryptlib.h" | |
68570797 | 64 | #include "internal/o_str.h" |
e6fa67fa | 65 | |
01768425 | 66 | #if !defined(OPENSSL_IMPLEMENTS_strncasecmp) && \ |
68b00c23 | 67 | !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_WINCE) && \ |
01768425 | 68 | !defined(NETWARE_CLIB) |
f12797a4 BL |
69 | # include <strings.h> |
70 | #endif | |
71 | ||
e6fa67fa | 72 | int OPENSSL_strncasecmp(const char *str1, const char *str2, size_t n) |
0f113f3e | 73 | { |
e6d27baf | 74 | #if defined(OPENSSL_IMPLEMENTS_strncasecmp) |
0f113f3e MC |
75 | while (*str1 && *str2 && n) { |
76 | int res = toupper(*str1) - toupper(*str2); | |
77 | if (res) | |
78 | return res < 0 ? -1 : 1; | |
79 | str1++; | |
80 | str2++; | |
81 | n--; | |
82 | } | |
83 | if (n == 0) | |
84 | return 0; | |
85 | if (*str1) | |
86 | return 1; | |
87 | if (*str2) | |
88 | return -1; | |
89 | return 0; | |
e6fa67fa | 90 | #else |
0f113f3e MC |
91 | /* |
92 | * Recursion hazard warning! Whenever strncasecmp is #defined as | |
93 | * OPENSSL_strncasecmp, OPENSSL_IMPLEMENTS_strncasecmp must be defined as | |
94 | * well. | |
95 | */ | |
96 | return strncasecmp(str1, str2, n); | |
e6fa67fa | 97 | #endif |
0f113f3e MC |
98 | } |
99 | ||
e6fa67fa | 100 | int OPENSSL_strcasecmp(const char *str1, const char *str2) |
0f113f3e | 101 | { |
e6d27baf | 102 | #if defined(OPENSSL_IMPLEMENTS_strncasecmp) |
0f113f3e | 103 | return OPENSSL_strncasecmp(str1, str2, (size_t)-1); |
e6fa67fa | 104 | #else |
0f113f3e | 105 | return strcasecmp(str1, str2); |
e6fa67fa | 106 | #endif |
0f113f3e | 107 | } |
e6fa67fa | 108 | |
0f113f3e MC |
109 | int OPENSSL_memcmp(const void *v1, const void *v2, size_t n) |
110 | { | |
111 | const unsigned char *c1 = v1, *c2 = v2; | |
112 | int ret = 0; | |
7a06050c | 113 | |
0f113f3e MC |
114 | while (n && (ret = *c1 - *c2) == 0) |
115 | n--, c1++, c2++; | |
7a06050c | 116 | |
0f113f3e MC |
117 | return ret; |
118 | } | |
7644a9ae RS |
119 | |
120 | char *CRYPTO_strdup(const char *str, const char* file, int line) | |
121 | { | |
122 | char *ret; | |
123 | ||
124 | if (str == NULL) | |
125 | return NULL; | |
126 | ret = CRYPTO_malloc(strlen(str) + 1, file, line); | |
127 | if (ret != NULL) | |
128 | strcpy(ret, str); | |
129 | return ret; | |
130 | } | |
131 | ||
132 | char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line) | |
133 | { | |
134 | const char *cp; | |
135 | size_t maxlen; | |
136 | char *ret; | |
137 | ||
138 | if (str == NULL) | |
139 | return NULL; | |
140 | ||
141 | /* Get length. */ | |
142 | for (cp = str, maxlen = s; maxlen-- != 0 && *cp != '\0'; ++cp) | |
143 | continue; | |
144 | maxlen = cp - str; | |
145 | ||
146 | ret = CRYPTO_malloc(maxlen + 1, file, line); | |
147 | if (ret) { | |
148 | memcpy(ret, str, maxlen); | |
149 | ret[maxlen] = '\0'; | |
150 | } | |
151 | return ret; | |
152 | } | |
153 | ||
154 | void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line) | |
155 | { | |
156 | void *ret; | |
157 | ||
158 | if (data == NULL || siz >= INT_MAX) | |
159 | return NULL; | |
160 | ||
161 | ret = CRYPTO_malloc(siz, file, line); | |
162 | if (ret == NULL) { | |
163 | CRYPTOerr(CRYPTO_F_CRYPTO_MEMDUP, ERR_R_MALLOC_FAILURE); | |
164 | return NULL; | |
165 | } | |
166 | return memcpy(ret, data, siz); | |
167 | } | |
168 | ||
169 | size_t OPENSSL_strnlen(const char *str, size_t maxlen) | |
170 | { | |
171 | const char *p; | |
172 | ||
173 | for (p = str; maxlen-- != 0 && *p != '\0'; ++p) ; | |
174 | ||
175 | return p - str; | |
176 | } | |
177 | ||
178 | size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size) | |
179 | { | |
180 | size_t l = 0; | |
181 | for (; size > 1 && *src; size--) { | |
182 | *dst++ = *src++; | |
183 | l++; | |
184 | } | |
185 | if (size) | |
186 | *dst = '\0'; | |
187 | return l + strlen(src); | |
188 | } | |
189 | ||
190 | size_t OPENSSL_strlcat(char *dst, const char *src, size_t size) | |
191 | { | |
192 | size_t l = 0; | |
193 | for (; size > 0 && *dst; size--, dst++) | |
194 | l++; | |
195 | return l + OPENSSL_strlcpy(dst, src, size); | |
196 | } |