tvdos app package wip

This commit is contained in:
minjaesong
2023-05-06 19:06:43 +09:00
parent fe6f247a76
commit 831485bc35
3 changed files with 799 additions and 1 deletions

View File

@@ -0,0 +1,114 @@
const filepath = exec_args[1]
if (!filepath) {
println(`Usage: appexec path/to/application.app`)
return 0
}
const file = files.open(_G.shell.resolvePathInput(filepath).full)
if (!file.exists) {
println("File not found.")
return 1
}
if (file.isDirectory) {
println("Not an app file.")
return 2
}
const filebytes = file.sread()
// check magic
if (filebytes.substring(0,4) != "\x7FApP") {
println("Not an app file.")
return 2
}
const endianness = filebytes.charCodeAt(4)
const sectionComp = filebytes.charCodeAt(6)
const sectionCount = filebytes.charCodeAt(7)
const targetOS = filebytes.charCodeAt(8)
const decompFun = (1 == sectionComp) ? (b) => gzip.decomp(b) : (b) => b
const decompToPtrFun = (1 == sectionComp) ? (b, target) => gzip.decompTo(b, target) : TODO()
if (targetOS != 1 && targetOS != 0) {
println("App is not an TVDOS executable.")
return 3
}
function strToInt48(str) {
let s = [...str].map(it=>it.charCodeAt(0))
return ((4294967296 + (s[0] << 40)) + (s[1] << 32) + (s[2] << 24) + (s[3] << 16) + (s[4] << 8) + s[5]) - 4294967296
}
function makeHash(length) {
let e = "YBNDRFG8EJKMCPQXOTLVWIS2A345H769"
let m = e.length
let s = ""
for (let i = 0; i < length; i++) {
s += e[Math.floor(Math.random()*m)]
}
return s
}
const PATH_MOUNT = `$:/TMP/${makeHash(32)}/`
// READ SECTIONS
let sectionTable = []
let rodata = {}
for (let i = 0; i < sectionCount; i++) {
let sectName = filebytes.substring(16 * (i+1), 16 * (i+1) + 10).trimNull()
let sectOffset = strToInt48(filebytes.substring(16 * (i+1) + 10, 16 * (i+1) + 16))
sectionTable.push([sectName, sectOffset])
}
for (let i = 0; i < sectionTable.length - 1; i++) {
let [sectName, sectOffset] = sectionTable[i]
let nextSectOffset = sectionTable[i+1][1]
let uncompLen = strToInt48(filebytes.substring(sectOffset, sectOffset + 6))
let compPayload = filebytes.substring(sectOffset + 6, nextSectOffset)
if ("RODATA" == sectName) {
let rodataPtr = 0
while (rodataPtr < nextSectOffset - sectOffset) {
let labelLen = filebytes.charCodeAt(sectOffset + rodataPtr)
let label = filebytes.substring(sectOffset + rodataPtr + 1, sectOffset + rodataPtr + 1 + labelLen)
let payloadLen = strToInt48(filebytes.substring(sectOffset + rodataPtr + 1 + labelLen, sectOffset + rodataPtr + 1 + labelLen + 6))
let uncompLen = strToInt48(filebytes.substring(sectOffset + rodataPtr + 1 + labelLen + 6, sectOffset + rodataPtr + 1 + labelLen + 12))
let sectPayload = filebytes.substring(sectOffset + rodataPtr + 1 + labelLen + 12, sectOffset + rodataPtr + 1 + labelLen + 12 + payloadLen)
try {
let ptr = sys.malloc(uncompLen)
decompToPtrFun(sectPayload, ptr)
rodata[label] = ptr
}
catch (e) {
rodata[label] = null
}
decompFun(payload)
rodataPtr += 13 + labelLen + payloadLen
}
}
else if ("TEXT" == sectName) {
let program = String.fromCharCode.apply(null, decompFun(compPayload))
// inject RODATA map
let rodataSnippet = `const __RODATA=Object.freeze(${JSON.stringify(rodata)});`
files.open(PATH_MOUNT + "run.com").swrite(rodataSnippet+program)
}
}
_G.shell.execute(PATH_MOUNT + "run.com")
// TODO delete PATH_MOUNT

View File

@@ -51,7 +51,7 @@ class PmemFSfile {
// Javascript array OR JVM byte[]
else if (Array.isArray(bytes) || bytes.toString().startsWith("[B")) {
this.data = ""
for (let i = 0; i < array.length; i++) {
for (let i = 0; i < bytes.length; i++) {
this.data += String.fromCharCode(bytes[i])
}
}

684
tvdos_app_package.html Normal file

File diff suppressed because one or more lines are too long