Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/noun/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ const c_source_files = [_][]const u8{
"jets/e/tape.c",
"jets/e/trip.c",
"jets/e/urwasm.c",
"jets/e/zlib.c",
"jets/f/cell.c",
"jets/f/comb.c",
"jets/f/cons.c",
Expand Down
11 changes: 10 additions & 1 deletion pkg/noun/jets/136/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ static u3j_core _136_hex_checksum_d[] =
};


static u3j_harm _136_hex__decompress_zlib_a[] = {{".2", u3we_decompress_zlib}, {}};
static u3j_harm _136_hex__decompress_gzip_a[] = {{".2", u3we_decompress_gzip}, {}};
static u3j_core _136_hex__zlib_d[] = {
{"decompress-zlib", 7, _136_hex__decompress_zlib_a, 0, no_hashes },
{"decompress-gzip", 7, _136_hex__decompress_gzip_a, 0, no_hashes },
{}};


static u3j_harm _136_hex_coed__ed_scad_a[] = {{".2", u3wee_scad}, {}};
static u3j_harm _136_hex_coed__ed_scas_a[] = {{".2", u3wee_scas}, {}};
static u3j_harm _136_hex_coed__ed_scap_a[] = {{".2", u3wee_scap}, {}};
Expand Down Expand Up @@ -1016,9 +1024,10 @@ static u3j_core _136_hex_d[] =
{ "secp", 6, 0, _136_hex_secp_d, no_hashes },
{ "mimes", 31, 0, _136_hex_mimes_d, no_hashes },
{ "json", 31, 0, _136_hex_json_d, no_hashes },
{ "checksum", 15, 0, _136_hex_checksum_d, no_hashes},
{ "wasm-sur-v0", 3, 0, _136_hex_wasm_sur_d, no_hashes },
{ "bytestream-v0", 31, 0, _136_hex_bytestream_d, no_hashes},
{ "checksum", 15, 0, _136_hex_checksum_d, no_hashes},
{ "zlib-v0", 31, 0, _136_hex__zlib_d, no_hashes },
{}
};

Expand Down
225 changes: 225 additions & 0 deletions pkg/noun/jets/e/zlib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
/// @file

#include <allocate.h>
#include <stdio.h>
#include "zlib.h"

#include "jets/q.h"
#include "jets/w.h"

#include "noun.h"

static void*
zlib_malloc(voidpf opaque, uInt items, uInt size)
{
size_t len = items * size;
void* result = u3a_malloc(len);
return result;
}

static void
zlib_free(voidpf opaque, voidpf address)
{
u3a_free(address);
}

