Beispiel #1
0
//
// Flatten the valid iolist to the buffer of
// appropriate size pointed to by ptr
//
uint8_t *iolist_flatten(term_t l, uint8_t *ptr)
{
	if (is_nil(l))
		return ptr;

	if (is_cons(l))
	{
		do {
			uint32_t *term_data = peel_cons(l);
			term_t e = term_data[0];
			if (is_int(e))
				*ptr++ = int_value(e);
			else
			{
				assert(is_list(e) || (is_boxed(e) && is_binary(peel_boxed(e))));
				ptr = iolist_flatten(e, ptr);
			}
			l = term_data[1];
			if (is_boxed(l) && is_binary(peel_boxed(l)))
				return iolist_flatten(l, ptr);
		} while (is_cons(l));

		assert(is_nil(l));
	}
	else // is_binary()
	{
		bits_t bs, to;
		bits_get_real(peel_boxed(l), &bs);
		bits_init_buf(ptr, (bs.ends +7) /8, &to);
		ptr += (bs.ends - bs.starts) /8;
		bits_copy(&bs, &to);
		assert(bs.starts == bs.ends);
	}
	return ptr;
}
Beispiel #2
0
static int64_t iolist_size2(int depth, term_t l)
{
	if (depth > IOLIST_MAX_DEPTH)
		return -TOO_DEEP;

	if (is_nil(l))
		return 0;

	if (is_cons(l))
	{
		int64_t size = 0;
		do {
			uint32_t *term_data = peel_cons(l);
			term_t e = term_data[0];
			if (is_int(e))
			{
				if (int_value(e) < 0 || int_value(e) > 255)
					return -BAD_ARG;
				size++;
			}
			else
			{
				if (!is_list(e) && (!is_boxed(e) || !is_binary(peel_boxed(e))))
					return -BAD_ARG;
				int64_t s = iolist_size2(depth+1, e);
				if (s < 0)
					return s;
				size += s;
			}
			l = term_data[1];
			if (is_boxed(l) && is_binary(peel_boxed(l)))
			{
				// odd list with binary tail allowed
				int64_t s = iolist_size2(depth+1, l);
				if (s < 0)
					return s;
				return size +s;
			}	
		} while (is_cons(l));

		if (!is_nil(l))
			return -BAD_ARG;

		return size;
	}
	else if (is_boxed_binary(l))
	{
		bits_t bs;
		bits_get_real(peel_boxed(l), &bs);

		int64_t bit_size = bit_size = bs.ends - bs.starts;
		if ((bit_size & 7) != 0)
			return -1;

		return bit_size /8;
	}
	else
		return -BAD_ARG;
}
Beispiel #3
0
static int64_t bits_list_size2(int depth, term_t l)
{
	if (depth > BITS_LIST_MAX_DEPTH)
		return -TOO_DEEP;

	if (is_nil(l))
		return 0;

	if (is_cons(l))
	{
		int64_t size = 0;
		do {
			uint32_t *term_data = peel_cons(l);
			term_t e = term_data[0];
			if (is_int(e))
			{
				if (int_value(e) < 0 || int_value(e) > 255)
					return -BAD_ARG;
				size += 8;
			}
			else
			{
				if (!is_list(e) && (!is_boxed(e) || !is_binary(peel_boxed(e))))
					return -BAD_ARG;
				int64_t s = bits_list_size2(depth+1, e);
				if (s < 0)
					return s;
				size += s;
			}
			l = term_data[1];
			if (is_boxed(l) && is_binary(peel_boxed(l)))
			{
				// odd list with binary tail allowed
				int64_t s = bits_list_size2(depth+1, l);
				if (s < 0)
					return s;
				size += s;
				if (size > MAX_BIT_SIZE)
					return -TOO_LONG;
				return size;
			}	
		} while (is_cons(l));

		if (!is_nil(l))
			return -BAD_ARG;
		if (size > MAX_BIT_SIZE)
			return -TOO_LONG;
		return size;
	}
	else // is_binary()
	{
		bits_t bs;
		bits_get_real(peel_boxed(l), &bs);
		if (bs.ends - bs.starts > MAX_BIT_SIZE)
			return -TOO_LONG;
		return bs.ends - bs.starts;
	}
}
Beispiel #4
0
static int is_term_smaller_3(uint32_t *bin1, uint32_t *bin2)
{
	assert(is_binary(bin1) && is_binary(bin2));
	bits_t bs1, bs2;

	bits_get_real(bin1, &bs1);
	bits_get_real(bin2, &bs2);

	return (bits_compare(&bs1, &bs2) < 0);
}
Beispiel #5
0
term_t bif_open0_3(term_t FileName, term_t Mode, term_t Perms, process_t *ctx)
{
	apr_status_t rs;
	apr_pool_t *p;
	apr_file_t *file;
	port_t *port;

	if (!is_binary(FileName) || !is_int(Mode) || !is_int(Perms))
		return A_BADARG;

	apr_pool_create(&p, 0);
	rs = apr_file_open(&file, (char *)bin_data(FileName), (apr_uint32_t)int_value(Mode), (apr_uint32_t)int_value(Perms), p);
	if (rs != 0)
	{
		apr_pool_destroy(p);
		return decipher_status(rs);
	}

	port = port_file_make(file);

	//set initial port owner
	//port->owner_in = port->owner_out = proc_pid(ctx, port->xp);
	port->owner_in = port->owner_out = A_UNDEFINED;

	//put port to polling ring
	port_register(port);

	result(port_id(port, proc_gc_pool(ctx)));
	return AI_OK;
}
Beispiel #6
0
BIF_RETTYPE is_bitstring_1(BIF_ALIST_1)
{
    if (is_binary(BIF_ARG_1)) {
	BIF_RET(am_true);
    }
    BIF_RET(am_false);
}
Beispiel #7
0
BIF_RETTYPE is_binary_1(BIF_ALIST_1)
{
    if (is_binary(BIF_ARG_1) && binary_bitsize(BIF_ARG_1) == 0) {
	BIF_RET(am_true);
    }
    BIF_RET(am_false);
}
Beispiel #8
0
	DataBuffer NetGameEventValue::get_binary() const
	{
		if (is_binary())
			return value_binary;
		else
			throw Exception("NetGameEventValue is not a binary");
	}
