Adjust isRiscV helper to check for PVM\0 bytes (#1895)
* Adjust `isRiscV` helper to check for `PVM\0` bytes * Also check for ELF * Adjust tests * Update description
This commit is contained in:
@@ -6,15 +6,15 @@
|
||||
import { isRiscV } from './index.js';
|
||||
|
||||
describe('isRiscV', (): void => {
|
||||
it('is false on non-risc-v header', (): void => {
|
||||
it('is false on non-elf/non-pvm header', (): void => {
|
||||
expect(isRiscV(new Uint8Array([0, 97, 115, 109, 1, 2, 3]))).toEqual(false);
|
||||
});
|
||||
|
||||
it('is false on partial-risc-v header', (): void => {
|
||||
it('is false on partial-elf/pvm header', (): void => {
|
||||
expect(isRiscV(new Uint8Array([0x7f, 0x45, 0x4c]))).toEqual(false);
|
||||
});
|
||||
|
||||
it('is false on empty byte before risc-v header', (): void => {
|
||||
it('is false on empty byte before elf/pvm header', (): void => {
|
||||
expect(
|
||||
isRiscV(
|
||||
new Uint8Array([0x00, 0x7f, 0x45, 0x4c, 0x46, 0xff, 0x00, 0x42, 0x23])
|
||||
@@ -22,9 +22,15 @@ describe('isRiscV', (): void => {
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('is true when a risc-v header is found', (): void => {
|
||||
it('is true when a elf header is found', (): void => {
|
||||
expect(
|
||||
isRiscV(new Uint8Array([0x7f, 0x45, 0x4c, 0x46, 0xff, 0x00, 0x42, 0x23]))
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('is true when a pvm header is found', (): void => {
|
||||
expect(
|
||||
isRiscV(new Uint8Array([0x50, 0x56, 0x4d, 0x00, 0xff, 0x00, 0x42, 0x23]))
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,8 +4,12 @@
|
||||
import { u8aEq } from '../u8a/eq.js';
|
||||
import { isU8a } from './u8a.js';
|
||||
|
||||
// general elf header
|
||||
const ELF_MAGIC = new Uint8Array([0x7f, 0x45, 0x4c, 0x46]); // ELF magic bytes: 0x7f, 'E', 'L', 'F'
|
||||
|
||||
// contract-specific PVM header
|
||||
const PVM_MAGIC = new Uint8Array([0x50, 0x56, 0x4d, 0x00]); // 'P', 'V', 'M', 0x00
|
||||
|
||||
/**
|
||||
* @name isRiscV
|
||||
* @summary Tests if the input has a RISC-V header
|
||||
@@ -13,5 +17,11 @@ const ELF_MAGIC = new Uint8Array([0x7f, 0x45, 0x4c, 0x46]); // ELF magic bytes:
|
||||
* Checks to see if the input Uint8Array contains a valid RISC-V header
|
||||
*/
|
||||
export function isRiscV (bytes: unknown): bytes is Uint8Array {
|
||||
return isU8a(bytes) && u8aEq(bytes.subarray(0, 4), ELF_MAGIC);
|
||||
if (isU8a(bytes)) {
|
||||
const start = bytes.subarray(0, 4);
|
||||
|
||||
return u8aEq(start, PVM_MAGIC) || u8aEq(start, ELF_MAGIC);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user