summaryrefslogtreecommitdiff
path: root/asdcplib/src/KM_fileio.cpp
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2013-01-28 00:11:30 +0000
committerCarl Hetherington <cth@carlh.net>2013-01-28 00:11:30 +0000
commita246eb45b34ebc6bf277694b295f693706be8c6a (patch)
treebb5d78210f036eaddd8ad66af1325bc3d6636fcb /asdcplib/src/KM_fileio.cpp
parent103c20d48c22f0c604e402de41bce7336ef9b386 (diff)
Add support for hashing mono picture MXF writes on the way out.
Diffstat (limited to 'asdcplib/src/KM_fileio.cpp')
-rw-r--r--asdcplib/src/KM_fileio.cpp49
1 files changed, 46 insertions, 3 deletions
diff --git a/asdcplib/src/KM_fileio.cpp b/asdcplib/src/KM_fileio.cpp
index 2914f982..138bc3b4 100644
--- a/asdcplib/src/KM_fileio.cpp
+++ b/asdcplib/src/KM_fileio.cpp
@@ -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
//------------------------------------------------------------------------------------------