1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
|
/** @file
AML Stream.
Copyright (c) 2019 - 2020, Arm Limited. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef AML_STREAM_H_
#define AML_STREAM_H_
#include <AmlInclude.h>
/** Stream direction.
Enum to choose the direction the stream is progressing.
*/
typedef enum EAmlStreamDirection {
EAmlStreamDirectionInvalid, ///< Invalid AML Stream direction.
EAmlStreamDirectionForward, ///< Forward direction.
/// The Stream goes toward higher addresses.
EAmlStreamDirectionBackward, ///< Forward direction.
/// The Stream goes toward lower addresses.
EAmlStreamDirectionMax, ///< Max enum.
} EAML_STREAM_DIRECTION;
/** Stream.
This structure is used as a wrapper around a buffer. It allows to do common
buffer manipulations (read, write, etc.) while preventing buffer overflows.
*/
typedef struct AmlStream {
/// Pointer to a buffer.
UINT8 *Buffer;
/// Size of Buffer.
UINT32 MaxBufferSize;
/// Index in the Buffer.
/// The Index field allows to keep track of how many bytes have been
/// read/written in the Buffer, and to retrieve the current stream position.
/// 0 <= Index <= MaxBufferSize.
/// If Index == MaxBufferSize, no more action is allowed on the stream.
UINT32 Index;
/// The direction the stream is progressing.
/// If the stream goes backward (toward lower addresses), the bytes written
/// to the stream are not reverted.
/// In the example below, writing "Hello" to the stream will not revert
/// the string. The end of the stream buffer will contain "Hello world!".
/// Similarly, moving the stream position will be done according to the
/// direction of the stream.
/// Stream buffer:
/// +---------------+-----+-----+-----+-----+-----+-----+---- +------+
/// |-------------- | ' ' | 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' |
/// +---------------+-----+-----+-----+-----+-----+-----+---- +------+
/// ^
/// Current position.
EAML_STREAM_DIRECTION Direction;
} AML_STREAM;
/** Check whether a StreamPtr is a valid Stream.
@param [in] Stream Pointer to a stream.
@retval TRUE Stream is a pointer to a stream.
@retval FALSE Otherwise.
*/
#define IS_STREAM(Stream) ( \
(((AML_STREAM*)Stream) != NULL) && \
(((AML_STREAM*)Stream)->Buffer != NULL))
/** Check whether a Stream is at the end of its buffer.
@param [in] Stream Pointer to a stream.
@retval TRUE Stream is a pointer to a non-full stream.
@retval FALSE Otherwise.
*/
#define IS_END_OF_STREAM(Stream) ( \
(((AML_STREAM*)Stream)->Index == \
((AML_STREAM*)Stream)->MaxBufferSize))
/** Check Stream goes forward.
@param [in] Stream Pointer to a stream.
@retval TRUE Stream goes forward.
@retval FALSE Otherwise.
*/
#define IS_STREAM_FORWARD(Stream) ( \
((AML_STREAM*)Stream)->Direction == EAmlStreamDirectionForward)
/** Check Stream goes backward.
@param [in] Stream Pointer to a stream.
@retval TRUE Stream goes backward.
@retval FALSE Otherwise.
*/
#define IS_STREAM_BACKWARD(Stream) ( \
((AML_STREAM*)Stream)->Direction == EAmlStreamDirectionBackward)
/** Initialize a stream.
@param [in, out] Stream Pointer to the stream to initialize.
@param [in] Buffer Buffer to initialize Stream with.
Point to the beginning of the Buffer.
@param [in] MaxBufferSize Maximum size of Buffer.
@param [in] Direction Direction Stream is progressing
(forward, backward).
@retval EFI_SUCCESS The function completed successfully.
@retval EFI_INVALID_PARAMETER Invalid parameter.
**/
EFI_STATUS
EFIAPI
AmlStreamInit (
IN OUT AML_STREAM *Stream,
IN UINT8 *Buffer,
IN UINT32 MaxBufferSize,
IN EAML_STREAM_DIRECTION Direction
);
/** Clone a stream.
Cloning a stream means copying all the values of the input Stream
in the ClonedStream.
@param [in] Stream Pointer to the stream to clone.
@param [in] ClonedStream Pointer to the stream to initialize.
@retval EFI_SUCCESS The function completed successfully.
@retval EFI_INVALID_PARAMETER Invalid parameter.
**/
EFI_STATUS
EFIAPI
AmlStreamClone (
IN CONST AML_STREAM *Stream,
OUT AML_STREAM *ClonedStream
);
/** Initialize a sub-stream from a stream.
A sub-stream is a stream initialized at the current position of the input
stream:
- the Buffer field points to the current position of the input stream;
- the Index field is set to 0;
- the MaxBufferSize field is set to the remaining size of the input stream;
- the direction is conserved;
E.g.: For a forward stream:
+----------------+----------------+
|ABCD.........XYZ| Free Space |
+----------------+----------------+
^ ^ ^
Stream: Buffer CurrPos EndOfBuff
Sub-stream: Buffer/CurrPos EndOfBuff
@param [in] Stream Pointer to the stream from which a sub-stream is
created.
@param [in] SubStream Pointer to the stream to initialize.
@retval EFI_SUCCESS The function completed successfully.
@retval EFI_INVALID_PARAMETER Invalid parameter.
**/
EFI_STATUS
EFIAPI
AmlStreamInitSubStream (
IN CONST AML_STREAM *Stream,
OUT AML_STREAM *SubStream
);
/** Get the buffer of a stream.
@param [in] Stream Pointer to a stream.
@return The stream's Buffer.
NULL otherwise.
**/
UINT8 *
EFIAPI
AmlStreamGetBuffer (
IN CONST AML_STREAM *Stream
);
/** Get the size of Stream's Buffer.
@param [in] Stream Pointer to a stream.
@return The Size of Stream's Buffer.
Return 0 if Stream is invalid.
**/
UINT32
EFIAPI
AmlStreamGetMaxBufferSize (
IN CONST AML_STREAM *Stream
);
/** Reduce the maximal size of Stream's Buffer (MaxBufferSize field).
@param [in] Stream Pointer to a stream.
@param [in] Diff Value to subtract to the Stream's MaxBufferSize.
0 < x < MaxBufferSize - Index.
@retval EFI_SUCCESS The function completed successfully.
@retval EFI_INVALID_PARAMETER Invalid parameter.
**/
EFI_STATUS
EFIAPI
AmlStreamReduceMaxBufferSize (
IN AML_STREAM *Stream,
IN UINT32 Diff
);
/** Get Stream's Index.
Stream's Index is incremented when writing data, reading data,
or moving the position in the Stream.
It can be seen as an index:
- starting at the beginning of Stream's Buffer if the stream goes forward;
- starting at the end of Stream's Buffer if the stream goes backward.
@param [in] Stream Pointer to a stream.
@return Stream's Index.
Return 0 if Stream is invalid.
**/
UINT32
EFIAPI
AmlStreamGetIndex (
IN CONST AML_STREAM *Stream
);
/** Get Stream's Direction.
@param [in] Stream Pointer to a stream.
@return Stream's Direction.
Return EAmlStreamDirectionUnknown if Stream is invalid.
**/
EAML_STREAM_DIRECTION
EFIAPI
AmlStreamGetDirection (
IN CONST AML_STREAM *Stream
);
/** Return a pointer to the current position in the stream.
@param [in] Stream Pointer to a stream.
@return The current position in the stream.
Return NULL if error.
**/
UINT8 *
EFIAPI
AmlStreamGetCurrPos (
IN CONST AML_STREAM *Stream
);
/** Get the space available in the stream.
@param [in] Stream Pointer to a stream.
@return Remaining space available in the stream.
Zero in case of error or if the stream is at its end.
**/
UINT32
EFIAPI
AmlStreamGetFreeSpace (
IN CONST AML_STREAM *Stream
);
/** Move Stream by Offset bytes.
The stream current position is moved according to the stream direction
(forward, backward).
@param [in] Stream Pointer to a stream.
The stream must not be at its end.
@param [in] Offset Offset to move the stream of.
@retval EFI_SUCCESS The function completed successfully.
@retval EFI_INVALID_PARAMETER Invalid parameter.
@retval EFI_BUFFER_TOO_SMALL No space left in the buffer.
**/
EFI_STATUS
EFIAPI
AmlStreamProgress (
IN AML_STREAM *Stream,
IN UINT32 Offset
);
/** Rewind Stream of Offset bytes.
The stream current position is rewound according to the stream direction
(forward, backward). A stream going forward will be rewound backward.
@param [in] Stream Pointer to a stream.
@param [in] Offset Offset to rewind the stream of.
@retval EFI_SUCCESS The function completed successfully.
@retval EFI_INVALID_PARAMETER Invalid parameter.
@retval EFI_BUFFER_TOO_SMALL No space left in the buffer.
**/
EFI_STATUS
EFIAPI
AmlStreamRewind (
IN AML_STREAM *Stream,
IN UINT32 Offset
);
/** Reset the Stream (move the current position to the initial position).
@param [in] Stream Pointer to a stream.
@retval EFI_SUCCESS The function completed successfully.
@retval EFI_INVALID_PARAMETER Invalid parameter.
**/
EFI_STATUS
EFIAPI
AmlStreamReset (
IN AML_STREAM *Stream
);
/** Peek one byte at Stream's current position.
Stream's position is not moved when peeking.
@param [in] Stream Pointer to a stream.
The stream must not be at its end.
@param [out] OutByte Pointer holding the byte value of
the stream current position.
@retval EFI_SUCCESS The function completed successfully.
@retval EFI_INVALID_PARAMETER Invalid parameter.
@retval EFI_BUFFER_TOO_SMALL No space left in the buffer.
**/
EFI_STATUS
EFIAPI
AmlStreamPeekByte (
IN AML_STREAM *Stream,
OUT UINT8 *OutByte
);
/** Read one byte at Stream's current position.
The stream current position is moved when reading.
@param [in] Stream Pointer to a stream.
The stream must not be at its end.
@param [out] OutByte Pointer holding the byte value of
the stream current position.
@retval EFI_SUCCESS The function completed successfully.
@retval EFI_INVALID_PARAMETER Invalid parameter.
@retval EFI_BUFFER_TOO_SMALL No space left in the buffer.
**/
EFI_STATUS
EFIAPI
AmlStreamReadByte (
IN AML_STREAM *Stream,
OUT UINT8 *OutByte
);
/** Write Size bytes in the stream.
If the stream goes backward (toward lower addresses), the bytes written
to the stream are not reverted.
In the example below, writing "Hello" to the stream will not revert
the string. The end of the stream buffer will contain "Hello world!".
Stream buffer:
+---------------+-----+-----+-----+-----+-----+-----+---- +------+
| ..... | ' ' | 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' |
+---------------+-----+-----+-----+-----+-----+-----+---- +------+
^
Current position.
@param [in] Stream Pointer to a stream.
The stream must not be at its end.
@param [in] Buffer Pointer to the data to write.
@param [in] Size Number of bytes to write.
@retval EFI_SUCCESS The function completed successfully.
@retval EFI_INVALID_PARAMETER Invalid parameter.
@retval EFI_BUFFER_TOO_SMALL No space left in the buffer.
**/
EFI_STATUS
EFIAPI
AmlStreamWrite (
IN AML_STREAM *Stream,
IN CONST UINT8 *Buffer,
IN UINT32 Size
);
/** Compare Size bytes between Stream1 and Stream2 from their
respective current position.
Stream1 and Stream2 must go in the same direction.
Stream1 and Stream2 are left unchanged.
@param [in] Stream1 First stream to compare.
The stream must not be at its end.
@param [in] Stream2 Second stream to compare.
The stream must not be at its end.
@param [in] Size Number of bytes to compare.
Must be lower than the minimum remaining space of
Stream1 and Stream2.
Must be non-zero.
@retval TRUE If Stream1 and Stream2 have Size bytes equal,
from their respective current position.
The function completed successfully.
@retval FALSE Otherwise.
**/
BOOLEAN
EFIAPI
AmlStreamCmp (
IN CONST AML_STREAM *Stream1,
IN CONST AML_STREAM *Stream2,
IN UINT32 Size
);
/** Copy Size bytes of the stream's data to DstBuffer.
For a backward stream, the bytes are copied starting from the
current stream position.
@param [out] DstBuffer Destination Buffer to copy the data to.
@param [in] MaxDstBufferSize Maximum size of DstBuffer.
Must be non-zero.
@param [in] Stream Pointer to the stream to copy the data from.
@param [in] Size Number of bytes to copy from the stream
buffer.
Must be lower than MaxDstBufferSize.
Must be lower than Stream's MaxBufferSize.
Return success if zero.
@retval EFI_SUCCESS The function completed successfully.
@retval EFI_INVALID_PARAMETER Invalid parameter.
**/
EFI_STATUS
EFIAPI
AmlStreamCpyS (
OUT CHAR8 *DstBuffer,
IN UINT32 MaxDstBufferSize,
IN AML_STREAM *Stream,
IN UINT32 Size
);
#endif // AML_STREAM_H_
|