Debian Codegraph @

bento4-embedded/Source/C++/Apps/Mp4DcfPackager/Mp4DcfPackager.cpp

raw
1 /*****************************************************************
2 |
3 | AP4 - MP4 DCF Packager
4 |
5 | Copyright 2002-2008 Axiomatic Systems, LLC
6 |
7 |
8 | This file is part of Bento4/AP4 (MP4 Atom Processing Library).
9 |
10 | Unless you have obtained Bento4 under a difference license,
11 | this version of Bento4 is Bento4|GPL.
12 | Bento4|GPL is free software; you can redistribute it and/or modify
13 | it under the terms of the GNU General Public License as published by
14 | the Free Software Foundation; either version 2, or (at your option)
15 | any later version.
16 |
17 | Bento4|GPL is distributed in the hope that it will be useful,
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 | GNU General Public License for more details.
21 |
22 | You should have received a copy of the GNU General Public License
23 | along with Bento4|GPL; see the file COPYING. If not, write to the
24 | Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 | 02111-1307, USA.
26 |
27 ****************************************************************/
28
29 /*----------------------------------------------------------------------
30 | includes
31 +---------------------------------------------------------------------*/
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #include "Ap4.h"
36
37 /*----------------------------------------------------------------------
38 | constants
39 +---------------------------------------------------------------------*/
40 #define BANNER "MP4 DCF Packager - Version 1.0.1\n"\
41 "(Bento4 Version " AP4_VERSION_STRING ")\n"\
42 "(c) 2002-2009 Axiomatic Systems, LLC"
43
44 /*----------------------------------------------------------------------
45 | PrintUsageAndExit
46 +---------------------------------------------------------------------*/
47 static void
48 PrintUsageAndExit()
49 {
50 fprintf(stderr,
51 BANNER
52 "\n\n"
53 "usage: mp4dcfpackager --method <method> [options] <input> <output>\n"
54 " --method: <method> is NULL, CBC or CTR\n"
55 " Options:\n"
56 " --show-progress: show progress details\n"
57 " --content-type: content MIME type\n"
58 " --content-id: content ID\n"
59 " --rights-issuer: rights issuer URL\n"
60 " --key <k>:<iv>\n"
61 " Specifies the key to use for encryption.\n"
62 " <k> a 128-bit key in hex (32 characters)\n"
63 " and <iv> a 128-bit IV or salting key in hex (32 characters)\n"
64 " --textual-header <name>:<value>\n"
65 " Specifies a textual header where <name> is the header name,\n"
66 " and <value> is the header value\n"
67 " (several --textual-header options can be used)\n"
68 "\n"
69 );
70 exit(1);
71 }
72
73 /*----------------------------------------------------------------------
74 | main
75 +---------------------------------------------------------------------*/
76 int
77 main(int argc, char** argv)
78 {
79 if (argc == 1) PrintUsageAndExit();
80
81 // parse options
82 AP4_UI08 encryption_method = 0;
83 bool encryption_method_is_set = false;
84 AP4_UI08 padding_scheme = 0;
85 const char* input_filename = NULL;
86 const char* output_filename = NULL;
87 bool show_progress = false;
88 bool key_is_set = false;
89 unsigned char key[16];
90 unsigned char iv[16];
91 AP4_BlockCipher::CipherMode cipher_mode = AP4_BlockCipher::CBC;
92 const char* content_type = "";
93 const char* content_id = "";
94 const char* rights_issuer_url = "";
95 AP4_LargeSize plaintext_length = 0;
96 AP4_DataBuffer textual_headers_buffer;
97 AP4_TrackPropertyMap textual_headers;
98
99 AP4_SetMemory(key, 0, sizeof(key));
100 AP4_SetMemory(iv, 0, sizeof(iv));
101
102 // parse the command line arguments
103 char* arg;
104 while ((arg = *++argv)) {
105 if (!strcmp(arg, "--method")) {
106 arg = *++argv;
107 if (!strcmp(arg, "CBC")) {
108 encryption_method = AP4_OMA_DCF_ENCRYPTION_METHOD_AES_CBC;
109 encryption_method_is_set = true;
110 padding_scheme = AP4_OMA_DCF_PADDING_SCHEME_RFC_2630;
111 cipher_mode = AP4_BlockCipher::CBC;
112 } else if (!strcmp(arg, "CTR")) {
113 encryption_method = AP4_OMA_DCF_ENCRYPTION_METHOD_AES_CTR;
114 encryption_method_is_set = true;
115 padding_scheme = AP4_OMA_DCF_PADDING_SCHEME_NONE;
116 cipher_mode = AP4_BlockCipher::CTR;
117 } else if (!strcmp(arg, "NULL")) {
118 encryption_method = AP4_OMA_DCF_ENCRYPTION_METHOD_NULL;
119 encryption_method_is_set = true;
120 padding_scheme = AP4_OMA_DCF_PADDING_SCHEME_NONE;
121 } else {
122 fprintf(stderr, "ERROR: invalid value for --method argument\n");
123 return 1;
124 }
125 } else if (!strcmp(arg, "--show-progress")) {
126 show_progress = true;
127 } else if (!strcmp(arg, "--content-type")) {
128 content_type = *++argv;
129 if (content_type == NULL) {
130 fprintf(stderr, "ERROR: missing argument for --content-type option\n");
131 return 1;
132 }
133 } else if (!strcmp(arg, "--content-id")) {
134 content_id = *++argv;
135 if (content_id == NULL) {
136 fprintf(stderr, "ERROR: missing argument for --content-id option\n");
137 return 1;
138 }
139 } else if (!strcmp(arg, "--rights-issuer")) {
140 rights_issuer_url = *++argv;
141 if (rights_issuer_url == NULL) {
142 fprintf(stderr, "ERROR: missing argument for --rights-issuer option\n");
143 return 1;
144 }
145 } else if (!strcmp(arg, "--key")) {
146 if (!encryption_method_is_set) {
147 fprintf(stderr, "ERROR: --method argument must appear before --key\n");
148 return 1;
149 } else if (encryption_method == AP4_OMA_DCF_ENCRYPTION_METHOD_NULL) {
150 fprintf(stderr, "ERROR: --key cannot be used with --method NULL\n");
151 return 1;
152 }
153 arg = *++argv;
154 if (arg == NULL) {
155 fprintf(stderr, "ERROR: missing argument for --key option\n");
156 return 1;
157 }
158 char* key_ascii = NULL;
159 char* iv_ascii = NULL;
160 if (AP4_FAILED(AP4_SplitArgs(arg, key_ascii, iv_ascii))) {
161 fprintf(stderr, "ERROR: invalid argument for --key option\n");
162 return 1;
163 }
164 if (AP4_ParseHex(key_ascii, key, 16)) {
165 fprintf(stderr, "ERROR: invalid hex format for key\n");
166 }
167 if (AP4_ParseHex(iv_ascii, iv, 16)) {
168 fprintf(stderr, "ERROR: invalid hex format for iv\n");
169 return 1;
170 }
171 // check that the key is not already there
172 if (key_is_set) {
173 fprintf(stderr, "ERROR: key already set\n");
174 return 1;
175 }
176 key_is_set = true;
177 } else if (!strcmp(arg, "--textual-header")) {
178 char* name = NULL;
179 char* value = NULL;
180 arg = *++argv;
181 if (arg == NULL) {
182 fprintf(stderr, "ERROR: missing argument for --textual-header option\n");
183 return 1;
184 }
185 if (AP4_FAILED(AP4_SplitArgs(arg, name, value))) {
186 fprintf(stderr, "ERROR: invalid argument for --textual-header option\n");
187 return 1;
188 }
189
190 // check that the property is not already set
191 if (textual_headers.GetProperty(0, name)) {
192 fprintf(stderr, "ERROR: textual header %s already set\n", name);
193 return 1;
194 }
195 // set the property in the map
196 textual_headers.SetProperty(0, name, value);
197 } else if (input_filename == NULL) {
198 input_filename = arg;
199 } else if (output_filename == NULL) {
200 output_filename = arg;
201 } else {
202 fprintf(stderr, "ERROR: unexpected argument (%s)\n", arg);
203 return 1;
204 }
205 }
206
207 // check the arguments
208 if (!encryption_method_is_set) {
209 fprintf(stderr, "ERROR: missing --method argument\n");
210 return 1;
211 }
212 if (!key_is_set) {
213 fprintf(stderr, "ERROR: encryption key not specified\n");
214 return 1;
215 }
216 if (input_filename == NULL) {
217 fprintf(stderr, "ERROR: missing input filename\n");
218 return 1;
219 }
220 if (output_filename == NULL) {
221 fprintf(stderr, "ERROR: missing output filename\n");
222 return 1;
223 }
224 (void)show_progress; // avoid warnings
225
226 // convert to a textual headers buffer
227 textual_headers.GetTextualHeaders(0, textual_headers_buffer);
228
229 // create the input stream
230 AP4_Result result;
231 AP4_ByteStream* input = NULL;
232 result = AP4_FileByteStream::Create(input_filename, AP4_FileByteStream::STREAM_MODE_READ, input);
233 if (AP4_FAILED(result)) {
234 fprintf(stderr, "ERROR: cannot open input file (%s) %d\n", input_filename, result);
235 return 1;
236 }
237
238 // get the size of the input
239 result = input->GetSize(plaintext_length);
240 if (AP4_FAILED(result)) {
241 fprintf(stderr, "ERROR: cannot get the size of the input\n");
242 return 1;
243 }
244
245 // create an encrypting stream for the input
246 AP4_ByteStream* encrypted_stream;
247 if (encryption_method == AP4_OMA_DCF_ENCRYPTION_METHOD_NULL) {
248 encrypted_stream = input;
249 } else {
250 result = AP4_EncryptingStream::Create(cipher_mode, *input, iv, 16, key, 16, true, &AP4_DefaultBlockCipherFactory::Instance, encrypted_stream);
251 if (AP4_FAILED(result)) {
252 fprintf(stderr, "ERROR: failed to create cipher (%d)\n", result);
253 return 1;
254 }
255 }
256
257 // create the output stream
258 AP4_ByteStream* output = NULL;
259 result = AP4_FileByteStream::Create(output_filename, AP4_FileByteStream::STREAM_MODE_WRITE, output);
260 if (AP4_FAILED(result)) {
261 fprintf(stderr, "ERROR: cannot open output file (%s) %d\n", output_filename, result);
262 return 1;
263 }
264
265 // create the file
266 AP4_File file;
267
268 // set the brand
269 AP4_UI32 compatible_brands[1] = {AP4_OMA_DCF_BRAND_ODCF};
270 file.SetFileType(AP4_OMA_DCF_BRAND_ODCF, 2, compatible_brands, 1);
271
272 // create the odrm atom (force a 64-bit size)
273 AP4_ContainerAtom* odrm = new AP4_ContainerAtom(AP4_ATOM_TYPE_ODRM, AP4_FULL_ATOM_HEADER_SIZE_64, true, 0, 0);
274
275 // create the ohdr atom
276 AP4_OhdrAtom* ohdr = new AP4_OhdrAtom(encryption_method,
277 padding_scheme,
278 plaintext_length,
279 content_id,
280 rights_issuer_url,
281 textual_headers_buffer.GetData(),
282 textual_headers_buffer.GetDataSize());
283
284 // create the odhe atom (the ownership is transfered)
285 AP4_OdheAtom* odhe = new AP4_OdheAtom(content_type, ohdr);
286 odrm->AddChild(odhe);
287
288 // create the odda atom
289 AP4_OddaAtom* odda = new AP4_OddaAtom(*encrypted_stream);
290 odrm->AddChild(odda);
291
292 // add the odrm atom to the file (the owndership is transfered)
293 file.GetTopLevelAtoms().Add(odrm);
294
295 // write the file to the output
296 AP4_FileWriter::Write(file, *output);
297
298 // cleanup
299 input->Release();
300 output->Release();
301
302 return 0;
303 }