Example usage for io.netty.buffer ByteBuf skipBytes

List of usage examples for io.netty.buffer ByteBuf skipBytes

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf skipBytes.

Prototype

public abstract ByteBuf skipBytes(int length);

Source Link

Document

Increases the current readerIndex by the specified length in this buffer.

Usage

From source file:com.yahoo.pulsar.client.impl.ProducerImpl.java

License:Apache License

/**
 * Computes checksum again and verifies it against existing checksum. If checksum doesn't match it means that
 * message is corrupt.//from w ww  . j a  va  2 s .  c  o  m
 * 
 * @param op
 * @return returns true only if message is not modified and computed-checksum is same as previous checksum else
 *         return false that means that message is corrupted. Returns true if checksum is not present.
 */
protected boolean verifyLocalBufferIsNotCorrupted(OpSendMsg op) {
    DoubleByteBuf msg = getDoubleByteBuf(op.cmd);

    if (msg != null) {
        ByteBuf headerFrame = msg.getFirst();
        msg.markReaderIndex();
        headerFrame.markReaderIndex();
        try {
            // skip bytes up to checksum index
            headerFrame.skipBytes(4); // skip [total-size]
            int cmdSize = (int) headerFrame.readUnsignedInt();
            headerFrame.skipBytes(cmdSize);
            // verify if checksum present
            if (hasChecksum(headerFrame)) {
                int checksum = readChecksum(headerFrame).intValue();
                // msg.readerIndex is already at header-payload index, Recompute checksum for headers-payload
                int metadataChecksum = computeChecksum(headerFrame);
                long computedChecksum = resumeChecksum(metadataChecksum, msg.getSecond());
                return checksum == computedChecksum;
            } else {
                log.warn("[{}] [{}] checksum is not present into message with id {}", topic, producerName,
                        op.sequenceId);
            }
        } finally {
            headerFrame.resetReaderIndex();
            msg.resetReaderIndex();
        }
        return true;
    } else {
        log.warn("[{}] Failed while casting {} into DoubleByteBuf", producerName, op.cmd.getClass().getName());
        return false;
    }
}

From source file:com.yahoo.pulsar.client.impl.ProducerImpl.java

License:Apache License

/**
 * Strips checksum from {@link OpSendMsg} command if present else ignore it.   
 * //from   w  w  w .  j ava  2 s . c o m
 * @param op
 */
private void stripChecksum(OpSendMsg op) {
    op.cmd.markReaderIndex();
    int totalMsgBufSize = op.cmd.readableBytes();
    DoubleByteBuf msg = getDoubleByteBuf(op.cmd);
    if (msg != null) {
        ByteBuf headerFrame = msg.getFirst();
        msg.markReaderIndex();
        headerFrame.markReaderIndex();
        try {
            headerFrame.skipBytes(4); // skip [total-size]
            int cmdSize = (int) headerFrame.readUnsignedInt();

            // verify if checksum present
            headerFrame.skipBytes(cmdSize);

            if (!hasChecksum(headerFrame)) {
                headerFrame.resetReaderIndex();
                return;
            }

            int headerSize = 4 + 4 + cmdSize; // [total-size] [cmd-length] [cmd-size]
            int checksumSize = 4 + 2; // [magic-number] [checksum-size]
            int checksumMark = (headerSize + checksumSize); // [header-size] [checksum-size]
            int metaPayloadSize = (totalMsgBufSize - checksumMark); // metadataPayload = totalSize - checksumMark
            int newTotalFrameSizeLength = 4 + cmdSize + metaPayloadSize; // new total-size without checksum
            headerFrame.resetReaderIndex();
            int headerFrameSize = headerFrame.readableBytes();

            headerFrame.setInt(0, newTotalFrameSizeLength); // rewrite new [total-size]
            ByteBuf metadata = headerFrame.slice(checksumMark, headerFrameSize - checksumMark); // sliced only
                                                                                                // metadata
            headerFrame.writerIndex(headerSize); // set headerFrame write-index to overwrite metadata over checksum
            metadata.readBytes(headerFrame, metadata.readableBytes());
            headerFrame.capacity(headerFrameSize - checksumSize); // reduce capacity by removed checksum bytes
            headerFrame.resetReaderIndex();

        } finally {
            op.cmd.resetReaderIndex();
        }
    } else {
        log.warn("[{}] Failed while casting {} into DoubleByteBuf", producerName, op.cmd.getClass().getName());
    }
}

