|
Message
From: cvs at opencores.org<cvs@o...>
Date: Wed Jan 24 20:36:48 CET 2007
Subject: [cvs-checkins] MODIFIED: jop ...
Date: 00/07/01 24:20:36 Added: jop/java/target/src/common/ejip/jtcpip/util Bitmap.java Debug.java NumFunctions.java StringFunctions.java Util.java Log: move from ejip2 to ejip Revision Changes Path 1.1 jop/java/target/src/common/ejip/jtcpip/util/Bitmap.java http://www.opencores.org/cvsweb.shtml/jop/java/target/src/common/ejip/jtcpip/util/Bitmap.java?rev=1.1&content-type=text/x-cvsweb-markup Index: Bitmap.java =================================================================== /* * Copyright (c) 2006-2007 Graz University of Technology. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. 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. * * 3. The names "Graz University of Technology" and "IAIK of Graz University of * Technology" must not be used to endorse or promote products derived from * this software without prior written permission. * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR 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 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package ejip.jtcpip.util; /** * Class to handle Bitmaps * * @author Ulrich Feichter * @author Tobias Kellner * @author Christof Rath * @version $Rev: 939 $ $Date: 2007/01/24 19:36:48 $ */ public class Bitmap { /** Size of the bitmap in bits */ int size; /** Bit pool */ byte[] bitmap; /** * Initalizes the bitmap. * * @param size * Desired number of bits in the bitmap */ public Bitmap(int size) { this.size = size; bitmap = new byte[NumFunctions.divRoundUp(size, 8)]; // if the size doesn't fall onto a byte boundary add another byte } /** * Clears the complete bitmap. */ public void clearBitmap() { for (int i = 0; i < bitmap.length; i++) bitmap[i] = 0; } /** * Sets a single bit in the bitmap. * * @param pos * Position of the bit to set */ public void setBit(int pos) { if (pos < size) bitmap[pos / 8] |= (byte) (1 << (pos % 8)); } /** * Clears a single bit in the bitmap *
* @param pos
*/
public void clearBit(int pos)
{
if (pos < size)
bitmap[pos / 8] &= ~(byte) (1 << (pos % 8));
}
/**
* Sets multiple bits. Set <code>count</code> bits, starting at
* <code>pos</code>.
*
* @param pos
* Position of the first bit to set
* @param count
* Number of bits to set
*/
public void setBits(int pos, int count)
{
if (pos >= size)
return;
if ((pos + count) >= size)
count = (size - pos);
if (pos % 8 > 0) // pos doesn'f fall on a byte boundary
{
// => handle the first bits bit per bit
int i;
for (i = pos; i < (pos + Math.min(8 - (pos % 8), count)); i++)
setBit(i);
count -= (i - pos);
pos = i;
}
if (count == 0)
return;
if (count % 8 > 0) // the last bits do not fall on a byte boundary
{
// => handle the last bits bit per bit
for (int i = (pos + count - (count % 8)); i < (pos + count); i++)
setBit(i);
count -= (pos + count) % 8;
}
if (count == 0)
return;
for (int i = (pos / 8); i < (pos / 8) + (count / 8); i++)
// handle the rest (whole bytes in the middle)
bitmap[i] = (byte) 0xFF;
}
/**
* Clears multiple bits. Clear <code>count</code> bits, starting at
* <code>pos</code>.
*
* @param pos
* Position of the first bit to clear
* @param count
* Number of bits to clear
*/
public void clearBits(int pos, int count)
{
if (pos >= size)
return;
if ((pos + count) >= size)
count = (size - pos);
if (pos % 8 > 0) // pos doesn'f fall on a byte boundary
{
// => handle the first bits bit per bit
int i;
for (i = pos; i < (pos + Math.min(8 - (pos % 8), count)); i++)
clearBit(i);
count -= (i - pos);
pos = i;
}
if (count == 0)
return;
if (count % 8 > 0) // the last bits do not fall on a byte boundary
{
// => handle the last bits bit per bit
for (int i = (pos + count - (count % 8)); i < (pos + count); i++)
clearBit(i);
count -= (pos + count) % 8;
}
if (count == 0)
return;
for (int i = (pos / 8); i < (pos / 8) + (count / 8); i++)
// handle the rest (whole bytes in the middle)
bitmap[i] = 0;
}
/**
* Check whether a bit at a given position is set.
*
* @param pos
* Position of the bit to check
* @return Whether the bit is set
*/
public boolean isSet(int pos)
{
if (pos >= size)
return false;
int i = (byte) (1 << (pos % 8)) & bitmap[pos / 8];
return i != 0;
}
/**
* Tests if all bits of the bitmap are set.
*
* @return True if all bits are set
*/
public boolean allSet()
{
if (size % 8 > 0) // the last byte is not filled by the bitmap
{
// Set all bits that exceed the bitmap to 1
int i = 0xFF << (size % 8);
bitmap[bitmap.length - 1] |= i;
}
for (int i = 0; i < bitmap.length; i++)
if (bitmap[i] != (byte) 0xFF)
return false;
return true;
}
/**
* Tests if all bits of the bitmap until a certain position are set.
*
* @param until
* Number of bits to check from the start
* @return True if all bits until <code>until</code> are set
*/
public boolean allSet(int until)
{
if (until >= size)
return allSet();
for (int i = 0; i < until / 8; i++)
if (bitmap[i] != (byte) 0xFF)
return false;
if (until % 8 > 0) // the last byte is not filled by the bitmap
{
// Set all bits that ecxeed the bitmap to 1
byte b = bitmap[until / 8];
int m = 0xFF << (until % 8);
b |= m;
if (b != (byte) 0xFF)
return false;
}
return true;
}
/**
* Returns a string that shows every bit in the given byte.
*
* @param b
* The byte
* @return string representation of a byte
*/
public String byteToBitString(byte b)
{
String res = "|";
for (byte j = 0; j < 8; j++)
res += ((b >> j) & (byte) 1) == 1 ? "1|" : "0|";
return res;
}
/**
* Prints the content of the bitmap to {@link System#out}.
*/
public void print()
{
System.out.println("+---------------+");
for (int i = 0; i < bitmap.length; i++)
if ((i == bitmap.length - 1) && (size % 8 > 0))
{
char[] s = byteToBitString(bitmap[i]).toCharArray();
for (byte b = (byte) (size % 8); b < 8; b++)
s[b * 2 + 1] = 'x';
System.out.println(s);
}
else
System.out.println(byteToBitString(bitmap[i]));
System.out.println("+---------------+");
}
}
1.1 jop/java/target/src/common/ejip/jtcpip/util/Debug.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/common/ejip/jtcpip/util/Debug.java?rev=1.1&content-type=text/x-cvsweb-markup
Index: Debug.java
===================================================================
/*
* Copyright (c) 2006-2007 Graz University of Technology. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. The names "Graz University of Technology" and "IAIK of Graz University of
* Technology" must not be used to endorse or promote products derived from
* this software without prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR 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 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package ejip.jtcpip.util;
//import java.io.BufferedReader;
/**
* Some useful static methods for debugging.
*
* @author Ulrich Feichter
* @author Tobias Kellner
* @author Christof Rath
* @version $Rev: 976 $ $Date: 2007/01/24 19:36:48 $
*/
public class Debug
{
/** */
private Debug()
{
};
/** Debugging enabled? */
final static public boolean enabled = true;
/** Debug flag: ethernet */
final static public short DBG_ETH = 0x01;
/** Debug flag: IP */
final static public short DBG_IP = 0x02;
/** Debug flag: TCP */
final static public short DBG_TCP = 0x04;
/** Debug flag: UDP */
final static public short DBG_UDP = 0x08;
/** Debug flag: ICMP */
final static public short DBG_ICMP = 0x10;
/** Debug flag: HTTP */
final static public short DBG_HTTP = 0x20;
/** Debug flag: Other */
final static public short DBG_OTHER = 0x80;
/** Show all debug messages */
final static private short DBG_ALL = 0xFF;
/** Show no debug messages */
final static private short DBG_NONE = 0x00;
/**
* Enable debugging for certain parts. Possible values:
* <ul>
* <li>00000001 0x01 Ethernet (driver)
* <li>00000010 0x02 IP
* <li>00000100 0x04 TCP
* <li>00001000 0x08 UDP
* <li>00010000 0x10 ICMP
* <li>00100000 0x20 HTTP
* <li>01000000 0x40
* <li>10000000 0x80 Other
* <li>11111111 0xFF All
* <li>00000000 0x00 None
* </ul>
*
* Should be set during runtime! Therefore dbgFlagsToDisplay
* is public and writeable...
*/
static public short dbgFlagsToDisplay = DBG_ALL;
/** Is the next message the first debug message on the line? */
static private boolean firstMsg = true;
/**
* Convert a byte[] array to readable string format. This makes the "hex"
* readable!
*
* @return result String buffer in String format
* @param in
* byte[] buffer to convert to string format
*/
static public String byteArrayToHexString(byte in[])
{
byte ch = 0x00;
int i = 0;
if (in == null || in.length <= 0)
return null;
String pseudo[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D",
"E", "F" };
StringBuffer out = new StringBuffer(in.length * 2);
while (i < in.length)
{
ch = (byte) (in[i] & 0xF0); // Strip off high nibble
ch = (byte) (ch >>> 4); // shift the bits down
ch = (byte) (ch & 0x0F); // must do this is high order bit is on!
out.append(pseudo[(int) ch]); // convert the nibble to a String
// Character
ch = (byte) (in[i] & 0x0F); // Strip off low nibble
out.append(pseudo[(int) ch]); // convert the nibble to a String
// Character
out.append(" "); // add a space between bytes
i++;
}
String rslt = new String(out);
return rslt;
}
/**
* Convert an int[] array to readable string format. This makes the "hex"
* readable!
*
* @return result String buffer in String format
* @param in
* int[] buffer to convert to string format
* @param length
*/
static public String intArrayToHexString(int[] in, int length)
{
String out = new String();
if (length == 0)
length = in.length;
for (int i = 0; i < length; i++)
out += intToHexString(in[i]) + " ";
return out;
}
/**
* Convert a integer to readable string format. This makes the "hex"
* readable!
*
* @return result String buffer in String format
* @param in
* int to convert to string format
*/
static public String intToHexString(int in)
{
byte[] b_array = new byte[4];
for (int i = 0; i < 4; i++)
b_array[3 - i] = (byte) ((in >> (i * 8)) & 0xFF);
return byteArrayToHexString(b_array);
}
/**
* Print a debug message if the corresponding debug flag is set. Appends a
* newline.
*
* @param msg
* The message
* @param flag
* The debug flag
*/
static public void println(String msg, short flag)
{
if(flag == Debug.DBG_TCP)
System.out.println(msg);
// if ((dbgFlagsToDisplay & flag) != 0)
// System.out.println(firstMsg ? prependDebugFlag(msg, flag) : msg);
// firstMsg = true;
}
/**
* Print a debug message if the corresponding debug flag is set.
*
* @param msg
* The message
* @param flag
* The debug flag
*/
static public void print(String msg, short flag)
{
if(flag == Debug.DBG_TCP)
System.out.print(msg);
// if ((dbgFlagsToDisplay & flag) != 0)
// System.out.print(firstMsg ? prependDebugFlag(msg, flag) : msg);
// firstMsg = false;
}
/**
* Prefixes the given message with a String containing the given debug flag.
*
* @param msg
* The message
* @param flag
* The debug flag
* @return The string with the prefix
*/
static private String prependDebugFlag(String msg, short flag)
{
switch (flag)
{
case DBG_ETH:
return "Eth: " + msg;
case DBG_IP:
return "IP: " + msg;
case DBG_OTHER:
return "Dbg: " + msg;
case DBG_TCP:
return "TCP: " + msg;
case DBG_UDP:
return "UDP: " + msg;
case DBG_ICMP:
return "ICMP:" + msg;
case DBG_HTTP:
return "HTTP:" + msg;
default:
return "???: " + msg;
}
}
/**
* If the flags are set right it will be invoked. The method waits for a
* user input and returns it as String
*
* @param flag
* The debug flag
* @return The user input or null
*/
/*
* static public String readln(short flag) { if ((DBG & flag) != 0) {
* BufferedReader br = new BufferedReader(new
* InputStreamReader(java.lang.System.in)); String cmd = null; try { cmd =
* br.readLine(); } catch (IOException ioe) { System.out.println("IO error
* trying to read your command!"); } return cmd; } return null; }
*/
}
1.1 jop/java/target/src/common/ejip/jtcpip/util/NumFunctions.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/common/ejip/jtcpip/util/NumFunctions.java?rev=1.1&content-type=text/x-cvsweb-markup
Index: NumFunctions.java
===================================================================
/*
* Copyright (c) 2006-2007 Graz University of Technology. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. The names "Graz University of Technology" and "IAIK of Graz University of
* Technology" must not be used to endorse or promote products derived from
* this software without prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR 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 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package ejip.jtcpip.util;
import java.util.Random;
/**
* This class contains some util functions.
*
* @author Ulrich Feichter
* @author Tobias Kellner
* @author Christof Rath
* @version $Rev: 930 $ $Date: 2007/01/24 19:36:48 $
*/
public class NumFunctions
{
/** random number generator */
public static Random rand;
public static void init(){
rand = new Random();
}
/**
* A unsigned test if a greater b.
*
* @param a
* @param b
* @return 1 if a > b, 0 if a = b and -1 if a < b
*/
public static byte unsignedGt(byte a, byte b)
{
return relGt(a, b, (byte) 0);
}
/**
* A unsigned test if a greater b relative to a third number. Example: If
* zeroPt = -10 then -10 is the smallest number and -11 the greatest or if
* zeroPt = 0 then 0 is the smallest number and -128 is the greatest
* Returns:
* <ul>
* <li>1 if a > b
* <li>0 if a = b
* <li>-1 if a < b
* </ul>
*
* @param a
* @param b
* @param zeroPt
* @return 1 if a > b, 0 if a = b and -1 if a < b
*/
public static byte relGt(byte a, byte b, byte zeroPt)
{
if (a >= zeroPt)
if (b >= zeroPt)
return (byte) (a > b ? 1 : (a < b ? -1 : 0));
else
return -1;
else if (b < zeroPt)
return (byte) (a > b ? 1 : (a < b ? -1 : 0));
else
return 1;
}
/**
* A unsigned test if a greater b.
*
* @param a
* @param b
* @return 1 if a > b, 0 if a = b and -1 if a < b
*/
public static byte unsignedGt(short a, short b)
{
return relGt(a, b, (short) 0);
}
/**
* A unsigned test if a greater b relative to a third number. Example: If
* zeroPt = -10 then -10 is the smallest number and -11 the greatest or if
* zeroPt = 0 then 0 is the smallest number and -128 is the greatest
*
* @param a
* @param b
* @param zeroPt
* @return 1 if a > b, 0 if a = b and -1 if a < b
*/
public static byte relGt(short a, short b, short zeroPt)
{
if (a >= zeroPt)
if (b >= zeroPt)
return (byte) (a > b ? 1 : (a < b ? -1 : 0));
else
return -1;
else if (b < zeroPt)
return (byte) (a > b ? 1 : (a < b ? -1 : 0));
else
return 1;
}
/**
* A unsigned test if a greater b.
*
* @param a
* @param b
* @return 1 if a > b, 0 if a = b and -1 if a < b
*/
public static byte unsignedGt(int a, int b)
{
return relGt(a, b, 0);
}
/**
* A unsigned test if a greater b relative to a third number. Example: If
* zeroPt = -10 then -10 is the smallest number and -11 the greatest or if
* zeroPt = 0 then 0 is the smallest number and -128 is the greatest
*
* @param a
* @param b
* @param zeroPt
* @return 1 if a > b, 0 if a = b and -1 if a < b
*/
public static byte relGt(int a, int b, int zeroPt)
{
if (a >= zeroPt)
if (b >= zeroPt)
return (byte) (a > b ? 1 : (a < b ? -1 : 0));
else
return -1;
else if (b < zeroPt)
return (byte) (a > b ? 1 : (a < b ? -1 : 0));
else
return 1;
}
/**
* Return the rounded up integer division result of a / b.
*
* @param a
* dividend
* @param b
* divisor
* @return rounded up divison result
*/
public static int divRoundUp(int a, int b)
{
return a / b + (a % b != 0 ? 1 : 0);
}
/**
* Return the rounded up integer division result of a / b.
*
* @param a
* dividend
* @param b
* divisor
* @return rounded up divison result
*/
public static short divRoundUp(short a, short b)
{
return (short) (a / b + (a % b != 0 ? 1 : 0));
}
/**
* Return the rounded up integer division result of a / b.
*
* @param a
* dividend
* @param b
* divisor
* @return rounded up divison result
*/
public static byte divRoundUp(byte a, byte b)
{
return (byte) (a / b + (a % b != 0 ? 1 : 0));
}
/**
* Calculate the difference between two values.
* Considers overflow cases.
*
* @param biggerVal
* The bigger Value
* @param smallerVal
* The smaller Value
* @return the difference
*/
public static int calcDiffWithOverflow(int biggerVal, int smallerVal)
{
if (biggerVal < smallerVal)
return (Integer.MAX_VALUE - smallerVal) + (biggerVal - Integer.MIN_VALUE) + 1;
else
return biggerVal - smallerVal;
}
/**
* Tests if a value is between two other values. Considers overflow cases.
*
* @param smallerVal
* The smaller boundary value
* @param biggerVal
* The bigger boundare value
* @param testVal
* The value to test
* @return true if testVal is between smallerVal and biggerVal
*/
public static boolean isBetween(int smallerVal, int biggerVal, int testVal)
{
if (biggerVal < smallerVal)
return ((testVal > smallerVal) && (testVal <= Integer.MAX_VALUE))
|| ((testVal >= Integer.MIN_VALUE) && (testVal < biggerVal));
else
return (testVal > smallerVal) && (testVal < biggerVal);
}
/**
* Tests if a value is between two other values (or equal to the higher
* value). Considers overflow cases.
*
* @param smallerVal
* The smaller boundary value
* @param biggerVal
* The bigger boundare value
* @param testVal
* The value to test
* @return true if testVal is between smallerVal and biggerVal (or equal to
* biggerVal)
*/
public static boolean isBetweenOrEqualBigger(int smallerVal, int biggerVal, int testVal)
{
if(biggerVal == testVal)
return true;
if (biggerVal < smallerVal)
return ((testVal > smallerVal) && (testVal <= Integer.MAX_VALUE))
|| ((testVal >= Integer.MIN_VALUE) && (testVal <= biggerVal));
else
return (testVal > smallerVal) && (testVal <= biggerVal);
}
/**
* Tests if a value is between two other values (or equal to the lower
* value). Considers overflow cases.
*
* @param smallerVal
* The smaller boundary value
* @param biggerVal
* The bigger boundare value
* @param testVal
* The value to test
* @return true if testVal is between smallerVal and biggerVal (or equal to
* smallerVal)
*/
public static boolean isBetweenOrEqualSmaller(int smallerVal, int biggerVal, int testVal)
{
if(smallerVal == testVal)
return true;
if (biggerVal < smallerVal)
return ((testVal >= smallerVal) && (testVal <= Integer.MAX_VALUE))
|| ((testVal >= Integer.MIN_VALUE) && (testVal < biggerVal));
else
return (testVal >= smallerVal) && (testVal < biggerVal);
}
}
1.1 jop/java/target/src/common/ejip/jtcpip/util/StringFunctions.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/common/ejip/jtcpip/util/StringFunctions.java?rev=1.1&content-type=text/x-cvsweb-markup
Index: StringFunctions.java
===================================================================
/*
* Copyright (c) 2006-2007 Graz University of Technology. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. The names "Graz University of Technology" and "IAIK of Graz University of
* Technology" must not be used to endorse or promote products derived from
* this software without prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR 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 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package ejip.jtcpip.util;
import ejip.jtcpip.IP;
import ejip.jtcpip.JtcpipException;
/**
* Some locally useful string manipulation / parsing functions.
*
* @author Ulrich Feichter
* @author Tobias Kellner
* @author Christof Rath
* @version $Rev: 943 $ $Date: 2007/01/24 19:36:48 $
*/
public class StringFunctions
{
/** Invalid MAC address format */
// private static JtcpipException macException = new JtcpipException("Invalid MAC Address String");
/**
* Converts a string "00:00:00:00:00:00" into the corresponding int[]
*
* @param macAddr
* @param buffer
* @throws JtcpipException
*/
public static void macStrToByteArr(String macAddr, int[] buffer)
{
char[] c = macAddr.toCharArray();
int j = 0;
String s = "";
if (buffer == null)
buffer = new int[6];
for (int i = 0; i < c.length; i++)
{
if (c[i] != ':')
s += c[i];
if (c[i] == ':' || i == (c.length - 1))
{
buffer[j] = Integer.parseInt(s, 16);
if (buffer[j] < 0 || buffer[j] > 255)
if (Debug.enabled)
Debug.println("StringFunctions.macStrToByteArr():Index out of range",Debug.DBG_OTHER);
s = "";
j++;
if (j == buffer.length)
{
if (i < (c.length - 1))
if (Debug.enabled)
Debug.println("StringFunctions.macStrToByteArr():Index out of range",Debug.DBG_OTHER);
return;
}
}
}
if (j != 6)
if (Debug.enabled)
Debug.println("StringFunctions.macStrToByteArr():Index out of range",Debug.DBG_OTHER);
}
/**
* Retrieves the ip address value from a string.
*
* @param connectorString
* of the form //ip_addr:port[;option=value...]
* @return the ip address as integer
*/
public static int getAddrFromConnectorStr(String connectorString)
{
int offset = connectorString.indexOf('/');
if (offset >= 0)
{
if (connectorString.charAt(offset + 1) != '/')
throw new IllegalArgumentException("single '/' in connector string");
offset += 2;
}
else
offset = 0;
int colon = connectorString.indexOf(':', offset);
if (colon < 1)
throw new IllegalArgumentException("no ':' in connector string");
if (colon == 1)
return 0xFFFFFFFF; // 255.255.255.255
try
{
return IP.ipStringToInt(connectorString.substring(offset, colon));
} catch (JtcpipException e)
{
Debug.println("Wrong IP address in connector string (" + connectorString + ")",
Debug.DBG_OTHER);
return 0;
}
}
/**
* Retrieves the port value from a string.
*
* @param connectorString
* of the form //ip_addr:port[;option=value...]
* @return the port as int or -1 in case of a failure
*/
public static int getPortFromConnectorStr(String connectorString)
{
int offset = connectorString.indexOf('/');
if (offset >= 0)
{
if (connectorString.charAt(offset + 1) != '/')
throw new IllegalArgumentException("single '/' in connector string");
offset += 2;
}
else
offset = 0;
int colon = connectorString.indexOf(':', offset); //if a full connector string is given there is already a ':' just in front of '//'
int semic = connectorString.indexOf(';'); // in case there are
// options...
if (colon < 1)
return -1;
try
{
if (semic < 1)
semic = Integer.parseInt(connectorString.substring(colon + 1));
else
semic = Integer.parseInt(connectorString.substring(colon + 1, semic));
} catch (Exception e)
{
semic = -1;
}
if (semic < 0 || semic > 0xFFFF)
{
Debug.println(
"Port number out of bounds in connector string (" + connectorString + ")",
Debug.DBG_OTHER);
return -1;
}
else
return semic;
}
}
1.1 jop/java/target/src/common/ejip/jtcpip/util/Util.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/common/ejip/jtcpip/util/Util.java?rev=1.1&content-type=text/x-cvsweb-markup
Index: Util.java
===================================================================
/*
* Copyright (c) 2006-2007 Graz University of Technology. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. The names "Graz University of Technology" and "IAIK of Graz University of
* Technology" must not be used to endorse or promote products derived from
* this software without prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR 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 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package ejip.jtcpip.util;
import java.util.Random;
import ejip.jtcpip.IP;
import ejip.jtcpip.JtcpipException;
/**
* This class contains some util functions.
*
* @author Ulrich Feichter
* @author Tobias Kellner
* @author Christof Rath
* @version $Rev: 861 $ $Date: 2007/01/24 19:36:48 $
*/
public class Util {
/** random number generator */
public static Random rand;
/** Invalid MAC address format */
private static JtcpipException macException;
public static void init() {
rand = new Random();
macException = new JtcpipException("Invalid MAC Address String");
}
/**
* A unsigned test if a greater b.
*
* @param a
* @param b
* @return 1 if a > b, 0 if a = b and -1 if a < b
*/
public static byte unsignedGt(byte a, byte b) {
return relGt(a, b, (byte) 0);
}
/**
* A unsigned test if a greater b relative to a third number. Example: If
* zeroPt = -10 then -10 is the smallest number and -11 the greatest or if
* zeroPt = 0 then 0 is the smallest number and -128 is the greatest
* Returns:
* <ul>
* <li>1 if a > b
* <li>0 if a = b
* <li>-1 if a < b
* </ul>
*
* @param a
* @param b
* @param zeroPt
* @return 1 if a > b, 0 if a = b and -1 if a < b
*/
public static byte relGt(byte a, byte b, byte zeroPt) {
if (a >= zeroPt)
if (b >= zeroPt)
return (byte) (a > b ? 1 : (a < b ? -1 : 0));
else
return -1;
else if (b < zeroPt)
return (byte) (a > b ? 1 : (a < b ? -1 : 0));
else
return 1;
}
/**
* A unsigned test if a greater b.
*
* @param a
* @param b
* @return 1 if a > b, 0 if a = b and -1 if a < b
*/
public static byte unsignedGt(short a, short b) {
return relGt(a, b, (short) 0);
}
/**
* A unsigned test if a greater b relative to a third number. Example: If
* zeroPt = -10 then -10 is the smallest number and -11 the greatest or if
* zeroPt = 0 then 0 is the smallest number and -128 is the greatest
*
* @param a
* @param b
* @param zeroPt
* @return 1 if a > b, 0 if a = b and -1 if a < b
*/
public static byte relGt(short a, short b, short zeroPt) {
if (a >= zeroPt)
if (b >= zeroPt)
return (byte) (a > b ? 1 : (a < b ? -1 : 0));
else
return -1;
else if (b < zeroPt)
return (byte) (a > b ? 1 : (a < b ? -1 : 0));
else
return 1;
}
/**
* A unsigned test if a greater b.
*
* @param a
* @param b
* @return 1 if a > b, 0 if a = b and -1 if a < b
*/
public static byte unsignedGt(int a, int b) {
return relGt(a, b, 0);
}
/**
* A unsigned test if a greater b relative to a third number. Example: If
* zeroPt = -10 then -10 is the smallest number and -11 the greatest or if
* zeroPt = 0 then 0 is the smallest number and -128 is the greatest
*
* @param a
* @param b
* @param zeroPt
* @return 1 if a > b, 0 if a = b and -1 if a < b
*/
public static byte relGt(int a, int b, int zeroPt) {
if (a >= zeroPt)
if (b >= zeroPt)
return (byte) (a > b ? 1 : (a < b ? -1 : 0));
else
return -1;
else if (b < zeroPt)
return (byte) (a > b ? 1 : (a < b ? -1 : 0));
else
return 1;
}
/**
* Return the rounded up integer division result of a / b.
*
* @param a
* dividend
* @param b
* divisor
* @return rounded up divison result
*/
public static int divRoundUp(int a, int b) {
return a / b + (a % b != 0 ? 1 : 0);
}
/**
* Return the rounded up integer division result of a / b.
*
* @param a
* dividend
* @param b
* divisor
* @return rounded up divison result
*/
public static short divRoundUp(short a, short b) {
return (short) (a / b + (a % b != 0 ? 1 : 0));
}
/**
* Return the rounded up integer division result of a / b.
*
* @param a
* dividend
* @param b
* divisor
* @return rounded up divison result
*/
public static byte divRoundUp(byte a, byte b) {
return (byte) (a / b + (a % b != 0 ? 1 : 0));
}
/**
* Calculate the difference between two values. Considers overflow cases.
*
* @param biggerVal
* The bigger Value
* @param smallerVal
* The smaller Value
* @return the difference
*/
public static int calcDiffWithOverflow(int biggerVal, int smallerVal) {
if (biggerVal < smallerVal)
return (Integer.MAX_VALUE - smallerVal)
+ (biggerVal - Integer.MIN_VALUE) + 1;
else
return biggerVal - smallerVal;
}
/**
* Converts a string "00:00:00:00:00:00" into the corresponding int[]
*
* @param macAddr
* @param buffer
* @throws JtcpipException
*/
public static void macStrToByteArr(String macAddr, int[] buffer)
throws JtcpipException {
char[] c = macAddr.toCharArray();
int j = 0;
String s = "";
if (buffer == null)
buffer = new int[6];
for (int i = 0; i < c.length; i++) {
if (c[i] != ':')
s += c[i];
if (c[i] == ':' || i == (c.length - 1)) {
buffer[j] = Integer.parseInt(s, 16);
if (buffer[j] < 0 || buffer[j] > 255)
throw macException;
s = "";
j++;
if (j == buffer.length) {
if (i < (c.length - 1))
throw macException;
return;
}
}
}
if (j != 6)
throw macException;
}
/**
* Tests if a value is between two other values. Considers overflow cases.
*
* @param smallerVal
* The smaller boundary value
* @param biggerVal
* The bigger boundare value
* @param testVal
* The value to test
* @return true if testVal is between smallerVal and biggerVal
*/
public static boolean isBetween(int smallerVal, int biggerVal, int testVal) {
if (biggerVal < smallerVal)
return ((testVal > smallerVal) && (testVal <= Integer.MAX_VALUE))
|| ((testVal >= Integer.MIN_VALUE) && (testVal < biggerVal));
else
return (testVal > smallerVal) && (testVal < biggerVal);
}
/**
* Tests if a value is between two other values (or equal to the higher
* value). Considers overflow cases.
*
* @param smallerVal
* The smaller boundary value
* @param biggerVal
* The bigger boundare value
* @param testVal
* The value to test
* @return true if testVal is between smallerVal and biggerVal (or equal to
* biggerVal)
*/
public static boolean isBetweenOrEqualBigger(int smallerVal, int biggerVal,
int testVal) {
if (biggerVal < smallerVal)
return ((testVal > smallerVal) && (testVal <= Integer.MAX_VALUE))
|| ((testVal >= Integer.MIN_VALUE) && (testVal <= biggerVal));
else
return (testVal > smallerVal) && (testVal <= biggerVal);
}
/**
* Tests if a value is between two other values (or equal to the lower
* value). Considers overflow cases.
*
* @param smallerVal
* The smaller boundary value
* @param biggerVal
* The bigger boundare value
* @param testVal
* The value to test
* @return true if testVal is between smallerVal and biggerVal (or equal to
* smallerVal)
*/
public static boolean isBetweenOrEqualSmaller(int smallerVal,
int biggerVal, int testVal) {
if (biggerVal < smallerVal)
return ((testVal >= smallerVal) && (testVal <= Integer.MAX_VALUE))
|| ((testVal >= Integer.MIN_VALUE) && (testVal < biggerVal));
else
return (testVal >= smallerVal) && (testVal < biggerVal);
}
/**
* Retrieves the ip address value from a string.
*
* @param connectorString
* of the form //ip_addr:port[;option=value...]
* @return the ip address as integer
*/
public static int getAddrFromConnectorStr(String connectorString) {
int offset = 0;
if (connectorString.charAt(0) == '/')
offset = 2;
int colon = connectorString.indexOf(':');
if (colon < 1)
throw new IllegalArgumentException("no ':' in connector string");
if (colon == 1)
return 0xFFFFFFFF; // 255.255.255.255
try {
return IP.ipStringToInt(connectorString.substring(offset, colon));
} catch (JtcpipException e) {
Debug.println("Wrong IP address in connector string ("
+ connectorString + ")", Debug.DBG_OTHER);
return 0;
}
}
/**
* Retrieves the port value from a string.
*
* @param connectorString
* of the form //ip_addr:port[;option=value...]
* @return the port as int or -1 in case of a failure
*/
public static int getPortFromConnectorStr(String connectorString) {
int colon = connectorString.indexOf(':');
int semic = connectorString.indexOf(';'); // in case there are
// options...
if (colon < 1)
return -1;
try {
if (semic < 1)
semic = Integer.parseInt(connectorString.substring(colon + 1));
else
semic = Integer.parseInt(connectorString.substring(colon + 1,
semic));
} catch (Exception e) {
semic = -1;
}
if (semic < 0 || semic > 0xFFFF) {
Debug.println("Port number out of bounds in connector string ("
+ connectorString + ")", Debug.DBG_OTHER);
return -1;
} else
return semic;
}
}
|
 |