tor  master
Macros | Typedefs | Functions
buffers.h File Reference

Header file for buffers.c. More...

#include "compat.h"
#include "torint.h"
#include "testsupport.h"
Include dependency graph for buffers.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define buf_free(b)   FREE_AND_NULL(buf_t, buf_free_, (b))
 
#define PEEK_BUF_STARTSWITH_MAX   16
 

Typedefs

typedef struct buf_t buf_t
 

Functions

buf_t * buf_new (void)
 
buf_t * buf_new_with_capacity (size_t size)
 
size_t buf_get_default_chunk_size (const buf_t *buf)
 
void buf_free_ (buf_t *buf)
 
void buf_clear (buf_t *buf)
 
buf_t * buf_copy (const buf_t *buf)
 
 MOCK_DECL (size_t, buf_datalen,(const buf_t *buf))
 
size_t buf_allocation (const buf_t *buf)
 
size_t buf_slack (const buf_t *buf)
 
uint32_t buf_get_oldest_chunk_timestamp (const buf_t *buf, uint32_t now)
 
size_t buf_get_total_allocation (void)
 
int buf_read_from_socket (buf_t *buf, tor_socket_t s, size_t at_most, int *reached_eof, int *socket_error)
 
int buf_flush_to_socket (buf_t *buf, tor_socket_t s, size_t sz, size_t *buf_flushlen)
 
int buf_add (buf_t *buf, const char *string, size_t string_len)
 
void buf_add_string (buf_t *buf, const char *string)
 