Beispiel #9
0
Eterm enif_make_sub_binary(ErlNifEnv* env, ERL_NIF_TERM bin_term,
			   size_t pos, size_t size)
{
    ErlSubBin* sb;
    Eterm orig;
    Uint offset, bit_offset, bit_size; 
#ifdef DEBUG
    unsigned src_size;

    ASSERT(is_binary(bin_term));
    src_size = binary_size(bin_term);
    ASSERT(pos <= src_size);
    ASSERT(size <= src_size);
    ASSERT(pos + size <= src_size);   
#endif
    sb = (ErlSubBin*) alloc_heap(env, ERL_SUB_BIN_SIZE);
    ERTS_GET_REAL_BIN(bin_term, orig, offset, bit_offset, bit_size);
    sb->thing_word = HEADER_SUB_BIN;
    sb->size = size;
    sb->offs = offset + pos;
    sb->orig = orig;
    sb->bitoffs = bit_offset;
    sb->bitsize = 0;
    sb->is_writable = 0;
    return make_binary(sb);
}
Beispiel #10
0
term_t bif_rc4_init1(term_t Key, process_t *ctx)
{
	apr_byte_t *s;
	apr_byte_t i, j;
	apr_byte_t key_len;
	apr_byte_t *key_data;

	if (!is_binary(Key))
		return A_BADARG;
	s = xalloc(proc_gc_pool(ctx), 256+2);	//2 for i and j
	key_len = (apr_byte_t)int_value(bin_size(Key));
	key_data = bin_data(Key);

	i = 0;
	do {
		s[i] = i++;
	} while (i != 0);
	
	i = j = 0;
	do {
		apr_byte_t temp;
		j += key_data[i%key_len]+s[i];
		temp = s[i];
		s[i] = s[j];
		s[j] = temp;
		i++;
	} while (i != 0);

	s[256] = 0;
	s[257] = 0;

	result(make_binary(intnum(256+2), s, proc_gc_pool(ctx)));
	return AI_OK;
}
Beispiel #11
0
string convertFileName(FileName fn, size_t dim) {
	string header;
	switch (fn) {
	case FileName::AntichainsText:
	case FileName::AntichainsBinary:
		header = "data/antichains";
		break;
	case FileName::AntichainsBackupText:
	case FileName::AntichainsBackupBinary:
		header = "data/antichainsbackup";
		break;
	case FileName::ClassesText:
	case FileName::ClassesBinary:
		header = "data/classes";
		break;
	case FileName::ClassesBackupText:
	case FileName::ClassesBackupBinary:
		header = "data/classesbackup";
		break;
	default:
		assert(false);
	};

	string suffix;
	if (is_binary(fn)) {
		suffix = ".bin";
	} else {
		suffix = ".txt";
	}

	return header + to_string(dim) + suffix;
}
Beispiel #12
0
int enif_inspect_iolist_as_binary(ErlNifEnv* env, Eterm term, ErlNifBinary* bin)
{
    struct enif_tmp_obj_t* tobj;
    ErtsAlcType_t allocator;
    Uint sz;
    if (is_binary(term)) {
	return enif_inspect_binary(env,term,bin);
    }
    if (is_nil(term)) {
	bin->data = (unsigned char*) &bin->data; /* dummy non-NULL */
	bin->size = 0;
	bin->bin_term = THE_NON_VALUE;
	bin->ref_bin = NULL;
	return 1;
    }
    if (erts_iolist_size(term, &sz)) {
	return 0;
    }

    allocator = is_proc_bound(env) ? ERTS_ALC_T_TMP : ERTS_ALC_T_NIF;
    tobj = erts_alloc(allocator, sz + sizeof(struct enif_tmp_obj_t));
    tobj->allocator = allocator;
    tobj->next = env->tmp_obj_list;
    tobj->dtor = &tmp_alloc_dtor;
    env->tmp_obj_list = tobj;

    bin->data = (unsigned char*) &tobj[1]; 
    bin->size = sz;
    bin->bin_term = THE_NON_VALUE;
    bin->ref_bin = NULL;
    io_list_to_buf(term, (char*) bin->data, sz);
    ADD_READONLY_CHECK(env, bin->data, bin->size); 
    return 1;
}
Beispiel #13
0
int get_input(int *sum){
  printf("Enter a binary number:  ");

  int tmp_sum = 0;
  char ch;

  while ( (ch = getchar()) != ASCII_NEWLINE ){
    if (is_binary(ch)){
      tmp_sum <<= 1;
      tmp_sum += convert_char_to_int(ch);
    } else if (is_exit_code(ch)){
      // exit_code found so return EXIT
      return EXIT;
    } else { // bad input
      // discard the rest of the line
      while ( (ch = getchar()) != ASCII_NEWLINE );
      // return false
      return FALSE;
    }
  }

  *sum += tmp_sum;

  // keep the sum 8 bits
  while (*sum >= HIGHEST_SUM){
    *sum = *sum - HIGHEST_SUM;
  }

  // everything worked
  return TRUE;
}
void content_manager::load_file( u32 str_hash, void* data, type_name type)
{
	if( type == type_name::geometry )
	{
		if( is_binary( str_hash ) )
		{
			load_binary( str_hash, data, type );
		}
		else
		{
			geometry_file geo_file( _filelist.find( str_hash )->second, geometry_file::wavefront, _content_pool );
			geo_file.create_geometry_content( (geometry_content*)data );
		}
		return;
	}

	if( type == type_name::texture )
	{
		if( is_binary( str_hash ) )
		{
			load_binary( str_hash, data, type );
		}
		else
		{
			((texture_content*)data)->data = (crap::texture*)_content_pool->allocate( sizeof(crap::texture) );
			crap::string256 path = DATA_PATH;
			path += _filelist.find( str_hash )->second;
			*((texture_content*)data)->data = crap::create_texture( path.cstring(), crap::tga );
		}
		return;
	}

	if( type == type_name::shader )
	{
		if( is_binary( str_hash ) )
		{
			load_binary( str_hash, data, type );
		}
		else
		{
			shader_file sdr_file( _filelist.find( str_hash )->second, _content_pool );
			sdr_file.create_shader_content( (shader_content*)data );
		}
		return;
	}
}
Beispiel #15
0
term_t bif_md5_1(term_t Data, process_t *ctx)
{
	apr_byte_t *digest = xalloc(proc_gc_pool(ctx), MD5_DIGESTSIZE);
	if (!is_binary(Data))
		return A_BADARG;
	md5(digest, bin_data(Data), (apr_size_t)int_value(bin_size(Data)));
	result(make_binary(intnum(MD5_DIGESTSIZE), digest, proc_gc_pool(ctx)));
	return AI_OK;
}
Beispiel #16
0
term_t bif_md5_update2(term_t Data, term_t Context, process_t *ctx)
{
	apr_size_t size;
	md5_ctx_t *tmp;
	if (!is_binary(Data) || !is_binary(Context))
		return A_BADARG;
	if (int_value2(bin_size(Context)) != sizeof(md5_ctx_t))
		return A_BADARG;
	size = (apr_size_t)int_value(bin_size(Data));
	
	tmp = xalloc(proc_gc_pool(ctx), sizeof(*tmp));
	memcpy(tmp, bin_data(Context), sizeof(*tmp));
	
	md5_update(tmp, bin_data(Data), size);
	result(make_binary(intnum(sizeof(*tmp)),
		(apr_byte_t *)tmp, proc_gc_pool(ctx)));
	return AI_OK;
}
Beispiel #17
0
/*
 * test:
 *
 *      i)      flags   file flags
 *
 *                      "f"     [ -f path ]
 *                      "d"     [ -d path ]
 *                      "r"     [ -r path ]
 *                      "s"     [ -s path ]
 *                      "w"     [ -w path ]
 *                      "x"     [ -x path ]
 *                      "b"     [ -b path ]
 *
 *      i)      path    path
 *                      if NULL then previous path.
 *      r)              0: no, 1: ok
 *
 * You can specify more than one character. It assumed 'AND' test.
 */
