Remove debug.
[libdcp.git] / src / verify.cc
1 /*
2     Copyright (C) 2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 #include "verify.h"
35 #include "dcp.h"
36 #include "cpl.h"
37 #include "reel.h"
38 #include "reel_picture_asset.h"
39 #include "reel_sound_asset.h"
40 #include "exceptions.h"
41 #include <boost/foreach.hpp>
42 #include <list>
43 #include <vector>
44 #include <iostream>
45
46 using std::list;
47 using std::vector;
48 using std::string;
49 using std::cout;
50 using boost::shared_ptr;
51 using boost::optional;
52 using boost::function;
53
54 using namespace dcp;
55
56 enum Result {
57         RESULT_GOOD,
58         RESULT_CPL_PKL_DIFFER,
59         RESULT_BAD
60 };
61
62 static Result
63 verify_asset (shared_ptr<DCP> dcp, shared_ptr<ReelAsset> reel_asset, function<void (float)> progress)
64 {
65         string const actual_hash = reel_asset->asset_ref()->hash(progress);
66
67         shared_ptr<PKL> pkl = dcp->pkl();
68         /* We've read this DCP in so it must have a PKL */
69         DCP_ASSERT (pkl);
70
71         shared_ptr<Asset> asset = reel_asset->asset_ref().asset();
72         string const pkl_hash = pkl->hash (reel_asset->asset_ref()->id());
73
74         optional<string> cpl_hash = reel_asset->hash();
75         if (cpl_hash && *cpl_hash != pkl_hash) {
76                 return RESULT_CPL_PKL_DIFFER;
77         }
78
79         if (actual_hash != pkl_hash) {
80                 return RESULT_BAD;
81         }
82
83         return RESULT_GOOD;
84 }
85
86 list<VerificationNote>
87 dcp::verify (vector<boost::filesystem::path> directories, function<void (string, optional<boost::filesystem::path>)> stage, function<void (float)> progress)
88 {
89         list<VerificationNote> notes;
90
91         list<shared_ptr<DCP> > dcps;
92         BOOST_FOREACH (boost::filesystem::path i, directories) {
93                 dcps.push_back (shared_ptr<DCP> (new DCP (i)));
94         }
95
96         BOOST_FOREACH (shared_ptr<DCP> dcp, dcps) {
97                 stage ("Checking DCP", dcp->directory());
98                 DCP::ReadErrors errors;
99                 try {
100                         dcp->read (true, &errors);
101                 } catch (DCPReadError& e) {
102                         notes.push_back (VerificationNote (VerificationNote::VERIFY_ERROR, e.what ()));
103                 } catch (XMLError& e) {
104                         notes.push_back (VerificationNote (VerificationNote::VERIFY_ERROR, e.what ()));
105                 }
106
107                 BOOST_FOREACH (shared_ptr<CPL> cpl, dcp->cpls()) {
108                         stage ("Checking CPL", cpl->file());
109                         BOOST_FOREACH (shared_ptr<Reel> reel, cpl->reels()) {
110                                 stage ("Checking reel", optional<boost::filesystem::path>());
111                                 if (reel->main_picture()) {
112                                         stage ("Checking picture asset hash", reel->main_picture()->asset()->file());
113                                         Result const r = verify_asset (dcp, reel->main_picture(), progress);
114                                         switch (r) {
115                                         case RESULT_BAD:
116                                                 notes.push_back (VerificationNote (VerificationNote::VERIFY_ERROR, "Picture asset hash is incorrect."));
117                                                 break;
118                                         case RESULT_CPL_PKL_DIFFER:
119                                                 notes.push_back (VerificationNote (VerificationNote::VERIFY_ERROR, "PKL and CPL hashes differ for picture asset."));
120                                                 break;
121                                         default:
122                                                 break;
123                                         }
124                                 }
125                                 if (reel->main_sound()) {
126                                         stage ("Checking sound asset hash", reel->main_sound()->asset()->file());
127                                         Result const r = verify_asset (dcp, reel->main_sound(), progress);
128                                         switch (r) {
129                                         case RESULT_BAD:
130                                                 notes.push_back (VerificationNote (VerificationNote::VERIFY_ERROR, "Sound asset hash is incorrect."));
131                                                 break;
132                                         case RESULT_CPL_PKL_DIFFER:
133                                                 notes.push_back (VerificationNote (VerificationNote::VERIFY_ERROR, "PKL and CPL hashes differ for sound asset."));
134                                                 break;
135                                         default:
136                                                 break;
137                                         }
138                                 }
139                         }
140                 }
141         }
142
143         return notes;
144 }