1 /* mp4.c
2  *
3  * MP4 (ISO/IEC 14496-12) file format decoder for the Wiretap library.
4  *
5  * Wiretap Library
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  */
9 
10 #include "mp4.h"
11 
12 #include <string.h>
13 
14 #include "file_wrappers.h"
15 #include "wtap-int.h"
16 
17 static const guint8 mp4_magic[] = { 'f', 't', 'y', 'p' };
18 
19 static int mp4_file_type_subtype = -1;
20 
21 void register_mp4(void);
22 
23 wtap_open_return_val
mp4_open(wtap * wth,int * err,gchar ** err_info)24 mp4_open(wtap *wth, int *err, gchar **err_info)
25 {
26 	char magic_buf[8];
27 	int bytes_read;
28 
29 	bytes_read = file_read(magic_buf, sizeof (magic_buf), wth->fh);
30 
31 	if (bytes_read < 0) {
32 		*err = file_error(wth->fh, err_info);
33 		return WTAP_OPEN_ERROR;
34 	}
35 	if (bytes_read == 0)
36 		return WTAP_OPEN_NOT_MINE;
37 
38 	if (bytes_read == sizeof (magic_buf) &&
39 			memcmp(magic_buf + 4, mp4_magic, sizeof (mp4_magic)))
40 		return WTAP_OPEN_NOT_MINE;
41 
42 	if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
43 		return WTAP_OPEN_ERROR;
44 
45 	wth->file_type_subtype = mp4_file_type_subtype;
46 	wth->file_encap = WTAP_ENCAP_MP4;
47 	wth->file_tsprec = WTAP_TSPREC_SEC;
48 	wth->subtype_read = wtap_full_file_read;
49 	wth->subtype_seek_read = wtap_full_file_seek_read;
50 	wth->snapshot_length = 0;
51 
52 	return WTAP_OPEN_MINE;
53 }
54 
55 static const struct supported_block_type mp4_blocks_supported[] = {
56 	/*
57 	 * This is a file format that we dissect, so we provide
58 	 * only one "packet" with the file's contents, and don't
59 	 * support any options.
60 	 */
61 	{ WTAP_BLOCK_PACKET, ONE_BLOCK_SUPPORTED, NO_OPTIONS_SUPPORTED }
62 };
63 
64 static const struct file_type_subtype_info mp4_info = {
65 	"MP4 media", "mp4", "mp4", NULL,
66 	FALSE, BLOCKS_SUPPORTED(mp4_blocks_supported),
67 	NULL, NULL, NULL
68 };
69 
register_mp4(void)70 void register_mp4(void)
71 {
72 	mp4_file_type_subtype = wtap_register_file_type_subtype(&mp4_info);
73 
74 	/*
75 	 * Register name for backwards compatibility with the
76 	 * wtap_filetypes table in Lua.
77 	 */
78 	wtap_register_backwards_compatibility_lua_name("MP4",
79 	    mp4_file_type_subtype);
80 }
81 
82 /*
83  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
84  *
85  * Local variables:
86  * c-basic-offset: 8
87  * tab-width: 8
88  * indent-tabs-mode: t
89  * End:
90  *
91  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
92  * :indentSize=8:tabSize=8:noTabs=false:
93  */
94