2 * Copyright 2003-2017 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
12 #include <openssl/crypto.h>
13 #include "internal/cryptlib.h"
14 #include "internal/o_str.h"
16 int OPENSSL_memcmp(const void *v1, const void *v2, size_t n)
18 const unsigned char *c1 = v1, *c2 = v2;
21 while (n && (ret = *c1 - *c2) == 0)
27 char *CRYPTO_strdup(const char *str, const char* file, int line)
33 ret = CRYPTO_malloc(strlen(str) + 1, file, line);
39 char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line)
47 maxlen = OPENSSL_strnlen(str, s);
49 ret = CRYPTO_malloc(maxlen + 1, file, line);
51 memcpy(ret, str, maxlen);
57 void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line)
61 if (data == NULL || siz >= INT_MAX)
64 ret = CRYPTO_malloc(siz, file, line);
66 CRYPTOerr(CRYPTO_F_CRYPTO_MEMDUP, ERR_R_MALLOC_FAILURE);
69 return memcpy(ret, data, siz);
72 size_t OPENSSL_strnlen(const char *str, size_t maxlen)
76 for (p = str; maxlen-- != 0 && *p != '\0'; ++p) ;
81 size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)
84 for (; size > 1 && *src; size--) {
90 return l + strlen(src);
93 size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)
96 for (; size > 0 && *dst; size--, dst++)
98 return l + OPENSSL_strlcpy(dst, src, size);
101 int OPENSSL_hexchar2int(unsigned char c)
103 #ifdef CHARSET_EBCDIC
145 * Give a string of hex digits convert to a buffer
147 unsigned char *OPENSSL_hexstr2buf(const char *str, long *len)
149 unsigned char *hexbuf, *q;
150 unsigned char ch, cl;
152 const unsigned char *p;
156 if ((hexbuf = OPENSSL_malloc(s >> 1)) == NULL) {
157 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF, ERR_R_MALLOC_FAILURE);
160 for (p = (const unsigned char *)str, q = hexbuf; *p; ) {
166 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF,
167 CRYPTO_R_ODD_NUMBER_OF_DIGITS);
168 OPENSSL_free(hexbuf);
171 cli = OPENSSL_hexchar2int(cl);
172 chi = OPENSSL_hexchar2int(ch);
173 if (cli < 0 || chi < 0) {
174 OPENSSL_free(hexbuf);
175 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF, CRYPTO_R_ILLEGAL_HEX_DIGIT);
178 *q++ = (unsigned char)((chi << 4) | cli);
187 * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
188 * hex representation @@@ (Contents of buffer are always kept in ASCII, also
189 * on EBCDIC machines)
191 char *OPENSSL_buf2hexstr(const unsigned char *buffer, long len)
193 static const char hexdig[] = "0123456789ABCDEF";
195 const unsigned char *p;
200 return OPENSSL_zalloc(1);
203 if ((tmp = OPENSSL_malloc(len * 3)) == NULL) {
204 CRYPTOerr(CRYPTO_F_OPENSSL_BUF2HEXSTR, ERR_R_MALLOC_FAILURE);
208 for (i = 0, p = buffer; i < len; i++, p++) {
209 *q++ = hexdig[(*p >> 4) & 0xf];
210 *q++ = hexdig[*p & 0xf];
214 #ifdef CHARSET_EBCDIC
215 ebcdic2ascii(tmp, tmp, q - tmp - 1);
221 int openssl_strerror_r(int errnum, char *buf, size_t buflen)
223 #if defined(_MSC_VER) && _MSC_VER>=1400
224 return !strerror_s(buf, buflen, errnum);
225 #elif defined(_GNU_SOURCE)
229 * GNU strerror_r may not actually set buf.
230 * It can return a pointer to some (immutable) static string in which case
231 * buf is left unused.
233 err = strerror_r(errnum, buf, buflen);
237 * If err is statically allocated, err != buf and we need to copy the data.
238 * If err points somewhere inside buf, OPENSSL_strlcpy can handle this,
239 * since src and dest are not annotated with __restrict and the function
240 * reads src byte for byte and writes to dest.
241 * If err == buf we do not have to copy anything.
244 OPENSSL_strlcpy(buf, err, buflen);
246 #elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
247 (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
249 * We can use "real" strerror_r. The OpenSSL version differs in that it
250 * gives 1 on success and 0 on failure for consistency with other OpenSSL
251 * functions. Real strerror_r does it the other way around
253 return !strerror_r(errnum, buf, buflen);
257 /* Fall back to non-thread safe strerror()...its all we can do */
260 err = strerror(errnum);
261 /* Can this ever happen? */
264 OPENSSL_strlcpy(buf, err, buflen);