|
|
|
@@ -96,12 +96,13 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
private void makeMaps() { |
|
|
|
int i; |
|
|
|
nInUse = 0; |
|
|
|
for (i = 0; i < 256; i++) |
|
|
|
for (i = 0; i < 256; i++) { |
|
|
|
if (inUse[i]) { |
|
|
|
seqToUnseq[nInUse] = (char)i; |
|
|
|
unseqToSeq[i] = (char)nInUse; |
|
|
|
nInUse++; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
protected static void hbMakeCodeLengths(char[] len, int[] freq, |
|
|
|
@@ -117,8 +118,9 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
int weight[] = new int[MAX_ALPHA_SIZE * 2]; |
|
|
|
int parent[] = new int[MAX_ALPHA_SIZE * 2]; |
|
|
|
|
|
|
|
for (i = 0; i < alphaSize; i++) |
|
|
|
for (i = 0; i < alphaSize; i++) { |
|
|
|
weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8; |
|
|
|
} |
|
|
|
|
|
|
|
while (true) { |
|
|
|
nNodes = alphaSize; |
|
|
|
@@ -143,8 +145,9 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
heap[zz] = tmp; |
|
|
|
} |
|
|
|
} |
|
|
|
if (!(nHeap < (MAX_ALPHA_SIZE+2))) |
|
|
|
if (!(nHeap < (MAX_ALPHA_SIZE+2))) { |
|
|
|
panic(); |
|
|
|
} |
|
|
|
|
|
|
|
while (nHeap > 1) { |
|
|
|
n1 = heap[1]; |
|
|
|
@@ -156,13 +159,16 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
tmp = heap[zz]; |
|
|
|
while (true) { |
|
|
|
yy = zz << 1; |
|
|
|
if (yy > nHeap) |
|
|
|
if (yy > nHeap) { |
|
|
|
break; |
|
|
|
} |
|
|
|
if (yy < nHeap && |
|
|
|
weight[heap[yy+1]] < weight[heap[yy]]) |
|
|
|
weight[heap[yy+1]] < weight[heap[yy]]) { |
|
|
|
yy++; |
|
|
|
if (weight[tmp] < weight[heap[yy]]) |
|
|
|
} |
|
|
|
if (weight[tmp] < weight[heap[yy]]) { |
|
|
|
break; |
|
|
|
} |
|
|
|
heap[zz] = heap[yy]; |
|
|
|
zz = yy; |
|
|
|
} |
|
|
|
@@ -177,13 +183,16 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
tmp = heap[zz]; |
|
|
|
while (true) { |
|
|
|
yy = zz << 1; |
|
|
|
if (yy > nHeap) |
|
|
|
if (yy > nHeap) { |
|
|
|
break; |
|
|
|
} |
|
|
|
if (yy < nHeap && |
|
|
|
weight[heap[yy+1]] < weight[heap[yy]]) |
|
|
|
weight[heap[yy+1]] < weight[heap[yy]]) { |
|
|
|
yy++; |
|
|
|
if (weight[tmp] < weight[heap[yy]]) |
|
|
|
} |
|
|
|
if (weight[tmp] < weight[heap[yy]]) { |
|
|
|
break; |
|
|
|
} |
|
|
|
heap[zz] = heap[yy]; |
|
|
|
zz = yy; |
|
|
|
} |
|
|
|
@@ -213,8 +222,9 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
heap[zz] = tmp; |
|
|
|
} |
|
|
|
} |
|
|
|
if (!(nNodes < (MAX_ALPHA_SIZE * 2))) |
|
|
|
if (!(nNodes < (MAX_ALPHA_SIZE * 2))) { |
|
|
|
panic(); |
|
|
|
} |
|
|
|
|
|
|
|
tooLong = false; |
|
|
|
for (i = 1; i <= alphaSize; i++) { |
|
|
|
@@ -225,12 +235,14 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
j++; |
|
|
|
} |
|
|
|
len[i-1] = (char)j; |
|
|
|
if (j > maxLen) |
|
|
|
if (j > maxLen) { |
|
|
|
tooLong = true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (! tooLong) |
|
|
|
if (! tooLong) { |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
for (i = 1; i < alphaSize; i++) { |
|
|
|
j = weight[i] >> 8; |
|
|
|
@@ -403,11 +415,13 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
} |
|
|
|
|
|
|
|
public void close() throws IOException { |
|
|
|
if(closed) |
|
|
|
if(closed) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if(runLength > 0) |
|
|
|
if(runLength > 0) { |
|
|
|
writeRun(); |
|
|
|
} |
|
|
|
currentChar = -1; |
|
|
|
endBlock(); |
|
|
|
endCompression(); |
|
|
|
@@ -445,8 +459,9 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
last = -1; |
|
|
|
// ch = 0; |
|
|
|
|
|
|
|
for(int i = 0; i < 256; i++) |
|
|
|
for(int i = 0; i < 256; i++) { |
|
|
|
inUse[i] = false; |
|
|
|
} |
|
|
|
|
|
|
|
/* 20 is just a paranoia constant */ |
|
|
|
allowableBlockSize = baseBlockSize * blockSize100k - 20; |
|
|
|
@@ -521,11 +536,12 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
|
|
|
|
vec = 0; |
|
|
|
for (n = minLen; n <= maxLen; n++) { |
|
|
|
for (i = 0; i < alphaSize; i++) |
|
|
|
for (i = 0; i < alphaSize; i++) { |
|
|
|
if (length[i] == n) { |
|
|
|
code[i] = vec; |
|
|
|
vec++; |
|
|
|
}; |
|
|
|
} |
|
|
|
}; |
|
|
|
vec <<= 1; |
|
|
|
} |
|
|
|
} |
|
|
|
@@ -593,24 +609,28 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
int nGroups, nBytes; |
|
|
|
|
|
|
|
alphaSize = nInUse + 2; |
|
|
|
for (t = 0; t < N_GROUPS; t++) |
|
|
|
for (v = 0; v < alphaSize; v++) |
|
|
|
for (t = 0; t < N_GROUPS; t++) { |
|
|
|
for (v = 0; v < alphaSize; v++) { |
|
|
|
len[t][v] = (char)GREATER_ICOST; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/* Decide how many coding tables to use */ |
|
|
|
if (nMTF <= 0) |
|
|
|
if (nMTF <= 0) { |
|
|
|
panic(); |
|
|
|
} |
|
|
|
|
|
|
|
if (nMTF < 200) |
|
|
|
if (nMTF < 200) { |
|
|
|
nGroups = 2; |
|
|
|
else if (nMTF < 600) |
|
|
|
} else if (nMTF < 600) { |
|
|
|
nGroups = 3; |
|
|
|
else if (nMTF < 1200) |
|
|
|
} else if (nMTF < 1200) { |
|
|
|
nGroups = 4; |
|
|
|
else if (nMTF < 2400) |
|
|
|
} else if (nMTF < 2400) { |
|
|
|
nGroups = 5; |
|
|
|
else |
|
|
|
} else { |
|
|
|
nGroups = 6; |
|
|
|
} |
|
|
|
|
|
|
|
/* Generate an initial set of coding tables */ { |
|
|
|
int nPart, remF, tFreq, aFreq; |
|
|
|
@@ -633,11 +653,13 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
ge--; |
|
|
|
} |
|
|
|
|
|
|
|
for (v = 0; v < alphaSize; v++) |
|
|
|
if (v >= gs && v <= ge) |
|
|
|
for (v = 0; v < alphaSize; v++) { |
|
|
|
if (v >= gs && v <= ge) { |
|
|
|
len[nPart-1][v] = (char)LESSER_ICOST; |
|
|
|
else |
|
|
|
} else { |
|
|
|
len[nPart-1][v] = (char)GREATER_ICOST; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
nPart--; |
|
|
|
gs = ge+1; |
|
|
|
@@ -652,12 +674,14 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
Iterate up to N_ITERS times to improve the tables. |
|
|
|
*/ |
|
|
|
for (iter = 0; iter < N_ITERS; iter++) { |
|
|
|
for (t = 0; t < nGroups; t++) |
|
|
|
for (t = 0; t < nGroups; t++) { |
|
|
|
fave[t] = 0; |
|
|
|
} |
|
|
|
|
|
|
|
for (t = 0; t < nGroups; t++) |
|
|
|
for (v = 0; v < alphaSize; v++) |
|
|
|
for (v = 0; v < alphaSize; v++) { |
|
|
|
rfreq[t][v] = 0; |
|
|
|
} |
|
|
|
|
|
|
|
nSelectors = 0; |
|
|
|
totc = 0; |
|
|
|
@@ -665,18 +689,22 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
while (true) { |
|
|
|
|
|
|
|
/* Set group start & end marks. */ |
|
|
|
if (gs >= nMTF) |
|
|
|
if (gs >= nMTF) { |
|
|
|
break; |
|
|
|
} |
|
|
|
ge = gs + G_SIZE - 1; |
|
|
|
if (ge >= nMTF) |
|
|
|
if (ge >= nMTF) { |
|
|
|
ge = nMTF-1; |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
Calculate the cost of this group as coded |
|
|
|
by each of the coding tables. |
|
|
|
*/ |
|
|
|
for (t = 0; t < nGroups; t++) |
|
|
|
cost[t] = 0; |
|
|
|
for (t = 0; t < nGroups; t++) { { |
|
|
|
cost[t |
|
|
|
}] = 0; |
|
|
|
} |
|
|
|
|
|
|
|
if (nGroups == 6) { |
|
|
|
short cost0, cost1, cost2, cost3, cost4, cost5; |
|
|
|
@@ -699,8 +727,9 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
} else { |
|
|
|
for (i = gs; i <= ge; i++) { |
|
|
|
short icv = szptr[i]; |
|
|
|
for (t = 0; t < nGroups; t++) |
|
|
|
for (t = 0; t < nGroups; t++) { |
|
|
|
cost[t] += len[t][icv]; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@@ -710,11 +739,12 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
*/ |
|
|
|
bc = 999999999; |
|
|
|
bt = -1; |
|
|
|
for (t = 0; t < nGroups; t++) |
|
|
|
for (t = 0; t < nGroups; t++) { |
|
|
|
if (cost[t] < bc) { |
|
|
|
bc = cost[t]; |
|
|
|
bt = t; |
|
|
|
}; |
|
|
|
} |
|
|
|
}; |
|
|
|
totc += bc; |
|
|
|
fave[bt]++; |
|
|
|
selector[nSelectors] = (char)bt; |
|
|
|
@@ -723,8 +753,9 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
/* |
|
|
|
Increment the symbol frequencies for the selected table. |
|
|
|
*/ |
|
|
|
for (i = gs; i <= ge; i++) |
|
|
|
for (i = gs; i <= ge; i++) { |
|
|
|
rfreq[bt][szptr[i]]++; |
|
|
|
} |
|
|
|
|
|
|
|
gs = ge+1; |
|
|
|
} |
|
|
|
@@ -732,8 +763,9 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
/* |
|
|
|
Recompute the tables based on the accumulated frequencies. |
|
|
|
*/ |
|
|
|
for (t = 0; t < nGroups; t++) |
|
|
|
for (t = 0; t < nGroups; t++) { |
|
|
|
hbMakeCodeLengths(len[t], rfreq[t], alphaSize, 20); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
rfreq = null; |
|
|
|
@@ -742,16 +774,18 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
|
|
|
|
if (!(nGroups < 8)) |
|
|
|
panic(); |
|
|
|
if (!(nSelectors < 32768 && nSelectors <= (2 + (900000 / G_SIZE)))) |
|
|
|
if (!(nSelectors < 32768 && nSelectors <= (2 + (900000 / G_SIZE)))) { |
|
|
|
panic(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* Compute MTF values for the selectors. */ |
|
|
|
{ |
|
|
|
char pos[] = new char[N_GROUPS]; |
|
|
|
char ll_i, tmp2, tmp; |
|
|
|
for (i = 0; i < nGroups; i++) |
|
|
|
for (i = 0; i < nGroups; i++) { |
|
|
|
pos[i] = (char)i; |
|
|
|
} |
|
|
|
for (i = 0; i < nSelectors; i++) { |
|
|
|
ll_i = selector[i]; |
|
|
|
j = 0; |
|
|
|
@@ -763,7 +797,8 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
pos[j] = tmp2; |
|
|
|
} |
|
|
|
pos[0] = tmp; |
|
|
|
selectorMtf[i] = (char)j; |
|
|
|
|
|
|
|
} selectorMtf[i] = (char) {j; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@@ -774,15 +809,19 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
minLen = 32; |
|
|
|
maxLen = 0; |
|
|
|
for (i = 0; i < alphaSize; i++) { |
|
|
|
if (len[t][i] > maxLen) |
|
|
|
if (len[t][i] > maxLen) { |
|
|
|
maxLen = len[t][i]; |
|
|
|
if (len[t][i] < minLen) |
|
|
|
} |
|
|
|
if (len[t][i] < minLen) { |
|
|
|
minLen = len[t][i]; |
|
|
|
} |
|
|
|
} |
|
|
|
if (maxLen > 20) |
|
|
|
if (maxLen > 20) { |
|
|
|
panic(); |
|
|
|
if (minLen < 1) |
|
|
|
} |
|
|
|
if (minLen < 1) { |
|
|
|
panic(); |
|
|
|
} |
|
|
|
hbAssignCodes(code[t], len[t], minLen, maxLen, alphaSize); |
|
|
|
} |
|
|
|
|
|
|
|
@@ -791,25 +830,33 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
boolean inUse16[] = new boolean[16]; |
|
|
|
for (i = 0; i < 16; i++) { |
|
|
|
inUse16[i] = false; |
|
|
|
for (j = 0; j < 16; j++) |
|
|
|
if (inUse[i * 16 + j]) |
|
|
|
for (j = 0; j < 16; j++) { |
|
|
|
if (inUse[i * 16 + j]) { |
|
|
|
inUse16[i] = true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
nBytes = bytesOut; |
|
|
|
for (i = 0; i < 16; i++) |
|
|
|
if (inUse16[i]) |
|
|
|
for (i = 0; i < 16; i++) { |
|
|
|
if (inUse16[i]) { |
|
|
|
bsW(1,1); |
|
|
|
else |
|
|
|
} else { |
|
|
|
bsW(1,0); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
for (i = 0; i < 16; i++) |
|
|
|
if (inUse16[i]) |
|
|
|
for (j = 0; j < 16; j++) |
|
|
|
if (inUse[i * 16 + j]) |
|
|
|
for (i = 0; i < 16; i++) { |
|
|
|
if (inUse16[i]) { |
|
|
|
for (j = 0; j < 16; j++) { |
|
|
|
if (inUse[i * 16 + j]) { |
|
|
|
bsW(1,1); |
|
|
|
else |
|
|
|
} else { |
|
|
|
bsW(1,0); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@@ -818,8 +865,9 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
bsW ( 3, nGroups ); |
|
|
|
bsW ( 15, nSelectors ); |
|
|
|
for (i = 0; i < nSelectors; i++) { |
|
|
|
for (j = 0; j < selectorMtf[i]; j++) |
|
|
|
for (j = 0; j < selectorMtf[i]; j++) { |
|
|
|
bsW(1,1); |
|
|
|
} |
|
|
|
bsW(1,0); |
|
|
|
} |
|
|
|
|
|
|
|
@@ -847,11 +895,13 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
selCtr = 0; |
|
|
|
gs = 0; |
|
|
|
while (true) { |
|
|
|
if (gs >= nMTF) |
|
|
|
if (gs >= nMTF) { |
|
|
|
break; |
|
|
|
} |
|
|
|
ge = gs + G_SIZE - 1; |
|
|
|
if (ge >= nMTF) |
|
|
|
if (ge >= nMTF) { |
|
|
|
ge = nMTF-1; |
|
|
|
} |
|
|
|
for (i = gs; i <= ge; i++) { |
|
|
|
bsW(len [selector[selCtr]] [szptr[i]], |
|
|
|
code [selector[selCtr]] [szptr[i]] ); |
|
|
|
@@ -860,8 +910,9 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
gs = ge+1; |
|
|
|
selCtr++; |
|
|
|
} |
|
|
|
if (!(selCtr == nSelectors)) |
|
|
|
if (!(selCtr == nSelectors)) { |
|
|
|
panic(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void moveToFrontCodeAndSend () throws IOException { |
|
|
|
@@ -877,12 +928,14 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
int v; |
|
|
|
|
|
|
|
bigN = hi - lo + 1; |
|
|
|
if (bigN < 2) |
|
|
|
if (bigN < 2) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
hp = 0; |
|
|
|
while (incs[hp] < bigN) |
|
|
|
while (incs[hp] < bigN) { |
|
|
|
hp++; |
|
|
|
} |
|
|
|
hp--; |
|
|
|
|
|
|
|
for (; hp >= 0; hp--) { |
|
|
|
@@ -891,49 +944,56 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
i = lo + h; |
|
|
|
while (true) { |
|
|
|
/* copy 1 */ |
|
|
|
if (i > hi) |
|
|
|
if (i > hi) { |
|
|
|
break; |
|
|
|
} |
|
|
|
v = zptr[i]; |
|
|
|
j = i; |
|
|
|
while ( fullGtU ( zptr[j-h]+d, v+d ) ) { |
|
|
|
zptr[j] = zptr[j-h]; |
|
|
|
j = j - h; |
|
|
|
if (j <= (lo + h - 1)) |
|
|
|
if (j <= (lo + h - 1)) { |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
zptr[j] = v; |
|
|
|
i++; |
|
|
|
|
|
|
|
/* copy 2 */ |
|
|
|
if (i > hi) |
|
|
|
if (i > hi) { |
|
|
|
break; |
|
|
|
} |
|
|
|
v = zptr[i]; |
|
|
|
j = i; |
|
|
|
while ( fullGtU ( zptr[j-h]+d, v+d ) ) { |
|
|
|
zptr[j] = zptr[j-h]; |
|
|
|
j = j - h; |
|
|
|
if (j <= (lo + h - 1)) |
|
|
|
if (j <= (lo + h - 1)) { |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
zptr[j] = v; |
|
|
|
i++; |
|
|
|
|
|
|
|
/* copy 3 */ |
|
|
|
if (i > hi) |
|
|
|
if (i > hi) { |
|
|
|
break; |
|
|
|
} |
|
|
|
v = zptr[i]; |
|
|
|
j = i; |
|
|
|
while ( fullGtU ( zptr[j-h]+d, v+d ) ) { |
|
|
|
zptr[j] = zptr[j-h]; |
|
|
|
j = j - h; |
|
|
|
if (j <= (lo + h - 1)) |
|
|
|
if (j <= (lo + h - 1)) { |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
zptr[j] = v; |
|
|
|
i++; |
|
|
|
|
|
|
|
if (workDone > workLimit && firstAttempt) |
|
|
|
if (workDone > workLimit && firstAttempt) { |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@@ -962,8 +1022,9 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
b = c; |
|
|
|
c = t; |
|
|
|
} |
|
|
|
if (a > b) |
|
|
|
if (a > b) { |
|
|
|
b = a; |
|
|
|
} |
|
|
|
return b; |
|
|
|
} |
|
|
|
|
|
|
|
@@ -977,8 +1038,9 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
int unLo, unHi, ltLo, gtHi, med, n, m; |
|
|
|
int sp, lo, hi, d; |
|
|
|
StackElem[] stack = new StackElem[QSORT_STACK_SIZE]; |
|
|
|
for(int count = 0; count < QSORT_STACK_SIZE; count++) |
|
|
|
for(int count = 0; count < QSORT_STACK_SIZE; count++) { |
|
|
|
stack[count] = new StackElem(); |
|
|
|
} |
|
|
|
|
|
|
|
sp = 0; |
|
|
|
|
|
|
|
@@ -988,8 +1050,9 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
sp++; |
|
|
|
|
|
|
|
while (sp > 0) { |
|
|
|
if (sp >= QSORT_STACK_SIZE) |
|
|
|
if (sp >= QSORT_STACK_SIZE) { |
|
|
|
panic(); |
|
|
|
} |
|
|
|
|
|
|
|
sp--; |
|
|
|
lo = stack[sp].ll; |
|
|
|
@@ -998,8 +1061,9 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
|
|
|
|
if (hi - lo < SMALL_THRESH || d > DEPTH_THRESH) { |
|
|
|
simpleSort(lo, hi, d); |
|
|
|
if (workDone > workLimit && firstAttempt) |
|
|
|
if (workDone > workLimit && firstAttempt) { |
|
|
|
return; |
|
|
|
} |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
@@ -1012,8 +1076,9 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
|
|
|
|
while (true) { |
|
|
|
while (true) { |
|
|
|
if (unLo > unHi) |
|
|
|
if (unLo > unHi) { |
|
|
|
break; |
|
|
|
} |
|
|
|
n = ((int)block[zptr[unLo]+d + 1]) - med; |
|
|
|
if (n == 0) { |
|
|
|
int temp = 0; |
|
|
|
@@ -1024,13 +1089,15 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
unLo++; |
|
|
|
continue; |
|
|
|
}; |
|
|
|
if (n > 0) |
|
|
|
if (n > 0) { |
|
|
|
break; |
|
|
|
} |
|
|
|
unLo++; |
|
|
|
} |
|
|
|
while (true) { |
|
|
|
if (unLo > unHi) |
|
|
|
if (unLo > unHi) { |
|
|
|
break; |
|
|
|
} |
|
|
|
n = ((int)block[zptr[unHi]+d + 1]) - med; |
|
|
|
if (n == 0) { |
|
|
|
int temp = 0; |
|
|
|
@@ -1041,12 +1108,14 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
unHi--; |
|
|
|
continue; |
|
|
|
}; |
|
|
|
if (n < 0) |
|
|
|
if (n < 0) { |
|
|
|
break; |
|
|
|
} |
|
|
|
unHi--; |
|
|
|
} |
|
|
|
if (unLo > unHi) |
|
|
|
if (unLo > unHi) { |
|
|
|
break; |
|
|
|
} |
|
|
|
int temp = 0; |
|
|
|
temp = zptr[unLo]; |
|
|
|
zptr[unLo] = zptr[unHi]; |
|
|
|
@@ -1103,10 +1172,12 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
*/ |
|
|
|
|
|
|
|
// if (verbosity >= 4) fprintf ( stderr, " sort initialise ...\n" ); |
|
|
|
for (i = 0; i < NUM_OVERSHOOT_BYTES; i++) |
|
|
|
for (i = 0; i < NUM_OVERSHOOT_BYTES; i++) { |
|
|
|
block[last + i + 2] = block[(i % (last + 1)) + 1]; |
|
|
|
for (i = 0; i <= last + NUM_OVERSHOOT_BYTES; i++) |
|
|
|
} |
|
|
|
for (i = 0; i <= last + NUM_OVERSHOOT_BYTES; i++) { |
|
|
|
quadrant[i] = 0; |
|
|
|
} |
|
|
|
|
|
|
|
block[0] = (char)(block[last + 1]); |
|
|
|
|
|
|
|
@@ -1115,8 +1186,9 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
Use simpleSort(), since the full sorting mechanism |
|
|
|
has quite a large constant overhead. |
|
|
|
*/ |
|
|
|
for (i = 0; i <= last; i++) |
|
|
|
for (i = 0; i <= last; i++) { |
|
|
|
zptr[i] = i; |
|
|
|
} |
|
|
|
firstAttempt = false; |
|
|
|
workDone = workLimit = 0; |
|
|
|
simpleSort ( 0, last, 0 ); |
|
|
|
@@ -1125,8 +1197,9 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
for (i = 0; i <= 255; i++) |
|
|
|
bigDone[i] = false; |
|
|
|
|
|
|
|
for (i = 0; i <= 65536; i++) |
|
|
|
for (i = 0; i <= 65536; i++) { |
|
|
|
ftab[i] = 0; |
|
|
|
} |
|
|
|
|
|
|
|
c1 = block[0]; |
|
|
|
for (i = 0; i <= last; i++) { |
|
|
|
@@ -1135,8 +1208,9 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
c1 = c2; |
|
|
|
} |
|
|
|
|
|
|
|
for (i = 1; i <= 65536; i++) |
|
|
|
for (i = 1; i <= 65536; i++) { |
|
|
|
ftab[i] += ftab[i - 1]; |
|
|
|
} |
|
|
|
|
|
|
|
c1 = block[1]; |
|
|
|
for (i = 0; i < last; i++) { |
|
|
|
@@ -1157,14 +1231,16 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
big bucket. |
|
|
|
*/ |
|
|
|
|
|
|
|
for (i = 0; i <= 255; i++) |
|
|
|
for (i = 0; i <= 255; i++) { |
|
|
|
runningOrder[i] = i; |
|
|
|
} |
|
|
|
|
|
|
|
{ |
|
|
|
int vv; |
|
|
|
int h = 1; |
|
|
|
do |
|
|
|
do { |
|
|
|
h = 3 * h + 1; |
|
|
|
} |
|
|
|
while (h <= 256); |
|
|
|
do { |
|
|
|
h = h / 3; |
|
|
|
@@ -1172,12 +1248,15 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
vv = runningOrder[i]; |
|
|
|
j = i; |
|
|
|
while ((ftab[((runningOrder[j-h])+1) << 8] |
|
|
|
- ftab[(runningOrder[j-h]) << 8]) > |
|
|
|
- ftab[(runningOrder[j-h]) { |
|
|
|
|
|
|
|
} << 8]) > |
|
|
|
(ftab[((vv)+1) << 8] - ftab[(vv) << 8])) { |
|
|
|
runningOrder[j] = runningOrder[j-h]; |
|
|
|
j = j - h; |
|
|
|
if (j <= (h - 1)) |
|
|
|
if (j <= (h - 1)) { |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
runningOrder[j] = vv; |
|
|
|
} |
|
|
|
@@ -1209,8 +1288,9 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
if (hi > lo) { |
|
|
|
qSort3 ( lo, hi, 2 ); |
|
|
|
numQSorted += ( hi - lo + 1 ); |
|
|
|
if (workDone > workLimit && firstAttempt) |
|
|
|
if (workDone > workLimit && firstAttempt) { |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
ftab[sb] |= SETMASK; |
|
|
|
} |
|
|
|
@@ -1231,27 +1311,31 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
int bbSize = (ftab[(ss+1) << 8] & CLEARMASK) - bbStart; |
|
|
|
int shifts = 0; |
|
|
|
|
|
|
|
while ((bbSize >> shifts) > 65534) |
|
|
|
while ((bbSize >> shifts) > 65534) { |
|
|
|
shifts++; |
|
|
|
} |
|
|
|
|
|
|
|
for (j = 0; j < bbSize; j++) { |
|
|
|
int a2update = zptr[bbStart + j]; |
|
|
|
int qVal = (j >> shifts); |
|
|
|
quadrant[a2update] = qVal; |
|
|
|
if (a2update < NUM_OVERSHOOT_BYTES) |
|
|
|
if (a2update < NUM_OVERSHOOT_BYTES) { |
|
|
|
quadrant[a2update + last + 1] = qVal; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (! ( ((bbSize-1) >> shifts) <= 65535 )) |
|
|
|
if (! ( ((bbSize-1) >> shifts) <= 65535 )) { |
|
|
|
panic(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
Now scan this big bucket so as to synthesise the |
|
|
|
sorted order for small buckets [t, ss] for all t != ss. |
|
|
|
*/ |
|
|
|
for (j = 0; j <= 255; j++) |
|
|
|
for (j = 0; j <= 255; j++) { |
|
|
|
copy[j] = ftab[(j << 8) + ss] & CLEARMASK; |
|
|
|
} |
|
|
|
|
|
|
|
for (j = ftab[ss << 8] & CLEARMASK; |
|
|
|
j < (ftab[(ss+1) << 8] & CLEARMASK); j++) { |
|
|
|
@@ -1262,8 +1346,9 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
for (j = 0; j <= 255; j++) |
|
|
|
for (j = 0; j <= 255; j++) { |
|
|
|
ftab[(j << 8) + ss] |= SETMASK; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@@ -1272,15 +1357,17 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
int i; |
|
|
|
int rNToGo = 0; |
|
|
|
int rTPos = 0; |
|
|
|
for (i = 0; i < 256; i++) |
|
|
|
for (i = 0; i < 256; i++) { |
|
|
|
inUse[i] = false; |
|
|
|
} |
|
|
|
|
|
|
|
for (i = 0; i <= last; i++) { |
|
|
|
if (rNToGo == 0) { |
|
|
|
rNToGo = (char)rNums[rTPos]; |
|
|
|
rTPos++; |
|
|
|
if(rTPos == 512) |
|
|
|
if(rTPos == 512) { |
|
|
|
rTPos = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
rNToGo--; |
|
|
|
block[i + 1] ^= ((rNToGo == 1) ? 1 : 0); |
|
|
|
@@ -1310,14 +1397,16 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
} |
|
|
|
|
|
|
|
origPtr = -1; |
|
|
|
for (i = 0; i <= last; i++) |
|
|
|
for (i = 0; i <= last; i++) { |
|
|
|
if (zptr[i] == 0) { |
|
|
|
origPtr = i; |
|
|
|
break; |
|
|
|
}; |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
if (origPtr == -1) |
|
|
|
if (origPtr == -1) { |
|
|
|
panic(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private boolean fullGtU(int i1, int i2) { |
|
|
|
@@ -1327,43 +1416,49 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
|
|
|
|
c1 = block[i1 + 1]; |
|
|
|
c2 = block[i2 + 1]; |
|
|
|
if (c1 != c2) |
|
|
|
if (c1 != c2) { |
|
|
|
return (c1 > c2); |
|
|
|
} |
|
|
|
i1++; |
|
|
|
i2++; |
|
|
|
|
|
|
|
c1 = block[i1 + 1]; |
|
|
|
c2 = block[i2 + 1]; |
|
|
|
if (c1 != c2) |
|
|
|
if (c1 != c2) { |
|
|
|
return (c1 > c2); |
|
|
|
} |
|
|
|
i1++; |
|
|
|
i2++; |
|
|
|
|
|
|
|
c1 = block[i1 + 1]; |
|
|
|
c2 = block[i2 + 1]; |
|
|
|
if (c1 != c2) |
|
|
|
if (c1 != c2) { |
|
|
|
return (c1 > c2); |
|
|
|
} |
|
|
|
i1++; |
|
|
|
i2++; |
|
|
|
|
|
|
|
c1 = block[i1 + 1]; |
|
|
|
c2 = block[i2 + 1]; |
|
|
|
if (c1 != c2) |
|
|
|
if (c1 != c2) { |
|
|
|
return (c1 > c2); |
|
|
|
} |
|
|
|
i1++; |
|
|
|
i2++; |
|
|
|
|
|
|
|
c1 = block[i1 + 1]; |
|
|
|
c2 = block[i2 + 1]; |
|
|
|
if (c1 != c2) |
|
|
|
if (c1 != c2) { |
|
|
|
return (c1 > c2); |
|
|
|
} |
|
|
|
i1++; |
|
|
|
i2++; |
|
|
|
|
|
|
|
c1 = block[i1 + 1]; |
|
|
|
c2 = block[i2 + 1]; |
|
|
|
if (c1 != c2) |
|
|
|
if (c1 != c2) { |
|
|
|
return (c1 > c2); |
|
|
|
} |
|
|
|
i1++; |
|
|
|
i2++; |
|
|
|
|
|
|
|
@@ -1372,45 +1467,53 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
do { |
|
|
|
c1 = block[i1 + 1]; |
|
|
|
c2 = block[i2 + 1]; |
|
|
|
if (c1 != c2) |
|
|
|
if (c1 != c2) { |
|
|
|
return (c1 > c2); |
|
|
|
} |
|
|
|
s1 = quadrant[i1]; |
|
|
|
s2 = quadrant[i2]; |
|
|
|
if (s1 != s2) |
|
|
|
if (s1 != s2) { |
|
|
|
return (s1 > s2); |
|
|
|
} |
|
|
|
i1++; |
|
|
|
i2++; |
|
|
|
|
|
|
|
c1 = block[i1 + 1]; |
|
|
|
c2 = block[i2 + 1]; |
|
|
|
if (c1 != c2) |
|
|
|
if (c1 != c2) { |
|
|
|
return (c1 > c2); |
|
|
|
} |
|
|
|
s1 = quadrant[i1]; |
|
|
|
s2 = quadrant[i2]; |
|
|
|
if (s1 != s2) |
|
|
|
if (s1 != s2) { |
|
|
|
return (s1 > s2); |
|
|
|
} |
|
|
|
i1++; |
|
|
|
i2++; |
|
|
|
|
|
|
|
c1 = block[i1 + 1]; |
|
|
|
c2 = block[i2 + 1]; |
|
|
|
if (c1 != c2) |
|
|
|
if (c1 != c2) { |
|
|
|
return (c1 > c2); |
|
|
|
} |
|
|
|
s1 = quadrant[i1]; |
|
|
|
s2 = quadrant[i2]; |
|
|
|
if (s1 != s2) |
|
|
|
if (s1 != s2) { |
|
|
|
return (s1 > s2); |
|
|
|
} |
|
|
|
i1++; |
|
|
|
i2++; |
|
|
|
|
|
|
|
c1 = block[i1 + 1]; |
|
|
|
c2 = block[i2 + 1]; |
|
|
|
if (c1 != c2) |
|
|
|
if (c1 != c2) { |
|
|
|
return (c1 > c2); |
|
|
|
} |
|
|
|
s1 = quadrant[i1]; |
|
|
|
s2 = quadrant[i2]; |
|
|
|
if (s1 != s2) |
|
|
|
if (s1 != s2) { |
|
|
|
return (s1 > s2); |
|
|
|
} |
|
|
|
i1++; |
|
|
|
i2++; |
|
|
|
|
|
|
|
@@ -1481,13 +1584,15 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
makeMaps(); |
|
|
|
EOB = nInUse+1; |
|
|
|
|
|
|
|
for (i = 0; i <= EOB; i++) |
|
|
|
for (i = 0; i <= EOB; i++) { |
|
|
|
mtfFreq[i] = 0; |
|
|
|
} |
|
|
|
|
|
|
|
wr = 0; |
|
|
|
zPend = 0; |
|
|
|
for (i = 0; i < nInUse; i++) |
|
|
|
for (i = 0; i < nInUse; i++) { |
|
|
|
yy[i] = (char) i; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i <= last; i++) { |
|
|
|
@@ -1523,8 +1628,9 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
mtfFreq[RUNB]++; |
|
|
|
break; |
|
|
|
}; |
|
|
|
if (zPend < 2) |
|
|
|
if (zPend < 2) { |
|
|
|
break; |
|
|
|
} |
|
|
|
zPend = (zPend - 2) / 2; |
|
|
|
}; |
|
|
|
zPend = 0; |
|
|
|
@@ -1550,8 +1656,9 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { |
|
|
|
mtfFreq[RUNB]++; |
|
|
|
break; |
|
|
|
} |
|
|
|
if (zPend < 2) |
|
|
|
if (zPend < 2) { |
|
|
|
break; |
|
|
|
} |
|
|
|
zPend = (zPend - 2) / 2; |
|
|
|
} |
|
|
|
} |
|
|
|
|