Replace dcp::Data with dcp::ArrayData
authorCarl Hetherington <cth@carlh.net>
Sun, 1 Nov 2020 22:40:01 +0000 (23:40 +0100)
committerCarl Hetherington <cth@carlh.net>
Mon, 2 Nov 2020 22:10:04 +0000 (23:10 +0100)
16 files changed:
src/lib/crypto.cc
src/lib/crypto.h
src/lib/dcp_video.cc
src/lib/dcp_video.h
src/lib/emailer.cc
src/lib/encode_server.cc
src/lib/ffmpeg_image_proxy.cc
src/lib/ffmpeg_image_proxy.h
src/lib/image.cc
src/lib/image.h
src/lib/reel_writer.cc
src/lib/string_text_file.cc
src/tools/server_test.cc
test/client_server_test.cc
test/crypto_test.cc
test/test.cc

index c67fc27fa17f9c5de899076ac1c70f54b79f112a..494924daad4486587bc7fb3200a900c3776390ec 100644 (file)
@@ -34,39 +34,39 @@ using namespace dcpomatic;
 /** The cipher that this code uses */
 #define CIPHER EVP_aes_256_cbc()
 
-dcp::Data
+dcp::ArrayData
 dcpomatic::random_iv ()
 {
        EVP_CIPHER const * cipher = CIPHER;
-       dcp::Data iv (EVP_CIPHER_iv_length(cipher));
-       RAND_bytes (iv.data().get(), iv.size());
+       dcp::ArrayData iv (EVP_CIPHER_iv_length(cipher));
+       RAND_bytes (iv.data(), iv.size());
        return iv;
 }
 
-dcp::Data
-dcpomatic::encrypt (string plaintext, dcp::Data key, dcp::Data iv)
+dcp::ArrayData
+dcpomatic::encrypt (string plaintext, dcp::ArrayData key, dcp::ArrayData iv)
 {
        EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new ();
        if (!ctx) {
                throw CryptoError ("could not create cipher context");
        }
 
-       int r = EVP_EncryptInit_ex (ctx, CIPHER, 0, key.data().get(), iv.data().get());
+       int r = EVP_EncryptInit_ex (ctx, CIPHER, 0, key.data(), iv.data());
        if (r != 1) {
                throw CryptoError ("could not initialise cipher context for encryption");
        }
 
-       dcp::Data ciphertext (plaintext.size() * 2);
+       dcp::ArrayData ciphertext (plaintext.size() * 2);
 
        int len;
-       r = EVP_EncryptUpdate (ctx, ciphertext.data().get(), &len, (uint8_t const *) plaintext.c_str(), plaintext.size());
+       r = EVP_EncryptUpdate (ctx, ciphertext.data(), &len, (uint8_t const *) plaintext.c_str(), plaintext.size());
        if (r != 1) {
                throw CryptoError ("could not encrypt data");
        }
 
        int ciphertext_len = len;
 
-       r = EVP_EncryptFinal_ex (ctx, ciphertext.data().get() + len, &len);
+       r = EVP_EncryptFinal_ex (ctx, ciphertext.data() + len, &len);
        if (r != 1) {
                throw CryptoError ("could not finish encryption");
        }
@@ -79,40 +79,40 @@ dcpomatic::encrypt (string plaintext, dcp::Data key, dcp::Data iv)
 }
 
 string
