Exemplo n.º 1
0
int main(int cgc_argc, char *cgc_argv[])
{
    unsigned char *payload = NULL, *buf;
    unsigned int payload_len = 0;
    cgc_size_t bytes;

    cgc_negotiate_type1(0xfefefefe, 0xfefefefe, 3);

    payload = cgc_append_buf(payload, &payload_len, (unsigned char *)"\x00\x00\x80\x0c\x06\x67\xff\xff\xff\xff", 10);

    buf = cgc_malloc(264);
    cgc_random(buf, 264, &bytes);
    payload = cgc_append_buf(payload, &payload_len, buf, 264);
    cgc_free(buf);

    payload = cgc_append_var("TYPE1_REG", payload, &payload_len);
    payload = cgc_append_var("TYPE1_IP", payload, &payload_len);

    buf = cgc_malloc(0x8000);
    cgc_random(buf, 0x8000, &bytes);
    payload = cgc_append_buf(payload, &payload_len, buf, 0x8000 - 264 - 8 + 6);
    cgc_free(buf);

    cgc_transmit_all(1, payload, payload_len);
    cgc_free(payload);
}
Exemplo n.º 2
0
void *cgc_realloc(void *ptr, cgc_size_t size)
{
    if (size == 0)
    {
        cgc_free(ptr);
        return NULL;
    }
    if (ptr == NULL)
    {
        return cgc_malloc(size);
    }
    block_t *block = (block_t *)((intptr_t)ptr - OVERHEAD_BYTES);
    if (size > block->size/2 && size < block->size-OVERHEAD_BYTES)
    {
        //cgc_printf("realloc(%08X, %d) = %08X\n", ptr, size, ptr);
        return ptr;
    }
    void *newmem = cgc_malloc(size);
    if (newmem == NULL)
        return NULL;
    cgc_size_t tocopy = block->size-OVERHEAD_BYTES;
    if (tocopy > size)
        tocopy = size;
    cgc_memcpy(newmem, ptr, tocopy);
    cgc_free(ptr);
    //cgc_printf("realloc(%08X, %d) = %08X\n", ptr, size, newmem);
    return newmem;
}
Exemplo n.º 3
0
int cgc_enqueue_copy(queue_t **queue, char *data, cgc_size_t size)
{
    if (data == NULL)
        return -1;

    int data_len = cgc_strlen(data) + 1;
    if (data_len > size)
        return -1;

    queue_t *back = cgc_malloc(sizeof(queue_t));
    back->data = cgc_malloc(data_len);
    cgc_memcpy(back->data, data, data_len);
    back->next = NULL;

    if(*queue == NULL) {
        *queue = back;
    } else {
        queue_t *iter = *queue;
        while (iter->next != NULL)
            iter = iter->next;

        iter->next = back;
    }

    return 0;
}
Exemplo n.º 4
0
int cgc_enq_msg(msg_queue *q, message_t *msg)
{
  if (msg)
  {
    if (*q == NULL)
    {
      *q = (node_t *) cgc_malloc(sizeof(node_t));
      if (*q == NULL)
        return -1;
      (*q)->msg = msg;
      (*q)->next = NULL;
    }
    else
    {
      node_t *n;
      node_t *tmp = (node_t *) cgc_malloc(sizeof(node_t));
      if (tmp == NULL)
        return -1;
      tmp->msg = msg;
      tmp->next = NULL;
      for (n = *q; n->next != NULL; n = n->next);
      n->next = tmp;
    }
  }
  return 0;
}
Exemplo n.º 5
0
error_t cgc_team_new(team_t **team)
{
    if (!team)
        return ERR_INTERNAL;
    *team = (team_t *) cgc_malloc(sizeof(team_t));
    cgc_memset(*team, 0, sizeof(team_t));
    (*team)->members = (user_t **) cgc_malloc(sizeof(user_t *) * 8);
    (*team)->sz_members = 8;
    cgc_memset((*team)->members, 0, (*team)->sz_members * sizeof(user_t *));
    return ERR_OK;
}
Exemplo n.º 6
0
int main(int cgc_argc, char *cgc_argv[]) {
   cgc_negotiate_type1(0x0, 0x0, 0);
   do {
      unsigned char *read_00000;
      unsigned int read_00000_len;
      unsigned int read_00000_ptr = 0;
      //**** length read
      read_00000_len = 2;
      read_00000 = (unsigned char*)cgc_malloc(read_00000_len);
      int read_00000_res = cgc_length_read(0, read_00000, read_00000_len);
      if (read_00000_res) {} //silence unused variable warning
      cgc_free(read_00000);
      if (read_00000_ptr) {}  //silence unused variable warning if any
   } while (0);
   do {
      //*** writing data
      static unsigned char write_00000_00000[] = 
         "\x22\x41\x22\x20\x2a\x20\x34\x32\x39\x34\x39\x36\x37\x32\x39\x35"
         "\x0a";
      static unsigned int write_00000_00000_len = 17;
      unsigned char *write_00000 = NULL;
      unsigned int write_00000_len = 0;
      write_00000 = cgc_append_buf(write_00000, &write_00000_len, write_00000_00000, write_00000_00000_len);
      if (write_00000_len > 0) {
         cgc_transmit_all(1, write_00000, write_00000_len);
      }
      cgc_free(write_00000);
   } while (0);
   do {
      unsigned char *read_00001;
      unsigned int read_00001_len;
      unsigned int read_00001_ptr = 0;
      //**** length read
      read_00001_len = 2;
      read_00001 = (unsigned char*)cgc_malloc(read_00001_len);
      int read_00001_res = cgc_length_read(0, read_00001, read_00001_len);
      if (read_00001_res) {} //silence unused variable warning
      cgc_free(read_00001);
      if (read_00001_ptr) {}  //silence unused variable warning if any
   } while (0);
   do {
      //*** writing data
      static unsigned char write_00001_00000[] = 
         "\x74\x65\x72\x6d\x69\x6e\x61\x74\x65\x0a";
      static unsigned int write_00001_00000_len = 10;
      unsigned char *write_00001 = NULL;
      unsigned int write_00001_len = 0;
      write_00001 = cgc_append_buf(write_00001, &write_00001_len, write_00001_00000, write_00001_00000_len);
      if (write_00001_len > 0) {
         cgc_transmit_all(1, write_00001, write_00001_len);
      }
      cgc_free(write_00001);
   } while (0);
}
Exemplo n.º 7
0
compounds_sample_t *cgc_alloc_sample_st(const cgc_size_t sample_size){


	compounds_sample_t * p = (compounds_sample_t *) cgc_malloc(sizeof(compounds_sample_t));
	if(p == NULL)
		return p;
	p->sample = (unsigned int *) cgc_malloc(sample_size * sizeof(unsigned int));
	if(p->sample == NULL)
		return NULL;
	p->sample_size = sample_size;
	return p;
}
Exemplo n.º 8
0
/**
 * Function to initialize base parts of a hash table
 *
 * @param size 	Size for hash table
 * @return 		Pointer to new hash table
 */
