Files
Aaru.Compression.Native/adc.c
Natalia Portillo adecbc7c7f Unify external API signatures across all buffer transforms
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.
2026-08-31 12:26:05 +01:00

146 lines
4.1 KiB
C

//
// Created by claunia on 17/10/21.
//
#include <stdint.h>
#include <string.h>
#include "library.h"
#include "adc.h"
FORCE_INLINE int GetChunkType(uint8_t byt)
{
if(byt & 0x80) return ADC_PLAIN;
if(byt & 0x40) return ADC_THREE_BYTE;
return ADC_TWO_BYTE;
}
FORCE_INLINE int GetChunkSize(uint8_t byt)
{
switch(GetChunkType(byt))
{
case ADC_PLAIN:
return (byt & 0x7F) + 1;
case ADC_TWO_BYTE:
return ((byt & 0x3F) >> 2) + 3;
case ADC_THREE_BYTE:
return (byt & 0x3F) + 4;
default:
return -1;
}
}
FORCE_INLINE int GetOffset(uint8_t chunk[])
{
switch(GetChunkType(chunk[0]))
{
case ADC_PLAIN:
return 0;
case ADC_TWO_BYTE:
return ((chunk[0] & 0x03) << 8) + chunk[1];
case ADC_THREE_BYTE:
return (chunk[1] << 8) + chunk[2];
default:
return -1;
}
}
static int32_t adc_decode(uint8_t *dst_buffer, int32_t dst_size, const uint8_t *src_buffer, int32_t src_size)
{
int inputPosition = 0;
int chunkSize;
int offset;
int chunkType;
int outPosition = 0;
uint8_t temp[3];
uint8_t readByte;
uint8_t lastByte;
int i;
while(inputPosition < src_size)
{
readByte = src_buffer[inputPosition++];
chunkType = GetChunkType(readByte);
switch(chunkType)
{
case ADC_PLAIN:
chunkSize = GetChunkSize(readByte);
if(outPosition + chunkSize > dst_size) goto finished;
memcpy(dst_buffer + outPosition, src_buffer + inputPosition, chunkSize);
outPosition += chunkSize;
inputPosition += chunkSize;
break;
case ADC_TWO_BYTE:
chunkSize = GetChunkSize(readByte);
temp[0] = readByte;
temp[1] = src_buffer[inputPosition++];
offset = GetOffset(temp);
if(outPosition + chunkSize > dst_size) goto finished;
if(offset == 0)
{
lastByte = dst_buffer[outPosition - 1];
for(i = 0; i < chunkSize; i++)
{
dst_buffer[outPosition] = lastByte;
outPosition++;
}
}
else
{
for(i = 0; i < chunkSize; i++)
{
dst_buffer[outPosition] = dst_buffer[outPosition - offset - 1];
outPosition++;
}
}
break;
case ADC_THREE_BYTE:
chunkSize = GetChunkSize(readByte);
temp[0] = readByte;
temp[1] = src_buffer[inputPosition++];
temp[2] = src_buffer[inputPosition++];
offset = GetOffset(temp);
if(outPosition + chunkSize > dst_size) goto finished;
if(offset == 0)
{
lastByte = dst_buffer[outPosition - 1];
for(i = 0; i < chunkSize; i++)
{
dst_buffer[outPosition] = lastByte;
outPosition++;
}
}
else
{
for(i = 0; i < chunkSize; i++)
{
dst_buffer[outPosition] = dst_buffer[outPosition - offset - 1];
outPosition++;
}
}
break;
}
}
finished:
return outPosition;
}
AARU_EXPORT int32_t AARU_CALL AARU_adc_decode_buffer(const uint8_t *src_buffer, size_t src_size, uint8_t *dst_buffer,
size_t *dst_size)
{
int32_t written;
if(!src_buffer || !dst_buffer || !dst_size) return AARU_ERROR_INVALID_ARGUMENT;
written = adc_decode(dst_buffer, (int32_t)*dst_size, src_buffer, (int32_t)src_size);
if(written < 0) return AARU_ERROR_FAILURE;
*dst_size = (size_t)written;
return AARU_ERROR_NONE;
}