Exemplo n.º 1
0
int main(int argc, char *argv[]) {
	auto db = get_database(argc, argv);
	test_init(8);
	try {
		db->execute("CREATE TABLE rusqltest (`value` INT(2) NOT NULL)");
		pass("could run CREATE TABLE");
	} catch(std::exception &e) {
		diag(e);
		fail("could run CREATE TABLE");
	}
	test_start_try(1);
	try {
		auto res = db->query("SHOW TABLES");
		test(contains(res, "rusqltest"), "rusqltest table created");
	} catch(std::exception &e) {
		diag(e);
	}
	test_finish_try();

	test_start_try(1);
	try {
		// SELECT on empty database
		auto res = db->query("SELECT * FROM rusqltest");
		test(!res, "no result rows in empty table");
	} catch(std::exception &e) {
		diag(e);
	}
	test_finish_try();

	test_start_try(3);
	try {
		db->execute("INSERT INTO rusqltest (`value`) VALUES (27)");
		auto res = db->query("SELECT * FROM rusqltest");
		test(res, "result row in empty table");
		test(res.get_uint64(0) == 27, "correct result row in empty table");
		test(res.get_uint64("value") == 27, "correct result row by name in empty table");
	} catch(std::exception &e) {
		diag(e);
	}
	test_finish_try();

	try {
		db->execute("DROP TABLE rusqltest");
		pass("could run DROP TABLE");
	} catch(std::exception &e) {
		diag(e);
		fail("could run DROP TABLE");
	}
	test_start_try(1);
	try {
		auto res = db->query("SHOW TABLES");
		test(!contains(res, "rusqltest"), "rusqltest table dropped");
	} catch(std::exception &e) {
		diag(e);
	}
	test_finish_try();
	return 0;
}
Exemplo n.º 2
0
/* Open a new audio stream in the ogm file */
int ogm_open_audio(ogm_format_t *ogm, ogm_media_t * handler, ogg_stream_state *sstate, int sno, stream_header *sth){

   int codec;
   char buf[5];

   ogm_stream_t *ogm_strm = NULL;
   media_stream_t *stream = NULL;

   memcpy(buf, sth->subtype, 4);
   buf[4] = 0;
   codec = strtoul(buf, NULL, 16);

   /* stream type determined by codec */

   ogm_strm = (ogm_stream_t *)malloc(sizeof(ogm_stream_t));
   stream = (media_stream_t *)ogm_strm;

   if (stream == NULL) {

      fprintf(stderr, "malloc failed.\n");
      exit(1);
   }

   memcpy(&(ogm_strm->header), sth, ogm->packet.bytes);

   ogm_add_stream((media_format_t*)ogm, (media_stream_t*)stream, sno, ogm_strm->stype);

   ogm_strm->sno = ogm->format.nastreams;
   ogm_strm->stype = 'a';
   stream->sample_rate = (long)(get_uint64(&sth->samples_per_unit) *
                         get_uint16(&sth->sh.audio.channels));

   ogm_strm->serial = sno;
   ogm_strm->acodec = codec;

   ogm_strm->instate = sstate;

   ogm_audio_log(("ogm_open_file: (a%d/%d) codec: %d (0x%04x) (%s), bits per "
                 "sample: %d channels: %hd  samples per second: %lld",
                  ogm->format.nastreams, ogm->format.numstreams, codec, codec,
                  codec == ACPCM ? "PCM" : codec == 55 ? "MP3" :
                  codec == ACMP3 ? "MP3" :
                  codec == ACAC3 ? "AC3" : "unknown",
                  get_uint16(&sth->bits_per_sample),
                  get_uint16(&sth->sh.audio.channels),
                  get_uint64(&sth->samples_per_unit)
                ));

   ogm_audio_log(("ogm_open_file: avgbytespersec: %d blockalign: %hd\n",
                  get_uint32(&sth->sh.audio.avgbytespersec),
                  get_uint16(&sth->sh.audio.blockalign)
                ));

   return MP_OK;
}
Exemplo n.º 3
0
void mpfx_manager::set(mpfx & n, uint64 v) {
    if (m_int_part_sz == 1) {
        if (v > static_cast<uint64>(UINT_MAX))
            throw overflow_exception();
    }

    if (v == 0) {
        reset(n);
    }
    else {
        allocate_if_needed(n);
        n.m_sign              = 0;
        unsigned * _v         = reinterpret_cast<unsigned*>(&v);
        unsigned * w          = words(n);
        for (unsigned i = 0; i < m_total_sz; i++) 
            w[i] = 0;
        w[m_frac_part_sz]     = _v[0];
        if (m_int_part_sz == 1) {
            SASSERT(_v[1] == 0);
        }
        else {
            w[m_frac_part_sz+1] = _v[1];
        }
    }
    SASSERT(is_int(n));
    SASSERT(get_uint64(n) == v);
    SASSERT(check(n));
}
Exemplo n.º 4
0
bool zmq::decoder_t::eight_byte_size_ready ()
{
    //  8-byte size is read. Allocate the buffer for message body and
    //  read the message data into it.
    size_t size = (size_t) get_uint64 (tmpbuf);

    //  There has to be at least one byte (the flags) in the message).
    if (!size) {
        decoding_error ();
        return false;
    }

    //  in_progress is initialised at this point so in theory we should
    //  close it before calling zmq_msg_init_size, however, it's a 0-byte
    //  message and thus we can treat it as uninitialised...
    int rc;
    if (maxmsgsize >= 0 && (int64_t) (size - 1) > maxmsgsize) {
        rc = -1;
        errno = ENOMEM;
    }
    else
        rc = in_progress.init_size (size - 1);
    if (rc != 0 && errno == ENOMEM) {
        rc = in_progress.init ();
        errno_assert (rc == 0);
        decoding_error ();
        return false;
    }
    errno_assert (rc == 0);

    next_step (tmpbuf, 1, &decoder_t::flags_ready);
    return true;
}
Exemplo n.º 5
0
/*
 *  stress_set_timer_freq()
 *	set timer frequency from given option
 */
