2 * Copyright (c) 2013, 2017 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowplugin.libraries.liblldp;
10 import java.util.Arrays;
11 import javax.annotation.Nonnull;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
16 * BitBufferHelper class that provides utility methods to - fetch specific bits
17 * from a serialized stream of bits - convert bits to primitive data type - like
18 * short, int, long - store bits in specified location in stream of bits -
19 * convert primitive data types to stream of bits.
21 public abstract class BitBufferHelper {
22 private static final Logger LOG = LoggerFactory.getLogger(BitBufferHelper.class);
24 public static final long BYTE_MASK = 0xFF;
27 // data: array where data are stored
28 // startOffset: bit from where to start reading
29 // numBits: number of bits to read
30 // All this function return an exception if overflow or underflow
33 * Returns the first byte from the byte array.
37 public static byte getByte(final byte[] data) {
38 if (data.length * NetUtils.NUM_BITS_IN_A_BYTE > Byte.SIZE) {
39 LOG.error("getByte", new BufferException("Container is too small for the number of requested bits"));
45 * Returns the short value for the byte array passed. Size of byte array is
46 * restricted to Short.SIZE
50 public static short getShort(final byte[] data) {
51 if (data.length > Short.SIZE) {
52 LOG.error("getShort", new BufferException("Container is too small for the number of requested bits"));
54 return (short) toNumber(data);
58 * Returns the short value for the last numBits of the byte array passed.
59 * Size of numBits is restricted to Short.SIZE
61 * @return short - the short value of byte array
63 public static short getShort(final byte[] data, final int numBits) {
64 if (numBits > Short.SIZE) {
65 LOG.error("getShort", new BufferException("Container is too small for the number of requested bits"));
67 int startOffset = data.length * NetUtils.NUM_BITS_IN_A_BYTE - numBits;
69 byte[] bits = BitBufferHelper.getBits(data, startOffset, numBits);
70 return (short) toNumber(bits, numBits);
71 } catch (final BufferException e) {
72 LOG.error("getBits failed", e);
78 * Returns the int value for the byte array passed. Size of byte array is
79 * restricted to Integer.SIZE
81 * @return int - the integer value of byte array
83 public static int getInt(final byte[] data) {
84 if (data.length > Integer.SIZE) {
85 LOG.error("getInt", new BufferException("Container is too small for the number of requested bits"));
87 return (int) toNumber(data);
91 * Returns the int value for the last numBits of the byte array passed. Size
92 * of numBits is restricted to Integer.SIZE
94 * @return int - the integer value of byte array
96 public static int getInt(final byte[] data, final int numBits) {
97 if (numBits > Integer.SIZE) {
98 LOG.error("getInt", new BufferException("Container is too small for the number of requested bits"));
100 int startOffset = data.length * NetUtils.NUM_BITS_IN_A_BYTE - numBits;
102 byte[] bits = BitBufferHelper.getBits(data, startOffset, numBits);
103 return (int) toNumber(bits, numBits);
104 } catch (final BufferException e) {
105 LOG.error("getBits failed", e);
111 * Returns the long value for the byte array passed. Size of byte array is
112 * restricted to Long.SIZE
114 * @return long - the integer value of byte array
116 public static long getLong(final byte[] data) {
117 if (data.length > Long.SIZE) {
118 LOG.error("getLong", new BufferException("Container is too small for the number of requested bits"));
120 return toNumber(data);
124 * Returns the long value for the last numBits of the byte array passed.
125 * Size of numBits is restricted to Long.SIZE
127 * @return long - the integer value of byte array
129 public static long getLong(final byte[] data, final int numBits) {
130 if (numBits > Long.SIZE) {
131 LOG.error("getLong", new BufferException("Container is too small for the number of requested bits"));
133 if (numBits > data.length * NetUtils.NUM_BITS_IN_A_BYTE) {
135 throw new BufferException("Trying to read more bits than contained in the data buffer");
136 } catch (final BufferException e) {
140 int startOffset = data.length * NetUtils.NUM_BITS_IN_A_BYTE - numBits;
142 byte[] bits = BitBufferHelper.getBits(data, startOffset, numBits);
143 return toNumber(bits, numBits);
144 } catch (final BufferException e) {
145 LOG.error("getBits failed", e);
151 * Reads the specified number of bits from the passed byte array starting to
152 * read from the specified offset The bits read are stored in a byte array
153 * which size is dictated by the number of bits to be stored. The bits are
154 * stored in the byte array LSB aligned.
157 * Ex. Read 7 bits at offset 10 0 9 10 16 17 0101000010 | 0000101 |
158 * 1111001010010101011 will be returned as {0,0,0,0,0,1,0,1}
161 * - offset to start fetching bits from data from
163 * - number of bits to be fetched from data
164 * @return byte [] - LSB aligned bits
166 * @throws BufferException
167 * when the startOffset and numBits parameters are not congruent
168 * with the data buffer size
171 public static byte[] getBits(final byte[] data, final int startOffset, final int numBits) throws BufferException {
173 int extranumBits = numBits % NetUtils.NUM_BITS_IN_A_BYTE;
174 final int extraOffsetBits = startOffset % NetUtils.NUM_BITS_IN_A_BYTE;
175 int numBytes = numBits % NetUtils.NUM_BITS_IN_A_BYTE != 0 ? 1 + numBits / NetUtils.NUM_BITS_IN_A_BYTE
176 : numBits / NetUtils.NUM_BITS_IN_A_BYTE;
177 startByteOffset = startOffset / NetUtils.NUM_BITS_IN_A_BYTE;
178 byte[] bytes = new byte[numBytes];
183 checkExceptions(data, startOffset, numBits);
185 if (extraOffsetBits == 0) {
186 if (extranumBits == 0) {
187 System.arraycopy(data, startByteOffset, bytes, 0, numBytes);
190 System.arraycopy(data, startByteOffset, bytes, 0, numBytes - 1);
191 bytes[numBytes - 1] = (byte) (data[startByteOffset + numBytes - 1] & getMSBMask(extranumBits));
197 for (index = 0; index < numBits / NetUtils.NUM_BITS_IN_A_BYTE; index++) {
198 // Reading numBytes starting from offset
199 valfromcurr = data[startByteOffset + index] & getLSBMask(NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits);
200 valfromnext = data[startByteOffset + index + 1] & getMSBMask(extraOffsetBits);
201 bytes[index] = (byte) (valfromcurr << extraOffsetBits
202 | valfromnext >> NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits);
204 // Now adding the rest of the bits if any
205 if (extranumBits != 0) {
206 if (extranumBits < NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits) {
207 valfromnext = (byte) (data[startByteOffset + index] & getMSBMask(extranumBits) >> extraOffsetBits);
208 bytes[index] = (byte) (valfromnext << extraOffsetBits);
209 } else if (extranumBits == NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits) {
210 valfromcurr = data[startByteOffset + index]
211 & getLSBMask(NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits);
212 bytes[index] = (byte) (valfromcurr << extraOffsetBits);
214 valfromcurr = data[startByteOffset + index]
215 & getLSBMask(NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits);
216 valfromnext = data[startByteOffset + index + 1]
217 & getMSBMask(extranumBits - (NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits));
218 bytes[index] = (byte) (valfromcurr << extraOffsetBits
219 | valfromnext >> NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits);
224 // Aligns the bits to LSB
225 return shiftBitsToLSB(bytes, numBits);
229 // data: array where data will be stored
230 // input: the data that need to be stored in the data array
231 // startOffset: bit from where to start writing
232 // numBits: number of bits to read
235 * Bits are expected to be stored in the input byte array from LSB.
238 * to set the input byte
240 * byte to be inserted
242 * offset of data[] to start inserting byte from
244 * number of bits of input to be inserted into data[]
246 * @throws BufferException
247 * when the input, startOffset and numBits are not congruent
248 * with the data buffer size
250 public static void setByte(final byte[] data, final byte input, final int startOffset, final int numBits)
251 throws BufferException {
252 byte[] inputByteArray = new byte[1];
253 Arrays.fill(inputByteArray, 0, 1, input);
254 setBytes(data, inputByteArray, startOffset, numBits);
258 * Bits are expected to be stored in the input byte array from LSB.
261 * to set the input byte
263 * bytes to be inserted
265 * offset of data[] to start inserting byte from
267 * number of bits of input to be inserted into data[]
268 * @throws BufferException
269 * when the startOffset and numBits parameters are not congruent
270 * with data and input buffers' size
272 public static void setBytes(final byte[] data, final byte[] input, final int startOffset, final int numBits)
273 throws BufferException {
274 checkExceptions(data, startOffset, numBits);
275 insertBits(data, input, startOffset, numBits);
279 * Returns numBits 1's in the MSB position.
281 public static int getMSBMask(final int numBits) {
283 for (int i = 0; i < numBits; i++) {
284 mask = mask | 1 << 7 - i;
290 * Returns numBits 1's in the LSB position.
292 public static int getLSBMask(final int numBits) {
294 for (int i = 0; i < numBits; i++) {
295 mask = mask | 1 << i;
301 * Returns the numerical value of the byte array passed.
303 * @return long - numerical value of byte array passed
305 public static long toNumber(final byte[] array) {
307 for (byte anArray : array) {
312 ret = ret << Byte.SIZE | value;
318 * Returns the numerical value of the last numBits (LSB bits) of the byte array passed.
320 * @return long - numerical value of byte array passed
322 public static long toNumber(final byte[] array, final int numBits) {
323 int length = numBits / Byte.SIZE;
324 int bitsRest = numBits % Byte.SIZE;
325 int startOffset = array.length - length;
328 for (int i = Math.max(0, startOffset - 1); i < array.length; i++) {
329 int value = array[i];
330 if (i == startOffset - 1) {
331 value &= getLSBMask(bitsRest);
336 ret = ret << Byte.SIZE | value;
343 * Accepts a number as input and returns its value in byte form in LSB
344 * aligned form example: input = 5000 [1001110001000] bytes = 19, -120
345 * [00010011] [10001000].
347 public static byte[] toByteArray(final Number input) {
348 Class<? extends Number> dataType = input.getClass();
350 long longValue = input.longValue();
352 if (dataType == Byte.class || dataType == byte.class) {
354 } else if (dataType == Short.class || dataType == short.class) {
356 } else if (dataType == Integer.class || dataType == int.class) {
358 } else if (dataType == Long.class || dataType == long.class) {
361 throw new IllegalArgumentException("Parameter must one of the following: Short/Int/Long\n");
364 int length = size / NetUtils.NUM_BITS_IN_A_BYTE;
365 byte[] bytes = new byte[length];
367 // Getting the bytes from input value
368 for (int i = 0; i < length; i++) {
369 bytes[i] = (byte) (longValue >> NetUtils.NUM_BITS_IN_A_BYTE * (length - i - 1) & BYTE_MASK);
375 * Accepts a number as input and returns its value in byte form in MSB
376 * aligned form example: input = 5000 [1001110001000] bytes = -114, 64
377 * [10011100] [01000000].
380 * - the number of bits to be returned
384 public static byte[] toByteArray(final Number input, final int numBits) {
385 Class<? extends Number> dataType = input.getClass();
387 long longValue = input.longValue();
389 if (dataType == Short.class) {
391 } else if (dataType == Integer.class) {
393 } else if (dataType == Long.class) {
396 throw new IllegalArgumentException("Parameter must one of the following: Short/Int/Long\n");
399 int length = size / NetUtils.NUM_BITS_IN_A_BYTE;
400 byte[] bytes = new byte[length];
401 byte[] inputbytes = new byte[length];
404 // Getting the bytes from input value
405 for (int i = 0; i < length; i++) {
406 bytes[i] = (byte) (longValue >> NetUtils.NUM_BITS_IN_A_BYTE * (length - i - 1) & BYTE_MASK);
409 if (bytes[0] == 0 && dataType == Long.class || bytes[0] == 0 && dataType == Integer.class) {
411 for (index = 0; index < length; ++index) {
412 if (bytes[index] != 0) {
413 bytes[0] = bytes[index];
417 System.arraycopy(bytes, index, inputbytes, 0, length - index);
418 Arrays.fill(bytes, length - index + 1, length - 1, (byte) 0);
420 System.arraycopy(bytes, 0, inputbytes, 0, length);
423 shiftedBytes = shiftBitsToMSB(inputbytes, numBits);
429 * Takes an LSB aligned byte array and returned the LSB numBits in a MSB
430 * aligned byte array.
433 * It aligns the last numBits bits to the head of the byte array following
434 * them with numBits % 8 zero bits.
437 * Example: For inputbytes = [00000111][01110001] and numBits = 12 it
438 * returns: shiftedBytes = [01110111][00010000]
441 * - number of bits to be left aligned
444 public static byte[] shiftBitsToMSB(final byte[] inputBytes, final int numBits) {
445 int numBitstoShiftBy;
446 int leadZeroesMSB = 8;
448 int size = inputBytes.length;
449 byte[] shiftedBytes = new byte[size];
451 for (int i = 0; i < Byte.SIZE; i++) {
452 if ((byte) (inputBytes[0] & getMSBMask(i + 1)) != 0) {
458 if (numBits % NetUtils.NUM_BITS_IN_A_BYTE == 0) {
459 numBitstoShiftBy = 0;
461 numBitstoShiftBy = NetUtils.NUM_BITS_IN_A_BYTE - numBits % NetUtils.NUM_BITS_IN_A_BYTE < leadZeroesMSB
462 ? NetUtils.NUM_BITS_IN_A_BYTE - numBits % NetUtils.NUM_BITS_IN_A_BYTE : leadZeroesMSB;
464 if (numBitstoShiftBy == 0) {
468 if (numBits < NetUtils.NUM_BITS_IN_A_BYTE) {
469 // inputbytes.length = 1 OR read less than a byte
470 shiftedBytes[0] = (byte) ((inputBytes[0] & getLSBMask(numBits)) << numBitstoShiftBy);
472 // # of bits to read from last byte
473 numEndRestBits = NetUtils.NUM_BITS_IN_A_BYTE
474 - (inputBytes.length * NetUtils.NUM_BITS_IN_A_BYTE - numBits - numBitstoShiftBy);
476 for (int i = 0; i < size - 1; i++) {
477 if (i + 1 == size - 1) {
478 if (numEndRestBits > numBitstoShiftBy) {
479 shiftedBytes[i] = (byte) (inputBytes[i] << numBitstoShiftBy
480 | (inputBytes[i + 1] & getMSBMask(numBitstoShiftBy)) >> numEndRestBits
482 shiftedBytes[i + 1] = (byte) ((inputBytes[i + 1]
483 & getLSBMask(numEndRestBits - numBitstoShiftBy)) << numBitstoShiftBy);
485 shiftedBytes[i] = (byte) (inputBytes[i] << numBitstoShiftBy
486 | (inputBytes[i + 1] & getMSBMask(numEndRestBits)) >> NetUtils.NUM_BITS_IN_A_BYTE
490 shiftedBytes[i] = (byte) (inputBytes[i] << numBitstoShiftBy
491 | (inputBytes[i + 1] & getMSBMask(numBitstoShiftBy)) >> NetUtils.NUM_BITS_IN_A_BYTE
500 * It aligns the first numBits bits to the right end of the byte array
501 * preceding them with numBits % 8 zero bits.
504 * Example: For inputbytes = [01110111][00010000] and numBits = 12 it
505 * returns: shiftedBytes = [00000111][01110001]
507 * @param inputBytes input bytes
509 * - number of bits to be right aligned
512 public static byte[] shiftBitsToLSB(final byte[] inputBytes, final int numBits) {
513 int numBytes = inputBytes.length;
514 int numBitstoShift = numBits % NetUtils.NUM_BITS_IN_A_BYTE;
515 byte[] shiftedBytes = new byte[numBytes];
519 if (numBitstoShift == 0) {
523 for (int i = 1; i < numBytes; i++) {
524 inputLsb = inputBytes[i - 1] & getLSBMask(NetUtils.NUM_BITS_IN_A_BYTE - numBitstoShift);
525 inputLsb = inputLsb < 0 ? inputLsb + 256 : inputLsb;
526 inputMsb = inputBytes[i] & getMSBMask(numBitstoShift);
527 inputMsb = inputBytes[i] < 0 ? inputBytes[i] + 256 : inputBytes[i];
528 shiftedBytes[i] = (byte) (inputLsb << numBitstoShift
529 | inputMsb >> NetUtils.NUM_BITS_IN_A_BYTE - numBitstoShift);
531 inputMsb = inputBytes[0] & getMSBMask(numBitstoShift);
532 inputMsb = inputMsb < 0 ? inputMsb + 256 : inputMsb;
533 shiftedBytes[0] = (byte) (inputMsb >> NetUtils.NUM_BITS_IN_A_BYTE - numBitstoShift);
538 * Insert in the data buffer at position dictated by the offset the number
539 * of bits specified from the input data byte array. The input byte array
540 * has the bits stored starting from the LSB
542 public static void insertBits(final byte[] data, final byte[] inputdataLSB, final int startOffset,
544 byte[] inputdata = shiftBitsToMSB(inputdataLSB, numBits); // Align to
549 int numBytes = numBits / NetUtils.NUM_BITS_IN_A_BYTE;
550 int startByteOffset = startOffset / NetUtils.NUM_BITS_IN_A_BYTE;
551 int extraOffsetBits = startOffset % NetUtils.NUM_BITS_IN_A_BYTE;
552 int extranumBits = numBits % NetUtils.NUM_BITS_IN_A_BYTE;
553 int restBits = numBits % NetUtils.NUM_BITS_IN_A_BYTE;
555 int inputLSBbits = 0;
561 if (extraOffsetBits == 0) {
562 if (extranumBits == 0) {
563 numBytes = numBits / NetUtils.NUM_BITS_IN_A_BYTE;
564 System.arraycopy(inputdata, 0, data, startByteOffset, numBytes);
566 System.arraycopy(inputdata, 0, data, startByteOffset, numBytes);
567 data[startByteOffset + numBytes] = (byte) (data[startByteOffset + numBytes]
568 | inputdata[numBytes] & getMSBMask(extranumBits));
572 for (index = 0; index < numBytes; index++) {
574 inputLSBbits = inputdata[index - 1] & getLSBMask(extraOffsetBits);
576 inputMSBbits = (byte) (inputdata[index] & getMSBMask(NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits));
577 inputMSBbits = inputMSBbits >= 0 ? inputMSBbits : inputMSBbits + 256;
578 data[startByteOffset + index] = (byte) (data[startByteOffset + index]
579 | inputLSBbits << NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits
580 | inputMSBbits >> extraOffsetBits);
581 inputMSBbits = inputLSBbits = 0;
583 if (restBits < NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits) {
585 inputLSBbits = inputdata[index - 1] & getLSBMask(extraOffsetBits);
587 inputMSBbits = (byte) (inputdata[index] & getMSBMask(restBits));
588 inputMSBbits = inputMSBbits >= 0 ? inputMSBbits : inputMSBbits + 256;
589 data[startByteOffset + index] = (byte) (data[startByteOffset + index]
590 | inputLSBbits << NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits
591 | inputMSBbits >> extraOffsetBits);
592 } else if (restBits == NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits) {
594 inputLSBbits = inputdata[index - 1] & getLSBMask(extraOffsetBits);
596 inputMSBbits = (byte) (inputdata[index] & getMSBMask(NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits));
597 inputMSBbits = inputMSBbits >= 0 ? inputMSBbits : inputMSBbits + 256;
598 data[startByteOffset + index] = (byte) (data[startByteOffset + index]
599 | inputLSBbits << NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits
600 | inputMSBbits >> extraOffsetBits);
603 inputLSBbits = inputdata[index - 1] & getLSBMask(extraOffsetBits);
605 inputMSBbits = (byte) (inputdata[index] & getMSBMask(NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits));
606 inputMSBbits = inputMSBbits >= 0 ? inputMSBbits : inputMSBbits + 256;
607 data[startByteOffset + index] = (byte) (data[startByteOffset + index]
608 | inputLSBbits << NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits
609 | inputMSBbits >> extraOffsetBits);
611 inputLSBbits = inputdata[index]
612 & getLSBMask(restBits - (NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits))
613 << NetUtils.NUM_BITS_IN_A_BYTE
615 data[startByteOffset + index + 1] = (byte) (data[startByteOffset + index + 1]
616 | inputLSBbits << NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits);
622 * Checks for overflow and underflow exceptions.
624 * @throws BufferException
625 * when the startOffset and numBits parameters are not congruent
626 * with the data buffer's size
628 public static void checkExceptions(final byte[] data, final int startOffset, final int numBits)
629 throws BufferException {
632 endOffsetByte = startOffset / NetUtils.NUM_BITS_IN_A_BYTE + numBits / NetUtils.NUM_BITS_IN_A_BYTE
633 + (numBits % NetUtils.NUM_BITS_IN_A_BYTE != 0 ? 1
634 : startOffset % NetUtils.NUM_BITS_IN_A_BYTE != 0 ? 1 : 0);
635 startByteOffset = startOffset / NetUtils.NUM_BITS_IN_A_BYTE;
638 throw new BufferException("data[] is null\n");
641 if (startOffset < 0 || startByteOffset >= data.length || endOffsetByte > data.length || numBits < 0
642 || numBits > NetUtils.NUM_BITS_IN_A_BYTE * data.length) {
643 throw new BufferException("Illegal arguement/out of bound exception - data.length = " + data.length
644 + " startOffset = " + startOffset + " numBits " + numBits);