Giter VIP home page Giter VIP logo

lz4-ruby's Introduction

lz4-ruby

Ruby bindings for LZ4.

Installation

gem install lz4-ruby

Usage

require 'rubygems'
require 'lz4-ruby'

# compress (fast)
compressed = LZ4::compress("hello, world")

# compress (high compression)
compressed = LZ4::compressHC("hello, world")

# uncompress
uncompressed = LZ4::uncompress(compressed)

Benchmark

Tested on VirtualBox VM : 2-core / 4GB RAM (Host : Core i5-2520M / 8GB RAM).

method  ratio(bpc)      comp.time(ms)   uncomp.time(ms)
-------------------------------------------------------
lz4     4.559           519.25          182.67
snappy  4.668           1050.73         257.94
lzo     4.279           1000.13         574.77

TODO

  • Support streaming methods.

  • Support compression level (LZ4HC)

  • Write API documents.

Copyright © 2012 KOMIYA Atsushi. See LICENSE.txt for further details.

Contributors

About LZ4

This Rubygem includes LZ4 codes written by Yann Collet.

LZ4 - Fast LZ compression algorithm
Copyright (C) 2011-2012, Yann Collet.
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

You can contact the author at :
- LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html
- LZ4 source repository : http://code.google.com/p/lz4/

lz4-ruby's People

Contributors

dearblue avatar headius avatar komiya-atsushi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

lz4-ruby's Issues

Security contact needed

Hello,

The GitHub Security Lab team has found a potential vulnerability in your project. Please create a Security Advisory and invite me in to further disclose and discuss the vulnerability details and potential fix. Alternatively, please add a Security Policy containing a security email address to send the details to.

If you prefer to contact us by email, please reach out to [email protected] with reference to GHSL-2022-027.

Thank you,
Agustin Gianni
GitHub Security Lab

Compressing Images uploaded using CarrierWave

Compressing strings works perfectly with lz4. But I'm having trouble compressing images.

I've uploaded the image using CarrierWave gem. Here's how I display the image:

<%= image_tag profile.avatar_url %>

But when I try to compress using: <% comp = LZ4::compress(profile.avatar) %>,
it throws this error, " undefined method `bytesize' for #AvatarUploader:0x007fa1aaec49f8 ".

I've posted this on StackOverflow but no one has gotten back to me in the past week. I'd be happy if you could help me out on this! :)

About "Support streaming methods"

https://github.com/komiya-atsushi/lz4-ruby#todo の『Support streaming methods.』についてですが、こちらでは生の LZ4 データを扱えるものが動作するものとして出来ており、手元には lz4cli で扱っている lz4 ストリームデータの入出力にも対応したものも出来ています (legacy stream は入力のみです)。

