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 #define DEFAULT_SEPARATOR ':'
18 char *CRYPTO_strdup(const char *str
, const char* file
, int line
)
24 ret
= CRYPTO_malloc(strlen(str
) + 1, file
, line
);
30 char *CRYPTO_strndup(const char *str
, size_t s
, const char* file
, int line
)
38 maxlen
= OPENSSL_strnlen(str
, s
);
40 ret
= CRYPTO_malloc(maxlen
+ 1, file
, line
);
42 memcpy(ret
, str
, maxlen
);
43 ret
[maxlen
] = CH_ZERO
;
48 void *CRYPTO_memdup(const void *data
, size_t siz
, const char* file
, int line
)
52 if (data
== NULL
|| siz
>= INT_MAX
)
55 ret
= CRYPTO_malloc(siz
, file
, line
);
57 CRYPTOerr(CRYPTO_F_CRYPTO_MEMDUP
, ERR_R_MALLOC_FAILURE
);
60 return memcpy(ret
, data
, siz
);
63 size_t OPENSSL_strnlen(const char *str
, size_t maxlen
)
67 for (p
= str
; maxlen
-- != 0 && *p
!= CH_ZERO
; ++p
) ;
72 size_t OPENSSL_strlcpy(char *dst
, const char *src
, size_t size
)
75 for (; size
> 1 && *src
; size
--) {
81 return l
+ strlen(src
);
84 size_t OPENSSL_strlcat(char *dst
, const char *src
, size_t size
)
87 for (; size
> 0 && *dst
; size
--, dst
++)
89 return l
+ OPENSSL_strlcpy(dst
, src
, size
);
92 int OPENSSL_hexchar2int(unsigned char c
)
135 static int hexstr2buf_sep(unsigned char *buf
, size_t buf_n
, size_t *buflen
,
136 const char *str
, const char sep
)
139 unsigned char ch
, cl
;
141 const unsigned char *p
;
144 for (p
= (const unsigned char *)str
, q
= buf
, cnt
= 0; *p
; ) {
146 /* A separator of CH_ZERO means there is no separator */
147 if (ch
== sep
&& sep
!= CH_ZERO
)
151 CRYPTOerr(0, CRYPTO_R_ODD_NUMBER_OF_DIGITS
);
154 cli
= OPENSSL_hexchar2int(cl
);
155 chi
= OPENSSL_hexchar2int(ch
);
156 if (cli
< 0 || chi
< 0) {
157 CRYPTOerr(0, CRYPTO_R_ILLEGAL_HEX_DIGIT
);
163 CRYPTOerr(0, CRYPTO_R_TOO_SMALL_BUFFER
);
166 *q
++ = (unsigned char)((chi
<< 4) | cli
);
176 * Given a string of hex digits convert to a buffer
178 int OPENSSL_hexstr2buf_ex(unsigned char *buf
, size_t buf_n
, size_t *buflen
,
181 return hexstr2buf_sep(buf
, buf_n
, buflen
, str
, DEFAULT_SEPARATOR
);
184 unsigned char *openssl_hexstr2buf_sep(const char *str
, long *buflen
,
188 size_t buf_n
, tmp_buflen
;
190 buf_n
= strlen(str
) >> 1;
191 if ((buf
= OPENSSL_malloc(buf_n
)) == NULL
) {
192 CRYPTOerr(0, ERR_R_MALLOC_FAILURE
);
199 if (hexstr2buf_sep(buf
, buf_n
, &tmp_buflen
, str
, sep
)) {
201 *buflen
= (long)tmp_buflen
;
208 unsigned char *OPENSSL_hexstr2buf(const char *str
, long *buflen
)
210 return openssl_hexstr2buf_sep(str
, buflen
, DEFAULT_SEPARATOR
);
213 static int buf2hexstr_sep(char *str
, size_t str_n
, size_t *strlen
,
214 const unsigned char *buf
, size_t buflen
,
217 static const char hexdig
[] = "0123456789ABCDEF";
218 const unsigned char *p
;
221 int has_sep
= (sep
!= CH_ZERO
);
222 size_t len
= has_sep
? buflen
* 3 : 1 + buflen
* 2;
229 if (str_n
< (unsigned long)len
) {
230 CRYPTOerr(0, CRYPTO_R_TOO_SMALL_BUFFER
);
235 for (i
= 0, p
= buf
; i
< buflen
; i
++, p
++) {
236 *q
++ = hexdig
[(*p
>> 4) & 0xf];
237 *q
++ = hexdig
[*p
& 0xf];
245 #ifdef CHARSET_EBCDIC
246 ebcdic2ascii(str
, str
, q
- str
- 1);
251 int OPENSSL_buf2hexstr_ex(char *str
, size_t str_n
, size_t *strlen
,
252 const unsigned char *buf
, size_t buflen
)
254 return buf2hexstr_sep(str
, str_n
, strlen
, buf
, buflen
, DEFAULT_SEPARATOR
);
257 char *openssl_buf2hexstr_sep(const unsigned char *buf
, long buflen
, char sep
)
263 return OPENSSL_zalloc(1);
265 tmp_n
= (sep
!= CH_ZERO
) ? buflen
* 3 : 1 + buflen
* 2;
266 if ((tmp
= OPENSSL_malloc(tmp_n
)) == NULL
) {
267 CRYPTOerr(0, ERR_R_MALLOC_FAILURE
);
271 if (buf2hexstr_sep(tmp
, tmp_n
, NULL
, buf
, buflen
, sep
))
279 * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
280 * hex representation @@@ (Contents of buffer are always kept in ASCII, also
281 * on EBCDIC machines)
283 char *OPENSSL_buf2hexstr(const unsigned char *buf
, long buflen
)
285 return openssl_buf2hexstr_sep(buf
, buflen
, ':');
288 int openssl_strerror_r(int errnum
, char *buf
, size_t buflen
)
290 #if defined(_MSC_VER) && _MSC_VER>=1400
291 return !strerror_s(buf
, buflen
, errnum
);
292 #elif defined(_GNU_SOURCE)
296 * GNU strerror_r may not actually set buf.
297 * It can return a pointer to some (immutable) static string in which case
298 * buf is left unused.
300 err
= strerror_r(errnum
, buf
, buflen
);
301 if (err
== NULL
|| buflen
== 0)
304 * If err is statically allocated, err != buf and we need to copy the data.
305 * If err points somewhere inside buf, OPENSSL_strlcpy can handle this,
306 * since src and dest are not annotated with __restrict and the function
307 * reads src byte for byte and writes to dest.
308 * If err == buf we do not have to copy anything.
311 OPENSSL_strlcpy(buf
, err
, buflen
);
313 #elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
314 (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
316 * We can use "real" strerror_r. The OpenSSL version differs in that it
317 * gives 1 on success and 0 on failure for consistency with other OpenSSL
318 * functions. Real strerror_r does it the other way around
320 return !strerror_r(errnum
, buf
, buflen
);
324 /* Fall back to non-thread safe strerror()...its all we can do */
327 err
= strerror(errnum
);
328 /* Can this ever happen? */
331 OPENSSL_strlcpy(buf
, err
, buflen
);