void stress_set_timer_freq(const char *optarg)
{
	set_timer_freq = true;
	opt_timer_freq = get_uint64(optarg);
	check_range("timer-freq", opt_timer_freq,
		MIN_TIMER_FREQ, MAX_TIMER_FREQ);
}
Exemplo n.º 6
0
static void
test_util_format_unaligned_accessors(void *ignored)
{
  (void)ignored;
  char buf[9] = "onionsoup"; // 6f6e696f6e736f7570

  tt_u64_op(get_uint64(buf+1), OP_EQ, htonll(U64_LITERAL(0x6e696f6e736f7570)));
  tt_uint_op(get_uint32(buf+1), OP_EQ, htonl(0x6e696f6e));
  tt_uint_op(get_uint16(buf+1), OP_EQ, htons(0x6e69));
  tt_uint_op(get_uint8(buf+1), OP_EQ, 0x6e);

  set_uint8(buf+7, 0x61);
  tt_mem_op(buf, OP_EQ, "onionsoap", 9);

  set_uint16(buf+6, htons(0x746f));
  tt_mem_op(buf, OP_EQ, "onionstop", 9);

  set_uint32(buf+1, htonl(0x78696465));
  tt_mem_op(buf, OP_EQ, "oxidestop", 9);

  set_uint64(buf+1, htonll(U64_LITERAL(0x6266757363617465)));
  tt_mem_op(buf, OP_EQ, "obfuscate", 9);
 done:
  ;
}
Exemplo n.º 7
0
 T get( const field_desc_type *fld ) const
 {
     switch(fld->cpp_type( )) {
     case field_desc_type::CPPTYPE_INT32:
         return boost::lexical_cast<T>(get_int32(fld));
     case field_desc_type::CPPTYPE_INT64:
         return boost::lexical_cast<T>(get_int64(fld));
     case field_desc_type::CPPTYPE_UINT32:
         return boost::lexical_cast<T>(get_uint32(fld));
     case field_desc_type::CPPTYPE_UINT64:
         return boost::lexical_cast<T>(get_uint64(fld));
     case field_desc_type::CPPTYPE_FLOAT:
         return boost::lexical_cast<T>(get_float(fld));
     case field_desc_type::CPPTYPE_DOUBLE:
         return boost::lexical_cast<T>(get_double(fld));
     case field_desc_type::CPPTYPE_ENUM:
         return boost::lexical_cast<T>(get_enum(fld)->number());
     case field_desc_type::CPPTYPE_STRING:
         return boost::lexical_cast<T>(get_string(fld));
     case field_desc_type::CPPTYPE_BOOL:
         return boost::lexical_cast<T>(get_bool(fld));
     case field_desc_type::CPPTYPE_MESSAGE:
         throw std::bad_cast( );
     default:
         return T( );
     }
 }
