iPXE
linebuf.h File Reference

Line buffering. More...

#include <stdint.h>
#include <stddef.h>

Go to the source code of this file.

Data Structures

struct  line_buffer
 A line buffer. More...

Functions

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

Detailed Description

Line buffering.

Definition in file linebuf.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ buffered_line()

char * buffered_line ( struct line_buffer * linebuf)
extern

Retrieve buffered-up line.

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

Definition at line 46 of file linebuf.c.

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

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().

◆ line_buffer()

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

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 92 of file linebuf.c.

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

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

Referenced by http_rx_linebuf(), and linebuf_consume_okx().

◆ empty_line_buffer()

void empty_line_buffer ( struct line_buffer * linebuf)
extern

Discard line buffer contents.

Parameters
linebufLine buffer

Definition at line 66 of file linebuf.c.

66 {
67
68 free ( linebuf->data );
69 linebuf->data = NULL;
70 linebuf->len = 0;
71 linebuf->consumed = 0;
72}
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55

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().