MSVC: add missing source files.
[L-SMASH.git] / common / bytes.c
blob95b9b35a7ef112256efec8ac22b716877d700f5e
1 /*****************************************************************************
2 * bytes.c
3 *****************************************************************************
4 * Copyright (C) 2010-2015 L-SMASH project
6 * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *****************************************************************************/
21 /* This file is available under an ISC license. */
23 #include "internal.h" /* must be placed first */
25 #include <string.h>
26 #include <limits.h>
28 lsmash_bs_t *lsmash_bs_create( void )
30 lsmash_bs_t *bs = lsmash_malloc_zero( sizeof(lsmash_bs_t) );
31 if( !bs )
32 return NULL;
33 bs->unseekable = 1;
34 bs->buffer.internal = 1;
35 bs->buffer.max_size = BS_MAX_DEFAULT_READ_SIZE;
36 return bs;
39 static void bs_buffer_free( lsmash_bs_t *bs )
41 if( bs->buffer.internal )
42 lsmash_free( bs->buffer.data );
43 bs->buffer.data = NULL;
44 bs->buffer.alloc = 0;
45 bs->buffer.store = 0;
46 bs->buffer.pos = 0;
49 void lsmash_bs_cleanup( lsmash_bs_t *bs )
51 if( !bs )
52 return;
53 bs_buffer_free( bs );
54 lsmash_free( bs );
57 int lsmash_bs_set_empty_stream( lsmash_bs_t *bs, uint8_t *data, size_t size )
59 if( !bs )
60 return LSMASH_ERR_FUNCTION_PARAM;
61 bs->stream = NULL; /* empty stream */
62 bs->eof = 1; /* unreadable because of empty stream */
63 bs->eob = 0; /* readable on the buffer */
64 bs->error = 0;
65 bs->unseekable = 1; /* only seek on the buffer */
66 bs->written = size; /* behave as if the size of the empty stream is 'size' */
67 bs->offset = size; /* behave as if the poiter of the stream is at the end */
68 bs->buffer.unseekable = 0; /* only seek on the buffer */
69 bs->buffer.internal = 0; /* must not be allocated internally */
70 bs->buffer.data = data;
71 bs->buffer.store = size;
72 bs->buffer.alloc = size;
73 bs->buffer.pos = 0;
74 bs->buffer.max_size = 0; /* make no sense */
75 bs->buffer.count = 0;
76 bs->read = NULL;
77 bs->write = NULL;
78 bs->seek = NULL;
79 return 0;
82 void lsmash_bs_empty( lsmash_bs_t *bs )
84 if( !bs )
85 return;
86 if( bs->buffer.data )
87 memset( bs->buffer.data, 0, bs->buffer.alloc );
88 bs->buffer.store = 0;
89 bs->buffer.pos = 0;
92 static void bs_alloc( lsmash_bs_t *bs, size_t alloc )
94 if( (bs->buffer.alloc >= alloc) || bs->error )
95 return;
96 if( !bs->buffer.internal )
98 /* We cannot re-allocate the memory block. */
99 bs->error = 1;
100 return;
102 alloc = LSMASH_MAX( alloc, bs->buffer.max_size );
103 uint8_t *data;
104 if( !bs->buffer.data )
105 data = lsmash_malloc( alloc );
106 else
107 data = lsmash_realloc( bs->buffer.data, alloc );
108 if( !data )
110 bs_buffer_free( bs );
111 bs->error = 1;
112 return;
114 bs->buffer.internal = 1;
115 bs->buffer.data = data;
116 bs->buffer.alloc = alloc;
119 static uint64_t bs_estimate_seek_offset( lsmash_bs_t *bs, int64_t offset, int whence )
121 /* Calculate the offset after the seek. */
122 uint64_t dst_offset;
123 if( whence == SEEK_SET )
125 assert( offset >= 0 );
126 if( bs->written < offset )
127 dst_offset = bs->written;
128 else
129 dst_offset = offset;
131 else if( whence == SEEK_CUR )
133 if( offset < 0 && bs->offset < -offset )
134 dst_offset = 0;
135 else if( offset > 0 && bs->written < bs->offset + offset )
136 dst_offset = bs->written;
137 else
138 dst_offset = bs->offset + offset;
140 else /* if( whence == SEEK_END ) */
142 assert( offset <= 0 );
143 if( bs->written < -offset )
144 dst_offset = 0;
145 else
146 dst_offset = bs->written + offset;
148 return dst_offset;
151 /* TODO: Support offset > INT64_MAX */
152 int64_t lsmash_bs_write_seek( lsmash_bs_t *bs, int64_t offset, int whence )
154 if( bs->unseekable )
155 return LSMASH_ERR_NAMELESS;
156 if( whence != SEEK_SET && whence != SEEK_CUR && whence != SEEK_END )
157 return LSMASH_ERR_FUNCTION_PARAM;
158 /* Try to seek the stream. */
159 int64_t ret = bs->seek( bs->stream, offset, whence );
160 if( ret < 0 )
161 return ret;
162 bs->offset = bs_estimate_seek_offset( bs, offset, whence );
163 bs->eof = 0;
164 bs->eob = 0;
165 return ret;
168 /* TODO: Support offset > INT64_MAX */
169 int64_t lsmash_bs_read_seek( lsmash_bs_t *bs, int64_t offset, int whence )
171 if( whence != SEEK_SET && whence != SEEK_CUR && whence != SEEK_END )
172 return LSMASH_ERR_FUNCTION_PARAM;
173 if( whence == SEEK_CUR )
174 offset -= lsmash_bs_get_remaining_buffer_size( bs );
175 /* Check whether we can seek on the buffer. */
176 if( !bs->buffer.unseekable )
178 assert( bs->offset >= bs->buffer.store );
179 uint64_t dst_offset = bs_estimate_seek_offset( bs, offset, whence );
180 uint64_t offset_s = bs->offset - bs->buffer.store;
181 uint64_t offset_e = bs->offset;
182 if( bs->unseekable || (dst_offset >= offset_s && dst_offset < offset_e) )
184 /* OK, we can. So, seek on the buffer. */
185 bs->buffer.pos = dst_offset - offset_s;
186 bs->eob = 0;
187 return lsmash_bs_get_stream_pos( bs );
190 if( bs->unseekable )
191 return LSMASH_ERR_NAMELESS;
192 /* Try to seek the stream. */
193 int64_t ret = bs->seek( bs->stream, offset, whence );
194 if( ret < 0 )
195 return ret;
196 bs->offset = ret;
197 bs->written = LSMASH_MAX( bs->written, bs->offset );
198 bs->eof = 0;
199 bs->eob = 0;
200 /* The data on the buffer is invalid. */
201 lsmash_bs_empty( bs );
202 return ret;
205 static void bs_dispose_past_data( lsmash_bs_t *bs )
207 /* Move remainder bytes. */
208 assert( bs->buffer.store >= bs->buffer.pos );
209 size_t remainder = lsmash_bs_get_remaining_buffer_size( bs );
210 if( bs->buffer.pos && remainder )
211 memmove( lsmash_bs_get_buffer_data_start( bs ), lsmash_bs_get_buffer_data( bs ), remainder );
212 bs->buffer.store = remainder;
213 bs->buffer.pos = 0;
216 /*---- bitstream writer ----*/
217 void lsmash_bs_put_byte( lsmash_bs_t *bs, uint8_t value )
219 if( bs->buffer.internal
220 || bs->buffer.data )
222 bs_alloc( bs, bs->buffer.store + 1 );
223 if( bs->error )
224 return;
225 bs->buffer.data[ bs->buffer.store ] = value;
227 ++ bs->buffer.store;
230 void lsmash_bs_put_bytes( lsmash_bs_t *bs, uint32_t size, void *value )
232 if( size == 0 || !value )
233 return;
234 if( bs->buffer.internal
235 || bs->buffer.data )
237 bs_alloc( bs, bs->buffer.store + size );
238 if( bs->error )
239 return;
240 memcpy( lsmash_bs_get_buffer_data_end( bs ), value, size );
242 bs->buffer.store += size;
245 void lsmash_bs_put_be16( lsmash_bs_t *bs, uint16_t value )
247 lsmash_bs_put_byte( bs, value >> 8 );
248 lsmash_bs_put_byte( bs, value );
251 void lsmash_bs_put_be24( lsmash_bs_t *bs, uint32_t value )
253 lsmash_bs_put_byte( bs, value >> 16 );
254 lsmash_bs_put_be16( bs, value );
257 void lsmash_bs_put_be32( lsmash_bs_t *bs, uint32_t value )
259 lsmash_bs_put_be16( bs, value >> 16 );
260 lsmash_bs_put_be16( bs, value );
263 void lsmash_bs_put_be64( lsmash_bs_t *bs, uint64_t value )
265 lsmash_bs_put_be32( bs, value >> 32 );
266 lsmash_bs_put_be32( bs, value );
269 void lsmash_bs_put_byte_from_64( lsmash_bs_t *bs, uint64_t value )
271 lsmash_bs_put_byte( bs, value );
274 void lsmash_bs_put_be16_from_64( lsmash_bs_t *bs, uint64_t value )
276 lsmash_bs_put_be16( bs, value );
279 void lsmash_bs_put_be24_from_64( lsmash_bs_t *bs, uint64_t value )
281 lsmash_bs_put_be24( bs, value );
284 void lsmash_bs_put_be32_from_64( lsmash_bs_t *bs, uint64_t value )
286 lsmash_bs_put_be32( bs, value );
289 void lsmash_bs_put_le16( lsmash_bs_t *bs, uint16_t value )
291 lsmash_bs_put_byte( bs, value );
292 lsmash_bs_put_byte( bs, value >> 8 );
295 void lsmash_bs_put_le32( lsmash_bs_t *bs, uint32_t value )
297 lsmash_bs_put_le16( bs, value );
298 lsmash_bs_put_le16( bs, value >> 16 );
301 int lsmash_bs_flush_buffer( lsmash_bs_t *bs )
303 if( !bs )
304 return LSMASH_ERR_FUNCTION_PARAM;
305 if( bs->buffer.store == 0
306 || (bs->stream && bs->write && !bs->buffer.data) )
307 return 0;
308 if( bs->error
309 || (bs->stream && bs->write && bs->write( bs->stream, lsmash_bs_get_buffer_data_start( bs ), bs->buffer.store ) != bs->buffer.store) )
311 bs_buffer_free( bs );
312 bs->error = 1;
313 return LSMASH_ERR_NAMELESS;
315 if( bs->write )
317 bs->written += bs->buffer.store;
318 bs->offset += bs->buffer.store;
320 bs->buffer.store = 0;
321 return 0;
324 int lsmash_bs_write_data( lsmash_bs_t *bs, uint8_t *buf, size_t size )
326 if( !bs || size > INT_MAX )
327 return LSMASH_ERR_FUNCTION_PARAM;
328 if( !buf || size == 0 )
329 return 0;
330 if( bs->error || !bs->stream )
332 bs_buffer_free( bs );
333 bs->error = 1;
334 return LSMASH_ERR_NAMELESS;
336 int write_size = bs->write( bs->stream, buf, size );
337 bs->written += write_size;
338 bs->offset += write_size;
339 return write_size != size ? LSMASH_ERR_NAMELESS : 0;
342 void *lsmash_bs_export_data( lsmash_bs_t *bs, uint32_t *length )
344 if( !bs || !bs->buffer.data || bs->buffer.store == 0 || bs->error )
345 return NULL;
346 void *buf = lsmash_memdup( lsmash_bs_get_buffer_data_start( bs ), bs->buffer.store );
347 if( !buf )
348 return NULL;
349 if( length )
350 *length = bs->buffer.store;
351 return buf;
353 /*---- ----*/
355 /*---- bitstream reader ----*/
356 static void bs_fill_buffer( lsmash_bs_t *bs )
358 if( bs->eof || bs->error )
359 return;
360 if( !bs->read || !bs->stream || bs->buffer.max_size == 0 )
362 bs->eof = 1;
363 return;
365 if( !bs->buffer.data )
367 bs_alloc( bs, bs->buffer.max_size );
368 if( bs->error )
369 return;
371 /* Read bytes from the stream to fill the buffer. */
372 bs_dispose_past_data( bs );
373 while( bs->buffer.alloc > bs->buffer.store )
375 uint64_t invalid_buffer_size = bs->buffer.alloc - bs->buffer.store;
376 int max_read_size = LSMASH_MIN( invalid_buffer_size, bs->buffer.max_size );
377 int read_size = bs->read( bs->stream, lsmash_bs_get_buffer_data_end( bs ), max_read_size );
378 if( read_size == 0 )
380 bs->eof = 1;
381 return;
383 else if( read_size < 0 )
385 bs->error = 1;
386 return;
388 bs->buffer.unseekable = 0;
389 bs->buffer.store += read_size;
390 bs->offset += read_size;
391 bs->written = LSMASH_MAX( bs->written, bs->offset );
395 uint8_t lsmash_bs_show_byte( lsmash_bs_t *bs, uint32_t offset )
397 if( bs->error )
398 return 0;
399 if( offset >= lsmash_bs_get_remaining_buffer_size( bs ) )
401 bs_fill_buffer( bs );
402 if( bs->error )
403 return 0;
404 if( offset >= lsmash_bs_get_remaining_buffer_size( bs ) )
406 if( bs->eof )
407 /* No more read from both the stream and the buffer. */
408 return 0;
409 /* We need increase the buffer size. */
410 bs_alloc( bs, bs->buffer.pos + offset + 1 );
411 bs_fill_buffer( bs );
412 if( bs->error )
413 return 0;
416 return bs->buffer.data[ bs->buffer.pos + offset ];
419 uint16_t lsmash_bs_show_be16( lsmash_bs_t *bs, uint32_t offset )
421 return ((uint16_t)lsmash_bs_show_byte( bs, offset ) << 8)
422 | ((uint16_t)lsmash_bs_show_byte( bs, offset + 1 ));
425 uint32_t lsmash_bs_show_be24( lsmash_bs_t *bs, uint32_t offset )
427 return ((uint32_t)lsmash_bs_show_byte( bs, offset ) << 16)
428 | ((uint32_t)lsmash_bs_show_byte( bs, offset + 1 ) << 8)
429 | ((uint32_t)lsmash_bs_show_byte( bs, offset + 2 ));
432 uint32_t lsmash_bs_show_be32( lsmash_bs_t *bs, uint32_t offset )
434 return ((uint32_t)lsmash_bs_show_byte( bs, offset ) << 24)
435 | ((uint32_t)lsmash_bs_show_byte( bs, offset + 1 ) << 16)
436 | ((uint32_t)lsmash_bs_show_byte( bs, offset + 2 ) << 8)
437 | ((uint32_t)lsmash_bs_show_byte( bs, offset + 3 ));
440 uint64_t lsmash_bs_show_be64( lsmash_bs_t *bs, uint32_t offset )
442 return ((uint64_t)lsmash_bs_show_byte( bs, offset ) << 56)
443 | ((uint64_t)lsmash_bs_show_byte( bs, offset + 1 ) << 48)
444 | ((uint64_t)lsmash_bs_show_byte( bs, offset + 2 ) << 40)
445 | ((uint64_t)lsmash_bs_show_byte( bs, offset + 3 ) << 32)
446 | ((uint64_t)lsmash_bs_show_byte( bs, offset + 4 ) << 24)
447 | ((uint64_t)lsmash_bs_show_byte( bs, offset + 5 ) << 16)
448 | ((uint64_t)lsmash_bs_show_byte( bs, offset + 6 ) << 8)
449 | ((uint64_t)lsmash_bs_show_byte( bs, offset + 7 ));
452 uint8_t lsmash_bs_get_byte( lsmash_bs_t *bs )
454 if( bs->eob || bs->error )
455 return 0;
456 assert( bs->buffer.pos <= bs->buffer.store );
457 if( bs->buffer.pos == bs->buffer.store )
459 bs_fill_buffer( bs );
460 if( bs->error )
461 return 0;
462 if( bs->buffer.pos == bs->buffer.store && bs->eof )
464 /* No more read from both the stream and the buffer. */
465 bs->eob = 1;
466 return 0;
469 ++ bs->buffer.count; /* increment counter */
470 return bs->buffer.data[ bs->buffer.pos ++ ];
473 void lsmash_bs_skip_bytes( lsmash_bs_t *bs, uint32_t size )
475 if( bs->eob || bs->error || size == 0 )
476 return;
477 uint64_t remainder;
478 uint64_t offset = 0;
479 while( size > lsmash_bs_get_remaining_buffer_size( bs ) )
481 remainder = lsmash_bs_get_remaining_buffer_size( bs );
482 offset += remainder;
483 size -= remainder;
484 bs->buffer.pos = bs->buffer.store;
485 if( bs->eof )
487 /* No more read from both the stream and the buffer. */
488 bs->eob = 1;
489 break;
491 else
493 bs_fill_buffer( bs );
494 if( bs->error )
495 break;
498 remainder = LSMASH_MIN( size, lsmash_bs_get_remaining_buffer_size( bs ) );
499 offset += remainder;
500 bs->buffer.pos += remainder;
501 bs->buffer.count += offset;
504 void lsmash_bs_skip_bytes_64( lsmash_bs_t *bs, uint64_t size )
506 while( size )
508 uint64_t skip_bytes = LSMASH_MIN( size, UINT32_MAX );
509 lsmash_bs_skip_bytes( bs, (uint32_t)skip_bytes );
510 size -= skip_bytes;
511 if( bs->eob )
512 return;
516 static int64_t bs_get_bytes( lsmash_bs_t *bs, uint32_t size, uint8_t *buf )
518 size_t remainder;
519 size_t remain_size = size;
520 uintptr_t offset = 0;
521 while( remain_size > lsmash_bs_get_remaining_buffer_size( bs ) )
523 remainder = lsmash_bs_get_remaining_buffer_size( bs );
524 memcpy( buf + offset, lsmash_bs_get_buffer_data( bs ), remainder );
525 offset += remainder;
526 remain_size -= remainder;
527 bs->buffer.pos = bs->buffer.store;
528 if( bs->eof )
530 /* No more read from both the stream and the buffer. */
531 bs->eob = 1;
532 break;
534 else
536 bs_fill_buffer( bs );
537 if( bs->error )
539 bs->buffer.count += offset;
540 return LSMASH_ERR_NAMELESS;
544 remainder = LSMASH_MIN( remain_size, lsmash_bs_get_remaining_buffer_size( bs ) );
545 memcpy( buf + offset, lsmash_bs_get_buffer_data( bs ), remainder );
546 offset += remainder;
547 bs->buffer.pos += remainder;
548 bs->buffer.count += offset;
549 if( offset < size )
550 memset( buf + offset, 0, size - offset );
551 return (int64_t)offset;
554 uint8_t *lsmash_bs_get_bytes( lsmash_bs_t *bs, uint32_t size )
556 if( bs->eob || bs->error || size == 0 )
557 return NULL;
558 uint8_t *value = lsmash_malloc( size );
559 if( !value )
561 bs->error = 1;
562 return NULL;
564 if( bs_get_bytes( bs, size, value ) < 0 )
566 lsmash_free( value );
567 return NULL;
569 return value;
572 int64_t lsmash_bs_get_bytes_ex( lsmash_bs_t *bs, uint32_t size, uint8_t *value )
574 if( size == 0 )
575 return 0;
576 if( bs->eob || bs->error )
577 return LSMASH_ERR_NAMELESS;
578 return bs_get_bytes( bs, size, value );
581 uint16_t lsmash_bs_get_be16( lsmash_bs_t *bs )
583 uint16_t value = lsmash_bs_get_byte( bs );
584 return (value<<8) | lsmash_bs_get_byte( bs );
587 uint32_t lsmash_bs_get_be24( lsmash_bs_t *bs )
589 uint32_t value = lsmash_bs_get_byte( bs );
590 return (value<<16) | lsmash_bs_get_be16( bs );
593 uint32_t lsmash_bs_get_be32( lsmash_bs_t *bs )
595 uint32_t value = lsmash_bs_get_be16( bs );
596 return (value<<16) | lsmash_bs_get_be16( bs );
599 uint64_t lsmash_bs_get_be64( lsmash_bs_t *bs )
601 uint64_t value = lsmash_bs_get_be32( bs );
602 return (value<<32) | lsmash_bs_get_be32( bs );
605 uint64_t lsmash_bs_get_byte_to_64( lsmash_bs_t *bs )
607 return lsmash_bs_get_byte( bs );
610 uint64_t lsmash_bs_get_be16_to_64( lsmash_bs_t *bs )
612 return lsmash_bs_get_be16( bs );
615 uint64_t lsmash_bs_get_be24_to_64( lsmash_bs_t *bs )
617 return lsmash_bs_get_be24( bs );
620 uint64_t lsmash_bs_get_be32_to_64( lsmash_bs_t *bs )
622 return lsmash_bs_get_be32( bs );
625 uint16_t lsmash_bs_get_le16( lsmash_bs_t *bs )
627 uint16_t value = lsmash_bs_get_byte( bs );
628 return value | (lsmash_bs_get_byte( bs ) << 8);
631 uint32_t lsmash_bs_get_le32( lsmash_bs_t *bs )
633 uint32_t value = lsmash_bs_get_le16( bs );
634 return value | (lsmash_bs_get_le16( bs ) << 16);
637 int lsmash_bs_read( lsmash_bs_t *bs, uint32_t size )
639 if( !bs || size > INT_MAX )
640 return LSMASH_ERR_FUNCTION_PARAM;
641 if( size == 0 )
642 return 0;
643 bs_alloc( bs, bs->buffer.store + size );
644 if( bs->error || !bs->stream )
646 bs->error = 1;
647 return LSMASH_ERR_NAMELESS;
649 int read_size = bs->read( bs->stream, lsmash_bs_get_buffer_data_end( bs ), size );
650 if( read_size == 0 )
652 bs->eof = 1;
653 return 0;
655 else if( read_size < 0 )
657 bs->error = 1;
658 return LSMASH_ERR_NAMELESS;
660 bs->buffer.store += read_size;
661 bs->offset += read_size;
662 bs->written = LSMASH_MAX( bs->written, bs->offset );
663 return read_size;
666 int lsmash_bs_read_data( lsmash_bs_t *bs, uint8_t *buf, size_t *size )
668 if( !bs || !size || *size > INT_MAX )
669 return LSMASH_ERR_FUNCTION_PARAM;
670 if( !buf || *size == 0 )
671 return 0;
672 if( bs->error || !bs->stream )
674 bs->error = 1;
675 return LSMASH_ERR_NAMELESS;
677 int read_size = bs->read( bs->stream, buf, *size );
678 if( read_size == 0 )
679 bs->eof = 1;
680 else if( read_size < 0 )
682 bs->error = 1;
683 return LSMASH_ERR_NAMELESS;
685 bs->buffer.unseekable = 1;
686 bs->offset += read_size;
687 *size = read_size;
688 bs->written = LSMASH_MAX( bs->written, bs->offset );
689 return 0;
692 int lsmash_bs_import_data( lsmash_bs_t *bs, void *data, uint32_t length )
694 if( !bs || !data || length == 0 )
695 return LSMASH_ERR_FUNCTION_PARAM;
696 if( bs->error )
697 return LSMASH_ERR_NAMELESS;
698 bs_alloc( bs, bs->buffer.store + length );
699 if( bs->error || !bs->buffer.data ) /* means, failed to alloc. */
701 bs_buffer_free( bs );
702 return LSMASH_ERR_NAMELESS;
704 memcpy( lsmash_bs_get_buffer_data_end( bs ), data, length );
705 bs->buffer.store += length;
706 return 0;
708 /*---- ----*/
710 /*---- basic I/O ----*/
711 int lsmash_fread_wrapper( void *opaque, uint8_t *buf, int size )
713 int read_size = fread( buf, 1, size, (FILE *)opaque );
714 return ferror( (FILE *)opaque ) ? LSMASH_ERR_NAMELESS : read_size;
717 int lsmash_fwrite_wrapper( void *opaque, uint8_t *buf, int size )
719 return fwrite( buf, 1, size, (FILE *)opaque );
722 int64_t lsmash_fseek_wrapper( void *opaque, int64_t offset, int whence )
724 if( lsmash_fseek( (FILE *)opaque, offset, whence ) != 0 )
725 return LSMASH_ERR_NAMELESS;
726 return lsmash_ftell( (FILE *)opaque );