262 Commits

Author SHA1 Message Date
598d9224a7 Bound the block caches by bytes held instead of by entry count
The read caches sized themselves as MAX_CACHE_SIZE / (SectorSize << shift),
but ctx->shift is only ever assigned on the DDT v1 path, and the context is
zeroed at open. On every DDT v2 image the divisor therefore collapsed to the
sector size and the limit came out as 262144 entries, while each block_cache
entry holds a whole decompressed data block of SectorSize << dataShift bytes,
8 MiB with the default data_shift of 12. That is a ceiling of roughly 2 TiB
rather than the intended 512 MiB, so the count limit was never reached: a
sequential sector-by-sector pass cached the entire decompressed image and
freed none of it until aaruf_close(). Comparing two 40 GB images this way was
killed by the OOM reaper.

An entry count cannot express a memory budget when the values vary in size,
and here they vary by five orders of magnitude: block_header_cache holds
BlockHeader structs while block_cache holds megabyte payloads, yet both were
given the same limit. Track the byte size of each value instead and evict
least-recently-used entries until the cache is back under budget, and drop the
block geometry from cache initialization entirely. The block cache gets the
512 MiB MAX_CACHE_SIZE it always intended; block headers get a separate 8 MiB
budget, which covers far more blocks than the payload cache can hold.

The entry just inserted is never evicted, so callers may keep using the
pointer they handed over for the rest of the call. That also removes the
use-after-free that a zero limit used to cause: the old eviction loop started
at the hash head and could free the entry just added, after which the caller
read from freed memory.
2026-09-02 00:03:22 +01:00
Cacodemon345
11d667ec9b Fixes to make it compile with -Werror=strict-prototype
Also ensures that some IDE warnings get ignored only on Clang
2026-07-13 02:45:17 +06:00
a1dd1f307a Allow track 0 to be a valid data track. 2026-07-11 12:15:24 +01:00
Rebecca Wallander
a6345b7efc Handle flux-only images 2026-05-01 09:14:06 +02:00
2676d8fe0c Merge branch 'devel' of github.com:aaru-dps/libaaruformat into devel 2026-04-12 14:49:24 +01:00
540538426e Merge pull request #39 from FakeShemp/fakeshemp/write-long
Actually write the BD sector tags to file
2026-04-12 14:49:13 +01:00
a5bd3279f5 Do EC recovery on secondary level DDT2 reading. 2026-04-12 14:45:52 +01:00
1f7532b0ee Add EC recovery of non-data blocks. 2026-04-12 14:37:54 +01:00
81c367995f Update recovery footer size and related documentation for AaruRecoveryFooter 2026-04-12 14:17:56 +01:00
Rebecca Wallander
71f1ab5290 Actually write the BD sector tags to file 2026-04-12 15:09:38 +02:00
0811c4de50 Adjust auto erasure coding setter. 2026-04-11 22:49:41 +01:00
edc65f6b1c Add function to set EC based on recoverability percentage. 2026-04-11 22:11:56 +01:00
6f7cfbdf5e Recover EC of other block types. 2026-04-11 21:27:12 +01:00
92ca533db8 Use EC to recover damaged data blocks. 2026-04-11 21:02:44 +01:00
af4503f376 Write EC when writing data blocks. 2026-04-11 20:28:48 +01:00
eb188f48d2 Add erasure coding structures. 2026-04-11 20:16:18 +01:00
acfa2f0d2f Add data types for erasure coding. 2026-04-11 20:13:59 +01:00
2aea0dbb5a Fix max sector and media tags. 2026-04-11 12:00:33 +01:00
Rebecca Wallander
d6b6accbf5 Add raw blu-ray and aacs tags 2026-04-10 19:47:34 +02:00
112f53dc3d More 64-bit file offset fixes. 2026-04-04 16:32:25 +01:00
774ccbc8ae Fix tests compilation in MSVC. 2026-04-04 15:54:38 +01:00
2aac1ce89c refactor: update file I/O to support large files with aaru_off_t type 2026-04-04 15:10:46 +01:00
Kevin Bortis
d8ff7b4a28 refactor: split close.c into reader/writer
Separate writer finalization from resource cleanup in close.c to
enable a future reader-only library target (aaruformatread).

close_write.c contains all write_* helpers and aaruf_finalize_write().
close.c retains aaruf_close() with cleanup only, dispatching writer
finalization via a function pointer set by aaruf_create()/aaruf_open().

Zero behavioral change — same tests pass, same error semantics.
2026-04-02 08:32:10 +02:00
Kevin Bortis
6fabbefb30 Extend zstd compression to all sections, add featureIncompatible bit 2026-04-01 19:45:41 +02:00
Kevin Bortis
3c00aed352 Replace hardcoded LZMA thread count with LZMA_THREADS(ctx)
The LZMA encoder was called with numThreads=8 at all 19 call sites,
but LZMA only supports 1 or 2 (and _7ZIP_ST forces it to 1 anyway).
Replace with LZMA_THREADS(ctx) macro that clamps ctx->num_threads
to [1, 2], making the code say what it means and honoring the
consumer's threading preference from the threads=N option.
2026-03-29 19:14:38 +02:00
Kevin Bortis
120174e4cd Add multi-threaded zstd compression support
Switch zstd encoder from simple API (ZSTD_compress) to advanced API
(ZSTD_CCtx + ZSTD_compress2) with configurable worker threads via
ZSTD_c_nbWorkers. Consumer controls threading through threads=N
option string parameter. Default is 1 (single-threaded, bit-identical
output to previous behavior).

