00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "../client/client.h"
00011 #include "unzip.h"
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 #ifndef _ZCONF_H
00090 #define _ZCONF_H
00091
00092
00093 #ifndef MAX_MEM_LEVEL
00094 # ifdef MAXSEG_64K
00095 # define MAX_MEM_LEVEL 8
00096 # else
00097 # define MAX_MEM_LEVEL 9
00098 # endif
00099 #endif
00100
00101
00102
00103
00104
00105
00106 #ifndef MAX_WBITS
00107 # define MAX_WBITS 15
00108 #endif
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 #ifndef OF
00126 #define OF(args) args
00127 #endif
00128
00129 typedef unsigned char Byte;
00130 typedef unsigned int uInt;
00131 typedef unsigned long uLong;
00132 typedef Byte *voidp;
00133
00134 #ifndef SEEK_SET
00135 # define SEEK_SET 0
00136 # define SEEK_CUR 1
00137 # define SEEK_END 2
00138 #endif
00139
00140 #endif
00141
00142 #define ZLIB_VERSION "1.1.3"
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 #define Z_NO_FLUSH 0
00200 #define Z_PARTIAL_FLUSH 1
00201 #define Z_SYNC_FLUSH 2
00202 #define Z_FULL_FLUSH 3
00203 #define Z_FINISH 4
00204
00205
00206 #define Z_OK 0
00207 #define Z_STREAM_END 1
00208 #define Z_NEED_DICT 2
00209 #define Z_ERRNO (-1)
00210 #define Z_STREAM_ERROR (-2)
00211 #define Z_DATA_ERROR (-3)
00212 #define Z_MEM_ERROR (-4)
00213 #define Z_BUF_ERROR (-5)
00214 #define Z_VERSION_ERROR (-6)
00215
00216
00217
00218
00219 #define Z_NO_COMPRESSION 0
00220 #define Z_BEST_SPEED 1
00221 #define Z_BEST_COMPRESSION 9
00222 #define Z_DEFAULT_COMPRESSION (-1)
00223
00224
00225 #define Z_FILTERED 1
00226 #define Z_HUFFMAN_ONLY 2
00227 #define Z_DEFAULT_STRATEGY 0
00228
00229
00230 #define Z_BINARY 0
00231 #define Z_ASCII 1
00232 #define Z_UNKNOWN 2
00233
00234
00235 #define Z_DEFLATED 8
00236
00237
00238 #define Z_NULL 0
00239
00240 #define zlib_version zlibVersion()
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388 static int inflate OF((z_streamp strm, int flush));
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457 static int inflateEnd OF((z_streamp strm));
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669 static int inflateReset OF((z_streamp strm));
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745 typedef voidp gzFile;
00746
00747 gzFile gzopen OF((const char *path, const char *mode));
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763 gzFile gzdopen OF((int fd, const char *mode));
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776 int gzsetparams OF((gzFile file, int level, int strategy));
00777
00778
00779
00780
00781
00782
00783
00784 int gzread OF((gzFile file, voidp buf, unsigned len));
00785
00786
00787
00788
00789
00790
00791
00792 int gzwrite OF((gzFile file,
00793 const voidp buf, unsigned len));
00794
00795
00796
00797
00798
00799
00800 int QDECL gzprintf OF((gzFile file, const char *format, ...));
00801
00802
00803
00804
00805
00806
00807 int gzputs OF((gzFile file, const char *s));
00808
00809
00810
00811
00812
00813
00814 char * gzgets OF((gzFile file, char *buf, int len));
00815
00816
00817
00818
00819
00820
00821
00822
00823 int gzputc OF((gzFile file, int c));
00824
00825
00826
00827
00828
00829 int gzgetc OF((gzFile file));
00830
00831
00832
00833
00834
00835 int gzflush OF((gzFile file, int flush));
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845 long gzseek OF((gzFile file,
00846 long offset, int whence));
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863 int gzrewind OF((gzFile file));
00864
00865
00866
00867
00868
00869
00870 long gztell OF((gzFile file));
00871
00872
00873
00874
00875
00876
00877
00878
00879 int gzeof OF((gzFile file));
00880
00881
00882
00883
00884
00885 int gzclose OF((gzFile file));
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909 static uLong adler32 OF((uLong adler, const Byte *buf, uInt len));
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941 static int inflateInit2_ OF((z_streamp strm, int windowBits,
00942 const char *version, int stream_size));
00943
00944 #define deflateInit(strm, level) \
00945 deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream))
00946 #define inflateInit(strm) \
00947 inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream))
00948 #define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
00949 deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
00950 (strategy), ZLIB_VERSION, sizeof(z_stream))
00951 #define inflateInit2(strm, windowBits) \
00952 inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
00953
00954
00955
00956
00957
00958
00959 typedef unsigned char uch;
00960 typedef unsigned short ush;
00961 typedef unsigned long ulg;
00962
00963
00964
00965
00966 #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
00967
00968 #define ERR_RETURN(strm,err) \
00969 return (strm->msg = (char*)ERR_MSG(err), (err))
00970
00971
00972
00973
00974 #ifndef DEF_WBITS
00975 # define DEF_WBITS MAX_WBITS
00976 #endif
00977
00978
00979 #if MAX_MEM_LEVEL >= 8
00980 # define DEF_MEM_LEVEL 8
00981 #else
00982 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
00983 #endif
00984
00985
00986 #define STORED_BLOCK 0
00987 #define STATIC_TREES 1
00988 #define DYN_TREES 2
00989
00990
00991 #define MIN_MATCH 3
00992 #define MAX_MATCH 258
00993
00994
00995 #define PRESET_DICT 0x20
00996
00997
00998
00999
01000
01001 #ifndef OS_CODE
01002 # define OS_CODE 0x03
01003 #endif
01004
01005 #ifndef F_OPEN
01006 # define F_OPEN(name, mode) fopen((name), (mode))
01007 #endif
01008
01009
01010
01011 #ifdef HAVE_STRERROR
01012 extern char *strerror OF((int));
01013 # define zstrerror(errnum) strerror(errnum)
01014 #else
01015 # define zstrerror(errnum) ""
01016 #endif
01017
01018 #define zmemcpy Com_Memcpy
01019 #define zmemcmp memcmp
01020 #define zmemzero(dest, len) Com_Memset(dest, 0, len)
01021
01022
01023 #ifdef _ZIP_DEBUG_
01024 int z_verbose = 0;
01025 # define Assert(cond,msg) assert(cond);
01026
01027 # define Trace(x) {if (z_verbose>=0) Sys_Error x ;}
01028 # define Tracev(x) {if (z_verbose>0) Sys_Error x ;}
01029 # define Tracevv(x) {if (z_verbose>1) Sys_Error x ;}
01030 # define Tracec(c,x) {if (z_verbose>0 && (c)) Sys_Error x ;}
01031 # define Tracecv(c,x) {if (z_verbose>1 && (c)) Sys_Error x ;}
01032 #else
01033 # define Assert(cond,msg)
01034 # define Trace(x)
01035 # define Tracev(x)
01036 # define Tracevv(x)
01037 # define Tracec(c,x)
01038 # define Tracecv(c,x)
01039 #endif
01040
01041
01042 typedef uLong (*check_func) OF((uLong check, const Byte *buf, uInt len));
01043 static voidp zcalloc OF((voidp opaque, unsigned items, unsigned size));
01044 static void zcfree OF((voidp opaque, voidp ptr));
01045
01046 #define ZALLOC(strm, items, size) \
01047 (*((strm)->zalloc))((strm)->opaque, (items), (size))
01048 #define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidp)(addr))
01049 #define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
01050
01051
01052 #if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES) && \
01053 !defined(CASESENSITIVITYDEFAULT_NO)
01054 #define CASESENSITIVITYDEFAULT_NO
01055 #endif
01056
01057
01058 #ifndef UNZ_BUFSIZE
01059 #define UNZ_BUFSIZE (65536)
01060 #endif
01061
01062 #ifndef UNZ_MAXFILENAMEINZIP
01063 #define UNZ_MAXFILENAMEINZIP (256)
01064 #endif
01065
01066 #ifndef ALLOC
01067 # define ALLOC(size) (Z_Malloc(size))
01068 #endif
01069 #ifndef TRYFREE
01070 # define TRYFREE(p) {if (p) Z_Free(p);}
01071 #endif
01072
01073 #define SIZECENTRALDIRITEM (0x2e)
01074 #define SIZEZIPLOCALHEADER (0x1e)
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107 static int unzlocal_getShort (FILE* fin, uLong *pX)
01108 {
01109 short v;
01110
01111 fread( &v, sizeof(v), 1, fin );
01112
01113 *pX = LittleShort( v);
01114 return UNZ_OK;
01115
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134 }
01135
01136 static int unzlocal_getLong (FILE *fin, uLong *pX)
01137 {
01138 int v;
01139
01140 fread( &v, sizeof(v), 1, fin );
01141
01142 *pX = LittleLong( v);
01143 return UNZ_OK;
01144
01145
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170
01171 }
01172
01173
01174
01175 static int strcmpcasenosensitive_internal (const char* fileName1,const char* fileName2)
01176 {
01177 for (;;)
01178 {
01179 char c1=*(fileName1++);
01180 char c2=*(fileName2++);
01181 if ((c1>='a') && (c1<='z'))
01182 c1 -= 0x20;
01183 if ((c2>='a') && (c2<='z'))
01184 c2 -= 0x20;
01185 if (c1=='\0')
01186 return ((c2=='\0') ? 0 : -1);
01187 if (c2=='\0')
01188 return 1;
01189 if (c1<c2)
01190 return -1;
01191 if (c1>c2)
01192 return 1;
01193 }
01194 }
01195
01196
01197 #ifdef CASESENSITIVITYDEFAULT_NO
01198 #define CASESENSITIVITYDEFAULTVALUE 2
01199 #else
01200 #define CASESENSITIVITYDEFAULTVALUE 1
01201 #endif
01202
01203 #ifndef STRCMPCASENOSENTIVEFUNCTION
01204 #define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal
01205 #endif
01206
01207
01208
01209
01210
01211
01212
01213
01214
01215
01216 extern int unzStringFileNameCompare (const char* fileName1,const char* fileName2,int iCaseSensitivity)
01217 {
01218 if (iCaseSensitivity==0)
01219 iCaseSensitivity=CASESENSITIVITYDEFAULTVALUE;
01220
01221 if (iCaseSensitivity==1)
01222 return strcmp(fileName1,fileName2);
01223
01224 return STRCMPCASENOSENTIVEFUNCTION(fileName1,fileName2);
01225 }
01226
01227 #define BUFREADCOMMENT (0x400)
01228
01229
01230
01231
01232
01233 extern uLong unzlocal_SearchCentralDir(FILE *fin)
01234 {
01235 unsigned char* buf;
01236 uLong uSizeFile;
01237 uLong uBackRead;
01238 uLong uMaxBack=0xffff;
01239 uLong uPosFound=0;
01240
01241 if (fseek(fin,0,SEEK_END) != 0)
01242 return 0;
01243
01244
01245 uSizeFile = ftell( fin );
01246
01247 if (uMaxBack>uSizeFile)
01248 uMaxBack = uSizeFile;
01249
01250 buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
01251 if (buf==NULL)
01252 return 0;
01253
01254 uBackRead = 4;
01255 while (uBackRead<uMaxBack)
01256 {
01257 uLong uReadSize,uReadPos ;
01258 int i;
01259 if (uBackRead+BUFREADCOMMENT>uMaxBack)
01260 uBackRead = uMaxBack;
01261 else
01262 uBackRead+=BUFREADCOMMENT;
01263 uReadPos = uSizeFile-uBackRead ;
01264
01265 uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
01266 (BUFREADCOMMENT+4) : (uSizeFile-uReadPos);
01267 if (fseek(fin,uReadPos,SEEK_SET)!=0)
01268 break;
01269
01270 if (fread(buf,(uInt)uReadSize,1,fin)!=1)
01271 break;
01272
01273 for (i=(int)uReadSize-3; (i--)>0;)
01274 if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
01275 ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
01276 {
01277 uPosFound = uReadPos+i;
01278 break;
01279 }
01280
01281 if (uPosFound!=0)
01282 break;
01283 }
01284 TRYFREE(buf);
01285 return uPosFound;
01286 }
01287
01288 extern unzFile unzReOpen (const char* path, unzFile file)
01289 {
01290 unz_s *s;
01291 FILE * fin;
01292
01293 fin=fopen(path,"rb");
01294 if (fin==NULL)
01295 return NULL;
01296
01297 s=(unz_s*)ALLOC(sizeof(unz_s));
01298 Com_Memcpy(s, (unz_s*)file, sizeof(unz_s));
01299
01300 s->file = fin;
01301 return (unzFile)s;
01302 }
01303
01304
01305
01306
01307
01308
01309
01310
01311
01312
01313 extern unzFile unzOpen (const char* path)
01314 {
01315 unz_s us;
01316 unz_s *s;
01317 uLong central_pos,uL;
01318 FILE * fin ;
01319
01320 uLong number_disk;
01321
01322 uLong number_disk_with_CD;
01323
01324 uLong number_entry_CD;
01325
01326
01327
01328 int err=UNZ_OK;
01329
01330 fin=fopen(path,"rb");
01331 if (fin==NULL)
01332 return NULL;
01333
01334 central_pos = unzlocal_SearchCentralDir(fin);
01335 if (central_pos==0)
01336 err=UNZ_ERRNO;
01337
01338 if (fseek(fin,central_pos,SEEK_SET)!=0)
01339 err=UNZ_ERRNO;
01340
01341
01342 if (unzlocal_getLong(fin,&uL)!=UNZ_OK)
01343 err=UNZ_ERRNO;
01344
01345
01346 if (unzlocal_getShort(fin,&number_disk)!=UNZ_OK)
01347 err=UNZ_ERRNO;
01348
01349
01350 if (unzlocal_getShort(fin,&number_disk_with_CD)!=UNZ_OK)
01351 err=UNZ_ERRNO;
01352
01353
01354 if (unzlocal_getShort(fin,&us.gi.number_entry)!=UNZ_OK)
01355 err=UNZ_ERRNO;
01356
01357
01358 if (unzlocal_getShort(fin,&number_entry_CD)!=UNZ_OK)
01359 err=UNZ_ERRNO;
01360
01361 if ((number_entry_CD!=us.gi.number_entry) ||
01362 (number_disk_with_CD!=0) ||
01363 (number_disk!=0))
01364 err=UNZ_BADZIPFILE;
01365
01366
01367 if (unzlocal_getLong(fin,&us.size_central_dir)!=UNZ_OK)
01368 err=UNZ_ERRNO;
01369
01370
01371
01372 if (unzlocal_getLong(fin,&us.offset_central_dir)!=UNZ_OK)
01373 err=UNZ_ERRNO;
01374
01375
01376 if (unzlocal_getShort(fin,&us.gi.size_comment)!=UNZ_OK)
01377 err=UNZ_ERRNO;
01378
01379 if ((central_pos<us.offset_central_dir+us.size_central_dir) &&
01380 (err==UNZ_OK))
01381 err=UNZ_BADZIPFILE;
01382
01383 if (err!=UNZ_OK)
01384 {
01385 fclose(fin);
01386 return NULL;
01387 }
01388
01389 us.file=fin;
01390 us.byte_before_the_zipfile = central_pos -
01391 (us.offset_central_dir+us.size_central_dir);
01392 us.central_pos = central_pos;
01393 us.pfile_in_zip_read = NULL;
01394
01395
01396 s=(unz_s*)ALLOC(sizeof(unz_s));
01397 *s=us;
01398
01399 return (unzFile)s;
01400 }
01401
01402
01403
01404
01405
01406
01407
01408 extern int unzClose (unzFile file)
01409 {
01410 unz_s* s;
01411 if (file==NULL)
01412 return UNZ_PARAMERROR;
01413 s=(unz_s*)file;
01414
01415 if (s->pfile_in_zip_read!=NULL)
01416 unzCloseCurrentFile(file);
01417
01418 fclose(s->file);
01419 TRYFREE(s);
01420 return UNZ_OK;
01421 }
01422
01423
01424
01425
01426
01427
01428 extern int unzGetGlobalInfo (unzFile file,unz_global_info *pglobal_info)
01429 {
01430 unz_s* s;
01431 if (file==NULL)
01432 return UNZ_PARAMERROR;
01433 s=(unz_s*)file;
01434 *pglobal_info=s->gi;
01435 return UNZ_OK;
01436 }
01437
01438
01439
01440
01441
01442 static void unzlocal_DosDateToTmuDate (uLong ulDosDate, tm_unz* ptm)
01443 {
01444 uLong uDate;
01445 uDate = (uLong)(ulDosDate>>16);
01446 ptm->tm_mday = (uInt)(uDate&0x1f) ;
01447 ptm->tm_mon = (uInt)((((uDate)&0x1E0)/0x20)-1) ;
01448 ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ;
01449
01450 ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800);
01451 ptm->tm_min = (uInt) ((ulDosDate&0x7E0)/0x20) ;
01452 ptm->tm_sec = (uInt) (2*(ulDosDate&0x1f)) ;
01453 }
01454
01455
01456
01457
01458 static int unzlocal_GetCurrentFileInfoInternal (unzFile file,
01459 unz_file_info *pfile_info,
01460 unz_file_info_internal
01461 *pfile_info_internal,
01462 char *szFileName,
01463 uLong fileNameBufferSize,
01464 void *extraField,
01465 uLong extraFieldBufferSize,
01466 char *szComment,
01467 uLong commentBufferSize)
01468 {
01469 unz_s* s;
01470 unz_file_info file_info;
01471 unz_file_info_internal file_info_internal;
01472 int err=UNZ_OK;
01473 uLong uMagic;
01474 long lSeek=0;
01475
01476 if (file==NULL)
01477 return UNZ_PARAMERROR;
01478 s=(unz_s*)file;
01479 if (fseek(s->file,s->pos_in_central_dir+s->byte_before_the_zipfile,SEEK_SET)!=0)
01480 err=UNZ_ERRNO;
01481
01482
01483
01484 if (err==UNZ_OK) {
01485 if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK)
01486 err=UNZ_ERRNO;
01487 else if (uMagic!=0x02014b50)
01488 err=UNZ_BADZIPFILE;
01489 }
01490 if (unzlocal_getShort(s->file,&file_info.version) != UNZ_OK)
01491 err=UNZ_ERRNO;
01492
01493 if (unzlocal_getShort(s->file,&file_info.version_needed) != UNZ_OK)
01494 err=UNZ_ERRNO;
01495
01496 if (unzlocal_getShort(s->file,&file_info.flag) != UNZ_OK)
01497 err=UNZ_ERRNO;
01498
01499 if (unzlocal_getShort(s->file,&file_info.compression_method) != UNZ_OK)
01500 err=UNZ_ERRNO;
01501
01502 if (unzlocal_getLong(s->file,&file_info.dosDate) != UNZ_OK)
01503 err=UNZ_ERRNO;
01504
01505 unzlocal_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date);
01506
01507 if (unzlocal_getLong(s->file,&file_info.crc) != UNZ_OK)
01508 err=UNZ_ERRNO;
01509
01510 if (unzlocal_getLong(s->file,&file_info.compressed_size) != UNZ_OK)
01511 err=UNZ_ERRNO;
01512
01513 if (unzlocal_getLong(s->file,&file_info.uncompressed_size) != UNZ_OK)
01514 err=UNZ_ERRNO;
01515
01516 if (unzlocal_getShort(s->file,&file_info.size_filename) != UNZ_OK)
01517 err=UNZ_ERRNO;
01518
01519 if (unzlocal_getShort(s->file,&file_info.size_file_extra) != UNZ_OK)
01520 err=UNZ_ERRNO;
01521
01522 if (unzlocal_getShort(s->file,&file_info.size_file_comment) != UNZ_OK)
01523 err=UNZ_ERRNO;
01524
01525 if (unzlocal_getShort(s->file,&file_info.disk_num_start) != UNZ_OK)
01526 err=UNZ_ERRNO;
01527
01528 if (unzlocal_getShort(s->file,&file_info.internal_fa) != UNZ_OK)
01529 err=UNZ_ERRNO;
01530
01531 if (unzlocal_getLong(s->file,&file_info.external_fa) != UNZ_OK)
01532 err=UNZ_ERRNO;
01533
01534 if (unzlocal_getLong(s->file,&file_info_internal.offset_curfile) != UNZ_OK)
01535 err=UNZ_ERRNO;
01536
01537 lSeek+=file_info.size_filename;
01538 if ((err==UNZ_OK) && (szFileName!=NULL))
01539 {
01540 uLong uSizeRead ;
01541 if (file_info.size_filename<fileNameBufferSize)
01542 {
01543 *(szFileName+file_info.size_filename)='\0';
01544 uSizeRead = file_info.size_filename;
01545 }
01546 else
01547 uSizeRead = fileNameBufferSize;
01548
01549 if ((file_info.size_filename>0) && (fileNameBufferSize>0))
01550 if (fread(szFileName,(uInt)uSizeRead,1,s->file)!=1)
01551 err=UNZ_ERRNO;
01552 lSeek -= uSizeRead;
01553 }
01554
01555
01556 if ((err==UNZ_OK) && (extraField!=NULL))
01557 {
01558 uLong uSizeRead ;
01559 if (file_info.size_file_extra<extraFieldBufferSize)
01560 uSizeRead = file_info.size_file_extra;
01561 else
01562 uSizeRead = extraFieldBufferSize;
01563
01564 if (lSeek!=0) {
01565 if (fseek(s->file,lSeek,SEEK_CUR)==0)
01566 lSeek=0;
01567 else
01568 err=UNZ_ERRNO;
01569 }
01570 if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0)) {
01571 if (fread(extraField,(uInt)uSizeRead,1,s->file)!=1)
01572 err=UNZ_ERRNO;
01573 }
01574 lSeek += file_info.size_file_extra - uSizeRead;
01575 }
01576 else
01577 lSeek+=file_info.size_file_extra;
01578
01579
01580 if ((err==UNZ_OK) && (szComment!=NULL))
01581 {
01582 uLong uSizeRead ;
01583 if (file_info.size_file_comment<commentBufferSize)
01584 {
01585 *(szComment+file_info.size_file_comment)='\0';
01586 uSizeRead = file_info.size_file_comment;
01587 }
01588 else
01589 uSizeRead = commentBufferSize;
01590
01591 if (lSeek!=0) {
01592 if (fseek(s->file,lSeek,SEEK_CUR)==0)
01593 lSeek=0;
01594 else
01595 err=UNZ_ERRNO;
01596 }
01597 if ((file_info.size_file_comment>0) && (commentBufferSize>0)) {
01598 if (fread(szComment,(uInt)uSizeRead,1,s->file)!=1)
01599 err=UNZ_ERRNO;
01600 }
01601 lSeek+=file_info.size_file_comment - uSizeRead;
01602 }
01603 else
01604 lSeek+=file_info.size_file_comment;
01605
01606 if ((err==UNZ_OK) && (pfile_info!=NULL))
01607 *pfile_info=file_info;
01608
01609 if ((err==UNZ_OK) && (pfile_info_internal!=NULL))
01610 *pfile_info_internal=file_info_internal;
01611
01612 return err;
01613 }
01614
01615
01616
01617
01618
01619
01620
01621
01622 extern int unzGetCurrentFileInfo ( unzFile file, unz_file_info *pfile_info,
01623 char *szFileName, uLong fileNameBufferSize,
01624 void *extraField, uLong extraFieldBufferSize,
01625 char *szComment, uLong commentBufferSize)
01626 {
01627 return unzlocal_GetCurrentFileInfoInternal(file,pfile_info,NULL,
01628 szFileName,fileNameBufferSize,
01629 extraField,extraFieldBufferSize,
01630 szComment,commentBufferSize);
01631 }
01632
01633
01634
01635
01636
01637 extern int unzGoToFirstFile (unzFile file)
01638 {
01639 int err=UNZ_OK;
01640 unz_s* s;
01641 if (file==NULL)
01642 return UNZ_PARAMERROR;
01643 s=(unz_s*)file;
01644 s->pos_in_central_dir=s->offset_central_dir;
01645 s->num_file=0;
01646 err=unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
01647 &s->cur_file_info_internal,
01648 NULL,0,NULL,0,NULL,0);
01649 s->current_file_ok = (err == UNZ_OK);
01650 return err;
01651 }
01652
01653
01654
01655
01656
01657
01658
01659 extern int unzGoToNextFile (unzFile file)
01660 {
01661 unz_s* s;
01662 int err;
01663
01664 if (file==NULL)
01665 return UNZ_PARAMERROR;
01666 s=(unz_s*)file;
01667 if (!s->current_file_ok)
01668 return UNZ_END_OF_LIST_OF_FILE;
01669 if (s->num_file+1==s->gi.number_entry)
01670 return UNZ_END_OF_LIST_OF_FILE;
01671
01672 s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename +
01673 s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ;
01674 s->num_file++;
01675 err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
01676 &s->cur_file_info_internal,
01677 NULL,0,NULL,0,NULL,0);
01678 s->current_file_ok = (err == UNZ_OK);
01679 return err;
01680 }
01681
01682
01683
01684
01685
01686 extern int unzGetCurrentFileInfoPosition (unzFile file, unsigned long *pos )
01687 {
01688 unz_s* s;
01689
01690 if (file==NULL)
01691 return UNZ_PARAMERROR;
01692 s=(unz_s*)file;
01693
01694 *pos = s->pos_in_central_dir;
01695 return UNZ_OK;
01696 }
01697
01698
01699
01700
01701
01702 extern int unzSetCurrentFileInfoPosition (unzFile file, unsigned long pos )
01703 {
01704 unz_s* s;
01705 int err;
01706
01707 if (file==NULL)
01708 return UNZ_PARAMERROR;
01709 s=(unz_s*)file;
01710
01711 s->pos_in_central_dir = pos;
01712 err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
01713 &s->cur_file_info_internal,
01714 NULL,0,NULL,0,NULL,0);
01715 s->current_file_ok = (err == UNZ_OK);
01716 return UNZ_OK;
01717 }
01718
01719
01720
01721
01722
01723
01724
01725
01726
01727 extern int unzLocateFile (unzFile file, const char *szFileName, int iCaseSensitivity)
01728 {
01729 unz_s* s;
01730 int err;
01731
01732
01733 uLong num_fileSaved;
01734 uLong pos_in_central_dirSaved;
01735
01736
01737 if (file==NULL)
01738 return UNZ_PARAMERROR;
01739
01740 if (strlen(szFileName)>=UNZ_MAXFILENAMEINZIP)
01741 return UNZ_PARAMERROR;
01742
01743 s=(unz_s*)file;
01744 if (!s->current_file_ok)
01745 return UNZ_END_OF_LIST_OF_FILE;
01746
01747 num_fileSaved = s->num_file;
01748 pos_in_central_dirSaved = s->pos_in_central_dir;
01749
01750 err = unzGoToFirstFile(file);
01751
01752 while (err == UNZ_OK)
01753 {
01754 char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1];
01755 unzGetCurrentFileInfo(file,NULL,
01756 szCurrentFileName,sizeof(szCurrentFileName)-1,
01757 NULL,0,NULL,0);
01758 if (unzStringFileNameCompare(szCurrentFileName,
01759 szFileName,iCaseSensitivity)==0)
01760 return UNZ_OK;
01761 err = unzGoToNextFile(file);
01762 }
01763
01764 s->num_file = num_fileSaved ;
01765 s->pos_in_central_dir = pos_in_central_dirSaved ;
01766 return err;
01767 }
01768
01769
01770
01771
01772
01773
01774
01775
01776
01777 static int unzlocal_CheckCurrentFileCoherencyHeader (unz_s* s, uInt* piSizeVar,
01778 uLong *poffset_local_extrafield,
01779 uInt *psize_local_extrafield)
01780 {
01781 uLong uMagic,uData,uFlags;
01782 uLong size_filename;
01783 uLong size_extra_field;
01784 int err=UNZ_OK;
01785
01786 *piSizeVar = 0;
01787 *poffset_local_extrafield = 0;
01788 *psize_local_extrafield = 0;
01789
01790 if (fseek(s->file,s->cur_file_info_internal.offset_curfile +
01791 s->byte_before_the_zipfile,SEEK_SET)!=0)
01792 return UNZ_ERRNO;
01793
01794
01795 if (err==UNZ_OK) {
01796 if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK)
01797 err=UNZ_ERRNO;
01798 else if (uMagic!=0x04034b50)
01799 err=UNZ_BADZIPFILE;
01800 }
01801 if (unzlocal_getShort(s->file,&uData) != UNZ_OK)
01802 err=UNZ_ERRNO;
01803
01804
01805
01806
01807 if (unzlocal_getShort(s->file,&uFlags) != UNZ_OK)
01808 err=UNZ_ERRNO;
01809
01810 if (unzlocal_getShort(s->file,&uData) != UNZ_OK)
01811 err=UNZ_ERRNO;
01812 else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compression_method))
01813 err=UNZ_BADZIPFILE;
01814
01815 if ((err==UNZ_OK) && (s->cur_file_info.compression_method!=0) &&
01816 (s->cur_file_info.compression_method!=Z_DEFLATED))
01817 err=UNZ_BADZIPFILE;
01818
01819 if (unzlocal_getLong(s->file,&uData) != UNZ_OK)
01820 err=UNZ_ERRNO;
01821
01822 if (unzlocal_getLong(s->file,&uData) != UNZ_OK)
01823 err=UNZ_ERRNO;
01824 else if ((err==UNZ_OK) && (uData!=s->cur_file_info.crc) &&
01825 ((uFlags & 8)==0))
01826 err=UNZ_BADZIPFILE;
01827
01828 if (unzlocal_getLong(s->file,&uData) != UNZ_OK)
01829 err=UNZ_ERRNO;
01830 else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compressed_size) &&
01831 ((uFlags & 8)==0))
01832 err=UNZ_BADZIPFILE;
01833
01834 if (unzlocal_getLong(s->file,&uData) != UNZ_OK)
01835 err=UNZ_ERRNO;
01836 else if ((err==UNZ_OK) && (uData!=s->cur_file_info.uncompressed_size) &&
01837 ((uFlags & 8)==0))
01838 err=UNZ_BADZIPFILE;
01839
01840
01841 if (unzlocal_getShort(s->file,&size_filename) != UNZ_OK)
01842 err=UNZ_ERRNO;
01843 else if ((err==UNZ_OK) && (size_filename!=s->cur_file_info.size_filename))
01844 err=UNZ_BADZIPFILE;
01845
01846 *piSizeVar += (uInt)size_filename;
01847
01848 if (unzlocal_getShort(s->file,&size_extra_field) != UNZ_OK)
01849 err=UNZ_ERRNO;
01850 *poffset_local_extrafield= s->cur_file_info_internal.offset_curfile +
01851 SIZEZIPLOCALHEADER + size_filename;
01852 *psize_local_extrafield = (uInt)size_extra_field;
01853
01854 *piSizeVar += (uInt)size_extra_field;
01855
01856 return err;
01857 }
01858
01859
01860
01861
01862
01863 extern int unzOpenCurrentFile (unzFile file)
01864 {
01865 int err=UNZ_OK;
01866 int Store;
01867 uInt iSizeVar;
01868 unz_s* s;
01869 file_in_zip_read_info_s* pfile_in_zip_read_info;
01870 uLong offset_local_extrafield;
01871 uInt size_local_extrafield;
01872
01873 if (file==NULL)
01874 return UNZ_PARAMERROR;
01875 s=(unz_s*)file;
01876 if (!s->current_file_ok)
01877 return UNZ_PARAMERROR;
01878
01879 if (s->pfile_in_zip_read != NULL)
01880 unzCloseCurrentFile(file);
01881
01882 if (unzlocal_CheckCurrentFileCoherencyHeader(s,&iSizeVar,
01883 &offset_local_extrafield,&size_local_extrafield)!=UNZ_OK)
01884 return UNZ_BADZIPFILE;
01885
01886 pfile_in_zip_read_info = (file_in_zip_read_info_s*)
01887 ALLOC(sizeof(file_in_zip_read_info_s));
01888 if (pfile_in_zip_read_info==NULL)
01889 return UNZ_INTERNALERROR;
01890
01891 pfile_in_zip_read_info->read_buffer=(char*)ALLOC(UNZ_BUFSIZE);
01892 pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield;
01893 pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield;
01894 pfile_in_zip_read_info->pos_local_extrafield=0;
01895
01896 if (pfile_in_zip_read_info->read_buffer==NULL)
01897 {
01898 TRYFREE(pfile_in_zip_read_info);
01899 return UNZ_INTERNALERROR;
01900 }
01901
01902 pfile_in_zip_read_info->stream_initialised=0;
01903
01904 if ((s->cur_file_info.compression_method!=0) &&
01905 (s->cur_file_info.compression_method!=Z_DEFLATED))
01906 err=UNZ_BADZIPFILE;
01907 Store = s->cur_file_info.compression_method==0;
01908
01909 pfile_in_zip_read_info->crc32_wait=s->cur_file_info.crc;
01910 pfile_in_zip_read_info->crc32=0;
01911 pfile_in_zip_read_info->compression_method =
01912 s->cur_file_info.compression_method;
01913 pfile_in_zip_read_info->file=s->file;
01914 pfile_in_zip_read_info->byte_before_the_zipfile=s->byte_before_the_zipfile;
01915
01916 pfile_in_zip_read_info->stream.total_out = 0;
01917
01918 if (!Store)
01919 {
01920 pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
01921 pfile_in_zip_read_info->stream.zfree = (free_func)0;
01922 pfile_in_zip_read_info->stream.opaque = (voidp)0;
01923
01924 err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS);
01925 if (err == Z_OK)
01926 pfile_in_zip_read_info->stream_initialised=1;
01927
01928
01929
01930
01931
01932
01933
01934 }
01935 pfile_in_zip_read_info->rest_read_compressed =
01936 s->cur_file_info.compressed_size ;
01937 pfile_in_zip_read_info->rest_read_uncompressed =
01938 s->cur_file_info.uncompressed_size ;
01939
01940
01941 pfile_in_zip_read_info->pos_in_zipfile =
01942 s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER +
01943 iSizeVar;
01944
01945 pfile_in_zip_read_info->stream.avail_in = (uInt)0;
01946
01947
01948 s->pfile_in_zip_read = pfile_in_zip_read_info;
01949 return UNZ_OK;
01950 }
01951
01952
01953
01954
01955
01956
01957
01958
01959
01960
01961
01962
01963 extern int unzReadCurrentFile (unzFile file, void *buf, unsigned len)
01964 {
01965 int err=UNZ_OK;
01966 uInt iRead = 0;
01967 unz_s* s;
01968 file_in_zip_read_info_s* pfile_in_zip_read_info;
01969 if (file==NULL)
01970 return UNZ_PARAMERROR;
01971 s=(unz_s*)file;
01972 pfile_in_zip_read_info=s->pfile_in_zip_read;
01973
01974 if (pfile_in_zip_read_info==NULL)
01975 return UNZ_PARAMERROR;
01976
01977
01978 if ((pfile_in_zip_read_info->read_buffer == NULL))
01979 return UNZ_END_OF_LIST_OF_FILE;
01980 if (len==0)
01981 return 0;
01982
01983 pfile_in_zip_read_info->stream.next_out = (Byte*)buf;
01984
01985 pfile_in_zip_read_info->stream.avail_out = (uInt)len;
01986
01987 if (len>pfile_in_zip_read_info->rest_read_uncompressed)
01988 pfile_in_zip_read_info->stream.avail_out =
01989 (uInt)pfile_in_zip_read_info->rest_read_uncompressed;
01990
01991 while (pfile_in_zip_read_info->stream.avail_out>0)
01992 {
01993 if ((pfile_in_zip_read_info->stream.avail_in==0) &&
01994 (pfile_in_zip_read_info->rest_read_compressed>0))
01995 {
01996 uInt uReadThis = UNZ_BUFSIZE;
01997 if (pfile_in_zip_read_info->rest_read_compressed<uReadThis)
01998 uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed;
01999 if (uReadThis == 0)
02000 return UNZ_EOF;
02001 if (s->cur_file_info.compressed_size == pfile_in_zip_read_info->rest_read_compressed)
02002 if (fseek(pfile_in_zip_read_info->file,
02003 pfile_in_zip_read_info->pos_in_zipfile +
02004 pfile_in_zip_read_info->byte_before_the_zipfile,SEEK_SET)!=0)
02005 return UNZ_ERRNO;
02006 if (fread(pfile_in_zip_read_info->read_buffer,uReadThis,1,
02007 pfile_in_zip_read_info->file)!=1)
02008 return UNZ_ERRNO;
02009 pfile_in_zip_read_info->pos_in_zipfile += uReadThis;
02010
02011 pfile_in_zip_read_info->rest_read_compressed-=uReadThis;
02012
02013 pfile_in_zip_read_info->stream.next_in =
02014 (Byte*)pfile_in_zip_read_info->read_buffer;
02015 pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis;
02016 }
02017
02018 if (pfile_in_zip_read_info->compression_method==0)
02019 {
02020 uInt uDoCopy,i ;
02021 if (pfile_in_zip_read_info->stream.avail_out <
02022 pfile_in_zip_read_info->stream.avail_in)
02023 uDoCopy = pfile_in_zip_read_info->stream.avail_out ;
02024 else
02025 uDoCopy = pfile_in_zip_read_info->stream.avail_in ;
02026
02027 for (i=0;i<uDoCopy;i++)
02028 *(pfile_in_zip_read_info->stream.next_out+i) =
02029 *(pfile_in_zip_read_info->stream.next_in+i);
02030
02031
02032
02033
02034 pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy;
02035 pfile_in_zip_read_info->stream.avail_in -= uDoCopy;
02036 pfile_in_zip_read_info->stream.avail_out -= uDoCopy;
02037 pfile_in_zip_read_info->stream.next_out += uDoCopy;
02038 pfile_in_zip_read_info->stream.next_in += uDoCopy;
02039 pfile_in_zip_read_info->stream.total_out += uDoCopy;
02040 iRead += uDoCopy;
02041 }
02042 else
02043 {
02044 uLong uTotalOutBefore,uTotalOutAfter;
02045 const Byte *bufBefore;
02046 uLong uOutThis;
02047 int flush=Z_SYNC_FLUSH;
02048
02049 uTotalOutBefore = pfile_in_zip_read_info->stream.total_out;
02050 bufBefore = pfile_in_zip_read_info->stream.next_out;
02051
02052
02053
02054
02055
02056
02057
02058 err=inflate(&pfile_in_zip_read_info->stream,flush);
02059
02060 uTotalOutAfter = pfile_in_zip_read_info->stream.total_out;
02061 uOutThis = uTotalOutAfter-uTotalOutBefore;
02062
02063
02064
02065
02066
02067 pfile_in_zip_read_info->rest_read_uncompressed -=
02068 uOutThis;
02069
02070 iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);
02071
02072 if (err==Z_STREAM_END)
02073 return (iRead==0) ? UNZ_EOF : iRead;
02074 if (err!=Z_OK)
02075 break;
02076 }
02077 }
02078
02079 if (err==Z_OK)
02080 return iRead;
02081 return err;
02082 }
02083
02084
02085
02086
02087
02088 extern long unztell (unzFile file)
02089 {
02090 unz_s* s;
02091 file_in_zip_read_info_s* pfile_in_zip_read_info;
02092 if (file==NULL)
02093 return UNZ_PARAMERROR;
02094 s=(unz_s*)file;
02095 pfile_in_zip_read_info=s->pfile_in_zip_read;
02096
02097 if (pfile_in_zip_read_info==NULL)
02098 return UNZ_PARAMERROR;
02099
02100 return (long)pfile_in_zip_read_info->stream.total_out;
02101 }
02102
02103
02104
02105
02106
02107 extern int unzeof (unzFile file)
02108 {
02109 unz_s* s;
02110 file_in_zip_read_info_s* pfile_in_zip_read_info;
02111 if (file==NULL)
02112 return UNZ_PARAMERROR;
02113 s=(unz_s*)file;
02114 pfile_in_zip_read_info=s->pfile_in_zip_read;
02115
02116 if (pfile_in_zip_read_info==NULL)
02117 return UNZ_PARAMERROR;
02118
02119 if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
02120 return 1;
02121 else
02122 return 0;
02123 }
02124
02125
02126
02127
02128
02129
02130
02131
02132
02133
02134
02135
02136
02137
02138
02139 extern int unzGetLocalExtrafield (unzFile file,void *buf,unsigned len)
02140 {
02141 unz_s* s;
02142 file_in_zip_read_info_s* pfile_in_zip_read_info;
02143 uInt read_now;
02144 uLong size_to_read;
02145
02146 if (file==NULL)
02147 return UNZ_PARAMERROR;
02148 s=(unz_s*)file;
02149 pfile_in_zip_read_info=s->pfile_in_zip_read;
02150
02151 if (pfile_in_zip_read_info==NULL)
02152 return UNZ_PARAMERROR;
02153
02154 size_to_read = (pfile_in_zip_read_info->size_local_extrafield -
02155 pfile_in_zip_read_info->pos_local_extrafield);
02156
02157 if (buf==NULL)
02158 return (int)size_to_read;
02159
02160 if (len>size_to_read)
02161 read_now = (uInt)size_to_read;
02162 else
02163 read_now = (uInt)len ;
02164
02165 if (read_now==0)
02166 return 0;
02167
02168 if (fseek(pfile_in_zip_read_info->file,
02169 pfile_in_zip_read_info->offset_local_extrafield +
02170 pfile_in_zip_read_info->pos_local_extrafield,SEEK_SET)!=0)
02171 return UNZ_ERRNO;
02172
02173 if (fread(buf,(uInt)size_to_read,1,pfile_in_zip_read_info->file)!=1)
02174 return UNZ_ERRNO;
02175
02176 return (int)read_now;
02177 }
02178
02179
02180
02181
02182
02183 extern int unzCloseCurrentFile (unzFile file)
02184 {
02185 int err=UNZ_OK;
02186
02187 unz_s* s;
02188 file_in_zip_read_info_s* pfile_in_zip_read_info;
02189 if (file==NULL)
02190 return UNZ_PARAMERROR;
02191 s=(unz_s*)file;
02192 pfile_in_zip_read_info=s->pfile_in_zip_read;
02193
02194 if (pfile_in_zip_read_info==NULL)
02195 return UNZ_PARAMERROR;
02196
02197
02198
02199
02200
02201
02202
02203
02204
02205 TRYFREE(pfile_in_zip_read_info->read_buffer);
02206 pfile_in_zip_read_info->read_buffer = NULL;
02207 if (pfile_in_zip_read_info->stream_initialised)
02208 inflateEnd(&pfile_in_zip_read_info->stream);
02209
02210 pfile_in_zip_read_info->stream_initialised = 0;
02211 TRYFREE(pfile_in_zip_read_info);
02212
02213 s->pfile_in_zip_read=NULL;
02214
02215 return err;
02216 }
02217
02218
02219
02220
02221
02222
02223
02224 extern int unzGetGlobalComment (unzFile file, char *szComment, uLong uSizeBuf)
02225 {
02226 unz_s* s;
02227 uLong uReadThis ;
02228 if (file==NULL)
02229 return UNZ_PARAMERROR;
02230 s=(unz_s*)file;
02231
02232 uReadThis = uSizeBuf;
02233 if (uReadThis>s->gi.size_comment)
02234 uReadThis = s->gi.size_comment;
02235
02236 if (fseek(s->file,s->central_pos+22,SEEK_SET)!=0)
02237 return UNZ_ERRNO;
02238
02239 if (uReadThis>0)
02240 {
02241 *szComment='\0';
02242 if (fread(szComment,(uInt)uReadThis,1,s->file)!=1)
02243 return UNZ_ERRNO;
02244 }
02245
02246 if ((szComment != NULL) && (uSizeBuf > s->gi.size_comment))
02247 *(szComment+s->gi.size_comment)='\0';
02248 return (int)uReadThis;
02249 }
02250
02251
02252
02253
02254
02255
02256
02257
02258
02259
02260
02261 struct inflate_blocks_state;
02262 typedef struct inflate_blocks_state inflate_blocks_statef;
02263
02264 static inflate_blocks_statef * inflate_blocks_new OF((
02265 z_streamp z,
02266 check_func c,
02267 uInt w));
02268
02269 static int inflate_blocks OF((
02270 inflate_blocks_statef *,
02271 z_streamp ,
02272 int));
02273
02274 static void inflate_blocks_reset OF((
02275 inflate_blocks_statef *,
02276 z_streamp ,
02277 uLong *));
02278
02279 static int inflate_blocks_free OF((
02280 inflate_blocks_statef *,
02281 z_streamp));
02282
02283 #if 0
02284 static void inflate_set_dictionary OF((
02285 inflate_blocks_statef *s,
02286 const Byte *d,
02287 uInt n));
02288
02289 static int inflate_blocks_sync_point OF((
02290 inflate_blocks_statef *s));
02291 #endif
02292
02293
02294 #define exop word.what.Exop
02295 #define bits word.what.Bits
02296
02297
02298 static const uInt border[] = {
02299 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
02300
02301
02302
02303
02304
02305
02306
02307
02308
02309
02310
02311
02312
02313
02314 typedef struct inflate_huft_s inflate_huft;
02315
02316 struct inflate_huft_s {
02317 union {
02318 struct {
02319 Byte Exop;
02320 Byte Bits;
02321 } what;
02322 uInt pad;
02323 } word;
02324 uInt base;
02325
02326 };
02327
02328
02329
02330
02331
02332
02333 #define MANY 1440
02334
02335 static int inflate_trees_bits OF((
02336 uInt *,
02337 uInt *,
02338 inflate_huft * *,
02339 inflate_huft *,
02340 z_streamp));
02341
02342 static int inflate_trees_dynamic OF((
02343 uInt,
02344 uInt,
02345 uInt *,
02346 uInt *,
02347 uInt *,
02348 inflate_huft * *,
02349 inflate_huft * *,
02350 inflate_huft *,
02351 z_streamp));
02352
02353 static int inflate_trees_fixed OF((
02354 uInt *,
02355 uInt *,
02356 inflate_huft * *,
02357 inflate_huft * *,
02358 z_streamp));
02359
02360
02361
02362
02363
02364
02365
02366
02367
02368
02369
02370
02371 struct inflate_codes_state;
02372 typedef struct inflate_codes_state inflate_codes_statef;
02373
02374 static inflate_codes_statef *inflate_codes_new OF((
02375 uInt, uInt,
02376 inflate_huft *, inflate_huft *,
02377 z_streamp ));
02378
02379 static int inflate_codes OF((
02380 inflate_blocks_statef *,
02381 z_streamp ,
02382 int));
02383
02384 static void inflate_codes_free OF((
02385 inflate_codes_statef *,
02386 z_streamp ));
02387
02388
02389
02390
02391
02392
02393
02394
02395
02396
02397
02398 #ifndef _INFUTIL_H
02399 #define _INFUTIL_H
02400
02401 typedef enum {
02402 TYPE,
02403 LENS,
02404 STORED,
02405 TABLE,
02406 BTREE,
02407 DTREE,
02408 CODES,
02409 DRY,
02410 DONE,
02411 BAD}
02412 inflate_block_mode;
02413
02414
02415 struct inflate_blocks_state {
02416
02417
02418 inflate_block_mode mode;
02419
02420
02421 union {
02422 uInt left;
02423 struct {
02424 uInt table;
02425 uInt index;
02426 uInt *blens;
02427 uInt bb;
02428 inflate_huft *tb;
02429 } trees;
02430 struct {
02431 inflate_codes_statef
02432 *codes;
02433 } decode;
02434 } sub;
02435 uInt last;
02436
02437
02438 uInt bitk;
02439 uLong bitb;
02440 inflate_huft *hufts;
02441 Byte *window;
02442 Byte *end;
02443 Byte *read;
02444 Byte *write;
02445 check_func checkfn;
02446 uLong check;
02447
02448 };
02449
02450
02451
02452
02453 #define UPDBITS {s->bitb=b;s->bitk=k;}
02454 #define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;}
02455 #define UPDOUT {s->write=q;}
02456 #define UPDATE {UPDBITS UPDIN UPDOUT}
02457 #define LEAVE {UPDATE return inflate_flush(s,z,r);}
02458
02459 #define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
02460 #define NEEDBYTE {if(n)r=Z_OK;else LEAVE}
02461 #define NEXTBYTE (n--,*p++)
02462 #define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
02463 #define DUMPBITS(j) {b>>=(j);k-=(j);}
02464
02465 #define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)
02466 #define LOADOUT {q=s->write;m=(uInt)WAVAIL;}
02467 #define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}}
02468 #define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}
02469 #define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;}
02470 #define OUTBYTE(a) {*q++=(Byte)(a);m--;}
02471
02472 #define LOAD {LOADIN LOADOUT}
02473
02474
02475 static uInt inflate_mask[17];
02476
02477
02478 static int inflate_flush OF((
02479 inflate_blocks_statef *,
02480 z_streamp ,
02481 int));
02482
02483 #endif
02484
02485
02486
02487
02488
02489
02490
02491
02492
02493
02494
02495
02496
02497
02498
02499
02500
02501
02502
02503
02504
02505
02506
02507
02508
02509
02510
02511
02512
02513
02514
02515
02516
02517
02518
02519
02520
02521
02522
02523
02524
02525
02526
02527
02528
02529
02530
02531
02532 void inflate_blocks_reset(inflate_blocks_statef *s, z_streamp z, uLong *c)
02533 {
02534 if (c != Z_NULL)
02535 *c = s->check;
02536 if (s->mode == BTREE || s->mode == DTREE)
02537 ZFREE(z, s->sub.trees.blens);
02538 if (s->mode == CODES)
02539 inflate_codes_free(s->sub.decode.codes, z);
02540 s->mode = TYPE;
02541 s->bitk = 0;
02542 s->bitb = 0;
02543 s->read = s->write = s->window;
02544 if (s->checkfn != Z_NULL)
02545 z->adler = s->check = (*s->checkfn)(0L, (const Byte *)Z_NULL, 0);
02546 Tracev(("inflate: blocks reset\n"));
02547 }
02548
02549
02550 inflate_blocks_statef *inflate_blocks_new(z_streamp z, check_func c, uInt w)
02551 {
02552 inflate_blocks_statef *s;
02553
02554 if ((s = (inflate_blocks_statef *)ZALLOC
02555 (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
02556 return s;
02557 if ((s->hufts =
02558 (inflate_huft *)ZALLOC(z, sizeof(inflate_huft), MANY)) == Z_NULL)
02559 {
02560 ZFREE(z, s);
02561 return Z_NULL;
02562 }
02563 if ((s->window = (Byte *)ZALLOC(z, 1, w)) == Z_NULL)
02564 {
02565 ZFREE(z, s->hufts);
02566 ZFREE(z, s);
02567 return Z_NULL;
02568 }
02569 s->end = s->window + w;
02570 s->checkfn = c;
02571 s->mode = TYPE;
02572 Tracev(("inflate: blocks allocated\n"));
02573 inflate_blocks_reset(s, z, Z_NULL);
02574 return s;
02575 }
02576
02577
02578 int inflate_blocks(inflate_blocks_statef *s, z_streamp z, int r)
02579 {
02580 uInt t;
02581 uLong b;
02582 uInt k;
02583 Byte *p;
02584 uInt n;
02585 Byte *q;
02586 uInt m;
02587
02588
02589 LOAD
02590
02591
02592 while (1) switch (s->mode)
02593 {
02594 case TYPE:
02595 NEEDBITS(3)
02596 t = (uInt)b & 7;
02597 s->last = t & 1;
02598 switch (t >> 1)
02599 {
02600 case 0:
02601 Tracev(("inflate: stored block%s\n",
02602 s->last ? " (last)" : ""));
02603 DUMPBITS(3)
02604 t = k & 7;
02605 DUMPBITS(t)
02606 s->mode = LENS;
02607 break;
02608 case 1:
02609 Tracev(("inflate: fixed codes block%s\n",
02610 s->last ? " (last)" : ""));
02611 {
02612 uInt bl, bd;
02613 inflate_huft *tl, *td;
02614 inflate_trees_fixed(&bl, &bd, &tl, &td, z);
02615 s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
02616 if (s->sub.decode.codes == Z_NULL)
02617 {
02618 r = Z_MEM_ERROR;
02619 LEAVE
02620 }
02621 }
02622 DUMPBITS(3)
02623 s->mode = CODES;
02624 break;
02625 case 2:
02626 Tracev(("inflate: dynamic codes block%s\n",
02627 s->last ? " (last)" : ""));
02628 DUMPBITS(3)
02629 s->mode = TABLE;
02630 break;
02631 case 3:
02632 DUMPBITS(3)
02633 s->mode = BAD;
02634 z->msg = (char*)"invalid block type";
02635 r = Z_DATA_ERROR;
02636 LEAVE
02637 }
02638 break;
02639 case LENS:
02640 NEEDBITS(32)
02641 if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
02642 {
02643 s->mode = BAD;
02644 z->msg = (char*)"invalid stored block lengths";
02645 r = Z_DATA_ERROR;
02646 LEAVE
02647 }
02648 s->sub.left = (uInt)b & 0xffff;
02649 b = k = 0;
02650 Tracev(("inflate: stored length %u\n", s->sub.left));
02651 s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);
02652 break;
02653 case STORED:
02654 if (n == 0)
02655 LEAVE
02656 NEEDOUT
02657 t = s->sub.left;
02658 if (t > n) t = n;
02659 if (t > m) t = m;
02660 zmemcpy(q, p, t);
02661 p += t; n -= t;
02662 q += t; m -= t;
02663 if ((s->sub.left -= t) != 0)
02664 break;
02665 Tracev(("inflate: stored end, %lu total out\n",
02666 z->total_out + (q >= s->read ? q - s->read :
02667 (s->end - s->read) + (q - s->window))));
02668 s->mode = s->last ? DRY : TYPE;
02669 break;
02670 case TABLE:
02671 NEEDBITS(14)
02672 s->sub.trees.table = t = (uInt)b & 0x3fff;
02673 #ifndef PKZIP_BUG_WORKAROUND
02674 if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
02675 {
02676 s->mode = BAD;
02677 z->msg = (char*)"too many length or distance symbols";
02678 r = Z_DATA_ERROR;
02679 LEAVE
02680 }
02681 #endif
02682 t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
02683 if ((s->sub.trees.blens = (uInt*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
02684 {
02685 r = Z_MEM_ERROR;
02686 LEAVE
02687 }
02688 DUMPBITS(14)
02689 s->sub.trees.index = 0;
02690 Tracev(("inflate: table sizes ok\n"));
02691 s->mode = BTREE;
02692 case BTREE:
02693 while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
02694 {
02695 NEEDBITS(3)
02696 s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
02697 DUMPBITS(3)
02698 }
02699 while (s->sub.trees.index < 19)
02700 s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
02701 s->sub.trees.bb = 7;
02702 t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
02703 &s->sub.trees.tb, s->hufts, z);
02704 if (t != Z_OK)
02705 {
02706 ZFREE(z, s->sub.trees.blens);
02707 r = t;
02708 if (r == Z_DATA_ERROR)
02709 s->mode = BAD;
02710 LEAVE
02711 }
02712 s->sub.trees.index = 0;
02713 Tracev(("inflate: bits tree ok\n"));
02714 s->mode = DTREE;
02715 case DTREE:
02716 while (t = s->sub.trees.table,
02717 s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
02718 {
02719 inflate_huft *h;
02720 uInt i, j, c;
02721
02722 t = s->sub.trees.bb;
02723 NEEDBITS(t)
02724 h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
02725 t = h->bits;
02726 c = h->base;
02727 if (c < 16)
02728 {
02729 DUMPBITS(t)
02730 s->sub.trees.blens[s->sub.trees.index++] = c;
02731 }
02732 else
02733 {
02734 i = c == 18 ? 7 : c - 14;
02735 j = c == 18 ? 11 : 3;
02736 NEEDBITS(t + i)
02737 DUMPBITS(t)
02738 j += (uInt)b & inflate_mask[i];
02739 DUMPBITS(i)
02740 i = s->sub.trees.index;
02741 t = s->sub.trees.table;
02742 if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
02743 (c == 16 && i < 1))
02744 {
02745 ZFREE(z, s->sub.trees.blens);
02746 s->mode = BAD;
02747 z->msg = (char*)"invalid bit length repeat";
02748 r = Z_DATA_ERROR;
02749 LEAVE
02750 }
02751 c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
02752 do {
02753 s->sub.trees.blens[i++] = c;
02754 } while (--j);
02755 s->sub.trees.index = i;
02756 }
02757 }
02758 s->sub.trees.tb = Z_NULL;
02759 {
02760 uInt bl, bd;
02761 inflate_huft *tl, *td;
02762 inflate_codes_statef *c;
02763
02764 tl = NULL;
02765 td = NULL;
02766 bl = 9;
02767 bd = 6;
02768 t = s->sub.trees.table;
02769 t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
02770 s->sub.trees.blens, &bl, &bd, &tl, &td,
02771 s->hufts, z);
02772 ZFREE(z, s->sub.trees.blens);
02773 if (t != Z_OK)
02774 {
02775 if (t == (uInt)Z_DATA_ERROR)
02776 s->mode = BAD;
02777 r = t;
02778 LEAVE
02779 }
02780 Tracev(("inflate: trees ok\n"));
02781 if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
02782 {
02783 r = Z_MEM_ERROR;
02784 LEAVE
02785 }
02786 s->sub.decode.codes = c;
02787 }
02788 s->mode = CODES;
02789 case CODES:
02790 UPDATE
02791 if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
02792 return inflate_flush(s, z, r);
02793 r = Z_OK;
02794 inflate_codes_free(s->sub.decode.codes, z);
02795 LOAD
02796 Tracev(("inflate: codes end, %lu total out\n",
02797 z->total_out + (q >= s->read ? q - s->read :
02798 (s->end - s->read) + (q - s->window))));
02799 if (!s->last)
02800 {
02801 s->mode = TYPE;
02802 break;
02803 }
02804 s->mode = DRY;
02805 case DRY:
02806 FLUSH
02807 if (s->read != s->write)
02808 LEAVE
02809 s->mode = DONE;
02810 case DONE:
02811 r = Z_STREAM_END;
02812 LEAVE
02813 case BAD:
02814 r = Z_DATA_ERROR;
02815 LEAVE
02816 default:
02817 r = Z_STREAM_ERROR;
02818 LEAVE
02819 }
02820 }
02821
02822
02823 int inflate_blocks_free(inflate_blocks_statef *s, z_streamp z)
02824 {
02825 inflate_blocks_reset(s, z, Z_NULL);
02826 ZFREE(z, s->window);
02827 ZFREE(z, s->hufts);
02828 ZFREE(z, s);
02829 Tracev(("inflate: blocks freed\n"));
02830 return Z_OK;
02831 }
02832
02833 #if 0
02834 void inflate_set_dictionary(inflate_blocks_statef *s, const Byte *d, uInt n)
02835 {
02836 zmemcpy(s->window, d, n);
02837 s->read = s->write = s->window + n;
02838 }
02839
02840
02841
02842
02843
02844 int inflate_blocks_sync_point(inflate_blocks_statef *s)
02845 {
02846 return s->mode == LENS;
02847 }
02848 #endif
02849
02850
02851
02852 static uInt inflate_mask[17] = {
02853 0x0000,
02854 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
02855 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
02856 };
02857
02858
02859
02860 int inflate_flush(inflate_blocks_statef *s, z_streamp z, int r)
02861 {
02862 uInt n;
02863 Byte *p;
02864 Byte *q;
02865
02866
02867 p = z->next_out;
02868 q = s->read;
02869
02870
02871 n = (uInt)((q <= s->write ? s->write : s->end) - q);
02872 if (n > z->avail_out) n = z->avail_out;
02873 if (n && r == Z_BUF_ERROR) r = Z_OK;
02874
02875
02876 z->avail_out -= n;
02877 z->total_out += n;
02878
02879
02880 if (s->checkfn != Z_NULL)
02881 z->adler = s->check = (*s->checkfn)(s->check, q, n);
02882
02883
02884 zmemcpy(p, q, n);
02885 p += n;
02886 q += n;
02887
02888
02889 if (q == s->end)
02890 {
02891
02892 q = s->window;
02893 if (s->write == s->end)
02894 s->write = s->window;
02895
02896
02897 n = (uInt)(s->write - q);
02898 if (n > z->avail_out) n = z->avail_out;
02899 if (n && r == Z_BUF_ERROR) r = Z_OK;
02900
02901
02902 z->avail_out -= n;
02903 z->total_out += n;
02904
02905
02906 if (s->checkfn != Z_NULL)
02907 z->adler = s->check = (*s->checkfn)(s->check, q, n);
02908
02909
02910 zmemcpy(p, q, n);
02911 p += n;
02912 q += n;
02913 }
02914
02915
02916 z->next_out = p;
02917 s->read = q;
02918
02919
02920 return r;
02921 }
02922
02923
02924
02925
02926
02927
02928 static const char inflate_copyright[] =
02929 " inflate 1.1.3 Copyright 1995-1998 Mark Adler ";
02930
02931
02932
02933
02934
02935
02936
02937
02938 #define exop word.what.Exop
02939 #define bits word.what.Bits
02940
02941
02942 static int huft_build OF((
02943 uInt *,
02944 uInt,
02945 uInt,
02946 const uInt *,
02947 const uInt *,
02948 inflate_huft **,
02949 uInt *,
02950 inflate_huft *,
02951 uInt *,
02952 uInt * ));
02953
02954
02955 static const uInt cplens[31] = {
02956 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
02957 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
02958
02959 static const uInt cplext[31] = {
02960 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
02961 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112};
02962 static const uInt cpdist[30] = {
02963 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
02964 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
02965 8193, 12289, 16385, 24577};
02966 static const uInt cpdext[30] = {
02967 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
02968 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
02969 12, 12, 13, 13};
02970
02971
02972
02973
02974
02975
02976
02977
02978
02979
02980
02981
02982
02983
02984
02985
02986
02987
02988
02989
02990
02991
02992
02993
02994
02995
02996
02997
02998
02999
03000
03001
03002
03003
03004
03005 #define BMAX 15
03006
03007 static int huft_build(uInt *b, uInt n, uInt s, const uInt *d, const uInt *e, inflate_huft ** t, uInt *m, inflate_huft *hp, uInt *hn, uInt *v)
03008
03009
03010
03011
03012
03013
03014
03015
03016
03017
03018
03019
03020
03021
03022
03023 {
03024
03025 uInt a;
03026 uInt c[BMAX+1];
03027 uInt f;
03028 int g;
03029 int h;
03030 register uInt i;
03031 register uInt j;
03032 register int k;
03033 int l;
03034 uInt mask;
03035 register uInt *p;
03036 inflate_huft *q;
03037 struct inflate_huft_s r;
03038 inflate_huft *u[BMAX];
03039 register int w;
03040 uInt x[BMAX+1];
03041 uInt *xp;
03042 int y;
03043 uInt z;
03044
03045
03046
03047 p = c;
03048 #define C0 *p++ = 0;
03049 #define C2 C0 C0 C0 C0
03050 #define C4 C2 C2 C2 C2
03051 C4
03052 p = b; i = n;
03053 do {
03054 c[*p++]++;
03055 } while (--i);
03056 if (c[0] == n)
03057 {
03058 *t = (inflate_huft *)Z_NULL;
03059 *m = 0;
03060 return Z_OK;
03061 }
03062
03063
03064
03065 l = *m;
03066 for (j = 1; j <= BMAX; j++)
03067 if (c[j])
03068 break;
03069 k = j;
03070 if ((uInt)l < j)
03071 l = j;
03072 for (i = BMAX; i; i--)
03073 if (c[i])
03074 break;
03075 g = i;
03076 if ((uInt)l > i)
03077 l = i;
03078 *m = l;
03079
03080
03081
03082 for (y = 1 << j; j < i; j++, y <<= 1)
03083 if ((y -= c[j]) < 0)
03084 return Z_DATA_ERROR;
03085 if ((y -= c[i]) < 0)
03086 return Z_DATA_ERROR;
03087 c[i] += y;
03088
03089
03090
03091 x[1] = j = 0;
03092 p = c + 1; xp = x + 2;
03093 while (--i) {
03094 *xp++ = (j += *p++);
03095 }
03096
03097
03098
03099 p = b; i = 0;
03100 do {
03101 if ((j = *p++) != 0)
03102 v[x[j]++] = i;
03103 } while (++i < n);
03104 n = x[g];
03105
03106
03107
03108 x[0] = i = 0;
03109 p = v;
03110 h = -1;
03111 w = -l;
03112 u[0] = (inflate_huft *)Z_NULL;
03113 q = (inflate_huft *)Z_NULL;
03114 z = 0;
03115
03116
03117 for (; k <= g; k++)
03118 {
03119 a = c[k];
03120 while (a--)
03121 {
03122
03123
03124 while (k > w + l)
03125 {
03126 h++;
03127 w += l;
03128
03129
03130 z = g - w;
03131 z = z > (uInt)l ? l : z;
03132 if ((f = 1 << (j = k - w)) > a + 1)
03133 {
03134 f -= a + 1;
03135 xp = c + k;
03136 if (j < z)
03137 while (++j < z)
03138 {
03139 if ((f <<= 1) <= *++xp)
03140 break;
03141 f -= *xp;
03142 }
03143 }
03144 z = 1 << j;
03145
03146
03147 if (*hn + z > MANY)
03148 return Z_MEM_ERROR;
03149 u[h] = q = hp + *hn;
03150 *hn += z;
03151
03152
03153 if (h)
03154 {
03155 x[h] = i;
03156 r.bits = (Byte)l;
03157 r.exop = (Byte)j;
03158 j = i >> (w - l);
03159 r.base = (uInt)(q - u[h-1] - j);
03160 u[h-1][j] = r;
03161 }
03162 else
03163 *t = q;
03164 }
03165
03166
03167 r.bits = (Byte)(k - w);
03168 if (p >= v + n)
03169 r.exop = 128 + 64;
03170 else if (*p < s)
03171 {
03172 r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);
03173 r.base = *p++;
03174 }
03175 else
03176 {
03177 r.exop = (Byte)(e[*p - s] + 16 + 64);
03178 r.base = d[*p++ - s];
03179 }
03180
03181
03182 f = 1 << (k - w);
03183 for (j = i >> w; j < z; j += f)
03184 q[j] = r;
03185
03186
03187 for (j = 1 << (k - 1); i & j; j >>= 1)
03188 i ^= j;
03189 i ^= j;
03190
03191
03192 mask = (1 << w) - 1;
03193 while ((i & mask) != x[h])
03194 {
03195 h--;
03196 w -= l;
03197 mask = (1 << w) - 1;
03198 }
03199 }
03200 }
03201
03202
03203
03204 return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
03205 }
03206
03207
03208 int inflate_trees_bits(uInt *c, uInt *bb, inflate_huft * *tb, inflate_huft *hp, z_streamp z)
03209
03210
03211
03212
03213
03214 {
03215 int r;
03216 uInt hn = 0;
03217 uInt *v;
03218
03219 if ((v = (uInt*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL)
03220 return Z_MEM_ERROR;
03221 r = huft_build(c, 19, 19, (uInt*)Z_NULL, (uInt*)Z_NULL,
03222 tb, bb, hp, &hn, v);
03223 if (r == Z_DATA_ERROR)
03224 z->msg = (char*)"oversubscribed dynamic bit lengths tree";
03225 else if (r == Z_BUF_ERROR || *bb == 0)
03226 {
03227 z->msg = (char*)"incomplete dynamic bit lengths tree";
03228 r = Z_DATA_ERROR;
03229 }
03230 ZFREE(z, v);
03231 return r;
03232 }
03233
03234
03235 int inflate_trees_dynamic(uInt nl, uInt nd, uInt *c, uInt *bl, uInt *bd, inflate_huft * *tl, inflate_huft * *td, inflate_huft *hp, z_streamp z)
03236
03237
03238
03239
03240
03241
03242
03243
03244
03245 {
03246 int r;
03247 uInt hn = 0;
03248 uInt *v;
03249
03250
03251 if ((v = (uInt*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
03252 return Z_MEM_ERROR;
03253
03254
03255 r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
03256 if (r != Z_OK || *bl == 0)
03257 {
03258 if (r == Z_DATA_ERROR)
03259 z->msg = (char*)"oversubscribed literal/length tree";
03260 else if (r != Z_MEM_ERROR)
03261 {
03262 z->msg = (char*)"incomplete literal/length tree";
03263 r = Z_DATA_ERROR;
03264 }
03265 ZFREE(z, v);
03266 return r;
03267 }
03268
03269
03270 r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
03271 if (r != Z_OK || (*bd == 0 && nl > 257))
03272 {
03273 if (r == Z_DATA_ERROR)
03274 z->msg = (char*)"oversubscribed distance tree";
03275 else if (r == Z_BUF_ERROR) {
03276 #ifdef PKZIP_BUG_WORKAROUND
03277 r = Z_OK;
03278 }
03279 #else
03280 z->msg = (char*)"incomplete distance tree";
03281 r = Z_DATA_ERROR;
03282 }
03283 else if (r != Z_MEM_ERROR)
03284 {
03285 z->msg = (char*)"empty distance tree with lengths";
03286 r = Z_DATA_ERROR;
03287 }
03288 ZFREE(z, v);
03289 return r;
03290 #endif
03291 }
03292
03293
03294 ZFREE(z, v);
03295 return Z_OK;
03296 }
03297
03298
03299
03300
03301
03302
03303
03304
03305
03306
03307 static uInt fixed_bl = 9;
03308 static uInt fixed_bd = 5;
03309 static inflate_huft fixed_tl[] = {
03310 {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
03311 {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},192},
03312 {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},160},
03313 {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},224},
03314 {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},144},
03315 {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},208},
03316 {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},176},
03317 {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},240},
03318 {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
03319 {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},200},
03320 {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},168},
03321 {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},232},
03322 {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},152},
03323 {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},216},
03324 {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},184},
03325 {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},248},
03326 {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
03327 {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},196},
03328 {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},164},
03329 {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},228},
03330 {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},148},
03331 {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},212},
03332 {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},180},
03333 {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},244},
03334 {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
03335 {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},204},
03336 {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},172},
03337 {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},236},
03338 {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},156},
03339 {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},220},
03340 {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},188},
03341 {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},252},
03342 {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
03343 {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},194},
03344 {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},162},
03345 {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},226},
03346 {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},146},
03347 {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},210},
03348 {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},178},
03349 {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},242},
03350 {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
03351 {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},202},
03352 {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},170},
03353 {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},234},
03354 {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},154},
03355 {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},218},
03356 {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},186},
03357 {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},250},
03358 {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
03359 {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},198},
03360 {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},166},
03361 {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},230},
03362 {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},150},
03363 {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},214},
03364 {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},182},
03365 {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},246},
03366 {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
03367 {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},206},
03368 {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},174},
03369 {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},238},
03370 {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},158},
03371 {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},222},
03372 {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},190},
03373 {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},254},
03374 {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
03375 {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},193},
03376 {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},161},
03377 {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},225},
03378 {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},145},
03379 {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},209},
03380 {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},177},
03381 {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},241},
03382 {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
03383 {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},201},
03384 {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},169},
03385 {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},233},
03386 {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},153},
03387 {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},217},
03388 {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},185},
03389 {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},249},
03390 {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
03391 {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},197},
03392 {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},165},
03393 {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},229},
03394 {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},149},
03395 {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},213},
03396 {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},181},
03397 {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},245},
03398 {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
03399 {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},205},
03400 {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},173},
03401 {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},237},
03402 {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},157},
03403 {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},221},
03404 {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},189},
03405 {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},253},
03406 {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
03407 {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},195},
03408 {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},163},
03409 {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},227},
03410 {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},147},
03411 {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},211},
03412 {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},179},
03413 {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},243},
03414 {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
03415 {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},203},
03416 {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},171},
03417 {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},235},
03418 {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},155},
03419 {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},219},
03420 {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},187},
03421 {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},251},
03422 {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
03423 {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},199},
03424 {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},167},
03425 {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},231},
03426 {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},151},
03427 {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},215},
03428 {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},183},
03429 {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},247},
03430 {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
03431 {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},207},
03432 {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},175},
03433 {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},239},
03434 {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},159},
03435 {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},223},
03436 {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},191},
03437 {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},255}
03438 };
03439 static inflate_huft fixed_td[] = {
03440 {{{80,5}},1}, {{{87,5}},257}, {{{83,5}},17}, {{{91,5}},4097},
03441 {{{81,5}},5}, {{{89,5}},1025}, {{{85,5}},65}, {{{93,5}},16385},
03442 {{{80,5}},3}, {{{88,5}},513}, {{{84,5}},33}, {{{92,5}},8193},
03443 {{{82,5}},9}, {{{90,5}},2049}, {{{86,5}},129}, {{{192,5}},24577},
03444 {{{80,5}},2}, {{{87,5}},385}, {{{83,5}},25}, {{{91,5}},6145},
03445 {{{81,5}},7}, {{{89,5}},1537}, {{{85,5}},97}, {{{93,5}},24577},
03446 {{{80,5}},4}, {{{88,5}},769}, {{{84,5}},49}, {{{92,5}},12289},
03447 {{{82,5}},13}, {{{90,5}},3073}, {{{86,5}},193}, {{{192,5}},24577}
03448 };
03449
03450 int inflate_trees_fixed(uInt *bl, uInt *bd, inflate_huft * *tl, inflate_huft * *td, z_streamp z)
03451
03452
03453
03454
03455
03456 {
03457 *bl = fixed_bl;
03458 *bd = fixed_bd;
03459 *tl = fixed_tl;
03460 *td = fixed_td;
03461 return Z_OK;
03462 }
03463
03464
03465 #define exop word.what.Exop
03466 #define bits word.what.Bits
03467
03468
03469 #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
03470 #define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;}
03471
03472
03473
03474
03475
03476
03477 static int inflate_fast(uInt bl, uInt bd, inflate_huft *tl, inflate_huft *td, inflate_blocks_statef *s, z_streamp z)
03478 {
03479 inflate_huft *t;
03480 uInt e;
03481 uLong b;
03482 uInt k;
03483 Byte *p;
03484 uInt n;
03485 Byte *q;
03486 uInt m;
03487 uInt ml;
03488 uInt md;
03489 uInt c;
03490 uInt d;
03491 Byte *r;
03492
03493
03494 LOAD
03495
03496
03497 ml = inflate_mask[bl];
03498 md = inflate_mask[bd];
03499
03500
03501 do {
03502
03503 GRABBITS(20)
03504 if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
03505 {
03506 DUMPBITS(t->bits)
03507 Tracevv((t->base >= 0x20 && t->base < 0x7f ?
03508 "inflate: * literal '%c'\n" :
03509 "inflate: * literal 0x%02x\n", t->base));
03510 *q++ = (Byte)t->base;
03511 m--;
03512 continue;
03513 }
03514 do {
03515 DUMPBITS(t->bits)
03516 if (e & 16)
03517 {
03518
03519 e &= 15;
03520 c = t->base + ((uInt)b & inflate_mask[e]);
03521 DUMPBITS(e)
03522 Tracevv(("inflate: * length %u\n", c));
03523
03524
03525 GRABBITS(15);
03526 e = (t = td + ((uInt)b & md))->exop;
03527 do {
03528 DUMPBITS(t->bits)
03529 if (e & 16)
03530 {
03531
03532 e &= 15;
03533 GRABBITS(e)
03534 d = t->base + ((uInt)b & inflate_mask[e]);
03535 DUMPBITS(e)
03536 Tracevv(("inflate: * distance %u\n", d));
03537
03538
03539 m -= c;
03540 if ((uInt)(q - s->window) >= d)
03541 {
03542 r = q - d;
03543 *q++ = *r++; c--;
03544 *q++ = *r++; c--;
03545 }
03546 else
03547 {
03548 e = d - (uInt)(q - s->window);
03549 r = s->end - e;
03550 if (c > e)
03551 {
03552 c -= e;
03553 do {
03554 *q++ = *r++;
03555 } while (--e);
03556 r = s->window;
03557 }
03558 }
03559 do {
03560 *q++ = *r++;
03561 } while (--c);
03562 break;
03563 }
03564 else if ((e & 64) == 0)
03565 {
03566 t += t->base;
03567 e = (t += ((uInt)b & inflate_mask[e]))->exop;
03568 }
03569 else
03570 {
03571 z->msg = (char*)"invalid distance code";
03572 UNGRAB
03573 UPDATE
03574 return Z_DATA_ERROR;
03575 }
03576 } while (1);
03577 break;
03578 }
03579 if ((e & 64) == 0)
03580 {
03581 t += t->base;
03582 if ((e = (t += ((uInt)b & inflate_mask[e]))->exop) == 0)
03583 {
03584 DUMPBITS(t->bits)
03585 Tracevv((t->base >= 0x20 && t->base < 0x7f ?
03586 "inflate: * literal '%c'\n" :
03587 "inflate: * literal 0x%02x\n", t->base));
03588 *q++ = (Byte)t->base;
03589 m--;
03590 break;
03591 }
03592 }
03593 else if (e & 32)
03594 {
03595 Tracevv(("inflate: * end of block\n"));
03596 UNGRAB
03597 UPDATE
03598 return Z_STREAM_END;
03599 }
03600 else
03601 {
03602 z->msg = (char*)"invalid literal/length code";
03603 UNGRAB
03604 UPDATE
03605 return Z_DATA_ERROR;
03606 }
03607 } while (1);
03608 } while (m >= 258 && n >= 10);
03609
03610
03611 UNGRAB
03612 UPDATE
03613 return Z_OK;
03614 }
03615
03616
03617
03618
03619
03620
03621
03622 #define exop word.what.Exop
03623 #define bits word.what.Bits
03624
03625 typedef enum {
03626 START,
03627 LEN,
03628 LENEXT,
03629 DIST,
03630 DISTEXT,
03631 COPY,
03632 LIT,
03633 WASH,
03634 END,
03635 BADCODE}
03636 inflate_codes_mode;
03637
03638
03639 struct inflate_codes_state {
03640
03641
03642 inflate_codes_mode mode;
03643
03644
03645 uInt len;
03646 union {
03647 struct {
03648 inflate_huft *tree;
03649 uInt need;
03650 } code;
03651 uInt lit;
03652 struct {
03653 uInt get;
03654 uInt dist;
03655 } copy;
03656 } sub;
03657
03658
03659 Byte lbits;
03660 Byte dbits;
03661 inflate_huft *ltree;
03662 inflate_huft *dtree;
03663
03664 };
03665
03666
03667 inflate_codes_statef *inflate_codes_new(uInt bl, uInt bd, inflate_huft *tl, inflate_huft *td, z_streamp z)
03668 {
03669 inflate_codes_statef *c;
03670
03671 if ((c = (inflate_codes_statef *)
03672 ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
03673 {
03674 c->mode = START;
03675 c->lbits = (Byte)bl;
03676 c->dbits = (Byte)bd;
03677 c->ltree = tl;
03678 c->dtree = td;
03679 Tracev(("inflate: codes new\n"));
03680 }
03681 return c;
03682 }
03683
03684
03685 int inflate_codes(inflate_blocks_statef *s, z_streamp z, int r)
03686 {
03687 uInt j;
03688 inflate_huft *t;
03689 uInt e;
03690 uLong b;
03691 uInt k;
03692 Byte *p;
03693 uInt n;
03694 Byte *q;
03695 uInt m;
03696 Byte *f;
03697 inflate_codes_statef *c = s->sub.decode.codes;
03698
03699
03700 LOAD
03701
03702
03703 while (1) switch (c->mode)
03704 {
03705 case START:
03706 #ifndef SLOW
03707 if (m >= 258 && n >= 10)
03708 {
03709 UPDATE
03710 r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
03711 LOAD
03712 if (r != Z_OK)
03713 {
03714 c->mode = r == Z_STREAM_END ? WASH : BADCODE;
03715 break;
03716 }
03717 }
03718 #endif
03719 c->sub.code.need = c->lbits;
03720 c->sub.code.tree = c->ltree;
03721 c->mode = LEN;
03722 case LEN:
03723 j = c->sub.code.need;
03724 NEEDBITS(j)
03725 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
03726 DUMPBITS(t->bits)
03727 e = (uInt)(t->exop);
03728 if (e == 0)
03729 {
03730 c->sub.lit = t->base;
03731 Tracevv((t->base >= 0x20 && t->base < 0x7f ?
03732 "inflate: literal '%c'\n" :
03733 "inflate: literal 0x%02x\n", t->base));
03734 c->mode = LIT;
03735 break;
03736 }
03737 if (e & 16)
03738 {
03739 c->sub.copy.get = e & 15;
03740 c->len = t->base;
03741 c->mode = LENEXT;
03742 break;
03743 }
03744 if ((e & 64) == 0)
03745 {
03746 c->sub.code.need = e;
03747 c->sub.code.tree = t + t->base;
03748 break;
03749 }
03750 if (e & 32)
03751 {
03752 Tracevv(("inflate: end of block\n"));
03753 c->mode = WASH;
03754 break;
03755 }
03756 c->mode = BADCODE;
03757 z->msg = (char*)"invalid literal/length code";
03758 r = Z_DATA_ERROR;
03759 LEAVE
03760 case LENEXT:
03761 j = c->sub.copy.get;
03762 NEEDBITS(j)
03763 c->len += (uInt)b & inflate_mask[j];
03764 DUMPBITS(j)
03765 c->sub.code.need = c->dbits;
03766 c->sub.code.tree = c->dtree;
03767 Tracevv(("inflate: length %u\n", c->len));
03768 c->mode = DIST;
03769 case DIST:
03770 j = c->sub.code.need;
03771 NEEDBITS(j)
03772 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
03773 DUMPBITS(t->bits)
03774 e = (uInt)(t->exop);
03775 if (e & 16)
03776 {
03777 c->sub.copy.get = e & 15;
03778 c->sub.copy.dist = t->base;
03779 c->mode = DISTEXT;
03780 break;
03781 }
03782 if ((e & 64) == 0)
03783 {
03784 c->sub.code.need = e;
03785 c->sub.code.tree = t + t->base;
03786 break;
03787 }
03788 c->mode = BADCODE;
03789 z->msg = (char*)"invalid distance code";
03790 r = Z_DATA_ERROR;
03791 LEAVE
03792 case DISTEXT:
03793 j = c->sub.copy.get;
03794 NEEDBITS(j)
03795 c->sub.copy.dist += (uInt)b & inflate_mask[j];
03796 DUMPBITS(j)
03797 Tracevv(("inflate: distance %u\n", c->sub.copy.dist));
03798 c->mode = COPY;
03799 case COPY:
03800 #ifndef __TURBOC__
03801 f = (uInt)(q - s->window) < c->sub.copy.dist ?
03802 s->end - (c->sub.copy.dist - (q - s->window)) :
03803 q - c->sub.copy.dist;
03804 #else
03805 f = q - c->sub.copy.dist;
03806 if ((uInt)(q - s->window) < c->sub.copy.dist)
03807 f = s->end - (c->sub.copy.dist - (uInt)(q - s->window));
03808 #endif
03809 while (c->len)
03810 {
03811 NEEDOUT
03812 OUTBYTE(*f++)
03813 if (f == s->end)
03814 f = s->window;
03815 c->len--;
03816 }
03817 c->mode = START;
03818 break;
03819 case LIT:
03820 NEEDOUT
03821 OUTBYTE(c->sub.lit)
03822 c->mode = START;
03823 break;
03824 case WASH:
03825 if (k > 7)
03826 {
03827 Assert(k < 16, "inflate_codes grabbed too many bytes")
03828 k -= 8;
03829 n++;
03830 p--;
03831 }
03832 FLUSH
03833 if (s->read != s->write)
03834 LEAVE
03835 c->mode = END;
03836 case END:
03837 r = Z_STREAM_END;
03838 LEAVE
03839 case BADCODE:
03840 r = Z_DATA_ERROR;
03841 LEAVE
03842 default:
03843 r = Z_STREAM_ERROR;
03844 LEAVE
03845 }
03846 #ifdef NEED_DUMMY_RETURN
03847 return Z_STREAM_ERROR;
03848 #endif
03849 }
03850
03851
03852 void inflate_codes_free(inflate_codes_statef *c, z_streamp z)
03853 {
03854 ZFREE(z, c);
03855 Tracev(("inflate: codes free\n"));
03856 }
03857
03858
03859
03860
03861
03862
03863 #define BASE 65521L
03864 #define NMAX 5552
03865
03866
03867 #undef DO1
03868 #undef DO2
03869 #undef DO4
03870 #undef DO8
03871
03872 #define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
03873 #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
03874 #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
03875 #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
03876 #define DO16(buf) DO8(buf,0); DO8(buf,8);
03877
03878
03879 static uLong adler32(uLong adler, const Byte *buf, uInt len)
03880 {
03881 unsigned long s1 = adler & 0xffff;
03882 unsigned long s2 = (adler >> 16) & 0xffff;
03883 int k;
03884
03885 if (buf == Z_NULL) return 1L;
03886
03887 while (len > 0) {
03888 k = len < NMAX ? len : NMAX;
03889 len -= k;
03890 while (k >= 16) {
03891 DO16(buf);
03892 buf += 16;
03893 k -= 16;
03894 }
03895 if (k != 0) do {
03896 s1 += *buf++;
03897 s2 += s1;
03898 } while (--k);
03899 s1 %= BASE;
03900 s2 %= BASE;
03901 }
03902 return (s2 << 16) | s1;
03903 }
03904
03905
03906
03907
03908
03909
03910
03911
03912
03913
03914
03915
03916 static inflate_blocks_statef * inflate_blocks_new OF((
03917 z_streamp z,
03918 check_func c,
03919 uInt w));
03920
03921 static int inflate_blocks OF((
03922 inflate_blocks_statef *,
03923 z_streamp ,
03924 int));
03925
03926 static void inflate_blocks_reset OF((
03927 inflate_blocks_statef *,
03928 z_streamp ,
03929 uLong *));
03930
03931 static int inflate_blocks_free OF((
03932 inflate_blocks_statef *,
03933 z_streamp));
03934
03935 #if 0
03936 static void inflate_set_dictionary OF((
03937 inflate_blocks_statef *s,
03938 const Byte *d,
03939 uInt n));
03940
03941 static int inflate_blocks_sync_point OF((
03942 inflate_blocks_statef *s));
03943 #endif
03944
03945 typedef enum {
03946 imMETHOD,
03947 imFLAG,
03948 imDICT4,
03949 imDICT3,
03950 imDICT2,
03951 imDICT1,
03952 imDICT0,
03953 imBLOCKS,
03954 imCHECK4,
03955 imCHECK3,
03956 imCHECK2,
03957 imCHECK1,
03958 imDONE,
03959 imBAD}
03960 inflate_mode;
03961
03962
03963 struct internal_state {
03964
03965
03966 inflate_mode mode;
03967
03968
03969 union {
03970 uInt method;
03971 struct {
03972 uLong was;
03973 uLong need;
03974 } check;
03975 uInt marker;
03976 } sub;
03977
03978
03979 int nowrap;
03980 uInt wbits;
03981 inflate_blocks_statef
03982 *blocks;
03983
03984 };
03985
03986
03987 int inflateReset(z_streamp z)
03988 {
03989 if (z == Z_NULL || z->state == Z_NULL)
03990 return Z_STREAM_ERROR;
03991 z->total_in = z->total_out = 0;
03992 z->msg = Z_NULL;
03993 z->state->mode = z->state->nowrap ? imBLOCKS : imMETHOD;
03994 inflate_blocks_reset(z->state->blocks, z, Z_NULL);
03995 Tracev(("inflate: reset\n"));
03996 return Z_OK;
03997 }
03998
03999
04000 int inflateEnd(z_streamp z)
04001 {
04002 if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
04003 return Z_STREAM_ERROR;
04004 if (z->state->blocks != Z_NULL)
04005 inflate_blocks_free(z->state->blocks, z);
04006 ZFREE(z, z->state);
04007 z->state = Z_NULL;
04008 Tracev(("inflate: end\n"));
04009 return Z_OK;
04010 }
04011
04012
04013
04014 int inflateInit2_(z_streamp z, int w, const char *version, int stream_size)
04015 {
04016 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
04017 stream_size != sizeof(z_stream))
04018 return Z_VERSION_ERROR;
04019
04020
04021 if (z == Z_NULL)
04022 return Z_STREAM_ERROR;
04023 z->msg = Z_NULL;
04024 if (z->zalloc == Z_NULL)
04025 {
04026 z->zalloc = (void *(*)(void *, unsigned, unsigned))zcalloc;
04027 z->opaque = (voidp)0;
04028 }
04029 if (z->zfree == Z_NULL) z->zfree = (void (*)(void *, void *))zcfree;
04030 if ((z->state = (struct internal_state *)
04031 ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
04032 return Z_MEM_ERROR;
04033 z->state->blocks = Z_NULL;
04034
04035
04036 z->state->nowrap = 0;
04037 if (w < 0)
04038 {
04039 w = - w;
04040 z->state->nowrap = 1;
04041 }
04042
04043
04044 if (w < 8 || w > 15)
04045 {
04046 inflateEnd(z);
04047 return Z_STREAM_ERROR;
04048 }
04049 z->state->wbits = (uInt)w;
04050
04051
04052 if ((z->state->blocks =
04053 inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
04054 == Z_NULL)
04055 {
04056 inflateEnd(z);
04057 return Z_MEM_ERROR;
04058 }
04059 Tracev(("inflate: allocated\n"));
04060
04061
04062 inflateReset(z);
04063 return Z_OK;
04064 }
04065
04066 #if 0
04067 int inflateInit_(z_streamp z, const char *version, int stream_size)
04068 {
04069 return inflateInit2_(z, DEF_WBITS, version, stream_size);
04070 }
04071 #endif
04072
04073 #define iNEEDBYTE {if(z->avail_in==0)return r;r=f;}
04074 #define iNEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
04075
04076 int inflate(z_streamp z, int f)
04077 {
04078 int r;
04079 uInt b;
04080
04081 if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
04082 return Z_STREAM_ERROR;
04083 f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
04084 r = Z_BUF_ERROR;
04085 while (1) switch (z->state->mode)
04086 {
04087 case imMETHOD:
04088 iNEEDBYTE
04089 if (((z->state->sub.method = iNEXTBYTE) & 0xf) != Z_DEFLATED)
04090 {
04091 z->state->mode = imBAD;
04092 z->msg = (char*)"unknown compression method";
04093 z->state->sub.marker = 5;
04094 break;
04095 }
04096 if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
04097 {
04098 z->state->mode = imBAD;
04099 z->msg = (char*)"invalid window size";
04100 z->state->sub.marker = 5;
04101 break;
04102 }
04103 z->state->mode = imFLAG;
04104 case imFLAG:
04105 iNEEDBYTE
04106 b = iNEXTBYTE;
04107 if (((z->state->sub.method << 8) + b) % 31)
04108 {
04109 z->state->mode = imBAD;
04110 z->msg = (char*)"incorrect header check";
04111 z->state->sub.marker = 5;
04112 break;
04113 }
04114 Tracev(("inflate: zlib header ok\n"));
04115 if (!(b & PRESET_DICT))
04116 {
04117 z->state->mode = imBLOCKS;
04118 break;
04119 }
04120 z->state->mode = imDICT4;
04121 case imDICT4:
04122 iNEEDBYTE
04123 z->state->sub.check.need = (uLong)iNEXTBYTE << 24;
04124 z->state->mode = imDICT3;
04125 case imDICT3:
04126 iNEEDBYTE
04127 z->state->sub.check.need += (uLong)iNEXTBYTE << 16;
04128 z->state->mode = imDICT2;
04129 case imDICT2:
04130 iNEEDBYTE
04131 z->state->sub.check.need += (uLong)iNEXTBYTE << 8;
04132 z->state->mode = imDICT1;
04133 case imDICT1:
04134 iNEEDBYTE
04135 z->state->sub.check.need += (uLong)iNEXTBYTE;
04136 z->adler = z->state->sub.check.need;
04137 z->state->mode = imDICT0;
04138 return Z_NEED_DICT;
04139 case imDICT0:
04140 z->state->mode = imBAD;
04141 z->msg = (char*)"need dictionary";
04142 z->state->sub.marker = 0;
04143 return Z_STREAM_ERROR;
04144 case imBLOCKS:
04145 r = inflate_blocks(z->state->blocks, z, r);
04146 if (r == Z_DATA_ERROR)
04147 {
04148 z->state->mode = imBAD;
04149 z->state->sub.marker = 0;
04150 break;
04151 }
04152 if (r == Z_OK)
04153 r = f;
04154 if (r != Z_STREAM_END)
04155 return r;
04156 r = f;
04157 inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
04158 if (z->state->nowrap)
04159 {
04160 z->state->mode = imDONE;
04161 break;
04162 }
04163 z->state->mode = imCHECK4;
04164 case imCHECK4:
04165 iNEEDBYTE
04166 z->state->sub.check.need = (uLong)iNEXTBYTE << 24;
04167 z->state->mode = imCHECK3;
04168 case imCHECK3:
04169 iNEEDBYTE
04170 z->state->sub.check.need += (uLong)iNEXTBYTE << 16;
04171 z->state->mode = imCHECK2;
04172 case imCHECK2:
04173 iNEEDBYTE
04174 z->state->sub.check.need += (uLong)iNEXTBYTE << 8;
04175 z->state->mode = imCHECK1;
04176 case imCHECK1:
04177 iNEEDBYTE
04178 z->state->sub.check.need += (uLong)iNEXTBYTE;
04179
04180 if (z->state->sub.check.was != z->state->sub.check.need)
04181 {
04182 z->state->mode = imBAD;
04183 z->msg = (char*)"incorrect data check";
04184 z->state->sub.marker = 5;
04185 break;
04186 }
04187 Tracev(("inflate: zlib check ok\n"));
04188 z->state->mode = imDONE;
04189 case imDONE:
04190 return Z_STREAM_END;
04191 case imBAD:
04192 return Z_DATA_ERROR;
04193 default:
04194 return Z_STREAM_ERROR;
04195 }
04196 #ifdef NEED_DUMMY_RETURN
04197 return Z_STREAM_ERROR;
04198 #endif
04199 }
04200
04201
04202 #if 0
04203 int inflateSetDictionary(z_streamp z, const Byte *dictionary, uInt dictLength)
04204 {
04205 uInt length = dictLength;
04206
04207 if (z == Z_NULL || z->state == Z_NULL || z->state->mode != imDICT0)
04208 return Z_STREAM_ERROR;
04209
04210 if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
04211 z->adler = 1L;
04212
04213 if (length >= ((uInt)1<<z->state->wbits))
04214 {
04215 length = (1<<z->state->wbits)-1;
04216 dictionary += dictLength - length;
04217 }
04218 inflate_set_dictionary(z->state->blocks, dictionary, length);
04219 z->state->mode = imBLOCKS;
04220 return Z_OK;
04221 }
04222
04223 int inflateSync(z_streamp z)
04224 {
04225 uInt n;
04226 Byte *p;
04227 uInt m;
04228 uLong r, w;
04229
04230
04231 if (z == Z_NULL || z->state == Z_NULL)
04232 return Z_STREAM_ERROR;
04233 if (z->state->mode != imBAD)
04234 {
04235 z->state->mode = imBAD;
04236 z->state->sub.marker = 0;
04237 }
04238 if ((n = z->avail_in) == 0)
04239 return Z_BUF_ERROR;
04240 p = z->next_in;
04241 m = z->state->sub.marker;
04242
04243
04244 while (n && m < 4)
04245 {
04246 static const Byte mark[4] = {0, 0, 0xff, 0xff};
04247 if (*p == mark[m])
04248 m++;
04249 else if (*p)
04250 m = 0;
04251 else
04252 m = 4 - m;
04253 p++, n--;
04254 }
04255
04256
04257 z->total_in += p - z->next_in;
04258 z->next_in = p;
04259 z->avail_in = n;
04260 z->state->sub.marker = m;
04261
04262
04263 if (m != 4)
04264 return Z_DATA_ERROR;
04265 r = z->total_in; w = z->total_out;
04266 inflateReset(z);
04267 z->total_in = r; z->total_out = w;
04268 z->state->mode = imBLOCKS;
04269 return Z_OK;
04270 }
04271
04272
04273
04274
04275
04276
04277
04278
04279 int inflateSyncPoint(z_streamp z)
04280 {
04281 if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL)
04282 return Z_STREAM_ERROR;
04283 return inflate_blocks_sync_point(z->state->blocks);
04284 }
04285 #endif
04286
04287 voidp zcalloc (voidp opaque, unsigned items, unsigned size)
04288 {
04289 if (opaque) items += size - size;
04290 return (voidp)Z_Malloc(items*size);
04291 }
04292
04293 void zcfree (voidp opaque, voidp ptr)
04294 {
04295 Z_Free(ptr);
04296 if (opaque) return;
04297 }
04298
04299