bufref.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include "urldata.h"
  26. #include "bufref.h"
  27. #include "strdup.h"
  28. #include "curl_memory.h"
  29. #include "memdebug.h"
  30. #ifdef DEBUGBUILD
  31. #define SIGNATURE 0x5c48e9b2 /* Random pattern. */
  32. #endif
  33. /*
  34. * Init a bufref struct.
  35. */
  36. void Curl_bufref_init(struct bufref *br)
  37. {
  38. DEBUGASSERT(br);
  39. br->dtor = NULL;
  40. br->ptr = NULL;
  41. br->len = 0;
  42. #ifdef DEBUGBUILD
  43. br->signature = SIGNATURE;
  44. #endif
  45. }
  46. /*
  47. * Free the buffer and re-init the necessary fields. It does not touch the
  48. * 'signature' field and thus this buffer reference can be reused.
  49. */
  50. void Curl_bufref_free(struct bufref *br)
  51. {
  52. DEBUGASSERT(br);
  53. DEBUGASSERT(br->signature == SIGNATURE);
  54. DEBUGASSERT(br->ptr || !br->len);
  55. if(br->ptr && br->dtor)
  56. br->dtor(CURL_UNCONST(br->ptr));
  57. br->dtor = NULL;
  58. br->ptr = NULL;
  59. br->len = 0;
  60. }
  61. /*
  62. * Set the buffer reference to new values. The previously referenced buffer
  63. * is released before assignment.
  64. */
  65. void Curl_bufref_set(struct bufref *br, const void *ptr, size_t len,
  66. void (*dtor)(void *))
  67. {
  68. DEBUGASSERT(ptr || !len);
  69. DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH);
  70. Curl_bufref_free(br);
  71. br->ptr = (const unsigned char *) ptr;
  72. br->len = len;
  73. br->dtor = dtor;
  74. }
  75. /*
  76. * Get a pointer to the referenced buffer.
  77. */
  78. const unsigned char *Curl_bufref_ptr(const struct bufref *br)
  79. {
  80. DEBUGASSERT(br);
  81. DEBUGASSERT(br->signature == SIGNATURE);
  82. DEBUGASSERT(br->ptr || !br->len);
  83. return br->ptr;
  84. }
  85. /*
  86. * Get the length of the referenced buffer data.
  87. */
  88. size_t Curl_bufref_len(const struct bufref *br)
  89. {
  90. DEBUGASSERT(br);
  91. DEBUGASSERT(br->signature == SIGNATURE);
  92. DEBUGASSERT(br->ptr || !br->len);
  93. return br->len;
  94. }
  95. CURLcode Curl_bufref_memdup(struct bufref *br, const void *ptr, size_t len)
  96. {
  97. unsigned char *cpy = NULL;
  98. DEBUGASSERT(br);
  99. DEBUGASSERT(br->signature == SIGNATURE);
  100. DEBUGASSERT(br->ptr || !br->len);
  101. DEBUGASSERT(ptr || !len);
  102. DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH);
  103. if(ptr) {
  104. cpy = Curl_memdup0(ptr, len);
  105. if(!cpy)
  106. return CURLE_OUT_OF_MEMORY;
  107. }
  108. Curl_bufref_set(br, cpy, len, curl_free);
  109. return CURLE_OK;
  110. }