| 1 | package cz.vutbr.feec.packets; |
| 2 | |
| 3 | /** |
| 4 | * The Class PacketUtils. |
| 5 | * |
| 6 | * @brief This class provides generic packet assembly and disassembly functions, which |
| 7 | * are used in various parts of the project e.g. assembling and disassembling |
| 8 | * RTP and RTCP packets etc. |
| 9 | */ |
| 10 | |
| 11 | public class PacketUtils { |
| 12 | |
| 13 | /** |
| 14 | * Bounds packet length to 32 bits. |
| 15 | * |
| 16 | * @param length the length |
| 17 | * |
| 18 | * @return bounded length on octets count |
| 19 | */ |
| 20 | public synchronized static int bound32Bit(int length) { |
| 21 | int result = length; |
| 22 | int rest = length % 4; |
| 23 | if(rest != 0){ |
| 24 | result += 4 - rest; |
| 25 | } |
| 26 | return result; |
| 27 | } |
| 28 | /** |
| 29 | * Bounds packet length to 32 bits. |
| 30 | * |
| 31 | * @param length the length |
| 32 | * |
| 33 | * @return bounded length on octets count |
| 34 | */ |
| 35 | public synchronized static int bound32(int length) { |
| 36 | int result = length; |
| 37 | int rest = length % 4; |
| 38 | if(rest != 0){ |
| 39 | result += 4 - rest; |
| 40 | } |
| 41 | return result; |
| 42 | } |
| 43 | public synchronized static int bound32(byte[] buffer, int offset, int length) { |
| 44 | int result = length; |
| 45 | int rest = length % 4; |
| 46 | if(rest != 0){ |
| 47 | result += 4 - rest; |
| 48 | } |
| 49 | // mazu prazdna mista |
| 50 | for (int i = length+1; i <= result; i++) { |
| 51 | buffer[offset + i] = 0; |
| 52 | } |
| 53 | return result; |
| 54 | } |
| 55 | public static int copy(byte[] src, byte[] dst, int dstPos, int length) { |
| 56 | System.arraycopy(src, 0, dst, dstPos, length ); |
| 57 | return length; |
| 58 | } |
| 59 | @Deprecated |
| 60 | public static int copy(byte[] src, int dstPos, byte[] dst) { |
| 61 | return copy(src, dst, dstPos, src.length ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Append two byte arrays. Appends packet B at the end of Packet A (Assuming |
| 66 | * Bytes as elements). Returns packet ( AB ). |
| 67 | * |
| 68 | * @param packetB The second packet. |
| 69 | * @param packetA The first packet. |
| 70 | * |
| 71 | * @return The desired packet which is A concatenated with B. |
| 72 | */ |
| 73 | @Deprecated |
| 74 | public synchronized static byte[] append(byte[] packetA, byte[] packetB) { |
| 75 | // Create a new array whose size is equal to sum of packets |
| 76 | // being added |
| 77 | byte packetAB[] = new byte[packetA.length + packetB.length]; |
| 78 | |
| 79 | // First paste in packetA |
| 80 | for (int i = 0; i < packetA.length; i++) |
| 81 | packetAB[i] = packetA[i]; |
| 82 | |
| 83 | // Now start pasting packetB |
| 84 | for (int i = 0; i < packetB.length; i++) |
| 85 | packetAB[i + packetA.length] = packetB[i]; |
| 86 | |
| 87 | return packetAB; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Check size. |
| 92 | * |
| 93 | * @param value the value |
| 94 | * @param noOfBits the no of bits |
| 95 | * |
| 96 | * @return true, if check size |
| 97 | */ |
| 98 | public static boolean checkSize(long value, int noOfBits) { |
| 99 | long i = 1; |
| 100 | i = i << noOfBits; |
| 101 | return i > value && value >=0; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Convert signed int to long by taking 2's complement if necessary. |
| 106 | * |
| 107 | * @param intToConvert The signed integer which will be converted to Long. |
| 108 | * |
| 109 | * @return The unsigned long representation of the signed int. |
| 110 | */ |
| 111 | |
| 112 | public synchronized static long convertSignedIntToLong(int intToConvert) { |
| 113 | int in = intToConvert; |
| 114 | |
| 115 | in = (in << 1) >> 1; |
| 116 | |
| 117 | long Lin = (long) in; |
| 118 | Lin = Lin + 0x7FFFFFFF; |
| 119 | |
| 120 | return Lin; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Convert 64 bit long to n bytes. |
| 125 | * @Deprecated Use setOctet2 setOctet3 or setOctet4 instead |
| 126 | * |
| 127 | * @param ldata The long from which the n byte array will be constructed. |
| 128 | * @param numberOfBytes The desired number of bytes to convert the long to. |
| 129 | * |
| 130 | * @return The desired byte array which is populated with the long value. |
| 131 | */ |
| 132 | @Deprecated |
| 133 | public synchronized static byte[] longToBytes(long longData, int numberOfBytes) { |
| 134 | byte buff[] = new byte[numberOfBytes]; |
| 135 | |
| 136 | for (int i = numberOfBytes - 1; i >= 0; i--) { |
| 137 | // Keep assigning the right most 8 bits to the |
| 138 | // byte arrays while shift 8 bits during each iteration |
| 139 | buff[i] = (byte) longData; |
| 140 | longData = longData >> 8; |
| 141 | } |
| 142 | return buff; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Copy all bytes 'from' to 'to'. Starting on 'to[startIndex]'. |
| 147 | * @Deprecated Use setOctet2 setOctet3 or setOctet4 instead |
| 148 | * |
| 149 | * @param to the to |
| 150 | * @param from the from |
| 151 | * @param startIndex the start index |
| 152 | */ |
| 153 | @Deprecated |
| 154 | public synchronized static void copyBytes(byte[] to, byte[] from, |
| 155 | int startIndex) { |
| 156 | for (int i = 0; i < from.length; ++i) { |
| 157 | to[startIndex + i] = from[i]; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Copy bytes. |
| 163 | * @Deprecated Use setOctet2 setOctet3 or setOctet4 instead |
| 164 | * |
| 165 | * @param to the to |
| 166 | * @param length the length |
| 167 | * @param from the from |
| 168 | * @param startIndex the start index |
| 169 | */ |
| 170 | @Deprecated |
| 171 | public synchronized static void copyBytes(byte[] to, byte[] from, int length, |
| 172 | int startIndex) { |
| 173 | for (int i = 0; i < length; ++i) { |
| 174 | to[startIndex + i] = from[i]; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Calculate number of octets required to fit the given number of octets |
| 180 | * into 32 bit boundary. |
| 181 | * |
| 182 | * @param LengthOfUnpaddedPacket the Length of unpadded packet |
| 183 | * |
| 184 | * @return The required number of octets which must be appended to this |
| 185 | * packet to make it fit into a 32 bit boundary. |
| 186 | */ |
| 187 | |
| 188 | public synchronized static int calculatePadLength(int LengthOfUnpaddedPacket) { |
| 189 | // Determine the number of 8 bit words required to fit the packet in 32 |
| 190 | // bit boundary |
| 191 | int remain = (int) Math.IEEEremainder((double) LengthOfUnpaddedPacket, |
| 192 | (double) 4); |
| 193 | |
| 194 | int PadLen = 0; |
| 195 | |
| 196 | // e.g. remainder -1, then we need to pad 1 extra |
| 197 | // byte to the end to make it to the 32 bit boundary |
| 198 | if (remain < 0) |
| 199 | PadLen = Math.abs(remain); |
| 200 | |
| 201 | // e.g. remainder is +1 then we need to pad 3 extra bytes |
| 202 | else if (remain > 0) |
| 203 | PadLen = 4 - remain; |
| 204 | |
| 205 | return (PadLen); |
| 206 | |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Get 1 octet. (respects Network byte order) |
| 211 | * |
| 212 | * @param rawData the raw data |
| 213 | * @param offset the offset |
| 214 | * |
| 215 | * @return the octet1 |
| 216 | * @throws PacketParseException |
| 217 | */ |
| 218 | public static final int getOctet1(byte[] buffer, int offset) throws PacketParseException{ |
| 219 | if(buffer.length-1 < offset) { |
| 220 | throw new PacketParseException("Exceeded buffer length"); |
| 221 | } |
| 222 | return (buffer[offset] & 0xff); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * get 2 octets. (respects Network byte order) |
| 227 | * |
| 228 | * @param buffer the raw data |
| 229 | * @param offset the offset |
| 230 | * |
| 231 | * @return the octet2 |
| 232 | */ |
| 233 | public static final int getOctet2(byte[] buffer, int offset) throws PacketParseException{ |
| 234 | if(buffer.length-2 < offset) { |
| 235 | throw new PacketParseException("Exceeded buffer length"); |
| 236 | } |
| 237 | // from network byte order to host byte order |
| 238 | return ((buffer[offset] & 0xff) << 8 ) |
| 239 | | ((buffer[offset+1] & 0xff) ); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Get 4 octets. (respects Network byte order) |
| 244 | * |
| 245 | * @param rawData the raw data |
| 246 | * @param offset the offset |
| 247 | * |
| 248 | * @return the octet4 |
| 249 | * @throws PacketParseException |
| 250 | */ |
| 251 | public static final long getOctet4(byte[] buffer, int offset) throws PacketParseException { |
| 252 | if(buffer.length-4 < offset) { |
| 253 | throw new PacketParseException("Exceeded buffer length"); |
| 254 | } |
| 255 | long val = |
| 256 | (((long)buffer[offset + 0] & 0xff) << 24) |
| 257 | | (((long)buffer[offset + 1] & 0xff) << 16) |
| 258 | | (((long)buffer[offset + 2] & 0xff) << 8) |
| 259 | | ((long)buffer[offset + 3] & 0xff); |
| 260 | return val & 0xffffffff; |
| 261 | } |
| 262 | |
| 263 | |
| 264 | |
| 265 | /** |
| 266 | * Set 8 octets. (respects Network byte order) |
| 267 | * |
| 268 | * @param value the value |
| 269 | * @param buffer the raw data |
| 270 | * @param offset the offset |
| 271 | */ |
| 272 | @Deprecated |
| 273 | public static final void setOctet8(byte[] buffer, int offset, long value) { |
| 274 | buffer[offset+7] = (byte)(value >> 56); |
| 275 | buffer[offset+6] = (byte)(value >> 48); |
| 276 | buffer[offset+5] = (byte)(value >> 40); |
| 277 | buffer[offset+4] = (byte)(value >> 32); |
| 278 | buffer[offset+3] = (byte)(value >> 24); |
| 279 | buffer[offset+2] = (byte)(value >> 16); |
| 280 | buffer[offset+1] = (byte)(value >> 8); |
| 281 | buffer[offset] = (byte)(value); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Get 8 octets. (respects Network byte order) |
| 286 | * |
| 287 | * @param buffer the raw data |
| 288 | * @param offset the offset |
| 289 | * |
| 290 | * @return the octet8 |
| 291 | */ |
| 292 | @Deprecated |
| 293 | public static final long getOctet8(byte[] buffer, int offset) { |
| 294 | return |
| 295 | ((buffer[7 + offset] & 0xffl ) << 56) |
| 296 | | ((buffer[6 + offset] & 0xffl ) << 48) |
| 297 | | ((buffer[5 + offset] & 0xffl ) << 40) |
| 298 | | ((buffer[4 + offset] & 0xffl ) << 32) |
| 299 | | ((buffer[3 + offset] & 0xffl ) << 24) |
| 300 | | ((buffer[2 + offset] & 0xffl) << 16) |
| 301 | | ((buffer[1 + offset] & 0xffl) << 8) |
| 302 | | (buffer[0 + offset] & 0xffl); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Set 2 octets. (respects Network byte order) |
| 307 | * |
| 308 | * @param value the value |
| 309 | * @param buffer the raw data |
| 310 | * @param offset the offset |
| 311 | */ |
| 312 | public static final void setOctet2(byte[] buffer, int offset, int value) { |
| 313 | // network byte order |
| 314 | buffer[offset +1] = (byte)((value) & 0xff); |
| 315 | buffer[offset +0] = (byte)((value & 0xff00) >> 8); |
| 316 | } |
| 317 | |
| 318 | public static final void setOctet2(byte[] buffer, int offset, long value) { |
| 319 | // network byte order |
| 320 | buffer[offset +1] = (byte)((value) & 0xff); |
| 321 | buffer[offset+ 0] = (byte)((value & 0xff00) >> 8); |
| 322 | } |
| 323 | /** |
| 324 | * Set 4 octets. (respects Network byte order) |
| 325 | * |
| 326 | * @param value the value |
| 327 | * @param buffer the raw data |
| 328 | * @param offset the offset |
| 329 | */ |
| 330 | public static final void setOctet4(byte[] buffer, int offset, long value) { |
| 331 | buffer[offset+0] = (byte)(value >> 24); |
| 332 | buffer[offset+1] = (byte)(value >> 16); |
| 333 | buffer[offset+2] = (byte)(value >> 8); |
| 334 | buffer[offset+3] = (byte)(value); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Set 2 octets. (respects Network byte order) |
| 339 | * |
| 340 | * @param value the value |
| 341 | * @param buffer the raw data |
| 342 | * @param offset the offset |
| 343 | */ |
| 344 | public static final void setOctet3(byte[] buffer, int offset, long value) { |
| 345 | // network byte order |
| 346 | buffer[offset+2] = (byte)((value) & 0xff); |
| 347 | buffer[offset+1] = (byte)((value & 0xff00) >> 8); |
| 348 | buffer[offset+0] = (byte)((value & 0xff0000) >> 16); |
| 349 | } |
| 350 | public static final int setOctets(byte[] buffer, int offset, String value, int length) { |
| 351 | |
| 352 | byte[] valueB = value.getBytes(); |
| 353 | |
| 354 | for (int i = 0; i < length; i++) { |
| 355 | |
| 356 | buffer[offset + i] = valueB[i]; |
| 357 | |
| 358 | } |
| 359 | return (length/4); |
| 360 | } |
| 361 | |
| 362 | |
| 363 | public static final String getOctets(byte[] buffer, int startPos, int lengthOfText){ |
| 364 | |
| 365 | byte[] value = new byte[lengthOfText]; |
| 366 | for (int i = 0; i < lengthOfText; i++) { |
| 367 | value[i]=buffer[i+startPos]; |
| 368 | } |
| 369 | String text = new String(value); |
| 370 | return text ; |
| 371 | } |
| 372 | /** |
| 373 | * Set 1 octets. (respects Network byte order) |
| 374 | * |
| 375 | * @param value the value |
| 376 | * @param buffer the raw data |
| 377 | * @param offset the offset |
| 378 | */ |
| 379 | public static final void setOctet1(byte[] buffer, int offset, long value) { |
| 380 | // network byte order |
| 381 | buffer[offset] = (byte)((value) & 0xff); |
| 382 | } |
| 383 | /** |
| 384 | * get 2 octets. (respects Network byte order) |
| 385 | * |
| 386 | * @param buffer the raw data |
| 387 | * @param offset the offset |
| 388 | * |
| 389 | * @return the octet3 |
| 390 | */ |
| 391 | public static final long getOctet3(byte[] buffer, int offset) { |
| 392 | // from network byte order to host byte order |
| 393 | return (buffer[offset+2] & 0xff) |
| 394 | | ((buffer[offset+1] & 0xff) <<8) |
| 395 | | ((buffer[offset+0] & 0xff) <<16); |
| 396 | } |
| 397 | } |