mirror of
https://github.com/aaru-dps/Aaru.Compression.Native.git
synced 2026-09-16 19:56:52 +00:00
Every exported buffer transform now has the same shape:
int32_t AARU_xxx(const uint8_t *src_buffer, size_t src_size,
uint8_t *dst_buffer, size_t *dst_size, ...extras);
On entry *dst_size is the capacity of dst_buffer; on success it holds the
number of bytes written. The return value is 0 (AARU_ERROR_NONE) on success
and non-zero on error, either an AARU_ERROR_* constant or the underlying
library's own error code when it is propagated.
Changes by category:
- Parameter reorder only: all AARU_zip_*, AARU_stuffit*, AARU_dd_*,
AARU_cpt_*, AARU_lzo_*, AARU_xz_*, AARU_lzma2_*, AARU_kencode_*,
AARU_apple_lzh_* and ace_decompress_*.
- Byte count return replaced by an out-parameter plus a status code:
AARU_adc_*, AARU_apple_rle_*, AARU_lz4_*, AARU_lzip_*, AARU_lzfse_*,
AARU_lzvn_*, AARU_zstd_* and AARU_flac_*. Their algorithm bodies are kept
as static internals behind a thin exported wrapper.
- Type normalisation: bzip2 uint32_t* -> size_t*, zstd void* -> uint8_t*,
ZIP PPMd size_t -> size_t*, and int/unsigned char -> int32_t/uint8_t
throughout the header.
- AARU_lzma_decode_buffer no longer takes src_size as an in/out pointer; the
consumed length is tracked internally.
- Already conforming decompressors (arc_*, pak_*, ha_*, lha_*, larc_*,
pmarc_*, arj*, rar*, lh5_decompress) only had their declarations
normalised.
The streaming LZD context API and AARU_get_acn_version are left alone, as
they are not buffer transforms.
Tests updated to the new call convention; all 78 pass.
246 lines
7.0 KiB
C++
246 lines
7.0 KiB
C++
/*
|
|
* This file is part of the Aaru Data Preservation Suite.
|
|
* Copyright (c) 2019-2026 Natalia Portillo.
|
|
*
|
|
* This library is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License as
|
|
* published by the Free Software Foundation; either version 2.1 of the
|
|
* License, or (at your option) any later version.
|
|
*
|
|
* This library is distributed in the hope that it will be useful, but
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <climits>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
|
|
#include "../../library.h"
|
|
#include "../crc32.h"
|
|
#include "dart_helpers.h"
|
|
#include "gtest/gtest.h"
|
|
|
|
typedef struct
|
|
{
|
|
uint8_t *data;
|
|
size_t total_size;
|
|
dart_header_t header;
|
|
int16_t block_lengths[DART_BLOCK_ARRAY_LEN_HIGH];
|
|
int num_blocks;
|
|
uint8_t *raw;
|
|
size_t raw_size;
|
|
size_t data_offset;
|
|
} dart_image_t;
|
|
|
|
static size_t read_file_to_buffer(const char *path, uint8_t **out)
|
|
{
|
|
FILE *f = fopen(path, "rb");
|
|
if(!f) return 0;
|
|
fseek(f, 0, SEEK_END);
|
|
long sz = ftell(f);
|
|
fseek(f, 0, SEEK_SET);
|
|
if(sz <= 0)
|
|
{
|
|
fclose(f);
|
|
return 0;
|
|
}
|
|
*out = (uint8_t *)malloc(sz);
|
|
if(!*out)
|
|
{
|
|
fclose(f);
|
|
return 0;
|
|
}
|
|
size_t rd = fread(*out, 1, sz, f);
|
|
fclose(f);
|
|
if(rd != (size_t)sz)
|
|
{
|
|
free(*out);
|
|
*out = NULL;
|
|
return 0;
|
|
}
|
|
return (size_t)sz;
|
|
}
|
|
|
|
static size_t lzip_decompress_file(const char *path, uint8_t **out)
|
|
{
|
|
uint8_t *lz_buf = NULL;
|
|
size_t lz_size = read_file_to_buffer(path, &lz_buf);
|
|
if(lz_size == 0) return 0;
|
|
|
|
size_t try_sizes[] = {512 * 1024, 1024 * 1024, 2 * 1024 * 1024};
|
|
for(int i = 0; i < 3; i++)
|
|
{
|
|
*out = (uint8_t *)malloc(try_sizes[i]);
|
|
size_t out_len = try_sizes[i];
|
|
int32_t result = AARU_lzip_decode_buffer(lz_buf, lz_size, *out, &out_len);
|
|
if(result == 0 && out_len > 0)
|
|
{
|
|
free(lz_buf);
|
|
return out_len;
|
|
}
|
|
free(*out);
|
|
*out = NULL;
|
|
}
|
|
free(lz_buf);
|
|
return 0;
|
|
}
|
|
|
|
static bool load_dart_image(const char *path, dart_image_t *img)
|
|
{
|
|
memset(img, 0, sizeof(*img));
|
|
|
|
img->raw_size = lzip_decompress_file(path, &img->raw);
|
|
if(img->raw_size == 0) return false;
|
|
|
|
img->data_offset = dart_parse_header(img->raw, img->raw_size, &img->header, img->block_lengths, &img->num_blocks);
|
|
if(img->data_offset == 0)
|
|
{
|
|
free(img->raw);
|
|
img->raw = NULL;
|
|
return false;
|
|
}
|
|
|
|
int active_blocks = 0;
|
|
for(int i = 0; i < img->num_blocks; i++)
|
|
{
|
|
if(img->block_lengths[i] != 0) active_blocks++;
|
|
}
|
|
img->total_size = active_blocks * DART_BUFFER_SIZE;
|
|
|
|
if(img->header.srcCmp == DART_COMPRESS_RLE)
|
|
{
|
|
img->data = (uint8_t *)malloc(img->total_size);
|
|
size_t out_pos = 0;
|
|
size_t in_pos = img->data_offset;
|
|
|
|
for(int i = 0; i < img->num_blocks; i++)
|
|
{
|
|
if(img->block_lengths[i] == 0) continue;
|
|
|
|
if(img->block_lengths[i] == -1)
|
|
{
|
|
if(in_pos + DART_BUFFER_SIZE <= img->raw_size)
|
|
memcpy(img->data + out_pos, img->raw + in_pos, DART_BUFFER_SIZE);
|
|
in_pos += DART_BUFFER_SIZE;
|
|
}
|
|
else
|
|
{
|
|
size_t comp_size = (size_t)img->block_lengths[i] * 2;
|
|
if(in_pos + comp_size <= img->raw_size)
|
|
{
|
|
size_t rle_out_len = DART_BUFFER_SIZE;
|
|
AARU_apple_rle_decode_buffer(img->raw + in_pos, comp_size, img->data + out_pos, &rle_out_len);
|
|
}
|
|
in_pos += comp_size;
|
|
}
|
|
out_pos += DART_BUFFER_SIZE;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static void free_dart_image(dart_image_t *img)
|
|
{
|
|
if(img->data) free(img->data);
|
|
if(img->raw) free(img->raw);
|
|
memset(img, 0, sizeof(*img));
|
|
}
|
|
|
|
static bool verify_dart_pair(const char *fast_name, const char *best_name)
|
|
{
|
|
char path[PATH_MAX];
|
|
char fast_path[PATH_MAX];
|
|
char best_path[PATH_MAX];
|
|
|
|
getcwd(path, PATH_MAX);
|
|
snprintf(fast_path, PATH_MAX, "%s/data/%s", path, fast_name);
|
|
snprintf(best_path, PATH_MAX, "%s/data/%s", path, best_name);
|
|
|
|
dart_image_t fast_img, best_img;
|
|
|
|
if(!load_dart_image(fast_path, &fast_img))
|
|
{
|
|
printf(" SKIP: cannot load %s\n", fast_name);
|
|
return false;
|
|
}
|
|
if(!load_dart_image(best_path, &best_img))
|
|
{
|
|
printf(" SKIP: cannot load %s\n", best_name);
|
|
free_dart_image(&fast_img);
|
|
return false;
|
|
}
|
|
|
|
if(fast_img.header.srcCmp != DART_COMPRESS_RLE || best_img.header.srcCmp != DART_COMPRESS_LZH)
|
|
{
|
|
printf(" SKIP: unexpected compression types\n");
|
|
free_dart_image(&fast_img);
|
|
free_dart_image(&best_img);
|
|
return false;
|
|
}
|
|
|
|
size_t in_pos = best_img.data_offset;
|
|
size_t ref_block_idx = 0;
|
|
int blocks_ok = 0;
|
|
int blocks_fail = 0;
|
|
|
|
for(int i = 0; i < best_img.num_blocks; i++)
|
|
{
|
|
if(best_img.block_lengths[i] == 0)
|
|
{
|
|
if(fast_img.block_lengths[i] != 0) ref_block_idx++;
|
|
continue;
|
|
}
|
|
|
|
const uint8_t *reference = fast_img.data + ref_block_idx * DART_BUFFER_SIZE;
|
|
|
|
if(best_img.block_lengths[i] == -1)
|
|
{
|
|
if(memcmp(best_img.raw + in_pos, reference, DART_BUFFER_SIZE) == 0)
|
|
blocks_ok++;
|
|
else
|
|
blocks_fail++;
|
|
in_pos += DART_BUFFER_SIZE;
|
|
}
|
|
else
|
|
{
|
|
size_t comp_size = (size_t)best_img.block_lengths[i];
|
|
uint8_t out[DART_BUFFER_SIZE];
|
|
size_t out_len = DART_BUFFER_SIZE;
|
|
|
|
memset(out, 0, DART_BUFFER_SIZE);
|
|
|
|
int ret = AARU_apple_lzh_decode_buffer(best_img.raw + in_pos, comp_size, out, &out_len);
|
|
if(ret == 0 && out_len == DART_BUFFER_SIZE && memcmp(out, reference, DART_BUFFER_SIZE) == 0)
|
|
blocks_ok++;
|
|
else
|
|
blocks_fail++;
|
|
in_pos += comp_size;
|
|
}
|
|
ref_block_idx++;
|
|
}
|
|
|
|
printf(" %s: %d OK, %d FAIL\n", best_name, blocks_ok, blocks_fail);
|
|
|
|
free_dart_image(&fast_img);
|
|
free_dart_image(&best_img);
|
|
|
|
return blocks_fail == 0 && blocks_ok > 0;
|
|
}
|
|
|
|
TEST(DartAppleLzh, mf2dd_hfs) { EXPECT_TRUE(verify_dart_pair("mf2dd_hfs_fast.dart.lz", "mf2dd_hfs_best.dart.lz")); }
|
|
|
|
TEST(DartAppleLzh, mf2dd_mfs) { EXPECT_TRUE(verify_dart_pair("mf2dd_mfs_fast.dart.lz", "mf2dd_mfs_best.dart.lz")); }
|
|
|
|
TEST(DartAppleLzh, mf1dd_hfs) { EXPECT_TRUE(verify_dart_pair("mf1dd_hfs_fast.dart.lz", "mf1dd_hfs_best.dart.lz")); }
|
|
|
|
TEST(DartAppleLzh, mf1dd_mfs) { EXPECT_TRUE(verify_dart_pair("mf1dd_mfs_fast.dart.lz", "mf1dd_mfs_best.dart.lz")); }
|