Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
try a couple suggestions from davisp
  • Loading branch information
joewilliams committed Jun 7, 2012
1 parent 99bf4ab commit e64df3b
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions c_src/lz4.c
Expand Up @@ -44,8 +44,13 @@ static inline void store_le32(char *c, u_int32_t x)

static inline u_int32_t load_le32(const char *c)
{
const u_int8_t *d = (const u_int8_t *)c;
return d[0] | (d[1] << 8) | (d[2] << 16) | (d[3] << 24);
const u_int8_t *d = (const u_int8_t *)c;
u_int32_t r = d[0];

r = (r << 8) | d[1];
r = (r << 8) | d[2];
r = (r << 8) | d[3];
return r;
}

static const int hdr_size = sizeof(u_int32_t);
Expand Down Expand Up @@ -89,6 +94,7 @@ compress(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
ErlNifBinary source;
char *result_buf = NULL;
int dest_size;
int real_size;

if (!enif_inspect_binary(env, argv[0], &source)) return enif_make_badarg(env);

Expand All @@ -98,7 +104,8 @@ compress(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {

store_le32(result_buf, source.size);

LZ4_compress(source.data, result_buf + hdr_size, source.size);
real_size = LZ4_compress(source.data, result_buf + hdr_size, source.size);
enif_realloc_binary(&result, real_size);

return enif_make_tuple2(env, atom_ok, result);
}
Expand Down

0 comments on commit e64df3b

Please sign in to comment.