comparison aviobuf.c @ 885:da1d5db0ce5c libavformat

COSMETICS: Remove all trailing whitespace.
author diego
date Sat, 17 Dec 2005 18:14:38 +0000
parents 66cc656ea404
children d70e50f1495f
comparison
equal deleted inserted replaced
884:2ece9c9dd94c 885:da1d5db0ce5c
33 { 33 {
34 s->buffer = buffer; 34 s->buffer = buffer;
35 s->buffer_size = buffer_size; 35 s->buffer_size = buffer_size;
36 s->buf_ptr = buffer; 36 s->buf_ptr = buffer;
37 s->write_flag = write_flag; 37 s->write_flag = write_flag;
38 if (!s->write_flag) 38 if (!s->write_flag)
39 s->buf_end = buffer; 39 s->buf_end = buffer;
40 else 40 else
41 s->buf_end = buffer + buffer_size; 41 s->buf_end = buffer + buffer_size;
42 s->opaque = opaque; 42 s->opaque = opaque;
43 s->write_packet = write_packet; 43 s->write_packet = write_packet;
50 s->is_streamed = 0; 50 s->is_streamed = 0;
51 s->max_packet_size = 0; 51 s->max_packet_size = 0;
52 s->update_checksum= NULL; 52 s->update_checksum= NULL;
53 return 0; 53 return 0;
54 } 54 }
55 55
56 56
57 #ifdef CONFIG_MUXERS 57 #ifdef CONFIG_MUXERS
58 static void flush_buffer(ByteIOContext *s) 58 static void flush_buffer(ByteIOContext *s)
59 { 59 {
60 if (s->buf_ptr > s->buffer) { 60 if (s->buf_ptr > s->buffer) {
74 } 74 }
75 75
76 void put_byte(ByteIOContext *s, int b) 76 void put_byte(ByteIOContext *s, int b)
77 { 77 {
78 *(s->buf_ptr)++ = b; 78 *(s->buf_ptr)++ = b;
79 if (s->buf_ptr >= s->buf_end) 79 if (s->buf_ptr >= s->buf_end)
80 flush_buffer(s); 80 flush_buffer(s);
81 } 81 }
82 82
83 void put_buffer(ByteIOContext *s, const unsigned char *buf, int size) 83 void put_buffer(ByteIOContext *s, const unsigned char *buf, int size)
84 { 84 {
89 if (len > size) 89 if (len > size)
90 len = size; 90 len = size;
91 memcpy(s->buf_ptr, buf, len); 91 memcpy(s->buf_ptr, buf, len);
92 s->buf_ptr += len; 92 s->buf_ptr += len;
93 93
94 if (s->buf_ptr >= s->buf_end) 94 if (s->buf_ptr >= s->buf_end)
95 flush_buffer(s); 95 flush_buffer(s);
96 96
97 buf += len; 97 buf += len;
98 size -= len; 98 size -= len;
99 } 99 }
110 { 110 {
111 offset_t offset1; 111 offset_t offset1;
112 112
113 if (whence != SEEK_CUR && whence != SEEK_SET) 113 if (whence != SEEK_CUR && whence != SEEK_SET)
114 return -EINVAL; 114 return -EINVAL;
115 115
116 #ifdef CONFIG_MUXERS 116 #ifdef CONFIG_MUXERS
117 if (s->write_flag) { 117 if (s->write_flag) {
118 if (whence == SEEK_CUR) { 118 if (whence == SEEK_CUR) {
119 offset1 = s->pos + (s->buf_ptr - s->buffer); 119 offset1 = s->pos + (s->buf_ptr - s->buffer);
120 if (offset == 0) 120 if (offset == 0)
121 return offset1; 121 return offset1;
122 offset += offset1; 122 offset += offset1;
123 } 123 }
124 offset1 = offset - s->pos; 124 offset1 = offset - s->pos;
125 if (!s->must_flush && 125 if (!s->must_flush &&
126 offset1 >= 0 && offset1 < (s->buf_end - s->buffer)) { 126 offset1 >= 0 && offset1 < (s->buf_end - s->buffer)) {
127 /* can do the seek inside the buffer */ 127 /* can do the seek inside the buffer */
128 s->buf_ptr = s->buffer + offset1; 128 s->buf_ptr = s->buffer + offset1;
129 } else { 129 } else {
130 if (!s->seek) 130 if (!s->seek)
133 s->must_flush = 1; 133 s->must_flush = 1;
134 s->buf_ptr = s->buffer; 134 s->buf_ptr = s->buffer;
135 s->seek(s->opaque, offset, SEEK_SET); 135 s->seek(s->opaque, offset, SEEK_SET);
136 s->pos = offset; 136 s->pos = offset;
137 } 137 }
138 } else 138 } else
139 #endif //CONFIG_MUXERS 139 #endif //CONFIG_MUXERS
140 { 140 {
141 if (whence == SEEK_CUR) { 141 if (whence == SEEK_CUR) {
142 offset1 = s->pos - (s->buf_end - s->buffer) + (s->buf_ptr - s->buffer); 142 offset1 = s->pos - (s->buf_end - s->buffer) + (s->buf_ptr - s->buffer);
143 if (offset == 0) 143 if (offset == 0)
173 } 173 }
174 174
175 offset_t url_fsize(ByteIOContext *s) 175 offset_t url_fsize(ByteIOContext *s)
176 { 176 {
177 offset_t size; 177 offset_t size;
178 178
179 if (!s->seek) 179 if (!s->seek)
180 return -EPIPE; 180 return -EPIPE;
181 size = s->seek(s->opaque, -1, SEEK_END) + 1; 181 size = s->seek(s->opaque, -1, SEEK_END) + 1;
182 s->seek(s->opaque, s->pos, SEEK_SET); 182 s->seek(s->opaque, s->pos, SEEK_SET);
183 return size; 183 return size;
373 } 373 }
374 374
375 int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size) 375 int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size)
376 { 376 {
377 int len; 377 int len;
378 378
379 if(size<0) 379 if(size<0)
380 return -1; 380 return -1;
381 381
382 len = s->buf_end - s->buf_ptr; 382 len = s->buf_end - s->buf_ptr;
383 if (len == 0) { 383 if (len == 0) {
445 445
446 while ((c = get_byte(s))) { 446 while ((c = get_byte(s))) {
447 if (i < maxlen-1) 447 if (i < maxlen-1)
448 buf[i++] = c; 448 buf[i++] = c;
449 } 449 }
450 450
451 buf[i] = 0; /* Ensure null terminated, but may be truncated */ 451 buf[i] = 0; /* Ensure null terminated, but may be truncated */
452 452
453 return buf; 453 return buf;
454 } 454 }
455 455
489 int url_fdopen(ByteIOContext *s, URLContext *h) 489 int url_fdopen(ByteIOContext *s, URLContext *h)
490 { 490 {
491 uint8_t *buffer; 491 uint8_t *buffer;
492 int buffer_size, max_packet_size; 492 int buffer_size, max_packet_size;
493 493
494 494
495 max_packet_size = url_get_max_packet_size(h); 495 max_packet_size = url_get_max_packet_size(h);
496 if (max_packet_size) { 496 if (max_packet_size) {
497 buffer_size = max_packet_size; /* no need to bufferize more than one packet */ 497 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
498 } else { 498 } else {
499 buffer_size = IO_BUFFER_SIZE; 499 buffer_size = IO_BUFFER_SIZE;
500 } 500 }
501 buffer = av_malloc(buffer_size); 501 buffer = av_malloc(buffer_size);
502 if (!buffer) 502 if (!buffer)
503 return -ENOMEM; 503 return -ENOMEM;
504 504
505 if (init_put_byte(s, buffer, buffer_size, 505 if (init_put_byte(s, buffer, buffer_size,
506 (h->flags & URL_WRONLY || h->flags & URL_RDWR), h, 506 (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
507 url_read_packet, url_write_packet, url_seek_packet) < 0) { 507 url_read_packet, url_write_packet, url_seek_packet) < 0) {
508 av_free(buffer); 508 av_free(buffer);
509 return AVERROR_IO; 509 return AVERROR_IO;
510 } 510 }
523 523
524 av_free(s->buffer); 524 av_free(s->buffer);
525 s->buffer = buffer; 525 s->buffer = buffer;
526 s->buffer_size = buf_size; 526 s->buffer_size = buf_size;
527 s->buf_ptr = buffer; 527 s->buf_ptr = buffer;
528 if (!s->write_flag) 528 if (!s->write_flag)
529 s->buf_end = buffer; 529 s->buf_end = buffer;
530 else 530 else
531 s->buf_end = buffer + buf_size; 531 s->buf_end = buffer + buf_size;
532 return 0; 532 return 0;
533 } 533 }
551 } 551 }
552 552
553 int url_fclose(ByteIOContext *s) 553 int url_fclose(ByteIOContext *s)
554 { 554 {
555 URLContext *h = s->opaque; 555 URLContext *h = s->opaque;
556 556
557 av_free(s->buffer); 557 av_free(s->buffer);
558 memset(s, 0, sizeof(ByteIOContext)); 558 memset(s, 0, sizeof(ByteIOContext));
559 return url_close(h); 559 return url_close(h);
560 } 560 }
561 561
601 if (buf_size > 0) 601 if (buf_size > 0)
602 *q = '\0'; 602 *q = '\0';
603 return buf; 603 return buf;
604 } 604 }
605 605
606 /* 606 /*
607 * Return the maximum packet size associated to packetized buffered file 607 * Return the maximum packet size associated to packetized buffered file
608 * handle. If the file is not packetized (stream like http or file on 608 * handle. If the file is not packetized (stream like http or file on
609 * disk), then 0 is returned. 609 * disk), then 0 is returned.
610 * 610 *
611 * @param h buffered file handle 611 * @param h buffered file handle
612 * @return maximum packet size in bytes 612 * @return maximum packet size in bytes
613 */ 613 */
614 int url_fget_max_packet_size(ByteIOContext *s) 614 int url_fget_max_packet_size(ByteIOContext *s)
615 { 615 {
618 618
619 #ifdef CONFIG_MUXERS 619 #ifdef CONFIG_MUXERS
620 /* buffer handling */ 620 /* buffer handling */
621 int url_open_buf(ByteIOContext *s, uint8_t *buf, int buf_size, int flags) 621 int url_open_buf(ByteIOContext *s, uint8_t *buf, int buf_size, int flags)
622 { 622 {
623 return init_put_byte(s, buf, buf_size, 623 return init_put_byte(s, buf, buf_size,
624 (flags & URL_WRONLY || flags & URL_RDWR), 624 (flags & URL_WRONLY || flags & URL_RDWR),
625 NULL, NULL, NULL, NULL); 625 NULL, NULL, NULL, NULL);
626 } 626 }
627 627
628 /* return the written or read size */ 628 /* return the written or read size */
643 643
644 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size) 644 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
645 { 645 {
646 DynBuffer *d = opaque; 646 DynBuffer *d = opaque;
647 int new_size, new_allocated_size; 647 int new_size, new_allocated_size;
648 648
649 /* reallocate buffer if needed */ 649 /* reallocate buffer if needed */
650 new_size = d->pos + buf_size; 650 new_size = d->pos + buf_size;
651 new_allocated_size = d->allocated_size; 651 new_allocated_size = d->allocated_size;
652 if(new_size < d->pos || new_size > INT_MAX/2) 652 if(new_size < d->pos || new_size > INT_MAX/2)
653 return -1; 653 return -1;
654 while (new_size > new_allocated_size) { 654 while (new_size > new_allocated_size) {
655 if (!new_allocated_size) 655 if (!new_allocated_size)
656 new_allocated_size = new_size; 656 new_allocated_size = new_size;
657 else 657 else
658 new_allocated_size += new_allocated_size / 2 + 1; 658 new_allocated_size += new_allocated_size / 2 + 1;
659 } 659 }
660 660
661 if (new_allocated_size > d->allocated_size) { 661 if (new_allocated_size > d->allocated_size) {
662 d->buffer = av_realloc(d->buffer, new_allocated_size); 662 d->buffer = av_realloc(d->buffer, new_allocated_size);
663 if(d->buffer == NULL) 663 if(d->buffer == NULL)
664 return -1234; 664 return -1234;
665 d->allocated_size = new_allocated_size; 665 d->allocated_size = new_allocated_size;
705 705
706 static int url_open_dyn_buf_internal(ByteIOContext *s, int max_packet_size) 706 static int url_open_dyn_buf_internal(ByteIOContext *s, int max_packet_size)
707 { 707 {
708 DynBuffer *d; 708 DynBuffer *d;
709 int io_buffer_size, ret; 709 int io_buffer_size, ret;
710 710
711 if (max_packet_size) 711 if (max_packet_size)
712 io_buffer_size = max_packet_size; 712 io_buffer_size = max_packet_size;
713 else 713 else
714 io_buffer_size = 1024; 714 io_buffer_size = 1024;
715 715
716 if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size) 716 if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
717 return -1; 717 return -1;
718 d = av_malloc(sizeof(DynBuffer) + io_buffer_size); 718 d = av_malloc(sizeof(DynBuffer) + io_buffer_size);
719 if (!d) 719 if (!d)
720 return -1; 720 return -1;
721 d->io_buffer_size = io_buffer_size; 721 d->io_buffer_size = io_buffer_size;
722 d->buffer = NULL; 722 d->buffer = NULL;
723 d->pos = 0; 723 d->pos = 0;
724 d->size = 0; 724 d->size = 0;
725 d->allocated_size = 0; 725 d->allocated_size = 0;
726 ret = init_put_byte(s, d->io_buffer, io_buffer_size, 726 ret = init_put_byte(s, d->io_buffer, io_buffer_size,
727 1, d, NULL, 727 1, d, NULL,
728 max_packet_size ? dyn_packet_buf_write : dyn_buf_write, 728 max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
729 max_packet_size ? NULL : dyn_buf_seek); 729 max_packet_size ? NULL : dyn_buf_seek);
730 if (ret == 0) { 730 if (ret == 0) {
731 s->max_packet_size = max_packet_size; 731 s->max_packet_size = max_packet_size;
732 } 732 }
733 return ret; 733 return ret;
734 } 734 }
735 735
736 /* 736 /*
737 * Open a write only memory stream. 737 * Open a write only memory stream.
738 * 738 *
739 * @param s new IO context 739 * @param s new IO context
740 * @return zero if no error. 740 * @return zero if no error.
741 */ 741 */
742 int url_open_dyn_buf(ByteIOContext *s) 742 int url_open_dyn_buf(ByteIOContext *s)
743 { 743 {
746 746
747 /* 747 /*
748 * Open a write only packetized memory stream with a maximum packet 748 * Open a write only packetized memory stream with a maximum packet
749 * size of 'max_packet_size'. The stream is stored in a memory buffer 749 * size of 'max_packet_size'. The stream is stored in a memory buffer
750 * with a big endian 4 byte header giving the packet size in bytes. 750 * with a big endian 4 byte header giving the packet size in bytes.
751 * 751 *
752 * @param s new IO context 752 * @param s new IO context
753 * @param max_packet_size maximum packet size (must be > 0) 753 * @param max_packet_size maximum packet size (must be > 0)
754 * @return zero if no error. 754 * @return zero if no error.
755 */ 755 */
756 int url_open_dyn_packet_buf(ByteIOContext *s, int max_packet_size) 756 int url_open_dyn_packet_buf(ByteIOContext *s, int max_packet_size)
757 { 757 {
758 if (max_packet_size <= 0) 758 if (max_packet_size <= 0)
759 return -1; 759 return -1;
760 return url_open_dyn_buf_internal(s, max_packet_size); 760 return url_open_dyn_buf_internal(s, max_packet_size);
761 } 761 }
762 762
763 /* 763 /*
764 * Return the written size and a pointer to the buffer. The buffer 764 * Return the written size and a pointer to the buffer. The buffer
765 * must be freed with av_free(). 765 * must be freed with av_free().
766 * @param s IO context 766 * @param s IO context
767 * @param pointer to a byte buffer 767 * @param pointer to a byte buffer
768 * @return the length of the byte buffer 768 * @return the length of the byte buffer
769 */ 769 */
770 int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer) 770 int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer)