Exemplo n.º 1
0
bloomy_t* bloomy_new(size_t size, hash_t hash1, hash_t hash2, hash_t hash3)
{
  bloomy_t *bloomy;

  if (size < 1)
    return NULL;

  bloomy = (bloomy_t *) malloc(sizeof(bloomy_t));
  if (bloomy == NULL)
    goto fail;
  cgc_memset(bloomy, 0, sizeof(bloomy_t));
  bloomy->size = size;

  bloomy->bits = (uint8_t *) malloc(size);
  if (bloomy->bits == NULL)
    goto fail;
  cgc_memset(bloomy->bits, 0, size);

  bloomy->hashes[0] = hash1;
  bloomy->hashes[1] = hash2;
  bloomy->hashes[2] = hash3;

  return bloomy;

fail:
  bloomy_free(bloomy);
  return NULL;
}
Exemplo n.º 2
0
// initialize game data
void init_game_data(tt_data * data) {
	cgc_memset(data->top, EMPTY, 3);
	cgc_memset(data->middle, EMPTY, 3);
	cgc_memset(data->bottom, EMPTY, 3);
	data->moves = 0;
	data->winner = EMPTY;
}
Exemplo n.º 3
0
void cgc_handle_decompress()
{
    int i;
    unsigned char buf[95], *out = NULL;
    unsigned char data[MAX_DATA_SIZE];
    cgc_size_t outlen = 0, len = 0;
    sc_obj_t *sc = NULL;

    cgc_printf("Key?\n");
    if (cgc_fread(buf, sizeof(buf), cgc_stdin) != sizeof(buf))
        goto fail;

    cgc_memset(data, 0, sizeof(data));
    for (i = 0; i < sizeof(buf); ++i)
    {
        if (buf[i] < 32 || buf[i] > 126)
            goto fail;
        if (data[buf[i]]++ > 0)
            goto fail;
    }

    cgc_printf("Length?\n");
    cgc_memset(data, 0, sizeof(data));
    cgc_fflush(cgc_stdout);
    if (cgc_freaduntil((char *) data, sizeof(data), '\n', cgc_stdin) < 0)
        goto fail;
    len = cgc_strtoul((char *) data, NULL, 10);
    if (len > MAX_DATA_SIZE)
        goto fail;

    cgc_printf("Data?\n");
    cgc_memset(data, 0, sizeof(data));
    if (cgc_fread((char *) data, len, cgc_stdin) < 0)
        goto fail;

    sc = cgc_sc_new(buf);
    sc->data = data;
    sc->data_len = len;
    if (cgc_sc_sdecompress(sc, &out, &outlen) < 0)
        goto fail;
    cgc_printf("Compressed Size: %d\n", sc->data_len);
    cgc_printf("Original Size: %d\n", cgc_strlen((char *) out));
    cgc_printf("Original Data: ");
    cgc_fwrite(out, outlen, cgc_stdout);
    cgc_fwrite("\n", 1, cgc_stdout);
    goto done;

fail:
    cgc_printf("error.\n");
done:
    if (sc)
        cgc_free(sc);
    if (out)
        cgc_free(out);
}
Exemplo n.º 4
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.º 5
0
void FlushOutput() {
  printf("@s\n", line);
  cgc_memset(line, '\0', sizeof(line));
  if (in_a_box) {
    cgc_memset(line, ' ', sizeof(line) - 1);
    line[0] = '*';
    line[79] = '*';
    line_length = 2;
  } else {
    line_length = 0;
  }
}
Exemplo n.º 6
0
void cgc_handle_compress()
{
    int i;
    unsigned char buf[95], *out = NULL;
    unsigned char data[MAX_DATA_SIZE];
    cgc_size_t outlen = 0;
    sc_obj_t *sc = NULL;

    cgc_printf("Key?\n");
    if (cgc_fread(buf, sizeof(buf), cgc_stdin) != sizeof(buf))
        goto fail;

    cgc_memset(data, 0, sizeof(data));
    for (i = 0; i < sizeof(buf); ++i)
    {
        if (buf[i] < 32 || buf[i] > 126)
            goto fail;
        if (data[buf[i]]++ > 0)
            goto fail;
    }

    cgc_printf("Data?\n");
    cgc_memset(data, 0, sizeof(data));
    cgc_fflush(cgc_stdout);
    if (cgc_freaduntil((char *) data, sizeof(data), '\0', cgc_stdin) < 0)
        goto fail;

    sc = cgc_sc_new(buf);
    sc->data = data;
    sc->data_len = cgc_strlen((char *)data);
    if (cgc_sc_scompress(sc, &out, &outlen) < 0)
        goto fail;
    cgc_printf("Original Size: %d\n", sc->data_len);
    cgc_printf("Compressed Size: %d (%d%%)\n", outlen, ((int) ((outlen / (sc->data_len * 1.0)) * 100)));
    cgc_printf("Compressed Data: ");
    for (i = 0; i < outlen && i < 32; ++i)
        cgc_printf("%02X", out[i]);
    cgc_printf("\n");

    goto done;

fail:
    cgc_printf("error.\n");
done:
    if (sc)
        cgc_free(sc);
    if (out)
        cgc_free(out);
}
Exemplo n.º 7
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.º 8
0
void cgc_table_setup(void) {
	cgc_memset(&table, '\0', sizeof(Table));
	table.id = TABLE_ID;
	table.status = VACANT;
	table.seats = cgc_calculate_seats_count();
	table.customers = NULL;
}
Exemplo n.º 9
0
int main(void) {

    int ret = SUCCESS;

    // Get op (1st byte), dispatch to function.
    while(1) {

        rx_bytes = 0;
        cgc_memset(rx_buf, 0, BUF_RX_SZ);

        if (SUCCESS != (ret = receive_all(STDIN, (void *)&rx_buf, BUF_RX_SZ, &rx_bytes))) { 
#ifdef DEBUG
            fprintf(stderr, "[E] main | during receive_all()\n");
#endif
            ret = ERRNO_RECV;
            goto _bail_main;
        }

        // "zero indicates nothing was received or end-of-file"
        // Not sending any more calculations is a way to cgc_exit the calculator.
        if (0 == rx_bytes) {
#ifdef DEBUG
            fprintf(stderr, "[E] main | got no bytes; bailing...\n", rx_bytes);
#endif
            goto _bail_main;
        }

        dispatch();
    }

_bail_main:
    return ret;
}  
Exemplo n.º 10
0
int main(void) {
    char buf[1024];
    int ret;

    do_config();
    setup();
    sleep(2);

    while (1) {
        cgc_memset(buf, 0, sizeof(buf));
        ret = read_until(read_fd, buf, sizeof(buf), '\n');
        if (ret == -1)
            cgc_exit(4);

        if (ret > 0) {

            if (buf[0] == 0) {
                cgc_exit(0);
            }

            ret = search(buf, cgc_strlen(buf));
            if (ret != 1) {
                printf(write_fd, "%s\n", buf);
            }
        }
    }
    cgc_exit(1);
}
Exemplo n.º 11
0
int main(void)
{
  unsigned size = 0;
  size_t cgc_read = 0;
  uint8_t buf[MAX_SIZE];
  cgc_memset(buf, 0, sizeof(buf));

  if (receive(STDIN, &size, sizeof(size), &cgc_read) != 0)
    cgc_exit(1);

  if (cgc_read != 4)
    cgc_exit(1);

  if (size > MAX_SIZE) {
    printf("too big\n");
    cgc_exit(1);
  }

  if (read_exactly(STDIN, buf, size) != 0)
    cgc_exit(1);

  element *e = decode(buf, (unsigned) buf + size);
  if (e == NULL)
    cgc_exit(1);

  pprint(e);
}
Exemplo n.º 12
0
char *word_list_to_str(const list *l)
{
    if (!l)
        return NULL;

#define START_SIZE 128
    char *flat = calloc(1, START_SIZE);
    size_t cur_size = START_SIZE;

    if (!flat)
        error(EALLOC);

    const list *p = l;

    while (p && p->d) {
        size_t cur_len = cgc_strlen(flat);
        if (cgc_strlen(flat) + cgc_strlen(p->d) + cgc_strlen(word_sep) + 1 > cur_size) {
            cur_size = (cgc_strlen(flat) + cgc_strlen(p->d) + cgc_strlen(word_sep) + 1) * 2;
            flat = realloc(flat, cur_size);
            if (!flat)
                error(EALLOC);

            cgc_memset(flat + cur_len, 0, cur_size - cur_len);
        }

        strcat(flat, p->d);
        strcat(flat, word_sep);
        p = p->n;
    }

    return flat;
}
Exemplo n.º 13
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.º 14
0
void create_resp_pkt(tfttp_pkt* pkt, char *resp) {
    cgc_memset(pkt,'\0', TFTTP_STRUCT_SIZE); 
    strcpy((char *)pkt->data, resp);
    pkt->hdr.code = RESP;
    //for now, fixed length responses until pollers can support otherwise
    //pkt->size = sizeof(enum pkttype)+cgc_strlen(resp)+1; 
    pkt->hdr.size = 256; //-_-
}
Exemplo n.º 15
0
static void gf_poly_mul(poly_t *dst, const poly_t *a, const poly_t *b)
{
    dst->degree = a->degree + b->degree - 1;
    cgc_memset(dst->terms, 0, dst->degree);

    for (int i = 0; i < a->degree; i++)
    for (int j = 0; j < b->degree; j++)
        dst->terms[i + j] ^= gf_mul(a->terms[i], b->terms[j]);
}
Exemplo n.º 16
0
error_t cgc_user_set_nick(user_t *user, const char *nick)
{
    if (!user || !nick)
        return ERR_INTERNAL;
    if (cgc_strlen(nick) >= sizeof(user->nick))
        return ERR_NICK_LONG;
    cgc_memset(user->nick, 0, sizeof(user->nick));
    cgc_strcpy(user->nick, nick);
    return ERR_OK;
}
Exemplo n.º 17
0
error_t cgc_user_set_pass(user_t *user, const char *pass)
{
    if (!user || !pass)
        return ERR_INTERNAL;
    if (cgc_strlen(pass) >= sizeof(user->pass))
        return ERR_PASS_LONG;
    cgc_memset(user->pass, 0, sizeof(user->pass));
    cgc_strcpy(user->pass, pass);
    return ERR_OK;
}
Exemplo n.º 18
0
error_t cgc_team_change_name(team_t *team, const char *name)
{
    if (!team || !name)
        return ERR_INTERNAL;
    if (cgc_strlen(name) >= sizeof(team->name))
        return ERR_NAME_LONG;
    cgc_memset(team->name, 0, sizeof(team->name));
    cgc_strcpy(team->name, name);
    return ERR_OK;
}
Exemplo n.º 19
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.º 20
0
void *calloc(size_t nmemb, size_t size)
{
    uint64_t total = (uint64_t)nmemb * size;
    if (total > UINT32_MAX)
        return NULL;

    void *ret = malloc((size_t)total);
    if (ret == NULL)
        return NULL;
    cgc_memset(ret, 0, (size_t)total);
    return ret;
}
Exemplo n.º 21
0
/*
 * Database
 */
