]> git.proxmox.com Git - mirror_edk2.git/blame - DynamicTablesPkg/Library/Common/AmlLib/Stream/AmlStream.c
DynamicTablesPkg: Apply uncrustify changes
[mirror_edk2.git] / DynamicTablesPkg / Library / Common / AmlLib / Stream / AmlStream.c
CommitLineData
7f293b25
SM
1/** @file\r
2 AML Stream.\r
3\r
4 Copyright (c) 2019 - 2020, Arm Limited. All rights reserved.<BR>\r
5\r
6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
7**/\r
8\r
9#include <Stream/AmlStream.h>\r
10\r
11/** Initialize a stream.\r
12\r
13 @param [in, out] Stream Pointer to the stream to initialize.\r
14 @param [in] Buffer Buffer to initialize Stream with.\r
15 Point to the beginning of the Buffer.\r
16 @param [in] MaxBufferSize Maximum size of Buffer.\r
17 @param [in] Direction Direction Stream is progressing\r
18 (forward, backward).\r
19\r
20 @retval EFI_SUCCESS The function completed successfully.\r
21 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
22**/\r
23EFI_STATUS\r
24EFIAPI\r
25AmlStreamInit (\r
731c67e1
MK
26 IN OUT AML_STREAM *Stream,\r
27 IN UINT8 *Buffer,\r
28 IN UINT32 MaxBufferSize,\r
29 IN EAML_STREAM_DIRECTION Direction\r
7f293b25
SM
30 )\r
31{\r
32 if ((Stream == NULL) ||\r
33 (Buffer == NULL) ||\r
34 (MaxBufferSize == 0) ||\r
35 ((Direction != EAmlStreamDirectionForward) &&\r
731c67e1
MK
36 (Direction != EAmlStreamDirectionBackward)))\r
37 {\r
7f293b25
SM
38 ASSERT (0);\r
39 return EFI_INVALID_PARAMETER;\r
40 }\r
41\r
731c67e1 42 Stream->Buffer = Buffer;\r
7f293b25 43 Stream->MaxBufferSize = MaxBufferSize;\r
731c67e1
MK
44 Stream->Index = 0;\r
45 Stream->Direction = Direction;\r
7f293b25
SM
46\r
47 return EFI_SUCCESS;\r
48}\r
49\r
50/** Clone a stream.\r
51\r
52 Cloning a stream means copying all the values of the input Stream\r
53 in the ClonedStream.\r
54\r
55 @param [in] Stream Pointer to the stream to clone.\r
5e0b708f 56 @param [out] ClonedStream Pointer to the stream to initialize.\r
7f293b25
SM
57\r
58 @retval EFI_SUCCESS The function completed successfully.\r
59 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
60**/\r
61EFI_STATUS\r
62EFIAPI\r
63AmlStreamClone (\r
731c67e1
MK
64 IN CONST AML_STREAM *Stream,\r
65 OUT AML_STREAM *ClonedStream\r
7f293b25
SM
66 )\r
67{\r
68 if (!IS_STREAM (Stream) ||\r
731c67e1
MK
69 (ClonedStream == NULL))\r
70 {\r
7f293b25
SM
71 ASSERT (0);\r
72 return EFI_INVALID_PARAMETER;\r
73 }\r
74\r
731c67e1 75 ClonedStream->Buffer = Stream->Buffer;\r
7f293b25 76 ClonedStream->MaxBufferSize = Stream->MaxBufferSize;\r
731c67e1
MK
77 ClonedStream->Index = Stream->Index;\r
78 ClonedStream->Direction = Stream->Direction;\r
7f293b25
SM
79\r
80 return EFI_SUCCESS;\r
81}\r
82\r
83/** Initialize a sub-stream from a stream.\r
84\r
85 A sub-stream is a stream initialized at the current position of the input\r
86 stream:\r
87 - the Buffer field points to the current position of the input stream;\r
88 - the Index field is set to 0;\r
89 - the MaxBufferSize field is set to the remaining size of the input stream;\r
90 - the direction is conserved;\r
91\r
92 E.g.: For a forward stream:\r
93 +----------------+----------------+\r
94 |ABCD.........XYZ| Free Space |\r
95 +----------------+----------------+\r
96 ^ ^ ^\r
97 Stream: Buffer CurrPos EndOfBuff\r
98 Sub-stream: Buffer/CurrPos EndOfBuff\r
99\r
100 @param [in] Stream Pointer to the stream from which a sub-stream is\r
101 created.\r
5e0b708f 102 @param [out] SubStream Pointer to the stream to initialize.\r
7f293b25
SM
103\r
104 @retval EFI_SUCCESS The function completed successfully.\r
105 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
106**/\r
107EFI_STATUS\r
108EFIAPI\r
109AmlStreamInitSubStream (\r
731c67e1
MK
110 IN CONST AML_STREAM *Stream,\r
111 OUT AML_STREAM *SubStream\r
7f293b25
SM
112 )\r
113{\r
114 if (!IS_STREAM (Stream) ||\r
731c67e1
MK
115 (SubStream == NULL))\r
116 {\r
7f293b25
SM
117 ASSERT (0);\r
118 return EFI_INVALID_PARAMETER;\r
119 }\r
120\r
121 if (IS_STREAM_FORWARD (Stream)) {\r
122 SubStream->Buffer = AmlStreamGetCurrPos (Stream);\r
123 } else if (IS_STREAM_BACKWARD (Stream)) {\r
124 SubStream->Buffer = Stream->Buffer;\r
125 } else {\r
126 ASSERT (0);\r
127 return EFI_INVALID_PARAMETER;\r
128 }\r
129\r
130 SubStream->MaxBufferSize = AmlStreamGetFreeSpace (Stream);\r
731c67e1
MK
131 SubStream->Index = 0;\r
132 SubStream->Direction = Stream->Direction;\r
7f293b25
SM
133\r
134 return EFI_SUCCESS;\r
135}\r
136\r
137/** Get the buffer of a stream.\r
138\r
139 @param [in] Stream Pointer to a stream.\r
140\r
141 @return The stream's Buffer.\r
142 NULL otherwise.\r
143**/\r
144UINT8 *\r
145EFIAPI\r
146AmlStreamGetBuffer (\r
731c67e1 147 IN CONST AML_STREAM *Stream\r
7f293b25
SM
148 )\r
149{\r
150 if (!IS_STREAM (Stream)) {\r
151 ASSERT (0);\r
152 return NULL;\r
153 }\r
731c67e1 154\r
7f293b25
SM
155 return Stream->Buffer;\r
156}\r
157\r
158/** Get the size of Stream's Buffer.\r
159\r
160 @param [in] Stream Pointer to a stream.\r
161\r
162 @return The Size of Stream's Buffer.\r
163 Return 0 if Stream is invalid.\r
164**/\r
165UINT32\r
166EFIAPI\r
167AmlStreamGetMaxBufferSize (\r
731c67e1 168 IN CONST AML_STREAM *Stream\r
7f293b25
SM
169 )\r
170{\r
171 if (!IS_STREAM (Stream)) {\r
172 ASSERT (0);\r
173 return 0;\r
174 }\r
731c67e1 175\r
7f293b25
SM
176 return Stream->MaxBufferSize;\r
177}\r
178\r
179/** Reduce the maximal size of Stream's Buffer (MaxBufferSize field).\r
180\r
181 @param [in] Stream Pointer to a stream.\r
182 @param [in] Diff Value to subtract to the Stream's MaxBufferSize.\r
183 0 < x < MaxBufferSize - Index.\r
184\r
185 @retval EFI_SUCCESS The function completed successfully.\r
186 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
187**/\r
188EFI_STATUS\r
189EFIAPI\r
190AmlStreamReduceMaxBufferSize (\r
731c67e1
MK
191 IN AML_STREAM *Stream,\r
192 IN UINT32 Diff\r
7f293b25
SM
193 )\r
194{\r
195 if (!IS_STREAM (Stream) ||\r
196 (Diff == 0) ||\r
731c67e1
MK
197 ((Stream->MaxBufferSize - Diff) <= Stream->Index))\r
198 {\r
7f293b25
SM
199 ASSERT (0);\r
200 return EFI_INVALID_PARAMETER;\r
201 }\r
202\r
203 Stream->MaxBufferSize -= Diff;\r
204 return EFI_SUCCESS;\r
205}\r
206\r
207/** Get Stream's Index.\r
208\r
209 Stream's Index is incremented when writing data, reading data,\r
210 or moving the position in the Stream.\r
211 It can be seen as an index:\r
212 - starting at the beginning of Stream's Buffer if the stream goes forward;\r
213 - starting at the end of Stream's Buffer if the stream goes backward.\r
214\r
215 @param [in] Stream Pointer to a stream.\r
216\r
217 @return Stream's Index.\r
218 Return 0 if Stream is invalid.\r
219**/\r
220UINT32\r
221EFIAPI\r
222AmlStreamGetIndex (\r
731c67e1 223 IN CONST AML_STREAM *Stream\r
7f293b25
SM
224 )\r
225{\r
226 if (!IS_STREAM (Stream)) {\r
227 ASSERT (0);\r
228 return 0;\r
229 }\r
731c67e1 230\r
7f293b25
SM
231 return Stream->Index;\r
232}\r
233\r
234/** Get Stream's Direction.\r
235\r
236 @param [in] Stream Pointer to a stream.\r
237\r
238 @return Stream's Direction.\r
239 Return EAmlStreamDirectionUnknown if Stream is invalid.\r
240**/\r
241EAML_STREAM_DIRECTION\r
242EFIAPI\r
243AmlStreamGetDirection (\r
731c67e1 244 IN CONST AML_STREAM *Stream\r
7f293b25
SM
245 )\r
246{\r
247 if (!IS_STREAM (Stream)) {\r
248 ASSERT (0);\r
249 return EAmlStreamDirectionInvalid;\r
250 }\r
731c67e1 251\r
7f293b25
SM
252 return Stream->Direction;\r
253}\r
254\r
255/** Return a pointer to the current position in the stream.\r
256\r
257 @param [in] Stream Pointer to a stream.\r
258\r
259 @return The current position in the stream.\r
260 Return NULL if error.\r
261**/\r
262UINT8 *\r
263EFIAPI\r
264AmlStreamGetCurrPos (\r
731c67e1 265 IN CONST AML_STREAM *Stream\r
7f293b25
SM
266 )\r
267{\r
268 if (!IS_STREAM (Stream)) {\r
269 ASSERT (0);\r
270 return NULL;\r
271 }\r
272\r
273 if (IS_STREAM_FORWARD (Stream)) {\r
274 return Stream->Buffer + Stream->Index;\r
275 } else if (IS_STREAM_BACKWARD (Stream)) {\r
276 return Stream->Buffer + (Stream->MaxBufferSize - 1) - Stream->Index;\r
277 } else {\r
278 ASSERT (0);\r
279 return NULL;\r
280 }\r
281}\r
282\r
283/** Get the space available in the stream.\r
284\r
285 @param [in] Stream Pointer to a stream.\r
286\r
287 @return Remaining space available in the stream.\r
288 Zero in case of error or if the stream is at its end.\r
289**/\r
290UINT32\r
291EFIAPI\r
292AmlStreamGetFreeSpace (\r
731c67e1 293 IN CONST AML_STREAM *Stream\r
7f293b25
SM
294 )\r
295{\r
296 if (!IS_STREAM (Stream)) {\r
297 ASSERT (0);\r
298 return 0;\r
299 }\r
300\r
301 if (Stream->Index > Stream->MaxBufferSize) {\r
302 ASSERT (0);\r
303 return 0;\r
304 }\r
305\r
306 return Stream->MaxBufferSize - Stream->Index;\r
307}\r
308\r
309/** Move Stream by Offset bytes.\r
310\r
311 The stream current position is moved according to the stream direction\r
312 (forward, backward).\r
313\r
314 @param [in] Stream Pointer to a stream.\r
315 The stream must not be at its end.\r
316 @param [in] Offset Offset to move the stream of.\r
317\r
318 @retval EFI_SUCCESS The function completed successfully.\r
319 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
320 @retval EFI_BUFFER_TOO_SMALL No space left in the buffer.\r
321**/\r
322EFI_STATUS\r
323EFIAPI\r
324AmlStreamProgress (\r
731c67e1
MK
325 IN AML_STREAM *Stream,\r
326 IN UINT32 Offset\r
7f293b25
SM
327 )\r
328{\r
329 if (!IS_STREAM (Stream) ||\r
731c67e1
MK
330 IS_END_OF_STREAM (Stream) ||\r
331 (Offset == 0))\r
332 {\r
7f293b25
SM
333 ASSERT (0);\r
334 return EFI_INVALID_PARAMETER;\r
335 }\r
336\r
337 if (AmlStreamGetFreeSpace (Stream) < Offset) {\r
338 ASSERT (0);\r
339 return EFI_BUFFER_TOO_SMALL;\r
340 }\r
341\r
342 Stream->Index += Offset;\r
343\r
344 return EFI_SUCCESS;\r
345}\r
346\r
347/** Rewind Stream of Offset bytes.\r
348\r
349 The stream current position is rewound according to the stream direction\r
350 (forward, backward). A stream going forward will be rewound backward.\r
351\r
352 @param [in] Stream Pointer to a stream.\r
353 @param [in] Offset Offset to rewind the stream of.\r
354\r
355 @retval EFI_SUCCESS The function completed successfully.\r
356 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
357 @retval EFI_BUFFER_TOO_SMALL No space left in the buffer.\r
358**/\r
359EFI_STATUS\r
360EFIAPI\r
361AmlStreamRewind (\r
731c67e1
MK
362 IN AML_STREAM *Stream,\r
363 IN UINT32 Offset\r
7f293b25
SM
364 )\r
365{\r
366 if (!IS_STREAM (Stream) ||\r
731c67e1
MK
367 (Offset == 0))\r
368 {\r
7f293b25
SM
369 ASSERT (0);\r
370 return EFI_INVALID_PARAMETER;\r
371 }\r
372\r
373 if (AmlStreamGetIndex (Stream) < Offset) {\r
374 ASSERT (0);\r
375 return EFI_BUFFER_TOO_SMALL;\r
376 }\r
377\r
378 Stream->Index -= Offset;\r
379\r
380 return EFI_SUCCESS;\r
381}\r
382\r
383/** Reset the Stream (move the current position to the initial position).\r
384\r
385 @param [in] Stream Pointer to a stream.\r
386\r
387 @retval EFI_SUCCESS The function completed successfully.\r
388 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
389**/\r
390EFI_STATUS\r
391EFIAPI\r
392AmlStreamReset (\r
731c67e1 393 IN AML_STREAM *Stream\r
7f293b25
SM
394 )\r
395{\r
396 if (!IS_STREAM (Stream)) {\r
397 ASSERT (0);\r
398 return EFI_INVALID_PARAMETER;\r
399 }\r
400\r
401 Stream->Index = 0;\r
402\r
403 return EFI_SUCCESS;\r
404}\r
405\r
406/** Peek one byte at Stream's current position.\r
407\r
408 Stream's position is not moved when peeking.\r
409\r
410 @param [in] Stream Pointer to a stream.\r
411 The stream must not be at its end.\r
412 @param [out] OutByte Pointer holding the byte value of\r
413 the stream current position.\r
414\r
415 @retval EFI_SUCCESS The function completed successfully.\r
416 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
417 @retval EFI_BUFFER_TOO_SMALL No space left in the buffer.\r
418**/\r
419EFI_STATUS\r
420EFIAPI\r
421AmlStreamPeekByte (\r
731c67e1
MK
422 IN AML_STREAM *Stream,\r
423 OUT UINT8 *OutByte\r
7f293b25
SM
424 )\r
425{\r
731c67e1 426 UINT8 *CurPos;\r
7f293b25
SM
427\r
428 if (!IS_STREAM (Stream) ||\r
429 IS_END_OF_STREAM (Stream) ||\r
731c67e1
MK
430 (OutByte == NULL))\r
431 {\r
7f293b25
SM
432 ASSERT (0);\r
433 return EFI_INVALID_PARAMETER;\r
434 }\r
435\r
436 CurPos = AmlStreamGetCurrPos (Stream);\r
437 if (CurPos == NULL) {\r
438 ASSERT (0);\r
439 return EFI_INVALID_PARAMETER;\r
440 }\r
441\r
442 *OutByte = *CurPos;\r
443 return EFI_SUCCESS;\r
444}\r
445\r
446/** Read one byte at Stream's current position.\r
447\r
448 The stream current position is moved when reading.\r
449\r
450 @param [in] Stream Pointer to a stream.\r
451 The stream must not be at its end.\r
452 @param [out] OutByte Pointer holding the byte value of\r
453 the stream current position.\r
454\r
455 @retval EFI_SUCCESS The function completed successfully.\r
456 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
457 @retval EFI_BUFFER_TOO_SMALL No space left in the buffer.\r
458**/\r
459EFI_STATUS\r
460EFIAPI\r
461AmlStreamReadByte (\r
731c67e1
MK
462 IN AML_STREAM *Stream,\r
463 OUT UINT8 *OutByte\r
7f293b25
SM
464 )\r
465{\r
731c67e1 466 EFI_STATUS Status;\r
7f293b25
SM
467\r
468 if (!IS_STREAM (Stream) ||\r
469 IS_END_OF_STREAM (Stream) ||\r
731c67e1
MK
470 (OutByte == NULL))\r
471 {\r
7f293b25
SM
472 ASSERT (0);\r
473 return EFI_INVALID_PARAMETER;\r
474 }\r
475\r
476 // Stream is checked in the function call.\r
477 Status = AmlStreamPeekByte (Stream, OutByte);\r
478 if (EFI_ERROR (Status)) {\r
479 ASSERT (0);\r
480 return Status;\r
481 }\r
482\r
483 Status = AmlStreamProgress (Stream, 1);\r
484 ASSERT_EFI_ERROR (Status);\r
485 return Status;\r
486}\r
487\r
488/** Write Size bytes in the stream.\r
489\r
490 If the stream goes backward (toward lower addresses), the bytes written\r
491 to the stream are not reverted.\r
492 In the example below, writing "Hello" to the stream will not revert\r
493 the string. The end of the stream buffer will contain "Hello world!".\r
494 Stream buffer:\r
495 +---------------+-----+-----+-----+-----+-----+-----+---- +------+\r
496 | ..... | ' ' | 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' |\r
497 +---------------+-----+-----+-----+-----+-----+-----+---- +------+\r
498 ^\r
499 Current position.\r
500\r
501 @param [in] Stream Pointer to a stream.\r
502 The stream must not be at its end.\r
503 @param [in] Buffer Pointer to the data to write.\r
504 @param [in] Size Number of bytes to write.\r
505\r
506 @retval EFI_SUCCESS The function completed successfully.\r
507 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
508 @retval EFI_BUFFER_TOO_SMALL No space left in the buffer.\r
509**/\r
510EFI_STATUS\r
511EFIAPI\r
512AmlStreamWrite (\r
731c67e1
MK
513 IN AML_STREAM *Stream,\r
514 IN CONST UINT8 *Buffer,\r
515 IN UINT32 Size\r
7f293b25
SM
516 )\r
517{\r
731c67e1 518 UINT8 *CurrPos;\r
7f293b25
SM
519\r
520 if (!IS_STREAM (Stream) ||\r
521 IS_END_OF_STREAM (Stream) ||\r
522 (Buffer == NULL) ||\r
731c67e1
MK
523 (Size == 0))\r
524 {\r
7f293b25
SM
525 ASSERT (0);\r
526 return EFI_INVALID_PARAMETER;\r
527 }\r
528\r
529 if (AmlStreamGetFreeSpace (Stream) < Size) {\r
530 ASSERT (0);\r
531 return EFI_BUFFER_TOO_SMALL;\r
532 }\r
533\r
534 CurrPos = AmlStreamGetCurrPos (Stream);\r
535\r
536 // If the Stream goes backward, prepare some space to copy the data.\r
537 if (IS_STREAM_BACKWARD (Stream)) {\r
538 CurrPos -= Size;\r
539 }\r
540\r
541 CopyMem (CurrPos, Buffer, Size);\r
542 Stream->Index += Size;\r
543\r
544 return EFI_SUCCESS;\r
545}\r
546\r
547/** Compare Size bytes between Stream1 and Stream2 from their\r
548 respective current position.\r
549\r
550 Stream1 and Stream2 must go in the same direction.\r
551 Stream1 and Stream2 are left unchanged.\r
552\r
553 @param [in] Stream1 First stream to compare.\r
554 The stream must not be at its end.\r
555 @param [in] Stream2 Second stream to compare.\r
556 The stream must not be at its end.\r
557 @param [in] Size Number of bytes to compare.\r
558 Must be lower than the minimum remaining space of\r
559 Stream1 and Stream2.\r
560 Must be non-zero.\r
561\r
562 @retval TRUE If Stream1 and Stream2 have Size bytes equal,\r
563 from their respective current position.\r
564 The function completed successfully.\r
565 @retval FALSE Otherwise.\r
566**/\r
567BOOLEAN\r
568EFIAPI\r
569AmlStreamCmp (\r
731c67e1
MK
570 IN CONST AML_STREAM *Stream1,\r
571 IN CONST AML_STREAM *Stream2,\r
572 IN UINT32 Size\r
7f293b25
SM
573 )\r
574{\r
731c67e1
MK
575 UINT32 MinSize;\r
576 UINT8 *CurrPosStream1;\r
577 UINT8 *CurrPosStream2;\r
7f293b25
SM
578\r
579 if (!IS_STREAM (Stream1) ||\r
580 IS_END_OF_STREAM (Stream1) ||\r
581 !IS_STREAM (Stream2) ||\r
582 IS_END_OF_STREAM (Stream2) ||\r
583 (Stream1->Direction != Stream2->Direction) ||\r
731c67e1
MK
584 (Size == 0))\r
585 {\r
7f293b25
SM
586 ASSERT (0);\r
587 return FALSE;\r
588 }\r
589\r
590 // Check the Size is not longer than the remaining size of\r
591 // Stream1 and Stream2.\r
592 MinSize = MIN (\r
593 AmlStreamGetFreeSpace (Stream1),\r
594 AmlStreamGetFreeSpace (Stream2)\r
595 );\r
596 if (MinSize < Size) {\r
597 ASSERT (0);\r
598 return FALSE;\r
599 }\r
600\r
601 CurrPosStream1 = AmlStreamGetCurrPos (Stream1);\r
602 if (CurrPosStream1 == NULL) {\r
603 ASSERT (0);\r
604 return FALSE;\r
605 }\r
731c67e1 606\r
7f293b25
SM
607 CurrPosStream2 = AmlStreamGetCurrPos (Stream2);\r
608 if (CurrPosStream2 == NULL) {\r
609 ASSERT (0);\r
610 return FALSE;\r
611 }\r
612\r
613 if (Stream1->Direction == EAmlStreamDirectionForward) {\r
614 return (0 == CompareMem (CurrPosStream1, CurrPosStream2, MinSize));\r
615 }\r
616\r
617 // The stream is already pointing on the last byte, thus the (-1).\r
618 // +---------------------+\r
619 // BStream | | | | | | | |M|E|T|0|\r
620 // +---------------------+\r
621 // ^\r
622 // CurrPos\r
623 return (0 == CompareMem (\r
731c67e1
MK
624 CurrPosStream1 - (MinSize - 1),\r
625 CurrPosStream2 - (MinSize - 1),\r
626 MinSize\r
627 ));\r
7f293b25
SM
628}\r
629\r
630/** Copy Size bytes of the stream's data to DstBuffer.\r
631\r
632 For a backward stream, the bytes are copied starting from the\r
633 current stream position.\r
634\r
635 @param [out] DstBuffer Destination Buffer to copy the data to.\r
636 @param [in] MaxDstBufferSize Maximum size of DstBuffer.\r
637 Must be non-zero.\r
638 @param [in] Stream Pointer to the stream to copy the data from.\r
639 @param [in] Size Number of bytes to copy from the stream\r
640 buffer.\r
641 Must be lower than MaxDstBufferSize.\r
642 Must be lower than Stream's MaxBufferSize.\r
643 Return success if zero.\r
644\r
645 @retval EFI_SUCCESS The function completed successfully.\r
646 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
647**/\r
648EFI_STATUS\r
649EFIAPI\r
650AmlStreamCpyS (\r
731c67e1
MK
651 OUT CHAR8 *DstBuffer,\r
652 IN UINT32 MaxDstBufferSize,\r
653 IN AML_STREAM *Stream,\r
654 IN UINT32 Size\r
7f293b25
SM
655 )\r
656{\r
731c67e1 657 CHAR8 *StreamBufferStart;\r
7f293b25
SM
658\r
659 // Stream is checked in the function call.\r
660 if ((DstBuffer == NULL) ||\r
661 (MaxDstBufferSize == 0) ||\r
662 (Size > MaxDstBufferSize) ||\r
731c67e1
MK
663 (Size > AmlStreamGetMaxBufferSize (Stream)))\r
664 {\r
7f293b25
SM
665 ASSERT (0);\r
666 return EFI_INVALID_PARAMETER;\r
667 }\r
668\r
669 if (Size == 0) {\r
670 return EFI_SUCCESS;\r
671 }\r
672\r
673 // Find the address at which the data is starting.\r
731c67e1
MK
674 StreamBufferStart = (CHAR8 *)(IS_STREAM_FORWARD (Stream) ?\r
675 Stream->Buffer :\r
676 AmlStreamGetCurrPos (Stream));\r
7f293b25
SM
677\r
678 CopyMem (DstBuffer, StreamBufferStart, Size);\r
679\r
680 return EFI_SUCCESS;\r
681}\r