Skip to content

Commit

Permalink
Add GitRekt.Git.object_zlib_inflate/1
Browse files Browse the repository at this point in the history
See issues #1 and #3 for more details.
  • Loading branch information
redrabbit committed Nov 29, 2017
1 parent 377248e commit 563042b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions apps/gitrekt/c_src/geef.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ static ErlNifFunc geef_funcs[] =
{"object_repository", 1, geef_object_repository},
{"object_lookup", 2, geef_object_lookup},
{"object_id", 1, geef_object_id},
{"object_zlib_inflate", 1, geef_object_zlib_inflate},
{"commit_tree", 1, geef_commit_tree},
{"commit_tree_id", 1, geef_commit_tree_id},
{"commit_create", 8, geef_commit_create},
Expand Down
37 changes: 37 additions & 0 deletions apps/gitrekt/c_src/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "repository.h"
#include "object.h"
#include "oid.h"
#include <zlib.h>
#include <string.h>
#include <git2.h>

Expand Down Expand Up @@ -113,3 +114,39 @@ geef_object_id(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])

return enif_make_tuple2(env, atoms.ok, enif_make_binary(env, &bin));
}

ERL_NIF_TERM
geef_object_zlib_inflate(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
ErlNifBinary bin;
z_stream z;
z.zalloc = Z_NULL;
z.zfree = Z_NULL;
z.opaque = Z_NULL;

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

if (!geef_terminate_binary(&bin)) {
enif_release_binary(&bin);
return geef_oom(env);
}

char output[bin.size*2];

z.avail_in = bin.size;
z.next_in = bin.data;
z.avail_out = bin.size*2;
z.next_out = output;

inflateInit(&z);
inflate(&z, Z_NO_FLUSH);
inflateEnd(&z);

if (enif_alloc_binary(z.total_out, &bin) < 0)
return -1;

memcpy(bin.data, output, z.total_out);

return enif_make_tuple3(env, atoms.ok, enif_make_binary(env, &bin), enif_make_ulong(env, z.total_in));
}
3 changes: 2 additions & 1 deletion apps/gitrekt/c_src/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ typedef struct {
ERL_NIF_TERM geef_object_repository(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
ERL_NIF_TERM geef_object_lookup(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
ERL_NIF_TERM geef_object_id(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
ERL_NIF_TERM geef_object_zlib_inflate(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);

ERL_NIF_TERM geef_object_type2atom(const git_otype type);
git_otype geef_object_atom2type(ERL_NIF_TERM term);

git_otype geef_object_atom2type(ERL_NIF_TERM term);
void geef_object_free(ErlNifEnv *env, void *cd);

#endif
8 changes: 8 additions & 0 deletions apps/gitrekt/lib/gitrekt/git.ex
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,14 @@ defmodule GitRekt.Git do
raise Code.LoadError, file: @nif_path_lib
end

@doc """
Inflates the given `data` with *zlib*.
"""
@spec object_zlib_inflate(binary) :: {:ok, binary, non_neg_integer} | {:error, term}
def object_zlib_inflate(_data) do
raise Code.LoadError, file: @nif_path_lib
end

@doc """
Returns the tree id for the given `commit`.
"""
Expand Down

0 comments on commit 563042b

Please sign in to comment.