Пример #1
0
/* Tests that tape_save_p() truncates the tape properly */
static void
test_tape_save_p_truncate()
{
  char *filename = tmpnam(NULL);  
  FILE *fp;
  unsigned char mem[65536];
  int i;

  observer_status_init();
  tape_init(observer);

  /* Save 3 lots of header and data blocks */
  tape_attach(filename);
  for (i = 0; i < 3; i++) {
    generate_block(mem+(100*i), 25+i, 'A'+i);
    generate_block(mem+(100*i)+50, 30+i, 6+i);
    tape_save_p(mem+(100*i), 25+i);
    tape_save_p(mem+(100*i)+50, 30+i);
  }
  tape_detach();

  tape_attach(filename);

  /* Load first header and data blocks */
  for(i = 0; i < 10; i++) {
    mem[9986+i] = mem[100+i];
  }
  tape_load_p(mem, 1000);
  tape_load_p(mem, 1150);

  /* Save 1 set of header and data blocks after the first 2 blocks */
  generate_block(mem+2000, 28, 'N');
  generate_block(mem+2050, 40, 20);
  tape_save_p(mem+2000, 28);
  tape_save_p(mem+2050, 20);
  tape_detach();

  /* Check that tape only contains first header and data blocks followed by
   * the last saved header and data blocks */
  fp = fopen(filename, "rb");
  assert(fp != NULL);

  assert(block_correct(fp, mem, 25));
  assert(block_correct(fp, mem+50, 30));
  
  assert(block_correct(fp, mem+2000, 28));
  assert(block_correct(fp, mem+2050, 20));

  assert(fgetc(fp) == EOF);
  fclose(fp);
}
Пример #2
0
int main(int argc, char *argv[]) {
    pa_mempool *pool;
    pa_sample_spec a, b;
    pa_cvolume v;

    pa_log_set_level(PA_LOG_DEBUG);

    pa_assert_se(pool = pa_mempool_new(FALSE, 0));

    a.channels = b.channels = 1;
    a.rate = b.rate = 44100;

    v.channels = a.channels;
    v.values[0] = pa_sw_volume_from_linear(0.5);

    for (a.format = 0; a.format < PA_SAMPLE_MAX; a.format ++) {
        for (b.format = 0; b.format < PA_SAMPLE_MAX; b.format ++) {
            pa_resampler *forth, *back;
            pa_memchunk i, j, k;

            printf("=== %s -> %s -> %s -> /2\n",
                   pa_sample_format_to_string(a.format),
                   pa_sample_format_to_string(b.format),
                   pa_sample_format_to_string(a.format));

            pa_assert_se(forth = pa_resampler_new(pool, &a, NULL, &b, NULL, PA_RESAMPLER_AUTO, 0));
            pa_assert_se(back = pa_resampler_new(pool, &b, NULL, &a, NULL, PA_RESAMPLER_AUTO, 0));

            i.memblock = generate_block(pool, &a);
            i.length = pa_memblock_get_length(i.memblock);
            i.index = 0;
            pa_resampler_run(forth, &i, &j);
            pa_resampler_run(back, &j, &k);

            printf("before:  ");
            dump_block(&a, &i);
            printf("after :  ");
            dump_block(&b, &j);
            printf("reverse: ");
            dump_block(&a, &k);

            pa_memblock_unref(j.memblock);
            pa_memblock_unref(k.memblock);

            pa_volume_memchunk(&i, &a, &v);
            printf("volume:  ");
            dump_block(&a, &i);

            pa_memblock_unref(i.memblock);

            pa_resampler_free(forth);
            pa_resampler_free(back);
        }
    }

    pa_mempool_free(pool);

    return 0;
}
Пример #3
0
static void
test_tape_save_p()
{
  char *filename = tmpnam(NULL);
  FILE *fp;
  unsigned char mem[65536];

  /* Create header and data block */
  generate_block(mem, 25, 'A');
  generate_block(mem+50, 300, 6);

  observer_status_init();
  tape_init(observer);

  tape_attach(filename);

  /* Save the blocks */

  tape_save_p(mem, 25);
  assert(observer_status.observer_called == 1);
  assert(observer_status.tape_attached == 1);
  assert(observer_status.tape_pos == 28);
  assert(observer_status.message_type == TAPE_MESSAGE);
  assert(strcmp(observer_status.message, "Saving to file: BCDEFGHIJK") == 0);

  tape_save_p(mem+50, 300);
  assert(observer_status.observer_called == 1);
  assert(observer_status.tape_attached == 1);
  assert(observer_status.tape_pos == 331);
  assert(observer_status.message_type == TAPE_MESSAGE);
  assert(strcmp(observer_status.message, "Save complete.") == 0);

  tape_detach();

  /* Check the header and data blocks */
  fp = fopen(filename, "rb");
  assert(fp != NULL);
 
  assert(block_correct(fp, mem, 25));
  assert(block_correct(fp, mem+50, 300));
  assert(fgetc(fp) == EOF);
  fclose(fp);
}
Пример #4
0
int32_t broadcast_if_generator(struct consensus_model *model,struct crypto777_node *nn,uint32_t blocknum)
{
    uint64_t sortbuf[MAXPEERS+1][4]; uint8_t msg[8192];
    struct crypto777_generator gen;
    struct crypto777_block *prev,*block;
    char *blockstr = 0;
    uint32_t mytimestamp;
    int32_t i,n,len,lag;
    cJSON *json;
    if ( (prev= model->blocks[blocknum-1]) == 0 )
        return(-1);
    if ( (n= calc_generators(nn,sortbuf,model,blocknum)) > 0 )
    {
        mytimestamp = (uint32_t)time(NULL);
        calc_generator(&gen,calc_stake(nn,model),nn->email,mytimestamp,prev->gen.metric,prev->gen.hash,model->POW_DIFF);
        if ( (json= generators_json(model,nn,sortbuf,n,blocknum,gen.metric)) != 0 )
        {
            blockstr = cJSON_Print(json);
            free_json(json);
            _stripwhite(blockstr,' ');
            strcpy((char *)msg,blockstr);
            //printf("node.%d (%s)\n",nn->nodeid,blockstr);
            free(blockstr);
        }
        len = (int32_t)strlen((char *)msg) + 1;
        lag = (mytimestamp - prev->timestamp);
        for (i=0; i<lag&&i<n; i++)
        {
            if ( sortbuf[i][1] == nn->nodeid )
            {
                if ( 0 && nn->nodeid == 0 )
                    printf("msg%-4d: (%s).%d\n",nn->nodeid,(char *)msg,len);
                block = generate_block(model,nn,msg,len,blocknum,mytimestamp);
                crypto777_broadcast(nn,(uint8_t *)block,block->blocksize,model->packet_leverage,0);
                nn->lastblocktimestamp = mytimestamp;
                Generator++;
                return(1);
            }
        }
    }
    return(0);
}
Пример #5
0
 void wait_for_maintenance() {
   generate_blocks( db.get_dynamic_global_properties().next_maintenance_time );
   generate_block();
 }
