LZWDecoder.java

1
/*
2
 * Copyright 2002-2008 by Paulo Soares.
3
 * 
4
 * This code was originally released in 2001 by SUN (see class
5
 * com.sun.media.imageioimpl.plugins.tiff.TIFFLZWDecompressor.java)
6
 * using the BSD license in a specific wording. In a mail dating from
7
 * January 23, 2008, Brian Burkhalter (@sun.com) gave us permission
8
 * to use the code under the following version of the BSD license:
9
 *
10
 * Copyright (c) 2005 Sun Microsystems, Inc. All  Rights Reserved.
11
 * 
12
 * Redistribution and use in source and binary forms, with or without
13
 * modification, are permitted provided that the following conditions
14
 * are met: 
15
 * 
16
 * - Redistribution of source code must retain the above copyright 
17
 *   notice, this  list of conditions and the following disclaimer.
18
 * 
19
 * - Redistribution in binary form must reproduce the above copyright
20
 *   notice, this list of conditions and the following disclaimer in 
21
 *   the documentation and/or other materials provided with the
22
 *   distribution.
23
 * 
24
 * Neither the name of Sun Microsystems, Inc. or the names of 
25
 * contributors may be used to endorse or promote products derived 
26
 * from this software without specific prior written permission.
27
 * 
28
 * This software is provided "AS IS," without a warranty of any 
29
 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND 
30
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, 
31
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
32
 * EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL 
33
 * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF 
34
 * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
35
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR 
36
 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
37
 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
38
 * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
39
 * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
40
 * POSSIBILITY OF SUCH DAMAGES. 
41
 * 
42
 * You acknowledge that this software is not designed or intended for 
43
 * use in the design, construction, operation or maintenance of any 
44
 * nuclear facility.
45
 */
46
package com.lowagie.text.pdf;
47
import java.io.IOException;
48
import java.io.OutputStream;
49
import com.lowagie.text.error_messages.MessageLocalization;
50
51
import com.lowagie.text.ExceptionConverter;
52
/**
53
 * A class for performing LZW decoding.
54
 *
55
 *
56
 */
