All files / Sources/Common/Core/Endian index.js

38.46% Statements 10/26
16.66% Branches 1/6
50% Functions 1/2
42.85% Lines 9/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40  1x 1x 1x 1x 1x 1x 1x 1x         1x                                                    
export function getEndianness() {
  const a = new ArrayBuffer(4);
  const b = new Uint8Array(a);
  const c = new Uint32Array(a);
  b[0] = 0xa1;
  b[1] = 0xb2;
  b[2] = 0xc3;
  b[3] = 0xd4;
  if (c[0] === 0xd4c3b2a1) return 'LittleEndian';
  if (c[0] === 0xa1b2c3d4) return 'BigEndian';
  return null;
}
 
export const ENDIANNESS = getEndianness();
 
export function swapBytes(buffer, wordSize) {
  if (wordSize < 2) {
    return;
  }
 
  const bytes = new Int8Array(buffer);
  const size = bytes.length;
  const tempBuffer = [];
 
  for (let i = 0; i < size; i += wordSize) {
    for (let j = 0; j < wordSize; j++) {
      tempBuffer.push(bytes[i + j]);
    }
    for (let j = 0; j < wordSize; j++) {
      bytes[i + j] = tempBuffer.pop();
    }
  }
}
 
export default {
  ENDIANNESS,
  getEndianness,
  swapBytes,
};