int
test(const char *flags, const char *path)
{
        static struct stat sb;
        int c;

        if (path != NULL)
                if (stat(path, &sb) < 0)
                        return 0;
        while ((c = *flags++) != 0) {
                switch (c) {
                case 'b':
                        if (!is_binary(path))
                                return 0;
                        break;
                case 'f':
                        if (!S_ISREG(sb.st_mode))
                                return 0;
                        break;
                case 'd':
                        if (!S_ISDIR(sb.st_mode))
                                return 0;
                        break;
                case 'r':
                        if (access(path, R_OK) < 0)
                                return 0;
                        break;
                case 's':
                        if (sb.st_size == 0)
                                return 0;
                        break;
                case 'w':
                        if (access(path, W_OK) < 0)
                                return 0;
                        break;
                case 'x':
#ifdef _WIN32
                        /* Look at file extension to determine executability */
                        if (strlen(path) < 5)
                                return 0;
                        if (!S_ISREG(sb.st_mode))
                                return 0;
                        if (!locatestring(path, ".exe", MATCH_AT_LAST|IGNORE_CASE) &&
                                !locatestring(path, ".com", MATCH_AT_LAST|IGNORE_CASE) &&
                                !locatestring(path, ".bat", MATCH_AT_LAST|IGNORE_CASE))
                                return 0;
#else
                        if (access(path, X_OK) < 0)
                                return 0;
#endif
                        break;
                default:
                        break;
                }
        }
        return 1;
}
Beispiel #18
0
term_t bif_sendto4(term_t Sock, term_t RemIP, term_t RemPort, term_t Bin, process_t *ctx)
{
	apr_status_t rs;
	port_t *port;
	apr_socket_t *sock;
	const char *host;
	int udp_port;
	apr_sockaddr_t *sa;
	apr_pool_t *p;

	if (!is_port(Sock))
		return A_BADARG;
	if (!is_binary(RemIP) || !is_int(RemPort))
		return A_BADARG;
	if (!is_binary(Bin))
		return A_BADARG;

	port = port_lookup(prp_serial(Sock));
	if (port == 0)
		return A_CLOSED;
	if (!port->is_socket(port))
		return A_BADARG;
	sock = port->get_socket(port);

	host = (const char *)bin_data(RemIP);
	udp_port = (apr_port_t)int_value(RemPort);

	apr_pool_create(&p, 0);
	rs = apr_sockaddr_info_get(&sa, host, APR_INET, udp_port, 0, p);
	if (rs == 0)
	{
		apr_size_t len = (apr_size_t)int_value(bin_size(Bin));
		rs = apr_socket_sendto(sock, sa, 0, (const char *)bin_data(Bin), &len);
	}

	if (rs != 0)
	{
		apr_pool_destroy(p);
		return decipher_status(rs);
	}

	result(A_OK);
	return AI_OK;
}
Beispiel #19
0
// inet:getaddrs0/2 [4]
term_t bif_getaddrs0_2(term_t Addr, term_t Family, proc_t *proc)
{
	apr_status_t rs;
	apr_pool_t *tmp;
	apr_sockaddr_t *sa;
	const char *host;
	term_t addrs = nil;

	if (!is_binary(Addr) || !is_atom(Family))
		bif_bad_arg0();

	if (Family != A_INET)
		bif_exception(A_NOT_SUPPORTED);

	host = (const char *)peel(Addr)->binary.data;	//null-terminated by caller

	apr_pool_create(&tmp, 0);
	rs = apr_sockaddr_info_get(&sa, host, APR_INET, 0, 0, tmp);
	if (rs == 0)
	{
		term_t first = nil;
		term_t last = nil;

		while (sa)
		{
			struct in_addr ia = *(struct in_addr *)sa->ipaddr_ptr;
			apr_byte_t qs[4];
			term_t ip;

#if APR_IS_BIGENDIAN
			PUT32(qs, ia.s_addr);
#else
			PUT32_LE(qs, ia.s_addr);
#endif

			ip = heap_tuple4(proc->heap,
				tag_int(qs[0]),
				tag_int(qs[1]),
				tag_int(qs[2]),
				tag_int(qs[3]));

			cons_up(first, last, ip, proc->heap);

			sa = sa->next;
		}

		addrs = first;
	}

	apr_pool_destroy(tmp);

	if (rs != 0)
		bif_exception(decipher_status(rs));

	return addrs;
}
Beispiel #20
0
term_t bif_rc4_update2(term_t Text, term_t Opaque, process_t *ctx)
{
	apr_byte_t *text_data;
	apr_uint32_t text_size, k;
	apr_byte_t *s;
	apr_byte_t i, j;
	term_t Text1, Opaque1;

	if (!is_binary(Text) || !is_binary(Opaque) || bin_size(Opaque) != intnum(256+2))
		return A_BADARG;

	text_size = int_value2(bin_size(Text));
	text_data = xalloc(proc_gc_pool(ctx), text_size);
	memcpy(text_data, bin_data(Text), text_size);
	
	s = xalloc(proc_gc_pool(ctx), 256+2);
	memcpy(s, bin_data(Opaque), 256+2);

	i = s[256];
	j = s[257];

	for (k = 0; k < text_size; k++)
	{
		apr_byte_t temp;
		i++;
		j += s[i];
		temp = s[i];
		s[i] = s[j];
		s[j] = temp;

		text_data[k] ^= s[(s[i]+s[j]) & 255];
	}

	s[256] = i;
	s[257] = j;

	Text1 = make_binary(intnum(text_size), text_data, proc_gc_pool(ctx));
	Opaque1 = make_binary(intnum(256+2), s, proc_gc_pool(ctx));

	result(make_tuple2(Text1, Opaque1, proc_gc_pool(ctx)));
	return AI_OK;
}
Beispiel #21
0
term_t bif_md5_final1(term_t Context, process_t *ctx)
{
	apr_byte_t *data;
	if (!is_binary(Context) ||
			int_value2(bin_size(Context)) != sizeof(md5_ctx_t))
		return A_BADARG;
	data = xalloc(proc_gc_pool(ctx), 16);
	md5_final(data, (md5_ctx_t *)bin_data(Context));
	result(make_binary(intnum(MD5_DIGESTSIZE), data, proc_gc_pool(ctx)));
	return AI_OK;
}
Beispiel #22
0
 parse_result read_lvl(string input, int from, int lvl) {
     from = skipSpaces(input,from);
     if (from >= (int)input.length()) {
         throw "End of input reached before the parsing finished";
     }
     if (lvl <= 0) { // parens or variable
         if (input[from] == '(') {
             parse_result pr = read_lvl(input,from+1,START_LVL);
             pr.idx = skipSpaces(input, pr.idx);
             if (pr.idx >= (int)input.length()) {
                 throw "End of input reached before the parsing finished";
             } else if (input[pr.idx] != ')') {
                 throw "'(' at character "+pr.idx;
             }
             pr.idx = pr.idx+1;
             return pr;
         } else {
             return read_var(input, from);
         }
     } else {
         operateur op = operateur_for_level(lvl);
         string s_op = operateur2string(op);
         if (is_binary(op)) {
             parse_result pr1 = read_lvl(input,from,lvl-1);
             pr1.idx = skipSpaces(input,pr1.idx);
             if ( input.compare(pr1.idx, s_op.length(), s_op) == 0 ) {
                 parse_result pr2 = read_lvl(input,pr1.idx+(int)s_op.length(), lvl);
                 parse_result res;
                 res.f = new formule();
                 res.f -> op = op;
                 res.f -> arg1 = pr1.f;
                 res.f -> arg2 = pr2.f;
                 res.idx = pr2.idx;
                 return res;
             } else {
                 return pr1;
             }
         } else {
             if ( input.compare(from, s_op.length(), s_op) == 0 )  {
                 parse_result pr = read_lvl(input,from + (int)s_op.length(),lvl);
                 parse_result res;
                 res.idx = pr.idx;
                 res.f = new formule();
                 res.f->op = op;
                 res.f->arg = pr.f;
                 return res;
             } else {
                 return read_lvl(input,from,lvl-1);
             }
         }
     }
 }