57
public class LZWDecoder {
58
59
    byte[][] stringTable;
60
    byte[] data = null;
61
    OutputStream uncompData;
62
    int tableIndex, bitsToGet = 9;
63
    int bytePointer, bitPointer;
64
    int nextData = 0;
65
    int nextBits = 0;
66
67
    int[] andTable = {
68
            511,
69
            1023,
70
            2047,
71
            4095
72
    };
73
    
74
    public LZWDecoder() {
75
    }
76
    
77
    /**
78
     * Method to decode LZW compressed data.
79
     *
80
     * @param data            The compressed data.
81
     * @param uncompData      Array to return the uncompressed data in.
82
     */
83
    public void decode(byte[] data, OutputStream uncompData) {
84
        
85 2 1. decode : negated conditional → NO_COVERAGE
2. decode : negated conditional → NO_COVERAGE
        if(data[0] == (byte)0x00 && data[1] == (byte)0x01) {
86
            throw new RuntimeException(MessageLocalization.getComposedMessage("lzw.flavour.not.supported"));
87
        }
88
        
89 1 1. decode : removed call to com/lowagie/text/pdf/LZWDecoder::initializeStringTable → NO_COVERAGE
        initializeStringTable();
90
        
91
        this.data = data;
92
        this.uncompData = uncompData;
93
        
94
        // Initialize pointers
95
        bytePointer = 0;
96
        bitPointer = 0;
97
        
98
        nextData = 0;
99
        nextBits = 0;
100
        
101
        int code, oldCode = 0;
102
        byte[] string;
103
        
104 1 1. decode : negated conditional → NO_COVERAGE
        while ((code = getNextCode()) != 257) {
105
            
106 1 1. decode : negated conditional → NO_COVERAGE
            if (code == 256) {
107
                
108 1 1. decode : removed call to com/lowagie/text/pdf/LZWDecoder::initializeStringTable → NO_COVERAGE
                initializeStringTable();
109
                code = getNextCode();
110
                
111 1 1. decode : negated conditional → NO_COVERAGE
                if (code == 257) {
112
                    break;
113
                }
114
                
115 1 1. decode : removed call to com/lowagie/text/pdf/LZWDecoder::writeString → NO_COVERAGE
                writeString(stringTable[code]);
116
                oldCode = code;
117
                
118
            } else {
119
                
120 2 1. decode : changed conditional boundary → NO_COVERAGE
2. decode : negated conditional → NO_COVERAGE
                if (code < tableIndex) {
121
                    
122
                    string = stringTable[code];
123
                    
124 1 1. decode : removed call to com/lowagie/text/pdf/LZWDecoder::writeString → NO_COVERAGE
                    writeString(string);
125 1 1. decode : removed call to com/lowagie/text/pdf/LZWDecoder::addStringToTable → NO_COVERAGE
                    addStringToTable(stringTable[oldCode], string[0]);
126
                    oldCode = code;
127
                    
128
                } else {
129
                    
130
                    string = stringTable[oldCode];
131
                    string = composeString(string, string[0]);
132 1 1. decode : removed call to com/lowagie/text/pdf/LZWDecoder::writeString → NO_COVERAGE
                    writeString(string);
133 1 1. decode : removed call to com/lowagie/text/pdf/LZWDecoder::addStringToTable → NO_COVERAGE
                    addStringToTable(string);
134
                    oldCode = code;
135
                }
136
            }
137
        }
138
    }
139
    
140
    
141
    /**
142
     * Initialize the string table.
143
     */
144
    public void initializeStringTable() {
145
        
146
        stringTable = new byte[8192][];
147
        
148 3 1. initializeStringTable : changed conditional boundary → NO_COVERAGE
2. initializeStringTable : Changed increment from 1 to -1 → NO_COVERAGE
3. initializeStringTable : negated conditional → NO_COVERAGE
        for (int i=0; i<256; i++) {
149
            stringTable[i] = new byte[1];
150
            stringTable[i][0] = (byte)i;
151
        }
152
        
153
        tableIndex = 258;
154
        bitsToGet = 9;
155
    }
156
    
157
    /**
158
     * Write out the string just uncompressed.
159
     */
160
    public void writeString(byte[] string) {
161
        try {
162 1 1. writeString : removed call to java/io/OutputStream::write → NO_COVERAGE
            uncompData.write(string);
163
        }
164
        catch (IOException e) {
165
            throw new ExceptionConverter(e);
166
        }
167
    }
168
    
169
    /**
170
     * Add a new string to the string table.
171
     */
172
    public void addStringToTable(byte[] oldString, byte newString) {
173
        int length = oldString.length;
174 1 1. addStringToTable : Replaced integer addition with subtraction → NO_COVERAGE
        byte[] string = new byte[length + 1];
175 1 1. addStringToTable : removed call to java/lang/System::arraycopy → NO_COVERAGE
        System.arraycopy(oldString, 0, string, 0, length);
176
        string[length] = newString;
177
        
178
        // Add this new String to the table
179 1 1. addStringToTable : Replaced integer addition with subtraction → NO_COVERAGE
        stringTable[tableIndex++] = string;
180
        
181 1 1. addStringToTable : negated conditional → NO_COVERAGE
        if (tableIndex == 511) {
182
            bitsToGet = 10;
183 1 1. addStringToTable : negated conditional → NO_COVERAGE
        } else if (tableIndex == 1023) {
184
            bitsToGet = 11;
185 1 1. addStringToTable : negated conditional → NO_COVERAGE
        } else if (tableIndex == 2047) {
186
            bitsToGet = 12;
187
        }
188
    }
189
    
190
    /**
191
     * Add a new string to the string table.
192
     */
193
    public void addStringToTable(byte[] string) {
194
        
195
        // Add this new String to the table
196 1 1. addStringToTable : Replaced integer addition with subtraction → NO_COVERAGE
        stringTable[tableIndex++] = string;
197
        
198 1 1. addStringToTable : negated conditional → NO_COVERAGE
        if (tableIndex == 511) {
199
            bitsToGet = 10;
200 1 1. addStringToTable : negated conditional → NO_COVERAGE
        } else if (tableIndex == 1023) {
201
            bitsToGet = 11;
202 1 1. addStringToTable : negated conditional → NO_COVERAGE
        } else if (tableIndex == 2047) {
203
            bitsToGet = 12;
204
        }
205
    }
206
    
207
    /**
208
     * Append <code>newString</code> to the end of <code>oldString</code>.
209
     */
210
    public byte[] composeString(byte[] oldString, byte newString) {
211
        int length = oldString.length;
212 1 1. composeString : Replaced integer addition with subtraction → NO_COVERAGE
        byte[] string = new byte[length + 1];
213 1 1. composeString : removed call to java/lang/System::arraycopy → NO_COVERAGE
        System.arraycopy(oldString, 0, string, 0, length);
214
        string[length] = newString;
215
        
216 1 1. composeString : mutated return of Object value for com/lowagie/text/pdf/LZWDecoder::composeString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return string;
217
    }
218
    
219
    // Returns the next 9, 10, 11 or 12 bits
220
    public int getNextCode() {
221
        // Attempt to get the next code. The exception is caught to make
222
        // this robust to cases wherein the EndOfInformation code has been
223
        // omitted from a strip. Examples of such cases have been observed
224
        // in practice.
225
        try {
226 4 1. getNextCode : Replaced Shift Left with Shift Right → NO_COVERAGE
2. getNextCode : Replaced integer addition with subtraction → NO_COVERAGE
3. getNextCode : Replaced bitwise AND with OR → NO_COVERAGE
4. getNextCode : Replaced bitwise OR with AND → NO_COVERAGE
            nextData = (nextData << 8) | (data[bytePointer++] & 0xff);
227 1 1. getNextCode : Replaced integer addition with subtraction → NO_COVERAGE
            nextBits += 8;
228
            
229 2 1. getNextCode : changed conditional boundary → NO_COVERAGE
2. getNextCode : negated conditional → NO_COVERAGE
            if (nextBits < bitsToGet) {
230 4 1. getNextCode : Replaced Shift Left with Shift Right → NO_COVERAGE
2. getNextCode : Replaced integer addition with subtraction → NO_COVERAGE
3. getNextCode : Replaced bitwise AND with OR → NO_COVERAGE
4. getNextCode : Replaced bitwise OR with AND → NO_COVERAGE
                nextData = (nextData << 8) | (data[bytePointer++] & 0xff);
231 1 1. getNextCode : Replaced integer addition with subtraction → NO_COVERAGE
                nextBits += 8;
232
            }
233
            
234 4 1. getNextCode : Replaced integer subtraction with addition → NO_COVERAGE
2. getNextCode : Replaced Shift Right with Shift Left → NO_COVERAGE
3. getNextCode : Replaced integer subtraction with addition → NO_COVERAGE
4. getNextCode : Replaced bitwise AND with OR → NO_COVERAGE
            int code =
235
            (nextData >> (nextBits - bitsToGet)) & andTable[bitsToGet-9];
236 1 1. getNextCode : Replaced integer subtraction with addition → NO_COVERAGE
            nextBits -= bitsToGet;
237
            
238 1 1. getNextCode : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
            return code;
239
        } catch(ArrayIndexOutOfBoundsException e) {
240
            // Strip not terminated as expected: return EndOfInformation code.
241 1 1. getNextCode : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
            return 257;
242
        }
243
    }
244
}