From source file:com.yahoo.pulsar.common.api.Commands.java

License:Apache License

public static Long readChecksum(ByteBuf buffer) {
    if (hasChecksum(buffer)) {
        buffer.skipBytes(2); //skip magic bytes
        return buffer.readUnsignedInt();
    } else {//from   w  w w .  ja v a2  s. c  o m
        return null;
    }
}

From source file:com.yahoo.pulsar.common.compression.CommandsTest.java

License:Apache License

@Test
public void testChecksumSendCommand() throws Exception {

    // test checksum in send command
    String producerName = "prod-name";
    int sequenceId = 0;
    ByteBuf data = Unpooled.buffer(1024);
    MessageMetadata messageMetadata = MessageMetadata.newBuilder().setPublishTime(System.currentTimeMillis())
            .setProducerName(producerName).setSequenceId(sequenceId).build();
    int expectedChecksum = computeChecksum(messageMetadata, data);
    ByteBuf clientCommand = Commands.newSend(1, 0, 1, ChecksumType.Crc32c, messageMetadata, data);
    clientCommand.retain();/*from  w w w .j  a  v a 2  s.c  om*/
    ByteBuffer inputBytes = clientCommand.nioBuffer();
    ByteBuf receivedBuf = Unpooled.wrappedBuffer(inputBytes);
    receivedBuf.skipBytes(4); //skip [total-size]
    int cmdSize = (int) receivedBuf.readUnsignedInt();
    receivedBuf.readerIndex(8 + cmdSize);
    int startMessagePos = receivedBuf.readerIndex();

    /*** 1. verify checksum and metadataParsing ***/
    boolean hasChecksum = Commands.hasChecksum(receivedBuf);
    int checksum = Commands.readChecksum(receivedBuf).intValue();

    // verify checksum is present
    assertTrue(hasChecksum);
    // verify checksum value
    assertEquals(expectedChecksum, checksum);
    MessageMetadata metadata = Commands.parseMessageMetadata(receivedBuf);
    // verify metadata parsing
    assertEquals(metadata.getProducerName(), producerName);

    /** 2. parseMessageMetadata should skip checksum if present **/
    receivedBuf.readerIndex(startMessagePos);
    metadata = Commands.parseMessageMetadata(receivedBuf);
    // verify metadata parsing
    assertEquals(metadata.getProducerName(), producerName);

}

From source file:com.zanclus.ssh.transport.SSHFrameDecoder.java

License:Apache License

