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