Ejemplo n.º 1
0
Archivo: txdb.c Proyecto: haraldh/bitc
static int
txdb_add_to_hashtable(struct txdb      *txdb,
                      const void       *buf,
                      size_t            len,
                      const uint256    *txHash,
                      const uint256    *blkHash,
                      uint64            timestamp,
                      struct tx_entry **txePtr)
{
    struct tx_entry *txe;
    struct buff b;
    int res;
    bool s;

    buff_init(&b, (char *)buf, len);

    txe = safe_malloc(sizeof *txe);
    if (blkHash) {
        memcpy(&txe->blkHash, blkHash, sizeof *blkHash);
    } else {
        memset(&txe->blkHash, 0, sizeof txe->blkHash);
    }
    txe->relevant = 0; /* for now */
    txe->timestamp = timestamp; // only really useful for 'relevant' ones.

    res = deserialize_tx(&b, &txe->tx);
    ASSERT(res == 0);

    s = hashtable_insert(txdb->hash_tx, txHash, sizeof *txHash, txe);
    ASSERT(s);

    *txePtr = txe;

    return 0;
}
Ejemplo n.º 2
0
int main(void)
{
	/* NOTE! 这些初始化函数的位置别随便改动! */
	trap_init();	/* 在设置其他中断之前 */
	console_init();	/* 需打印的在这之后 */
	keyboard_init();
	hd_init();		/* 需读磁盘的放其后 */
/*	graph_init(); */
	mem_init();
	sched_init();
	buff_init();	/* 在内存初始化之后 */
	inode_init();
	file_table_init();
	debug_init();
	sti();
	super_init(0);	/* 开中断之后 */
	files_init();

/*	unsigned short color = rgb_to_565color(255,255,255);
	draw_rect(0, 0, 800, 600, color, 1); */

	move_to_user_mode();

	/*
	 * 进程0马上execve(),替换掉用户态空间,这样进程0就
	 * 可以写时复制了。注意此时原来的用户态堆栈也被丢弃
	 * 了,换成了新堆栈。更多参见内存管理。
	 */
	if(!execve("/init"))
		printf("main: execve init-process failed.\n");
Ejemplo n.º 3
0
int script_parse_pubkey_hash(const uint8 *scriptPubKey, size_t scriptLength,
                             uint160 *pubkey) {
  enum script_txout_type type;
  struct buff buf;
  size_t datalen;
  uint8 *data;
  int res;

  uint160_zero_out(pubkey);
  buff_init(&buf, (uint8 *)scriptPubKey, scriptLength);

  res = script_match_type(&buf, &type, &data, &datalen);
  if (res) {
    NOT_TESTED();
    return 1;
  }

  switch (type) {
    case TX_PUBKEY:
      Warning(LGPFX " script TX_PUBKEY\n");
      NOT_TESTED();
      hash160_calc(data, datalen, pubkey);
      break;
    case TX_PUBKEYHASH:
      ASSERT(datalen == sizeof(uint160));
      memcpy(pubkey, data, sizeof(uint160));
      break;
    default:
      NOT_TESTED();
      res = 1;
      break;
  }
  free(data);
  return res;
}
Ejemplo n.º 4
0
void* mr_map(void* id_arg)
{
    struct wc_cache;
    uint32_t tid;
    off_t size;
    char* buff;
    char last;
    int ret;

    last = 0;
    ret = -1;
    size = 0;
    tid = ((struct thread_info_s*)id_arg)->thread_num;

    if((ret = buff_init(tid)))
        goto _END;

    while(!(ret = buff_read(tid, &buff, &size, &last)))
    {
        /* Nothing to do */
        if(!size)
            break;

        if(buff_hdl(tid, buff, size, last))
            goto _END;

        /* If this was the last buffer */
        if(last)
            break;
    }

    if(buff_destroy())
        goto _END;

    /* wait for other worker before merging */
    if(pthread_barrier_wait(&barrier) > 0)
    {
        perror("bar wait");
        goto _END;
    }

    /* merge results (done in parrallel) */
    ret = wcc_merge_results(tid, nb_thread);

    //wcc_print(tid);
_END:
    return ((void*) (long) ret);
}
Ejemplo n.º 5
0
logger_t* logger_init(char *filename) {
    created_logger = (logger_t*)calloc(1, sizeof(logger_t));
    if(created_logger == NULL) {
        perror("Initialization of logger failed!\n");
        return NULL;
    }
    if(filename == NULL) {
        filename = LOG_FILE_BY_DEFAULT;
    }
    created_logger -> file_pointer = fopen(filename, "w");
    if((created_logger -> file_pointer) == NULL) {
        perror("File initialiation failed!\n");
        free(created_logger);
        return NULL;
    }
    if(buff_init() == -2) {
        perror("Initialization of logger failed!\n");
        return NULL;
    }
    created_logger -> type_of_log_by_default = Debug;
    return created_logger;
}
Ejemplo n.º 6
0
Archivo: txdb.c Proyecto: haraldh/bitc
static struct tx_ser_data *
txdb_deserialize_tx_data(const void *val,
                         size_t vlen)
{
    struct tx_ser_data *tx;
    struct buff buf;

    ASSERT(val);

    buff_init(&buf, (void *)val, vlen);

    tx = safe_malloc(sizeof *tx);

    deserialize_uint256(&buf, &tx->blkHash);
    deserialize_uint64(&buf,  &tx->timestamp);
    deserialize_varint(&buf,  &tx->len);

    tx->buf = safe_calloc(1, tx->len + 1);
    deserialize_bytes(&buf, tx->buf, tx->len);

    ASSERT(buff_space_left(&buf) == 0);

    return tx;
}
Ejemplo n.º 7
0
buff_t *buff_new(byte *buff, uint used, uint size)
{
    buff_t *self = xmalloc(sizeof(buff_t));
    buff_init(self, buff, used, size);
    return self;
}
Ejemplo n.º 8
0
void	buff_free(Buffer *buf)
{
	if(buf->data)
		mdvi_free(buf->data);
	buff_init(buf);	
}
Ejemplo n.º 9
0
/* Note: the given strings are modified in place */
static char *parse_epsf_special(EpsfBox *box, char **ret, 
    char *prefix, char *arg)
{
    static struct {
        char *name;
        int  has_arg;
        char *value;
    } keys[] = {
        {"llx", 1, "0"},
        {"lly", 1, "0"},
        {"urx", 1, "0"},
        {"ury", 1, "0"},
        {"rwi", 1, "0"},
        {"rhi", 1, "0"},
        {"hoffset", 1, "0"},
        {"voffset", 1, "0"},
        {"hsize", 1, "612"},
        {"vsize", 1, "792"},
        {"hscale", 1, "100"},
        {"vscale", 1, "100"},
        {"angle", 1, "0"},
        {"clip", 0, "0"}
    };
#define NKEYS    (sizeof(keys) / sizeof(keys[0]))
    char    *ptr;
    char    *filename;
    double    value[NKEYS];
    Uchar    present[NKEYS];
    Buffer    buffer;
    char    *name;
    int    i;
    double    originx;
    double    originy;
    double    hsize;
    double    vsize;
    double    hscale;
    double    vscale;
        
    /* this special has the form
     *   ["]file.ps["] [key=valye]*
     */
    
    /* scan the filename */
    while(*arg == ' ' || *arg == '\t')
        arg++;

    /* make a copy of the string */
    ptr = arg;

    if(*ptr == '"')
        for(name = ++ptr; *ptr && *ptr != '"'; ptr++);
    else
        for(name = ptr; *ptr && *ptr != ' ' && *ptr != '\t'; ptr++);
    if(ptr == name)
        return NULL;
    *ptr++ = 0;
    filename = name;

    /* reset values to defaults */
    for(i = 0; i < NKEYS; i++) {
        value[i] = atof(keys[i].value);
        present[i] = 0;
    }
    
    buff_init(&buffer);
    buff_add(&buffer, "@beginspecial ", 0);

    while(*ptr) {
        const char *keyname;
        char    *val;
        char    *p;

        while(*ptr == ' ' || *ptr == '\t')
            ptr++;
        keyname = ptr;
        
        /* get the whole key=value pair */
        for(; *ptr && *ptr != ' ' && *ptr != '\t'; ptr++);
        
        if(*ptr) *ptr++ = 0;
        /* now we shouldn't touch `ptr' anymore */
        
        /* now work on this pair */
        p = strchr(keyname, '=');
        if(p == NULL)
            val = NULL;
        else {
            *p++ = 0;
            if(*p == '"') {
                val = ++p;
                /* skip until closing quote */
                while(*p && *p != '"')
                    p++;
                if(*p != '"')
                    mdvi_warning(
                    _("%s: malformed value for key `%s'\n"),
                        filename, keyname);
            } else
                val = p;
        }

        /* lookup the key */
        for(i = 0; i < NKEYS; i++)
            if(STRCEQ(keys[i].name, keyname))
                break;
        if(i == NKEYS) {
            mdvi_warning(_("%s: unknown key `%s' ignored\n"),
                     filename, keyname);
            continue;
        }
        if(keys[i].has_arg && val == NULL) {
            mdvi_warning(_("%s: no argument for key `%s', using defaults\n"),
                     filename, keyname);
            val = keys[i].value;
        } else if(!keys[i].has_arg && val) {
            mdvi_warning(_("%s: argument `%s' ignored for key `%s'\n"),
                     filename, val, keyname);
            val = NULL;
        }
        if(val)
            value[i] = atof(val);
        
        /* put the argument */
        buff_add(&buffer, val, 0);
        buff_add(&buffer, " @", 2);
        buff_add(&buffer, keyname, 0);
        buff_add(&buffer, " ", 1);
        
        /* remember that this option was given */
        present[i] = 0xff;
    }
    buff_add(&buffer, " @setspecial", 0);
    
    /* now compute the bounding box (code comes from dvips) */
    originx = 0;
    originy = 0;
    hscale = 1;
    vscale = 1;
    hsize = 0;
    vsize = 0;
    
    if(present[HSIZE])
        hsize = value[HSIZE];
    if(present[VSIZE])
        vsize = value[VSIZE];
    if(present[HOFF])
        originx = value[HOFF];
    if(present[VOFF])
        originy = value[VOFF];
    if(present[HSCALE])
        hscale = value[HSCALE] / 100.0;
    if(present[VSCALE])
        vscale = value[VSCALE] / 100.0;
    if(present[URX] && present[LLX])
        hsize = value[URX] - value[LLX];
    if(present[URY] && present[LLY])
        vsize = value[URY] - value[LLY];
    if(present[RWI] || present[RHI]) {
        if(present[RWI] && !present[RHI])
            hscale = vscale = value[RWI] / (10.0 * hsize);
        else if(present[RHI] && !present[RWI])
            hscale = vscale = value[RHI] / (10.0 * vsize);
        else {
            hscale = value[RWI] / (10.0 * hsize);
            vscale = value[RHI] / (10.0 * vsize);
        }
    }
    
    box->ox = originx;
    box->oy = originy;
    box->bw = hsize * hscale;
    box->bh = vsize * vscale;
    box->angle = value[ANGLE];

    *ret = buffer.data;
    
    return filename;
}
Ejemplo n.º 10
0
Archivo: txdb.c Proyecto: haraldh/bitc
static int
txdb_load_tx(struct txdb *txdb,
             const char  *key,
             size_t       klen,
             const char  *val,
             size_t       vlen)
{
    struct tx_ser_data *txd;
    struct tx_ser_key *txk;
    char hashStr[80];
    bool relevant = 0;
    uint256 txHash;
    bool confirmed;
    int res = 0;

    ASSERT(strncmp(key, "/tx/", 4) == 0);

    txk = txdb_deserialize_tx_key(key, klen);
    txd = txdb_deserialize_tx_data(val, vlen);

    ASSERT(txdb->tx_seq == txk->seq);
    txdb->tx_seq++;

    confirmed = !uint256_iszero(&txd->blkHash);

    hash256_calc(txd->buf, txd->len, &txHash);
    ASSERT(uint256_issame(&txHash, &txk->txHash));
    uint256_snprintf_reverse(hashStr, sizeof hashStr, &txHash);
    LOG(1, (LGPFX" loading %ctx %s\n", confirmed ? 'c' : 'u', hashStr));

    res = txdb_remember_tx(txdb, 1 /* not on disk, just hashtable */,
                           txd->timestamp, txd->buf, txd->len,
                           &txk->txHash, &txd->blkHash, &relevant);

    /*
     * If the transaction is still unconfirmed, add to relay set.
     */
    if (!confirmed) {
        struct buff buf;
        int numSec;

        numSec = time(NULL) - txd->timestamp;

        if (numSec > 0) {
            int hours = numSec / (60 * 60);
            int min  = (numSec % (60 * 60)) / 60;

            Log(LGPFX" unconfirmed tx %s was sent %d hours %d min ago.\n",
                hashStr, hours, min);
        }

        buff_init(&buf, txd->buf, txd->len);

        Log(LGPFX" adding tx %s to relay-set\n", hashStr);
        peergroup_new_tx_broadcast(btc->peerGroup, &buf,
                                   txd->timestamp + 2 * 60 * 60,
                                   &txHash);
    } else {
        uint256_snprintf_reverse(hashStr, sizeof hashStr, &txd->blkHash);
        Log(LGPFX" tx in block %s\n", hashStr);
    }

    free(txd->buf);
    free(txd);
    free(txk);

    return res;
}
Ejemplo n.º 11
0
int script_sign(struct wallet *wallet, const struct btc_msg_tx_out *txo,
                struct btc_msg_tx *tx, uint32 idx,
                enum script_hash_type hashType) {
  struct btc_msg_tx_in *txi;
  enum script_txout_type type;
  struct buff *scriptSig;
  struct buff scriptPubKey;
  uint8 *data_addr;
  size_t data_len;
  uint256 hash;
  int res;

  ASSERT(idx < tx->in_count);
  scriptSig = buff_alloc();
  buff_init(&scriptPubKey, txo->scriptPubKey, txo->scriptLength);

  Log_Bytes("scriptPubKey:", txo->scriptPubKey, txo->scriptLength);

  script_tx_sighash(wallet, &hash, &scriptPubKey, tx, idx, hashType);

  res = script_match_type(&scriptPubKey, &type, &data_addr, &data_len);
  if (res) {
    NOT_TESTED();
    goto exit;
  }

  switch (type) {
    case TX_PUBKEY:
      Warning(LGPFX " script TX_PUBKEY\n");
      NOT_IMPLEMENTED();
      break;
    case TX_PUBKEYHASH:
      (void)0;  // XXX: clang bug?
      uint160 *keyHash = (uint160 *)data_addr;

      ASSERT(data_len == sizeof(uint160));

      res = script_sign_hash(wallet, keyHash, &hash, hashType, scriptSig);
      if (res) {
        NOT_TESTED();
        goto exit;
      }

      res = script_push_pubkey(wallet, keyHash, scriptSig);
      if (res) {
        NOT_TESTED();
        goto exit;
      }
      break;
    default:
      NOT_IMPLEMENTED();
      Warning(LGPFX " script TX_NONSTANDARD\n");
      break;
  }

  txi = tx->tx_in + idx;
  txi->scriptLength = buff_curlen(scriptSig);
  txi->scriptSig = buff_base(scriptSig);

exit:
  free(scriptSig);
  free(data_addr);

  return res;
}