Inflater.cpp 5.52 KB
Newer Older
daFischer's avatar
first  
daFischer committed
1 2
/* 
 * File:   Inflater.cpp
3
 * Author: Johannes Fischer
daFischer's avatar
first  
daFischer committed
4 5
 * 
 * Created on May 8, 2014, 12:46 PM
6 7
 * 
 * Credits to http://zlib.net/zlib_how.html
daFischer's avatar
first  
daFischer committed
8 9 10 11
 */

#include "Inflater.h"

daFischer's avatar
daFischer committed
12
Inflater::Inflater(FILE* f)
daFischer's avatar
first  
daFischer committed
13
{
daFischer's avatar
daFischer committed
14
    source=f;
daFischer's avatar
first  
daFischer committed
15 16 17 18
    outOffset=0;
    
    // allocate inflate state
    strm.zfree = Z_NULL;
19
    strm.zalloc = Z_NULL;
daFischer's avatar
first  
daFischer committed
20 21 22 23 24 25
    strm.opaque = Z_NULL;
    strm.avail_in = 0;
    strm.next_in = Z_NULL;
    ret = inflateInit(&strm);
    if (ret != Z_OK)
    {
26
        printf("There has been an error in Inflater (1)\n");
daFischer's avatar
first  
daFischer committed
27 28 29
        return;
    }
    
30 31
    //Read CHUNK bytes from the file
    strm.avail_in = fread(in, 1, CHUNK, source);
daFischer's avatar
daFischer committed
32
    if (ferror(source)) {
daFischer's avatar
first  
daFischer committed
33 34 35 36 37 38 39 40
        (void)inflateEnd(&strm);
        ret = Z_ERRNO;
    }
    
    if (strm.avail_in == 0)
        ret = Z_ERRNO;
    if (ret != Z_OK)
    {
41 42
        if(VERBOSE)
            printf("There has been an error in Inflater (2)\n");
daFischer's avatar
first  
daFischer committed
43 44 45 46 47
        return;
    }
    
    strm.next_in = in;
    
48
    strm.avail_out = CHUNK;
daFischer's avatar
first  
daFischer committed
49 50
    strm.next_out = out;
    
51
    //Inflate the read chunk and write to the char Array out
daFischer's avatar
first  
daFischer committed
52
    ret = inflate(&strm, Z_NO_FLUSH);
53 54 55
    
    //Test for any problem that might have appeared
    assert(ret != Z_STREAM_ERROR);
daFischer's avatar
first  
daFischer committed
56 57
    switch (ret) {
        case Z_NEED_DICT:
58
            ret = Z_DATA_ERROR;
daFischer's avatar
first  
daFischer committed
59 60 61
        case Z_DATA_ERROR:
        case Z_MEM_ERROR:
            (void)inflateEnd(&strm);
62
                printf("There has been an error in Inflater (3)\n");
daFischer's avatar
first  
daFischer committed
63 64 65 66 67 68 69 70 71
            return;
    }
}

Inflater::~Inflater()
{
    //clean up and return
    (void)inflateEnd(&strm);
    ret = Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
72 73
    if(VERBOSE)
        printf("Inflater Endresult: %d\n",ret);
74
    fclose(source);
daFischer's avatar
first  
daFischer committed
75 76
}

77 78 79 80 81
/**
 * inflates and reads one Char from the File
 * @param Byte The char to be filled by reading
 * @return returns Z_OK if no problem has occurred
 */