-dcpomatic::decrypt (dcp::Data ciphertext, dcp::Data key, dcp::Data iv)
+dcpomatic::decrypt (dcp::ArrayData ciphertext, dcp::ArrayData key, dcp::ArrayData iv)
 {
        EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new ();
        if (!ctx) {
                throw CryptoError ("could not create cipher context");
        }
 
-       int r = EVP_DecryptInit_ex (ctx, CIPHER, 0, key.data().get(), iv.data().get());
+       int r = EVP_DecryptInit_ex (ctx, CIPHER, 0, key.data(), iv.data());
        if (r != 1) {
                throw CryptoError ("could not initialise cipher context for decryption");
        }
 
-       dcp::Data plaintext (ciphertext.size() * 2);
+       dcp::ArrayData plaintext (ciphertext.size() * 2);
 
        int len;
-       r = EVP_DecryptUpdate (ctx, plaintext.data().get(), &len, ciphertext.data().get(), ciphertext.size());
+       r = EVP_DecryptUpdate (ctx, plaintext.data(), &len, ciphertext.data(), ciphertext.size());
        if (r != 1) {
                throw CryptoError ("could not decrypt data");
        }
 
        int plaintext_len = len;
 
-       r = EVP_DecryptFinal_ex (ctx, plaintext.data().get() + len, &len);
+       r = EVP_DecryptFinal_ex (ctx, plaintext.data() + len, &len);
        if (r != 1) {
                throw CryptoError ("could not finish decryption");
        }
 
        plaintext_len += len;
        plaintext.set_size (plaintext_len + 1);
-       plaintext.data().get()[plaintext_len] = '\0';
+       plaintext.data()[plaintext_len] = '\0';
 
        EVP_CIPHER_CTX_free (ctx);
 
-       return string ((char *) plaintext.data().get());
+       return string ((char *) plaintext.data());
 }
 
 int
index e450d4db1da729ad7b0299f0d479efffe1b69a7c..ee0ff9b558d6026e816ff81aac0d6cc0de7d86a1 100644 (file)
 
 */
 
-#include <dcp/data.h>
+#include <dcp/array_data.h>
 
 namespace dcpomatic {
-       
-dcp::Data random_iv ();
-dcp::Data encrypt (std::string plaintext, dcp::Data key, dcp::Data iv);
-std::string decrypt (dcp::Data ciphertext, dcp::Data key, dcp::Data iv);
-int crypto_key_length ();      
+
+dcp::ArrayData random_iv ();
+dcp::ArrayData encrypt (std::string plaintext, dcp::ArrayData key, dcp::ArrayData iv);
+std::string decrypt (dcp::ArrayData ciphertext, dcp::ArrayData key, dcp::ArrayData iv);
+int crypto_key_length ();
 
 }
 
index 36928b3fcfb8ac4f22c1cf674fa612d2a755a6a3..b3461e56908fa015ab38255d41f209647e508c21 100644 (file)
@@ -60,7 +60,7 @@ using std::string;
 using std::cout;
 using boost::shared_ptr;
 using dcp::Size;
-using dcp::Data;
+using dcp::ArrayData;
 using dcp::raw_convert;
 #if BOOST_VERSION >= 106100
 using namespace boost::placeholders;
