GithubHelp home page GithubHelp logo

lua-ffi-zlib's Introduction

lua-ffi-zlib

A Lua module using LuaJIT's FFI feature to access zlib. Intended primarily for use within OpenResty to allow manipulation of gzip encoded HTTP responses.

Methods

Basic methods allowing for simple compression or decompression of gzip data

inflateGzip

Syntax: ok, err = inflateGzip(input, output, chunk?, windowBits?)

  • input should be a function that accepts a chunksize as its only argument and return that many bytes of the gzip stream
  • output will receive a string of decompressed data as its only argument, do with it as you will!
  • chunk is the size of the input and output buffers, optional and defaults to 16KB
  • windowBits is passed to inflateInit2(), should be left as default for most cases. See zlib manual for details

On error returns false and the error message, otherwise true and the last status message

deflateGzip

Syntax: ok, err = deflateGzip(input, output, chunk?, options?)

  • input should be a function that accepts a chunksize as its only argument and return that many bytes of uncompressed data.
  • output will receive a string of compressed data as its only argument, do with it as you will!
  • chunk is the size of the input and output buffers, optional and defaults to 16KB
  • options is a table of options to pass to deflateInit2() Valid options are level, memLevel, strategy and windowBits, see zlib manual for details

On error returns false and the error message, otherwise true and the last status message

Example

Reads a file and output the decompressed version.

Roughly equivalent to running gzip -dc file.gz > out_file | tee

local table_insert = table.insert
local table_concat = table.concat
local zlib = require('lib.ffi-zlib')

local f = io.open(arg[1], "rb")
local out_f = io.open(arg[2], "w")

local input = function(bufsize)
    -- Read the next chunk
    local d = f:read(bufsize)
    if d == nil then
        return nil
    end
    return d
end

local output_table = {}
local output = function(data)
    table_insert(output_table, data)
    local ok, err = out_f:write(data)
    if not ok then
        -- abort decompression when error occurs
        return nil, err
    end
end

-- Decompress the data
local ok, err = zlib.inflateGzip(input, output)
if not ok then
    print(err)
    return
end

local decompressed = table_concat(output_table,'')

print(decompressed)

Advanced Usage

Several other methods are available for advanced usage. Some of these map directly to functions in the zlib library itself, see the manual for full details. Others are lower level utility functions.

createStream

Synax: stream, inbuf, outbuf = createStream(bufsize)

Returns a z_stream struct, input buffer and output buffer of length bufsize

initInflate

Syntax: ok = initInflate(stream, windowBits?)

Calls zlib's inflateInit2 with given stream, defaults to automatic header detection.

initDeflate

Syntax: ok = initDeflate(stream, options?)

Calls zlib's deflateInit2 with the given stream. options is an optional table that can set level, memLevel, strategy and windowBits

deflate

Syntax: ok, err = deflate(input, output, bufsize, stream, inbuf, outbuf)

  • input is a function that takes a chunk size argument and returns at most that many input bytes
  • output is a function that takes a string argument of output data
  • bufsize is the length of the output buffer
  • inbuf cdata input buffer
  • outpuf ccdata output buffer

This function will loop until all input data is consumed (input returns nil) or an error occurs. It will then clean up the stream and return an error code

inflate

Syntax: ok, err = inflate(input, output, bufsize, stream, inbuf, outbuf)

  • input is a function that takes a chunk size argument and returns at most that many input bytes
  • output is a function that takes a string argument of output data
  • bufsize is the length of the output buffer
  • inbuf cdata input buffer
  • outpuf ccdata output buffer

This function will loop until all input data is consumed (input returns nil) or an error occurs. It will then clean up the stream and return an error code

adler

Syntax: chksum = adler(str, chksum?)

Computes an adler32 checksum for a string, updates an existing checksum if provided

crc

Syntax: chksum = crc(str, chksum?)

Computes an crc32 checksum for a string, updates an existing checksum if provided

zlib_err

Syntax: err = zlib_err(code)

Returns the string representation of a zlib error code

lua-ffi-zlib's People

Contributors

fffonion avatar hamishforbes avatar lalawue avatar pintsized avatar sjnam avatar spacewander avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.