2 * Copyright 2003-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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"
15 char *CRYPTO_strdup(const char *str
, const char* file
, int line
)
21 ret
= CRYPTO_malloc(strlen(str
) + 1, file
, line
);
27 char *CRYPTO_strndup(const char *str
, size_t s
, const char* file
, int line
)
35 maxlen
= OPENSSL_strnlen(str
, s
);
37 ret
= CRYPTO_malloc(maxlen
+ 1, file
, line
);
39 memcpy(ret
, str
, maxlen
);
45 void *CRYPTO_memdup(const void *data
, size_t siz
, const char* file
, int line
)
49 if (data
== NULL
|| siz
>= INT_MAX
)
52 ret
= CRYPTO_malloc(siz
, file
, line
);
54 CRYPTOerr(CRYPTO_F_CRYPTO_MEMDUP
, ERR_R_MALLOC_FAILURE
);
57 return memcpy(ret
, data
, siz
);
60 size_t OPENSSL_strnlen(const char *str
, size_t maxlen
)
64 for (p
= str
; maxlen
-- != 0 && *p
!= '\0'; ++p
) ;
69 size_t OPENSSL_strlcpy(char *dst
, const char *src
, size_t size
)
72 for (; size
> 1 && *src
; size
--) {
78 return l
+ strlen(src
);
81 size_t OPENSSL_strlcat(char *dst
, const char *src
, size_t size
)
84 for (; size
> 0 && *dst
; size
--, dst
++)
86 return l
+ OPENSSL_strlcpy(dst
, src
, size
);
89 int OPENSSL_hexchar2int(unsigned char c
)
133 * Give a string of hex digits convert to a buffer
135 int OPENSSL_hexstr2buf_ex(unsigned char *buf
, size_t buf_n
, size_t *buflen
,
139 unsigned char ch
, cl
;
141 const unsigned char *p
;
144 for (p
= (const unsigned char *)str
, q
= buf
, cnt
= 0; *p
; ) {
150 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF_EX
,
151 CRYPTO_R_ODD_NUMBER_OF_DIGITS
);
154 cli
= OPENSSL_hexchar2int(cl
);
155 chi
= OPENSSL_hexchar2int(ch
);
156 if (cli
< 0 || chi
< 0) {
157 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF_EX
,
158 CRYPTO_R_ILLEGAL_HEX_DIGIT
);
164 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF_EX
,
165 CRYPTO_R_TOO_SMALL_BUFFER
);
168 *q
++ = (unsigned char)((chi
<< 4) | cli
);
177 unsigned char *OPENSSL_hexstr2buf(const char *str
, long *buflen
)
180 size_t buf_n
, tmp_buflen
;
182 buf_n
= strlen(str
) >> 1;
183 if ((buf
= OPENSSL_malloc(buf_n
)) == NULL
) {
184 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF
, ERR_R_MALLOC_FAILURE
);
191 if (OPENSSL_hexstr2buf_ex(buf
, buf_n
, &tmp_buflen
, str
)) {
193 *buflen
= (long)tmp_buflen
;
200 int OPENSSL_buf2hexstr_ex(char *str
, size_t str_n
, size_t *strlen
,
201 const unsigned char *buf
, size_t buflen
)
203 static const char hexdig
[] = "0123456789ABCDEF";
204 const unsigned char *p
;
209 *strlen
= buflen
* 3;
213 if (str_n
< (unsigned long)buflen
* 3) {
214 CRYPTOerr(CRYPTO_F_OPENSSL_BUF2HEXSTR_EX
, CRYPTO_R_TOO_SMALL_BUFFER
);
219 for (i
= 0, p
= buf
; i
< buflen
; i
++, p
++) {
220 *q
++ = hexdig
[(*p
>> 4) & 0xf];
221 *q
++ = hexdig
[*p
& 0xf];
225 #ifdef CHARSET_EBCDIC
226 ebcdic2ascii(str
, str
, q
- str
- 1);
232 * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
233 * hex representation @@@ (Contents of buffer are always kept in ASCII, also
234 * on EBCDIC machines)
236 char *OPENSSL_buf2hexstr(const unsigned char *buf
, long buflen
)
242 return OPENSSL_zalloc(1);
245 if ((tmp
= OPENSSL_malloc(tmp_n
)) == NULL
) {
246 CRYPTOerr(CRYPTO_F_OPENSSL_BUF2HEXSTR
, ERR_R_MALLOC_FAILURE
);
250 if (OPENSSL_buf2hexstr_ex(tmp
, tmp_n
, NULL
, buf
, buflen
))
256 int openssl_strerror_r(int errnum
, char *buf
, size_t buflen
)
258 #if defined(_MSC_VER) && _MSC_VER>=1400
259 return !strerror_s(buf
, buflen
, errnum
);
260 #elif defined(_GNU_SOURCE)
264 * GNU strerror_r may not actually set buf.
265 * It can return a pointer to some (immutable) static string in which case
266 * buf is left unused.
268 err
= strerror_r(errnum
, buf
, buflen
);
269 if (err
== NULL
|| buflen
== 0)
272 * If err is statically allocated, err != buf and we need to copy the data.
273 * If err points somewhere inside buf, OPENSSL_strlcpy can handle this,
274 * since src and dest are not annotated with __restrict and the function
275 * reads src byte for byte and writes to dest.
276 * If err == buf we do not have to copy anything.
279 OPENSSL_strlcpy(buf
, err
, buflen
);
281 #elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
282 (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
284 * We can use "real" strerror_r. The OpenSSL version differs in that it
285 * gives 1 on success and 0 on failure for consistency with other OpenSSL
286 * functions. Real strerror_r does it the other way around
288 return !strerror_r(errnum
, buf
, buflen
);
292 /* Fall back to non-thread safe strerror()...its all we can do */
295 err
= strerror(errnum
);
296 /* Can this ever happen? */
299 OPENSSL_strlcpy(buf
, err
, buflen
);