Esempio n. 1
0
/* 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);
    }
  }
Esempio n. 3
0
File: unix.c Progetto: Gruelty/urbit
/* _unix_dump(): dump noun to file.
*/
static void
_unix_dump(FILE*   fil,
           u2_noun som)
{
  if ( u2_no == u2_dust(som) ) {
    mpz_t amp;

    if ( u2_yes == _unix_term(som) ) {
      c3_w met_w = u2_met(3, som);
      c3_y *buf_y = alloca(met_w + 1);

      u2_bytes(0, met_w, buf_y, som);
      buf_y[met_w] = 0;
      fprintf(fil, "%%%s", buf_y);
    }
    else {
      u2_mp(amp, som);
      gmp_fprintf(fil, "%Zd", amp);
      mpz_clear(amp);
    }
  }
  else {
    fputc('[', fil);
    _unix_dump(fil, u2_h(som));
    fprintf(fil, " ");
    _unix_dump_in(fil, u2_t(som));
    fputc(']', fil);
  }
}
Esempio n. 4
0
File: bail.c Progetto: MacTop/urbit
/* u2_bi_bytes():
**
**  Copy bytes (a_w) through (a_w + b_w - 1) from (d) to (c).
*/
void
u2_bi_bytes(u2_ray  wir_r,
            c3_w    a_w,
            c3_w    b_w,
            c3_y*   c_y,
            u2_noun d)
{
  if ( u2_no == u2_stud(d) ) u2_bl_bail(wir_r, c3__exit);

  u2_bytes(a_w, b_w, c_y, d);
}
Esempio n. 5
0
File: benx.c Progetto: Gruelty/urbit
/* _print_term(): print a terminal.
*/
static void
_print_term(u2_noun som,
            FILE*   fil_F)
{
  if ( u2_yes == u2_stud(som) ) {
    c3_w len_w = u2_met(3, som);
    c3_y *som_y = alloca(len_w) + 1;

    u2_bytes(0, len_w, som_y, som);
    som_y[len_w] = 0;
    fprintf(fil_F, "%s", (c3_c *)som_y);
  }
}
Esempio n. 6
0
File: unix.c Progetto: Gruelty/urbit
/* _unix_term(): u2_yes iff `tat` should be printed as a term.
*/
static u2_bean
_unix_term(u2_atom tat)
{
  c3_w met_w = u2_met(3, tat);

  if ( met_w >= 2 ) {
    c3_y *buf_y = alloca(met_w);
    c3_w i_w;

    u2_bytes(0, met_w, buf_y, tat);

    for ( i_w=0; i_w < met_w; i_w++ ) {
      if ( ((buf_y[i_w] < 'a') || (buf_y[i_w] > 'x')) && (buf_y[i_w] != '-') ) {
        return u2_no;
      }
    }
    return u2_yes;
  }
  else return u2_no; 
}
Esempio n. 7
0
File: unix.c Progetto: Gruelty/urbit
/* u2_ux_write(): write a path/extension as an atom.
*/
u2_bean
u2_ux_write(u2_wire     wir_r,
            u2_atom     som,
            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;
    c3_w    fln_w;
    c3_y*   fil_y;

    fid_i = open(nam_c, O_WRONLY | O_CREAT, 0666);
    if ( fid_i < 0 ) {
      return u2_no;
    }
    fln_w = u2_met(3, som);
    fil_y = malloc(fln_w);
    u2_bytes(0, fln_w, fil_y, som);

    if ( fln_w != write(fid_i, fil_y, fln_w) ) {
      return u2_no;
    }
    close(fid_i);

    return u2_yes;
  }
}