Source

IronPython.Zlib / src / zlib.net / InfBlocks.cs

Full commit
Jeff Hardy 9134f7e 















































































































































































































































































































































































































































































































































































































































































































































































































































































  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
172
173
174
175
176
177
178
179
180
181
182
183
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
// Copyright (c) 2006, ComponentAce
// http://www.componentace.com
// All rights reserved.

// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

// Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
// 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. 
// Neither the name of ComponentAce 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 OWNER 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.

/*
Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. 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. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE 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.
*/
/*
* This program is based on zlib-1.1.3, so all credit should go authors
* Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
* and contributors of zlib.
*/
using System;


namespace ComponentAce.Compression.Libs.ZLib
{

    internal enum InflateBlockMode
    {
        TYPE = 0, // get type bits (3, including End bit)
        LENS = 1, // get lengths for stored
        STORED = 2, // processing stored block
        TABLE = 3, // get table lengths
        BTREE = 4, // get bit lengths tree for a dynamic block
        DTREE = 5, // get length, distance trees for a dynamic block
        CODES = 6, // processing fixed or dynamic block
        DRY = 7, // output remaining Window bytes
        DONE = 8, // finished last block, done
        BAD = 9 // a data error--stuck here
    }

    internal sealed class InfBlocks
    {
        #region Fields

        private const int MANY = 1440;

        /// <summary>
        /// current inflate_block mode 
        /// </summary>
        private InflateBlockMode mode;

        /// <summary>
        /// if STORED, bytes left to copy 
        /// </summary>
        private int left;

        /// <summary>
        /// table lengths (14 bits) 
        /// </summary>
        private int table;

        /// <summary>
        /// index into blens (or border) 
        /// </summary>
        private int index;

        /// <summary>
        /// bit lengths of codes 
        /// </summary>
        private int[] blens;

        /// <summary>
        /// bit length tree depth 
        /// </summary>
        private int[] bb = new int[1];

        /// <summary>
        /// bit length decoding tree 
        /// </summary>
        private int[] tb = new int[1];

        /// <summary>
        /// if CODES, current state 
        /// </summary>
        private InfCodes codes;

        /// <summary>
        /// true if this block is the last block 
        /// </summary>
        private int last;

        // mode independent information 

        /// <summary>
        /// bits in bit buffer 
        /// </summary>
        private int bitk;

        /// <summary>
        /// bit buffer 
        /// </summary>
        private int bitb;

        /// <summary>
        /// single malloc for tree space 
        /// </summary>
        private int[] hufts;

        /// <summary>
        /// sliding Window 
        /// </summary>
        private byte[] window;

        /// <summary>
        /// one byte after sliding Window 
        /// </summary>
        private int end;

        /// <summary>
        /// Window ReadPos pointer 
        /// </summary>
        private int read;

        /// <summary>
        /// Window WritePos pointer 
        /// </summary>
        private int write;

        /// <summary>
        /// need check
        /// </summary>
        private bool needCheck;

        /// <summary>
        /// check on output 
        /// </summary>
        private long check;

        #endregion

        #region Properties

        /// <summary>
        /// sliding window 
        /// </summary>
        public byte[] Window
        {
            get { return window; }
            set { window = value; }
        }

        /// <summary>
        /// one byte after sliding Window 
        /// </summary>
        public int End
        {
            get { return end; }
            set { end = value; }
        }

        /// <summary>
        /// Window ReadPos pointer 
        /// </summary>
        public int ReadPos
        {
            get { return read; }
            set { read = value; }
        }

        /// <summary>
        /// Window WritePos pointer 
        /// </summary>
        public int WritePos
        {
            get { return write; }
            set { write = value; }
        }

        /// <summary>
        /// bits in bit buffer 
        /// </summary>
        public int BitK
        {
            get { return bitk; }
            set { bitk = value; }
        }

        /// <summary>
        /// bit buffer 
        /// </summary>
        public int BitB
        {
            get { return bitb; }
            set { bitb = value; }
        }

        #endregion

        #region Methods

        internal InfBlocks(ZStream z, bool needCheck, int w)
        {
            hufts = new int[MANY * 3];
            window = new byte[w];
            end = w;
            this.needCheck = needCheck;
            mode = InflateBlockMode.TYPE;
            reset(z, null);
        }

