mms.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * MMS protocol common definitions.
  3. * Copyright (c) 2006,2007 Ryan Martell
  4. * Copyright (c) 2007 Björn Axelsson
  5. * Copyright (c) 2010 Zhentan Feng <spyfeng at gmail dot com>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "mms.h"
  24. #include "asf.h"
  25. #include "libavutil/intreadwrite.h"
  26. #define MMS_MAX_STREAMS 256 /**< arbitrary sanity check value */
  27. int ff_mms_read_header(MMSContext *mms, uint8_t *buf, const int size)
  28. {
  29. char *pos;
  30. int size_to_copy;
  31. int remaining_size = mms->asf_header_size - mms->asf_header_read_size;
  32. size_to_copy = FFMIN(size, remaining_size);
  33. pos = mms->asf_header + mms->asf_header_read_size;
  34. memcpy(buf, pos, size_to_copy);
  35. if (mms->asf_header_read_size == mms->asf_header_size) {
  36. av_freep(&mms->asf_header); // which contains asf header
  37. }
  38. mms->asf_header_read_size += size_to_copy;
  39. return size_to_copy;
  40. }
  41. int ff_mms_read_data(MMSContext *mms, uint8_t *buf, const int size)
  42. {
  43. int read_size;
  44. read_size = FFMIN(size, mms->remaining_in_len);
  45. memcpy(buf, mms->read_in_ptr, read_size);
  46. mms->remaining_in_len -= read_size;
  47. mms->read_in_ptr += read_size;
  48. return read_size;
  49. }
  50. int ff_mms_asf_header_parser(MMSContext *mms)
  51. {
  52. uint8_t *p = mms->asf_header;
  53. uint8_t *end;
  54. int flags, stream_id;
  55. mms->stream_num = 0;
  56. if (mms->asf_header_size < sizeof(ff_asf_guid) * 2 + 22 ||
  57. memcmp(p, ff_asf_header, sizeof(ff_asf_guid))) {
  58. av_log(NULL, AV_LOG_ERROR,
  59. "Corrupt stream (invalid ASF header, size=%d)\n",
  60. mms->asf_header_size);
  61. return AVERROR_INVALIDDATA;
  62. }
  63. end = mms->asf_header + mms->asf_header_size;
  64. p += sizeof(ff_asf_guid) + 14;
  65. while(end - p >= sizeof(ff_asf_guid) + 8) {
  66. uint64_t chunksize;
  67. if (!memcmp(p, ff_asf_data_header, sizeof(ff_asf_guid))) {
  68. chunksize = 50; // see Reference [2] section 5.1
  69. } else {
  70. chunksize = AV_RL64(p + sizeof(ff_asf_guid));
  71. }
  72. if (!chunksize || chunksize > end - p) {
  73. av_log(NULL, AV_LOG_ERROR,
  74. "Corrupt stream (header chunksize %"PRId64" is invalid)\n",
  75. chunksize);
  76. return AVERROR_INVALIDDATA;
  77. }
  78. if (!memcmp(p, ff_asf_file_header, sizeof(ff_asf_guid))) {
  79. /* read packet size */
  80. if (end - p > sizeof(ff_asf_guid) * 2 + 68) {
  81. mms->asf_packet_len = AV_RL32(p + sizeof(ff_asf_guid) * 2 + 64);
  82. if (mms->asf_packet_len <= 0 || mms->asf_packet_len > sizeof(mms->in_buffer)) {
  83. av_log(NULL, AV_LOG_ERROR,
  84. "Corrupt stream (too large pkt_len %d)\n",
  85. mms->asf_packet_len);
  86. return AVERROR_INVALIDDATA;
  87. }
  88. }
  89. } else if (!memcmp(p, ff_asf_stream_header, sizeof(ff_asf_guid))) {
  90. flags = AV_RL16(p + sizeof(ff_asf_guid)*3 + 24);
  91. stream_id = flags & 0x7F;
  92. //The second condition is for checking CS_PKT_STREAM_ID_REQUEST packet size,
  93. //we can calcuate the packet size by stream_num.
  94. //Please see function send_stream_selection_request().
  95. if (mms->stream_num < MMS_MAX_STREAMS &&
  96. 46 + mms->stream_num * 6 < sizeof(mms->out_buffer)) {
  97. mms->streams = av_fast_realloc(mms->streams,
  98. &mms->nb_streams_allocated,
  99. (mms->stream_num + 1) * sizeof(MMSStream));
  100. mms->streams[mms->stream_num].id = stream_id;
  101. mms->stream_num++;
  102. } else {
  103. av_log(NULL, AV_LOG_ERROR,
  104. "Corrupt stream (too many A/V streams)\n");
  105. return AVERROR_INVALIDDATA;
  106. }
  107. } else if (!memcmp(p, ff_asf_ext_stream_header, sizeof(ff_asf_guid))) {
  108. if (end - p >= 88) {
  109. int stream_count = AV_RL16(p + 84), ext_len_count = AV_RL16(p + 86);
  110. uint64_t skip_bytes = 88;
  111. while (stream_count--) {
  112. if (end - p < skip_bytes + 4) {
  113. av_log(NULL, AV_LOG_ERROR,
  114. "Corrupt stream (next stream name length is not in the buffer)\n");
  115. return AVERROR_INVALIDDATA;
  116. }
  117. skip_bytes += 4 + AV_RL16(p + skip_bytes + 2);
  118. }
  119. while (ext_len_count--) {
  120. if (end - p < skip_bytes + 22) {
  121. av_log(NULL, AV_LOG_ERROR,
  122. "Corrupt stream (next extension system info length is not in the buffer)\n");
  123. return AVERROR_INVALIDDATA;
  124. }
  125. skip_bytes += 22 + AV_RL32(p + skip_bytes + 18);
  126. }
  127. if (end - p < skip_bytes) {
  128. av_log(NULL, AV_LOG_ERROR,
  129. "Corrupt stream (the last extension system info length is invalid)\n");
  130. return AVERROR_INVALIDDATA;
  131. }
  132. if (chunksize - skip_bytes > 24)
  133. chunksize = skip_bytes;
  134. }
  135. } else if (!memcmp(p, ff_asf_head1_guid, sizeof(ff_asf_guid))) {
  136. chunksize = 46; // see references [2] section 3.4. This should be set 46.
  137. }
  138. p += chunksize;
  139. }
  140. return 0;
  141. }