]> gitweb.factorcode.org Git - factor.git/blob - extra/compression/bzip3/ffi/ffi.factor
add bzip3 compression
[factor.git] / extra / compression / bzip3 / ffi / ffi.factor
1 ! Copyright (C) 2022 Raghu Ranganathan.
2 ! See http://factorcode.org/license.txt for BSD license.
3
4 ! Makes use of Kamila Szewczyk's bzip3 library.
5 ! See https://github.com/kspalaiologos/bzip3/blob/master/include/libbz3.h for the API specifics.
6 USING: alien alien.libraries alien.c-types alien.syntax
7        classes.struct combinators system words ;
8 IN: compression.bzip3.ffi
9 << "bzip3" {
10   { [ os windows? ] [ "bzip3.dll" ] }
11   { [ os macosx? ] [ "libbzip3.dylib" ] }
12   { [ os unix? ] [ "libbzip3.so" ] }
13 } cond cdecl add-library >>
14
15 LIBRARY: bzip3
16
17 TYPEDEF: uint8_t u8
18 TYPEDEF: uint16_t u16
19 TYPEDEF: uint32_t u32
20 TYPEDEF: uint64_t u64
21 TYPEDEF: int8_t s8
22 TYPEDEF: int16_t s16
23 TYPEDEF: int32_t s32
24
25
26 ! typedef struct {
27 !     /* Input/output. */
28 !     u8 *in_queue, *out_queue;
29 !     s32 input_ptr, output_ptr, input_max;
30
31 !     /* C0, C1 - used for making the initial prediction, C2 used for an APM with a slightly low
32 !        learning rate (6) and 512 contexts. kanzi merges C0 and C1, uses slightly different
33 !        counter initialisation code and prediction code which from my tests tends to be suboptimal. */
34 !     u16 C0[256], C1[256][256], C2[512][17];
35 ! } state;
36 STRUCT: state
37   { in_queue u8* } { out_queue u8* }
38   { input_ptr s32 } { output_ptr s32 } { input_max s32 }
39   { C0 u16[256] } { C1 u16[256][256] } { C2 u16[512][17] }
40 ;
41
42 ! struct bz3_state {
43 !     u8 * swap_buffer;
44 !     s32 block_size;
45 !     s32 *sais_array, *lzp_lut;
46 !     state * cm_state;
47 !     s8 last_error;
48 ! };
49 STRUCT: bz3_state 
50   { swap_buffer u8* }
51   { block_size s32 }
52   { sais_array s32* } { lzp_lut s32* }
53   { cm_state state* }
54   { last_error s8 }
55 ;
56
57 FUNCTION: c-string bz3_version ( )
58 FUNCTION: int8_t bz3_last_error ( bz3_state* state )
59 FUNCTION: c-string bz3_strerror ( bz3_state* state )
60 FUNCTION: bz3_state* bz3_new ( int32_t block_size )
61 FUNCTION: void bz3_free ( bz3_state* state )
62 FUNCTION: size_t bz3_bound ( size_t input_size )
63
64 ! HIGH LEVEL APIs
65 FUNCTION: int bz3_compress ( uint32_t block_size, uint8_t* in, uint8_t* out, size_t in_size, size_t* out_size )
66 FUNCTION: int bz3_decompress ( uint8_t* in, uint8_t* out, size_t in_size, size_t* out_size )
67
68 ! LOW LEVEL APIs
69 FUNCTION: int32_t bz3_encode_block ( bz3_state* state, uint8_t* buffer, int32_t size )
70 FUNCTION: int32_t bz3_decode_block ( bz3_state* state, uint8_t* buffer, int32_t size, int32_t orig_size )
71 FUNCTION: void bz3_encode_blocks ( bz3_state* states[], uint8_t* buffers[], int32_t sizes[], int32_t n )
72 FUNCTION: void bz3_decode_blocks ( bz3_state* states[], uint8_t* buffers[], int32_t sizes[], int32_t orig_sizes[], int32_t n )
73