Skip to content

Commit

Permalink
feat: introduce muntargz extract api function
Browse files Browse the repository at this point in the history
  • Loading branch information
jaromil committed Dec 15, 2024
1 parent ba51b90 commit fcad0d7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
23 changes: 20 additions & 3 deletions src/muntar.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static int mtar_rewind(mtar_t *tar) {


#include <sys/stat.h> // for mkdir(2)
#if defined(_WIN32
#if defined(_WIN32)
#include <windows.h>
#define makedir(path) CreateDirectory(path, NULL)
#else
Expand Down Expand Up @@ -237,13 +237,30 @@ int untar_to_path(const char *path, const uint8_t *buf,
return(MTAR_ESUCCESS);
}

#if !defined(NOGUNZIP)
// gunzip and untar all in one
#include <tinf.h>
#define DECOMPRESSED_SIZE_RATIO 6 // raise this on errors
int untargz_to_path(const char *path, const uint8_t *buf,
const unsigned int len) {
return(0);
int res;
unsigned int destlen = len*6;
uint8_t *dest = malloc(destlen);
res = tinf_gzip_uncompress(dest,&destlen,buf,len);
// fprintf(stdout,"Compressed source length: %u\n",len);
// fprintf(stdout,"Uncompressed destination length: %u\n",destlen);
// fprintf(stdout,"Max buffer length: %u\n",len*6);
// fprintf(stdout,"Multiplier ratio: %u\n",destlen/len);
if(res != TINF_OK) {
fprintf(stderr,"Error in gunzip decompression (untargz_to_path)\n");
free(dest);
exit(res);
}
res = untar_to_path(path, dest, destlen);
free(dest);
return(res);
}

#endif

int mtar_load(mtar_t *tar, const char *name,
const uint8_t *buf, size_t size) {
Expand Down
4 changes: 4 additions & 0 deletions src/muntar.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
// used by extract_embeddings(char *tmpdir)
int untar_to_path(const char *path,
const uint8_t *buf, const unsigned int len);
#if !defined(NOGUNZIP)
int untargz_to_path(const char *path,
const uint8_t *buf, const unsigned int len);
#endif

enum {
MTAR_ESUCCESS = 0,
Expand Down
33 changes: 28 additions & 5 deletions test/muntar.bats
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@ int main(int argc, char **argv) {
exit(0);
}
EOF
gcc -o muntar_list -I ${R}/src \
gcc -o muntar_list -DNOGUNZIP -I ${R}/src \
${R}/src/muntar.c ${R}/src/io.c examples.c muntar_list.c
run ./muntar_list
assert_success
assert_line --partial "examples/"
assert_line --partial "examples/donut.c (743 bytes)"
}


@test "muntar extract contents" {
cat << EOF > muntar_extract.c
#include <stdio.h>
Expand All @@ -63,13 +62,11 @@ int main(int argc, char **argv) {
exit(res);
}
EOF
gcc -o muntar_extract -I ${R}/src \
gcc -o muntar_extract -DNOGUNZIP -I ${R}/src \
${R}/src/muntar.c examples.c muntar_extract.c
run ./muntar_extract ${TMP}/extracted
>&3 cat ${TMP}/extracted/examples/donut.c
assert_success
# assert_file_size_equals ${TMP}/extracted/examples/donut.c \
# `ls -l ${R}/examples/donut.c | cut -d' ' -f5`
l=`sha256sum ${TMP}/extracted/examples/donut.c | cut -d' ' -f1`
r=`sha256sum ${R}/examples/donut.c | cut -d' ' -f1`
assert_equal $l $r
Expand Down Expand Up @@ -125,3 +122,29 @@ EOF
r=`sha256sum examples.tar | cut -d' ' -f1`
assert_equal $l $r
}


@test "muntargz extract contents" {
cat << EOF > muntargz_extract.c
#include <stdio.h>
#include <stdlib.h>
#include <muntar.h>
extern unsigned char examples_tar_gz[];
extern unsigned int examples_tar_gz_len;
int main(int argc, char **argv) {
int res;
fprintf(stderr,"extract to %s\n",argv[1]);
res = untargz_to_path(argv[1], examples_tar_gz, examples_tar_gz_len);
exit(res);
}
EOF
gcc -o muntargz_extract -I ${R}/src \
${R}/src/tinfgzip.c ${R}/src/tinflate.c ${R}/src/muntar.c \
examples_gzip.c muntargz_extract.c
run ./muntargz_extract ${TMP}/extractgz
>&3 cat ${TMP}/extractgz/examples/donut.c
assert_success
l=`sha256sum ${TMP}/extractgz/examples/donut.c | cut -d' ' -f1`
r=`sha256sum ${R}/examples/donut.c | cut -d' ' -f1`
assert_equal $l $r
}

0 comments on commit fcad0d7

Please sign in to comment.