Skip to content

Commit

Permalink
feat: Add gzstd anz gunzstd executables as examples
Browse files Browse the repository at this point in the history
  • Loading branch information
prantlf committed Oct 26, 2023
1 parent b34cd33 commit d4b7fb3
Show file tree
Hide file tree
Showing 27 changed files with 202 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.DS_Store
*_test
*.dSYM
/gzstd
/gunzstd
LICENSE.z
LICENSE.uz
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"**/.git": true,
"**/.DS_Store": true,
"**/*_test": true,
"**/*.dSYM": true
"**/*.dSYM": true,
"gzstd": true,
"gunzstd": true
},
"search.exclude": {
"**/node_modules": true
Expand Down
15 changes: 12 additions & 3 deletions Makefile
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
77 changes: 77 additions & 0 deletions cmd/gunzstd/gunzstd.v
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()}')
}
}
}
69 changes: 69 additions & 0 deletions cmd/gzstd/gzstd.v
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()}')
}
}
}
3 changes: 2 additions & 1 deletion make.bat
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
35 changes: 35 additions & 0 deletions test.sh
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.

0 comments on commit d4b7fb3

Please sign in to comment.