From d969775d8fdae6fe01bd5bd50a81cba318287ea8 Mon Sep 17 00:00:00 2001 From: Fang Date: Fri, 13 Jul 2018 02:01:31 +0200 Subject: [PATCH] Implement jet for ++sha-1:sha. --- include/jets/q.h | 1 + include/jets/w.h | 1 + jets/e/sha1.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ jets/tree.c | 7 ++++++ meson.build | 1 + 5 files changed, 72 insertions(+) create mode 100644 jets/e/sha1.c diff --git a/include/jets/q.h b/include/jets/q.h index 09a3a0c24..bf9ff4cce 100644 --- a/include/jets/q.h +++ b/include/jets/q.h @@ -134,6 +134,7 @@ u3_noun u3qe_shay(u3_atom, u3_atom); u3_noun u3qe_shas(u3_atom, u3_atom); u3_noun u3qe_shal(u3_atom, u3_atom); + u3_noun u3qe_sha1(u3_atom, u3_atom); u3_noun u3qeo_raw(u3_atom, u3_atom); diff --git a/include/jets/w.h b/include/jets/w.h index c656d4172..1d8f55cf8 100644 --- a/include/jets/w.h +++ b/include/jets/w.h @@ -145,6 +145,7 @@ u3_noun u3we_shay(u3_noun); u3_noun u3we_shas(u3_noun); u3_noun u3we_shal(u3_noun); + u3_noun u3we_sha1(u3_noun); u3_noun u3weo_raw(u3_noun); diff --git a/jets/e/sha1.c b/jets/e/sha1.c new file mode 100644 index 000000000..2910c1fbe --- /dev/null +++ b/jets/e/sha1.c @@ -0,0 +1,62 @@ +/* j/5/sha1.c +** +*/ +#include "all.h" + + +#if defined(U3_OS_osx) +#include +#else +#include +#endif + +/* functions +*/ + + u3_noun + u3qe_sha1(u3_atom wid, + u3_atom dat) + { + c3_assert(_(u3a_is_cat(wid))); + dat = u3qc_rev(3, wid, dat); + + //NOTE XX fixme we shouldn't c3_malloc in inner road, + // this is also done in other SHA jets + c3_y* fat_y = c3_malloc(wid + 1); + u3r_bytes(0, wid, fat_y, dat); + { + c3_y dig_y[32]; +#if defined(U3_OS_osx) + CC_SHA1_CTX ctx_h; + + CC_SHA1_Init(&ctx_h); + CC_SHA1_Update(&ctx_h, fat_y, wid); + CC_SHA1_Final(dig_y, &ctx_h); +#else + SHA1_CTX ctx_h; + + SHA1_Init(&ctx_h); + SHA1_Update(&ctx_h, fat_y, wid); + SHA1_Final(dig_y, &ctx_h); +#endif + free(fat_y); + u3z(dat); + return u3kc_rev(3, 20, u3i_bytes(20, dig_y)); + } + } + + u3_noun + u3we_sha1(u3_noun cor) + { + u3_noun wid, dat; + + if ( (c3n == u3r_mean(cor, u3x_sam_2, &wid, u3x_sam_3, &dat, 0)) || + (c3n == u3ud(wid)) || + (c3n == u3a_is_cat(wid)) || + (c3n == u3ud(dat)) ) + { + return u3m_bail(c3__exit); + } else { + return u3qe_sha1(wid, dat); + } + } diff --git a/jets/tree.c b/jets/tree.c index ef71fb74d..2c36b6419 100644 --- a/jets/tree.c +++ b/jets/tree.c @@ -446,6 +446,12 @@ static u3j_core _143_qua_d[] = {} }; + static u3j_harm _143_tri__sha_sha1_a[] = {{".2", u3we_sha1}, {}}; + static u3j_core _143_tri__sha_d[] = + { { "sha1", _143_tri__sha_sha1_a }, + {} + }; + static u3j_harm _143_tri_shax_a[] = {{".2", u3we_shax}, {}}; static u3j_harm _143_tri_shay_a[] = {{".2", u3we_shay}, {}}; static u3j_harm _143_tri_shas_a[] = {{".2", u3we_shas}, {}}; @@ -460,6 +466,7 @@ static u3j_core _143_tri_d[] = { "rq", 0, _143_tri__rq_d }, { "rh", 0, _143_tri__rh_d }, { "og", 0, _143_tri__og_d }, + { "sha", 0, _143_tri__sha_d }, { "shax", _143_tri_shax_a }, { "shay", _143_tri_shay_a }, { "shas", _143_tri_shas_a }, diff --git a/meson.build b/meson.build index 21311ab86..bcc3e3da1 100644 --- a/meson.build +++ b/meson.build @@ -118,6 +118,7 @@ jets_e_src = [ 'jets/e/rub.c', 'jets/e/scr.c', 'jets/e/shax.c', +'jets/e/sha1.c', 'jets/e/lore.c', 'jets/e/loss.c', 'jets/e/lune.c',