2004-11-25 23:20:45 +01:00
/ *
2008-04-07 20:36:09 +02:00
* Copyright ( c ) 2002 - 2008 LWJGL Project
2004-06-12 22:28:34 +02:00
* All rights reserved .
2004-11-25 23:20:45 +01:00
*
2004-02-04 01:17:13 +01:00
* Redistribution and use in source and binary forms , with or without
2004-11-25 23:20:45 +01:00
* modification , are permitted provided that the following conditions are
2004-06-12 22:28:34 +02:00
* met :
2004-11-25 23:20:45 +01:00
*
* * Redistributions of source code must retain the above copyright
2004-06-12 22:28:34 +02:00
* notice , this list of conditions and the following disclaimer .
*
* * Redistributions in binary form must reproduce the above copyright
* notice , this list of conditions and the following disclaimer in the
* documentation and / or other materials provided with the distribution .
*
2004-11-25 23:20:45 +01:00
* * Neither the name of ' LWJGL ' nor the names of
* its contributors may be used to endorse or promote products derived
2004-06-12 22:28:34 +02:00
* from this software without specific prior written permission .
2004-11-25 23:20:45 +01:00
*
2004-06-12 22:28:34 +02:00
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* " AS IS " AND ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED
* TO , THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2004-11-25 23:20:45 +01:00
* PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT , INDIRECT , INCIDENTAL , SPECIAL ,
* EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT NOT LIMITED TO ,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE , DATA , OR
2004-06-12 22:28:34 +02:00
* PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY THEORY OF
2004-11-25 23:20:45 +01:00
* LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT ( INCLUDING
2004-06-12 22:28:34 +02:00
* NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
2004-02-04 01:17:13 +01:00
* /
2004-07-29 13:20:43 +02:00
package org.lwjgl ;
2004-07-26 15:36:04 +02:00
2010-09-30 19:21:50 +02:00
import java.nio.* ;
2004-04-03 23:08:23 +02:00
2004-02-04 01:17:13 +01:00
/ * *
2005-02-07 19:48:22 +01:00
* < p > A class to check buffer boundaries in general . If there is unsufficient space
2004-02-15 20:41:51 +01:00
* in the buffer when the call is made then a buffer overflow would otherwise
* occur and cause unexpected behaviour , a crash , or worse , a security risk .
2007-04-11 19:30:13 +02:00
*
* Internal class , don ' t use .
2005-02-07 19:48:22 +01:00
* < / p >
2004-02-04 01:17:13 +01:00
* @author cix_foo < cix_foo @users.sourceforge.net >
2004-07-29 13:20:43 +02:00
* @author elias_naur < elias_naur @users.sourceforge.net >
2004-02-04 01:17:13 +01:00
* @version $Revision$
2006-05-25 15:03:35 +02:00
* $Id$
2004-02-04 01:17:13 +01:00
* /
2004-07-29 13:20:43 +02:00
public class BufferChecks {
2004-02-04 01:17:13 +01:00
/** Static methods only! */
2004-02-15 20:41:51 +01:00
private BufferChecks ( ) {
2004-02-04 01:17:13 +01:00
}
2004-02-18 23:31:00 +01:00
2005-02-16 17:04:29 +01:00
/ * *
* Helper methods to ensure a function pointer is not - null ( 0 )
* /
public static void checkFunctionAddress ( long pointer ) {
2010-04-23 01:21:48 +02:00
if ( LWJGLUtil . CHECKS & & pointer = = 0 ) {
2005-02-17 13:48:06 +01:00
throw new IllegalStateException ( " Function is not supported " ) ;
2005-02-16 17:04:29 +01:00
}
}
2005-02-09 14:44:52 +01:00
/ * *
* Helper methods to ensure a ByteBuffer is null - terminated
* /
public static void checkNullTerminated ( ByteBuffer buf ) {
2010-04-23 01:21:48 +02:00
if ( LWJGLUtil . CHECKS & & buf . get ( buf . limit ( ) - 1 ) ! = 0 ) {
2005-02-09 14:44:52 +01:00
throw new IllegalArgumentException ( " Missing null termination " ) ;
}
}
2009-12-04 05:49:19 +01:00
public static void checkNullTerminated ( ByteBuffer buf , int count ) {
2010-04-23 01:21:48 +02:00
if ( LWJGLUtil . CHECKS ) {
int nullFound = 0 ;
for ( int i = buf . position ( ) ; i < buf . limit ( ) ; i + + ) {
if ( buf . get ( i ) = = 0 )
nullFound + + ;
}
if ( nullFound < count )
throw new IllegalArgumentException ( " Missing null termination " ) ;
2009-12-04 05:49:19 +01:00
}
}
2010-09-27 01:43:24 +02:00
/** Helper method to ensure an IntBuffer is null-terminated */
2010-03-11 22:06:49 +01:00
public static void checkNullTerminated ( IntBuffer buf ) {
2010-09-30 19:21:50 +02:00
if ( LWJGLUtil . CHECKS & & buf . get ( buf . limit ( ) - 1 ) ! = 0 ) {
2010-03-11 22:06:49 +01:00
throw new IllegalArgumentException ( " Missing null termination " ) ;
}
}
2010-09-27 01:43:24 +02:00
/** Helper method to ensure a LongBuffer is null-terminated */
public static void checkNullTerminated ( LongBuffer buf ) {
2010-09-30 19:21:50 +02:00
if ( LWJGLUtil . CHECKS & & buf . get ( buf . limit ( ) - 1 ) ! = 0 ) {
2010-09-27 01:43:24 +02:00
throw new IllegalArgumentException ( " Missing null termination " ) ;
}
}
/** Helper method to ensure a PointerBuffer is null-terminated */
public static void checkNullTerminated ( PointerBuffer buf ) {
2010-09-30 19:21:50 +02:00
if ( LWJGLUtil . CHECKS & & buf . get ( buf . limit ( ) - 1 ) ! = 0 ) {
2010-09-27 01:43:24 +02:00
throw new IllegalArgumentException ( " Missing null termination " ) ;
}
}
2005-02-15 12:05:36 +01:00
public static void checkNotNull ( Object o ) {
2010-04-23 01:21:48 +02:00
if ( LWJGLUtil . CHECKS & & o = = null )
2005-02-15 12:05:36 +01:00
throw new IllegalArgumentException ( " Null argument " ) ;
}
2006-05-25 15:03:35 +02:00
2004-07-26 15:36:04 +02:00
/ * *
* Helper methods to ensure a buffer is direct ( and , implicitly , non - null ) .
* /
2004-07-29 13:20:43 +02:00
public static void checkDirect ( ByteBuffer buf ) {
2010-04-23 01:21:48 +02:00
if ( LWJGLUtil . CHECKS & & ! buf . isDirect ( ) ) {
2004-07-26 15:36:04 +02:00
throw new IllegalArgumentException ( " ByteBuffer is not direct " ) ;
}
}
2004-11-25 23:20:45 +01:00
2004-07-29 13:20:43 +02:00
public static void checkDirect ( ShortBuffer buf ) {
2010-04-23 01:21:48 +02:00
if ( LWJGLUtil . CHECKS & & ! buf . isDirect ( ) ) {
2004-07-26 15:36:04 +02:00
throw new IllegalArgumentException ( " ShortBuffer is not direct " ) ;
}
}
2004-11-25 23:20:45 +01:00
2004-07-29 13:20:43 +02:00
public static void checkDirect ( IntBuffer buf ) {
2010-04-23 01:21:48 +02:00
if ( LWJGLUtil . CHECKS & & ! buf . isDirect ( ) ) {
2004-07-26 15:36:04 +02:00
throw new IllegalArgumentException ( " IntBuffer is not direct " ) ;
}
}
2004-11-25 23:20:45 +01:00
2006-05-25 15:03:35 +02:00
public static void checkDirect ( LongBuffer buf ) {
2010-04-23 01:21:48 +02:00
if ( LWJGLUtil . CHECKS & & ! buf . isDirect ( ) ) {
2006-05-25 15:03:35 +02:00
throw new IllegalArgumentException ( " LongBuffer is not direct " ) ;
}
}
public static void checkDirect ( FloatBuffer buf ) {
2010-04-23 01:21:48 +02:00
if ( LWJGLUtil . CHECKS & & ! buf . isDirect ( ) ) {
2006-05-25 15:03:35 +02:00
throw new IllegalArgumentException ( " FloatBuffer is not direct " ) ;
}
}
2004-07-29 13:20:43 +02:00
public static void checkDirect ( DoubleBuffer buf ) {
2010-04-23 01:21:48 +02:00
if ( LWJGLUtil . CHECKS & & ! buf . isDirect ( ) ) {
2006-05-25 15:03:35 +02:00
throw new IllegalArgumentException ( " DoubleBuffer is not direct " ) ;
2004-07-26 15:36:04 +02:00
}
}
2004-11-25 23:20:45 +01:00
2010-09-27 01:43:24 +02:00
public static void checkDirect ( PointerBuffer buf ) {
// NO-OP, PointerBuffer is always direct.
}
public static void checkArray ( Object [ ] array ) {
2010-09-28 23:11:35 +02:00
if ( LWJGLUtil . CHECKS & & ( array = = null | | array . length = = 0 ) )
2010-09-27 01:43:24 +02:00
throw new IllegalArgumentException ( " Invalid array " ) ;
}
2006-05-29 14:30:23 +02:00
/ * *
* This is a separate call to help inline checkBufferSize .
* /
private static void throwBufferSizeException ( Buffer buf , int size ) {
2011-05-04 21:24:57 +02:00
throw new IllegalArgumentException ( " Number of remaining buffer elements is " + buf . remaining ( ) + " , must be at least " + size + " . Because at most " + size + " elements can be returned, a buffer with at least " + size + " elements is required, regardless of actual returned element count " ) ;
2006-05-29 14:30:23 +02:00
}
2008-08-19 18:46:03 +02:00
2010-09-27 01:43:24 +02:00
private static void throwBufferSizeException ( PointerBuffer buf , int size ) {
throw new IllegalArgumentException ( " Number of remaining pointer buffer elements is " + buf . remaining ( ) + " , must be at least " + size ) ;
}
private static void throwArraySizeException ( Object [ ] array , int size ) {
throw new IllegalArgumentException ( " Number of array elements is " + array . length + " , must be at least " + size ) ;
}
private static void throwArraySizeException ( long [ ] array , int size ) {
throw new IllegalArgumentException ( " Number of array elements is " + array . length + " , must be at least " + size ) ;
}
2004-02-04 01:17:13 +01:00
/ * *
2004-02-15 20:41:51 +01:00
* Helper method to ensure a buffer is big enough to receive data from a
2004-02-18 23:31:00 +01:00
* glGet * operation .
2004-11-25 23:20:45 +01:00
*
2004-02-15 20:41:51 +01:00
* @param buf
* The buffer to check
2004-02-18 23:31:00 +01:00
* @param size
* The minimum buffer size
2004-11-25 23:20:45 +01:00
* @throws IllegalArgumentException
2004-02-04 01:17:13 +01:00
* /
2007-04-11 19:30:13 +02:00
public static void checkBufferSize ( Buffer buf , int size ) {
2010-04-23 01:21:48 +02:00
if ( LWJGLUtil . CHECKS & & buf . remaining ( ) < size ) {
2006-05-29 14:30:23 +02:00
throwBufferSizeException ( buf , size ) ;
2004-02-04 01:17:13 +01:00
}
}
2004-07-26 15:36:04 +02:00
2010-09-30 19:21:50 +02:00
/ * *
* Detects the buffer type and performs the corresponding check
* and also returns the buffer position in bytes .
*
* @param buffer the buffer to check
* @param size the size to check
*
* @return the buffer position in bytes
* /
public static int checkBuffer ( final Buffer buffer , final int size ) {
final int posShift ;
if ( buffer instanceof ByteBuffer ) {
BufferChecks . checkBuffer ( ( ByteBuffer ) buffer , size ) ;
posShift = 0 ;
} else if ( buffer instanceof ShortBuffer ) {
BufferChecks . checkBuffer ( ( ShortBuffer ) buffer , size ) ;
posShift = 1 ;
} else if ( buffer instanceof IntBuffer ) {
BufferChecks . checkBuffer ( ( IntBuffer ) buffer , size ) ;
posShift = 2 ;
} else if ( buffer instanceof LongBuffer ) {
BufferChecks . checkBuffer ( ( LongBuffer ) buffer , size ) ;
posShift = 4 ;
} else if ( buffer instanceof FloatBuffer ) {
BufferChecks . checkBuffer ( ( FloatBuffer ) buffer , size ) ;
posShift = 2 ;
} else if ( buffer instanceof DoubleBuffer ) {
BufferChecks . checkBuffer ( ( DoubleBuffer ) buffer , size ) ;
posShift = 4 ;
} else
throw new IllegalArgumentException ( " Unsupported Buffer type specified: " + buffer . getClass ( ) ) ;
return buffer . position ( ) < < posShift ;
}
2004-07-29 13:20:43 +02:00
public static void checkBuffer ( ByteBuffer buf , int size ) {
2010-04-23 01:21:48 +02:00
if ( LWJGLUtil . CHECKS ) {
checkBufferSize ( buf , size ) ;
checkDirect ( buf ) ;
}
2004-07-26 15:36:04 +02:00
}
2006-05-25 15:03:35 +02:00
public static void checkBuffer ( ShortBuffer buf , int size ) {
2010-04-23 01:21:48 +02:00
if ( LWJGLUtil . CHECKS ) {
checkBufferSize ( buf , size ) ;
checkDirect ( buf ) ;
}
2006-05-25 15:03:35 +02:00
}
2004-07-29 13:20:43 +02:00
public static void checkBuffer ( IntBuffer buf , int size ) {
2010-04-23 01:21:48 +02:00
if ( LWJGLUtil . CHECKS ) {
checkBufferSize ( buf , size ) ;
checkDirect ( buf ) ;
}
2004-07-26 15:36:04 +02:00
}
2006-05-25 15:03:35 +02:00
public static void checkBuffer ( LongBuffer buf , int size ) {
2010-04-23 01:21:48 +02:00
if ( LWJGLUtil . CHECKS ) {
checkBufferSize ( buf , size ) ;
checkDirect ( buf ) ;
}
2004-07-26 15:36:04 +02:00
}
2004-07-29 13:20:43 +02:00
public static void checkBuffer ( FloatBuffer buf , int size ) {
2010-04-23 01:21:48 +02:00
if ( LWJGLUtil . CHECKS ) {
checkBufferSize ( buf , size ) ;
checkDirect ( buf ) ;
}
2004-07-26 15:36:04 +02:00
}
2004-07-29 13:20:43 +02:00
public static void checkBuffer ( DoubleBuffer buf , int size ) {
2010-04-23 01:21:48 +02:00
if ( LWJGLUtil . CHECKS ) {
checkBufferSize ( buf , size ) ;
checkDirect ( buf ) ;
}
2004-07-26 15:36:04 +02:00
}
2010-09-27 01:43:24 +02:00
public static void checkBuffer ( PointerBuffer buf , int size ) {
if ( LWJGLUtil . CHECKS & & buf . remaining ( ) < size ) {
throwBufferSizeException ( buf , size ) ;
}
}
public static void checkArray ( Object [ ] array , int size ) {
if ( LWJGLUtil . CHECKS & & array . length < size )
throwArraySizeException ( array , size ) ;
}
public static void checkArray ( long [ ] array , int size ) {
if ( LWJGLUtil . CHECKS & & array . length < size )
throwArraySizeException ( array , size ) ;
}
}