Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jet zlib decompression library #711

Open
wants to merge 50 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
e2747c4
First commit
nathanlever Feb 14, 2024
f62d766
"Git pull"
nathanlever Feb 14, 2024
3b1738e
vere: decrements/rectifies kelvins
joemfb Apr 8, 2024
3b2da8f
Merge branch 'develop' of https://github.com/urbit/vere into develop
nathanlever Apr 11, 2024
5dbd674
gzip jet
nathanlever Apr 11, 2024
9f2b87c
Commit bazel files.
nathanlever May 20, 2024
ff5da07
commits bazel files
nathanlever May 20, 2024
43fc5be
Merge develop to next/kelvin/410 (#652)
pkova May 22, 2024
e9647a4
Add crc32 jet
nathanlever Feb 14, 2024
d7bb2c9
add zlib to dependencies
nathanlever May 23, 2024
2d10d98
remove old jet registration comments, correct crc32 parentheses
nathanlever May 23, 2024
bfd29c0
fix leading-zero placement
nathanlever Jun 4, 2024
bc3018c
Merge branch 'next/kelvin/410' into nathan/gzip
nathanlever Jun 7, 2024
b3987e2
crc: tidy up the crc32 jet
pkova Jun 25, 2024
a6eac43
crc: crc -> crc_w
pkova Jun 25, 2024
59abae9
gzip: remove comments
nathanlever Jun 25, 2024
1e6253d
crc: fix bogus head
pkova Jun 25, 2024
9e10ece
crc: add comment to move the jet to 137
pkova Jun 26, 2024
d4e2b6f
Add crc32 jet (#653)
pkova Jun 26, 2024
02c1e82
Fix jet registrations. Fix crc32 atom head.
nathanlever Jun 27, 2024
3d9e45d
pier: double boot protection
ripperi May 30, 2024
d0e6e49
king: king_curl_alloc to header
ripperi Jun 13, 2024
d9c49be
pier: type changes and fix double boot galaxy check
ripperi Jun 13, 2024
c150827
king: king_curl_bytes to header
ripperi Jun 14, 2024
0e6f2f3
pier: refactor double boot protection
ripperi Jun 14, 2024
88b7a38
pier: boot scry return value to unit
ripperi Jun 24, 2024
d8f21d4
king: break king_curl_bytes retry loop on 404
ripperi Jun 25, 2024
e8e0457
pier: add packet-pump-state length to %boot scry return value
ripperi Jun 25, 2024
62cc27a
pier: remove unnecessary variables from _boot_scry_cb
ripperi Jun 26, 2024
c642d95
pier: change number of live packets to next ack in %boot scry
ripperi Jun 27, 2024
827bcac
pier: change boot response type from @ud to @uw
ripperi Jun 27, 2024
e712922
pier: cleanup _czar_peer_rift
ripperi Jun 27, 2024
c4489aa
pier: change boot endpoint response type
ripperi Jun 28, 2024
6945599
king: break king_curl_bytes retry loop on client side errors
ripperi Jun 28, 2024
46575f8
pier: `_resolve_czar`, remove redundant `u3k`s & and free czar noun
ripperi Jul 2, 2024
8f9d0b2
pier: fix styling of double boot protection if-statements
ripperi Jul 2, 2024
600e0f3
pier: safely destructure %boot http response
ripperi Jul 2, 2024
92a8b32
pier: use `u3_king_bail` instead of `exit`
ripperi Jul 2, 2024
fad9af6
pier: change `%boot` response bodies into binary data
ripperi Jul 2, 2024
a53619a
Merge develop to next/kelvin/410 (#666)
pkova Jul 2, 2024
fe32777
remove custom oct_to_buf and buf_to_oct functions
nathanlever Jul 3, 2024
85ae9b3
Double boot protection (#657)
pkova Jul 10, 2024
699e6bf
Merge develop to next/kelvin/410 (#673)
pkova Jul 11, 2024
03e32d5
remove unnecessary files and file changes
nathanlever Jul 11, 2024
af94878
fix input leading zero issue, change function names
nathanlever Jul 11, 2024
f7e4863
add back crc32.c file and zlib dependency
nathanlever Jul 11, 2024
1d36910
Merge branch 'next/kelvin/410' into nathan/gzip
pkova Jul 12, 2024
2702de6
remove uv.h
nathanlever Jul 12, 2024
008a91d
zlib jets: rename gzip to zlib
mikolajpp Jul 25, 2024
c08fddf
zlib jets: bytestream decompression
mikolajpp Jul 25, 2024
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.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ vere_library(
"@sigsegv",
"@softfloat",
"@urcrypt",
"@zlib",
] + select({
"@platforms//os:macos": ["//pkg/noun/platform/darwin"],
"@platforms//os:linux": ["//pkg/noun/platform/linux"],
Expand Down
59 changes: 59 additions & 0 deletions pkg/noun/jets/e/crc32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/// @file

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

#include "jets/w.h"

#include "noun.h"

u3_noun
u3qe_crc32(u3_noun input_octs)
{
u3_atom head = u3h(input_octs);
u3_atom tail = u3t(input_octs);
c3_w tel_w = u3r_met(3, tail);
c3_w hed_w;
if ( c3n == u3r_safe_word(head, &hed_w) ) {
return u3m_bail(c3__fail);
}
c3_y* input;

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

if ( tel_w > hed_w ) {
return u3m_error("subtract-underflow");
}

c3_w led_w = hed_w - tel_w;
c3_w crc_w = 0;

crc_w = crc32(crc_w, input, tel_w);

while ( led_w > 0 ) {
c3_y byt_y = 0;
crc_w = crc32(crc_w, &byt_y, 1);
led_w--;
}

return u3i_word(crc_w);
}

u3_noun
u3we_crc32(u3_noun cor)
{
u3_noun a = u3r_at(u3x_sam, cor);

if ( (u3du(a) == c3y) && (u3ud(u3h(a)) == c3y) && (u3ud(u3t(a)) == c3y) ) {
return u3qe_crc32(a);
} else {
return u3m_bail(c3__exit);
}
}
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 @@ -212,6 +212,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
13 changes: 12 additions & 1 deletion pkg/noun/jets/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,14 @@ static c3_c* _140_hex_lune_ha[] = {
"417472f35b885fe6dd0715e78fd0920cb59f68b738aadc9768e73bc5efa0e570",
0
};

static u3j_harm _137_hex__crc32_a[] = {{".2", u3we_crc32}, {}};
static u3j_core _137_hex__crc_d[] = {{"crc32", 7, _137_hex__crc32_a, 0, no_hashes }, {}};
static u3j_harm _139_hex__decompress_zlib_a[] = {{".2", u3we_decompress_zlib}, {}};
static u3j_harm _139_hex__decompress_gzip_a[] = {{".2", u3we_decompress_gzip}, {}};
static u3j_core _139_hex__zlib_d[] = {
{"decompress-zlib", 7, _139_hex__decompress_zlib_a, 0, no_hashes },
{"decompress-gzip", 7, _139_hex__decompress_gzip_a, 0, no_hashes },
{}};
static u3j_harm _140_hex_coed__ed_puck_a[] = {{".2", u3wee_puck}, {}};
static c3_c* _140_hex_coed__ed_puck_ha[] = {
"1bc694675842345c50b0e20a2193bb5bcbb42f163fc832431a3d1822a81e4c98",
Expand Down Expand Up @@ -2346,6 +2353,9 @@ static u3j_core _138_hex_d[] =
{ "leer", 63, _140_hex_leer_a, 0, no_hashes },
{ "loss", 63, _140_hex_loss_a, 0, no_hashes },
{ "lune", 127, _140_hex_lune_a, 0, no_hashes },
// XX move me to 137 when it exists
{ "crc", 31, 0, _137_hex__crc_d, no_hashes },
{ "zlib", 31, 0, _139_hex__zlib_d, no_hashes },

{ "coed", 63, 0, _140_hex_coed_d, no_hashes },
{ "aes", 31, 0, _140_hex_aes_d, no_hashes },
Expand Down Expand Up @@ -2621,6 +2631,7 @@ static u3j_core _d[] =
{ { "k140", 0, 0, _k140_d, _k140_ha, 0, (u3j_core*) 140, 0 },
{ "k139", 0, 0, _k139_d, no_hashes, 0, (u3j_core*) 139, 0 },
{ "k138", 0, 0, _k138_d, no_hashes, 0, (u3j_core*) 138, 0 },
{ "k137", 0, 0, _k138_d, no_hashes, 0, (u3j_core*) 137, 0 },
{ "a50", 0, 0, _a50_d, _k140_ha, 0, (u3j_core*) c3__a50, 0 },
{}
};
Expand Down
4 changes: 4 additions & 0 deletions pkg/noun/jets/w.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@
u3_noun u3wes_gte(u3_noun);
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);

/** Tier 6.
**/
u3_noun u3wf_bull(u3_noun);
Expand Down
22 changes: 2 additions & 20 deletions pkg/vere/dawn.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,6 @@ _dawn_buf_to_oct(uv_buf_t buf_u)
return u3nc(len, u3i_bytes(buf_u.len, (const c3_y*)buf_u.base));
}


/* _dawn_curl_alloc(): allocate a response buffer for curl
*/
static size_t
_dawn_curl_alloc(void* dat_v, size_t uni_t, size_t mem_t, void* buf_v)
{
uv_buf_t* buf_u = buf_v;

size_t siz_t = uni_t * mem_t;
buf_u->base = c3_realloc(buf_u->base, 1 + siz_t + buf_u->len);

memcpy(buf_u->base + buf_u->len, dat_v, siz_t);
buf_u->len += siz_t;
buf_u->base[buf_u->len] = 0;

return siz_t;
}

/* _dawn_post_json(): POST JSON to url_c
*/
static uv_buf_t
Expand All @@ -84,7 +66,7 @@ _dawn_post_json(c3_c* url_c, uv_buf_t lod_u)
//
u3K.ssl_curl_f(curl);
curl_easy_setopt(curl, CURLOPT_URL, url_c);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _dawn_curl_alloc);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, king_curl_alloc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&buf_u);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hed_u);

Expand Down Expand Up @@ -131,7 +113,7 @@ _dawn_get_jam(c3_c* url_c)
//
u3K.ssl_curl_f(curl);
curl_easy_setopt(curl, CURLOPT_URL, url_c);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _dawn_curl_alloc);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, king_curl_alloc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&buf_u);

result = curl_easy_perform(curl);
Expand Down
Loading