comparison libmpdemux/ebml.c @ 31177:9175a9a22051

cosmetics: Reformat in K&R coding style.
author diego
date Thu, 27 May 2010 16:46:47 +0000
parents 9fc9d1e788aa
children e626f90df47e
comparison
equal deleted inserted replaced
31176:04c61b9e94c8 31177:9175a9a22051
37 37
38 /* 38 /*
39 * Read: the element content data ID. 39 * Read: the element content data ID.
40 * Return: the ID. 40 * Return: the ID.
41 */ 41 */
42 uint32_t 42 uint32_t ebml_read_id(stream_t *s, int *length)
43 ebml_read_id (stream_t *s, int *length) 43 {
44 { 44 int i, len_mask = 0x80;
45 int i, len_mask = 0x80; 45 uint32_t id;
46 uint32_t id; 46
47 47 for (i = 0, id = stream_read_char(s); i < 4 && !(id & len_mask); i++)
48 for (i=0, id=stream_read_char (s); i<4 && !(id & len_mask); i++) 48 len_mask >>= 1;
49 len_mask >>= 1; 49 if (i >= 4)
50 if (i >= 4) 50 return EBML_ID_INVALID;
51 return EBML_ID_INVALID; 51 if (length)
52 if (length) 52 *length = i + 1;
53 *length = i + 1; 53 while (i--)
54 while (i--) 54 id = (id << 8) | stream_read_char(s);
55 id = (id << 8) | stream_read_char (s); 55 return id;
56 return id;
57 } 56 }
58 57
59 /* 58 /*
60 * Read a variable length unsigned int. 59 * Read a variable length unsigned int.
61 */ 60 */
62 uint64_t 61 uint64_t ebml_read_vlen_uint(uint8_t *buffer, int *length)
63 ebml_read_vlen_uint (uint8_t *buffer, int *length) 62 {
64 { 63 int i, j, num_ffs = 0, len_mask = 0x80;
65 int i, j, num_ffs = 0, len_mask = 0x80; 64 uint64_t num;
66 uint64_t num; 65
67 66 for (i = 0, num = *buffer++; i < 8 && !(num & len_mask); i++)
68 for (i=0, num=*buffer++; i<8 && !(num & len_mask); i++) 67 len_mask >>= 1;
69 len_mask >>= 1; 68 if (i >= 8)
70 if (i >= 8) 69 return EBML_UINT_INVALID;
71 return EBML_UINT_INVALID; 70 j = i + 1;
72 j = i+1; 71 if (length)
73 if (length) 72 *length = j;
74 *length = j; 73 if ((int) (num &= (len_mask - 1)) == len_mask - 1)
75 if ((int)(num &= (len_mask - 1)) == len_mask - 1)
76 num_ffs++;
77 while (i--)
78 {
79 num = (num << 8) | *buffer++;
80 if ((num & 0xFF) == 0xFF)
81 num_ffs++; 74 num_ffs++;
82 } 75 while (i--) {
83 if (j == num_ffs) 76 num = (num << 8) | *buffer++;
84 return EBML_UINT_INVALID; 77 if ((num & 0xFF) == 0xFF)
85 return num; 78 num_ffs++;
79 }
80 if (j == num_ffs)
81 return EBML_UINT_INVALID;
82 return num;
86 } 83 }
87 84
88 /* 85 /*
89 * Read a variable length signed int. 86 * Read a variable length signed int.
90 */ 87 */
91 int64_t 88 int64_t ebml_read_vlen_int(uint8_t *buffer, int *length)
92 ebml_read_vlen_int (uint8_t *buffer, int *length) 89 {
93 { 90 uint64_t unum;
94 uint64_t unum; 91 int l;
95 int l; 92
96 93 /* read as unsigned number first */
97 /* read as unsigned number first */ 94 unum = ebml_read_vlen_uint(buffer, &l);
98 unum = ebml_read_vlen_uint (buffer, &l); 95 if (unum == EBML_UINT_INVALID)
99 if (unum == EBML_UINT_INVALID) 96 return EBML_INT_INVALID;
100 return EBML_INT_INVALID; 97 if (length)
101 if (length) 98 *length = l;
102 *length = l; 99
103 100 return unum - ((1 << ((7 * l) - 1)) - 1);
104 return unum - ((1 << ((7 * l) - 1)) - 1);
105 } 101 }
106 102
107 /* 103 /*
108 * Read: element content length. 104 * Read: element content length.
109 */ 105 */
110 uint64_t 106 uint64_t ebml_read_length(stream_t *s, int *length)
111 ebml_read_length (stream_t *s, int *length) 107 {
112 { 108 int i, j, num_ffs = 0, len_mask = 0x80;
113 int i, j, num_ffs = 0, len_mask = 0x80; 109 uint64_t len;
114 uint64_t len; 110
115 111 for (i = 0, len = stream_read_char(s); i < 8 && !(len & len_mask); i++)
116 for (i=0, len=stream_read_char (s); i<8 && !(len & len_mask); i++) 112 len_mask >>= 1;
117 len_mask >>= 1; 113 if (i >= 8)
118 if (i >= 8) 114 return EBML_UINT_INVALID;
119 return EBML_UINT_INVALID; 115 j = i + 1;
120 j = i+1; 116 if (length)
121 if (length) 117 *length = j;
122 *length = j; 118 if ((int) (len &= (len_mask - 1)) == len_mask - 1)
123 if ((int)(len &= (len_mask - 1)) == len_mask - 1)
124 num_ffs++;
125 while (i--)
126 {
127 len = (len << 8) | stream_read_char (s);
128 if ((len & 0xFF) == 0xFF)
129 num_ffs++; 119 num_ffs++;
130 } 120 while (i--) {
131 if (j == num_ffs) 121 len = (len << 8) | stream_read_char(s);
132 return EBML_UINT_INVALID; 122 if ((len & 0xFF) == 0xFF)
133 return len; 123 num_ffs++;
124 }
125 if (j == num_ffs)
126 return EBML_UINT_INVALID;
127 return len;
134 } 128 }
135 129
136 /* 130 /*
137 * Read the next element as an unsigned int. 131 * Read the next element as an unsigned int.
138 */ 132 */
139 uint64_t 133 uint64_t ebml_read_uint(stream_t *s, uint64_t *length)
140 ebml_read_uint (stream_t *s, uint64_t *length) 134 {
141 { 135 uint64_t len, value = 0;
142 uint64_t len, value = 0; 136 int l;
143 int l; 137
144 138 len = ebml_read_length(s, &l);
145 len = ebml_read_length (s, &l); 139 if (len == EBML_UINT_INVALID || len < 1 || len > 8)
146 if (len == EBML_UINT_INVALID || len < 1 || len > 8) 140 return EBML_UINT_INVALID;
147 return EBML_UINT_INVALID; 141 if (length)
148 if (length) 142 *length = len + l;
149 *length = len + l; 143
150 144 while (len--)
151 while (len--) 145 value = (value << 8) | stream_read_char(s);
152 value = (value << 8) | stream_read_char (s); 146
153 147 return value;
154 return value;
155 } 148 }
156 149
157 /* 150 /*
158 * Read the next element as a signed int. 151 * Read the next element as a signed int.
159 */ 152 */
160 int64_t 153 int64_t ebml_read_int(stream_t *s, uint64_t *length)
161 ebml_read_int (stream_t *s, uint64_t *length) 154 {
162 { 155 int64_t value = 0;
163 int64_t value = 0; 156 uint64_t len;
164 uint64_t len; 157 int l;
165 int l; 158
166 159 len = ebml_read_length(s, &l);
167 len = ebml_read_length (s, &l); 160 if (len == EBML_UINT_INVALID || len < 1 || len > 8)
168 if (len == EBML_UINT_INVALID || len < 1 || len > 8) 161 return EBML_INT_INVALID;
169 return EBML_INT_INVALID; 162 if (length)
170 if (length) 163 *length = len + l;
171 *length = len + l; 164
172 165 len--;
173 len--; 166 l = stream_read_char(s);
174 l = stream_read_char (s); 167 if (l & 0x80)
175 if (l & 0x80) 168 value = -1;
176 value = -1; 169 value = (value << 8) | l;
177 value = (value << 8) | l; 170 while (len--)
178 while (len--) 171 value = (value << 8) | stream_read_char(s);
179 value = (value << 8) | stream_read_char (s); 172
180 173 return value;
181 return value;
182 } 174 }
183 175
184 /* 176 /*
185 * Read the next element as a float. 177 * Read the next element as a float.
186 */ 178 */
187 long double 179 long double ebml_read_float(stream_t *s, uint64_t *length)
188 ebml_read_float (stream_t *s, uint64_t *length) 180 {
189 { 181 long double value;
190 long double value; 182 uint64_t len;
191 uint64_t len; 183 int l;
192 int l; 184
193 185 len = ebml_read_length(s, &l);
194 len = ebml_read_length (s, &l); 186 switch (len) {
195 switch (len)
196 {
197 case 4: 187 case 4:
198 value = av_int2flt(stream_read_dword(s)); 188 value = av_int2flt(stream_read_dword(s));
199 break; 189 break;
200 190
201 case 8: 191 case 8:
202 value = av_int2dbl(stream_read_qword(s)); 192 value = av_int2dbl(stream_read_qword(s));
203 break; 193 break;
204 194
205 default: 195 default:
206 return EBML_FLOAT_INVALID; 196 return EBML_FLOAT_INVALID;
207 } 197 }
208 198
209 if (length) 199 if (length)
210 *length = len + l; 200 *length = len + l;
211 201
212 return value; 202 return value;
213 } 203 }
214 204
215 /* 205 /*
216 * Read the next element as an ASCII string. 206 * Read the next element as an ASCII string.
217 */ 207 */
218 char * 208 char *ebml_read_ascii(stream_t *s, uint64_t *length)
219 ebml_read_ascii (stream_t *s, uint64_t *length) 209 {
220 { 210 uint64_t len;
221 uint64_t len; 211 char *str;
222 char *str; 212 int l;
223 int l; 213
224 214 len = ebml_read_length(s, &l);
225 len = ebml_read_length (s, &l); 215 if (len == EBML_UINT_INVALID)
226 if (len == EBML_UINT_INVALID) 216 return NULL;
227 return NULL; 217 if (len > SIZE_MAX - 1)
228 if (len > SIZE_MAX - 1) 218 return NULL;
229 return NULL; 219 if (length)
230 if (length) 220 *length = len + l;
231 *length = len + l; 221
232 222 str = malloc(len + 1);
233 str = malloc (len + 1); 223 if (stream_read(s, str, len) != (int) len) {
234 if (stream_read(s, str, len) != (int) len) 224 free(str);
235 { 225 return NULL;
236 free (str); 226 }
237 return NULL; 227 str[len] = '\0';
238 } 228
239 str[len] = '\0'; 229 return str;
240
241 return str;
242 } 230 }
243 231
244 /* 232 /*
245 * Read the next element as a UTF-8 string. 233 * Read the next element as a UTF-8 string.
246 */ 234 */
247 char * 235 char *ebml_read_utf8(stream_t *s, uint64_t *length)
248 ebml_read_utf8 (stream_t *s, uint64_t *length) 236 {
249 { 237 return ebml_read_ascii(s, length);
250 return ebml_read_ascii (s, length);
251 } 238 }
252 239
253 /* 240 /*
254 * Skip the next element. 241 * Skip the next element.
255 */ 242 */
256 int 243 int ebml_read_skip(stream_t *s, uint64_t *length)
257 ebml_read_skip (stream_t *s, uint64_t *length) 244 {
258 { 245 uint64_t len;
259 uint64_t len; 246 int l;
260 int l; 247
261 248 len = ebml_read_length(s, &l);
262 len = ebml_read_length (s, &l); 249 if (len == EBML_UINT_INVALID)
263 if (len == EBML_UINT_INVALID) 250 return 1;
264 return 1; 251 if (length)
265 if (length) 252 *length = len + l;
266 *length = len + l; 253
267 254 stream_skip(s, len);
268 stream_skip(s, len); 255
269 256 return 0;
270 return 0;
271 } 257 }
272 258
273 /* 259 /*
274 * Read the next element, but only the header. The contents 260 * Read the next element, but only the header. The contents
275 * are supposed to be sub-elements which can be read separately. 261 * are supposed to be sub-elements which can be read separately.
276 */ 262 */
277 uint32_t 263 uint32_t ebml_read_master(stream_t *s, uint64_t *length)
278 ebml_read_master (stream_t *s, uint64_t *length) 264 {
279 { 265 uint64_t len;
280 uint64_t len; 266 uint32_t id;
281 uint32_t id; 267
282 268 id = ebml_read_id(s, NULL);
283 id = ebml_read_id (s, NULL); 269 if (id == EBML_ID_INVALID)
284 if (id == EBML_ID_INVALID) 270 return id;
271
272 len = ebml_read_length(s, NULL);
273 if (len == EBML_UINT_INVALID)
274 return EBML_ID_INVALID;
275 if (length)
276 *length = len;
277
285 return id; 278 return id;
286
287 len = ebml_read_length (s, NULL);
288 if (len == EBML_UINT_INVALID)
289 return EBML_ID_INVALID;
290 if (length)
291 *length = len;
292
293 return id;
294 } 279 }
295 280
296 281
297 /* 282 /*
298 * Read an EBML header. 283 * Read an EBML header.
299 */ 284 */
300 char * 285 char *ebml_read_header(stream_t *s, int *version)
301 ebml_read_header (stream_t *s, int *version) 286 {
302 { 287 uint64_t length, l, num;
303 uint64_t length, l, num; 288 uint32_t id;
304 uint32_t id; 289 char *str = NULL;
305 char *str = NULL; 290
306 291 if (ebml_read_master(s, &length) != EBML_ID_HEADER)
307 if (ebml_read_master (s, &length) != EBML_ID_HEADER) 292 return 0;
308 return 0; 293
309 294 if (version)
310 if (version) 295 *version = 1;
311 *version = 1; 296
312 297 while (length > 0) {
313 while (length > 0) 298 id = ebml_read_id(s, NULL);
314 { 299 if (id == EBML_ID_INVALID)
315 id = ebml_read_id (s, NULL); 300 return NULL;
316 if (id == EBML_ID_INVALID) 301 length -= 2;
317 return NULL; 302
318 length -= 2; 303 switch (id) {
319 304 /* is our read version uptodate? */
320 switch (id)
321 {
322 /* is our read version uptodate? */
323 case EBML_ID_EBMLREADVERSION: 305 case EBML_ID_EBMLREADVERSION:
324 num = ebml_read_uint (s, &l); 306 num = ebml_read_uint(s, &l);
325 if (num != EBML_VERSION) 307 if (num != EBML_VERSION)
326 return NULL; 308 return NULL;
327 break; 309 break;
328 310
329 /* we only handle 8 byte lengths at max */ 311 /* we only handle 8 byte lengths at max */
330 case EBML_ID_EBMLMAXSIZELENGTH: 312 case EBML_ID_EBMLMAXSIZELENGTH:
331 num = ebml_read_uint (s, &l); 313 num = ebml_read_uint(s, &l);
332 if (num != sizeof (uint64_t)) 314 if (num != sizeof(uint64_t))
333 return NULL; 315 return NULL;
334 break; 316 break;
335 317
336 /* we handle 4 byte IDs at max */ 318 /* we handle 4 byte IDs at max */
337 case EBML_ID_EBMLMAXIDLENGTH: 319 case EBML_ID_EBMLMAXIDLENGTH:
338 num = ebml_read_uint (s, &l); 320 num = ebml_read_uint(s, &l);
339 if (num != sizeof (uint32_t)) 321 if (num != sizeof(uint32_t))
340 return NULL; 322 return NULL;
341 break; 323 break;
342 324
343 case EBML_ID_DOCTYPE: 325 case EBML_ID_DOCTYPE:
344 str = ebml_read_ascii (s, &l); 326 str = ebml_read_ascii(s, &l);
345 if (str == NULL) 327 if (str == NULL)
346 return NULL; 328 return NULL;
347 break; 329 break;
348 330
349 case EBML_ID_DOCTYPEREADVERSION: 331 case EBML_ID_DOCTYPEREADVERSION:
350 num = ebml_read_uint (s, &l); 332 num = ebml_read_uint(s, &l);
351 if (num == EBML_UINT_INVALID) 333 if (num == EBML_UINT_INVALID)
352 return NULL; 334 return NULL;
353 if (version) 335 if (version)
354 *version = num; 336 *version = num;
355 break; 337 break;
356 338
357 /* we ignore these two, they don't tell us anything we care about */ 339 /* we ignore these two, they don't tell us anything we care about */
358 case EBML_ID_VOID: 340 case EBML_ID_VOID:
359 case EBML_ID_EBMLVERSION: 341 case EBML_ID_EBMLVERSION:
360 case EBML_ID_DOCTYPEVERSION: 342 case EBML_ID_DOCTYPEVERSION:
361 default: 343 default:
362 if (ebml_read_skip (s, &l)) 344 if (ebml_read_skip(s, &l))
363 return NULL; 345 return NULL;
364 break; 346 break;
365 } 347 }
366 length -= l; 348 length -= l;
367 } 349 }
368 350
369 return str; 351 return str;
370 } 352 }