• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * IndexBase
3  *
4  * Author: Lasse Collin <lasse.collin@tukaani.org>
5  *
6  * This file has been put into the public domain.
7  * You can do whatever you want with this file.
8  */
9 
10 package org.tukaani.xz.index;
11 
12 import org.tukaani.xz.common.Util;
13 import org.tukaani.xz.XZIOException;
14 
15 abstract class IndexBase {
16     private final XZIOException invalidIndexException;
17     long blocksSum = 0;
18     long uncompressedSum = 0;
19     long indexListSize = 0;
20     long recordCount = 0;
21 
IndexBase(XZIOException invalidIndexException)22     IndexBase(XZIOException invalidIndexException) {
23         this.invalidIndexException = invalidIndexException;
24     }
25 
getUnpaddedIndexSize()26     private long getUnpaddedIndexSize() {
27         // Index Indicator + Number of Records + List of Records + CRC32
28         return 1 + Util.getVLISize(recordCount) + indexListSize + 4;
29     }
30 
getIndexSize()31     public long getIndexSize() {
32         return (getUnpaddedIndexSize() + 3) & ~3;
33     }
34 
getStreamSize()35     public long getStreamSize() {
36         return Util.STREAM_HEADER_SIZE + blocksSum + getIndexSize()
37                 + Util.STREAM_HEADER_SIZE;
38     }
39 
getIndexPaddingSize()40     int getIndexPaddingSize() {
41         return (int)((4 - getUnpaddedIndexSize()) & 3);
42     }
43 
add(long unpaddedSize, long uncompressedSize)44     void add(long unpaddedSize, long uncompressedSize) throws XZIOException {
45         blocksSum += (unpaddedSize + 3) & ~3;
46         uncompressedSum += uncompressedSize;
47         indexListSize += Util.getVLISize(unpaddedSize)
48                          + Util.getVLISize(uncompressedSize);
49         ++recordCount;
50 
51         if (blocksSum < 0 || uncompressedSum < 0
52                 || getIndexSize() > Util.BACKWARD_SIZE_MAX
53                 || getStreamSize() < 0)
54             throw invalidIndexException;
55     }
56 }
57