pcapng: centralize access to members of the wtap_optval_t union.

Have the "compute option size" and "write option" routines take a
wtap_optval_t * as an argument and choose the appropriate member of the
union.
This commit is contained in:
Guy Harris 2021-06-29 14:54:48 -07:00
parent ef180c1be6
commit 689ff5a3ad
1 changed files with 41 additions and 41 deletions

View File

@ -3541,11 +3541,11 @@ typedef struct compute_options_size_t
compute_option_size_func compute_option_size;
} compute_options_size_t;
static guint32 pcapng_compute_string_option_size(char *str)
static guint32 pcapng_compute_string_option_size(wtap_optval_t *optval)
{
guint32 size = 0, pad;
size = (guint32)strlen(str) & 0xffff;
size = (guint32)strlen(optval->stringval) & 0xffff;
if ((size % 4)) {
pad = 4 - (size % 4);
} else {
@ -3580,11 +3580,11 @@ static guint32 pcapng_compute_if_filter_option_size(wtap_optval_t *optval)
return size;
}
static guint32 pcapng_compute_custom_option_size(size_t custom_data_len)
static guint32 pcapng_compute_custom_option_size(wtap_optval_t *optval)
{
size_t size, pad;
size = custom_data_len + sizeof(guint32);
size = optval->custom_opt.custom_data_len + sizeof(guint32);
if (size > 65535) {
size = 65535;
}
@ -3612,11 +3612,11 @@ compute_block_option_size(wtap_block_t block _U_, guint option_id, wtap_opttype_
switch(option_id)
{
case OPT_COMMENT:
size = pcapng_compute_string_option_size(optval->stringval);
size = pcapng_compute_string_option_size(optval);
break;
case OPT_CUSTOM_STR_COPY:
case OPT_CUSTOM_BIN_COPY:
size = pcapng_compute_custom_option_size(optval->custom_opt.custom_data_len);
size = pcapng_compute_custom_option_size(optval);
break;
case OPT_CUSTOM_STR_NO_COPY:
case OPT_CUSTOM_BIN_NO_COPY:
@ -3686,7 +3686,7 @@ static guint32 compute_shb_option_size(wtap_block_t block _U_, guint option_id,
case OPT_SHB_HARDWARE:
case OPT_SHB_OS:
case OPT_SHB_USERAPPL:
size = pcapng_compute_string_option_size(optval->stringval);
size = pcapng_compute_string_option_size(optval);
break;
default:
/* Unknown options - size by datatype? */
@ -3719,7 +3719,7 @@ static gboolean pcapng_write_option_eofopt(wtap_dumper *wdh, int *err)
return TRUE;
}
static gboolean pcapng_write_uint8_option(wtap_dumper *wdh, guint option_id, guint8 uint8, int *err)
static gboolean pcapng_write_uint8_option(wtap_dumper *wdh, guint option_id, wtap_optval_t *optval, int *err)
{
struct pcapng_option_header option_hdr;
const guint32 zero_pad = 0;
@ -3730,7 +3730,7 @@ static gboolean pcapng_write_uint8_option(wtap_dumper *wdh, guint option_id, gui
return FALSE;
wdh->bytes_dumped += 4;
if (!wtap_dump_file_write(wdh, &uint8, 1, err))
if (!wtap_dump_file_write(wdh, &optval->uint8val, 1, err))
return FALSE;
wdh->bytes_dumped += 1;
@ -3741,7 +3741,7 @@ static gboolean pcapng_write_uint8_option(wtap_dumper *wdh, guint option_id, gui
return TRUE;
}
static gboolean pcapng_write_uint64_option(wtap_dumper *wdh, guint option_id, guint64 uint64, int *err)
static gboolean pcapng_write_uint64_option(wtap_dumper *wdh, guint option_id, wtap_optval_t *optval, int *err)
{
struct pcapng_option_header option_hdr;
@ -3751,14 +3751,14 @@ static gboolean pcapng_write_uint64_option(wtap_dumper *wdh, guint option_id, gu
return FALSE;
wdh->bytes_dumped += 4;
if (!wtap_dump_file_write(wdh, &uint64, sizeof(guint64), err))
if (!wtap_dump_file_write(wdh, &optval->uint64val, 8, err))
return FALSE;
wdh->bytes_dumped += 8;
return TRUE;
}
static gboolean pcapng_write_timestamp_option(wtap_dumper *wdh, guint option_id, guint64 timestamp, int *err)
static gboolean pcapng_write_timestamp_option(wtap_dumper *wdh, guint option_id, wtap_optval_t *optval, int *err)
{
struct pcapng_option_header option_hdr;
guint32 high, low;
@ -3769,22 +3769,22 @@ static gboolean pcapng_write_timestamp_option(wtap_dumper *wdh, guint option_id,
return FALSE;
wdh->bytes_dumped += 4;
high = (guint32)(timestamp >> 32);
low = (guint32)(timestamp >> 0);
if (!wtap_dump_file_write(wdh, &high, sizeof(guint32), err))
high = (guint32)(optval->uint64val >> 32);
low = (guint32)(optval->uint64val >> 0);
if (!wtap_dump_file_write(wdh, &high, 4, err))
return FALSE;
wdh->bytes_dumped += 4;
if (!wtap_dump_file_write(wdh, &low, sizeof(guint32), err))
if (!wtap_dump_file_write(wdh, &low, 4, err))
return FALSE;
wdh->bytes_dumped += 4;
return TRUE;
}
static gboolean pcapng_write_string_option(wtap_dumper *wdh, guint option_id, char *str, int *err)
static gboolean pcapng_write_string_option(wtap_dumper *wdh, guint option_id, wtap_optval_t *optval, int *err)
{
struct pcapng_option_header option_hdr;
size_t size = strlen(str);
size_t size = strlen(optval->stringval);
const guint32 zero_pad = 0;
guint32 pad;
@ -3807,7 +3807,7 @@ static gboolean pcapng_write_string_option(wtap_dumper *wdh, guint option_id, ch
return FALSE;
wdh->bytes_dumped += 4;
if (!wtap_dump_file_write(wdh, str, size, err))
if (!wtap_dump_file_write(wdh, optval->stringval, size, err))
return FALSE;
wdh->bytes_dumped += size;
@ -3919,7 +3919,7 @@ static gboolean pcapng_write_if_filter_option(wtap_dumper *wdh, guint option_id,
return TRUE;
}
static gboolean pcapng_write_custom_option(wtap_dumper *wdh, guint option_id, custom_opt_t *custom_option, int *err)
static gboolean pcapng_write_custom_option(wtap_dumper *wdh, guint option_id, wtap_optval_t *optval, int *err)
{
struct pcapng_option_header option_hdr;
const guint32 zero_pad = 0;
@ -3928,7 +3928,7 @@ static gboolean pcapng_write_custom_option(wtap_dumper *wdh, guint option_id, cu
if ((option_id == OPT_CUSTOM_STR_NO_COPY) ||
(option_id == OPT_CUSTOM_BIN_NO_COPY))
return TRUE;
if (custom_option->custom_data_len + sizeof(guint32) > 65535) {
if (optval->custom_opt.custom_data_len + sizeof(guint32) > 65535) {
/*
* Too big to fit in the option.
* Don't write anything.
@ -3940,24 +3940,24 @@ static gboolean pcapng_write_custom_option(wtap_dumper *wdh, guint option_id, cu
/* write option header */
option_hdr.type = (guint16)option_id;
option_hdr.value_length = (guint16)(custom_option->custom_data_len + sizeof(guint32));
option_hdr.value_length = (guint16)(optval->custom_opt.custom_data_len + sizeof(guint32));
if (!wtap_dump_file_write(wdh, &option_hdr, sizeof(struct pcapng_option_header), err))
return FALSE;
wdh->bytes_dumped += sizeof(struct pcapng_option_header);
/* write PEN */
if (!wtap_dump_file_write(wdh, &custom_option->pen, sizeof(guint32), err))
if (!wtap_dump_file_write(wdh, &optval->custom_opt.pen, sizeof(guint32), err))
return FALSE;
wdh->bytes_dumped += sizeof(guint32);
/* write custom data */
if (!wtap_dump_file_write(wdh, custom_option->custom_data, custom_option->custom_data_len, err))
if (!wtap_dump_file_write(wdh, optval->custom_opt.custom_data, optval->custom_opt.custom_data_len, err))
return FALSE;
wdh->bytes_dumped += custom_option->custom_data_len;
wdh->bytes_dumped += optval->custom_opt.custom_data_len;
/* write padding (if any) */
if ((custom_option->custom_data_len % 4)) {
pad = 4 - (custom_option->custom_data_len % 4);
if ((optval->custom_opt.custom_data_len % 4)) {
pad = 4 - (optval->custom_opt.custom_data_len % 4);
} else {
pad = 0;
}
@ -3982,12 +3982,12 @@ static gboolean write_block_option(wtap_block_t block, guint option_id, wtap_opt
switch(option_id)
{
case OPT_COMMENT:
if (!pcapng_write_string_option(write_options->wdh, option_id, optval->stringval, write_options->err))
if (!pcapng_write_string_option(write_options->wdh, option_id, optval, write_options->err))
return FALSE;
break;
case OPT_CUSTOM_STR_COPY:
case OPT_CUSTOM_BIN_COPY:
if (!pcapng_write_custom_option(write_options->wdh, option_id, &optval->custom_opt, write_options->err))
if (!pcapng_write_custom_option(write_options->wdh, option_id, optval, write_options->err))
return FALSE;
break;
case OPT_CUSTOM_STR_NO_COPY:
@ -4031,7 +4031,7 @@ static gboolean write_wtap_shb_option(wtap_dumper *wdh, wtap_block_t block _U_,
case OPT_SHB_HARDWARE:
case OPT_SHB_OS:
case OPT_SHB_USERAPPL:
if (!pcapng_write_string_option(wdh, option_id, optval->stringval, err))
if (!pcapng_write_string_option(wdh, option_id, optval, err))
return FALSE;
break;
default:
@ -4159,7 +4159,7 @@ pcapng_write_enhanced_packet_block(wtap_dumper *wdh, const wtap_rec *rec,
option = g_array_index(rec->custom_options, wtap_option_t, i);
if ((option.option_id == OPT_CUSTOM_STR_COPY) ||
(option.option_id == OPT_CUSTOM_BIN_COPY)) {
options_total_length += pcapng_compute_custom_option_size(option.value.custom_opt.custom_data_len) + 4;
options_total_length += pcapng_compute_custom_option_size(&option.value) + 4;
}
}
}
@ -4354,7 +4354,7 @@ pcapng_write_enhanced_packet_block(wtap_dumper *wdh, const wtap_rec *rec,
option = g_array_index(rec->custom_options, wtap_option_t, i);
if ((option.option_id == OPT_CUSTOM_STR_COPY) ||
(option.option_id == OPT_CUSTOM_BIN_COPY)) {
pcapng_write_custom_option(wdh, option.option_id, &option.value.custom_opt, err);
pcapng_write_custom_option(wdh, option.option_id, &option.value, err);
}
}
}
@ -4750,7 +4750,7 @@ compute_nrb_option_size(wtap_block_t block _U_, guint option_id, wtap_opttype_e
switch(option_id)
{
case OPT_NS_DNSNAME:
size = pcapng_compute_string_option_size(optval->stringval);
size = pcapng_compute_string_option_size(optval);
break;
case OPT_NS_DNSIP4ADDR:
size = 4;
@ -4785,7 +4785,7 @@ put_nrb_option(wtap_block_t block _U_, guint option_id, wtap_opttype_e option_ty
memcpy(*opt_ptrp, &option_hdr, 4);
*opt_ptrp += 4;
memcpy(*opt_ptrp, optval->stringval, size);
memcpy(*opt_ptrp, optval, size);
*opt_ptrp += size;
if ((size % 4)) {
@ -5169,7 +5169,7 @@ static gboolean write_wtap_isb_option(wtap_dumper *wdh, wtap_block_t block _U_,
{
case OPT_ISB_STARTTIME:
case OPT_ISB_ENDTIME:
if (!pcapng_write_timestamp_option(wdh, option_id, optval->uint64val, err))
if (!pcapng_write_timestamp_option(wdh, option_id, optval, err))
return FALSE;
break;
case OPT_ISB_IFRECV:
@ -5177,7 +5177,7 @@ static gboolean write_wtap_isb_option(wtap_dumper *wdh, wtap_block_t block _U_,
case OPT_ISB_FILTERACCEPT:
case OPT_ISB_OSDROP:
case OPT_ISB_USRDELIV:
if (!pcapng_write_uint64_option(wdh, option_id, optval->uint64val, err))
if (!pcapng_write_uint64_option(wdh, option_id, optval, err))
return FALSE;
break;
default:
@ -5242,7 +5242,7 @@ static guint32 compute_idb_option_size(wtap_block_t block _U_, guint option_id,
case OPT_IDB_DESCR:
case OPT_IDB_OS:
case OPT_IDB_HARDWARE:
size = pcapng_compute_string_option_size(optval->stringval);
size = pcapng_compute_string_option_size(optval);
break;
case OPT_IDB_SPEED:
size = 8;
@ -5272,15 +5272,15 @@ static gboolean write_wtap_idb_option(wtap_dumper *wdh, wtap_block_t block _U_,
case OPT_IDB_DESCR:
case OPT_IDB_OS:
case OPT_IDB_HARDWARE:
if (!pcapng_write_string_option(wdh, option_id, optval->stringval, err))
if (!pcapng_write_string_option(wdh, option_id, optval, err))
return FALSE;
break;
case OPT_IDB_SPEED:
if (!pcapng_write_uint64_option(wdh, option_id, optval->uint64val, err))
if (!pcapng_write_uint64_option(wdh, option_id, optval, err))
return FALSE;
break;
case OPT_IDB_TSRESOL:
if (!pcapng_write_uint8_option(wdh, option_id, optval->uint8val, err))
if (!pcapng_write_uint8_option(wdh, option_id, optval, err))
return FALSE;
break;
case OPT_IDB_FILTER:
@ -5288,7 +5288,7 @@ static gboolean write_wtap_idb_option(wtap_dumper *wdh, wtap_block_t block _U_,
return FALSE;
break;
case OPT_IDB_FCSLEN:
if (!pcapng_write_uint8_option(wdh, option_id, optval->uint8val, err))
if (!pcapng_write_uint8_option(wdh, option_id, optval, err))
return FALSE;
break;
default: