+ int length_remaining;
+ int packet_id_type;
+ int tmp;
+
+ // note we cache the first 8 bytes,
+ // then fill up the buffer with the others
+ tmp = AV_RL16(mms->in_buffer + 6);
+ length_remaining = (tmp - 8) & 0xffff;
+ mmst->incoming_packet_seq = AV_RL32(mms->in_buffer);
+ packet_id_type = mms->in_buffer[4];
+ mmst->incoming_flags = mms->in_buffer[5];
+
+ if (length_remaining < 0
+ || length_remaining > sizeof(mms->in_buffer) - 8) {
+ av_log(NULL, AV_LOG_ERROR,
+ "Data length %d is invalid or too large (max=%zu)\n",
+ length_remaining, sizeof(mms->in_buffer));
+ return AVERROR_INVALIDDATA;
+ }
+ mms->remaining_in_len = length_remaining;
+ mms->read_in_ptr = mms->in_buffer;
+ read_result= url_read_complete(mms->mms_hd, mms->in_buffer, length_remaining);
+ if(read_result != length_remaining) {
+ av_log(NULL, AV_LOG_ERROR,
+ "Failed to read packet data of size %d: %d (%s)\n",
+ length_remaining, read_result,
+ read_result < 0 ? strerror(read_result) :
+ "The server closed the connection");
+ return read_result < 0 ? read_result : AVERROR_IO;
+ }
+
+ // if we successfully read everything.
+ if(packet_id_type == mmst->header_packet_id) {
+ packet_type = SC_PKT_ASF_HEADER;
+ // Store the asf header
+ if(!mms->header_parsed) {
+ void *p = av_realloc(mms->asf_header,
+ mms->asf_header_size + mms->remaining_in_len);
+ if (!p) {
+ av_freep(&mms->asf_header);
+ return AVERROR(ENOMEM);
+ }
+ mms->asf_header = p;
+ memcpy(mms->asf_header + mms->asf_header_size,
+ mms->read_in_ptr, mms->remaining_in_len);
+ mms->asf_header_size += mms->remaining_in_len;
+ }
+ // 0x04 means asf header is sent in multiple packets.
+ if (mmst->incoming_flags == 0x04)
+ continue;
+ } else if(packet_id_type == mmst->packet_id) {
+ packet_type = SC_PKT_ASF_MEDIA;