summaryrefslogtreecommitdiffstats
path: root/libs/Compress/decompress.c
blob: 286e3a023180f85cc50a78b5864f7807739027a1 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
////////////////////////////////////////////////////////////////////////////////
///
/// @file       decompress.c
///
/// @project
///
/// @brief      Decompression Routines
///
////////////////////////////////////////////////////////////////////////////////
///
////////////////////////////////////////////////////////////////////////////////
///
/// @copyright Copyright (c) 2019, Evan Lojewski
/// @cond
///
/// All rights reserved.
///
/// Redistribution and use in source and binary forms, with or without
/// modification, are permitted provided that the following conditions are met:
/// 1. Redistributions of source code must retain the above copyright notice,
/// this list of conditions and the following disclaimer.
/// 2. Redistributions in binary form must reproduce the above copyright notice,
/// this list of conditions and the following disclaimer in the documentation
/// and/or other materials provided with the distribution.
/// 3. Neither the name of the copyright holder nor the
/// names of its contributors may be used to endorse or promote products
/// derived from this software without specific prior written permission.
///
////////////////////////////////////////////////////////////////////////////////
///
/// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
/// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
/// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
/// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
/// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
/// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
/// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
/// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
/// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
/// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
/// POSSIBILITY OF SUCH DAMAGE.
/// @endcond
////////////////////////////////////////////////////////////////////////////////

#include <Compress.h>

#include <stdio.h>

#define DICTIONARY_INIT_0x20   (0x20)
#define DICTIONARY_INIT_0x00   (0x00)
#define DICTIONARY_INIT_INDEX  (2014)
#define DICTIONARY_SIZE        (2048)

#define LITERAL_TYPE    (1)
#define REFERENCE_TYPE  (0)

static struct {
    uint8_t dictionary[DICTIONARY_SIZE];
    uint32_t cursor;
    const uint8_t* inBuffer;
    int32_t inBytes;

    uint8_t* outBuffer;
    int32_t outRemaining;
    int32_t outSent;
} g_DecompressorState;


static void state_init(const uint8_t* inBuffer, int32_t inBytes)
{
    g_DecompressorState.cursor = DICTIONARY_INIT_INDEX;
    int i = 0;
    for(; i < g_DecompressorState.cursor; i++)
    {
        g_DecompressorState.dictionary[i] = DICTIONARY_INIT_0x20;
    }

    for(; i < DICTIONARY_SIZE; i++)
    {
        g_DecompressorState.dictionary[i] = DICTIONARY_INIT_0x00;
    }

    g_DecompressorState.inBuffer = inBuffer;
    g_DecompressorState.inBytes = inBytes;
}

static void state_insert(uint8_t byte)
{
    g_DecompressorState.dictionary[g_DecompressorState.cursor] = byte;
    // Increment and wrap.
    g_DecompressorState.cursor = (g_DecompressorState.cursor + 1) % DICTIONARY_SIZE;
}

static uint8_t state_get_dictionary(uint16_t offset)
{
    offset = offset % DICTIONARY_SIZE;
    return g_DecompressorState.dictionary[offset];
}

static uint8_t state_get_byte(void)
{
    // uint8_t bytesLeft = g_DecompressorState.inBytes;
    uint8_t byte = 0;
    // if(bytesLeft > 0)
    // {
        byte = *g_DecompressorState.inBuffer;
        g_DecompressorState.inBuffer++;
        g_DecompressorState.inBytes--;
    // }

    return byte;
}

int32_t state_bytes_left(void)
{
    return g_DecompressorState.inBytes;
}

int32_t decompress(uint8_t* outBuffer, int32_t outBytes,
                   const uint8_t* inBuffer,  int32_t inBytes)
{
    int32_t actualSize = 0;
    state_init(inBuffer, inBytes);

    while(state_bytes_left() > 0)
    {
        uint8_t control = state_get_byte();
        for(int i = 0; i < 8; i++)
        {
            if(actualSize >= outBytes || !state_bytes_left())
            {
                // We have no bytes left, or we've filled up the output buffer
                break;
            }

            if((control & (1 << i)) == REFERENCE_TYPE)
            {
                // Read in two reference bytes
                uint8_t B0 = state_get_byte();
                uint8_t B1 = state_get_byte();

                uint16_t offset = (((uint16_t)B1 & 0xE0u) << 3u) | B0;
                uint16_t length = (B1 & 0x1Fu) + 3u;

                while(length && actualSize < outBytes)
                {
                    uint8_t literal = state_get_dictionary(offset);
                    state_insert(literal);

                    offset++;
                    length--;
                    outBuffer[actualSize++] = literal;
                }
            }
            else /* LITERAL_TYPE */
            {
                uint8_t literal = state_get_byte();;
                state_insert(literal);

                // Output
                outBuffer[actualSize++] = literal;

            }
        }
    }

    // printf("inBytes: %d (%d left), outBytes: %d, actualSize: %d\n", inBytes, state_bytes_left(), outBytes, actualSize);
    // while(1);

    return actualSize;
}
OpenPOWER on IntegriCloud