Skip to content

Commit

Permalink
Look through the file to find the nexe footer.
Browse files Browse the repository at this point in the history
Rather than hoping we find it on the first read.

When doing macOS signing the signature and entitlements are placed after
the nexe sentinel, so we may not find it immediately.
  • Loading branch information
bruce-one committed Apr 18, 2023
1 parent be5d20f commit d4e2e56
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions src/patches/boot-nexe.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
const fs = require('fs'),
fd = fs.openSync(process.execPath, 'r'),
stat = fs.fstatSync(fd),
defaultTailSize = 16000,
tailSize = Math.min(
stat.size,
require('os').platform() === 'darwin' ? defaultTailSize * 100 : defaultTailSize
),
tailWindow = Buffer.from(Array(tailSize))
tailSize = Math.min(stat.size, 16000),
tailWindow = Buffer.alloc(tailSize),
match = '<nexe' + '~~sentinel>',
matchLength = match.length,
lastBuffer = Buffer.alloc(matchLength + 32)

fs.readSync(fd, tailWindow, 0, tailSize, stat.size - tailSize)
let offset = stat.size,
footerPosition = -1,
footerPositionOffset = 0,
footer: Buffer

while (true) {
const bytesRead = fs.readSync(fd, tailWindow, 0, tailSize, offset - tailSize)
if (bytesRead === 0) break

const combinedBuffers = Buffer.concat([tailWindow, lastBuffer])
footerPosition = combinedBuffers.indexOf(match)

if (footerPosition > -1) {
footer = combinedBuffers.slice(footerPosition, footerPosition + 32)
break
}

if (offset < 0) break

tailWindow.copy(lastBuffer)
offset = offset - bytesRead
}

const footerPosition = tailWindow.indexOf('<nexe~~sentinel>')
if (footerPosition == -1) {
throw 'Invalid Nexe binary'
}

const footer = tailWindow.slice(footerPosition, footerPosition + 32),
contentSize = footer.readDoubleLE(16),
resourceSize = footer.readDoubleLE(24),
contentStart = stat.size - tailSize + footerPosition - resourceSize - contentSize,
const contentSize = footer!.readDoubleLE(16),
resourceSize = footer!.readDoubleLE(24),
contentStart = offset - tailSize + footerPosition - resourceSize - contentSize,
resourceStart = contentStart + contentSize

Object.defineProperty(
Expand Down Expand Up @@ -52,10 +70,10 @@ Object.defineProperty(
})()
)

const contentBuffer = Buffer.from(Array(contentSize)),
const contentBuffer = Buffer.alloc(contentSize),
Module = require('module')

fs.readSync(fd, contentBuffer, 0, contentSize, contentStart)
fs.closeSync(fd)

new Module(process.execPath, null)._compile(contentBuffer.toString(), process.execPath)
new Module(process.execPath, null)._compile(contentBuffer.slice(1).toString(), process.execPath)

0 comments on commit d4e2e56

Please sign in to comment.