-
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.
feat: Add gzstd anz gunzstd executables as examples
- Loading branch information
Showing
27 changed files
with
202 additions
and
5 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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
.DS_Store | ||
*_test | ||
*.dSYM | ||
/gzstd | ||
/gunzstd | ||
LICENSE.z | ||
LICENSE.uz |
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
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 |
---|---|---|
@@ -1,11 +1,20 @@ | ||
all: check test | ||
ifeq (1,${RELEASE}) | ||
VFLAGS=-prod | ||
endif | ||
|
||
all: check build test | ||
|
||
check: | ||
v fmt -w . | ||
v vet . | ||
|
||
build: | ||
v $(VFLAGS) -o gzstd cmd/gzstd/gzstd.v | ||
v $(VFLAGS) -o gunzstd cmd/gunzstd/gunzstd.v | ||
|
||
test: | ||
v test . | ||
v -use-os-system-to-run test . | ||
./test.sh | ||
|
||
clean: | ||
rm -rf src/*_test src/*.dSYM | ||
rm -rf src/*_test src/*.dSYM gzstd gunzstd |
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,77 @@ | ||
module main | ||
|
||
import os { create, exists, open, rm } | ||
import zstd { DecompressContext, StreamContext, new_decompress_context, new_decompress_stream_context } | ||
|
||
fn decompress(in_name string, dctx &DecompressContext, mut sctx StreamContext, mut buf []u8) ! { | ||
dctx.reset(zstd.ResetDir.session_only) | ||
|
||
mut in_file := open(in_name)! | ||
defer { | ||
in_file.close() | ||
} | ||
|
||
out_name := if in_name.ends_with('.z') { | ||
name := in_name[..in_name.len - 2] | ||
if exists(name) { | ||
'${name}.uz' | ||
} else { | ||
name | ||
} | ||
} else { | ||
'${in_name}.uz' | ||
} | ||
mut out_file := create(out_name)! | ||
defer { | ||
out_file.close() | ||
} | ||
mut out_file_ref := &out_file | ||
|
||
drain := fn [mut out_file_ref] (buf &u8, len int) ! { | ||
unsafe { out_file_ref.write_full_buffer(buf, usize(len))! } | ||
} | ||
abort := fn [out_name, mut out_file_ref] () { | ||
out_file_ref.close() | ||
rm(out_name) or { eprintln('cleanup failed: ${err.msg()}') } | ||
} | ||
|
||
for { | ||
len := in_file.read_into_ptr(buf.data, buf.len) or { | ||
abort() | ||
return err | ||
} | ||
last := len < buf.len | ||
unsafe { | ||
dctx.decompress_chunk_at(mut sctx, buf.data, len, last, drain) or { | ||
abort() | ||
return err | ||
} | ||
} | ||
if last { | ||
break | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
if os.args.len < 2 { | ||
eprintln('name of file to decompress missing') | ||
exit(1) | ||
} | ||
|
||
dctx := new_decompress_context()! | ||
defer { | ||
dctx.free() | ||
} | ||
mut sctx := new_decompress_stream_context() | ||
mut buf := []u8{len: zstd.decompress_stream_in_size} | ||
|
||
for i in 1 .. os.args.len { | ||
name := os.args[i] | ||
if _ := decompress(name, dctx, mut sctx, mut buf) { | ||
println('${name} decompressed') | ||
} else { | ||
println('${name} failed: ${err.msg()}') | ||
} | ||
} | ||
} |
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,69 @@ | ||
module main | ||
|
||
import os { create, open, rm } | ||
import zstd { CompressContext, CompressParam, StreamContext, new_compress_context, new_compress_stream_context } | ||
|
||
fn compress(in_name string, cctx &CompressContext, mut sctx StreamContext, mut buf []u8) ! { | ||
cctx.reset(zstd.ResetDir.session_only) | ||
|
||
mut in_file := open(in_name)! | ||
defer { | ||
in_file.close() | ||
} | ||
|
||
out_name := '${in_name}.z' | ||
mut out_file := create(out_name)! | ||
defer { | ||
out_file.close() | ||
} | ||
mut out_file_ref := &out_file | ||
|
||
drain := fn [mut out_file_ref] (buf &u8, len int) ! { | ||
unsafe { out_file_ref.write_full_buffer(buf, usize(len))! } | ||
} | ||
abort := fn [out_name, mut out_file_ref] () { | ||
out_file_ref.close() | ||
rm(out_name) or { eprintln('cleanup failed: ${err.msg()}') } | ||
} | ||
|
||
for { | ||
len := in_file.read_into_ptr(buf.data, buf.len) or { | ||
abort() | ||
return err | ||
} | ||
last := len < buf.len | ||
unsafe { | ||
cctx.compress_chunk_at(mut sctx, buf.data, len, last, drain) or { | ||
abort() | ||
return err | ||
} | ||
} | ||
if last { | ||
break | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
if os.args.len < 2 { | ||
eprintln('name of file to compress missing') | ||
exit(1) | ||
} | ||
|
||
cctx := new_compress_context()! | ||
defer { | ||
cctx.free() | ||
} | ||
cctx.set_param(CompressParam.checksum_flag, 1)! | ||
mut sctx := new_compress_stream_context() | ||
mut buf := []u8{len: zstd.compress_stream_in_size} | ||
|
||
for i in 1 .. os.args.len { | ||
name := os.args[i] | ||
if _ := compress(name, cctx, mut sctx, mut buf) { | ||
println('${name} compressed') | ||
} else { | ||
println('${name} failed: ${err.msg()}') | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
v test . | ||
v -prod -o gzstd cmd/gzstd/gzstd.v | ||
v -prod -o gunzstd cmd/gunzstd/gunzstd.v |
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,35 @@ | ||
#!/bin/sh | ||
|
||
set -e | ||
|
||
test() { | ||
if [ "$2" != "" ]; then | ||
echo "----------------------------------------" | ||
fi | ||
echo "$1" | ||
echo "----------------------------------------" | ||
} | ||
|
||
test "compress" | ||
./gzstd LICENSE | ||
if [ ! -f "LICENSE.z" ]; then | ||
echo "archive not compressed" | ||
exit 1 | ||
fi | ||
|
||
test "decompress" | ||
./gunzstd LICENSE.z | ||
if [ ! -f "LICENSE.uz" ]; then | ||
echo "archive not decompressed" | ||
exit 1 | ||
fi | ||
|
||
test "compare" | ||
diff LICENSE LICENSE.uz | ||
if [ $? -ne 0 ]; then | ||
exit 1 | ||
fi | ||
|
||
rm LICENSE.z LICENSE.uz | ||
|
||
echo "done" |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.