TDengine/cmake/in/avro.lang.c.src.codec.c.in
freemine 6267763e85
Freemine.cmake.3.0 (#30663)
* chore(ci): with cmake verbose, temporarily

* chore(cmake): prefer Threads::Threads

* fix(cmake): debug building gtest on ubuntu 20.04 for the moment

* fix(cmake): try adding Threads::Threads

* fix(cmake): debug building gtest on ubuntu 20.04 for the moment

* fix(cmake): revert taosd-ci-build.yml

* fix(cmake): debug building on ubuntu 20.04 for the moment

* fix(cmake): debug building on ubuntu 20.04 for the moment

* fix(cmake): debug building on ubuntu 20.04 for the moment, adding dl right after DEP_ext_libuv

* fix(cmake): debug building on ubuntu 20.04 for the moment, try full run

* fix(cmake): fix building on ubuntu 20.04

* fix(cmake): dl is only valid in non-windows platform

* fix(cmake): dl order

* refactor(cmake): adjust option/cache variables for external dependencies

* refactor(cmake): mv patch files from contrib/ to cmake/

* refactor(cmake): typo

* refactor(cmake): adding cache on ci

* refactor(cmake):
1. ext_lzma2 is only used by Linux
2. fix bug in taosd-ci-build.yml

* enh: adding functionalities of build.sh

* fix(ci): `cmake -B` shall maintain consistency

* refactor(cmake): LOCAL_REPO/LOCAL_URL as cache variable

* refactor(cmake): taosadapter to be built in main build directory rather than .externals

* refactor(cmake): adding rpath for taosadapter, linux now

* refactor(cmake): adding rpath for taosadapter, macos now

* chore(cmake): remove some comments

* debug(ci): bypass TD_EXTERNALS_USE_ONLY

* debug(ci): bypass .externals caching for the moment

* fix(cmake): temporarily use taosadapter/main branch because this branch(freemine.cmake.3.0) has not include taos_fetch_fields_e from upstream

* refactor(cmake): clean GIT_TAG/GIT_SHALLOW pairs

* enh(ci): use build.sh rather than hard-coded in taosd-ci-build.yml, linux for now

* debug(ci): undefined __isoc23_strtoull

* debug(ci): benchmarkTest

* debug(ci): what if use_only = off

* debug(ci): remove .externals to make a refresh build

* debug(ci): back to cache, and ls files of gtest

* debug(ci): remove find coz {} not escaped

* debug(ci): cache key seems missing "matrix.os"

* refactor(ci): fixed cache key and reopen macos/windows

* chore(ci): literally -j4

* chore(ci): Trigger GitHub Actions re-run

* chore(ci): Trigger GitHub Actions re-run

* fix(ci): typo

* chore(ci): JOBS calculated by build.sh itself

* refactor(ci): enable WEBSOCKET in build.sh by default

* refactor(ci): enable WEBSOCKET in build.sh by default, macOS

* chore(cmake): remove obsoletes

* refactor(ci): stop service before running taosd

* chore(ci): `./build.sh install` just install TDengine, you will have to start taosd by `./build.sh start`

* refactor(cmake): adding source code found on http://emfisis.physics.uiowa.edu/Software/C/libargp/

* refactor(cmake): moving jansson/snappy/avro to .externals, and porting BUILD_TEST/WEBSOCKET on Windows

* refactor(cmake): porting linux

* refactor(cmake): porting macos
1. check argp.h
2. re-enable building taosdump

* refactor(cmake): porting linux
1. adjust CMAKE_C/CXX_FLAGS

* refactor(cmake): porting windows, adjust CMAKE_C/CXX_FLAGS

* refactor(cmake): use cmake_parse_arguments in macro INIT_EXT

* refactor(cmake): format ci cache directives

* refactor(cmake): adding comments for ci externals-cache directives

* refactor(cmake): adding check_env.cmake to checking libraries installed on the system

* refactor(cmake): porting linux, make homebrew-installed-libs available

* refactor(cmake): remove BYPRODUCTS from INIT_EXT macro

* refactor(cmake): INC_DIR is oneValueArg

* refactor(cmake): taosdump depends on ext_xz(LZMA)

* refactor(cmake): add copy_pthreadVC3 target on windows

* refactor(cmake): mv taosd-ci-build.yml to tdengine-build.yml

* chore(ci): try to fix yml syntax error

* refactor(cmake): use brew --prefix to get HomeBrew's install path

* refactor(cmake): typo

* debug(ci): build and cache externals first

* debug(ci): adding -Wno-stringop-overread for the moment

* debug(ci): make Threads::Threads public from os target

* debug(ci): make os public

* debug(ci): re-enable start and test

* debug(ci): let it go even if taosBenchmark failed

* debug(ci): copy logic from TDengine/main

* debug(ci): try ./build.sh start/test

* debug(ci): revert to original simple test logic in ci, taosBenchark/taos

* chore(cmake): code-convention and ci-logic

* debug(ci): show versions

* debug(ci): forcefully use specific GIT_TAG by commit-id for taos-connector-rust.git and taosadapter.git

* refactor(cmake): remove empty lines

* debug(ci): typo

* debug(ci): set supportVnodes to 256 before starting taosd

* debug(ci): fix: sudo and pipe

* debug(ci): cat /etc/taos/taos.cfg to check if supportVnodes effective

* debug(ci): cat forceReadConfig to taos.cfg

* debug(ci): adding taos -s "show dnodes"

* debug(ci): print more info

* debug(ci):
1. git log .... in an independant step
2. sleep 3 deliberately

* build: release ver-3.3.6.0

* chore(ci): comment and remove unnecessary code

* refactor(ci): cleanup tools/CMakeLists.txt
1. remove unnecessary or uneffective codes
2. better approach to pass compile flags to go

* chore(cmake): remove unnecessary code

* merge(cmake): porting linux

* merge(cmake): porting macOS

* merge(cmake): porting windows

* merge(cmake): pitfall fix: use literal format string instead

* merge(cmake): ver-3.3.6.0 for both taosadatper and taosws

* merge(cmake): adjust ci for debug purposes

* merge(cmake): add dependenices to make sure native built

* merge(main): link os

* merge(main): revert to main

* chore(cmake): remove freemine'd comments

* chore(cmake): make github ci to run with empty change

* refactor(cmake): remove unnecessaries

* refactor(cmake): remove unnecessaries

* chore(cmake):
1. remove taosudf in build.sh::do_purge
2. packaging taosBenchmark

* chore(cmake):
1. typo corrections
2. add -DCMAKE_POLICY_VERSION_MINIMUM=3.5 to bypass compatibility check imposed by latest cmake since 4.0

* chore(cmake): show cmake version

* chore(cmake): add -DCMAKE_POLICY_VERSION_MINIMUM=3.5 to bypass compatibility check imposed by latest cmake since 4.0

* chore(cmake): typo

* chore(cmake): remove unnecessaries

* cmake: debug enterprise compilation

* refactor: preparatory work before merging the freemin PR

* refactor(cmake): revert to d91205594a

* refactor(cmake): optimize by adjusting cache key

* chore(cmake): make github ci to run with empty change

* refactor(cmake): move libs3 and azure to external.cmake

* refactor(cmake): porting macOS

* refactor(cmake): move cos to external.cmake

* refactor(cmake): bypass s3/azure/cos on windows for the moment

* refactor(cmake): try find_package(Threads)

* refactor(cmake): add dl/pthread in libs3

* refactor(cmake): enable s3/azure for the moment, hard-coded

* refactor(cmake): adjust BUILD_WITH_ANALYSIS option

* refactor(cmake):
1. enum AVROTYPE => enum enAVROTYPE
2. remove INSTALL(CODE ...) from tools/taos-tools/CMakeLists.txt

* refactor(cmake): portint macOS and typo

* refactor(cmake): ticking external cache key to force refresh external building

* refactor(cmake): remove obsoletes

* refactor(cmake): remove obsoletes

* refactor(cmake): remove obsoletes, contrib/deps-download related

* refactor(cmake): adding build.bat, and use build.bat in .github/workflows/tdengine-build.yml

* refactor(cmake):
1. call .\build.bat
2. remove POST_BUILD which is not valid in this signature of add_custom_command

* refactor(ci): use 'NMake Makefiles JOM'

* refactor(ci): typo

* refactor(ci): debug

* refactor(ci): debug

* refactor(ci): debug

* refactor(ci): use call on windows

* refactor(cmake): move utils/TSZ to contrib/TSZ

* chore(cmake): make github ci to run with empty change

* refactor(cmake): mv cmake.* -> *.cmake and related in CMakeLists.txt

* refactor(cmake): debugging integrating community

* refactor(cmake): debugging integrating community

* chore(cmake): make github ci to run with empty change

* refactor(cmake): revert geos.git back to 3.12.0

* refactor(cmake): revert doc/** to main branch

* refactor(cmake): revert tests/** to main branch, except tests/taosc_test/CMakeLists.txt

* refactor(cmake): revert test/cases/** to main branch

* refactor(cmake): revert test/docs/** to main branch

* refactor(cmake): revert tools/taos-tools/deps/{avro,jansson,libargp,snappy,zlib-1.2.13} to main branch, which need to be removed later

* refactor(ci): revert .github/workflows/{deploy-case-docs.yml,tdengine-doc-build.yml} to main branch

* refactor(cmake): revert tcommon.h and related to main branch

* refactor(cmake): revert test/* stuffs back to main branch

* refactor(cmake): revert libuv.git back to v1.49.2

* refactor(cmake): revert .h/.c and related to main branch

* refactor(cmake): typo format string

* refactor(cmake): typo correction, incorrect use of macro `assert`

* refactor(cmake): adding /wd4028 to bypass thttp.c under enterprise build for the moment

* refactor(cmake):
1. adding some rules to .gitignore for the last resort to save
2. adding source files manually to source/libs/parser/CMakeLists.txt to exclude potential sql.c generated by old build system

* refactor(cmake): add -Wno-unused-result for the moment

* refactor(cmake): adjust target dependencies

* refactor(cmake): tweaking building of mxml

* refactor(cmake): adjust externals CMAKE_ARGS to save compilation time

* refactor(cmake): temporarily use tomchon-patch-3

* refactor(cmake): temporarily adding dumpbin directives

* refactor(cmake): adding NODEFAULTLIB temporarily for debugging purpose

* refactor(cmake): adding NODEFAULTLIB:libcpmt temporarily for debugging purpose

* refactor(cmake): adding NODEFAULTLIB:cpmt temporarily for debugging purpose

* chore(cmake): make github ci to run with empty change

* chore(cmake): make github ci to run with empty change

* refactor(ci): use freemine.debug branch for the moment

* refactor(ci): revert back to use run-tests-on-windows.yml of tomchon-patch-3 branch

* chore(cmake): make github ci to run with empty change

* refactor(cmake): remove temporary debug directives

* refactor(cmake): move cmake_policy(..CMP0135..) to the root of this project

* refactor(cmake): Remove temporary debugging conditional and restore original logic

* chore(cmake): adding -B in army tests

* chore(cmake): make github ci to run with empty change

* chore(cmake): revert: adding -B in army tests

* refactor(cmake):
1. use ver-3.3.6.3 of taosadapter
2. pitfall fix: do not link with taosws for taos shell

* refactor(cmake): revert taosadapter to ver-3.3.6.0

* chore(cmake): adding logging message in more detail

* chore(cmake): adding logging message in more detail

* chore(ci): debugging by using ls -l

* chore(ci): typo

* chore(ci): typo

* refactor(cmake): revert to remove debug code, and taosadapter to ver-3.3.6.3

* refactor(cmake): remove DEP_ext_taosws

* chore(cmake): make github ci to run with empty change

* chore(cmake): make github ci to run with empty change

* chore(cmake): make github ci to run with empty change

* chore(cmake): make github ci to run with empty change

* chore(cmake): typo correction: utils/test/c/sml_test.c

* debug(cmake): test cases debug

* chore(cmake): make github ci to run with empty change

* chore(ci): using tomchon-patch-3 branch for run-tests-on-linux for the moment

* chore(cmake): make github ci to run with empty change

* debug(test): revert utils/test/c/sml_test.c back to tdengine/main branch

* debug(test): adding VERBOSE=1 and gcc --version

* debug(test): revert tests/parallel_test/cases.task back to tdengine/main branch

* debug: add log in run_case.sh

* fix: test log

* fix: docker run privileged

* tes: add debug test in  cases.task

* refactor(cmake): remove ref to \contrib\<dep-name> and ~/.cos-local-{1,2} from CMakeLists.txt(s)

* refactor(cmake): make taosudf link with libtaos.so

* chore(cmake): fix compiler warning as error problem

* refactor(cmake): revert 7d7f03832d

* chore(cmake): only udf.sim and udfpy.sim for the moment

* debug(ci): debug udf.sim for the moment

* debug(ci): debug udf.sim for the moment

* debug(ci): debug udf.sim for the moment

* debug(ci): debug only udf.sim for the moment

* debug(ci): debug only udf.sim for the moment, using tee

* debug(ci): debug only udf.sim for the moment, revert using tee

* debug(ci): debug only udf.sim for the moment, adding TAOS_LIB again

* debug(cmake): adding -rdynamic for the moment

* debug(cmake): re-adding udfpy.sim

* debug(cmake): do NOT run with Sanitizer for udfpy.sim for the moment

* chore(cmake): revert cases.task, and remove ubuntu-20.04 from tdengine-build.yml

* refactor(cmake): using ENABLE_EXPORTS to export symbols in taosudf

* chore(cmake): make github ci to run with empty change

* chore(cmake): remove stuffs for debug purpose and revert

---------

Co-authored-by: support-platform <it@taosdata.com>
Co-authored-by: Simon Guan <slguan@taosdata.com>
Co-authored-by: Simon Guan <guanshengliang@qq.com>
Co-authored-by: haoranchen <haoran920c@163.com>
2025-04-19 14:03:29 +08:00

620 lines
13 KiB
C

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include <string.h>
#ifdef SNAPPY_CODEC
#include <snappy-c.h>
# if defined(__APPLE__)
# include <libkern/OSByteOrder.h>
# define __bswap_32 OSSwapInt32
# elif defined(__FreeBSD__)
# include <sys/endian.h>
# define __bswap_32 bswap32
# elif defined(_WIN32)
# include <stdlib.h>
# define __bswap_32 _byteswap_ulong
# else
# include <byteswap.h>
# endif
#endif
#ifdef DEFLATE_CODEC
#include <zlib.h>
#endif
#ifdef LZMA_CODEC
#include <lzma.h>
#endif
#include "avro/errors.h"
#include "avro/allocation.h"
#include "codec.h"
#define DEFAULT_BLOCK_SIZE (16 * 1024)
/* NULL codec */
static int
codec_null(avro_codec_t codec)
{
codec->name = "null";
codec->type = AVRO_CODEC_NULL;
codec->block_size = 0;
codec->used_size = 0;
codec->block_data = NULL;
codec->codec_data = NULL;
return 0;
}
static int encode_null(avro_codec_t c, void * data, int64_t len)
{
c->block_data = data;
c->block_size = len;
c->used_size = len;
return 0;
}
static int decode_null(avro_codec_t c, void * data, int64_t len)
{
c->block_data = data;
c->block_size = len;
c->used_size = len;
return 0;
}
static int reset_null(avro_codec_t c)
{
c->block_data = NULL;
c->block_size = 0;
c->used_size = 0;
c->codec_data = NULL;
return 0;
}
/* Snappy codec */
#ifdef SNAPPY_CODEC
static int
codec_snappy(avro_codec_t codec)
{
codec->name = "snappy";
codec->type = AVRO_CODEC_SNAPPY;
codec->block_size = 0;
codec->used_size = 0;
codec->block_data = NULL;
codec->codec_data = NULL;
return 0;
}
static int encode_snappy(avro_codec_t c, void * data, int64_t len)
{
uint32_t crc;
size_t outlen = snappy_max_compressed_length(len);
if (!c->block_data) {
c->block_data = avro_malloc(outlen+4);
c->block_size = outlen+4;
} else if (c->block_size < (int64_t) (outlen+4)) {
c->block_data = avro_realloc(c->block_data, c->block_size, (outlen+4));
c->block_size = outlen+4;
}
if (!c->block_data) {
avro_set_error("Cannot allocate memory for snappy");
return 1;
}
if (snappy_compress((const char *)data, len, (char*)c->block_data, &outlen) != SNAPPY_OK)
{
avro_set_error("Error compressing block with Snappy");
return 1;
}
crc = __bswap_32(crc32(0, (const Bytef *)data, len));
memcpy((char*)c->block_data+outlen, &crc, 4);
c->used_size = outlen+4;
return 0;
}
static int decode_snappy(avro_codec_t c, void * data, int64_t len)
{
uint32_t crc;
size_t outlen;
if (snappy_uncompressed_length((const char*)data, len-4, &outlen) != SNAPPY_OK) {
avro_set_error("Uncompressed length error in snappy");
return 1;
}
if (!c->block_data) {
c->block_data = avro_malloc(outlen);
c->block_size = outlen;
} else if ( (size_t)c->block_size < outlen) {
c->block_data = avro_realloc(c->block_data, c->block_size, outlen);
c->block_size = outlen;
}
if (!c->block_data)
{
avro_set_error("Cannot allocate memory for snappy");
return 1;
}
if (snappy_uncompress((const char*)data, len-4, (char*)c->block_data, &outlen) != SNAPPY_OK)
{
avro_set_error("Error uncompressing block with Snappy");
return 1;
}
crc = __bswap_32(crc32(0, (const Bytef *)c->block_data, outlen));
if (memcmp(&crc, (char*)data+len-4, 4))
{
avro_set_error("CRC32 check failure uncompressing block with Snappy");
return 1;
}
c->used_size = outlen;
return 0;
}
static int reset_snappy(avro_codec_t c)
{
if (c->block_data) {
avro_free(c->block_data, c->block_size);
}
c->block_data = NULL;
c->block_size = 0;
c->used_size = 0;
c->codec_data = NULL;
return 0;
}
#endif // SNAPPY_CODEC
/* Deflate codec */
#ifdef DEFLATE_CODEC
struct codec_data_deflate {
z_stream deflate;
z_stream inflate;
};
#define codec_data_deflate_stream(cd) &((struct codec_data_deflate *)cd)->deflate
#define codec_data_inflate_stream(cd) &((struct codec_data_deflate *)cd)->inflate
static int
codec_deflate(avro_codec_t codec)
{
codec->name = "deflate";
codec->type = AVRO_CODEC_DEFLATE;
codec->block_size = 0;
codec->used_size = 0;
codec->block_data = NULL;
codec->codec_data = avro_new(struct codec_data_deflate);
if (!codec->codec_data) {
avro_set_error("Cannot allocate memory for zlib");
return 1;
}
z_stream *ds = codec_data_deflate_stream(codec->codec_data);
z_stream *is = codec_data_inflate_stream(codec->codec_data);
memset(ds, 0, sizeof(z_stream));
memset(is, 0, sizeof(z_stream));
ds->zalloc = is->zalloc = Z_NULL;
ds->zfree = is->zfree = Z_NULL;
ds->opaque = is->opaque = Z_NULL;
if (deflateInit2(ds, Z_BEST_COMPRESSION, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
avro_freet(struct codec_data_deflate, codec->codec_data);
codec->codec_data = NULL;
avro_set_error("Cannot initialize zlib deflate");
return 1;
}
if (inflateInit2(is, -15) != Z_OK) {
avro_freet(struct codec_data_deflate, codec->codec_data);
codec->codec_data = NULL;
avro_set_error("Cannot initialize zlib inflate");
return 1;
}
return 0;
}
static int encode_deflate(avro_codec_t c, void * data, int64_t len)
{
int err;
int64_t defl_len = compressBound((uLong)len * 1.2);
if (!c->block_data) {
c->block_data = avro_malloc(defl_len);
c->block_size = defl_len;
} else if ( c->block_size < defl_len) {
c->block_data = avro_realloc(c->block_data, c->block_size, defl_len);
c->block_size = defl_len;
}
if (!c->block_data)
{
avro_set_error("Cannot allocate memory for deflate");
return 1;
}
c->used_size = 0;
z_stream *s = codec_data_deflate_stream(c->codec_data);
s->next_in = (Bytef*)data;
s->avail_in = (uInt)len;
s->next_out = (Bytef*)c->block_data;
s->avail_out = (uInt)c->block_size;
s->total_out = 0;
err = deflate(s, Z_FINISH);
if (err != Z_STREAM_END) {
deflateEnd(s);
if (err != Z_OK) {
avro_set_error("Error compressing block with deflate (%i)", err);
return 1;
}
return 0;
}
// zlib resizes the buffer?
c->block_size = s->total_out;
c->used_size = s->total_out;
if (deflateReset(s) != Z_OK) {
return 1;
}
return 0;
}
static int decode_deflate(avro_codec_t c, void * data, int64_t len)
{
int err;
z_stream *s = codec_data_inflate_stream(c->codec_data);
if (!c->block_data) {
c->block_data = avro_malloc(DEFAULT_BLOCK_SIZE);
c->block_size = DEFAULT_BLOCK_SIZE;
}
if (!c->block_data)
{
avro_set_error("Cannot allocate memory for deflate");
return 1;
}
c->used_size = 0;
s->next_in = (Bytef*)data;
s->avail_in = len;
s->next_out = (Bytef*)c->block_data;
s->avail_out = c->block_size;
s->total_out = 0;
do
{
err = inflate(s, Z_FINISH);
// Apparently if there is yet available space in the output then something
// has gone wrong in decompressing the data (according to cpython zlibmodule.c)
if (err == Z_BUF_ERROR && s->avail_out > 0) {
inflateEnd(s);
avro_set_error("Error decompressing block with deflate, possible data error");
return 1;
}
// The buffer was not big enough. resize it.
if (err == Z_BUF_ERROR)
{
c->block_data = avro_realloc(c->block_data, c->block_size, c->block_size * 2);
s->next_out = (Bytef*)c->block_data + s->total_out;
s->avail_out += c->block_size;
c->block_size = c->block_size * 2;
}
} while (err == Z_BUF_ERROR);
if (err != Z_STREAM_END) {
inflateEnd(s);
if (err != Z_OK) {
avro_set_error("Error decompressing block with deflate (%i)", err);
return 1;
}
return 0;
}
c->used_size = s->total_out;
if (inflateReset(s) != Z_OK) {
avro_set_error("Error resetting deflate decompression");
return 1;
}
return 0;
}
static int reset_deflate(avro_codec_t c)
{
if (c->block_data) {
avro_free(c->block_data, c->block_size);
}
if (c->codec_data) {
deflateEnd(codec_data_deflate_stream(c->codec_data));
inflateEnd(codec_data_inflate_stream(c->codec_data));
avro_freet(struct codec_data_deflate, c->codec_data);
}
c->block_data = NULL;
c->block_size = 0;
c->used_size = 0;
c->codec_data = NULL;
return 0;
}
#endif // DEFLATE_CODEC
/* LZMA codec */
#ifdef LZMA_CODEC
struct codec_data_lzma {
lzma_filter filters[2];
lzma_options_lzma options;
};
#define codec_data_lzma_filters(cd) ((struct codec_data_lzma *)cd)->filters
#define codec_data_lzma_options(cd) &((struct codec_data_lzma *)cd)->options
static int
codec_lzma(avro_codec_t codec)
{
codec->name = "lzma";
codec->type = AVRO_CODEC_LZMA;
codec->block_size = 0;
codec->used_size = 0;
codec->block_data = NULL;
codec->codec_data = avro_new(struct codec_data_lzma);
if (!codec->codec_data) {
avro_set_error("Cannot allocate memory for lzma");
return 1;
}
lzma_options_lzma* opt = codec_data_lzma_options(codec->codec_data);
lzma_lzma_preset(opt, LZMA_PRESET_DEFAULT);
lzma_filter* filters = codec_data_lzma_filters(codec->codec_data);
filters[0].id = LZMA_FILTER_LZMA2;
filters[0].options = opt;
filters[1].id = LZMA_VLI_UNKNOWN;
filters[1].options = NULL;
return 0;
}
static int encode_lzma(avro_codec_t codec, void * data, int64_t len)
{
lzma_ret ret;
size_t written = 0;
lzma_filter* filters = codec_data_lzma_filters(codec->codec_data);
int64_t buff_len = len + lzma_raw_encoder_memusage(filters);
if (!codec->block_data) {
codec->block_data = avro_malloc(buff_len);
codec->block_size = buff_len;
}
if (!codec->block_data)
{
avro_set_error("Cannot allocate memory for lzma encoder");
return 1;
}
ret = lzma_raw_buffer_encode(filters, NULL, data, len, codec->block_data, &written, codec->block_size);
codec->used_size = written;
if (ret != LZMA_OK) {
avro_set_error("Error in lzma encoder");
return 1;
}
return 0;
}
static int decode_lzma(avro_codec_t codec, void * data, int64_t len)
{
size_t read_pos = 0;
size_t write_pos = 0;
lzma_ret ret;
lzma_filter* filters = codec_data_lzma_filters(codec->codec_data);
if (!codec->block_data) {
codec->block_data = avro_malloc(DEFAULT_BLOCK_SIZE);
codec->block_size = DEFAULT_BLOCK_SIZE;
}
if (!codec->block_data) {
avro_set_error("Cannot allocate memory for lzma decoder");
return 1;
}
do
{
ret = lzma_raw_buffer_decode(filters, NULL, data,
&read_pos, len, codec->block_data, &write_pos,
codec->block_size);
codec->used_size = write_pos;
// If it ran out of space to decode, give it more!!
// It will continue where it left off because of read_pos and write_pos.
if (ret == LZMA_BUF_ERROR) {
codec->block_data = avro_realloc(codec->block_data, codec->block_size, codec->block_size * 2);
codec->block_size = codec->block_size * 2;
}
} while (ret == LZMA_BUF_ERROR);
if (ret != LZMA_OK) {
avro_set_error("Error in lzma decoder");
return 1;
}
return 0;
}
static int reset_lzma(avro_codec_t c)
{
if (c->block_data) {
avro_free(c->block_data, c->block_size);
}
if (c->codec_data) {
avro_freet(struct codec_data_lzma, c->codec_data);
}
c->block_data = NULL;
c->block_size = 0;
c->used_size = 0;
c->codec_data = NULL;
return 0;
}
#endif // LZMA_CODEC
/* Common interface */
int avro_codec(avro_codec_t codec, const char *type)
{
if (type == NULL) {
return codec_null(codec);
}
#ifdef SNAPPY_CODEC
if (strcmp("snappy", type) == 0) {
return codec_snappy(codec);
}
#endif
#ifdef DEFLATE_CODEC
if (strcmp("deflate", type) == 0) {
return codec_deflate(codec);
}
#endif
#ifdef LZMA_CODEC
if (strcmp("lzma", type) == 0) {
return codec_lzma(codec);
}
#endif
if (strcmp("null", type) == 0) {
return codec_null(codec);
}
avro_set_error("Unknown codec %s", type);
return 1;
}
int avro_codec_encode(avro_codec_t c, void * data, int64_t len)
{
switch(c->type)
{
case AVRO_CODEC_NULL:
return encode_null(c, data, len);
#ifdef SNAPPY_CODEC
case AVRO_CODEC_SNAPPY:
return encode_snappy(c, data, len);
#endif
#ifdef DEFLATE_CODEC
case AVRO_CODEC_DEFLATE:
return encode_deflate(c, data, len);
#endif
#ifdef LZMA_CODEC
case AVRO_CODEC_LZMA:
return encode_lzma(c, data, len);
#endif
default:
return 1;
}
}
int avro_codec_decode(avro_codec_t c, void * data, int64_t len)
{
switch(c->type)
{
case AVRO_CODEC_NULL:
return decode_null(c, data, len);
#ifdef SNAPPY_CODEC
case AVRO_CODEC_SNAPPY:
return decode_snappy(c, data, len);
#endif
#ifdef DEFLATE_CODEC
case AVRO_CODEC_DEFLATE:
return decode_deflate(c, data, len);
#endif
#ifdef LZMA_CODEC
case AVRO_CODEC_LZMA:
return decode_lzma(c, data, len);
#endif
default:
return 1;
}
}
int avro_codec_reset(avro_codec_t c)
{
switch(c->type)
{
case AVRO_CODEC_NULL:
return reset_null(c);
#ifdef SNAPPY_CODEC
case AVRO_CODEC_SNAPPY:
return reset_snappy(c);
#endif
#ifdef DEFLATE_CODEC
case AVRO_CODEC_DEFLATE:
return reset_deflate(c);
#endif
#ifdef LZMA_CODEC
case AVRO_CODEC_LZMA:
return reset_lzma(c);
#endif
default:
return 1;
}
}