Program: 33381
Project: 33381
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<?> jpegInfoOffset=ifd.removeField(TiffTag.JPEG_INTERCHANGE_FORMAT.getValue());
  if (jpegInfoOffset != null) {
    copyJPEGInfoOffset(rin,rout,jpegInfoOffset.getDataAsLong()[0],offset);
    jpegInfoOffset=new LongField(TiffTag.JPEG_INTERCHANGE_FORMAT.getValue(),new int[]{offset});
    ifd.addField(jpegInfoOffset);
  }
  TiffField<?> jpegTable=ifd.removeField(TiffTag.JPEG_DC_TABLES.getValue());
  if (jpegTable != null) {
    ifd.addField(copyJPEGHufTable(rin,rout,jpegTable,(int)rout.getStreamPointer()));
  }
  jpegTable=ifd.removeField(TiffTag.JPEG_AC_TABLES.getValue());
  if (jpegTable != null) {
    ifd.addField(copyJPEGHufTable(rin,rout,jpegTable,(int)rout.getStreamPointer()));
  }
  jpegTable=ifd.removeField(TiffTag.JPEG_Q_TABLES.getValue());
  if (jpegTable != null) {
    ifd.addField(copyJPEGQTable(rin,rout,jpegTable,(int)rout.getStreamPointer()));
  }
  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.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<?> jpegInfoOffset=ifd.removeField(TiffTag.JPEG_INTERCHANGE_FORMAT.getValue());
  if (jpegInfoOffset != null) {
    copyJPEGInfoOffset(rin,rout,jpegInfoOffset.getDataAsLong()[0],offset);
    jpegInfoOffset=new LongField(TiffTag.JPEG_INTERCHANGE_FORMAT.getValue(),new int[]{offset});
    ifd.addField(jpegInfoOffset);
  }
  TiffField<?> jpegTable=ifd.removeField(TiffTag.JPEG_DC_TABLES.getValue());
  if (jpegTable != null) {
    ifd.addField(copyJPEGHufTable(rin,rout,jpegTable,(int)rout.getStreamPointer()));
  }
  jpegTable=ifd.removeField(TiffTag.JPEG_AC_TABLES.getValue());
  if (jpegTable != null) {
    ifd.addField(copyJPEGHufTable(rin,rout,jpegTable,(int)rout.getStreamPointer()));
  }
  jpegTable=ifd.removeField(TiffTag.JPEG_Q_TABLES.getValue());
  if (jpegTable != null) {
    ifd.addField(copyJPEGQTable(rin,rout,jpegTable,(int)rout.getStreamPointer()));
  }
  return (int)rout.getStreamPointer();
}