/* _cf_path_1: write at/inpath. */ static c3_w _cf_path_1(c3_c* buf_c, c3_w pos_w, u2_noun hut) // retain { if ( u2_no == u2_cr_du(hut) ) { c3_w met_w = u2_cr_met(3, hut); if ( buf_c ) u2_cr_bytes(0, met_w, (c3_y*)(buf_c + pos_w), hut); return (pos_w + met_w); } else { c3_w met_w = u2_cr_met(3, u2h(hut)); c3_w end_w = _cf_path_1(buf_c, pos_w, u2t(hut)); u2_noun san = u2h(hut); if ( buf_c ) buf_c[end_w] = '/'; end_w++; // little security thing - last ditch // if ( c3_s2('.', '.') == san ) { san = c3_s3('.','.','.'); } if ( buf_c ) u2_cr_bytes(0, met_w, (c3_y*)(buf_c + end_w), san); end_w += met_w; return end_w; } }
/* u2_cf_flat_save(): save `som` as `mod` at `pas`. */ u2_bean u2_cf_flat_save(u2_noun mod, u2_noun pas, u2_noun som) { c3_assert(c3__atom == mod); { c3_c* pas_c = u2_cr_string(pas); c3_i fid_i; c3_w fln_w; c3_y* fil_y; fid_i = open(pas_c, O_WRONLY | O_CREAT, 0666); free(pas_c); u2_cz(pas); if ( fid_i < 0 ) { perror(pas_c); u2_cz(som); return u2_no; } fln_w = u2_met(3, som); fil_y = c3_malloc(fln_w); u2_cr_bytes(0, fln_w, fil_y, som); u2_cz(som); if ( fln_w != write(fid_i, fil_y, fln_w) ) { return u2_no; } close(fid_i); return u2_yes; } }
/* unix_save(): save a file. */ static void _unix_save(c3_c* pax_c, u2_atom oat) { c3_i fid_i = open(pax_c, O_WRONLY | O_CREAT | O_TRUNC, 0666); c3_w fln_w, rit_w; c3_y* oat_y; if ( fid_i < 0 ) { uL(fprintf(uH, "%s: %s\n", pax_c, strerror(errno))); u2_cm_bail(c3__fail); } fln_w = u2_met(3, oat); oat_y = malloc(fln_w); u2_cr_bytes(0, fln_w, oat_y, oat); u2z(oat); rit_w = write(fid_i, oat_y, fln_w); close(fid_i); free(oat_y); if ( rit_w != fln_w ) { uL(fprintf(uH, "%s: %s\n", pax_c, strerror(errno))); c3_assert(0); } }
/* u2_walk_save(): save file or bail. */ void u2_walk_save(c3_c* pas_c, u2_noun tim, u2_atom pad) { c3_i fid_i = open(pas_c, O_WRONLY | O_CREAT | O_TRUNC, 0666); c3_w fln_w, rit_w; c3_y* pad_y; if ( fid_i < 0 ) { uL(fprintf(uH, "%s: %s\n", pas_c, strerror(errno))); u2_cm_bail(c3__fail); } fln_w = u2_met(3, pad); pad_y = malloc(fln_w); u2_cr_bytes(0, fln_w, pad_y, pad); u2z(pad); rit_w = write(fid_i, pad_y, fln_w); close(fid_i); free(pad_y); if ( rit_w != fln_w ) { uL(fprintf(uH, "%s: %s\n", pas_c, strerror(errno))); u2_cm_bail(c3__fail); } if ( 0 != tim ) { struct timeval tim_tv[2]; u2_time_out_tv(&tim_tv[0], u2k(tim)); u2_time_out_tv(&tim_tv[1], tim); utimes(pas_c, tim_tv); } }
/* u2_cr_string(): `a` as malloced C string. */ c3_c* u2_cr_string(u2_atom a) { c3_w met_w = u2_cr_met(3, a); c3_c* str_c = c3_malloc(met_w + 1); u2_cr_bytes(0, met_w, (c3_y*)str_c, a); str_c[met_w] = 0; return str_c; }
u2_weak j2_mcd(Pt5, aesc, de)(u2_wire wir_r, u2_atom a, u2_atom b) { c3_y a_y[32]; c3_y b_y[16]; #if defined(U2_OS_osx) size_t siz_i = 0; #else AES_KEY key_u; #endif c3_assert(u2_cr_met(3, a) <= 32); c3_assert(u2_cr_met(3, b) <= 16); u2_cr_bytes(0, 32, a_y, a); u2_cr_bytes(0, 16, b_y, b); #if defined(U2_OS_osx) if ( kCCSuccess != CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionECBMode, a_y, kCCKeySizeAES256, 0, b_y, 16, b_y, 16, &siz_i) ) { return u2_bl_bail(wir_r, c3__exit); } else c3_assert(16 == siz_i); #else if ( 0 != AES_set_decrypt_key(a_y, 256, &key_u) ) { return u2_bl_bail(wir_r, c3__exit); } else { AES_decrypt(b_y, b_y, &key_u); } #endif return u2_ci_bytes(16, b_y); }
/* _term_it_path(): path for console file. */ static c3_c* _term_it_path(u2_bean fyl, u2_noun pax) { c3_w len_w; c3_c *pas_c; // measure // len_w = strlen(u2_Host.cpu_c); { u2_noun wiz = pax; while ( u2_nul != wiz ) { len_w += (1 + u2_cr_met(3, u2h(wiz))); wiz = u2t(wiz); } } // cut // pas_c = malloc(len_w + 1); strncpy(pas_c, u2_Host.cpu_c, len_w); pas_c[len_w] = '\0'; { u2_noun wiz = pax; c3_c* waq_c = (pas_c + strlen(pas_c)); while ( u2_nul != wiz ) { c3_w tis_w = u2_cr_met(3, u2h(wiz)); if ( (u2_yes == fyl) && (u2_nul == u2t(wiz)) ) { *waq_c++ = '.'; } else *waq_c++ = '/'; u2_cr_bytes(0, tis_w, (c3_y*)waq_c, u2h(wiz)); waq_c += tis_w; wiz = u2t(wiz); } *waq_c = 0; } u2z(pax); return pas_c; }
/* _http_octs_to_bod(): translate octet-stream noun into body. */ static u2_hbod* _http_octs_to_bod(u2_noun oct) { c3_w len_w; if ( !u2_fly_is_cat(u2h(oct)) ) { // 2GB max u2_cm_bail(c3__fail); return 0; } len_w = u2h(oct); { u2_hbod* bod_u = c3_malloc(len_w + sizeof(*bod_u)); bod_u->len_w = len_w; u2_cr_bytes(0, len_w, bod_u->hun_y, u2t(oct)); bod_u->nex_u = 0; u2z(oct); return bod_u; } }