-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
176 lines (144 loc) · 3.6 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <getopt.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "mp4tree.h"
#include "options.h"
/*
******************************************************************************
* Main functionality *
******************************************************************************
*/
int
process_file(const char * filename)
{
uint8_t * buf = NULL;
struct stat sb = {0};
int fd = -1;
ssize_t len = 0;
printf("Reading file %s\n", filename);
if (stat(filename, &sb) < 0)
{
perror("stat");
exit(EXIT_FAILURE);
}
buf = malloc(sb.st_size);
if (buf == NULL)
{
printf("Failed to allocate memory\n");
exit(EXIT_FAILURE);
}
fd = open(filename, 0);
if (fd < 0)
{
perror("open");
goto errout;
}
len = read(fd, buf, sb.st_size);
printf("Read %zd bytes \n", len);
if (len < 0)
{
perror("read");
goto errout;
}
printf("File Content:\n");
mp4tree_print(buf, len, 0);
if (buf != NULL)
free(buf);
return EXIT_SUCCESS;
errout:
if (buf != NULL)
free(buf);
return EXIT_FAILURE;
}
static int
mp4tree_parse_options(
int argc,
char ** argv)
{
int optix = 0;
int c;
static struct option options[] =
{
{"truncate", required_argument, 0, 't'},
{"filter", required_argument, 0, 'f'},
{"help", 0, 0, 'h'},
{"selftest", 0, 0, 's'},
{0, 0, 0, 0}
};
/* Set default options */
memset(&g_options, 0, sizeof(g_options));
g_options.truncate = 256;
while (1)
{
c = getopt_long(argc, argv, "t:f:i:hs",
options, &optix);
if (c == -1)
break;
switch (c)
{
case 't':
g_options.truncate = atoi(optarg);
break;
case 'f':
g_options.filter = optarg;
break;
case 's':
g_options.selftest = true;
return 0;
case 'i':
g_options.initseg = optarg;
break;
case 'h':
default:
return -1;
}
}
/* File name */
if (optind < argc)
g_options.filename = argv[optind];
else
return -1;
return 0;
}
static void
mp4tree_usage_print(const char * binary)
{
printf("Description:\n");
printf(" This program parses and prints the content of an mp4 file.\n");
printf("Usage: %s [OPTION]... [FILE]\n", binary);
printf(" Available OPTIONs:\n");
printf(" -t, --truncate=N Truncate boxes larger N bytes (default N=256)\n");
printf(" -s, --selftest Run self test\n");
printf(" -i, --initseg=<path> Also parse init segment at <path>\n");
printf("\n");
}
int
main(int argc, char **argv)
{
int status;
if (mp4tree_parse_options(argc, argv) < 0)
{
mp4tree_usage_print(argv[0]);
exit(EXIT_FAILURE);
}
if (g_options.selftest)
{
return mp4tree_selftest();
}
if (g_options.initseg)
{
status = process_file(g_options.initseg);
if (status != EXIT_SUCCESS)
{
fprintf(stderr, "Error parsing init segment %s\n", g_options.initseg);
return status;
}
}
status = process_file(g_options.filename);
return status;
}