Program: 4595
Project: 4595
Buggy source:
/** 
 * An MPEG audio file is built up from smaller parts called frames, which are generally independent items. Each frame has its own header and audio data that follows. There is NO MPEG file header; therefore, you can cut any part of MPEG file and play it correctly (cut on frame boundaries!), excluding MPEG 1 Layer III frames which are often dependent on another. To read info about an MPEG file, you can find the first frame, read its header and assume that the other frames are the same. Exceptions to this are VBR (variable bit rate) and ABR (average bit rate) files. The frame header is constituted by the very first four bytes (32bits) in a frame. The first 11 bits are always set on(1) and they're called "frame sync". Frame CRC is optional and 16 bits long; it follows the frame header. After the CRC comes the audio data. ::EXAMPLE:: MP3 file header format (4 byte length or 32 bits) byte[4] = { -1, -5, 80, 108 }  -1 << 24  +  -5 << 16  +  80 << 08  +  108 << 0    {HdrCRC} 11111111     11101010     00110000     11000000     {0000} AAAAAAAA     AAABBCCD     EEEEFFGH     IIJJKLMM     {ZZZZ} Label, Position(bits), Description	  A (31-21) Frame sync  All bits set (1) B (20,19) MPEG Audio version ID 00 - MPEG Ver 2.5, 01 - reserved, 10 - Ver 2, 11 - Ver 1 Note: MPEG Ver 2.5 is not official; bit # 20 indicates 2.5 C (18,17) Layer description 00 - reserved, 01 - Layer III, 10 - Layer II, 11 - Layer I D    (16) Protection bit 0 - None, 1 - Protected by CRC (16bit crc follows header) E (15,12) Bitrate index, version and layer bits V1,L1 V1,L2 V1,L3 V2,L1 V2, L2 & L3 F (11,10)  G     (9) Padding bit    0 - frame not padded, 1 - frame padded with one extra slot Note: Padding is used to fit the bit rates exactly. H     (8) Private bit  0 - not private, 1 - private Note: May be freely used for other needs of an application. I   (7,6) Channel Mode 00 - Stereo, 01 - Joint stereo, 10 - Dual (Stereo), 11 - Mono J   (5,4) Mode extension (Only if Joint stereo) Used to join data; bits dynamically generated by an encoder. K     (3) Copyright 0 - Audio is not copyrighted, 1 - Audio is marked copyrighted L     (2) Original 0 - Copy of original media, 1 - Original media  M   (1,0) Emphasis 00 - none, 01 - 50/15 ms, 10 - reserved, 11 - CCIT J.17 Z (32-35) CRC  !!OPTIONAL!! Note: NOT part of header, just appended on end when needed We read in bytes from the beginning of the mp3 file looking for the 4 byte header; we can't assume it starts at byte 0 because ID3 tags may be prepended before the first valid header. The loop below strolls through buffered chunks of the file looking for the header. As an optimization, we check the first 10 bytes initially as it may contain the header; if it doesn't we then check the first 10 bytes for an ID3v2 header and fetch the tag's length, skipping those bytes leading us directly to the header. If neither are found, it's a brute force search. With each chunk, we step forward one byte at a time, and test the current byte plus the next 3 bytes for a valid mp3 header.
 * @exception java.io.IOException mp3 fileName had no valid header
 */
