Rev

Rev 1822 | Details | Compare with Previous | Last modification | View Log | SVN | Bug Tracker

Rev Author Line No. Line
1665 garciay 1
/**
2
 * @author      ETSI / STF481 / Yann Garcia
3
 * @version     $URL: file:///D:/RepositoriesNew/ITS/trunk/javasrc/codec/org/etsi/ttcn/codec/its/security/IntX.java $
4
 *              $Id: IntX.java 2655 2017-01-26 10:46:08Z filatov $
5
 */
1494 garciay 6
package org.etsi.ttcn.codec.its.security;
7
 
2655 filatov 8
import org.etsi.common.ByteHelper;
1494 garciay 9
import org.etsi.ttcn.codec.CodecBuffer;
10
import org.etsi.ttcn.codec.MainCodec;
11
import org.etsi.ttcn.codec.generic.Integer;
12
import org.etsi.ttcn.tci.IntegerValue;
13
import org.etsi.ttcn.tci.Type;
14
import org.etsi.ttcn.tci.Value;
15
 
1534 garciay 16
/**
17
 * @desc Codec for an integer of variable length
1789 garciay 18
 * @see Draft ETSI TS 103 097 V1.1.14 Clause 4.2.1   IntX
1534 garciay 19
 */
1494 garciay 20
public class IntX extends Integer {
1534 garciay 21
 
22
    /**
23
     * Specialised constructor
24
     * @param mainCodec The MainCodec reference
25
     */
1494 garciay 26
    public IntX(MainCodec mainCodec) {
27
        super(mainCodec);
28
    }
29
 
1534 garciay 30
    /**
31
     * @desc Decoder for an integer of variable length
32
     */
1494 garciay 33
    @Override
34
    public Value decode(CodecBuffer buf, Type decodingHypothesis) {
2655 filatov 35
//        TERFactory.getInstance().logDebug(">>> IntX.decode: " + decodingHypothesis.getName());
1534 garciay 36
 
1494 garciay 37
        // Read the first byte
38
        byte msb = buf.getBytes(0, 1)[0];
39
        if ((msb & 0x80) == 0x00) { // Integer < 128
40
            mainCodec.setHint("IntXLen", "8");
2655 filatov 41
            mainCodec.setHint("HeaderFieldContainer.its_aidLen", "8"); // TCT3 workaround, i don't know how to abstract this :-(
1494 garciay 42
            return super.decode(buf, decodingHypothesis);
43
        } else {
1789 garciay 44
            // Decode the length. The encoding of the length shall use at most 7 bits set to 1 (see Draft ETSI TS 103 097 V1.1.14 Clause 4.2.1    IntX)
1494 garciay 45
            byte bit;
46
            byte byteLen = 1;
47
            do {
48
                bit = (byte) ((byte) (msb << byteLen++) & 0x80);
49
            } while (bit != 0x00);
50
            // Set the IntX length
1737 garciay 51
            mainCodec.setHint(decodingHypothesis.getName() + "Len", String.valueOf(byteLen * Byte.SIZE ));
2655 filatov 52
            mainCodec.setHint("HeaderFieldContainer.its_aid" + "Len", String.valueOf(byteLen * Byte.SIZE )); // TCT3 workaround, i don't know how to abstract this :-(
1494 garciay 53
            // Remove the length from the real integer value
54
            byte[] newBuf = buf.getBytes();
55
            newBuf[0] &= (byte)(Math.pow(2.0, 8 - byteLen + 1) - 1);
56
            CodecBuffer newBuffer = new CodecBuffer(newBuf);
57
            buf.overwriteWith(newBuffer);
58
            // Decode it
59
            return super.decode(buf, decodingHypothesis);
60
        }
61
    }
62
 
1534 garciay 63
    /**
64
     * @desc Encoder for an integer of variable length
65
     */
1494 garciay 66
    @Override
67
    public CodecBuffer encode(Value value) {
2655 filatov 68
//        TERFactory.getInstance().logDebug(">>> IntX.encode: " + value.getType().getName());
1494 garciay 69
 
2655 filatov 70
        int iv = mainCodec.getTciCDRequired().getInteger((IntegerValue)value);
71
        byte[] result = TlsHelper.getInstance().size2tls(iv);
72
        mainCodec.setHint("integerLen", String.valueOf(result.length * Byte.SIZE ));
73
        IntegerValue newValue = mainCodec.getTciCDRequired().setInteger(ByteHelper.byteArrayToInt(result));
74
        return super.encode(newValue);
1494 garciay 75
    }
76
 
77
} // End of class IntX