Пример #6
0
 void wait_for_hf_core_216() {
   generate_blocks( HARDFORK_CORE_216_TIME );
   generate_block();
 }
Пример #7
0
 void expire_feed() {
   generate_blocks(db.head_block_time() + GRAPHENE_DEFAULT_PRICE_FEED_LIFETIME);
   generate_block();
   FC_ASSERT( swan().bitasset_data(db).current_feed.settlement_price.is_null() );
 }
Пример #8
0
int main(int argc, char *argv[]) {
    pa_mempool *pool = NULL;
    pa_sample_spec a, b;
    int ret = 1, c;
    bool all_formats = true;
    pa_resample_method_t method;
    int seconds;
    unsigned crossover_freq = 120;

    static const struct option long_options[] = {
        {"help",                  0, NULL, 'h'},
        {"verbose",               0, NULL, 'v'},
        {"version",               0, NULL, ARG_VERSION},
        {"from-rate",             1, NULL, ARG_FROM_SAMPLERATE},
        {"from-format",           1, NULL, ARG_FROM_SAMPLEFORMAT},
        {"from-channels",         1, NULL, ARG_FROM_CHANNELS},
        {"to-rate",               1, NULL, ARG_TO_SAMPLERATE},
        {"to-format",             1, NULL, ARG_TO_SAMPLEFORMAT},
        {"to-channels",           1, NULL, ARG_TO_CHANNELS},
        {"seconds",               1, NULL, ARG_SECONDS},
        {"resample-method",       1, NULL, ARG_RESAMPLE_METHOD},
        {"dump-resample-methods", 0, NULL, ARG_DUMP_RESAMPLE_METHODS},
        {NULL,                    0, NULL, 0}
    };

    setlocale(LC_ALL, "");
#ifdef ENABLE_NLS
    bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
#endif

    pa_log_set_level(PA_LOG_WARN);
    if (!getenv("MAKE_CHECK"))
        pa_log_set_level(PA_LOG_INFO);

    pa_assert_se(pool = pa_mempool_new(false, 0));

    a.channels = b.channels = 1;
    a.rate = b.rate = 44100;
    a.format = b.format = PA_SAMPLE_S16LE;

    method = PA_RESAMPLER_AUTO;
    seconds = 60;

    while ((c = getopt_long(argc, argv, "hv", long_options, NULL)) != -1) {

        switch (c) {
            case 'h' :
                help(argv[0]);
                ret = 0;
                goto quit;

            case 'v':
                pa_log_set_level(PA_LOG_DEBUG);
                break;

            case ARG_VERSION:
                printf(_("%s %s\n"), argv[0], PACKAGE_VERSION);
                ret = 0;
                goto quit;

            case ARG_DUMP_RESAMPLE_METHODS:
                dump_resample_methods();
                ret = 0;
                goto quit;

            case ARG_FROM_CHANNELS:
                a.channels = (uint8_t) atoi(optarg);
                break;

            case ARG_FROM_SAMPLEFORMAT:
                a.format = pa_parse_sample_format(optarg);
                all_formats = false;
                break;

            case ARG_FROM_SAMPLERATE:
                a.rate = (uint32_t) atoi(optarg);
                break;

            case ARG_TO_CHANNELS:
                b.channels = (uint8_t) atoi(optarg);
                break;

            case ARG_TO_SAMPLEFORMAT:
                b.format = pa_parse_sample_format(optarg);
                all_formats = false;
                break;

            case ARG_TO_SAMPLERATE:
                b.rate = (uint32_t) atoi(optarg);
                break;

            case ARG_SECONDS:
                seconds = atoi(optarg);
                break;

            case ARG_RESAMPLE_METHOD:
                if (*optarg == '\0' || pa_streq(optarg, "help")) {
                    dump_resample_methods();
                    ret = 0;
                    goto quit;
                }
                method = pa_parse_resample_method(optarg);
                break;

            default:
                goto quit;
        }
    }

    ret = 0;
    pa_assert_se(pool = pa_mempool_new(false, 0));

    if (!all_formats) {

        pa_resampler *resampler;
        pa_memchunk i, j;
        pa_usec_t ts;

        pa_log_debug("Compilation CFLAGS: %s", PA_CFLAGS);
        pa_log_debug("=== %d seconds: %d Hz %d ch (%s) -> %d Hz %d ch (%s)", seconds,
                   a.rate, a.channels, pa_sample_format_to_string(a.format),
                   b.rate, b.channels, pa_sample_format_to_string(b.format));

        ts = pa_rtclock_now();
        pa_assert_se(resampler = pa_resampler_new(pool, &a, NULL, &b, NULL, crossover_freq, method, 0));
        pa_log_info("init: %llu", (long long unsigned)(pa_rtclock_now() - ts));

        i.memblock = pa_memblock_new(pool, pa_usec_to_bytes(1*PA_USEC_PER_SEC, &a));

        ts = pa_rtclock_now();
        i.length = pa_memblock_get_length(i.memblock);
        i.index = 0;
        while (seconds--) {
            pa_resampler_run(resampler, &i, &j);
            if (j.memblock)
                pa_memblock_unref(j.memblock);
        }
        pa_log_info("resampling: %llu", (long long unsigned)(pa_rtclock_now() - ts));
        pa_memblock_unref(i.memblock);

        pa_resampler_free(resampler);

        goto quit;
    }

    for (a.format = 0; a.format < PA_SAMPLE_MAX; a.format ++) {
        for (b.format = 0; b.format < PA_SAMPLE_MAX; b.format ++) {
            pa_resampler *forth, *back;
            pa_memchunk i, j, k;

            pa_log_debug("=== %s -> %s -> %s -> /2",
                       pa_sample_format_to_string(a.format),
                       pa_sample_format_to_string(b.format),
                       pa_sample_format_to_string(a.format));

            pa_assert_se(forth = pa_resampler_new(pool, &a, NULL, &b, NULL, crossover_freq, method, 0));
            pa_assert_se(back = pa_resampler_new(pool, &b, NULL, &a, NULL, crossover_freq, method, 0));

            i.memblock = generate_block(pool, &a);
            i.length = pa_memblock_get_length(i.memblock);
            i.index = 0;
            pa_resampler_run(forth, &i, &j);
            pa_resampler_run(back, &j, &k);

            dump_block("before", &a, &i);
            dump_block("after", &b, &j);
            dump_block("reverse", &a, &k);

            pa_memblock_unref(i.memblock);
            pa_memblock_unref(j.memblock);
            pa_memblock_unref(k.memblock);

            pa_resampler_free(forth);
            pa_resampler_free(back);
        }
    }

 quit:
    if (pool)
        pa_mempool_free(pool);

    return ret;
}
Пример #9
0
static code_block *generate_do_while(code_block *parent, loop_statement *loop) {
  size_t body_block = parent->system->block_count;
  code_block *body = fork_block(parent), *context = body;

  if (loop->body) {
    struct generate_loop_trigger_data data = {
      .loop = loop,
      .done = false,
      .prev_block = body,
      .context_block = &context
    };
    body = generate_block(body, loop->body, loop_trigger, &data);
  }

  // if the body ends, it implicitly continues
  if (body) {
    block_node *node = xmalloc(sizeof(*node));
    node->block = body;
    node->next = loop->continue_node;
    loop->continue_node = node;
  }

  // if we ever continue the loop, we'll want to include the available variables
  // these variables:
  // - need to be in the outermost scope in the loop
  // - need to be available from all continue statements (including the final
  //   implicit continue)
  if (loop->continue_node) {
    code_block *condition = tangle_blocks(context, loop->continue_node);

    condition = generate_expression(condition, loop->condition);

    code_block *body_replay, *post;
    weave_blocks(parent, loop->break_node, condition, &body_replay, &post);

    join_blocks(body_replay, body_block);

    return post;
  }

  // loop only breaks, never continues (effectively not a loop)
  if (loop->break_node) {
    fprintf(stderr, "do-while condition not reachable\n");
    return tangle_blocks(parent, loop->break_node);
  }

  fprintf(stderr, "infinite loop, do-while condition not reachable\n");
  return NULL;
}

