1 /* 2 * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package com.sun.imageio.plugins.tiff; 26 27 import java.io.IOException; 28 import javax.imageio.IIOException; 29 import javax.imageio.plugins.tiff.BaselineTIFFTagSet; 30 31 class TIFFLZWDecompressor extends TIFFDecompressor { 32 33 private static final int CLEAR_CODE = 256; 34 private static final int EOI_CODE = 257; 35 private static final int FIRST_CODE = 258; 36 37 private static final int andTable[] = { 38 511, 39 1023, 40 2047, 41 4095 42 }; 43 44 private int predictor; 45 46 // whether to reverse the bits in each byte of the input data, i.e., 47 // convert right-to-left fill order (lsb) to left-to-right (msb). 48 private boolean flipBits; 49 50 private byte[] srcData; 51 private byte[] dstData; 52 53 private int srcIndex; 54 private int dstIndex; 55 56 private byte stringTable[][]; 57 private int tableIndex, bitsToGet = 9; 58 59 private int nextData = 0; 60 private int nextBits = 0; 61 62 public TIFFLZWDecompressor(int predictor, int fillOrder) 63 throws IIOException { 64 super(); 65 66 if (predictor != BaselineTIFFTagSet.PREDICTOR_NONE && 67 predictor != 68 BaselineTIFFTagSet.PREDICTOR_HORIZONTAL_DIFFERENCING) { 69 throw new IIOException("Illegal value for Predictor in " + 70 "TIFF file"); 71 } 72 73 this.predictor = predictor; 74 75 flipBits = fillOrder == BaselineTIFFTagSet.FILL_ORDER_RIGHT_TO_LEFT; 76 } 198 } 199 200 /** 201 * Initialize the string table. 202 */ 203 public void initializeStringTable() { 204 stringTable = new byte[4096][]; 205 206 for (int i = 0; i < CLEAR_CODE; i++) { 207 stringTable[i] = new byte[1]; 208 stringTable[i][0] = (byte)i; 209 } 210 211 tableIndex = FIRST_CODE; 212 bitsToGet = 9; 213 } 214 215 /** 216 * Write out the string just uncompressed. 217 */ 218 public void writeString(byte string[]) { 219 if(dstIndex < dstData.length) { 220 int maxIndex = Math.min(string.length, 221 dstData.length - dstIndex); 222 223 for (int i=0; i < maxIndex; i++) { 224 dstData[dstIndex++] = string[i]; 225 } 226 } 227 } 228 229 /** 230 * Add a new string to the string table. 231 */ 232 public void addStringToTable(byte oldString[], byte newString) { 233 int length = oldString.length; 234 byte string[] = new byte[length + 1]; 235 System.arraycopy(oldString, 0, string, 0, length); 236 string[length] = newString; 237 238 // Add this new String to the table 239 stringTable[tableIndex++] = string; 240 241 if (tableIndex == 511) { 242 bitsToGet = 10; 243 } else if (tableIndex == 1023) { 244 bitsToGet = 11; 245 } else if (tableIndex == 2047) { 246 bitsToGet = 12; 247 } 248 } 249 250 /** 251 * Add a new string to the string table. 252 */ 253 public void addStringToTable(byte string[]) { 254 // Add this new String to the table 255 stringTable[tableIndex++] = string; 256 257 if (tableIndex == 511) { 258 bitsToGet = 10; 259 } else if (tableIndex == 1023) { 260 bitsToGet = 11; 261 } else if (tableIndex == 2047) { 262 bitsToGet = 12; 263 } 264 } 265 266 /** 267 * Append {@code newString} to the end of {@code oldString}. 268 */ 269 public byte[] composeString(byte oldString[], byte newString) { 270 int length = oldString.length; 271 byte string[] = new byte[length + 1]; 272 System.arraycopy(oldString, 0, string, 0, length); 273 string[length] = newString; 274 275 return string; 276 } 277 278 // Returns the next 9, 10, 11 or 12 bits 279 public int getNextCode() { 280 // Attempt to get the next code. The exception is caught to make 281 // this robust to cases wherein the EndOfInformation code has been 282 // omitted from a strip. Examples of such cases have been observed 283 // in practice. 284 285 try { 286 nextData = (nextData << 8) | (srcData[srcIndex++] & 0xff); 287 nextBits += 8; 288 289 if (nextBits < bitsToGet) { 290 nextData = (nextData << 8) | (srcData[srcIndex++] & 0xff); 291 nextBits += 8; | 1 /* 2 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package com.sun.imageio.plugins.tiff; 26 27 import java.io.IOException; 28 import javax.imageio.IIOException; 29 import javax.imageio.plugins.tiff.BaselineTIFFTagSet; 30 31 class TIFFLZWDecompressor extends TIFFDecompressor { 32 33 private static final int CLEAR_CODE = 256; 34 private static final int EOI_CODE = 257; 35 private static final int FIRST_CODE = 258; 36 37 private static final int[] andTable = { 38 511, 39 1023, 40 2047, 41 4095 42 }; 43 44 private int predictor; 45 46 // whether to reverse the bits in each byte of the input data, i.e., 47 // convert right-to-left fill order (lsb) to left-to-right (msb). 48 private boolean flipBits; 49 50 private byte[] srcData; 51 private byte[] dstData; 52 53 private int srcIndex; 54 private int dstIndex; 55 56 private byte[][] stringTable; 57 private int tableIndex, bitsToGet = 9; 58 59 private int nextData = 0; 60 private int nextBits = 0; 61 62 public TIFFLZWDecompressor(int predictor, int fillOrder) 63 throws IIOException { 64 super(); 65 66 if (predictor != BaselineTIFFTagSet.PREDICTOR_NONE && 67 predictor != 68 BaselineTIFFTagSet.PREDICTOR_HORIZONTAL_DIFFERENCING) { 69 throw new IIOException("Illegal value for Predictor in " + 70 "TIFF file"); 71 } 72 73 this.predictor = predictor; 74 75 flipBits = fillOrder == BaselineTIFFTagSet.FILL_ORDER_RIGHT_TO_LEFT; 76 } 198 } 199 200 /** 201 * Initialize the string table. 202 */ 203 public void initializeStringTable() { 204 stringTable = new byte[4096][]; 205 206 for (int i = 0; i < CLEAR_CODE; i++) { 207 stringTable[i] = new byte[1]; 208 stringTable[i][0] = (byte)i; 209 } 210 211 tableIndex = FIRST_CODE; 212 bitsToGet = 9; 213 } 214 215 /** 216 * Write out the string just uncompressed. 217 */ 218 public void writeString(byte[] string) { 219 if(dstIndex < dstData.length) { 220 int maxIndex = Math.min(string.length, 221 dstData.length - dstIndex); 222 223 for (int i=0; i < maxIndex; i++) { 224 dstData[dstIndex++] = string[i]; 225 } 226 } 227 } 228 229 /** 230 * Add a new string to the string table. 231 */ 232 public void addStringToTable(byte[] oldString, byte newString) { 233 int length = oldString.length; 234 byte[] string = new byte[length + 1]; 235 System.arraycopy(oldString, 0, string, 0, length); 236 string[length] = newString; 237 238 // Add this new String to the table 239 stringTable[tableIndex++] = string; 240 241 if (tableIndex == 511) { 242 bitsToGet = 10; 243 } else if (tableIndex == 1023) { 244 bitsToGet = 11; 245 } else if (tableIndex == 2047) { 246 bitsToGet = 12; 247 } 248 } 249 250 /** 251 * Add a new string to the string table. 252 */ 253 public void addStringToTable(byte[] string) { 254 // Add this new String to the table 255 stringTable[tableIndex++] = string; 256 257 if (tableIndex == 511) { 258 bitsToGet = 10; 259 } else if (tableIndex == 1023) { 260 bitsToGet = 11; 261 } else if (tableIndex == 2047) { 262 bitsToGet = 12; 263 } 264 } 265 266 /** 267 * Append {@code newString} to the end of {@code oldString}. 268 */ 269 public byte[] composeString(byte[] oldString, byte newString) { 270 int length = oldString.length; 271 byte[] string = new byte[length + 1]; 272 System.arraycopy(oldString, 0, string, 0, length); 273 string[length] = newString; 274 275 return string; 276 } 277 278 // Returns the next 9, 10, 11 or 12 bits 279 public int getNextCode() { 280 // Attempt to get the next code. The exception is caught to make 281 // this robust to cases wherein the EndOfInformation code has been 282 // omitted from a strip. Examples of such cases have been observed 283 // in practice. 284 285 try { 286 nextData = (nextData << 8) | (srcData[srcIndex++] & 0xff); 287 nextBits += 8; 288 289 if (nextBits < bitsToGet) { 290 nextData = (nextData << 8) | (srcData[srcIndex++] & 0xff); 291 nextBits += 8; |