1 /*
   2  * Copyright (c) 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.ByteArrayInputStream;
  25 
  26 import javax.sound.sampled.AudioFileFormat;
  27 import javax.sound.sampled.AudioFormat;
  28 import javax.sound.sampled.AudioInputStream;
  29 import javax.sound.sampled.AudioSystem;
  30 
  31 /**
  32  * @test
  33  * @bug 8132782
  34  */
  35 public final class RecognizeHugeWaveExtFiles {
  36 
  37     /**
  38      * The maximum size in bytes per WAVE specification.
  39      */
  40     private static final /*unsigned int */ long MAX_UNSIGNED_INT = 0xffffffffL;
  41 
  42     /**
  43      * The supported wave ext format and sample size in bits.
  44      */
  45     private static final int[][] waveTypeBits = {
  46             {0xFFFE/*WAVE_FORMAT_EXTENSIBLE*/, 8}
  47     };
  48 
  49     /**
  50      * The list of supported sample rates(stored as unsigned int).
  51      */
  52     private static final int[] sampleRates = {
  53             8000, 11025, 16000, 22050, 32000, 37800, 44056, 44100, 47250, 48000,
  54             50000, 50400, 88200, 96000, 176400, 192000, 352800, 2822400,
  55             5644800, Integer.MAX_VALUE
  56     };
  57 
  58     /**
  59      * The list of supported channels (stored as unsigned int).
  60      */
  61     private static final int[] channels = {
  62             1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  63     };
  64 
  65     /**
  66      * The list of supported size of data (stored as unsigned int).
  67      * <p>
  68      * The {@code MAX_UNSIGNED_INT} is a maximum size.
  69      */
  70     private static final long[] dataSizes = {
  71             0, 1, 2, 3, Integer.MAX_VALUE - 1, Integer.MAX_VALUE,
  72             (long) Integer.MAX_VALUE + 1, MAX_UNSIGNED_INT - 1, MAX_UNSIGNED_INT
  73     };
  74 
  75     public static void main(final String[] args) throws Exception {
  76         for (final int[] type : waveTypeBits) {
  77             for (final int sampleRate : sampleRates) {
  78                 for (final int channel : channels) {
  79                     for (final long dataSize : dataSizes) {
  80                         testAFF(type, sampleRate, channel, dataSize);
  81                         testAIS(type, sampleRate, channel, dataSize);
  82                     }
  83                 }
  84             }
  85         }
  86     }
  87 
  88     /**
  89      * Tests the {@code AudioFileFormat} fetched from the fake header.
  90      * <p>
  91      * Note that the frameLength and byteLength are stored as int which means
  92      * that {@code AudioFileFormat} will store the data above {@code MAX_INT} as
  93      * NOT_SPECIFIED.
  94      */
  95     private static void testAFF(final int[] type, final int rate,
  96                                 final int channel, final long size)
  97             throws Exception {
  98         final byte[] header = createHeader(type, rate, channel, size);
  99         final ByteArrayInputStream fake = new ByteArrayInputStream(header);
 100         final AudioFileFormat aff = AudioSystem.getAudioFileFormat(fake);
 101         final AudioFormat format = aff.getFormat();
 102 
 103         if (aff.getType() != AudioFileFormat.Type.WAVE) {
 104             throw new RuntimeException("Error");
 105         }
 106 
 107         final long frameLength = size / format.getFrameSize();
 108         if (frameLength <= Integer.MAX_VALUE) {
 109             if (aff.getFrameLength() != frameLength) {
 110                 System.err.println("Expected: " + frameLength);
 111                 System.err.println("Actual: " + aff.getFrameLength());
 112                 throw new RuntimeException();
 113             }
 114         } else {
 115             if (aff.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
 116                 System.err.println("Expected: " + AudioSystem.NOT_SPECIFIED);
 117                 System.err.println("Actual: " + aff.getFrameLength());
 118                 throw new RuntimeException();
 119             }
 120         }
 121         validateFormat(type[1], rate, channel, aff.getFormat());
 122     }
 123 
 124     /**
 125      * Tests the {@code AudioInputStream} fetched from the fake header.
 126      * <p>
 127      * Note that the frameLength is stored as long which means that {@code
 128      * AudioInputStream} must store all possible data from wave file.
 129      */
 130     private static void testAIS(final int[] type, final int rate,
 131                                 final int channel, final long size)
 132             throws Exception {
 133         final byte[] header = createHeader(type, rate, channel, size);
 134         final ByteArrayInputStream fake = new ByteArrayInputStream(header);
 135         final AudioInputStream ais = AudioSystem.getAudioInputStream(fake);
 136         final AudioFormat format = ais.getFormat();
 137         final long frameLength = size / format.getFrameSize();
 138         if (frameLength != ais.getFrameLength()) {
 139             System.err.println("Expected: " + frameLength);
 140             System.err.println("Actual: " + ais.getFrameLength());
 141             throw new RuntimeException();
 142         }
 143         if (ais.available() < 0) {
 144             System.err.println("available should be >=0: " + ais.available());
 145             throw new RuntimeException();
 146         }
 147 
 148         validateFormat(type[1], rate, channel, format);
 149     }
 150 
 151     /**
 152      * Tests that format contains the same data as were provided to the fake
 153      * stream.
 154      */
 155     private static void validateFormat(final int bits, final int rate,
 156                                        final int channel,
 157                                        final AudioFormat format) {
 158 
 159         if (Float.compare(format.getSampleRate(), rate) != 0) {
 160             System.err.println("Expected: " + rate);
 161             System.err.println("Actual: " + format.getSampleRate());
 162             throw new RuntimeException();
 163         }
 164         if (format.getChannels() != channel) {
 165             System.err.println("Expected: " + channel);
 166             System.err.println("Actual: " + format.getChannels());
 167             throw new RuntimeException();
 168         }
 169         int frameSize = ((bits + 7) / 8) * channel;
 170         if (format.getFrameSize() != frameSize) {
 171             System.err.println("Expected: " + frameSize);
 172             System.err.println("Actual: " + format.getFrameSize());
 173             throw new RuntimeException();
 174         }
 175     }
 176 
 177     /**
 178      * Creates the custom header of the WAVE file. It is expected that all
 179      * passed data are supported.
 180      */
 181     private static byte[] createHeader(final int[] type, final int rate,
 182                                        final int channel, final long size) {
 183         final int frameSize = ((type[1] + 7) / 8) * channel;
 184         return new byte[]{
 185                 // RIFF_MAGIC
 186                 0x52, 0x49, 0x46, 0x46,
 187                 // fileLength
 188                 -1, -1, -1, -1,
 189                 //  waveMagic
 190                 0x57, 0x41, 0x56, 0x45,
 191                 // FMT_MAGIC
 192                 0x66, 0x6d, 0x74, 0x20,
 193                 // size
 194                 40, 0, 0, 0,
 195                 // wav_type  WAVE_FORMAT_EXTENSIBLE
 196                 (byte) (type[0]), (byte) (type[0] >> 8),
 197                 // channels
 198                 (byte) (channel), (byte) (channel >> 8),
 199                 // samplerate
 200                 (byte) (rate), (byte) (rate >> 8), (byte) (rate >> 16),
 201                 (byte) (rate >> 24),
 202                 // framerate
 203                 1, 0, 0, 0,
 204                 // framesize
 205                 (byte) (frameSize), (byte) (frameSize >> 8),
 206                 // bits
 207                 (byte) type[1], 0,
 208                 // cbsize
 209                 22, 0,
 210                 // validBitsPerSample
 211                 8, 0,
 212                 // channelMask
 213                 0, 0, 0, 0,
 214                 // SUBTYPE_IEEE_FLOAT
 215                 // i1
 216                 0x3, 0x0, 0x0, 0x0,
 217                 //s1
 218                 0x0, 0x0,
 219                 //s2
 220                 0x10, 0,
 221                 //x1
 222                 (byte) 0x80,
 223                 //x2
 224                 0x0,
 225                 //x3
 226                 0x0,
 227                 //x4
 228                 (byte) 0xaa,
 229                 //x5
 230                 0x0,
 231                 //x6
 232                 0x38,
 233                 //x7
 234                 (byte) 0x9b,
 235                 //x8
 236                 0x71,
 237                 // DATA_MAGIC
 238                 0x64, 0x61, 0x74, 0x61,
 239                 // data size
 240                 (byte) (size), (byte) (size >> 8), (byte) (size >> 16),
 241                 (byte) (size >> 24)
 242                 // data
 243                 , 0, 0, 0, 0, 0
 244         };
 245     }
 246 }