Add new write() method.
[libdcp.git] / tools / dcpdumpsub.cc
1 /*
2     Copyright (C) 2015 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 "smpte_subtitle_asset.h"
35 #include "decrypted_kdm.h"
36 #include "decrypted_kdm_key.h"
37 #include "encrypted_kdm.h"
38 #include "util.h"
39 #include <getopt.h>
40 #include <cstdlib>
41 #include <string>
42 #include <iostream>
43
44 using std::string;
45 using std::cout;
46 using std::cerr;
47 using std::map;
48 using boost::optional;
49
50 static void
51 help (string n)
52 {
53         cerr << "Syntax: " << n << " [OPTION] <MXF>\n"
54              << "  -h, --help         show this help\n"
55              << "  -n, --no-fonts     don't extract fonts\n"
56              << "  -k, --kdm          KDM file\n"
57              << "  -p, --private-key  private key file\n";
58 }
59
60 int
61 main (int argc, char* argv[])
62 {
63         dcp::init ();
64
65         bool extract_fonts = true;
66         optional<boost::filesystem::path> kdm_file;
67         optional<boost::filesystem::path> private_key_file;
68
69         int option_index = 0;
70         while (1) {
71                 static struct option long_options[] = {
72                         { "help", no_argument, 0, 'h'},
73                         { "no-fonts", no_argument, 0, 'n'},
74                         { "kdm", required_argument, 0, 'k'},
75                         { "private-key", required_argument, 0, 'p'},
76                         { 0, 0, 0, 0 }
77                 };
78
79                 int c = getopt_long (argc, argv, "hnk:p:", long_options, &option_index);
80
81                 if (c == -1) {
82                         break;
83                 }
84
85                 switch (c) {
86                 case 'h':
87                         help (argv[0]);
88                         exit (EXIT_SUCCESS);
89                 case 'n':
90                         extract_fonts = false;
91                         break;
92                 case 'k':
93                         kdm_file = optarg;
94                         break;
95                 case 'p':
96                         private_key_file = optarg;
97                         break;
98                 }
99         }
100
101         if (argc <= optind || argc > (optind + 2)) {
102                 help (argv[0]);
103                 exit (EXIT_FAILURE);
104         }
105
106         dcp::SMPTESubtitleAsset sub (argv[optind]);
107
108         if (sub.key_id() && (!kdm_file || !private_key_file)) {
109                 cerr << "Subtitle MXF is encrypted so you must provide a KDM and private key.\n";
110                 exit (EXIT_FAILURE);
111         }
112
113         if (sub.key_id()) {
114                 dcp::EncryptedKDM encrypted_kdm (dcp::file_to_string (kdm_file.get ()));
115                 dcp::DecryptedKDM decrypted_kdm (encrypted_kdm, dcp::file_to_string (private_key_file.get()));
116                 bool done = false;
117                 for (auto const& i: decrypted_kdm.keys()) {
118                         if (i.id() == *sub.key_id()) {
119                                 sub.set_key (i.key ());
120                                 done = true;
121                         }
122                 }
123                 if (!done) {
124                         cerr << "Could not find required key in KDM.\n";
125                         exit (EXIT_FAILURE);
126                 }
127         }
128
129         cout << sub.xml_as_string() << "\n";
130
131         if (extract_fonts) {
132                 map<string, dcp::ArrayData> fonts = sub.font_data ();
133                 for (map<string, dcp::ArrayData>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
134                         FILE* f = dcp::fopen_boost (i->first + ".ttf", "wb");
135                         if (!f) {
136                                 cerr << "Could not open font file " << i->first << ".ttf for writing";
137                                 exit (EXIT_FAILURE);
138                         }
139                         fwrite (i->second.data(), 1, i->second.size(), f);
140                         fclose (f);
141                 }
142         }
143 }