internal int Inflate(ZlibCodec z, int f)
{
int r;
int b;
if (z == null)
throw new ZlibException ("Codec is null. ");
if (z.istate == null)
throw new ZlibException ("InflateManager is null. ");
if (z.InputBuffer == null)
throw new ZlibException ("InputBuffer is null. ");
//return ZlibConstants.Z_STREAM_ERROR;
f = (f == ZlibConstants.Z_FINISH)
? ZlibConstants.Z_BUF_ERROR
: ZlibConstants.Z_OK;
r = ZlibConstants.Z_BUF_ERROR;
while (true) {
switch (z.istate.mode) {
case METHOD:
if (z.AvailableBytesIn == 0)
return r;
r = f;
z.AvailableBytesIn--;
z.TotalBytesIn++;
if (((z.istate.method = z.InputBuffer [z.NextIn++]) & 0xf) != Z_DEFLATED) {
z.istate.mode = BAD;
z.Message = String.Format ("unknown compression method (0x{0:X2})", z.istate.method);
z.istate.marker = 5; // can't try inflateSync
break;
}
if ((z.istate.method >> 4) + 8 > z.istate.wbits) {
z.istate.mode = BAD;
z.Message = String.Format ("invalid window size ({0})", (z.istate.method >> 4) + 8);
z.istate.marker = 5; // can't try inflateSync
break;
}
z.istate.mode = FLAG;
goto case FLAG;
case FLAG:
if (z.AvailableBytesIn == 0)
return r;
r = f;
z.AvailableBytesIn--;
z.TotalBytesIn++;
b = (z.InputBuffer [z.NextIn++]) & 0xff;
if ((((z.istate.method << 8) + b) % 31) != 0) {
z.istate.mode = BAD;
z.Message = "incorrect header check";
z.istate.marker = 5; // can't try inflateSync
break;
}
if ((b & PRESET_DICT) == 0) {
z.istate.mode = BLOCKS;
break;
}
z.istate.mode = DICT4;
goto case DICT4;
case DICT4:
if (z.AvailableBytesIn == 0)
return r;
r = f;
z.AvailableBytesIn--;
z.TotalBytesIn++;
z.istate.need = ((z.InputBuffer [z.NextIn++] & 0xff) << 24) & unchecked((int)0xff000000L);
z.istate.mode = DICT3;
goto case DICT3;
case DICT3:
if (z.AvailableBytesIn == 0)
return r;
r = f;
z.AvailableBytesIn--;
z.TotalBytesIn++;
z.istate.need += (((z.InputBuffer [z.NextIn++] & 0xff) << 16) & 0xff0000L);
z.istate.mode = DICT2;
goto case DICT2;
case DICT2:
if (z.AvailableBytesIn == 0)
return r;
r = f;
z.AvailableBytesIn--;
z.TotalBytesIn++;
z.istate.need += (((z.InputBuffer [z.NextIn++] & 0xff) << 8) & 0xff00L);
z.istate.mode = DICT1;
goto case DICT1;
case DICT1:
if (z.AvailableBytesIn == 0)
return r;
r = f;
z.AvailableBytesIn--;
z.TotalBytesIn++;
z.istate.need += (z.InputBuffer [z.NextIn++] & 0xffL);
z._Adler32 = z.istate.need;
z.istate.mode = DICT0;
return ZlibConstants.Z_NEED_DICT;
case DICT0:
z.istate.mode = BAD;
z.Message = "need dictionary";
z.istate.marker = 0; // can try inflateSync
return ZlibConstants.Z_STREAM_ERROR;
case BLOCKS:
r = z.istate.blocks.Process (z, r);
if (r == ZlibConstants.Z_DATA_ERROR) {
z.istate.mode = BAD;
z.istate.marker = 0; // can try inflateSync
break;
}
if (r == ZlibConstants.Z_OK)
r = f;
if (r != ZlibConstants.Z_STREAM_END)
return r;
r = f;
z.istate.blocks.Reset (z, z.istate.was);
if (!z.istate.HandleRfc1950HeaderBytes) {
z.istate.mode = DONE;
break;
}
z.istate.mode = CHECK4;
goto case CHECK4;
case CHECK4:
if (z.AvailableBytesIn == 0)
return r;
r = f;
z.AvailableBytesIn--;
z.TotalBytesIn++;
z.istate.need = ((z.InputBuffer [z.NextIn++] & 0xff) << 24) & unchecked((int)0xff000000L);
z.istate.mode = CHECK3;
goto case CHECK3;
case CHECK3:
if (z.AvailableBytesIn == 0)
return r;
r = f;
z.AvailableBytesIn--;
z.TotalBytesIn++;
z.istate.need += (((z.InputBuffer [z.NextIn++] & 0xff) << 16) & 0xff0000L);
z.istate.mode = CHECK2;
goto case CHECK2;
case CHECK2:
if (z.AvailableBytesIn == 0)
return r;
r = f;
z.AvailableBytesIn--;
z.TotalBytesIn++;
z.istate.need += (((z.InputBuffer [z.NextIn++] & 0xff) << 8) & 0xff00L);
z.istate.mode = CHECK1;
goto case CHECK1;
case CHECK1:
if (z.AvailableBytesIn == 0)
return r;
r = f;
z.AvailableBytesIn--;
z.TotalBytesIn++;
z.istate.need += (z.InputBuffer [z.NextIn++] & 0xffL);
unchecked {
if (((int)(z.istate.was [0])) != ((int)(z.istate.need))) {
z.istate.mode = BAD;
z.Message = "incorrect data check";
z.istate.marker = 5; // can't try inflateSync
break;
}
}
z.istate.mode = DONE;
goto case DONE;
case DONE:
return ZlibConstants.Z_STREAM_END;
case BAD:
throw new ZlibException (String.Format ("Bad state ({0})", z.Message));
//return ZlibConstants.Z_DATA_ERROR;
default:
throw new ZlibException ("Stream error.");
//return ZlibConstants.Z_STREAM_ERROR;
}
}
}