Exemplo n.º 8
0
static int
remote_access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val,
                   int write, void *arg)
{
  Debug (3, "called, regno %d\n", (int) reg);
  unw_fuchsia_info_t* cxt = arg;
  zx_handle_t thread = cxt->thread;
  if (write)
  {
    Debug (3, "writing to reg\n");
    return -UNW_EINVAL;
  }
  if (!unw_is_greg (reg))
  {
    Debug (3, "bad regnum: %d\n", (int) reg);
    return -UNW_EBADREG;
  }

  zx_thread_state_general_regs_t regs;
  zx_status_t r =
      zx_thread_read_state(thread, ZX_THREAD_STATE_GENERAL_REGS, &regs, sizeof(regs));
  if (r < 0)
  {
    Debug (3, "error reading gregs: %d\n", r);
    return -UNW_EUNSPEC;
  }

  const char* buf = (const char*)&regs;
  if (sizeof(*val) == sizeof(uint32_t))
    *val = get_uint32 (buf + fuchsia_greg_offset[reg]);
  else
    *val = get_uint64 (buf + fuchsia_greg_offset[reg]);
  Debug (3, "reg val: 0x%llx\n", (long long) *val);
  return 0;
}
Exemplo n.º 9
0
void stress_set_fifo_readers(const char *optarg)
{
	set_fifo_readers = true;
	opt_fifo_readers = get_uint64(optarg);
	check_range("fifo-readers", opt_fifo_readers,
		MIN_FIFO_READERS, MAX_FIFO_READERS);
}
Exemplo n.º 10
0
int zmq::v2_decoder_t::eight_byte_size_ready (unsigned char const* read_from) {
    //  The payload size is encoded as 64-bit unsigned integer.
    //  The most significant byte comes first.
    const uint64_t msg_size = get_uint64(tmpbuf);

    return size_ready(msg_size, read_from);
}
Exemplo n.º 11
0
bool BinarySource_get_fxp_attrs(BinarySource *src, struct fxp_attrs *attrs)
{
    attrs->flags = get_uint32(src);
    if (attrs->flags & SSH_FILEXFER_ATTR_SIZE)
        attrs->size = get_uint64(src);
    if (attrs->flags & SSH_FILEXFER_ATTR_UIDGID) {
	attrs->uid = get_uint32(src);
	attrs->gid = get_uint32(src);
    }
    if (attrs->flags & SSH_FILEXFER_ATTR_PERMISSIONS)
	attrs->permissions = get_uint32(src);
    if (attrs->flags & SSH_FILEXFER_ATTR_ACMODTIME) {
	attrs->atime = get_uint32(src);
	attrs->mtime = get_uint32(src);
    }
    if (attrs->flags & SSH_FILEXFER_ATTR_EXTENDED) {
	unsigned long count = get_uint32(src);
	while (count--) {
	    /*
	     * We should try to analyse these, if we ever find one
	     * we recognise.
	     */
	    get_string(src);
	    get_string(src);
	}
    }
    return true;
}
Exemplo n.º 12
0
int zmq::curve_server_t::process_hello (msg_t *msg_)
{
	puts("zmq::curve_server_t::process_hello start");

	if (msg_->size() != 200) {
        //  Temporary support for security debugging
        puts ("CURVE I: client HELLO is not correct size");
        errno = EPROTO;
        return -1;
    }

    const uint8_t * const hello = static_cast <uint8_t *> (msg_->data ());
    if (memcmp (hello, "\x05HELLO", 6)) {
        //  Temporary support for security debugging
        puts ("CURVE I: client HELLO has invalid command name");
        errno = EPROTO;
        return -1;
    }

    const uint8_t major = hello [6];
    const uint8_t minor = hello [7];

    if (major != 1 || minor != 0) {
        //  Temporary support for security debugging
        puts ("CURVE I: client HELLO has unknown version number");
        errno = EPROTO;
        return -1;
    }

    //  Save client's short-term public key (C')
    memcpy (cn_client, hello + 80, 32);

    uint8_t hello_nonce [crypto_box_NONCEBYTES];
    uint8_t hello_plaintext [crypto_box_ZEROBYTES + 64];
    uint8_t hello_box [crypto_box_BOXZEROBYTES + 80];

    memcpy (hello_nonce, "CurveZMQHELLO---", 16);
    memcpy (hello_nonce + 16, hello + 112, 8);
    cn_peer_nonce = get_uint64(hello + 112);

    memset (hello_box, 0, crypto_box_BOXZEROBYTES);
    memcpy (hello_box + crypto_box_BOXZEROBYTES, hello + 120, 80);

    //  Open Box [64 * %x0](C'->S)
    int rc = crypto_box_open (hello_plaintext, hello_box,
                              sizeof hello_box,
                              hello_nonce, cn_client, secret_key);
    if (rc != 0) {
        //  Temporary support for security debugging
        puts ("CURVE I: cannot open client HELLO -- wrong server key?");
        errno = EPROTO;
        return -1;
    }

    state = send_welcome;

	puts("zmq::curve_server_t::process_hello end");

	return rc;
}
Exemplo n.º 13
0
bool zmq::bp_decoder_t::eight_byte_size_ready ()
{
    //  8-byte size is read. Allocate the buffer for message body and
    //  read the message data into it.
    message.rebuild ((size_t) get_uint64 (tmpbuf));
    next_step (message.data (), message.size (), &bp_decoder_t::message_ready);
    return true;
}
Exemplo n.º 14
0
static int stress_set_lease_breakers(const char *opt)
{
	uint64_t lease_breakers;

	lease_breakers = get_uint64(opt);
	check_range("lease-breakers", lease_breakers,
		MIN_LEASE_BREAKERS, MAX_LEASE_BREAKERS);
	return set_setting("lease-breakers", TYPE_ID_UINT64, &lease_breakers);
}
Exemplo n.º 15
0
/*
 *  stress_set_mergesort_size()
 *	set mergesort size
 */
