diff options
| author | Carl Hetherington <cth@carlh.net> | 2024-03-16 21:50:24 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2024-03-21 20:29:50 +0100 |
| commit | 4898f9d60bc621cb464faa00fb50146495d76928 (patch) | |
| tree | 8d756384f4bd9cc866aa278eac8dd594082304d7 | |
| parent | 8b59eda953e7fa37506e6cb6add7dcb7d5ae8dce (diff) | |
Support hashing while writing MXFs.
| -rwxr-xr-x | src/AS_DCP.h | 5 | ||||
| -rwxr-xr-x | src/AS_DCP_JP2K.cpp | 24 | ||||
| -rwxr-xr-x | src/AS_DCP_internal.h | 4 | ||||
| -rw-r--r-- | src/KM_fileio.cpp | 64 | ||||
| -rwxr-xr-x | src/KM_fileio.h | 9 | ||||
| -rwxr-xr-x | src/h__Writer.cpp | 18 |
6 files changed, 102 insertions, 22 deletions
diff --git a/src/AS_DCP.h b/src/AS_DCP.h index dac5cd1..fcf9253 100755 --- a/src/AS_DCP.h +++ b/src/AS_DCP.h @@ -1270,9 +1270,10 @@ namespace ASDCP { // Writes a frame of essence to the MXF file. If the optional AESEncContext // argument is present, the essence is encrypted prior to writing. + // A MD5 hash of the data that we write is written to hash if it is not 0 // Fails if the file is not open, is finalized, or an operating system // error occurs. - Result_t WriteFrame(const FrameBuffer&, AESEncContext* = 0, HMACContext* = 0); + Result_t WriteFrame(const FrameBuffer&, AESEncContext* = 0, HMACContext* = 0, std::string* hash = 0); Result_t FakeWriteFrame(int size); @@ -1391,7 +1392,7 @@ namespace ASDCP { // RESULT_SPHASE will be returned if phase is reversed. The first frame // written must be left eye. Result_t WriteFrame(const FrameBuffer&, StereoscopicPhase_t phase, - AESEncContext* = 0, HMACContext* = 0); + AESEncContext* = 0, HMACContext* = 0, std::string* hash = 0); Result_t FakeWriteFrame(int size, StereoscopicPhase_t phase); diff --git a/src/AS_DCP_JP2K.cpp b/src/AS_DCP_JP2K.cpp index 5e7d7a3..33bde17 100755 --- a/src/AS_DCP_JP2K.cpp +++ b/src/AS_DCP_JP2K.cpp @@ -1161,7 +1161,7 @@ public: Result_t OpenWrite(const std::string&, EssenceType_t type, ui32_t HeaderSize, bool); Result_t SetSourceStream(const PictureDescriptor&, const std::string& label, ASDCP::Rational LocalEditRate = ASDCP::Rational(0,0)); - Result_t WriteFrame(const JP2K::FrameBuffer&, bool add_index, AESEncContext*, HMACContext*); + Result_t WriteFrame(const JP2K::FrameBuffer&, bool add_index, AESEncContext*, HMACContext*, std::string* hash = 0); Result_t FakeWriteFrame(int size, bool add_index); Result_t Finalize(); }; @@ -1269,7 +1269,7 @@ lh__Writer::SetSourceStream(const PictureDescriptor& PDesc, const std::string& l // ASDCP::Result_t lh__Writer::WriteFrame(const JP2K::FrameBuffer& FrameBuf, bool add_index, - AESEncContext* Ctx, HMACContext* HMAC) + AESEncContext* Ctx, HMACContext* HMAC, std::string* hash) { Result_t result = RESULT_OK; @@ -1279,7 +1279,7 @@ lh__Writer::WriteFrame(const JP2K::FrameBuffer& FrameBuf, bool add_index, ui64_t StreamOffset = m_StreamOffset; if ( ASDCP_SUCCESS(result) ) - result = WriteEKLVPacket(FrameBuf, m_EssenceUL, MXF_BER_LENGTH, Ctx, HMAC); + result = WriteEKLVPacket(FrameBuf, m_EssenceUL, MXF_BER_LENGTH, Ctx, HMAC, hash); if ( ASDCP_SUCCESS(result) && add_index ) { @@ -1430,12 +1430,12 @@ ASDCP::JP2K::MXFWriter::OpenWrite(const std::string& filename, const WriterInfo& // Fails if the file is not open, is finalized, or an operating system // error occurs. ASDCP::Result_t -ASDCP::JP2K::MXFWriter::WriteFrame(const FrameBuffer& FrameBuf, AESEncContext* Ctx, HMACContext* HMAC) +ASDCP::JP2K::MXFWriter::WriteFrame(const FrameBuffer& FrameBuf, AESEncContext* Ctx, HMACContext* HMAC, std::string* hash) { if ( m_Writer.empty() ) return RESULT_INIT; - return m_Writer->WriteFrame(FrameBuf, true, Ctx, HMAC); + return m_Writer->WriteFrame(FrameBuf, true, Ctx, HMAC, hash); } ASDCP::Result_t @@ -1478,7 +1478,7 @@ public: // Result_t WriteFrame(const FrameBuffer& FrameBuf, StereoscopicPhase_t phase, - AESEncContext* Ctx, HMACContext* HMAC) + AESEncContext* Ctx, HMACContext* HMAC, std::string* hash) { if ( m_NextPhase != phase ) return RESULT_SPHASE; @@ -1486,11 +1486,11 @@ public: if ( phase == SP_LEFT ) { m_NextPhase = SP_RIGHT; - return lh__Writer::WriteFrame(FrameBuf, true, Ctx, HMAC); + return lh__Writer::WriteFrame(FrameBuf, true, Ctx, HMAC, hash); } m_NextPhase = SP_LEFT; - return lh__Writer::WriteFrame(FrameBuf, false, Ctx, HMAC); + return lh__Writer::WriteFrame(FrameBuf, false, Ctx, HMAC, hash); } Result_t FakeWriteFrame(int size, StereoscopicPhase_t phase) @@ -1653,10 +1653,10 @@ ASDCP::JP2K::MXFSWriter::WriteFrame(const SFrameBuffer& FrameBuf, AESEncContext* if ( m_Writer.empty() ) return RESULT_INIT; - Result_t result = m_Writer->WriteFrame(FrameBuf.Left, SP_LEFT, Ctx, HMAC); + Result_t result = m_Writer->WriteFrame(FrameBuf.Left, SP_LEFT, Ctx, HMAC, 0); if ( ASDCP_SUCCESS(result) ) - result = m_Writer->WriteFrame(FrameBuf.Right, SP_RIGHT, Ctx, HMAC); + result = m_Writer->WriteFrame(FrameBuf.Right, SP_RIGHT, Ctx, HMAC, 0); return result; } @@ -1667,12 +1667,12 @@ ASDCP::JP2K::MXFSWriter::WriteFrame(const SFrameBuffer& FrameBuf, AESEncContext* // error occurs. ASDCP::Result_t ASDCP::JP2K::MXFSWriter::WriteFrame(const FrameBuffer& FrameBuf, StereoscopicPhase_t phase, - AESEncContext* Ctx, HMACContext* HMAC) + AESEncContext* Ctx, HMACContext* HMAC, std::string* hash) { if ( m_Writer.empty() ) return RESULT_INIT; - return m_Writer->WriteFrame(FrameBuf, phase, Ctx, HMAC); + return m_Writer->WriteFrame(FrameBuf, phase, Ctx, HMAC, hash); } ASDCP::Result_t diff --git a/src/AS_DCP_internal.h b/src/AS_DCP_internal.h index 31c141c..5e7b9cc 100755 --- a/src/AS_DCP_internal.h +++ b/src/AS_DCP_internal.h @@ -183,7 +183,7 @@ extern MXF::RIP *g_RIP; const ASDCP::WriterInfo& Info, ASDCP::FrameBuffer& CtFrameBuf, ui32_t& FramesWritten, ui64_t & StreamOffset, const ASDCP::FrameBuffer& FrameBuf, const byte_t* EssenceUL, const ui32_t& MinEssenceElementBerLength, - AESEncContext* Ctx, HMACContext* HMAC); + AESEncContext* Ctx, HMACContext* HMAC, std::string* hash = 0); // class KLReader : public ASDCP::KLVPacket @@ -965,7 +965,7 @@ extern MXF::RIP *g_RIP; Result_t CreateBodyPart(const MXF::Rational& EditRate, ui32_t BytesPerEditUnit = 0); Result_t WriteEKLVPacket(const ASDCP::FrameBuffer& FrameBuf,const byte_t* EssenceUL, const ui32_t& MinEssenceElementBerLength, - AESEncContext* Ctx, HMACContext* HMAC); + AESEncContext* Ctx, HMACContext* HMAC, std::string* hash = 0); Result_t FakeWriteEKLVPacket(int size); Result_t WriteASDCPFooter(); }; diff --git a/src/KM_fileio.cpp b/src/KM_fileio.cpp index 3ccb3f6..14bb16a 100644 --- a/src/KM_fileio.cpp +++ b/src/KM_fileio.cpp @@ -743,7 +743,9 @@ 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() {} // @@ -768,6 +770,57 @@ 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); + + char buffer[33]; + snprintf( + buffer, + 33, + // 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", + digest[0], + digest[1], + digest[2], + digest[3], + digest[4], + digest[5], + digest[6], + digest[7], + digest[8], + digest[9], + digest[10], + digest[11], + digest[12], + digest[13], + digest[14], + digest[15] + ); + + return buffer; +} + Kumu::FileReader::FileReader() { m_Handle = INVALID_HANDLE_VALUE; @@ -1086,6 +1139,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; } @@ -1116,6 +1170,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; } @@ -1259,6 +1315,11 @@ 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; @@ -1282,6 +1343,7 @@ Kumu::FileWriter::Write(const byte_t* buf, ui32_t buf_len, ui32_t* bytes_written if ( write_size == -1L || (ui32_t)write_size != buf_len ) return RESULT_WRITEFAIL; + MaybeHash(buf, buf_len); *bytes_written = write_size; return RESULT_OK; } diff --git a/src/KM_fileio.h b/src/KM_fileio.h index 1ef5e75..d8147e3 100755 --- a/src/KM_fileio.h +++ b/src/KM_fileio.h @@ -34,6 +34,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include <KM_util.h> #include <string> +#include <openssl/md5.h> #ifdef KM_WIN32 # include <io.h> @@ -133,7 +134,7 @@ namespace Kumu // // error: 'void Kumu::compile_time_size_checker() [with bool sizecheck = false]' previously declared here // - // This is happening because the equality being tested below is false. The reason for this + // This is happening because the equality being tested below is false. The reason for this // will depend on your OS, but on Linux it is probably because you have not used -D_FILE_OFFSET_BITS=64 // Adding this magic macro to your CFLAGS will get you going again. If you are on a system that // does not support 64-bit files, you can disable this check by using -DKM_SMALL_FILES_OK. You @@ -380,6 +381,8 @@ namespace Kumu class h__iovec; mem_ptr<h__iovec> m_IOVec; KM_NO_COPY_CONSTRUCT(FileWriter); + bool m_Hashing; + MD5_CTX m_MD5Context; public: FileWriter(); @@ -399,6 +402,10 @@ namespace Kumu // the iovec list will be written to disk before the given buffer,as though // you had called Writev() first. Result_t Write(const byte_t*, ui32_t, ui32_t* = 0); // write buffer to disk + + void StartHashing(); + void MaybeHash(void const*, int); + std::string StopHashing(); }; Result_t CreateDirectoriesInPath(const std::string& Path); diff --git a/src/h__Writer.cpp b/src/h__Writer.cpp index c7aa8ab..4eaf72f 100755 --- a/src/h__Writer.cpp +++ b/src/h__Writer.cpp @@ -319,12 +319,12 @@ ASDCP::h__ASDCPWriter::WriteASDCPHeader(const std::string& PackageLabel, const U // Result_t ASDCP::h__ASDCPWriter::WriteEKLVPacket(const ASDCP::FrameBuffer& FrameBuf,const byte_t* EssenceUL, - const ui32_t& MinEssenceElementBerLength, - AESEncContext* Ctx, HMACContext* HMAC) + const ui32_t& MinEssenceElementBerLength, + AESEncContext* Ctx, HMACContext* HMAC, std::string* hash) { return Write_EKLV_Packet(m_File, *m_Dict, m_HeaderPart, m_Info, m_CtFrameBuf, m_FramesWritten, m_StreamOffset, FrameBuf, EssenceUL, MinEssenceElementBerLength, - Ctx, HMAC); + Ctx, HMAC, hash); } Result_t @@ -393,11 +393,16 @@ ASDCP::Write_EKLV_Packet(Kumu::FileWriter& File, const ASDCP::Dictionary& Dict, const ASDCP::WriterInfo& Info, ASDCP::FrameBuffer& CtFrameBuf, ui32_t& FramesWritten, ui64_t & StreamOffset, const ASDCP::FrameBuffer& FrameBuf, const byte_t* EssenceUL, const ui32_t& MinEssenceElementBerLength, - AESEncContext* Ctx, HMACContext* HMAC) + AESEncContext* Ctx, HMACContext* HMAC, std::string* hash) { Result_t result = RESULT_OK; IntegrityPack IntPack; + if (hash) + { + File.StartHashing(); + } + byte_t overhead[128]; Kumu::MemIOWriter Overhead(overhead, 128); // We declare HMACOverhead and its buffer in the outer scope, even though it is not used on @@ -538,6 +543,11 @@ ASDCP::Write_EKLV_Packet(Kumu::FileWriter& File, const ASDCP::Dictionary& Dict, if ( ASDCP_SUCCESS(result) ) result = File.Writev(); + if (hash) + { + *hash = File.StopHashing(); + } + return result; } |
