Program: 33377
Project: 33377
Buggy source:
/** 
 * @param offset offset to write page image data
 * @return the position where to write the IFD for the current image page
 */
private static int copyPageData(IFD ifd,int offset,RandomAccessInputStream rin,RandomAccessOutputStream rout) throws IOException {
  TiffField<?> stripOffSets=ifd.removeField(TiffTag.STRIP_OFFSETS.getValue());
  if (stripOffSets == null)   stripOffSets=ifd.removeField(TiffTag.TILE_OFFSETS.getValue());
  TiffField<?> stripByteCounts=ifd.getField(TiffTag.STRIP_BYTE_COUNTS.getValue());
  if (stripByteCounts == null)   stripByteCounts=ifd.getField(TiffTag.TILE_BYTE_COUNTS.getValue());
  if (stripOffSets != null) {
    int[] counts=stripByteCounts.getDataAsLong();
    int[] off=stripOffSets.getDataAsLong();
    int[] temp=new int[off.length];
    TiffField<?> tiffField=ifd.getField(TiffTag.COMPRESSION.getValue());
    if (tiffField != null && tiffField.getDataAsLong()[0] == 1) {
      int planaryConfiguration=1;
      tiffField=ifd.getField(TiffTag.PLANAR_CONFIGURATTION.getValue());
      if (tiffField != null)       planaryConfiguration=tiffField.getDataAsLong()[0];
      tiffField=ifd.getField(TiffTag.SAMPLES_PER_PIXEL.getValue());
      int samplesPerPixel=1;
      if (tiffField != null)       samplesPerPixel=tiffField.getDataAsLong()[0];
      if ((planaryConfiguration == 1 && off.length == 1) || (planaryConfiguration == 2 && off.length == samplesPerPixel)) {
        int[] totalBytes2Read=getBytes2Read(ifd);
        for (int i=0; i < off.length; i++)         counts[i]=totalBytes2Read[i];
      }
    }
    rout.seek(offset);
    for (int i=0; i < off.length; i++) {
      rin.seek(off[i]);
      byte[] buf=new byte[counts[i]];
      rin.readFully(buf);
      rout.write(buf);
      temp[i]=offset;
      offset+=buf.length;
    }
    if (ifd.getField(TiffTag.STRIP_BYTE_COUNTS.getValue()) != null)     stripOffSets=new LongField(TiffTag.STRIP_OFFSETS.getValue(),temp);
 else     stripOffSets=new LongField(TiffTag.TILE_OFFSETS.getValue(),temp);
    ifd.addField(stripOffSets);
  }
  String copyRight="String_Node_Str";
  ifd.addField(new ASCIIField(TiffTag.COPYRIGHT.getValue(),copyRight));
  String softWare="String_Node_Str";
  ifd.addField(new ASCIIField(TiffTag.SOFTWARE.getValue(),softWare));
  TiffField<?> jpegIFOffset=ifd.removeField(TiffTag.JPEG_INTERCHANGE_FORMAT.getValue());
  if (jpegIFOffset != null) {
    TiffField<?> jpegIFByteCount=ifd.removeField(TiffTag.JPEG_INTERCHANGE_FORMAT_LENGTH.getValue());
    try {
      if (jpegIFByteCount != null) {
        rin.seek(jpegIFOffset.getDataAsLong()[0]);
        byte[] bytes2Read=new byte[jpegIFByteCount.getDataAsLong()[0]];
        rin.readFully(bytes2Read);
        rout.seek(offset);
        rout.write(bytes2Read);
        ifd.addField(jpegIFByteCount);
      }
 else {
        long startOffset=rout.getStreamPointer();
        copyJPEGIFByteCount(rin,rout,jpegIFOffset.getDataAsLong()[0],offset);
        long endOffset=rout.getStreamPointer();
        ifd.addField(new LongField(TiffTag.JPEG_INTERCHANGE_FORMAT_LENGTH.getValue(),new int[]{(int)(endOffset - startOffset)}));
      }
      jpegIFOffset=new LongField(TiffTag.JPEG_INTERCHANGE_FORMAT.getValue(),new int[]{offset});
      ifd.addField(jpegIFOffset);
    }
 catch (    EOFException ex) {
      ;
    }
    ;
  }
  TiffField<?> jpegTable=ifd.removeField(TiffTag.JPEG_DC_TABLES.getValue());
  if (jpegTable != null) {
    try {
      ifd.addField(copyJPEGHufTable(rin,rout,jpegTable,(int)rout.getStreamPointer()));
    }
 catch (    EOFException ex) {
      ;
    }
  }
  jpegTable=ifd.removeField(TiffTag.JPEG_AC_TABLES.getValue());
  if (jpegTable != null) {
    try {
      ifd.addField(copyJPEGHufTable(rin,rout,jpegTable,(int)rout.getStreamPointer()));
    }
 catch (    EOFException ex) {
      ;
    }
  }
  jpegTable=ifd.removeField(TiffTag.JPEG_Q_TABLES.getValue());
  if (jpegTable != null) {
    try {
      ifd.addField(copyJPEGQTable(rin,rout,jpegTable,(int)rout.getStreamPointer()));
    }
 catch (    EOFException ex) {
      ;
    }
  }
  return (int)rout.getStreamPointer();
}

