| | 1 | | using System; |
| | 2 | | using System.IO; |
| | 3 | | using ICSharpCode.SharpZipLib.Checksum; |
| | 4 | |
|
| | 5 | | namespace ICSharpCode.SharpZipLib.BZip2 |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// An input stream that decompresses files in the BZip2 format |
| | 9 | | /// </summary> |
| | 10 | | public class BZip2InputStream : Stream |
| | 11 | | { |
| | 12 | | #region Constants |
| | 13 | | const int START_BLOCK_STATE = 1; |
| | 14 | | const int RAND_PART_A_STATE = 2; |
| | 15 | | const int RAND_PART_B_STATE = 3; |
| | 16 | | const int RAND_PART_C_STATE = 4; |
| | 17 | | const int NO_RAND_PART_A_STATE = 5; |
| | 18 | | const int NO_RAND_PART_B_STATE = 6; |
| | 19 | | const int NO_RAND_PART_C_STATE = 7; |
| | 20 | | #endregion |
| | 21 | | #region Constructors |
| | 22 | | /// <summary> |
| | 23 | | /// Construct instance for reading from stream |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="stream">Data source</param> |
| 1 | 26 | | public BZip2InputStream(Stream stream) |
| | 27 | | { |
| | 28 | | // init arrays |
| 14 | 29 | | for (int i = 0; i < BZip2Constants.GroupCount; ++i) { |
| 6 | 30 | | limit[i] = new int[BZip2Constants.MaximumAlphaSize]; |
| 6 | 31 | | baseArray[i] = new int[BZip2Constants.MaximumAlphaSize]; |
| 6 | 32 | | perm[i] = new int[BZip2Constants.MaximumAlphaSize]; |
| | 33 | | } |
| | 34 | |
|
| 1 | 35 | | BsSetStream(stream); |
| 1 | 36 | | Initialize(); |
| 1 | 37 | | InitBlock(); |
| 1 | 38 | | SetupBlock(); |
| 1 | 39 | | } |
| | 40 | |
|
| | 41 | | #endregion |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Get/set flag indicating ownership of underlying stream. |
| | 45 | | /// When the flag is true <see cref="Close"></see> will close the underlying stream also. |
| | 46 | | /// </summary> |
| | 47 | | public bool IsStreamOwner { |
| 1 | 48 | | get { return isStreamOwner; } |
| 0 | 49 | | set { isStreamOwner = value; } |
| | 50 | | } |
| | 51 | |
|
| | 52 | |
|
| | 53 | | #region Stream Overrides |
| | 54 | | /// <summary> |
| | 55 | | /// Gets a value indicating if the stream supports reading |
| | 56 | | /// </summary> |
| | 57 | | public override bool CanRead { |
| | 58 | | get { |
| 0 | 59 | | return baseStream.CanRead; |
| | 60 | | } |
| | 61 | | } |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Gets a value indicating whether the current stream supports seeking. |
| | 65 | | /// </summary> |
| | 66 | | public override bool CanSeek { |
| | 67 | | get { |
| 0 | 68 | | return baseStream.CanSeek; |
| | 69 | | } |
| | 70 | | } |
| | 71 | |
|
| | 72 | | /// <summary> |
| | 73 | | /// Gets a value indicating whether the current stream supports writing. |
| | 74 | | /// This property always returns false |
| | 75 | | /// </summary> |
| | 76 | | public override bool CanWrite { |
| | 77 | | get { |
| 0 | 78 | | return false; |
| | 79 | | } |
| | 80 | | } |
| | 81 | |
|
| | 82 | | /// <summary> |
| | 83 | | /// Gets the length in bytes of the stream. |
| | 84 | | /// </summary> |
| | 85 | | public override long Length { |
| | 86 | | get { |
| 0 | 87 | | return baseStream.Length; |
| | 88 | | } |
| | 89 | | } |
| | 90 | |
|
| | 91 | | /// <summary> |
| | 92 | | /// Gets or sets the streams position. |
| | 93 | | /// Setting the position is not supported and will throw a NotSupportException |
| | 94 | | /// </summary> |
| | 95 | | /// <exception cref="NotSupportedException">Any attempt to set the position</exception> |
| | 96 | | public override long Position { |
| | 97 | | get { |
| 0 | 98 | | return baseStream.Position; |
| | 99 | | } |
| | 100 | | set { |
| 0 | 101 | | throw new NotSupportedException("BZip2InputStream position cannot be set"); |
| | 102 | | } |
| | 103 | | } |
| | 104 | |
|
| | 105 | | /// <summary> |
| | 106 | | /// Flushes the stream. |
| | 107 | | /// </summary> |
| | 108 | | public override void Flush() |
| | 109 | | { |
| 0 | 110 | | if (baseStream != null) { |
| 0 | 111 | | baseStream.Flush(); |
| | 112 | | } |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | /// <summary> |
| | 116 | | /// Set the streams position. This operation is not supported and will throw a NotSupportedException |
| | 117 | | /// </summary> |
| | 118 | | /// <param name="offset">A byte offset relative to the <paramref name="origin"/> parameter.</param> |
| | 119 | | /// <param name="origin">A value of type <see cref="SeekOrigin"/> indicating the reference point used to obtain the |
| | 120 | | /// <returns>The new position of the stream.</returns> |
| | 121 | | /// <exception cref="NotSupportedException">Any access</exception> |
| | 122 | | public override long Seek(long offset, SeekOrigin origin) |
| | 123 | | { |
| 0 | 124 | | throw new NotSupportedException("BZip2InputStream Seek not supported"); |
| | 125 | | } |
| | 126 | |
|
| | 127 | | /// <summary> |
| | 128 | | /// Sets the length of this stream to the given value. |
| | 129 | | /// This operation is not supported and will throw a NotSupportedExceptionortedException |
| | 130 | | /// </summary> |
| | 131 | | /// <param name="value">The new length for the stream.</param> |
| | 132 | | /// <exception cref="NotSupportedException">Any access</exception> |
| | 133 | | public override void SetLength(long value) |
| | 134 | | { |
| 0 | 135 | | throw new NotSupportedException("BZip2InputStream SetLength not supported"); |
| | 136 | | } |
| | 137 | |
|
| | 138 | | /// <summary> |
| | 139 | | /// Writes a block of bytes to this stream using data from a buffer. |
| | 140 | | /// This operation is not supported and will throw a NotSupportedException |
| | 141 | | /// </summary> |
| | 142 | | /// <param name="buffer">The buffer to source data from.</param> |
| | 143 | | /// <param name="offset">The offset to start obtaining data from.</param> |
| | 144 | | /// <param name="count">The number of bytes of data to write.</param> |
| | 145 | | /// <exception cref="NotSupportedException">Any access</exception> |
| | 146 | | public override void Write(byte[] buffer, int offset, int count) |
| | 147 | | { |
| 0 | 148 | | throw new NotSupportedException("BZip2InputStream Write not supported"); |
| | 149 | | } |
| | 150 | |
|
| | 151 | | /// <summary> |
| | 152 | | /// Writes a byte to the current position in the file stream. |
| | 153 | | /// This operation is not supported and will throw a NotSupportedException |
| | 154 | | /// </summary> |
| | 155 | | /// <param name="value">The value to write.</param> |
| | 156 | | /// <exception cref="NotSupportedException">Any access</exception> |
| | 157 | | public override void WriteByte(byte value) |
| | 158 | | { |
| 0 | 159 | | throw new NotSupportedException("BZip2InputStream WriteByte not supported"); |
| | 160 | | } |
| | 161 | |
|
| | 162 | | /// <summary> |
| | 163 | | /// Read a sequence of bytes and advances the read position by one byte. |
| | 164 | | /// </summary> |
| | 165 | | /// <param name="buffer">Array of bytes to store values in</param> |
| | 166 | | /// <param name="offset">Offset in array to begin storing data</param> |
| | 167 | | /// <param name="count">The maximum number of bytes to read</param> |
| | 168 | | /// <returns>The total number of bytes read into the buffer. This might be less |
| | 169 | | /// than the number of bytes requested if that number of bytes are not |
| | 170 | | /// currently available or zero if the end of the stream is reached. |
| | 171 | | /// </returns> |
| | 172 | | public override int Read(byte[] buffer, int offset, int count) |
| | 173 | | { |
| 1 | 174 | | if (buffer == null) { |
| 0 | 175 | | throw new ArgumentNullException(nameof(buffer)); |
| | 176 | | } |
| | 177 | |
|
| 2 | 178 | | for (int i = 0; i < count; ++i) { |
| 1 | 179 | | int rb = ReadByte(); |
| 1 | 180 | | if (rb == -1) { |
| 1 | 181 | | return i; |
| | 182 | | } |
| 0 | 183 | | buffer[offset + i] = (byte)rb; |
| | 184 | | } |
| 0 | 185 | | return count; |
| | 186 | | } |
| | 187 | |
|
| | 188 | | /// <summary> |
| | 189 | | /// Closes the stream, releasing any associated resources. |
| | 190 | | /// </summary> |
| | 191 | | public override void Close() |
| | 192 | | { |
| 1 | 193 | | if (IsStreamOwner && (baseStream != null)) { |
| 1 | 194 | | baseStream.Close(); |
| | 195 | | } |
| 1 | 196 | | } |
| | 197 | | /// <summary> |
| | 198 | | /// Read a byte from stream advancing position |
| | 199 | | /// </summary> |
| | 200 | | /// <returns>byte read or -1 on end of stream</returns> |
| | 201 | | public override int ReadByte() |
| | 202 | | { |
| 1 | 203 | | if (streamEnd) { |
| 1 | 204 | | return -1; // ok |
| | 205 | | } |
| | 206 | |
|
| 0 | 207 | | int retChar = currentChar; |
| 0 | 208 | | switch (currentState) { |
| | 209 | | case RAND_PART_B_STATE: |
| 0 | 210 | | SetupRandPartB(); |
| 0 | 211 | | break; |
| | 212 | | case RAND_PART_C_STATE: |
| 0 | 213 | | SetupRandPartC(); |
| 0 | 214 | | break; |
| | 215 | | case NO_RAND_PART_B_STATE: |
| 0 | 216 | | SetupNoRandPartB(); |
| 0 | 217 | | break; |
| | 218 | | case NO_RAND_PART_C_STATE: |
| 0 | 219 | | SetupNoRandPartC(); |
| | 220 | | break; |
| | 221 | | case START_BLOCK_STATE: |
| | 222 | | case NO_RAND_PART_A_STATE: |
| | 223 | | case RAND_PART_A_STATE: |
| | 224 | | break; |
| | 225 | | } |
| 0 | 226 | | return retChar; |
| | 227 | | } |
| | 228 | |
|
| | 229 | | #endregion |
| | 230 | |
|
| | 231 | | void MakeMaps() |
| | 232 | | { |
| 0 | 233 | | nInUse = 0; |
| 0 | 234 | | for (int i = 0; i < 256; ++i) { |
| 0 | 235 | | if (inUse[i]) { |
| 0 | 236 | | seqToUnseq[nInUse] = (byte)i; |
| 0 | 237 | | unseqToSeq[i] = (byte)nInUse; |
| 0 | 238 | | nInUse++; |
| | 239 | | } |
| | 240 | | } |
| 0 | 241 | | } |
| | 242 | |
|
| | 243 | | void Initialize() |
| | 244 | | { |
| 1 | 245 | | char magic1 = BsGetUChar(); |
| 1 | 246 | | char magic2 = BsGetUChar(); |
| | 247 | |
|
| 1 | 248 | | char magic3 = BsGetUChar(); |
| 1 | 249 | | char magic4 = BsGetUChar(); |
| | 250 | |
|
| 1 | 251 | | if (magic1 != 'B' || magic2 != 'Z' || magic3 != 'h' || magic4 < '1' || magic4 > '9') { |
| 0 | 252 | | streamEnd = true; |
| 0 | 253 | | return; |
| | 254 | | } |
| | 255 | |
|
| 1 | 256 | | SetDecompressStructureSizes(magic4 - '0'); |
| 1 | 257 | | computedCombinedCRC = 0; |
| 1 | 258 | | } |
| | 259 | |
|
| | 260 | | void InitBlock() |
| | 261 | | { |
| 1 | 262 | | char magic1 = BsGetUChar(); |
| 1 | 263 | | char magic2 = BsGetUChar(); |
| 1 | 264 | | char magic3 = BsGetUChar(); |
| 1 | 265 | | char magic4 = BsGetUChar(); |
| 1 | 266 | | char magic5 = BsGetUChar(); |
| 1 | 267 | | char magic6 = BsGetUChar(); |
| | 268 | |
|
| 1 | 269 | | if (magic1 == 0x17 && magic2 == 0x72 && magic3 == 0x45 && magic4 == 0x38 && magic5 == 0x50 && magic6 == 0x90) { |
| 1 | 270 | | Complete(); |
| 1 | 271 | | return; |
| | 272 | | } |
| | 273 | |
|
| 0 | 274 | | if (magic1 != 0x31 || magic2 != 0x41 || magic3 != 0x59 || magic4 != 0x26 || magic5 != 0x53 || magic6 != 0x59) { |
| 0 | 275 | | BadBlockHeader(); |
| 0 | 276 | | streamEnd = true; |
| 0 | 277 | | return; |
| | 278 | | } |
| | 279 | |
|
| 0 | 280 | | storedBlockCRC = BsGetInt32(); |
| | 281 | |
|
| 0 | 282 | | blockRandomised = (BsR(1) == 1); |
| | 283 | |
|
| 0 | 284 | | GetAndMoveToFrontDecode(); |
| | 285 | |
|
| 0 | 286 | | mCrc.Reset(); |
| 0 | 287 | | currentState = START_BLOCK_STATE; |
| 0 | 288 | | } |
| | 289 | |
|
| | 290 | | void EndBlock() |
| | 291 | | { |
| 0 | 292 | | computedBlockCRC = (int)mCrc.Value; |
| | 293 | |
|
| | 294 | | // -- A bad CRC is considered a fatal error. -- |
| 0 | 295 | | if (storedBlockCRC != computedBlockCRC) { |
| 0 | 296 | | CrcError(); |
| | 297 | | } |
| | 298 | |
|
| | 299 | | // 1528150659 |
| 0 | 300 | | computedCombinedCRC = ((computedCombinedCRC << 1) & 0xFFFFFFFF) | (computedCombinedCRC >> 31); |
| 0 | 301 | | computedCombinedCRC = computedCombinedCRC ^ (uint)computedBlockCRC; |
| 0 | 302 | | } |
| | 303 | |
|
| | 304 | | void Complete() |
| | 305 | | { |
| 1 | 306 | | storedCombinedCRC = BsGetInt32(); |
| 1 | 307 | | if (storedCombinedCRC != (int)computedCombinedCRC) { |
| 0 | 308 | | CrcError(); |
| | 309 | | } |
| | 310 | |
|
| 1 | 311 | | streamEnd = true; |
| 1 | 312 | | } |
| | 313 | |
|
| | 314 | | void BsSetStream(Stream stream) |
| | 315 | | { |
| 1 | 316 | | baseStream = stream; |
| 1 | 317 | | bsLive = 0; |
| 1 | 318 | | bsBuff = 0; |
| 1 | 319 | | } |
| | 320 | |
|
| | 321 | | void FillBuffer() |
| | 322 | | { |
| 14 | 323 | | int thech = 0; |
| | 324 | |
|
| | 325 | | try { |
| 14 | 326 | | thech = baseStream.ReadByte(); |
| 14 | 327 | | } catch (Exception) { |
| 0 | 328 | | CompressedStreamEOF(); |
| 0 | 329 | | } |
| | 330 | |
|
| 14 | 331 | | if (thech == -1) { |
| 0 | 332 | | CompressedStreamEOF(); |
| | 333 | | } |
| | 334 | |
|
| 14 | 335 | | bsBuff = (bsBuff << 8) | (thech & 0xFF); |
| 14 | 336 | | bsLive += 8; |
| 14 | 337 | | } |
| | 338 | |
|
| | 339 | | int BsR(int n) |
| | 340 | | { |
| 28 | 341 | | while (bsLive < n) { |
| 14 | 342 | | FillBuffer(); |
| | 343 | | } |
| | 344 | |
|
| 14 | 345 | | int v = (bsBuff >> (bsLive - n)) & ((1 << n) - 1); |
| 14 | 346 | | bsLive -= n; |
| 14 | 347 | | return v; |
| | 348 | | } |
| | 349 | |
|
| | 350 | | char BsGetUChar() |
| | 351 | | { |
| 10 | 352 | | return (char)BsR(8); |
| | 353 | | } |
| | 354 | |
|
| | 355 | | int BsGetIntVS(int numBits) |
| | 356 | | { |
| 0 | 357 | | return BsR(numBits); |
| | 358 | | } |
| | 359 | |
|
| | 360 | | int BsGetInt32() |
| | 361 | | { |
| 1 | 362 | | int result = BsR(8); |
| 1 | 363 | | result = (result << 8) | BsR(8); |
| 1 | 364 | | result = (result << 8) | BsR(8); |
| 1 | 365 | | result = (result << 8) | BsR(8); |
| 1 | 366 | | return result; |
| | 367 | | } |
| | 368 | |
|
| | 369 | | void RecvDecodingTables() |
| | 370 | | { |
| 0 | 371 | | char[][] len = new char[BZip2Constants.GroupCount][]; |
| 0 | 372 | | for (int i = 0; i < BZip2Constants.GroupCount; ++i) { |
| 0 | 373 | | len[i] = new char[BZip2Constants.MaximumAlphaSize]; |
| | 374 | | } |
| | 375 | |
|
| 0 | 376 | | bool[] inUse16 = new bool[16]; |
| | 377 | |
|
| | 378 | | //--- Receive the mapping table --- |
| 0 | 379 | | for (int i = 0; i < 16; i++) { |
| 0 | 380 | | inUse16[i] = (BsR(1) == 1); |
| | 381 | | } |
| | 382 | |
|
| 0 | 383 | | for (int i = 0; i < 16; i++) { |
| 0 | 384 | | if (inUse16[i]) { |
| 0 | 385 | | for (int j = 0; j < 16; j++) { |
| 0 | 386 | | inUse[i * 16 + j] = (BsR(1) == 1); |
| | 387 | | } |
| 0 | 388 | | } else { |
| 0 | 389 | | for (int j = 0; j < 16; j++) { |
| 0 | 390 | | inUse[i * 16 + j] = false; |
| | 391 | | } |
| | 392 | | } |
| | 393 | | } |
| | 394 | |
|
| 0 | 395 | | MakeMaps(); |
| 0 | 396 | | int alphaSize = nInUse + 2; |
| | 397 | |
|
| | 398 | | //--- Now the selectors --- |
| 0 | 399 | | int nGroups = BsR(3); |
| 0 | 400 | | int nSelectors = BsR(15); |
| | 401 | |
|
| 0 | 402 | | for (int i = 0; i < nSelectors; i++) { |
| 0 | 403 | | int j = 0; |
| 0 | 404 | | while (BsR(1) == 1) { |
| 0 | 405 | | j++; |
| | 406 | | } |
| 0 | 407 | | selectorMtf[i] = (byte)j; |
| | 408 | | } |
| | 409 | |
|
| | 410 | | //--- Undo the MTF values for the selectors. --- |
| 0 | 411 | | byte[] pos = new byte[BZip2Constants.GroupCount]; |
| 0 | 412 | | for (int v = 0; v < nGroups; v++) { |
| 0 | 413 | | pos[v] = (byte)v; |
| | 414 | | } |
| | 415 | |
|
| 0 | 416 | | for (int i = 0; i < nSelectors; i++) { |
| 0 | 417 | | int v = selectorMtf[i]; |
| 0 | 418 | | byte tmp = pos[v]; |
| 0 | 419 | | while (v > 0) { |
| 0 | 420 | | pos[v] = pos[v - 1]; |
| 0 | 421 | | v--; |
| | 422 | | } |
| 0 | 423 | | pos[0] = tmp; |
| 0 | 424 | | selector[i] = tmp; |
| | 425 | | } |
| | 426 | |
|
| | 427 | | //--- Now the coding tables --- |
| 0 | 428 | | for (int t = 0; t < nGroups; t++) { |
| 0 | 429 | | int curr = BsR(5); |
| 0 | 430 | | for (int i = 0; i < alphaSize; i++) { |
| 0 | 431 | | while (BsR(1) == 1) { |
| 0 | 432 | | if (BsR(1) == 0) { |
| 0 | 433 | | curr++; |
| 0 | 434 | | } else { |
| 0 | 435 | | curr--; |
| | 436 | | } |
| | 437 | | } |
| 0 | 438 | | len[t][i] = (char)curr; |
| | 439 | | } |
| | 440 | | } |
| | 441 | |
|
| | 442 | | //--- Create the Huffman decoding tables --- |
| 0 | 443 | | for (int t = 0; t < nGroups; t++) { |
| 0 | 444 | | int minLen = 32; |
| 0 | 445 | | int maxLen = 0; |
| 0 | 446 | | for (int i = 0; i < alphaSize; i++) { |
| 0 | 447 | | maxLen = Math.Max(maxLen, len[t][i]); |
| 0 | 448 | | minLen = Math.Min(minLen, len[t][i]); |
| | 449 | | } |
| 0 | 450 | | HbCreateDecodeTables(limit[t], baseArray[t], perm[t], len[t], minLen, maxLen, alphaSize); |
| 0 | 451 | | minLens[t] = minLen; |
| | 452 | | } |
| 0 | 453 | | } |
| | 454 | |
|
| | 455 | | void GetAndMoveToFrontDecode() |
| | 456 | | { |
| 0 | 457 | | byte[] yy = new byte[256]; |
| | 458 | | int nextSym; |
| | 459 | |
|
| 0 | 460 | | int limitLast = BZip2Constants.BaseBlockSize * blockSize100k; |
| 0 | 461 | | origPtr = BsGetIntVS(24); |
| | 462 | |
|
| 0 | 463 | | RecvDecodingTables(); |
| 0 | 464 | | int EOB = nInUse + 1; |
| 0 | 465 | | int groupNo = -1; |
| 0 | 466 | | int groupPos = 0; |
| | 467 | |
|
| | 468 | | /*-- |
| | 469 | | Setting up the unzftab entries here is not strictly |
| | 470 | | necessary, but it does save having to do it later |
| | 471 | | in a separate pass, and so saves a block's worth of |
| | 472 | | cache misses. |
| | 473 | | --*/ |
| 0 | 474 | | for (int i = 0; i <= 255; i++) { |
| 0 | 475 | | unzftab[i] = 0; |
| | 476 | | } |
| | 477 | |
|
| 0 | 478 | | for (int i = 0; i <= 255; i++) { |
| 0 | 479 | | yy[i] = (byte)i; |
| | 480 | | } |
| | 481 | |
|
| 0 | 482 | | last = -1; |
| | 483 | |
|
| 0 | 484 | | if (groupPos == 0) { |
| 0 | 485 | | groupNo++; |
| 0 | 486 | | groupPos = BZip2Constants.GroupSize; |
| | 487 | | } |
| | 488 | |
|
| 0 | 489 | | groupPos--; |
| 0 | 490 | | int zt = selector[groupNo]; |
| 0 | 491 | | int zn = minLens[zt]; |
| 0 | 492 | | int zvec = BsR(zn); |
| | 493 | | int zj; |
| | 494 | |
|
| 0 | 495 | | while (zvec > limit[zt][zn]) { |
| 0 | 496 | | if (zn > 20) { // the longest code |
| 0 | 497 | | throw new BZip2Exception("Bzip data error"); |
| | 498 | | } |
| 0 | 499 | | zn++; |
| 0 | 500 | | while (bsLive < 1) { |
| 0 | 501 | | FillBuffer(); |
| | 502 | | } |
| 0 | 503 | | zj = (bsBuff >> (bsLive - 1)) & 1; |
| 0 | 504 | | bsLive--; |
| 0 | 505 | | zvec = (zvec << 1) | zj; |
| | 506 | | } |
| 0 | 507 | | if (zvec - baseArray[zt][zn] < 0 || zvec - baseArray[zt][zn] >= BZip2Constants.MaximumAlphaSize) { |
| 0 | 508 | | throw new BZip2Exception("Bzip data error"); |
| | 509 | | } |
| 0 | 510 | | nextSym = perm[zt][zvec - baseArray[zt][zn]]; |
| | 511 | |
|
| | 512 | | while (true) { |
| 0 | 513 | | if (nextSym == EOB) { |
| | 514 | | break; |
| | 515 | | } |
| | 516 | |
|
| 0 | 517 | | if (nextSym == BZip2Constants.RunA || nextSym == BZip2Constants.RunB) { |
| 0 | 518 | | int s = -1; |
| 0 | 519 | | int n = 1; |
| | 520 | | do { |
| 0 | 521 | | if (nextSym == BZip2Constants.RunA) { |
| 0 | 522 | | s += (0 + 1) * n; |
| 0 | 523 | | } else if (nextSym == BZip2Constants.RunB) { |
| 0 | 524 | | s += (1 + 1) * n; |
| | 525 | | } |
| | 526 | |
|
| 0 | 527 | | n <<= 1; |
| | 528 | |
|
| 0 | 529 | | if (groupPos == 0) { |
| 0 | 530 | | groupNo++; |
| 0 | 531 | | groupPos = BZip2Constants.GroupSize; |
| | 532 | | } |
| | 533 | |
|
| 0 | 534 | | groupPos--; |
| | 535 | |
|
| 0 | 536 | | zt = selector[groupNo]; |
| 0 | 537 | | zn = minLens[zt]; |
| 0 | 538 | | zvec = BsR(zn); |
| | 539 | |
|
| 0 | 540 | | while (zvec > limit[zt][zn]) { |
| 0 | 541 | | zn++; |
| 0 | 542 | | while (bsLive < 1) { |
| 0 | 543 | | FillBuffer(); |
| | 544 | | } |
| 0 | 545 | | zj = (bsBuff >> (bsLive - 1)) & 1; |
| 0 | 546 | | bsLive--; |
| 0 | 547 | | zvec = (zvec << 1) | zj; |
| | 548 | | } |
| 0 | 549 | | nextSym = perm[zt][zvec - baseArray[zt][zn]]; |
| 0 | 550 | | } while (nextSym == BZip2Constants.RunA || nextSym == BZip2Constants.RunB); |
| | 551 | |
|
| 0 | 552 | | s++; |
| 0 | 553 | | byte ch = seqToUnseq[yy[0]]; |
| 0 | 554 | | unzftab[ch] += s; |
| | 555 | |
|
| 0 | 556 | | while (s > 0) { |
| 0 | 557 | | last++; |
| 0 | 558 | | ll8[last] = ch; |
| 0 | 559 | | s--; |
| | 560 | | } |
| | 561 | |
|
| 0 | 562 | | if (last >= limitLast) { |
| 0 | 563 | | BlockOverrun(); |
| | 564 | | } |
| 0 | 565 | | continue; |
| | 566 | | } else { |
| 0 | 567 | | last++; |
| 0 | 568 | | if (last >= limitLast) { |
| 0 | 569 | | BlockOverrun(); |
| | 570 | | } |
| | 571 | |
|
| 0 | 572 | | byte tmp = yy[nextSym - 1]; |
| 0 | 573 | | unzftab[seqToUnseq[tmp]]++; |
| 0 | 574 | | ll8[last] = seqToUnseq[tmp]; |
| | 575 | |
|
| 0 | 576 | | for (int j = nextSym - 1; j > 0; --j) { |
| 0 | 577 | | yy[j] = yy[j - 1]; |
| | 578 | | } |
| 0 | 579 | | yy[0] = tmp; |
| | 580 | |
|
| 0 | 581 | | if (groupPos == 0) { |
| 0 | 582 | | groupNo++; |
| 0 | 583 | | groupPos = BZip2Constants.GroupSize; |
| | 584 | | } |
| | 585 | |
|
| 0 | 586 | | groupPos--; |
| 0 | 587 | | zt = selector[groupNo]; |
| 0 | 588 | | zn = minLens[zt]; |
| 0 | 589 | | zvec = BsR(zn); |
| 0 | 590 | | while (zvec > limit[zt][zn]) { |
| 0 | 591 | | zn++; |
| 0 | 592 | | while (bsLive < 1) { |
| 0 | 593 | | FillBuffer(); |
| | 594 | | } |
| 0 | 595 | | zj = (bsBuff >> (bsLive - 1)) & 1; |
| 0 | 596 | | bsLive--; |
| 0 | 597 | | zvec = (zvec << 1) | zj; |
| | 598 | | } |
| 0 | 599 | | nextSym = perm[zt][zvec - baseArray[zt][zn]]; |
| 0 | 600 | | continue; |
| | 601 | | } |
| | 602 | | } |
| 0 | 603 | | } |
| | 604 | |
|
| | 605 | | void SetupBlock() |
| | 606 | | { |
| 1 | 607 | | int[] cftab = new int[257]; |
| | 608 | |
|
| 1 | 609 | | cftab[0] = 0; |
| 1 | 610 | | Array.Copy(unzftab, 0, cftab, 1, 256); |
| | 611 | |
|
| 514 | 612 | | for (int i = 1; i <= 256; i++) { |
| 256 | 613 | | cftab[i] += cftab[i - 1]; |
| | 614 | | } |
| | 615 | |
|
| 4 | 616 | | for (int i = 0; i <= last; i++) { |
| 1 | 617 | | byte ch = ll8[i]; |
| 1 | 618 | | tt[cftab[ch]] = i; |
| 1 | 619 | | cftab[ch]++; |
| | 620 | | } |
| | 621 | |
|
| 1 | 622 | | cftab = null; |
| | 623 | |
|
| 1 | 624 | | tPos = tt[origPtr]; |
| | 625 | |
|
| 1 | 626 | | count = 0; |
| 1 | 627 | | i2 = 0; |
| 1 | 628 | | ch2 = 256; /*-- not a char and not EOF --*/ |
| | 629 | |
|
| 1 | 630 | | if (blockRandomised) { |
| 0 | 631 | | rNToGo = 0; |
| 0 | 632 | | rTPos = 0; |
| 0 | 633 | | SetupRandPartA(); |
| 0 | 634 | | } else { |
| 1 | 635 | | SetupNoRandPartA(); |
| | 636 | | } |
| 1 | 637 | | } |
| | 638 | |
|
| | 639 | | void SetupRandPartA() |
| | 640 | | { |
| 0 | 641 | | if (i2 <= last) { |
| 0 | 642 | | chPrev = ch2; |
| 0 | 643 | | ch2 = ll8[tPos]; |
| 0 | 644 | | tPos = tt[tPos]; |
| 0 | 645 | | if (rNToGo == 0) { |
| 0 | 646 | | rNToGo = BZip2Constants.RandomNumbers[rTPos]; |
| 0 | 647 | | rTPos++; |
| 0 | 648 | | if (rTPos == 512) { |
| 0 | 649 | | rTPos = 0; |
| | 650 | | } |
| | 651 | | } |
| 0 | 652 | | rNToGo--; |
| 0 | 653 | | ch2 ^= (int)((rNToGo == 1) ? 1 : 0); |
| 0 | 654 | | i2++; |
| | 655 | |
|
| 0 | 656 | | currentChar = ch2; |
| 0 | 657 | | currentState = RAND_PART_B_STATE; |
| 0 | 658 | | mCrc.Update(ch2); |
| 0 | 659 | | } else { |
| 0 | 660 | | EndBlock(); |
| 0 | 661 | | InitBlock(); |
| 0 | 662 | | SetupBlock(); |
| | 663 | | } |
| 0 | 664 | | } |
| | 665 | |
|
| | 666 | | void SetupNoRandPartA() |
| | 667 | | { |
| 1 | 668 | | if (i2 <= last) { |
| 1 | 669 | | chPrev = ch2; |
| 1 | 670 | | ch2 = ll8[tPos]; |
| 1 | 671 | | tPos = tt[tPos]; |
| 1 | 672 | | i2++; |
| | 673 | |
|
| 1 | 674 | | currentChar = ch2; |
| 1 | 675 | | currentState = NO_RAND_PART_B_STATE; |
| 1 | 676 | | mCrc.Update(ch2); |
| 1 | 677 | | } else { |
| 0 | 678 | | EndBlock(); |
| 0 | 679 | | InitBlock(); |
| 0 | 680 | | SetupBlock(); |
| | 681 | | } |
| 0 | 682 | | } |
| | 683 | |
|
| | 684 | | void SetupRandPartB() |
| | 685 | | { |
| 0 | 686 | | if (ch2 != chPrev) { |
| 0 | 687 | | currentState = RAND_PART_A_STATE; |
| 0 | 688 | | count = 1; |
| 0 | 689 | | SetupRandPartA(); |
| 0 | 690 | | } else { |
| 0 | 691 | | count++; |
| 0 | 692 | | if (count >= 4) { |
| 0 | 693 | | z = ll8[tPos]; |
| 0 | 694 | | tPos = tt[tPos]; |
| 0 | 695 | | if (rNToGo == 0) { |
| 0 | 696 | | rNToGo = BZip2Constants.RandomNumbers[rTPos]; |
| 0 | 697 | | rTPos++; |
| 0 | 698 | | if (rTPos == 512) { |
| 0 | 699 | | rTPos = 0; |
| | 700 | | } |
| | 701 | | } |
| 0 | 702 | | rNToGo--; |
| 0 | 703 | | z ^= (byte)((rNToGo == 1) ? 1 : 0); |
| 0 | 704 | | j2 = 0; |
| 0 | 705 | | currentState = RAND_PART_C_STATE; |
| 0 | 706 | | SetupRandPartC(); |
| 0 | 707 | | } else { |
| 0 | 708 | | currentState = RAND_PART_A_STATE; |
| 0 | 709 | | SetupRandPartA(); |
| | 710 | | } |
| | 711 | | } |
| 0 | 712 | | } |
| | 713 | |
|
| | 714 | | void SetupRandPartC() |
| | 715 | | { |
| 0 | 716 | | if (j2 < (int)z) { |
| 0 | 717 | | currentChar = ch2; |
| 0 | 718 | | mCrc.Update(ch2); |
| 0 | 719 | | j2++; |
| 0 | 720 | | } else { |
| 0 | 721 | | currentState = RAND_PART_A_STATE; |
| 0 | 722 | | i2++; |
| 0 | 723 | | count = 0; |
| 0 | 724 | | SetupRandPartA(); |
| | 725 | | } |
| 0 | 726 | | } |
| | 727 | |
|
| | 728 | | void SetupNoRandPartB() |
| | 729 | | { |
| 0 | 730 | | if (ch2 != chPrev) { |
| 0 | 731 | | currentState = NO_RAND_PART_A_STATE; |
| 0 | 732 | | count = 1; |
| 0 | 733 | | SetupNoRandPartA(); |
| 0 | 734 | | } else { |
| 0 | 735 | | count++; |
| 0 | 736 | | if (count >= 4) { |
| 0 | 737 | | z = ll8[tPos]; |
| 0 | 738 | | tPos = tt[tPos]; |
| 0 | 739 | | currentState = NO_RAND_PART_C_STATE; |
| 0 | 740 | | j2 = 0; |
| 0 | 741 | | SetupNoRandPartC(); |
| 0 | 742 | | } else { |
| 0 | 743 | | currentState = NO_RAND_PART_A_STATE; |
| 0 | 744 | | SetupNoRandPartA(); |
| | 745 | | } |
| | 746 | | } |
| 0 | 747 | | } |
| | 748 | |
|
| | 749 | | void SetupNoRandPartC() |
| | 750 | | { |
| 0 | 751 | | if (j2 < (int)z) { |
| 0 | 752 | | currentChar = ch2; |
| 0 | 753 | | mCrc.Update(ch2); |
| 0 | 754 | | j2++; |
| 0 | 755 | | } else { |
| 0 | 756 | | currentState = NO_RAND_PART_A_STATE; |
| 0 | 757 | | i2++; |
| 0 | 758 | | count = 0; |
| 0 | 759 | | SetupNoRandPartA(); |
| | 760 | | } |
| 0 | 761 | | } |
| | 762 | |
|
| | 763 | | void SetDecompressStructureSizes(int newSize100k) |
| | 764 | | { |
| 1 | 765 | | if (!(0 <= newSize100k && newSize100k <= 9 && 0 <= blockSize100k && blockSize100k <= 9)) { |
| 0 | 766 | | throw new BZip2Exception("Invalid block size"); |
| | 767 | | } |
| | 768 | |
|
| 1 | 769 | | blockSize100k = newSize100k; |
| | 770 | |
|
| 1 | 771 | | if (newSize100k == 0) { |
| 0 | 772 | | return; |
| | 773 | | } |
| | 774 | |
|
| 1 | 775 | | int n = BZip2Constants.BaseBlockSize * newSize100k; |
| 1 | 776 | | ll8 = new byte[n]; |
| 1 | 777 | | tt = new int[n]; |
| 1 | 778 | | } |
| | 779 | |
|
| | 780 | | static void CompressedStreamEOF() |
| | 781 | | { |
| 0 | 782 | | throw new EndOfStreamException("BZip2 input stream end of compressed stream"); |
| | 783 | | } |
| | 784 | |
|
| | 785 | | static void BlockOverrun() |
| | 786 | | { |
| 0 | 787 | | throw new BZip2Exception("BZip2 input stream block overrun"); |
| | 788 | | } |
| | 789 | |
|
| | 790 | | static void BadBlockHeader() |
| | 791 | | { |
| 0 | 792 | | throw new BZip2Exception("BZip2 input stream bad block header"); |
| | 793 | | } |
| | 794 | |
|
| | 795 | | static void CrcError() |
| | 796 | | { |
| 0 | 797 | | throw new BZip2Exception("BZip2 input stream crc error"); |
| | 798 | | } |
| | 799 | |
|
| | 800 | | static void HbCreateDecodeTables(int[] limit, int[] baseArray, int[] perm, char[] length, int minLen, int maxLen, in |
| | 801 | | { |
| 0 | 802 | | int pp = 0; |
| | 803 | |
|
| 0 | 804 | | for (int i = minLen; i <= maxLen; ++i) { |
| 0 | 805 | | for (int j = 0; j < alphaSize; ++j) { |
| 0 | 806 | | if (length[j] == i) { |
| 0 | 807 | | perm[pp] = j; |
| 0 | 808 | | ++pp; |
| | 809 | | } |
| | 810 | | } |
| | 811 | | } |
| | 812 | |
|
| 0 | 813 | | for (int i = 0; i < BZip2Constants.MaximumCodeLength; i++) { |
| 0 | 814 | | baseArray[i] = 0; |
| | 815 | | } |
| | 816 | |
|
| 0 | 817 | | for (int i = 0; i < alphaSize; i++) { |
| 0 | 818 | | ++baseArray[length[i] + 1]; |
| | 819 | | } |
| | 820 | |
|
| 0 | 821 | | for (int i = 1; i < BZip2Constants.MaximumCodeLength; i++) { |
| 0 | 822 | | baseArray[i] += baseArray[i - 1]; |
| | 823 | | } |
| | 824 | |
|
| 0 | 825 | | for (int i = 0; i < BZip2Constants.MaximumCodeLength; i++) { |
| 0 | 826 | | limit[i] = 0; |
| | 827 | | } |
| | 828 | |
|
| 0 | 829 | | int vec = 0; |
| | 830 | |
|
| 0 | 831 | | for (int i = minLen; i <= maxLen; i++) { |
| 0 | 832 | | vec += (baseArray[i + 1] - baseArray[i]); |
| 0 | 833 | | limit[i] = vec - 1; |
| 0 | 834 | | vec <<= 1; |
| | 835 | | } |
| | 836 | |
|
| 0 | 837 | | for (int i = minLen + 1; i <= maxLen; i++) { |
| 0 | 838 | | baseArray[i] = ((limit[i - 1] + 1) << 1) - baseArray[i]; |
| | 839 | | } |
| 0 | 840 | | } |
| | 841 | |
|
| | 842 | | #region Instance Fields |
| | 843 | | /*-- |
| | 844 | | index of the last char in the block, so |
| | 845 | | the block size == last + 1. |
| | 846 | | --*/ |
| | 847 | | int last; |
| | 848 | |
|
| | 849 | | /*-- |
| | 850 | | index in zptr[] of original string after sorting. |
| | 851 | | --*/ |
| | 852 | | int origPtr; |
| | 853 | |
|
| | 854 | | /*-- |
| | 855 | | always: in the range 0 .. 9. |
| | 856 | | The current block size is 100000 * this number. |
| | 857 | | --*/ |
| | 858 | | int blockSize100k; |
| | 859 | |
|
| | 860 | | bool blockRandomised; |
| | 861 | |
|
| | 862 | | int bsBuff; |
| | 863 | | int bsLive; |
| 1 | 864 | | IChecksum mCrc = new BZip2Crc(); |
| | 865 | |
|
| 1 | 866 | | bool[] inUse = new bool[256]; |
| | 867 | | int nInUse; |
| | 868 | |
|
| 1 | 869 | | byte[] seqToUnseq = new byte[256]; |
| 1 | 870 | | byte[] unseqToSeq = new byte[256]; |
| | 871 | |
|
| 1 | 872 | | byte[] selector = new byte[BZip2Constants.MaximumSelectors]; |
| 1 | 873 | | byte[] selectorMtf = new byte[BZip2Constants.MaximumSelectors]; |
| | 874 | |
|
| | 875 | | int[] tt; |
| | 876 | | byte[] ll8; |
| | 877 | |
|
| | 878 | | /*-- |
| | 879 | | freq table collected to save a pass over the data |
| | 880 | | during decompression. |
| | 881 | | --*/ |
| 1 | 882 | | int[] unzftab = new int[256]; |
| | 883 | |
|
| 1 | 884 | | int[][] limit = new int[BZip2Constants.GroupCount][]; |
| 1 | 885 | | int[][] baseArray = new int[BZip2Constants.GroupCount][]; |
| 1 | 886 | | int[][] perm = new int[BZip2Constants.GroupCount][]; |
| 1 | 887 | | int[] minLens = new int[BZip2Constants.GroupCount]; |
| | 888 | |
|
| | 889 | | Stream baseStream; |
| | 890 | | bool streamEnd; |
| | 891 | |
|
| 1 | 892 | | int currentChar = -1; |
| | 893 | |
|
| 1 | 894 | | int currentState = START_BLOCK_STATE; |
| | 895 | |
|
| | 896 | | int storedBlockCRC, storedCombinedCRC; |
| | 897 | | int computedBlockCRC; |
| | 898 | | uint computedCombinedCRC; |
| | 899 | |
|
| | 900 | | int count, chPrev, ch2; |
| | 901 | | int tPos; |
| | 902 | | int rNToGo; |
| | 903 | | int rTPos; |
| | 904 | | int i2, j2; |
| | 905 | | byte z; |
| 1 | 906 | | bool isStreamOwner = true; |
| | 907 | | #endregion |
| | 908 | | } |
| | 909 | | } |