List of usage examples for io.netty.buffer ByteBuf writerIndex
public abstract int writerIndex();
From source file:de.unipassau.isl.evs.ssh.core.network.handler.Decrypter.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> decoded) throws Exception { try {/* www. j av a 2 s . c o m*/ final int encryptedLength = in.readInt(); final int decryptedLength = decryptCipher.getOutputSize(encryptedLength); final ByteBuf out = ctx.alloc().buffer(decryptedLength); //Log.v(TAG, "Decrypting " + encryptedLength + "b data to " + decryptedLength + "b of decrypted data"); decryptCipher.doFinal(in.nioBuffer(in.readerIndex(), encryptedLength), out.nioBuffer(out.writerIndex(), decryptedLength)); out.writerIndex(out.writerIndex() + decryptedLength); in.readerIndex(in.readerIndex() + encryptedLength); decoded.add(out); } catch (GeneralSecurityException | RuntimeException e) { ctx.close(); throw e; } }
From source file:de.unipassau.isl.evs.ssh.core.network.handler.Encrypter.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception { final int decryptedLength = in.readableBytes(); final int encryptedLength = encryptCipher.getOutputSize(decryptedLength); //Log.v(TAG, "Encrypting " + decryptedLength + "b data to " + encryptedLength + "b of encrypted data"); out.writeInt(encryptedLength);/* w w w .j a v a 2 s . c om*/ out.ensureWritable(encryptedLength); final ByteBuffer inNio = in.nioBuffer(in.readerIndex(), decryptedLength); final ByteBuffer outNio = out.nioBuffer(out.writerIndex(), encryptedLength); encryptCipher.doFinal(inNio, outNio); if (inNio.hasRemaining()) { Log.wtf(TAG, "Crypto library did not read all bytes for encryption (" + inNio.remaining() + " remaining)"); } if (outNio.hasRemaining()) { Log.wtf(TAG, "Crypto library did not write all bytes for encryption (" + outNio.remaining() + " remaining)"); } out.writerIndex(out.writerIndex() + encryptedLength); in.readerIndex(in.readerIndex() + decryptedLength); }
From source file:de.unipassau.isl.evs.ssh.core.network.handler.SignatureGenerator.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { final int dataLength = msg.readableBytes(); msg.markReaderIndex();/* w w w. j a va 2 s .c o m*/ out.writeInt(dataLength); out.writeBytes(msg); msg.resetReaderIndex(); signSignature.update(msg.nioBuffer()); msg.readerIndex(msg.writerIndex()); final byte[] signature = signSignature.sign(); final int signatureLength = signature.length; out.writeInt(signatureLength); out.writeBytes(signature); //Log.v(TAG, "Signed " + dataLength + "b of data with " + signatureLength + "b signature" + // (Log.isLoggable(TAG, Log.VERBOSE) ? ": " + Arrays.toString(signature) : "")); }
From source file:divconq.ctp.stream.GzipStream.java
License:Open Source License
@Override public ReturnOption handle(FileDescriptor file, ByteBuf data) { if (file == FileDescriptor.FINAL) return this.downstream.handle(file, data); // we don't know what to do with a folder at this stage - gzip is for file content only // folder scanning is upstream in the FileSourceStream and partners if (file.isFolder()) return ReturnOption.CONTINUE; // init if not set for this round of processing if (this.deflater == null) { this.deflater = new Deflater(this.compressionLevel, true); this.crc.reset(); this.writeHeader = true; }/*w w w .ja va 2s . com*/ ByteBuf in = data; ByteBuf out = null; if (in != null) { byte[] inAry = in.array(); // always allow for a header (10) plus footer (8) plus extra (12) // in addition to content int sizeEstimate = (int) Math.ceil(in.readableBytes() * 1.001) + 30; out = Hub.instance.getBufferAllocator().heapBuffer(sizeEstimate); if (this.writeHeader) { this.writeHeader = false; out.writeBytes(gzipHeader); } this.crc.update(inAry, in.arrayOffset(), in.writerIndex()); this.deflater.setInput(inAry, in.arrayOffset(), in.writerIndex()); while (!this.deflater.needsInput()) deflate(out); } else out = Hub.instance.getBufferAllocator().heapBuffer(30); FileDescriptor blk = new FileDescriptor(); if (StringUtil.isEmpty(this.lastpath)) { if (StringUtil.isNotEmpty(this.nameHint)) this.lastpath = "/" + this.nameHint; else if (file.getPath() != null) this.lastpath = "/" + GzipUtils.getCompressedFilename(file.path().getFileName()); else this.lastpath = "/" + FileUtil.randomFilename("gz"); } blk.setPath(this.lastpath); file.setModTime(System.currentTimeMillis()); if (file.isEof()) { this.deflater.finish(); while (!this.deflater.finished()) deflate(out); int crcValue = (int) this.crc.getValue(); out.writeByte(crcValue); out.writeByte(crcValue >>> 8); out.writeByte(crcValue >>> 16); out.writeByte(crcValue >>> 24); int uncBytes = this.deflater.getTotalIn(); out.writeByte(uncBytes); out.writeByte(uncBytes >>> 8); out.writeByte(uncBytes >>> 16); out.writeByte(uncBytes >>> 24); this.deflater.end(); this.deflater = null; // cause a reset for next time we use stream blk.setEof(true); } if (in != null) in.release(); return this.downstream.handle(blk, out); }
From source file:divconq.ctp.stream.GzipStream.java
License:Open Source License
protected void deflate(ByteBuf out) { int numBytes = 0; do {//w ww. j ava 2 s . com byte[] o = out.array(); numBytes = this.deflater.deflate(o, out.arrayOffset() + out.writerIndex(), out.writableBytes(), Deflater.SYNC_FLUSH); out.writerIndex(out.writerIndex() + numBytes); } while (numBytes > 0); }
From source file:divconq.ctp.stream.TarStream.java
License:Open Source License
@Override public ReturnOption handle(FileDescriptor file, ByteBuf data) { if (file == FileDescriptor.FINAL) { if (this.tstream == null) return this.downstream.handle(file, data); this.finalflag = true; }/*from ww w . j a v a 2 s . c om*/ // I don't think tar cares about folder entries at this stage - tar is for file content only // folder scanning is upstream in the FileSourceStream and partners // TODO try with ending / to file name if (file.isFolder()) return ReturnOption.CONTINUE; // init if not set for this round of processing if (this.tstream == null) { this.bstream = new CyclingByteBufferOutputStream(); this.tstream = new TarArchiveOutputStream(this.bstream); this.tstream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); } ByteBuf in = data; ByteBuf out = null; // always allow for a header (512) and/or footer (1024) in addition to content int sizeEstimate = (in != null) ? in.readableBytes() + 2048 : 2048; out = Hub.instance.getBufferAllocator().heapBuffer(sizeEstimate); this.bstream.installBuffer(out); // TODO if there is no output available to send and not EOF then just request more, // no need to send a message that is empty and not EOF FileDescriptor blk = new FileDescriptor(); if (StringUtil.isNotEmpty(this.lastpath)) { blk.setPath(this.lastpath); } else { if (file.hasPath()) this.lastpath = "/" + (StringUtil.isNotEmpty(this.nameHint) ? this.nameHint : file.path().getFileName()) + ".tar"; else if (StringUtil.isNotEmpty(this.nameHint)) this.lastpath = "/" + this.nameHint + ".tar"; else this.lastpath = "/" + FileUtil.randomFilename() + ".tar"; blk.setPath(this.lastpath); } blk.setModTime(System.currentTimeMillis()); if (!this.archiveopenflag && !this.finalflag) { TarArchiveEntry tentry = new TarArchiveEntry(file.getPath().toString().substring(1), true); tentry.setSize(file.getSize()); tentry.setModTime(file.getModTime()); try { this.tstream.putArchiveEntry(tentry); } catch (IOException x) { if (in != null) in.release(); out.release(); OperationContext.get().getTaskRun().kill("Problem writing tar entry: " + x); return ReturnOption.DONE; } this.archiveopenflag = true; } if (in != null) try { this.tstream.write(in.array(), in.arrayOffset(), in.writerIndex()); } catch (IOException x) { in.release(); out.release(); OperationContext.get().getTaskRun().kill("Problem writing tar body: " + x); return ReturnOption.DONE; } if (file.isEof()) { try { this.tstream.closeArchiveEntry(); } catch (IOException x) { if (in != null) in.release(); out.release(); OperationContext.get().getTaskRun().kill("Problem closing tar entry: " + x); return ReturnOption.DONE; } this.archiveopenflag = false; } if (in != null) in.release(); if (file == FileDescriptor.FINAL) { blk.setEof(true); try { this.tstream.close(); } catch (IOException x) { //in.release(); out.release(); OperationContext.get().getTaskRun().kill("Problem closing tar stream: " + x); return ReturnOption.DONE; } this.tstream = null; this.bstream = null; } else this.bstream.uninstallBuffer(); // we are done with out forever, don't reference it System.out.println("tar sending: " + out.readableBytes()); ReturnOption v = this.downstream.handle(blk, out); if (!this.finalflag) return v; if (v == ReturnOption.CONTINUE) return this.downstream.handle(FileDescriptor.FINAL, null); return ReturnOption.DONE; }
From source file:divconq.ctp.stream.UngzipStream.java
License:Open Source License
protected void inflate(ByteBuf in) { switch (this.gzipState) { case HEADER_START: if (in.readableBytes() < 10) return; // read magic numbers int magic0 = in.readByte(); int magic1 = in.readByte(); if (magic0 != 31) { OperationContext.get().getTaskRun().kill("Input is not in the GZIP format"); return; }//from w w w . j a v a 2 s .com this.crc.update(magic0); this.crc.update(magic1); int method = in.readUnsignedByte(); if (method != Deflater.DEFLATED) { OperationContext.get().getTaskRun() .kill("Unsupported compression method " + method + " in the GZIP header"); return; } this.crc.update(method); this.flags = in.readUnsignedByte(); this.crc.update(this.flags); if ((this.flags & FRESERVED) != 0) { OperationContext.get().getTaskRun().kill("Reserved flags are set in the GZIP header"); return; } // mtime (int) this.crc.update(in.readByte()); this.crc.update(in.readByte()); this.crc.update(in.readByte()); this.crc.update(in.readByte()); this.crc.update(in.readUnsignedByte()); // extra flags this.crc.update(in.readUnsignedByte()); // operating system this.gzipState = GzipState.FLG_READ; case FLG_READ: if ((this.flags & FEXTRA) != 0) { if (in.readableBytes() < 2) return; int xlen1 = in.readUnsignedByte(); int xlen2 = in.readUnsignedByte(); this.crc.update(xlen1); this.crc.update(xlen2); this.xlen |= xlen1 << 8 | xlen2; } this.gzipState = GzipState.XLEN_READ; case XLEN_READ: if (this.xlen != -1) { if (in.readableBytes() < xlen) return; byte[] xtra = new byte[xlen]; in.readBytes(xtra); this.crc.update(xtra); } this.gzipState = GzipState.SKIP_FNAME; case SKIP_FNAME: if ((this.flags & FNAME) != 0) { boolean gotend = false; while (in.isReadable()) { int b = in.readUnsignedByte(); this.crc.update(b); if (b == 0x00) { gotend = true; break; } } if (!gotend) return; } this.gzipState = GzipState.SKIP_COMMENT; case SKIP_COMMENT: if ((this.flags & FCOMMENT) != 0) { boolean gotend = false; while (in.isReadable()) { int b = in.readUnsignedByte(); this.crc.update(b); if (b == 0x00) { gotend = true; break; } } if (!gotend) return; } this.gzipState = GzipState.PROCESS_FHCRC; case PROCESS_FHCRC: if ((this.flags & FHCRC) != 0) { if (in.readableBytes() < 4) return; long crcValue = 0; for (int i = 0; i < 4; ++i) crcValue |= (long) in.readUnsignedByte() << i * 8; long readCrc = crc.getValue(); if (crcValue != readCrc) { OperationContext.get().getTaskRun() .kill("CRC value missmatch. Expected: " + crcValue + ", Got: " + readCrc); return; } } this.crc.reset(); this.gzipState = GzipState.PRROCESS_CONTENT; case PRROCESS_CONTENT: int readableBytes = in.readableBytes(); if (readableBytes < 1) return; if (in.hasArray()) { this.inflater.setInput(in.array(), in.arrayOffset() + in.readerIndex(), readableBytes); } else { byte[] array = new byte[readableBytes]; in.getBytes(in.readerIndex(), array); this.inflater.setInput(array); } int maxOutputLength = this.inflater.getRemaining() << 1; ByteBuf decompressed = Hub.instance.getBufferAllocator().heapBuffer(maxOutputLength); boolean readFooter = false; byte[] outArray = decompressed.array(); try { while (!this.inflater.needsInput()) { int writerIndex = decompressed.writerIndex(); int outIndex = decompressed.arrayOffset() + writerIndex; int length = decompressed.writableBytes(); if (length == 0) { // completely filled the buffer allocate a new one and start to fill it this.outlist.add(decompressed); decompressed = Hub.instance.getBufferAllocator().heapBuffer(maxOutputLength); outArray = decompressed.array(); continue; } int outputLength = this.inflater.inflate(outArray, outIndex, length); if (outputLength > 0) { decompressed.writerIndex(writerIndex + outputLength); this.crc.update(outArray, outIndex, outputLength); } else { if (this.inflater.needsDictionary()) { if (this.dictionary == null) { OperationContext.get().getTaskRun().kill( "decompression failure, unable to set dictionary as non was specified"); return; } this.inflater.setDictionary(this.dictionary); } } if (this.inflater.finished()) { readFooter = true; break; } } in.skipBytes(readableBytes - this.inflater.getRemaining()); } catch (DataFormatException x) { OperationContext.get().getTaskRun().kill("decompression failure: " + x); return; } finally { if (decompressed.isReadable()) { this.outlist.add(decompressed); } else { decompressed.release(); } } if (!readFooter) break; this.gzipState = GzipState.PROCESS_FOOTER; case PROCESS_FOOTER: if (in.readableBytes() < 8) return; long crcValue = 0; for (int i = 0; i < 4; ++i) crcValue |= (long) in.readUnsignedByte() << i * 8; long readCrc = this.crc.getValue(); if (crcValue != readCrc) { OperationContext.get().getTaskRun() .kill("CRC value missmatch. Expected: " + crcValue + ", Got: " + readCrc); return; } // read ISIZE and verify int dataLength = 0; for (int i = 0; i < 4; ++i) dataLength |= in.readUnsignedByte() << i * 8; int readLength = this.inflater.getTotalOut(); if (dataLength != readLength) { OperationContext.get().getTaskRun() .kill("Number of bytes mismatch. Expected: " + dataLength + ", Got: " + readLength); return; } this.gzipState = GzipState.DONE; case DONE: break; } }
From source file:divconq.net.ssl.SslHandler.java
License:Apache License
private SSLEngineResult wrap(SSLEngine engine, ByteBuf in, ByteBuf out) throws SSLException { ByteBuffer in0 = in.nioBuffer(); if (!in0.isDirect()) { ByteBuffer newIn0 = ByteBuffer.allocateDirect(in0.remaining()); newIn0.put(in0).flip();/*ww w. j a v a 2 s . com*/ in0 = newIn0; } for (;;) { ByteBuffer out0 = out.nioBuffer(out.writerIndex(), out.writableBytes()); SSLEngineResult result = engine.wrap(in0, out0); in.skipBytes(result.bytesConsumed()); out.writerIndex(out.writerIndex() + result.bytesProduced()); switch (result.getStatus()) { case BUFFER_OVERFLOW: out.ensureWritable(maxPacketBufferSize); break; default: return result; } } }
From source file:divconq.net.ssl.SslHandler.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws SSLException { final int startOffset = in.readerIndex(); final int endOffset = in.writerIndex(); int offset = startOffset; int totalLength = 0; // If we calculated the length of the current SSL record before, use that information. if (packetLength > 0) { if (endOffset - startOffset < packetLength) { return; } else {/*from w ww .j a v a2 s . co m*/ offset += packetLength; totalLength = packetLength; packetLength = 0; } } boolean nonSslRecord = false; while (totalLength < OpenSslEngine.MAX_ENCRYPTED_PACKET_LENGTH) { final int readableBytes = endOffset - offset; if (readableBytes < 5) { break; } final int packetLength = getEncryptedPacketLength(in, offset); if (packetLength == -1) { nonSslRecord = true; break; } assert packetLength > 0; if (packetLength > readableBytes) { // wait until the whole packet can be read this.packetLength = packetLength; break; } int newTotalLength = totalLength + packetLength; if (newTotalLength > OpenSslEngine.MAX_ENCRYPTED_PACKET_LENGTH) { // Don't read too much. break; } // We have a whole packet. // Increment the offset to handle the next packet. offset += packetLength; totalLength = newTotalLength; } if (totalLength > 0) { // The buffer contains one or more full SSL records. // Slice out the whole packet so unwrap will only be called with complete packets. // Also directly reset the packetLength. This is needed as unwrap(..) may trigger // decode(...) again via: // 1) unwrap(..) is called // 2) wrap(...) is called from within unwrap(...) // 3) wrap(...) calls unwrapLater(...) // 4) unwrapLater(...) calls decode(...) // // See https://github.com/netty/netty/issues/1534 in.skipBytes(totalLength); final ByteBuffer inNetBuf = in.nioBuffer(startOffset, totalLength); unwrap(ctx, inNetBuf, totalLength); assert !inNetBuf.hasRemaining() || engine.isInboundDone(); } if (nonSslRecord) { // Not an SSL/TLS packet NotSslRecordException e = new NotSslRecordException( "not an SSL/TLS record: " + ByteBufUtil.hexDump(in)); in.skipBytes(in.readableBytes()); ctx.fireExceptionCaught(e); setHandshakeFailure(e); } }
From source file:divconq.net.ssl.SslHandler.java
License:Apache License
private static SSLEngineResult unwrap(SSLEngine engine, ByteBuffer in, ByteBuf out) throws SSLException { int overflows = 0; for (;;) {// w w w .j a v a2s. c om ByteBuffer out0 = out.nioBuffer(out.writerIndex(), out.writableBytes()); SSLEngineResult result = engine.unwrap(in, out0); out.writerIndex(out.writerIndex() + result.bytesProduced()); switch (result.getStatus()) { case BUFFER_OVERFLOW: int max = engine.getSession().getApplicationBufferSize(); switch (overflows++) { case 0: out.ensureWritable(Math.min(max, in.remaining())); break; default: out.ensureWritable(max); } break; default: return result; } } }