Beispiel #23
0
//
// Flatten the valid bits list to the bits_t context
//
void bits_list_flatten(term_t l, bits_t *bs)
{
	if (is_nil(l))
		return;

	if (is_cons(l))
	{
		do {
			uint32_t *term_data = peel_cons(l);
			term_t e = term_data[0];
			if (is_int(e))
			{
				int o = int_value(e);
				assert(o >= 0 && o < 256);
				bits_put_octet(bs, (uint8_t)o);
			}
			else
			{
				assert(is_list(e) || (is_boxed(e) && is_binary(peel_boxed(e))));
				bits_list_flatten(e, bs);
			}
			l = term_data[1];
			if (is_boxed(l) && is_binary(peel_boxed(l)))
			{
				bits_list_flatten(l, bs);
				return;
			}
		} while (is_cons(l));

		assert(is_nil(l));
	}
	else // is_binary()
	{
		bits_t source;
		bits_get_real(peel_boxed(l), &source);
		bits_copy(&source, bs);
	}
}
Beispiel #24
0
BIF_RETTYPE iolist_to_iovec_1(BIF_ALIST_1) {
    BIF_RETTYPE result;

    if (is_nil(BIF_ARG_1)) {
        BIF_RET(NIL);
    } else if (is_binary(BIF_ARG_1)) {
        if (binary_bitsize(BIF_ARG_1) != 0) {
            ASSERT(!(BIF_P->flags & F_DISABLE_GC));
            BIF_ERROR(BIF_P, BADARG);
        } else if (binary_size(BIF_ARG_1) != 0) {
            Eterm *hp = HAlloc(BIF_P, 2);

            BIF_RET(CONS(hp, BIF_ARG_1, NIL));
        } else {
            BIF_RET(NIL);
        }
    } else if (is_internal_magic_ref(BIF_ARG_1)) {
        iol2v_state_t *state;
        Binary *magic;

        magic = erts_magic_ref2bin(BIF_ARG_1);

        if (ERTS_MAGIC_BIN_DESTRUCTOR(magic) != &iol2v_state_destructor) {
            ASSERT(!(BIF_P->flags & F_DISABLE_GC));
            BIF_ERROR(BIF_P, BADARG);
        }

        ASSERT(BIF_P->flags & F_DISABLE_GC);

        state = ERTS_MAGIC_BIN_UNALIGNED_DATA(magic);
        result = iol2v_continue(state);
    } else if (!is_list(BIF_ARG_1)) {
        ASSERT(!(BIF_P->flags & F_DISABLE_GC));
        BIF_ERROR(BIF_P, BADARG);
    } else {
        iol2v_state_t state;

        iol2v_init(&state, BIF_P, BIF_ARG_1);

        erts_set_gc_state(BIF_P, 0);

        result = iol2v_continue(&state);
    }

    if (result != THE_NON_VALUE || BIF_P->freason != TRAP) {
        erts_set_gc_state(BIF_P, 1);
    }

    BIF_RET(result);
}
Beispiel #25
0
term_t bif_open_socket2(term_t LocIP, term_t LocPort, process_t *ctx)
{
	apr_status_t rs;
	apr_pool_t *p;
	apr_sockaddr_t *sa;
	apr_socket_t *socket;
	port_t *port;
	term_t id;

	const char *host;
	apr_port_t udp_port;

	if (LocIP != A_ANY && !is_binary(LocIP))
		return A_BADARG;
	if (!is_int(LocPort))
		return A_BADARG;

	host = (LocIP == A_ANY) ?0 :(const char *)bin_data(LocIP);
	udp_port = (apr_port_t)int_value(LocPort);

	apr_pool_create(&p, 0);

	rs = apr_sockaddr_info_get(&sa, host, APR_INET, udp_port, 0, p);
	if (rs == 0)
		rs = apr_socket_create(&socket,
			APR_INET, SOCK_DGRAM, APR_PROTO_UDP, p); //only APR_INET is supported, not APR_INET6
	if (rs == 0)
		rs = apr_socket_bind(socket, sa);
	if (rs == 0)
		rs = apr_socket_opt_set(socket, APR_SO_NONBLOCK, 1);

	if (rs != 0)
	{
		apr_pool_destroy(p);
		return decipher_status(rs);
	}

	port = port_udp_make(socket);	//takes care of pool p

	//add to poll ring
	port_register(port);

	//set initial port owner
	port->owner_in = port->owner_out = proc_pid(ctx, port->xp);

	id = make_port(my_node, port->key, my_creation, proc_gc_pool(ctx));
	result(id);
	return AI_OK;
}
Beispiel #26
0
int Image::n_connected_components() const {
    if(!is_binary())
        throw std::runtime_error("The image is not binary");
    int n = 0;
    Image copy(*this);
    Color fill_color(0.5,0,0);
    for(int x=0 ; x < copy.m_width ; ++x)
    for(int y=0 ; y < copy.m_height; ++y) {
        Color c = copy.color_at(x, y);
        if(c != fill_color) {
            seed_fill_sweep(&copy, x, y, c, fill_color);
            ++n;
        }

    }
    return n;
}
Beispiel #27
0
term_t bif_write0_2(term_t Port, term_t Bin, process_t *ctx)
{
	apr_status_t rs;
	apr_size_t size;
	port_t *p;
	if (!is_port(Port) || !is_binary(Bin))
		return A_BADARG;
	p = port_lookup(prp_serial(Port));
	if (p == 0)
		return A_BADARG;
	size = (apr_size_t)int_value(bin_size(Bin));
	rs = p->write(p, bin_data(Bin), &size);
	if (rs != 0)
		return decipher_status(rs); //TODO: something may still be written
	result(intnum(size));
	return AI_OK;
}
Beispiel #28
0
/**
 * Numérote les variables d'une formule.
 * @param form La formule dont on veut numéroter les variables.
 * @param correspondance La table de correspondance.
 * @see formule
 */