しかしながら実装されている API には差異が大きく、どうしたものかと悩んだまま pullreq を躊躇してしまっています 6(^ ^;

komiya-atsushi さんは raw stream API に対してどのように考えておられるのか、ご意見を頂きたいと思います。

dearblue による raw stream を実装したコードは https://github.com/dearblue/lz4-ruby/blob/support-rawstream/ext/lz4ruby/lz4ruby.c#LC238 以降にあります (2月中にコミットして、そのまま停滞しているものです)。

現段階における API は次のようになっています (いろいろと端折っていたりクラス・メソッド名がアレな感じだったりしますが):

  • LZ4.raw_stream_encode : LZ4Internal::RawStreamEncoder.new の呼び出しで、圧縮器の生成
  • LZ4.raw_stream_decode : LZ4Internal::RawStreamDecoder.new の呼び出しで、伸張器の生成
  • LZ4Internal::RawStreamEncoder#update / LZ4Internal::RawStreamDecoder#update : LZ4.raw_compress / LZ4.raw_uncompress のストリーム圧縮・伸張処理版で、LZ4.raw_compress / LZ4.raw_uncompress とメソッド引数と戻り値が同じ
  • LZ4Internal::RawStreamEncoder#reset / LZ4Internal::RawStreamDecoder#reset : 圧縮器・伸張器を使いまわせるように再初期化

生のストリーム処理の流れとしては:

  1. LZ4.raw_stream_encode または LZ4.raw_stream_decode で生のストリーム圧縮器・伸張器を生成、取得する
  2. 得た圧縮器・伸張器の #update を必要なだけ繰り返して呼び出す
  3. 圧縮器・伸張器は使い終わったら使いっぱなしで、GC によって回収される (圧縮器の作業バッファは、ブロックサイズの分だけ確保する必要があります。これを即時に開放する機能があるのが望ましいと考えますが、実装には反映されていません)

となっています。

簡素な利用例については https://github.com/dearblue/lz4-ruby/blob/support-rawstream/lib/lz4-ruby.rb#LC54 のコメント中にあります。

あるいは独自リリースしてしまってもよろしいでしょうか。

よろしくお願いします。

Non-standard header?

I tried using lz4-ruby to implement compression in my Cassandra driver, cql-rb. Cassandra supports LZ4 compression of requests and responses, but lz4-ruby can't decompress the messages coming from Cassandra because it expects the message to have a header.

Is this header a standard LZ4 thing, or is it something you've added just to be able to use the faster decompression methods?

Compression/decompression of multibyte characters fails

Using lz4-ruby 0.3.1:

input = 'Ķ' * 100
output = LZ4.uncompress(LZ4.compress(input))
output.force_encoding(Encoding::UTF_8)
input.should == output # false

The exact characters don't matter, it just seems to matter that they are multibyte. Is there a String#size call somewhere that should have been a String#bytesize?

Support of jsonlz4 files

Hello, I haven't been able to decompress a .jsonlz4 file with this gem as it says my file is corrupt. I'm able to use a Python script on it successfully though.

require 'lz4-ruby'

filepath = "recovery.jsonlz4"
File.exists?(filepath) # true

file = File.read(filepath)
LZ4::uncompress(file)
# .rvm/gems/ruby-3.0.2/gems/lz4-ruby-0.3.3/lib/lz4-ruby.rb:28:in `uncompress': Compressed data is maybe corrupted. (LZ4Internal::Error)

# Python script removes the first 8 characters, so I also tried that
LZ4::uncompress(file[8..])
# .rvm/gems/ruby-3.0.2/gems/lz4-ruby-0.3.3/lib/lz4-ruby.rb:28:in `uncompress': Compressed data is maybe corrupted. (LZ4Internal::Error)

If this is not supported, that's fine. Wanted to make sure I'm not doing something wrong. I noticed streaming methods is mentioned as a to-do on the README which might be related.

Note: This filetype is used by Firefox for profile files.

LZ4 compression changes string encoding

Here is the following commands in irb that reproduce this issue. Why does my UTF8 encoded string turn into an ASCII string? When I uncompress it, it will remain ASCII. But, that is not my original string. Because of this conversion that takes place I actually lose some of my UTF8 data.

1.9.3-p484 :003 > t = "teststring"
=> "teststring"
1.9.3-p484 :004 > t.encoding
=> #Encoding:UTF-8
1.9.3-p484 :005 > require 'lz4-ruby'
=> true
1.9.3-p484 :006 > result = LZ4::compress t
=> "\n\xA0teststring"
1.9.3-p484 :007 > result.encoding
=> #Encoding:ASCII-8BIT

Frame format?

I'm having trouble decompressing (compressed with lz4-ruby) in the browser using lz4js. It seems lz4-ruby does not frame the resulting compressed bytes into frames.

lz4-ruby:

$ ruby -rlz4-ruby -e 'print LZ4.compress("hello, world")' | hexdump -C
00000000  0c c0 68 65 6c 6c 6f 2c  20 77 6f 72 6c 64        |..hello, world|
0000000e

Using the command lz4c:

$ echo -n 'hello, world' | lz4c  | hexdump -C
00000000  04 22 4d 18 64 40 a7 0c  00 00 80 68 65 6c 6c 6f  |."[email protected]|
00000010  2c 20 77 6f 72 6c 64 00  00 00 00 d7 ff a5 4f     |, world.......O|
0000001f

Am I doing something wrong?

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.