-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 879469d
Showing
12 changed files
with
715 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
root = true | ||
|
||
[*.cr] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 2 | ||
trim_trailing_whitespace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/docs/ | ||
/lib/ | ||
/bin/ | ||
/.shards/ | ||
*.dwarf | ||
|
||
# Libraries don't need dependency lock | ||
# Dependencies will be locked in applications that use them | ||
/shard.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
language: crystal | ||
|
||
# Uncomment the following if you'd like Travis to run specs and check code formatting | ||
# script: | ||
# - crystal spec | ||
# - crystal tool format --check |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2019 Ali Naqvi | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Crystal Brotli | ||
|
||
Crystal bindings to the [Brotli](https://github.com/google/brotli) compression library. | ||
|
||
## Installation | ||
|
||
1. Add the dependency to your `shard.yml`: | ||
|
||
```yaml | ||
dependencies: | ||
brotli: | ||
github: naqvis/brotli.cr | ||
``` | ||
2. Run `shards install` | ||
|
||
## Usage | ||
|
||
```crystal | ||
require "brotli" | ||
``` | ||
|
||
`brotli` shard provides both `Brotli::Reader` and `Brotli::Writer` , as well as `Brotli#decode` and `Brotli#encode` methods for quick usage. | ||
|
||
Refer to `specs` for sample usage. | ||
|
||
## Example: decompress an brotli file | ||
# | ||
```crystal | ||
require "brotli" | ||
string = File.open("file.br") do |file| | ||
Brotli::Reader.open(file) do |brotli| | ||
brotli.gets_to_end | ||
end | ||
end | ||
pp string | ||
``` | ||
|
||
## Example: compress to brotli compression format | ||
# | ||
```crystal | ||
require "brotli" | ||
File.write("file.txt", "abcd") | ||
File.open("./file.txt", "r") do |input_file| | ||
File.open("./file.br", "w") do |output_file| | ||
Brotli::Writer.open(output_file) do |brotli| | ||
IO.copy(input_file, brotli) | ||
end | ||
end | ||
end | ||
``` | ||
|
||
## Development | ||
|
||
To run all tests: | ||
|
||
``` | ||
crystal spec | ||
``` | ||
|
||
## Contributing | ||
|
||
1. Fork it (<https://github.com/naqvis/brotli.cr/fork>) | ||
2. Create your feature branch (`git checkout -b my-new-feature`) | ||
3. Commit your changes (`git commit -am 'Add some feature'`) | ||
4. Push to the branch (`git push origin my-new-feature`) | ||
5. Create a new Pull Request | ||
|
||
## Contributors | ||
|
||
- [Ali Naqvi](https://github.com/naqvis) - creator and maintainer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
name: brotli | ||
version: 0.1.0 | ||
|
||
authors: | ||
- Ali Naqvi <syed.alinaqvi@gmail.com> | ||
description: | | ||
Crystal bindings to the Brotli compression library. | ||
crystal: 0.31.1 | ||
|
||
license: MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
require "./spec_helper" | ||
|
||
describe Brotli do | ||
# TODO: Write tests | ||
|
||
it "Test Encode No Write" do | ||
buf = IO::Memory.new | ||
br = Brotli::Writer.new(buf) | ||
br.close | ||
|
||
# check write after close | ||
expect_raises(IO::Error) do | ||
br.write "hi".to_slice | ||
end | ||
end | ||
|
||
it "Test Encode Empty Write" do | ||
buf = IO::Memory.new | ||
Brotli::Writer.open(buf, options: Brotli::WriterOptions.new(quality: 5_u32)) do |br| | ||
br.write Bytes.empty | ||
end | ||
end | ||
|
||
it "Test Writer" do | ||
# Test basic encoder usage | ||
input = "<html><body><H1>Hello world</H1></body></html>" | ||
buf = IO::Memory.new | ||
inp = IO::Memory.new(input) | ||
enc = Brotli::Writer.new(buf, options: Brotli::WriterOptions.new(quality: 1_u32)) | ||
IO.copy inp, enc | ||
enc.close | ||
buf.rewind | ||
check_compressed_data buf.to_slice, input.to_slice | ||
inp.close | ||
buf.close | ||
end | ||
|
||
it "Test Encoder Stream" do | ||
# Test that output is streamed. | ||
# Adjust window size to ensure the encoder outputs at least enough bytes | ||
# to fill the window | ||
lgwin = 16 | ||
win_size = Math.pw2ceil(lgwin) | ||
input = Bytes.new(8 * win_size) | ||
Random.new.random_bytes(input) | ||
half_input = input[0, input.size//2] | ||
buf = IO::Memory.new | ||
Brotli::Writer.open(buf, options: Brotli::WriterOptions.new(lgwin: lgwin.to_u32)) do |br| | ||
br.write half_input | ||
end | ||
# We've fed more data than the sliding window size. Check that some | ||
# compressed data has been output | ||
fail "Output length is 0 after #{half_input.size} bytes written" if buf.size == 0 | ||
|
||
check_compressed_data(buf.to_slice, half_input) | ||
end | ||
|
||
it "Test Encoder Large Input" do | ||
input = Bytes.new(1000000) | ||
Random.new.random_bytes(input) | ||
buf = IO::Memory.new | ||
Brotli::Writer.open(buf, options: Brotli::WriterOptions.new(quality: 5_u32)) do |br| | ||
br.write input | ||
end | ||
buf.rewind | ||
check_compressed_data(buf.to_slice, input) | ||
end | ||
|
||
it "Test Encoder Flush" do | ||
input = Bytes.new(1000) | ||
Random.new.random_bytes(input) | ||
buf = IO::Memory.new | ||
Brotli::Writer.open(buf, options: Brotli::WriterOptions.new(quality: 5_u32)) do |br| | ||
br.write input | ||
br.flush | ||
fail "0 bytes written after flush" if buf.size == 0 | ||
end | ||
buf.rewind | ||
check_compressed_data(buf.to_slice, input) | ||
end | ||
|
||
it "Test Reader" do | ||
data = "hello crystal!" * 10000 | ||
compressed = Brotli.encode(data, Brotli::WriterOptions.new(quality: 5_u32)) | ||
uncompressed = Brotli.decode(compressed) | ||
|
||
data.to_slice.should eq(uncompressed) | ||
end | ||
|
||
it "Test Decode Trailing Data" do | ||
data = "hello crystal!" * 10000 | ||
compressed = Brotli.encode(data, Brotli::WriterOptions.new(quality: 5_u32)) | ||
corrupt = Bytes.new(compressed.size + 1) | ||
corrupt.copy_from(compressed.to_unsafe, compressed.size) | ||
expect_raises(Brotli::BrotliError, "excessive input") do | ||
Brotli.decode(corrupt) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require "spec" | ||
require "../src/brotli" | ||
|
||
def check_compressed_data(compressed_data : Slice, want : Slice) | ||
uncompressed = Brotli.decode(compressed_data) | ||
if uncompressed != want | ||
fail "Data doesn't uncompress to the original value \n" + | ||
"Length of original: #{want.size}\n" + | ||
"Length of uncompressed: #{uncompressed.size}" | ||
end | ||
|
||
uncompressed.should eq(want) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# `Brotli` Crystal Wrapper | ||
module Brotli | ||
VERSION = "0.1.0" | ||
|
||
class BrotliError < Exception | ||
end | ||
|
||
def self.decode(compressed : Slice) | ||
buf = IO::Memory.new(compressed) | ||
uncompressed = Reader.open(buf) do |br| | ||
br.gets_to_end | ||
end | ||
uncompressed.to_slice | ||
end | ||
|
||
def self.encode(content : String, options : WriterOptions = WriterOptions.default) | ||
encode(content.to_slice, options) | ||
end | ||
|
||
def self.encode(content : Slice, options : WriterOptions = WriterOptions.default) | ||
buf = IO::Memory.new | ||
Brotli::Writer.open(buf) do |br| | ||
br.write content | ||
end | ||
buf.rewind | ||
buf.to_slice | ||
end | ||
|
||
def self.decoder_version_string | ||
version_string LibBrotli.decoder_version | ||
end | ||
|
||
def self.encoder_version_string | ||
version_string LibBrotli.encoder_version | ||
end | ||
|
||
private def self.version_string(v) | ||
sprintf "%d.%d.%d", [v >> 24, (v >> 12) & 0xFFF, v & 0xFFF] | ||
end | ||
end | ||
|
||
require "./brotli/*" |
Oops, something went wrong.