Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

236 řádky
6.4 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/buf.h>
  57. #include <string.h>
  58. #include <openssl/mem.h>
  59. #include <openssl/err.h>
  60. BUF_MEM *BUF_MEM_new(void) {
  61. BUF_MEM *ret;
  62. ret = OPENSSL_malloc(sizeof(BUF_MEM));
  63. if (ret == NULL) {
  64. OPENSSL_PUT_ERROR(BUF, ERR_R_MALLOC_FAILURE);
  65. return NULL;
  66. }
  67. memset(ret, 0, sizeof(BUF_MEM));
  68. return ret;
  69. }
  70. void BUF_MEM_free(BUF_MEM *buf) {
  71. if (buf == NULL) {
  72. return;
  73. }
  74. if (buf->data != NULL) {
  75. OPENSSL_cleanse(buf->data, buf->max);
  76. OPENSSL_free(buf->data);
  77. }
  78. OPENSSL_free(buf);
  79. }
  80. static size_t buf_mem_grow(BUF_MEM *buf, size_t len, char clean) {
  81. char *new_buf;
  82. size_t n, alloc_size;
  83. if (buf->length >= len) {
  84. buf->length = len;
  85. return len;
  86. }
  87. if (buf->max >= len) {
  88. memset(&buf->data[buf->length], 0, len - buf->length);
  89. buf->length = len;
  90. return len;
  91. }
  92. n = len + 3;
  93. if (n < len) {
  94. /* overflow */
  95. OPENSSL_PUT_ERROR(BUF, ERR_R_MALLOC_FAILURE);
  96. return 0;
  97. }
  98. n = n / 3;
  99. alloc_size = n * 4;
  100. if (alloc_size / 4 != n) {
  101. /* overflow */
  102. OPENSSL_PUT_ERROR(BUF, ERR_R_MALLOC_FAILURE);
  103. return 0;
  104. }
  105. if (buf->data == NULL) {
  106. new_buf = OPENSSL_malloc(alloc_size);
  107. } else {
  108. if (clean) {
  109. new_buf = OPENSSL_realloc_clean(buf->data, buf->max, alloc_size);
  110. } else {
  111. new_buf = OPENSSL_realloc(buf->data, alloc_size);
  112. }
  113. }
  114. if (new_buf == NULL) {
  115. OPENSSL_PUT_ERROR(BUF, ERR_R_MALLOC_FAILURE);
  116. len = 0;
  117. } else {
  118. buf->data = new_buf;
  119. buf->max = alloc_size;
  120. memset(&buf->data[buf->length], 0, len - buf->length);
  121. buf->length = len;
  122. }
  123. return len;
  124. }
  125. size_t BUF_MEM_grow(BUF_MEM *buf, size_t len) {
  126. return buf_mem_grow(buf, len, 0 /* don't clear old buffer contents. */);
  127. }
  128. size_t BUF_MEM_grow_clean(BUF_MEM *buf, size_t len) {
  129. return buf_mem_grow(buf, len, 1 /* clear old buffer contents. */);
  130. }
  131. char *BUF_strdup(const char *buf) {
  132. if (buf == NULL) {
  133. return NULL;
  134. }
  135. return BUF_strndup(buf, strlen(buf));
  136. }
  137. size_t BUF_strnlen(const char *str, size_t max_len) {
  138. size_t i;
  139. for (i = 0; i < max_len; i++) {
  140. if (str[i] == 0) {
  141. break;
  142. }
  143. }
  144. return i;
  145. }
  146. char *BUF_strndup(const char *buf, size_t size) {
  147. char *ret;
  148. size_t alloc_size;
  149. if (buf == NULL) {
  150. return NULL;
  151. }
  152. size = BUF_strnlen(buf, size);
  153. alloc_size = size + 1;
  154. if (alloc_size < size) {
  155. /* overflow */
  156. OPENSSL_PUT_ERROR(BUF, ERR_R_MALLOC_FAILURE);
  157. return NULL;
  158. }
  159. ret = OPENSSL_malloc(alloc_size);
  160. if (ret == NULL) {
  161. OPENSSL_PUT_ERROR(BUF, ERR_R_MALLOC_FAILURE);
  162. return NULL;
  163. }
  164. memcpy(ret, buf, size);
  165. ret[size] = '\0';
  166. return ret;
  167. }
  168. size_t BUF_strlcpy(char *dst, const char *src, size_t dst_size) {
  169. size_t l = 0;
  170. for (; dst_size > 1 && *src; dst_size--) {
  171. *dst++ = *src++;
  172. l++;
  173. }
  174. if (dst_size) {
  175. *dst = 0;
  176. }
  177. return l + strlen(src);
  178. }
  179. size_t BUF_strlcat(char *dst, const char *src, size_t dst_size) {
  180. size_t l = 0;
  181. for (; dst_size > 0 && *dst; dst_size--, dst++) {
  182. l++;
  183. }
  184. return l + BUF_strlcpy(dst, src, dst_size);
  185. }
  186. void *BUF_memdup(const void *data, size_t dst_size) {
  187. void *ret;
  188. if (data == NULL) {
  189. return NULL;
  190. }
  191. ret = OPENSSL_malloc(dst_size);
  192. if (ret == NULL) {
  193. OPENSSL_PUT_ERROR(BUF, ERR_R_MALLOC_FAILURE);
  194. return NULL;
  195. }
  196. memcpy(ret, data, dst_size);
  197. return ret;
  198. }