Skip to content

Commit

Permalink
Merge pull request #844 from greenbone/GEA-635_Use_compression_in_fee…
Browse files Browse the repository at this point in the history
…d_updates

Add: Added a function that opens a gzip file for reading via a file descriptor.
  • Loading branch information
mattmundell authored Oct 21, 2024
2 parents 3c5fb2a + 79d6d3d commit bb09b59
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
32 changes: 32 additions & 0 deletions util/compressutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,35 @@ gvm_gzip_open_file_reader (const char *path)
FILE *file = fopencookie (gz_file, "r", io_functions);
return file;
}

/**
* @brief Opens a gzip file as a FILE* stream for reading and decompression.
*
* @param[in] fd File descriptor of the gzip file to open.
*
* @return The FILE* on success, NULL otherwise.
*/
FILE *
gvm_gzip_open_file_reader_fd (int fd)
{
static cookie_io_functions_t io_functions = {
.read = gz_file_read,
.write = NULL,
.seek = NULL,
.close = gz_file_close,
};

if (fd < 0)
{
return NULL;
}

gzFile gz_file = gzdopen (fd, "r");
if (gz_file == NULL)
{
return NULL;
}

FILE *file = fopencookie (gz_file, "r", io_functions);
return file;
}
3 changes: 3 additions & 0 deletions util/compressutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ gvm_uncompress (const void *, unsigned long, unsigned long *);
FILE *
gvm_gzip_open_file_reader (const char *);

FILE *
gvm_gzip_open_file_reader_fd (int);

#endif /* not _GVM_COMPRESSUTILS_H */
28 changes: 27 additions & 1 deletion util/compressutils_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <cgreen/cgreen.h>
#include <cgreen/mocks.h>
#include <fcntl.h>

Describe (compressutils);
BeforeEach (compressutils)
Expand Down Expand Up @@ -79,6 +80,30 @@ Ensure (compressutils, can_uncompress_using_reader)
assert_that (fclose (stream), is_equal_to (0));
}

Ensure (compressutils, can_uncompress_using_fd_reader)
{
const char *testdata = "TEST-12345-12345-TEST";
size_t compressed_len;
char *compressed =
gvm_compress_gzipheader (testdata, strlen (testdata) + 1, &compressed_len);

char compressed_filename[35] = "/tmp/gvm_gzip_test_XXXXXX";
int compressed_fd = mkstemp (compressed_filename);
write (compressed_fd, compressed, compressed_len);
close (compressed_fd);

compressed_fd = open (compressed_filename, O_RDONLY);

FILE *stream = gvm_gzip_open_file_reader_fd (compressed_fd);
assert_that (stream, is_not_null);

gchar *uncompressed = g_malloc0 (30);
fread (uncompressed, 1, 30, stream);
assert_that (uncompressed, is_equal_to_string (testdata));

assert_that (fclose (stream), is_equal_to (0));
}

/* Test suite. */
int
main (int argc, char **argv)
Expand All @@ -92,9 +117,10 @@ main (int argc, char **argv)
add_test_with_context (suite, compressutils,
can_compress_and_uncompress_with_header);
add_test_with_context (suite, compressutils, can_uncompress_using_reader);
add_test_with_context (suite, compressutils, can_uncompress_using_fd_reader);

if (argc > 1)
return run_single_test (suite, argv[1], create_text_reporter ());

return run_test_suite (suite, create_text_reporter ());
}
}

0 comments on commit bb09b59

Please sign in to comment.