2015-03-31 21:06:42 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Christian Basler
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package ch.dissem.bitmessage.utils;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
2015-04-14 19:47:53 +02:00
|
|
|
import static ch.dissem.bitmessage.utils.AccessCounter.inc;
|
|
|
|
|
2015-03-31 21:06:42 +02:00
|
|
|
/**
|
|
|
|
* This class handles decoding simple types from byte stream, according to
|
|
|
|
* https://bitmessage.org/wiki/Protocol_specification#Common_structures
|
|
|
|
*/
|
|
|
|
public class Decode {
|
2016-06-12 20:53:05 +02:00
|
|
|
public static byte[] shortVarBytes(InputStream in, AccessCounter counter) throws IOException {
|
|
|
|
int length = uint16(in, counter);
|
|
|
|
return bytes(in, length, counter);
|
2015-05-09 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 20:53:05 +02:00
|
|
|
public static byte[] varBytes(InputStream in) throws IOException {
|
|
|
|
return varBytes(in, null);
|
2015-05-09 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 20:53:05 +02:00
|
|
|
public static byte[] varBytes(InputStream in, AccessCounter counter) throws IOException {
|
|
|
|
int length = (int) varInt(in, counter);
|
|
|
|
return bytes(in, length, counter);
|
2015-05-09 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 20:53:05 +02:00
|
|
|
public static byte[] bytes(InputStream in, int count) throws IOException {
|
|
|
|
return bytes(in, count, null);
|
2015-04-14 19:47:53 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 20:53:05 +02:00
|
|
|
public static byte[] bytes(InputStream in, int count, AccessCounter counter) throws IOException {
|
2015-03-31 21:06:42 +02:00
|
|
|
byte[] result = new byte[count];
|
2015-04-07 18:48:58 +02:00
|
|
|
int off = 0;
|
|
|
|
while (off < count) {
|
2016-06-12 20:53:05 +02:00
|
|
|
int read = in.read(result, off, count - off);
|
2015-04-14 19:47:53 +02:00
|
|
|
if (read < 0) {
|
|
|
|
throw new IOException("Unexpected end of stream, wanted to read " + count + " bytes but only got " + off);
|
|
|
|
}
|
|
|
|
off += read;
|
2015-04-07 18:48:58 +02:00
|
|
|
}
|
2015-04-14 19:47:53 +02:00
|
|
|
inc(counter, count);
|
2015-03-31 21:06:42 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-06-12 20:53:05 +02:00
|
|
|
public static long[] varIntList(InputStream in) throws IOException {
|
|
|
|
int length = (int) varInt(in);
|
2015-03-31 21:06:42 +02:00
|
|
|
long[] result = new long[length];
|
|
|
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
2016-06-12 20:53:05 +02:00
|
|
|
result[i] = varInt(in);
|
2015-03-31 21:06:42 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-06-12 20:53:05 +02:00
|
|
|
public static long varInt(InputStream in) throws IOException {
|
|
|
|
return varInt(in, null);
|
2015-04-14 19:47:53 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 20:53:05 +02:00
|
|
|
public static long varInt(InputStream in, AccessCounter counter) throws IOException {
|
|
|
|
int first = in.read();
|
2015-04-14 19:47:53 +02:00
|
|
|
inc(counter);
|
2015-03-31 21:06:42 +02:00
|
|
|
switch (first) {
|
|
|
|
case 0xfd:
|
2016-06-12 20:53:05 +02:00
|
|
|
return uint16(in, counter);
|
2015-03-31 21:06:42 +02:00
|
|
|
case 0xfe:
|
2016-06-12 20:53:05 +02:00
|
|
|
return uint32(in, counter);
|
2015-03-31 21:06:42 +02:00
|
|
|
case 0xff:
|
2016-06-12 20:53:05 +02:00
|
|
|
return int64(in, counter);
|
2015-03-31 21:06:42 +02:00
|
|
|
default:
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-12 20:53:05 +02:00
|
|
|
public static int uint8(InputStream in) throws IOException {
|
|
|
|
return in.read();
|
2015-03-31 21:06:42 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 20:53:05 +02:00
|
|
|
public static int uint16(InputStream in) throws IOException {
|
|
|
|
return uint16(in, null);
|
2015-04-14 19:47:53 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 20:53:05 +02:00
|
|
|
public static int uint16(InputStream in, AccessCounter counter) throws IOException {
|
2015-04-14 19:47:53 +02:00
|
|
|
inc(counter, 2);
|
2016-06-12 20:53:05 +02:00
|
|
|
return in.read() << 8 | in.read();
|
2015-03-31 21:06:42 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 20:53:05 +02:00
|
|
|
public static long uint32(InputStream in) throws IOException {
|
|
|
|
return uint32(in, null);
|
2015-04-14 19:47:53 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 20:53:05 +02:00
|
|
|
public static long uint32(InputStream in, AccessCounter counter) throws IOException {
|
2015-04-14 19:47:53 +02:00
|
|
|
inc(counter, 4);
|
2016-06-12 20:53:05 +02:00
|
|
|
return in.read() << 24 | in.read() << 16 | in.read() << 8 | in.read();
|
2015-03-31 21:06:42 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 20:53:05 +02:00
|
|
|
public static long uint32(ByteBuffer in) {
|
|
|
|
return u(in.get()) << 24 | u(in.get()) << 16 | u(in.get()) << 8 | u(in.get());
|
2016-06-01 17:38:49 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 20:53:05 +02:00
|
|
|
public static int int32(InputStream in) throws IOException {
|
|
|
|
return int32(in, null);
|
2015-05-09 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 20:53:05 +02:00
|
|
|
public static int int32(InputStream in, AccessCounter counter) throws IOException {
|
2015-05-09 17:27:45 +02:00
|
|
|
inc(counter, 4);
|
2016-06-12 20:53:05 +02:00
|
|
|
return ByteBuffer.wrap(bytes(in, 4)).getInt();
|
2015-03-31 21:06:42 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 20:53:05 +02:00
|
|
|
public static long int64(InputStream in) throws IOException {
|
|
|
|
return int64(in, null);
|
2015-04-14 19:47:53 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 20:53:05 +02:00
|
|
|
public static long int64(InputStream in, AccessCounter counter) throws IOException {
|
2015-04-14 19:47:53 +02:00
|
|
|
inc(counter, 8);
|
2016-06-12 20:53:05 +02:00
|
|
|
return ByteBuffer.wrap(bytes(in, 8)).getLong();
|
2015-03-31 21:06:42 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 20:53:05 +02:00
|
|
|
public static String varString(InputStream in) throws IOException {
|
|
|
|
return varString(in, null);
|
2015-12-08 20:27:32 +01:00
|
|
|
}
|
|
|
|
|
2016-06-12 20:53:05 +02:00
|
|
|
public static String varString(InputStream in, AccessCounter counter) throws IOException {
|
|
|
|
int length = (int) varInt(in, counter);
|
2015-03-31 21:06:42 +02:00
|
|
|
// FIXME: technically, it says the length in characters, but I think this one might be correct
|
2015-04-17 11:17:39 +02:00
|
|
|
// otherwise it will get complicated, as we'll need to read UTF-8 char by char...
|
2016-06-12 20:53:05 +02:00
|
|
|
return new String(bytes(in, length, counter), "utf-8");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the given byte as if it were unsigned.
|
|
|
|
*/
|
|
|
|
private static int u(byte b) {
|
|
|
|
return b & 0xFF;
|
2015-03-31 21:06:42 +02:00
|
|
|
}
|
|
|
|
}
|