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/. */ |
| 4 | package org.onosproject.xran.codecs.ber; |
| 5 | |
| 6 | import java.io.EOFException; |
| 7 | import java.io.IOException; |
| 8 | import java.io.InputStream; |
| 9 | import java.io.Serializable; |
| 10 | |
| 11 | public class BerLength implements Serializable { |
| 12 | |
| 13 | private static final long serialVersionUID = 1L; |
| 14 | |
| 15 | public int val; |
| 16 | |
| 17 | public BerLength() { |
| 18 | } |
| 19 | |
| 20 | public static int skip(InputStream is) throws IOException { |
| 21 | |
| 22 | int val = is.read(); |
| 23 | if (val == -1) { |
| 24 | throw new EOFException("Unexpected end of input stream."); |
| 25 | } |
| 26 | |
| 27 | if ((val & 0x80) == 0) { |
| 28 | return 1; |
| 29 | } |
| 30 | |
| 31 | int lengthLength = val & 0x7f; |
| 32 | |
| 33 | // check for indefinite length |
| 34 | if (lengthLength == 0) { |
| 35 | val = -1; |
| 36 | return 1; |
| 37 | } |
| 38 | |
| 39 | if (lengthLength > 4) { |
| 40 | throw new IOException("Length is out of bound!"); |
| 41 | } |
| 42 | |
| 43 | for (int i = 0; i < lengthLength; i++) { |
| 44 | int nextByte = is.read(); |
| 45 | if (nextByte == -1) { |
| 46 | throw new EOFException("Unexpected end of input stream."); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return lengthLength + 1; |
| 51 | } |
| 52 | |
| 53 | public static int encodeLength(BerByteArrayOutputStream os, int length) throws IOException { |
| 54 | // the indefinite form is ignored for now |
| 55 | |
| 56 | if (length <= 127) { |
| 57 | // this is the short form, it is coded differently than the long |
| 58 | // form for values > 127 |
| 59 | os.write((byte) length); |
| 60 | return 1; |
| 61 | } else { |
| 62 | int numLengthBytes = 1; |
| 63 | |
| 64 | while (((int) (Math.pow(2, 8 * numLengthBytes) - 1)) < length) { |
| 65 | numLengthBytes++; |
| 66 | } |
| 67 | |
| 68 | for (int i = 0; i < numLengthBytes; i++) { |
| 69 | os.write((length >> 8 * i) & 0xff); |
| 70 | } |
| 71 | |
| 72 | os.write(0x80 | numLengthBytes); |
| 73 | |
| 74 | return 1 + numLengthBytes; |
| 75 | } |
| 76 | |
| 77 | } |
| 78 | |
| 79 | public int decode(InputStream is) throws IOException { |
| 80 | |
| 81 | val = is.read(); |
| 82 | if (val == -1) { |
| 83 | throw new EOFException("Unexpected end of input stream."); |
| 84 | } |
| 85 | |
| 86 | if ((val & 0x80) == 0) { |
| 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | int lengthLength = val & 0x7f; |
| 91 | |
| 92 | // check for indefinite length |
| 93 | if (lengthLength == 0) { |
| 94 | val = -1; |
| 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | if (lengthLength > 4) { |
| 99 | throw new IOException("Length is out of bound!"); |
| 100 | } |
| 101 | |
| 102 | val = 0; |
| 103 | |
| 104 | for (int i = 0; i < lengthLength; i++) { |
| 105 | int nextByte = is.read(); |
| 106 | if (nextByte == -1) { |
| 107 | throw new EOFException("Unexpected end of input stream."); |
| 108 | } |
| 109 | val |= nextByte << (8 * (lengthLength - i - 1)); |
| 110 | } |
| 111 | |
| 112 | return lengthLength + 1; |
| 113 | } |
| 114 | |
| 115 | } |