Mutations

85

1.1
Location : decode
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : decode
Killed by : none
negated conditional → NO_COVERAGE

89

1.1
Location : decode
Killed by : none
removed call to com/lowagie/text/pdf/LZWDecoder::initializeStringTable → NO_COVERAGE

104

1.1
Location : decode
Killed by : none
negated conditional → NO_COVERAGE

106

1.1
Location : decode
Killed by : none
negated conditional → NO_COVERAGE

108

1.1
Location : decode
Killed by : none
removed call to com/lowagie/text/pdf/LZWDecoder::initializeStringTable → NO_COVERAGE

111

1.1
Location : decode
Killed by : none
negated conditional → NO_COVERAGE

115

1.1
Location : decode
Killed by : none
removed call to com/lowagie/text/pdf/LZWDecoder::writeString → NO_COVERAGE

120

1.1
Location : decode
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : decode
Killed by : none
negated conditional → NO_COVERAGE

124

1.1
Location : decode
Killed by : none
removed call to com/lowagie/text/pdf/LZWDecoder::writeString → NO_COVERAGE

125

1.1
Location : decode
Killed by : none
removed call to com/lowagie/text/pdf/LZWDecoder::addStringToTable → NO_COVERAGE

132

1.1
Location : decode
Killed by : none
removed call to com/lowagie/text/pdf/LZWDecoder::writeString → NO_COVERAGE

