bufref.c 3.1 KB

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