@@ -118,12 +118,12 @@ DCPVideo::convert_to_xyz (shared_ptr<const PlayerVideo> frame, dcp::NoteHandler
 /** J2K-encode this frame on the local host.
  *  @return Encoded data.
  */
-Data
+ArrayData
 DCPVideo::encode_locally ()
 {
        string const comment = Config::instance()->dcp_j2k_comment();
 
-       Data enc = dcp::compress_j2k (
+       ArrayData enc = dcp::compress_j2k (
                convert_to_xyz (_frame, boost::bind(&Log::dcp_log, dcpomatic_log.get(), _1, _2)),
                _j2k_bandwidth,
                _frames_per_second,
@@ -154,7 +154,7 @@ DCPVideo::encode_locally ()
  *  @param timeout timeout in seconds.
  *  @return Encoded data.
  */
-Data
+ArrayData
 DCPVideo::encode_remotely (EncodeServerDescription serv, int timeout)
 {
        boost::asio::io_service io_service;
@@ -192,9 +192,9 @@ DCPVideo::encode_remotely (EncodeServerDescription serv, int timeout)
        */
        Socket::ReadDigestScope ds (socket);
        LOG_TIMING("start-remote-encode thread=%1", thread_id ());
-       Data e (socket->read_uint32 ());
+       ArrayData e (socket->read_uint32 ());
        LOG_TIMING("start-remote-receive thread=%1", thread_id ());
-       socket->read (e.data().get(), e.size());
+       socket->read (e.data(), e.size());
        LOG_TIMING("finish-remote-receive thread=%1", thread_id ());
        if (!ds.check()) {
                throw NetworkError ("Checksums do not match");
index 81ddc4470c9e748f81289262a3da382eae11ee68..aa11d7d3c62b4b1591efd3d0ec77d774e1f57355 100644 (file)
@@ -21,7 +21,7 @@
 #include "types.h"
 #include "encode_server_description.h"
 #include <libcxml/cxml.h>
-#include <dcp/data.h>
+#include <dcp/array_data.h>
 
 /** @file  src/dcp_video_frame.h
  *  @brief A single frame of video destined for a DCP.
@@ -45,8 +45,8 @@ public:
        DCPVideo (boost::shared_ptr<const PlayerVideo>, int, int, int, Resolution);
        DCPVideo (boost::shared_ptr<const PlayerVideo>, cxml::ConstNodePtr);
 
-       dcp::Data encode_locally ();
-       dcp::Data encode_remotely (EncodeServerDescription, int timeout = 30);
+       dcp::ArrayData encode_locally ();
+       dcp::ArrayData encode_remotely (EncodeServerDescription, int timeout = 30);
 
        int index () const {
                return _index;
index dc216e90cc087c30d5f553703786f34566e07bc1..6fe537eb1ce759d8a1f43ee44f1a651be9051058 100644 (file)
@@ -35,7 +35,7 @@ using std::list;
 using std::cout;
 using std::pair;
 using boost::shared_ptr;
-using dcp::Data;
+using dcp::ArrayData;
 
 Emailer::Emailer (string from, list<string> to, string subject, string body)
        : _from (from)
@@ -155,8 +155,8 @@ Emailer::send (string server, int port, EmailProtocol protocol, string user, str
                BIO* bio = BIO_new (BIO_s_mem());
                bio = BIO_push (b64, bio);
 
-               Data data (i.file);
-               BIO_write (bio, data.data().get(), data.size());
+               ArrayData data (i.file);
+               BIO_write (bio, data.data(), data.size());
                (void) BIO_flush (bio);
 
                char* out;
index f4224798bce211f423574aed7c0496ecb1e0958e..9e096fdfac037f852fc820bbd3c2414c8ca325c8 100644 (file)
@@ -65,8 +65,8 @@ using boost::thread;
 using boost::bind;
 using boost::scoped_array;
 using boost::optional;
+using dcp::ArrayData;
 using dcp::Size;
-using dcp::Data;
 using dcp::raw_convert;
 
 EncodeServer::EncodeServer (bool verbose, int num_threads)
@@ -145,14 +145,14 @@ EncodeServer::process (shared_ptr<Socket> socket, struct timeval& after_read, st
 
        gettimeofday (&after_read, 0);
 
-       Data encoded = dcp_video_frame.encode_locally ();
+       ArrayData encoded = dcp_video_frame.encode_locally ();
 
        gettimeofday (&after_encode, 0);
 
        try {
                Socket::WriteDigestScope ds (socket);
                socket->write (encoded.size());
-               socket->write (encoded.data().get(), encoded.size());
+               socket->write (encoded.data(), encoded.size());
        } catch (std::exception& e) {
                cerr << "Send failed; frame " << dcp_video_frame.index() << "\n";
                LOG_ERROR ("Send failed; frame %1", dcp_video_frame.index());
index f2b72059b9a013f59bad2c66e7ad56baa1dc207d..602185bb8b4ade76013935cd6a096a08c957d995 100644 (file)
@@ -56,7 +56,7 @@ FFmpegImageProxy::FFmpegImageProxy (boost::filesystem::path path)
 
 }
 
-FFmpegImageProxy::FFmpegImageProxy (dcp::Data data)
+FFmpegImageProxy::FFmpegImageProxy (dcp::ArrayData data)
        : _data (data)
        , _pos (0)
 {
@@ -67,8 +67,8 @@ FFmpegImageProxy::FFmpegImageProxy (shared_ptr<cxml::Node>, shared_ptr<Socket> s
        : _pos (0)
 {
        uint32_t const size = socket->read_uint32 ();
-       _data = dcp::Data (size);
-       socket->read (_data.data().get(), size);
+       _data = dcp::ArrayData (size);
+       socket->read (_data.data(), size);
 }
 
 static int
@@ -90,7 +90,7 @@ FFmpegImageProxy::avio_read (uint8_t* buffer, int const amount)
        if (to_do == 0) {
                return AVERROR_EOF;
        }
-       memcpy (buffer, _data.data().get() + _pos, to_do);
+       memcpy (buffer, _data.data() + _pos, to_do);
        _pos += to_do;
        return to_do;
 }
@@ -212,7 +212,7 @@ void
 FFmpegImageProxy::write_to_socket (shared_ptr<Socket> socket) const
 {
        socket->write (_data.size());
-       socket->write (_data.data().get(), _data.size());
+       socket->write (_data.data(), _data.size());
 }
 
 bool
@@ -223,11 +223,7 @@ FFmpegImageProxy::same (shared_ptr<const ImageProxy> other) const
                return false;
        }
 
-       if (_data.size() != mp->_data.size()) {
-               return false;
-       }
-
-       return memcmp (_data.data().get(), mp->_data.data().get(), _data.size()) == 0;
+       return _data == mp->_data;
 }
 
 size_t
index aa77003a4f9169c90bbf47471806700369f6b96e..62b99d280f42e4b7f42f790e2cb52f0be66d475b 100644 (file)
@@ -19,7 +19,7 @@
 */
 
 #include "image_proxy.h"
-#include <dcp/data.h>
+#include <dcp/array_data.h>
 #include <boost/thread/mutex.hpp>
 #include <boost/filesystem.hpp>
 
@@ -27,7 +27,7 @@ class FFmpegImageProxy : public ImageProxy
 {
 public:
        explicit FFmpegImageProxy (boost::filesystem::path);
-       explicit FFmpegImageProxy (dcp::Data);
+       explicit FFmpegImageProxy (dcp::ArrayData);
        FFmpegImageProxy (boost::shared_ptr<cxml::Node> xml, boost::shared_ptr<Socket> socket);
 
        Result image (
@@ -43,7 +43,7 @@ public:
        int64_t avio_seek (int64_t const pos, int whence);
 
 private:
-       dcp::Data _data;
+       dcp::ArrayData _data;
        mutable int64_t _pos;
        /** Path of a file that this image came from, if applicable; stored so that
            failed-decode errors can give more detail.
index 002c7df9ad11fb9e80947fcf51300096c5df27d2..03f1bf6dc626664dfd88727674629e4b6d8abef1 100644 (file)
@@ -1278,7 +1278,7 @@ Image::png_error (char const * message)
        throw EncodeError (String::compose ("Error during PNG write: %1", message));
 }
 
-dcp::Data
+dcp::ArrayData
 Image::as_png () const
 {
        DCPOMATIC_ASSERT (bytes_per_pixel(0) == 4);
@@ -1317,5 +1317,5 @@ Image::as_png () const
        png_destroy_write_struct (&png_ptr, &info_ptr);
        png_free (png_ptr, row_pointers);
 
-       return dcp::Data (state.data, state.size);
+       return dcp::ArrayData (state.data, state.size);
 }
index eab71c2b1db7aace54c4568ed02f3ab012e18dac..ab9b3c78a40f4436527f077b2facd3cb98d883d7 100644 (file)
@@ -31,6 +31,7 @@
 extern "C" {
 #include <libavutil/pixfmt.h>
 }
+#include <dcp/array_data.h>
 #include <dcp/colour_conversion.h>
 #include <boost/shared_ptr.hpp>
 #include <boost/enable_shared_from_this.hpp>
@@ -83,7 +84,7 @@ public:
 
        size_t memory_used () const;
 
-       dcp::Data as_png () const;
+       dcp::ArrayData as_png () const;
 
        void png_error (char const * message);
 
index a42c6182ab5fc37bf758a5c014a145a7def45060..097b9d84b376eba7224af4cbc37a4555e069d025 100644 (file)
@@ -762,15 +762,15 @@ ReelWriter::existing_picture_frame_ok (FILE* asset_file, shared_ptr<InfoFileHand
 
        /* Read the data from the asset and hash it */
        dcpomatic_fseek (asset_file, info.offset, SEEK_SET);
-       Data data (info.size);
-       size_t const read = fread (data.data().get(), 1, data.size(), asset_file);
+       ArrayData data (info.size);
+       size_t const read = fread (data.data(), 1, data.size(), asset_file);
        LOG_GENERAL ("Read %1 bytes of asset data; wanted %2", read, info.size);
        if (read != static_cast<size_t> (data.size ())) {
                LOG_GENERAL ("Existing frame %1 is incomplete", frame);
                ok = false;
        } else {
                Digester digester;
-               digester.add (data.data().get(), data.size());
+               digester.add (data.data(), data.size());
                LOG_GENERAL ("Hash %1 vs %2", digester.get(), info.hash);
                if (digester.get() != info.hash) {
                        LOG_GENERAL ("Existing frame %1 failed hash check", frame);
index d3c56832dfb85a40ccc979b6d85a7b1cdf13779d..793619003aab29cfdee6ec0170ef440c4b63ad3d 100644 (file)
@@ -38,7 +38,7 @@ using std::string;
 using boost::shared_ptr;
 using boost::scoped_array;
 using boost::optional;
-using dcp::Data;
+using dcp::ArrayData;
 using namespace dcpomatic;
 
 StringTextFile::StringTextFile (shared_ptr<const StringTextFileContent> content)
@@ -64,11 +64,11 @@ StringTextFile::StringTextFile (shared_ptr<const StringTextFileContent> content)
        } else {
                /* Text-based file; sort out its character encoding before we try to parse it */
 
-               Data in (content->path (0));
+               ArrayData in (content->path (0));
 
                UErrorCode status = U_ZERO_ERROR;
                UCharsetDetector* detector = ucsdet_open (&status);
-               ucsdet_setText (detector, reinterpret_cast<const char *> (in.data().get()), in.size(), &status);
+               ucsdet_setText (detector, reinterpret_cast<const char *>(in.data()), in.size(), &status);
 
                UCharsetMatch const * match = ucsdet_detect (detector, &status);
                char const * in_charset = ucsdet_getName (match, &status);
@@ -78,7 +78,7 @@ StringTextFile::StringTextFile (shared_ptr<const StringTextFileContent> content)
                scoped_array<uint16_t> utf16 (new uint16_t[in.size() * 2]);
                int const utf16_len = ucnv_toUChars (
                                to_utf16, reinterpret_cast<UChar*>(utf16.get()), in.size() * 2,
-                               reinterpret_cast<const char *> (in.data().get()), in.size(),
+                               reinterpret_cast<const char *>(in.data()), in.size(),
                                &status
                                );
 
index 99a6b4cafedf4d02b74bb48a5d3f8dc7837ce709..b64970f620bf8aa5ac45f842798c22d67350b42f 100644 (file)
@@ -46,7 +46,7 @@ using boost::bind;
 #if BOOST_VERSION >= 106100
 using namespace boost::placeholders;
 #endif
-using dcp::Data;
+using dcp::ArrayData;
 
 static shared_ptr<Film> film;
 static EncodeServerDescription* server;
@@ -63,8 +63,8 @@ process_video (shared_ptr<PlayerVideo> pvf)
 
        ++frame_count;
 
-       Data local_encoded = local->encode_locally ();
-       Data remote_encoded;
+       ArrayData local_encoded = local->encode_locally ();
+       ArrayData remote_encoded;
 
        string remote_error;
        try {
@@ -83,8 +83,8 @@ process_video (shared_ptr<PlayerVideo> pvf)
                return;
        }
 
-       uint8_t* p = local_encoded.data().get ();
-       uint8_t* q = remote_encoded.data().get ();
+       uint8_t* p = local_encoded.data();
+       uint8_t* q = remote_encoded.data();
        for (int i = 0; i < local_encoded.size(); ++i) {
                if (*p++ != *q++) {
                        cout << "\033[0;31mdata differ\033[0m at byte " << i << "\n";
index 75cee85b325e5e4d2b452463af3ca21231edd67f..22eeedbabe3f74f791d4a9126252d23d873ac7d3 100644 (file)
@@ -46,16 +46,16 @@ using boost::shared_ptr;
 using boost::thread;
 using boost::optional;
 using boost::weak_ptr;
-using dcp::Data;
+using dcp::ArrayData;
 
 void
-do_remote_encode (shared_ptr<DCPVideo> frame, EncodeServerDescription description, Data locally_encoded)
+do_remote_encode (shared_ptr<DCPVideo> frame, EncodeServerDescription description, ArrayData locally_encoded)
 {
-       Data remotely_encoded;
+       ArrayData remotely_encoded;
        BOOST_REQUIRE_NO_THROW (remotely_encoded = frame->encode_remotely (description, 1200));
 
        BOOST_REQUIRE_EQUAL (locally_encoded.size(), remotely_encoded.size());
-       BOOST_CHECK_EQUAL (memcmp (locally_encoded.data().get(), remotely_encoded.data().get(), locally_encoded.size()), 0);
+       BOOST_CHECK_EQUAL (memcmp (locally_encoded.data(), remotely_encoded.data(), locally_encoded.size()), 0);
 }
 
 BOOST_AUTO_TEST_CASE (client_server_test_rgb)
@@ -117,7 +117,7 @@ BOOST_AUTO_TEST_CASE (client_server_test_rgb)
                        )
                );
 
-       Data locally_encoded = frame->encode_locally ();
+       ArrayData locally_encoded = frame->encode_locally ();
 
        EncodeServer* server = new EncodeServer (true, 2);
 
@@ -203,7 +203,7 @@ BOOST_AUTO_TEST_CASE (client_server_test_yuv)
                        )
                );
 
-       Data locally_encoded = frame->encode_locally ();
+       ArrayData locally_encoded = frame->encode_locally ();
 
        EncodeServer* server = new EncodeServer (true, 2);
 
@@ -274,7 +274,7 @@ BOOST_AUTO_TEST_CASE (client_server_test_j2k)
                        )
                );
 
-       Data raw_locally_encoded = raw_frame->encode_locally ();
+       ArrayData raw_locally_encoded = raw_frame->encode_locally ();
 
        shared_ptr<PlayerVideo> j2k_pvf (
                new PlayerVideo (
@@ -303,7 +303,7 @@ BOOST_AUTO_TEST_CASE (client_server_test_j2k)
                        )
                );
 
-       Data j2k_locally_encoded = j2k_frame->encode_locally ();
+       ArrayData j2k_locally_encoded = j2k_frame->encode_locally ();
 
        EncodeServer* server = new EncodeServer (true, 2);
 
index e1069b42fa155f63c1a9daffeb9642e3afaad157..26c6748b8bd453bc8a2f472d267469c58d3be03b 100644 (file)
@@ -29,12 +29,12 @@ using std::list;
 
 BOOST_AUTO_TEST_CASE (crypto_test)
 {
-       dcp::Data key (dcpomatic::crypto_key_length());
-       dcp::Data iv = dcpomatic::random_iv ();
+       dcp::ArrayData key (dcpomatic::crypto_key_length());
+       dcp::ArrayData iv = dcpomatic::random_iv ();
 
-       RAND_bytes (key.data().get(), dcpomatic::crypto_key_length());
+       RAND_bytes (key.data(), dcpomatic::crypto_key_length());
 
-       dcp::Data ciphertext = dcpomatic::encrypt ("Can you see any fish?", key, iv);
+       dcp::ArrayData ciphertext = dcpomatic::encrypt ("Can you see any fish?", key, iv);
        BOOST_REQUIRE_EQUAL (dcpomatic::decrypt (ciphertext, key, iv), "Can you see any fish?");
 
        key.data()[5]++;
index 03e8768f406dec95180e7629082c294b0784c823..9ac202b8051c11a305c058fcbc8dd417b7162414 100644 (file)
@@ -687,7 +687,7 @@ write_image (shared_ptr<const Image> image, boost::filesystem::path file)
        png_destroy_write_struct (&png_ptr, &info_ptr);
        png_free (png_ptr, row_pointers);
 
-       dcp::Data(state.data, state.size).write(file);
+       dcp::ArrayData(state.data, state.size).write(file);
 }