buffers.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file buffers.h
  8. * \brief Header file for buffers.c.
  9. **/
  10. #ifndef TOR_BUFFERS_H
  11. #define TOR_BUFFERS_H
  12. #include "compat.h"
  13. #include "compat.h"
  14. #include "torint.h"
  15. #include "testsupport.h"
  16. typedef struct buf_t buf_t;
  17. struct tor_tls_t;
  18. struct tor_compress_state_t;
  19. struct ext_or_cmd_t;
  20. buf_t *buf_new(void);
  21. buf_t *buf_new_with_capacity(size_t size);
  22. size_t buf_get_default_chunk_size(const buf_t *buf);
  23. void buf_free(buf_t *buf);
  24. void buf_clear(buf_t *buf);
  25. buf_t *buf_copy(const buf_t *buf);
  26. MOCK_DECL(size_t, buf_datalen, (const buf_t *buf));
  27. size_t buf_allocation(const buf_t *buf);
  28. size_t buf_slack(const buf_t *buf);
  29. uint32_t buf_get_oldest_chunk_timestamp(const buf_t *buf, uint32_t now);
  30. size_t buf_get_total_allocation(void);
  31. int read_to_buf(tor_socket_t s, size_t at_most, buf_t *buf, int *reached_eof,
  32. int *socket_error);
  33. int read_to_buf_tls(struct tor_tls_t *tls, size_t at_most, buf_t *buf);
  34. int flush_buf(tor_socket_t s, buf_t *buf, size_t sz, size_t *buf_flushlen);
  35. int flush_buf_tls(struct tor_tls_t *tls, buf_t *buf, size_t sz,
  36. size_t *buf_flushlen);
  37. int write_to_buf(const char *string, size_t string_len, buf_t *buf);
  38. int write_to_buf_compress(buf_t *buf, struct tor_compress_state_t *state,
  39. const char *data, size_t data_len, int done);
  40. int move_buf_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen);
  41. void peek_from_buf(char *string, size_t string_len, const buf_t *buf);
  42. void buf_remove_from_front(buf_t *buf, size_t n);
  43. int fetch_from_buf(char *string, size_t string_len, buf_t *buf);
  44. int fetch_from_buf_line(buf_t *buf, char *data_out, size_t *data_len);
  45. #define PEEK_BUF_STARTSWITH_MAX 16
  46. int peek_buf_startswith(const buf_t *buf, const char *cmd);
  47. int buf_set_to_copy(buf_t **output,
  48. const buf_t *input);
  49. void assert_buf_ok(buf_t *buf);
  50. int buf_find_string_offset(const buf_t *buf, const char *s, size_t n);
  51. void buf_pullup(buf_t *buf, size_t bytes,
  52. const char **head_out, size_t *len_out);
  53. #ifdef BUFFERS_PRIVATE
  54. #ifdef TOR_UNIT_TESTS
  55. buf_t *buf_new_with_data(const char *cp, size_t sz);
  56. #endif
  57. ATTR_UNUSED STATIC size_t preferred_chunk_size(size_t target);
  58. #define DEBUG_CHUNK_ALLOC
  59. /** A single chunk on a buffer. */
  60. typedef struct chunk_t {
  61. struct chunk_t *next; /**< The next chunk on the buffer. */
  62. size_t datalen; /**< The number of bytes stored in this chunk */
  63. size_t memlen; /**< The number of usable bytes of storage in <b>mem</b>. */
  64. #ifdef DEBUG_CHUNK_ALLOC
  65. size_t DBG_alloc;
  66. #endif
  67. char *data; /**< A pointer to the first byte of data stored in <b>mem</b>. */
  68. uint32_t inserted_time; /**< Timestamp in truncated ms since epoch
  69. * when this chunk was inserted. */
  70. char mem[FLEXIBLE_ARRAY_MEMBER]; /**< The actual memory used for storage in
  71. * this chunk. */
  72. } chunk_t;
  73. /** Magic value for buf_t.magic, to catch pointer errors. */
  74. #define BUFFER_MAGIC 0xB0FFF312u
  75. /** A resizeable buffer, optimized for reading and writing. */
  76. struct buf_t {
  77. uint32_t magic; /**< Magic cookie for debugging: Must be set to
  78. * BUFFER_MAGIC. */
  79. size_t datalen; /**< How many bytes is this buffer holding right now? */
  80. size_t default_chunk_size; /**< Don't allocate any chunks smaller than
  81. * this for this buffer. */
  82. chunk_t *head; /**< First chunk in the list, or NULL for none. */
  83. chunk_t *tail; /**< Last chunk in the list, or NULL for none. */
  84. };
  85. #endif
  86. #endif