aes70
Version:
A controller library for the AES70 protocol.
34 lines (28 loc) • 965 B
JavaScript
import { createType } from './createType.js';
export function OcaBlobFixedLen(Length) {
return createType({
isConstantLength: true,
canEncode: function (value) {
if (typeof value !== 'object') return false;
return (
(value instanceof Uint8Array || Array.isArray(value)) &&
value.length === Length
);
},
encodedLength: function (value) {
return Length;
},
encodeTo: function (dataView, pos, value) {
if (!Array.isArray(value) && !(value instanceof Uint8Array))
throw new TypeError('Expected Array or Uint8Array');
const length = value.length;
if (length !== Length) throw new Error('Length mismatch.');
const u8 = new Uint8Array(dataView.buffer, dataView.byteOffset);
u8.set(value, pos);
return pos + Length;
},
decode: function (dataView, pos) {
return new Uint8Array(dataView.buffer, dataView.byteOffset + pos, Length);
},
});
}