bool cParsedNBT::ReadCompound(void) { // Reads the latest tag as a compound int ParentIdx = m_Tags.size() - 1; int PrevSibling = -1; while (true) { NEEDBYTES(1); eTagType TagType = (eTagType)(m_Data[m_Pos]); m_Pos++; if (TagType == TAG_End) { break; } m_Tags.push_back(cFastNBTTag(TagType, ParentIdx, PrevSibling)); if (PrevSibling >= 0) { m_Tags[PrevSibling].m_NextSibling = m_Tags.size() - 1; } else { m_Tags[ParentIdx].m_FirstChild = m_Tags.size() - 1; } PrevSibling = m_Tags.size() - 1; RETURN_FALSE_IF_FALSE(ReadString(m_Tags.back().m_NameStart, m_Tags.back().m_NameLength)); RETURN_FALSE_IF_FALSE(ReadTag()); } // while (true) m_Tags[ParentIdx].m_LastChild = PrevSibling; return true; }
bool cByteBuffer::ReadBuf(void * a_Buffer, int a_Count) { CHECK_THREAD; CheckValid(); ASSERT(a_Count >= 0); NEEDBYTES(a_Count); char * Dst = (char *)a_Buffer; // So that we can do byte math int BytesToEndOfBuffer = m_BufferSize - m_ReadPos; ASSERT(BytesToEndOfBuffer >= 0); // Sanity check if (BytesToEndOfBuffer <= a_Count) { // Reading across the ringbuffer end, read the first part and adjust parameters: if (BytesToEndOfBuffer > 0) { memcpy(Dst, m_Buffer + m_ReadPos, BytesToEndOfBuffer); Dst += BytesToEndOfBuffer; a_Count -= BytesToEndOfBuffer; } m_ReadPos = 0; } // Read the rest of the bytes in a single read (guaranteed to fit): if (a_Count > 0) { memcpy(Dst, m_Buffer + m_ReadPos, a_Count); m_ReadPos += a_Count; } return true; }
bool cParsedNBT::ReadList(eTagType a_ChildrenType) { // Reads the latest tag as a list of items of type a_ChildrenType // Read the count: NEEDBYTES(4); int Count = ntohl(*((int *)(m_Data + m_Pos))); m_Pos += 4; if (Count < 0) { return false; } // Read items: int ParentIdx = m_Tags.size() - 1; int PrevSibling = -1; for (int i = 0; i < Count; i++) { m_Tags.push_back(cFastNBTTag(a_ChildrenType, ParentIdx, PrevSibling)); if (PrevSibling >= 0) { m_Tags[PrevSibling].m_NextSibling = m_Tags.size() - 1; } else { m_Tags[ParentIdx].m_FirstChild = m_Tags.size() - 1; } PrevSibling = m_Tags.size() - 1; RETURN_FALSE_IF_FALSE(ReadTag()); } // for (i) m_Tags[ParentIdx].m_LastChild = PrevSibling; return true; }
bool cByteBuffer::ReadString(AString & a_String, int a_Count) { CHECK_THREAD; CheckValid(); ASSERT(a_Count >= 0); NEEDBYTES(a_Count); a_String.clear(); a_String.reserve(a_Count); int BytesToEndOfBuffer = m_BufferSize - m_ReadPos; ASSERT(BytesToEndOfBuffer >= 0); // Sanity check if (BytesToEndOfBuffer <= a_Count) { // Reading across the ringbuffer end, read the first part and adjust parameters: if (BytesToEndOfBuffer > 0) { a_String.assign(m_Buffer + m_ReadPos, BytesToEndOfBuffer); a_Count -= BytesToEndOfBuffer; } m_ReadPos = 0; } // Read the rest of the bytes in a single read (guaranteed to fit): if (a_Count > 0) { a_String.append(m_Buffer + m_ReadPos, a_Count); m_ReadPos += a_Count; } return true; }
bool cByteBuffer::ReadByte(unsigned char & a_Value) { CHECK_THREAD; CheckValid(); NEEDBYTES(1); ReadBuf(&a_Value, 1); return true; }
bool cByteBuffer::ReadBEDouble(double & a_Value) { CHECK_THREAD; CheckValid(); NEEDBYTES(8); ReadBuf(&a_Value, 8); a_Value = NetworkToHostDouble8(&a_Value); return true; }
bool cByteBuffer::ReadBEFloat(float & a_Value) { CHECK_THREAD; CheckValid(); NEEDBYTES(4); ReadBuf(&a_Value, 4); a_Value = NetworkToHostFloat4(&a_Value); return true; }
bool cByteBuffer::ReadBEInt64(Int64 & a_Value) { CHECK_THREAD; CheckValid(); NEEDBYTES(8); ReadBuf(&a_Value, 8); a_Value = NetworkToHostLong8(&a_Value); return true; }
bool cByteBuffer::ReadBEInt(int & a_Value) { CHECK_THREAD; CheckValid(); NEEDBYTES(4); ReadBuf(&a_Value, 4); a_Value = ntohl(a_Value); return true; }
bool cByteBuffer::ReadBEShort(short & a_Value) { CHECK_THREAD; CheckValid(); NEEDBYTES(2); ReadBuf(&a_Value, 2); a_Value = ntohs(a_Value); return true; }
bool cByteBuffer::ReadBool(bool & a_Value) { CHECK_THREAD; CheckValid(); NEEDBYTES(1); char Value = 0; ReadBuf(&Value, 1); a_Value = (Value != 0); return true; }
bool cParsedNBT::ReadString(int & a_StringStart, int & a_StringLen) { NEEDBYTES(2); a_StringStart = m_Pos + 2; a_StringLen = ntohs(*((short *)(m_Data + m_Pos))); if (a_StringLen < 0) { // Invalid string length return false; } m_Pos += 2 + a_StringLen; return true; }
bool cParsedNBT::ReadTag(void) { cFastNBTTag & Tag = m_Tags.back(); switch (Tag.m_Type) { CASE_SIMPLE_TAG(Byte, 1) CASE_SIMPLE_TAG(Short, 2) CASE_SIMPLE_TAG(Int, 4) CASE_SIMPLE_TAG(Long, 8) CASE_SIMPLE_TAG(Float, 4) CASE_SIMPLE_TAG(Double, 8) case TAG_String: { return ReadString(Tag.m_DataStart, Tag.m_DataLength); } case TAG_ByteArray: { NEEDBYTES(4); int len = ntohl(*((int *)(m_Data + m_Pos))); m_Pos += 4; if (len < 0) { // Invalid length return false; } NEEDBYTES(len); Tag.m_DataLength = len; Tag.m_DataStart = m_Pos; m_Pos += len; return true; } case TAG_List: { NEEDBYTES(1); eTagType ItemType = (eTagType)m_Data[m_Pos]; m_Pos++; RETURN_FALSE_IF_FALSE(ReadList(ItemType)); return true; } case TAG_Compound: { RETURN_FALSE_IF_FALSE(ReadCompound()); return true; } case TAG_IntArray: { NEEDBYTES(4); int len = ntohl(*((int *)(m_Data + m_Pos))); m_Pos += 4; if (len < 0) { // Invalid length return false; } len *= 4; NEEDBYTES(len); Tag.m_DataLength = len; Tag.m_DataStart = m_Pos; m_Pos += len; return true; } default: { ASSERT(!"Unhandled NBT tag type"); return false; } } // switch (iType) }
static int demux (uint8_t * buf, uint8_t * end, int flags) { static int mpeg1_skip_table[16] = { 0, 0, 4, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; /* * the demuxer keeps some state between calls: * if "state" = DEMUX_HEADER, then "head_buf" contains the first * "bytes" bytes from some header. * if "state" == DEMUX_DATA, then we need to copy "bytes" bytes * of ES data before the next header. * if "state" == DEMUX_SKIP, then we need to skip "bytes" bytes * of data before the next header. * * NEEDBYTES makes sure we have the requested number of bytes for a * header. If we dont, it copies what we have into head_buf and returns, * so that when we come back with more data we finish decoding this header. * * DONEBYTES updates "buf" to point after the header we just parsed. */ #define DEMUX_HEADER 0 #define DEMUX_DATA 1 #define DEMUX_SKIP 2 static int state = DEMUX_SKIP; static int state_bytes = 0; static uint8_t head_buf[268]; uint8_t * header; int bytes; int len; #define NEEDBYTES(x) \ do { \ int missing; \ \ missing = (x) - bytes; \ if (missing > 0) { \ if (header == head_buf) { \ if (missing <= end - buf) { \ memcpy (header + bytes, buf, missing); \ buf += missing; \ bytes = (x); \ } else { \ memcpy (header + bytes, buf, end - buf); \ state_bytes = bytes + end - buf; \ return 0; \ } \ } else { \ memcpy (head_buf, header, bytes); \ state = DEMUX_HEADER; \ state_bytes = bytes; \ return 0; \ } \ } \ } while (0) #define DONEBYTES(x) \ do { \ if (header != head_buf) \ buf = header + (x); \ } while (0) if (flags & DEMUX_PAYLOAD_START) goto payload_start; switch (state) { case DEMUX_HEADER: if (state_bytes > 0) { header = head_buf; bytes = state_bytes; goto continue_header; } break; case DEMUX_DATA: if (demux_pid || (state_bytes > end - buf)) { fwrite (buf, end - buf, 1, stdout); state_bytes -= end - buf; return 0; } fwrite (buf, state_bytes, 1, stdout); buf += state_bytes; break; case DEMUX_SKIP: if (demux_pid || (state_bytes > end - buf)) { state_bytes -= end - buf; return 0; } buf += state_bytes; break; } while (1) { if (demux_pid) { state = DEMUX_SKIP; return 0; } payload_start: header = buf; bytes = end - buf; continue_header: NEEDBYTES (4); if (header[0] || header[1] || (header[2] != 1)) { if (demux_pid) { state = DEMUX_SKIP; return 0; } else if (header != head_buf) { buf++; goto payload_start; } else { header[0] = header[1]; header[1] = header[2]; header[2] = header[3]; bytes = 3; goto continue_header; } } if (demux_pid || demux_pes) { if (header[3] != 0xbd) { fprintf (stderr, "bad stream id %x\n", header[3]); exit (1); } NEEDBYTES (9); if ((header[6] & 0xc0) != 0x80) { /* not mpeg2 */ fprintf (stderr, "bad multiplex - not mpeg2\n"); exit (1); } len = 9 + header[8]; NEEDBYTES (len); DONEBYTES (len); bytes = 6 + (header[4] << 8) + header[5] - len; if (bytes > end - buf) { fwrite (buf, end - buf, 1, stdout); state = DEMUX_DATA; state_bytes = bytes - (end - buf); return 0; } else if (bytes > 0) { fwrite (buf, bytes, 1, stdout); buf += bytes; } } else switch (header[3]) { case 0xb9: /* program end code */ /* DONEBYTES (4); */ /* break; */ return 1; case 0xba: /* pack header */ NEEDBYTES (5); if ((header[4] & 0xc0) == 0x40) { /* mpeg2 */ NEEDBYTES (14); len = 14 + (header[13] & 7); NEEDBYTES (len); DONEBYTES (len); /* header points to the mpeg2 pack header */ } else if ((header[4] & 0xf0) == 0x20) { /* mpeg1 */ NEEDBYTES (12); DONEBYTES (12); /* header points to the mpeg1 pack header */ } else { fprintf (stderr, "weird pack header\n"); DONEBYTES (5); } break; case 0xbd: /* private stream 1 */ NEEDBYTES (7); if ((header[6] & 0xc0) == 0x80) { /* mpeg2 */ NEEDBYTES (9); len = 10 + header[8]; NEEDBYTES (len); /* header points to the mpeg2 pes header */ } else { /* mpeg1 */ len = 7; while ((header-1)[len] == 0xff) { len++; NEEDBYTES (len); if (len == 23) { fprintf (stderr, "too much stuffing\n"); break; } } if (((header-1)[len] & 0xc0) == 0x40) { len += 2; NEEDBYTES (len); } len += mpeg1_skip_table[(header - 1)[len] >> 4] + 1; NEEDBYTES (len); /* header points to the mpeg1 pes header */ } if ((header-1)[len] != demux_track) { DONEBYTES (len); bytes = 6 + (header[4] << 8) + header[5] - len; if (bytes <= 0) continue; goto skip; } len += 3; NEEDBYTES (len); DONEBYTES (len); bytes = 6 + (header[4] << 8) + header[5] - len; if (bytes > end - buf) { fwrite (buf, end - buf, 1, stdout); state = DEMUX_DATA; state_bytes = bytes - (end - buf); return 0; } else if (bytes > 0) { fwrite (buf, bytes, 1, stdout); buf += bytes; } break; default: if (header[3] < 0xb9) { fprintf (stderr, "looks like a video stream, not system stream\n"); exit (1); } else { NEEDBYTES (6); DONEBYTES (6); bytes = (header[4] << 8) + header[5]; skip: if (bytes > end - buf) { state = DEMUX_SKIP; state_bytes = bytes - (end - buf); return 0; } buf += bytes; } } } }