couchfile_pointer_info* read_pointer(char* buf, int pos) { //Parse KP pair into a couchfile_pointer_info {K, {ptr, reduce_value, subtreesize}} couchfile_pointer_info *p = malloc(sizeof(couchfile_pointer_info)); p->writerq_resource = NULL; //DBG("%u,%u,%u,%u\n", buf[pos+0], buf[pos+1], buf[pos+2], buf[pos+3]); ei_decode_tuple_header(buf, &pos, NULL); //arity 2 term_to_buf(&p->key, buf, &pos); ei_decode_tuple_header(buf, &pos, NULL); //arity 3 ei_decode_ulonglong(buf, &pos, (unsigned long long*) &p->pointer); term_to_buf(&p->reduce_value, buf, &pos); ei_decode_ulonglong(buf, &pos, (unsigned long long*) &p->subtreesize); return p; }
int find_first_gteq(char* buf, int pos, void* key, compare_info* lu, int at_least) { int list_arity, inner_arity; int list_pos = 0, cmp_val; off_t pair_pos = 0; if(ei_decode_list_header(buf, &pos, &list_arity) < 0) { return ERROR_PARSE; } while(list_pos < list_arity) { //{<<"key", some other term} pair_pos = pos; //Save pos of kv/kp pair tuple if(ei_decode_tuple_header(buf, &pos, &inner_arity) < 0) { return ERROR_PARSE; } lu->last_cmp_key = (*lu->from_ext)(lu, buf,pos); cmp_val = (*lu->compare) (lu->last_cmp_key, key); lu->last_cmp_val = cmp_val; lu->list_pos = list_pos; if(cmp_val >= 0 && list_pos >= at_least) { break; } ei_skip_term(buf, &pos); //skip over the key ei_skip_term(buf, &pos); //skip over the value list_pos++; } return pair_pos; }
static void cmd_ei_send_funs(char* buf, int len) { int index = 0, n; long fd; erlang_pid pid; ei_x_buff x; erlang_fun fun1, fun2; if (ei_decode_long(buf, &index, &fd) < 0) fail("expected long"); if (ei_decode_pid(buf, &index, &pid) < 0) fail("expected pid (node)"); if (ei_decode_tuple_header(buf, &index, &n) < 0) fail("expected tuple"); if (n != 2) fail("expected tuple"); if (ei_decode_fun(buf, &index, &fun1) < 0) fail("expected Fun1"); if (ei_decode_fun(buf, &index, &fun2) < 0) fail("expected Fun2"); if (ei_x_new_with_version(&x) < 0) fail("ei_x_new_with_version"); if (ei_x_encode_tuple_header(&x, 2) < 0) fail("encode tuple header"); if (ei_x_encode_fun(&x, &fun1) < 0) fail("encode fun1"); if (ei_x_encode_fun(&x, &fun2) < 0) fail("encode fun2"); free_fun(&fun1); free_fun(&fun2); send_errno_result(ei_send(fd, &pid, x.buff, x.index)); ei_x_free(&x); }
static int prepared_bind(sqlite3_drv_t *drv, char *buffer, int buffer_size) { int result; unsigned int prepared_index; long long_prepared_index; int index = 0, type, size; sqlite3_stmt *statement; #ifdef DEBUG fprintf(drv->log, "Finalizing prepared statement: %.*s\n", buffer_size, buffer); fflush(drv->log); #endif ei_decode_version(buffer, &index, NULL); ei_decode_tuple_header(buffer, &index, &size); // assert(size == 2); ei_decode_long(buffer, &index, &long_prepared_index); prepared_index = (unsigned int) long_prepared_index; if (prepared_index >= drv->prepared_count) { return output_error(drv, SQLITE_MISUSE, "Trying to bind non-existent prepared statement"); } statement = drv->prepared_stmts[prepared_index]; result = bind_parameters(drv, buffer, buffer_size, &index, statement, &type, &size); if (result == SQLITE_OK) { return output_ok(drv); } else { return result; // error has already been output } }
static void handle_write(const char *req, int *req_index) { if (!uart_is_open(uart)) { send_error_response("ebadf"); return; } int term_size; if (ei_decode_tuple_header(req, req_index, &term_size) < 0 || term_size != 2) errx(EXIT_FAILURE, "expecting {data, timeout}"); int term_type; if (ei_get_type(req, req_index, &term_type, &term_size) < 0 || term_type != ERL_BINARY_EXT) errx(EXIT_FAILURE, "expecting data as a binary"); uint8_t *to_write = malloc(term_size); long amount_to_write; if (ei_decode_binary(req, req_index, to_write, &amount_to_write) < 0) errx(EXIT_FAILURE, "decode binary error?"); long timeout; if (ei_decode_long(req, req_index, &timeout) < 0) errx(EXIT_FAILURE, "expecting timeout"); // uart_write always invokes a callback when it completes (error or no error). uart_write(uart, to_write, amount_to_write, timeout); }
static void resize(Cmd *cmd) { char *buff = cmd->data; int len = cmd->size; Gd *gd = cmd->gd; int index = 0; unsigned long width, height, srcW, srcH; gdImagePtr destination = NULL; ei_decode_version(buff, &index, NULL); ei_decode_tuple_header(buff, &index, NULL); ei_decode_ulong(buff, &index, &width); ei_decode_ulong(buff, &index, &height); if (NULL == gd->image) { driver_failure_atom(gd->port, "null_image"); return; } srcW = gdImageSX(gd->image); srcH = gdImageSY(gd->image); destination = gdImageCreateTrueColor(width, height); if (NULL == destination) { driver_failure_posix(gd->port, ENOMEM); return; } gdImageCopyResampled(destination, gd->image, 0, 0, 0, 0, width, height, srcW, srcH); gdImageDestroy(gd->image); gd->image = destination; send_atom(gd->port, "ok"); }
int my_decode_tuple_header(const char *buf, int *index, struct my_obj* obj) { int ret = ei_decode_tuple_header(buf, index, &obj->u.arity); if (ret == 0 && obj) obj->nterms = obj->u.arity; return ret; }
static void crop(Cmd *cmd) { char *buff = cmd->data; int len = cmd->size; Gd *gd = cmd->gd; int index = 0; long width, height; int srcW, srcH, srcX, srcY, destX, destY, playX, playY; gdImagePtr destination = NULL; ei_decode_version(buff, &index, NULL); ei_decode_tuple_header(buff, &index, NULL); ei_decode_long(buff, &index, &width); ei_decode_long(buff, &index, &height); if (NULL == gd->image) { driver_failure_atom(gd->port, "null_image"); return; } srcW = gdImageSX(gd->image); srcH = gdImageSY(gd->image); destination = gdImageCreateTrueColor(width, height); if (NULL == destination) { driver_failure_posix(gd->port, ENOMEM); return; } gdImageFilledRectangle(destination, 0, 0, width, height, gdImageColorAllocate(destination, 255, 255, 255)); destX = (width - srcW) / 2; destY = (height - srcH) / 2; gdImageCopy(destination, gd->image, destX, destY, 0, 0, srcW, srcH); gdImageDestroy(gd->image); gd->image = destination; send_atom(gd->port, "ok"); }
decode_result process_data(ErlDrvData handle,unsigned char* buf, ErlDrvSizeT* sz,char*& html_content,char*& url_address){ int index = 0,size=0,type=0,ver=0; if (ei_decode_version((char*)buf, &index,&ver)){ //data encoding version mismatch return decode_result{false,std::pair<std::string,std::string>("",""), "data encoding version mismatch"}; } else if (ei_get_type((char*)buf,&index,&type,&size)){ //must be a binary return decode_result{false,std::pair<std::string,std::string>("",""), "must be a binary"}; } else{ int tuple_arity; ei_decode_tuple_header((char*)buf,&index,&tuple_arity); ei_get_type((char*)buf,&index,&type,&size); url_address = (char*)driver_alloc((size+2)*sizeof(char)); ei_decode_string((char*)buf,&index,url_address); ei_get_type((char*)buf,&index,&type,&size); //cout << "Html Content Size " << size << endl; html_content = (char*)driver_alloc((size+2)*sizeof(char)); ei_decode_string((char*)buf,&index,html_content); *sz = size; std::string url_address_str(url_address); std::string html_content_str(html_content); return decode_result{true,std::pair<std::string,std::string>(url_address_str,html_content_str),NULL}; } }
int next_pointer(GEOSCommand *command, int argc, char *name, void** data) { int arity; char atom[MAXATOMLEN]; unsigned long pointer; //decode tuple if(ei_decode_tuple_header(command->param_bytes, &command->index, &arity)) { return -1; } //verify if it's the right label if(arity!=argc) { return -1; } //decode pointer label if(ei_decode_atom(command->param_bytes,&command->index, atom)) { return -1; } if(strcmp(atom,name)) { return -1; } //decode the pointer if(ei_decode_ulong(command->param_bytes, &command->index, &pointer)) { return -1; } *data = (void *) pointer; return 0; }
static void output(ErlDrvData drv_data, char *buf, ErlDrvSizeT) { driver_data_t *data = (driver_data_t *)drv_data; int result = 0; int index = 0, version, arity; ei_decode_version(buf, &index, &version); ei_decode_tuple_header(buf, &index, &arity); if (2 == arity) { result = ergen_driver_do_txn(data->driver, buf, &index); if(result) { ergen_drv_output_bool(data->port, result); } else { ergen_drv_output_error1(data->port, "badmatch", "cmd"); } return; } ergen_drv_output_error0(data->port, "badarg"); }
/** * @brief Decode and forward requests from Elixir to the appropriate handlers * @param req the undecoded request * @param cookie */ static void handle_elixir_request(const char *req, void *cookie) { (void) cookie; // Commands are of the form {Command, Arguments}: // { atom(), term() } int req_index = sizeof(uint16_t); if (ei_decode_version(req, &req_index, NULL) < 0) errx(EXIT_FAILURE, "Message version issue?"); int arity; if (ei_decode_tuple_header(req, &req_index, &arity) < 0 || arity != 2) errx(EXIT_FAILURE, "expecting {cmd, args} tuple"); char cmd[MAXATOMLEN]; if (ei_decode_atom(req, &req_index, cmd) < 0) errx(EXIT_FAILURE, "expecting command atom"); for (struct request_handler *rh = request_handlers; rh->name != NULL; rh++) { if (strcmp(cmd, rh->name) == 0) { rh->handler(req, &req_index); return; } } errx(EXIT_FAILURE, "unknown command: %s", cmd); }
/* ** Data sent from erlang to port. */ static void trace_file_output(ErlDrvData handle, char *buf, ErlDrvSizeT len) { int index = 1; ei_print_term(stdout, buf, &index); erts_printf("\n"); /* erts_printf("output: %d: %u\n", rc, (unsigned char)buff[0]); */ int arity; char atom[MAXATOMLEN]; char type[MAXATOMLEN]; char pidstr[100]; index = 1; erlang_pid pid; if(!ei_decode_tuple_header(buf, &index, &arity)) { if(!ei_decode_atom(buf, &index, atom)) { if(strcmp("trace", atom) == 0) { if(!ei_decode_pid(buf, &index, &pid)) { if(!ei_decode_atom(buf, &index, type)) { snprintf(pidstr, 100, "<0.%d.%d>", pid.num, pid.serial); erts_printf("%s: %s\n", pidstr, type); dispatch(pidstr, type, index, buf); } } } } } }
static int iconv_erl_control(ErlDrvData drv_data, unsigned int command, char *buf, int len, char **rbuf, int rlen) { int i; int size; int index = 0; int avail; size_t inleft, outleft; ErlDrvBinary *b; char *from, *to, *string, *stmp, *rstring, *rtmp; iconv_t cd; ei_decode_version(buf, &index, &i); ei_decode_tuple_header(buf, &index, &i); ei_get_type(buf, &index, &i, &size); from = malloc(size + 1); ei_decode_string(buf, &index, from); ei_get_type(buf, &index, &i, &size); to = malloc(size + 1); ei_decode_string(buf, &index, to); ei_get_type(buf, &index, &i, &size); stmp = string = malloc(size + 1); ei_decode_string(buf, &index, string); cd = iconv_open(to, from); if(cd == (iconv_t) -1) { cd = iconv_open("ascii", "ascii"); if(cd == (iconv_t) -1) { cd = iconv_open("ascii", "ascii"); } } outleft = avail = 4*size; inleft = size; rtmp = rstring = malloc(avail); iconv(cd, &stmp, &inleft, &rtmp, &outleft); size = rtmp - rstring; //printf("size=%d, res=%s\r\n", size, rstring); *rbuf = (char*)(b = driver_alloc_binary(size)); memcpy(b->orig_bytes, rstring, size); free(from); free(to); free(string); free(rstring); iconv_close(cd); return size; }
void do_wattroff(state *st) { int arity; long slot, attrs; ei_decode_tuple_header(st->args, &(st->index), &arity); ei_decode_long(st->args, &(st->index), &slot); ei_decode_long(st->args, &(st->index), &attrs); encode_ok_reply(st, wattroff(st->win[slot], (int)attrs)); }
void do_move(state *st) { int arity; long y, x; ei_decode_tuple_header(st->args, &(st->index), &arity); ei_decode_long(st->args, &(st->index), &y); ei_decode_long(st->args, &(st->index), &x); encode_ok_reply(st, move((int)y, (int)x)); }
void do_keypad(state *st) { int arity, bf; long slot; ei_decode_tuple_header(st->args, &(st->index), &arity); ei_decode_long(st->args, &(st->index), &slot); ei_decode_boolean(st->args, &(st->index), &bf); encode_ok_reply(st, keypad(st->win[slot], bf)); }
ETERM *erl_global_whereis(int fd, const char *name, char *node) { char buf[EISMALLBUF]; char *bufp=buf; char tmpbuf[64]; int index = 0; erlang_pid *self = erl_self(); erlang_pid epid; ETERM *opid; erlang_msg msg; int i; int version,arity,msglen; self->num = fd; /* FIXME looks strange to change something?! */ ei_encode_version(buf,&index); ei_encode_tuple_header(buf,&index,2); ei_encode_pid(buf,&index,self); /* PidFrom */ ei_encode_tuple_header(buf,&index,5); ei_encode_atom(buf,&index,"call"); /* call */ ei_encode_atom(buf,&index,"global"); /* Mod */ ei_encode_atom(buf,&index,"whereis_name"); /* Fun */ ei_encode_list_header(buf,&index,1); /* Args: [ name ] */ ei_encode_atom(buf,&index,name); ei_encode_empty_list(buf,&index); ei_encode_atom(buf,&index,"user"); /* user */ /* make the rpc call */ if (ei_send_reg_encoded(fd,self,"rex",buf,index)) return NULL; while (1) { index = EISMALLBUF; if (!(i = ei_recv_internal(fd,&bufp,&index,&msg,&msglen,1,0))) continue; else break; } if (i != ERL_SEND) return NULL; /* expecting { rex, pid } */ index = 0; if (ei_decode_version(buf,&index,&version) || ei_decode_tuple_header(buf,&index,&arity) || (arity != 2) || ei_decode_atom(buf,&index,tmpbuf) || strcmp(tmpbuf,"rex") || ei_decode_pid(buf,&index,&epid)) return NULL; /* bad response from other side */ /* put the pid into a format for the caller */ index = 0; ei_encode_pid(buf,&index,&epid); opid = erl_decode((unsigned char*)buf); /* extract the nodename for the caller */ if (node) strcpy(node,epid.node); return opid; }
void do_mvaddch(state *st) { int arity; long y, x, ch; ei_decode_tuple_header(st->args, &(st->index), &arity); ei_decode_long(st->args, &(st->index), &y); ei_decode_long(st->args, &(st->index), &x); ei_decode_long(st->args, &(st->index), &ch); encode_ok_reply(st, mvaddch((int)y, (int)x, (chtype)ch)); }
void do_init_pair(state *st) { int arity; long pairnum, fcolor, bcolor; ei_decode_tuple_header(st->args, &(st->index), &arity); ei_decode_long(st->args, &(st->index), &pairnum); ei_decode_long(st->args, &(st->index), &fcolor); ei_decode_long(st->args, &(st->index), &bcolor); encode_ok_reply(st, init_pair((int)pairnum, (int)fcolor, (int)bcolor)); }
void do_wvline(state *st) { int arity; long slot, ch, max; ei_decode_tuple_header(st->args, &(st->index), &arity); ei_decode_long(st->args, &(st->index), &slot); ei_decode_long(st->args, &(st->index), &ch); ei_decode_long(st->args, &(st->index), &max); encode_ok_reply(st, wvline(st->win[slot], (chtype)ch, (int)max)); }
void do_wmove(state *st) { int arity; long slot, y, x; ei_decode_tuple_header(st->args, &(st->index), &arity); ei_decode_long(st->args, &(st->index), &slot); ei_decode_long(st->args, &(st->index), &y); ei_decode_long(st->args, &(st->index), &x); encode_ok_reply(st, wmove(st->win[slot], (int)y, (int)x)); }
void do_box(state *st) { int arity; long slot, verch, horch; ei_decode_tuple_header(st->args, &(st->index), &arity); ei_decode_long(st->args, &(st->index), &slot); ei_decode_long(st->args, &(st->index), &verch); ei_decode_long(st->args, &(st->index), &horch); encode_ok_reply(st, box(st->win[slot], (chtype)verch, (chtype)horch)); }
void do_waddch(state *st) { int arity; long slot; char ch = 0; ei_decode_tuple_header(st->args, &(st->index), &arity); ei_decode_long(st->args, &(st->index), &slot); ei_decode_char(st->args, &(st->index), &ch); encode_ok_reply(st, waddch(st->win[slot], ch)); }
void do_addstr(state *st) { int arity; long strlen; ei_decode_tuple_header(st->args, &(st->index), &arity); ei_decode_long(st->args, &(st->index), &strlen); char str[strlen]; ei_decode_string(st->args, &(st->index), str); encode_ok_reply(st, addnstr(str, strlen)); }
/*----------------------------------------------------------------- * MAIN *----------------------------------------------------------------*/ int main(int argc, char *argv[]) { byte* buf; int size = BUF_SIZE; if (argc > 1) { test(argc, argv); return 0; } fprintf(stderr, "gssapi started\r\n"); if ((buf = malloc(size)) == NULL) return -1; while ( (buf = read_cmd(buf, &size)) ) { int res = 0; int index = 0; int version, arity; char command[MAXATOMLEN]; ei_x_buff result; port_func func; /* Ensure that we are receiving the binary term by reading and * stripping the version byte */ EI(ei_decode_version(buf, &index, &version)); /* Our marshalling spec is that we are expecting a tuple {Command, Arg1} */ EI(ei_decode_tuple_header(buf, &index, &arity)); EI(arity != 2); EI(ei_decode_atom(buf, &index, command)); /* Prepare the output buffer that will hold {ok, Result} or {error, Reason} */ EI(ei_x_new_with_version(&result) || ei_x_encode_tuple_header(&result, 2)); /* fprintf(stderr, "command: %s\r\n", command); */ func = lookup_func(command); if (func) { res = func(buf, index, &result); } else { EI(ei_x_encode_atom(&result, "error") || ei_x_encode_atom(&result, "unsupported_command")); } write_cmd(&result); ei_x_free(&result); } /* error: */ fprintf(stderr, "No more command, exiting\r\n"); return 0; }
void terms_to_coordseq(GEOSCommand *command) { char *buffer = command->param_bytes; int *index = &command->index; int size; GEOSCoordSequence *coordSeq = NULL; GEOSContextHandle_t handle = command->driver_data->handle; if(ei_decode_list_header(buffer, index, &size)) { erl_send_error(command, "notalist"); return; } unsigned int i; for(i = 0; i < size; i++) { //for each element int digitc; double x,y,z; if(ei_decode_tuple_header(buffer, index, &digitc)) { erl_send_error(command, "invalidlist"); if(coordSeq!=NULL) GEOSCoordSeq_destroy_r(handle, coordSeq); return; } if(coordSeq == NULL) { coordSeq = GEOSCoordSeq_create_r(handle, size, digitc); if(coordSeq == NULL) { erl_send_error(command, "create"); return; } } if(next_number(command, &x) || GEOSCoordSeq_setX_r(handle, coordSeq, i, x)==0) { erl_send_error(command, "setx"); GEOSCoordSeq_destroy_r(handle, coordSeq); return; } if(next_number(command, &y) || GEOSCoordSeq_setY_r(handle, coordSeq, i, y)==0) { erl_send_error(command, "sety"); GEOSCoordSeq_destroy_r(handle, coordSeq); return; } if(digitc == 3) { if(next_number(command, &z) || GEOSCoordSeq_setZ_r(handle, coordSeq, i, z)==0) { erl_send_error(command, "setz"); GEOSCoordSeq_destroy_r(handle, coordSeq); return; } } } ERL_WRITE_PTR_GEOSCOORDSEQUENCE(command, coordSeq); }
static int unwrap(char *buf, int index, ei_x_buff *presult) { ei_x_buff result = *presult; /* {unwrap, {Idx, Input}} -> {ok, {conf_state, Output}} */ int arity; gss_buffer_desc in; gss_buffer_desc out; long idx; int conf_state; OM_uint32 maj_stat, min_stat; gss_qop_t qop; memset(&in, 0, sizeof(in)); memset(&out, 0, sizeof(out)); EI(ei_decode_tuple_header(buf, &index, &arity)); EI(arity != 2); EI(ei_decode_long(buf, &index, &idx)); EI(decode_gssapi_binary(buf, &index, &in)); if (idx < 0 || idx >= MAX_SESSIONS || !g_sessions[idx]) ENCODE_ERROR("bad_instance"); maj_stat = gss_unwrap(&min_stat, g_sessions[idx], &in, &out, &conf_state, &qop); if (!GSS_ERROR(maj_stat)) { const char *conf_str = conf_state ? "true":"false"; EI(ei_x_encode_atom(&result, "ok") || ei_x_encode_tuple_header(&result, 2) || ei_x_encode_atom(&result, conf_str) || ei_x_encode_binary(&result, out.value, out.length) ); } else { EI(ei_x_encode_atom(&result, "error") || ei_x_encode_long(&result, maj_stat)); } error: if (in.value) gss_release_buffer(&min_stat, &in); if (out.value) gss_release_buffer(&min_stat, &out); *presult = result; return 0; }
eqmlTerm(const char * buf, int index = 0) : _buf(buf), _index(index) { if (index == 0) ei_decode_version(_buf, &_index, NULL); ei_get_type(_buf, &_index, &_type, &_size); if (isTuple()) ei_decode_tuple_header(_buf, &_index, &_arity); }
static switch_status_t handle_msg_sendmsg(listener_t *listener, int arity, ei_x_buff * buf, ei_x_buff * rbuf) { char uuid[SWITCH_UUID_FORMATTED_LENGTH + 1]; int headerlength; if (ei_decode_string_or_binary(buf->buff, &buf->index, SWITCH_UUID_FORMATTED_LENGTH, uuid) || ei_decode_list_header(buf->buff, &buf->index, &headerlength)) { ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "badarg"); } else { switch_core_session_t *session; if (!zstr_buf(uuid) && (session = switch_core_session_locate(uuid))) { switch_event_t *event; if (switch_event_create(&event, SWITCH_EVENT_SEND_MESSAGE) == SWITCH_STATUS_SUCCESS) { char key[1024]; char value[1024]; int i = 0; switch_bool_t fail = SWITCH_FALSE; while (!ei_decode_tuple_header(buf->buff, &buf->index, &arity) && arity == 2) { i++; if (ei_decode_string(buf->buff, &buf->index, key) || ei_decode_string(buf->buff, &buf->index, value)) { fail = SWITCH_TRUE; break; } switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, key, value); } if (headerlength != i || fail) { ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "badarg"); } else { if (switch_core_session_queue_private_event(session, &event, SWITCH_FALSE) == SWITCH_STATUS_SUCCESS) { ei_x_encode_atom(rbuf, "ok"); } else { ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "badmem"); } } } /* release the lock returned by session locate */ switch_core_session_rwunlock(session); } else { ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "nosession"); } } return SWITCH_STATUS_SUCCESS; }