summaryrefslogtreecommitdiff
path: root/tools/src/minilzlib/inputbuf.c
blob: 67d652ce6b60a7153be4f6a4099e3246513ee088 (plain)
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
/*++

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
    //
    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,
    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;
}

uint32_t
BfTell (
    void
    )
{
    return In.Offset;
}

bool
BfRead (
    uint8_t* Byte
    )
{
    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 (
    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;
}