Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoder updates ascii #57

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 0 additions & 32 deletions src/main/java/com/exactpro/epfast/decoder/IDecodeContext.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2019-2020 Exactpro (Exactpro Systems Limited)
*
* 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 com.exactpro.epfast.decoder;

import com.exactpro.epfast.decoder.message.DecoderCommand;
import com.exactpro.epfast.decoder.message.DecoderState;
import com.exactpro.epfast.decoder.message.UnionRegister;
import io.netty.buffer.ByteBuf;

public abstract class StreamDecoderCommand implements DecoderCommand {

public static final int FINISHED = 1;

public static final int MORE_DATA_NEEDED = 0;

protected static final int CLEAR_STOP_BIT_MASK = 0b01111111;

protected abstract int decode(ByteBuf buf, UnionRegister register);

@Override
public int executeOn(DecoderState decoderState) {
if (!decoderState.inputBuffer.isReadable()) {
decoderState.canProceed = false;
return MORE_DATA_NEEDED;
}
return decode(decoderState.inputBuffer, decoderState.register);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@

package com.exactpro.epfast.decoder.ascii;

import com.exactpro.epfast.decoder.IDecodeContext;
import com.exactpro.epfast.decoder.OverflowException;
import com.exactpro.epfast.decoder.StreamDecoderCommand;
import com.exactpro.epfast.decoder.message.UnionRegister;
import io.netty.buffer.ByteBuf;

public abstract class DecodeAsciiString implements IDecodeContext {
public abstract class DecodeAsciiString extends StreamDecoderCommand {

final StringBuilder stringBuilder = new StringBuilder();

private boolean ready;

boolean zeroPreamble;
boolean decodingPreamble = true;

final boolean checkOverlong;

Expand All @@ -38,56 +38,57 @@ public abstract class DecodeAsciiString implements IDecodeContext {
this.checkOverlong = checkOverlong;
}

public void decode(ByteBuf buf) {
reset();
@Override
public int decode(ByteBuf buf, UnionRegister register) {
int readerIndex = buf.readerIndex();
int readLimit = buf.writerIndex();
if (buf.getByte(readerIndex) == 0) {
zeroPreamble = true;
while (decodingPreamble && !ready && (readerIndex < readLimit)) {
accumulateValue(getPreambleByte(readerIndex++, buf));
}
accumulateValue(buf.getByte(readerIndex++));
while ((readerIndex < readLimit) && !ready) {
accumulateValue(buf.getByte(readerIndex++));
while (!ready && (readerIndex < readLimit)) {
accumulateValue(getByte(readerIndex++, buf));
}
buf.readerIndex(readerIndex);
}

public void continueDecode(ByteBuf buf) {
int readerIndex = buf.readerIndex();
int readLimit = buf.writerIndex();
while ((readerIndex < readLimit) && !ready) {
accumulateValue(buf.getByte(readerIndex++));
if (ready) {
setResult(register);
reset();
return FINISHED;
} else {
return MORE_DATA_NEEDED;
}
buf.readerIndex(readerIndex);
}

public abstract String getValue() throws OverflowException;
public abstract void setResult(UnionRegister unionRegister);

public boolean isReady() {
return ready;
}

public boolean isOverlong() {
return (zeroPreamble && (zeroCount < stringBuilder.length()));
private int getPreambleByte(int index, ByteBuf buf) {
int aByte = getByte(index, buf);
if (aByte == 0) {
++zeroCount;
} else {
decodingPreamble = false;
}
return aByte;
}

private void accumulateValue(int oneByte) {
if (oneByte < 0) { // if stop bit is set
oneByte &= CLEAR_STOP_BIT_MASK;
private int getByte(int index, ByteBuf buf) {
int aByte = buf.getByte(index);
if (aByte < 0) { // if stop bit is set
aByte &= CLEAR_STOP_BIT_MASK;
ready = true;
}
if (oneByte == 0) {
zeroCount++;
}
return aByte;
}

private void accumulateValue(int aByte) {
if (stringBuilder.length() < MAX_ALLOWED_LENGTH) {
stringBuilder.append((char) oneByte);
stringBuilder.append((char) aByte);
}
}

public final void reset() {
stringBuilder.setLength(0);
ready = false;
decodingPreamble = true;
zeroCount = 0;
zeroPreamble = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,44 @@

package com.exactpro.epfast.decoder.ascii;

import com.exactpro.epfast.decoder.OverflowException;
import com.exactpro.epfast.decoder.message.UnionRegister;

public final class DecodeMandatoryAsciiString extends DecodeAsciiString {

public DecodeMandatoryAsciiString() {
public DecodeMandatoryAsciiString() {
this(false);
}

public DecodeMandatoryAsciiString(boolean checkOverlong) {
public DecodeMandatoryAsciiString(boolean checkOverlong) {
super(checkOverlong);
}

public String getValue() throws OverflowException {
@Override
public void setResult(UnionRegister register) {
if (stringBuilder.length() >= MAX_ALLOWED_LENGTH) {
throw new OverflowException("String is longer than allowed");
}
if (zeroCount < stringBuilder.length()) {
if (zeroPreamble && checkOverlong) {
throw new OverflowException("String with zero preamble can't contain any value except 0");
register.isOverflow = true;
register.isOverlong = false;
register.isNull = false;
register.infoMessage = "String is longer than allowed";
} else if (zeroCount < stringBuilder.length()) {
register.isNull = false;
register.isOverflow = false;
register.isOverlong = false;
if ((zeroCount > 1) && checkOverlong) {
register.stringValue = stringBuilder.substring(1);
} else if (zeroCount == 1) {
register.isOverlong = true;
register.infoMessage = "String is overlong if first 7 bits after zero preamble are not \\0";
register.stringValue = stringBuilder.substring(1);
} else {
return stringBuilder.toString();
register.stringValue = stringBuilder.toString();
}
} else if (zeroCount == 1) {
return "";
} else {
stringBuilder.setLength(zeroCount - 1);
return stringBuilder.toString();
register.isOverflow = false;
register.isOverlong = false;
register.isNull = false;
register.stringValue = stringBuilder.toString();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.exactpro.epfast.decoder.ascii;

import com.exactpro.epfast.decoder.OverflowException;
import com.exactpro.epfast.decoder.message.UnionRegister;

public final class DecodeNullableAsciiString extends DecodeAsciiString {

Expand All @@ -28,23 +28,41 @@ public DecodeNullableAsciiString(boolean checkOverlong) {
super(checkOverlong);
}

public String getValue() throws OverflowException {
@Override
public void setResult(UnionRegister register) {
if (stringBuilder.length() >= MAX_ALLOWED_LENGTH) {
throw new OverflowException("String is longer than allowed");
}
if (zeroCount < stringBuilder.length()) {
if (zeroPreamble && checkOverlong) {
throw new OverflowException("String with zero preamble can't contain any value except 0");
register.isOverflow = true;
register.isOverlong = false;
register.isNull = false;
register.infoMessage = "String is longer than allowed";
} else if (zeroCount < stringBuilder.length()) {
register.isNull = false;
register.isOverflow = false;
register.isOverlong = false;
if ((zeroCount > 2) && checkOverlong) {
register.stringValue = stringBuilder.substring(2);
} else if ((zeroCount == 1) && checkOverlong) {
register.isOverlong = true;
register.infoMessage = "String is overlong if first 7 bits after zero preamble are not \\0";
register.stringValue = stringBuilder.substring(1);
} else if ((zeroCount == 2) && checkOverlong) {
register.isOverlong = true;
register.infoMessage = "String is overlong if first 7 bits after zero preamble are not \\0";
register.stringValue = stringBuilder.substring(2);
} else {
return stringBuilder.toString();
register.stringValue = stringBuilder.toString();
}
} else if (zeroCount == 1) {
return null;
} else if (zeroCount == 2) {
return "";
register.isOverflow = false;
register.isNull = true;
register.isOverlong = false;
register.stringValue = null;
} else {
stringBuilder.setLength(zeroCount - 2);
return stringBuilder.toString();
register.isOverflow = false;
register.isOverlong = false;
register.isNull = false;
register.stringValue = stringBuilder.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,36 @@

package com.exactpro.epfast.decoder.decimal;

import com.exactpro.epfast.decoder.IDecodeContext;
import com.exactpro.epfast.decoder.OverflowException;
import com.exactpro.epfast.decoder.StreamDecoderCommand;
import com.exactpro.epfast.decoder.integer.DecodeMandatoryInt64;
import io.netty.buffer.ByteBuf;
import com.exactpro.epfast.decoder.message.UnionRegister;

import java.math.BigDecimal;

public abstract class DecodeDecimal implements IDecodeContext {
public abstract class DecodeDecimal extends StreamDecoderCommand {

DecodeMandatoryInt64 mantissaDecoder = new DecodeMandatoryInt64();

long mantissa;

boolean exponentReady;

boolean startedMantissa;

boolean ready;

boolean exponentOverflow;

boolean mantissaOverflow;

public abstract void decode(ByteBuf buf);
boolean exponentOverlong;

public abstract void continueDecode(ByteBuf buf);
boolean mantissaOverlong;

public final void reset() {
exponentReady = false;
startedMantissa = false;
ready = false;
exponentOverflow = false;
mantissaOverflow = false;
exponentOverlong = false;
mantissaOverlong = false;
}

public abstract BigDecimal getValue() throws OverflowException;

public boolean isReady() {
return ready;
}

public abstract boolean isOverlong();
public abstract void setResult(UnionRegister register);
}
Loading