本文整理汇总了C++中enif_make_binary函数的典型用法代码示例。如果您正苦于以下问题:C++ enif_make_binary函数的具体用法?C++ enif_make_binary怎么用?C++ enif_make_binary使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了enif_make_binary函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: nacl_hash
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: norm
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: nacl_randombytes
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);
}
示例4: _hh_rotate
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: salt_secretbox
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: emmap_read
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: seg
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: blake2_hash
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: salt_sign_open
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: make_binary
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: keypair_to_binary
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: geef_commit_tree_id
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: nif_compress
//--
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: aes_ecb_decrypt
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: decompress
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;
}