diff --git a/pkg/noun/BUILD.bazel b/pkg/noun/BUILD.bazel index a6b8de6d7a..fd6963cb82 100644 --- a/pkg/noun/BUILD.bazel +++ b/pkg/noun/BUILD.bazel @@ -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"], diff --git a/pkg/noun/jets/e/crc32.c b/pkg/noun/jets/e/crc32.c new file mode 100644 index 0000000000..1afc473a30 --- /dev/null +++ b/pkg/noun/jets/e/crc32.c @@ -0,0 +1,59 @@ +/// @file + +#include +#include +#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); + } +} diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index c51859a928..e4fc71643c 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -270,6 +270,12 @@ static c3_c* _140_hex_lune_ha[] = { 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 _140_hex_coed__ed_puck_a[] = {{".2", u3wee_puck}, {}}; static c3_c* _140_hex_coed__ed_puck_ha[] = { "1bc694675842345c50b0e20a2193bb5bcbb42f163fc832431a3d1822a81e4c98", @@ -2346,6 +2352,8 @@ 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 }, { "coed", 63, 0, _140_hex_coed_d, no_hashes }, { "aes", 31, 0, _140_hex_aes_d, no_hashes }, diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index 34716971bd..07e8498344 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -291,6 +291,8 @@ u3_noun u3wes_gte(u3_noun); u3_noun u3wes_gth(u3_noun); + u3_noun u3we_crc32(u3_noun); + /** Tier 6. **/ u3_noun u3wf_bull(u3_noun);