X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2FKLV.cpp;h=29a1c81639db14d3a600a98ed88db586203f4093;hb=830570c46c4d39a8a5767f83875e3ef2f79ecc98;hp=b31969e3467a92b7ce707219a30d4f502d7034b4;hpb=30d642bd3b8474744dfbdcc2bdc46cdf827102c4;p=asdcplib.git diff --git a/src/KLV.cpp b/src/KLV.cpp index b31969e..29a1c81 100755 --- a/src/KLV.cpp +++ b/src/KLV.cpp @@ -30,7 +30,8 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "KLV.h" -#include +#include +using Kumu::DefaultLogSink; // This is how much we read when we're reading from a file and we don't know @@ -69,12 +70,27 @@ ASDCP::KLVPacket::InitFromBuffer(const byte_t* buf, ui32_t buf_len) return RESULT_FAIL; } + ui32_t ber_len = Kumu::BER_length(buf + SMPTE_UL_LENGTH); + + if ( ber_len > ( buf_len - SMPTE_UL_LENGTH ) ) + { + DefaultLogSink().Error("BER encoding length exceeds buffer size\n"); + return RESULT_FAIL; + } + + if ( ber_len == 0 ) + { + DefaultLogSink().Error("KLV format error, zero BER length not allowed\n"); + return RESULT_FAIL; + } + ui64_t tmp_size; - if ( ! read_BER(buf + SMPTE_UL_LENGTH, &tmp_size) ) + if ( ! Kumu::read_BER(buf + SMPTE_UL_LENGTH, &tmp_size) ) return RESULT_FAIL; - m_ValueLength = tmp_size; - m_KLLength = SMPTE_UL_LENGTH + BER_length(buf + SMPTE_UL_LENGTH); + assert (tmp_size <= 0xFFFFFFFFL); + m_ValueLength = (ui32_t) tmp_size; + m_KLLength = SMPTE_UL_LENGTH + Kumu::BER_length(buf + SMPTE_UL_LENGTH); m_KeyStart = buf; m_ValueStart = buf + m_KLLength; return RESULT_OK; @@ -102,7 +118,7 @@ ASDCP::KLVPacket::WriteKLToBuffer(ASDCP::FrameBuffer& Buffer, const byte_t* labe memcpy(Buffer.Data() + Buffer.Size(), label, SMPTE_UL_LENGTH); - if ( ! write_BER(Buffer.Data() + Buffer.Size() + SMPTE_UL_LENGTH, length, MXF_BER_LENGTH) ) + if ( ! Kumu::write_BER(Buffer.Data() + Buffer.Size() + SMPTE_UL_LENGTH, length, MXF_BER_LENGTH) ) return RESULT_FAIL; Buffer.Size(Buffer.Size() + kl_length); @@ -119,15 +135,15 @@ ASDCP::KLVPacket::Dump(FILE* stream, bool show_hex) if ( m_KeyStart != 0 ) { assert(m_ValueStart); - - for ( ui32_t i = 0; i < SMPTE_UL_LENGTH; i++ ) - fprintf(stream, "%02x.", m_KeyStart[i]); + UL TmpUL(m_KeyStart); + char buf[64]; + fprintf(stream, "%s", TmpUL.EncodeString(buf, 64)); const MDDEntry* Entry = Dict::FindUL(m_KeyStart); - fprintf(stream, "\b len: %7lu (%s)\n", m_ValueLength, (Entry ? Entry->name : "Unknown")); + fprintf(stream, " len: %7u (%s)\n", m_ValueLength, (Entry ? Entry->name : "Unknown")); if ( show_hex && m_ValueLength < 1000 ) - hexdump(m_ValueStart, ASDCP::xmin(m_ValueLength, (ui32_t)64), stream); + Kumu::hexdump(m_ValueStart, Kumu::xmin(m_ValueLength, (ui32_t)64), stream); } else { @@ -137,7 +153,7 @@ ASDCP::KLVPacket::Dump(FILE* stream, bool show_hex) // ASDCP::Result_t -ASDCP::KLVFilePacket::InitFromFile(const FileReader& Reader, const byte_t* label) +ASDCP::KLVFilePacket::InitFromFile(const Kumu::FileReader& Reader, const byte_t* label) { Result_t result = KLVFilePacket::InitFromFile(Reader); @@ -148,9 +164,9 @@ ASDCP::KLVFilePacket::InitFromFile(const FileReader& Reader, const byte_t* label return result; } -// +// TODO: refactor to use InitFromBuffer ASDCP::Result_t -ASDCP::KLVFilePacket::InitFromFile(const FileReader& Reader) +ASDCP::KLVFilePacket::InitFromFile(const Kumu::FileReader& Reader) { ui32_t read_count; byte_t tmp_data[tmp_read_size]; @@ -166,7 +182,7 @@ ASDCP::KLVFilePacket::InitFromFile(const FileReader& Reader) if ( read_count < (SMPTE_UL_LENGTH + 1) ) { - DefaultLogSink().Error("Short read of Key and Length got %lu\n", read_count); + DefaultLogSink().Error("Short read of Key and Length got %u\n", read_count); return RESULT_READFAIL; } @@ -177,7 +193,7 @@ ASDCP::KLVFilePacket::InitFromFile(const FileReader& Reader) return RESULT_FAIL; } - if ( ! read_BER(tmp_data + SMPTE_UL_LENGTH, &tmp_size) ) + if ( ! Kumu::read_BER(tmp_data + SMPTE_UL_LENGTH, &tmp_size) ) { DefaultLogSink().Error("BER Length decoding error\n"); return RESULT_FAIL; @@ -185,16 +201,16 @@ ASDCP::KLVFilePacket::InitFromFile(const FileReader& Reader) if ( tmp_size > MAX_KLV_PACKET_LENGTH ) { - char intbuf[IntBufferLen]; - DefaultLogSink().Error("Packet length %s exceeds internal limit\n", - ui64sz(tmp_size, intbuf)); + Kumu::ui64Printer tmp_size_str(tmp_size); + DefaultLogSink().Error("Packet length %s exceeds internal limit\n", tmp_size_str.c_str()); return RESULT_FAIL; } ui32_t remainder = 0; - ui32_t ber_len = BER_length(tmp_data + SMPTE_UL_LENGTH); + ui32_t ber_len = Kumu::BER_length(tmp_data + SMPTE_UL_LENGTH); m_KLLength = SMPTE_UL_LENGTH + ber_len; - m_ValueLength = tmp_size; + assert(tmp_size <= 0xFFFFFFFFL); + m_ValueLength = (ui32_t) tmp_size; ui32_t packet_length = m_ValueLength + m_KLLength; result = m_Buffer.Capacity(packet_length); @@ -215,7 +231,7 @@ ASDCP::KLVFilePacket::InitFromFile(const FileReader& Reader) if ( (remainder = read_count - packet_length) != 0 ) { DefaultLogSink().Warn("Repositioning pointer for short packet\n"); - ASDCP::fpos_t pos = Reader.Tell(); + Kumu::fpos_t pos = Reader.Tell(); assert(pos > remainder); result = Reader.Seek(pos - remainder); } @@ -224,7 +240,7 @@ ASDCP::KLVFilePacket::InitFromFile(const FileReader& Reader) { if ( read_count < tmp_read_size ) { - DefaultLogSink().Error("Short read of packet body, expecting %lu, got %lu\n", + DefaultLogSink().Error("Short read of packet body, expecting %u, got %u\n", m_Buffer.Size(), read_count); return RESULT_READFAIL; } @@ -238,7 +254,7 @@ ASDCP::KLVFilePacket::InitFromFile(const FileReader& Reader) if ( read_count != remainder ) { - DefaultLogSink().Error("Short read of packet body, expecting %lu, got %lu\n", + DefaultLogSink().Error("Short read of packet body, expecting %u, got %u\n", remainder+tmp_read_size, read_count+tmp_read_size); result = RESULT_READFAIL; } @@ -250,12 +266,12 @@ ASDCP::KLVFilePacket::InitFromFile(const FileReader& Reader) // ASDCP::Result_t -ASDCP::KLVFilePacket::WriteKLToFile(FileWriter& Writer, const byte_t* label, ui32_t length) +ASDCP::KLVFilePacket::WriteKLToFile(Kumu::FileWriter& Writer, const byte_t* label, ui32_t length) { byte_t buffer[kl_length]; memcpy(buffer, label, SMPTE_UL_LENGTH); - if ( ! write_BER(buffer+SMPTE_UL_LENGTH, length, MXF_BER_LENGTH) ) + if ( ! Kumu::write_BER(buffer+SMPTE_UL_LENGTH, length, MXF_BER_LENGTH) ) return RESULT_FAIL; ui32_t write_count;