示例#1
0
static ERL_NIF_TERM nacl_hash(ErlNifEnv *env, int argc, ERL_NIF_TERM const argv[])
{
  ErlNifBinary input;
  ErlNifBinary result;

  if (!enif_inspect_iolist_as_binary(env, argv[0], &input))
    return enif_make_badarg(env);

  if (!enif_alloc_binary(crypto_hash_BYTES, &result))
    return nacl_error_tuple(env, "alloc_failed");

  crypto_hash(result.data, input.data, input.size);

  return enif_make_binary(env, &result);
}
示例#2
0
ERL_NIF_TERM norm(ErlNifEnv* env, ErlNifBinary in, 
    UNormalizationMode mode)
{
    int32_t ulen;
    ErlNifBinary out;
    UErrorCode status;

    ulen = TO_ULEN(in.size);
    do_norm(in, out, ulen, mode, status);
    if (status == U_BUFFER_OVERFLOW_ERROR) {
        do_norm(in, out, ulen, mode, status);
    }
    CHECK(env, status);
    return enif_make_binary(env, &out);
}
示例#3
0
static ERL_NIF_TERM nacl_randombytes(ErlNifEnv *env, int argc, ERL_NIF_TERM const argv[])
{
  unsigned int requested_size;
  ErlNifBinary result;

  if (!enif_get_uint(env, argv[0], &requested_size))
    return enif_make_badarg(env);

  if (!enif_alloc_binary(requested_size, &result))
    return nacl_error_tuple(env, "alloc_failed");

  randombytes(result.data, result.size);

  return enif_make_binary(env, &result);
}
ERL_NIF_TERM _hh_rotate(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    hh_ctx_t* ctx = NULL;
    hh_ctx_t* to = NULL;
    ErlNifBinary target;

    ErlNifResourceType* ctx_type = get_hh_ctx_type(env);
    if (argc != 2 ||
        ctx_type == NULL ||
        !enif_get_resource(env, argv[0], ctx_type, (void **)&ctx) ||
        ctx->data == NULL ||
        !enif_get_resource(env, argv[1], ctx_type, (void **)&to) ||
        to->data == NULL)
    {
        return enif_make_badarg(env);
    }

    hdr_histogram_t* diff_histogram;
    int rc = 0;
    rc = hdr_alloc(ctx->highest_trackable_value, ctx->significant_figures, &diff_histogram);

    if (ENOMEM == rc)
    {
        return make_error(env, "not_enough_memory");
    }

    hdr_rotate(ctx->data, to->data, diff_histogram);

    int size = 0;
    uint8_t* data = NULL;
    int success = hdr_encode_uncompressed(diff_histogram, &data, &size);

    if (!enif_alloc_binary(size, &target))
    {
        return make_error(env, "bad_hdr_binary_alloc");
    }
    target.size = size;
    memcpy(target.data, data, size);
    free(data);
    free(diff_histogram);

    if (success != 0)
    {
        return make_error(env, "bad_hdr_binary");
    }

    return enif_make_binary(env, &target);
}
示例#5
0
static nif_term_t
salt_secretbox(nif_heap_t *hp, int argc, const nif_term_t argv[])
{
	/* salt_secretbox(Plain_text, Nonce, Secret_key) -> Cipher_text. */
	nif_bin_t 		pt;
	nif_bin_t 		nc;
	nif_bin_t 		sk;
	nif_bin_t 		ct;
	nif_term_t 		raw;
	nif_term_t 		sub;

	if (argc != 3)
		return (BADARG);

	/* Unpack arguments ensuring they're suitably typed. */
	if (! enif_inspect_iolist_as_binary(hp, argv[0], &pt))
		return (BADARG);

	if (! enif_inspect_binary(hp, argv[1], &nc))
		return (BADARG);

	if (! enif_inspect_binary(hp, argv[2], &sk))
		return (BADARG);

	/* Check constraints on size and zero prefixing. */
	if (pt.size < crypto_secretbox_ZEROBYTES || pt.size > SALT_MAX_MESSAGE_SIZE)
		return (BADARG);
	if (memcmp((const void *)pt.data, &salt_secretbox_zerobytes[0], crypto_secretbox_ZEROBYTES) != 0)
		return (BADARG);

	if (nc.size != crypto_secretbox_NONCEBYTES)
		return (BADARG);

	if (sk.size != crypto_secretbox_KEYBYTES)
		return (BADARG);

	/* Allocate space for cipher text. NB: Passing ENOMEM as BADARG. */
	if (! enif_alloc_binary(pt.size, &ct))
		return (BADARG);

	/* Perform the crypto, strip leading zeros. */
	(void)crypto_secretbox(ct.data, pt.data, pt.size, nc.data, sk.data);

	raw = enif_make_binary(hp, &ct);
	sub = enif_make_sub_binary(hp, raw, crypto_secretbox_BOXZEROBYTES, ct.size - crypto_secretbox_BOXZEROBYTES);

	return (sub);
}
示例#6
0
static ERL_NIF_TERM emmap_read(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
  mhandle *handle;
  unsigned long bytes;

  if (enif_get_resource(env, argv[0], MMAP_RESOURCE, (void**)&handle)
      && enif_get_ulong(env, argv[1], &bytes)) {


    RW_LOCK;

    if (handle->position == handle->len) {
      RW_UNLOCK;
      return ATOM_EOF;
    }

    unsigned long new_pos = handle->position + bytes;
    if (new_pos > handle->len) { new_pos = handle->len; }
    long size = new_pos - handle->position;
    long start = handle->position;
    handle->position = new_pos;
    RW_UNLOCK;

    if (handle->direct) {

      ERL_NIF_TERM res = enif_make_resource_binary
        (env, handle, (void*) (((char*)handle->mem) + start), size);

      return enif_make_tuple2(env, ATOM_OK, res);

    } else {

      ErlNifBinary bin;
      // When it is non-direct, we have to allocate the binary
      if (!enif_alloc_binary((size_t) size, &bin)) {
        return make_error_tuple(env, ENOMEM);
      }

      memcpy(bin.data, (void*) (((char*)handle->mem) + start), size);

      ERL_NIF_TERM res = enif_make_binary(env, &bin);
      return enif_make_tuple2(env, ATOM_OK, res);
    }

  } else {
    return enif_make_badarg(env);
  }
}
示例#7
0
static ERL_NIF_TERM seg(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifBinary bin; 
    enif_inspect_binary(env, argv[0], &bin);     
    char *text = (char*) bin.data;
    size_t size = bin.size;
    rmmseg::Algorithm algo(text, size);
    std::vector<ERL_NIF_TERM> terms;
    for (rmmseg::Token tok = algo.next_token(); tok.length != 0; tok = algo.next_token()) {
        ErlNifBinary binText;
        enif_alloc_binary(tok.length, &binText);
        memcpy(binText.data, tok.text, tok.length);
        terms.push_back(enif_make_binary(env, &binText));
    }
    return enif_make_list_from_array(env, &terms[0], terms.size());
}
示例#8
0
文件: blake2_nifs.c 项目: b/blake2
ERL_NIF_TERM blake2_hash(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    int bits = 0;
    enif_get_int(env, argv[0], &bits);
    
    ErlNifBinary bin, out;
    enif_inspect_binary(env, argv[1], &bin);
    enif_alloc_binary_compat(env, (size_t)(bits/8), &out);

    int r = blake2b((uint8_t *)out.data, (const void *)bin.data, NULL, (bits / 8), bin.size, 0);
    if (r == 0) {
        return enif_make_tuple2(env, enif_make_atom(env, "ok"), enif_make_binary(env, &out));
    } else {
        return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_atom(env, "hash_failure"));
    }   
}
示例#9
0
static nif_term_t
salt_sign_open(nif_heap_t *hp, int argc, const nif_term_t argv[])
{
	/* salt_sign_open(Signed_msg, Public_key) -> {ok, Verified_msg} | forged_or_garbled. */
	unsigned long long 	len;
	nif_bin_t 		sm;
	nif_bin_t 		pk;
	nif_bin_t 		pm;
	nif_term_t 		raw;
	nif_term_t 		sub;
	nif_term_t 		tag;

	if (argc != 2)
		return (BADARG);

	/* Unpack arguments ensuring they're suitably typed. */
	if (! enif_inspect_iolist_as_binary(hp, argv[0], &sm))
		return (BADARG);

	if (! enif_inspect_binary(hp, argv[1], &pk))
		return (BADARG);

	/* Check constraints on size. */
	if (sm.size < 1 || sm.size > SALT_MAX_MESSAGE_SIZE)
		return (BADARG);

	if (pk.size != crypto_sign_PUBLICKEYBYTES)
		return (BADARG);

	/* Perform the crypto, potentially adjust signed message size. */
	if (! enif_alloc_binary(sm.size + crypto_sign_BYTES, &pm))
		return (BADARG);

	if (crypto_sign_open(pm.data, &len, sm.data, sm.size, pk.data) != 0) {
		enif_release_binary(&pm);

		return (enif_make_atom(hp, "forged_or_garbled"));
	}
	raw = enif_make_binary(hp, &pm);
	tag = enif_make_atom(hp, "ok");

	if (len != sm.size)
		sub = enif_make_sub_binary(hp, raw, 0, len);
	else
		sub = raw;
	return (enif_make_tuple2(hp, tag, sub));
}
示例#10
0
static ERL_NIF_TERM
make_binary(ErlNifEnv *env, const void *bytes, unsigned int size) 
{
    ErlNifBinary blob;
    ERL_NIF_TERM term;

    if(!enif_alloc_binary(size, &blob)) {
	    /* TODO: fix this */
	    return make_atom(env, "error");
    }

    memcpy(blob.data, bytes, size);
    term = enif_make_binary(env, &blob);
    enif_release_binary(&blob);

    return term;
}
示例#11
0
static void keypair_to_binary(brine_task_s *task) {
  ErlNifEnv *env = task->env;
  brine_keypair_s *keypair = task->options.signature.keys;
  ErlNifBinary blob;
  ERL_NIF_TERM result;

  if (!enif_alloc_binary(BRINE_BLOB_SZ, &blob)) {
    result = BRINE_ERROR_NO_MEMORY;
  }
  else {
    brine_serialize_keypair(keypair, blob.data, blob.size);
    result = enif_make_tuple2(env, enif_make_copy(env, BRINE_ATOM_OK), enif_make_binary(env, &blob));
    enif_release_binary(&blob);
  }
  enif_release_resource((void *) keypair);
  enif_send(NULL, &task->owner, task->env, enif_make_tuple2(env, task->ref, result));
}
示例#12
0
文件: commit.c 项目: edescourtis/geef
ERL_NIF_TERM
geef_commit_tree_id(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
	const git_oid *id;
	geef_object *obj;
	ErlNifBinary bin;

	if (!enif_get_resource(env, argv[0], geef_object_type, (void **) &obj))
		return enif_make_badarg(env);

	id = git_commit_tree_id((git_commit *) obj->obj);

	if (geef_oid_bin(&bin, id) < 0)
		return geef_oom(env);

	return enif_make_binary(env, &bin);
}
示例#13
0
//--
static 
ERL_NIF_TERM 
nif_compress(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifBinary input;

    if(!enif_inspect_iolist_as_binary(env, argv[0], &input)) 
    {
        return(enif_make_badarg(env));
    }

    ErlNifBinary bin;

    // [rad] Figure out maximum compression buffer size.
    uint32_t compressed_bytes = wfLZ_GetMaxCompressedSize(input.size);

    // [rad] Get size of recommended scratchpad.
    uint32_t scratchpad_bytes = wfLZ_GetWorkMemSize();

    if(!enif_alloc_binary(compressed_bytes, &bin))
    {
        return(erlang_produce_error(env, "not_enough_memory"));
    }

    // [rad] Allocate scratchpad buffer.
    uint8_t* scratchpad = malloc(scratchpad_bytes);

    // [rad] Perform compression.
    uint32_t data_comp_size = wfLZ_Compress((const uint8_t* const) input.data, input.size, (uint8_t* const) bin.data, (const uint8_t*) scratchpad, 0);
     
    // [rad] We no longer need scratchpad.
    free(scratchpad);

    if(!data_comp_size)
    {
        return(erlang_produce_error(env, "compression_error"));
    }

    if(!enif_realloc_binary(&bin, data_comp_size)) 
    {
        return(erlang_produce_error(env, "not_enough_memory"));
    }

    return(enif_make_binary(env, &bin));
}
示例#14
0
static ERL_NIF_TERM aes_ecb_decrypt(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifBinary in, out, key;
    // key must be binary
    if(!enif_inspect_binary(env, argv[0], &key)) {
        return enif_make_badarg(env);
    }
    // key size should be 16 byte
    if(key.size != 16) {
      return enif_make_badarg(env);
    }
    // cipher must be binary
    if(!enif_inspect_binary(env, argv[1], &in)) {
      return enif_make_badarg(env);
    }
    // cipher shoule be 16 byte block
    if(in.size % 16) {
      return enif_make_badarg(env);
    }

    unsigned char* decoded = (unsigned char*)malloc(sizeof(unsigned char)*in.size);

    struct aes_key_st* decrypt_key = (struct aes_key_st*)malloc(sizeof(AES_KEY));
    memset(decrypt_key, 0, sizeof(AES_KEY));
    AES_set_decrypt_key((unsigned char*)(key.data), 128, decrypt_key);

    int i = 0;
    for(i = 0; i < in.size; i += 16) {
      AES_decrypt((unsigned char*)&in.data[i], (unsigned char*)&decoded[i], decrypt_key);
    }
    //Remove padding
    unsigned char padding = (unsigned char) decoded[in.size-1];

    if(!enif_alloc_binary(in.size - padding, &out)) {
        free(decoded);
        free(decrypt_key);
        return enif_make_badarg(env);
    }
    
    strncpy((unsigned char*)out.data, decoded, in.size - padding);

    free(decoded);
    free(decrypt_key);
    return enif_make_binary(env, &out);
}
示例#15
0
ERL_NIF_TERM
decompress(task_t *task)
{
    size_t len;
    bool   status;

    ERL_NIF_TERM result;
    ErlNifBinary bin;

    len = -1;
    status = snappy::GetUncompressedLength(
        reinterpret_cast<const char *>(task->data.data),
        task->data.size, &len);

    if (!status) {
        result = enif_make_tuple2(task->env, atom_error,
            enif_make_string(task->env, "Data is not compressed",
                ERL_NIF_LATIN1));
        goto done;
    }

    if (!enif_alloc_binary(len, &bin)) {
        result = enif_make_tuple2(task->env, atom_error,
            enif_make_string(task->env, "Couldn't allocate memory",
                ERL_NIF_LATIN1));
        goto done;
    }

    status = snappy::RawUncompress(
        reinterpret_cast<const char*>(task->data.data),
        task->data.size, reinterpret_cast<char*>(bin.data));

    if (!status) {
        result = enif_make_tuple2(task->env, atom_error,
            enif_make_string(task->env, "Failed to decompress",
                ERL_NIF_LATIN1));
        goto done;
    }

    result = enif_make_tuple3(task->env, atom_ok, task->ref,
        enif_make_binary(task->env, &bin));

done:
    return result;
}
示例#16
0
static void
salt_reply_bytes(struct salt_msg *sm, nif_bin_t *bs)
{
	nif_heap_t 		*hp = sm->msg_heap;
	nif_term_t 		tag;
	nif_term_t 		res;
	nif_term_t 		msg;
	nif_term_t 		bb;

	/* From_pid ! {Mref, {ok, Bytes}} */
	bb = enif_make_binary(hp, bs);

	tag = enif_make_atom(hp, "ok");
	res = enif_make_tuple2(hp, tag, bb);
	msg = enif_make_tuple2(hp, sm->msg_mref, res);

	(void)enif_send(NULL, &sm->msg_from, hp, msg);
}
示例#17
0
    ERL_NIF_TERM
vert_virStoragePoolGetInfo(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
    VERT_RESOURCE *sp = NULL;
    ErlNifBinary info = {0};


    VERT_GET_RESOURCE(0, sp, VERT_RES_STORAGEPOOL);

    if (!enif_alloc_binary(sizeof(virStoragePoolInfo), &info))
        return error_tuple(env, atom_enomem);

    VERTERR(virStoragePoolGetInfo(sp->res, (virStoragePoolInfoPtr)info.data) < 0);

    return enif_make_tuple2(env,
            atom_ok,
            enif_make_binary(env, &info));
}
示例#18
0
static ERL_NIF_TERM
geef_hex_to_raw(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
  char sha[MAXBUFLEN];
  (void)memset(&sha, '\0', sizeof(sha));

  if (enif_get_string(env, argv[0], sha, sizeof(sha), ERL_NIF_LATIN1) < 1)
      return enif_make_badarg(env);

  git_oid oid;
  git_oid_fromstr(&oid, sha);

  ErlNifBinary ibin;
  enif_alloc_binary(20, &ibin);
  memcpy(ibin.data, (&oid)->id, 20);

  return enif_make_binary(env, &ibin);
}
示例#19
0
static ERL_NIF_TERM dirty_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    int n;
    char s[10];
    ErlNifBinary b;
    if (have_dirty_schedulers()) {
	assert(ERL_NIF_THR_DIRTY_CPU_SCHEDULER == enif_thread_type()
	       || ERL_NIF_THR_DIRTY_IO_SCHEDULER == enif_thread_type());
    }
    assert(argc == 3);
    enif_get_int(env, argv[0], &n);
    enif_get_string(env, argv[1], s, sizeof s, ERL_NIF_LATIN1);
    enif_inspect_binary(env, argv[2], &b);
    return enif_make_tuple3(env,
			    enif_make_int(env, n),
			    enif_make_string(env, s, ERL_NIF_LATIN1),
			    enif_make_binary(env, &b));
}
示例#20
0
static ERL_NIF_TERM aes_ecb_encrypt(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifBinary in, out, key;
    unsigned char* to_encode;

    // First argument must be a binary key
    if(!enif_inspect_binary(env, argv[0], &key)) {
        return enif_make_badarg(env);
    }
    // Key size must be 16 bytes
    if(key.size != 16) {
        return enif_make_badarg(env);
    }
    // Second argument must be a binary
    if(!enif_inspect_binary(env, argv[1], &in)) {
    return enif_make_badarg(env);
    }

    // Pad with PKCS5Padding
    unsigned char padding =(unsigned char) (16 - in.size % 16);
    to_encode = (unsigned char*)malloc(sizeof(unsigned char) * in.size + padding);
    strncpy(to_encode, (unsigned char*)(in.data), in.size);
    int j = 0;
    for(j = 0; j < padding; j++) {
        to_encode[in.size + j] = padding;
    }
    // If allocation fails we return badarg because there is no API for general errors.
    if(!enif_alloc_binary(in.size + padding, &out)) {
        free(to_encode);
        return enif_make_badarg(env);
    }
    //Init the key
    struct aes_key_st* encrypt_key = (struct aes_key_st*)malloc(sizeof(AES_KEY));
    memset(encrypt_key, 0, sizeof(AES_KEY));
    AES_set_encrypt_key((unsigned char*)(key.data), 128, encrypt_key);
    //Encode!
    int i = 0;
    for(i = 0; i < in.size + padding; i += 16) {
        AES_encrypt((unsigned char*)&to_encode[i], (unsigned char*)&out.data[i], encrypt_key);
    }
    free(to_encode);
    free(encrypt_key);
    return enif_make_binary(env, &out);
}
示例#21
0
ERL_NIF_TERM
hash512(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifBinary message;
    ErlNifBinary hash;

    if (argc != 1) {
        return enif_make_badarg(env);
    }
    if (enif_inspect_binary(env, argv[1], &message) == 0 ||
        message.size == 0U) {
        return enif_make_badarg(env);
    }
    enif_alloc_binary(64, &hash);
    Hash(512, (const BitSequence *) message.data,
         (DataLength) message.size, (BitSequence *) hash.data);

    return enif_make_binary(env, &hash);
}
示例#22
0
文件: engine.c 项目: bjorng/otp
ERL_NIF_TERM engine_get_next_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{/* (Engine) */
#ifdef HAS_ENGINE_SUPPORT
    ERL_NIF_TERM ret, result;
    ENGINE *engine;
    ErlNifBinary engine_bin;
    struct engine_ctx *ctx, *next_ctx = NULL;

    // Get Engine
    ASSERT(argc == 1);

    if (!enif_get_resource(env, argv[0], engine_ctx_rtype, (void**)&ctx))
        goto bad_arg;

    if ((engine = ENGINE_get_next(ctx->engine)) == NULL) {
        if (!enif_alloc_binary(0, &engine_bin))
            goto err;
        engine_bin.size = 0;
        return enif_make_tuple2(env, atom_ok, enif_make_binary(env, &engine_bin));
    }

    if ((next_ctx = enif_alloc_resource(engine_ctx_rtype, sizeof(struct engine_ctx))) == NULL)
        goto err;
    next_ctx->engine = engine;
    next_ctx->id = NULL;

    result = enif_make_resource(env, next_ctx);
    ret = enif_make_tuple2(env, atom_ok, result);
    goto done;

 bad_arg:
 err:
    ret = enif_make_badarg(env);

 done:
    if (next_ctx)
        enif_release_resource(next_ctx);
    return ret;

#else
    return atom_notsup;
#endif
}
示例#23
0
文件: ewpcap.c 项目: hfeeki/ewpcap
void
ewpcap_send(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)
{
    EWPCAP_STATE *ep = (EWPCAP_STATE *)user;
    ErlNifBinary buf = {0};
    int rv = 0;


    /* XXX no way to indicate an error? */
    if (ep->p == NULL)
        return;

    if (!enif_alloc_binary(h->caplen, &buf)) {
        pcap_breakloop(ep->p);
        return;
    }

    (void)memcpy(buf.data, bytes, buf.size);

    /* {ewpcap, Ref, DatalinkType, Time, ActualLength, Packet} */
    rv = enif_send(
             NULL,
             &ep->pid,
             ep->env,
             enif_make_tuple6(ep->env,
                              atom_ewpcap,
                              enif_make_copy(ep->env, ep->ref),
                              enif_make_int(ep->env, ep->datalink),
                              enif_make_tuple3(ep->env,
                                      enif_make_ulong(ep->env, abs(h->ts.tv_sec / 1000000)),
                                      enif_make_ulong(ep->env, h->ts.tv_sec % 1000000),
                                      enif_make_ulong(ep->env, h->ts.tv_usec)
                                              ),
                              enif_make_ulong(ep->env, h->len),
                              enif_make_binary(ep->env, &buf)
                             )
         );

    if (!rv)
        pcap_breakloop(ep->p);

    enif_clear_env(ep->env);
}
示例#24
0
文件: cb.c 项目: muut/cberl
ERL_NIF_TERM cb_http(ErlNifEnv* env, handle_t* handle, void* obj)
{
    http_args_t* args = (http_args_t*)obj;

    struct libcouchbase_callback_http cb = {0};
    lcb_error_t ret;
    lcb_http_request_t req;

    lcb_http_cmd_t cmd;
    cmd.version = 0;
    if (args->type != 3) {
        cmd.v.v0.path = args->path;
        cmd.v.v0.npath = strlen(args->path);
    }
    cmd.v.v0.body = args->body;
    cmd.v.v0.nbody = strlen(args->body);
    cmd.v.v0.method = args->method;
    cmd.v.v0.chunked = 0; // no support for chunking
    cmd.v.v0.content_type = args->content_type;

    ret = lcb_make_http_request(handle->instance, &cb, args->type, &cmd, &req);

    if (ret != LCB_SUCCESS) {
        return return_lcb_error(env, ret);
    }

    lcb_wait(handle->instance);

    enif_free(args->content_type);
    enif_free(args->body);
    enif_free(args->path);

    if(cb.ret.error != LCB_SUCCESS) {
        return return_lcb_error(env, cb.ret.error);
    }

    ErlNifBinary value_binary;
    enif_alloc_binary(cb.ret.size, &value_binary);
    memcpy(value_binary.data, cb.ret.data, cb.ret.size);
    free(cb.ret.data);
    free(cb.ret.key);
    return enif_make_tuple3(env, A_OK(env), enif_make_int(env, cb.status), enif_make_binary(env, &value_binary));
}
示例#25
0
/* 0: resource */
    static ERL_NIF_TERM