static code_block *generate_while(code_block *parent, loop_statement *loop) {
  if (loop->condition->operation.type == O_LITERAL &&
      loop->condition->type->type == T_BOOL && loop->condition->value_bool) {
    loop->type = S_DO_WHILE;
    return generate_do_while(parent, loop);
  }

  size_t condition_block = parent->system->block_count;
  code_block *condition = fork_block(parent);

  condition = generate_expression(condition, loop->condition);

  code_block *body, *post;
  branch_into(condition, &body, &post);

  // TODO: this is using undocumented behavior
  size_t /*body_block = parent->system->block_count - 2,
    */post_block = parent->system->block_count - 1;

  loop->condition_block = condition_block;
  loop->post_block = post_block;

  if (loop->body) {
    body = generate_block(body, loop->body, NULL, NULL);
  }

  if (body) {
    join_blocks(body, condition_block);
  }

  return post;
}

static code_block *generate_loop(code_block *parent, loop_statement *loop) {
  return loop->type == S_DO_WHILE
    ? generate_do_while(parent, loop)
    : generate_while(parent, loop);
}

static code_block *generate_if(code_block *parent, if_statement *branch) {
  parent = generate_expression(parent, branch->condition);

  code_block *first, *second;
  branch_into(parent, &first, &second);

  first = branch->first ? generate_block(first, branch->first, NULL, NULL)
    : first;
  second = branch->second ? generate_block(second, branch->second, NULL, NULL)
    : second;

  switch (((!first) << 1) | (!second)) {
  case 0: // both exist
    return merge_blocks(parent, first, second);
  case 1: // the first block exists
    return rejoin_block(parent, first);
  case 2: // the second block exists
    return rejoin_block(parent, second);
  case 3:
    return NULL;
  }

  fprintf(stderr, "if statement generation logical failure\n");
  abort();
}
Пример #10
0
static code_block *generate_block(code_block *parent, block_statement *block,
    block_iter iter, void *iter_data) {
  for (symbol_entry *c = block->class_head; c; c = c->next) {
    generate_class_stub(parent->system, c->classtype);
  }

  for (symbol_entry *f = block->function_head; f; f = f->next) {
    generate_function_stub(parent->system, f->function);
  }

  for (symbol_entry *c = block->class_head; c; c = c->next) {
    generate_class(parent->system, c->classtype);
  }

  for (symbol_entry *f = block->function_head; f; f = f->next) {
    generate_function(parent->system, f->function);
  }

  for (statement *node = block->body; node; node = node->next) {
    switch (node->type) {
    case S_BLOCK: {
      code_block *body = fork_block(parent);
      body = generate_block(body, (block_statement*) node, NULL, NULL);
      parent = rejoin_block(parent, body);
    } break;
    case S_BREAK:
    case S_CONTINUE:
      generate_control(parent, (control_statement*) node);
      parent = NULL;
      break;
    case S_DEFINE:
      parent = generate_define(parent, (define_statement*) node);
      break;
    case S_LET:
      parent = generate_let(parent, (let_statement*) node);
      break;
    case S_DO_WHILE:
    case S_WHILE:
      parent = generate_loop(parent, (loop_statement*) node);
      break;
    case S_EXPRESSION: {
      expression_statement *express = (expression_statement*) node;
      parent = generate_expression(parent, express->value);
    } break;
    case S_IF:
      parent = generate_if(parent, (if_statement*) node);
      break;
    case S_RETURN:
      generate_return(parent, (return_statement*) node);
      parent = NULL;
      break;
    // already processed
    case S_CLASS:
    case S_FUNCTION:
    case S_TYPEDEF:
      break;
    }

    // most recently processed statement terminates abruptly
    if (parent == NULL) {
      if (node->next) {
        fprintf(stderr, "not reachable\n");
      }

      return NULL;
    }

    if (iter != NULL) {
      iter(parent, iter_data);
    }
  }

  return parent;
}
Пример #11
0
static void generate_component(component comp, fncode fn)
{
  clist args;

  set_lineno(comp->lineno, fn);

  switch (comp->vclass)
    {
    case c_assign:
      {
	ulong offset;
        bool is_static;
	variable_class vclass = env_lookup(comp->u.assign.symbol, &offset,
                                           false, true, &is_static);
	component val = comp->u.assign.value;

	if (val->vclass == c_closure)
	  {
	    /* Defining a function, give it a name */
	    if (vclass == global_var)
	      val->u.closure->varname = comp->u.assign.symbol;
	    else
	      {
		char *varname = allocate(fnmemory(fn), strlen(comp->u.assign.symbol) + 7);

		sprintf(varname, "local-%s", comp->u.assign.symbol);
		val->u.closure->varname = varname;
	      }
	  }

        if (is_static)
          {
            ins1(op_recall + vclass, offset, fn);
            generate_component(comp->u.assign.value, fn);
            mexecute(g_symbol_set, NULL, 2, fn);
            break;
          }

	generate_component(comp->u.assign.value, fn);

	set_lineno(comp->lineno, fn);

        if (vclass == global_var)
	  massign(offset, comp->u.assign.symbol, fn);
	else
	  ins1(op_assign + vclass, offset, fn);
	/* Note: varname becomes a dangling pointer when fnmemory(fn) is
	   deallocated, but it is never used again so this does not cause
	   a problem. */
	break;
      }
    case c_vref:
    case c_recall:
      {
        bool is_vref = comp->vclass == c_vref;
	ulong offset;
        bool is_static;
	variable_class vclass = env_lookup(comp->u.recall, &offset,
                                           true, is_vref, &is_static);

        if (is_static)
          {
            assert(vclass != global_var);
            ins1(op_recall + vclass, offset, fn);
            ulong gidx = is_vref ? g_make_symbol_ref : g_symbol_get;
            mexecute(gidx, NULL, 1, fn);
            break;
          }
	if (vclass != global_var)
          ins1((is_vref ? op_vref : op_recall) + vclass, offset, fn);
        else if (is_vref)
          {
            if (!mwritable(offset, comp->u.recall))
              return;
            ins_constant(makeint(offset), fn);
          }
        else
          mrecall(offset, comp->u.recall, fn);
        if (is_vref)
          mexecute(g_make_variable_ref, "make_variable_ref", 1, fn);
	break;
      }
    case c_constant:
      ins_constant(make_constant(comp->u.cst), fn);
      break;
    case c_closure:
      {
	uword idx;

	idx = add_constant(generate_function(comp->u.closure, false, fn), fn);
	if (idx < ARG1_MAX) ins1(op_closure_code1, idx, fn);
	else ins2(op_closure_code2, idx, fn);
	break;
      }
    case c_block:
      generate_block(comp->u.blk, fn);
      break;
    case c_labeled:
      start_block(comp->u.labeled.name, fn);
      generate_component(comp->u.labeled.expression, fn);
      end_block(fn);
      break;
    case c_exit:
      generate_component(comp->u.labeled.expression, fn);
      if (!exit_block(comp->u.labeled.name, fn)) {
	if (!comp->u.labeled.name)
	  log_error("no loop to exit from");
	else
	  log_error("no block labeled %s", comp->u.labeled.name);
      }
      break;
    case c_execute:
      {
	uword count;

	generate_args(comp->u.execute->next, fn, &count);
	set_lineno(comp->lineno, fn);
	generate_execute(comp->u.execute->c, count, fn);
	break;
      }
    case c_builtin:
      args = comp->u.builtin.args;

      switch (comp->u.builtin.fn)
	{
	case b_if: {
          block cb = new_codeblock(fnmemory(fn), NULL,
                                   new_clist(fnmemory(fn), args->next->c,
                                             new_clist(fnmemory(fn),
                                                       component_undefined,
                                                       NULL)),
                                   NULL, NULL, -1);
	  generate_if(args->c, new_component(fnmemory(fn),
                                             args->next->c->lineno,
                                             c_block, cb),
		      component_undefined, fn);
	  break;
        }
	case b_ifelse:
	  generate_if(args->c, args->next->c, args->next->next->c, fn);
	  break;
	case b_sc_and: case b_sc_or:
	  generate_if(comp, component_true, component_false, fn);
	  break;

	case b_while:
	  generate_while(args->c, args->next->c, fn);
	  break;

	case b_loop:
	  {
	    label loop = new_label(fn);

            env_start_loop();
	    set_label(loop, fn);
	    start_block(NULL, fn);
	    generate_component(args->c, fn);
	    branch(op_loop1, loop, fn);
	    end_block(fn);
            env_end_loop();
	    adjust_depth(1, fn);
	    break;
	  }

	case b_add: case b_subtract:
	case b_ref: case b_set:
	case b_bitor: case b_bitand:
	case b_not:
	case b_eq: case b_ne:
	case b_lt: case b_le: case b_ge: case b_gt:
	  {
	    uword count;

	    assert(comp->u.builtin.fn < last_builtin);
	    generate_args(args, fn, &count);
	    set_lineno(comp->lineno, fn);
	    ins0(builtin_ops[comp->u.builtin.fn], fn);
	    break;
	  }
	default:
	  {
	    uword count;

	    assert(comp->u.builtin.fn < last_builtin);
	    generate_args(args, fn, &count);
	    set_lineno(comp->lineno, fn);
	    mexecute(builtin_functions[comp->u.builtin.fn], NULL, count, fn);
	    break;
	  }
	}
      break;
    default: abort();
    }
}
Пример #12
0
void generate_component(component comp, const char *mlabel, bool discard, fncode fn)
{
  clist args;

  switch (comp->vclass)
    {
    case c_assign:
      {
	u16 offset;
	mtype t;
	variable_class vclass = env_lookup(comp->l, comp->u.assign.symbol, &offset, &t, FALSE);
	component val = comp->u.assign.value;

	if (val->vclass == c_closure)
	  {
	    /* Defining a function, give it a name */
	    if (vclass == global_var)
	      val->u.closure->varname = comp->u.assign.symbol;
	    else
	      {
		char *varname = allocate(fnmemory(fn), strlen(comp->u.assign.symbol) + 7);

		sprintf(varname, "local-%s", comp->u.assign.symbol);
		val->u.closure->varname = varname;
	      }
	  }
	generate_component(comp->u.assign.value, NULL, FALSE, fn);
	if (t != stype_any)
	  ins0(OPmscheck4 + t, fn);
	if (vclass == global_var)
	  massign(comp->l, offset, comp->u.assign.symbol, fn);
	else if (vclass == closure_var)
	  ins1(OPmwritec, offset, fn);
	else
	  ins1(OPmwritel, offset, fn);
	/* Note: varname becomes a dangling pointer when fnmemory(fn) is
	   deallocated, but it is never used again so this does not cause
	   a problem. */
	break;
      }
    case c_recall:
      scompile_recall(comp->l, comp->u.recall, fn);
      break;
    case c_constant:
      ins_constant(make_constant(comp->u.cst, FALSE, fn), fn);
      break;
    case c_scheme:
      scheme_compile_mgc(comp->l, make_constant(comp->u.cst, TRUE, fn), discard, fn);
      discard = FALSE;
      break;
    case c_closure:
      generate_function(comp->u.closure, fn);
      break;
    case c_block:
      generate_block(comp->u.blk, discard, fn);
      discard = FALSE;
      break;
    case c_decl: 
      {
	vlist decl, next;

	/* declare variables one at a time (any x = y, y = 2; is an error) */
	for (decl = comp->u.decls; decl; decl = next)
	  {
	    next = decl->next;
	    decl->next = NULL;

	    env_declare(decl);
	    generate_decls(decl, fn);
	  }
	generate_component(component_undefined, NULL, FALSE, fn);
	break;
      }
    case c_labeled: {
      start_block(comp->u.labeled.name, FALSE, discard, fn);
      generate_component(comp->u.labeled.expression, comp->u.labeled.name, discard, fn);
      end_block(fn);
      discard = FALSE;
      break;
    }
    case c_exit:
      {
	bool discard_exit;
	label exitlab = exit_block(comp->u.labeled.name, FALSE, &discard_exit, fn);

	if (comp->u.labeled.expression != component_undefined && discard_exit)
	  warning(comp->l, "break result is ignored");
	generate_component(comp->u.labeled.expression, NULL, discard_exit, fn);
	if (exitlab)
	  branch(OPmba3, exitlab, fn);
	else 
	  {
	    if (!comp->u.labeled.name)
	      log_error(comp->l, "No loop to exit from");
	    else
	      log_error(comp->l, "No block labeled %s", comp->u.labeled.name);
	  }
	/* Callers expect generate_component to increase stack depth by 1  */
	if (discard_exit)
	  adjust_depth(1, fn);
	break;
      }
    case c_continue:
      {
	bool discard_exit; /* Meaningless for continue blocks */
	label exitlab = exit_block(comp->u.labeled.name, TRUE, &discard_exit, fn);

	if (exitlab)
	  branch(OPmba3, exitlab, fn);
	else 
	  {
	    if (comp->u.labeled.name[0] == '<')
	      log_error(comp->l, "No loop to continue");
	    else
	      log_error(comp->l, "No loop labeled %s", comp->u.labeled.name);
	  }
	/* Callers expect generate_component to increase stack depth by 1 (*/
	adjust_depth(1, fn);
	break;
      }
    case c_execute:
      {
	u16 count;

	generate_args(comp->u.execute->next, fn, &count);
	generate_execute(comp->u.execute->c, count, fn);
	break;
      }
    case c_builtin:
      args = comp->u.builtin.args;

      switch (comp->u.builtin.fn)
	{
	case b_if:
	  generate_if(args->c, args->next->c, NULL, TRUE, fn);
	  generate_component(component_undefined, NULL, FALSE, fn);
	  break;
	case b_ifelse:
	  generate_if(args->c, args->next->c, args->next->next->c, discard, fn);
	  discard = FALSE;
	  break;
	case b_sc_and: case b_sc_or:
	  generate_if(comp, component_true, component_false, discard, fn);
	  discard = FALSE;
	  break;

	case b_while:
	  enter_loop(fn);
	  generate_while(args->c, args->next->c, mlabel, discard, fn);
	  exit_loop(fn);
	  discard = FALSE;
	  break;

	case b_dowhile:
	  enter_loop(fn);
	  generate_dowhile(args->c, args->next->c, mlabel, discard, fn);
	  exit_loop(fn);
	  discard = FALSE;
	  break;

	case b_for:
	  enter_loop(fn);
	  generate_for(args->c, args->next->c, args->next->next->c,
		       args->next->next->next->c, mlabel, discard, fn);
	  exit_loop(fn);
	  discard = FALSE;
	  break;

	default:
	  {
	    u16 count;

	    assert(comp->u.builtin.fn < last_builtin);
	    generate_args(args, fn, &count);
	    ins0(builtin_ops[comp->u.builtin.fn], fn);
	    break;
	  }
	case b_cons:
	  {
	    u16 count;
	    u16 goffset;
	    mtype t;

	    assert(comp->u.builtin.fn < last_builtin);
	    generate_args(args, fn, &count);
	    goffset = global_lookup(fnglobals(fn),
				    builtin_functions[comp->u.builtin.fn], &t);
	    mexecute(comp->l, goffset, NULL, count, fn);
	    break;
	  }
	}
      break;
    default: assert(0);
    }
  if (discard)
    ins0(OPmpop, fn);
}
Пример #13
0
static int
generate(struct ccdrbg_state *rng,
         unsigned long dataOutLength, void *dataOut,
         unsigned long additionalLength, const void *additional)
{
    int rc = CCDRBG_STATUS_OK;
    unsigned long i;
    unsigned long len;
    uint8_t		*p;
    uint32_t	*temp;
    const char	*input_string[1];
    uint32_t	length[1];
    struct ccdrbg_nistctr_state *drbg = (struct ccdrbg_nistctr_state *)rng;
    uint32_t	buffer[CCADRBG_OUTLEN(drbg)];
    uint32_t	additional_buffer[CCADRBG_SEEDLEN_INTS(drbg)];
    unsigned long blocks = dataOutLength / CCADRBG_OUTLEN(drbg);

    /* [1] If reseed_counter > reseed_interval ... */
    rc = validate_gen_params(drbg, dataOutLength, (additional !=NULL)?additionalLength:0); cc_require(rc==CCDRBG_STATUS_OK, errOut);

    /* [2] If (addional_input != Null), then */
    if (additional && additionalLength)
    {
        if(drbg->use_df) {
            input_string[0] = additional;
            /* typecast: guaranteed to fit by the checks above */
            length[0] = (uint32_t)additionalLength;
            /* [2.1] additional = Block_Cipher_df(additional, seedlen) */
            rc = df(drbg, input_string, length, 1,
                    (uint8_t *)additional_buffer, sizeof(additional_buffer));
            cc_require(rc==CCDRBG_STATUS_OK, errOut);
        } else {
            cc_clear(sizeof(additional_buffer), additional_buffer);
            cc_assert(additionalLength==0 || additionalLength==sizeof(additional_buffer)); //additionalLength is validated above
            CC_MEMCPY(additional_buffer, additional, additionalLength);
        }

        /* [2.2] (Key, V) = Update(additional, Key, V) */
        rc=drbg_update(drbg, additional_buffer); cc_require(rc==CCDRBG_STATUS_OK, errOut);
    }

    if (blocks && check_int_alignment(dataOut))
    {
        /* [3] temp = Null */
        temp = (uint32_t *)dataOut;
        for (i = 0; i < blocks; ++i)
        {
            // Here is the conundrum that is FIPS.
            // In order to test the DRBG for CAVS one must NOT set strictFIPS
            // and thus not have a compliant DRBG.  That is the only way to
            // ensure that the CAVS test will pass.  On the other hand the
            // 'normal' usage of the DRBG MUST set the strictFIPS flag.  So
            // that which is used by our customers is NOT what was tested.
            rc = generate_block(drbg, temp); cc_require(rc==CCDRBG_STATUS_OK, errOut);
            temp += CCADRBG_OUTLEN_INTS(drbg);
            dataOutLength -= CCADRBG_OUTLEN(drbg);
        }

        dataOut = (uint8_t *)temp;
    }

    /* [3] temp = Null */
    temp = buffer;

    len = CCADRBG_OUTLEN(drbg);

    /* [4] While (len(temp) < requested_number_of_bits) do: */
    p = dataOut;
    while (dataOutLength > 0)
    {
        // See note above.
        rc = generate_block(drbg, temp); cc_require(rc==CCDRBG_STATUS_OK, errOut);
        if (dataOutLength < CCADRBG_OUTLEN(drbg))
            len = dataOutLength;

        memcpy(p, temp, len);

        p += len;
        dataOutLength -= len;
    }

    /* [6] (Key, V) = Update(additional, Key, V) */
    rc = drbg_update(drbg, additional && additionalLength ? &additional_buffer[0] :
                     &drbg->nullInput[0]);
    cc_require(rc==CCDRBG_STATUS_OK, errOut);
    
    /* [7] reseed_counter = reseed_counter + 1 */
    ++drbg->reseed_counter;
    
errOut:
    cc_clear(sizeof(additional_buffer),additional_buffer);
    return rc;
}