/* functions */ u2_weak // produce j2_mbc(Pt5, shax)(u2_wire wir_r, u2_atom a) // retain { c3_w met_w = u2_met(3, a); c3_y* fat_y = malloc(met_w + 1); u2_bytes(0, met_w, fat_y, a); { c3_y dig_y[32]; #if defined(U2_OS_linux) SHA256_CTX ctx_h; SHA256_Init(&ctx_h); SHA256_Update(&ctx_h, fat_y, met_w); SHA256_Final(dig_y, &ctx_h); #elif defined(U2_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); #endif return u2_rl_bytes(wir_r, 32, dig_y); } }
u2_weak // produce j2_mbc(Pt5, shal)(u2_wire wir_r, u2_atom a, // retain u2_atom b) // retain { c3_assert(u2_fly_is_cat(a)); c3_y* fat_y = c3_malloc(a + 1); u2_bytes(0, a, fat_y, b); { c3_y dig_y[64]; #if defined(U2_OS_osx) CC_SHA512_CTX ctx_h; CC_SHA512_Init(&ctx_h); CC_SHA512_Update(&ctx_h, fat_y, a); CC_SHA512_Final(dig_y, &ctx_h); #else SHA512_CTX ctx_h; SHA512_Init(&ctx_h); SHA512_Update(&ctx_h, fat_y, a); SHA512_Final(dig_y, &ctx_h); #endif free(fat_y); return u2_rl_bytes(wir_r, 64, dig_y); } }
/* u2_bn_bytes(): ** ** Copy [a] bytes from [b]. */ u2_noun u2_bn_bytes(u2_ray wir_r, c3_w a_w, const c3_y* b_y) { return u2_bl_good(wir_r, u2_rl_bytes(wir_r, a_w, b_y)); }
/* u2_ux_read(): read a filesystem path/extension into an atom. */ u2_weak u2_ux_read(u2_ray wir_r, const c3_c* paf_c, const c3_c* ext_c) { c3_w len_w; c3_c* nam_c; if ( ext_c ) len_w = strlen(paf_c) + 1 + strlen(ext_c); else len_w = strlen(paf_c); nam_c = alloca(len_w + 1); if ( ext_c ) { sprintf(nam_c, "%s.%s", paf_c, ext_c); } else sprintf(nam_c, "%s", paf_c); { c3_i fid_i; struct stat sat_s; c3_w fln_w; c3_c* fil_c; u2_atom fil; fid_i = open(nam_c, O_RDONLY, 0666); if ( (fid_i < 0) || (fstat(fid_i, &sat_s) < 0) ) { return u2_none; } fln_w = sat_s.st_size; fil_c = malloc(sat_s.st_size); if ( fln_w != read(fid_i, fil_c, fln_w) ) { return u2_none; } close(fid_i); fil = u2_rl_bytes(wir_r, fln_w, (c3_y *)fil_c); free(fil_c); return fil; } }