void buf_add_printf (buf_t *buf, const char *format,...) CHECK_PRINTF(2
 
void void buf_add_vprintf (buf_t *buf, const char *format, va_list args) CHECK_PRINTF(2
 
void void int buf_add_compress (buf_t *buf, struct tor_compress_state_t *state, const char *data, size_t data_len, int done)
 
int buf_move_to_buf (buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen)
 
void buf_move_all (buf_t *buf_out, buf_t *buf_in)
 
void buf_peek (const buf_t *buf, char *string, size_t string_len)
 
void buf_drain (buf_t *buf, size_t n)
 
int buf_get_bytes (buf_t *buf, char *string, size_t string_len)
 
int buf_get_line (buf_t *buf, char *data_out, size_t *data_len)
 
int buf_peek_startswith (const buf_t *buf, const char *cmd)
 
int buf_set_to_copy (buf_t **output, const buf_t *input)
 
void buf_assert_ok (buf_t *buf)
 
int buf_find_string_offset (const buf_t *buf, const char *s, size_t n)
 
void buf_pullup (buf_t *buf, size_t bytes, const char **head_out, size_t *len_out)
 
char * buf_extract (buf_t *buf, size_t *sz_out)
 

Detailed Description

Header file for buffers.c.

Function Documentation

◆ buf_add()

int buf_add ( buf_t *  buf,
const char *  string,
size_t  string_len 
)

Append string_len bytes from string to the end of buf.

Return the new length of the buffer on success, -1 on failure.

Here is the caller graph for this function:

◆ buf_add_compress()

void void int buf_add_compress ( buf_t *  buf,
tor_compress_state_t state,
const char *  data,
size_t  data_len,
const int  done 
)

Compress or uncompress the data_len bytes in data using the compression state state, appending the result to buf. If done is true, flush the data in the state and finish the compression/uncompression. Return -1 on failure, 0 on success.

Here is the call graph for this function:

◆ buf_add_string()

void buf_add_string ( buf_t *  buf,
const char *  string 
)

Add a nul-terminated string to buf, not including the terminating NUL.

Here is the call graph for this function:

◆ buf_allocation()

size_t buf_allocation ( const buf_t *  buf)

Return the total length of all chunks used in buf.

◆ buf_assert_ok()

void buf_assert_ok ( buf_t *  buf)

Log an error and exit if buf is corrupted.

◆ buf_clear()

void buf_clear ( buf_t *  buf)

Remove all data from buf.

Here is the caller graph for this function:

◆ buf_copy()

buf_t* buf_copy ( const buf_t *  buf)

Return a new copy of buf

Here is the call graph for this function:

◆ buf_drain()

void buf_drain ( buf_t *  buf,
size_t  n 
)

Remove the first n bytes from buf.

◆ buf_extract()

char* buf_extract ( buf_t *  buf,
size_t *  sz_out 
)

Return a heap-allocated string containing the contents of buf, plus a NUL byte. If sz_out is provided, set *sz_out to the length of the returned string, not including the terminating NUL.

◆ buf_find_string_offset()

int buf_find_string_offset ( const buf_t *  buf,
const char *  s,
size_t  n 
)

Return the first position in buf at which the n-character string s occurs, or -1 if it does not occur.

◆ buf_flush_to_socket()

int buf_flush_to_socket ( buf_t *  buf,
tor_socket_t  s,
size_t  sz,
size_t *  buf_flushlen 
)

Write data from buf to the socket s. Write at most sz bytes, decrement *buf_flushlen by the number of bytes actually written, and remove the written bytes from the buffer. Return the number of bytes written on success, -1 on failure. Return 0 if write() would block.

◆ buf_free_()

void buf_free_ ( buf_t *  buf)

Release storage held by buf.

Here is the call graph for this function:

◆ buf_get_bytes()

int buf_get_bytes ( buf_t *  buf,
char *  string,
size_t  string_len 
)

Remove string_len bytes from the front of buf, and store them into string. Return the new buffer size. string_len must be <= the number of bytes on the buffer.

Here is the caller graph for this function:

◆ buf_get_line()

int buf_get_line ( buf_t *  buf,
char *  data_out,
size_t *  data_len 
)

Try to read a single LF-terminated line from buf, and write it (including the LF), NUL-terminated, into the *data_len byte buffer at data_out. Set *data_len to the number of bytes in the line, not counting the terminating NUL. Return 1 if we read a whole line, return 0 if we don't have a whole line yet, and return -1 if the line length exceeds *data_len.

Here is the caller graph for this function:

◆ buf_get_oldest_chunk_timestamp()

uint32_t buf_get_oldest_chunk_timestamp ( const buf_t *  buf,
uint32_t  now 
)

Return the age of the oldest chunk in the buffer buf, in timestamp units. Requires the current monotonic timestamp as its input now.

◆ buf_move_all()

void buf_move_all ( buf_t *  buf_out,
buf_t *  buf_in 
)

Moves all data from buf_in to buf_out, without copying.

◆ buf_move_to_buf()

int buf_move_to_buf ( buf_t *  buf_out,
buf_t *  buf_in,
size_t *  buf_flushlen 
)

Move up to *buf_flushlen bytes from buf_in to buf_out, and modify *buf_flushlen appropriately. Return the number of bytes actually copied.

◆ buf_new()

buf_t* buf_new ( void  )

Allocate and return a new buffer with default capacity.

Here is the caller graph for this function:

◆ buf_new_with_capacity()

buf_t* buf_new_with_capacity ( size_t  size)

Create and return a new buf with default chunk capacity size.

Here is the call graph for this function:

◆ buf_peek()

void buf_peek ( const buf_t *  buf,
char *  string,
size_t  string_len 
)

Helper: copy the first string_len bytes from buf onto string.

◆ buf_peek_startswith()

int buf_peek_startswith ( const buf_t *  buf,
const char *  cmd 
)

Return 1 iff buf starts with cmd. cmd must be a null terminated string, of no more than PEEK_BUF_STARTSWITH_MAX bytes.

◆ buf_pullup()

void buf_pullup ( buf_t *  buf,
size_t  bytes,
const char **  head_out,
size_t *  len_out 
)

Collapse data from the first N chunks from buf into buf->head, growing it as necessary, until buf->head has the first bytes bytes of data from the buffer, or until buf->head has all the data in buf.

Set *head_out to point to the first byte of available data, and *len_out to the number of bytes of data available at *head_out. Note that *len_out may be more or less than bytes, depending on the number of bytes available.

◆ buf_read_from_socket()

int buf_read_from_socket ( buf_t *  buf,
tor_socket_t  s,
size_t  at_most,
int *  reached_eof,
int *  socket_error 
)

Read from socket s, writing onto end of buf. Read at most at_most bytes, growing the buffer as necessary. If recv() returns 0 (because of EOF), set *reached_eof to 1 and return 0. Return -1 on error; else return the number of bytes read.

◆ buf_set_to_copy()

int buf_set_to_copy ( buf_t **  output,
const buf_t *  input 
)

Set *output to contain a copy of the data in *input

◆ buf_slack()

size_t buf_slack ( const buf_t *  buf)

Return the number of bytes that can be added to buf without performing any additional allocation.