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
)
34 size
= strlen(str
) + 1;
35 ret
= CRYPTO_malloc(size
, file
, line
);
37 memcpy(ret
, str
, size
);
41 char *CRYPTO_strndup(const char *str
, size_t s
, const char* file
, int line
)
49 maxlen
= OPENSSL_strnlen(str
, s
);
51 ret
= CRYPTO_malloc(maxlen
+ 1, file
, line
);
53 memcpy(ret
, str
, maxlen
);
59 void *CRYPTO_memdup(const void *data
, size_t siz
, const char* file
, int line
)
63 if (data
== NULL
|| siz
>= INT_MAX
)
66 ret
= CRYPTO_malloc(siz
, file
, line
);
68 CRYPTOerr(CRYPTO_F_CRYPTO_MEMDUP
, ERR_R_MALLOC_FAILURE
);
71 return memcpy(ret
, data
, siz
);
74 size_t OPENSSL_strnlen(const char *str
, size_t maxlen
)
78 for (p
= str
; maxlen
-- != 0 && *p
!= '\0'; ++p
) ;
83 size_t OPENSSL_strlcpy(char *dst
, const char *src
, size_t size
)
86 for (; size
> 1 && *src
; size
--) {
92 return l
+ strlen(src
);
95 size_t OPENSSL_strlcat(char *dst
, const char *src
, size_t size
)
98 for (; size
> 0 && *dst
; size
--, dst
++)
100 return l
+ OPENSSL_strlcpy(dst
, src
, size
);
103 int OPENSSL_hexchar2int(unsigned char c
)
105 #ifdef CHARSET_EBCDIC
147 * Give a string of hex digits convert to a buffer
149 unsigned char *OPENSSL_hexstr2buf(const char *str
, long *len
)
151 unsigned char *hexbuf
, *q
;
152 unsigned char ch
, cl
;
154 const unsigned char *p
;
158 if ((hexbuf
= OPENSSL_malloc(s
>> 1)) == NULL
) {
159 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF
, ERR_R_MALLOC_FAILURE
);
162 for (p
= (const unsigned char *)str
, q
= hexbuf
; *p
; ) {
168 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF
,
169 CRYPTO_R_ODD_NUMBER_OF_DIGITS
);
170 OPENSSL_free(hexbuf
);
173 cli
= OPENSSL_hexchar2int(cl
);
174 chi
= OPENSSL_hexchar2int(ch
);
175 if (cli
< 0 || chi
< 0) {
176 OPENSSL_free(hexbuf
);
177 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF
, CRYPTO_R_ILLEGAL_HEX_DIGIT
);
180 *q
++ = (unsigned char)((chi
<< 4) | cli
);
189 * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
190 * hex representation @@@ (Contents of buffer are always kept in ASCII, also
191 * on EBCDIC machines)
193 char *OPENSSL_buf2hexstr(const unsigned char *buffer
, long len
)
195 static const char hexdig
[] = "0123456789ABCDEF";
197 const unsigned char *p
;
202 return OPENSSL_zalloc(1);
205 if ((tmp
= OPENSSL_malloc(len
* 3)) == NULL
) {
206 CRYPTOerr(CRYPTO_F_OPENSSL_BUF2HEXSTR
, ERR_R_MALLOC_FAILURE
);
210 for (i
= 0, p
= buffer
; i
< len
; i
++, p
++) {
211 *q
++ = hexdig
[(*p
>> 4) & 0xf];
212 *q
++ = hexdig
[*p
& 0xf];
216 #ifdef CHARSET_EBCDIC
217 ebcdic2ascii(tmp
, tmp
, q
- tmp
- 1);
223 int openssl_strerror_r(int errnum
, char *buf
, size_t buflen
)
225 #if defined(_MSC_VER) && _MSC_VER>=1400
226 return !strerror_s(buf
, buflen
, errnum
);
227 #elif defined(_GNU_SOURCE)
228 return strerror_r(errnum
, buf
, buflen
) != NULL
;
229 #elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
230 (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
232 * We can use "real" strerror_r. The OpenSSL version differs in that it
233 * gives 1 on success and 0 on failure for consistency with other OpenSSL
234 * functions. Real strerror_r does it the other way around
236 return !strerror_r(errnum
, buf
, buflen
);
239 /* Fall back to non-thread safe strerror()...its all we can do */
242 err
= strerror(errnum
);
243 /* Can this ever happen? */
246 strncpy(buf
, err
, buflen
- 1);
247 buf
[buflen
- 1] = '\0';