/**
 * Given an input buffer, payload length, and padding length; parse the input buffer bytes to
 * extract the payload data/*from w w  w. jav a2  s. c  o  m*/
 * @param payloadLen The number of bytes which are expected in the encoded payload
 * @param in The input {@link ByteBuf}
 * @param paddingLen The expected length of the random padding bytes
 * @return An array of {@code byte}s containing the decrypted payload
 * @throws IllegalAccessException Thrown by the {@link SSHFrameDecoder#calculateMAC(byte[])} method
 * @throws InvalidCipherTextException Thrown by the {@link SSHFrameDecoder#decryptPayload(byte[])} method 
 * @throws InstantiationException Thrown by the {@link SSHFrameDecoder#calculateMAC(byte[])} method
 * @throws InvalidMACException Thrown by the {@link SSHFrameDecoder#validateMAC(byte[], byte[])} method
 */
private byte[] decodePayload(int payloadLen, ByteBuf in, int paddingLen)
        throws IllegalAccessException, InvalidCipherTextException, InstantiationException, InvalidMACException {
    // Read the payload
    byte[] payload = new byte[payloadLen];
    in.readBytes(payload);

    byte[] plaintext = decryptPayload(payload);

    // Skip the random padding
    in.skipBytes(paddingLen);

    byte[] computedMAC = calculateMAC(plaintext);

    // Read MAC from ByteBuf
    byte[] recievedMAC = new byte[algorithm.digestLen()];
    in.readBytes(recievedMAC);

    validateMAC(recievedMAC, computedMAC);
    LOG.debug("Message Packet decoded and MAC verified.");
    return plaintext;
}

From source file:cyril.server.io.AuthHandler.java

License:Open Source License

private boolean handleIncomingDeclined(ChannelHandlerContext ctx, ByteBuf in) {
    boolean goOn;
    int tokenStuff = Math.min(login.token.length - index, in.readableBytes());
    in.skipBytes(tokenStuff);
    index += tokenStuff;//ww  w. j ava  2 s .  c o m
    goOn = false;
    // Don't abort halfway, that might be exploited.
    if (index >= login.token.length) {
        readerAborts(ctx);
    }
    return goOn;
}

From source file:discord4j.voice.VoiceSocket.java

License:Open Source License

private static String getNullTerminatedString(ByteBuf buffer, int offset) {
    buffer.skipBytes(offset);
    ByteArrayOutputStream os = new ByteArrayOutputStream(15);
    byte c;/*w ww. j  a  va  2s.  c  o  m*/
    while ((c = buffer.readByte()) != 0) {
        os.write(c);
    }

    return new String(os.toByteArray(), StandardCharsets.US_ASCII);
}

From source file:divconq.ctp.stream.FunnelStream.java

License:Open Source License

public ReturnOption nextMessage() {
    FileDescriptor curr = this.current;

    if (curr == null)
        return ReturnOption.CONTINUE;

    FileDescriptor blk = new FileDescriptor();
    blk.copyAttributes(curr);/*w w  w . jav  a2  s.c o  m*/

    ByteBuf payload = this.currbuf;

    if ((payload != null) && payload.isReadable()) {
        int ramt = Math.min(this.aperture, payload.readableBytes());

        ByteBuf pslice = payload.copy(payload.readerIndex(), ramt);

        payload.skipBytes(ramt);

        // TODO blk.payloadoffset = 0;         

        blk.setEof(!payload.isReadable() && curr.isEof());

        if (blk.isEof()) {
            payload.release();

            this.current = null;
            this.currbuf = null;
        }

        payload = pslice;
    } else {
        blk.setEof(curr.isEof());

        if (payload != null)
            payload.release();

        payload = null;
        this.current = null;
        this.currbuf = null;
    }

    // current has been sent at least once
    this.relayed = true;

    return this.downstream.handle(blk, payload);
}

From source file:divconq.ctp.stream.SplitStream.java

License:Open Source License

@Override
public ReturnOption handle(FileDescriptor file, ByteBuf data) {
    if (file == FileDescriptor.FINAL)
        return this.downstream.handle(file, data);

    ByteBuf in = data;

    if (in != null) {
        while (in.isReadable()) {
            int amt = Math.min(in.readableBytes(), this.size - this.currchunk);

            ByteBuf out = in.copy(in.readerIndex(), amt);

            in.skipBytes(amt);
            this.currchunk += amt;

            boolean eof = (this.currchunk == this.size) || (!in.isReadable() && file.isEof());

            this.nextMessage(out, file, eof);

            if (eof) {
                this.seqnum++;
                this.currchunk = 0;
            }//from   www .j a v a 2 s .  c  o m
        }

        in.release();
    } else if (file.isEof()) {
        this.nextMessage(null, file, false);
    }

    // write all messages in the queue
    while (this.outlist.size() > 0) {
        ReturnOption ret = this.downstream.handle(this.outlist.remove(0), this.outbuf.remove(0));

        if (ret != ReturnOption.CONTINUE)
            return ret;
    }

    return ReturnOption.CONTINUE;
}

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;
        }//w ww.ja  v  a2 s .co  m

        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;
    }
}