daFischer's avatar
first  
daFischer committed
82 83
bool Inflater::readByte(char* Byte)
{
daFischer's avatar
daFischer committed
84 85 86 87 88 89
    if(addedChars.size()>0)
    {
        *Byte=addedChars[0];
        addedChars=addedChars.substr(1);
        return ret;
    }
daFischer's avatar
first  
daFischer committed
90 91
    if (ret != Z_OK)
    {
92
        //There has been an error, but the function is still being called. To avoid random results we 'read' 0.
93
        *Byte=0;
daFischer's avatar
first  
daFischer committed
94 95
        return Z_ERRNO;
    }
96 97
    //If all chars from the inflated chunk in the char array have been read, out has to be refilled
    while(outOffset >= CHUNK - strm.avail_out)
daFischer's avatar
first  
daFischer committed
98
    {
99
        //If everything has been inflated already, nothing can be done anymore; return.
daFischer's avatar
first  
daFischer committed
100 101
        if (ret == Z_STREAM_END)
            return ret;
102 103 104
        
        //Check whether Array in has to be refilled
        if(strm.avail_out != 0)
daFischer's avatar
first  
daFischer committed
105
        {
106
            //Fill Array in with CHUNK bytes from the file
daFischer's avatar
daFischer committed
107 108
            strm.avail_in = fread(in, 1, CHUNK, source);
            if (ferror(source)) {
daFischer's avatar
first  
daFischer committed
109 110 111 112 113 114 115 116 117 118 119
                (void)inflateEnd(&strm);
                return ret = Z_ERRNO;
            }
            if (strm.avail_in == 0)
                return ret = Z_ERRNO;
            strm.next_in = in;
        }

        strm.avail_out = CHUNK;
        strm.next_out = out;

120
        //Inflate a part of the char array in and write to the char array out
daFischer's avatar
first  
daFischer committed
121
        ret = inflate(&strm, Z_NO_FLUSH);
122 123
        
        //Test for any problem that might have appeared
daFischer's avatar
first  
daFischer committed
124
        if(ret == Z_STREAM_ERROR)
125
            printf("There has been an error in Inflater (4)\n");
daFischer's avatar
first  
daFischer committed
126 127 128 129 130 131 132 133 134 135 136 137 138
        assert(ret != Z_STREAM_ERROR);  //state not clobbered
        switch (ret) {
            case Z_NEED_DICT:
                ret = Z_DATA_ERROR;     //and fall through
            case Z_DATA_ERROR:
            case Z_MEM_ERROR:
                (void)inflateEnd(&strm);
                return ret;
        }
        outOffset=0;
    }
    *Byte = out[outOffset];
    outOffset++;
139
    
daFischer's avatar
first  
daFischer committed
140 141 142
    return ret;
}

143 144 145 146 147
/**
 * Fills a SizedArray with as many Chars as memory has been reserved
 * The SizedArray has to be initialized beforehand
 * @param sArray Pointer to the SizedArray
 * @return returns Z_OK if successful
daFischer's avatar
daFischer committed
148
 */
daFischer's avatar
first  
daFischer committed
149 150 151 152 153 154 155 156 157 158 159 160
bool Inflater::readSizedArray(SizedArray* sArray)
{
    bool r;
    for(int i=0; i<sArray->length; i++)
    {
        r=readByte(sArray->array+i);
        if(r!=Z_OK)
            return r;
    }
    return Z_OK;
}

161 162 163 164 165
/**
 * mallocs and fills a char array
 * @param length how many bytes have to be read
 * @param end if this is true, the char array ends with \0 (malloced bytes are [length]+1 in that case)
 * @return Pointer to the char Array
daFischer's avatar
daFischer committed
166 167
 */
char* Inflater::readCharArray(int length, bool end)
daFischer's avatar
first  
daFischer committed
168 169
{
    bool r;
daFischer's avatar
daFischer committed
170
    char* byteArray=(char*)malloc(length+ (end ? 1 : 0));
daFischer's avatar
first  
daFischer committed
171 172
    for(int i=0;i<length;i++)
    {
daFischer's avatar
daFischer committed
173
        r=readByte(&(byteArray[i]));
daFischer's avatar
first  
daFischer committed
174
        if(r!=Z_OK)
175 176 177
        {
            printf("Inflater error: %x\n",r);
            free(byteArray);
daFischer's avatar
daFischer committed
178
            return NULL;
179
        }
daFischer's avatar
first  
daFischer committed
180
    }
daFischer's avatar
daFischer committed
181 182 183
    if(end)
        byteArray[length]=0;
    return byteArray;
daFischer's avatar
first  
daFischer committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
}

bool Inflater::readShort(short* s)
{
    bool r;
    char* pointer=(char*)s;
    for(int i=1; i>=0; i--)
    {
        r=readByte(&(pointer[i]));
        if(r!=Z_OK)
            return r;
    }
    return Z_OK;
}

bool Inflater::readInt(int* s)
{
    bool r;
    char* pointer=(char*)s;
    for(int i=3; i>=0; i--)
    {
        r=readByte(&(pointer[i]));
        if(r!=Z_OK)
            return r;
    }
    return Z_OK;
}

daFischer's avatar
daFischer committed
212 213 214 215 216 217 218
bool Inflater::skipBytes(int number) {
    bool r;
    char waste;
    for(int i=0;i<number;i++)
    {
        r=readByte(&waste);
        if(r!=Z_OK)
219
        {
daFischer's avatar
daFischer committed
220
            return r;
221
        }
daFischer's avatar
daFischer committed
222
    }
223
    return r;
daFischer's avatar
daFischer committed
224 225
}

226 227 228 229
/**
 * If too many chars have been read, this can be used to give them back.
 * @param c
 */
daFischer's avatar
daFischer committed
230 231 232 233 234 235 236
void Inflater::addChar(char c) {
    addedChars+=c;
}

long int Inflater::getProgress() {
    return ftell(source);
}
daFischer's avatar
daFischer committed
237

daFischer's avatar
first  
daFischer committed
238 239 240
bool Inflater::endOfFile(){
    return ret!=Z_OK;
}