Changeset 13041 for OpenSceneGraph/trunk/src/osgPlugins/zip/unzip.cpp
- Timestamp:
- 03/21/12 18:36:20 (14 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/src/osgPlugins/zip/unzip.cpp
r12611 r13041 528 528 // static string (which must not be deallocated). 529 529 530 // Advanced functions 530 // Advanced functions 531 531 532 532 // The following functions are needed only in some special applications. … … 545 545 // can be determined from the Adler32 value returned by this call of 546 546 // inflate. The compressor and decompressor must use exactly the same 547 // dictionary. 547 // dictionary. 548 548 // 549 549 // inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a … … 556 556 557 557 int inflateSync (z_streamp strm); 558 // 558 // 559 559 // Skips invalid compressed data until a full flush point can be found, or until all 560 560 // available input is skipped. No output is provided. … … 647 647 #define ERR_RETURN(strm,err) \ 648 648 return (strm->msg = (char*)ERR_MSG(err), (err)) 649 // To be used only when the state is known to be valid 649 // To be used only when the state is known to be valid 650 650 651 651 // common constants … … 655 655 #define STATIC_TREES 1 656 656 #define DYN_TREES 2 657 // The three kinds of block type 657 // The three kinds of block type 658 658 659 659 #define MIN_MATCH 3 660 660 #define MAX_MATCH 258 661 // The minimum and maximum match lengths 662 663 #define PRESET_DICT 0x20 // preset dictionary flag in zlib header 664 665 // target dependencies 661 // The minimum and maximum match lengths 662 663 #define PRESET_DICT 0x20 // preset dictionary flag in zlib header 664 665 // target dependencies 666 666 667 667 #define OS_CODE 0x0b // Window 95 & Windows NT … … 669 669 670 670 671 // functions 671 // functions 672 672 673 673 #define zmemzero(dest, len) memset(dest, 0, len) … … 814 814 IBM_CODES, // processing fixed or dynamic block 815 815 IBM_DRY, // output remaining window bytes 816 IBM_DONE, // finished last block, done 817 IBM_BAD} // got a data error--stuck here 816 IBM_DONE, // finished last block, done 817 IBM_BAD} // got a data error--stuck here 818 818 inflate_block_mode; 819 819 820 // inflate blocks semi-private state 820 // inflate blocks semi-private state 821 821 struct inflate_blocks_state { 822 822 823 // mode 824 inflate_block_mode mode; // current inflate_block mode 825 826 // mode dependent information 823 // mode 824 inflate_block_mode mode; // current inflate_block mode 825 826 // mode dependent information 827 827 union { 828 uInt left; // if STORED, bytes left to copy 828 uInt left; // if STORED, bytes left to copy 829 829 struct { 830 uInt table; // table lengths (14 bits) 830 uInt table; // table lengths (14 bits) 831 831 uInt index; // index into blens (or border) 832 832 uInt *blens; // bit lengths of codes 833 uInt bb; // bit length tree depth 834 inflate_huft *tb; // bit length decoding tree 835 } trees; // if DTREE, decoding info for trees 833 uInt bb; // bit length tree depth 834 inflate_huft *tb; // bit length decoding tree 835 } trees; // if DTREE, decoding info for trees 836 836 struct { 837 inflate_codes_statef 837 inflate_codes_statef 838 838 *codes; 839 } decode; // if CODES, current state 839 } decode; // if CODES, current state 840 840 } sub; // submode 841 uInt last; // true if this block is the last block 842 843 // mode independent information 844 uInt bitk; // bits in bit buffer 845 uLong bitb; // bit buffer 846 inflate_huft *hufts; // single malloc for tree space 847 Byte *window; // sliding window 848 Byte *end; // one byte after sliding window 849 Byte *read; // window read pointer 850 Byte *write; // window write pointer 851 check_func checkfn; // check function 852 uLong check; // check on output 841 uInt last; // true if this block is the last block 842 843 // mode independent information 844 uInt bitk; // bits in bit buffer 845 uLong bitb; // bit buffer 846 inflate_huft *hufts; // single malloc for tree space 847 Byte *window; // sliding window 848 Byte *end; // one byte after sliding window 849 Byte *read; // window read pointer 850 Byte *write; // window write pointer 851 check_func checkfn; // check function 852 uLong check; // check on output 853 853 854 854 }; … … 856 856 857 857 // defines for inflate input/output 858 // update pointers and return 858 // update pointers and return 859 859 #define UPDBITS {s->bitb=b;s->bitk=k;} 860 860 #define UPDIN {z->avail_in=n;z->total_in+=(uLong)(p-z->next_in);z->next_in=p;} … … 862 862 #define UPDATE {UPDBITS UPDIN UPDOUT} 863 863 #define LEAVE {UPDATE return inflate_flush(s,z,r);} 864 // get bytes and bits 864 // get bytes and bits 865 865 #define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;} 866 866 #define NEEDBYTE {if(n)r=Z_OK;else LEAVE} … … 868 868 #define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}} 869 869 #define DUMPBITS(j) {b>>=(j);k-=(j);} 870 // output bytes 870 // output bytes 871 871 #define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q) 872 872 #define LOADOUT {q=s->write;m=(uInt)WAVAIL;} … … 875 875 #define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;} 876 876 #define OUTBYTE(a) {*q++=(Byte)(a);m--;} 877 // load local pointers 877 // load local pointers 878 878 #define LOAD {LOADIN LOADOUT} 879 879 880 // masks for lower bits (size given to avoid silly warnings with Visual C++) 880 // masks for lower bits (size given to avoid silly warnings with Visual C++) 881 881 // And'ing with mask[n] masks the lower n bits 882 882 const uInt inflate_mask[17] = { … … 1049 1049 Byte *q; 1050 1050 1051 // local copies of source and destination pointers 1051 // local copies of source and destination pointers 1052 1052 p = z->next_out; 1053 1053 q = s->read; 1054 1054 1055 // compute number of bytes to copy as far as end of window 1055 // compute number of bytes to copy as far as end of window 1056 1056 n = (uInt)((q <= s->write ? s->write : s->end) - q); 1057 1057 if (n > z->avail_out) n = z->avail_out; … … 1062 1062 z->total_out += n; 1063 1063 1064 // update check information 1064 // update check information 1065 1065 if (s->checkfn != Z_NULL) 1066 1066 z->adler = s->check = (*s->checkfn)(s->check, q, n); 1067 1067 1068 // copy as far as end of window 1068 // copy as far as end of window 1069 1069 if (n!=0) // check for n!=0 to avoid waking up CodeGuard 1070 1070 { memcpy(p, q, n); … … 1076 1076 if (q == s->end) 1077 1077 { 1078 // wrap pointers 1078 // wrap pointers 1079 1079 q = s->window; 1080 1080 if (s->write == s->end) 1081 1081 s->write = s->window; 1082 1082 1083 // compute bytes to copy 1083 // compute bytes to copy 1084 1084 n = (uInt)(s->write - q); 1085 1085 if (n > z->avail_out) n = z->avail_out; 1086 1086 if (n && r == Z_BUF_ERROR) r = Z_OK; 1087 1087 1088 // update counters 1088 // update counters 1089 1089 z->avail_out -= n; 1090 1090 z->total_out += n; 1091 1091 1092 // update check information 1092 // update check information 1093 1093 if (s->checkfn != Z_NULL) 1094 1094 z->adler = s->check = (*s->checkfn)(s->check, q, n); … … 1115 1115 #define bits word.what.Bits 1116 1116 1117 typedef enum { // waiting for "i:"=input, "o:"=output, "x:"=nothing 1118 START, // x: set up for LEN 1119 LEN, // i: get length/literal/eob next 1120 LENEXT, // i: getting length extra (have base) 1121 DIST, // i: get distance next 1122 DISTEXT, // i: getting distance extra 1117 typedef enum { // waiting for "i:"=input, "o:"=output, "x:"=nothing 1118 START, // x: set up for LEN 1119 LEN, // i: get length/literal/eob next 1120 LENEXT, // i: getting length extra (have base) 1121 DIST, // i: get distance next 1122 DISTEXT, // i: getting distance extra 1123 1123 COPY, // o: copying bytes in window, waiting for space 1124 LIT, // o: got literal, waiting for output space 1125 WASH, // o: got eob, possibly still output waiting 1126 END, // x: got eob and all data flushed 1127 BADCODE} // x: got error 1124 LIT, // o: got literal, waiting for output space 1125 WASH, // o: got eob, possibly still output waiting 1126 END, // x: got eob and all data flushed 1127 BADCODE} // x: got error 1128 1128 inflate_codes_mode; 1129 1129 … … 1131 1131 struct inflate_codes_state { 1132 1132 1133 // mode 1134 inflate_codes_mode mode; // current inflate_codes mode 1135 1136 // mode dependent information 1133 // mode 1134 inflate_codes_mode mode; // current inflate_codes mode 1135 1136 // mode dependent information 1137 1137 uInt len; 1138 1138 union { 1139 1139 struct { 1140 const inflate_huft *tree; // pointer into tree 1141 uInt need; // bits needed 1142 } code; // if LEN or DIST, where in tree 1143 uInt lit; // if LIT, literal 1140 const inflate_huft *tree; // pointer into tree 1141 uInt need; // bits needed 1142 } code; // if LEN or DIST, where in tree 1143 uInt lit; // if LIT, literal 1144 1144 struct { 1145 uInt get; // bits to get for extra 1146 uInt dist; // distance back to copy from 1147 } copy; // if EXT or COPY, where and how much 1145 uInt get; // bits to get for extra 1146 uInt dist; // distance back to copy from 1147 } copy; // if EXT or COPY, where and how much 1148 1148 } sub; // submode 1149 1149 1150 // mode independent information 1151 Byte lbits; // ltree bits decoded per branch 1152 Byte dbits; // dtree bits decoder per branch 1150 // mode independent information 1151 Byte lbits; // ltree bits decoded per branch 1152 Byte dbits; // dtree bits decoder per branch 1153 1153 const inflate_huft *ltree; // literal/length/eob tree 1154 1154 const inflate_huft *dtree; // distance tree … … 1222 1222 DUMPBITS(t->bits) 1223 1223 e = (uInt)(t->exop); 1224 if (e == 0) // literal 1224 if (e == 0) // literal 1225 1225 { 1226 1226 c->sub.lit = t->base; … … 1231 1231 break; 1232 1232 } 1233 if (e & 16) // length 1233 if (e & 16) // length 1234 1234 { 1235 1235 c->sub.copy.get = e & 15; … … 1238 1238 break; 1239 1239 } 1240 if ((e & 64) == 0) // next table 1240 if ((e & 64) == 0) // next table 1241 1241 { 1242 1242 c->sub.code.need = e; … … 1244 1244 break; 1245 1245 } 1246 if (e & 32) // end of block 1246 if (e & 32) // end of block 1247 1247 { 1248 1248 LuTracevv((stderr, "inflate: end of block\n")); … … 1250 1250 break; 1251 1251 } 1252 c->mode = BADCODE; // invalid code 1252 c->mode = BADCODE; // invalid code 1253 1253 z->msg = (char*)"invalid literal/length code"; 1254 1254 r = Z_DATA_ERROR; 1255 1255 LEAVE 1256 case LENEXT: // i: getting length extra (have base) 1256 case LENEXT: // i: getting length extra (have base) 1257 1257 j = c->sub.copy.get; 1258 1258 NEEDBITS(j) … … 1263 1263 LuTracevv((stderr, "inflate: length %u\n", c->len)); 1264 1264 c->mode = DIST; 1265 case DIST: // i: get distance next 1265 case DIST: // i: get distance next 1266 1266 j = c->sub.code.need; 1267 1267 NEEDBITS(j) … … 1269 1269 DUMPBITS(t->bits) 1270 1270 e = (uInt)(t->exop); 1271 if (e & 16) // distance 1271 if (e & 16) // distance 1272 1272 { 1273 1273 c->sub.copy.get = e & 15; … … 1276 1276 break; 1277 1277 } 1278 if ((e & 64) == 0) // next table 1278 if ((e & 64) == 0) // next table 1279 1279 { 1280 1280 c->sub.code.need = e; … … 1282 1282 break; 1283 1283 } 1284 c->mode = BADCODE; // invalid code 1284 c->mode = BADCODE; // invalid code 1285 1285 z->msg = (char*)"invalid distance code"; 1286 1286 r = Z_DATA_ERROR; 1287 1287 LEAVE 1288 case DISTEXT: // i: getting distance extra 1288 case DISTEXT: // i: getting distance extra 1289 1289 j = c->sub.copy.get; 1290 1290 NEEDBITS(j) … … 1293 1293 LuTracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist)); 1294 1294 c->mode = COPY; 1295 case COPY: // o: copying bytes in window, waiting for space 1295 case COPY: // o: copying bytes in window, waiting for space 1296 1296 f = q - c->sub.copy.dist; 1297 1297 while (f < s->window) // modulo window size-"while" instead 1298 f += s->end - s->window; // of "if" handles invalid distances 1298 f += s->end - s->window; // of "if" handles invalid distances 1299 1299 while (c->len) 1300 1300 { … … 1307 1307 c->mode = START; 1308 1308 break; 1309 case LIT: // o: got literal, waiting for output space 1309 case LIT: // o: got literal, waiting for output space 1310 1310 NEEDOUT 1311 1311 OUTBYTE(c->sub.lit) 1312 1312 c->mode = START; 1313 1313 break; 1314 case WASH: // o: got eob, possibly more output 1315 if (k > 7) // return unused byte, if any 1314 case WASH: // o: got eob, possibly more output 1315 if (k > 7) // return unused byte, if any 1316 1316 { 1317 1317 //Assert(k < 16, "inflate_codes grabbed too many bytes") 1318 1318 k -= 8; 1319 1319 n++; 1320 p--; // can always return one 1320 p--; // can always return one 1321 1321 } 1322 1322 FLUSH … … 1348 1348 // For conditions of distribution and use, see copyright notice in zlib.h 1349 1349 1350 //struct inflate_codes_state {int dummy;}; // for buggy compilers 1350 //struct inflate_codes_state {int dummy;}; // for buggy compilers 1351 1351 1352 1352 … … 1454 1454 uInt n; // bytes available there 1455 1455 Byte *q; // output window write pointer 1456 uInt m; // bytes to end of window or read pointer 1457 1458 // copy input/output information to locals (UPDATE macro restores) 1456 uInt m; // bytes to end of window or read pointer 1457 1458 // copy input/output information to locals (UPDATE macro restores) 1459 1459 LOAD 1460 1460 1461 // process input based on current state 1461 // process input based on current state 1462 1462 for(;;) switch (s->mode) 1463 1463 { … … 1468 1468 switch (t >> 1) 1469 1469 { 1470 case 0: // stored 1470 case 0: // stored 1471 1471 LuTracev((stderr, "inflate: stored block%s\n", 1472 1472 s->last ? " (last)" : "")); 1473 1473 DUMPBITS(3) 1474 t = k & 7; // go to byte boundary 1474 t = k & 7; // go to byte boundary 1475 1475 DUMPBITS(t) 1476 1476 s->mode = IBM_LENS; // get length of stored block 1477 1477 break; 1478 case 1: // fixed 1478 case 1: // fixed 1479 1479 LuTracev((stderr, "inflate: fixed codes block%s\n", 1480 1480 s->last ? " (last)" : "")); … … 1494 1494 s->mode = IBM_CODES; 1495 1495 break; 1496 case 2: // dynamic 1496 case 2: // dynamic 1497 1497 LuTracev((stderr, "inflate: dynamic codes block%s\n", 1498 1498 s->last ? " (last)" : "")); … … 1518 1518 } 1519 1519 s->sub.left = (uInt)b & 0xffff; 1520 b = k = 0; // dump bits 1520 b = k = 0; // dump bits 1521 1521 LuTracev((stderr, "inflate: stored length %u\n", s->sub.left)); 1522 1522 s->mode = s->sub.left ? IBM_STORED : (s->last ? IBM_DRY : IBM_TYPE); … … 1603 1603 s->sub.trees.blens[s->sub.trees.index++] = c; 1604 1604 } 1605 else // c == 16..18 1605 else // c == 16..18 1606 1606 { 1607 1607 i = c == 18 ? 7 : c - 14; … … 1635 1635 inflate_codes_statef *c; 1636 1636 1637 bl = 9; // must be <= 9 for lookahead assumptions 1637 bl = 9; // must be <= 9 for lookahead assumptions 1638 1638 bd = 6; // must be <= 9 for lookahead assumptions 1639 1639 t = s->sub.trees.table; … … 1730 1730 const uInt *, // list of extra bits for non-simple codes 1731 1731 inflate_huft **,// result: starting table 1732 uInt *, // maximum lookup bits (returns actual) 1733 inflate_huft *, // space for trees 1734 uInt *, // hufts used in space 1735 uInt * ); // space for values 1736 1737 // Tables for deflate from PKZIP's appnote.txt. 1732 uInt *, // maximum lookup bits (returns actual) 1733 inflate_huft *, // space for trees 1734 uInt *, // hufts used in space 1735 uInt * ); // space for values 1736 1737 // Tables for deflate from PKZIP's appnote.txt. 1738 1738 const uInt cplens[31] = { // Copy lengths for literal codes 257..285 1739 1739 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, … … 1747 1747 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 1748 1748 8193, 12289, 16385, 24577}; 1749 const uInt cpdext[30] = { // Extra bits for distance codes 1749 const uInt cpdext[30] = { // Extra bits for distance codes 1750 1750 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 1751 1751 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, … … 1785 1785 1786 1786 1787 // If BMAX needs to be larger than 16, then h and x[] should be uLong. 1787 // If BMAX needs to be larger than 16, then h and x[] should be uLong. 1788 1788 #define BMAX 15 // maximum bit length of any code 1789 1789 … … 1807 1807 uInt a; // counter for codes of length k 1808 1808 uInt c[BMAX+1]; // bit length count table 1809 uInt f; // i repeats in table every f entries 1810 int g; // maximum code length 1811 int h; // table level 1812 register uInt i; // counter, current code 1809 uInt f; // i repeats in table every f entries 1810 int g; // maximum code length 1811 int h; // table level 1812 register uInt i; // counter, current code 1813 1813 register uInt j; // counter 1814 register int k; // number of bits in current code 1815 int l; // bits per table (returned in m) 1816 uInt mask; // (1 << w) - 1, to avoid cc -O bug on HP 1814 register int k; // number of bits in current code 1815 int l; // bits per table (returned in m) 1816 uInt mask; // (1 << w) - 1, to avoid cc -O bug on HP 1817 1817 register uInt *p; // pointer into c[], b[], or v[] 1818 inflate_huft *q; // points to current table 1819 struct inflate_huft_s r; // table entry for structure assignment 1820 inflate_huft *u[BMAX]; // table stack 1821 register int w; // bits before this table == (l * h) 1822 uInt x[BMAX+1]; // bit offsets, then code stack 1823 uInt *xp; // pointer into x 1824 int y; // number of dummy codes added 1825 uInt z; // number of entries in current table 1818 inflate_huft *q; // points to current table 1819 struct inflate_huft_s r; // table entry for structure assignment 1820 inflate_huft *u[BMAX]; // table stack 1821 register int w; // bits before this table == (l * h) 1822 uInt x[BMAX+1]; // bit offsets, then code stack 1823 uInt *xp; // pointer into x 1824 int y; // number of dummy codes added 1825 uInt z; // number of entries in current table 1826 1826 1827 1827 // provide a default value 1828 1828 r.base = 0; 1829 1829 1830 // Generate counts for each bit length 1830 // Generate counts for each bit length 1831 1831 p = c; 1832 1832 #define C0 *p++ = 0; … … 1836 1836 p = b; i = n; 1837 1837 do { 1838 c[*p++]++; // assume all entries <= BMAX 1838 c[*p++]++; // assume all entries <= BMAX 1839 1839 } while (--i); 1840 if (c[0] == n) // null input--all zero length codes 1840 if (c[0] == n) // null input--all zero length codes 1841 1841 { 1842 1842 *t = (inflate_huft *)Z_NULL; … … 1846 1846 1847 1847 1848 // Find minimum and maximum length, bound *m by those 1848 // Find minimum and maximum length, bound *m by those 1849 1849 l = *m; 1850 1850 for (j = 1; j <= BMAX; j++) 1851 1851 if (c[j]) 1852 1852 break; 1853 k = j; // minimum code length 1853 k = j; // minimum code length 1854 1854 if ((uInt)l < j) 1855 1855 l = j; … … 1857 1857 if (c[i]) 1858 1858 break; 1859 g = i; // maximum code length 1859 g = i; // maximum code length 1860 1860 if ((uInt)l > i) 1861 1861 l = i; … … 1863 1863 1864 1864 1865 // Adjust last length count to fill out codes, if needed 1865 // Adjust last length count to fill out codes, if needed 1866 1866 for (y = 1 << j; j < i; j++, y <<= 1) 1867 1867 if ((y -= c[j]) < 0) … … 1872 1872 1873 1873 1874 // Generate starting offsets into the value table for each length 1874 // Generate starting offsets into the value table for each length 1875 1875 x[1] = j = 0; 1876 1876 p = c + 1; xp = x + 2; 1877 while (--i) { // note that i == g from above 1877 while (--i) { // note that i == g from above 1878 1878 *xp++ = (j += *p++); 1879 1879 } 1880 1880 1881 1881 1882 // Make a table of values in order of bit lengths 1882 // Make a table of values in order of bit lengths 1883 1883 p = b; i = 0; 1884 1884 do { … … 1886 1886 v[x[j]++] = i; 1887 1887 } while (++i < n); 1888 n = x[g]; // set n to length of v 1889 1890 1891 // Generate the Huffman codes and for each, make the table entries 1892 x[0] = i = 0; // first Huffman code is zero 1893 p = v; // grab values in bit order 1894 h = -1; // no tables yet--level -1 1895 w = -l; // bits decoded == (l * h) 1896 u[0] = (inflate_huft *)Z_NULL; // just to keep compilers happy 1897 q = (inflate_huft *)Z_NULL; // ditto 1898 z = 0; // ditto 1899 1900 // go through the bit lengths (k already is bits in shortest code) 1888 n = x[g]; // set n to length of v 1889 1890 1891 // Generate the Huffman codes and for each, make the table entries 1892 x[0] = i = 0; // first Huffman code is zero 1893 p = v; // grab values in bit order 1894 h = -1; // no tables yet--level -1 1895 w = -l; // bits decoded == (l * h) 1896 u[0] = (inflate_huft *)Z_NULL; // just to keep compilers happy 1897 q = (inflate_huft *)Z_NULL; // ditto 1898 z = 0; // ditto 1899 1900 // go through the bit lengths (k already is bits in shortest code) 1901 1901 for (; k <= g; k++) 1902 1902 { … … 1904 1904 while (a--) 1905 1905 { 1906 // here i is the Huffman code of length k bits for value *p 1907 // make tables up to required level 1906 // here i is the Huffman code of length k bits for value *p 1907 // make tables up to required level 1908 1908 while (k > w + l) 1909 1909 { 1910 1910 h++; 1911 w += l; // previous table always l bits 1911 w += l; // previous table always l bits 1912 1912 1913 1913 // compute minimum size table less than or equal to l bits 1914 1914 z = g - w; 1915 z = z > (uInt)l ? l : z; // table size upper limit 1916 if ((f = 1 << (j = k - w)) > a + 1) // try a k-w bit table 1917 { // too few codes for k-w bit table 1918 f -= a + 1; // deduct codes from patterns left 1915 z = z > (uInt)l ? l : z; // table size upper limit 1916 if ((f = 1 << (j = k - w)) > a + 1) // try a k-w bit table 1917 { // too few codes for k-w bit table 1918 f -= a + 1; // deduct codes from patterns left 1919 1919 xp = c + k; 1920 1920 if (j < z) 1921 while (++j < z) // try smaller tables up to z bits 1921 while (++j < z) // try smaller tables up to z bits 1922 1922 { 1923 1923 if ((f <<= 1) <= *++xp) 1924 break; // enough codes to use up j bits 1924 break; // enough codes to use up j bits 1925 1925 f -= *xp; // else deduct codes from patterns 1926 1926 } 1927 1927 } 1928 z = 1 << j; // table entries for j-bit table 1929 1930 // allocate new table 1931 if (*hn + z > MANY) // (note: doesn't matter for fixed) 1932 return Z_DATA_ERROR; // overflow of MANY 1928 z = 1 << j; // table entries for j-bit table 1929 1930 // allocate new table 1931 if (*hn + z > MANY) // (note: doesn't matter for fixed) 1932 return Z_DATA_ERROR; // overflow of MANY 1933 1933 u[h] = q = hp + *hn; 1934 1934 *hn += z; 1935 1935 1936 // connect to last table, if there is one 1936 // connect to last table, if there is one 1937 1937 if (h) 1938 1938 { 1939 1939 x[h] = i; // save pattern for backing up 1940 r.bits = (Byte)l; // bits to dump before this table 1941 r.exop = (Byte)j; // bits in this table 1940 r.bits = (Byte)l; // bits to dump before this table 1941 r.exop = (Byte)j; // bits in this table 1942 1942 j = i >> (w - l); 1943 r.base = (uInt)(q - u[h-1] - j); // offset to this table 1944 u[h-1][j] = r; // connect to last table 1943 r.base = (uInt)(q - u[h-1] - j); // offset to this table 1944 u[h-1][j] = r; // connect to last table 1945 1945 } 1946 1946 else 1947 *t = q; // first table is returned result 1947 *t = q; // first table is returned result 1948 1948 } 1949 1949 1950 // set up table entry in r 1950 // set up table entry in r 1951 1951 r.bits = (Byte)(k - w); 1952 1952 if (p >= v + n) 1953 r.exop = 128 + 64; // out of values--invalid code 1953 r.exop = 128 + 64; // out of values--invalid code 1954 1954 else if (*p < s) 1955 1955 { 1956 r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); // 256 is end-of-block 1957 r.base = *p++; // simple code is just the value 1956 r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); // 256 is end-of-block 1957 r.base = *p++; // simple code is just the value 1958 1958 } 1959 1959 else 1960 1960 { 1961 r.exop = (Byte)(e[*p - s] + 16 + 64);// non-simple--look up in lists 1961 r.exop = (Byte)(e[*p - s] + 16 + 64);// non-simple--look up in lists 1962 1962 r.base = d[*p++ - s]; 1963 1963 } … … 1968 1968 q[j] = r; 1969 1969 1970 // backwards increment the k-bit code i 1970 // backwards increment the k-bit code i 1971 1971 for (j = 1 << (k - 1); i & j; j >>= 1) 1972 1972 i ^= j; 1973 1973 i ^= j; 1974 1974 1975 // backup over finished tables 1976 mask = (1 << w) - 1; // needed on HP, cc -O bug 1975 // backup over finished tables 1976 mask = (1 << w) - 1; // needed on HP, cc -O bug 1977 1977 while ((i & mask) != x[h]) 1978 1978 { … … 1985 1985 1986 1986 1987 // Return Z_BUF_ERROR if we were given an incomplete table 1987 // Return Z_BUF_ERROR if we were given an incomplete table 1988 1988 return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK; 1989 1989 } … … 1998 1998 { 1999 1999 int r; 2000 uInt hn = 0; // hufts used in space 2001 uInt *v; // work area for huft_build 2000 uInt hn = 0; // hufts used in space 2001 uInt *v; // work area for huft_build 2002 2002 2003 2003 if ((v = (uInt*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL) … … 2029 2029 { 2030 2030 int r; 2031 uInt hn = 0; // hufts used in space 2032 uInt *v; // work area for huft_build 2033 2034 // allocate work area 2031 uInt hn = 0; // hufts used in space 2032 uInt *v; // work area for huft_build 2033 2034 // allocate work area 2035 2035 if ((v = (uInt*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL) 2036 2036 return Z_MEM_ERROR; 2037 2037 2038 // build literal/length tree 2038 // build literal/length tree 2039 2039 r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v); 2040 2040 if (r != Z_OK || *bl == 0) … … 2051 2051 } 2052 2052 2053 // build distance tree 2053 // build distance tree 2054 2054 r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v); 2055 2055 if (r != Z_OK || (*bd == 0 && nl > 257)) … … 2070 2070 } 2071 2071 2072 // done 2072 // done 2073 2073 ZFREE(z, v); 2074 2074 return Z_OK; … … 2100 2100 2101 2101 2102 //struct inflate_codes_state {int dummy;}; // for buggy compilers 2103 2104 2105 // macros for bit input with no checking and for returning unused bytes 2102 //struct inflate_codes_state {int dummy;}; // for buggy compilers 2103 2104 2105 // macros for bit input with no checking and for returning unused bytes 2106 2106 #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}} 2107 2107 #define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;} … … 2110 2110 // (the maximum string length) and number of input bytes available 2111 2111 // at least ten. The ten bytes are six bytes for the longest length/ 2112 // distance pair plus four bytes for overloading the bit buffer. 2112 // distance pair plus four bytes for overloading the bit buffer. 2113 2113 2114 2114 int inflate_fast( … … 2119 2119 z_streamp z) 2120 2120 { 2121 const inflate_huft *t; // temporary pointer 2122 uInt e; // extra bits or operation 2123 uLong b; // bit buffer 2124 uInt k; // bits in bit buffer 2125 Byte *p; // input data pointer 2126 uInt n; // bytes available there 2127 Byte *q; // output window write pointer 2128 uInt m; // bytes to end of window or read pointer 2121 const inflate_huft *t; // temporary pointer 2122 uInt e; // extra bits or operation 2123 uLong b; // bit buffer 2124 uInt k; // bits in bit buffer 2125 Byte *p; // input data pointer 2126 uInt n; // bytes available there 2127 Byte *q; // output window write pointer 2128 uInt m; // bytes to end of window or read pointer 2129 2129 uInt ml; // mask for literal/length tree 2130 uInt md; // mask for distance tree 2131 uInt c; // bytes to copy 2132 uInt d; // distance back to copy from 2133 Byte *r; // copy source pointer 2134 2135 // load input, output, bit values 2130 uInt md; // mask for distance tree 2131 uInt c; // bytes to copy 2132 uInt d; // distance back to copy from 2133 Byte *r; // copy source pointer 2134 2135 // load input, output, bit values 2136 2136 LOAD 2137 2137 2138 // initialize masks 2138 // initialize masks 2139 2139 ml = inflate_mask[bl]; 2140 2140 md = inflate_mask[bd]; 2141 2141 2142 // do until not enough input or output space for fast loop 2143 do { // assume called with m >= 258 && n >= 10 2144 // get literal/length code 2145 GRABBITS(20) // max bits for literal/length code 2142 // do until not enough input or output space for fast loop 2143 do { // assume called with m >= 258 && n >= 10 2144 // get literal/length code 2145 GRABBITS(20) // max bits for literal/length code 2146 2146 if ((e = (t = tl + ((uInt)b & ml))->exop) == 0) 2147 2147 { … … 2158 2158 if (e & 16) 2159 2159 { 2160 // get extra bits for length 2160 // get extra bits for length 2161 2161 e &= 15; 2162 2162 c = t->base + ((uInt)b & inflate_mask[e]); … … 2164 2164 LuTracevv((stderr, "inflate: * length %u\n", c)); 2165 2165 2166 // decode distance base of block to copy 2167 GRABBITS(15); // max bits for distance code 2166 // decode distance base of block to copy 2167 GRABBITS(15); // max bits for distance code 2168 2168 e = (t = td + ((uInt)b & md))->exop; 2169 2169 for (;;) { … … 2171 2171 if (e & 16) 2172 2172 { 2173 // get extra bits to add to distance base 2173 // get extra bits to add to distance base 2174 2174 e &= 15; 2175 GRABBITS(e) // get extra bits (up to 13) 2175 GRABBITS(e) // get extra bits (up to 13) 2176 2176 d = t->base + ((uInt)b & inflate_mask[e]); 2177 2177 DUMPBITS(e) … … 2595 2595 LuTracev((stderr, "inflate: allocated\n")); 2596 2596 2597 // reset state 2597 // reset state 2598 2598 inflateReset(z); 2599 2599 return Z_OK; … … 2640 2640 z->state->mode = IM_BAD; 2641 2641 z->msg = (char*)"incorrect header check"; 2642 z->state->sub.marker = 5; // can't try inflateSync 2642 z->state->sub.marker = 5; // can't try inflateSync 2643 2643 break; 2644 2644 } … … 2671 2671 z->state->mode = IM_BAD; 2672 2672 z->msg = (char*)"need dictionary"; 2673 z->state->sub.marker = 0; // can try inflateSync 2673 z->state->sub.marker = 0; // can try inflateSync 2674 2674 return Z_STREAM_ERROR; 2675 2675 case IM_BLOCKS: … … 2678 2678 { 2679 2679 z->state->mode = IM_BAD; 2680 z->state->sub.marker = 0; // can try inflateSync 2680 z->state->sub.marker = 0; // can try inflateSync 2681 2681 break; 2682 2682 } … … 2713 2713 z->state->mode = IM_BAD; 2714 2714 z->msg = (char*)"incorrect data check"; 2715 z->state->sub.marker = 5; // can't try inflateSync 2715 z->state->sub.marker = 5; // can't try inflateSync 2716 2716 break; 2717 2717 } … … 2753 2753 unz_file_info_internal_s(): 2754 2754 offset_curfile(0) {} 2755 2755 2756 2756 uLong offset_curfile;// relative offset of local header 4 bytes 2757 2757 } unz_file_info_internal; … … 2840 2840 int lufseek(LUFILE *stream, long offset, int whence) 2841 2841 { if (stream->is_handle && stream->canseek) 2842 { 2842 { 2843 2843 #ifdef ZIP_STD 2844 2844 return fseek(stream->h,stream->initial_offset+offset,whence); … … 2864 2864 { unsigned int toread = (unsigned int)(size*n); 2865 2865 if (stream->is_handle) 2866 { 2866 { 2867 2867 #ifdef ZIP_STD 2868 2868 return fread(ptr,size,n,stream->h); … … 3015 3015 err = unzlocal_getByte(fin,&i); 3016 3016 x = (uLong)i; 3017 3017 3018 3018 if (err==UNZ_OK) 3019 3019 err = unzlocal_getByte(fin,&i); … … 3027 3027 err = unzlocal_getByte(fin,&i); 3028 3028 x += ((uLong)i)<<24; 3029 3029 3030 3030 if (err==UNZ_OK) 3031 3031 *pX = x; … … 3036 3036 3037 3037 3038 // My own strcmpi / strcasecmp 3038 // My own strcmpi / strcasecmp 3039 3039 int strcmpcasenosensitive_internal (const char* fileName1,const char *fileName2) 3040 3040 { … … 3069 3069 { if (iCaseSensitivity==1) return strcmp(fileName1,fileName2); 3070 3070 else return strcmpcasenosensitive_internal(fileName1,fileName2); 3071 } 3071 } 3072 3072 3073 3073 #define BUFREADCOMMENT (0x400) … … 3169 3169 int unzClose (unzFile file) 3170 3170 { 3171 3171 3172 3172 if (file==NULL) 3173 3173 return UNZ_PARAMERROR; … … 3186 3186 // Write info about the ZipFile in the *pglobal_info structure. 3187 3187 // No preparation of the structure is needed 3188 // return UNZ_OK if there is no problem. 3188 // return UNZ_OK if there is no problem. 3189 3189 int unzGetGlobalInfo (unzFile file,unz_global_info *pglobal_info) 3190 3190 { … … 4179 4179 // make sure there rd is always null terminated 4180 4180 rd[MAX_PATH] = 0; 4181 4181 4182 4182 size_t len=_tcslen(rd); 4183 4183 if (len>0 && (rd[len-1]=='/' || rd[len-1]=='\\')) rd[len-1]=0; … … 4198 4198 name++; 4199 4199 } 4200 TCHAR cd[MAX_PATH]; *cd=0; 4201 if (rootdir!=0) 4200 TCHAR cd[MAX_PATH]; *cd=0; 4201 if (rootdir!=0) 4202 4202 #ifdef ZIP_STD 4203 4203 strncpy(cd,rootdir,MAX_PATH); 4204 4204 #else 4205 4205 _tcsncpy_s(cd,MAX_PATH,rootdir,MAX_PATH); 4206 #endif 4207 4206 #endif 4207 4208 4208 cd[MAX_PATH-1]=0; 4209 4209 size_t len=_tcslen(cd); 4210 4210 4211 4211 #ifdef ZIP_STD 4212 4212 strncpy(cd+len,dir,MAX_PATH-len); 4213 4213 #else 4214 4214 _tcsncpy_s(cd+len,MAX_PATH-len,dir,MAX_PATH-len); 4215 #endif 4216 4217 4215 #endif 4216 4217 4218 4218 cd[MAX_PATH-1]=0; 4219 4219 #ifdef ZIP_STD … … 4240 4240 bool reached_eof; 4241 4241 int res = unzReadCurrentFile(uf,dst,len,&reached_eof); 4242 if (res<=0) 4242 if (res<=0) 4243 4243 { 4244 4244 unzCloseCurrentFile(uf); currentfile=-1; … … 4293 4293 // is how the user retrieve's the file's name within the zip) never returns absolute paths. 4294 4294 const TCHAR *name=ufn; const TCHAR *c=name; while (*c!=0) {if (*c=='/' || *c=='\\') name=c+1; c++;} 4295 TCHAR dir[MAX_PATH]; 4295 TCHAR dir[MAX_PATH]; 4296 4296 4297 4297 #ifdef ZIP_STD … … 4299 4299 #else 4300 4300 _tcsncpy_s(dir,MAX_PATH,ufn,MAX_PATH); 4301 #endif 4302 4301 #endif 4302 4303 4303 4304 4304 if (name==ufn) *dir=0; else dir[name-ufn]=0; 4305 4305 bool isabsolute = (dir[0]=='/' || dir[0]=='\\' || (dir[0]!=0 && dir[1]==':')); 4306 4306 4307 if (isabsolute) 4307 if (isabsolute) 4308 4308 { 4309 4309 4310 4310 #ifdef ZIP_STD 4311 4311 size_t dirlen=_tcslen(dir); … … 4314 4314 #else 4315 4315 _tsprintf(fn,MAX_PATH,_T("%s%s"),dir,name); 4316 #endif 4316 #endif 4317 4317 EnsureDirectory(0,dir); 4318 4318 4319 4319 } 4320 else 4320 else 4321 4321 { 4322 4322 #ifdef ZIP_STD … … 4341 4341 unzOpenCurrentFile(uf,password); 4342 4342 if (unzbuf==0) unzbuf=new char[16384]; DWORD haderr=0; 4343 // 4343 // 4344 4344 4345 4345 for (; haderr==0;) … … 4415 4415 #else 4416 4416 _tcsncpy_s(buf,_tcslen(buf),msg,n); 4417 #endif 4418 4419 4420 4421 4417 #endif 4418 4419 4420 4421 4422 4422 buf[n]=0; 4423 4423 return mlen;
