/* functions */ u3_noun u3qc_muk(u3_atom seed, u3_atom len, u3_atom key) { c3_w seed_w; c3_w len_w; c3_y *key_y; c3_w out_w; c3_assert(u3r_met(5, seed) <= 1); c3_assert(u3r_met(0, len) <= 31); c3_assert(u3r_met(3, key) <= len); seed_w = u3r_word(0, seed); len_w = u3r_word(0, len); if (len_w > 0) { /* can't u3a_calloc 0 bytes */ key_y = u3a_calloc(sizeof(c3_y), len); } else { key_y = 0; } u3r_bytes(0, len, key_y, key); MurmurHash3_x86_32(key_y, len, seed_w, &out_w); u3a_free(key_y); return u3i_words(1, &out_w); }
u3_noun u3qee_shar(u3_atom pub, u3_atom sek) { c3_y pub_y[32], sek_y[32], self_y[32], exp_y[64], shr_y[32]; c3_w met_pub_w, met_sek_w; met_pub_w = u3r_met(3, pub); met_sek_w = u3r_met(3, sek); if ( (met_pub_w > 32) || (met_sek_w > 32) ) { return u3m_bail(c3__exit); } u3r_bytes(0, 32, pub_y, pub); u3r_bytes(0, 32, sek_y, sek); memset(self_y, 0, 32); memset(exp_y, 0, 64); memset(shr_y, 0, 32); ed25519_create_keypair(self_y, exp_y, sek_y); ed25519_key_exchange(shr_y, pub_y, exp_y); return u3i_bytes(32, shr_y); }
/* functions */ u3_noun u3qc_mix(u3_atom a, u3_atom b) { c3_w lna_w = u3r_met(5, a); c3_w lnb_w = u3r_met(5, b); if ( (lna_w == 0) && (lnb_w == 0) ) { return 0; } else { c3_w len_w = c3_max(lna_w, lnb_w); c3_w* sal_w = u3a_slab(len_w); if ( 0 == sal_w ) { return u3m_bail(c3__fail); } else { c3_w i_w; u3r_chop(5, 0, lna_w, 0, sal_w, a); for ( i_w = 0; i_w < lnb_w; i_w++ ) { sal_w[i_w] ^= u3r_word(i_w, b); } return u3a_malt(sal_w); } } }
/* functions */ u3_noun u3qf_slot(u3_atom axe, u3_noun vax) { u3_noun f*g = u3r_at(axe, u3t(vax)); fprintf(stderr, "slot axe %d\r\n", axe); if ( u3_none == f*g ) { return u3m_bail(c3__exit); } else { u3_noun typ = u3qfu_peek( } c3_w i_w, met_w = c3_min(u3r_met(3, axe), u3r_met(3, vax)); if ( c3n == _slot_fiz(axe, vax) ) { return c3n; } for ( i_w = 0; i_w < met_w; i_w++ ) { c3_y axe_y = u3r_byte(i_w, axe); c3_y vax_y = u3r_byte(i_w, vax); if ( (axe_y >= 'A') && (axe_y <= 'Z') ) axe_y = 0; if ( (vax_y >= 'A') && (vax_y <= 'Z') ) vax_y = 0; if ( axe_y && vax_y && (axe_y != vax_y) ) { return c3n; } } return c3y; }
// u3_noun // u3qe_shax( // u3_atom a) // { // c3_w met_w = u3r_met(3, a); // return u3qe_shay(met_w, a); // } // XX preformance u3_noun u3qe_shax(u3_atom a) { c3_w met_w = u3r_met(3, a); c3_y* fat_y = u3a_malloc(met_w + 1); u3r_bytes(0, met_w, fat_y, a); { c3_y dig_y[32]; #if defined(U3_OS_osx) CC_SHA256_CTX ctx_h; CC_SHA256_Init(&ctx_h); CC_SHA256_Update(&ctx_h, fat_y, met_w); CC_SHA256_Final(dig_y, &ctx_h); #else SHA256_CTX ctx_h; SHA256_Init(&ctx_h); SHA256_Update(&ctx_h, fat_y, met_w); SHA256_Final(dig_y, &ctx_h); #endif u3a_free(fat_y); return u3i_bytes(32, dig_y); } }
/* functions */ u3_noun u3qc_rsh(u3_atom a, u3_atom b, u3_atom c) { if ( !_(u3a_is_cat(a)) || (a >= 32) ) { return u3m_bail(c3__fail); } else if ( !_(u3a_is_cat(b)) ) { return 0; } else { c3_g a_g = a; c3_w b_w = b; c3_w len_w = u3r_met(a_g, c); if ( b_w >= len_w ) { return 0; } else { c3_w* sal_w = u3a_slaq(a_g, (len_w - b_w)); if ( 0 == sal_w ) { return u3m_bail(c3__fail); } u3r_chop(a_g, b_w, (len_w - b_w), 0, sal_w, c); // return u3a_moot(sal_w); return u3a_malt(sal_w); } } }
/* functions */ u3_noun u3qc_met(u3_atom a, u3_atom b) { if ( !_(u3a_is_cat(a)) || (a >= 32) ) { if ( 0 == b ) { return 0; } else return 1; } else { c3_w met_w = u3r_met(a, b); if ( !_(u3a_is_cat(met_w)) ) { return u3i_words(1, &met_w); } else return u3r_met(a, b); } }
/* _term_it_path(): path for console file. */ static c3_c* _term_it_path(u3_noun fyl, u3_noun pax) { c3_w len_w; c3_c *pas_c; // measure // len_w = strlen(u3_Host.dir_c); { u3_noun wiz = pax; while ( u3_nul != wiz ) { len_w += (1 + u3r_met(3, u3h(wiz))); wiz = u3t(wiz); } } // cut // pas_c = c3_malloc(len_w + 1); strncpy(pas_c, u3_Host.dir_c, len_w); pas_c[len_w] = '\0'; { u3_noun wiz = pax; c3_c* waq_c = (pas_c + strlen(pas_c)); while ( u3_nul != wiz ) { c3_w tis_w = u3r_met(3, u3h(wiz)); if ( (c3y == fyl) && (u3_nul == u3t(wiz)) ) { *waq_c++ = '.'; } else *waq_c++ = '/'; u3r_bytes(0, tis_w, (c3_y*)waq_c, u3h(wiz)); waq_c += tis_w; wiz = u3t(wiz); } *waq_c = 0; } u3z(pax); return pas_c; }
/* functions */ static u3_noun _fitz_fiz(u3_noun yaz, u3_noun wix) { c3_w yaz_w = u3r_met(3, yaz); c3_w wix_w = u3r_met(3, wix); c3_y yaz_y, wix_y; yaz_y = (0 == yaz_w) ? 0 : u3r_byte((yaz_w - 1), yaz); if ( (yaz_y < 'A') || (yaz_y > 'Z') ) yaz_y = 0; wix_y = (0 == wix_w) ? 0 : u3r_byte((wix_w - 1), wix); if ( (wix_y < 'A') || (wix_y > 'Z') ) wix_y = 0; if ( yaz_y && wix_y ) { if ( !wix_y || (wix_y > yaz_y) ) { return c3n; } } return c3y; }
u3_noun u3qf_fitz(u3_noun yaz, u3_noun wix) { c3_w i_w, met_w = c3_min(u3r_met(3, yaz), u3r_met(3, wix)); if ( c3n == _fitz_fiz(yaz, wix) ) { return c3n; } for ( i_w = 0; i_w < met_w; i_w++ ) { c3_y yaz_y = u3r_byte(i_w, yaz); c3_y wix_y = u3r_byte(i_w, wix); if ( (yaz_y >= 'A') && (yaz_y <= 'Z') ) yaz_y = 0; if ( (wix_y >= 'A') && (wix_y <= 'Z') ) wix_y = 0; if ( yaz_y && wix_y && (yaz_y != wix_y) ) { return c3n; } } return c3y; }
/* functions */ u3_noun u3qc_cap(u3_atom a) { c3_w met_w = u3r_met(0, a); if ( met_w < 2 ) { return u3m_bail(c3__exit); } else if ( (1 == u3r_bit((met_w - 2), a)) ) { return 3; } else { return 2; } }
/* functions */ u3_noun u3qc_cut(u3_atom a, u3_atom b, u3_atom c, u3_atom d) { if ( !_(u3a_is_cat(a)) || (a >= 32) ) { return u3m_bail(c3__fail); } if ( !_(u3a_is_cat(b)) ) { return 0; } if ( !_(u3a_is_cat(c)) ) { c = 0x7fffffff; } { c3_g a_g = a; c3_w b_w = b; c3_w c_w = c; c3_w len_w = u3r_met(a_g, d); if ( (0 == c_w) || (b_w >= len_w) ) { return 0; } if ( b_w + c_w > len_w ) { c_w = (len_w - b_w); } if ( (b_w == 0) && (c_w == len_w) ) { return u3k(d); } else { c3_w* sal_w = u3a_slaq(a_g, c_w); if ( 0 == sal_w ) { return u3m_bail(c3__fail); } u3r_chop(a_g, b_w, c_w, 0, sal_w, d); return u3a_malt(sal_w); } } }
/* _unix_write_file_hard(): write to a file, overwriting what's there */ static c3_w _unix_write_file_hard(c3_c* pax_c, u3_noun mim) { c3_i fid_i = open(pax_c, O_WRONLY | O_CREAT | O_TRUNC, 0666); c3_w len_w, rit_w, siz_w, mug_w = 0; c3_y* dat_y; u3_noun dat = u3t(u3t(mim)); if ( fid_i < 0 ) { uL(fprintf(uH, "error opening %s for writing: %s\r\n", pax_c, strerror(errno))); u3z(mim); return 0; } siz_w = u3h(u3t(mim)); len_w = u3r_met(3, dat); dat_y = c3_malloc(siz_w); memset(dat_y, 0, siz_w); u3r_bytes(0, len_w, dat_y, dat); u3z(mim); rit_w = write(fid_i, dat_y, siz_w); if ( rit_w != siz_w ) { uL(fprintf(uH, "error writing %s: %s\r\n", pax_c, strerror(errno))); mug_w = 0; } else { mug_w = u3r_mug_bytes(dat_y, len_w); } close(fid_i); free(dat_y); return mug_w; }
/* functions */ u3_noun u3qc_peg( u3_atom a, u3_atom b) { u3_atom c, d, e, f, g, h; c = u3r_met(0, b); d = u3qa_dec(c); e = u3qc_lsh(0, d, 1); f = u3qa_sub(b, e); g = u3qc_lsh(0, d, a); h = u3qa_add(f, g); u3z(c); u3z(d); u3z(e); u3z(f); u3z(g); return h; }
/* functions */ static u3_noun _cqee_veri(u3_noun s, u3_noun m, u3_noun pk) { c3_y sig_y[64]; c3_y pub_y[32]; c3_w ret; c3_y* mes_y; c3_w mesm_w = u3r_met(3, m); memset(sig_y, 0, 64); memset(pub_y, 0, 32); mes_y = c3_malloc(mesm_w); u3r_bytes(0, 64, sig_y, s); u3r_bytes(0, 32, pub_y, pk); u3r_bytes(0, mesm_w, mes_y, m); ret = ed25519_verify(sig_y, mes_y, mesm_w, pub_y) == 1 ? c3y : c3n; free(mes_y); return ret; }
/* functions */ u3_noun u3qc_mas(u3_atom a) { c3_w b_w; u3_atom c, d, e, f; b_w = u3r_met(0, a); if ( b_w < 2 ) { return u3m_bail(c3__exit); } else { c = u3qc_bex((b_w - 1)); d = u3qc_bex((b_w - 2)); e = u3qa_sub(a, c); f = u3qc_con(e, d); u3z(c); u3z(d); u3z(e); return f; } }
/* _sist_rest_nuu(): upgrade log from previous format. */ static void _sist_rest_nuu(u3_ulog* lug_u, u3_uled led_u, c3_c* old_c) { c3_c nuu_c[2048]; u3_noun roe = u3_nul; c3_i fid_i = lug_u->fid_i; c3_i fud_i; c3_i ret_i; c3_d end_d = lug_u->len_d; uL(fprintf(uH, "rest: converting log from prior format\n")); c3_assert(led_u.mag_l == u3r_mug('f')); if ( -1 == lseek64(fid_i, 4ULL * end_d, SEEK_SET) ) { uL(fprintf(uH, "rest_nuu failed (a)\n")); perror("lseek64"); u3_lo_bail(); } while ( end_d != c3_wiseof(u3_uled) ) { c3_d tar_d; u3_olar lar_u; c3_w* img_w; u3_noun ron; tar_d = (end_d - (c3_d)c3_wiseof(u3_olar)); if ( -1 == lseek64(fid_i, 4ULL * tar_d, SEEK_SET) ) { uL(fprintf(uH, "rest_nuu failed (b)\n")); perror("lseek64"); u3_lo_bail(); } if ( sizeof(u3_olar) != read(fid_i, &lar_u, sizeof(u3_olar)) ) { uL(fprintf(uH, "rest_nuu failed (c)\n")); perror("read"); u3_lo_bail(); } if ( lar_u.syn_w != u3r_mug((c3_w)tar_d) ) { uL(fprintf(uH, "rest_nuu failed (d)\n")); u3_lo_bail(); } img_w = c3_malloc(4 * lar_u.len_w); end_d = (tar_d - (c3_d)lar_u.len_w); if ( -1 == lseek64(fid_i, 4ULL * end_d, SEEK_SET) ) { uL(fprintf(uH, "rest_nuu failed (e)\n")); perror("lseek64"); u3_lo_bail(); } if ( (4 * lar_u.len_w) != read(fid_i, img_w, (4 * lar_u.len_w)) ) { uL(fprintf(uH, "rest_nuu failed (f)\n")); perror("read"); u3_lo_bail(); } ron = u3i_words(lar_u.len_w, img_w); free(img_w); if ( lar_u.mug_w != u3r_mug(ron) ) { uL(fprintf(uH, "rest_nuu failed (g)\n")); u3_lo_bail(); } roe = u3nc(ron, roe); } if ( 0 != close(fid_i) ) { uL(fprintf(uH, "rest: could not close\n")); perror("close"); u3_lo_bail(); } ret_i = snprintf(nuu_c, 2048, "%s/.urb/ham.hope", u3_Host.dir_c); c3_assert(ret_i < 2048); if ( (fud_i = open(nuu_c, O_CREAT | O_TRUNC | O_RDWR, 0600)) < 0 ) { uL(fprintf(uH, "rest: can't open record (%s)\n", nuu_c)); perror("open"); u3_lo_bail(); } led_u.mag_l = u3r_mug('g'); if ( (sizeof(led_u) != write(fud_i, &led_u, sizeof(led_u))) ) { uL(fprintf(uH, "rest: can't write header\n")); perror("write"); u3_lo_bail(); } { c3_d ent_d = 1; c3_assert(end_d == c3_wiseof(u3_uled)); while ( u3_nul != roe ) { u3_noun ovo = u3k(u3h(roe)); u3_noun nex = u3k(u3t(roe)); u3_ular lar_u; c3_w* img_w; c3_d tar_d; lar_u.len_w = u3r_met(5, ovo); tar_d = end_d + lar_u.len_w; lar_u.syn_w = u3r_mug(tar_d); lar_u.ent_d = ent_d; lar_u.tem_w = 0; lar_u.typ_w = c3__ov; lar_u.mug_w = u3r_mug_both(u3r_mug(ovo), u3r_mug_both(u3r_mug(0), u3r_mug(c3__ov))); img_w = c3_malloc(lar_u.len_w << 2); u3r_words(0, lar_u.len_w, img_w, ovo); u3z(ovo); if ( (lar_u.len_w << 2) != write(fud_i, img_w, lar_u.len_w << 2) ) { uL(fprintf(uH, "rest_nuu failed (h)\n")); perror("write"); u3_lo_bail(); } if ( sizeof(u3_ular) != write(fud_i, &lar_u, sizeof(u3_ular)) ) { uL(fprintf(uH, "rest_nuu failed (i)\n")); perror("write"); u3_lo_bail(); } ent_d++; end_d = tar_d + c3_wiseof(u3_ular); u3z(roe); roe = nex; } } if ( 0 != rename(nuu_c, old_c) ) { uL(fprintf(uH, "rest_nuu failed (k)\n")); perror("rename"); u3_lo_bail(); } if ( -1 == lseek64(fud_i, sizeof(u3_uled), SEEK_SET) ) { uL(fprintf(uH, "rest_nuu failed (l)\n")); perror("lseek64"); u3_lo_bail(); } lug_u->fid_i = fud_i; lug_u->len_d = end_d; }