o_str.c 7.3 KB

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