TianoCore EDK2 master
Loading...
Searching...
No Matches
AmlStream.c
Go to the documentation of this file.
1
9#include <Stream/AmlStream.h>
10
24EFIAPI
26 IN OUT AML_STREAM *Stream,
27 IN UINT8 *Buffer,
28 IN UINT32 MaxBufferSize,
30 )
31{
32 if ((Stream == NULL) ||
33 (Buffer == NULL) ||
34 (MaxBufferSize == 0) ||
35 ((Direction != EAmlStreamDirectionForward) &&
36 (Direction != EAmlStreamDirectionBackward)))
37 {
38 ASSERT (0);
39 return EFI_INVALID_PARAMETER;
40 }
41
42 Stream->Buffer = Buffer;
43 Stream->MaxBufferSize = MaxBufferSize;
44 Stream->Index = 0;
45 Stream->Direction = Direction;
46
47 return EFI_SUCCESS;
48}
49
62EFIAPI
64 IN CONST AML_STREAM *Stream,
65 OUT AML_STREAM *ClonedStream
66 )
67{
68 if (!IS_STREAM (Stream) ||
69 (ClonedStream == NULL))
70 {
71 ASSERT (0);
72 return EFI_INVALID_PARAMETER;
73 }
74
75 ClonedStream->Buffer = Stream->Buffer;
76 ClonedStream->MaxBufferSize = Stream->MaxBufferSize;
77 ClonedStream->Index = Stream->Index;
78 ClonedStream->Direction = Stream->Direction;
79
80 return EFI_SUCCESS;
81}
82
108EFIAPI
110 IN CONST AML_STREAM *Stream,
111 OUT AML_STREAM *SubStream
112 )
113{
114 if (!IS_STREAM (Stream) ||
115 (SubStream == NULL))
116 {
117 ASSERT (0);
118 return EFI_INVALID_PARAMETER;
119 }
120
121 if (IS_STREAM_FORWARD (Stream)) {
122 SubStream->Buffer = AmlStreamGetCurrPos (Stream);
123 } else if (IS_STREAM_BACKWARD (Stream)) {
124 SubStream->Buffer = Stream->Buffer;
125 } else {
126 ASSERT (0);
127 return EFI_INVALID_PARAMETER;
128 }
129
130 SubStream->MaxBufferSize = AmlStreamGetFreeSpace (Stream);
131 SubStream->Index = 0;
132 SubStream->Direction = Stream->Direction;
133
134 return EFI_SUCCESS;
135}
136
144UINT8 *
145EFIAPI
147 IN CONST AML_STREAM *Stream
148 )
149{
150 if (!IS_STREAM (Stream)) {
151 ASSERT (0);
152 return NULL;
153 }
154
155 return Stream->Buffer;
156}
157
165UINT32
166EFIAPI
168 IN CONST AML_STREAM *Stream
169 )
170{
171 if (!IS_STREAM (Stream)) {
172 ASSERT (0);
173 return 0;
174 }
175
176 return Stream->MaxBufferSize;
177}
178
189EFIAPI
191 IN AML_STREAM *Stream,
192 IN UINT32 Diff
193 )
194{
195 if (!IS_STREAM (Stream) ||
196 (Diff == 0) ||
197 ((Stream->MaxBufferSize - Diff) <= Stream->Index))
198 {
199 ASSERT (0);
200 return EFI_INVALID_PARAMETER;
201 }
202
203 Stream->MaxBufferSize -= Diff;
204 return EFI_SUCCESS;
205}
206
220UINT32
221EFIAPI
223 IN CONST AML_STREAM *Stream
224 )
225{
226 if (!IS_STREAM (Stream)) {
227 ASSERT (0);
228 return 0;
229 }
230
231 return Stream->Index;
232}
233
242EFIAPI
244 IN CONST AML_STREAM *Stream
245 )
246{
247 if (!IS_STREAM (Stream)) {
248 ASSERT (0);
250 }
251
252 return Stream->Direction;
253}
254
262UINT8 *
263EFIAPI
265 IN CONST AML_STREAM *Stream
266 )
267{
268 if (!IS_STREAM (Stream)) {
269 ASSERT (0);
270 return NULL;
271 }
272
273 if (IS_STREAM_FORWARD (Stream)) {
274 return Stream->Buffer + Stream->Index;
275 } else if (IS_STREAM_BACKWARD (Stream)) {
276 return Stream->Buffer + (Stream->MaxBufferSize - 1) - Stream->Index;
277 } else {
278 ASSERT (0);
279 return NULL;
280 }
281}
282
290UINT32
291EFIAPI
293 IN CONST AML_STREAM *Stream
294 )
295{
296 if (!IS_STREAM (Stream)) {
297 ASSERT (0);
298 return 0;
299 }
300
301 if (Stream->Index > Stream->MaxBufferSize) {
302 ASSERT (0);
303 return 0;
304 }
305
306 return Stream->MaxBufferSize - Stream->Index;
307}
308
323EFIAPI
325 IN AML_STREAM *Stream,
326 IN UINT32 Offset
327 )
328{
329 if (!IS_STREAM (Stream) ||
330 IS_END_OF_STREAM (Stream) ||
331 (Offset == 0))
332 {
333 ASSERT (0);
334 return EFI_INVALID_PARAMETER;
335 }
336
337 if (AmlStreamGetFreeSpace (Stream) < Offset) {
338 ASSERT (0);
339 return EFI_BUFFER_TOO_SMALL;
340 }
341
342 Stream->Index += Offset;
343
344 return EFI_SUCCESS;
345}
346
360EFIAPI
362 IN AML_STREAM *Stream,
363 IN UINT32 Offset
364 )
365{
366 if (!IS_STREAM (Stream) ||
367 (Offset == 0))
368 {
369 ASSERT (0);
370 return EFI_INVALID_PARAMETER;
371 }
372
373 if (AmlStreamGetIndex (Stream) < Offset) {
374 ASSERT (0);
375 return EFI_BUFFER_TOO_SMALL;
376 }
377
378 Stream->Index -= Offset;
379
380 return EFI_SUCCESS;
381}
382
391EFIAPI
393 IN AML_STREAM *Stream
394 )
395{
396 if (!IS_STREAM (Stream)) {
397 ASSERT (0);
398 return EFI_INVALID_PARAMETER;
399 }
400
401 Stream->Index = 0;
402
403 return EFI_SUCCESS;
404}
405
420EFIAPI
422 IN AML_STREAM *Stream,
423 OUT UINT8 *OutByte
424 )
425{
426 UINT8 *CurPos;
427
428 if (!IS_STREAM (Stream) ||
429 IS_END_OF_STREAM (Stream) ||
430 (OutByte == NULL))
431 {
432 ASSERT (0);
433 return EFI_INVALID_PARAMETER;
434 }
435
436 CurPos = AmlStreamGetCurrPos (Stream);
437 if (CurPos == NULL) {
438 ASSERT (0);
439 return EFI_INVALID_PARAMETER;
440 }
441
442 *OutByte = *CurPos;
443 return EFI_SUCCESS;
444}
445
460EFIAPI
462 IN AML_STREAM *Stream,
463 OUT UINT8 *OutByte
464 )
465{
466 EFI_STATUS Status;
467
468 if (!IS_STREAM (Stream) ||
469 IS_END_OF_STREAM (Stream) ||
470 (OutByte == NULL))
471 {
472 ASSERT (0);
473 return EFI_INVALID_PARAMETER;
474 }
475
476 // Stream is checked in the function call.
477 Status = AmlStreamPeekByte (Stream, OutByte);
478 if (EFI_ERROR (Status)) {
479 ASSERT (0);
480 return Status;
481 }
482
483 Status = AmlStreamProgress (Stream, 1);
484 ASSERT_EFI_ERROR (Status);
485 return Status;
486}
487
511EFIAPI
513 IN AML_STREAM *Stream,
514 IN CONST UINT8 *Buffer,
515 IN UINT32 Size
516 )
517{
518 UINT8 *CurrPos;
519
520 if (!IS_STREAM (Stream) ||
521 IS_END_OF_STREAM (Stream) ||
522 (Buffer == NULL) ||
523 (Size == 0))
524 {
525 ASSERT (0);
526 return EFI_INVALID_PARAMETER;
527 }
528
529 if (AmlStreamGetFreeSpace (Stream) < Size) {
530 ASSERT (0);
531 return EFI_BUFFER_TOO_SMALL;
532 }
533
534 CurrPos = AmlStreamGetCurrPos (Stream);
535
536 // If the Stream goes backward, prepare some space to copy the data.
537 if (IS_STREAM_BACKWARD (Stream)) {
538 CurrPos -= Size;
539 }
540
541 CopyMem (CurrPos, Buffer, Size);
542 Stream->Index += Size;
543
544 return EFI_SUCCESS;
545}
546
567BOOLEAN
568EFIAPI
570 IN CONST AML_STREAM *Stream1,
571 IN CONST AML_STREAM *Stream2,
572 IN UINT32 Size
573 )
574{
575 UINT32 MinSize;
576 UINT8 *CurrPosStream1;
577 UINT8 *CurrPosStream2;
578
579 if (!IS_STREAM (Stream1) ||
580 IS_END_OF_STREAM (Stream1) ||
581 !IS_STREAM (Stream2) ||
582 IS_END_OF_STREAM (Stream2) ||
583 (Stream1->Direction != Stream2->Direction) ||
584 (Size == 0))
585 {
586 ASSERT (0);
587 return FALSE;
588 }
589
590 // Check the Size is not longer than the remaining size of
591 // Stream1 and Stream2.
592 MinSize = MIN (
593 AmlStreamGetFreeSpace (Stream1),
594 AmlStreamGetFreeSpace (Stream2)
595 );
596 if (MinSize < Size) {
597 ASSERT (0);
598 return FALSE;
599 }
600
601 CurrPosStream1 = AmlStreamGetCurrPos (Stream1);
602 if (CurrPosStream1 == NULL) {
603 ASSERT (0);
604 return FALSE;
605 }
606
607 CurrPosStream2 = AmlStreamGetCurrPos (Stream2);
608 if (CurrPosStream2 == NULL) {
609 ASSERT (0);
610 return FALSE;
611 }
612
613 if (Stream1->Direction == EAmlStreamDirectionForward) {
614 return (0 == CompareMem (CurrPosStream1, CurrPosStream2, MinSize));
615 }
616
617 // The stream is already pointing on the last byte, thus the (-1).
618 // +---------------------+
619 // BStream | | | | | | | |M|E|T|0|
620 // +---------------------+
621 // ^
622 // CurrPos
623 return (0 == CompareMem (
624 CurrPosStream1 - (MinSize - 1),
625 CurrPosStream2 - (MinSize - 1),
626 MinSize
627 ));
628}
629
649EFIAPI
651 OUT CHAR8 *DstBuffer,
652 IN UINT32 MaxDstBufferSize,
653 IN AML_STREAM *Stream,
654 IN UINT32 Size
655 )
656{
657 CHAR8 *StreamBufferStart;
658
659 // Stream is checked in the function call.
660 if ((DstBuffer == NULL) ||
661 (MaxDstBufferSize == 0) ||
662 (Size > MaxDstBufferSize) ||
663 (Size > AmlStreamGetMaxBufferSize (Stream)))
664 {
665 ASSERT (0);
666 return EFI_INVALID_PARAMETER;
667 }
668
669 if (Size == 0) {
670 return EFI_SUCCESS;
671 }
672
673 // Find the address at which the data is starting.
674 StreamBufferStart = (CHAR8 *)(IS_STREAM_FORWARD (Stream) ?
675 Stream->Buffer :
676 AmlStreamGetCurrPos (Stream));
677
678 CopyMem (DstBuffer, StreamBufferStart, Size);
679
680 return EFI_SUCCESS;
681}
EFI_STATUS EFIAPI AmlStreamReset(IN AML_STREAM *Stream)
Definition: AmlStream.c:392
UINT32 EFIAPI AmlStreamGetMaxBufferSize(IN CONST AML_STREAM *Stream)
Definition: AmlStream.c:167
UINT32 EFIAPI AmlStreamGetIndex(IN CONST AML_STREAM *Stream)
Definition: AmlStream.c:222
EFI_STATUS EFIAPI AmlStreamReadByte(IN AML_STREAM *Stream, OUT UINT8 *OutByte)
Definition: AmlStream.c:461
EAML_STREAM_DIRECTION EFIAPI AmlStreamGetDirection(IN CONST AML_STREAM *Stream)
Definition: AmlStream.c:243
EFI_STATUS EFIAPI AmlStreamPeekByte(IN AML_STREAM *Stream, OUT UINT8 *OutByte)
Definition: AmlStream.c:421
UINT32 EFIAPI AmlStreamGetFreeSpace(IN CONST AML_STREAM *Stream)
Definition: AmlStream.c:292
EFI_STATUS EFIAPI AmlStreamCpyS(OUT CHAR8 *DstBuffer, IN UINT32 MaxDstBufferSize, IN AML_STREAM *Stream, IN UINT32 Size)
Definition: AmlStream.c:650
EFI_STATUS EFIAPI AmlStreamRewind(IN AML_STREAM *Stream, IN UINT32 Offset)
Definition: AmlStream.c:361
EFI_STATUS EFIAPI AmlStreamProgress(IN AML_STREAM *Stream, IN UINT32 Offset)
Definition: AmlStream.c:324
EFI_STATUS EFIAPI AmlStreamClone(IN CONST AML_STREAM *Stream, OUT AML_STREAM *ClonedStream)
Definition: AmlStream.c:63
EFI_STATUS EFIAPI AmlStreamWrite(IN AML_STREAM *Stream, IN CONST UINT8 *Buffer, IN UINT32 Size)
Definition: AmlStream.c:512
UINT8 *EFIAPI AmlStreamGetCurrPos(IN CONST AML_STREAM *Stream)
Definition: AmlStream.c:264
UINT8 *EFIAPI AmlStreamGetBuffer(IN CONST AML_STREAM *Stream)
Definition: AmlStream.c:146
EFI_STATUS EFIAPI AmlStreamReduceMaxBufferSize(IN AML_STREAM *Stream, IN UINT32 Diff)
Definition: AmlStream.c:190
EFI_STATUS EFIAPI AmlStreamInitSubStream(IN CONST AML_STREAM *Stream, OUT AML_STREAM *SubStream)
Definition: AmlStream.c:109
BOOLEAN EFIAPI AmlStreamCmp(IN CONST AML_STREAM *Stream1, IN CONST AML_STREAM *Stream2, IN UINT32 Size)
Definition: AmlStream.c:569
EFI_STATUS EFIAPI AmlStreamInit(IN OUT AML_STREAM *Stream, IN UINT8 *Buffer, IN UINT32 MaxBufferSize, IN EAML_STREAM_DIRECTION Direction)
Definition: AmlStream.c:25
enum EAmlStreamDirection EAML_STREAM_DIRECTION
#define IS_STREAM_FORWARD(Stream)
Definition: AmlStream.h:91
@ EAmlStreamDirectionForward
Definition: AmlStream.h:20
@ EAmlStreamDirectionBackward
Definition: AmlStream.h:22
@ EAmlStreamDirectionInvalid
Invalid AML Stream direction.
Definition: AmlStream.h:19
#define IS_STREAM(Stream)
Definition: AmlStream.h:69
#define IS_STREAM_BACKWARD(Stream)
Definition: AmlStream.h:101
#define IS_END_OF_STREAM(Stream)
Definition: AmlStream.h:80
INTN EFIAPI CompareMem(IN CONST VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define MIN(a, b)
Definition: Base.h:1007
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112