CGI-SE-Trusted-Services / c2c-common

http://pvendil.github.io/c2c-common/
GNU Affero General Public License v3.0
45 stars 18 forks source link

COERInteger encode negative number error #8

Open Eivense opened 4 years ago

Eivense commented 4 years ago

Hi,pvendil @pvendil

l found when the value is negative (e.g. -10000),it will be encode to 0000D8F0. When decoding, it is calculated as 55536,-10000 need to be encoded to FFFFD8F0 When the value is negative,the buffer need to be filled with 0xff

private void serializeSigned(DataOutputStream out) throws IOException {
        byte[] val = value.toByteArray();

        int signOctet = 0;
        if(val[0] == 0x00 && val.length > 1){
            signOctet++;
        }

        if(isSignedAndBetween(NEGATIVE_TWO_PWR_63, TWO_PWR_63_MINUS_1)){
            byte[] buffer = new byte[getSignedBufferSize()];
                        // if value is negative buffer need to be filled with 0xff
            System.arraycopy(val, signOctet, buffer, buffer.length - (val.length -signOctet), val.length -signOctet);
            out.write(buffer);
        }else{
            COEREncodeHelper.writeLengthDeterminant(val.length-signOctet, out);
            out.write(val, signOctet, val.length -signOctet);
        }
    }
pvendil commented 4 years ago

Hi,

It seems you are right, I will look into this, hopefully in the coming week with a fix for this.

pvendil commented 4 years ago

Hi sorry for late reply, I have now update the code to encode COER integers properly. And it included in release 2.0.0-Beta4