2 * Copyright 2003-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
13 #include <openssl/crypto.h>
14 #include "internal/cryptlib.h"
15 #include "internal/o_str.h"
17 #if !defined(OPENSSL_IMPLEMENTS_strncasecmp) && \
18 !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_WINCE) && \
19 !defined(NETWARE_CLIB)
23 int OPENSSL_strncasecmp(const char *str1, const char *str2, size_t n)
25 #if defined(OPENSSL_IMPLEMENTS_strncasecmp)
26 while (*str1 && *str2 && n) {
27 int res = toupper(*str1) - toupper(*str2);
29 return res < 0 ? -1 : 1;
43 * Recursion hazard warning! Whenever strncasecmp is #defined as
44 * OPENSSL_strncasecmp, OPENSSL_IMPLEMENTS_strncasecmp must be defined as
47 return strncasecmp(str1, str2, n);
51 int OPENSSL_strcasecmp(const char *str1, const char *str2)
53 #if defined(OPENSSL_IMPLEMENTS_strncasecmp)
54 return OPENSSL_strncasecmp(str1, str2, (size_t)-1);
56 return strcasecmp(str1, str2);
60 int OPENSSL_memcmp(const void *v1, const void *v2, size_t n)
62 const unsigned char *c1 = v1, *c2 = v2;
65 while (n && (ret = *c1 - *c2) == 0)
71 char *CRYPTO_strdup(const char *str, const char* file, int line)
78 size = strlen(str) + 1;
79 ret = CRYPTO_malloc(size, file, line);
81 memcpy(ret, str, size);
85 char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line)
93 maxlen = OPENSSL_strnlen(str, s);
95 ret = CRYPTO_malloc(maxlen + 1, file, line);
97 memcpy(ret, str, maxlen);
103 void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line)
107 if (data == NULL || siz >= INT_MAX)
110 ret = CRYPTO_malloc(siz, file, line);
112 CRYPTOerr(CRYPTO_F_CRYPTO_MEMDUP, ERR_R_MALLOC_FAILURE);
115 return memcpy(ret, data, siz);
118 size_t OPENSSL_strnlen(const char *str, size_t maxlen)
122 for (p = str; maxlen-- != 0 && *p != '\0'; ++p) ;
127 size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)
130 for (; size > 1 && *src; size--) {
136 return l + strlen(src);
139 size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)
142 for (; size > 0 && *dst; size--, dst++)
144 return l + OPENSSL_strlcpy(dst, src, size);
147 int OPENSSL_hexchar2int(unsigned char c)
149 #ifdef CHARSET_EBCDIC
191 * Give a string of hex digits convert to a buffer
193 unsigned char *OPENSSL_hexstr2buf(const char *str, long *len)
195 unsigned char *hexbuf, *q;
196 unsigned char ch, cl;
198 const unsigned char *p;
202 if ((hexbuf = OPENSSL_malloc(s >> 1)) == NULL) {
203 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF, ERR_R_MALLOC_FAILURE);
206 for (p = (const unsigned char *)str, q = hexbuf; *p; ) {
212 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF,
213 CRYPTO_R_ODD_NUMBER_OF_DIGITS);
214 OPENSSL_free(hexbuf);
217 cli = OPENSSL_hexchar2int(cl);
218 chi = OPENSSL_hexchar2int(ch);
219 if (cli < 0 || chi < 0) {
220 OPENSSL_free(hexbuf);
221 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF, CRYPTO_R_ILLEGAL_HEX_DIGIT);
224 *q++ = (unsigned char)((chi << 4) | cli);
233 * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
234 * hex representation @@@ (Contents of buffer are always kept in ASCII, also
235 * on EBCDIC machines)
237 char *OPENSSL_buf2hexstr(const unsigned char *buffer, long len)
239 const static char hexdig[] = "0123456789ABCDEF";
241 const unsigned char *p;
244 if ((tmp = OPENSSL_malloc(len * 3 + 1)) == NULL) {
245 CRYPTOerr(CRYPTO_F_OPENSSL_BUF2HEXSTR, ERR_R_MALLOC_FAILURE);
249 for (i = 0, p = buffer; i < len; i++, p++) {
250 *q++ = hexdig[(*p >> 4) & 0xf];
251 *q++ = hexdig[*p & 0xf];
255 #ifdef CHARSET_EBCDIC
256 ebcdic2ascii(tmp, tmp, q - tmp - 1);
262 int openssl_strerror_r(int errnum, char *buf, size_t buflen)
264 #if defined(OPENSSL_SYS_WINDOWS)
265 if (strerror_s(buf, buflen, errnum) == EINVAL)
268 #elif (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE
270 * We can use "real" strerror_r. The OpenSSL version differs in that it
271 * gives 1 on success and 0 on failure for consistency with other OpenSSL
272 * functions. Real strerror_r does it the other way around
274 return !strerror_r(errnum, buf, buflen);
277 /* Fall back to non-thread safe strerror()...its all we can do */
280 err = strerror(errnum);
281 /* Can this ever happen? */
284 strncpy(buf, err, buflen - 1);
285 buf[buflen - 1] = '\0';