bool G2Packet::ReadPacket(char* pszType, quint32& nLength, bool* pbCompound) { if ( GetRemaining() == 0 ) return false; char nInput = ReadByte(); if ( nInput == 0 ) return false; char nLenLen = ( nInput & 0xC0 ) >> 6; char nTypeLen = ( nInput & 0x38 ) >> 3; char nFlags = ( nInput & 0x07 ); if ( GetRemaining() < nTypeLen + nLenLen + 1 ) throw packet_error(); nLength = 0; Read( &nLength, nLenLen ); if ( GetRemaining() < (int)nLength + nTypeLen + 1 ) throw packet_error(); Read( pszType, nTypeLen + 1 ); pszType[ nTypeLen + 1 ] = 0; if ( pbCompound ) { *pbCompound = ( nFlags & G2_FLAG_COMPOUND ) == G2_FLAG_COMPOUND; } else { if ( nFlags & G2_FLAG_COMPOUND ) SkipCompound( nLength ); } return true; }
uint32_t CowCircularBuffer::PutBuffer(void *buf, uint32_t size) { uint32_t numToPut = 0; uint32_t top = 0; uint32_t bottom = 0; if ((size > GetSize()) || !size) { printf("Invalid size to put!"); return numToPut; } if (m_OverwriteWhenFull) { // overwrite anything with the new buffer numToPut = size; // update the start marker if necessary if (numToPut > GetRemaining()) { m_StartIndex = (m_StartIndex + numToPut) % m_Size; } } else { // only fill up what's left numToPut = (GetRemaining() > size) ? size : GetRemaining(); } // determine if we have to wrap around // top is the high end of the circular buffer // bottom is the beginning of the circular buffer if ((m_EndIndex + numToPut) > m_Size) { top = m_Size - m_EndIndex; bottom = numToPut - top; } else { top = size; } memcpy(&m_Buffer[m_EndIndex], buf, top); m_EndIndex += top; if (bottom) { memcpy(m_Buffer, &((char *)buf)[top], bottom); m_EndIndex = bottom; } return numToPut; }
void Buffer::Append(const char* buffer_, unsigned length_) { if (length_ > GetRemaining()) Allocate(length + length_); memcpy(GetPosition(), buffer_, length_); Lengthen(length_); }
unsigned Buffer::Appendf(const char* fmt, ...) { unsigned required; va_list ap; while (true) { va_start(ap, fmt); required = VWritef(GetPosition(), GetRemaining(), fmt, ap); va_end(ap); // snwritef returns number of bytes required if (required <= GetRemaining()) break; Allocate(length + required, true); } length += required; return required; }
ECode FloatArrayBuffer::Slice( /* [out] */ IFloatBuffer** buffer) { VALIDATE_NOT_NULL(buffer) Int32 remainvalue = 0; GetRemaining(&remainvalue); AutoPtr<FloatArrayBuffer> fab = new FloatArrayBuffer(); FAIL_RETURN(fab->constructor(remainvalue, mBackingArray, mArrayOffset + mPosition, mIsReadOnly)) *buffer = IFloatBuffer::Probe(fab); REFCOUNT_ADD(*buffer) return NOERROR; }
ECode Int32ArrayBuffer::Compact() { if (mIsReadOnly) { // throw new ReadOnlyBufferException(); return E_READ_ONLY_BUFFER_EXCEPTION; } // System.arraycopy(backingArray, mPosition + mArrayOffset, backingArray, mArrayOffset, remaining()); Int32 reaminvalue = 0; GetRemaining(&reaminvalue); mBackingArray->Copy(mArrayOffset, mBackingArray, mPosition + mArrayOffset, reaminvalue); mPosition = mLimit - mPosition; mLimit = mCapacity; mMark = UNSET_MARK; return NOERROR; }
ECode Int32ArrayBuffer::Get( /* [out] */ ArrayOf<Int32>* dst, /* [in] */ Int32 dstOffset, /* [in] */ Int32 int32Count) { VALIDATE_NOT_NULL(dst); Int32 remaining = 0; GetRemaining(&remaining); if (int32Count > remaining) { // throw new BufferUnderflowException(); return E_BUFFER_UNDER_FLOW_EXCEPTION; } dst->Copy(dstOffset, mBackingArray, mArrayOffset + mPosition, int32Count); mPosition += int32Count; return NOERROR; }
bool G2Packet::GetTo(QUuid& pGUID) { if ( m_bCompound == false ) return false; if ( GetRemaining() < 4 + 16 ) return false; char* pTest = m_oBuffer.data() + m_nPosition; if ( pTest[0] != 0x48 ) return false; if ( pTest[1] != 0x10 ) return false; if ( pTest[2] != 'T' ) return false; if ( pTest[3] != 'O' ) return false; m_nPosition = 4; pGUID = ReadGUID(); m_nPosition = 0; return true; }
bool CDSMSplitterFile::Sync(UINT64& syncpos, dsmp_t& type, UINT64& len, __int64 limit) { BitByteAlign(); limit += DSMSW_SIZE; for (UINT64 id = 0; (id&((1ui64<<(DSMSW_SIZE<<3))-1)) != DSMSW; id = (id << 8) | (BYTE)BitRead(8)) { if (limit-- <= 0 || GetRemaining() <= 2) { return false; } } syncpos = GetPos() - (DSMSW_SIZE<<3); type = (dsmp_t)BitRead(5); len = BitRead(((int)BitRead(3)+1)<<3); return true; }
ECode Int32ArrayBuffer::Put( /* [in] */ const ArrayOf<Int32>& src, /* [in] */ Int32 srcOffset, /* [in] */ Int32 doubleCount) { if (mIsReadOnly) { // throw new ReadOnlyBufferException(); return E_READ_ONLY_BUFFER_EXCEPTION; } Int32 remainvalue = 0; GetRemaining(&remainvalue); if (doubleCount > remainvalue) { // throw new BufferOverflowException(); return E_BUFFER_OVERFLOW_EXCEPTION; } // System.arraycopy(src, srcOffset, backingArray, mArrayOffset + mPosition, doubleCount); mBackingArray->Copy(mArrayOffset + mPosition, &src, srcOffset, doubleCount); mPosition += doubleCount; return NOERROR; }
bool irc::tokenstream::GetToken(std::string &token) { bool first = !pos; if (!spacesepstream::GetToken(token)) return false; /* This is the last parameter */ if (token[0] == ':' && !first) { token.erase(token.begin()); if (!StreamEnd()) { token += ' '; token += GetRemaining(); } pos = tokens.length() + 1; } return true; }
__int64 CDSMSplitterFile::FindSyncPoint(REFERENCE_TIME rt) { if (/*!m_sps.IsEmpty()*/ m_sps.GetCount() > 1) { int i = range_bsearch(m_sps, m_rtFirst + rt); return i >= 0 ? m_sps[i].fp : 0; } if (m_rtDuration <= 0 || rt <= m_rtFirst) { return 0; } // ok, do the hard way then dsmp_t type; UINT64 syncpos, len; // 1. find some boundaries close to rt's position (minpos, maxpos) __int64 minpos = 0, maxpos = GetLength(); for (int i = 0; i < 10 && (maxpos - minpos) >= 1024*1024; i++) { Seek((minpos + maxpos) / 2); while (GetPos() < maxpos) { if (!Sync(syncpos, type, len)) { continue; } __int64 pos = GetPos(); if (type == DSMP_SAMPLE) { Packet p; if (Read(len, &p, false) && p.rtStart != Packet::INVALID_TIME) { REFERENCE_TIME dt = (p.rtStart -= m_rtFirst) - rt; if (dt >= 0) { maxpos = max((__int64)syncpos - 65536, minpos); } else { minpos = syncpos; } break; } } Seek(pos + len); } } // 2. find the first packet just after rt (maxpos) Seek(minpos); while (GetRemaining()) { if (!Sync(syncpos, type, len)) { continue; } __int64 pos = GetPos(); if (type == DSMP_SAMPLE) { Packet p; if (Read(len, &p, false) && p.rtStart != Packet::INVALID_TIME) { REFERENCE_TIME dt = (p.rtStart -= m_rtFirst) - rt; if (dt >= 0) { maxpos = (__int64)syncpos; break; } } } Seek(pos + len); } // 3. iterate backwards from maxpos and find at least one syncpoint for every stream, except for subtitle streams CAtlMap<BYTE,BYTE> ids; { POSITION pos = m_mts.GetStartPosition(); while (pos) { BYTE id; CMediaType mt; m_mts.GetNextAssoc(pos, id, mt); if (mt.majortype != MEDIATYPE_Text && mt.majortype != MEDIATYPE_Subtitle) { ids[id] = 0; } } } __int64 ret = maxpos; while (maxpos > 0 && !ids.IsEmpty()) { minpos = max(0, maxpos - 65536); Seek(minpos); while (Sync(syncpos, type, len) && GetPos() < maxpos) { UINT64 pos = GetPos(); if (type == DSMP_SAMPLE) { Packet p; if (Read(len, &p, false) && p.rtStart != Packet::INVALID_TIME && p.bSyncPoint) { BYTE id = (BYTE)p.TrackNumber, tmp; if (ids.Lookup(id, tmp)) { ids.RemoveKey((BYTE)p.TrackNumber); ret = min(ret, (__int64)syncpos); } } } Seek(pos + len); } maxpos = minpos; } return ret; }
REFERENCE_TIME CMpegSplitterFile::NextPTS(DWORD TrackNum) { REFERENCE_TIME rt = -1; __int64 rtpos = -1; BYTE b; while(GetRemaining()) { if(m_type == ps || m_type == es) { if(!NextMpegStartCode(b)) // continue; {ASSERT(0); break;} rtpos = GetPos()-4; #if (EVO_SUPPORT == 0) if(b >= 0xbd && b < 0xf0) #else if((b >= 0xbd && b < 0xf0) || (b == 0xfd)) #endif { peshdr h; if(!Read(h, b) || !h.len) continue; __int64 pos = GetPos(); if(h.fpts && AddStream(0, b, h.len) == TrackNum) { ASSERT(h.pts >= m_rtMin && h.pts <= m_rtMax); rt = h.pts; break; } Seek(pos + h.len); } } else if(m_type == ts) { trhdr h; if(!Read(h)) continue; rtpos = GetPos()-4; if(h.payload && h.payloadstart && ISVALIDPID(h.pid)) { peshdr h2; if(NextMpegStartCode(b, 4) && Read(h2, b)) // pes packet { if(h2.fpts && AddStream(h.pid, b, DWORD(h.bytes - (GetPos() - rtpos)) == TrackNum)) { ASSERT(h2.pts >= m_rtMin && h2.pts <= m_rtMax); rt = h2.pts; break; } } } Seek(h.next); } else if(m_type == pva) { pvahdr h; if(!Read(h)) continue; if(h.fpts) { rt = h.pts; break; } } } if(rtpos >= 0) Seek(rtpos); if(rt >= 0) rt -= m_rtMin; return rt; }
void AddUsed(SQLLEN cbRead) { I(cbRead <= GetRemaining()); bytesUsed += (int)cbRead; }
HRESULT CNutFile::Init() { Seek(0); if(BitRead(64) != NUTM) return E_FAIL; m_streams.RemoveAll(); Seek(0); while(GetRemaining()) { frame_header fh; fh.checksum_flag = 1; UINT64 id = 0; if(BitRead(1, true) == 0 || (id = BitRead(64)) == NUTK) break; packet_header ph; Read(ph); if(id == NUTM) { Read(m_mh); } else if(id == NUTS) { CAutoPtr<stream_header> sh(new stream_header()); Read(*sh); if(sh->stream_class == SC_VIDEO) Read(sh->vsh); else if(sh->stream_class == SC_AUDIO) Read(sh->ash); // else if(sh->stream_class == SC_SUBTITLE) ; // nothing to do m_streams.AddTail(sh); } else if(id == NUTX) { index_header ih; Read(ih); } else if(id == NUTI) { info_header ih; Read(ih); } else if(id == 0) // frame { ASSERT(0); break; } if(fh.checksum_flag) { Seek(ph.pos + ph.fptr - 4); ph.checksum = (UINT32)BitRead(32); } Seek(ph.pos + ph.fptr); } Seek(0); return m_streams.GetCount() ? S_OK : E_FAIL; }