nif_buf(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
    ALLOC_STATE *p = NULL;

    ErlNifBinary buf = {0};

    if (!enif_get_resource(env, argv[0], PROCKET_ALLOC_RESOURCE, (void **)&p))
        return enif_make_badarg(env);

    if (!enif_alloc_binary(p->size, &buf))
        return error_tuple(env, ENOMEM);

    (void)memcpy(buf.data, p->buf, buf.size);

    return enif_make_tuple2(env,
            atom_ok,
            enif_make_binary(env, &buf));
}
示例#26
0
static ERL_NIF_TERM
geef_object_read(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
  char path[MAXBUFLEN];
  char sha[MAXBUFLEN];
  (void)memset(&path, '\0', sizeof(path));
  (void)memset(&sha, '\0', sizeof(sha));

  if (enif_get_string(env, argv[0], path, sizeof(path), ERL_NIF_LATIN1) < 1)
      return enif_make_badarg(env);
  if (enif_get_string(env, argv[1], sha, sizeof(sha), ERL_NIF_LATIN1) < 1)
      return enif_make_badarg(env);

  git_odb *odb;
  git_odb_open(&odb, path);

  git_oid oid;
  git_oid_fromstr(&oid, sha);

  int error;
  git_odb_object *obj;

  error = git_odb_read(&obj, odb, &oid);
  if (error)
  {
    git_odb_free(odb);
    return enif_make_tuple2(env,
        enif_make_atom(env, "error"),
        enif_make_string(env, "object does not exist", ERL_NIF_LATIN1));
  }

  size_t len;
  len = git_odb_object_size(obj);

  ErlNifBinary ibin;
  enif_alloc_binary(len, &ibin);
  memcpy(ibin.data, git_odb_object_data(obj), len);

  git_odb_free(odb);

  return enif_make_binary(env, &ibin);
}
示例#27
0
static ERL_NIF_TERM elibart_search(ErlNifEnv* env, int argc,
                                          const ERL_NIF_TERM argv[])
{
    art_tree* t;
    ErlNifBinary key;
    unsigned char buffer[BUFF_SIZE]; // 256K buffer
    unsigned char *key_copy = buffer;
    
    // extract arguments atr_tree, key
    if(argc != 2)
      return enif_make_badarg(env);
    if(!enif_get_resource(env, argv[0], elibart_RESOURCE, (void**) &t))
        return enif_make_badarg(env);
    if (!enif_inspect_binary(env, argv[1], &key))
        return enif_make_badarg(env);

    // buffer size not enough, pay the price
    if (key.size > BUFF_SIZE)
        key_copy = malloc(key.size + 1);

    // TODO review -- is it possible not to copy the key just to add '\0'?
    memcpy(key_copy, key.data, key.size);
    key_copy[key.size] = '\0';

    // search the art_tree for the given key
    art_elem_struct *value = art_search(t, key_copy, key.size + 1);

    // buffer size not enough, pay the price
    if (key.size > BUFF_SIZE)
        free(key_copy);

    // key does not exist in the art_tree
    if (!value)
        return mk_atom(env, "empty");

    // key exixts, return the associated value
    ErlNifBinary res;
    enif_alloc_binary(value->size, &res);
    memcpy(res.data, value->data, value->size);

    return enif_make_tuple2(env, mk_atom(env, "ok"), enif_make_binary(env, &res));
}
示例#28
0
文件: serctl.c 项目: jj1bdx/srly
    static ERL_NIF_TERM
nif_cfsetospeed(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifBinary buf = {0};
    unsigned int speed = 0;


    if (!enif_inspect_binary(env, argv[0], &buf) || (buf.size != sizeof(struct termios)))
        return enif_make_badarg(env);

    if (!enif_get_uint(env, argv[1], &speed))
        return enif_make_badarg(env);

    if (!enif_realloc_binary(&buf, buf.size))
        return enif_make_badarg(env);

    (void)cfsetospeed((struct termios *)buf.data, speed);

    return enif_make_binary(env, &buf);
}
示例#29
0
ERL_NIF_TERM
geef_revparse_single(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
	ErlNifBinary bin, id;
	geef_repository *repo;
	geef_object *obj;
	ERL_NIF_TERM type, term_obj;

	if (!enif_get_resource(env, argv[0], geef_repository_type, (void **) &repo))
		return enif_make_badarg(env);

	if (!enif_inspect_iolist_as_binary(env, argv[1], &bin))
		return enif_make_badarg(env);

	if (geef_terminate_binary(&bin) < 0)
		return geef_oom(env);

	obj = enif_alloc_resource(geef_object_type, sizeof(geef_object));
	if (!obj)
		return geef_oom(env);

	if (git_revparse_single(&obj->obj, repo->repo, (char *) bin.data) < 0) {
		enif_release_binary(&bin);
		enif_release_resource(obj);
		return geef_error(env);
	}

	type = geef_object_type2atom(git_object_type(obj->obj));

	if (geef_oid_bin(&id, git_object_id(obj->obj)) < 0)
		return geef_oom(env);


	term_obj = enif_make_resource(env, obj);
	enif_release_resource(obj);

	obj->repo = repo;
	enif_keep_resource(repo);

	return enif_make_tuple4(env, atoms.ok, term_obj, type, enif_make_binary(env, &id));
}
示例#30
0
/**
 * Retrieve an object from LRU-Storage
 */
static ERL_NIF_TERM leo_mcerl_nif_get(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
  lcu_cache* cache;
  String key;
  String val;

  ErlNifResourceType* pert;
  ErlNifBinary keybin;
  ErlNifBinary bin;

  if (argc < 2) {
    return enif_make_badarg(env);
  }

  pert = (ErlNifResourceType*)enif_priv_data(env);
  if (!enif_get_resource(env, argv[0], pert, (void**)&cache)) {
    return enif_make_badarg(env);
  }

  if (!enif_inspect_binary(env, argv[1], &keybin)) {
    return enif_make_badarg(env);
  }

  if (keybin.size <= 0) {
    return enif_make_badarg(env);
  }

  key.str = keybin.data;
  key.len = keybin.size;
  lcu_cache_get(cache, key, &val);

  if (val.str == NULL) {
    return atom_not_found;
  }

  if (!enif_alloc_binary(val.len, &bin)) {
    return enif_make_badarg(env);
  }

  memcpy(bin.data, val.str, val.len);
  return enif_make_tuple2(env, atom_ok, enif_make_binary(env, &bin));
}