Damn it; my lovely resilient scheme falls down when you have a protocol
that switches from line-oriented to byte-oriented partway through, such as HTTP.
This commit is contained in:
parent
ad22cccc09
commit
83b7933f8a
@ -30,33 +30,13 @@
|
|||||||
#include <gpxe/linebuf.h>
|
#include <gpxe/linebuf.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Line terminators
|
* Retrieve buffered-up line
|
||||||
*
|
*
|
||||||
* These values are used in the @c skip_terminators bitmask.
|
* @v linebuf Line buffer
|
||||||
|
* @ret line Buffered line, or NULL if no line ready to read
|
||||||
*/
|
*/
|
||||||
enum line_terminators {
|
char * buffered_line ( struct line_buffer *linebuf ) {
|
||||||
TERM_CR = 1,
|
return ( linebuf->ready ? linebuf->data : NULL );
|
||||||
TERM_NL = 2,
|
|
||||||
TERM_NUL = 4,
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get terminator ID corresponding to character
|
|
||||||
*
|
|
||||||
* @v character Character
|
|
||||||
* @ret terminator_id Terminator ID, or -1
|
|
||||||
*/
|
|
||||||
static int terminator_id ( unsigned int character ) {
|
|
||||||
switch ( character ) {
|
|
||||||
case '\r':
|
|
||||||
return TERM_CR;
|
|
||||||
case '\n':
|
|
||||||
return TERM_NL;
|
|
||||||
case '\0':
|
|
||||||
return TERM_NUL;
|
|
||||||
default:
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -68,6 +48,7 @@ void empty_line_buffer ( struct line_buffer *linebuf ) {
|
|||||||
free ( linebuf->data );
|
free ( linebuf->data );
|
||||||
linebuf->data = NULL;
|
linebuf->data = NULL;
|
||||||
linebuf->len = 0;
|
linebuf->len = 0;
|
||||||
|
linebuf->ready = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -76,61 +57,60 @@ void empty_line_buffer ( struct line_buffer *linebuf ) {
|
|||||||
* @v linebuf Line buffer
|
* @v linebuf Line buffer
|
||||||
* @v data New data to add
|
* @v data New data to add
|
||||||
* @v len Length of new data to add
|
* @v len Length of new data to add
|
||||||
* @ret buffered Amount of data consumed and added to the buffer
|
* @ret rc Return status code
|
||||||
* @ret <0 Out of memory
|
|
||||||
*
|
*
|
||||||
* If line_buffer() does not consume the entirety of the new data
|
* If line_buffer() returns >0, then an end of line has been reached
|
||||||
* (i.e. if @c buffered is not equal to @c len), then an end of line
|
* and the buffered-up line can be obtained from buffered_line().
|
||||||
* has been reached and the buffered-up line can be obtained from
|
* Carriage returns and newlines will have been stripped, and the line
|
||||||
* buffered_line(). Carriage returns and newlines will have been
|
* will be NUL-terminated. This buffered line is valid only until the
|
||||||
* stripped, and the line will be NUL-terminated. This buffered line
|
* next call to line_buffer() (or to empty_line_buffer()).
|
||||||
* is valid only until the next call to line_buffer() (or to
|
*
|
||||||
* empty_line_buffer()).
|
* @c data and @c len will be updated to reflect the data consumed by
|
||||||
|
* line_buffer().
|
||||||
*
|
*
|
||||||
* Note that line buffers use dynamically allocated storage; you
|
* Note that line buffers use dynamically allocated storage; you
|
||||||
* should call empty_line_buffer() before freeing a @c struct @c
|
* should call empty_line_buffer() before freeing a @c struct @c
|
||||||
* line_buffer.
|
* line_buffer.
|
||||||
*/
|
*/
|
||||||
int line_buffer ( struct line_buffer *linebuf, const char *data, size_t len ) {
|
int line_buffer ( struct line_buffer *linebuf,
|
||||||
size_t consume = 0;
|
const char **data, size_t *len ) {
|
||||||
size_t copy = 0;
|
const char *eol;
|
||||||
|
size_t consume;
|
||||||
size_t new_len;
|
size_t new_len;
|
||||||
char *new_data;
|
char *new_data;
|
||||||
int terminator;
|
|
||||||
|
|
||||||
/* First, handle the termination of the previous line */
|
/* Free any completed line from previous iteration */
|
||||||
if ( linebuf->skip_terminators ) {
|
if ( linebuf->ready )
|
||||||
/* Free buffered string */
|
|
||||||
empty_line_buffer ( linebuf );
|
empty_line_buffer ( linebuf );
|
||||||
/* Skip over any terminators from the end of a previous line */
|
|
||||||
for ( ; consume < len ; consume++ ) {
|
|
||||||
terminator = terminator_id ( data[consume] );
|
|
||||||
if ( ( terminator < 0 ) ||
|
|
||||||
! ( linebuf->skip_terminators & terminator ) ) {
|
|
||||||
linebuf->skip_terminators = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
linebuf->skip_terminators &= ~terminator;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Scan up to the next terminator, if any */
|
/* Search for line terminator */
|
||||||
for ( ; consume < len ; consume++, copy++ ) {
|
if ( ( eol = memchr ( *data, '\n', *len ) ) ) {
|
||||||
if ( terminator_id ( data[consume] ) >= 0 ) {
|
consume = ( eol - *data + 1 );
|
||||||
linebuf->skip_terminators = -1U;
|
} else {
|
||||||
break;
|
consume = *len;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Reallocate data buffer and copy in new data */
|
/* Reallocate data buffer and copy in new data */
|
||||||
new_len = ( linebuf->len + copy );
|
new_len = ( linebuf->len + consume );
|
||||||
new_data = realloc ( linebuf->data, ( new_len + 1 ) );
|
new_data = realloc ( linebuf->data, ( new_len + 1 ) );
|
||||||
if ( ! new_data )
|
if ( ! new_data )
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
memcpy ( ( new_data + linebuf->len ), ( data + consume - copy ),
|
memcpy ( ( new_data + linebuf->len ), *data, consume );
|
||||||
copy );
|
|
||||||
new_data[new_len] = '\0';
|
new_data[new_len] = '\0';
|
||||||
linebuf->data = new_data;
|
linebuf->data = new_data;
|
||||||
linebuf->len = new_len;
|
linebuf->len = new_len;
|
||||||
return consume;
|
|
||||||
|
/* Update data and len */
|
||||||
|
*data += consume;
|
||||||
|
*len -= consume;
|
||||||
|
|
||||||
|
/* If we have reached end of line, trim the line and mark as ready */
|
||||||
|
if ( eol ) {
|
||||||
|
linebuf->data[--linebuf->len] = '\0'; /* trim NL */
|
||||||
|
if ( linebuf->data[linebuf->len - 1] == '\r' )
|
||||||
|
linebuf->data[--linebuf->len] = '\0'; /* trim CR */
|
||||||
|
linebuf->ready = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -12,26 +12,17 @@
|
|||||||
|
|
||||||
/** A line buffer */
|
/** A line buffer */
|
||||||
struct line_buffer {
|
struct line_buffer {
|
||||||
/** Current data in the buffer */
|
/** Current string in the buffer */
|
||||||
char *data;
|
char *data;
|
||||||
/** Length of current data */
|
/** Length of current string, excluding the terminating NUL */
|
||||||
size_t len;
|
size_t len;
|
||||||
/** Bitmask of terminating characters to skip over */
|
/** String is ready to read */
|
||||||
unsigned int skip_terminators;
|
int ready;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
extern char * buffered_line ( struct line_buffer *linebuf );
|
||||||
* Retrieve buffered-up line
|
extern int line_buffer ( struct line_buffer *linebuf,
|
||||||
*
|
const char **data, size_t *len );
|
||||||
* @v linebuf Line buffer
|
|
||||||
* @ret line Buffered line, or NULL if no line present
|
|
||||||
*/
|
|
||||||
static inline char * buffered_line ( struct line_buffer *linebuf ) {
|
|
||||||
return linebuf->data;
|
|
||||||
}
|
|
||||||
|
|
||||||
extern int line_buffer ( struct line_buffer *linebuf, const char *data,
|
|
||||||
size_t len );
|
|
||||||
extern void empty_line_buffer ( struct line_buffer *linebuf );
|
extern void empty_line_buffer ( struct line_buffer *linebuf );
|
||||||
|
|
||||||
#endif /* _GPXE_LINEBUF_H */
|
#endif /* _GPXE_LINEBUF_H */
|
||||||
|
@ -5,8 +5,8 @@
|
|||||||
|
|
||||||
static const char data1[] =
|
static const char data1[] =
|
||||||
"Hello world\r\n"
|
"Hello world\r\n"
|
||||||
"This is a particularly mean set of lines\n"
|
"This is a reasonably nice set of lines\n"
|
||||||
"with a mixture of terminators\r\r\n"
|
"with not many different terminators\r\n\r\n"
|
||||||
"There should be exactly one blank line above\n"
|
"There should be exactly one blank line above\n"
|
||||||
"and this line should never appear at all since it has no terminator";
|
"and this line should never appear at all since it has no terminator";
|
||||||
|
|
||||||
@ -14,13 +14,18 @@ void linebuf_test ( void ) {
|
|||||||
struct line_buffer linebuf;
|
struct line_buffer linebuf;
|
||||||
const char *data = data1;
|
const char *data = data1;
|
||||||
size_t len = ( sizeof ( data1 ) - 1 /* be mean; strip the NUL */ );
|
size_t len = ( sizeof ( data1 ) - 1 /* be mean; strip the NUL */ );
|
||||||
size_t buffered;
|
char *line;
|
||||||
|
int rc;
|
||||||
|
|
||||||
memset ( &linebuf, 0, sizeof ( linebuf ) );
|
memset ( &linebuf, 0, sizeof ( linebuf ) );
|
||||||
while ( ( buffered = line_buffer ( &linebuf, data, len ) ) != len ) {
|
while ( len ) {
|
||||||
printf ( "\"%s\"\n", buffered_line ( &linebuf ) );
|
if ( ( rc = line_buffer ( &linebuf, &data, &len ) ) != 0 ) {
|
||||||
data += buffered;
|
printf ( "line_buffer() failed: %s\n",
|
||||||
len -= buffered;
|
strerror ( rc ) );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if ( ( line = buffered_line ( &linebuf ) ) )
|
||||||
|
printf ( "\"%s\"\n", line );
|
||||||
}
|
}
|
||||||
|
|
||||||
empty_line_buffer ( &linebuf );
|
empty_line_buffer ( &linebuf );
|
||||||
|
Reference in New Issue
Block a user