diff options
author | Guy Harris <guy@alum.mit.edu> | 2018-11-15 20:06:36 -0800 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2018-11-16 09:20:36 +0000 |
commit | a1372f6d017cb9798dce7de5e25d329c82a2da79 (patch) | |
tree | 4588e90f67d25c13f4b944242328b901e0a27514 /wiretap | |
parent | e12753d5f6e6f474af9934e8102cb4190aaa5846 (diff) |
Use an enum for compression types in various interfaces.
This:
1) means that we don't have to flag the compression argument with a
comment to indicate what it means (FALSE doesn't obviously say "not
compressed", WTAP_UNCOMPRESSED does);
2) leaves space in the interfaces in question for additional compression
types.
(No, this is not part 1 of an implementation of additional compression
types, it's just an API cleanup. Implementing additional compression
types involves significant work in libwiretap, as well as UI changes to
replace "compress the file" checkboxes with something to indicate *how*
to compress the file, or to always use some other form of compression).
Change-Id: I1d23dc720be10158e6b34f97baa247ba8a537abf
Reviewed-on: https://code.wireshark.org/review/30660
Petri-Dish: Guy Harris <guy@alum.mit.edu>
Tested-by: Petri Dish Buildbot
Reviewed-by: Guy Harris <guy@alum.mit.edu>
Diffstat (limited to 'wiretap')
-rw-r--r-- | wiretap/file_access.c | 70 | ||||
-rw-r--r-- | wiretap/merge.c | 8 | ||||
-rw-r--r-- | wiretap/nettrace_3gpp_32_423.c | 3 | ||||
-rw-r--r-- | wiretap/wtap-int.h | 2 | ||||
-rw-r--r-- | wiretap/wtap.c | 9 | ||||
-rw-r--r-- | wiretap/wtap.h | 29 |
6 files changed, 74 insertions, 47 deletions
diff --git a/wiretap/file_access.c b/wiretap/file_access.c index d0093600bb..e6a8d517c6 100644 --- a/wiretap/file_access.c +++ b/wiretap/file_access.c @@ -2222,17 +2222,19 @@ wtap_dump_supports_comment_types(int file_type_subtype, guint32 comment_types) return FALSE; } -static gboolean wtap_dump_open_check(int file_type_subtype, int encap, gboolean comressed, int *err); +static gboolean wtap_dump_open_check(int file_type_subtype, int encap, gboolean compressed, int *err); static wtap_dumper* wtap_dump_alloc_wdh(int file_type_subtype, int encap, int snaplen, - gboolean compressed, int *err); -static gboolean wtap_dump_open_finish(wtap_dumper *wdh, int file_type_subtype, gboolean compressed, int *err); + wtap_compression_type compression_type, + int *err); +static gboolean wtap_dump_open_finish(wtap_dumper *wdh, int file_type_subtype, + int *err); static WFILE_T wtap_dump_file_open(wtap_dumper *wdh, const char *filename); static WFILE_T wtap_dump_file_fdopen(wtap_dumper *wdh, int fd); static int wtap_dump_file_close(wtap_dumper *wdh); static wtap_dumper * -wtap_dump_init_dumper(int file_type_subtype, gboolean compressed, +wtap_dump_init_dumper(int file_type_subtype, wtap_compression_type compression_type, const wtap_dump_params *params, int *err) { wtap_dumper *wdh; @@ -2241,12 +2243,16 @@ wtap_dump_init_dumper(int file_type_subtype, gboolean compressed, GArray *interfaces = params->idb_inf ? params->idb_inf->interface_data : NULL; /* Check whether we can open a capture file with that file type - and that encapsulation. */ - if (!wtap_dump_open_check(file_type_subtype, params->encap, compressed, err)) + and that encapsulation, and, if the compression type isn't + "uncompressed", whether we can write a *compressed* file + of that file type. */ + if (!wtap_dump_open_check(file_type_subtype, params->encap, + (compression_type != WTAP_UNCOMPRESSED), err)) return NULL; /* Allocate a data structure for the output stream. */ - wdh = wtap_dump_alloc_wdh(file_type_subtype, params->encap, params->snaplen, compressed, err); + wdh = wtap_dump_alloc_wdh(file_type_subtype, params->encap, + params->snaplen, compression_type, err); if (wdh == NULL) return NULL; /* couldn't allocate it */ @@ -2309,13 +2315,15 @@ wtap_dump_init_dumper(int file_type_subtype, gboolean compressed, wtap_dumper * wtap_dump_open(const char *filename, int file_type_subtype, - gboolean compressed, const wtap_dump_params *params, int *err) + wtap_compression_type compression_type, const wtap_dump_params *params, + int *err) { wtap_dumper *wdh; WFILE_T fh; /* Allocate and initialize a data structure for the output stream. */ - wdh = wtap_dump_init_dumper(file_type_subtype, compressed, params, err); + wdh = wtap_dump_init_dumper(file_type_subtype, compression_type, params, + err); if (wdh == NULL) return NULL; @@ -2330,7 +2338,7 @@ wtap_dump_open(const char *filename, int file_type_subtype, } wdh->fh = fh; - if (!wtap_dump_open_finish(wdh, file_type_subtype, compressed, err)) { + if (!wtap_dump_open_finish(wdh, file_type_subtype, err)) { /* Get rid of the file we created; we couldn't finish opening it. */ wtap_dump_file_close(wdh); @@ -2343,7 +2351,7 @@ wtap_dump_open(const char *filename, int file_type_subtype, wtap_dumper * wtap_dump_open_tempfile(char **filenamep, const char *pfx, - int file_type_subtype, gboolean compressed, + int file_type_subtype, wtap_compression_type compression_type, const wtap_dump_params *params, int *err) { int fd; @@ -2355,7 +2363,8 @@ wtap_dump_open_tempfile(char **filenamep, const char *pfx, *filenamep = NULL; /* Allocate and initialize a data structure for the output stream. */ - wdh = wtap_dump_init_dumper(file_type_subtype, compressed, params, err); + wdh = wtap_dump_init_dumper(file_type_subtype, compression_type, params, + err); if (wdh == NULL) return NULL; @@ -2380,7 +2389,7 @@ wtap_dump_open_tempfile(char **filenamep, const char *pfx, } wdh->fh = fh; - if (!wtap_dump_open_finish(wdh, file_type_subtype, compressed, err)) { + if (!wtap_dump_open_finish(wdh, file_type_subtype, err)) { /* Get rid of the file we created; we couldn't finish opening it. */ wtap_dump_file_close(wdh); @@ -2392,14 +2401,15 @@ wtap_dump_open_tempfile(char **filenamep, const char *pfx, } wtap_dumper * -wtap_dump_fdopen(int fd, int file_type_subtype, gboolean compressed, +wtap_dump_fdopen(int fd, int file_type_subtype, wtap_compression_type compression_type, const wtap_dump_params *params, int *err) { wtap_dumper *wdh; WFILE_T fh; /* Allocate and initialize a data structure for the output stream. */ - wdh = wtap_dump_init_dumper(file_type_subtype, compressed, params, err); + wdh = wtap_dump_init_dumper(file_type_subtype, compression_type, params, + err); if (wdh == NULL) return NULL; @@ -2414,7 +2424,7 @@ wtap_dump_fdopen(int fd, int file_type_subtype, gboolean compressed, } wdh->fh = fh; - if (!wtap_dump_open_finish(wdh, file_type_subtype, compressed, err)) { + if (!wtap_dump_open_finish(wdh, file_type_subtype, err)) { wtap_dump_file_close(wdh); g_free(wdh); return NULL; @@ -2423,7 +2433,7 @@ wtap_dump_fdopen(int fd, int file_type_subtype, gboolean compressed, } wtap_dumper * -wtap_dump_open_stdout(int file_type_subtype, gboolean compressed, +wtap_dump_open_stdout(int file_type_subtype, wtap_compression_type compression_type, const wtap_dump_params *params, int *err) { int new_fd; @@ -2455,7 +2465,8 @@ wtap_dump_open_stdout(int file_type_subtype, gboolean compressed, } #endif - wdh = wtap_dump_fdopen(new_fd, file_type_subtype, compressed, params, err); + wdh = wtap_dump_fdopen(new_fd, file_type_subtype, compression_type, + params, err); if (wdh == NULL) { /* Failed; close the new FD */ ws_close(new_fd); @@ -2499,7 +2510,8 @@ wtap_dump_open_check(int file_type_subtype, int encap, gboolean compressed, int } static wtap_dumper * -wtap_dump_alloc_wdh(int file_type_subtype, int encap, int snaplen, gboolean compressed, int *err) +wtap_dump_alloc_wdh(int file_type_subtype, int encap, int snaplen, + wtap_compression_type compression_type, int *err) { wtap_dumper *wdh; @@ -2512,21 +2524,21 @@ wtap_dump_alloc_wdh(int file_type_subtype, int encap, int snaplen, gboolean comp wdh->file_type_subtype = file_type_subtype; wdh->snaplen = snaplen; wdh->encap = encap; - wdh->compressed = compressed; + wdh->compression_type = compression_type; wdh->wslua_data = NULL; wdh->interface_data = g_array_new(FALSE, FALSE, sizeof(wtap_block_t)); return wdh; } static gboolean -wtap_dump_open_finish(wtap_dumper *wdh, int file_type_subtype, gboolean compressed, int *err) +wtap_dump_open_finish(wtap_dumper *wdh, int file_type_subtype, int *err) { int fd; gboolean cant_seek; /* Can we do a seek on the file descriptor? If not, note that fact. */ - if(compressed) { + if (wdh->compression_type != WTAP_UNCOMPRESSED) { cant_seek = TRUE; } else { fd = ws_fileno((FILE *)wdh->fh); @@ -2572,7 +2584,7 @@ void wtap_dump_flush(wtap_dumper *wdh) { #ifdef HAVE_ZLIB - if(wdh->compressed) { + if (wdh->compression_type == WTAP_GZIP_COMPRESSED) { gzwfile_flush((GZWFILE_T)wdh->fh); } else #endif @@ -2647,7 +2659,7 @@ gboolean wtap_dump_get_needs_reload(wtap_dumper *wdh) { static WFILE_T wtap_dump_file_open(wtap_dumper *wdh, const char *filename) { - if(wdh->compressed) { + if (wdh->compression_type == WTAP_GZIP_COMPRESSED) { return gzwfile_open(filename); } else { return ws_fopen(filename, "wb"); @@ -2666,7 +2678,7 @@ wtap_dump_file_open(wtap_dumper *wdh _U_, const char *filename) static WFILE_T wtap_dump_file_fdopen(wtap_dumper *wdh, int fd) { - if(wdh->compressed) { + if (wdh->compression_type == WTAP_GZIP_COMPRESSED) { return gzwfile_fdopen(fd); } else { return ws_fdopen(fd, "wb"); @@ -2687,7 +2699,7 @@ wtap_dump_file_write(wtap_dumper *wdh, const void *buf, size_t bufsize, int *err size_t nwritten; #ifdef HAVE_ZLIB - if (wdh->compressed) { + if (wdh->compression_type == WTAP_GZIP_COMPRESSED) { nwritten = gzwfile_write((GZWFILE_T)wdh->fh, buf, (unsigned int) bufsize); /* * gzwfile_write() returns 0 on error. @@ -2721,7 +2733,7 @@ static int wtap_dump_file_close(wtap_dumper *wdh) { #ifdef HAVE_ZLIB - if(wdh->compressed) + if (wdh->compression_type == WTAP_GZIP_COMPRESSED) return gzwfile_close((GZWFILE_T)wdh->fh); else #endif @@ -2732,7 +2744,7 @@ gint64 wtap_dump_file_seek(wtap_dumper *wdh, gint64 offset, int whence, int *err) { #ifdef HAVE_ZLIB - if(wdh->compressed) { + if (wdh->compression_type != WTAP_UNCOMPRESSED) { *err = WTAP_ERR_CANT_SEEK_COMPRESSED; return -1; } else @@ -2753,7 +2765,7 @@ wtap_dump_file_tell(wtap_dumper *wdh, int *err) { gint64 rval; #ifdef HAVE_ZLIB - if(wdh->compressed) { + if (wdh->compression_type != WTAP_UNCOMPRESSED) { *err = WTAP_ERR_CANT_SEEK_COMPRESSED; return -1; } else diff --git a/wiretap/merge.c b/wiretap/merge.c index 0a5c69fb9a..0e1c50cf9f 100644 --- a/wiretap/merge.c +++ b/wiretap/merge.c @@ -1022,8 +1022,8 @@ merge_files(const gchar* out_filename, const int file_type, params.shb_hdrs = shb_hdrs; params.idb_inf = idb_inf; } - pdh = wtap_dump_open(out_filename, file_type, FALSE /* compressed */, - ¶ms, err); + pdh = wtap_dump_open(out_filename, file_type, WTAP_UNCOMPRESSED, ¶ms, + err); if (pdh == NULL) { merge_close_in_files(in_file_count, in_files); g_free(in_files); @@ -1126,7 +1126,7 @@ merge_files_to_tempfile(gchar **out_filenamep, const char *pfx, params.idb_inf = idb_inf; } pdh = wtap_dump_open_tempfile(out_filenamep, pfx, file_type, - FALSE /* compressed */, ¶ms, err); + WTAP_UNCOMPRESSED, ¶ms, err); if (pdh == NULL) { merge_close_in_files(in_file_count, in_files); g_free(in_files); @@ -1223,7 +1223,7 @@ merge_files_to_stdout(const int file_type, const char *const *in_filenames, params.shb_hdrs = shb_hdrs; params.idb_inf = idb_inf; } - pdh = wtap_dump_open_stdout(file_type, FALSE /* compressed */, ¶ms, err); + pdh = wtap_dump_open_stdout(file_type, WTAP_UNCOMPRESSED, ¶ms, err); if (pdh == NULL) { merge_close_in_files(in_file_count, in_files); g_free(in_files); diff --git a/wiretap/nettrace_3gpp_32_423.c b/wiretap/nettrace_3gpp_32_423.c index 7d19e0aed4..8ea97335a4 100644 --- a/wiretap/nettrace_3gpp_32_423.c +++ b/wiretap/nettrace_3gpp_32_423.c @@ -799,7 +799,8 @@ create_temp_pcapng_file(wtap *wth, int *err, gchar **err_info, nettrace_3gpp_32_ .idb_inf = idb_inf, }; wdh_exp_pdu = wtap_dump_fdopen(import_file_fd, WTAP_FILE_TYPE_SUBTYPE_PCAPNG, - FALSE, ¶ms, &exp_pdu_file_err); + WTAP_UNCOMPRESSED, ¶ms, + &exp_pdu_file_err); if (wdh_exp_pdu == NULL) { result = WTAP_OPEN_ERROR; goto end; diff --git a/wiretap/wtap-int.h b/wiretap/wtap-int.h index f62a8dffde..fcd4b41167 100644 --- a/wiretap/wtap-int.h +++ b/wiretap/wtap-int.h @@ -88,7 +88,7 @@ struct wtap_dumper { int file_type_subtype; int snaplen; int encap; - gboolean compressed; + wtap_compression_type compression_type; gboolean needs_reload; /* TRUE if the file requires re-loading after saving with wtap */ gint64 bytes_dumped; diff --git a/wiretap/wtap.c b/wiretap/wtap.c index 4f73628393..3b7c31891f 100644 --- a/wiretap/wtap.c +++ b/wiretap/wtap.c @@ -77,10 +77,13 @@ wtap_file_type_subtype(wtap *wth) return wth->file_type_subtype; } -gboolean -wtap_iscompressed(wtap *wth) +wtap_compression_type +wtap_get_compression_type(wtap *wth) { - return file_iscompressed((wth->fh == NULL) ? wth->random_fh : wth->fh); + gboolean is_compressed; + + is_compressed = file_iscompressed((wth->fh == NULL) ? wth->random_fh : wth->fh); + return is_compressed ? WTAP_GZIP_COMPRESSED : WTAP_UNCOMPRESSED; } guint diff --git a/wiretap/wtap.h b/wiretap/wtap.h index 374c1ad3ba..eb6685323d 100644 --- a/wiretap/wtap.h +++ b/wiretap/wtap.h @@ -1690,6 +1690,14 @@ void wtap_rec_init(wtap_rec *rec); WS_DLL_PUBLIC void wtap_rec_cleanup(wtap_rec *rec); +/* + * Types of compression for a file, including "none". + */ +typedef enum { + WTAP_UNCOMPRESSED, + WTAP_GZIP_COMPRESSED +} wtap_compression_type; + /*** get various information snippets about the current file ***/ /** Return an approximation of the amount of data we've read sequentially @@ -1699,7 +1707,7 @@ gint64 wtap_read_so_far(wtap *wth); WS_DLL_PUBLIC gint64 wtap_file_size(wtap *wth, int *err); WS_DLL_PUBLIC -gboolean wtap_iscompressed(wtap *wth); +wtap_compression_type wtap_get_compression_type(wtap *wth); WS_DLL_PUBLIC guint wtap_snapshot_length(wtap *wth); /* per file */ WS_DLL_PUBLIC @@ -1897,14 +1905,15 @@ void wtap_dump_params_cleanup(wtap_dump_params *params); * * @param filename The new file's name. * @param file_type_subtype The WTAP_FILE_TYPE_SUBTYPE_XXX file type. - * @param compressed True if file should be compressed. + * @param compression_type Type of compression to use when writing, if any * @param params The per-file information for this file. * @param[out] err Will be set to an error code on failure. * @return The newly created dumper object, or NULL on failure. */ WS_DLL_PUBLIC wtap_dumper* wtap_dump_open(const char *filename, int file_type_subtype, - gboolean compressed, const wtap_dump_params *params, int *err); + wtap_compression_type compression_type, const wtap_dump_params *params, + int *err); /** * @brief Creates a dumper for a temporary file. @@ -1913,14 +1922,14 @@ wtap_dumper* wtap_dump_open(const char *filename, int file_type_subtype, * pathname of the temporary file; it's allocated with g_malloc() * @param pfx A string to be used as the prefix for the temporary file name * @param file_type_subtype The WTAP_FILE_TYPE_SUBTYPE_XXX file type. - * @param compressed True if file should be compressed. + * @param compression_type Type of compression to use when writing, if any * @param params The per-file information for this file. * @param[out] err Will be set to an error code on failure. * @return The newly created dumper object, or NULL on failure. */ WS_DLL_PUBLIC wtap_dumper* wtap_dump_open_tempfile(char **filenamep, const char *pfx, - int file_type_subtype, gboolean compressed, + int file_type_subtype, wtap_compression_type compression_type, const wtap_dump_params *params, int *err); /** @@ -1928,14 +1937,15 @@ wtap_dumper* wtap_dump_open_tempfile(char **filenamep, const char *pfx, * * @param fd The file descriptor for which the dumper should be created. * @param file_type_subtype The WTAP_FILE_TYPE_SUBTYPE_XXX file type. - * @param compressed True if file should be compressed. + * @param compression_type Type of compression to use when writing, if any * @param params The per-file information for this file. * @param[out] err Will be set to an error code on failure. * @return The newly created dumper object, or NULL on failure. */ WS_DLL_PUBLIC wtap_dumper* wtap_dump_fdopen(int fd, int file_type_subtype, - gboolean compressed, const wtap_dump_params *params, int *err); + wtap_compression_type compression_type, const wtap_dump_params *params, + int *err); /** * @brief Creates a dumper for the standard output. @@ -1943,14 +1953,15 @@ wtap_dumper* wtap_dump_fdopen(int fd, int file_type_subtype, * @param file_type_subtype The WTAP_FILE_TYPE_SUBTYPE_XXX file type. * @param encap The WTAP_ENCAP_XXX encapsulation type (WTAP_ENCAP_PER_PACKET for multi) * @param snaplen The maximum packet capture length. - * @param compressed True if file should be compressed. + * @param compression_type Type of compression to use when writing, if any * @param params The per-file information for this file. * @param[out] err Will be set to an error code on failure. * @return The newly created dumper object, or NULL on failure. */ WS_DLL_PUBLIC wtap_dumper* wtap_dump_open_stdout(int file_type_subtype, - gboolean compressed, const wtap_dump_params *params, int *err); + wtap_compression_type compression_type, const wtap_dump_params *params, + int *err); WS_DLL_PUBLIC gboolean wtap_dump(wtap_dumper *, const wtap_rec *, const guint8 *, |