void NifStream( unsigned int const & val, ostream& out, const NifInfo & info ) { if ( info.endian == sys_endian ) { WriteUInt( val, out ); } else { WriteUInt( SwapEndian(val), out ); } }
void LiteralSInt(signed long long value) { if (value < 0) { WriteByte(Trace::TYPE_SINT); WriteUInt(-value); } else { WriteByte(Trace::TYPE_UINT); WriteUInt(value); } }
// Writes a WebM BlockGroup with DiscardPadding. The structure is as follows: // Indentation shows sub-levels // BlockGroup // Block // Data // DiscardPadding uint64 WriteBlockWithDiscardPadding(IMkvWriter* writer, const uint8* data, uint64 length, int64 discard_padding, uint64 track_number, int64 timecode, uint64 is_key) { if (!data || length < 1 || discard_padding <= 0) return 0; const uint64 block_payload_size = 4 + length; const uint64 block_elem_size = EbmlMasterElementSize(kMkvBlock, block_payload_size) + block_payload_size; const uint64 discard_padding_elem_size = EbmlElementSize(kMkvDiscardPadding, discard_padding); const uint64 block_group_payload_size = block_elem_size + discard_padding_elem_size; const uint64 block_group_elem_size = EbmlMasterElementSize( kMkvBlockGroup, block_group_payload_size) + block_group_payload_size; if (!WriteEbmlMasterElement(writer, kMkvBlockGroup, block_group_payload_size)) return 0; if (!WriteEbmlMasterElement(writer, kMkvBlock, block_payload_size)) return 0; if (WriteUInt(writer, track_number)) return 0; if (SerializeInt(writer, timecode, 2)) return 0; uint64 flags = 0; if (is_key) flags |= 0x80; if (SerializeInt(writer, flags, 1)) return 0; if (writer->Write(data, static_cast<uint32>(length))) return 0; if (WriteID(writer, kMkvDiscardPadding)) return 0; const uint64 size = GetUIntSize(discard_padding); if (WriteUInt(writer, size)) return false; if (SerializeInt(writer, discard_padding, static_cast<int32>(size))) return false; return block_group_elem_size; }
uint64 WriteSimpleBlock(IMkvWriter* writer, const uint8* data, uint64 length, uint64 track_number, int64 timecode, uint64 is_key) { if (!writer) return false; if (!data || length < 1) return false; // Here we only permit track number values to be no greater than // 126, which the largest value we can store having a Matroska // integer representation of only 1 byte. if (track_number < 1 || track_number > 126) return false; // Technically the timestamp for a block can be less than the // timestamp for the cluster itself (remember that block timestamp // is a signed, 16-bit integer). However, as a simplification we // only permit non-negative cluster-relative timestamps for blocks. if (timecode < 0 || timecode > kMaxBlockTimecode) return false; if (WriteID(writer, kMkvSimpleBlock)) return 0; const int32 size = static_cast<int32>(length) + 4; if (WriteUInt(writer, size)) return 0; if (WriteUInt(writer, static_cast<uint64>(track_number))) return 0; if (SerializeInt(writer, timecode, 2)) return 0; uint64 flags = 0; if (is_key) flags |= 0x80; if (SerializeInt(writer, flags, 1)) return 0; if (writer->Write(data, static_cast<uint32>(length))) return 0; const uint64 element_size = GetUIntSize(kMkvSimpleBlock) + GetCodedUIntSize(size) + 4 + length; return element_size; }
void BeginStruct(const StructSig *sig) { WriteByte(Trace::TYPE_STRUCT); WriteUInt(sig->id); if (!lookup(structs, sig->id)) { WriteString(sig->name); WriteUInt(sig->num_members); for (unsigned i = 0; i < sig->num_members; ++i) { WriteString(sig->members[i]); } structs[sig->id] = true; } }
unsigned BeginEnter(const FunctionSig &function) { OS::AcquireMutex(); Open(); WriteByte(Trace::EVENT_ENTER); WriteUInt(function.id); if (!lookup(functions, function.id)) { WriteString(function.name); WriteUInt(function.num_args); for (unsigned i = 0; i < function.num_args; ++i) { WriteString(function.args[i]); } functions[function.id] = true; } return call_no++; }
void WriteBool( bool val, ostream& out, unsigned int version ) { if ( version < 0x04010001 ) { //Bools are stored as integers before version 4.1.0.1 if (val) WriteUInt( 1, out ); else WriteUInt( 0, out ); } else { //And as bytes from 4.1.0.1 on if (val) WriteByte( 1, out ); else WriteByte( 0, out ); } }
void LiteralBitmask(const BitmaskSig &bitmask, unsigned long long value) { WriteByte(Trace::TYPE_BITMASK); WriteUInt(bitmask.id); if (!lookup(bitmasks, bitmask.id)) { WriteUInt(bitmask.count); for (unsigned i = 0; i < bitmask.count; ++i) { if (i != 0 && bitmask.values[i].value == 0) { OS::DebugMessage("apitrace: bitmask %s is zero but is not first flag\n", bitmask.values[i].name); } WriteString(bitmask.values[i].name); WriteUInt(bitmask.values[i].value); } bitmasks[bitmask.id] = true; } WriteUInt(value); }
uint64 WriteVoidElement(IMkvWriter* writer, uint64 size) { if (!writer) return false; // Subtract one for the void ID and the coded size. uint64 void_entry_size = size - 1 - GetCodedUIntSize(size-1); uint64 void_size = EbmlMasterElementSize(kMkvVoid, void_entry_size) + void_entry_size; if (void_size != size) return 0; const int64 payload_position = writer->Position(); if (payload_position < 0) return 0; if (WriteID(writer, kMkvVoid)) return 0; if (WriteUInt(writer, void_entry_size)) return 0; const uint8 value = 0; for (int32 i = 0; i < static_cast<int32>(void_entry_size); ++i) { if (writer->Write(&value, 1)) return 0; } const int64 stop_position = writer->Position(); if (stop_position < 0 || stop_position - payload_position != static_cast<int64>(void_size)) return 0; return void_size; }
void LiteralOpaque(const void *addr) { if (!addr) { LiteralNull(); return; } WriteByte(Trace::TYPE_OPAQUE); WriteUInt((size_t)addr); }
void LiteralEnum(const EnumSig *sig) { WriteByte(Trace::TYPE_ENUM); WriteUInt(sig->id); if (!lookup(enums, sig->id)) { WriteString(sig->name); LiteralSInt(sig->value); enums[sig->id] = true; } }
void LiteralString(const char *str, size_t len) { if (!str) { LiteralNull(); return; } WriteByte(Trace::TYPE_STRING); WriteUInt(len); Write(str, len); }
void WritePtr32( void * val, ostream& out ){ #if __SIZEOF_POINTER__ == 4 // 32 bit WriteUInt( (unsigned int)val, out ); #else // 64 bit union intpoint_t { void *ptr; struct { unsigned int id1; unsigned int id2; }; } ptr; ptr.ptr = val; // xor the two parts // (maybe a more advanced hash function would be better, experience will tell) WriteUInt(ptr.id1 ^ ptr.id2, out); #endif }
void LiteralBlob(const void *data, size_t size) { if (!data) { LiteralNull(); return; } WriteByte(Trace::TYPE_BLOB); WriteUInt(size); if (size) { Write(data, size); } }
bool WriteEbmlMasterElement(IMkvWriter* writer, uint64 type, uint64 size) { if (!writer) return false; if (WriteID(writer, type)) return false; if (WriteUInt(writer, size)) return false; return true; }
bool WriteEbmlElement(IMkvWriter* writer, uint64 type, float value) { if (!writer) return false; if (WriteID(writer, type)) return false; if (WriteUInt(writer, 4)) return false; if (SerializeFloat(writer, value)) return false; return true; }
void cProtocol125::SendBlockChanges(int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes) { cCSLock Lock(m_CSPacket); if (a_Changes.size() == 1) { // Special packet for single-block changes const sSetBlock & blk = a_Changes.front(); SendBlockChange(a_ChunkX * cChunkDef::Width + blk.x, blk.y, a_ChunkZ * cChunkDef::Width + blk.z, blk.BlockType, blk.BlockMeta); return; } WriteByte (PACKET_MULTI_BLOCK); WriteInt (a_ChunkX); WriteInt (a_ChunkZ); WriteShort((short)a_Changes.size()); WriteUInt ((UInt32)(4 * a_Changes.size())); for (sSetBlockVector::const_iterator itr = a_Changes.begin(), end = a_Changes.end(); itr != end; ++itr) { UInt32 Coords = ((UInt32)itr->y) | ((UInt32)(itr->z << 8)) | ((UInt32)(itr->x << 12)); UInt32 Blocks = ((UInt32)itr->BlockMeta) | ((UInt32)(itr->BlockType << 4)); WriteUInt(Coords << 16 | Blocks); } Flush(); }
bool WriteEbmlDateElement(IMkvWriter* writer, uint64 type, int64 value) { if (!writer) return false; if (WriteID(writer, type)) return false; if (WriteUInt(writer, kDateElementSize)) return false; if (SerializeInt(writer, value, kDateElementSize)) return false; return true; }
bool WriteEbmlElement(IMkvWriter* writer, uint64 type, uint64 value) { if (!writer) return false; if (WriteID(writer, type)) return false; const uint64 size = GetUIntSize(value); if (WriteUInt(writer, size)) return false; if (SerializeInt(writer, value, static_cast<int32>(size))) return false; return true; }
bool WriteEbmlElement(IMkvWriter* writer, uint64 type, const char* value) { if (!writer || !value) return false; if (WriteID(writer, type)) return false; const int32 length = strlen(value); if (WriteUInt(writer, length)) return false; if (writer->Write(value, length)) return false; return true; }
bool WriteEbmlElement(IMkvWriter* writer, uint64 type, const uint8* value, uint64 size) { if (!writer || !value || size < 1) return false; if (WriteID(writer, type)) return false; if (WriteUInt(writer, size)) return false; if (writer->Write(value, static_cast<uint32>(size))) return false; return true; }
void debug_stream_test_cb (puObject* obj) { SStream* s = new SStream; strncpy (s->filename, "teststream.txt",(PATH_MAX-1)); strncpy (s->mode, "w",3); OpenStream (s); WriteComment ("Comment...testing testing testing", s); WriteComment ("", s); WriteTag ('bgno', "---- object ----", s); WriteTag ('bgno', "---- nested Object ----", s); WriteTag ('int_', "---- int ----", s); int i = 500; WriteInt (&i, s); WriteTag ('uint', "---- unsigned int ----", s); unsigned int ui = 12345678; WriteUInt (&ui, s); float f = 12345.67f; WriteTag ('flot', "---- float ----", s); WriteFloat (&f, s); double d = 987654.3210; WriteTag ('dubl', "---- double ----", s); WriteDouble (&d, s); WriteTag ('stng', "---- string ----", s); WriteString ("This a string", s); SVector v; v.x = 1.0; v.y = 2.0; v.z = 3.0; WriteTag ('vect', "--- vector ----", s); WriteVector (&v, s); SPosition pos; pos.lat = 1000.0; pos.lon = 2000.0; pos.alt = 3000.0; WriteTag ('posn', "---- position ----", s); WritePosition (&pos, s); SMessage mesg; WriteTag ('mesg', "---- message ----", s); WriteMessage (&mesg, s); WriteTag ('endo', s); WriteTag ('endo', s); CloseStream (s); delete s; }
void BeginArray(size_t length) { WriteByte(Trace::TYPE_ARRAY); WriteUInt(length); }
// Writes a WebM BlockGroup with BlockAdditional data. The structure is as // follows: // Indentation shows sub-levels // BlockGroup // Block // Data // BlockAdditions // BlockMore // BlockAddID // 1 (Denotes Alpha) // BlockAdditional // Data uint64 WriteBlockWithAdditional(IMkvWriter* writer, const uint8* data, uint64 length, const uint8* additional, uint64 additional_length, uint64 add_id, uint64 track_number, int64 timecode, uint64 is_key) { if (!data || !additional || length < 1 || additional_length < 1) return 0; const uint64 block_payload_size = 4 + length; const uint64 block_elem_size = EbmlMasterElementSize(kMkvBlock, block_payload_size) + block_payload_size; const uint64 block_additional_elem_size = EbmlElementSize(kMkvBlockAdditional, additional, additional_length); const uint64 block_addid_elem_size = EbmlElementSize(kMkvBlockAddID, add_id); const uint64 block_more_payload_size = block_addid_elem_size + block_additional_elem_size; const uint64 block_more_elem_size = EbmlMasterElementSize( kMkvBlockMore, block_more_payload_size) + block_more_payload_size; const uint64 block_additions_payload_size = block_more_elem_size; const uint64 block_additions_elem_size = EbmlMasterElementSize( kMkvBlockAdditions, block_additions_payload_size) + block_additions_payload_size; const uint64 block_group_payload_size = block_elem_size + block_additions_elem_size; const uint64 block_group_elem_size = EbmlMasterElementSize( kMkvBlockGroup, block_group_payload_size) + block_group_payload_size; if (!WriteEbmlMasterElement(writer, kMkvBlockGroup, block_group_payload_size)) return 0; if (!WriteEbmlMasterElement(writer, kMkvBlock, block_payload_size)) return 0; if (WriteUInt(writer, track_number)) return 0; if (SerializeInt(writer, timecode, 2)) return 0; uint64 flags = 0; if (is_key) flags |= 0x80; if (SerializeInt(writer, flags, 1)) return 0; if (writer->Write(data, static_cast<uint32>(length))) return 0; if (!WriteEbmlMasterElement(writer, kMkvBlockAdditions, block_additions_payload_size)) return 0; if (!WriteEbmlMasterElement(writer, kMkvBlockMore, block_more_payload_size)) return 0; if (!WriteEbmlElement(writer, kMkvBlockAddID, add_id)) return 0; if (!WriteEbmlElement(writer, kMkvBlockAdditional, additional, additional_length)) return 0; return block_group_elem_size; }
void BeginArg(unsigned index) { WriteByte(Trace::CALL_ARG); WriteUInt(index); }
// We must write the metadata (key)frame as a BlockGroup element, // because we need to specify a duration for the frame. The // BlockGroup element comprises the frame itself and its duration, // and is laid out as follows: // // BlockGroup tag // BlockGroup size // Block tag // Block size // (the frame is the block payload) // Duration tag // Duration size // (duration payload) // uint64 WriteMetadataBlock(IMkvWriter* writer, const uint8* data, uint64 length, uint64 track_number, int64 timecode, uint64 duration) { // We don't backtrack when writing to the stream, so we must // pre-compute the BlockGroup size, by summing the sizes of each // sub-element (the block and the duration). // We use a single byte for the track number of the block, which // means the block header is exactly 4 bytes. // TODO(matthewjheaney): use EbmlMasterElementSize and WriteEbmlMasterElement const uint64 block_payload_size = 4 + length; const int32 block_size = GetCodedUIntSize(block_payload_size); const uint64 block_elem_size = 1 + block_size + block_payload_size; const int32 duration_payload_size = GetUIntSize(duration); const int32 duration_size = GetCodedUIntSize(duration_payload_size); const uint64 duration_elem_size = 1 + duration_size + duration_payload_size; const uint64 blockg_payload_size = block_elem_size + duration_elem_size; const int32 blockg_size = GetCodedUIntSize(blockg_payload_size); const uint64 blockg_elem_size = 1 + blockg_size + blockg_payload_size; if (WriteID(writer, kMkvBlockGroup)) // 1-byte ID size return 0; if (WriteUInt(writer, blockg_payload_size)) return 0; // Write Block element if (WriteID(writer, kMkvBlock)) // 1-byte ID size return 0; if (WriteUInt(writer, block_payload_size)) return 0; // Byte 1 of 4 if (WriteUInt(writer, track_number)) return 0; // Bytes 2 & 3 of 4 if (SerializeInt(writer, timecode, 2)) return 0; // Byte 4 of 4 const uint64 flags = 0; if (SerializeInt(writer, flags, 1)) return 0; // Now write the actual frame (of metadata) if (writer->Write(data, static_cast<uint32>(length))) return 0; // Write Duration element if (WriteID(writer, kMkvBlockDuration)) // 1-byte ID size return 0; if (WriteUInt(writer, duration_payload_size)) return 0; if (SerializeInt(writer, duration, duration_payload_size)) return 0; // Note that we don't write a reference time as part of the block // group; no reference time(s) indicates that this block is a // keyframe. (Unlike the case for a SimpleBlock element, the header // bits of the Block sub-element of a BlockGroup element do not // indicate keyframe status. The keyframe status is inferred from // the absence of reference time sub-elements.) return blockg_elem_size; }
void BeginLeave(unsigned call) { OS::AcquireMutex(); WriteByte(Trace::EVENT_LEAVE); WriteUInt(call); }
void WriteString( string const & val, ostream& out ) { WriteUInt( (unsigned int)(val.size()), out ); out.write( val.c_str(), std::streamsize(val.size()) ); }
void Open(void) { if (!g_gzFile) { _Open("trace"); WriteUInt(TRACE_VERSION); } }
void LiteralUInt(unsigned long long value) { WriteByte(Trace::TYPE_UINT); WriteUInt(value); }