123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- /*++
- Copyright (c) Alex Ionescu. All rights reserved.
- Module Name:
- inputbuf.c
- Abstract:
- This module implements helper functions for managing the input buffer that
- contains arithmetic-coded LZ77 match distance-length pairs and raw literals
- Both seeking (such that an external reader can refer to multiple bytes) and
- reading (capturing) an individual byte are supported. Support for aligning
- input data to 4 bytes (which is a requirement for XZ-encoded files) is also
- implemented.
- Author:
- Alex Ionescu (@aionescu) 15-Apr-2020 - Initial version
- Environment:
- Windows & Linux, user mode and kernel mode.
- --*/
- #include "minlzlib.h"
- //
- // Input Buffer State
- //
- typedef struct _BUFFER_STATE
- {
- //
- // Start of the buffer, current offset, current packet end, and total input size
- //
- const uint8_t* Buffer;
- uint32_t Offset;
- uint32_t SoftLimit;
- uint32_t Size;
- } BUFFER_STATE, * PBUFFER_STATE;
- BUFFER_STATE In;
- bool
- BfAlign (
- void
- )
- {
- uint8_t padByte;
- //
- // Keep reading until we reach 32-bit alignment. All bytes must be zero.
- //
- while (In.Offset & 3)
- {
- if (!BfRead(&padByte) || (padByte != 0))
- {
- return false;
- }
- }
- return true;
- }
- bool
- BfSetSoftLimit (
- uint32_t Remaining
- )
- {
- if ((In.Size - In.Offset) < Remaining)
- {
- return false;
- }
- In.SoftLimit = In.Offset + Remaining;
- return true;
- }
- void
- BfResetSoftLimit (
- void
- )
- {
- In.SoftLimit = In.Size;
- }
- bool
- BfSeek (
- uint32_t Length,
- const uint8_t** Bytes
- )
- {
- //
- // Make sure the input buffer has enough space to seek the desired size, if
- // it does, return the current position and then seek past the desired size
- //
- if ((In.Offset + Length) > In.SoftLimit)
- {
- *Bytes = 0;
- return false;
- }
- *Bytes = &In.Buffer[In.Offset];
- In.Offset += Length;
- return true;
- }
- bool
- BfRead (
- uint8_t* Byte
- )
- {
- const uint8_t* pByte;
- //
- // Seek past the byte and read it
- //
- if (!BfSeek(sizeof(*Byte), &pByte))
- {
- *Byte = 0;
- return false;
- }
- *Byte = *pByte;
- return true;
- }
- void
- BfInitialize (
- const uint8_t* InputBuffer,
- uint32_t InputSize
- )
- {
- //
- // Save all the data in the context buffer state
- //
- In.Buffer = InputBuffer;
- In.Size = InputSize;
- In.SoftLimit = InputSize;
- In.Offset = 0;
- }
|