buffer.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #ifndef _LSUP_BUFFER_H
  2. #define _LSUP_BUFFER_H
  3. #include "core.h"
  4. /** @brief General-purpose data buffer.
  5. *
  6. * The structure is transparently exposed so that the related API only defines
  7. * few basic helper methods. Other operations, such as appending, may be
  8. * performed by simply using the addr and size attributes.
  9. *
  10. * A buffer can be initialized once and reused multiple times, e.g. in a loop,
  11. * without being freed between iterations, by using #LSUP_buffer_init.
  12. */
  13. typedef struct LSUP_Buffer {
  14. /*@null@*/ void *addr;
  15. size_t size;
  16. } LSUP_Buffer;
  17. /** Initialize or reuse a buffer handle.
  18. *
  19. * The handle must have been created with #LSUP_buffer_new*().
  20. *
  21. * The data block is resized without being freed first. The handle must be
  22. * eventually freed with #LSUP_buffer_done() after use.
  23. *
  24. * @param buf[in] A buffer handle obtained with #LSUP_buffer_new or by manual
  25. * allocation.
  26. *
  27. * @param size[in] New size.
  28. *
  29. * @param data[in] If not NULL, data to replace the existing ones. The size
  30. * of the data to be copied is determined by the size parameter. If NULL, the
  31. * existing data are preserved as with a normal realloc().
  32. */
  33. LSUP_rc
  34. LSUP_buffer_init (LSUP_Buffer *buf, const size_t size, const void *data);
  35. /** @brief Create a new buffer and optionally populate it with data.
  36. *
  37. * To change the buffer size and/or data later call #LSUP_buffer_init.
  38. *
  39. * To copy a buffer just do buf2 = LSUP_buffer_new (buf1->size, buf1->addr);
  40. *
  41. * @param size[in] Length of the data.
  42. *
  43. * @param data[in] Optional data to initially populate the object with. If
  44. * NULL, the buffer data are garbage.
  45. *
  46. * @return LSUP_Buffer pointer. It must be freed with #LSUP_buffer_free. NULL
  47. * on error.
  48. */
  49. inline LSUP_Buffer *
  50. LSUP_buffer_new (const size_t size, const void *data)
  51. {
  52. LSUP_Buffer *buf = calloc (1, sizeof (*buf));
  53. if (!buf) return NULL;
  54. if (LSUP_buffer_init (buf, size, data) != LSUP_OK) {
  55. free (buf->addr);
  56. free (buf);
  57. return NULL;
  58. }
  59. return buf;
  60. }
  61. /** @brief Dummy buffer to be used with #LSUP_buffer_init.
  62. */
  63. #define BUF_DUMMY LSUP_buffer_new (0, NULL)
  64. /** @brief Free the content of a buffer.
  65. */
  66. void LSUP_buffer_done (LSUP_Buffer *buf);
  67. /** @brief Free a buffer.
  68. */
  69. void LSUP_buffer_free (LSUP_Buffer *buf);
  70. /** @brief Print a byte string of a given length in a human-readable format.
  71. *
  72. * The string is printed in Python style: printable characters are output
  73. * literally, and non-printable ones as hex sequences.
  74. */
  75. void LSUP_buffer_print (const LSUP_Buffer *buf);
  76. /** @brief Format a buffer into anb ASCII string.
  77. *
  78. * The string has non-printable characters escaped as "\xNN".
  79. *
  80. * @param buf[in] Buffer to convert.
  81. *
  82. * @return Formatted string. It must be freed with free().
  83. */
  84. char *
  85. LSUP_buffer_as_str (const LSUP_Buffer *buf);
  86. /** @brief Compare two buffers.
  87. *
  88. * The return value is the same as memcmp.
  89. */
  90. inline int LSUP_buffer_cmp (const LSUP_Buffer *buf1, const LSUP_Buffer *buf2)
  91. { return memcmp (buf1->addr, buf2->addr, max (buf1->size, buf2->size)); }
  92. /** @brief Return whether two buffers are equal.
  93. *
  94. * This may be faster than #LSUP_buffer_cmp() because it does a size comparison
  95. * first.
  96. */
  97. inline bool LSUP_buffer_eq (const LSUP_Buffer *buf1, const LSUP_Buffer *buf2)
  98. {
  99. if (buf1->size != buf2->size) return false;
  100. return (LSUP_buffer_cmp (buf1, buf2) == 0) ? true : false;
  101. }
  102. #endif