static bool decode_visit_dbpointer(const bson_iter_t *iter, const char *key, size_t v_collection_len, const char *v_collection, const bson_oid_t *v_oid, void *data) { decode_state *ds = data; ERL_NIF_TERM col, oid, out; if(!make_binary(ds->env, &col, v_collection, v_collection_len)) { return true; } if(v_oid) { if(!make_binary(ds->env, &oid, v_oid->bytes, 12)) { return true; } } else { oid = ds->st->atom_null; } out = enif_make_tuple4( ds->env, ds->st->atom_s_ref, col, ds->st->atom_s_oid, oid), vec_push(ds->vec, out); return false; }
static ERL_NIF_TERM make_error(ErlNifEnv* env, yaml_parser_t *parser) { ERL_NIF_TERM err; switch (parser->error) { case YAML_MEMORY_ERROR: err = enif_make_atom(env, "memory_error"); break; case YAML_PARSER_ERROR: err = enif_make_tuple4(env, enif_make_atom(env, "parser_error"), make_binary(env, (const unsigned char*) parser->problem), enif_make_uint(env, parser->problem_mark.line), enif_make_uint(env, parser->problem_mark.column)); break; case YAML_SCANNER_ERROR: err = enif_make_tuple4(env, enif_make_atom(env, "scanner_error"), make_binary(env, (const unsigned char*) parser->problem), enif_make_uint(env, parser->problem_mark.line), enif_make_uint(env, parser->problem_mark.column)); break; default: err = enif_make_atom(env, "unexpected_error"); break; } return enif_make_tuple2(env, enif_make_atom(env, "error"), err); }
//find all parents of profile in graph; profile has only freq helices //transitive reduction to the rescue again KEY* find_parents(char *profile) { int i,b1,b2,N,num = 0,k = 0; HASHTBL *hash; KEY *node,*parent,*begin = NULL; begin = malloc(sizeof(KEY)); begin->data = ""; begin->next = NULL; b2 = make_binary(profile,&num); //printf("finding parents for %s of length %d and binary %d\n",profile,num,b2); for (i = 0; i < num-1; i++) { if (!(hash = graph[i])) continue; for (node = hashtbl_getkeys(hash); node; node = node->next) { b1 = make_binary(node->data,&k); //printf("investigating %sof size %d with binary %d\n",node->data,i+1,b1); N = b1 & b2; if (N == b1) { //printf("found parent %sof size %d with binary %d\n",node->data,i+1,b1); parent = malloc(sizeof(KEY)); parent->data = node->data; parent->next = begin; begin = parent; } } } return begin; }
//finds helical difference between profile -> origprof //k1 and k2 are num of helices in profile and origprof //return difference with triplets char* find_diff(HASHTBL *hash,char *profile, char *origprof, int *k1, int *k2) { int size = INIT_SIZE,b1,b2,xor,i; char *diff,*id; diff = malloc(sizeof(char)*ARRAYSIZE*size); diff[0] = '\0'; b1 = make_binary(profile,k1); b2 = make_binary(origprof,k2); xor = b1 ^ b2; //printf("b1 is %d, b2 is %d, and xor is %d\n",b1,b2,xor); for (i = 0; xor > 0; xor>>=1,i++) { if ((xor & 1)==1) { hashtbl_insert(hash,table[i],"1"); id = hashtbl_get(idhash,table[i]); if (strlen(diff)+strlen(table[i])+strlen(id)+6 > ARRAYSIZE*size) diff = resize(&size,strlen(diff)+strlen(table[i])+strlen(id)+6,diff); if (strlen(diff)>1) strcat(diff,"\\n"); sprintf(diff,"%s%s: %s",diff,table[i],id); } } //printf("diff is %s between %s and %s\n",diff,origprof,profile); return diff; }
static bool decode_visit_regex(const bson_iter_t *iter, const char *key, const char *v_regex, const char *v_options, void *data) { decode_state *ds = data; ERL_NIF_TERM regex, options, out; if(!make_binary(ds->env, ®ex, v_regex, strlen(v_regex))) { return true; } if(!make_binary(ds->env, &options, v_options, strlen(v_options))) { return true; } out = enif_make_tuple4(ds->env, ds->st->atom_s_regex, regex, ds->st->atom_s_options, options); vec_push(ds->vec, out); return false; }
exprt dereferencet::dereference_plus( const exprt &expr, const exprt &offset, const typet &type) { if(expr.operands().size()>2) return dereference_rec(make_binary(expr), offset, type); // binary exprt pointer=expr.op0(), integer=expr.op1(); if(ns.follow(integer.type()).id()==ID_pointer) std::swap(pointer, integer); // multiply integer by object size exprt size=size_of_expr(pointer.type().subtype(), ns); if(size.is_nil()) throw "dereference failed to get object size for pointer arithmetic"; // make types of offset and size match if(size.type()!=integer.type()) integer.make_typecast(size.type()); exprt new_offset=plus_exprt(offset, mult_exprt(size, integer)); return dereference_rec(pointer, new_offset, type); }
term_t bif_read0_2(term_t Port, term_t Len, process_t *ctx) { apr_status_t rs; apr_size_t size; apr_byte_t *buf; term_t bin; port_t *p; if (!is_port(Port) || !is_int(Len)) return A_BADARG; p = port_lookup(prp_serial(Port)); if (p == 0) return A_BADARG; size = (apr_size_t)int_value(Len); buf = xalloc(proc_gc_pool(ctx), size); rs = p->read(p, buf, &size); if (size == 0 && APR_STATUS_IS_EOF(rs)) result(A_EOF); else if (size == 0 && rs != 0) return decipher_status(rs); else { bin = make_binary(intnum(size), buf, proc_gc_pool(ctx)); result(bin); } return AI_OK; }
apr_status_t port_socket_close0(port_t *self) { port_socket_data_t *data = self->data; process_t *proc = proc_lookup(pid_serial(self->owner_in)); if (proc) { xpool_t *tmp = xpool_make(self->pool); int len = buffer_len(data->in_buf); term_t msg; if (len > 0) { term_t bin = make_binary(intnum(len), buffer_ptr(data->in_buf), tmp); msg = make_tuple3(A_TCP, port_id(self, tmp), bin, tmp); proc_new_mail(proc, msg); buffer_clear(data->in_buf); } msg = make_tuple2(A_TCP_CLOSED, port_id(self, tmp), tmp); proc_new_mail(proc, msg); xpool_destroy(tmp); } return apr_socket_close(data->sock); }
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); }
static bool decode_visit_codewscope(const bson_iter_t *iter, const char *key, size_t v_code_len, const char *v_code, const bson_t *v_scope, void *data) { decode_state *ds = data; decode_state cs; ERL_NIF_TERM code, scope, out; if(!make_binary(ds->env, &code, v_code, v_code_len)) { return true; } init_child_state(ds, &cs); cs.depth = ds->depth; cs.keys = true; if(!iter_bson(v_scope, &scope, &cs)) { return true; } out = enif_make_tuple4( ds->env, ds->st->atom_s_javascript, code, ds->st->atom_s_scope, scope); vec_push(ds->vec, out); return false; }
term_t bif_embedded_module1(term_t Mod, process_t *ctx) { xmod_bin_t *mp, *me; cstr_t *s; if (!is_atom(Mod)) return A_BADARG; s = atoms_get(proc_atoms(ctx), index(Mod)); mp = (xmod_bin_t *)module_bins->elts; me = mp + module_bins->nelts; while (mp < me) { if (scomp(mp->name, s)) { term_t bin = make_binary(intnum(mp->size), mp->data, proc_gc_pool(ctx)); result(make_tuple3(A_OK, bin, bool(mp->is_preloaded), proc_gc_pool(ctx))); return AI_OK; } mp++; } result(A_FALSE); return AI_OK; }
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; }
static ERL_NIF_TERM mruby2erl(ErlNifEnv* env, mrb_state* mrb, mrb_value value) { if (mrb_nil_p(value)) { return enif_make_atom(env, "nil"); } else { switch(value.tt) { case MRB_TT_TRUE: return enif_make_atom(env, "true"); case MRB_TT_FALSE: return enif_make_atom(env, "false"); case MRB_TT_SYMBOL: return enif_make_atom(env, _mrb_symbol(mrb, value)); case MRB_TT_FIXNUM: return enif_make_int(env, _mrb_fixnum(value)); case MRB_TT_FLOAT: return enif_make_double(env, _mrb_float(value)); case MRB_TT_STRING: return make_binary(env, _mrb_string(mrb, value)); case MRB_TT_ARRAY: return make_array(env, mrb, value); case MRB_TT_HASH: return make_hash(env, mrb, value); default : return enif_make_atom(env, "nil"); } } }
//TODO: change this whole thing to use iterators instead of accessing elements //encodes a string into binary representation and then prints //both the binary out and the tree structure out to the screen void encode(const std::string& value) { //traverse through the string and find every unique element and put it //in a vector of Nodes, if there is already a Node with the same key //increment the frequency of the of the key std::vector<Node*> table; for(int x = 0; x < static_cast<int>(value.size()); ++x) { //change the value in the iterator to a character char c = value[x]; //get a vector iterator from the has_key function unsigned int table_it = has_key(table, c); //if the key isn't in the table add it into the table if(table_it == table.size()) { table.push_back(new Node(c)); continue; } //increment the frequency of the key if the key is found table[table_it]->frequency += 1; } //put all the nodes into a min heap and pop off the nodes to create //binary trees. Make each parent node have an '*' as the key Heap m_heap(table); int num_nodes = m_heap.heap_size; while(num_nodes > 0) { Node* parent = new Node(m_heap.pop(), m_heap.pop(), '*'); m_heap.push(parent); num_nodes = m_heap.heap_size; } //traverse the tree, every time going to the left append a '0' to the binary //every time going to the right append a '1' to the binary. Once a leaf node is reached //look up the key in the vector table and put the binary in the node with the key Node* tree = m_heap.pop(); make_binary(tree, table); //traverse the initial string, at every character look up the key in the vector and print out the //binary for that key print(value, table); //do a preorder traversal of the binary tree and print out the characters tree_print(tree); std::cout << '\n'; }
int main() { int len = rand() % 16; printf("len : %d\n", len); char * str; str = make_binary(len); printf("str : %s\n", str); }
// // MAKE_String: C // void MAKE_String(REBVAL *out, enum Reb_Kind kind, const REBVAL *def) { REBSER *ser; // goto would cross initialization if (IS_INTEGER(def)) { // // !!! R3-Alpha tolerated decimal, e.g. `make string! 3.14`, which // is semantically nebulous (round up, down?) and generally bad. // ser = Make_Binary(Int32s(def, 0)); Val_Init_Series(out, kind, ser); return; } else if (IS_BLOCK(def)) { // // The construction syntax for making strings or binaries that are // preloaded with an offset into the data is #[binary [#{0001} 2]]. // In R3-Alpha make definitions didn't have to be a single value // (they are for compatibility between construction syntax and MAKE // in Ren-C). So the positional syntax was #[binary! #{0001} 2]... // while #[binary [#{0001} 2]] would join the pieces together in order // to produce #{000102}. That behavior is not available in Ren-C. if (VAL_ARRAY_LEN_AT(def) != 2) goto bad_make; RELVAL *any_binstr = VAL_ARRAY_AT(def); if (!ANY_BINSTR(any_binstr)) goto bad_make; if (IS_BINARY(any_binstr) != LOGICAL(kind == REB_BINARY)) goto bad_make; RELVAL *index = VAL_ARRAY_AT(def) + 1; if (!IS_INTEGER(index)) goto bad_make; REBINT i = Int32(index) - 1 + VAL_INDEX(any_binstr); if (i < 0 || i > cast(REBINT, VAL_LEN_AT(any_binstr))) goto bad_make; Val_Init_Series_Index(out, kind, VAL_SERIES(any_binstr), i); return; } if (kind == REB_BINARY) ser = make_binary(def, TRUE); else ser = MAKE_TO_String_Common(def); if (!ser) goto bad_make; Val_Init_Series_Index(out, kind, ser, 0); return; bad_make: fail (Error_Bad_Make(kind, def)); }
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; }
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; }
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; }
term_t bif_md5_init0(process_t *ctx) { term_t md5; // md5_ctx_t is safe for Erlang gc etc md5_ctx_t *data = xalloc(proc_gc_pool(ctx), sizeof(md5_ctx_t)); md5_init(data); md5 = make_binary(intnum(sizeof(md5_ctx_t)), (apr_byte_t *)data, proc_gc_pool(ctx)); result(md5); return AI_OK; }
term_t bif_sha1_init0(process_t *ctx) { term_t sha1; // sha1_ctx_t is safe for Erlang gc etc sha1_ctx_t *data = xalloc(proc_gc_pool(ctx), sizeof(sha1_ctx_t)); sha1_init(data); sha1 = make_binary(intnum(sizeof(sha1_ctx_t)), (apr_byte_t *)data, proc_gc_pool(ctx)); result(sha1); return AI_OK; }
// // TO_String: C // void TO_String(REBVAL *out, enum Reb_Kind kind, const REBVAL *arg) { REBSER *ser; if (kind == REB_BINARY) ser = make_binary(arg, FALSE); else ser = MAKE_TO_String_Common(arg); if (!ser) fail (Error_Invalid_Arg(arg)); Val_Init_Series(out, kind, ser); }
static ERL_NIF_TERM make_cell(ErlNifEnv *env, sqlite3_stmt *statement, unsigned int i) { int type = sqlite3_column_type(statement, i); switch(type) { case SQLITE_INTEGER: return enif_make_int(env, sqlite3_column_int(statement, i)); case SQLITE_FLOAT: return enif_make_double(env, sqlite3_column_double(statement, i)); case SQLITE_BLOB: return enif_make_tuple2(env, make_atom(env, "blob"), make_binary(env, sqlite3_column_blob(statement, i), sqlite3_column_bytes(statement, i))); case SQLITE_NULL: return make_atom(env, "undefined"); case SQLITE_TEXT: return make_binary(env, sqlite3_column_text(statement, i), sqlite3_column_bytes(statement, i)); default: return make_atom(env, "should_not_happen"); } }
void main() { long n; while(scanf("%ld",&n)==1) { if(n==0)break; make_binary(n); str_rev(); printf("The parity of %s is %d (mod 2).\n",s,p); } }
bvt bv_refinementt::convert_mult(const exprt &expr) { if(!do_arithmetic_refinement || expr.type().id()==ID_fixedbv) return SUB::convert_mult(expr); // we catch any multiplication // unless it involves a constant const exprt::operandst &operands=expr.operands(); const typet &type=ns.follow(expr.type()); assert(operands.size()>=2); if(operands.size()>2) return convert_mult(make_binary(expr)); // make binary // we keep multiplication by a constant for integers if(type.id()!=ID_floatbv) if(operands[0].is_constant() || operands[1].is_constant()) return SUB::convert_mult(expr); bvt bv; approximationt &a=add_approximation(expr, bv); // initially, we have a partial interpretation for integers if(type.id()==ID_signedbv || type.id()==ID_unsignedbv) { // x*0==0 and 0*x==0 literalt op0_zero=bv_utils.is_zero(a.op0_bv); literalt op1_zero=bv_utils.is_zero(a.op1_bv); literalt res_zero=bv_utils.is_zero(a.result_bv); prop.l_set_to_true( prop.limplies(prop.lor(op0_zero, op1_zero), res_zero)); // x*1==x and 1*x==x literalt op0_one=bv_utils.is_one(a.op0_bv); literalt op1_one=bv_utils.is_one(a.op1_bv); literalt res_op0=bv_utils.equal(a.op0_bv, a.result_bv); literalt res_op1=bv_utils.equal(a.op1_bv, a.result_bv); prop.l_set_to_true(prop.limplies(op0_one, res_op1)); prop.l_set_to_true(prop.limplies(op1_one, res_op0)); } return bv; }
static bool decode_visit_symbol(const bson_iter_t *iter, const char *key, size_t v_symbol_len, const char *v_symbol, void *data) { decode_state *ds = data; ERL_NIF_TERM out; if(!make_binary(ds->env, &out, v_symbol, v_symbol_len)) { return true; } vec_push(ds->vec, out); return false; }
static bool decode_visit_code(const bson_iter_t *iter, const char *key, size_t v_code_len, const char *v_code, void *data) { decode_state *ds = data; ERL_NIF_TERM code, out; if(!make_binary(ds->env, &code, v_code, v_code_len)) { return true; } out = enif_make_tuple2(ds->env, ds->st->atom_s_javascript, code); vec_push(ds->vec, out); return false; }
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; }
static bool decode_visit_oid(const bson_iter_t *iter, const char *key, const bson_oid_t *v_oid, void *data) { decode_state *ds = data; ERL_NIF_TERM out; bson_return_val_if_fail(v_oid, true); if(!make_binary(ds->env, &out, v_oid->bytes, 12)) { return true; } out = enif_make_tuple2(ds->env, ds->st->atom_s_oid, out); vec_push(ds->vec, out); return false; }
static bool decode_visit_before(const bson_iter_t *iter, const char *key, void *data) { decode_state *ds = data; ERL_NIF_TERM out; LOG("decode visit key: %s, type: %d\r\n", key, bson_iter_type(iter)); if(ds->keys) { if(!make_binary(ds->env, &out, key, strlen(key))) { return true; } vec_push(ds->vec, out); } return false; }