void numerote(const formule *form, map<string, unsigned int> &correspondance) {
	if (form->op == o_variable) {
		if(correspondance.count(*(form->nom)) == 1) {
			return;
		}
		int num = correspondance.size() + 1;
		correspondance[*(form->nom)] = num;
		cout << "variable " << *(form->nom) << " est le n°" << correspondance[*(form->nom)] << endl;
	} else {
        if(is_binary(form->op)) {
            numerote(form->arg1, correspondance);
			numerote(form->arg2, correspondance);
        } else {
            numerote(form->arg, correspondance);
        }
	}
}
Beispiel #29
0
/**
 * Traduit une formule en sa forme conjonctive.
 * @param form La formule simplifiée à transformer.
 * @param correspondance La liste des correspondances nom<->numéro des variables de la formule.
 * @return La forme conjonctive équivalente à la formule form.
 * @see formule
 * @see forme_conjonctive
 * @see simplifie_formule(const formule*, const bool)
 */
forme_conjonctive trad_forme_conjonctive(const formule *form, map<string, unsigned int> &correspondance) {
	forme_conjonctive fc_out, fc1, fc2;
	if(is_binary(form->op)) {
		fc1 = trad_forme_conjonctive(form->arg1, correspondance);
		fc2 = trad_forme_conjonctive(form->arg2, correspondance);
	}
	switch(form->op) {
		case o_variable:
		{
			clause cl;
			cl.push_back(correspondance[*(form->nom)]);
			fc_out.push_back(cl);
			break;
		}

		case o_non:
		{
			clause cl;
			cl.push_back(-correspondance[*(form->arg->nom)]);
			fc_out.push_back(cl);
			break;
		}

		case o_ou:
		{
			forme_conjonctive::const_iterator it1, it2;
			clause cl;

			for(it1=fc1.begin(); it1!=fc1.end(); it1++) {
				for(it2=fc2.begin(); it2!=fc2.end(); it2++) {
					cl.clear();
					set_union(it1->begin(), it1->end(), it2->begin(), it2->end(), insert_iterator<clause>(cl, cl.begin()));
					fc_out.push_back(cl);
				}
			}
			break;
		}

		case o_et:
		{
			set_union(fc1.begin(), fc1.end(), fc2.begin(), fc2.end(), insert_iterator<forme_conjonctive>(fc_out, fc_out.begin()));
			break;
		}
	}
	return fc_out;
}
Beispiel #30
0
BIF_RETTYPE size_1(BIF_ALIST_1)
{
    if (is_tuple(BIF_ARG_1)) {
	Eterm* tupleptr = tuple_val(BIF_ARG_1);

	BIF_RET(make_small(arityval(*tupleptr)));
    } else if (is_binary(BIF_ARG_1)) {
	Uint sz = binary_size(BIF_ARG_1);
	if (IS_USMALL(0, sz)) {
	    return make_small(sz);
	} else {
	    Eterm* hp = HAlloc(BIF_P, BIG_UINT_HEAP_SIZE);
	    BIF_RET(uint_to_big(sz, hp));
	}
    }
    BIF_ERROR(BIF_P, BADARG);
}