Skip to content

Commit

Permalink
Add crc32 jet (#653)
Browse files Browse the repository at this point in the history
Adds crc32 jet. This will be used to validate uncompressed gzip data.
urbit/urbit#6997
  • Loading branch information
pkova authored Jun 26, 2024
2 parents 43fc5be + 9e10ece commit d4e2b6f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
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);
}
}
8 changes: 8 additions & 0 deletions pkg/noun/jets/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 },
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 @@ -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);
Expand Down

0 comments on commit d4e2b6f

Please sign in to comment.