Skip to content

Commit

Permalink
jets: jet +crc32:crc:checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
levernathan authored and mikolajpp committed Aug 19, 2024
1 parent f5d289c commit 42d5c38
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
56 changes: 56 additions & 0 deletions pkg/noun/jets/e/crc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/// @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 tail = u3t(input_octs);
c3_w len_w = u3r_met(3, tail);
c3_y* input;

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

c3_w p_octs_w;

if (c3n == u3r_safe_word(u3h(input_octs), &p_octs_w)) {
return u3_none;
}

c3_w lead_w = p_octs_w - len_w;
c3_w crc = 0L;

crc = crc32(crc, input, len_w);

c3_w zero_w = 0;
while (lead_w-- > 0) {
crc = crc32(crc, &zero_w, 1);
}

return u3i_word(crc);
}

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);
}
}
20 changes: 14 additions & 6 deletions pkg/noun/jets/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -2341,13 +2341,20 @@ static u3j_core _138_hex_blake_d[] =
{}
};

static u3j_harm _138_hex_adler32_a[] = {{".2", u3we_adler32, c3y}, {}};
static u3j_core _138_hex_adler_d[] =
{ { "adler32", 7, _138_hex_adler32_a, 0, no_hashes },
static u3j_harm _137_hex__adler32_a[] = {{".2", u3we_adler32, c3y}, {}};
static u3j_core _137_hex__adler_d[] =
{ { "adler32", 7, _137_hex__adler32_a, 0, no_hashes },
{}
};
static u3j_core _138_hex_checksum_d[] =
{ { "adler", 7, 0, _138_hex_adler_d, no_hashes },
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_core _137_hex_checksum_d[] =
{ { "adler", 3, 0, _137_hex__adler_d, no_hashes },
{ "crc", 3, 0, _137_hex__crc_d, no_hashes},
{}
};

Expand All @@ -2369,7 +2376,7 @@ static u3j_core _138_hex_d[] =
{ "secp", 6, 0, _140_hex_secp_d, no_hashes },
{ "mimes", 31, 0, _140_hex_mimes_d, no_hashes },
{ "json", 31, 0, _139_hex_json_d, no_hashes },
{ "checksum", 15, 0, _138_hex_checksum_d, no_hashes},
{ "checksum", 15, 0, _137_hex_checksum_d, no_hashes},
{}
};

Expand Down Expand Up @@ -2632,6 +2639,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
1 change: 1 addition & 0 deletions pkg/noun/jets/w.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@
u3_noun u3we_blake3_compress(u3_noun);

u3_noun u3we_adler32(u3_noun);
u3_noun u3we_crc32(u3_noun);

u3_noun u3we_ripe(u3_noun);

Expand Down

0 comments on commit 42d5c38

Please sign in to comment.