- Enable ZSTD_MULTITHREAD compile definition and link pthreads
- Add num_threads field to options struct and context
- Parse threads=N in option string (clamped >= 1)
- Rewrite aaruf_zstd_encode_buffer() with num_threads parameter
- Update all call sites (write.c, close.c)
2026-03-29 12:56:19 +02:00
Kevin Bortis
8a8a89450d lru: use native uint64 keys instead of string conversion
Replace string-keyed uthash lookups with HASH_FIND/HASH_ADD on native
uint64_t keys. This eliminates per-lookup malloc/snprintf/strlen/free
overhead from the block cache hot path (2 lookups per sector read).

Before: malloc(17) + snprintf hex + HASH_FIND_STR + free per lookup
After:  HASH_FIND with 8-byte integer key, zero allocations

Also removes the unused string-key API (find_in_cache/add_to_cache)
and the uint64_to_string helper — all callers use uint64 keys.

Fix off-by-one in eviction: >= caused cache to hold max_items-1
entries instead of max_items. Changed to > so the configured
capacity is honored exactly.
2026-03-19 20:35:42 +01:00
6dbd5f1bb2 Merge pull request #12 from RomTholos/fix/cross-compilation-arm-riscv
Fix cross-compilation for ARM FPU variants and add RISC-V support
2026-03-19 11:58:49 +00:00
Kevin Bortis
bd01e7d8a5 Fix cross-compilation for ARM FPU variants and add RISC-V support 2026-03-19 12:34:00 +01:00
Kevin Bortis
6be36b6bda Add Zstandard compression support
Implements zstd as an alternative to LZMA for data block and
subchannel compression using the allocated compression IDs 4
(kCompressionZstd) and 5 (kCompressionZstdCst).

Adds zstd v1.5.7 as a bundled submodule with a static library
build including x86_64 assembly fast path for decompression.

Write path selects zstd or LZMA based on the zstd option.
Subchannel blocks use zstd+CST instead of LZMA+CST when enabled.
LZMA properties header is only written for LZMA-based compression
types, preventing format corruption in zstd-compressed images.

Activated via options string: "zstd=true;zstd_level=19".
Default remains LZMA for backwards compatibility.

Tested: SHA-256 verified lossless roundtrips across 9 disc systems
(Dreamcast, Saturn, Mega CD, PC Engine CD, Neo Geo CD, PS1, PS2 CD,
PS2 DVD) and PS1 SBI subchannel preservation with zstd+CST.
2026-03-19 08:45:07 +01:00
Kevin Bortis
95908b328a Reserve compression IDs 4 and 5 for Zstandard (zstd)
Adds kCompressionZstd = 4 and kCompressionZstdCst = 5 to the
CompressionType enum, following the existing LZMA/LzmaCst pattern.

Ref: https://github.com/aaru-dps/libaaruformat/issues/5
2026-03-18 22:20:26 +01:00
694601cc63 Enhance Wii junk data handling: reconstruct junk in user data area during read and write operations 2026-03-16 22:54:53 +00:00
5b93c56e7b Add support for Nintendo Wii and GameCube junk map and encryption: implement lazy initialization for NgcwJunkEntry and WiiPartitionRegion 2026-03-16 18:34:24 +00:00
53bbb3c7a6 Add new data types for Nintendo Wii and GameCube: introduce WiiPartitionKeyMap and NgcwJunkMap for enhanced region mapping 2026-03-16 18:31:27 +00:00
7ee97105b5 Add new sector status: introduce SectorStatusGenerable to indicate content that can be generated using a known algorithm 2026-03-16 17:28:54 +00:00
913d0508a2 Add recursion guard for Wii U re-encryption: implement flag to prevent recursive calls during sector processing 2026-03-16 13:28:16 +00:00
93357cbf1f Add Wii U encryption support to context structure and cleanup logic 2026-03-16 11:11:40 +00:00
5857e16662 Add Wii U partition-to-key mapping to enums and media tags 2026-03-16 10:51:17 +00:00
73dac131b8 Implement PS3 encryption support with lazy initialization and error handling 2026-03-15 19:12:59 +00:00
d53faf7a38 Rename track type enums for consistency 2026-03-15 18:18:15 +00:00
3d81f50afe Rename data type enum. 2026-03-14 21:45:02 +00:00
a5b8054a14 Rename compression enum. 2026-03-14 20:39:56 +00:00
ce9f6ba610 Rename media tags. 2026-03-14 20:18:35 +00:00
43b226bbfa Rename sector tags. 2026-03-14 19:54:52 +00:00
6fa82b4dfe Add feature compatibility checks for V2+ images and define new error codes 2026-03-14 18:52:51 +00:00
1516e9ff24 Add PS3 related media tags. 2026-03-09 20:11:07 +00:00
30965a0c94 Add Nintendo Wii U disc key to enums and update related functions 2026-03-08 19:59:28 +00:00
4df22c47ef Add Floppy Write-Protect status to media tag and data type enums 2026-01-06 14:00:55 +00:00
Rebecca Wallander
c412030a8b Use block alignment offset instead of absolute offset 2026-01-01 13:37:21 +01:00
Rebecca Wallander
86b6680e3e Add data type to datastream block 2026-01-01 13:37:20 +01:00