Debian Codegraph @

src/examples/convtab_dump.c

raw
1 /*
2 * This file is part of libbdplus
3 * Copyright (C) 2013 VideoLAN
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see
17 * <http://www.gnu.org/licenses/>.
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <inttypes.h>
23 #include <string.h>
24
25 // endian safe fetch
26 #define FETCH4(X) (uint32_t)(((X)[0]<<24)|(X)[1]<<16|(X)[2]<<8|(X)[3])
27 #define FETCHU2(X) (uint32_t)((uint16_t)(((X)[0]<<8)|(X)[1]))
28
29 // raw table
30 #define MAX_TAB_SIZE 64*1024*1024
31 uint8_t tab[MAX_TAB_SIZE];
32 uint32_t index[0xffff];
33
34 static size_t _read_tab(const char *file)
35 {
36 FILE *fp = fopen(file, "rb");
37 size_t len;
38
39 if (!fp) {
40 perror("fopen(): ");
41 return 0;
42 }
43
44 memset(tab, 0, MAX_TAB_SIZE);
45
46 len = fread(tab, 1, MAX_TAB_SIZE, fp);
47 if (len < 1) {
48 perror("fread(): ");
49 }
50
51 fclose(fp);
52
53 printf("Read %zd bytes from %s\n", len, file);
54
55 return len;
56 }
57
58 int main(int argc, char **argv)
59 {
60 uint32_t numTables, table;
61 uint32_t ptr = 0;
62 uint32_t offset = 0;
63 int errors = 0;
64
65 if (argc < 2) {
66 fprintf(stderr, "%s /path/to/conv_tab.bin\n", argv[0]);
67 exit(1);
68 }
69
70 if (_read_tab(argv[1]) < 1) {
71 fprintf(stderr, "Error reading %s\n", argv[1]);
72 exit(1);
73 }
74
75 numTables = FETCHU2(&tab[ptr]);
76 ptr += 2;
77
78 printf("%d tables:\n", numTables);
79
80
81 for (table = 0; table < numTables; table++) {
82 uint32_t tableID, numSegments, segment;
83
84 tableID = FETCH4(&tab[ptr]);
85 ptr += 4;
86 numSegments = FETCHU2(&tab[ptr]);
87 ptr += 2;
88
89 printf("Table %d: %05d.m2ts (%d segments)\n", table, tableID, numSegments);
90
91 for (segment = 0; segment < numSegments; segment++) {
92 uint32_t numEntries, entry;
93 uint64_t prev_off1 = 0;
94
95 offset = FETCH4(&tab[ptr + (segment * 4) ]);
96
97 numEntries = FETCH4(&tab[offset]);
98 offset += 4;
99
100 printf(" Segment %d: %d entries\n", segment, numEntries);
101
102 // read index table
103 for (entry = 0; entry < numEntries; entry++) {
104 index[entry] = FETCH4(&tab[offset]);
105 offset += 4;
106 }
107
108 // read data
109 for (entry = 0; entry < numEntries; entry++) {
110
111 uint8_t flags = tab[ offset ];
112 offset += 1;
113
114 uint32_t tmp = FETCH4(&tab[offset]); // only fetch 3bytes, 24 bits.
115 offset += 3;
116 tmp &= 0xFFFFFF00;
117
118 uint32_t patch0_address_adjust = (tmp & 0xFFF00000) >> 20;
119 uint32_t patch1_address_adjust = (tmp & 0x000FFF00) >> 8;
120
121 uint32_t patch0_buffer_offset = tab[offset++];
122 uint32_t patch1_buffer_offset = tab[offset++];
123 uint8_t patch0[5], patch1[5];
124
125 memcpy(patch0, &tab[ offset ], sizeof(patch0));
126 offset += 5;
127 memcpy(patch1, &tab[ offset ], sizeof(patch1));
128 offset += 5;
129
130 uint64_t off0 = (( (uint64_t)index[entry] +
131 (uint64_t)patch0_address_adjust) *
132 (uint64_t)0xC0 +
133 (uint64_t)patch0_buffer_offset);
134
135 uint64_t off1 = (( (uint64_t)index[entry] +
136 (uint64_t)patch0_address_adjust +
137 (uint64_t)patch1_address_adjust) *
138 (uint64_t)0xC0 +
139 (uint64_t)patch1_buffer_offset);
140
141 printf(" Entry %d: flags 0x%x (%d)\n", entry, flags, flags>>6);
142 printf(" %08X %08X: %02X %02X %02X %02X %02X\n",
143 (uint32_t)(off0 >> 32), (uint32_t)(off0 & 0xffffffff),
144 patch0[0], patch0[1], patch0[2], patch0[3], patch0[4]);
145 printf(" %08X %08X: %02X %02X %02X %02X %02X\n",
146 (uint32_t)(off1 >> 32), (uint32_t)(off1 & 0xffffffff),
147 patch1[0], patch1[1], patch1[2], patch1[3], patch1[4]);
148
149 if ((flags >> 6) == 3) {
150 printf(" *** invalid flags ***\n");
151 errors++;
152 }
153 if ((flags >> 6) == 1) {
154 if (patch0_buffer_offset < 8 || patch1_buffer_offset < 8) {
155 printf(" *** Invalid in-packet offset (inside ts header) ***\n");
156 errors++;
157 }
158 if (patch0_buffer_offset > 187 || patch1_buffer_offset > 187) {
159 printf(" *** Invalid in-packet offset (overwrite) ***\n");
160 errors++;
161 }
162 if (off0 >= off1) {
163 printf(" *** invalid offset (off0 >= off1) ***\n");
164 errors++;
165 }
166 if (off0 < prev_off1) {
167 printf(" *** invalid offset sequence (not monotonic) ***\n");
168 errors++;
169 }
170 prev_off1 = off1;
171 }
172
173 } // for entry
174
175 } // for segment
176
177 ptr = offset;
178
179 } // for table
180
181 return errors;
182 }