]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/o_str.c
various spelling fixes
[thirdparty/openssl.git] / crypto / o_str.c
CommitLineData
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 72int 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 100int 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
109int 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
120char *CRYPTO_strdup(const char *str, const char* file, int line)
121{
122 char *ret;
a89c9a0d 123 size_t size;
7644a9ae
RS
124
125 if (str == NULL)
126 return NULL;
a89c9a0d
DM
127 size = strlen(str) + 1;
128 ret = CRYPTO_malloc(size, file, line);
7644a9ae 129 if (ret != NULL)
a89c9a0d 130 memcpy(ret, str, size);
7644a9ae
RS
131 return ret;
132}
133
134char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line)
135{
7644a9ae
RS
136 size_t maxlen;
137 char *ret;
138
139 if (str == NULL)
140 return NULL;
141
b6e78584 142 maxlen = OPENSSL_strnlen(str, s);
7644a9ae
RS
143
144 ret = CRYPTO_malloc(maxlen + 1, file, line);
145 if (ret) {
146 memcpy(ret, str, maxlen);
147 ret[maxlen] = '\0';
148 }
149 return ret;
150}
151
152void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line)
153{
154 void *ret;
155
156 if (data == NULL || siz >= INT_MAX)
157 return NULL;
158
159 ret = CRYPTO_malloc(siz, file, line);
160 if (ret == NULL) {
161 CRYPTOerr(CRYPTO_F_CRYPTO_MEMDUP, ERR_R_MALLOC_FAILURE);
162 return NULL;
163 }
164 return memcpy(ret, data, siz);
165}
166
167size_t OPENSSL_strnlen(const char *str, size_t maxlen)
168{
169 const char *p;
170
171 for (p = str; maxlen-- != 0 && *p != '\0'; ++p) ;
172
173 return p - str;
174}
175
176size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)
177{
178 size_t l = 0;
179 for (; size > 1 && *src; size--) {
180 *dst++ = *src++;
181 l++;
182 }
183 if (size)
184 *dst = '\0';
185 return l + strlen(src);
186}
187
188size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)
189{
190 size_t l = 0;
191 for (; size > 0 && *dst; size--, dst++)
192 l++;
193 return l + OPENSSL_strlcpy(dst, src, size);
194}
14f051a0
RS
195
196int OPENSSL_hexchar2int(unsigned char c)
197{
198#ifdef CHARSET_EBCDIC
199 c = os_toebcdic[c];
200#endif
201
202 switch (c) {
203 case '0':
204 return 0;
205 case '1':
206 return 1;
207 case '2':
208 return 2;
209 case '3':
210 return 3;
211 case '4':
212 return 4;
213 case '5':
214 return 5;
215 case '6':
216 return 6;
217 case '7':
218 return 7;
219 case '8':
220 return 8;
221 case '9':
222 return 9;
223 case 'a': case 'A':
224 return 0x0A;
225 case 'b': case 'B':
226 return 0x0B;
227 case 'c': case 'C':
228 return 0x0C;
229 case 'd': case 'D':
230 return 0x0D;
231 case 'e': case 'E':
232 return 0x0E;
233 case 'f': case 'F':
234 return 0x0F;
235 }
236 return -1;
237}
238
239/*
240 * Give a string of hex digits convert to a buffer
241 */
242unsigned char *OPENSSL_hexstr2buf(const char *str, long *len)
243{
244 unsigned char *hexbuf, *q;
245 unsigned char ch, cl;
235f9329 246 int chi, cli;
14f051a0
RS
247 const unsigned char *p;
248 size_t s;
249
250 s = strlen(str);
251 if ((hexbuf = OPENSSL_malloc(s >> 1)) == NULL) {
252 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF, ERR_R_MALLOC_FAILURE);
253 return NULL;
254 }
255 for (p = (const unsigned char *)str, q = hexbuf; *p; ) {
256 ch = *p++;
257 if (ch == ':')
258 continue;
259 cl = *p++;
260 if (!cl) {
261 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF,
262 CRYPTO_R_ODD_NUMBER_OF_DIGITS);
263 OPENSSL_free(hexbuf);
264 return NULL;
265 }
235f9329
MC
266 cli = OPENSSL_hexchar2int(cl);
267 chi = OPENSSL_hexchar2int(ch);
268 if (cli < 0 || chi < 0) {
14f051a0
RS
269 OPENSSL_free(hexbuf);
270 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF, CRYPTO_R_ILLEGAL_HEX_DIGIT);
271 return NULL;
272 }
235f9329 273 *q++ = (unsigned char)((chi << 4) | cli);
14f051a0
RS
274 }
275
276 if (len)
277 *len = q - hexbuf;
278 return hexbuf;
279}
280
281/*
282 * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
283 * hex representation @@@ (Contents of buffer are always kept in ASCII, also
284 * on EBCDIC machines)
285 */
286char *OPENSSL_buf2hexstr(const unsigned char *buffer, long len)
287{
288 const static char hexdig[] = "0123456789ABCDEF";
289 char *tmp, *q;
290 const unsigned char *p;
291 int i;
292
293 if ((tmp = OPENSSL_malloc(len * 3 + 1)) == NULL) {
294 CRYPTOerr(CRYPTO_F_OPENSSL_BUF2HEXSTR, ERR_R_MALLOC_FAILURE);
295 return NULL;
296 }
297 q = tmp;
298 for (i = 0, p = buffer; i < len; i++, p++) {
299 *q++ = hexdig[(*p >> 4) & 0xf];
300 *q++ = hexdig[*p & 0xf];
301 *q++ = ':';
302 }
303 q[-1] = 0;
304#ifdef CHARSET_EBCDIC
305 ebcdic2ascii(tmp, tmp, q - tmp - 1);
306#endif
307
308 return tmp;
309}