/*-------------------------------------------------------------*/
/*--- Compression machinery (not incl block sorting) ---*/
/*--- compress.c ---*/
/*-------------------------------------------------------------*/
/* ------------------------------------------------------------------
lossless, block-sorting data compression.
Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
Please read the WARNING, DISCLAIMER and PATENTS sections in the
README file.
This program is released under the terms of the license contained
in the file LICENSE.
------------------------------------------------------------------ */
/* 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 <sys/ccompile.h>
#include "bzlib_private.h"
/*---------------------------------------------------*/
/*--- Bit stream I/O ---*/
/*---------------------------------------------------*/
/*---------------------------------------------------*/
{
s->bsLive = 0;
s->bsBuff = 0;
}
/*---------------------------------------------------*/
static
{
while (s->bsLive > 0) {
s->numZ++;
s->bsBuff <<= 8;
s->bsLive -= 8;
}
}
/*---------------------------------------------------*/
{ \
while (s->bsLive >= 8) { \
s->numZ++; \
s->bsBuff <<= 8; \
s->bsLive -= 8; \
} \
}
/*---------------------------------------------------*/
static
{
bsNEEDW ( n );
s->bsLive += n;
}
/*---------------------------------------------------*/
static
{
}
/*---------------------------------------------------*/
static
{
}
/*---------------------------------------------------*/
/*--- The back end proper ---*/
/*---------------------------------------------------*/
/*---------------------------------------------------*/
static
{
Int32 i;
s->nInUse = 0;
for (i = 0; i < 256; i++)
if (s->inUse[i]) {
s->unseqToSeq[i] = s->nInUse;
s->nInUse++;
}
}
/*---------------------------------------------------*/
static
{
Int32 i, j;
/*
After sorting (eg, here),
s->arr1 [ 0 .. s->nblock-1 ] holds sorted order,
and
((UChar*)s->arr2) [ 0 .. s->nblock-1 ]
holds the original block data.
The first thing to do is generate the MTF values,
and put them in
((UInt16*)s->arr1) [ 0 .. s->nblock-1 ].
Because there are strictly fewer or equal MTF values
than block values, ptr values in this area are overwritten
with MTF values only when they are no longer needed.
The final compressed bitstream is generated into the
area starting at
(UChar*) (&((UChar*)s->arr2)[s->nblock])
These storage aliases are set up in bzCompressInit(),
except for the last one, which is arranged in
compressBlock().
*/
makeMaps_e ( s );
wr = 0;
zPend = 0;
for (i = 0; i < s->nblock; i++) {
zPend++;
} else {
if (zPend > 0) {
zPend--;
while (True) {
if (zPend & 1) {
} else {
}
if (zPend < 2) break;
};
zPend = 0;
}
{
ryy_j++;
};
}
}
}
if (zPend > 0) {
zPend--;
while (True) {
if (zPend & 1) {
} else {
}
if (zPend < 2) break;
};
zPend = 0;
}
}
/*---------------------------------------------------*/
#define BZ_LESSER_ICOST 0
static
{
/*--
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.
--*/
if (s->verbosity >= 3)
VPrintf3( " %d in block, %d after MTF & 1-2 coding, "
"%d+2 syms in use\n",
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 ---*/
nGroups = 6;
/*--- Generate an initial set of coding tables ---*/
{
gs = 0;
while (nPart > 0) {
aFreq = 0;
ge++;
}
ge--;
}
if (s->verbosity >= 3)
VPrintf5( " initial group %d, [%d .. %d], "
"has %d syms (%4.1f%%)\n",
for (v = 0; v < alphaSize; v++)
nPart--;
}
}
/*---
Iterate up to BZ_N_ITERS times to improve the tables.
---*/
for (t = 0; t < nGroups; t++)
for (v = 0; v < alphaSize; v++)
s->rfreq[t][v] = 0;
/*---
Set up an auxiliary length table which is used to fast-track
the common case (nGroups == 6).
---*/
if (nGroups == 6) {
for (v = 0; v < alphaSize; v++) {
}
}
nSelectors = 0;
totc = 0;
gs = 0;
while (True) {
/*--- Set group start & end marks. --*/
/*--
Calculate the cost of this group as coded
by each of the coding tables.
--*/
/*--- fast track the common case ---*/
} else {
/*--- slow version which correctly handles all situations ---*/
}
}
/*--
Find the coding table which is best for this group,
and record its identity in the selector table.
--*/
for (t = 0; t < nGroups; t++)
nSelectors++;
/*--
Increment the symbol frequencies for the selected table.
--*/
/*--- fast track the common case ---*/
} else {
/*--- slow version which correctly handles all situations ---*/
}
}
if (s->verbosity >= 3) {
VPrintf2 ( " pass %d: size is %d, grp uses are ",
for (t = 0; t < nGroups; t++)
VPrintf0 ( "\n" );
}
/*--
Recompute the tables based on the accumulated frequencies.
--*/
/* maxLen was changed from 20 to 17 in bzip2-1.0.3. See
comment in huffman.c for details. */
for (t = 0; t < nGroups; t++)
}
3003 );
/*--- Compute MTF values for the selectors. ---*/
{
for (i = 0; i < nSelectors; i++) {
j = 0;
j++;
};
s->selectorMtf[i] = j;
}
};
/*--- Assign actual codes for the tables. --*/
for (t = 0; t < nGroups; t++) {
minLen = 32;
maxLen = 0;
for (i = 0; i < alphaSize; i++) {
}
}
/*--- Transmit the mapping table. ---*/
{
for (i = 0; i < 16; i++) {
for (j = 0; j < 16; j++)
}
for (i = 0; i < 16; i++)
for (i = 0; i < 16; i++)
if (inUse16[i])
for (j = 0; j < 16; j++) {
}
if (s->verbosity >= 3)
}
/*--- Now the selectors. ---*/
for (i = 0; i < nSelectors; i++) {
bsW(s,1,0);
}
if (s->verbosity >= 3)
/*--- Now the coding tables. ---*/
for (t = 0; t < nGroups; t++) {
for (i = 0; i < alphaSize; i++) {
bsW ( s, 1, 0 );
}
}
if (s->verbosity >= 3)
/*--- And finally, the block data proper ---*/
selCtr = 0;
gs = 0;
while (True) {
/*--- fast track the common case ---*/
bsW ( s, \
} else {
/*--- slow version which correctly handles all situations ---*/
bsW ( s,
}
}
selCtr++;
}
if (s->verbosity >= 3)
}
/*---------------------------------------------------*/
{
if (s->nblock > 0) {
BZ_FINALISE_CRC ( s->blockCRC );
s->combinedCRC ^= s->blockCRC;
if (s->verbosity >= 2)
VPrintf4( " block %d: crc = 0x%08x, "
"combined CRC = 0x%08x, size = %d\n",
BZ2_blockSort ( s );
}
/*-- If this is the first block, create the stream header. --*/
if (s->blockNo == 1) {
BZ2_bsInitWrite ( s );
bsPutUChar ( s, BZ_HDR_B );
bsPutUChar ( s, BZ_HDR_Z );
bsPutUChar ( s, BZ_HDR_h );
}
if (s->nblock > 0) {
/*-- Now the block's CRC, so it is in a known place. --*/
bsPutUInt32 ( s, s->blockCRC );
/*--
Now a single bit indicating (non-)randomisation.
As of version 0.9.5, we use a better sorting algorithm
which makes randomisation unnecessary. So always set
the randomised bit to 'no'. Of course, the decoder
still needs to be able to handle randomised blocks
so as to maintain backwards compatibility with
older versions of bzip2.
--*/
bsW(s,1,0);
generateMTFValues ( s );
sendMTFValues ( s );
}
/*-- If this is the last block, add the stream trailer. --*/
if (is_last_block) {
bsPutUInt32 ( s, s->combinedCRC );
if (s->verbosity >= 2)
bsFinishWrite ( s );
}
}
/*-------------------------------------------------------------*/
/*--- end compress.c ---*/
/*-------------------------------------------------------------*/