-
Notifications
You must be signed in to change notification settings - Fork 4
/
nx_inflate.c
1631 lines (1336 loc) · 46 KB
/
nx_inflate.c
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
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* NX-GZIP compression accelerator user library
* implementing zlib compression library interfaces
*
* Copyright (C) IBM Corporation, 2011-2017
*
* Licenses for GPLv2 and Apache v2.0:
*
* GPLv2:
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* Apache v2.0:
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Authors: Bulent Abali <[email protected]>
* Xiao Lei Hu <[email protected]>
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <assert.h>
#include <errno.h>
#include <sys/fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <endian.h>
#include "zlib.h"
#include "copy-paste.h"
#include "nx-ftw.h"
#include "nxu.h"
#include "nx.h"
#include "nx-gzip.h"
#include "nx_zlib.h"
#include "nx_dbg.h"
#define INF_HIS_LEN (1<<15) /* Fixed 32K history length */
#define INF_MAX_DICT_LEN INF_HIS_LEN
#define INF_MIN_INPUT_LEN 300 /* greater than 288 for dht plus 3 bit header plus 1 byte */
#define INF_MAX_COMPRESSION_RATIO 1032 /* https://stackoverflow.com/a/42865320/5504692 */
#define INF_MAX_EXPANSION_BYTES (INF_MIN_INPUT_LEN * INF_MAX_COMPRESSION_RATIO)
/* move the overflow from the current fifo head-32KB to the fifo_out
buffer beginning. Fifo_out starts with 32KB history then */
#define fifo_out_len_check(s) \
do { if ((s)->cur_out > (s)->len_out/2) { \
memmove((s)->fifo_out, (s)->fifo_out + (s)->cur_out - INF_HIS_LEN, INF_HIS_LEN + (s)->used_out); \
(s)->cur_out = INF_HIS_LEN; } \
} while(0)
#define fifo_in_len_check(s) \
do { if ((s)->cur_in > (s)->len_in/2) { \
memmove((s)->fifo_in, (s)->fifo_in + (s)->cur_in, (s)->used_in); \
(s)->cur_in = 0; } \
} while(0)
static int nx_inflate_(nx_streamp s, int flush);
int nx_inflateResetKeep(z_streamp strm)
{
nx_streamp s;
if (strm == Z_NULL)
return Z_STREAM_ERROR;
s = (nx_streamp) strm->state;
strm->total_in = strm->total_out = s->total_in = 0;
strm->msg = Z_NULL;
return Z_OK;
}
int nx_inflateReset(z_streamp strm)
{
nx_streamp s;
if (strm == Z_NULL)
return Z_STREAM_ERROR;
s = (nx_streamp) strm->state;
strm->msg = Z_NULL;
if (s->wrap)
s->adler = s->wrap & 1;
s->total_in = s->total_out = 0;
s->used_in = s->used_out = 0;
s->cur_in = 0;
s->cur_out = INF_HIS_LEN; /* keep a 32k gap here */
s->inf_state = 0;
s->resuming = 0;
s->history_len = 0;
s->is_final = 0;
s->trailer_len = 0;
s->nxcmdp = &s->nxcmd0;
s->crc32 = INIT_CRC;
s->adler32 = INIT_ADLER;
s->ckidx = 0;
s->cksum = INIT_CRC;
s->total_time = 0;
return nx_inflateResetKeep(strm);
}
static int nx_inflateReset2(z_streamp strm, int windowBits)
{
int wrap;
nx_streamp s;
if (strm == Z_NULL) return Z_STREAM_ERROR;
s = (nx_streamp) strm->state;
if (s == NULL) return Z_STREAM_ERROR;
/* Note: NX-GZIP does not do windows smaller than 32KB;
silently accept all window sizes */
/* extract wrap request from windowBits parameter */
if (windowBits < 0) {
wrap = HEADER_RAW;
windowBits = -windowBits;
}
else if (windowBits >= 8 && windowBits <= 15)
wrap = HEADER_ZLIB;
else if (windowBits >= 8+16 && windowBits <= 15+16)
wrap = HEADER_GZIP;
else if (windowBits >= 8+32 && windowBits <= 15+32)
wrap = HEADER_ZLIB | HEADER_GZIP; /* auto detect header */
else if (windowBits == 0) {
/* zlib.h states "can also be zero to request that
inflate use the window size in the zlib header of
the compressed stream. */
wrap = HEADER_ZLIB;
windowBits = 15;
}
else return Z_STREAM_ERROR;
s->wrap = wrap;
s->windowBits = windowBits;
return nx_inflateReset(strm);
}
int nx_inflateInit2_(z_streamp strm, int windowBits, const char *version, int stream_size)
{
int ret;
nx_streamp s;
nx_devp_t h;
nx_hw_init();
if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
stream_size != (int)(sizeof(z_stream)))
return Z_VERSION_ERROR;
if (strm == Z_NULL) return Z_STREAM_ERROR;
/* statistic */
zlib_stats_inc(&zlib_stats.inflateInit);
strm->msg = Z_NULL; /* in case we return an error */
h = nx_open(-1); /* if want to pick specific NX device, set env NX_GZIP_DEV_NUM */
if (!h) {
prt_err("cannot open NX device\n");
return Z_STREAM_ERROR;
}
s = nx_alloc_buffer(sizeof(*s), nx_config.page_sz, 0);
if (s == NULL) return Z_MEM_ERROR;
memset(s, 0, sizeof(*s));
s->zstrm = strm;
s->nxcmdp = &s->nxcmd0;
s->page_sz = nx_config.page_sz;
s->nxdevp = h;
// s->gzhead = NULL;
s->gzhead = nx_alloc_buffer(sizeof(gz_header), nx_config.page_sz, 0);
s->ddl_in = s->dde_in;
s->ddl_out = s->dde_out;
/* small input data will be buffered here */
s->fifo_in = NULL;
/* overflow buffer */
s->fifo_out = NULL;
strm->state = (void *) s;
ret = nx_inflateReset2(strm, windowBits);
if (ret != Z_OK) {
prt_err("nx_inflateReset2\n");
goto reset_err;
}
return ret;
reset_err:
alloc_err:
if (s->gzhead)
nx_free_buffer(s->gzhead, 0, 0);
if (s)
nx_free_buffer(s, 0, 0);
strm->state = Z_NULL;
return ret;
}
int nx_inflateInit_(z_streamp strm, const char *version, int stream_size)
{
return nx_inflateInit2_(strm, DEF_WBITS, version, stream_size);
}
int nx_inflateEnd(z_streamp strm)
{
nx_streamp s;
if (strm == Z_NULL) return Z_STREAM_ERROR;
s = (nx_streamp) strm->state;
if (s == NULL) return Z_STREAM_ERROR;
/* statistic */
zlib_stats_inc(&zlib_stats.inflateEnd);
/* TODO add here Z_DATA_ERROR if the stream was freed
prematurely (when some input or output was discarded). */
nx_inflateReset(strm);
nx_free_buffer(s->fifo_in, s->len_in, 0);
nx_free_buffer(s->fifo_out, s->len_out, 0);
nx_free_buffer(s->dict, s->dict_alloc_len, 0);
nx_close(s->nxdevp);
if (s->gzhead != NULL) nx_free_buffer(s->gzhead, sizeof(gz_header), 0);
nx_free_buffer(s, sizeof(*s), 0);
return Z_OK;
}
int nx_inflate(z_streamp strm, int flush)
{
int rc = Z_OK;
nx_streamp s;
unsigned int avail_in_slot, avail_out_slot;
uint64_t t1, t2;
if (strm == Z_NULL) return Z_STREAM_ERROR;
s = (nx_streamp) strm->state;
if (s == NULL) return Z_STREAM_ERROR;
if (flush == Z_BLOCK || flush == Z_TREES) {
strm->msg = (char *)"Z_BLOCK or Z_TREES not implemented";
prt_err("Z_BLOCK or Z_TREES not implemented!\n");
return Z_STREAM_ERROR;
}
if (s->fifo_out == NULL) {
/* overflow buffer is about 40% of s->avail_in */
s->len_out = (INF_HIS_LEN*2 + (s->zstrm->avail_in * 40)/100);
/* for the max possible expansion of inflate input */
s->len_out = NX_MAX( INF_MAX_EXPANSION_BYTES, s->len_out);
s->len_out = NX_MAX( INF_HIS_LEN << 3, s->len_out );
if (NULL == (s->fifo_out = nx_alloc_buffer(s->len_out, nx_config.page_sz, 0))) {
prt_err("nx_alloc_buffer for inflate fifo_out\n");
return Z_MEM_ERROR;
}
}
/* statistic */
if (nx_gzip_gather_statistics()) {
pthread_mutex_lock(&zlib_stats_mutex);
avail_in_slot = strm->avail_in / 4096;
if (avail_in_slot >= ZLIB_SIZE_SLOTS)
avail_in_slot = ZLIB_SIZE_SLOTS - 1;
zlib_stats.inflate_avail_in[avail_in_slot]++;
avail_out_slot = strm->avail_out / 4096;
if (avail_out_slot >= ZLIB_SIZE_SLOTS)
avail_out_slot = ZLIB_SIZE_SLOTS - 1;
zlib_stats.inflate_avail_out[avail_out_slot]++;
zlib_stats.inflate++;
zlib_stats.inflate_len += strm->avail_in;
t1 = get_nxtime_now();
pthread_mutex_unlock(&zlib_stats_mutex);
}
/* copy in from user stream to internal structures */
copy_stream_in(s, s->zstrm);
copy_stream_out(s, s->zstrm);
inf_forever:
/* inflate state machine */
prt_info("%d: inf_state %d\n", __LINE__, s->inf_state);
switch (s->inf_state) {
unsigned int c, copy;
case inf_state_header:
if (s->wrap == (HEADER_ZLIB | HEADER_GZIP)) {
/* auto detect zlib/gzip */
nx_inflate_get_byte(s, c);
if (c == 0x1f) {
/* looks like gzip; see rfc1952 ID2 and ID2 fields */
s->inf_state = inf_state_gzip_id2;
s->wrap = HEADER_GZIP;
}
else if (((c & 0x0f) == 0x08) && ( ((c >> 4) & 0x0f) < 8)) {
/* looks like zlib; see rfc1950 CMF fields, CM and CINFO */
s->inf_state = inf_state_zlib_flg;
s->zlib_cmf = c;
s->wrap = HEADER_ZLIB;
}
else {
strm->msg = (char *)"incorrect header";
s->inf_state = inf_state_data_error;
}
}
else if (s->wrap == HEADER_ZLIB) {
/* look for a zlib header */
s->inf_state = inf_state_zlib_id1;
if (s->gzhead != NULL)
s->gzhead->done = -1;
}
else if (s->wrap == HEADER_GZIP) {
/* look for a gzip header */
if (s->gzhead != NULL)
s->gzhead->done = 0;
s->inf_state = inf_state_gzip_id1;
}
else {
/* raw inflate doesn't use checksums but we do
* it anyway since comes for free */
s->crc32 = INIT_CRC;
s->adler32 = INIT_ADLER;
s->inf_state = inf_state_inflate; /* go to inflate proper */
}
break;
case inf_state_gzip_id1:
nx_inflate_get_byte(s, c);
if (c != 0x1f) {
strm->msg = (char *)"incorrect gzip header";
s->inf_state = inf_state_data_error;
break;
}
s->inf_state = inf_state_gzip_id2;
/* fall thru */
case inf_state_gzip_id2:
nx_inflate_get_byte(s, c);
if (c != 0x8b) {
strm->msg = (char *)"incorrect gzip header";
s->inf_state = inf_state_data_error;
break;
}
s->inf_state = inf_state_gzip_cm;
/* fall thru */
case inf_state_gzip_cm:
nx_inflate_get_byte(s, c);
if (c != 0x08) {
strm->msg = (char *)"unknown compression method";
s->inf_state = inf_state_data_error;
break;
}
s->inf_state = inf_state_gzip_flg;
/* fall thru */
case inf_state_gzip_flg:
nx_inflate_get_byte(s, c);
s->gzflags = c;
if (s->gzflags & 0xe0 != 0) { /* reserved bits are set */
strm->msg = (char *)"unknown header flags set";
s->inf_state = inf_state_data_error;
break;
}
if (s->gzhead != NULL) {
/* FLG field of the file says this is compressed text */
s->gzhead->text = (int) (s->gzflags & 1);
s->gzhead->time = 0;
}
s->inf_held = 0;
s->inf_state = inf_state_gzip_mtime;
/* fall thru */
case inf_state_gzip_mtime:
if (s->gzhead != NULL){
while( s->inf_held < 4) { /* need 4 bytes for MTIME */
nx_inflate_get_byte(s, c);
s->gzhead->time = c << (8 * s->inf_held) | s->gzhead->time;
++ s->inf_held;
}
s->inf_held = 0;
assert( ((s->gzhead->time & (1<<31)) == 0) );
/* assertion is a reminder for endian check; either
fires right away or in the year 2038 if we're still
alive */
}
s->inf_state = inf_state_gzip_xfl;
/* fall thru */
case inf_state_gzip_xfl:
nx_inflate_get_byte(s, c);
if (s->gzhead != NULL)
s->gzhead->xflags = c;
s->inf_state = inf_state_gzip_os;
/* fall thru */
case inf_state_gzip_os:
nx_inflate_get_byte(s, c);
if (s->gzhead != NULL)
s->gzhead->os = c;
s->inf_held = 0;
s->length = 0;
s->inf_state = inf_state_gzip_xlen;
/* fall thru */
case inf_state_gzip_xlen:
if (s->gzflags & 0x04) { /* fextra was set */
while( s->inf_held < 2 ) {
nx_inflate_get_byte(s, c);
s->length = s->length | (c << (s->inf_held * 8));
++ s->inf_held;
}
s->length = le32toh(s->length);
if (s->gzhead != NULL)
s->gzhead->extra_len = s->length;
}
else if (s->gzhead != NULL)
s->gzhead->extra = NULL;
s->inf_held = 0;
s->inf_state = inf_state_gzip_extra;
/* fall thru */
case inf_state_gzip_extra:
if (s->gzflags & 0x04) { /* fextra was set */
copy = s->length;
if (copy > s->avail_in) copy = s->avail_in;
if (copy) {
if (s->gzhead != NULL &&
s->gzhead->extra != NULL) {
unsigned int len = s->gzhead->extra_len - s->length;
memcpy(s->gzhead->extra + len, s->next_in,
len + copy > s->gzhead->extra_max ?
s->gzhead->extra_max - len : copy);
}
update_stream_in(s, copy);
s->length -= copy;
}
if (s->length) goto inf_return; /* more extra data to copy */
}
s->length = 0;
s->inf_state = inf_state_gzip_name;
/* fall thru */
case inf_state_gzip_name:
if (s->gzflags & 0x08) { /* fname was set */
if (s->avail_in == 0) goto inf_return;
copy = 0;
do {
c = (unsigned int)(s->next_in[copy++]);
if (s->gzhead != NULL &&
s->gzhead->name != NULL &&
s->length < s->gzhead->name_max )
s->gzhead->name[s->length++] = (char) c;
} while (!!c && copy < s->avail_in);
update_stream_in(s, copy);
if (!!c) goto inf_return; /* need more name */
}
else if (s->gzhead != NULL)
s->gzhead->name = NULL;
s->length = 0;
s->inf_state = inf_state_gzip_comment;
/* fall thru */
case inf_state_gzip_comment:
if (s->gzflags & 0x10) { /* fcomment was set */
if (s->avail_in == 0) goto inf_return;
copy = 0;
do {
c = (unsigned int)(s->next_in[copy++]);
if (s->gzhead != NULL &&
s->gzhead->comment != NULL &&
s->length < s->gzhead->comm_max )
s->gzhead->comment[s->length++] = (char) c;
} while (!!c && copy < s->avail_in);
update_stream_in(s, copy);
if (!!c) goto inf_return; /* need more comment */
}
else if (s->gzhead != NULL)
s->gzhead->comment = NULL;
s->length = 0;
s->inf_held = 0;
s->inf_state = inf_state_gzip_hcrc;
/* fall thru */
case inf_state_gzip_hcrc:
if (s->gzflags & 0x02) { /* fhcrc was set */
while( s->inf_held < 2 ) {
nx_inflate_get_byte(s, c);
s->hcrc16 = s->hcrc16 << 8 | c;
++ s->inf_held;
}
s->hcrc16 = le16toh(s->hcrc16);
s->gzhead->hcrc = 1;
s->gzhead->done = 1;
/* Compare stored and compute hcrc checksums here */
if (s->hcrc16 != s->cksum & 0xffff) {
strm->msg = (char *)"header crc mismatch";
s->inf_state = inf_state_data_error;
break;
}
}
else if (s->gzhead != NULL)
s->gzhead->hcrc = 0;
s->inf_held = 0;
s->adler = s->crc32 = INIT_CRC;
s->inf_state = inf_state_inflate; /* go to inflate proper */
break;
case inf_state_zlib_id1:
nx_inflate_get_byte(s, c);
if ((c & 0x0f) != 0x08) {
strm->msg = (char *)"unknown compression method";
s->inf_state = inf_state_data_error;
break;
} else if (((c >> 4) & 0x0f) >= 8) {
strm->msg = (char *)"invalid window size";
s->inf_state = inf_state_data_error;
break;
}
else {
s->inf_state = inf_state_zlib_flg; /* zlib flg field */
s->zlib_cmf = c;
}
/* fall thru */
case inf_state_zlib_flg:
nx_inflate_get_byte(s, c);
if ( ((s->zlib_cmf * 256 + c) % 31) != 0 ) {
strm->msg = (char *)"incorrect header check";
s->inf_state = inf_state_data_error;
break;
}
/* FIXME: Need double check and test here */
if (c & 1<<5) {
s->inf_state = inf_state_zlib_dictid;
s->dict_id = 0;
s->dict_len = 0;
}
else {
s->inf_state = inf_state_inflate; /* go to inflate proper */
s->adler = s->adler32 = INIT_ADLER;
}
s->inf_held = 0;
break;
case inf_state_zlib_dictid:
while (s->inf_held < 4) {
nx_inflate_get_byte(s, c);
s->dict_id = (s->dict_id << 8) | (c & 0xff);
++ s->inf_held;
}
prt_info("need dictionary %x\n", s->dict_id);
strm->adler = s->dict_id; /* asking user to supply this dict with dict_id */
s->inf_state = inf_state_zlib_dict;
s->inf_held = 0;
s->dict_len = 0;
case inf_state_zlib_dict:
if (s->dict_len == 0) {
return Z_NEED_DICT;
}
s->adler = s->adler32 = INIT_ADLER;
s->inf_state = inf_state_inflate; /* go to inflate proper */
case inf_state_inflate:
rc = nx_inflate_(s, flush);
goto inf_return;
case inf_state_data_error:
rc = Z_DATA_ERROR;
goto inf_return;
case inf_state_mem_error:
rc = Z_MEM_ERROR;
break;
case inf_state_buf_error:
rc = Z_BUF_ERROR;
break;
default:
rc = Z_STREAM_ERROR;
break;
}
goto inf_forever;
inf_return:
/* copy out to user stream */
copy_stream_in(s->zstrm, s);
copy_stream_out(s->zstrm, s);
/* statistic */
if (nx_gzip_gather_statistics()) {
pthread_mutex_lock(&zlib_stats_mutex);
t2 = get_nxtime_now();
zlib_stats.inflate_time += get_nxtime_diff(t1,t2);
pthread_mutex_unlock(&zlib_stats_mutex);
}
return rc;
}
static inline void nx_inflate_update_checksum(nx_streamp s)
{
nx_gzip_crb_cpb_t *cmdp = s->nxcmdp;
s->crc32 = get32(cmdp->cpb, out_crc);
s->adler32 = get32(cmdp->cpb, out_adler);
if (s->wrap == HEADER_GZIP)
s->zstrm->adler = s->adler = s->crc32;
else if (s->wrap == HEADER_ZLIB)
s->zstrm->adler = s->adler = s->adler32;
}
/* 0 is verify only, 1 is copy only, 2 is both copy and verify */
static int nx_inflate_verify_checksum(nx_streamp s, int copy)
{
nx_gzip_crb_cpb_t *cmdp = s->nxcmdp;
char *tail;
uint32_t cksum, isize;
int i;
if (copy > 0) {
/* to handle the case of crc and isize spanning fifo_in
* and next_in */
int need, got;
if (s->wrap == HEADER_GZIP)
need = 8;
else if (s->wrap == HEADER_ZLIB)
need = 4;
else
need = 0;
/* if partial copy exist from previous calls */
need = NX_MAX( NX_MIN(need - s->trailer_len, need), 0 );
/* copy need bytes from fifo_in */
got = NX_MIN(s->used_in, need);
if (got > 0) {
memcpy(s->trailer, s->fifo_in + s->cur_in, got);
s->trailer_len = got;
s->used_in -= got;
s->cur_in += got;
fifo_in_len_check(s);
}
/* copy any remaining from next_in */
got = NX_MIN(s->avail_in, need - got);
if (got > 0) {
memcpy(s->trailer + s->trailer_len, s->next_in, got);
s->trailer_len += got;
update_stream_in(s, got);
}
if (copy == 1)
return Z_OK; /* copy only */
}
tail = s->trailer;
if (s->wrap == HEADER_GZIP) {
if (s->trailer_len == 8) {
/* crc32 and isize are present; compare checksums */
cksum = (tail[0] | tail[1]<<8 | tail[2]<<16 | tail[3]<<24);
isize = (tail[4] | tail[5]<<8 | tail[6]<<16 | tail[7]<<24);
prt_info("computed checksum %08x isize %08x\n", cmdp->cpb.out_crc, (uint32_t)(s->total_out % (1ULL<<32)));
prt_info("stored checksum %08x isize %08x\n", cksum, isize);
nx_inflate_update_checksum(s);
if (cksum == cmdp->cpb.out_crc && isize == (uint32_t)(s->total_out % (1ULL<<32)) )
return Z_STREAM_END;
else {
prt_info("checksum or isize mismatch\n");
return Z_STREAM_ERROR;
}
}
else return Z_OK; /* didn't receive all */
}
else if (s->wrap == HEADER_ZLIB) {
if (s->trailer_len == 4) {
/* adler32 is present; compare checksums */
cksum = (tail[0] | tail[1]<<8 | tail[2]<<16 | tail[3]<<24);
prt_info("computed checksum %08x\n", cmdp->cpb.out_adler);
prt_info("stored checksum %08x\n", cksum);
nx_inflate_update_checksum(s);
if (cksum == cmdp->cpb.out_adler)
return Z_STREAM_END;
else {
prt_info("checksum mismatch\n");
return Z_STREAM_ERROR;
}
}
else return Z_OK; /* didn't receive all */
}
/* raw data does not check crc */
return Z_STREAM_END;
}
/* we need to overlay any dictionary on top of the inflate history
and calculate lengths rounding up to 16 byte integrals */
static int nx_amend_history_with_dict(nx_streamp s, int *hlen, int *dlen )
{
int dict_len = NX_MIN(s->dict_len, INF_MAX_DICT_LEN);
int hist_len;
*hlen = *dlen = 0;
ASSERT(s->history_len >= 0);
if (s->history_len <= dict_len) {
/* dictionary takes over inflate history */
hist_len = 0;
/* round up per NX-gzip history alignment requirements;
no segfaults since nx_alloc_buffer is padded in front */
dict_len = ((dict_len + NXQWSZ - 1) / NXQWSZ) * NXQWSZ;
}
else {
/* dictionary overlays history; round up to 16 byte integral */
hist_len = ((s->history_len + NXQWSZ - 1) / NXQWSZ) * NXQWSZ;
hist_len = hist_len - dict_len;
/* at this point hist_len + dict_len are 16 byte integral;
we need to add histlen bytes from s->fifo_out to the ddl;
then add dict_len bytes from s->dict to the ddl;
then comes the user data in next_in; */
}
*hlen = hist_len;
*dlen = dict_len;
return 0;
}
static int nx_inflate_(nx_streamp s, int flush)
{
/* queuing, file ops, byte counting */
uint32_t read_sz, write_sz, free_space, source_sz, target_sz;
long loop_cnt = 0, loop_max = 0xffff;
/* inflate benefits from large jobs; memcopies must be amortized */
uint32_t inflate_per_job_len = 64 * nx_config.per_job_len;
/* nx hardware */
uint32_t sfbt, subc, spbc, tpbc, nx_ce, fc;
nx_gzip_crb_cpb_t *cmdp = s->nxcmdp;
nx_dde_t *ddl_in = s->ddl_in;
nx_dde_t *ddl_out = s->ddl_out;
int pgfault_retries, target_space_retries, partial_bits;
int cc, rc;
int nx_history_len; /* includes dictionary and history going in to nx-gzip */
print_dbg_info(s, __LINE__);
if ((flush == Z_FINISH) && (s->avail_in == 0) && (s->used_out == 0))
return Z_STREAM_END;
if (s->avail_in == 0 && s->used_in == 0 && s->avail_out == 0 && s->used_out == 0)
return Z_STREAM_END;
if (s->is_final == 1 && s->used_out == 0) {
/* returning from avail_out==0 */
return nx_inflate_verify_checksum(s, 2); /* copy and verify */
}
/* duplicating zlib behavior */
if ((s->avail_in > 0 && s->next_in == NULL) || (s->next_out == NULL))
return Z_STREAM_ERROR;
if (s->next_in != NULL && s->next_out != NULL && s->avail_out == 0)
return Z_BUF_ERROR;
copy_fifo_out_to_next_out:
if (++loop_cnt == loop_max) {
prt_err("cannot make progress; too many loops loop_cnt = %ld\n", (long)loop_cnt);
return Z_STREAM_END;
}
/* if fifo_out is not empty, first copy contents to next_out.
* Remember to keep up to last 32KB as the history in fifo_out. */
if (s->used_out > 0) {
write_sz = NX_MIN(s->used_out, s->avail_out);
if (write_sz > 0) {
memcpy(s->next_out, s->fifo_out + s->cur_out, write_sz);
update_stream_out(s, write_sz);
s->used_out -= write_sz;
s->cur_out += write_sz;
fifo_out_len_check(s);
}
print_dbg_info(s, __LINE__);
if (s->used_out > 0 && s->avail_out == 0) {
prt_info("need more avail_out\n");
return Z_OK; /* Need more space to write to */
}
if (s->is_final == 1) {
return nx_inflate_verify_checksum(s, 2);
}
}
assert(s->used_out == 0);
/* if s->avail_out and s->avail_in is 0, return */
if (s->avail_out == 0 || (s->avail_in == 0 && s->used_in == 0)) return Z_OK;
if (s->used_out == 0 && s->avail_in == 0 && s->used_in == 0) return Z_OK;
/* we should flush all data to next_out here, s->used_out should be 0 */
small_next_in:
/* used_in is the data amount waiting in fifo_in; avail_in is
the data amount waiting in the user buffer next_in */
if (s->avail_in < nx_config.soft_copy_threshold && s->avail_out > 0) {
if (s->fifo_in == NULL) {
s->len_in = nx_config.soft_copy_threshold * 2;
if (NULL == (s->fifo_in = nx_alloc_buffer(s->len_in, nx_config.page_sz, 0))) {
prt_err("nx_alloc_buffer for inflate fifo_in\n");
return Z_MEM_ERROR;
}
}
/* reset fifo head to reduce unnecessary wrap arounds */
s->cur_in = (s->used_in == 0) ? 0 : s->cur_in;
fifo_in_len_check(s);
free_space = s->len_in - s->cur_in - s->used_in;
read_sz = NX_MIN(free_space, s->avail_in);
if (read_sz > 0) {
/* copy from next_in to the offset cur_in + used_in */
memcpy(s->fifo_in + s->cur_in + s->used_in, s->next_in, read_sz);
update_stream_in(s, read_sz);
s->used_in = s->used_in + read_sz;
}
}
print_dbg_info(s, __LINE__);
decomp_state:
/* NX decompresses input data */
/* address/len lists */
clearp_dde(ddl_in);
clearp_dde(ddl_out);
nx_history_len = s->history_len;
/* FC, CRC, HistLen, Table 6-6 */
if (s->resuming || (s->dict_len > 0)) {
/* Resuming a partially decompressed input. The key
to resume is supplying the max 32KB dictionary
(history) to NX, which is basically the last 32KB
or less of the output earlier produced. And also
make sure partial checksums are carried forward
*/
fc = GZIP_FC_DECOMPRESS_RESUME;
/* Crc of prev job passed to the job to be resumed */
put32(cmdp->cpb, in_crc, s->crc32);
put32(cmdp->cpb, in_adler, s->adler32);
/* Round up the sizes to quadword. Section 2.10
Rounding up will not segfault because
nx_alloc_buffer has padding at the beginning */
if (s->dict_len > 0) {
int hlen, dlen;
/* lays dict on top of hist */
nx_amend_history_with_dict(s, &hlen, &dlen );
/* sum is integral of 16 */
nx_history_len = hlen + dlen;
ASSERT( (nx_history_len % 16) == 0 );
cmdp->cpb.in_histlen = 0;
putnn(cmdp->cpb, in_histlen, nx_history_len / NXQWSZ);
ASSERT(!!s->dict && !!s->fifo_out);
/* add hlen bytes from the beginning of the history */
if (hlen > 0)
nx_append_dde(ddl_in, s->fifo_out + (s->cur_out - s->history_len), hlen);
/* add dlen bytes from the dictionary */
if (dlen > 0)
nx_append_dde(ddl_in, s->dict, dlen);
if (s->wrap == HEADER_ZLIB) {
/* in the raw mode pass crc as is; in the zlib mode
initialize them */
put32(cmdp->cpb, in_crc, INIT_CRC );
put32(cmdp->cpb, in_adler, INIT_ADLER);
put32(cmdp->cpb, out_crc, INIT_CRC );
put32(cmdp->cpb, out_adler, INIT_ADLER);
}
s->last_comp_ratio = NX_MAX( NX_MIN(1000UL, s->last_comp_ratio), 100L );
print_dbg_info(s, __LINE__);
}
else {
/* no dictionary here */
ASSERT( s->dict_len == 0 );
nx_history_len = (nx_history_len + NXQWSZ - 1) / NXQWSZ;
putnn(cmdp->cpb, in_histlen, nx_history_len);
nx_history_len = nx_history_len * NXQWSZ; /* convert to bytes */
if (nx_history_len > 0) {
/* deflate history goes in first */
ASSERT(s->cur_out >= nx_history_len);
nx_append_dde(ddl_in, s->fifo_out + (s->cur_out - nx_history_len), nx_history_len);
}
print_dbg_info(s, __LINE__);
}
}
else {
/* First decompress job */
fc = GZIP_FC_DECOMPRESS;