133

1.1
Location : decode
Killed by : none
removed call to com/lowagie/text/pdf/LZWDecoder::addStringToTable → NO_COVERAGE

148

1.1
Location : initializeStringTable
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : initializeStringTable
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

3.3
Location : initializeStringTable
Killed by : none
negated conditional → NO_COVERAGE

162

1.1
Location : writeString
Killed by : none
removed call to java/io/OutputStream::write → NO_COVERAGE

174

1.1
Location : addStringToTable
Killed by : none
Replaced integer addition with subtraction → NO_COVERAGE

175

1.1
Location : addStringToTable
Killed by : none
removed call to java/lang/System::arraycopy → NO_COVERAGE

179

1.1
Location : addStringToTable
Killed by : none
Replaced integer addition with subtraction → NO_COVERAGE

181

1.1
Location : addStringToTable
Killed by : none
negated conditional → NO_COVERAGE

183

1.1
Location : addStringToTable
Killed by : none
negated conditional → NO_COVERAGE

185

1.1
Location : addStringToTable
Killed by : none
negated conditional → NO_COVERAGE

196

1.1
Location : addStringToTable
Killed by : none
Replaced integer addition with subtraction → NO_COVERAGE

198

1.1
Location : addStringToTable
Killed by : none
negated conditional → NO_COVERAGE

200

1.1
Location : addStringToTable
Killed by : none
negated conditional → NO_COVERAGE

202

1.1
Location : addStringToTable
Killed by : none
negated conditional → NO_COVERAGE

212

1.1
Location : composeString
Killed by : none
Replaced integer addition with subtraction → NO_COVERAGE

213

1.1
Location : composeString
Killed by : none
removed call to java/lang/System::arraycopy → NO_COVERAGE

216

1.1
Location : composeString
Killed by : none
mutated return of Object value for com/lowagie/text/pdf/LZWDecoder::composeString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

226

1.1
Location : getNextCode
Killed by : none
Replaced Shift Left with Shift Right → NO_COVERAGE

2.2
Location : getNextCode
Killed by : none
Replaced integer addition with subtraction → NO_COVERAGE

3.3
Location : getNextCode
Killed by : none
Replaced bitwise AND with OR → NO_COVERAGE

4.4
Location : getNextCode
Killed by : none
Replaced bitwise OR with AND → NO_COVERAGE

227

1.1
Location : getNextCode
Killed by : none
Replaced integer addition with subtraction → NO_COVERAGE

229

1.1
Location : getNextCode
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : getNextCode
Killed by : none
negated conditional → NO_COVERAGE

230

1.1
Location : getNextCode
Killed by : none
Replaced Shift Left with Shift Right → NO_COVERAGE

2.2
Location : getNextCode
Killed by : none
Replaced integer addition with subtraction → NO_COVERAGE

3.3
Location : getNextCode
Killed by : none
Replaced bitwise AND with OR → NO_COVERAGE

4.4
Location : getNextCode
Killed by : none
Replaced bitwise OR with AND → NO_COVERAGE

231

1.1
Location : getNextCode
Killed by : none
Replaced integer addition with subtraction → NO_COVERAGE

234

1.1
Location : getNextCode
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

2.2
Location : getNextCode
Killed by : none
Replaced Shift Right with Shift Left → NO_COVERAGE

3.3
Location : getNextCode
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

4.4
Location : getNextCode
Killed by : none
Replaced bitwise AND with OR → NO_COVERAGE

236

1.1
Location : getNextCode
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

238

1.1
Location : getNextCode
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

241

1.1
Location : getNextCode
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.4.2