static int stress_set_mergesort_size(const char *opt)
{
	uint64_t mergesort_size;

	mergesort_size = get_uint64(opt);
	check_range("mergesort-size", mergesort_size,
		MIN_MERGESORT_SIZE, MAX_MERGESORT_SIZE);
	return set_setting("mergesort-size", TYPE_ID_UINT64, &mergesort_size);
}
Exemplo n.º 16
0
static int stress_set_semaphore_posix_procs(const char *opt)
{
	uint64_t semaphore_posix_procs;

	semaphore_posix_procs = get_uint64(opt);
	check_range("sem-procs", semaphore_posix_procs,
		MIN_SEMAPHORE_PROCS, MAX_SEMAPHORE_PROCS);
	return set_setting("sem-procs", TYPE_ID_UINT64, &semaphore_posix_procs);
}
Exemplo n.º 17
0
/*
 *  stress_set_lsearch_size()
 *      set lsearch size from given option string
 */
static int stress_set_lsearch_size(const char *opt)
{
	uint64_t lsearch_size;

	lsearch_size = get_uint64(opt);
	check_range("lsearch-size", lsearch_size,
		MIN_TSEARCH_SIZE, MAX_TSEARCH_SIZE);
	return set_setting("lsearch-size", TYPE_ID_UINT64, &lsearch_size);
}
Exemplo n.º 18
0
/*
 *  stress_set_timer_freq()
 *	set timer frequency from given option
 */