Fixed version (ground truth):
/** 
 * @param offset offset to write page image data
 * @return the position where to write the IFD for the current image page
 */
private static int copyPageData(IFD ifd,int offset,RandomAccessInputStream rin,RandomAccessOutputStream rout) throws IOException {
  TiffField<?> stripOffSets=ifd.removeField(TiffTag.STRIP_OFFSETS.getValue());
  if (stripOffSets == null)   stripOffSets=ifd.removeField(TiffTag.TILE_OFFSETS.getValue());
  TiffField<?> stripByteCounts=ifd.getField(TiffTag.STRIP_BYTE_COUNTS.getValue());
  if (stripByteCounts == null)   stripByteCounts=ifd.getField(TiffTag.TILE_BYTE_COUNTS.getValue());
  if (stripOffSets != null) {
    int[] counts=stripByteCounts.getDataAsLong();
    int[] off=stripOffSets.getDataAsLong();
    int[] temp=new int[off.length];
    TiffField<?> tiffField=ifd.getField(TiffTag.COMPRESSION.getValue());
    if ((tiffField == null) || (tiffField != null && tiffField.getDataAsLong()[0] == 1)) {
      int planaryConfiguration=1;
      tiffField=ifd.getField(TiffTag.PLANAR_CONFIGURATTION.getValue());
      if (tiffField != null)       planaryConfiguration=tiffField.getDataAsLong()[0];
      tiffField=ifd.getField(TiffTag.SAMPLES_PER_PIXEL.getValue());
      int samplesPerPixel=1;
      if (tiffField != null)       samplesPerPixel=tiffField.getDataAsLong()[0];
      if ((planaryConfiguration == 1 && off.length == 1) || (planaryConfiguration == 2 && off.length == samplesPerPixel)) {
        int[] totalBytes2Read=getBytes2Read(ifd);
        for (int i=0; i < off.length; i++)         counts[i]=totalBytes2Read[i];
      }
    }
    rout.seek(offset);
    for (int i=0; i < off.length; i++) {
      rin.seek(off[i]);
      byte[] buf=new byte[counts[i]];
      rin.readFully(buf);
      rout.write(buf);
      temp[i]=offset;
      offset+=buf.length;
    }
    if (ifd.getField(TiffTag.STRIP_BYTE_COUNTS.getValue()) != null)     stripOffSets=new LongField(TiffTag.STRIP_OFFSETS.getValue(),temp);
 else     stripOffSets=new LongField(TiffTag.TILE_OFFSETS.getValue(),temp);
    ifd.addField(stripOffSets);
  }
  String copyRight="String_Node_Str";
  ifd.addField(new ASCIIField(TiffTag.COPYRIGHT.getValue(),copyRight));
  String softWare="String_Node_Str";
  ifd.addField(new ASCIIField(TiffTag.SOFTWARE.getValue(),softWare));
  TiffField<?> jpegIFOffset=ifd.removeField(TiffTag.JPEG_INTERCHANGE_FORMAT.getValue());
  if (jpegIFOffset != null) {
    TiffField<?> jpegIFByteCount=ifd.removeField(TiffTag.JPEG_INTERCHANGE_FORMAT_LENGTH.getValue());
    try {
      if (jpegIFByteCount != null) {
        rin.seek(jpegIFOffset.getDataAsLong()[0]);
        byte[] bytes2Read=new byte[jpegIFByteCount.getDataAsLong()[0]];
        rin.readFully(bytes2Read);
        rout.seek(offset);
        rout.write(bytes2Read);
        ifd.addField(jpegIFByteCount);
      }
 else {
        long startOffset=rout.getStreamPointer();
        copyJPEGIFByteCount(rin,rout,jpegIFOffset.getDataAsLong()[0],offset);
        long endOffset=rout.getStreamPointer();
        ifd.addField(new LongField(TiffTag.JPEG_INTERCHANGE_FORMAT_LENGTH.getValue(),new int[]{(int)(endOffset - startOffset)}));
      }
      jpegIFOffset=new LongField(TiffTag.JPEG_INTERCHANGE_FORMAT.getValue(),new int[]{offset});
      ifd.addField(jpegIFOffset);
    }
 catch (    EOFException ex) {
      ;
    }
    ;
  }
  TiffField<?> jpegTable=ifd.removeField(TiffTag.JPEG_DC_TABLES.getValue());
  if (jpegTable != null) {
    try {
      ifd.addField(copyJPEGHufTable(rin,rout,jpegTable,(int)rout.getStreamPointer()));
    }
 catch (    EOFException ex) {
      ;
    }
  }
  jpegTable=ifd.removeField(TiffTag.JPEG_AC_TABLES.getValue());
  if (jpegTable != null) {
    try {
      ifd.addField(copyJPEGHufTable(rin,rout,jpegTable,(int)rout.getStreamPointer()));
    }
 catch (    EOFException ex) {
      ;
    }
  }
  jpegTable=ifd.removeField(TiffTag.JPEG_Q_TABLES.getValue());
  if (jpegTable != null) {
    try {
      ifd.addField(copyJPEGQTable(rin,rout,jpegTable,(int)rout.getStreamPointer()));
    }
 catch (    EOFException ex) {
      ;
    }
  }
  return (int)rout.getStreamPointer();
}