📄 compress.c
字号:
/*-------------------------------------------------------------*//*--- Compression machinery (not incl block sorting) ---*//*--- compress.c ---*//*-------------------------------------------------------------*//*-- This file is a part of bzip2 and/or libbzip2, a program and library for lossless, block-sorting data compression. Copyright (C) 1996-1998 Julian R Seward. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 3. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 4. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Julian Seward, Guildford, Surrey, UK. jseward@acm.org bzip2/libbzip2 version 0.9.0 of 28 June 1998 This program is based on (at least) the work of: Mike Burrows David Wheeler Peter Fenwick Alistair Moffat Radford Neal Ian H. Witten Robert Sedgewick Jon L. Bentley For more information on these sources, see the manual.--*//*-- CHANGES ~~~~~~~ 0.9.0 -- original version. 0.9.0a/b -- no changes in this file. 0.9.0c * changed setting of nGroups in sendMTFValues() so as to do a bit better on small files--*/#include "bzlib_private.h"/*---------------------------------------------------*//*--- Bit stream I/O ---*//*---------------------------------------------------*//*---------------------------------------------------*/void bsInitWrite ( EState* s ){ s->bsLive = 0; s->bsBuff = 0;}/*---------------------------------------------------*/staticvoid bsFinishWrite ( EState* s ){ while (s->bsLive > 0) { ((UChar*)(s->quadrant))[s->numZ] = (UChar)(s->bsBuff >> 24); s->numZ++; s->bsBuff <<= 8; s->bsLive -= 8; }}/*---------------------------------------------------*/#define bsNEEDW(nz) \{ \ while (s->bsLive >= 8) { \ ((UChar*)(s->quadrant))[s->numZ] \ = (UChar)(s->bsBuff >> 24); \ s->numZ++; \ s->bsBuff <<= 8; \ s->bsLive -= 8; \ } \}/*---------------------------------------------------*/staticvoid bsW ( EState* s, Int32 n, UInt32 v ){ bsNEEDW ( n ); s->bsBuff |= (v << (32 - s->bsLive - n)); s->bsLive += n;}/*---------------------------------------------------*/staticvoid bsPutUInt32 ( EState* s, UInt32 u ){ bsW ( s, 8, (u >> 24) & 0xffL ); bsW ( s, 8, (u >> 16) & 0xffL ); bsW ( s, 8, (u >> 8) & 0xffL ); bsW ( s, 8, u & 0xffL );}/*---------------------------------------------------*/staticvoid bsPutUChar ( EState* s, UChar c ){ bsW( s, 8, (UInt32)c );}/*---------------------------------------------------*//*--- The back end proper ---*//*---------------------------------------------------*//*---------------------------------------------------*/staticvoid makeMaps_e ( EState* s ){ Int32 i; s->nInUse = 0; for (i = 0; i < 256; i++) if (s->inUse[i]) { s->unseqToSeq[i] = s->nInUse; s->nInUse++; }}/*---------------------------------------------------*/staticvoid generateMTFValues ( EState* s ){ UChar yy[256]; Int32 i, j; UChar tmp; UChar tmp2; Int32 zPend; Int32 wr; Int32 EOB; makeMaps_e ( s ); EOB = s->nInUse+1; for (i = 0; i <= EOB; i++) s->mtfFreq[i] = 0; wr = 0; zPend = 0; for (i = 0; i < s->nInUse; i++) yy[i] = (UChar) i; for (i = 0; i < s->nblock; i++) { UChar ll_i; AssertD ( wr <= i, "generateMTFValues(1)" ); j = s->zptr[i]-1; if (j < 0) j += s->nblock; ll_i = s->unseqToSeq[s->block[j]]; AssertD ( ll_i < s->nInUse, "generateMTFValues(2a)" ); j = 0; tmp = yy[j]; while ( ll_i != tmp ) { j++; tmp2 = tmp; tmp = yy[j]; yy[j] = tmp2; }; yy[0] = tmp; if (j == 0) { zPend++; } else { if (zPend > 0) { zPend--; while (True) { switch (zPend % 2) { case 0: s->szptr[wr] = BZ_RUNA; wr++; s->mtfFreq[BZ_RUNA]++; break; case 1: s->szptr[wr] = BZ_RUNB; wr++; s->mtfFreq[BZ_RUNB]++; break; }; if (zPend < 2) break; zPend = (zPend - 2) / 2; }; zPend = 0; } s->szptr[wr] = j+1; wr++; s->mtfFreq[j+1]++; } } if (zPend > 0) { zPend--; while (True) { switch (zPend % 2) { case 0: s->szptr[wr] = BZ_RUNA; wr++; s->mtfFreq[BZ_RUNA]++; break; case 1: s->szptr[wr] = BZ_RUNB; wr++; s->mtfFreq[BZ_RUNB]++; break; }; if (zPend < 2) break; zPend = (zPend - 2) / 2; }; } s->szptr[wr] = EOB; wr++; s->mtfFreq[EOB]++; s->nMTF = wr;}/*---------------------------------------------------*/#define BZ_LESSER_ICOST 0#define BZ_GREATER_ICOST 15staticvoid sendMTFValues ( EState* s ){ Int32 v, t, i, j, gs, ge, totc, bt, bc, iter; Int32 nSelectors, alphaSize, minLen, maxLen, selCtr; Int32 nGroups, nBytes; /*-- UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; is a global since the decoder also needs it. Int32 code[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; Int32 rfreq[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; are also globals only used in this proc. Made global to keep stack frame size small. --*/ UInt16 cost[BZ_N_GROUPS]; Int32 fave[BZ_N_GROUPS]; if (s->verbosity >= 3) VPrintf3( " %d in block, %d after MTF & 1-2 coding, " "%d+2 syms in use\n", s->nblock, s->nMTF, s->nInUse ); alphaSize = s->nInUse+2; for (t = 0; t < BZ_N_GROUPS; t++) for (v = 0; v < alphaSize; v++) s->len[t][v] = BZ_GREATER_ICOST; /*--- Decide how many coding tables to use ---*/ AssertH ( s->nMTF > 0, 3001 ); if (s->nMTF < 200) nGroups = 2; else if (s->nMTF < 600) nGroups = 3; else if (s->nMTF < 1200) nGroups = 4; else if (s->nMTF < 2400) nGroups = 5; else nGroups = 6; /*--- Generate an initial set of coding tables ---*/ { Int32 nPart, remF, tFreq, aFreq; nPart = nGroups; remF = s->nMTF; gs = 0; while (nPart > 0) { tFreq = remF / nPart; ge = gs-1; aFreq = 0; while (aFreq < tFreq && ge < alphaSize-1) { ge++; aFreq += s->mtfFreq[ge]; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -