Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | * |
| 6 | * Copyright (c) 1992 Simon Glass |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 9 | #ifndef _membuf_H |
| 10 | #define _membuf_H |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 11 | |
Simon Glass | 72aacda | 2025-03-18 16:20:45 +0100 | [diff] [blame] | 12 | #include <stdbool.h> |
| 13 | |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 14 | /** |
Simon Glass | f7e87f9 | 2025-03-18 16:20:44 +0100 | [diff] [blame] | 15 | * @struct membuf: holds the state of a membuff - it is used for input and |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 16 | * output buffers. The buffer extends from @start to (@start + @size - 1). |
| 17 | * Data in the buffer extends from @tail to @head: it is written in at |
| 18 | * @head and read out from @tail. The membuff is empty when @head == @tail |
| 19 | * and full when adding another character would make @head == @tail. We |
| 20 | * therefore waste one character in the membuff to avoid having an extra flag |
| 21 | * to determine whether (when @head == @tail) the membuff is empty or full. |
| 22 | * |
| 23 | * xxxxxx data |
| 24 | * ...... empty |
| 25 | * |
| 26 | * .............xxxxxxxxxxxxxxxx......................... |
| 27 | * ^ ^ |
Simon Glass | fb8895b | 2025-03-18 16:20:48 +0100 | [diff] [blame] | 28 | * ^start tail head ^end |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 29 | * |
| 30 | * xxxxxxxxxxxxx................xxxxxxxxxxxxxxxxxxxxxxxxx |
| 31 | * ^ ^ |
| 32 | * head tail |
| 33 | */ |
Simon Glass | f7e87f9 | 2025-03-18 16:20:44 +0100 | [diff] [blame] | 34 | struct membuf { |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 35 | char *start; /** the start of the buffer */ |
| 36 | char *end; /** the end of the buffer (start + length) */ |
| 37 | char *head; /** current buffer head */ |
| 38 | char *tail; /** current buffer tail */ |
| 39 | }; |
| 40 | |
| 41 | /** |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 42 | * membuf_purge() - reset a membuff to the empty state |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 43 | * |
| 44 | * Initialise head and tail pointers so that the membuff becomes empty. |
| 45 | * |
| 46 | * @mb: membuff to purge |
| 47 | */ |
Simon Glass | f7e87f9 | 2025-03-18 16:20:44 +0100 | [diff] [blame] | 48 | void membuf_purge(struct membuf *mb); |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 51 | * membuf_putraw() - find out where bytes can be written |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 52 | * |
| 53 | * Work out where in the membuff some data could be written. Return a pointer |
| 54 | * to the address and the number of bytes which can be written there. If |
| 55 | * @update is true, the caller must then write the data immediately, since |
| 56 | * the membuff is updated as if the write has been done, |
| 57 | * |
| 58 | * Note that because the spare space in a membuff may not be contiguous, this |
| 59 | * function may not return @maxlen even if there is enough space in the |
| 60 | * membuff. However, by calling this function twice (with @update == true), |
| 61 | * you will get access to all the spare space. |
| 62 | * |
| 63 | * @mb: membuff to adjust |
| 64 | * @maxlen: the number of bytes we want to write |
| 65 | * @update: true to update the membuff as if the write happened, false to not |
| 66 | * @data: the address data can be written to |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 67 | * Return: number of bytes which can be written |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 68 | */ |
Simon Glass | f7e87f9 | 2025-03-18 16:20:44 +0100 | [diff] [blame] | 69 | int membuf_putraw(struct membuf *mb, int maxlen, bool update, char **data); |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 72 | * membuf_getraw() - find and return a pointer to available bytes |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 73 | * |
| 74 | * Returns a pointer to any valid input data in the given membuff and |
| 75 | * optionally marks it as read. Note that not all input data may not be |
| 76 | * returned, since data is not necessarily contiguous in the membuff. However, |
| 77 | * if you call this function twice (with @update == true) you are guaranteed |
| 78 | * to get all available data, in at most two installments. |
| 79 | * |
| 80 | * @mb: membuff to adjust |
| 81 | * @maxlen: maximum number of bytes to get |
| 82 | * @update: true to update the membuff as if the bytes have been read (use |
| 83 | * false to check bytes without reading them) |
| 84 | * @data: returns address of data in input membuff |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 85 | * Return: the number of bytes available at *@data |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 86 | */ |
Simon Glass | f7e87f9 | 2025-03-18 16:20:44 +0100 | [diff] [blame] | 87 | int membuf_getraw(struct membuf *mb, int maxlen, bool update, char **data); |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 88 | |
| 89 | /** |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 90 | * membuf_putbyte() - Writes a byte to a membuff |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 91 | * |
| 92 | * @mb: membuff to adjust |
| 93 | * @ch: byte to write |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 94 | * Return: true on success, false if membuff is full |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 95 | */ |
Simon Glass | f7e87f9 | 2025-03-18 16:20:44 +0100 | [diff] [blame] | 96 | bool membuf_putbyte(struct membuf *mb, int ch); |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * @mb: membuff to adjust |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 100 | * membuf_getbyte() - Read a byte from the membuff |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 101 | * Return: the byte read, or -1 if the membuff is empty |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 102 | */ |
Simon Glass | f7e87f9 | 2025-03-18 16:20:44 +0100 | [diff] [blame] | 103 | int membuf_getbyte(struct membuf *mb); |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 104 | |
| 105 | /** |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 106 | * membuf_peekbyte() - check the next available byte |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 107 | * |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 108 | * Return the next byte which membuf_getbyte() would return, without |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 109 | * removing it from the membuff. |
| 110 | * |
| 111 | * @mb: membuff to adjust |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 112 | * Return: the byte peeked, or -1 if the membuff is empty |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 113 | */ |
Simon Glass | f7e87f9 | 2025-03-18 16:20:44 +0100 | [diff] [blame] | 114 | int membuf_peekbyte(struct membuf *mb); |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 115 | |
| 116 | /** |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 117 | * membuf_get() - get data from a membuff |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 118 | * |
| 119 | * Copies any available data (up to @maxlen bytes) to @buff and removes it |
| 120 | * from the membuff. |
| 121 | * |
| 122 | * @mb: membuff to adjust |
| 123 | * @Buff: address of membuff to transfer bytes to |
| 124 | * @maxlen: maximum number of bytes to read |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 125 | * Return: the number of bytes read |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 126 | */ |
Simon Glass | f7e87f9 | 2025-03-18 16:20:44 +0100 | [diff] [blame] | 127 | int membuf_get(struct membuf *mb, char *buff, int maxlen); |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 128 | |
| 129 | /** |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 130 | * membuf_put() - write data to a membuff |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 131 | * |
| 132 | * Writes some data to a membuff. Returns the number of bytes added. If this |
| 133 | * is less than @lnehgt, then the membuff got full |
| 134 | * |
| 135 | * @mb: membuff to adjust |
| 136 | * @data: the data to write |
| 137 | * @length: number of bytes to write from 'data' |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 138 | * Return: the number of bytes added |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 139 | */ |
Simon Glass | f7e87f9 | 2025-03-18 16:20:44 +0100 | [diff] [blame] | 140 | int membuf_put(struct membuf *mb, const char *buff, int length); |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 141 | |
| 142 | /** |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 143 | * membuf_isempty() - check if a membuff is empty |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 144 | * |
| 145 | * @mb: membuff to check |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 146 | * Return: true if empty, else false |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 147 | */ |
Simon Glass | f7e87f9 | 2025-03-18 16:20:44 +0100 | [diff] [blame] | 148 | bool membuf_isempty(struct membuf *mb); |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 149 | |
| 150 | /** |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 151 | * membuf_avail() - check available data in a membuff |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 152 | * |
| 153 | * @mb: membuff to check |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 154 | * Return: number of bytes of data available |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 155 | */ |
Simon Glass | f7e87f9 | 2025-03-18 16:20:44 +0100 | [diff] [blame] | 156 | int membuf_avail(struct membuf *mb); |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 157 | |
| 158 | /** |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 159 | * membuf_size() - get the size of a membuff |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 160 | * |
| 161 | * Note that a membuff can only old data up to one byte less than its size. |
| 162 | * |
| 163 | * @mb: membuff to check |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 164 | * Return: total size |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 165 | */ |
Simon Glass | f7e87f9 | 2025-03-18 16:20:44 +0100 | [diff] [blame] | 166 | int membuf_size(struct membuf *mb); |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 167 | |
| 168 | /** |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 169 | * membuf_makecontig() - adjust all membuff data to be contiguous |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 170 | * |
| 171 | * This places all data in a membuff into a single contiguous lump, if |
| 172 | * possible |
| 173 | * |
| 174 | * @mb: membuff to adjust |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 175 | * Return: true on success |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 176 | */ |
Simon Glass | f7e87f9 | 2025-03-18 16:20:44 +0100 | [diff] [blame] | 177 | bool membuf_makecontig(struct membuf *mb); |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 178 | |
| 179 | /** |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 180 | * membuf_free() - find the number of bytes that can be written to a membuff |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 181 | * |
| 182 | * @mb: membuff to check |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 183 | * Return: returns the number of bytes free in a membuff |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 184 | */ |
Simon Glass | f7e87f9 | 2025-03-18 16:20:44 +0100 | [diff] [blame] | 185 | int membuf_free(struct membuf *mb); |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 186 | |
| 187 | /** |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 188 | * membuf_readline() - read a line of text from a membuff |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 189 | * |
| 190 | * Reads a line of text of up to 'maxlen' characters from a membuff and puts |
| 191 | * it in @str. Any character less than @minch is assumed to be the end of |
| 192 | * line character |
| 193 | * |
| 194 | * @mb: membuff to adjust |
| 195 | * @str: Place to put the line |
| 196 | * @maxlen: Maximum line length (excluding terminator) |
Simon Glass | fb8895b | 2025-03-18 16:20:48 +0100 | [diff] [blame] | 197 | * @minch: Minimum ASCII character to permit as part of the line (e.g. ' ') |
Ion Agorria | 7e06c1f | 2024-01-05 09:22:10 +0200 | [diff] [blame] | 198 | * @must_fit: If true then str is empty if line doesn't fit |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 199 | * Return: number of bytes read (including terminator) if a line has been |
Ion Agorria | 7e06c1f | 2024-01-05 09:22:10 +0200 | [diff] [blame] | 200 | * read, 0 if nothing was there or line didn't fit when must_fit is set |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 201 | */ |
Simon Glass | fb8895b | 2025-03-18 16:20:48 +0100 | [diff] [blame] | 202 | int membuf_readline(struct membuf *mb, char *str, int maxlen, int minch, |
| 203 | bool must_fit); |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 204 | |
| 205 | /** |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 206 | * membuf_extend_by() - expand a membuff |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 207 | * |
| 208 | * Extends a membuff by the given number of bytes |
| 209 | * |
| 210 | * @mb: membuff to adjust |
| 211 | * @by: Number of bytes to increase the size by |
| 212 | * @max: Maximum size to allow |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 213 | * Return: 0 if the expand succeeded, -ENOMEM if not enough memory, -E2BIG |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 214 | * if the the size would exceed @max |
| 215 | */ |
Simon Glass | f7e87f9 | 2025-03-18 16:20:44 +0100 | [diff] [blame] | 216 | int membuf_extend_by(struct membuf *mb, int by, int max); |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 217 | |
| 218 | /** |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 219 | * membuf_init() - set up a new membuff using an existing membuff |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 220 | * |
| 221 | * @mb: membuff to set up |
| 222 | * @buff: Address of buffer |
| 223 | * @size: Size of buffer |
| 224 | */ |
Simon Glass | f7e87f9 | 2025-03-18 16:20:44 +0100 | [diff] [blame] | 225 | void membuf_init(struct membuf *mb, char *buff, int size); |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 226 | |
| 227 | /** |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 228 | * membuf_uninit() - clear a membuff so it can no longer be used |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 229 | * |
| 230 | * @mb: membuff to uninit |
| 231 | */ |
Simon Glass | f7e87f9 | 2025-03-18 16:20:44 +0100 | [diff] [blame] | 232 | void membuf_uninit(struct membuf *mb); |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 233 | |
| 234 | /** |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 235 | * membuf_new() - create a new membuff |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 236 | * |
| 237 | * @mb: membuff to init |
| 238 | * @size: size of membuff to create |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 239 | * Return: 0 if OK, -ENOMEM if out of memory |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 240 | */ |
Simon Glass | f7e87f9 | 2025-03-18 16:20:44 +0100 | [diff] [blame] | 241 | int membuf_new(struct membuf *mb, int size); |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 242 | |
| 243 | /** |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 244 | * membuf_dispose() - free memory allocated to a membuff and uninit it |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 245 | * |
| 246 | * @mb: membuff to dispose |
| 247 | */ |
Simon Glass | f7e87f9 | 2025-03-18 16:20:44 +0100 | [diff] [blame] | 248 | void membuf_dispose(struct membuf *mb); |
Simon Glass | 1294ecf | 2015-11-08 23:47:47 -0700 | [diff] [blame] | 249 | |
| 250 | #endif |