diff options
| author | Pixel <pixel@nobis-crew.org> | 2009-04-30 13:47:27 -0700 | 
|---|---|---|
| committer | Pixel <pixel@nobis-crew.org> | 2009-04-30 13:47:27 -0700 | 
| commit | 1bd9d06b327531887f663d9c0222be6091ad65a9 (patch) | |
| tree | ef60c568bd99a611622f571c61cbdd40f4f5cdc3 | |
| parent | 7adc856d72fbe1fecc399cb462e108e8cc1d101a (diff) | |
Adding UCL n2e compression support.
| -rw-r--r-- | include/Handle.h | 2 | ||||
| -rw-r--r-- | lib/Handle.cc | 53 | ||||
| -rw-r--r-- | lib/LuaHandle.cc | 44 | 
3 files changed, 99 insertions, 0 deletions
| diff --git a/include/Handle.h b/include/Handle.h index 6697fdf..64b4243 100644 --- a/include/Handle.h +++ b/include/Handle.h @@ -71,6 +71,8 @@ class Handle : public Base {      static int GetNbHandles();      static int zlib_inflate(Handle * in, Handle * out) throw (GeneralException);      static int zlib_deflate(Handle * in, Handle * out) throw (GeneralException); +    static int ucl_compress(Handle * in, Handle * out) throw (GeneralException); +    static int ucl_decompress(Handle * in, Handle * out) throw (GeneralException);    protected:        Handle(int h);      int GetHandle() const; diff --git a/lib/Handle.cc b/lib/Handle.cc index 56e6c62..3e437a3 100644 --- a/lib/Handle.cc +++ b/lib/Handle.cc @@ -21,6 +21,7 @@  #include <string.h>  #include <errno.h>  #include <fcntl.h> +#include <ucl/ucl.h>  #ifdef HAVE_CONFIG_H  #include "config.h" @@ -870,3 +871,55 @@ int Handle::zlib_deflate(Handle * in, Handle * out) throw (GeneralException) {      return total_out;  } + +int Handle::ucl_compress(Handle * in, Handle * out) throw (GeneralException) { +    if (ucl_init() != UCL_E_OK) +        throw GeneralException("ucl_init failed"); +     +    unsigned char * b_in, * b_out; +    unsigned int len_in, len_out; +     +    len_in = in->GetSize(); +    len_out = len_in + len_in / 8 + 256; +     +    b_in = (unsigned char *) malloc(len_in); +    b_out = (unsigned char *) malloc(len_out); +    in->read(b_in, len_in); +     +    int r = ucl_nrv2e_99_compress(b_in, len_in, b_out, &len_out, NULL, 10, NULL, NULL); +    if (r != UCL_E_OK) +        throw GeneralException("Error happened during ucl_nrv2b_99_compress"); +     +    out->write(b_out, len_out); +     +    free(b_out); +    free(b_in); +     +    return len_out; +} + +int Handle::ucl_decompress(Handle * in, Handle * out) throw (GeneralException) { +    if (ucl_init() != UCL_E_OK) +        throw GeneralException("ucl_init failed"); +     +    unsigned char * b_in, * b_out; +    unsigned int len_in, len_out; +     +    len_in = in->GetSize(); +    len_out = len_in + len_in / 8 + 256; +     +    b_in = (unsigned char *) malloc(len_in); +    b_out = (unsigned char *) malloc(len_out); +    in->read(b_in, len_in); +     +    int r = ucl_nrv2e_decompress_8(b_in, len_in, b_out, &len_out, NULL); +    if (r != UCL_E_OK) +        throw GeneralException("Error happened during ucl_nrv2b_decompress_32"); +     +    out->write(b_out, len_out); +     +    free(b_out); +    free(b_in); +     +    return len_out; +} diff --git a/lib/LuaHandle.cc b/lib/LuaHandle.cc index e336f54..e2b621e 100644 --- a/lib/LuaHandle.cc +++ b/lib/LuaHandle.cc @@ -72,6 +72,8 @@ class sLuaHandle : public Base {      static int get_nb_buffer(lua_State * L);      static int zlib_inflate(lua_State * L);      static int zlib_deflate(lua_State * L); +    static int ucl_compress(lua_State * L); +    static int ucl_decompress(lua_State * L);    private:      static int read(lua_State * L, int);      static int write(lua_State * L, int); @@ -581,6 +583,46 @@ int sLuaHandle::zlib_deflate(lua_State * __L) {      return 1;  } +int sLuaHandle::ucl_compress(lua_State * __L) { +    Lua * L = Lua::find(__L); +    int n = L->gettop(); +    Handle * s, * d; +    int r; + +    if (n != 2) { +        L->error("Incorrect arguments to method `Handle::ucl_compress'"); +    } +     +    s = (Handle *) LuaObject::getme(L, 1); +    d = (Handle *) LuaObject::getme(L, 2); + +    r = Handle::ucl_compress(s, d); + +    L->push((lua_Number) r); + +    return 1; +} + +int sLuaHandle::ucl_decompress(lua_State * __L) { +    Lua * L = Lua::find(__L); +    int n = L->gettop(); +    Handle * s, * d; +    int r; + +    if (n != 2) { +        L->error("Incorrect arguments to method `Handle::ucl_decompress'"); +    } + +    s = (Handle *) LuaObject::getme(L, 1); +    d = (Handle *) LuaObject::getme(L, 2); + +    r = Handle::ucl_decompress(s, d); + +    L->push((lua_Number) r); + +    return 1; +} +  int sLuaHandle::seek(lua_State * __L) {      Lua * L = Lua::find(__L);      int n = L->gettop(); @@ -819,4 +861,6 @@ void LuaHandle::pushconstruct(Lua * L) {      L->declarefunc("zlib_inflate", sLuaHandle::zlib_inflate);      L->declarefunc("zlib_deflate", sLuaHandle::zlib_deflate); +    L->declarefunc("ucl_compress", sLuaHandle::ucl_compress); +    L->declarefunc("ucl_decompress", sLuaHandle::ucl_decompress);  } | 
