📜 ⬆️ ⬇️

libral - layer of abstraction access to compression libraries

Hi Habr! I want to submit my C / C ++ library libral, which has recently become open source under the GPL3 license. Perhaps someone it will be useful. The library provides a single interface to various lossless data compression algorithms.
Currently supported libraries:


This library allows you to easily switch between different compression algorithms, which will help you choose the best for the task with minimal code changes. Using it is very easy to write benchmarks, the code below.
The library is under development, so you should not count on stability for the time being.
The API is pretty simple. Each compression library is described by its driver with a single interface. At the moment (v0.1), access via the file I / O functions is not implemented for all drivers, while you can confidently use compress () / uncompress ().

struct file_driver_t{ fd_t *(* open)(const char *fname, int mode, void *params); int (* close)(fd_t *fd); off_t (* seek)(fd_t *fd, size_t offset, int whence); off_t (* tell)(fd_t *fd); size_t(* read)(fd_t *fd, char *buf, size_t size); size_t(* write)(fd_t *fd, char *buf, size_t size); int (* feof)(fd_t *fd); int (* ferror)(fd_t *fd); enum ral_status (* compress)(char *dest, size_t *destLen, const char *source, size_t sourceLen, void *param); enum ral_status (* uncompress)(char *dest, size_t *destLen, const char *source, size_t sourceLen, void *param); }; 

To select a specific driver, use the get_driver function:
 enum drivers_t {RAL_STDC, RAL_GZ, RAL_LZO, RAL_SNAPPY, RAL_BZIP, RAL_END}; struct file_driver_t *get_driver(enum drivers_t d); 

Example of use:
 #include <ral.h> #include <stdio.h> #include <stdlib.h> #include <string.h> void fill_rand(char *buf, size_t size){ // we want to fill half, to show compression for(size_t i = 0; i < size / 2; i++){ buf[i] = (char)rand(); } } int main(){ static const size_t size_small = 1000000; // some drivers need more space to outbufer while compressing static const size_t size_big = 2*size_small; static char in[size_small]; static char tmp[size_big]; static char out[size_big]; // you can simply change drivers here struct file_driver_t *dr = get_driver(RAL_BZIP); if( dr == NULL || dr->compress == NULL || dr->uncompress == NULL ){ fprintf(stderr, "cant get driver\n"); return -1; } fill_rand(in, size_small); size_t tmp_len = size_big; if( dr->compress(tmp, &tmp_len, in, size_small, NULL) != RAL_OK ){ fprintf(stderr, "compress failed\n"); return -1; } printf("Compressed: in size: %ld out size: %ld\n", size_small, tmp_len); size_t out_len = size_big; if( dr->uncompress(out, &out_len, tmp, tmp_len, NULL) != RAL_OK ){ fprintf(stderr, "uncompress failed\n"); return -1; } if( out_len != size_small || memcmp(in, out, size_small) != 0 ) { fprintf(stderr, "Input and output differs\n"); return -1; } else { printf("Decompression ok\n"); } return 0; } 

I will listen and take note of any comments any comments and suggestions, especially interested in opinions about the file IO API.
Page: github.com/artemg/ral
GIt: git: //github.com/artemg/ral.git
targz: github.com/downloads/artemg/ral/libral-0.1.tar.gz

')

Source: https://habr.com/ru/post/123080/


All Articles