mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 03:09:04 +01:00
Updated java.library.path.
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package org.briarproject.bramble.util;
|
||||
|
||||
public class ByteUtils {
|
||||
|
||||
/**
|
||||
* The maximum value that can be represented as an unsigned 16-bit integer.
|
||||
*/
|
||||
public static final int MAX_16_BIT_UNSIGNED = 65535; // 2^16 - 1
|
||||
|
||||
/**
|
||||
* The maximum value that can be represented as an unsigned 32-bit integer.
|
||||
*/
|
||||
public static final long MAX_32_BIT_UNSIGNED = 4294967295L; // 2^32 - 1
|
||||
|
||||
/** The number of bytes needed to encode a 16-bit integer. */
|
||||
public static final int INT_16_BYTES = 2;
|
||||
|
||||
/** The number of bytes needed to encode a 32-bit integer. */
|
||||
public static final int INT_32_BYTES = 4;
|
||||
|
||||
/** The number of bytes needed to encode a 64-bit integer. */
|
||||
public static final int INT_64_BYTES = 8;
|
||||
|
||||
public static void writeUint16(int src, byte[] dest, int offset) {
|
||||
if (src < 0) throw new IllegalArgumentException();
|
||||
if (src > MAX_16_BIT_UNSIGNED) throw new IllegalArgumentException();
|
||||
if (dest.length < offset + INT_16_BYTES)
|
||||
throw new IllegalArgumentException();
|
||||
dest[offset] = (byte) (src >> 8);
|
||||
dest[offset + 1] = (byte) (src & 0xFF);
|
||||
}
|
||||
|
||||
public static void writeUint32(long src, byte[] dest, int offset) {
|
||||
if (src < 0) throw new IllegalArgumentException();
|
||||
if (src > MAX_32_BIT_UNSIGNED) throw new IllegalArgumentException();
|
||||
if (dest.length < offset + INT_32_BYTES)
|
||||
throw new IllegalArgumentException();
|
||||
dest[offset] = (byte) (src >> 24);
|
||||
dest[offset + 1] = (byte) (src >> 16 & 0xFF);
|
||||
dest[offset + 2] = (byte) (src >> 8 & 0xFF);
|
||||
dest[offset + 3] = (byte) (src & 0xFF);
|
||||
}
|
||||
|
||||
public static void writeUint64(long src, byte[] dest, int offset) {
|
||||
if (src < 0) throw new IllegalArgumentException();
|
||||
if (dest.length < offset + INT_64_BYTES)
|
||||
throw new IllegalArgumentException();
|
||||
dest[offset] = (byte) (src >> 56);
|
||||
dest[offset + 1] = (byte) (src >> 48 & 0xFF);
|
||||
dest[offset + 2] = (byte) (src >> 40 & 0xFF);
|
||||
dest[offset + 3] = (byte) (src >> 32 & 0xFF);
|
||||
dest[offset + 4] = (byte) (src >> 24 & 0xFF);
|
||||
dest[offset + 5] = (byte) (src >> 16 & 0xFF);
|
||||
dest[offset + 6] = (byte) (src >> 8 & 0xFF);
|
||||
dest[offset + 7] = (byte) (src & 0xFF);
|
||||
}
|
||||
|
||||
public static int readUint16(byte[] src, int offset) {
|
||||
if (src.length < offset + INT_16_BYTES)
|
||||
throw new IllegalArgumentException();
|
||||
return ((src[offset] & 0xFF) << 8) | (src[offset + 1] & 0xFF);
|
||||
}
|
||||
|
||||
public static long readUint32(byte[] src, int offset) {
|
||||
if (src.length < offset + INT_32_BYTES)
|
||||
throw new IllegalArgumentException();
|
||||
return ((src[offset] & 0xFFL) << 24)
|
||||
| ((src[offset + 1] & 0xFFL) << 16)
|
||||
| ((src[offset + 2] & 0xFFL) << 8)
|
||||
| (src[offset + 3] & 0xFFL);
|
||||
}
|
||||
|
||||
public static long readUint64(byte[] src, int offset) {
|
||||
if (src.length < offset + INT_64_BYTES)
|
||||
throw new IllegalArgumentException();
|
||||
return ((src[offset] & 0xFFL) << 56)
|
||||
| ((src[offset + 1] & 0xFFL) << 48)
|
||||
| ((src[offset + 2] & 0xFFL) << 40)
|
||||
| ((src[offset + 3] & 0xFFL) << 32)
|
||||
| ((src[offset + 4] & 0xFFL) << 24)
|
||||
| ((src[offset + 5] & 0xFFL) << 16)
|
||||
| ((src[offset + 6] & 0xFFL) << 8)
|
||||
| (src[offset + 7] & 0xFFL);
|
||||
}
|
||||
|
||||
public static int readUint(byte[] src, int bits) {
|
||||
if (src.length << 3 < bits) throw new IllegalArgumentException();
|
||||
int dest = 0;
|
||||
for (int i = 0; i < bits; i++) {
|
||||
if ((src[i >> 3] & 128 >> (i & 7)) != 0) dest |= 1 << bits - i - 1;
|
||||
}
|
||||
if (dest < 0) throw new AssertionError();
|
||||
if (dest >= 1 << bits) throw new AssertionError();
|
||||
return dest;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package org.briarproject.bramble.util;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.EOFException;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@NotNullByDefault
|
||||
public class IoUtils {
|
||||
|
||||
public static void deleteFileOrDir(File f) {
|
||||
if (f.isFile()) {
|
||||
f.delete();
|
||||
} else if (f.isDirectory()) {
|
||||
File[] children = f.listFiles();
|
||||
if (children != null)
|
||||
for (File child : children) deleteFileOrDir(child);
|
||||
f.delete();
|
||||
}
|
||||
}
|
||||
|
||||
public static void copyAndClose(InputStream in, OutputStream out)
|
||||
throws IOException {
|
||||
byte[] buf = new byte[4096];
|
||||
try {
|
||||
while (true) {
|
||||
int read = in.read(buf);
|
||||
if (read == -1) break;
|
||||
out.write(buf, 0, read);
|
||||
}
|
||||
in.close();
|
||||
out.flush();
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
tryToClose(in);
|
||||
tryToClose(out);
|
||||
}
|
||||
}
|
||||
|
||||
private static void tryToClose(@Nullable Closeable c) {
|
||||
try {
|
||||
if (c != null) c.close();
|
||||
} catch (IOException e) {
|
||||
// We did our best
|
||||
}
|
||||
}
|
||||
|
||||
public static void read(InputStream in, byte[] b) throws IOException {
|
||||
int offset = 0;
|
||||
while (offset < b.length) {
|
||||
int read = in.read(b, offset, b.length - offset);
|
||||
if (read == -1) throw new EOFException();
|
||||
offset += read;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package org.briarproject.bramble.util;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@NotNullByDefault
|
||||
public class OsUtils {
|
||||
|
||||
@Nullable
|
||||
private static final String os = System.getProperty("os.name");
|
||||
@Nullable
|
||||
private static final String version = System.getProperty("os.version");
|
||||
@Nullable
|
||||
private static final String vendor = System.getProperty("java.vendor");
|
||||
|
||||
public static boolean isWindows() {
|
||||
return os != null && os.contains("Windows");
|
||||
}
|
||||
|
||||
public static boolean isMac() {
|
||||
return os != null && os.contains("Mac OS");
|
||||
}
|
||||
|
||||
public static boolean isMacLeopardOrNewer() {
|
||||
if (!isMac() || version == null) return false;
|
||||
try {
|
||||
String[] v = version.split("\\.");
|
||||
if (v.length != 3) return false;
|
||||
int major = Integer.parseInt(v[0]);
|
||||
int minor = Integer.parseInt(v[1]);
|
||||
return major >= 10 && minor >= 5;
|
||||
} catch (NumberFormatException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isLinux() {
|
||||
return os != null && os.contains("Linux") && !isAndroid();
|
||||
}
|
||||
|
||||
public static boolean isAndroid() {
|
||||
return vendor != null && vendor.contains("Android");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package org.briarproject.bramble.util;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketAddress;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@NotNullByDefault
|
||||
public class PrivacyUtils {
|
||||
|
||||
public static String scrubOnion(String onion) {
|
||||
// keep first three characters of onion address
|
||||
return onion.substring(0, 3) + "[scrubbed]";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String scrubMacAddress(@Nullable String address) {
|
||||
if (address == null) return null;
|
||||
// this is a fake address we need to know about
|
||||
if (address.equals("02:00:00:00:00:00")) return address;
|
||||
// keep first and last octet of MAC address
|
||||
return address.substring(0, 3) + "[scrubbed]"
|
||||
+ address.substring(14, 17);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String scrubInetAddress(InetAddress address) {
|
||||
// don't scrub link and site local addresses
|
||||
if (address.isLinkLocalAddress() || address.isSiteLocalAddress())
|
||||
return address.toString();
|
||||
// completely scrub IPv6 addresses
|
||||
if (address instanceof Inet6Address) return "[scrubbed]";
|
||||
// keep first and last octet of IPv4 addresses
|
||||
return scrubInetAddress(address.toString());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String scrubInetAddress(@Nullable String address) {
|
||||
if (address == null) return null;
|
||||
|
||||
int firstDot = address.indexOf(".");
|
||||
if (firstDot == -1) return "[scrubbed]";
|
||||
String prefix = address.substring(0, firstDot + 1);
|
||||
int lastDot = address.lastIndexOf(".");
|
||||
String suffix = address.substring(lastDot, address.length());
|
||||
return prefix + "[scrubbed]" + suffix;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String scrubSocketAddress(InetSocketAddress address) {
|
||||
InetAddress inetAddress = address.getAddress();
|
||||
return scrubInetAddress(inetAddress);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String scrubSocketAddress(SocketAddress address) {
|
||||
if (address instanceof InetSocketAddress)
|
||||
return scrubSocketAddress((InetSocketAddress) address);
|
||||
return scrubInetAddress(address.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package org.briarproject.bramble.util;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.CharacterCodingException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.CharsetDecoder;
|
||||
import java.util.Collection;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static java.nio.charset.CodingErrorAction.IGNORE;
|
||||
import static java.util.regex.Pattern.CASE_INSENSITIVE;
|
||||
|
||||
@NotNullByDefault
|
||||
public class StringUtils {
|
||||
|
||||
private static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||
private static Pattern MAC = Pattern.compile("[0-9a-f]{2}:[0-9a-f]{2}:" +
|
||||
"[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}",
|
||||
CASE_INSENSITIVE);
|
||||
|
||||
private static final char[] HEX = new char[] {
|
||||
'0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
|
||||
};
|
||||
|
||||
public static boolean isNullOrEmpty(@Nullable String s) {
|
||||
return s == null || s.length() == 0;
|
||||
}
|
||||
|
||||
public static String join(Collection<String> strings, String separator) {
|
||||
StringBuilder joined = new StringBuilder();
|
||||
for (String s : strings) {
|
||||
if (joined.length() > 0) joined.append(separator);
|
||||
joined.append(s);
|
||||
}
|
||||
return joined.toString();
|
||||
}
|
||||
|
||||
public static byte[] toUtf8(String s) {
|
||||
try {
|
||||
return s.getBytes("UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String fromUtf8(byte[] bytes) {
|
||||
return fromUtf8(bytes, 0, bytes.length);
|
||||
}
|
||||
|
||||
public static String fromUtf8(byte[] bytes, int off, int len) {
|
||||
CharsetDecoder decoder = UTF_8.newDecoder();
|
||||
decoder.onMalformedInput(IGNORE);
|
||||
decoder.onUnmappableCharacter(IGNORE);
|
||||
ByteBuffer buffer = ByteBuffer.wrap(bytes, off, len);
|
||||
try {
|
||||
return decoder.decode(buffer).toString();
|
||||
} catch (CharacterCodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String truncateUtf8(String s, int maxUtf8Length) {
|
||||
byte[] utf8 = toUtf8(s);
|
||||
if (utf8.length <= maxUtf8Length) return s;
|
||||
return fromUtf8(utf8, 0, maxUtf8Length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given byte array to a hex character array.
|
||||
*/
|
||||
private static char[] toHexChars(byte[] bytes) {
|
||||
char[] hex = new char[bytes.length * 2];
|
||||
for (int i = 0, j = 0; i < bytes.length; i++) {
|
||||
hex[j++] = HEX[(bytes[i] >> 4) & 0xF];
|
||||
hex[j++] = HEX[bytes[i] & 0xF];
|
||||
}
|
||||
return hex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given byte array to a hex string.
|
||||
*/
|
||||
public static String toHexString(byte[] bytes) {
|
||||
return new String(toHexChars(bytes));
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given hex string to a byte array.
|
||||
*/
|
||||
public static byte[] fromHexString(String hex) {
|
||||
int len = hex.length();
|
||||
if (len % 2 != 0)
|
||||
throw new IllegalArgumentException("Not a hex string");
|
||||
byte[] bytes = new byte[len / 2];
|
||||
for (int i = 0, j = 0; i < len; i += 2, j++) {
|
||||
int high = hexDigitToInt(hex.charAt(i));
|
||||
int low = hexDigitToInt(hex.charAt(i + 1));
|
||||
bytes[j] = (byte) ((high << 4) + low);
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
private static int hexDigitToInt(char c) {
|
||||
if (c >= '0' && c <= '9') return c - '0';
|
||||
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
|
||||
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
|
||||
throw new IllegalArgumentException("Not a hex digit: " + c);
|
||||
}
|
||||
|
||||
public static String trim(String s) {
|
||||
return s.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string is longer than maxLength
|
||||
*/
|
||||
public static boolean utf8IsTooLong(String s, int maxLength) {
|
||||
return toUtf8(s).length > maxLength;
|
||||
}
|
||||
|
||||
public static byte[] macToBytes(String mac) {
|
||||
if (!MAC.matcher(mac).matches()) throw new IllegalArgumentException();
|
||||
return fromHexString(mac.replaceAll(":", ""));
|
||||
}
|
||||
|
||||
public static String macToString(byte[] mac) {
|
||||
if (mac.length != 6) throw new IllegalArgumentException();
|
||||
StringBuilder s = new StringBuilder();
|
||||
for (byte b : mac) {
|
||||
if (s.length() > 0) s.append(':');
|
||||
s.append(HEX[(b >> 4) & 0xF]);
|
||||
s.append(HEX[b & 0xF]);
|
||||
}
|
||||
return s.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package org.briarproject.bramble.util;
|
||||
|
||||
import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.data.BdfDictionary;
|
||||
import org.briarproject.bramble.api.data.BdfList;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@NotNullByDefault
|
||||
public class ValidationUtils {
|
||||
|
||||
public static void checkLength(@Nullable String s, int minLength,
|
||||
int maxLength) throws FormatException {
|
||||
if (s != null) {
|
||||
int length = StringUtils.toUtf8(s).length;
|
||||
if (length < minLength) throw new FormatException();
|
||||
if (length > maxLength) throw new FormatException();
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkLength(@Nullable String s, int length)
|
||||
throws FormatException {
|
||||
if (s != null && StringUtils.toUtf8(s).length != length)
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
public static void checkLength(@Nullable byte[] b, int minLength,
|
||||
int maxLength) throws FormatException {
|
||||
if (b != null) {
|
||||
if (b.length < minLength) throw new FormatException();
|
||||
if (b.length > maxLength) throw new FormatException();
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkLength(@Nullable byte[] b, int length)
|
||||
throws FormatException {
|
||||
if (b != null && b.length != length) throw new FormatException();
|
||||
}
|
||||
|
||||
public static void checkSize(@Nullable BdfList list, int minSize,
|
||||
int maxSize) throws FormatException {
|
||||
if (list != null) {
|
||||
if (list.size() < minSize) throw new FormatException();
|
||||
if (list.size() > maxSize) throw new FormatException();
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkSize(@Nullable BdfList list, int size)
|
||||
throws FormatException {
|
||||
if (list != null && list.size() != size) throw new FormatException();
|
||||
}
|
||||
|
||||
public static void checkSize(@Nullable BdfDictionary dictionary,
|
||||
int minSize, int maxSize) throws FormatException {
|
||||
if (dictionary != null) {
|
||||
if (dictionary.size() < minSize) throw new FormatException();
|
||||
if (dictionary.size() > maxSize) throw new FormatException();
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkSize(@Nullable BdfDictionary dictionary, int size)
|
||||
throws FormatException {
|
||||
if (dictionary != null && dictionary.size() != size)
|
||||
throw new FormatException();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user