mirror of
https://huihui.cat/mirrors/MikroTikPatch.git
synced 2025-12-10 03:24:37 +01:00
Update npk.py
Signed-off-by: elseif <elseif@live.cn>
This commit is contained in:
parent
6cbdbffbcb
commit
4f9d2133fd
1 changed files with 20 additions and 31 deletions
47
npk.py
47
npk.py
|
|
@ -207,10 +207,9 @@ class NovaPackage(Package):
|
||||||
self._parts.append(NpkPartItem(NpkPartID(part_id),NpkInfo.unserialize_from(part_data)))
|
self._parts.append(NpkPartItem(NpkPartID(part_id),NpkInfo.unserialize_from(part_data)))
|
||||||
else:
|
else:
|
||||||
self._parts.append(NpkPartItem(NpkPartID(part_id),part_data))
|
self._parts.append(NpkPartItem(NpkPartID(part_id),part_data))
|
||||||
|
|
||||||
def set_null_block(self):
|
def set_null_block(self):
|
||||||
def rebuild_squashfs(data):
|
def rebuild_squashfs(data):
|
||||||
if len(part.data) < 4 or part.data[:4] != b'hsqs':
|
|
||||||
return part.data
|
|
||||||
with open('squashfs.sfs', 'wb') as f:
|
with open('squashfs.sfs', 'wb') as f:
|
||||||
f.write(data)
|
f.write(data)
|
||||||
os.system('unsquashfs -d squashfs-root squashfs.sfs')
|
os.system('unsquashfs -d squashfs-root squashfs.sfs')
|
||||||
|
|
@ -219,19 +218,20 @@ class NovaPackage(Package):
|
||||||
os.system('rm -rf squashfs-root')
|
os.system('rm -rf squashfs-root')
|
||||||
with open('squashfs.sfs', 'rb') as f:
|
with open('squashfs.sfs', 'rb') as f:
|
||||||
return f.read()
|
return f.read()
|
||||||
|
def get_size(package,size=8):
|
||||||
if len(self._packages) > 0:
|
|
||||||
has_squashfs = False
|
|
||||||
for package in self._packages:
|
|
||||||
for part in package._parts:
|
for part in package._parts:
|
||||||
if part.id == NpkPartID.SQUASHFS:
|
size += 6
|
||||||
|
size += len(part.data)
|
||||||
|
return size
|
||||||
|
|
||||||
|
def set_null(package,offset=None):
|
||||||
|
has_squashfs = False
|
||||||
|
for part in package._parts:
|
||||||
|
if part.id == NpkPartID.SQUASHFS and len(part.data) >= 4 and part.data[:4] in [b'hsqs',b'sqsh']:
|
||||||
part.data = rebuild_squashfs(part.data)
|
part.data = rebuild_squashfs(part.data)
|
||||||
has_squashfs = True
|
has_squashfs = True
|
||||||
|
if has_squashfs:
|
||||||
if not has_squashfs:
|
count = offset or 8
|
||||||
return
|
|
||||||
for package in self._packages:
|
|
||||||
count = 8
|
|
||||||
for part in package._parts:
|
for part in package._parts:
|
||||||
count += 6
|
count += 6
|
||||||
if part.id == NpkPartID.NULL_BLOCK:
|
if part.id == NpkPartID.NULL_BLOCK:
|
||||||
|
|
@ -241,23 +241,12 @@ class NovaPackage(Package):
|
||||||
pad_len = (4096 - (count % 4096)) % 4096
|
pad_len = (4096 - (count % 4096)) % 4096
|
||||||
package[NpkPartID.NULL_BLOCK].data = b'\x00' * pad_len
|
package[NpkPartID.NULL_BLOCK].data = b'\x00' * pad_len
|
||||||
|
|
||||||
else:
|
set_null(self)
|
||||||
has_squashfs = False
|
offset = get_size(self)
|
||||||
for part in self._parts:
|
for package in self._packages:
|
||||||
if part.id == NpkPartID.SQUASHFS:
|
set_null(package,offset)
|
||||||
part.data = rebuild_squashfs(part.data)
|
offset += get_size(package,0)
|
||||||
has_squashfs = True
|
|
||||||
if not has_squashfs:
|
|
||||||
return
|
|
||||||
count = 8
|
|
||||||
for part in self._parts:
|
|
||||||
count += 6
|
|
||||||
if part.id == NpkPartID.NULL_BLOCK:
|
|
||||||
break
|
|
||||||
count += len(part.data)
|
|
||||||
count += 6
|
|
||||||
pad_len = (4096 - (count % 4096)) % 4096
|
|
||||||
self[NpkPartID.NULL_BLOCK].data = b'\x00' * pad_len
|
|
||||||
def get_digest(self,hash_fnc,package:Package=None)->bytes:
|
def get_digest(self,hash_fnc,package:Package=None)->bytes:
|
||||||
parts = package._parts if package else self._parts
|
parts = package._parts if package else self._parts
|
||||||
for part in parts:
|
for part in parts:
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue