]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/mem_str.c
Add EVP_CIPHER_do_all_ex() and EVP_MD_do_all_ex()
[thirdparty/openssl.git] / crypto / mem_str.c
CommitLineData
b60cba3c
RS
1/*
2 * Copyright 2003-2017 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10#include "e_os.h"
11#include <limits.h>
12#include <openssl/crypto.h>
13#include "internal/cryptlib.h"
14
15char *CRYPTO_strdup(const char *str, const char* file, int line)
16{
17 char *ret;
18
19 if (str == NULL)
20 return NULL;
21 ret = CRYPTO_malloc(strlen(str) + 1, file, line);
22 if (ret != NULL)
23 strcpy(ret, str);
24 return ret;
25}
26
27char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line)
28{
29 size_t maxlen;
30 char *ret;
31
32 if (str == NULL)
33 return NULL;
34
35 maxlen = OPENSSL_strnlen(str, s);
36
37 ret = CRYPTO_malloc(maxlen + 1, file, line);
38 if (ret) {
39 memcpy(ret, str, maxlen);
40 ret[maxlen] = '\0';
41 }
42 return ret;
43}
44
45void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line)
46{
47 void *ret;
48
49 if (data == NULL || siz >= INT_MAX)
50 return NULL;
51
52 ret = CRYPTO_malloc(siz, file, line);
53 if (ret == NULL) {
54 CRYPTOerr(CRYPTO_F_CRYPTO_MEMDUP, ERR_R_MALLOC_FAILURE);
55 return NULL;
56 }
57 return memcpy(ret, data, siz);
58}
59
60/*
61 * Give a string of hex digits convert to a buffer
62 */
63unsigned char *OPENSSL_hexstr2buf(const char *str, long *len)
64{
65 unsigned char *hexbuf, *q;
66 unsigned char ch, cl;
67 int chi, cli;
68 const unsigned char *p;
69 size_t s;
70
71 s = strlen(str);
72 if ((hexbuf = OPENSSL_malloc(s >> 1)) == NULL) {
73 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF, ERR_R_MALLOC_FAILURE);
74 return NULL;
75 }
76 for (p = (const unsigned char *)str, q = hexbuf; *p; ) {
77 ch = *p++;
78 if (ch == ':')
79 continue;
80 cl = *p++;
81 if (!cl) {
82 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF,
83 CRYPTO_R_ODD_NUMBER_OF_DIGITS);
84 OPENSSL_free(hexbuf);
85 return NULL;
86 }
87 cli = OPENSSL_hexchar2int(cl);
88 chi = OPENSSL_hexchar2int(ch);
89 if (cli < 0 || chi < 0) {
90 OPENSSL_free(hexbuf);
91 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF, CRYPTO_R_ILLEGAL_HEX_DIGIT);
92 return NULL;
93 }
94 *q++ = (unsigned char)((chi << 4) | cli);
95 }
96
97 if (len)
98 *len = q - hexbuf;
99 return hexbuf;
100}
101
102/*
103 * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
104 * hex representation @@@ (Contents of buffer are always kept in ASCII, also
105 * on EBCDIC machines)
106 */
107char *OPENSSL_buf2hexstr(const unsigned char *buffer, long len)
108{
109 static const char hexdig[] = "0123456789ABCDEF";
110 char *tmp, *q;
111 const unsigned char *p;
112 int i;
113
114 if (len == 0)
115 return OPENSSL_zalloc(1);
116
117 if ((tmp = OPENSSL_malloc(len * 3)) == NULL) {
118 CRYPTOerr(CRYPTO_F_OPENSSL_BUF2HEXSTR, ERR_R_MALLOC_FAILURE);
119 return NULL;
120 }
121 q = tmp;
122 for (i = 0, p = buffer; i < len; i++, p++) {
123 *q++ = hexdig[(*p >> 4) & 0xf];
124 *q++ = hexdig[*p & 0xf];
125 *q++ = ':';
126 }
127 q[-1] = 0;
128#ifdef CHARSET_EBCDIC
129 ebcdic2ascii(tmp, tmp, q - tmp - 1);
130#endif
131
132 return tmp;
133}