bufref.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2021 - 2022, Daniel Stenberg, <daniel@haxx.se>, 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 "curl_memory.h"
  28. #include "memdebug.h"
  29. #define SIGNATURE 0x5c48e9b2 /* Random pattern. */
  30. /*
  31. * Init a bufref struct.
  32. */
  33. void Curl_bufref_init(struct bufref *br)
  34. {
  35. DEBUGASSERT(br);
  36. br->dtor = NULL;
  37. br->ptr = NULL;
  38. br->len = 0;
  39. #ifdef DEBUGBUILD
  40. br->signature = SIGNATURE;
  41. #endif
  42. }
  43. /*
  44. * Free the buffer and re-init the necessary fields. It doesn't touch the
  45. * 'signature' field and thus this buffer reference can be reused.
  46. */
  47. void Curl_bufref_free(struct bufref *br)
  48. {
  49. DEBUGASSERT(br);
  50. DEBUGASSERT(br->signature == SIGNATURE);
  51. DEBUGASSERT(br->ptr || !br->len);
  52. if(br->ptr && br->dtor)
  53. br->dtor((void *) br->ptr);
  54. br->dtor = NULL;
  55. br->ptr = NULL;
  56. br->len = 0;
  57. }
  58. /*
  59. * Set the buffer reference to new values. The previously referenced buffer
  60. * is released before assignment.
  61. */
  62. void Curl_bufref_set(struct bufref *br, const void *ptr, size_t len,
  63. void (*dtor)(void *))
  64. {
  65. DEBUGASSERT(ptr || !len);
  66. DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH);
  67. Curl_bufref_free(br);
  68. br->ptr = (const unsigned char *) ptr;
  69. br->len = len;
  70. br->dtor = dtor;
  71. }
  72. /*
  73. * Get a pointer to the referenced buffer.
  74. */
  75. const unsigned char *Curl_bufref_ptr(const struct bufref *br)
  76. {
  77. DEBUGASSERT(br);
  78. DEBUGASSERT(br->signature == SIGNATURE);
  79. DEBUGASSERT(br->ptr || !br->len);
  80. return br->ptr;
  81. }
  82. /*
  83. * Get the length of the referenced buffer data.
  84. */
  85. size_t Curl_bufref_len(const struct bufref *br)
  86. {
  87. DEBUGASSERT(br);
  88. DEBUGASSERT(br->signature == SIGNATURE);
  89. DEBUGASSERT(br->ptr || !br->len);
  90. return br->len;
  91. }
  92. CURLcode Curl_bufref_memdup(struct bufref *br, const void *ptr, size_t len)
  93. {
  94. unsigned char *cpy = NULL;
  95. DEBUGASSERT(br);
  96. DEBUGASSERT(br->signature == SIGNATURE);
  97. DEBUGASSERT(br->ptr || !br->len);
  98. DEBUGASSERT(ptr || !len);
  99. DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH);
  100. if(ptr) {
  101. cpy = malloc(len + 1);
  102. if(!cpy)
  103. return CURLE_OUT_OF_MEMORY;
  104. if(len)
  105. memcpy(cpy, ptr, len);
  106. cpy[len] = '\0';
  107. }
  108. Curl_bufref_set(br, cpy, len, curl_free);
  109. return CURLE_OK;
  110. }