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