sceutils 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
unarzl/arzl.py ADDED
@@ -0,0 +1,497 @@
1
+ from __future__ import annotations
2
+
3
+ ARZL_MAGIC = b"ARZL"
4
+
5
+ ARLZ_LITERAL_CTX_COUNT = 8
6
+ ARLZ_LITERAL_TREE_SIZE = 255
7
+ ARLZ_DIST_TREE_SIZE = 31
8
+ ARLZ_SHORT_DIST_TREE_SIZE = 7
9
+ ARLZ_DIST_SLOTS = 18
10
+ ARLZ_STATE_COUNT = 8
11
+ ARLZ_LEN_SLOTS = 8
12
+ ARLZ_EOF_LEN = 255
13
+
14
+ ERROR_INVALID_DATA = 0x80560200
15
+ ERROR_BUFFER_OVERFLOW = 0x80560201
16
+
17
+ # Byte offsets inside the SceArlzProbs structure from arlz.c.
18
+ _LIT_OFF = 0x000
19
+ _LIT_STRIDE = ARLZ_LITERAL_TREE_SIZE
20
+ _DIST_TREE_OFF = 0x7F8
21
+ _SHORT_DIST_TREE_OFF = 0x8F0
22
+ _DIST_BITS_OFF = 0x928
23
+ _IS_MATCH_OFF = 0xB68
24
+ _LEN_BITS_OFF = 0xBA8
25
+ _PROBS_SIZE = 0xCA8
26
+ # A little slack for the pointer-style accesses in the reference code.
27
+ _PROBS_PADDING = 256
28
+
29
+
30
+ class ArlzError(RuntimeError):
31
+ """Base class for ARZL errors."""
32
+
33
+ code: int
34
+
35
+ def __init__(self, code: int, message: str | None = None):
36
+ self.code = code
37
+ super().__init__(message or f"ARZL error 0x{code:08X}")
38
+
39
+
40
+ class ArlzInvalidDataError(ArlzError):
41
+ def __init__(self) -> None:
42
+ super().__init__(ERROR_INVALID_DATA, "invalid ARZL data")
43
+
44
+
45
+ class ArlzBufferOverflowError(ArlzError):
46
+ def __init__(self) -> None:
47
+ super().__init__(ERROR_BUFFER_OVERFLOW, "ARZL output buffer overflow")
48
+
49
+
50
+ class _Decoder:
51
+ __slots__ = ("src", "pos", "range", "code", "probs", "lit_shift")
52
+
53
+ def __init__(self, src: bytes) -> None:
54
+ self.src = src
55
+ self.pos = 5
56
+ prop = src[0]
57
+ fill = ((8 - ((prop >> 3) & 3)) << 4) & 0xF0
58
+ self.probs = bytearray([fill]) * (_PROBS_SIZE + _PROBS_PADDING)
59
+ self.lit_shift = prop & 7
60
+ self.range = 0xFFFFFFFF
61
+ self.code = (src[1] << 24) | (src[2] << 16) | (src[3] << 8) | src[4]
62
+
63
+ def _var_bits(self, table_off: int, slot: int, pos_ctx: int,
64
+ low_ctx: int) -> int:
65
+ r = self.range
66
+ c = self.code
67
+ p = self.pos
68
+ src = self.src
69
+ probs = self.probs
70
+ extra = slot - 3
71
+
72
+ # The reference uses a byte pointer into the probability struct, so
73
+ # offsets 8/16/24 may intentionally cross into the next sub-table.
74
+ prob_off = (table_off + slot * 32
75
+ + (((pos_ctx << slot) & 3) * 8) + (low_ctx & 7))
76
+
77
+ if (r >> 24) == 0:
78
+ r = (r << 8) & 0xFFFFFFFF
79
+ c = ((c << 8) | src[p]) & 0xFFFFFFFF
80
+ p += 1
81
+
82
+ prob = probs[prob_off]
83
+ next_prob = (prob - (prob >> 4)) & 0xFFFFFFFF
84
+ bound = prob * (r >> 8)
85
+ if c >= bound:
86
+ c -= bound
87
+ r -= bound
88
+ high = 4
89
+ val = 2
90
+ else:
91
+ next_prob = (next_prob + 15) & 0xFFFFFFFF
92
+ r = bound
93
+ high = 6
94
+ val = 3
95
+ probs[prob_off] = next_prob & 0xFF
96
+
97
+ if extra >= 0:
98
+ if extra != 0:
99
+ if (r >> 24) == 0:
100
+ r = (r << 8) & 0xFFFFFFFF
101
+ c = ((c << 8) | src[p]) & 0xFFFFFFFF
102
+ p += 1
103
+
104
+ prob = next_prob & 0xFF
105
+ next_prob = (prob - (prob >> 4)) & 0xFFFFFFFF
106
+ bound = prob * (r >> 8)
107
+ if c >= bound:
108
+ r -= bound
109
+ c -= bound
110
+ else:
111
+ next_prob = (next_prob + 15) & 0xFFFFFFFF
112
+ high = (high + 1) & 0xFFFFFFFF
113
+ r = bound
114
+ probs[prob_off] = next_prob & 0xFF
115
+
116
+ if extra != 1:
117
+ if (r >> 24) == 0:
118
+ r = (r << 8) & 0xFFFFFFFF
119
+ c = ((c << 8) | src[p]) & 0xFFFFFFFF
120
+ p += 1
121
+
122
+ while True:
123
+ r >>= 1
124
+ extra -= 1
125
+ high = ((high << 1) + (1 if c < r else 0)) & 0xFFFFFFFF
126
+ if c >= r:
127
+ c -= r
128
+ if extra == 1:
129
+ break
130
+
131
+ high = (high << 1) & 0xFFFFFFFF
132
+
133
+ if (r >> 24) == 0:
134
+ r = (r << 8) & 0xFFFFFFFF
135
+ c = ((c << 8) | src[p]) & 0xFFFFFFFF
136
+ p += 1
137
+
138
+ prob = probs[prob_off + 24]
139
+ next_prob = (prob - (prob >> 4)) & 0xFFFFFFFF
140
+ bound = prob * (r >> 8)
141
+ if c >= bound:
142
+ c -= bound
143
+ r -= bound
144
+ else:
145
+ high = (high + 1) & 0xFFFFFFFF
146
+ r = bound
147
+ next_prob = (next_prob + 15) & 0xFFFFFFFF
148
+ val = high
149
+ probs[prob_off + 24] = next_prob & 0xFF
150
+
151
+ if slot > 0:
152
+ if (r >> 24) == 0:
153
+ r = (r << 8) & 0xFFFFFFFF
154
+ c = ((c << 8) | src[p]) & 0xFFFFFFFF
155
+ p += 1
156
+
157
+ prob = probs[prob_off + 8]
158
+ val = (val << 1) & 0xFFFFFFFF
159
+ next_prob = (prob - (prob >> 4)) & 0xFFFFFFFF
160
+ bound = prob * (r >> 8)
161
+ if c >= bound:
162
+ r -= bound
163
+ c -= bound
164
+ else:
165
+ next_prob = (next_prob + 15) & 0xFFFFFFFF
166
+ val = (val + 1) & 0xFFFFFFFF
167
+ r = bound
168
+ probs[prob_off + 8] = next_prob & 0xFF
169
+
170
+ if slot != 1:
171
+ if (r >> 24) == 0:
172
+ r = (r << 8) & 0xFFFFFFFF
173
+ c = ((c << 8) | src[p]) & 0xFFFFFFFF
174
+ p += 1
175
+
176
+ prob = probs[prob_off + 16]
177
+ val = (val << 1) & 0xFFFFFFFF
178
+ next_prob = (prob - (prob >> 4)) & 0xFFFFFFFF
179
+ bound = prob * (r >> 8)
180
+ if c >= bound:
181
+ c -= bound
182
+ r -= bound
183
+ else:
184
+ val = (val + 1) & 0xFFFFFFFF
185
+ r = bound
186
+ next_prob = (next_prob + 15) & 0xFFFFFFFF
187
+ probs[prob_off + 16] = next_prob & 0xFF
188
+
189
+ self.range = r
190
+ self.code = c
191
+ self.pos = p
192
+ return val
193
+
194
+ def decode(self, capacity: int) -> tuple[bytes, int]:
195
+ src = self.src
196
+ prop = src[0]
197
+
198
+ if prop & 0x80:
199
+ size = self.code
200
+ if capacity < size:
201
+ raise ArlzBufferOverflowError()
202
+ return bytes(src[5:5 + size]), 5 + size
203
+
204
+ probs = self.probs
205
+ lit_shift = self.lit_shift
206
+ r = self.range
207
+ c = self.code
208
+ p = self.pos
209
+ out = bytearray(capacity)
210
+ out_end = capacity
211
+ out_pos = 0
212
+ sym = 0
213
+ state = 0
214
+
215
+ if out_end == 0:
216
+ raise ArlzBufferOverflowError()
217
+
218
+ while True:
219
+ # --- decode_literal ---
220
+ ctx = (sym & ~0x700) | ((out_pos & 7) << 8)
221
+ lit_off = (((ctx >> lit_shift) & 7) * _LIT_STRIDE)
222
+
223
+ sym = 1
224
+ while True:
225
+ if (r >> 24) == 0:
226
+ r = (r << 8) & 0xFFFFFFFF
227
+ c = ((c << 8) | src[p]) & 0xFFFFFFFF
228
+ p += 1
229
+
230
+ prob = probs[lit_off + sym - 1]
231
+ sym <<= 1
232
+ bound = prob * (r >> 8)
233
+ next_prob = (prob - (prob >> 3)) & 0xFFFFFFFF
234
+ r -= bound
235
+ if c < bound:
236
+ sym += 1
237
+ next_prob = (next_prob + 31) & 0xFFFFFFFF
238
+ r = bound
239
+ else:
240
+ c -= bound
241
+ probs[lit_off + (sym >> 1) - 1] = next_prob & 0xFF
242
+ if sym > 0xFF:
243
+ break
244
+
245
+ while True:
246
+ next_out = out_pos + 1
247
+ out[out_pos] = sym & 0xFF
248
+
249
+ # isMatch[0][state]: 0 = literal, 1 = match
250
+ if (r >> 24) == 0:
251
+ r = (r << 8) & 0xFFFFFFFF
252
+ c = ((c << 8) | src[p]) & 0xFFFFFFFF
253
+ p += 1
254
+
255
+ match_off = _IS_MATCH_OFF + state
256
+ prob = probs[match_off]
257
+ next_prob = (prob - (prob >> 4)) & 0xFFFFFFFF
258
+ bound = prob * (r >> 8)
259
+ if c >= bound:
260
+ probs[match_off] = next_prob & 0xFF
261
+ out_pos = next_out
262
+ state = state - 1 if state != 0 else 0
263
+ r -= bound
264
+ c -= bound
265
+ if out_pos >= out_end:
266
+ raise ArlzBufferOverflowError()
267
+ break
268
+
269
+ probs[match_off] = (next_prob + 15) & 0xFF
270
+ r = bound
271
+
272
+ # isMatch[1][state]: 0 = 2-byte match, 1 = longer match
273
+ if (r >> 24) == 0:
274
+ r = (r << 8) & 0xFFFFFFFF
275
+ c = ((c << 8) | src[p]) & 0xFFFFFFFF
276
+ p += 1
277
+
278
+ match_off = _IS_MATCH_OFF + 8 + state
279
+ prob = probs[match_off]
280
+ next_prob = (prob - (prob >> 4)) & 0xFFFFFFFF
281
+ bound = prob * (r >> 8)
282
+ if c >= bound:
283
+ r -= bound
284
+ c -= bound
285
+ probs[match_off] = next_prob & 0xFF
286
+ len_slot = -1
287
+ length = 1
288
+ tree_base = _SHORT_DIST_TREE_OFF
289
+ tree_limit = 64
290
+ else:
291
+ probs[match_off] = (next_prob + 15) & 0xFF
292
+ r = bound
293
+
294
+ len_slot = 0
295
+ while len_slot < 6:
296
+ if (r >> 24) == 0:
297
+ r = (r << 8) & 0xFFFFFFFF
298
+ c = ((c << 8) | src[p]) & 0xFFFFFFFF
299
+ p += 1
300
+
301
+ match_off = (_IS_MATCH_OFF
302
+ + (len_slot + 2) * 8 + state)
303
+ prob = probs[match_off]
304
+ next_prob = (prob - (prob >> 4)) & 0xFFFFFFFF
305
+ bound = prob * (r >> 8)
306
+ if c >= bound:
307
+ probs[match_off] = next_prob & 0xFF
308
+ c -= bound
309
+ r -= bound
310
+ break
311
+ probs[match_off] = (next_prob + 15) & 0xFF
312
+ r = bound
313
+ len_slot += 1
314
+
315
+ self.range = r
316
+ self.code = c
317
+ self.pos = p
318
+ length = self._var_bits(_LEN_BITS_OFF, len_slot,
319
+ next_out, state)
320
+ r = self.range
321
+ c = self.code
322
+ p = self.pos
323
+
324
+ if length == ARLZ_EOF_LEN:
325
+ return bytes(out[:next_out]), p
326
+
327
+ if length > 2:
328
+ tree_base = _DIST_TREE_OFF + len_slot + 1
329
+ tree_limit = 256
330
+ else:
331
+ tree_base = _SHORT_DIST_TREE_OFF + len_slot + 1
332
+ tree_limit = 64
333
+
334
+ tree_idx = 8
335
+ while True:
336
+ node_off = tree_base + tree_idx - 8
337
+ if (r >> 24) == 0:
338
+ r = (r << 8) & 0xFFFFFFFF
339
+ c = ((c << 8) | src[p]) & 0xFFFFFFFF
340
+ p += 1
341
+
342
+ prob = probs[node_off]
343
+ tree_idx <<= 1
344
+ bound = prob * (r >> 8)
345
+ next_prob = (prob - (prob >> 3)) & 0xFFFFFFFF
346
+ r -= bound
347
+ if c < bound:
348
+ tree_idx += 8
349
+ r = bound
350
+ next_prob = (next_prob + 31) & 0xFFFFFFFF
351
+ else:
352
+ c -= bound
353
+ probs[node_off] = next_prob & 0xFF
354
+ if tree_idx >= tree_limit:
355
+ break
356
+
357
+ rem = tree_idx - tree_limit
358
+ if rem != 0:
359
+ dist_slot = (rem >> 3) - 1
360
+ self.range = r
361
+ self.code = c
362
+ self.pos = p
363
+ dist = self._var_bits(_DIST_BITS_OFF, dist_slot,
364
+ 1 if length > 2 else 0,
365
+ len_slot) - 1
366
+ r = self.range
367
+ c = self.code
368
+ p = self.pos
369
+ else:
370
+ dist = 0
371
+
372
+ if dist == 0xFFFFFFFF or next_out <= dist:
373
+ raise ArlzInvalidDataError()
374
+
375
+ match_end = next_out + length
376
+ if out_end <= match_end:
377
+ raise ArlzBufferOverflowError()
378
+
379
+ # The match source is one byte before next_out - dist; this
380
+ # mirrors the byte-wise copy in the reference implementation.
381
+ src_start = next_out - dist - 1
382
+ if length <= dist + 1:
383
+ out[next_out:match_end] = out[src_start:src_start + length]
384
+ else:
385
+ for i in range(length):
386
+ out[next_out + i] = out[src_start + i]
387
+ sym = out[match_end - dist - 1]
388
+
389
+ state = 7
390
+ out_pos = match_end
391
+
392
+
393
+ def arzl_decode(src: bytes, capacity: int | None = None) -> tuple[bytes, int]:
394
+ """Decode an ARZL stream, returning ``(output, consumed)``.
395
+
396
+ ``src`` must start at the ARZL property byte (i.e. directly after the
397
+ optional ``ARZL`` magic). If ``capacity`` is omitted it is grown until
398
+ the stream fits.
399
+ """
400
+ if len(src) < 5:
401
+ raise ArlzInvalidDataError()
402
+
403
+ capacity = max(len(src), 1) if capacity is None else max(capacity, 1)
404
+
405
+ while True:
406
+ decoder = _Decoder(src)
407
+ try:
408
+ return decoder.decode(capacity)
409
+ except ArlzBufferOverflowError:
410
+ capacity *= 2
411
+ except IndexError:
412
+ raise ArlzInvalidDataError() from None
413
+
414
+
415
+ def arzl_decompress(src: bytes, capacity: int | None = None) -> bytes:
416
+ """Decode an ARZL stream. An ``ARZL`` magic prefix is optional."""
417
+ if src[:4] == ARZL_MAGIC:
418
+ src = src[4:]
419
+ return arzl_decode(src, capacity)[0]
420
+
421
+
422
+ def _get_bits(word: int, shift: int, width: int) -> int:
423
+ return (word >> shift) & ((1 << width) - 1)
424
+
425
+
426
+ def _set_bits(word: int, shift: int, width: int, value: int) -> int:
427
+ mask = ((1 << width) - 1) << shift
428
+ return (word & ~mask) | ((value << shift) & mask)
429
+
430
+
431
+ def arzl_arm_filter(data: bytes, encode: int = 0) -> bytes:
432
+ """Apply the SKBL Thumb-2 MOVT / BL filter.
433
+
434
+ ``encode`` selects the direction: ``0`` decodes (the usual case),
435
+ non-zero encodes.
436
+ """
437
+ size = len(data)
438
+ if size == 0:
439
+ return b""
440
+
441
+ buf = bytearray(data) + bytearray(4)
442
+ p = 0
443
+ while p < size:
444
+ next_off = p + 4
445
+ insn = int.from_bytes(buf[p:p + 4], "little")
446
+ op = insn & 0xF800F800
447
+
448
+ if (insn & 0x8000FBF0) == 0x0000F2C0:
449
+ # MOVT: swap the Rd and imm4 nibbles.
450
+ rd = _get_bits(insn, 24, 4)
451
+ imm4 = _get_bits(insn, 0, 4)
452
+ value = (insn & ~0xF) | rd
453
+ value = _set_bits(value, 24, 4, imm4)
454
+ buf[p:p + 4] = value.to_bytes(4, "little")
455
+ elif op == 0xF800F000:
456
+ # BL: relocate the 22-bit branch offset.
457
+ imm11_hi = _get_bits(insn, 0, 11)
458
+ imm11_lo = _get_bits(insn, 16, 11)
459
+ target = imm11_lo | (imm11_hi << 11)
460
+ offset = next_off >> 1
461
+ rel = (target - offset) if encode == 0 else (target + offset)
462
+ rel &= 0xFFFFFFFF
463
+ value = 0xF800F000
464
+ value = _set_bits(value, 0, 11, rel >> 11)
465
+ value = _set_bits(value, 16, 11, rel)
466
+ buf[p:p + 4] = value.to_bytes(4, "little")
467
+ elif (op >> 27) == 0x1E:
468
+ next_off = p + 2
469
+
470
+ p = next_off
471
+
472
+ return bytes(buf[:size])
473
+
474
+
475
+ def unarzl(data: bytes) -> bytes:
476
+ """Decode an ARZL-compressed segment and undo the ARM filtering."""
477
+ if data[:4] == ARZL_MAGIC:
478
+ data = data[4:]
479
+ return arzl_arm_filter(arzl_decompress(data), encode=0)
480
+
481
+
482
+ # Aliases matching the C function names.
483
+ sce_arlz_decode = arzl_decode
484
+ sce_arlz_arm_filter = arzl_arm_filter
485
+
486
+ __all__ = [
487
+ "ARZL_MAGIC",
488
+ "ArlzError",
489
+ "ArlzInvalidDataError",
490
+ "ArlzBufferOverflowError",
491
+ "arzl_decode",
492
+ "arzl_decompress",
493
+ "arzl_arm_filter",
494
+ "unarzl",
495
+ "sce_arlz_decode",
496
+ "sce_arlz_arm_filter",
497
+ ]