        /// <summary>
        /// Resets this InfBlocks class instance
        /// </summary>
        internal void reset(ZStream z, long[] c)
        {
            if (c != null)
                c[0] = check;
            if (mode == InflateBlockMode.BTREE || mode == InflateBlockMode.DTREE)
            {
                blens = null;
            }
            if (mode == InflateBlockMode.CODES)
            {
                codes.free(z);
            }
            mode = InflateBlockMode.TYPE;
            BitK = 0;
            BitB = 0;
            ReadPos = WritePos = 0;

            if (this.needCheck)
                z.adler = check = Adler32.GetAdler32Checksum(0L, null, 0, 0);
        }

        /// <summary>
        /// Block processing functions
        /// </summary>
        internal int proc(ZStream z, int r)
        {
            int t; // temporary storage
            int b; // bit buffer
            int k; // bits in bit buffer
            int p; // input data pointer
            int n; // bytes available there
            int q; // output Window WritePos pointer
            int m; // bytes to End of Window or ReadPos pointer

            // copy input/output information to locals (UPDATE macro restores)
            {
                p = z.next_in_index; n = z.avail_in; b = BitB; k = BitK;
            }
            {
                q = WritePos; m = (int)(q < ReadPos ? ReadPos - q - 1 : End - q);
            }

            // process input based on current state
            while (true)
            {
                switch (mode)
                {

                    case InflateBlockMode.TYPE:

                        while (k < (3))
                        {
                            if (n != 0)
                            {
                                r = (int)ZLibResultCode.Z_OK;
                            }
                            else
                            {
                                BitB = b; BitK = k;
                                z.avail_in = n;
                                z.total_in += p - z.next_in_index; z.next_in_index = p;
                                WritePos = q;
                                return inflate_flush(z, r);
                            }
                            ;
                            n--;
                            b |= (z.next_in[p++] & 0xff) << k;
                            k += 8;
                        }
                        t = (int)(b & 7);
                        last = t & 1;

                        switch (ZLibUtil.URShift(t, 1))
                        {

                            case 0:  // stored 
                                {
                                    b = ZLibUtil.URShift(b, (3)); k -= (3);
                                }
                                t = k & 7; // go to byte boundary
                                {
                                    b = ZLibUtil.URShift(b, (t)); k -= (t);
                                }
                                mode = InflateBlockMode.LENS; // get length of stored block
                                break;

                            case 1:  // fixed
                                {
                                    int[] bl = new int[1];
                                    int[] bd = new int[1];
                                    int[][] tl = new int[1][];
                                    int[][] td = new int[1][];

                                    InfTree.inflate_trees_fixed(bl, bd, tl, td, z);
                                    codes = new InfCodes(bl[0], bd[0], tl[0], td[0], z);
                                }
                                {
                                    b = ZLibUtil.URShift(b, (3)); k -= (3);
                                }

                                mode = InflateBlockMode.CODES;
                                break;

                            case 2:  // dynamic
                                {
                                    b = ZLibUtil.URShift(b, (3)); k -= (3);
                                }

                                mode = InflateBlockMode.TABLE;
                                break;

                            case 3:  // illegal
                                {
                                    b = ZLibUtil.URShift(b, (3)); k -= (3);
                                }
                                mode = InflateBlockMode.BAD;
                                z.msg = "invalid block type";
                                r = (int)ZLibResultCode.Z_DATA_ERROR;

                                BitB = b; BitK = k;
                                z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                                WritePos = q;
                                return inflate_flush(z, r);
                        }
                        break;

                    case InflateBlockMode.LENS:

                        while (k < (32))
                        {
                            if (n != 0)
                            {
                                r = (int)ZLibResultCode.Z_OK;
                            }
                            else
                            {
                                BitB = b; BitK = k;
                                z.avail_in = n;
                                z.total_in += p - z.next_in_index; z.next_in_index = p;
                                WritePos = q;
                                return inflate_flush(z, r);
                            }
                            ;
                            n--;
                            b |= (z.next_in[p++] & 0xff) << k;
                            k += 8;
                        }

                        if (((ZLibUtil.URShift((~b), 16)) & 0xffff) != (b & 0xffff))
                        {
                            mode = InflateBlockMode.BAD;
                            z.msg = "invalid stored block lengths";
                            r = (int)ZLibResultCode.Z_DATA_ERROR;

                            BitB = b; BitK = k;
                            z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                            WritePos = q;
                            return inflate_flush(z, r);
                        }
                        left = (b & 0xffff);
                        b = k = 0; // dump bits
                        mode = (left != 0) ? InflateBlockMode.STORED : (last != 0 ? InflateBlockMode.DRY : InflateBlockMode.TYPE);
                        break;

                    case InflateBlockMode.STORED:
                        if (n == 0)
                        {
                            BitB = b; BitK = k;
                            z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                            WritePos = q;
                            return inflate_flush(z, r);
                        }

                        if (m == 0)
                        {
                            if (q == End && ReadPos != 0)
                            {
                                q = 0; m = (int)(q < ReadPos ? ReadPos - q - 1 : End - q);
                            }
                            if (m == 0)
                            {
                                WritePos = q;
                                r = inflate_flush(z, r);
                                q = WritePos; m = (int)(q < ReadPos ? ReadPos - q - 1 : End - q);
                                if (q == End && ReadPos != 0)
                                {
                                    q = 0; m = (int)(q < ReadPos ? ReadPos - q - 1 : End - q);
                                }
                                if (m == 0)
                                {
                                    BitB = b; BitK = k;
                                    z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                                    WritePos = q;
                                    return inflate_flush(z, r);
                                }
                            }
                        }
                        r = (int)ZLibResultCode.Z_OK;

                        t = left;
                        if (t > n)
                            t = n;
                        if (t > m)
                            t = m;
                        Array.Copy(z.next_in, p, Window, q, t);
                        p += t; n -= t;
                        q += t; m -= t;
                        if ((left -= t) != 0)
                            break;
                        mode = last != 0 ? InflateBlockMode.DRY : InflateBlockMode.TYPE;
                        break;

                    case InflateBlockMode.TABLE:

                        while (k < (14))
                        {
                            if (n != 0)
                            {
                                r = (int)ZLibResultCode.Z_OK;
                            }
                            else
                            {
                                BitB = b; BitK = k;
                                z.avail_in = n;
                                z.total_in += p - z.next_in_index; z.next_in_index = p;
                                WritePos = q;
                                return inflate_flush(z, r);
                            }
                            ;
                            n--;
                            b |= (z.next_in[p++] & 0xff) << k;
                            k += 8;
                        }

                        table = t = (b & 0x3fff);
                        if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
                        {
                            mode = InflateBlockMode.BAD;
                            z.msg = "too many length or distance symbols";
                            r = (int)ZLibResultCode.Z_DATA_ERROR;

                            BitB = b; BitK = k;
                            z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                            WritePos = q;
                            return inflate_flush(z, r);
                        }
                        t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
                        blens = new int[t];
                        {
                            b = ZLibUtil.URShift(b, (14)); k -= (14);
                        }

                        index = 0;
                        mode = InflateBlockMode.BTREE;
                        goto case InflateBlockMode.BTREE;

                    case InflateBlockMode.BTREE:
                        while (index < 4 + (ZLibUtil.URShift(table, 10)))
                        {
                            while (k < (3))
                            {
                                if (n != 0)
                                {
                                    r = (int)ZLibResultCode.Z_OK;
                                }
                                else
                                {
                                    BitB = b; BitK = k;
                                    z.avail_in = n;
                                    z.total_in += p - z.next_in_index; z.next_in_index = p;
                                    WritePos = q;
                                    return inflate_flush(z, r);
                                }
                                ;
                                n--;
                                b |= (z.next_in[p++] & 0xff) << k;
                                k += 8;
                            }

                            blens[ZLibUtil.border[index++]] = b & 7;

                            {
                                b = ZLibUtil.URShift(b, (3)); k -= (3);
                            }
                        }

                        while (index < 19)
                        {
                            blens[ZLibUtil.border[index++]] = 0;
                        }

                        bb[0] = 7;
                        t = InfTree.inflate_trees_bits(blens, bb, tb, hufts, z);
                        if (t != (int)ZLibResultCode.Z_OK)
                        {
                            r = t;
                            if (r == (int)ZLibResultCode.Z_DATA_ERROR)
                            {
                                blens = null;
                                mode = InflateBlockMode.BAD;
                            }

                            BitB = b; BitK = k;
                            z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                            WritePos = q;
                            return inflate_flush(z, r);
                        }

                        index = 0;
                        mode = InflateBlockMode.DTREE;
                        goto case InflateBlockMode.DTREE;

                    case InflateBlockMode.DTREE:
                        while (true)
                        {
                            t = table;
                            if (!(index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f)))
                            {
                                break;
                            }


                            int i, j, c;

                            t = bb[0];

                            while (k < (t))
                            {
                                if (n != 0)
                                {
                                    r = (int)ZLibResultCode.Z_OK;
                                }
                                else
                                {
                                    BitB = b; BitK = k;
                                    z.avail_in = n;
                                    z.total_in += p - z.next_in_index; z.next_in_index = p;
                                    WritePos = q;
                                    return inflate_flush(z, r);
                                }
                                ;
                                n--;
                                b |= (z.next_in[p++] & 0xff) << k;
                                k += 8;
                            }

                            t = hufts[(tb[0] + (b & ZLibUtil.inflate_mask[t])) * 3 + 1];
                            c = hufts[(tb[0] + (b & ZLibUtil.inflate_mask[t])) * 3 + 2];

                            if (c < 16)
                            {
                                b = ZLibUtil.URShift(b, (t)); k -= (t);
                                blens[index++] = c;
                            }
                            else
                            {
                                // c == 16..18
                                i = c == 18 ? 7 : c - 14;
                                j = c == 18 ? 11 : 3;

                                while (k < (t + i))
                                {
                                    if (n != 0)
                                    {
                                        r = (int)ZLibResultCode.Z_OK;
                                    }
                                    else
                                    {
                                        BitB = b; BitK = k;
                                        z.avail_in = n;
                                        z.total_in += p - z.next_in_index; z.next_in_index = p;
                                        WritePos = q;
                                        return inflate_flush(z, r);
                                    }
                                    ;
                                    n--;
                                    b |= (z.next_in[p++] & 0xff) << k;
                                    k += 8;
                                }

                                b = ZLibUtil.URShift(b, (t)); k -= (t);

                                j += (b & ZLibUtil.inflate_mask[i]);

                                b = ZLibUtil.URShift(b, (i)); k -= (i);

                                i = index;
                                t = table;
                                if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) || (c == 16 && i < 1))
                                {
                                    blens = null;
                                    mode = InflateBlockMode.BAD;
                                    z.msg = "invalid bit length repeat";
                                    r = (int)ZLibResultCode.Z_DATA_ERROR;

                                    BitB = b; BitK = k;
                                    z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                                    WritePos = q;
                                    return inflate_flush(z, r);
                                }

                                c = c == 16 ? blens[i - 1] : 0;
                                do
                                {
                                    blens[i++] = c;
                                }
                                while (--j != 0);
                                index = i;
                            }
                        }

                        tb[0] = -1;
                        {
                            int[] bl = new int[1];
                            int[] bd = new int[1];
                            int[] tl = new int[1];
                            int[] td = new int[1];


                            bl[0] = 9; // must be <= 9 for lookahead assumptions
                            bd[0] = 6; // must be <= 9 for lookahead assumptions
                            t = table;
                            t = InfTree.inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f), blens, bl, bd, tl, td, hufts, z);
                            if (t != (int)ZLibResultCode.Z_OK)
                            {
                                if (t == (int)ZLibResultCode.Z_DATA_ERROR)
                                {
                                    blens = null;
                                    mode = InflateBlockMode.BAD;
                                }
                                r = t;

                                BitB = b; BitK = k;
                                z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                                WritePos = q;
                                return inflate_flush(z, r);
                            }

                            codes = new InfCodes(bl[0], bd[0], hufts, tl[0], hufts, td[0], z);
                        }
                        blens = null;
                        mode = InflateBlockMode.CODES;
                        goto case InflateBlockMode.CODES;

                    case InflateBlockMode.CODES:
                        BitB = b; BitK = k;
                        z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                        WritePos = q;

                        if ((r = codes.proc(this, z, r)) != (int)ZLibResultCode.Z_STREAM_END)
                        {
                            return inflate_flush(z, r);
                        }
                        r = (int)ZLibResultCode.Z_OK;
                        codes.free(z);

                        p = z.next_in_index; n = z.avail_in; b = BitB; k = BitK;
                        q = WritePos; m = (int)(q < ReadPos ? ReadPos - q - 1 : End - q);

                        if (last == 0)
                        {
                            mode = InflateBlockMode.TYPE;
                            break;
                        }
                        mode = InflateBlockMode.DRY;
                        goto case InflateBlockMode.DRY;

                    case InflateBlockMode.DRY:
                        WritePos = q;
                        r = inflate_flush(z, r);
                        q = WritePos; m = (int)(q < ReadPos ? ReadPos - q - 1 : End - q);
                        if (ReadPos != WritePos)
                        {
                            BitB = b; BitK = k;
                            z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                            WritePos = q;
                            return inflate_flush(z, r);
                        }
                        mode = InflateBlockMode.DONE;
                        goto case InflateBlockMode.DONE;

                    case InflateBlockMode.DONE:
                        r = (int)ZLibResultCode.Z_STREAM_END;

                        BitB = b; BitK = k;
                        z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                        WritePos = q;
                        return inflate_flush(z, r);

                    case InflateBlockMode.BAD:
                        r = (int)ZLibResultCode.Z_DATA_ERROR;

                        BitB = b; BitK = k;
                        z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                        WritePos = q;
                        return inflate_flush(z, r);


                    default:
                        r = (int)ZLibResultCode.Z_STREAM_ERROR;

                        BitB = b; BitK = k;
                        z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                        WritePos = q;
                        return inflate_flush(z, r);

                }
            }
        }

        /// <summary>
        /// Frees inner buffers
        /// </summary>
        internal void free(ZStream z)
        {
            reset(z, null);
            Window = null;
            hufts = null;
            //ZFREE(z, s);
        }

        /// <summary>
        /// Sets dictionary
        /// </summary>

        internal void set_dictionary(byte[] d, int start, int n)
        {
            Array.Copy(d, start, Window, 0, n);
            ReadPos = WritePos = n;
        }

        ///<summary>
        /// Returns true if inflate is currently at the End of a block generated
        /// by Z_SYNC_FLUSH or Z_FULL_FLUSH. 
        /// </summary>
        internal int sync_point()
        {
            return mode == InflateBlockMode.LENS ? 1 : 0;
        }

        /// <summary>
        /// copy as much as possible from the sliding Window to the output area
        /// </summary>
		internal int inflate_flush(ZStream z, int r)
		{
			int n;
			int p;
			int q;
			
			// local copies of source and destination pointers
			p = z.next_out_index;
			q = ReadPos;
			
			// compute number of bytes to copy as far as End of Window
			n = (int) ((q <= WritePos?WritePos:End) - q);
			if (n > z.avail_out)
				n = z.avail_out;
			if (n != 0 && r == (int)ZLibResultCode.Z_BUF_ERROR)
				r = (int)ZLibResultCode.Z_OK;
			
			// update counters
			z.avail_out -= n;
			z.total_out += n;
			
			// update check information
			if (this.needCheck)
				z.adler = check = Adler32.GetAdler32Checksum(check, Window, q, n);
			
			// copy as far as End of Window
			Array.Copy(Window, q, z.next_out, p, n);
			p += n;
			q += n;
			
			// see if more to copy at beginning of Window
			if (q == End)
			{
				// wrap pointers
				q = 0;
				if (WritePos == End)
					WritePos = 0;
				
				// compute bytes to copy
				n = WritePos - q;
				if (n > z.avail_out)
					n = z.avail_out;
				if (n != 0 && r == (int)ZLibResultCode.Z_BUF_ERROR)
					r = (int)ZLibResultCode.Z_OK;
				
				// update counters
				z.avail_out -= n;
				z.total_out += n;
				
				// update check information
				if (this.needCheck)
					z.adler = check = Adler32.GetAdler32Checksum(check, Window, q, n);
				
				// copy
				Array.Copy(Window, q, z.next_out, p, n);
				p += n;
				q += n;
			}
			
			// update pointers
			z.next_out_index = p;
			ReadPos = q;
			
			// done
			return r;
        }

        #endregion
    }
}