static ht_t *cgc_ht_init(unsigned int size) {
	ht_t *h = cgc_malloc(sizeof(ht_t));
	if (NULL == h) cgc__terminate(ERRNO_ALLOC);

	h->buckets_cnt = cgc_get_next_largest_prime(size);
	h->size = size;
	h->pair_cnt = 0;
	h->iter_idx = 0;
	h->iter_node = NULL;
	h->buckets = cgc_malloc(h->buckets_cnt * sizeof(struct list *));

	if (NULL == h->buckets) cgc__terminate(ERRNO_ALLOC);

	return h;
}
Exemplo n.º 9
0
acct_txn_entry_t * cgc_get_acct_txn_reg(uint32_t cid, uint32_t acd, uint32_t txn_id){

	account_entry_t * ae = cgc_get_account(cid, acd);
	


	if(ae == NULL){
		cgc_ERRNO = ERRNO_MP_NOT_FOUND;
		return NULL;
	}

	txn_entry_t * te = cgc_get_transaction(ae, txn_id);
	if(te == NULL){
		cgc_ERRNO = ERRNO_MP_NOT_FOUND;
		return NULL;
	}

	acct_txn_entry_t *aet = (acct_txn_entry_t *) cgc_malloc(sizeof(acct_txn_entry_t));
	if(aet == NULL){
		cgc_ERRNO = ERRNO_MP_ALLOC;
		return NULL;
	}
	aet->ae = ae;
	aet->txn = te;

	return aet;


};
Exemplo n.º 10
0
char * cgc_gen_chem_name(mixed_compound_t *mc, compounds_sample_t *rcs){
	// todo +1 in patched version

	unsigned int block_sz = 8;

	for(int i =0; i < rcs->sample_size; ++i){
		if(block_sz*i == MAX_NEW_CMPND_SZ)
			break;

		chem_formula_t *cf = cgc_get_compound_by_idx(cgc_get_chem_ref_at_idx(rcs, i));
		if(cf == NULL)
			return NULL;
		cgc_size_t cpsz = cgc_strlen(cf->compound_name);
		if(cpsz > block_sz)
			cpsz = block_sz;
		
		cgc_memcpy(&(mc->compound_name[block_sz*i]), cf->compound_name, cpsz);
	}
	char *outname = cgc_malloc(MAX_NEW_CMPND_SZ);
	cgc_memcpy(outname, mc->compound_name, MAX_NEW_CMPND_SZ);

	return outname;


}
Exemplo n.º 11
0
str_int_pair_t *cgc_get_s_i_pair(char *s, int i) {
	if (NULL == s) return NULL;

	str_int_pair_t *pair = cgc_malloc(sizeof(str_int_pair_t));
	if (NULL == pair) cgc__terminate(ERRNO_ALLOC);

	unsigned int len = cgc_strlen(s, '\0') + 1;
	pair->key = cgc_malloc(len * sizeof(char));
	if (NULL == pair->key) cgc__terminate(ERRNO_ALLOC);

	cgc_memcpy(pair->key, s, len);

	pair->value = i;

	return pair;
}
Exemplo n.º 12
0
void *cgc_ht_pair_insert(ht_t *h, void *pair) {

	unsigned int hash_val = h->fn_hash(h->fn_key_ptr(pair), h->buckets_cnt);

	if (NULL == h->buckets[hash_val]) {
		// create empty list for this bucket
		struct list *l = cgc_malloc(sizeof(struct list));
		if (NULL == l) cgc__terminate(ERRNO_ALLOC);

		cgc_list_init(l, NULL);
		h->buckets[hash_val] = l;
	}

	// determine if a pair with this key is already in this list
	struct node *n = cgc_list_find_node_with_data(h->buckets[hash_val], h->fn_key_in_pair, h->fn_key_ptr(pair));

	if (NULL != n) { // pair with this key exists, so remove it
		cgc_list_remove_node(h->buckets[hash_val], n);
		h->pair_cnt--;
	}

	// add new pair to list
	cgc_list_insert_at_end(h->buckets[hash_val], pair);
	h->pair_cnt++;

	// while it might be convenient to check/do re_hash here, I've decided to
	// omit that and force the user manually check/do re_hash as they desire.

	if (NULL == n) return NULL;

	return n->data;
}
Exemplo n.º 13
0
/**
 * Receive input buffer, convert to Stream object
 *
 * @return SUCCESS on success, else, -1 on error
 */
