VirtualBox

source: vbox/trunk/src/libs/openssl-3.3.2/crypto/o_str.c@ 108358

最後變更 在這個檔案從108358是 108206,由 vboxsync 提交於 5 週 前

openssl-3.3.2: Exported all files to OSE and removed .scm-settings ​bugref:10757

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 8.8 KB
 
1/*
2 * Copyright 2003-2024 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 "internal/e_os.h"
11#include <string.h>
12#include <limits.h>
13#include <openssl/crypto.h>
14#include "crypto/ctype.h"
15#include "internal/cryptlib.h"
16#include "internal/thread_once.h"
17
18#define DEFAULT_SEPARATOR ':'
19#define CH_ZERO '\0'
20
21char *CRYPTO_strdup(const char *str, const char* file, int line)
22{
23 char *ret;
24
25 if (str == NULL)
26 return NULL;
27 ret = CRYPTO_malloc(strlen(str) + 1, file, line);
28 if (ret != NULL)
29 strcpy(ret, str);
30 return ret;
31}
32
33char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line)
34{
35 size_t maxlen;
36 char *ret;
37
38 if (str == NULL)
39 return NULL;
40
41 maxlen = OPENSSL_strnlen(str, s);
42
43 ret = CRYPTO_malloc(maxlen + 1, file, line);
44 if (ret) {
45 memcpy(ret, str, maxlen);
46 ret[maxlen] = CH_ZERO;
47 }
48 return ret;
49}
50
51void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line)
52{
53 void *ret;
54
55 if (data == NULL || siz >= INT_MAX)
56 return NULL;
57
58 ret = CRYPTO_malloc(siz, file, line);
59 if (ret == NULL)
60 return NULL;
61 return memcpy(ret, data, siz);
62}
63
64size_t OPENSSL_strnlen(const char *str, size_t maxlen)
65{
66 const char *p;
67
68 for (p = str; maxlen-- != 0 && *p != CH_ZERO; ++p) ;
69
70 return p - str;
71}
72
73size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)
74{
75 size_t l = 0;
76 for (; size > 1 && *src; size--) {
77 *dst++ = *src++;
78 l++;
79 }
80 if (size)
81 *dst = CH_ZERO;
82 return l + strlen(src);
83}
84
85size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)
86{
87 size_t l = 0;
88 for (; size > 0 && *dst; size--, dst++)
89 l++;
90 return l + OPENSSL_strlcpy(dst, src, size);
91}
92
93int OPENSSL_hexchar2int(unsigned char c)
94{
95#ifdef CHARSET_EBCDIC
96 c = os_toebcdic[c];
97#endif
98
99 switch (c) {
100 case '0':
101 return 0;
102 case '1':
103 return 1;
104 case '2':
105 return 2;
106 case '3':
107 return 3;
108 case '4':
109 return 4;
110 case '5':
111 return 5;
112 case '6':
113 return 6;
114 case '7':
115 return 7;
116 case '8':
117 return 8;
118 case '9':
119 return 9;
120 case 'a': case 'A':
121 return 0x0A;
122 case 'b': case 'B':
123 return 0x0B;
124 case 'c': case 'C':
125 return 0x0C;
126 case 'd': case 'D':
127 return 0x0D;
128 case 'e': case 'E':
129 return 0x0E;
130 case 'f': case 'F':
131 return 0x0F;
132 }
133 return -1;
134}
135
136static int hexstr2buf_sep(unsigned char *buf, size_t buf_n, size_t *buflen,
137 const char *str, const char sep)
138{
139 unsigned char *q;
140 unsigned char ch, cl;
141 int chi, cli;
142 const unsigned char *p;
143 size_t cnt;
144
145 for (p = (const unsigned char *)str, q = buf, cnt = 0; *p; ) {
146 ch = *p++;
147 /* A separator of CH_ZERO means there is no separator */
148 if (ch == sep && sep != CH_ZERO)
149 continue;
150 cl = *p++;
151 if (!cl) {
152 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ODD_NUMBER_OF_DIGITS);
153 return 0;
154 }
155 cli = OPENSSL_hexchar2int(cl);
156 chi = OPENSSL_hexchar2int(ch);
157 if (cli < 0 || chi < 0) {
158 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ILLEGAL_HEX_DIGIT);
159 return 0;
160 }
161 cnt++;
162 if (q != NULL) {
163 if (cnt > buf_n) {
164 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
165 return 0;
166 }
167 *q++ = (unsigned char)((chi << 4) | cli);
168 }
169 }
170
171 if (buflen != NULL)
172 *buflen = cnt;
173 return 1;
174}
175
176/*
177 * Given a string of hex digits convert to a buffer
178 */
179int OPENSSL_hexstr2buf_ex(unsigned char *buf, size_t buf_n, size_t *buflen,
180 const char *str, const char sep)
181{
182 return hexstr2buf_sep(buf, buf_n, buflen, str, sep);
183}
184
185unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen,
186 const char sep)
187{
188 unsigned char *buf;
189 size_t buf_n, tmp_buflen;
190
191 buf_n = strlen(str);
192 if (buf_n <= 1) {
193 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_HEX_STRING_TOO_SHORT);
194 return NULL;
195 }
196 buf_n /= 2;
197 if ((buf = OPENSSL_malloc(buf_n)) == NULL)
198 return NULL;
199
200 if (buflen != NULL)
201 *buflen = 0;
202 tmp_buflen = 0;
203 if (hexstr2buf_sep(buf, buf_n, &tmp_buflen, str, sep)) {
204 if (buflen != NULL)
205 *buflen = (long)tmp_buflen;
206 return buf;
207 }
208 OPENSSL_free(buf);
209 return NULL;
210}
211
212unsigned char *OPENSSL_hexstr2buf(const char *str, long *buflen)
213{
214 return ossl_hexstr2buf_sep(str, buflen, DEFAULT_SEPARATOR);
215}
216
217static int buf2hexstr_sep(char *str, size_t str_n, size_t *strlength,
218 const unsigned char *buf, size_t buflen,
219 const char sep)
220{
221 static const char hexdig[] = "0123456789ABCDEF";
222 const unsigned char *p;
223 char *q;
224 size_t i;
225 int has_sep = (sep != CH_ZERO);
226 size_t len = has_sep ? buflen * 3 : 1 + buflen * 2;
227
228 if (len == 0)
229 ++len;
230 if (strlength != NULL)
231 *strlength = len;
232 if (str == NULL)
233 return 1;
234
235 if (str_n < len) {
236 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
237 return 0;
238 }
239
240 q = str;
241 for (i = 0, p = buf; i < buflen; i++, p++) {
242 *q++ = hexdig[(*p >> 4) & 0xf];
243 *q++ = hexdig[*p & 0xf];
244 if (has_sep)
245 *q++ = sep;
246 }
247 if (has_sep && buflen > 0)
248 --q;
249 *q = CH_ZERO;
250
251#ifdef CHARSET_EBCDIC
252 ebcdic2ascii(str, str, q - str);
253#endif
254 return 1;
255}
256
257int OPENSSL_buf2hexstr_ex(char *str, size_t str_n, size_t *strlength,
258 const unsigned char *buf, size_t buflen,
259 const char sep)
260{
261 return buf2hexstr_sep(str, str_n, strlength, buf, buflen, sep);
262}
263
264char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep)
265{
266 char *tmp;
267 size_t tmp_n;
268
269 if (buflen == 0)
270 return OPENSSL_zalloc(1);
271
272 tmp_n = (sep != CH_ZERO) ? buflen * 3 : 1 + buflen * 2;
273 if ((tmp = OPENSSL_malloc(tmp_n)) == NULL)
274 return NULL;
275
276 if (buf2hexstr_sep(tmp, tmp_n, NULL, buf, buflen, sep))
277 return tmp;
278 OPENSSL_free(tmp);
279 return NULL;
280}
281
282
283/*
284 * Given a buffer of length 'buflen' return a OPENSSL_malloc'ed string with
285 * its hex representation @@@ (Contents of buffer are always kept in ASCII,
286 * also on EBCDIC machines)
287 */
288char *OPENSSL_buf2hexstr(const unsigned char *buf, long buflen)
289{
290 return ossl_buf2hexstr_sep(buf, buflen, DEFAULT_SEPARATOR);
291}
292
293int openssl_strerror_r(int errnum, char *buf, size_t buflen)
294{
295#if defined(_MSC_VER) && _MSC_VER>=1400 && !defined(_WIN32_WCE)
296 return !strerror_s(buf, buflen, errnum);
297#elif defined(_GNU_SOURCE) /*VBox:*/ && !defined(RT_OS_SOLARIS)
298 char *err;
299
300 /*
301 * GNU strerror_r may not actually set buf.
302 * It can return a pointer to some (immutable) static string in which case
303 * buf is left unused.
304 */
305 err = strerror_r(errnum, buf, buflen);
306 if (err == NULL || buflen == 0)
307 return 0;
308 /*
309 * If err is statically allocated, err != buf and we need to copy the data.
310 * If err points somewhere inside buf, OPENSSL_strlcpy can handle this,
311 * since src and dest are not annotated with __restrict and the function
312 * reads src byte for byte and writes to dest.
313 * If err == buf we do not have to copy anything.
314 */
315 if (err != buf)
316 OPENSSL_strlcpy(buf, err, buflen);
317 return 1;
318#elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
319 (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
320 /*
321 * We can use "real" strerror_r. The OpenSSL version differs in that it
322 * gives 1 on success and 0 on failure for consistency with other OpenSSL
323 * functions. Real strerror_r does it the other way around
324 */
325 return !strerror_r(errnum, buf, buflen);
326#else
327 char *err;
328
329 /* Fall back to non-thread safe strerror()...its all we can do */
330 if (buflen < 2)
331 return 0;
332 err = strerror(errnum);
333 /* Can this ever happen? */
334 if (err == NULL)
335 return 0;
336 OPENSSL_strlcpy(buf, err, buflen);
337 return 1;
338#endif
339}
340
341int OPENSSL_strcasecmp(const char *s1, const char *s2)
342{
343 int t;
344
345 while ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) == 0)
346 if (*s1++ == '\0')
347 return 0;
348 return t;
349}
350
351int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n)
352{
353 int t;
354 size_t i;
355
356 for (i = 0; i < n; i++)
357 if ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) != 0)
358 return t;
359 else if (*s1++ == '\0')
360 return 0;
361 return 0;
362}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette