bufref.c 3.1 KB

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