4 Copyright (c) 2019 - 2020, Arm Limited. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <AmlInclude.h>
16 Enum to choose the direction the stream is progressing.
18 typedef enum EAmlStreamDirection
{
19 EAmlStreamDirectionInvalid
, ///< Invalid AML Stream direction.
20 EAmlStreamDirectionForward
, ///< Forward direction.
21 /// The Stream goes toward higher addresses.
22 EAmlStreamDirectionBackward
, ///< Forward direction.
23 /// The Stream goes toward lower addresses.
24 EAmlStreamDirectionMax
, ///< Max enum.
25 } EAML_STREAM_DIRECTION
;
29 This structure is used as a wrapper around a buffer. It allows to do common
30 buffer manipulations (read, write, etc.) while preventing buffer overflows.
32 typedef struct AmlStream
{
33 /// Pointer to a buffer.
39 /// Index in the Buffer.
40 /// The Index field allows to keep track of how many bytes have been
41 /// read/written in the Buffer, and to retrieve the current stream position.
42 /// 0 <= Index <= MaxBufferSize.
43 /// If Index == MaxBufferSize, no more action is allowed on the stream.
46 /// The direction the stream is progressing.
47 /// If the stream goes backward (toward lower addresses), the bytes written
48 /// to the stream are not reverted.
49 /// In the example below, writing "Hello" to the stream will not revert
50 /// the string. The end of the stream buffer will contain "Hello world!".
51 /// Similarly, moving the stream position will be done according to the
52 /// direction of the stream.
54 /// +---------------+-----+-----+-----+-----+-----+-----+---- +------+
55 /// |-------------- | ' ' | 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' |
56 /// +---------------+-----+-----+-----+-----+-----+-----+---- +------+
59 EAML_STREAM_DIRECTION Direction
;
62 /** Check whether a StreamPtr is a valid Stream.
64 @param [in] Stream Pointer to a stream.
66 @retval TRUE Stream is a pointer to a stream.
67 @retval FALSE Otherwise.
69 #define IS_STREAM(Stream) ( \
70 (((AML_STREAM*)Stream) != NULL) && \
71 (((AML_STREAM*)Stream)->Buffer != NULL))
73 /** Check whether a Stream is at the end of its buffer.
75 @param [in] Stream Pointer to a stream.
77 @retval TRUE Stream is a pointer to a non-full stream.
78 @retval FALSE Otherwise.
80 #define IS_END_OF_STREAM(Stream) ( \
81 (((AML_STREAM*)Stream)->Index == \
82 ((AML_STREAM*)Stream)->MaxBufferSize))
84 /** Check Stream goes forward.
86 @param [in] Stream Pointer to a stream.
88 @retval TRUE Stream goes forward.
89 @retval FALSE Otherwise.
91 #define IS_STREAM_FORWARD(Stream) ( \
92 ((AML_STREAM*)Stream)->Direction == EAmlStreamDirectionForward)
94 /** Check Stream goes backward.
96 @param [in] Stream Pointer to a stream.
98 @retval TRUE Stream goes backward.
99 @retval FALSE Otherwise.
101 #define IS_STREAM_BACKWARD(Stream) ( \
102 ((AML_STREAM*)Stream)->Direction == EAmlStreamDirectionBackward)
104 /** Initialize a stream.
106 @param [in, out] Stream Pointer to the stream to initialize.
107 @param [in] Buffer Buffer to initialize Stream with.
108 Point to the beginning of the Buffer.
109 @param [in] MaxBufferSize Maximum size of Buffer.
110 @param [in] Direction Direction Stream is progressing
113 @retval EFI_SUCCESS The function completed successfully.
114 @retval EFI_INVALID_PARAMETER Invalid parameter.
119 IN OUT AML_STREAM
* Stream
,
121 IN UINT32 MaxBufferSize
,
122 IN EAML_STREAM_DIRECTION Direction
127 Cloning a stream means copying all the values of the input Stream
130 @param [in] Stream Pointer to the stream to clone.
131 @param [in] ClonedStream Pointer to the stream to initialize.
133 @retval EFI_SUCCESS The function completed successfully.
134 @retval EFI_INVALID_PARAMETER Invalid parameter.
139 IN CONST AML_STREAM
* Stream
,
140 OUT AML_STREAM
* ClonedStream
143 /** Initialize a sub-stream from a stream.
145 A sub-stream is a stream initialized at the current position of the input
147 - the Buffer field points to the current position of the input stream;
148 - the Index field is set to 0;
149 - the MaxBufferSize field is set to the remaining size of the input stream;
150 - the direction is conserved;
152 E.g.: For a forward stream:
153 +----------------+----------------+
154 |ABCD.........XYZ| Free Space |
155 +----------------+----------------+
157 Stream: Buffer CurrPos EndOfBuff
158 Sub-stream: Buffer/CurrPos EndOfBuff
160 @param [in] Stream Pointer to the stream from which a sub-stream is
162 @param [in] SubStream Pointer to the stream to initialize.
164 @retval EFI_SUCCESS The function completed successfully.
165 @retval EFI_INVALID_PARAMETER Invalid parameter.
169 AmlStreamInitSubStream (
170 IN CONST AML_STREAM
* Stream
,
171 OUT AML_STREAM
* SubStream
174 /** Get the buffer of a stream.
176 @param [in] Stream Pointer to a stream.
178 @return The stream's Buffer.
184 IN CONST AML_STREAM
* Stream
187 /** Get the size of Stream's Buffer.
189 @param [in] Stream Pointer to a stream.
191 @return The Size of Stream's Buffer.
192 Return 0 if Stream is invalid.
196 AmlStreamGetMaxBufferSize (
197 IN CONST AML_STREAM
* Stream
200 /** Reduce the maximal size of Stream's Buffer (MaxBufferSize field).
202 @param [in] Stream Pointer to a stream.
203 @param [in] Diff Value to subtract to the Stream's MaxBufferSize.
204 0 < x < MaxBufferSize - Index.
206 @retval EFI_SUCCESS The function completed successfully.
207 @retval EFI_INVALID_PARAMETER Invalid parameter.
211 AmlStreamReduceMaxBufferSize (
212 IN AML_STREAM
* Stream
,
216 /** Get Stream's Index.
218 Stream's Index is incremented when writing data, reading data,
219 or moving the position in the Stream.
220 It can be seen as an index:
221 - starting at the beginning of Stream's Buffer if the stream goes forward;
222 - starting at the end of Stream's Buffer if the stream goes backward.
224 @param [in] Stream Pointer to a stream.
226 @return Stream's Index.
227 Return 0 if Stream is invalid.
232 IN CONST AML_STREAM
* Stream
235 /** Get Stream's Direction.
237 @param [in] Stream Pointer to a stream.
239 @return Stream's Direction.
240 Return EAmlStreamDirectionUnknown if Stream is invalid.
242 EAML_STREAM_DIRECTION
244 AmlStreamGetDirection (
245 IN CONST AML_STREAM
* Stream
248 /** Return a pointer to the current position in the stream.
250 @param [in] Stream Pointer to a stream.
252 @return The current position in the stream.
253 Return NULL if error.
257 AmlStreamGetCurrPos (
258 IN CONST AML_STREAM
* Stream
261 /** Get the space available in the stream.
263 @param [in] Stream Pointer to a stream.
265 @return Remaining space available in the stream.
266 Zero in case of error or if the stream is at its end.
270 AmlStreamGetFreeSpace (
271 IN CONST AML_STREAM
* Stream
274 /** Move Stream by Offset bytes.
276 The stream current position is moved according to the stream direction
279 @param [in] Stream Pointer to a stream.
280 The stream must not be at its end.
281 @param [in] Offset Offset to move the stream of.
283 @retval EFI_SUCCESS The function completed successfully.
284 @retval EFI_INVALID_PARAMETER Invalid parameter.
285 @retval EFI_BUFFER_TOO_SMALL No space left in the buffer.
290 IN AML_STREAM
* Stream
,
294 /** Rewind Stream of Offset bytes.
296 The stream current position is rewound according to the stream direction
297 (forward, backward). A stream going forward will be rewound backward.
299 @param [in] Stream Pointer to a stream.
300 @param [in] Offset Offset to rewind the stream of.
302 @retval EFI_SUCCESS The function completed successfully.
303 @retval EFI_INVALID_PARAMETER Invalid parameter.
304 @retval EFI_BUFFER_TOO_SMALL No space left in the buffer.
309 IN AML_STREAM
* Stream
,
313 /** Reset the Stream (move the current position to the initial position).
315 @param [in] Stream Pointer to a stream.
317 @retval EFI_SUCCESS The function completed successfully.
318 @retval EFI_INVALID_PARAMETER Invalid parameter.
323 IN AML_STREAM
* Stream
326 /** Peek one byte at Stream's current position.
328 Stream's position is not moved when peeking.
330 @param [in] Stream Pointer to a stream.
331 The stream must not be at its end.
332 @param [out] OutByte Pointer holding the byte value of
333 the stream current position.
335 @retval EFI_SUCCESS The function completed successfully.
336 @retval EFI_INVALID_PARAMETER Invalid parameter.
337 @retval EFI_BUFFER_TOO_SMALL No space left in the buffer.
342 IN AML_STREAM
* Stream
,
346 /** Read one byte at Stream's current position.
348 The stream current position is moved when reading.
350 @param [in] Stream Pointer to a stream.
351 The stream must not be at its end.
352 @param [out] OutByte Pointer holding the byte value of
353 the stream current position.
355 @retval EFI_SUCCESS The function completed successfully.
356 @retval EFI_INVALID_PARAMETER Invalid parameter.
357 @retval EFI_BUFFER_TOO_SMALL No space left in the buffer.
362 IN AML_STREAM
* Stream
,
366 /** Write Size bytes in the stream.
368 If the stream goes backward (toward lower addresses), the bytes written
369 to the stream are not reverted.
370 In the example below, writing "Hello" to the stream will not revert
371 the string. The end of the stream buffer will contain "Hello world!".
373 +---------------+-----+-----+-----+-----+-----+-----+---- +------+
374 | ..... | ' ' | 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' |
375 +---------------+-----+-----+-----+-----+-----+-----+---- +------+
379 @param [in] Stream Pointer to a stream.
380 The stream must not be at its end.
381 @param [in] Buffer Pointer to the data to write.
382 @param [in] Size Number of bytes to write.
384 @retval EFI_SUCCESS The function completed successfully.
385 @retval EFI_INVALID_PARAMETER Invalid parameter.
386 @retval EFI_BUFFER_TOO_SMALL No space left in the buffer.
391 IN AML_STREAM
* Stream
,
392 IN CONST UINT8
* Buffer
,
396 /** Compare Size bytes between Stream1 and Stream2 from their
397 respective current position.
399 Stream1 and Stream2 must go in the same direction.
400 Stream1 and Stream2 are left unchanged.
402 @param [in] Stream1 First stream to compare.
403 The stream must not be at its end.
404 @param [in] Stream2 Second stream to compare.
405 The stream must not be at its end.
406 @param [in] Size Number of bytes to compare.
407 Must be lower than the minimum remaining space of
411 @retval TRUE If Stream1 and Stream2 have Size bytes equal,
412 from their respective current position.
413 The function completed successfully.
414 @retval FALSE Otherwise.
419 IN CONST AML_STREAM
* Stream1
,
420 IN CONST AML_STREAM
* Stream2
,
424 /** Copy Size bytes of the stream's data to DstBuffer.
426 For a backward stream, the bytes are copied starting from the
427 current stream position.
429 @param [out] DstBuffer Destination Buffer to copy the data to.
430 @param [in] MaxDstBufferSize Maximum size of DstBuffer.
432 @param [in] Stream Pointer to the stream to copy the data from.
433 @param [in] Size Number of bytes to copy from the stream
435 Must be lower than MaxDstBufferSize.
436 Must be lower than Stream's MaxBufferSize.
437 Return success if zero.
439 @retval EFI_SUCCESS The function completed successfully.
440 @retval EFI_INVALID_PARAMETER Invalid parameter.
445 OUT CHAR8
* DstBuffer
,
446 IN UINT32 MaxDstBufferSize
,
447 IN AML_STREAM
* Stream
,
451 #endif // AML_STREAM_H_