2012-03-02 06:02:28 +00:00
|
|
|
/* mp2t.c
|
|
|
|
*
|
|
|
|
* ISO/IEC 13818-1 MPEG2-TS file format decoder for the Wiretap library.
|
|
|
|
* Written by Weston Schmidt <weston_schmidt@alumni.purdue.edu>
|
|
|
|
* Copyright 2012 Weston Schmidt
|
|
|
|
*
|
|
|
|
* Wiretap Library
|
2018-02-07 11:26:45 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2012-03-02 06:02:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "mp2t.h"
|
|
|
|
|
|
|
|
#include "wtap-int.h"
|
2014-07-15 23:40:46 +00:00
|
|
|
#include <wsutil/buffer.h>
|
2012-03-02 06:02:28 +00:00
|
|
|
#include "file_wrappers.h"
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#define MP2T_SYNC_BYTE 0x47
|
|
|
|
#define MP2T_SIZE 188
|
|
|
|
#define MP2T_QAM64_BITRATE 26970350 /* bits per second */
|
2015-03-19 04:51:03 +00:00
|
|
|
#define MP2T_PCR_CLOCK 27000000 /* cycles per second - 27MHz */
|
2012-03-02 06:02:28 +00:00
|
|
|
|
2012-03-30 23:19:49 +00:00
|
|
|
/* we try to detect trailing data up to 40 bytes after each packet */
|
|
|
|
#define TRAILER_LEN_MAX 40
|
|
|
|
|
|
|
|
/* number of consecutive packets we must read to decide that a file
|
|
|
|
is actually an mpeg2 ts */
|
|
|
|
#define SYNC_STEPS 10
|
|
|
|
|
|
|
|
|
2012-03-02 06:02:28 +00:00
|
|
|
typedef struct {
|
2015-12-02 00:35:00 +00:00
|
|
|
guint32 start_offset;
|
2015-03-19 04:51:03 +00:00
|
|
|
guint64 bitrate;
|
2012-03-30 23:19:49 +00:00
|
|
|
/* length of trailing data (e.g. FEC) that's appended after each packet */
|
|
|
|
guint8 trailer_len;
|
2012-03-02 06:02:28 +00:00
|
|
|
} mp2t_filetype_t;
|
|
|
|
|
wiretap: register most built-in file types from its module.
Remove most of the built-in file types from the table in
wiretap/file_access.c and, instead, have the file types register
themselves, using wtap_register_file_type_subtypes().
This reduces the source code changes needed to add a new file type from
three (add the handler, add the file type to the table in file_access.c,
add a #define for the file type in wiretap/wtap.h) to one (add the
handler). (It also requires adding the handler's source file to
wiretap/CMakeLists.txt, but that's required in both cases.)
A few remain because the WTAP_FILE_TYPE_SUBTYPE_ #define is used
elsewhere; that needs to be fixed.
Fix the wiretap/CMakefile.txt file to scan k12text.l, as that now
contains a registration routine. In the process, avoid scanning files
that don't implement a file type and won't ever have a registration
routine.
Add a Lua routine to fetch the total number of file types; we use that
in some code to construct the wtap_filetypes table, which we need to do
in order to continue to have all the values that used to come from the
WTAP_FILE_TYPE_SUBTYPE_ types.
While we're at it, add modelines to a file that lacked them.
2021-02-14 08:34:10 +00:00
|
|
|
static int mp2t_file_type_subtype = -1;
|
|
|
|
|
|
|
|
void register_mp2t(void);
|
|
|
|
|
2013-06-17 23:43:55 +00:00
|
|
|
static gboolean
|
2013-11-08 17:17:57 +00:00
|
|
|
mp2t_read_packet(mp2t_filetype_t *mp2t, FILE_T fh, gint64 offset,
|
2018-02-09 00:19:12 +00:00
|
|
|
wtap_rec *rec, Buffer *buf, int *err,
|
2013-06-17 23:43:55 +00:00
|
|
|
gchar **err_info)
|
2013-05-18 03:42:06 +00:00
|
|
|
{
|
|
|
|
guint64 tmp;
|
2013-06-17 23:43:55 +00:00
|
|
|
|
2016-04-30 23:11:08 +00:00
|
|
|
/*
|
Allow bigger snapshot lengths for D-Bus captures.
Use WTAP_MAX_PACKET_SIZE_STANDARD, set to 256KB, for everything except
for D-Bus captures. Use WTAP_MAX_PACKET_SIZE_DBUS, set to 128MB, for
them, because that's the largest possible D-Bus message size. See
https://bugs.freedesktop.org/show_bug.cgi?id=100220
for an example of the problems caused by limiting the snapshot length to
256KB for D-Bus.
Have a snapshot length of 0 in a capture_file structure mean "there is
no snapshot length for the file"; we don't need the has_snap field in
that case, a value of 0 mean "no, we don't have a snapshot length".
In dumpcap, start out with a pipe buffer size of 2KB, and grow it as
necessary. When checking for a too-big packet from a pipe, check
against the appropriate maximum - 128MB for DLT_DBUS, 256KB for
everything else.
Change-Id: Ib2ce7a0cf37b971fbc0318024fd011e18add8b20
Reviewed-on: https://code.wireshark.org/review/21952
Petri-Dish: Guy Harris <guy@alum.mit.edu>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2017-06-05 01:58:40 +00:00
|
|
|
* MP2T_SIZE will always be less than WTAP_MAX_PACKET_SIZE_STANDARD, so
|
2016-04-30 23:11:08 +00:00
|
|
|
* we don't have to worry about the packet being too big.
|
|
|
|
*/
|
2014-08-02 11:00:48 +00:00
|
|
|
ws_buffer_assure_space(buf, MP2T_SIZE);
|
Add some higher-level file-read APIs and use them.
Add wtap_read_bytes(), which takes a FILE_T, a pointer, a byte count, an
error number pointer, and an error string pointer as arguments, and that
treats a short read of any sort, including a read that returns 0 bytes,
as a WTAP_ERR_SHORT_READ error, and that returns the error number and
string through its last two arguments.
Add wtap_read_bytes_or_eof(), which is similar, but that treats a read
that returns 0 bytes as an EOF, supplying an error number of 0 as an EOF
indication.
Use those in file readers; that simplifies the code and makes it less
likely that somebody will fail to supply the error number and error
string on a file read error.
Change-Id: Ia5dba2a6f81151e87b614461349d611cffc16210
Reviewed-on: https://code.wireshark.org/review/4512
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2014-10-07 01:00:57 +00:00
|
|
|
if (!wtap_read_bytes_or_eof(fh, ws_buffer_start_ptr(buf), MP2T_SIZE, err, err_info))
|
2013-06-17 23:43:55 +00:00
|
|
|
return FALSE;
|
2013-05-18 03:42:06 +00:00
|
|
|
|
2018-02-09 00:19:12 +00:00
|
|
|
rec->rec_type = REC_TYPE_PACKET;
|
2021-08-30 02:12:13 +00:00
|
|
|
rec->block = wtap_block_create(WTAP_BLOCK_PACKET);
|
2014-05-24 18:28:30 +00:00
|
|
|
|
2013-05-18 03:42:06 +00:00
|
|
|
/* XXX - relative, not absolute, time stamps */
|
2018-02-09 00:19:12 +00:00
|
|
|
rec->presence_flags = WTAP_HAS_TS;
|
2013-05-18 03:42:06 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Every packet in an MPEG2-TS stream is has a fixed size of
|
|
|
|
* MP2T_SIZE plus the number of trailer bytes.
|
|
|
|
*
|
2015-08-28 19:05:20 +00:00
|
|
|
* We assume that the bits in the transport stream are supplied at
|
|
|
|
* a constant rate; is that guaranteed by all media that use
|
|
|
|
* MPEG2-TS? If so, the time offset, from the beginning of the
|
|
|
|
* stream, of a given packet is the packet offset, in bits, divided
|
|
|
|
* by the bitrate.
|
2013-05-18 03:42:06 +00:00
|
|
|
*
|
2015-08-28 19:05:20 +00:00
|
|
|
* It would be really cool to be able to configure the bitrate, in
|
|
|
|
* case our attempt to guess it from the PCRs of one of the programs
|
|
|
|
* doesn't get the right answer.
|
2013-05-18 03:42:06 +00:00
|
|
|
*/
|
2015-01-02 00:45:22 +00:00
|
|
|
tmp = ((guint64)(offset - mp2t->start_offset) * 8); /* offset, in bits */
|
2018-02-09 00:19:12 +00:00
|
|
|
rec->ts.secs = (time_t)(tmp / mp2t->bitrate);
|
|
|
|
rec->ts.nsecs = (int)((tmp % mp2t->bitrate) * 1000000000 / mp2t->bitrate);
|
2013-05-18 03:42:06 +00:00
|
|
|
|
2018-02-09 00:19:12 +00:00
|
|
|
rec->rec_header.packet_header.caplen = MP2T_SIZE;
|
|
|
|
rec->rec_header.packet_header.len = MP2T_SIZE;
|
2012-03-02 06:02:28 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-05-23 10:50:02 +00:00
|
|
|
static gboolean
|
2019-04-05 01:56:27 +00:00
|
|
|
mp2t_read(wtap *wth, wtap_rec *rec, Buffer *buf, int *err,
|
|
|
|
gchar **err_info, gint64 *data_offset)
|
2012-03-02 06:02:28 +00:00
|
|
|
{
|
|
|
|
mp2t_filetype_t *mp2t;
|
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
mp2t = (mp2t_filetype_t*) wth->priv;
|
2012-03-02 06:02:28 +00:00
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
*data_offset = file_tell(wth->fh);
|
2012-05-05 02:52:41 +00:00
|
|
|
|
2019-04-05 01:56:27 +00:00
|
|
|
if (!mp2t_read_packet(mp2t, wth->fh, *data_offset, rec, buf, err,
|
|
|
|
err_info)) {
|
2014-05-23 10:50:02 +00:00
|
|
|
return FALSE;
|
2012-03-02 06:02:28 +00:00
|
|
|
}
|
|
|
|
|
2012-03-30 23:19:49 +00:00
|
|
|
/* if there's a trailer, skip it and go to the start of the next packet */
|
2013-06-17 23:43:55 +00:00
|
|
|
if (mp2t->trailer_len!=0) {
|
2016-09-29 04:35:12 +00:00
|
|
|
if (!wtap_read_bytes(wth->fh, NULL, mp2t->trailer_len, err, err_info)) {
|
2014-05-23 10:50:02 +00:00
|
|
|
return FALSE;
|
2013-06-17 23:43:55 +00:00
|
|
|
}
|
2012-03-30 23:19:49 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 10:50:02 +00:00
|
|
|
return TRUE;
|
2012-03-02 06:02:28 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 10:50:02 +00:00
|
|
|
static gboolean
|
2018-02-09 00:19:12 +00:00
|
|
|
mp2t_seek_read(wtap *wth, gint64 seek_off, wtap_rec *rec,
|
2014-01-02 20:47:21 +00:00
|
|
|
Buffer *buf, int *err, gchar **err_info)
|
2012-03-02 06:02:28 +00:00
|
|
|
{
|
2013-05-18 03:42:06 +00:00
|
|
|
mp2t_filetype_t *mp2t;
|
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
if (-1 == file_seek(wth->random_fh, seek_off, SEEK_SET, err)) {
|
2014-05-23 10:50:02 +00:00
|
|
|
return FALSE;
|
2012-03-02 06:02:28 +00:00
|
|
|
}
|
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
mp2t = (mp2t_filetype_t*) wth->priv;
|
2013-05-18 03:42:06 +00:00
|
|
|
|
2018-02-09 00:19:12 +00:00
|
|
|
if (!mp2t_read_packet(mp2t, wth->random_fh, seek_off, rec, buf,
|
2013-06-17 23:43:55 +00:00
|
|
|
err, err_info)) {
|
|
|
|
if (*err == 0)
|
|
|
|
*err = WTAP_ERR_SHORT_READ;
|
2014-05-23 10:50:02 +00:00
|
|
|
return FALSE;
|
2013-06-17 23:43:55 +00:00
|
|
|
}
|
2014-05-23 10:50:02 +00:00
|
|
|
return TRUE;
|
2012-03-02 06:02:28 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 04:51:03 +00:00
|
|
|
static guint64
|
|
|
|
mp2t_read_pcr(guint8 *buffer)
|
|
|
|
{
|
|
|
|
guint64 base;
|
|
|
|
guint64 ext;
|
|
|
|
|
|
|
|
base = pntoh40(buffer);
|
|
|
|
base >>= 7;
|
|
|
|
|
|
|
|
ext = pntoh16(&buffer[4]);
|
|
|
|
ext &= 0x01ff;
|
|
|
|
|
|
|
|
return (base * 300 + ext);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2015-11-23 13:49:03 +00:00
|
|
|
mp2t_find_next_pcr(wtap *wth, guint8 trailer_len,
|
|
|
|
int *err, gchar **err_info, guint32 *idx, guint64 *pcr, guint16 *pid)
|
2015-03-19 04:51:03 +00:00
|
|
|
{
|
2015-12-03 02:08:09 +00:00
|
|
|
guint8 buffer[MP2T_SIZE+TRAILER_LEN_MAX];
|
2015-03-19 04:51:03 +00:00
|
|
|
gboolean found;
|
|
|
|
guint8 afc;
|
2016-04-25 22:12:00 +00:00
|
|
|
guint timeout = 0;
|
2015-03-19 04:51:03 +00:00
|
|
|
|
|
|
|
found = FALSE;
|
2016-04-25 22:12:00 +00:00
|
|
|
while (FALSE == found && timeout++ < SYNC_STEPS * SYNC_STEPS) {
|
2015-04-04 11:53:03 +00:00
|
|
|
(*idx)++;
|
2015-11-23 13:49:03 +00:00
|
|
|
if (!wtap_read_bytes_or_eof(
|
|
|
|
wth->fh, buffer, MP2T_SIZE+trailer_len, err, err_info)) {
|
2015-08-28 19:05:20 +00:00
|
|
|
/* Read error, short read, or EOF */
|
2015-03-19 04:51:03 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MP2T_SYNC_BYTE != buffer[0]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read out the AFC value. */
|
|
|
|
afc = 3 & (buffer[3] >> 4);
|
|
|
|
if (afc < 2) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the length. */
|
|
|
|
if (buffer[4] < 7) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that there is the PCR flag. */
|
|
|
|
if (0x10 != (0x10 & buffer[5])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have a PCR value! */
|
|
|
|
*pcr = mp2t_read_pcr(&buffer[6]);
|
|
|
|
*pid = 0x01ff & pntoh16(&buffer[1]);
|
|
|
|
found = TRUE;
|
|
|
|
}
|
|
|
|
|
2016-04-25 22:12:00 +00:00
|
|
|
return found;
|
2015-03-19 04:51:03 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 19:05:20 +00:00
|
|
|
static wtap_open_return_val
|
2015-12-02 00:35:00 +00:00
|
|
|
mp2t_bits_per_second(wtap *wth, guint32 first, guint8 trailer_len,
|
2015-11-23 13:49:03 +00:00
|
|
|
guint64 *bitrate, int *err, gchar **err_info)
|
2015-03-19 04:51:03 +00:00
|
|
|
{
|
|
|
|
guint32 pn1, pn2;
|
|
|
|
guint64 pcr1, pcr2;
|
|
|
|
guint16 pid1, pid2;
|
2015-04-04 11:53:03 +00:00
|
|
|
guint32 idx;
|
2015-03-19 04:51:03 +00:00
|
|
|
guint64 pcr_delta, bits_passed;
|
|
|
|
|
|
|
|
/* Find the first PCR + PID.
|
|
|
|
* Then find another PCR in that PID.
|
|
|
|
* Take the difference and that's our bitrate.
|
|
|
|
* All the different PCRs in different PIDs 'should' be the same.
|
2015-08-28 19:05:20 +00:00
|
|
|
*
|
|
|
|
* XXX - is this assuming that the time stamps in the PCRs correspond
|
|
|
|
* to the time scale of the underlying transport stream?
|
2015-03-19 04:51:03 +00:00
|
|
|
*/
|
2015-12-02 00:35:00 +00:00
|
|
|
idx = first;
|
2015-03-19 04:51:03 +00:00
|
|
|
|
2015-11-23 13:49:03 +00:00
|
|
|
if (!mp2t_find_next_pcr(wth, trailer_len, err, err_info, &idx, &pcr1, &pid1)) {
|
2015-08-28 19:05:20 +00:00
|
|
|
/* Read error, short read, or EOF */
|
|
|
|
if (*err == WTAP_ERR_SHORT_READ)
|
|
|
|
return WTAP_OPEN_NOT_MINE; /* not a full frame */
|
|
|
|
if (*err != 0)
|
|
|
|
return WTAP_OPEN_ERROR;
|
|
|
|
|
|
|
|
/* We don't have any PCRs, so we can't guess the bit rate.
|
|
|
|
* Default to something reasonable.
|
|
|
|
*/
|
|
|
|
*bitrate = MP2T_QAM64_BITRATE;
|
|
|
|
return WTAP_OPEN_MINE;
|
2015-03-19 04:51:03 +00:00
|
|
|
}
|
|
|
|
|
2015-04-04 11:53:03 +00:00
|
|
|
pn1 = idx;
|
2015-03-19 04:51:03 +00:00
|
|
|
pn2 = pn1;
|
|
|
|
|
|
|
|
while (pn1 == pn2) {
|
2015-11-23 13:49:03 +00:00
|
|
|
if (!mp2t_find_next_pcr(wth, trailer_len, err, err_info, &idx, &pcr2, &pid2)) {
|
2015-08-28 19:05:20 +00:00
|
|
|
/* Read error, short read, or EOF */
|
|
|
|
if (*err == WTAP_ERR_SHORT_READ)
|
|
|
|
return WTAP_OPEN_NOT_MINE; /* not a full frame */
|
|
|
|
if (*err != 0)
|
|
|
|
return WTAP_OPEN_ERROR;
|
|
|
|
|
|
|
|
/* We don't have two PCRs for the same PID, so we can't guess
|
|
|
|
* the bit rate.
|
|
|
|
* Default to something reasonable.
|
|
|
|
*/
|
|
|
|
*bitrate = MP2T_QAM64_BITRATE;
|
|
|
|
return WTAP_OPEN_MINE;
|
2015-03-19 04:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pid1 == pid2) {
|
2015-04-04 11:53:03 +00:00
|
|
|
pn2 = idx;
|
2015-03-19 04:51:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-28 19:05:20 +00:00
|
|
|
if (pcr2 <= pcr1) {
|
|
|
|
/* The PCRs for that PID didn't go forward; treat that as an
|
|
|
|
* indication that this isn't an MPEG-2 TS.
|
|
|
|
*/
|
|
|
|
return WTAP_OPEN_NOT_MINE;
|
|
|
|
}
|
2015-03-19 04:51:03 +00:00
|
|
|
pcr_delta = pcr2 - pcr1;
|
2016-05-10 21:06:57 +00:00
|
|
|
/* cast one of the factors to guint64
|
|
|
|
otherwise, the multiplication would use guint32 and could
|
|
|
|
overflow before the result is assigned to the guint64 bits_passed */
|
|
|
|
bits_passed = (guint64)MP2T_SIZE * (pn2 - pn1) * 8;
|
2015-03-19 04:51:03 +00:00
|
|
|
|
2015-08-28 19:05:20 +00:00
|
|
|
*bitrate = ((MP2T_PCR_CLOCK * bits_passed) / pcr_delta);
|
2015-12-02 00:35:00 +00:00
|
|
|
if (*bitrate == 0) {
|
|
|
|
/* pcr_delta < MP2T_PCR_CLOCK * bits_passed (pn2 != pn1,
|
|
|
|
* as that's the test for the loop above, so bits_passed
|
|
|
|
* is non-zero).
|
|
|
|
*
|
|
|
|
* That will produce a fractional bitrate, which turns
|
|
|
|
* into zero, causing a zero divide later.
|
|
|
|
*
|
|
|
|
* XXX - should we report this as "not ours"? A bitrate
|
|
|
|
* of less than 1 bit per second is not very useful for any
|
|
|
|
* form of audio/video, so presumably that's unlikely to
|
|
|
|
* be an MP2T file.
|
|
|
|
*/
|
|
|
|
return WTAP_OPEN_ERROR;
|
|
|
|
}
|
2015-08-28 19:05:20 +00:00
|
|
|
return WTAP_OPEN_MINE;
|
2015-03-19 04:51:03 +00:00
|
|
|
}
|
|
|
|
|
2014-10-09 23:44:15 +00:00
|
|
|
wtap_open_return_val
|
2014-05-09 05:18:49 +00:00
|
|
|
mp2t_open(wtap *wth, int *err, gchar **err_info)
|
2012-03-02 06:02:28 +00:00
|
|
|
{
|
2012-03-30 23:19:49 +00:00
|
|
|
guint8 buffer[MP2T_SIZE+TRAILER_LEN_MAX];
|
|
|
|
guint8 trailer_len = 0;
|
|
|
|
guint sync_steps = 0;
|
2015-12-02 00:35:00 +00:00
|
|
|
guint i;
|
|
|
|
guint32 first = 0;
|
2012-03-02 06:02:28 +00:00
|
|
|
mp2t_filetype_t *mp2t;
|
2015-08-28 19:05:20 +00:00
|
|
|
wtap_open_return_val status;
|
2015-03-19 04:51:03 +00:00
|
|
|
guint64 bitrate;
|
2012-03-02 06:02:28 +00:00
|
|
|
|
2012-03-30 23:19:49 +00:00
|
|
|
|
Add some higher-level file-read APIs and use them.
Add wtap_read_bytes(), which takes a FILE_T, a pointer, a byte count, an
error number pointer, and an error string pointer as arguments, and that
treats a short read of any sort, including a read that returns 0 bytes,
as a WTAP_ERR_SHORT_READ error, and that returns the error number and
string through its last two arguments.
Add wtap_read_bytes_or_eof(), which is similar, but that treats a read
that returns 0 bytes as an EOF, supplying an error number of 0 as an EOF
indication.
Use those in file readers; that simplifies the code and makes it less
likely that somebody will fail to supply the error number and error
string on a file read error.
Change-Id: Ia5dba2a6f81151e87b614461349d611cffc16210
Reviewed-on: https://code.wireshark.org/review/4512
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2014-10-07 01:00:57 +00:00
|
|
|
if (!wtap_read_bytes(wth->fh, buffer, MP2T_SIZE, err, err_info)) {
|
|
|
|
if (*err != WTAP_ERR_SHORT_READ)
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_ERROR;
|
|
|
|
return WTAP_OPEN_NOT_MINE;
|
2012-03-02 06:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < MP2T_SIZE; i++) {
|
|
|
|
if (MP2T_SYNC_BYTE == buffer[i]) {
|
|
|
|
first = i;
|
2015-12-02 00:35:00 +00:00
|
|
|
goto found;
|
2012-03-02 06:02:28 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-02 00:35:00 +00:00
|
|
|
/*
|
|
|
|
* No sync bytes found, so not an MPEG-2 Transport Stream file.
|
|
|
|
*/
|
|
|
|
return WTAP_OPEN_NOT_MINE; /* wrong file type - not an mpeg2 ts file */
|
2012-03-02 06:02:28 +00:00
|
|
|
|
2015-12-02 00:35:00 +00:00
|
|
|
found:
|
2014-05-09 05:18:49 +00:00
|
|
|
if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_ERROR;
|
2012-03-05 08:30:39 +00:00
|
|
|
}
|
2015-03-19 04:51:03 +00:00
|
|
|
|
2012-03-30 23:19:49 +00:00
|
|
|
/* read some packets and make sure they all start with a sync byte */
|
|
|
|
do {
|
2014-10-07 22:48:29 +00:00
|
|
|
if (!wtap_read_bytes(wth->fh, buffer, MP2T_SIZE+trailer_len, err, err_info)) {
|
|
|
|
if (*err != WTAP_ERR_SHORT_READ)
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_ERROR; /* read error */
|
|
|
|
if(sync_steps<2) return WTAP_OPEN_NOT_MINE; /* wrong file type - not an mpeg2 ts file */
|
2012-03-30 23:19:49 +00:00
|
|
|
break; /* end of file, that's ok if we're still in sync */
|
2012-04-18 08:32:12 +00:00
|
|
|
}
|
2012-03-30 23:19:49 +00:00
|
|
|
if (buffer[0] == MP2T_SYNC_BYTE) {
|
|
|
|
sync_steps++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* no sync byte found, check if trailing data is appended
|
|
|
|
and we have to increase the packet size */
|
|
|
|
|
|
|
|
/* if we've already detected a trailer field, we must remain in sync
|
|
|
|
another mismatch means we have no mpeg2 ts file */
|
|
|
|
if (trailer_len>0)
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_NOT_MINE;
|
2012-03-30 23:19:49 +00:00
|
|
|
|
|
|
|
/* check if a trailer is appended to the packet */
|
|
|
|
for (i=0; i<TRAILER_LEN_MAX; i++) {
|
|
|
|
if (buffer[i] == MP2T_SYNC_BYTE) {
|
|
|
|
trailer_len = i;
|
2014-05-09 05:18:49 +00:00
|
|
|
if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_ERROR;
|
2012-03-30 23:19:49 +00:00
|
|
|
}
|
|
|
|
sync_steps = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* no sync byte found in the vicinity, this is no mpeg2 ts file */
|
|
|
|
if (i==TRAILER_LEN_MAX)
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_NOT_MINE;
|
2012-03-30 23:19:49 +00:00
|
|
|
}
|
|
|
|
} while (sync_steps < SYNC_STEPS);
|
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_ERROR;
|
2012-03-02 06:02:28 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 04:51:03 +00:00
|
|
|
/* Ensure there is a valid bitrate */
|
2015-11-23 13:49:03 +00:00
|
|
|
status = mp2t_bits_per_second(wth, first, trailer_len,
|
|
|
|
&bitrate, err, err_info);
|
2015-08-28 19:05:20 +00:00
|
|
|
if (status != WTAP_OPEN_MINE) {
|
|
|
|
return status;
|
|
|
|
}
|
2015-03-19 04:51:03 +00:00
|
|
|
|
|
|
|
if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
|
|
|
|
return WTAP_OPEN_ERROR;
|
|
|
|
}
|
|
|
|
|
wiretap: register most built-in file types from its module.
Remove most of the built-in file types from the table in
wiretap/file_access.c and, instead, have the file types register
themselves, using wtap_register_file_type_subtypes().
This reduces the source code changes needed to add a new file type from
three (add the handler, add the file type to the table in file_access.c,
add a #define for the file type in wiretap/wtap.h) to one (add the
handler). (It also requires adding the handler's source file to
wiretap/CMakeLists.txt, but that's required in both cases.)
A few remain because the WTAP_FILE_TYPE_SUBTYPE_ #define is used
elsewhere; that needs to be fixed.
Fix the wiretap/CMakefile.txt file to scan k12text.l, as that now
contains a registration routine. In the process, avoid scanning files
that don't implement a file type and won't ever have a registration
routine.
Add a Lua routine to fetch the total number of file types; we use that
in some code to construct the wtap_filetypes table, which we need to do
in order to continue to have all the values that used to come from the
WTAP_FILE_TYPE_SUBTYPE_ types.
While we're at it, add modelines to a file that lacked them.
2021-02-14 08:34:10 +00:00
|
|
|
wth->file_type_subtype = mp2t_file_type_subtype;
|
2014-05-09 05:18:49 +00:00
|
|
|
wth->file_encap = WTAP_ENCAP_MPEG_2_TS;
|
2014-09-28 18:37:06 +00:00
|
|
|
wth->file_tsprec = WTAP_TSPREC_NSEC;
|
2014-05-09 05:18:49 +00:00
|
|
|
wth->subtype_read = mp2t_read;
|
|
|
|
wth->subtype_seek_read = mp2t_seek_read;
|
|
|
|
wth->snapshot_length = 0;
|
2012-03-02 06:02:28 +00:00
|
|
|
|
2020-12-21 02:30:28 +00:00
|
|
|
mp2t = g_new(mp2t_filetype_t, 1);
|
2012-03-02 06:02:28 +00:00
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
wth->priv = mp2t;
|
2012-05-05 02:52:41 +00:00
|
|
|
mp2t->start_offset = first;
|
2012-03-30 23:19:49 +00:00
|
|
|
mp2t->trailer_len = trailer_len;
|
2015-03-19 04:51:03 +00:00
|
|
|
mp2t->bitrate = bitrate;
|
2012-03-02 06:02:28 +00:00
|
|
|
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_MINE;
|
2012-03-02 06:02:28 +00:00
|
|
|
}
|
2015-01-02 00:45:22 +00:00
|
|
|
|
wiretap: have file handlers advertise blocks and options supported.
Instead of a "supports name resolution" Boolean and bitflags for types of
comments supported, provide a list of block types that the file
type/subtype supports, with each block type having a list of options
supported. Indicate whether "supported" means "one instance" or
"multiple instances".
"Supports" doesn't just mean "can be written", it also means "could be
read".
Rename WTAP_BLOCK_IF_DESCRIPTION to WTAP_BLOCK_IF_ID_AND_INFO, to
indicate that it provides, in addition to information about the
interface, an ID (implicitly, in pcapng files, by its ordinal number)
that is associated with every packet in the file. Emphasize that in
comments - just because your capture file format can list the interfaces
on which a capture was done, that doesn't mean it supports this; it
doesn't do so if the file doesn't indicate, for every packet, on which
of those interfaces it was captured (I'm looking at *you*, Microsoft
Network Monitor...).
Use APIs to query that information to do what the "does this file
type/subtype support name resolution information", "does this file
type/subtype support all of these comment types", and "does this file
type/subtype support - and require - interface IDs" APIs did.
Provide backwards compatibility for Lua.
This allows us to eliminate the WTAP_FILE_TYPE_SUBTYPE_ values for IBM's
iptrace; do so.
2021-02-21 22:18:04 +00:00
|
|
|
static const struct supported_block_type mp2t_blocks_supported[] = {
|
|
|
|
/*
|
|
|
|
* We support packet blocks, with no comments or other options.
|
|
|
|
*/
|
|
|
|
{ WTAP_BLOCK_PACKET, MULTIPLE_BLOCKS_SUPPORTED, NO_OPTIONS_SUPPORTED }
|
|
|
|
};
|
|
|
|
|
wiretap: register most built-in file types from its module.
Remove most of the built-in file types from the table in
wiretap/file_access.c and, instead, have the file types register
themselves, using wtap_register_file_type_subtypes().
This reduces the source code changes needed to add a new file type from
three (add the handler, add the file type to the table in file_access.c,
add a #define for the file type in wiretap/wtap.h) to one (add the
handler). (It also requires adding the handler's source file to
wiretap/CMakeLists.txt, but that's required in both cases.)
A few remain because the WTAP_FILE_TYPE_SUBTYPE_ #define is used
elsewhere; that needs to be fixed.
Fix the wiretap/CMakefile.txt file to scan k12text.l, as that now
contains a registration routine. In the process, avoid scanning files
that don't implement a file type and won't ever have a registration
routine.
Add a Lua routine to fetch the total number of file types; we use that
in some code to construct the wtap_filetypes table, which we need to do
in order to continue to have all the values that used to come from the
WTAP_FILE_TYPE_SUBTYPE_ types.
While we're at it, add modelines to a file that lacked them.
2021-02-14 08:34:10 +00:00
|
|
|
static const struct file_type_subtype_info mp2t_info = {
|
|
|
|
"MPEG2 transport stream", "mp2t", "mp2t", "ts;mpg",
|
wiretap: have file handlers advertise blocks and options supported.
Instead of a "supports name resolution" Boolean and bitflags for types of
comments supported, provide a list of block types that the file
type/subtype supports, with each block type having a list of options
supported. Indicate whether "supported" means "one instance" or
"multiple instances".
"Supports" doesn't just mean "can be written", it also means "could be
read".
Rename WTAP_BLOCK_IF_DESCRIPTION to WTAP_BLOCK_IF_ID_AND_INFO, to
indicate that it provides, in addition to information about the
interface, an ID (implicitly, in pcapng files, by its ordinal number)
that is associated with every packet in the file. Emphasize that in
comments - just because your capture file format can list the interfaces
on which a capture was done, that doesn't mean it supports this; it
doesn't do so if the file doesn't indicate, for every packet, on which
of those interfaces it was captured (I'm looking at *you*, Microsoft
Network Monitor...).
Use APIs to query that information to do what the "does this file
type/subtype support name resolution information", "does this file
type/subtype support all of these comment types", and "does this file
type/subtype support - and require - interface IDs" APIs did.
Provide backwards compatibility for Lua.
This allows us to eliminate the WTAP_FILE_TYPE_SUBTYPE_ values for IBM's
iptrace; do so.
2021-02-21 22:18:04 +00:00
|
|
|
FALSE, BLOCKS_SUPPORTED(mp2t_blocks_supported),
|
wiretap: register most built-in file types from its module.
Remove most of the built-in file types from the table in
wiretap/file_access.c and, instead, have the file types register
themselves, using wtap_register_file_type_subtypes().
This reduces the source code changes needed to add a new file type from
three (add the handler, add the file type to the table in file_access.c,
add a #define for the file type in wiretap/wtap.h) to one (add the
handler). (It also requires adding the handler's source file to
wiretap/CMakeLists.txt, but that's required in both cases.)
A few remain because the WTAP_FILE_TYPE_SUBTYPE_ #define is used
elsewhere; that needs to be fixed.
Fix the wiretap/CMakefile.txt file to scan k12text.l, as that now
contains a registration routine. In the process, avoid scanning files
that don't implement a file type and won't ever have a registration
routine.
Add a Lua routine to fetch the total number of file types; we use that
in some code to construct the wtap_filetypes table, which we need to do
in order to continue to have all the values that used to come from the
WTAP_FILE_TYPE_SUBTYPE_ types.
While we're at it, add modelines to a file that lacked them.
2021-02-14 08:34:10 +00:00
|
|
|
NULL, NULL, NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
void register_mp2t(void)
|
|
|
|
{
|
2021-02-24 03:10:35 +00:00
|
|
|
mp2t_file_type_subtype = wtap_register_file_type_subtype(&mp2t_info);
|
wiretap: more work on file type/subtypes.
Provide a wiretap routine to get an array of all savable file
type/subtypes, sorted with pcap and pcapng at the top, followed by the
other types, sorted either by the name or the description.
Use that routine to list options for the -F flag for various commands
Rename wtap_get_savable_file_types_subtypes() to
wtap_get_savable_file_types_subtypes_for_file(), to indicate that it
provides an array of all file type/subtypes in which a given file can be
saved. Have it sort all types, other than the default type/subtype and,
if there is one, the "other" type (both of which are put at the top), by
the name or the description.
Don't allow wtap_register_file_type_subtypes() to override any existing
registrations; have them always register a new type. In that routine,
if there are any emply slots in the table, due to an entry being
unregistered, use it rather than allocating a new slot.
Don't allow unregistration of built-in types.
Rename the "dump open table" to the "file type/subtype table", as it has
entries for all types/subtypes, even if we can't write them.
Initialize that table in a routine that pre-allocates the GArray before
filling it with built-in types/subtypes, so it doesn't keep getting
reallocated.
Get rid of wtap_num_file_types_subtypes - it's just a copy of the size
of the GArray.
Don't have wtap_file_type_subtype_description() crash if handed an
file type/subtype that isn't a valid array index - just return NULL, as
we do with wtap_file_type_subtype_name().
In wtap_name_to_file_type_subtype(), don't use WTAP_FILE_TYPE_SUBTYPE_
names for the backwards-compatibility names - map those names to the
current names, and then look them up. This reduces the number of
uses of hardwired WTAP_FILE_TYPE_SUBTYPE_ values.
Clean up the type of wtap_module_count - it has no need to be a gulong.
Have built-in wiretap file handlers register names to be used for their
file type/subtypes, rather than building the table in init.lua.
Add a new Lua C function get_wtap_filetypes() to construct the
wtap_filetypes table, based on the registered names, and use it in
init.lua.
Add a #define WSLUA_INTERNAL_FUNCTION to register functions intended
only for internal use in init.lua, so they can be made available from
Lua without being documented.
Get rid of WTAP_NUM_FILE_TYPES_SUBTYPES - most code has no need to use
it, as it can just request arrays of types, and the space of
type/subtype codes can be sparse due to registration in any case, so
code has to be careful using it.
wtap_get_num_file_types_subtypes() is no longer used, so remove it. It
returns the number of elements in the file type/subtype array, which is
not necessarily the name of known file type/subtypes, as there may have
been some deregistered types, and those types do *not* get removed from
the array, they just get cleared so that they're available for future
allocation (we don't want the indices of any registered types to changes
if another type is deregistered, as those indicates are the type/subtype
values, so we can't shrink the array).
Clean up white space and remove some comments that shouldn't have been
added.
2021-02-17 06:24:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Register name for backwards compatibility with the
|
|
|
|
* wtap_filetypes table in Lua.
|
|
|
|
*/
|
|
|
|
wtap_register_backwards_compatibility_lua_name("MPEG_2_TS",
|
|
|
|
mp2t_file_type_subtype);
|
wiretap: register most built-in file types from its module.
Remove most of the built-in file types from the table in
wiretap/file_access.c and, instead, have the file types register
themselves, using wtap_register_file_type_subtypes().
This reduces the source code changes needed to add a new file type from
three (add the handler, add the file type to the table in file_access.c,
add a #define for the file type in wiretap/wtap.h) to one (add the
handler). (It also requires adding the handler's source file to
wiretap/CMakeLists.txt, but that's required in both cases.)
A few remain because the WTAP_FILE_TYPE_SUBTYPE_ #define is used
elsewhere; that needs to be fixed.
Fix the wiretap/CMakefile.txt file to scan k12text.l, as that now
contains a registration routine. In the process, avoid scanning files
that don't implement a file type and won't ever have a registration
routine.
Add a Lua routine to fetch the total number of file types; we use that
in some code to construct the wtap_filetypes table, which we need to do
in order to continue to have all the values that used to come from the
WTAP_FILE_TYPE_SUBTYPE_ types.
While we're at it, add modelines to a file that lacked them.
2021-02-14 08:34:10 +00:00
|
|
|
}
|
|
|
|
|
2015-01-02 00:45:22 +00:00
|
|
|
/*
|
2019-07-26 18:43:17 +00:00
|
|
|
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
2015-01-02 00:45:22 +00:00
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* vi: set shiftwidth=4 tabstop=8 expandtab:
|
|
|
|
* :indentSize=4:tabSize=8:noTabs=true:
|
|
|
|
*/
|