mirror of
https://github.com/aaru-dps/Aaru.Compression.Native.git
synced 2025-12-16 11:14:30 +00:00
Add ARC non-repeat packing decompression implementation and tests.
This commit is contained in:
@@ -139,7 +139,8 @@ add_library("Aaru.Compression.Native" SHARED library.c apple_rle.c apple_rle.h a
|
||||
zoo/lh5.c
|
||||
zoo/lzh.h
|
||||
zoo/ar.h
|
||||
zoo/maketbl.c)
|
||||
zoo/maketbl.c
|
||||
arc/pack.c)
|
||||
|
||||
include(3rdparty/bzip2.cmake)
|
||||
include(3rdparty/flac.cmake)
|
||||
|
||||
78
arc/pack.c
Normal file
78
arc/pack.c
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* This file is part of the Aaru Data Preservation Suite.
|
||||
* Copyright (c) 2019-2025 Natalia Portillo.
|
||||
* Copyright © 2018-2019 David Ryskalczyk
|
||||
*
|
||||
* 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 <stdint.h>
|
||||
#include <string.h>
|
||||
#include "../library.h"
|
||||
|
||||
#define DLE 0x90 // Data Link Escape character, used as a repeat marker.
|
||||
|
||||
// Decompresses data using non-repeat packing.
|
||||
// This algorithm encodes runs of identical bytes.
|
||||
int arc_decompress_pack(const unsigned char *in_buf, size_t in_len, unsigned char *out_buf, size_t *out_len)
|
||||
{
|
||||
// Basic validation of pointers.
|
||||
if(!in_buf || !out_buf || !out_len) { return -1; }
|
||||
|
||||
size_t in_pos = 0;
|
||||
size_t out_pos = 0;
|
||||
unsigned char state = 0; // 0 for normal (NOHIST), 1 for in-repeat (INREP).
|
||||
unsigned char lastc = 0; // Last character seen.
|
||||
|
||||
// Loop through the input buffer until it's exhausted or the output buffer is full.
|
||||
while(in_pos < in_len && out_pos < *out_len)
|
||||
{
|
||||
if(state == 1)
|
||||
{ // We are in a repeat sequence.
|
||||
if(in_buf[in_pos])
|
||||
{ // The byte after DLE is the repeat count.
|
||||
unsigned char count = in_buf[in_pos];
|
||||
// Write the last character 'count' times.
|
||||
while(--count && out_pos < *out_len) { out_buf[out_pos++] = lastc; }
|
||||
}
|
||||
else
|
||||
{ // A count of 0 means the DLE character itself should be written.
|
||||
if(out_pos < *out_len) { out_buf[out_pos++] = DLE; }
|
||||
}
|
||||
state = 0; // Return to normal state.
|
||||
in_pos++;
|
||||
}
|
||||
else
|
||||
{ // Normal state.
|
||||
if(in_buf[in_pos] != DLE)
|
||||
{ // Not a repeat sequence.
|
||||
if(out_pos < *out_len)
|
||||
{
|
||||
// Copy the character and save it as the last character.
|
||||
out_buf[out_pos++] = lastc = in_buf[in_pos];
|
||||
}
|
||||
}
|
||||
else
|
||||
{ // DLE marks the start of a repeat sequence.
|
||||
state = 1; // Enter repeat state.
|
||||
}
|
||||
in_pos++;
|
||||
}
|
||||
}
|
||||
|
||||
// Update the output length to the number of bytes written.
|
||||
*out_len = out_pos;
|
||||
// Return success.
|
||||
return 0;
|
||||
}
|
||||
@@ -118,4 +118,7 @@ AARU_EXPORT int AARU_CALL lh5_decompress(const uint8_t *in_buf, size_t in_len, u
|
||||
|
||||
AARU_EXPORT uint64_t AARU_CALL AARU_get_acn_version();
|
||||
|
||||
// ARC method 3: Stored with non-repeat packing
|
||||
AARU_EXPORT int AARU_CALL arc_decompress_pack(const unsigned char *in_buf, size_t in_len, unsigned char *out_buf, size_t *out_len);
|
||||
|
||||
#endif // AARU_COMPRESSION_NATIVE_LIBRARY_H
|
||||
|
||||
@@ -45,9 +45,13 @@ file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/alice29.lzd
|
||||
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/alice29.lh5
|
||||
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/)
|
||||
|
||||
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/arcpack.bin
|
||||
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/)
|
||||
|
||||
# 'Google_Tests_run' is the target name
|
||||
# 'test1.cpp tests2.cpp' are source files with tests
|
||||
add_executable(tests_run apple_rle.cpp crc32.c crc32.h adc.cpp bzip2.cpp lzip.cpp lzfse.cpp zstd.cpp lzma.cpp flac.cpp
|
||||
zoo/lzd.cpp
|
||||
lh5.cpp)
|
||||
zoo/lzd.cpp arc/pack.cpp
|
||||
lh5.cpp
|
||||
arc/pack.cpp)
|
||||
target_link_libraries(tests_run gtest gtest_main "Aaru.Compression.Native")
|
||||
|
||||
82
tests/arc/pack.cpp
Normal file
82
tests/arc/pack.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* This file is part of the Aaru Data Preservation Suite.
|
||||
* Copyright (c) 2019-2025 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 "../../library.h"
|
||||
#include "../crc32.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#define EXPECTED_CRC32 0x66007dba
|
||||
|
||||
static const uint8_t *buffer;
|
||||
|
||||
class packFixture : public ::testing::Test
|
||||
{
|
||||
public:
|
||||
packFixture()
|
||||
{
|
||||
// initialization;
|
||||
// can also be done in SetUp()
|
||||
}
|
||||
|
||||
protected:
|
||||
void SetUp()
|
||||
{
|
||||
char path[PATH_MAX];
|
||||
char filename[PATH_MAX];
|
||||
|
||||
getcwd(path, PATH_MAX);
|
||||
snprintf(filename, PATH_MAX, "%s/data/arcpack.bin", path);
|
||||
|
||||
FILE *file = fopen(filename, "rb");
|
||||
buffer = (const uint8_t *)malloc(149855);
|
||||
fread((void *)buffer, 1, 149855, file);
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
void TearDown() { free((void *)buffer); }
|
||||
|
||||
~packFixture()
|
||||
{
|
||||
// resources cleanup, no exceptions allowed
|
||||
}
|
||||
|
||||
// shared user data
|
||||
};
|
||||
|
||||
TEST_F(packFixture, pack)
|
||||
{
|
||||
uint8_t params[] = {0x5D, 0x00, 0x00, 0x00, 0x02};
|
||||
size_t destLen = 152089;
|
||||
size_t srcLen = 149855;
|
||||
auto *outBuf = (uint8_t *)malloc(152089);
|
||||
|
||||
auto err = arc_decompress_pack(buffer, srcLen, outBuf, &destLen);
|
||||
|
||||
EXPECT_EQ(err, 0);
|
||||
EXPECT_EQ(destLen, 152089);
|
||||
|
||||
auto crc = crc32_data(outBuf, 152089);
|
||||
|
||||
free(outBuf);
|
||||
|
||||
EXPECT_EQ(crc, EXPECTED_CRC32);
|
||||
}
|
||||
3628
tests/data/arcpack.bin
Executable file
3628
tests/data/arcpack.bin
Executable file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user