public static Plaintext.Builder readWithoutSignature(Type type, InputStream in) throws IOException { long version = Decode.varInt(in); return new Builder(type) .addressVersion(version) .stream(Decode.varInt(in)) .behaviorBitfield(Decode.int32(in)) .publicSigningKey(Decode.bytes(in, 64)) .publicEncryptionKey(Decode.bytes(in, 64)) .nonceTrialsPerByte(version >= 3 ? Decode.varInt(in) : 0) .extraBytes(version >= 3 ? Decode.varInt(in) : 0) .destinationRipe(type == Type.MSG ? Decode.bytes(in, 20) : null) .encoding(Decode.varInt(in)) .message(Decode.varBytes(in)) .ackMessage(type == Type.MSG ? Decode.varBytes(in) : null); }
public static long varInt(InputStream stream, AccessCounter counter) throws IOException { int first = stream.read(); inc(counter); switch (first) { case 0xfd: return uint16(stream, counter); case 0xfe: return uint32(stream, counter); case 0xff: return int64(stream, counter); default: return first; } }
public static String varString(InputStream in) throws IOException { return varString(in, null); }
private static NetworkAddress parseAddress(InputStream stream, boolean light) throws IOException { long time; long streamNumber; if (!light) { time = Decode.int64(stream); streamNumber = Decode.uint32(stream); // This isn't consistent, not sure if this is correct } else { time = 0; streamNumber = 0; } long services = Decode.int64(stream); byte[] ipv6 = Decode.bytes(stream, 16); int port = Decode.uint16(stream); return new NetworkAddress.Builder().time(time).stream(streamNumber).services(services).ipv6(ipv6).port(port).build(); }
private static Version parseVersion(InputStream stream) throws IOException { int version = Decode.int32(stream); long services = Decode.int64(stream); long timestamp = Decode.int64(stream); NetworkAddress addrRecv = parseAddress(stream, true); NetworkAddress addrFrom = parseAddress(stream, true); long nonce = Decode.int64(stream); String userAgent = Decode.varString(stream); long[] streamNumbers = Decode.varIntList(stream); return new Version.Builder() .version(version) .services(services) .timestamp(timestamp) .addrRecv(addrRecv).addrFrom(addrFrom) .nonce(nonce) .userAgent(userAgent) .streams(streamNumbers).build(); }
public static ObjectMessage readObject(InputStream in, int length) throws IOException { AccessCounter counter = new AccessCounter(); byte nonce[] = Decode.bytes(in, 8, counter); long expiresTime = Decode.int64(in, counter); long objectType = Decode.uint32(in, counter); long version = Decode.varInt(in, counter); long stream = Decode.varInt(in, counter); byte[] data = Decode.bytes(in, length - counter.length()); ObjectPayload payload; try { ByteArrayInputStream dataStream = new ByteArrayInputStream(data); payload = Factory.getObjectPayload(objectType, version, stream, dataStream, data.length); } catch (IOException e) { LOG.trace("Could not parse object payload - using generic payload instead", e); payload = new GenericPayload(version, stream, data); } return new ObjectMessage.Builder() .nonce(nonce) .expiresTime(expiresTime) .objectType(objectType) .stream(stream) .payload(payload) .build(); }
public static CryptoBox read(InputStream stream, int length) throws IOException { AccessCounter counter = new AccessCounter(); return new Builder() .IV(Decode.bytes(stream, 16, counter)) .curveType(Decode.uint16(stream, counter)) .X(Decode.shortVarBytes(stream, counter)) .Y(Decode.shortVarBytes(stream, counter)) .encrypted(Decode.bytes(stream, length - counter.length() - 32)) .MAC(Decode.bytes(stream, 32)) .build(); }
public static NetworkMessage read(InputStream in) throws IOException { findMagic(in); String command = getCommand(in); int length = (int) Decode.uint32(in); byte[] checksum = Decode.bytes(in, 4); byte[] payloadBytes = Decode.bytes(in, length); if (testChecksum(checksum, payloadBytes)) { MessagePayload payload = getPayload(command, new ByteArrayInputStream(payloadBytes), length); if (payload != null) return new NetworkMessage(payload); else return null; } else { throw new IOException("Checksum failed for message '" + command + "'"); } }
public static byte[] bytes(InputStream stream, int count) throws IOException { return bytes(stream, count, null); }
public static byte[] varBytes(InputStream in) throws IOException { return varBytes(in, null); }
public static long uint32(InputStream in) throws IOException { return uint32(in, null); }
public static int int32(InputStream stream) throws IOException { return int32(stream, null); }
public static long int64(InputStream stream) throws IOException { return int64(stream, null); }
public static int uint16(InputStream stream) throws IOException { return uint16(stream, null); }
private static NetworkAddress parseAddress(InputStream stream, boolean light) throws IOException { long time; long streamNumber; if (!light) { time = Decode.int64(stream); streamNumber = Decode.uint32(stream); // This isn't consistent, not sure if this is correct } else { time = 0; streamNumber = 0; } long services = Decode.int64(stream); byte[] ipv6 = Decode.bytes(stream, 16); int port = Decode.uint16(stream); return new NetworkAddress.Builder() .time(time) .stream(streamNumber) .services(services) .ipv6(ipv6) .port(port) .build(); }