blob: 0c80fbb1d7fcfeb25a4f5f6931e2b6e8653e3112 [file] [log] [blame]
slowr60d4d102017-08-16 18:33:58 -07001/* 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 Mavrommatis96b255a2017-12-06 13:09:25 -08004package org.onosproject.xran.asn1lib.ber.types;
slowr60d4d102017-08-16 18:33:58 -07005
6import com.fasterxml.jackson.annotation.JsonIgnore;
7import com.fasterxml.jackson.annotation.JsonValue;
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -08008import org.onosproject.xran.asn1lib.ber.BerByteArrayOutputStream;
9import org.onosproject.xran.asn1lib.ber.BerLength;
10import org.onosproject.xran.asn1lib.ber.BerTag;
11import org.onosproject.xran.asn1lib.ber.internal.Util;
slowr60d4d102017-08-16 18:33:58 -070012
13import java.io.IOException;
14import java.io.InputStream;
15import java.io.Serializable;
16import java.math.BigInteger;
17
18public 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 Mavrommatis96b255a2017-12-06 13:09:25 -080022
23 @JsonIgnore public byte[] code = null;
slowr60d4d102017-08-16 18:33:58 -070024
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}