iPXE
Functions
linebuf.c File Reference

Line buffering. More...

#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ipxe/linebuf.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
char * buffered_line (struct line_buffer *linebuf)
 Retrieve buffered-up line. More...
 
void empty_line_buffer (struct line_buffer *linebuf)
 Discard line buffer contents. More...
 
int line_buffer (struct line_buffer *linebuf, const char *data, size_t len)
 Buffer up received data by lines. More...
 

Detailed Description

Line buffering.

Definition in file linebuf.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ buffered_line()

char* buffered_line ( struct line_buffer linebuf)

Retrieve buffered-up line.

Parameters
linebufLine buffer
Return values
lineBuffered line, or NULL if no line ready to read

Definition at line 45 of file linebuf.c.

45  {
46  char *line = &linebuf->data[ linebuf->len ];
47 
48  /* Fail unless we have a newly completed line to retrieve */
49  if ( ( linebuf->len == 0 ) || ( linebuf->consumed == 0 ) ||
50  ( *(--line) != '\0' ) )
51  return NULL;
52 
53  /* Identify start of line */
54  while ( ( line > linebuf->data ) && ( line[-1] != '\0' ) )
55  line--;
56 
57  return line;
58 }
size_t consumed
Most recently consumed length.
Definition: linebuf.h:22
char * data
Data buffer.
Definition: linebuf.h:18
size_t len
Length of buffered data.
Definition: linebuf.h:20
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References line_buffer::consumed, line_buffer::data, line_buffer::len, and NULL.

Referenced by http_rx_chunk_len(), http_rx_headers(), http_rx_trailers(), linebuf_consume_okx(), linebuf_empty_okx(), and linebuf_init_okx().

◆ empty_line_buffer()

void empty_line_buffer ( struct line_buffer linebuf)

Discard line buffer contents.

Parameters
linebufLine buffer

Definition at line 65 of file linebuf.c.

65  {
66 
67  free ( linebuf->data );
68  linebuf->data = NULL;
69  linebuf->len = 0;
70  linebuf->consumed = 0;
71 }
size_t consumed
Most recently consumed length.
Definition: linebuf.h:22
char * data
Data buffer.
Definition: linebuf.h:18
size_t len
Length of buffered data.
Definition: linebuf.h:20
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References line_buffer::consumed, line_buffer::data, free, line_buffer::len, and NULL.

Referenced by http_free(), http_rx_chunk_len(), http_rx_trailers(), http_tx_request(), and linebuf_empty_okx().

◆ line_buffer()

int line_buffer ( struct line_buffer linebuf,
const char *  data,
size_t  len 
)

Buffer up received data by lines.

Parameters
linebufLine buffer
dataNew data to add
lenLength of new data to add
Return values
lenConsumed length, or negative error number

After calling line_buffer(), use buffered_line() to determine whether or not a complete line is available. Carriage returns and newlines will have been stripped, and the line will be NUL-terminated. This buffered line is valid only until the next call to line_buffer() (or to empty_line_buffer()).

Note that line buffers use dynamically allocated storage; you should call empty_line_buffer() before freeing a struct line_buffer.

Definition at line 91 of file linebuf.c.

91  {
92  const char *eol;
93  size_t consume;
94  size_t new_len;
95  char *new_data;
96  char *lf;
97  char *cr;
98 
99  /* Search for line terminator */
100  if ( ( eol = memchr ( data, '\n', len ) ) ) {
101  consume = ( eol - data + 1 );
102  } else {
103  consume = len;
104  }
105 
106  /* Reject any embedded NULs within the data to be consumed */
107  if ( memchr ( data, '\0', consume ) )
108  return -EINVAL;
109 
110  /* Reallocate data buffer and copy in new data */
111  new_len = ( linebuf->len + consume );
112  new_data = realloc ( linebuf->data, ( new_len + 1 ) );
113  if ( ! new_data )
114  return -ENOMEM;
115  memcpy ( ( new_data + linebuf->len ), data, consume );
116  new_data[new_len] = '\0';
117  linebuf->data = new_data;
118  linebuf->len = new_len;
119 
120  /* If we have reached end of line, terminate the line */
121  if ( eol ) {
122 
123  /* Overwrite trailing LF (which must exist at this point) */
124  assert ( linebuf->len > 0 );
125  lf = &linebuf->data[ linebuf->len - 1 ];
126  assert ( *lf == '\n' );
127  *lf = '\0';
128 
129  /* Trim (and overwrite) trailing CR, if present */
130  if ( linebuf->len > 1 ) {
131  cr = ( lf - 1 );
132  if ( *cr == '\r' ) {
133  linebuf->len--;
134  *cr = '\0';
135  }
136  }
137  }
138 
139  /* Record consumed length */
140  linebuf->consumed = consume;
141 
142  return consume;
143 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
void * memchr(const void *src, int character, size_t len)
Find character within a memory region.
Definition: string.c:135
size_t consumed
Most recently consumed length.
Definition: linebuf.h:22
char * data
Data buffer.
Definition: linebuf.h:18
#define ENOMEM
Not enough space.
Definition: errno.h:534
void * memcpy(void *dest, const void *src, size_t len) __nonnull
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
size_t len
Length of buffered data.
Definition: linebuf.h:20
Definition: sis900.h:22
uint8_t data[48]
Additional event data.
Definition: ena.h:22
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition: malloc.c:521
uint32_t len
Length.
Definition: ena.h:14

References assert(), line_buffer::consumed, cr, line_buffer::data, data, EINVAL, ENOMEM, len, line_buffer::len, memchr(), memcpy(), and realloc().

Referenced by http_rx_linebuf(), and linebuf_consume_okx().