buffers.h 3.6 KB

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