aboutsummaryrefslogtreecommitdiffstats
path: root/wiretap/mpeg.c
diff options
context:
space:
mode:
authorGuy Harris <guy@alum.mit.edu>2014-10-06 18:00:57 -0700
committerGuy Harris <guy@alum.mit.edu>2014-10-07 01:01:59 +0000
commit670ebda4a6af0d30e033b0af48cfd15ce52c10eb (patch)
treeb092e44c944c4eb7566964da4cfb914e6002bd6d /wiretap/mpeg.c
parent6397ad43c2374ebde388041f2bd7ac925606a51e (diff)
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>
Diffstat (limited to 'wiretap/mpeg.c')
-rw-r--r--wiretap/mpeg.c83
1 files changed, 27 insertions, 56 deletions
diff --git a/wiretap/mpeg.c b/wiretap/mpeg.c
index 05ac414260..681e4baa50 100644
--- a/wiretap/mpeg.c
+++ b/wiretap/mpeg.c
@@ -70,25 +70,6 @@ mpeg_resync(FILE_T fh, int *err)
return count;
}
-static int
-mpeg_read_header(FILE_T fh, int *err, gchar **err_info, guint32 *n)
-{
- int bytes_read;
-
- errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(n, sizeof *n, fh);
- if (bytes_read != sizeof *n) {
- *err = file_error(fh, err_info);
- if (*err == 0 && bytes_read != 0)
- *err = WTAP_ERR_SHORT_READ;
- return -1;
- }
- *n = g_ntohl(*n);
- if (file_seek(fh, -(gint64)(sizeof *n), SEEK_CUR, err) == -1)
- return -1;
- return bytes_read;
-}
-
#define SCRHZ 27000000
static gboolean
@@ -97,25 +78,35 @@ mpeg_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
{
mpeg_t *mpeg = (mpeg_t *)wth->priv;
guint32 n;
- int bytes_read;
unsigned int packet_size;
nstime_t ts = mpeg->now;
- bytes_read = mpeg_read_header(fh, err, err_info, &n);
- if (bytes_read == -1)
+ /*
+ * 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)?
+ */
+ errno = WTAP_ERR_CANT_READ;
+ if (!wtap_read_bytes_or_eof(fh, &n, sizeof n, err, err_info))
return FALSE;
+ if (file_seek(fh, -(gint64)(sizeof n), SEEK_CUR, err) == -1)
+ return FALSE;
+ n = g_ntohl(n);
if (PES_VALID(n)) {
gint64 offset = file_tell(fh);
guint8 stream;
- if (file_seek(fh, 3, SEEK_CUR, err) == -1)
+ if (!file_skip(fh, 3, err))
return FALSE;
- bytes_read = file_read(&stream, sizeof stream, fh);
- if (bytes_read != sizeof stream) {
- *err = file_error(fh, err_info);
+ if (!wtap_read_bytes(fh, &stream, sizeof stream, err, err_info))
return FALSE;
- }
if (stream == 0xba) {
guint32 pack1;
@@ -123,32 +114,19 @@ mpeg_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
guint64 pack;
guint8 stuffing;
- bytes_read = file_read(&pack1, sizeof pack1, fh);
- if (bytes_read != sizeof pack1) {
- *err = file_error(fh, err_info);
- if (*err == 0 && bytes_read != 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, &pack1, sizeof pack1, err, err_info))
return FALSE;
- }
- bytes_read = file_read(&pack0, sizeof pack0, fh);
- if (bytes_read != sizeof pack0) {
- *err = file_error(fh, err_info);
- if (*err == 0 && bytes_read != 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, &pack0, sizeof pack0, err, err_info))
return FALSE;
- }
pack = (guint64)g_ntohl(pack1) << 32 | g_ntohl(pack0);
switch (pack >> 62) {
case 1:
- if (file_seek(fh, 1, SEEK_CUR, err) == -1)
+ if (!file_skip(fh, 1, err))
return FALSE;
- bytes_read = file_read(&stuffing,
- sizeof stuffing, fh);
- if (bytes_read != sizeof stuffing) {
- *err = file_error(fh, err_info);
+ if (!wtap_read_bytes(fh, &stuffing,
+ sizeof stuffing, err, err_info))
return FALSE;
- }
stuffing &= 0x07;
packet_size = 14 + stuffing;
@@ -173,13 +151,8 @@ mpeg_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
}
} else {
guint16 length;
- bytes_read = file_read(&length, sizeof length, fh);
- if (bytes_read != sizeof length) {
- *err = file_error(fh, err_info);
- if (*err == 0 && bytes_read != 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, &length, sizeof length, err, err_info))
return FALSE;
- }
length = g_ntohs(length);
packet_size = 6 + length;
}
@@ -262,16 +235,14 @@ struct _mpeg_magic {
int
mpeg_open(wtap *wth, int *err, gchar **err_info)
{
- int bytes_read;
char magic_buf[16];
struct _mpeg_magic* m;
mpeg_t *mpeg;
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(magic_buf, sizeof magic_buf, wth->fh);
- if (bytes_read != (int) sizeof magic_buf) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, magic_buf, sizeof magic_buf,
+ err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}