static inline int enc_unknown(Encoder* e, ERL_NIF_TERM value) { ErlNifBinary* bin = e->curr; ERL_NIF_TERM curr; if(e->i > 0) { if(!enc_result(e, &curr)) { return 0; } e->iolist = enif_make_list_cell(e->env, curr, e->iolist); e->iolen++; } e->iolist = enif_make_list_cell(e->env, value, e->iolist); e->iolen++; // Reinitialize our binary for the next buffer. e->curr = bin; if(!enif_alloc_binary(BIN_INC_SIZE, e->curr)) { return 0; } memset(e->curr->data, 0, e->curr->size); e->p = (char*) e->curr->data; e->u = (unsigned char*) e->curr->data; e->i = 0; return 1; }
int enc_done(Encoder* e, ERL_NIF_TERM* value) { ERL_NIF_TERM last; if(e->iolen == 0) { return enc_result(e, value); } if(e->i > 0 ) { if(!enc_result(e, &last)) { return 0; } e->iolist = enif_make_list_cell(e->env, last, e->iolist); e->iolen++; } *value = e->iolist; return 1; }
static inline int enc_unknown(Encoder* e, ERL_NIF_TERM value) { ErlNifBinary* bin = e->curr; ERL_NIF_TERM curr; if(e->i > 0) { if(!enc_result(e, &curr)) { return 0; } e->iolist = enif_make_list_cell(e->env, curr, e->iolist); e->iolen++; } e->iolist = enif_make_list_cell(e->env, value, e->iolist); e->iolen++; // Track the total number of bytes produced before // splitting our IO buffer. We add 16 to this value // as a rough estimate of the number of bytes that // a bignum might produce when encoded. e->iosize += e->i + 16; // Reinitialize our binary for the next buffer if we // used any data in the buffer. If we haven't used any // bytes in the buffer then we can safely reuse it // for anything following the unknown value. if(e->i > 0) { e->curr = bin; if(!enif_alloc_binary(BIN_INC_SIZE, e->curr)) { return 0; } memset(e->curr->data, 0, e->curr->size); e->p = (char*) e->curr->data; e->u = (unsigned char*) e->curr->data; e->i = 0; } return 1; }