iPXE
linebuf.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /**
27  * @file
28  *
29  * Line buffering
30  *
31  */
32 
33 #include <stdint.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <errno.h>
37 #include <ipxe/linebuf.h>
38 
39 /**
40  * Retrieve buffered-up line
41  *
42  * @v linebuf Line buffer
43  * @ret line Buffered line, or NULL if no line ready to read
44  */
45 char * buffered_line ( struct line_buffer *linebuf ) {
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 }
59 
60 /**
61  * Discard line buffer contents
62  *
63  * @v linebuf Line buffer
64  */
65 void empty_line_buffer ( struct line_buffer *linebuf ) {
66 
67  free ( linebuf->data );
68  linebuf->data = NULL;
69  linebuf->len = 0;
70  linebuf->consumed = 0;
71 }
72 
73 /**
74  * Buffer up received data by lines
75  *
76  * @v linebuf Line buffer
77  * @v data New data to add
78  * @v len Length of new data to add
79  * @ret len Consumed length, or negative error number
80  *
81  * After calling line_buffer(), use buffered_line() to determine
82  * whether or not a complete line is available. Carriage returns and
83  * newlines will have been stripped, and the line will be
84  * NUL-terminated. This buffered line is valid only until the next
85  * call to line_buffer() (or to empty_line_buffer()).
86  *
87  * Note that line buffers use dynamically allocated storage; you
88  * should call empty_line_buffer() before freeing a @c struct @c
89  * line_buffer.
90  */
91 int line_buffer ( struct line_buffer *linebuf, const char *data, size_t len ) {
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
Error codes.
void * memchr(const void *src, int character, size_t len)
Find character within a memory region.
Definition: string.c:119
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)
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
void empty_line_buffer(struct line_buffer *linebuf)
Discard line buffer contents.
Definition: linebuf.c:65
size_t len
Length of buffered data.
Definition: linebuf.h:20
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
char * buffered_line(struct line_buffer *linebuf)
Retrieve buffered-up line.
Definition: linebuf.c:45
Definition: sis900.h:22
uint32_t len
Length.
Definition: ena.h:14
A line buffer.
Definition: linebuf.h:16
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition: malloc.c:521
int line_buffer(struct line_buffer *linebuf, const char *data, size_t len)
Buffer up received data by lines.
Definition: linebuf.c:91
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
Line buffering.
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
String functions.