No seek-read routines use the length argument, so eliminate it from
[metze/wireshark/wip.git] / wiretap / mp2t.c
1 /* mp2t.c
2  *
3  * ISO/IEC 13818-1 MPEG2-TS file format decoder for the Wiretap library.
4  * Written by Weston Schmidt <weston_schmidt@alumni.purdue.edu>
5  * Copyright 2012 Weston Schmidt
6  *
7  * $Id$
8  *
9  * Wiretap Library
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include "config.h"
26
27 #ifdef HAVE_SYS_TYPES_H
28 #include <sys/types.h>
29 #endif
30
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34
35 #include "mp2t.h"
36
37 #include "wtap-int.h"
38 #include "buffer.h"
39 #include "file_wrappers.h"
40 #include <errno.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <time.h>
44
45 #define MP2T_SYNC_BYTE      0x47
46 #define MP2T_SIZE           188
47 #define MP2T_QAM256_BITRATE 38810700    /* bits per second */
48 #define MP2T_QAM64_BITRATE  26970350    /* bits per second */
49
50 /* we try to detect trailing data up to 40 bytes after each packet */
51 #define TRAILER_LEN_MAX 40
52
53 /* number of consecutive packets we must read to decide that a file
54    is actually an mpeg2 ts */
55 #define SYNC_STEPS   10
56
57
58 typedef struct {
59     int start_offset;
60     /* length of trailing data (e.g. FEC) that's appended after each packet */
61     guint8  trailer_len;
62 } mp2t_filetype_t;
63
64 static gboolean
65 mp2t_read_packet(mp2t_filetype_t *mp2t, FILE_T fh, gint64 offset,
66                  struct wtap_pkthdr *phdr, Buffer *buf, int *err,
67                  gchar **err_info)
68 {
69     guint64 tmp;
70     int bytes_read;
71
72     buffer_assure_space(buf, MP2T_SIZE);
73     errno = WTAP_ERR_CANT_READ;
74     bytes_read = file_read(buffer_start_ptr(buf), MP2T_SIZE, fh);
75     if (MP2T_SIZE != bytes_read) {
76         *err = file_error(fh, err_info);
77         /* bytes_read==0 is end of file, not a short read */
78         if (bytes_read>0 && *err == 0) {
79             *err = WTAP_ERR_SHORT_READ;
80         }
81         return FALSE;
82     }
83
84     /* XXX - relative, not absolute, time stamps */
85     phdr->presence_flags = WTAP_HAS_TS;
86
87     /*
88      * Every packet in an MPEG2-TS stream is has a fixed size of
89      * MP2T_SIZE plus the number of trailer bytes.
90      *
91      * The bitrate is constant, so the time offset, from the beginning
92      * of the stream, of a given packet is the packet offset, in bits,
93      * divided by the bitrate.
94      *
95      * It would be really cool to be able to configure the bitrate...
96      */
97     tmp = ((guint64)(offset - mp2t->start_offset) * 8); /* offset, in bits */
98     phdr->ts.secs = (time_t)(tmp / MP2T_QAM256_BITRATE);
99     phdr->ts.nsecs = (int)((tmp % MP2T_QAM256_BITRATE) * 1000000000 / MP2T_QAM256_BITRATE);
100
101     phdr->caplen = MP2T_SIZE;
102     phdr->len = MP2T_SIZE;
103
104     return TRUE;
105 }
106
107 static gboolean
108 mp2t_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
109 {
110     mp2t_filetype_t *mp2t;
111
112     mp2t = (mp2t_filetype_t*) wth->priv;
113
114     *data_offset = file_tell(wth->fh);
115
116     if (!mp2t_read_packet(mp2t, wth->fh, *data_offset, &wth->phdr,
117                           wth->frame_buffer, err, err_info)) {
118         return FALSE;
119     }
120
121     /* if there's a trailer, skip it and go to the start of the next packet */
122     if (mp2t->trailer_len!=0) {
123         if (-1 == file_seek(wth->fh, mp2t->trailer_len, SEEK_CUR, err)) {
124             return FALSE;
125         }
126     }
127
128     return TRUE;
129 }
130
131 static gboolean
132 mp2t_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
133         Buffer *buf, int *err, gchar **err_info)
134 {
135     mp2t_filetype_t *mp2t;
136
137     if (-1 == file_seek(wth->random_fh, seek_off, SEEK_SET, err)) {
138         return FALSE;
139     }
140
141     mp2t = (mp2t_filetype_t*) wth->priv;
142
143     if (!mp2t_read_packet(mp2t, wth->random_fh, seek_off, phdr, buf,
144                           err, err_info)) {
145         if (*err == 0)
146             *err = WTAP_ERR_SHORT_READ;
147         return FALSE;
148     }
149     return TRUE;
150 }
151
152 int
153 mp2t_open(wtap *wth, int *err, gchar **err_info)
154 {
155     int bytes_read;
156     guint8 buffer[MP2T_SIZE+TRAILER_LEN_MAX];
157     guint8 trailer_len = 0;
158     guint sync_steps = 0;
159     int i;
160     int first;
161     mp2t_filetype_t *mp2t;
162
163
164     errno = WTAP_ERR_CANT_READ;
165     bytes_read = file_read(buffer, MP2T_SIZE, wth->fh);
166
167     if (MP2T_SIZE != bytes_read) {
168         *err = file_error(wth->fh, err_info);
169         if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
170             return -1;
171         return 0;
172     }
173
174     first = -1;
175     for (i = 0; i < MP2T_SIZE; i++) {
176         if (MP2T_SYNC_BYTE == buffer[i]) {
177             first = i;
178             break;
179         }
180     }
181     if (-1 == first) {
182         return 0; /* wrong file type - not an mpeg2 ts file */
183     }
184
185     if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
186         return -1;
187     }
188     /* read some packets and make sure they all start with a sync byte */
189     do {
190        bytes_read = file_read(buffer, MP2T_SIZE+trailer_len, wth->fh);
191        if (bytes_read < 0) {
192           *err = file_error(wth->fh, err_info);
193           return -1;  /* read error */
194        }
195        if (bytes_read < MP2T_SIZE+trailer_len) {
196           if(sync_steps<2) return 0; /* wrong file type - not an mpeg2 ts file */
197           break;  /* end of file, that's ok if we're still in sync */
198        }
199        if (buffer[0] == MP2T_SYNC_BYTE) {
200                sync_steps++;
201        }
202        else {
203            /* no sync byte found, check if trailing data is appended
204               and we have to increase the packet size */
205
206            /* if we've already detected a trailer field, we must remain in sync
207               another mismatch means we have no mpeg2 ts file */
208            if (trailer_len>0)
209                return 0;
210
211            /* check if a trailer is appended to the packet */
212            for (i=0; i<TRAILER_LEN_MAX; i++) {
213                if (buffer[i] == MP2T_SYNC_BYTE) {
214                    trailer_len = i;
215                    if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
216                        return -1;
217                    }
218                    sync_steps = 0;
219                    break;
220                }
221            }
222            /* no sync byte found in the vicinity, this is no mpeg2 ts file */
223            if (i==TRAILER_LEN_MAX)
224                return 0;
225        }
226     } while (sync_steps < SYNC_STEPS);
227
228     if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
229         return -1;
230     }
231
232     wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_MPEG_2_TS;
233     wth->file_encap = WTAP_ENCAP_MPEG_2_TS;
234     wth->tsprecision = WTAP_FILE_TSPREC_NSEC;
235     wth->subtype_read = mp2t_read;
236     wth->subtype_seek_read = mp2t_seek_read;
237     wth->snapshot_length = 0;
238
239     mp2t = (mp2t_filetype_t*) g_malloc(sizeof(mp2t_filetype_t));
240     if (NULL == mp2t) {
241         return -1;
242     }
243
244     wth->priv = mp2t;
245     mp2t->start_offset = first;
246     mp2t->trailer_len = trailer_len;
247
248     return 1;
249 }