#define _AS_DCP_H_
#include <KM_error.h>
+#include <KM_platform.h>
#include <stdio.h>
#include <stdarg.h>
#include <math.h>
// 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);
// Closes the MXF file, writing the index and revised header.
Result_t Finalize();
+
+ // Return the current file offset in the MXF file that we are writing
+ ui64_t Tell() const;
};
//
Result_t OpenWrite(const char*, EssenceType_t type, ui32_t HeaderSize);
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 Finalize();
Result_t JP2K_PDesc_to_MD(JP2K::PictureDescriptor& PDesc);
};
//
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;
ui64_t StreamOffset = m_StreamOffset;
if ( ASDCP_SUCCESS(result) )
- result = WriteEKLVPacket(FrameBuf, m_EssenceUL, Ctx, HMAC);
+ result = WriteEKLVPacket(FrameBuf, m_EssenceUL, Ctx, HMAC, hash);
if ( ASDCP_SUCCESS(result) && add_index )
{
// 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);
}
// Closes the MXF file, writing the index and other closing information.
return m_Writer->Finalize();
}
+ui64_t
+ASDCP::JP2K::MXFWriter::Tell() const
+{
+ return m_Writer->m_File.Tell();
+}
+
//------------------------------------------------------------------------------------------
//
ui32_t TCFrameRate, ui32_t BytesPerEditUnit = 0);
Result_t WriteEKLVPacket(const ASDCP::FrameBuffer& FrameBuf,
- const byte_t* EssenceUL, AESEncContext* Ctx, HMACContext* HMAC);
+ const byte_t* EssenceUL, AESEncContext* Ctx, HMACContext* HMAC, std::string* hash = 0);
Result_t WriteMXFFooter();
#include <KM_fileio.h>
#include <KM_log.h>
#include <fcntl.h>
+#include <sstream>
+#include <iomanip>
#include <assert.h>
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.
// 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() {}
//
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
//------------------------------------------------------------------------------------------
break;
}
+ MaybeHash (iov->m_iovec[i].iov_base, iov->m_iovec[i].iov_len);
*bytes_written += tmp_count;
}
if ( result == 0 || *bytes_written != buf_len )
return Kumu::RESULT_WRITEFAIL;
+ MaybeHash (buf, buf_len);
+
return Kumu::RESULT_OK;
}
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;
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;
}
-#endif // KM_WIN32
+#endif
//------------------------------------------------------------------------------------------
#include <KM_util.h>
#include <string>
#include <boost/filesystem.hpp>
+#include <openssl/md5.h>
#ifdef KM_WIN32
# include <io.h>
class h__iovec;
mem_ptr<h__iovec> m_IOVec;
KM_NO_COPY_CONSTRUCT(FileWriter);
+ bool m_Hashing;
+ MD5_CTX m_MD5Context;
public:
FileWriter();
// 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);
// standard method of writing a plaintext or encrypted frame
Result_t
ASDCP::h__Writer::WriteEKLVPacket(const ASDCP::FrameBuffer& FrameBuf, const byte_t* EssenceUL,
- AESEncContext* Ctx, HMACContext* HMAC)
+ AESEncContext* Ctx, HMACContext* HMAC, std::string* hash)
{
Result_t result = RESULT_OK;
IntegrityPack IntPack;
+ m_File.StartHashing();
+
byte_t overhead[128];
Kumu::MemIOWriter Overhead(overhead, 128);
assert(m_Dict);
if ( ASDCP_SUCCESS(result) )
result = m_File.Writev();
+ if (hash) {
+ *hash = m_File.StopHashing();
+ }
+
return result;
}
using std::list;
using std::vector;
using std::max;
+using std::pair;
+using std::make_pair;
+using std::istream;
using boost::shared_ptr;
using boost::dynamic_pointer_cast;
using boost::lexical_cast;
throw FileError ("could not open JPEG2000 file for reading", path);
}
- /* XXX: passing 0 to WriteFrame ok? */
if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) {
throw MiscError ("error in writing video MXF");
}
shared_ptr<const MonoPictureFrame>
MonoPictureAsset::get_frame (int n) const
{
- return shared_ptr<const MonoPictureFrame> (new MonoPictureFrame (path().string(), n + _entry_point));
+ return shared_ptr<const MonoPictureFrame> (new MonoPictureFrame (path().string(), n));
}
shared_ptr<const StereoPictureFrame>
StereoPictureAsset::get_frame (int n) const
{
- return shared_ptr<const StereoPictureFrame> (new StereoPictureFrame (path().string(), n + _entry_point));
+ return shared_ptr<const StereoPictureFrame> (new StereoPictureFrame (path().string(), n));
}
shared_ptr<MonoPictureAssetWriter>
return shared_ptr<MonoPictureAssetWriter> (new MonoPictureAssetWriter (this));
}
+FrameInfo::FrameInfo (istream& s)
+{
+ s >> offset >> length >> hash;
+}
+
+void
+FrameInfo::write (ostream& s)
+{
+ s << offset << " " << length << " " << hash;
+}
+
struct MonoPictureAssetWriter::ASDCPState
{
ASDCPState()
}
-void
+FrameInfo
MonoPictureAssetWriter::write (uint8_t* data, int size)
{
assert (!_finalized);
}
}
- if (ASDCP_FAILURE (_state->mxf_writer.WriteFrame (_state->frame_buffer, 0, 0))) {
+ uint64_t const before_offset = _state->mxf_writer.Tell ();
+
+ string hash;
+ if (ASDCP_FAILURE (_state->mxf_writer.WriteFrame (_state->frame_buffer, 0, 0, &hash))) {
throw MiscError ("error in writing video MXF");
}
_frames_written++;
+ return FrameInfo (before_offset, _state->mxf_writer.Tell() - before_offset, hash);
}
void
*/
+#ifndef LIBDCP_PICTURE_ASSET_H
+#define LIBDCP_PICTURE_ASSET_H
+
/** @file src/picture_asset.h
* @brief An asset made up of JPEG2000 files
*/
class MonoPictureAsset;
+struct FrameInfo
+{
+ FrameInfo (uint64_t o, uint64_t l, std::string h)
+ : offset (o)
+ , length (l)
+ , hash (h)
+ {}
+
+ FrameInfo (std::istream& s);
+
+ void write (std::ostream& s);
+
+ uint64_t offset;
+ uint64_t length;
+ std::string hash;
+};
+
/** A helper class for writing to MonoPictureAssets progressively (i.e. writing frame-by-frame,
* rather than giving libdcp all the frames in one go).
*
public:
~MonoPictureAssetWriter ();
- void write (uint8_t* data, int size);
+ FrameInfo write (uint8_t* data, int size);
void finalize ();
private:
}
+
+#endif