int InitializeSignatureDatabase(signature_db* SignatureDatabase)
{
  if (!SignatureDatabase)
    return -1;

  SignatureDatabase->SignatureCount = 0;
  cgc_memset(SignatureDatabase->Signatures, 0, sizeof(signature *) * MAX_SIGNATURES);

  SignatureDatabase->Trie = NULL;
  SignatureDatabase->SearchMachine = xcalloc(1, sizeof(search_machine));

  return 0;
}
Exemplo n.º 22
0
int delete_message(uint8_t msg_num)
{
  if (msg_num >= APPLICATION_MAX_MESSAGES)
  {
    return -1;
  }
  cgc_memset(APPLICATION_MSG_BOARD[msg_num], 0, APPLICATION_MSG_LENGTH);
  for (int i = msg_num; i < APPLICATION_MAX_MESSAGES - 1; i++)
  {
    cgc_memcpy(APPLICATION_MSG_BOARD[i], APPLICATION_MSG_BOARD[i+1], APPLICATION_MSG_LENGTH);
  }
  numMessages--;
  return 1;
}
Exemplo n.º 23
0
static int
xor_login(char *name)
{
    size_t secret_size = sizeof(secret) - 1;
    size_t token_size, sig_size, signed_token_size;
    char *token, *signed_token;
    char sig[secret_size];

    if ((token = make_token(name)) == NULL)
        return -1;

    token_size = cgc_strlen(token);
    signed_token_size = token_size + 2 * secret_size + 2;

    if ((signed_token = realloc(token, signed_token_size)) == NULL) {
        free(token);
        return -1;
    }

    if ((sig_size = xor_sig(signed_token, token_size, sig)) == 0) {
        free(signed_token);
        return -1;
    }

    signed_token[token_size] = '|';
    bin_to_hex(signed_token + token_size + 1, sig, sig_size);
    signed_token[signed_token_size - 1] = '\n';

    if (write_all(STDOUT, signed_token, signed_token_size) != signed_token_size)
        return -1;

    cgc_memset(sig, '\x00', sig_size);
    cgc_memset(signed_token, '\x00', signed_token_size);
    free(signed_token);

    return 0;
}
Exemplo n.º 24
0
int play_ttt(int turn)
{
    unsigned char len_resp[128];
    //unsigned int resp_len = 0;
    //unsigned char *resp = NULL;
    char *select_guess_num_game = "3\n";

    cgc_memset(len_resp, 0, 128);
    read_until_game_prompt();
    length_read(STDIN, len_resp, cgc_strlen("Game # "));
    transmit_all(STDOUT, select_guess_num_game, cgc_strlen(select_guess_num_game));

    if (turn == COMPUTER) {
        switch(find_corner()) {
        case 0:
            transmit_all(STDOUT, "1,1\n", 4);
            transmit_all(STDOUT, "0,2\n", 4);
            transmit_all(STDOUT, "1,0\n", 4);
            transmit_all(STDOUT, "2,2\n", 4);
            return 0;
        case 1:
            transmit_all(STDOUT, "1,1\n", 4);
            transmit_all(STDOUT, "0,1\n", 4);
            transmit_all(STDOUT, "1,0\n", 4);
            transmit_all(STDOUT, "2,2\n", 4);
            return 1;
        case 2:
            transmit_all(STDOUT, "1,1\n", 4);
            transmit_all(STDOUT, "1,0\n", 4);
            transmit_all(STDOUT, "2,2\n", 4);
            transmit_all(STDOUT, "0,2\n", 4);
            return 2;
        case 3:
            transmit_all(STDOUT, "1,1\n", 4);
            transmit_all(STDOUT, "2,1\n", 4);
            transmit_all(STDOUT, "0,2\n", 4);
            transmit_all(STDOUT, "1,0\n", 4);
            return 3;
        }
    } else {
        transmit_all(STDOUT, "2,0\n", 4);
        transmit_all(STDOUT, "2,1\n", 4);
        transmit_all(STDOUT, "0,0\n", 4);
        transmit_all(STDOUT, "1,2\n", 4);
        transmit_all(STDOUT, "0,2\n", 4);
        return 5;
    }
    return -1;
}
Exemplo n.º 25
0
void init_db()
{
    int i;
    record rec;
    cgc_memset(&rec, 0, sizeof(rec));

    for (i = 0; i < sizeof(g_items) / sizeof(g_items[0]); i++)
    {
        rec.k.data.count = cgc_strlen(g_items[i].name) + 1;
        rec.k.data.data = (opaque *)g_items[i].name;
        rec.data.count = sizeof(g_items[i].details);
        rec.data.data = (opaque *)&g_items[i].details;
        db_insert(rec);
    }
}
Exemplo n.º 26
0
void *calloc(size_t count, size_t size)
{
    size_t n = count * size;
    void *ptr;

    if ((uintmax_t)count * size > (uintmax_t)SIZE_MAX)
        return NULL;

    ptr = malloc_alloc(&g_heap, n);
    if (ptr == NULL)
        return NULL;

    cgc_memset(ptr, 0, n);
    return ptr;
}
Exemplo n.º 27
0
int play_rps()
{
    unsigned char len_resp[128];
    unsigned int resp_len = 0;
    unsigned char *resp = NULL;
    char *select_guess_num_game = "1\n";
    char *hammer = "HAMMER\n";

    cgc_memset(len_resp, 0, 128);
    read_until_game_prompt();
    length_read(STDIN, len_resp, cgc_strlen("Game # "));
    transmit_all(STDOUT, select_guess_num_game, cgc_strlen(select_guess_num_game));

    cgc_memset(len_resp, 0, 128);
    length_read(STDIN, len_resp, cgc_strlen("Choose [HAMMER, SHEET, SHEERS]: "));
    transmit_all(STDOUT, hammer, cgc_strlen(hammer));
    delimited_read(STDIN, &resp, &resp_len, (unsigned char *)"\n", 1);
    delimited_read(STDIN, &resp, &resp_len, (unsigned char *)"\n", 1);
    if(memcmp(resp, "You Win!", 8) == 0) {
        return 0;
    }

    return 1;
}
Exemplo n.º 28
0
int PMPGenerate(PMP_File *pmp, Canvas *c) {
  int row_pad = 0;
  int odd_bytes = (c->x_size * 3) % 4;
  if (odd_bytes != 0) {
    row_pad = 4 - odd_bytes;
  }

  pmp->header.magic = 0x4d50;
  pmp->header.reserved = 0;
  pmp->header.header_size = sizeof(PMP_Info) + sizeof(PMP_Header);
  pmp->header.filesize = pmp->header.header_size + c->y_size * (c->x_size * 3 + row_pad);

  int file_pad = 0;
  odd_bytes = pmp->header.filesize % 4;
  if (odd_bytes != 0) {
    file_pad = 4 - odd_bytes;
    pmp->header.filesize = pmp->header.filesize + file_pad;
  }

  pmp->data_size = c->y_size * (c->x_size * 3 + row_pad) + file_pad;
  if (allocate(pmp->data_size, 0, (void **)&pmp->data) != 0) {
    _terminate(-1);
  } 
  cgc_memset(pmp->data, 0, pmp->data_size); 

  pmp->info.size = sizeof(PMP_Info);
  pmp->info.y_size = c->y_size;
  pmp->info.x_size = c->x_size;
  pmp->info.magic1 = 0x00180001;
  pmp->info.reserved1 = 0;
  pmp->info.image_size = c->y_size * (c->x_size * 3 + row_pad);
  pmp->info.magic2 = 0xb13;
  pmp->info.magic3 = 0xb13;
  pmp->info.reserved2 = 0;
  pmp->info.reserved3 = 0;

  char *data = pmp->data;
  int x, y;
  for (y = c->y_size - 1; y >= 0; y--) {
    for (x = 0; x < c->x_size; x++) {
      RGB_Color *color = GetColor(c, y, x, 0);
      cgc_memcpy(data, color, sizeof(RGB_Color));
      data += sizeof(RGB_Color);
    }
    data += row_pad;
  }
  return 0;
}
Exemplo n.º 29
0
char* strdup(char *s)
{
  int sz = 0;
  if (s)
  {
    sz = cgc_strlen(s) + 1;
    char *d = malloc(sz);
    if (d)
    {
      cgc_memset(d, 0, sz);
      cgc_memcpy(d, s, sz);
      return d;
    }
  }
  return NULL;
}
Exemplo n.º 30
0
int main() {
	int numInstructions;
	int recvd;
	int i;
	pruCPU cpu;
	cpu.numExecuted = 0;
	cgc_memset(cpu.code, 0xff, 0x4000);
	numInstructions = recvInt();
	for(i=0;i<numInstructions;i++) {
		cpu.code[i] = recvInt();
	}
	execute(&cpu);
	dumpState(&cpu);
	_terminate(0);

}