summaryrefslogtreecommitdiff
path: root/src/lib/util.cc
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2012-07-23 00:42:37 +0100
committerCarl Hetherington <cth@carlh.net>2012-07-23 00:42:37 +0100
commita7bc9d57f6a5c7c13400e700db50f21c283766f4 (patch)
tree726ef6f292e6fd1dca6bb1b2e3036040c02d2b00 /src/lib/util.cc
parent2fb6cdae8446699be824aaeb902f4bca1888657d (diff)
Use MD5 digest of content file as part of the J2C directory, rather than the filename itself.
Diffstat (limited to 'src/lib/util.cc')
-rw-r--r--src/lib/util.cc36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/lib/util.cc b/src/lib/util.cc
index 7388539cd..799d0e54e 100644
--- a/src/lib/util.cc
+++ b/src/lib/util.cc
@@ -25,6 +25,7 @@
#include <sstream>
#include <iomanip>
#include <iostream>
+#include <fstream>
#ifdef DVDOMATIC_POSIX
#include <execinfo.h>
#include <cxxabi.h>
@@ -35,6 +36,7 @@
#include <signal.h>
#include <boost/algorithm/string.hpp>
#include <openjpeg.h>
+#include <openssl/md5.h>
#include <magick/MagickCore.h>
#include <magick/version.h>
#include <libdcp/version.h>
@@ -469,3 +471,37 @@ md5_data (string title, void const * data, int size)
}
#endif
+string
+md5_digest (string file)
+{
+ ifstream f (file.c_str(), ios::binary);
+ if (!f.good ()) {
+ throw OpenFileError (file);
+ }
+
+ f.seekg (0, ios::end);
+ int bytes = f.tellg ();
+ f.seekg (0, ios::beg);
+
+ int const buffer_size = 64 * 1024;
+ char buffer[buffer_size];
+
+ MD5_CTX md5_context;
+ MD5_Init (&md5_context);
+ while (bytes > 0) {
+ int const t = min (bytes, buffer_size);
+ f.read (buffer, t);
+ MD5_Update (&md5_context, buffer, t);
+ bytes -= t;
+ }
+
+ unsigned char digest[MD5_DIGEST_LENGTH];
+ MD5_Final (digest, &md5_context);
+
+ stringstream s;
+ for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
+ s << hex << setfill('0') << setw(2) << ((int) digest[i]);
+ }
+
+ return s.str ();
+}