u3_noun
_decompress(u3_atom pos, u3_noun octs, int window_bits)
{
u3_atom p_octs = u3h(octs);
u3_atom q_octs = u3t(octs);

c3_w p_octs_w, pos_w;

if ( c3n == u3r_safe_word(p_octs, &p_octs_w) ) {
return u3_none;
}
if (c3n == u3r_safe_word(pos, &pos_w)) {
return u3_none;
}

c3_w len_w = u3r_met(3, q_octs);

int leading_zeros = 0;

if (p_octs_w > len_w) {
leading_zeros = p_octs_w - len_w;
}
else {
len_w = p_octs_w;
}

// Bytestream exhausted
//
if (pos_w >= len_w) {
return u3_none;
}

c3_y* input;

if (c3y == u3a_is_cat(q_octs)) {
input = (c3_y*)&q_octs + pos_w;
}
else {
u3a_atom* vat_u = u3a_to_ptr(q_octs);
input = (c3_y*)vat_u->buf_w + pos_w;
}

int ret;
z_stream strm;

if (pos_w < len_w) {
strm.avail_in = (len_w - pos_w);
}
else {
strm.avail_in = 0;
}

strm.zalloc = zlib_malloc;
strm.zfree = zlib_free;
strm.opaque = Z_NULL;
strm.next_in = input;

ret = inflateInit2(&strm, window_bits);

if (ret != Z_OK) {
u3l_log("%i", ret);
u3l_log("%s", strm.msg);
return u3m_bail(c3__exit);
}

c3_w chunk_w = len_w / 10;
u3i_slab sab_u;

#define INIT_SZ 16384
strm.avail_out = INIT_SZ;
u3i_slab_init(&sab_u, 3, INIT_SZ);
strm.next_out = sab_u.buf_y;

void* this_address = strm.next_out;

#define ZEROS_SZ 256
c3_y zeros[ZEROS_SZ];

if (leading_zeros) {
memset(zeros, 0, ZEROS_SZ);
}

while ((ret = inflate(&strm, Z_FINISH)) == Z_BUF_ERROR) {

// Output exhausted: reallocate
//
if (strm.avail_out == 0) {
strm.avail_out = chunk_w;

u3i_slab_grow(&sab_u, 3, strm.total_out + chunk_w);
strm.next_out = sab_u.buf_y + strm.total_out;
}

// Input exhausted: input leading zeros?
//
if (strm.avail_in == 0) {

if (leading_zeros) {
// Position in the stream exceeded atom bytes,
// but is still below stream length
//
if (strm.total_in + pos_w >= len_w
&& strm.total_in + pos_w < p_octs_w) {

c3_w rem_w = p_octs_w - (strm.total_in + pos_w);
strm.next_in = zeros;

if (rem_w > ZEROS_SZ) {
strm.avail_in = ZEROS_SZ;
}
else {
strm.avail_in = rem_w;
}
}
else {
u3l_log("%i", ret);
u3l_log("%s", strm.msg);
inflateEnd(&strm);
u3i_slab_free(&sab_u);
return u3m_bail(c3__exit);
}
}
else {
u3l_log("%i", ret);
u3l_log("%s", strm.msg);
inflateEnd(&strm);
u3i_slab_free(&sab_u);
return u3m_bail(c3__exit);
}
}
}
if (ret != Z_STREAM_END) {
u3l_log("%i", ret);
u3l_log("%s", strm.msg);
inflateEnd(&strm);
u3i_slab_free(&sab_u);
return u3m_bail(c3__exit);
}
ret = inflateEnd(&strm);

if (ret != Z_OK) {
u3l_log("%i", ret);
u3l_log("%s", strm.msg);
u3i_slab_free(&sab_u);
return u3m_bail(c3__exit);
}

u3_noun decompressed_octs = u3nc(strm.total_out, u3i_slab_mint(&sab_u));
u3_noun new_pos = pos_w + strm.total_in;
u3_noun new_stream = u3nc(u3i_word(new_pos), u3k(octs));

return u3nc(decompressed_octs, new_stream);
}

u3_noun
u3qe_decompress_gzip(u3_atom pos, u3_noun octs)
{
return _decompress(pos, octs, 31);
}
u3_noun
u3qe_decompress_zlib(u3_atom pos, u3_noun octs)
{
return _decompress(pos, octs, 15);
}

u3_noun
u3we_decompress_gzip(u3_noun cor)
{
u3_atom pos;
u3_noun octs;

u3_noun a = u3r_at(u3x_sam, cor);
u3x_cell(a, &pos, &octs);

if(_(u3a_is_atom(pos)) && _(u3a_is_cell(octs))) {
return u3qe_decompress_gzip(pos, octs);
}

else {
return u3m_bail(c3__exit);
}
}

u3_noun
u3we_decompress_zlib(u3_noun cor)
{
u3_atom pos;
u3_noun octs;

u3_noun a = u3r_at(u3x_sam, cor);
u3x_cell(a, &pos, &octs);

if(_(u3a_is_atom(pos)) && _(u3a_is_cell(octs))) {
return u3qe_decompress_zlib(pos, octs);
}

else {
return u3m_bail(c3__exit);
}
}
3 changes: 3 additions & 0 deletions pkg/noun/jets/q.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@
u3_noun u3qes_gte(u3_atom, u3_atom);
u3_noun u3qes_gth(u3_atom, u3_atom);

u3_noun u3qe_decompress_zlib(u3_atom, u3_noun);
u3_noun u3qe_decompress_gzip(u3_atom, u3_noun);

/** Tier 6.
**/
u3_noun u3qf_bull(u3_noun, u3_noun);
Expand Down
2 changes: 2 additions & 0 deletions pkg/noun/jets/w.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@
u3_noun u3wes_gth(u3_noun);

u3_noun u3we_crc32(u3_noun);
u3_noun u3we_decompress_zlib(u3_noun);
u3_noun u3we_decompress_gzip(u3_noun);

u3_noun u3we_lia_run(u3_noun);
u3_noun u3we_lia_run_once(u3_noun);
Expand Down