Add support for hashing mono picture MXF writes on the way out.
[libdcp.git] / asdcplib / src / KM_fileio.cpp
index bb35ba0f76759a8005909f5952b698b20a0d9c75..138bc3b4cfc1d8b813a26909787f37b01157ce81 100644 (file)
@@ -32,6 +32,8 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <KM_fileio.h>
 #include <KM_log.h>
 #include <fcntl.h>
+#include <sstream>
+#include <iomanip>
 
 #include <assert.h>
 
@@ -49,7 +51,6 @@ using namespace Kumu;
 typedef struct _stati64 fstat_t;
 #define S_IFLNK 0
 
-
 // win32 has WriteFileGather() and ReadFileScatter() but they
 // demand page alignment and page sizing, making them unsuitable
 // for use with arbitrary buffer sizes.
@@ -614,7 +615,10 @@ Kumu::FileReader::Size() const
 
 // these are declared here instead of in the header file
 // because we have a mem_ptr that is managing a hidden class
-Kumu::FileWriter::FileWriter() {}
+Kumu::FileWriter::FileWriter()
+       : m_Hashing (false)
+{}
+
 Kumu::FileWriter::~FileWriter() {}
 
 //
@@ -639,6 +643,37 @@ Kumu::FileWriter::Writev(const byte_t* buf, ui32_t buf_len)
   return RESULT_OK;
 }
 
+void
+Kumu::FileWriter::StartHashing()
+{
+       m_Hashing = true;
+       MD5_Init (&m_MD5Context);
+}
+
+void
+Kumu::FileWriter::MaybeHash(void const * data, int size)
+{
+       if (m_Hashing) {
+               MD5_Update (&m_MD5Context, data, size);
+       }
+}
+
+std::string
+Kumu::FileWriter::StopHashing()
+{
+       m_Hashing = false;
+       
+       unsigned char digest[MD5_DIGEST_LENGTH];
+       MD5_Final (digest, &m_MD5Context);
+
+       std::stringstream s;
+       for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
+               s << std::hex << std::setfill('0') << std::setw(2) << ((int) digest[i]);
+       }
+
+       return s.str ();
+}
+
 
 #ifdef KM_WIN32
 //------------------------------------------------------------------------------------------
@@ -830,6 +865,7 @@ Kumu::FileWriter::Writev(ui32_t* bytes_written)
          break;
        }
 
+      MaybeHash (iov->m_iovec[i].iov_base, iov->m_iovec[i].iov_len);
       *bytes_written += tmp_count;
     }
 
@@ -860,6 +896,8 @@ Kumu::FileWriter::Write(const byte_t* buf, ui32_t buf_len, ui32_t* bytes_written
   if ( result == 0 || *bytes_written != buf_len )
     return Kumu::RESULT_WRITEFAIL;
 
+  MaybeHash (buf, buf_len);
+  
   return Kumu::RESULT_OK;
 }
 
@@ -1006,6 +1044,10 @@ Kumu::FileWriter::Writev(ui32_t* bytes_written)
   if ( write_size == -1L || write_size != total_size )
     return RESULT_WRITEFAIL;
 
+  for (int i = 0; i < iov->m_Count; ++i) {
+         MaybeHash (iov->m_iovec[i].iov_base, iov->m_iovec[i].iov_len);
+  }
+
   iov->m_Count = 0;
   *bytes_written = write_size;  
   return RESULT_OK;
