iPXE
linebuf.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /**
00027  * @file
00028  *
00029  * Line buffering
00030  *
00031  */
00032 
00033 #include <stdint.h>
00034 #include <string.h>
00035 #include <stdlib.h>
00036 #include <errno.h>
00037 #include <ipxe/linebuf.h>
00038 
00039 /**
00040  * Retrieve buffered-up line
00041  *
00042  * @v linebuf           Line buffer
00043  * @ret line            Buffered line, or NULL if no line ready to read
00044  */
00045 char * buffered_line ( struct line_buffer *linebuf ) {
00046         char *line = &linebuf->data[ linebuf->len ];
00047 
00048         /* Fail unless we have a newly completed line to retrieve */
00049         if ( ( linebuf->len == 0 ) || ( linebuf->consumed == 0 ) ||
00050              ( *(--line) != '\0' ) )
00051                 return NULL;
00052 
00053         /* Identify start of line */
00054         while ( ( line > linebuf->data ) && ( line[-1] != '\0' ) )
00055                 line--;
00056 
00057         return line;
00058 }
00059 
00060 /**
00061  * Discard line buffer contents
00062  *
00063  * @v linebuf           Line buffer
00064  */
00065 void empty_line_buffer ( struct line_buffer *linebuf ) {
00066 
00067         free ( linebuf->data );
00068         linebuf->data = NULL;
00069         linebuf->len = 0;
00070         linebuf->consumed = 0;
00071 }
00072 
00073 /**
00074  * Buffer up received data by lines
00075  *
00076  * @v linebuf                   Line buffer
00077  * @v data                      New data to add
00078  * @v len                       Length of new data to add
00079  * @ret len                     Consumed length, or negative error number
00080  *
00081  * After calling line_buffer(), use buffered_line() to determine
00082  * whether or not a complete line is available.  Carriage returns and
00083  * newlines will have been stripped, and the line will be
00084  * NUL-terminated.  This buffered line is valid only until the next
00085  * call to line_buffer() (or to empty_line_buffer()).
00086  *
00087  * Note that line buffers use dynamically allocated storage; you
00088  * should call empty_line_buffer() before freeing a @c struct @c
00089  * line_buffer.
00090  */
00091 int line_buffer ( struct line_buffer *linebuf, const char *data, size_t len ) {
00092         const char *eol;
00093         size_t consume;
00094         size_t new_len;
00095         char *new_data;
00096         char *lf;
00097         char *cr;
00098 
00099         /* Search for line terminator */
00100         if ( ( eol = memchr ( data, '\n', len ) ) ) {
00101                 consume = ( eol - data + 1 );
00102         } else {
00103                 consume = len;
00104         }
00105 
00106         /* Reject any embedded NULs within the data to be consumed */
00107         if ( memchr ( data, '\0', consume ) )
00108                 return -EINVAL;
00109 
00110         /* Reallocate data buffer and copy in new data */
00111         new_len = ( linebuf->len + consume );
00112         new_data = realloc ( linebuf->data, ( new_len + 1 ) );
00113         if ( ! new_data )
00114                 return -ENOMEM;
00115         memcpy ( ( new_data + linebuf->len ), data, consume );
00116         new_data[new_len] = '\0';
00117         linebuf->data = new_data;
00118         linebuf->len = new_len;
00119 
00120         /* If we have reached end of line, terminate the line */
00121         if ( eol ) {
00122 
00123                 /* Overwrite trailing LF (which must exist at this point) */
00124                 assert ( linebuf->len > 0 );
00125                 lf = &linebuf->data[ linebuf->len - 1 ];
00126                 assert ( *lf == '\n' );
00127                 *lf = '\0';
00128 
00129                 /* Trim (and overwrite) trailing CR, if present */
00130                 if ( linebuf->len > 1 ) {
00131                         cr = ( lf - 1 );
00132                         if ( *cr == '\r' ) {
00133                                 linebuf->len--;
00134                                 *cr = '\0';
00135                         }
00136                 }
00137         }
00138 
00139         /* Record consumed length */
00140         linebuf->consumed = consume;
00141 
00142         return consume;
00143 }