Commit fixes found in porting.
[asdcplib.git] / src / KLV.cpp
index b57e805eab8ae8eb0c04d32058741ca88af48c82..29a1c81639db14d3a600a98ed88db586203f4093 100755 (executable)
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2005, John Hurst
+Copyright (c) 2005-2006, John Hurst
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
@@ -24,13 +24,14 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
-
-//
-// KLV.cpp
-//
+/*! \file    KLV.cpp
+  \version $Id$
+  \brief   KLV objects
+*/
 
 #include "KLV.h"
-#include <hex_utils.h>
+#include <KM_log.h>
+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,25 +135,25 @@ ASDCP::KLVPacket::Dump(FILE* stream, bool show_hex)
   if ( m_KeyStart != 0 )
     {
       assert(m_ValueStart);
+      UL TmpUL(m_KeyStart);
+      char buf[64];
+      fprintf(stream, "%s", TmpUL.EncodeString(buf, 64));
 
-      for ( ui32_t i = 0; i < SMPTE_UL_LENGTH; i++ )
-       fprintf(stream, "%02x.", m_KeyStart[i]);
-
-      const MDDEntry* Entry = GetMDDEntry(m_KeyStart);
-      fprintf(stream, "\b  len: %7lu (%s)\n", m_ValueLength, (Entry ? Entry->name : "Unknown"));
+      const MDDEntry* Entry = Dict::FindUL(m_KeyStart);
+      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
     {
-      fprintf(stream, "*** Malformed packet ***\n");
+      fprintf(stream, "*** Malformed KLV packet ***\n");
     }
 }
 
 // 
 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;