o_str.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright 2003-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. #include "e_os.h"
  10. #include <limits.h>
  11. #include <openssl/crypto.h>
  12. #include "internal/cryptlib.h"
  13. #include "internal/o_str.h"
  14. int OPENSSL_memcmp(const void *v1, const void *v2, size_t n)
  15. {
  16. const unsigned char *c1 = v1, *c2 = v2;
  17. int ret = 0;
  18. while (n && (ret = *c1 - *c2) == 0)
  19. n--, c1++, c2++;
  20. return ret;
  21. }
  22. char *CRYPTO_strdup(const char *str, const char* file, int line)
  23. {
  24. char *ret;
  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. char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line)
  33. {
  34. size_t maxlen;
  35. char *ret;
  36. if (str == NULL)
  37. return NULL;
  38. maxlen = OPENSSL_strnlen(str, s);
  39. ret = CRYPTO_malloc(maxlen + 1, file, line);
  40. if (ret) {
  41. memcpy(ret, str, maxlen);
  42. ret[maxlen] = '\0';
  43. }
  44. return ret;
  45. }
  46. void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line)
  47. {
  48. void *ret;
  49. if (data == NULL || siz >= INT_MAX)
  50. return NULL;
  51. ret = CRYPTO_malloc(siz, file, line);
  52. if (ret == NULL) {
  53. CRYPTOerr(CRYPTO_F_CRYPTO_MEMDUP, ERR_R_MALLOC_FAILURE);
  54. return NULL;
  55. }
  56. return memcpy(ret, data, siz);
  57. }
  58. size_t OPENSSL_strnlen(const char *str, size_t maxlen)
  59. {
  60. const char *p;
  61. for (p = str; maxlen-- != 0 && *p != '\0'; ++p) ;
  62. return p - str;
  63. }
  64. size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)
  65. {
  66. size_t l = 0;
  67. for (; size > 1 && *src; size--) {
  68. *dst++ = *src++;
  69. l++;
  70. }
  71. if (size)
  72. *dst = '\0';
  73. return l + strlen(src);
  74. }
  75. size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)
  76. {
  77. size_t l = 0;
  78. for (; size > 0 && *dst; size--, dst++)
  79. l++;
  80. return l + OPENSSL_strlcpy(dst, src, size);
  81. }
  82. int OPENSSL_hexchar2int(unsigned char c)
  83. {
  84. #ifdef CHARSET_EBCDIC
  85. c = os_toebcdic[c];
  86. #endif
  87. switch (c) {
  88. case '0':
  89. return 0;
  90. case '1':
  91. return 1;
  92. case '2':
  93. return 2;
  94. case '3':
  95. return 3;
  96. case '4':
  97. return 4;
  98. case '5':
  99. return 5;
  100. case '6':
  101. return 6;
  102. case '7':
  103. return 7;
  104. case '8':
  105. return 8;
  106. case '9':
  107. return 9;
  108. case 'a': case 'A':
  109. return 0x0A;
  110. case 'b': case 'B':
  111. return 0x0B;
  112. case 'c': case 'C':
  113. return 0x0C;
  114. case 'd': case 'D':
  115. return 0x0D;
  116. case 'e': case 'E':
  117. return 0x0E;
  118. case 'f': case 'F':
  119. return 0x0F;
  120. }
  121. return -1;
  122. }
  123. /*
  124. * Give a string of hex digits convert to a buffer
  125. */
  126. unsigned char *OPENSSL_hexstr2buf(const char *str, long *len)
  127. {
  128. unsigned char *hexbuf, *q;
  129. unsigned char ch, cl;
  130. int chi, cli;
  131. const unsigned char *p;
  132. size_t s;
  133. s = strlen(str);
  134. if ((hexbuf = OPENSSL_malloc(s >> 1)) == NULL) {
  135. CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF, ERR_R_MALLOC_FAILURE);
  136. return NULL;
  137. }
  138. for (p = (const unsigned char *)str, q = hexbuf; *p; ) {
  139. ch = *p++;
  140. if (ch == ':')
  141. continue;
  142. cl = *p++;
  143. if (!cl) {
  144. CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF,
  145. CRYPTO_R_ODD_NUMBER_OF_DIGITS);
  146. OPENSSL_free(hexbuf);
  147. return NULL;
  148. }
  149. cli = OPENSSL_hexchar2int(cl);
  150. chi = OPENSSL_hexchar2int(ch);
  151. if (cli < 0 || chi < 0) {
  152. OPENSSL_free(hexbuf);
  153. CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF, CRYPTO_R_ILLEGAL_HEX_DIGIT);
  154. return NULL;
  155. }
  156. *q++ = (unsigned char)((chi << 4) | cli);
  157. }
  158. if (len)
  159. *len = q - hexbuf;
  160. return hexbuf;
  161. }
  162. /*
  163. * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
  164. * hex representation @@@ (Contents of buffer are always kept in ASCII, also
  165. * on EBCDIC machines)
  166. */
  167. char *OPENSSL_buf2hexstr(const unsigned char *buffer, long len)
  168. {
  169. static const char hexdig[] = "0123456789ABCDEF";
  170. char *tmp, *q;
  171. const unsigned char *p;
  172. int i;
  173. if (len == 0)
  174. {
  175. return OPENSSL_zalloc(1);
  176. }
  177. if ((tmp = OPENSSL_malloc(len * 3)) == NULL) {
  178. CRYPTOerr(CRYPTO_F_OPENSSL_BUF2HEXSTR, ERR_R_MALLOC_FAILURE);
  179. return NULL;
  180. }
  181. q = tmp;
  182. for (i = 0, p = buffer; i < len; i++, p++) {
  183. *q++ = hexdig[(*p >> 4) & 0xf];
  184. *q++ = hexdig[*p & 0xf];
  185. *q++ = ':';
  186. }
  187. q[-1] = 0;
  188. #ifdef CHARSET_EBCDIC
  189. ebcdic2ascii(tmp, tmp, q - tmp - 1);
  190. #endif
  191. return tmp;
  192. }
  193. int openssl_strerror_r(int errnum, char *buf, size_t buflen)
  194. {
  195. #if defined(_MSC_VER) && _MSC_VER>=1400 && !defined(_WIN32_WCE)
  196. return !strerror_s(buf, buflen, errnum);
  197. #elif defined(_GNU_SOURCE)
  198. char *err;
  199. /*
  200. * GNU strerror_r may not actually set buf.
  201. * It can return a pointer to some (immutable) static string in which case
  202. * buf is left unused.
  203. */
  204. err = strerror_r(errnum, buf, buflen);
  205. if (err == NULL || buflen == 0)
  206. return 0;
  207. /*
  208. * If err is statically allocated, err != buf and we need to copy the data.
  209. * If err points somewhere inside buf, OPENSSL_strlcpy can handle this,
  210. * since src and dest are not annotated with __restrict and the function
  211. * reads src byte for byte and writes to dest.
  212. * If err == buf we do not have to copy anything.
  213. */
  214. if (err != buf)
  215. OPENSSL_strlcpy(buf, err, buflen);
  216. return 1;
  217. #elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
  218. (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
  219. /*
  220. * We can use "real" strerror_r. The OpenSSL version differs in that it
  221. * gives 1 on success and 0 on failure for consistency with other OpenSSL
  222. * functions. Real strerror_r does it the other way around
  223. */
  224. return !strerror_r(errnum, buf, buflen);
  225. #else
  226. char *err;
  227. /* Fall back to non-thread safe strerror()...its all we can do */
  228. if (buflen < 2)
  229. return 0;
  230. err = strerror(errnum);
  231. /* Can this ever happen? */
  232. if (err == NULL)
  233. return 0;
  234. OPENSSL_strlcpy(buf, err, buflen);
  235. return 1;
  236. #endif
  237. }