@@ -1025,6 +1067,7 @@ Kumu::FileWriter::Write(const byte_t* buf, ui32_t buf_len, ui32_t* bytes_written
     return RESULT_STATE;
 
   int write_size = write(m_Handle, buf, buf_len);
+  MaybeHash (buf, buf_len);
 
   if ( write_size == -1L || (ui32_t)write_size != buf_len )
     return RESULT_WRITEFAIL;
@@ -1034,7 +1077,7 @@ Kumu::FileWriter::Write(const byte_t* buf, ui32_t buf_len, ui32_t* bytes_written
 }
 
 
-#endif // KM_WIN32
+#endif
 
 //------------------------------------------------------------------------------------------
 
@@ -1102,7 +1145,7 @@ Kumu::WriteStringIntoFile(const char* filename, const std::string& inString)
 
 //
 Kumu::Result_t
-Kumu::ReadFileIntoObject(const std::string& Filename, Kumu::IArchive& Object, ui32_t max_size)
+Kumu::ReadFileIntoObject(const std::string& Filename, Kumu::IArchive& Object, ui32_t)
 {
   ByteString Buffer;
   ui32_t file_size = static_cast<ui32_t>(FileSize(Filename));
@@ -1163,7 +1206,7 @@ Kumu::WriteObjectIntoFile(const Kumu::IArchive& Object, const std::string& Filen
 
 //
 Result_t
-Kumu::ReadFileIntoBuffer(const std::string& Filename, Kumu::ByteString& Buffer, ui32_t max_size)
+Kumu::ReadFileIntoBuffer(const std::string& Filename, Kumu::ByteString& Buffer, ui32_t)
 {
   ui32_t file_size = FileSize(Filename);
   Result_t result = Buffer.Capacity(file_size);
@@ -1211,186 +1254,43 @@ Kumu::WriteBufferIntoFile(const Kumu::ByteString& Buffer, const std::string& Fil
 //------------------------------------------------------------------------------------------
 //
 
-
-// Win32 directory scanner
-//
-#ifdef KM_WIN32
-
-//
-Kumu::DirScanner::DirScanner(void) : m_Handle(-1) {}
-
-//
-//
-Result_t
-Kumu::DirScanner::Open(const char* filename)
-{
-  KM_TEST_NULL_STR_L(filename);
-
-  // we need to append a '*' to read the entire directory
-  ui32_t fn_len = strlen(filename); 
-  char* tmp_file = (char*)malloc(fn_len + 8);
-
-  if ( tmp_file == 0 )
-    return RESULT_ALLOC;
-
-  strcpy(tmp_file, filename);
-  char* p = &tmp_file[fn_len] - 1;
-
-  if ( *p != '/' && *p != '\\' )
-    {
-      p++;
-      *p++ = '/';
-    }
-
-  *p++ = '*';
-  *p = 0;
-  // whew...
-
-  m_Handle = _findfirsti64(tmp_file, &m_FileInfo);
-  Result_t result = RESULT_OK;
-
-  if ( m_Handle == -1 )
-    result = RESULT_NOT_FOUND;
-
-  return result;
-}
-
-
-//
-//
-Result_t
-Kumu::DirScanner::Close()
-{
-  if ( m_Handle == -1 )
-    return RESULT_FILEOPEN;
-
-  if ( _findclose((long)m_Handle) == -1 )
-    return RESULT_FAIL;
-
-  m_Handle = -1;
-  return RESULT_OK;
-}
-
-
-// This sets filename param to the same per-instance buffer every time, so
-// the value will change on the next call
-Result_t
-Kumu::DirScanner::GetNext(char* filename)
+Kumu::DirScanner::DirScanner()
 {
-  KM_TEST_NULL_L(filename);
-
-  if ( m_Handle == -1 )
-    return RESULT_FILEOPEN;
 
-  if ( m_FileInfo.name[0] == '\0' )
-    return RESULT_ENDOFFILE;
-
-  strncpy(filename, m_FileInfo.name, MaxFilePath);
-  Result_t result = RESULT_OK;
-
-  if ( _findnexti64((long)m_Handle, &m_FileInfo) == -1 )
-    {
-      m_FileInfo.name[0] = '\0';
-         
-      if ( errno != ENOENT )
-       result = RESULT_FAIL;
-    }
-
-  return result;
 }
 
-
-#else // KM_WIN32
-
-// POSIX directory scanner
-
-//
-Kumu::DirScanner::DirScanner(void) : m_Handle(NULL) {}
-
-//
 Result_t
-Kumu::DirScanner::Open(const char* filename)
+Kumu::DirScanner::Open (const char* filename)
 {
-  KM_TEST_NULL_STR_L(filename);
-
-  Result_t result = RESULT_OK;
+       KM_TEST_NULL_L (filename);
 
-  if ( ( m_Handle = opendir(filename) ) == NULL )
-    {
-      switch ( errno )
-       {
-       case ENOENT:
-       case ENOTDIR:
-         result = RESULT_NOTAFILE;
-       case EACCES:
-         result = RESULT_NO_PERM;
-       case ELOOP:
-       case ENAMETOOLONG:
-         result = RESULT_PARAM;
-       case EMFILE:
-       case ENFILE:
-         result = RESULT_STATE;
-       default:
-         DefaultLogSink().Error("DirScanner::Open(%s): %s\n", filename, strerror(errno));
-         result = RESULT_FAIL;
+       if (!boost::filesystem::is_directory(filename)) {
+               return RESULT_NOT_FOUND;
        }
-    }
-
-  return result;
+       
+       _iterator = boost::filesystem::directory_iterator (filename);
+       return RESULT_OK;
 }
 
-
-//
 Result_t
-Kumu::DirScanner::Close()
+Kumu::DirScanner::GetNext (char* filename)
 {
-  if ( m_Handle == NULL )
-    return RESULT_FILEOPEN;
-
-  if ( closedir(m_Handle) == -1 ) {
-    switch ( errno )
-      {
-      case EBADF:
-      case EINTR:
-       return RESULT_STATE;
-      default:
-       DefaultLogSink().Error("DirScanner::Close(): %s\n", strerror(errno));
-       return RESULT_FAIL;
-      }
-  }
-
-  m_Handle = NULL;
-  return RESULT_OK;
-}
-
-
-//
-Result_t
-Kumu::DirScanner::GetNext(char* filename)
-{
-  KM_TEST_NULL_L(filename);
-
-  if ( m_Handle == NULL )
-    return RESULT_FILEOPEN;
-
-  struct dirent* entry;
-
-  for (;;)
-    {
-      if ( ( entry = readdir(m_Handle)) == NULL )
-       return RESULT_ENDOFFILE;
-
-      break;
-    }
+       KM_TEST_NULL_L (filename);
+       
+       if (_iterator == boost::filesystem::directory_iterator()) {
+               return RESULT_ENDOFFILE;
+       }
 
-  strncpy(filename, entry->d_name, MaxFilePath);
-  return RESULT_OK;
+#if BOOST_FILESYSTEM_VERSION == 3      
+       std::string f = boost::filesystem::path(*_iterator).filename().generic_string();
+#else
+       std::string f = boost::filesystem::path(*_iterator).filename();
+#endif 
+       strncpy (filename, f.c_str(), MaxFilePath);
+       ++_iterator;
+       return RESULT_OK;
 }
 
-
-#endif // KM_WIN32
-
-
 //------------------------------------------------------------------------------------------
 
 //