2007-03-22 10:44:33 +00:00
|
|
|
/* mpeg.c
|
|
|
|
*
|
|
|
|
* MPEG file format decoder for the Wiretap library.
|
|
|
|
* Written by Shaun Jackman <sjackman@gmail.com>
|
|
|
|
* Copyright 2007 Shaun Jackman
|
|
|
|
*
|
|
|
|
* Wiretap Library
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-06-28 22:56:06 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-03-22 10:44:33 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2007-03-22 11:33:18 +00:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:44:33 +00:00
|
|
|
#include "mpeg.h"
|
2008-05-20 21:51:01 +00:00
|
|
|
#include "wsutil/mpeg-audio.h"
|
2007-03-22 10:44:33 +00:00
|
|
|
|
|
|
|
#include "wtap-int.h"
|
2014-07-15 23:40:46 +00:00
|
|
|
#include <wsutil/buffer.h>
|
2007-03-22 10:44:33 +00:00
|
|
|
#include "file_wrappers.h"
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#define PES_PREFIX 1
|
|
|
|
#define PES_VALID(n) (((n) >> 8 & 0xffffff) == PES_PREFIX)
|
|
|
|
|
2010-02-26 07:59:54 +00:00
|
|
|
typedef struct {
|
2013-11-09 10:38:02 +00:00
|
|
|
nstime_t now;
|
2010-02-26 07:59:54 +00:00
|
|
|
time_t t0;
|
|
|
|
} mpeg_t;
|
|
|
|
|
2013-11-08 17:17:57 +00:00
|
|
|
static int
|
2013-12-31 23:51:10 +00:00
|
|
|
mpeg_resync(FILE_T fh, int *err)
|
2007-03-22 10:44:33 +00:00
|
|
|
{
|
2013-12-31 23:51:10 +00:00
|
|
|
gint64 offset = file_tell(fh);
|
2009-03-09 21:18:55 +00:00
|
|
|
int count = 0;
|
2013-12-31 23:51:10 +00:00
|
|
|
int byte = file_getc(fh);
|
2007-03-22 10:44:33 +00:00
|
|
|
|
2008-06-23 20:06:20 +00:00
|
|
|
while (byte != EOF) {
|
|
|
|
if (byte == 0xff && count > 0) {
|
2013-12-31 23:51:10 +00:00
|
|
|
byte = file_getc(fh);
|
2008-06-23 20:06:20 +00:00
|
|
|
if (byte != EOF && (byte & 0xe0) == 0xe0)
|
2007-03-22 10:44:33 +00:00
|
|
|
break;
|
|
|
|
} else
|
2013-12-31 23:51:10 +00:00
|
|
|
byte = file_getc(fh);
|
2007-03-22 10:44:33 +00:00
|
|
|
count++;
|
|
|
|
}
|
2013-12-31 23:51:10 +00:00
|
|
|
if (file_seek(fh, offset, SEEK_SET, err) == -1)
|
2011-03-31 18:28:52 +00:00
|
|
|
return 0;
|
2007-03-22 10:44:33 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2007-08-21 22:00:06 +00:00
|
|
|
#define SCRHZ 27000000
|
|
|
|
|
2013-11-08 17:17:57 +00:00
|
|
|
static gboolean
|
2014-05-09 05:18:49 +00:00
|
|
|
mpeg_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
|
2013-12-31 23:51:10 +00:00
|
|
|
gboolean is_random, int *err, gchar **err_info)
|
2007-03-22 10:44:33 +00:00
|
|
|
{
|
2014-05-09 05:18:49 +00:00
|
|
|
mpeg_t *mpeg = (mpeg_t *)wth->priv;
|
2007-03-22 10:44:33 +00:00
|
|
|
guint32 n;
|
2009-03-09 21:18:55 +00:00
|
|
|
unsigned int packet_size;
|
2013-11-09 10:38:02 +00:00
|
|
|
nstime_t ts = mpeg->now;
|
2007-03-22 10:44:33 +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
|
|
|
/*
|
|
|
|
* All packets have at least 4 bytes in them. Read the first
|
|
|
|
* 4 bytes and determine whether it's a PES packet or not
|
|
|
|
* based on that.
|
|
|
|
*
|
|
|
|
* XXX - can an MPEG file contain a mixture of PES and non-PES
|
|
|
|
* packets? If not, can we determine whether the packets will
|
|
|
|
* be PES packets or not based on the magic number (i.e., if the
|
|
|
|
* file begins with 0x00 0x00 0x01, it contains PES packets,
|
|
|
|
* otherwise it doesn't)?
|
|
|
|
*/
|
|
|
|
if (!wtap_read_bytes_or_eof(fh, &n, sizeof n, err, err_info))
|
2007-03-22 10:44:33 +00:00
|
|
|
return FALSE;
|
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 (file_seek(fh, -(gint64)(sizeof n), SEEK_CUR, err) == -1)
|
|
|
|
return FALSE;
|
|
|
|
n = g_ntohl(n);
|
2007-03-22 10:44:33 +00:00
|
|
|
if (PES_VALID(n)) {
|
2013-12-31 23:51:10 +00:00
|
|
|
gint64 offset = file_tell(fh);
|
2007-03-22 10:44:33 +00:00
|
|
|
guint8 stream;
|
|
|
|
|
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 (!file_skip(fh, 3, err))
|
2007-03-22 10:44:33 +00:00
|
|
|
return FALSE;
|
|
|
|
|
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(fh, &stream, sizeof stream, err, err_info))
|
2007-03-22 10:44:33 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (stream == 0xba) {
|
|
|
|
guint32 pack1;
|
|
|
|
guint32 pack0;
|
|
|
|
guint64 pack;
|
|
|
|
guint8 stuffing;
|
|
|
|
|
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(fh, &pack1, sizeof pack1, err, err_info))
|
2007-03-22 10:44:33 +00:00
|
|
|
return FALSE;
|
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(fh, &pack0, sizeof pack0, err, err_info))
|
2007-03-22 10:44:33 +00:00
|
|
|
return FALSE;
|
|
|
|
pack = (guint64)g_ntohl(pack1) << 32 | g_ntohl(pack0);
|
|
|
|
|
|
|
|
switch (pack >> 62) {
|
|
|
|
case 1:
|
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 (!file_skip(fh, 1, err))
|
2007-03-22 10:44:33 +00:00
|
|
|
return FALSE;
|
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(fh, &stuffing,
|
|
|
|
sizeof stuffing, err, err_info))
|
2007-03-22 10:44:33 +00:00
|
|
|
return FALSE;
|
|
|
|
stuffing &= 0x07;
|
|
|
|
packet_size = 14 + stuffing;
|
|
|
|
|
2013-12-31 23:51:10 +00:00
|
|
|
if (!is_random) {
|
2007-08-21 22:00:06 +00:00
|
|
|
guint64 bytes = pack >> 16;
|
2008-06-23 20:06:20 +00:00
|
|
|
guint64 ts_val =
|
2007-08-21 22:00:06 +00:00
|
|
|
(bytes >> 43 & 0x0007) << 30 |
|
|
|
|
(bytes >> 27 & 0x7fff) << 15 |
|
|
|
|
(bytes >> 11 & 0x7fff) << 0;
|
2013-01-04 05:22:43 +00:00
|
|
|
guint ext = (guint)((bytes >> 1) & 0x1ff);
|
2008-06-23 20:06:20 +00:00
|
|
|
guint64 cr = 300 * ts_val + ext;
|
2013-01-04 05:22:43 +00:00
|
|
|
guint rem = (guint)(cr % SCRHZ);
|
2010-02-26 07:59:54 +00:00
|
|
|
mpeg->now.secs
|
|
|
|
= mpeg->t0 + (time_t)(cr / SCRHZ);
|
|
|
|
mpeg->now.nsecs
|
2007-08-22 02:45:42 +00:00
|
|
|
= (int)(G_GINT64_CONSTANT(1000000000) * rem / SCRHZ);
|
2007-08-21 22:00:06 +00:00
|
|
|
}
|
2010-02-26 07:59:54 +00:00
|
|
|
ts = mpeg->now;
|
2007-03-22 10:44:33 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
packet_size = 12;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
guint16 length;
|
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(fh, &length, sizeof length, err, err_info))
|
2007-03-22 10:44:33 +00:00
|
|
|
return FALSE;
|
|
|
|
length = g_ntohs(length);
|
|
|
|
packet_size = 6 + length;
|
|
|
|
}
|
|
|
|
|
2013-12-31 23:51:10 +00:00
|
|
|
if (file_seek(fh, offset, SEEK_SET, err) == -1)
|
2007-03-22 10:44:33 +00:00
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
struct mpa mpa;
|
|
|
|
|
|
|
|
MPA_UNMARSHAL(&mpa, n);
|
|
|
|
if (MPA_VALID(&mpa)) {
|
|
|
|
packet_size = MPA_BYTES(&mpa);
|
2013-12-31 23:51:10 +00:00
|
|
|
if (!is_random) {
|
|
|
|
mpeg->now.nsecs += MPA_DURATION_NS(&mpa);
|
|
|
|
if (mpeg->now.nsecs >= 1000000000) {
|
|
|
|
mpeg->now.secs++;
|
|
|
|
mpeg->now.nsecs -= 1000000000;
|
|
|
|
}
|
2007-03-22 10:44:33 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-12-31 23:51:10 +00:00
|
|
|
packet_size = mpeg_resync(fh, err);
|
2007-03-22 10:44:33 +00:00
|
|
|
if (packet_size == 0)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-31 23:51:10 +00:00
|
|
|
if (!wtap_read_packet_bytes(fh, buf, packet_size, err, err_info))
|
2007-03-22 10:44:33 +00:00
|
|
|
return FALSE;
|
2013-12-31 23:51:10 +00:00
|
|
|
|
2014-05-24 18:28:30 +00:00
|
|
|
phdr->rec_type = REC_TYPE_PACKET;
|
|
|
|
|
2012-03-02 10:17:36 +00:00
|
|
|
/* XXX - relative, not absolute, time stamps */
|
2013-12-31 23:51:10 +00:00
|
|
|
if (!is_random) {
|
|
|
|
phdr->presence_flags = WTAP_HAS_TS;
|
|
|
|
phdr->ts = ts;
|
|
|
|
}
|
|
|
|
phdr->caplen = packet_size;
|
|
|
|
phdr->len = packet_size;
|
|
|
|
|
2007-03-22 10:44:33 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-05-23 10:50:02 +00:00
|
|
|
static gboolean
|
2014-05-09 05:18:49 +00:00
|
|
|
mpeg_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
|
2013-12-31 23:51:10 +00:00
|
|
|
{
|
2014-05-09 05:18:49 +00:00
|
|
|
*data_offset = file_tell(wth->fh);
|
2013-12-31 23:51:10 +00:00
|
|
|
|
2014-05-23 10:50:02 +00:00
|
|
|
return mpeg_read_packet(wth, wth->fh, &wth->phdr, wth->frame_buffer,
|
|
|
|
FALSE, err, err_info);
|
2013-12-31 23:51:10 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 10:50:02 +00:00
|
|
|
static gboolean
|
2014-05-09 05:18:49 +00:00
|
|
|
mpeg_seek_read(wtap *wth, gint64 seek_off,
|
|
|
|
struct wtap_pkthdr *phdr, Buffer *buf,
|
2011-04-21 09:41:52 +00:00
|
|
|
int *err, gchar **err_info)
|
2007-03-22 10:44:33 +00:00
|
|
|
{
|
2014-05-09 05:18:49 +00:00
|
|
|
if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
|
2014-05-23 10:50:02 +00:00
|
|
|
return FALSE;
|
2013-12-31 23:51:10 +00:00
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
if (!mpeg_read_packet(wth, wth->random_fh, phdr, buf, TRUE, err,
|
2013-12-31 23:51:10 +00:00
|
|
|
err_info)) {
|
|
|
|
if (*err == 0)
|
|
|
|
*err = WTAP_ERR_SHORT_READ;
|
2014-05-23 10:50:02 +00:00
|
|
|
return FALSE;
|
2013-12-31 23:51:10 +00:00
|
|
|
}
|
2014-05-23 10:50:02 +00:00
|
|
|
return TRUE;
|
2007-03-22 10:44:33 +00:00
|
|
|
}
|
|
|
|
|
2007-03-25 19:00:59 +00:00
|
|
|
struct _mpeg_magic {
|
|
|
|
size_t len;
|
2007-04-21 22:58:03 +00:00
|
|
|
const gchar* match;
|
2007-03-25 19:00:59 +00:00
|
|
|
} magic[] = {
|
2007-08-06 22:34:26 +00:00
|
|
|
{ 3, "TAG" }, /* ID3v1 */
|
|
|
|
{ 3, "ID3" }, /* ID3v2 */
|
|
|
|
{ 3, "\0\0\1" }, /* MPEG PES */
|
2008-07-09 19:15:50 +00:00
|
|
|
{ 2, "\xff\xfb" }, /* MP3, taken from http://en.wikipedia.org/wiki/MP3#File_structure */
|
2007-08-06 22:34:26 +00:00
|
|
|
{ 0, NULL }
|
2007-03-25 19:00:59 +00:00
|
|
|
};
|
|
|
|
|
2014-10-09 23:44:15 +00:00
|
|
|
wtap_open_return_val
|
2014-05-09 05:18:49 +00:00
|
|
|
mpeg_open(wtap *wth, int *err, gchar **err_info)
|
2007-03-22 10:44:33 +00:00
|
|
|
{
|
2007-03-25 19:00:59 +00:00
|
|
|
char magic_buf[16];
|
All private data for a capture in a Wiretap module must be per-file, not
static to the module.
Add the older(?) ID tag for MPEG audio.
Just use the ID at the beginning to identify MPEG audio files; don't
check the file any further.
If the read of the magic number doesn't work, get the error, and, if
there is no error (i.e., it's a short read), just return 0 (meaning "no
error, but this isn't that type of file).
Similarly, if the magic number doesn't match, just return 0, so other
types of file are tried.
svn path=/trunk/; revision=21192
2007-03-25 22:07:40 +00:00
|
|
|
struct _mpeg_magic* m;
|
2010-02-26 07:59:54 +00:00
|
|
|
mpeg_t *mpeg;
|
2013-11-08 17:17:57 +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, magic_buf, sizeof magic_buf,
|
|
|
|
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;
|
2007-03-25 19:00:59 +00:00
|
|
|
}
|
|
|
|
|
2008-07-09 19:15:50 +00:00
|
|
|
for (m=magic; m->match; m++) {
|
|
|
|
if (memcmp(magic_buf, m->match, m->len) == 0)
|
2007-03-25 19:00:59 +00:00
|
|
|
goto good_magic;
|
|
|
|
}
|
2013-11-08 17:17:57 +00:00
|
|
|
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_NOT_MINE;
|
All private data for a capture in a Wiretap module must be per-file, not
static to the module.
Add the older(?) ID tag for MPEG audio.
Just use the ID at the beginning to identify MPEG audio files; don't
check the file any further.
If the read of the magic number doesn't work, get the error, and, if
there is no error (i.e., it's a short read), just return 0 (meaning "no
error, but this isn't that type of file).
Similarly, if the magic number doesn't match, just return 0, so other
types of file are tried.
svn path=/trunk/; revision=21192
2007-03-25 22:07:40 +00:00
|
|
|
|
2007-03-25 19:00:59 +00:00
|
|
|
good_magic:
|
All private data for a capture in a Wiretap module must be per-file, not
static to the module.
Add the older(?) ID tag for MPEG audio.
Just use the ID at the beginning to identify MPEG audio files; don't
check the file any further.
If the read of the magic number doesn't work, get the error, and, if
there is no error (i.e., it's a short read), just return 0 (meaning "no
error, but this isn't that type of file).
Similarly, if the magic number doesn't match, just return 0, so other
types of file are tried.
svn path=/trunk/; revision=21192
2007-03-25 22:07:40 +00:00
|
|
|
/* This appears to be a file with MPEG data. */
|
2014-05-09 05:18:49 +00:00
|
|
|
if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_ERROR;
|
2007-03-22 10:44:33 +00:00
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_MPEG;
|
|
|
|
wth->file_encap = WTAP_ENCAP_MPEG;
|
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 = mpeg_read;
|
|
|
|
wth->subtype_seek_read = mpeg_seek_read;
|
|
|
|
wth->snapshot_length = 0;
|
All private data for a capture in a Wiretap module must be per-file, not
static to the module.
Add the older(?) ID tag for MPEG audio.
Just use the ID at the beginning to identify MPEG audio files; don't
check the file any further.
If the read of the magic number doesn't work, get the error, and, if
there is no error (i.e., it's a short read), just return 0 (meaning "no
error, but this isn't that type of file).
Similarly, if the magic number doesn't match, just return 0, so other
types of file are tried.
svn path=/trunk/; revision=21192
2007-03-25 22:07:40 +00:00
|
|
|
|
2010-02-26 07:59:54 +00:00
|
|
|
mpeg = (mpeg_t *)g_malloc(sizeof(mpeg_t));
|
2014-05-09 05:18:49 +00:00
|
|
|
wth->priv = (void *)mpeg;
|
2012-02-25 23:24:34 +00:00
|
|
|
mpeg->now.secs = 0;
|
2010-02-26 07:59:54 +00:00
|
|
|
mpeg->now.nsecs = 0;
|
|
|
|
mpeg->t0 = mpeg->now.secs;
|
All private data for a capture in a Wiretap module must be per-file, not
static to the module.
Add the older(?) ID tag for MPEG audio.
Just use the ID at the beginning to identify MPEG audio files; don't
check the file any further.
If the read of the magic number doesn't work, get the error, and, if
there is no error (i.e., it's a short read), just return 0 (meaning "no
error, but this isn't that type of file).
Similarly, if the magic number doesn't match, just return 0, so other
types of file are tried.
svn path=/trunk/; revision=21192
2007-03-25 22:07:40 +00:00
|
|
|
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_MINE;
|
2007-03-22 10:44:33 +00:00
|
|
|
}
|