static int stress_set_timer_freq(const char *opt)
{
	uint64_t timer_freq;

	timer_freq = get_uint64(opt);
	check_range("timer-freq", timer_freq,
		MIN_TIMER_FREQ, MAX_TIMER_FREQ);
	return set_setting("timer-freq", TYPE_ID_UINT64, &timer_freq);
}
Exemplo n.º 19
0
void print_u64(cbuf_t* in, int n)
{
    int i;
    for (i = 0; i < n; i++) {
	u_int64_t val;
	get_uint64(in, &val);
	printf("%llu ", val);
    }
    printf("\n");
}
Exemplo n.º 20
0
void stress_set_aio_requests(const char *optarg)
{
	uint64_t aio_requests;

	set_aio_requests = true;
	aio_requests = get_uint64(optarg);
	check_range("aio-requests", aio_requests,
		MIN_AIO_REQUESTS, MAX_AIO_REQUESTS);
	opt_aio_requests = (int)aio_requests;
}
Exemplo n.º 21
0
int zmq::curve_client_t::process_ready (const uint8_t *msg_data,
                                        size_t msg_size)
{
    if (msg_size < 30) {
        session->get_socket ()->event_handshake_failed_protocol (
          session->get_endpoint (),
          ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_READY);
        errno = EPROTO;
        return -1;
    }

    const size_t clen = (msg_size - 14) + crypto_box_BOXZEROBYTES;

    uint8_t ready_nonce[crypto_box_NONCEBYTES];
    uint8_t *ready_plaintext =
      static_cast<uint8_t *> (malloc (crypto_box_ZEROBYTES + clen));
    alloc_assert (ready_plaintext);
    uint8_t *ready_box =
      static_cast<uint8_t *> (malloc (crypto_box_BOXZEROBYTES + 16 + clen));
    alloc_assert (ready_box);

    memset (ready_box, 0, crypto_box_BOXZEROBYTES);
    memcpy (ready_box + crypto_box_BOXZEROBYTES, msg_data + 14,
            clen - crypto_box_BOXZEROBYTES);

    memcpy (ready_nonce, "CurveZMQREADY---", 16);
    memcpy (ready_nonce + 16, msg_data + 6, 8);
    cn_peer_nonce = get_uint64 (msg_data + 6);

    int rc = crypto_box_open_afternm (ready_plaintext, ready_box, clen,
                                      ready_nonce, cn_precom);
    free (ready_box);

    if (rc != 0) {
        session->get_socket ()->event_handshake_failed_protocol (
          session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_CRYPTOGRAPHIC);
        errno = EPROTO;
        return -1;
    }

    rc = parse_metadata (ready_plaintext + crypto_box_ZEROBYTES,
                         clen - crypto_box_ZEROBYTES);
    free (ready_plaintext);

    if (rc == 0)
        state = connected;
    else {
        session->get_socket ()->event_handshake_failed_protocol (
          session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_INVALID_METADATA);
        errno = EPROTO;
    }

    return rc;
}
Exemplo n.º 22
0
tnf_float64_t
tnf_get_float64(tnf_datum_t datum)
{
	union {
		tnf_uint64_t	i64;
		tnf_float64_t	f64;
	} u;

	CHECK_SCALAR(datum);

	u.i64 = get_uint64(DATUM_TNF(datum), DATUM_VAL(datum)); /* XXX */
	return (u.f64);
}
Exemplo n.º 23
0
static int parse_supertcp(struct ftpparse *f, char *p[], int l[], 
  unsigned int count)
{
  unsigned long mon;
  unsigned long day;
  unsigned long year;
  unsigned long hour;
  unsigned long min;
  unsigned long sec;
  int mtimetype;
  uint64 size=0; /* optional, dirs */
  int x;
  int dir=0;

/* CMT             <DIR>           11-21-94        10:17 */
/* DESIGN1.DOC          11264      05-11-95        14:20 */

  if (count<4) return 0;
  x=scan_time(p[3],l[3],&hour,&min,&sec,&mtimetype);
  if (x!=l[3]) return 0;


  x=get_ulong(p[2],l[2],&mon); 
  if (x!=2 || p[2][x]!='-') return 0;
  x++;
  x+=get_ulong(p[2]+x,l[2]-x,&day); 
  if (x!=5 || p[2][x]!='-') return 0;
  x++;
  x+=get_ulong(p[2]+x,l[2]-x,&year); 
  if ((x!=8  && x!=10) || p[2][x]!=' ') return 0;
  if (!fix_year(&year)) return 0;
  if (my_byte_equal(p[1],5,"<DIR>")) 
    dir=1;
  else {
    x=get_uint64(p[1],l[1],&size);
    if (!x || p[1][x]!=' ') return 0;
  }

  f->name=p[0];
  f->namelen=l[0];
  f->size=size;
  if (!dir)
    f->sizetype=FTPPARSE_SIZE_BINARY;
  utcdate2tai (&f->mtime,year,mon,day,hour,min,sec);
  f->mtimetype=mtimetype;
  if (dir) f->flagtrycwd=1;
  else     f->flagtryretr=1;
  return 1;
}
Exemplo n.º 24
0
/* Parse the encoded commit. The format is:
 *    base64-encode( TIMESTAMP || H(REVEAL) )
 *
 * If successfully decoded and parsed, commit is updated and 0 is returned.
 * On error, return -1. */
STATIC int
commit_decode(const char *encoded, sr_commit_t *commit)
{
  int decoded_len = 0;
  size_t offset = 0;
  char b64_decoded[SR_COMMIT_LEN];

  tor_assert(encoded);
  tor_assert(commit);

  if (strlen(encoded) > SR_COMMIT_BASE64_LEN) {
    /* This means that if we base64 decode successfully the reveiced commit,
     * we'll end up with a bigger decoded commit thus unusable. */
    goto error;
  }

  /* Decode our encoded commit. Let's be careful here since _encoded_ is
   * coming from the network in a dirauth vote so we expect nothing more
   * than the base64 encoded length of a commit. */
  decoded_len = base64_decode(b64_decoded, sizeof(b64_decoded),
                              encoded, strlen(encoded));
  if (decoded_len < 0) {
    log_warn(LD_BUG, "SR: Commit from authority %s can't be decoded.",
             sr_commit_get_rsa_fpr(commit));
    goto error;
  }

  if (decoded_len != SR_COMMIT_LEN) {
    log_warn(LD_BUG, "SR: Commit from authority %s decoded length doesn't "
                     "match the expected length (%d vs %u).",
             sr_commit_get_rsa_fpr(commit), decoded_len,
             (unsigned)SR_COMMIT_LEN);
    goto error;
  }

  /* First is the timestamp (8 bytes). */
  commit->commit_ts = tor_ntohll(get_uint64(b64_decoded));
  offset += sizeof(uint64_t);
  /* Next is hashed reveal. */
  memcpy(commit->hashed_reveal, b64_decoded + offset,
         sizeof(commit->hashed_reveal));
  /* Copy the base64 blob to the commit. Useful for voting. */
  strlcpy(commit->encoded_commit, encoded, sizeof(commit->encoded_commit));

  return 0;

 error:
  return -1;
}
Exemplo n.º 25
0
/* Open a new video stream in the ogm file */
int ogm_open_video(ogm_media_t * handler, ogm_format_t *ogm, media_control_t *ctrl, ogg_stream_state *sstate, int sno, stream_header *sth){

   unsigned long codec;
   char ccodec[5];
   ogm_stream_t *ogm_strm = NULL;
   media_stream_t *stream = NULL;

   strncpy(ccodec, sth->subtype, 4);
   ccodec[4] = 0;
   codec = (sth->subtype[0] << 24) + (sth->subtype[1] << 16) +
            (sth->subtype[2] << 8) + sth->subtype[3];

   /* stream type ccodec */
   ogm_strm = (ogm_stream_t *)malloc(sizeof(ogm_stream_t));
   stream = (media_stream_t *)ogm_strm;

   if (stream == NULL) {

      ogm_video_log(("ogm_open_video: malloc failed.\n"));
      exit(1);
   }

   memcpy(&(ogm_strm->header), sth, ogm->packet.bytes);

   //nstrm++;
   //v_nstrm++;

   stream->handler = handler;
   
   ogm_strm->stype = 'v';
   ogm_strm->serial = sno;
   stream->sample_rate = 10000000 / (long)get_uint64(&sth->time_unit);

   ogm_strm->sno = (ogm->format.nvstreams+1);
   ogm_strm->instate = sstate;

   ogm_video_log(("ogm_open_video: (v%d/%d) fps: %ld width height: %dx%d " \
                  "codec: %p (%s)\n", (ogm->format.nvstreams+1), (ogm->format.numstreams+1),
                  stream->sample_rate,
                  get_uint32(&sth->sh.video.width),
                  get_uint32(&sth->sh.video.height), (void *)codec,
                  ccodec));

   ogm->format.add_stream((media_format_t*)ogm, stream, sno, ogm_strm->stype);
   /* End of video stream head parsing */

   return MP_OK;
}
Exemplo n.º 26
0
void mpfx_manager::set(mpfx & n, unsigned v) {
    if (v == 0) {
        reset(n);
    }
    else {
        allocate_if_needed(n);
        n.m_sign              = 0;
        unsigned * w          = words(n);
        for (unsigned i = 0; i < m_total_sz; i++) 
            w[i] = 0;
        w[m_frac_part_sz]     = v;
    }
    SASSERT(is_int(n));
    SASSERT(get_uint64(n) == v);
    SASSERT(check(n));
}
Exemplo n.º 27
0
/* Parse the b64 blob at <b>encoded</b> containing reveal information and
 * store the information in-place in <b>commit</b>. Return 0 on success else
 * a negative value. */
