void nsWebMBufferedParser::Append(const unsigned char* aBuffer, uint32_t aLength, nsTArray<nsWebMTimeDataOffset>& aMapping, ReentrantMonitor& aReentrantMonitor) { static const unsigned char CLUSTER_ID[] = { 0x1f, 0x43, 0xb6, 0x75 }; static const unsigned char TIMECODE_ID = 0xe7; static const unsigned char BLOCKGROUP_ID = 0xa0; static const unsigned char BLOCK_ID = 0xa1; static const unsigned char SIMPLEBLOCK_ID = 0xa3; const unsigned char* p = aBuffer; // Parse each byte in aBuffer one-by-one, producing timecodes and updating // aMapping as we go. Parser pauses at end of stream (which may be at any // point within the parse) and resumes parsing the next time Append is // called with new data. while (p < aBuffer + aLength) { switch (mState) { case CLUSTER_SYNC: if (*p++ == CLUSTER_ID[mClusterIDPos]) { mClusterIDPos += 1; } else { mClusterIDPos = 0; } // Cluster ID found, it's likely this is a valid sync point. If this // is a spurious match, the later parse steps will encounter an error // and return to CLUSTER_SYNC. if (mClusterIDPos == sizeof(CLUSTER_ID)) { mClusterIDPos = 0; mState = READ_VINT; mNextState = TIMECODE_SYNC; } break; case READ_VINT: { unsigned char c = *p++; uint32_t mask; mVIntLength = VIntLength(c, &mask); mVIntLeft = mVIntLength - 1; mVInt = c & ~mask; mState = READ_VINT_REST; break; } case READ_VINT_REST: if (mVIntLeft) { mVInt <<= 8; mVInt |= *p++; mVIntLeft -= 1; } else { mState = mNextState; } break; case TIMECODE_SYNC: if (*p++ != TIMECODE_ID) { p -= 1; mState = CLUSTER_SYNC; break; } mClusterTimecode = 0; mState = READ_VINT; mNextState = READ_CLUSTER_TIMECODE; break; case READ_CLUSTER_TIMECODE: if (mVInt) { mClusterTimecode <<= 8; mClusterTimecode |= *p++; mVInt -= 1; } else { mState = ANY_BLOCK_SYNC; } break; case ANY_BLOCK_SYNC: { unsigned char c = *p++; if (c == BLOCKGROUP_ID) { mState = READ_VINT; mNextState = ANY_BLOCK_SYNC; } else if (c == SIMPLEBLOCK_ID || c == BLOCK_ID) { mBlockOffset = mCurrentOffset + (p - aBuffer) - 1; mState = READ_VINT; mNextState = READ_BLOCK; } else { uint32_t length = VIntLength(c, nullptr); if (length == 4) { p -= 1; mState = CLUSTER_SYNC; } else { mState = READ_VINT; mNextState = SKIP_ELEMENT; } } break; } case READ_BLOCK: mBlockSize = mVInt; mBlockTimecode = 0; mBlockTimecodeLength = 2; mState = READ_VINT; mNextState = READ_BLOCK_TIMECODE; break; case READ_BLOCK_TIMECODE: if (mBlockTimecodeLength) { mBlockTimecode <<= 8; mBlockTimecode |= *p++; mBlockTimecodeLength -= 1; } else { // It's possible we've parsed this data before, so avoid inserting // duplicate nsWebMTimeDataOffset entries. { ReentrantMonitorAutoEnter mon(aReentrantMonitor); uint32_t idx; if (!aMapping.GreatestIndexLtEq(mBlockOffset, idx)) { nsWebMTimeDataOffset entry(mBlockOffset, mClusterTimecode + mBlockTimecode); aMapping.InsertElementAt(idx, entry); } } // Skip rest of block header and the block's payload. mBlockSize -= mVIntLength; mBlockSize -= 2; mSkipBytes = uint32_t(mBlockSize); mState = SKIP_DATA; mNextState = ANY_BLOCK_SYNC; } break; case SKIP_DATA: if (mSkipBytes) { uint32_t left = aLength - (p - aBuffer); left = NS_MIN(left, mSkipBytes); p += left; mSkipBytes -= left; } else { mState = mNextState; } break; case SKIP_ELEMENT: mSkipBytes = uint32_t(mVInt); mState = SKIP_DATA; mNextState = ANY_BLOCK_SYNC; break; } } NS_ASSERTION(p == aBuffer + aLength, "Must have parsed to end of data."); mCurrentOffset += aLength; }