cgc_ssize_t cgc_receive_input(void) {
    cgc_ssize_t res;
    Stream tmp;
    // recv Input type and size
    res = cgc_recv_all((char *)&tmp, sizeof(tmp));
    if (res != sizeof(tmp)) {
        cgc__terminate(ERRNO_RECV);
    }

    // check for invalid INPUT_TYPE
    if (cgc_memcmp(cgc_INPUT_TYPE_PLAIN, (const char *)tmp.type, sizeof(cgc_INPUT_TYPE_PLAIN)) &&
        cgc_memcmp(cgc_INPUT_TYPE_SERIALIZED, (const char *)tmp.type, sizeof(cgc_INPUT_TYPE_SERIALIZED))) {
        return -1;
    }

    in = cgc_malloc(sizeof(tmp) + tmp.size);
    MALLOC_OK(in);

    in->size = tmp.size;
    cgc_memcpy(in->type, tmp.type, sizeof(cgc_INPUT_TYPE_SERIALIZED));

    res = cgc_recv_all(in->content, in->size);
    if (res != in->size) {
        cgc__terminate(ERRNO_RECV);
    }

    return SUCCESS;
}
Exemplo n.º 14
0
int cgc_putenv(const char *name, const unsigned char *val, cgc_size_t val_len) {
   cgc_size_t i;

   if (name == NULL || val == NULL) {
      return -1;
   }

   for (i = 0; i < env_size; i++) {
      if (cgc_strcmp(name, env[i]->name) == 0) {
         cgc_free(env[i]->val);
         env[i]->val = cgc_memdup(val, val_len);
         env[i]->val_len = val_len;
         return 0;
      }
   }
   if (env_size == env_cap) {
      entry **e = cgc_realloc(env, (env_cap + 1) * sizeof(char*));
      if (e == NULL) {
         return -1;
      }
      env = e;
      env_cap++;
   }
   env_size++;
   env[i] = (entry*)cgc_malloc(sizeof(entry));
   env[i]->name = cgc_memdup((const unsigned char*)name, cgc_strlen(name) + 1);
   env[i]->val = cgc_memdup(val, val_len);
   env[i]->val_len = val_len;
   return 0;
}
Exemplo n.º 15
0
/**
* Allocate size number of bytes from the heap
* 
* @param size The number of bytes to allocate
*
* @return a pointer the allocated memory
*/
void* cgc_malloc(cgc_size_t size) {

	void* allocated=NULL;
	unsigned int msb=0;
	int ret=0;

	// Get pool
	msb = cgc_getMSB(size);

	if(size > (1 << msb) || msb == 0)
		msb+=1;
	if(msb > 11) {
		LargeChunk* lc_ptr;
		for(lc_ptr = largeChunks; lc_ptr != NULL; lc_ptr=lc_ptr->next);
		lc_ptr = (LargeChunk *) cgc_malloc(sizeof(LargeChunk));
		if((ret = cgc_allocate(size, 0, &lc_ptr->memory)))
			return NULL;
		lc_ptr->next = largeChunks;
		largeChunks = lc_ptr;
		lc_ptr->size = size;
		return lc_ptr->memory;
	}

	if((ret = cgc_getNextFreeChunk(msb, &allocated)))
		return NULL;

	return allocated;
}
Exemplo n.º 16
0
/**
 * Take the order of one customer
 *
 * @param c 	Customer
 * @return Pointer to a new Order or NULL if no order
 */
static Order *cgc_take_customer_order(Customer *c) {
	Order *o = cgc_malloc(sizeof(Order));
	MALLOC_OK(o);
	cgc_memset(o, '\0', sizeof(Order));

	o->t_id = table.id;
	o->c_id = c->id;
	DBG("Table status: %U\n", table.status);
	switch(table.status) {
		case APP_RTO:	// RTO => ready to order
			o->ftype = APP_TYPE;
			o->item = cgc_select_appetizer(cgc_get_appetizer_list(), c);
			DBG("Customer %U ordering appetizer\n", c->id);
			break;
		case MEAL_RTO:
			o->ftype = MEAL_TYPE;
			o->item = cgc_select_meal(cgc_get_meal_list(), c);
			DBG("Customer %U ordering meal\n", c->id);
			break;
		case DES_RTO:
			o->ftype = DES_TYPE;
			o->item = cgc_select_dessert(cgc_get_dessert_list(), c);
			DBG("Customer %U ordering dessert\n", c->id);
			break;
		default:		// WAIT => already ordered, VACANT, FINISHED
			DBG("Customer %U NOT ordering\n", c->id);
			cgc_free(o);
			return NULL;
	}
	return o;
}
Exemplo n.º 17
0
void cgc_send_view_res(int status, message_t *msg)
{
  response_t res;
  res.status_code = status;

  if (status == STATUS_OK)
  {
    if (msg == NULL)
      goto fail;
    res.data_len = sizeof(message_t) - sizeof(char *) + msg->text_len;
    res.data = cgc_malloc(res.data_len);
    if (res.data == NULL)
      goto fail;
    cgc_memcpy(res.data, (char *)msg, sizeof(message_t) - sizeof(char *));
    cgc_memcpy(res.data + sizeof(message_t) - sizeof(char *), msg->text, msg->text_len);
  }
  else
  {
fail:
    res.data = NULL;
    res.data_len = 0;
  }

  cgc_send_response(&res, CMD_VIEW);
}
Exemplo n.º 18
0
void cgc_send_login_res(int status, user_t *user)
{
  response_t res;
  res.status_code = status;

  if (status == STATUS_OK)
  {
    if (user == NULL)
      return;
    res.data = cgc_malloc(sizeof(short) + sizeof(int));
    if (res.data == NULL)
      return;
    *((short *) res.data) = user->user_id;
    *((int *) &res.data[2]) = user->auth_code;
    res.data_len = sizeof(short) + sizeof(int);
  }
  else
  {
    res.data = NULL;
    res.data_len = 0;
  }

  cgc_send_response(&res, CMD_LOGIN);

  if (res.data)
    cgc_free(res.data);
}
Exemplo n.º 19
0
void cgc_init_trex()
{
    if (g_all_states)
        return;

    g_all_states = cgc_malloc(sizeof(state_t*) * MAX_STATES);
    g_id = 0;
}
Exemplo n.º 20
0
exp_tree_t *alloc_exptree(exp_tree_t et)
{
	exp_tree_t *p = cgc_malloc(sizeof(exp_tree_t));
	if (!p)
		fail("cgc_malloc et");
	*p = et;
	return p;
}
Exemplo n.º 21
0
error_t cgc_user_new(user_t **user)
{
    if (!user)
        return ERR_INTERNAL;
    *user = (user_t *) cgc_malloc(sizeof(user_t));
    cgc_memset(*user, 0, sizeof(user_t));
    return ERR_OK;
}
Exemplo n.º 22
0
uint_uint_pair_t *cgc_get_ui_ui_pair(unsigned int k, unsigned int v) {
	uint_uint_pair_t *pair = cgc_malloc(sizeof(uint_uint_pair_t));
	if (NULL == pair) cgc__terminate(ERRNO_ALLOC);

	pair->key = k;
	pair->value = v;

	return pair;
}
Exemplo n.º 23
0
trie* new_trie()
{
    trie* t = cgc_malloc(sizeof(trie));
    int i;
    if (!t)
        fail("trie allocation");
    t->link = cgc_malloc(10 * sizeof(trie*));
    if (!t->link)
        fail("trie links");
    t->links = 0;
    t->link_alloc = 10;
    for (i = 0; i < 10; i++)
        t->link[i] = NULL;
    t->valid_token = 0;
    for(i = 0; i < 256; i++)
        t->map[i] = NULL;
    return t;
}
Exemplo n.º 24
0
int_int_pair_t *cgc_get_i_i_pair(int k, int v) {
	int_int_pair_t *pair = cgc_malloc(sizeof(int_int_pair_t));
	if (NULL == pair) cgc__terminate(ERRNO_ALLOC);

	pair->key = k;
	pair->value = v;

	return pair;
}
Exemplo n.º 25
0
static int do_concat(interp_t *interp, expr_t *e1, expr_t *e2)
{
    char *out;
    unsigned int len = 0;
    int result = 0;
    var_t v1, v2;
    
    v1.type = v2.type = VAR_NULL;

    if (!cgc_eval_expression(interp, e1))
        goto fail;

    move_var(&v1, &interp->result);

    if (!cgc_eval_expression(interp, e2))
        goto fail;

    move_var(&v2, &interp->result);

    if (v1.type == VAR_STRING)
        len += cgc_strlen(v1.v_string.value) + 1;
    else if (v1.type == VAR_NUMBER || v1.type == VAR_NULL)
        len += 20;
    else
        goto fail;

    if (v2.type == VAR_STRING)
        len += cgc_strlen(v2.v_string.value) + 1;
    else if (v2.type == VAR_NUMBER || v2.type == VAR_NULL)
        len += 20;
    else
        goto fail;

    out = cgc_malloc(len);
    if (out == NULL)
        goto fail;

    if (v1.type == VAR_STRING)
        cgc_sprintf(out, "%s", v1.v_string.value);
    else if (v1.type == VAR_NUMBER)
        cgc_sprintf(out, "%d", v1.type == VAR_NUMBER ? v1.v_number.value : 0);
    else
        out[0] = 0;

    if (v2.type == VAR_STRING)
        cgc_sprintf(out + cgc_strlen(out), "%s", v2.v_string.value);
    else if (v2.type == VAR_NUMBER)
        cgc_sprintf(out + cgc_strlen(out), "%d", v2.type == VAR_NUMBER ? v2.v_number.value : 0);

    result = set_result_string(interp, out);

fail:
    cgc_free_var(&v1);
    cgc_free_var(&v2);
    return result;
}
Exemplo n.º 26
0
static int cgc_lcg_init(rng_t *rng)
{
    lcg_priv_t *priv;
    rng->priv = priv = cgc_malloc(sizeof(lcg_priv_t));
    if (priv == NULL)
        return FAILURE;

    priv->state = 1;
    return SUCCESS;
}
Exemplo n.º 27
0
void cgc_handle_post(const char *str)
{
    if (g_state.state == S_POST_WAIT_TOPIC)
    {
        cgc_strncpy(g_state.post.topic, str, sizeof(g_state.post.topic)-1);
        g_state.post.topic[sizeof(g_state.post.topic)-1] = 0;

        send_string("Body?\n");
        g_state.state = S_POST_WAIT_BODY;
    }
    else if (g_state.state == S_POST_WAIT_BODY)
    {
        thread_t *t = g_state.post.thread;
        if (t == NULL)
        {
            t = cgc_malloc(sizeof(thread_t));
            if (t == NULL)
                goto fail;

            cgc_strcpy(t->topic, g_state.post.topic);
            t->id = g_next_id++;
            t->next = g_threads;
            t->messages = NULL;
            g_threads = t;
        }

        message_t *m = cgc_malloc(sizeof(message_t));
        if (m)
        {
            m->id = g_next_id++;
            cgc_strcpy(m->topic, g_state.post.topic);
            m->body = cgc_strdup(str);
            if (m->body == NULL)
                goto fail;

            m->next = t->messages;
            t->messages = m;
        }

fail:
        g_state.state = S_MENU;
    }
}
Exemplo n.º 28
0
void *cgc_calloc( cgc_size_t count, cgc_size_t obj_size )
{
    cgc_size_t allocation_size = (count * obj_size);
    void *pMemBuffer;

    pMemBuffer = cgc_malloc( allocation_size );

    cgc_memset( pMemBuffer, 0, allocation_size );

    return (pMemBuffer);
}
Exemplo n.º 29
0
struct node *cgc_list_create_node(void *data) {

	struct node *np = cgc_malloc(sizeof(struct node));
	if (NULL == np) {
		return np;
	}
	np->data = data;
	np->next = 0;
	np->prev = 0;
	return np;
}
Exemplo n.º 30
0
void cgc_buffer_append(uint8_t* data, uint32_t data_len)
{
  if (cgc_buffer_len + data_len > buffer_cap)
  {
    uint8_t* new = cgc_malloc(buffer_cap * 2);
    cgc_memcpy(new, cgc_buffer, cgc_buffer_len);
    cgc_free(cgc_buffer);

    buffer_cap *= 2;
    cgc_buffer = new;
  }