STATIC int
reveal_decode(const char *encoded, sr_commit_t *commit)
{
  int decoded_len = 0;
  char b64_decoded[SR_REVEAL_LEN];

  tor_assert(encoded);
  tor_assert(commit);

  if (strlen(encoded) > SR_REVEAL_BASE64_LEN) {
    /* This means that if we base64 decode successfully the received reveal
     * value, we'll end up with a bigger decoded value thus unusable. */
    goto error;
  }

  /* Decode our encoded reveal. Let's be careful here since _encoded_ is
   * coming from the network in a dirauth vote so we expect nothing more
   * than the base64 encoded length of our reveal. */
  decoded_len = base64_decode(b64_decoded, sizeof(b64_decoded),
                              encoded, strlen(encoded));
  if (decoded_len < 0) {
    log_warn(LD_BUG, "SR: Reveal from authority %s can't be decoded.",
             sr_commit_get_rsa_fpr(commit));
    goto error;
  }

  if (decoded_len != SR_REVEAL_LEN) {
    log_warn(LD_BUG, "SR: Reveal from authority %s decoded length is "
                     "doesn't match the expected length (%d vs %u)",
             sr_commit_get_rsa_fpr(commit), decoded_len,
             (unsigned)SR_REVEAL_LEN);
    goto error;
  }

  commit->reveal_ts = tor_ntohll(get_uint64(b64_decoded));
  /* Copy the last part, the random value. */
  memcpy(commit->random_number, b64_decoded + 8,
         sizeof(commit->random_number));
  /* Also copy the whole message to use during verification */
  strlcpy(commit->encoded_reveal, encoded, sizeof(commit->encoded_reveal));

  return 0;

 error:
  return -1;
}
Exemplo n.º 28
0
/* see http://cr.yp.to/ftp/list/eplf.html */
static int 
parse_eplf(struct ftpparse *f, char *buf, unsigned int len)
{
  unsigned int start,pos;
  unsigned long ul;
  if (buf[0]!='+') return 0;

  start=1;
  for (pos = 1;pos < len;pos++) {
    if ('\t'==buf[pos]) {
      f->name=buf+pos+1;
      f->namelen=len-pos-1;
      if (!f->namelen) return 0; /* huh? */
      f->format=FTPPARSE_FORMAT_EPLF;
      return 1;
    }
    if (',' != buf[pos])
      continue;
    switch(buf[start]) {
    case '/': f->flagtrycwd=1; break;
    case 'r': f->flagtryretr=1; break;
    case 's': 
      if (pos-start-1==0) return 0;
      if (get_uint64(buf+start+1,pos-start-1,&f->size)
	  !=pos-start-1) return 0;
      f->sizetype=FTPPARSE_SIZE_BINARY;
      break;
    case 'm':
      if (pos-start-1==0) return 0;
      if (get_ulong(buf+start+1,pos-start-1,&ul)!=pos-start-1) return 0;
      tai_unix(&f->mtime,ul);
      f->mtimetype = FTPPARSE_MTIME_LOCAL;
      break;
    case 'i':
      /* refuse zero bytes length ids */
      if (pos-start-1==0) return 0;
      f->idtype = FTPPARSE_ID_FULL;
      f->id=buf+start+1;
      f->idlen=pos-start-1;
      break;
    }
    start=pos+1;
  }
  return 0;
}
Exemplo n.º 29
0
int zmq::v1_decoder_t::eight_byte_size_ready ()
{
    //  8-byte payload length is read. Allocate the buffer
    //  for message body and read the message data into it.
    const uint64_t payload_length = get_uint64 (tmpbuf);

    //  There has to be at least one byte (the flags) in the message).
    if (payload_length == 0) {
        errno = EPROTO;
        return -1;
    }

    //  Message size must not exceed the maximum allowed size.
    if (maxmsgsize >= 0 && payload_length - 1 > (uint64_t) maxmsgsize) {
        errno = EMSGSIZE;
        return -1;
    }

    //  Message size must fit within range of size_t data type.
    if (payload_length - 1 > std::numeric_limits <size_t>::max ()) {
        errno = EMSGSIZE;
        return -1;
    }

    const size_t msg_size = static_cast <size_t> (payload_length - 1);

    //  in_progress is initialised at this point so in theory we should
    //  close it before calling init_size, however, it's a 0-byte
    //  message and thus we can treat it as uninitialised...
    int rc = in_progress.init_size (msg_size);
    if (rc != 0) {
        errno_assert (errno == ENOMEM);
        rc = in_progress.init ();
        errno_assert (rc == 0);
        errno = ENOMEM;
        return -1;
    }

    next_step (tmpbuf, 1, &v1_decoder_t::flags_ready);
    return 0;
}
Exemplo n.º 30
0
int zmq::v1_decoder_t::eight_byte_size_ready (unsigned char const*)
{
    //  8-byte payload length is read. Allocate the buffer
    //  for message body and read the message data into it.
    const uint64_t payload_length = get_uint64 (tmpbuf);

    //  There has to be at least one byte (the flags) in the message).
    if (payload_length == 0) {
        errno = EPROTO;
        return -1;
    }

    //  Message size must not exceed the maximum allowed size.
    if (maxmsgsize >= 0 && payload_length - 1 > (uint64_t) maxmsgsize) {
        errno = EMSGSIZE;
        return -1;
    }

    //  Message size must fit within range of size_t data type.
    if (payload_length - 1 > std::numeric_limits <size_t>::max ()) {
        errno = EMSGSIZE;
        return -1;
    }

    const size_t msg_size = static_cast <size_t> (payload_length - 1);

    int rc = in_progress.close();
    assert(rc == 0);
    rc = in_progress.init_size (msg_size);
    if (rc != 0) {
        errno_assert (errno == ENOMEM);
        rc = in_progress.init ();
        errno_assert (rc == 0);
        errno = ENOMEM;
        return -1;
    }

    next_step (tmpbuf, 1, &v1_decoder_t::flags_ready);
    return 0;
}