Skip to content

Commit

Permalink
ppsf (legacy): fix encoded length field
Browse files Browse the repository at this point in the history
  • Loading branch information
SoulMelody committed Dec 27, 2024
1 parent 7c3c6f2 commit 4bb7e6b
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions libresvip/plugins/ppsf/legacy_model.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import math

from construct import (
Byte,
Bytes,
BytesInteger,
Const,
ExprAdapter,
FixedSized,
FocusedSeq,
GreedyRange,
Int16ub,
Int16ul,
Int32ub,
Mapping,
PaddedString,
PascalString,
Expand All @@ -18,47 +20,55 @@
Struct,
Subconstruct,
Switch,
obj_,
this,
)
from construct_typed import Context

Int32ul = BytesInteger(4, swapped=True)


def ppsf_int_encoder(value: int, ctx: Context) -> int:
low = value & 0x7F
high = (value >> 7) << 8
width = math.floor(math.log(high, 256))
base = 0x80 << (width * 8)
high += base
return high + low


def ppsf_int_decoder(value: int, ctx: Context) -> int:
high = value >> 8
low = value & 0xFF
width = math.floor(math.log(high, 256))
base = 0x80 << (width * 8)
high -= base
return (high << 7) + low


def ppsf_prefixed_array(subcon: Subconstruct) -> Select:
return Select(
PrefixedArray(Byte, subcon),
PrefixedArray(
ExprAdapter(
Int16ub,
encoder=obj_ ^ 56960,
decoder=obj_ ^ 56960,
encoder=ppsf_int_encoder,
decoder=ppsf_int_decoder,
),
subcon,
),
PrefixedArray(
FocusedSeq(
"size",
Const(b"\x81"),
"size"
/ ExprAdapter(
Byte,
encoder=obj_ ^ 128,
decoder=obj_ ^ 128,
),
ExprAdapter(
BytesInteger(3),
encoder=ppsf_int_encoder,
decoder=ppsf_int_decoder,
),
subcon,
),
PrefixedArray(
FocusedSeq(
"size",
Const(b"\x81"),
"size"
/ ExprAdapter(
Int16ub,
encoder=obj_ * 2 - (obj_ & 127),
decoder=((obj_ & 127) + obj_) // 2,
),
ExprAdapter(
Int32ub,
encoder=ppsf_int_encoder,
decoder=ppsf_int_decoder,
),
subcon,
),
Expand Down

0 comments on commit 4bb7e6b

Please sign in to comment.