public MP3Info(String file) throws IOException {
  _file=file;
  int i=0;
  int pos=0;
  int adjustedEOB=0;
  int c=0;
  FileInputStream fis=null;
  byte[] buf=new byte[2048];
  try {
    fis=new FileInputStream(_file);
    c=fis.read(buf,0,buf.length);
    if (c < -1)     throw new IOException("String_Node_Str");
    if ((buf[0] == 'i' || buf[0] == 'I') && (buf[1] == 'd' || buf[1] == 'D') && (buf[2] == '3')) {
      i=buf[6] << 7 | buf[7] << 7 | buf[8] << 7 | buf[9];
      if (i > 0) {
        i+=10;
      }
 else       if (i < 0) {
        i=0;
      }
    }
    endheadersearch:     do {
      if (pos < buf.length - 3) {
        adjustedEOB=c - 3;
      }
 else {
        i=0;
        adjustedEOB=c;
      }
      for (; i < adjustedEOB; i++) {
        if (buf[i] != -1 || (buf[i + 1] & 255) < 224) {
          continue;
        }
        _header=(ByteOrder.ubyte2int(buf[i + 3])) | (ByteOrder.ubyte2int(buf[i + 2]) << 8) | (ByteOrder.ubyte2int(buf[i + 1]) << 16)| (ByteOrder.ubyte2int(buf[i]) << 24);
        if ((getVersionIndex() != 1) && (getLayerIndex() != 0) && (getBitrateIndex() != 0)&& (getBitrateIndex() != 15)&& (getFrequencyIndex() != 3)&& (getEmphasisIndex() != 2)) {
          pos+=i;
          break endheadersearch;
        }
      }
      if (adjustedEOB != -1) {
        buf[0]=buf[c - 3];
        buf[1]=buf[c - 2];
        buf[2]=buf[c - 1];
      }
      pos+=c - 3;
      c=fis.read(buf,3,buf.length - 3);
      if (c < 6)       throw new IOException("String_Node_Str");
    }
 while (c != -1 && pos < 100000);
    if (c == -1 || pos >= 100000) {
      _header=0;
      throw new IOException("String_Node_Str");
    }
    int need=buf.length - i - 3- 120- 36;
    if (need < 0) {
      need=-need;
      i-=need;
      int j=0;
      for (; need < buf.length; j++, need++) {
        buf[j]=buf[need];
      }
      c=fis.read(buf,j,buf.length - j);
    }
    if (getVersionIndex() == 3) {
      i+=(getModeIndex() == 3 ? 21 : 36);
    }
 else {
      i+=(getModeIndex() == 3 ? 23 : 21);
    }
switch (buf[i + 0]) {
case 88:
      if (((buf[i + 1] == 'i' || buf[i + 1] == 'I') && (buf[i + 2] == 'n' || buf[i + 2] == 'N') && (buf[i + 3] == 'g' || buf[i + 3] == 'G')))       loadXingHeader(buf,i + 4);
    break;
case 86:
  if ((buf[i + 1] == 'B' && buf[i + 2] == 'R' && buf[i + 3] == 'I'))   loadFhgHeader(buf,i + 4);
break;
}
}
  finally {
try {
if (fis != null) fis.close();
}
 catch (IOException e) {
}
}
}

Fixed version (ground truth):
/** 
 * An MPEG audio file is built up from smaller parts called frames, which are generally independent items. Each frame has its own header and audio data that follows. There is NO MPEG file header; therefore, you can cut any part of MPEG file and play it correctly (cut on frame boundaries!), excluding MPEG 1 Layer III frames which are often dependent on another. To read info about an MPEG file, you can find the first frame, read its header and assume that the other frames are the same. Exceptions to this are VBR (variable bit rate) and ABR (average bit rate) files. The frame header is constituted by the very first four bytes (32bits) in a frame. The first 11 bits are always set on(1) and they're called "frame sync". Frame CRC is optional and 16 bits long; it follows the frame header. After the CRC comes the audio data. ::EXAMPLE:: MP3 file header format (4 byte length or 32 bits) byte[4] = { -1, -5, 80, 108 }  -1 << 24  +  -5 << 16  +  80 << 08  +  108 << 0    {HdrCRC} 11111111     11101010     00110000     11000000     {0000} AAAAAAAA     AAABBCCD     EEEEFFGH     IIJJKLMM     {ZZZZ} Label, Position(bits), Description	  A (31-21) Frame sync  All bits set (1) B (20,19) MPEG Audio version ID 00 - MPEG Ver 2.5, 01 - reserved, 10 - Ver 2, 11 - Ver 1 Note: MPEG Ver 2.5 is not official; bit # 20 indicates 2.5 C (18,17) Layer description 00 - reserved, 01 - Layer III, 10 - Layer II, 11 - Layer I D    (16) Protection bit 0 - None, 1 - Protected by CRC (16bit crc follows header) E (15,12) Bitrate index, version and layer bits V1,L1 V1,L2 V1,L3 V2,L1 V2, L2 & L3 F (11,10)  G     (9) Padding bit    0 - frame not padded, 1 - frame padded with one extra slot Note: Padding is used to fit the bit rates exactly. H     (8) Private bit  0 - not private, 1 - private Note: May be freely used for other needs of an application. I   (7,6) Channel Mode 00 - Stereo, 01 - Joint stereo, 10 - Dual (Stereo), 11 - Mono J   (5,4) Mode extension (Only if Joint stereo) Used to join data; bits dynamically generated by an encoder. K     (3) Copyright 0 - Audio is not copyrighted, 1 - Audio is marked copyrighted L     (2) Original 0 - Copy of original media, 1 - Original media  M   (1,0) Emphasis 00 - none, 01 - 50/15 ms, 10 - reserved, 11 - CCIT J.17 Z (32-35) CRC  !!OPTIONAL!! Note: NOT part of header, just appended on end when needed We read in bytes from the beginning of the mp3 file looking for the 4 byte header; we can't assume it starts at byte 0 because ID3 tags may be prepended before the first valid header. The loop below strolls through buffered chunks of the file looking for the header. As an optimization, we check the first 10 bytes initially as it may contain the header; if it doesn't we then check the first 10 bytes for an ID3v2 header and fetch the tag's length, skipping those bytes leading us directly to the header. If neither are found, it's a brute force search. With each chunk, we step forward one byte at a time, and test the current byte plus the next 3 bytes for a valid mp3 header.
 * @exception java.io.IOException mp3 fileName had no valid header
 */
public MP3Info(String file) throws IOException {
  _file=file;
  int i=0;
  int pos=0;
  int adjustedEOB=0;
  int c=0;
  FileInputStream fis=null;
  byte[] buf=new byte[2048];
  try {
    fis=new FileInputStream(_file);
    c=fis.read(buf,0,buf.length);
    if (c < 4)     throw new IOException("String_Node_Str");
    if ((buf[0] == 'i' || buf[0] == 'I') && (buf[1] == 'd' || buf[1] == 'D') && (buf[2] == '3')) {
      i=buf[6] << 7 | buf[7] << 7 | buf[8] << 7 | buf[9];
      if (i > 0) {
        i+=10;
      }
 else       if (i < 0) {
        i=0;
      }
    }
    endheadersearch:     do {
      if (pos < buf.length - 3) {
        adjustedEOB=c - 3;
      }
 else {
        i=0;
        adjustedEOB=c;
      }
      for (; i < adjustedEOB; i++) {
        if (buf[i] != -1 || (buf[i + 1] & 255) < 224) {
          continue;
        }
        _header=(ByteOrder.ubyte2int(buf[i + 3])) | (ByteOrder.ubyte2int(buf[i + 2]) << 8) | (ByteOrder.ubyte2int(buf[i + 1]) << 16)| (ByteOrder.ubyte2int(buf[i]) << 24);
        if ((getVersionIndex() != 1) && (getLayerIndex() != 0) && (getBitrateIndex() != 0)&& (getBitrateIndex() != 15)&& (getFrequencyIndex() != 3)&& (getEmphasisIndex() != 2)) {
          pos+=i;
          break endheadersearch;
        }
      }
      if (adjustedEOB != -1) {
        buf[0]=buf[c - 3];
        buf[1]=buf[c - 2];
        buf[2]=buf[c - 1];
      }
      pos+=c - 3;
      c=fis.read(buf,3,buf.length - 3);
      if (c < 6)       throw new IOException("String_Node_Str");
    }
 while (c != -1 && pos < 100000);
    if (c == -1 || pos >= 100000) {
      _header=0;
      throw new IOException("String_Node_Str");
    }
    int need=buf.length - i - 3- 120- 36;
    if (need < 0) {
      need=-need;
      i-=need;
      int j=0;
      for (; need < buf.length; j++, need++) {
        buf[j]=buf[need];
      }
      c=fis.read(buf,j,buf.length - j);
    }
    if (getVersionIndex() == 3) {
      i+=(getModeIndex() == 3 ? 21 : 36);
    }
 else {
      i+=(getModeIndex() == 3 ? 23 : 21);
    }
switch (buf[i + 0]) {
case 88:
      if (((buf[i + 1] == 'i' || buf[i + 1] == 'I') && (buf[i + 2] == 'n' || buf[i + 2] == 'N') && (buf[i + 3] == 'g' || buf[i + 3] == 'G')))       loadXingHeader(buf,i + 4);
    break;
case 86:
  if ((buf[i + 1] == 'B' && buf[i + 2] == 'R' && buf[i + 3] == 'I'))   loadFhgHeader(buf,i + 4);
break;
}
}
  finally {
try {
if (fis != null) fis.close();
}
 catch (IOException e) {
}
}
}