Initial project import
This commit is contained in:
commit
23f2d29e32
11957 files changed
+1754577
No files matched your search
@@ -0,0 +1,219 @@
|
||||
module main
|
||||
|
||||
import compress.deflate
|
||||
import os
|
||||
|
||||
fn choose_cc() string {
|
||||
for cc in ['cc', 'gcc', 'clang'] {
|
||||
if os.execute('${cc} --version').exit_code == 0 {
|
||||
return cc
|
||||
}
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
fn compile_c_ref(workdir string) !string {
|
||||
cc := choose_cc()
|
||||
if cc == '' {
|
||||
return error('no C compiler found')
|
||||
}
|
||||
src := os.join_path(@DIR, 'deflate_ref.c')
|
||||
bin := os.join_path(workdir, 'deflate_cross_validate')
|
||||
res := os.execute('${cc} -O2 ${os.quoted_path(src)} -lz -o ${os.quoted_path(bin)}')
|
||||
if res.exit_code != 0 {
|
||||
return error('C compile failed: ${res.output}')
|
||||
}
|
||||
return bin
|
||||
}
|
||||
|
||||
fn main() {
|
||||
base_tmp := os.join_path(os.temp_dir(), 'v_deflate_interop')
|
||||
os.mkdir_all(base_tmp) or {
|
||||
eprintln('could not create base temp dir: ${err.msg()}')
|
||||
exit(1)
|
||||
}
|
||||
workdir := os.join_path(base_tmp, 'run_${os.getpid()}')
|
||||
os.mkdir_all(workdir) or {
|
||||
eprintln('could not create work dir: ${err.msg()}')
|
||||
exit(1)
|
||||
}
|
||||
defer {
|
||||
os.rmdir_all(workdir) or {}
|
||||
}
|
||||
|
||||
bin := compile_c_ref(workdir) or {
|
||||
eprintln('Skipping C cross-validation: ${err.msg()}')
|
||||
return
|
||||
}
|
||||
|
||||
input := 'deflate C cross-validation payload'.repeat(50).bytes()
|
||||
ip := os.join_path(workdir, 'xval_in.bin')
|
||||
cp := os.join_path(workdir, 'xval_c_zlib.bin')
|
||||
gp := os.join_path(workdir, 'xval_c_gzip.bin')
|
||||
vp := os.join_path(workdir, 'xval_v_zlib.bin')
|
||||
vgp := os.join_path(workdir, 'xval_v_gzip.bin')
|
||||
dp := os.join_path(workdir, 'xval_dec.bin')
|
||||
|
||||
os.write_file_array(ip, input) or {
|
||||
eprintln('write input failed: ${err.msg()}')
|
||||
exit(1)
|
||||
}
|
||||
|
||||
res1 :=
|
||||
os.execute('${os.quoted_path(bin)} compress ${os.quoted_path(ip)} ${os.quoted_path(cp)}')
|
||||
if res1.exit_code != 0 {
|
||||
eprintln('C zlib compress failed: ${res1.output}')
|
||||
exit(1)
|
||||
}
|
||||
c_zlib := os.read_bytes(cp) or {
|
||||
eprintln('read C zlib stream failed: ${err.msg()}')
|
||||
exit(1)
|
||||
}
|
||||
v_decoded_zlib := deflate.decompress(c_zlib) or {
|
||||
eprintln('V decompress of C zlib failed: ${err.msg()}')
|
||||
exit(1)
|
||||
}
|
||||
if v_decoded_zlib != input {
|
||||
eprintln('C zlib -> V mismatch')
|
||||
exit(1)
|
||||
}
|
||||
println('OK: C zlib -> V decompress')
|
||||
|
||||
res2 := os.execute('${os.quoted_path(bin)} gzip ${os.quoted_path(ip)} ${os.quoted_path(gp)}')
|
||||
if res2.exit_code != 0 {
|
||||
eprintln('C gzip failed: ${res2.output}')
|
||||
exit(1)
|
||||
}
|
||||
c_gzip := os.read_bytes(gp) or {
|
||||
eprintln('read C gzip stream failed: ${err.msg()}')
|
||||
exit(1)
|
||||
}
|
||||
v_decoded_gzip := deflate.decompress(c_gzip) or {
|
||||
eprintln('V decompress of C gzip failed: ${err.msg()}')
|
||||
exit(1)
|
||||
}
|
||||
if v_decoded_gzip != input {
|
||||
eprintln('C gzip -> V mismatch')
|
||||
exit(1)
|
||||
}
|
||||
println('OK: C gzip -> V decompress')
|
||||
|
||||
v_zlib := deflate.compress(input) or {
|
||||
eprintln('V zlib compress failed: ${err.msg()}')
|
||||
exit(1)
|
||||
}
|
||||
os.write_file_array(vp, v_zlib) or {
|
||||
eprintln('write V zlib failed: ${err.msg()}')
|
||||
exit(1)
|
||||
}
|
||||
res3 :=
|
||||
os.execute('${os.quoted_path(bin)} decompress ${os.quoted_path(vp)} ${os.quoted_path(dp)}')
|
||||
if res3.exit_code != 0 {
|
||||
eprintln('C decompress of V zlib failed: ${res3.output}')
|
||||
exit(1)
|
||||
}
|
||||
c_unzlib := os.read_bytes(dp) or {
|
||||
eprintln('read C zlib decompressed output failed: ${err.msg()}')
|
||||
exit(1)
|
||||
}
|
||||
if c_unzlib != input {
|
||||
eprintln('V zlib -> C mismatch')
|
||||
exit(1)
|
||||
}
|
||||
println('OK: V zlib -> C decompress')
|
||||
|
||||
v_gzip := deflate.compress(input, format: .gzip) or {
|
||||
eprintln('V gzip compress failed: ${err.msg()}')
|
||||
exit(1)
|
||||
}
|
||||
os.write_file_array(vgp, v_gzip) or {
|
||||
eprintln('write V gzip failed: ${err.msg()}')
|
||||
exit(1)
|
||||
}
|
||||
res4 := os.execute('${os.quoted_path(bin)} gunzip ${os.quoted_path(vgp)} ${os.quoted_path(dp)}')
|
||||
if res4.exit_code != 0 {
|
||||
eprintln('C gunzip of V gzip failed: ${res4.output}')
|
||||
exit(1)
|
||||
}
|
||||
c_ungzip := os.read_bytes(dp) or {
|
||||
eprintln('read C gzip decompressed output failed: ${err.msg()}')
|
||||
exit(1)
|
||||
}
|
||||
if c_ungzip != input {
|
||||
eprintln('V gzip -> C mismatch')
|
||||
exit(1)
|
||||
}
|
||||
println('OK: V gzip -> C decompress')
|
||||
|
||||
if os.execute('python3 --version').exit_code == 0 {
|
||||
py_src := os.join_path(@DIR, 'deflate_ref.py')
|
||||
py_driver := os.join_path(workdir, 'deflate_ref.py')
|
||||
os.cp(py_src, py_driver) or {
|
||||
eprintln('copy Python reference script failed: ${err.msg()}')
|
||||
exit(1)
|
||||
}
|
||||
|
||||
py_zp := os.join_path(workdir, 'xval_py_zlib.bin')
|
||||
py_gp := os.join_path(workdir, 'xval_py_gzip.bin')
|
||||
|
||||
res5 :=
|
||||
os.execute('python3 ${os.quoted_path(py_driver)} compress ${os.quoted_path(ip)} ${os.quoted_path(py_zp)}')
|
||||
if res5.exit_code != 0 {
|
||||
eprintln('Python zlib compress failed: ${res5.output}')
|
||||
exit(1)
|
||||
}
|
||||
py_zlib := os.read_bytes(py_zp) or {
|
||||
eprintln('read Python zlib stream failed: ${err.msg()}')
|
||||
exit(1)
|
||||
}
|
||||
if deflate.decompress(py_zlib) or { []u8{} } != input {
|
||||
eprintln('Python zlib -> V mismatch')
|
||||
exit(1)
|
||||
}
|
||||
println('OK: Python zlib -> V decompress')
|
||||
|
||||
res6 :=
|
||||
os.execute('python3 ${os.quoted_path(py_driver)} gzip ${os.quoted_path(ip)} ${os.quoted_path(py_gp)}')
|
||||
if res6.exit_code != 0 {
|
||||
eprintln('Python gzip failed: ${res6.output}')
|
||||
exit(1)
|
||||
}
|
||||
py_gzip := os.read_bytes(py_gp) or {
|
||||
eprintln('read Python gzip stream failed: ${err.msg()}')
|
||||
exit(1)
|
||||
}
|
||||
if deflate.decompress(py_gzip) or { []u8{} } != input {
|
||||
eprintln('Python gzip -> V mismatch')
|
||||
exit(1)
|
||||
}
|
||||
println('OK: Python gzip -> V decompress')
|
||||
|
||||
py_unz := os.join_path(workdir, 'xval_py_unz.bin')
|
||||
res7 :=
|
||||
os.execute('python3 ${os.quoted_path(py_driver)} decompress ${os.quoted_path(vp)} ${os.quoted_path(py_unz)}')
|
||||
if res7.exit_code != 0 {
|
||||
eprintln('Python decompress of V zlib failed: ${res7.output}')
|
||||
exit(1)
|
||||
}
|
||||
if (os.read_bytes(py_unz) or { []u8{} }) != input {
|
||||
eprintln('V zlib -> Python mismatch')
|
||||
exit(1)
|
||||
}
|
||||
println('OK: V zlib -> Python decompress')
|
||||
|
||||
py_ungz := os.join_path(workdir, 'xval_py_ungz.bin')
|
||||
res8 :=
|
||||
os.execute('python3 ${os.quoted_path(py_driver)} gunzip ${os.quoted_path(vgp)} ${os.quoted_path(py_ungz)}')
|
||||
if res8.exit_code != 0 {
|
||||
eprintln('Python gunzip of V gzip failed: ${res8.output}')
|
||||
exit(1)
|
||||
}
|
||||
if (os.read_bytes(py_ungz) or { []u8{} }) != input {
|
||||
eprintln('V gzip -> Python mismatch')
|
||||
exit(1)
|
||||
}
|
||||
println('OK: V gzip -> Python decompress')
|
||||
} else {
|
||||
eprintln('Skipping Python cross-validation: python3 not found')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <zlib.h>
|
||||
|
||||
static int rf(const char *p, unsigned char **o, size_t *n) {
|
||||
FILE *f = fopen(p, "rb");
|
||||
if (!f) return 1;
|
||||
fseek(f, 0, SEEK_END);
|
||||
long s = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
*o = malloc(*n = (size_t)s);
|
||||
if (!*o) {
|
||||
fclose(f);
|
||||
return 1;
|
||||
}
|
||||
if (fread(*o, 1, *n, f) != *n) {
|
||||
free(*o);
|
||||
fclose(f);
|
||||
return 1;
|
||||
}
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int wf(const char *p, const unsigned char *b, size_t n) {
|
||||
FILE *f = fopen(p, "wb");
|
||||
if (!f) return 1;
|
||||
if (fwrite(b, 1, n, f) != n) {
|
||||
fclose(f);
|
||||
return 1;
|
||||
}
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc != 4) {
|
||||
fputs("usage: xval compress|decompress|gzip|gunzip in out\n", stderr);
|
||||
return 2;
|
||||
}
|
||||
unsigned char *in;
|
||||
size_t in_n;
|
||||
if (rf(argv[2], &in, &in_n)) {
|
||||
fputs("read error\n", stderr);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "compress") == 0) {
|
||||
uLongf cap = compressBound((uLong)in_n);
|
||||
unsigned char *out = malloc(cap);
|
||||
if (!out) return 1;
|
||||
if (compress2(out, &cap, in, (uLong)in_n, Z_DEFAULT_COMPRESSION) != Z_OK) {
|
||||
fputs("compress2 failed\n", stderr);
|
||||
free(in);
|
||||
free(out);
|
||||
return 1;
|
||||
}
|
||||
wf(argv[3], out, (size_t)cap);
|
||||
free(out);
|
||||
} else if (strcmp(argv[1], "decompress") == 0) {
|
||||
uLongf cap = in_n * 8 + 65536;
|
||||
unsigned char *out = malloc(cap);
|
||||
if (!out) return 1;
|
||||
if (uncompress(out, &cap, in, (uLong)in_n) != Z_OK) {
|
||||
fputs("uncompress failed\n", stderr);
|
||||
free(in);
|
||||
free(out);
|
||||
return 1;
|
||||
}
|
||||
wf(argv[3], out, (size_t)cap);
|
||||
free(out);
|
||||
} else if (strcmp(argv[1], "gzip") == 0) {
|
||||
z_stream s;
|
||||
memset(&s, 0, sizeof(s));
|
||||
if (deflateInit2(&s, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 | 16, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
|
||||
free(in);
|
||||
return 1;
|
||||
}
|
||||
uLongf cap = deflateBound(&s, (uLong)in_n) + 32;
|
||||
unsigned char *out = malloc(cap);
|
||||
if (!out) {
|
||||
deflateEnd(&s);
|
||||
free(in);
|
||||
return 1;
|
||||
}
|
||||
s.next_in = in;
|
||||
s.avail_in = (uInt)in_n;
|
||||
s.next_out = out;
|
||||
s.avail_out = (uInt)cap;
|
||||
if (deflate(&s, Z_FINISH) != Z_STREAM_END) {
|
||||
fputs("gzip deflate failed\n", stderr);
|
||||
deflateEnd(&s);
|
||||
free(in);
|
||||
free(out);
|
||||
return 1;
|
||||
}
|
||||
wf(argv[3], out, (size_t)s.total_out);
|
||||
deflateEnd(&s);
|
||||
free(out);
|
||||
} else if (strcmp(argv[1], "gunzip") == 0) {
|
||||
z_stream s;
|
||||
memset(&s, 0, sizeof(s));
|
||||
if (inflateInit2(&s, 15 | 16) != Z_OK) {
|
||||
free(in);
|
||||
return 1;
|
||||
}
|
||||
uLongf cap = in_n * 8 + 65536;
|
||||
unsigned char *out = malloc(cap);
|
||||
if (!out) {
|
||||
inflateEnd(&s);
|
||||
free(in);
|
||||
return 1;
|
||||
}
|
||||
s.next_in = in;
|
||||
s.avail_in = (uInt)in_n;
|
||||
s.next_out = out;
|
||||
s.avail_out = (uInt)cap;
|
||||
if (inflate(&s, Z_FINISH) != Z_STREAM_END) {
|
||||
fputs("gunzip inflate failed\n", stderr);
|
||||
inflateEnd(&s);
|
||||
free(in);
|
||||
free(out);
|
||||
return 1;
|
||||
}
|
||||
wf(argv[3], out, (size_t)s.total_out);
|
||||
inflateEnd(&s);
|
||||
free(out);
|
||||
}
|
||||
|
||||
free(in);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import sys, zlib
|
||||
|
||||
def rf(p):
|
||||
with open(p, "rb") as f: return f.read()
|
||||
|
||||
def wf(p, b):
|
||||
with open(p, "wb") as f: f.write(b)
|
||||
|
||||
if len(sys.argv) != 4:
|
||||
print("usage: py_ref compress|decompress|gzip|gunzip in out", file=sys.stderr)
|
||||
sys.exit(2)
|
||||
|
||||
mode, inp, outp = sys.argv[1], sys.argv[2], sys.argv[3]
|
||||
data = rf(inp)
|
||||
if mode == "compress":
|
||||
wf(outp, zlib.compress(data))
|
||||
elif mode == "decompress":
|
||||
wf(outp, zlib.decompress(data))
|
||||
elif mode == "gzip":
|
||||
co = zlib.compressobj(level=6, wbits=16 + zlib.MAX_WBITS)
|
||||
wf(outp, co.compress(data) + co.flush())
|
||||
elif mode == "gunzip":
|
||||
wf(outp, zlib.decompress(data, 16 + zlib.MAX_WBITS))
|
||||
else:
|
||||
print("unknown mode", file=sys.stderr)
|
||||
sys.exit(2)
|
||||
|
||||
Reference in new issue
Block a user