slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame^] | 4 | package org.onosproject.xran.asn1lib.ber.types; |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 5 | |
| 6 | import com.fasterxml.jackson.annotation.JsonIgnore; |
| 7 | import com.fasterxml.jackson.annotation.JsonValue; |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame^] | 8 | import org.onosproject.xran.asn1lib.ber.BerByteArrayOutputStream; |
| 9 | import org.onosproject.xran.asn1lib.ber.BerLength; |
| 10 | import org.onosproject.xran.asn1lib.ber.BerTag; |
| 11 | import org.onosproject.xran.asn1lib.ber.internal.Util; |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 12 | |
| 13 | import java.io.IOException; |
| 14 | import java.io.InputStream; |
| 15 | import java.io.Serializable; |
| 16 | import java.math.BigInteger; |
| 17 | |
| 18 | public class BerInteger implements Serializable { |
| 19 | |
| 20 | public final static BerTag tag = new BerTag(BerTag.UNIVERSAL_CLASS, BerTag.PRIMITIVE, BerTag.INTEGER_TAG); |
| 21 | private static final long serialVersionUID = 1L; |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame^] | 22 | |
| 23 | @JsonIgnore public byte[] code = null; |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 24 | |
| 25 | public BigInteger value; |
| 26 | |
| 27 | public BerInteger() { |
| 28 | } |
| 29 | |
| 30 | public BerInteger(byte[] code) { |
| 31 | this.code = code; |
| 32 | } |
| 33 | |
| 34 | public BerInteger(BigInteger val) { |
| 35 | this.value = val; |
| 36 | } |
| 37 | |
| 38 | public BerInteger(long val) { |
| 39 | this.value = BigInteger.valueOf(val); |
| 40 | } |
| 41 | |
| 42 | public int encode(BerByteArrayOutputStream os) throws IOException { |
| 43 | return encode(os, true); |
| 44 | } |
| 45 | |
| 46 | public int encode(BerByteArrayOutputStream os, boolean withTag) throws IOException { |
| 47 | |
| 48 | if (code != null) { |
| 49 | for (int i = code.length - 1; i >= 0; i--) { |
| 50 | os.write(code[i]); |
| 51 | } |
| 52 | if (withTag) { |
| 53 | return tag.encode(os) + code.length; |
| 54 | } |
| 55 | return code.length; |
| 56 | } |
| 57 | |
| 58 | byte[] encoded = value.toByteArray(); |
| 59 | int codeLength = encoded.length; |
| 60 | os.write(encoded); |
| 61 | |
| 62 | codeLength += BerLength.encodeLength(os, codeLength); |
| 63 | |
| 64 | if (withTag) { |
| 65 | codeLength += tag.encode(os); |
| 66 | } |
| 67 | |
| 68 | return codeLength; |
| 69 | } |
| 70 | |
| 71 | public int decode(InputStream is) throws IOException { |
| 72 | return decode(is, true); |
| 73 | } |
| 74 | |
| 75 | public int decode(InputStream is, boolean withTag) throws IOException { |
| 76 | |
| 77 | int codeLength = 0; |
| 78 | |
| 79 | if (withTag) { |
| 80 | codeLength += tag.decodeAndCheck(is); |
| 81 | } |
| 82 | |
| 83 | BerLength length = new BerLength(); |
| 84 | codeLength += length.decode(is); |
| 85 | |
| 86 | if (length.val < 1) { |
| 87 | throw new IOException("Decoded length of BerInteger is not correct"); |
| 88 | } |
| 89 | |
| 90 | byte[] byteCode = new byte[length.val]; |
| 91 | Util.readFully(is, byteCode); |
| 92 | |
| 93 | codeLength += length.val; |
| 94 | |
| 95 | value = new BigInteger(byteCode); |
| 96 | |
| 97 | return codeLength; |
| 98 | } |
| 99 | |
| 100 | public void encodeAndSave(int encodingSizeGuess) throws IOException { |
| 101 | BerByteArrayOutputStream os = new BerByteArrayOutputStream(encodingSizeGuess); |
| 102 | encode(os, false); |
| 103 | code = os.getArray(); |
| 104 | } |
| 105 | |
| 106 | @JsonValue |
| 107 | @Override |
| 108 | public String toString() { |
| 109 | return "" + value; |
| 110 | } |
| 111 | |
| 112 | public byte byteValue() { |
| 113 | return value.byteValue(); |
| 114 | } |
| 115 | |
| 116 | public short shortValue() { |
| 117 | return value.shortValue(); |
| 118 | } |
| 119 | |
| 120 | public int intValue() { |
| 121 | return value.intValue(); |
| 122 | } |
| 123 | |
| 124 | public long longValue() { |
| 125 | return value.longValue(); |
| 126 | } |
| 127 | |
| 128 | } |