1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
/*
Copyright (c) 2004-2015, John Hurst
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*! \file h__Reader.cpp
\version $Id$
\brief MXF file reader base class
*/
#define DEFAULT_MD_DECL
#include "AS_DCP_internal.h"
#include "KLV.h"
using namespace ASDCP;
using namespace ASDCP::MXF;
static Kumu::Mutex sg_DefaultMDInitLock;
static bool sg_DefaultMDTypesInit = false;
static const ASDCP::Dictionary *sg_dict = 0;
//
void
ASDCP::default_md_object_init()
{
if ( ! sg_DefaultMDTypesInit )
{
Kumu::AutoMutex BlockLock(sg_DefaultMDInitLock);
if ( ! sg_DefaultMDTypesInit )
{
sg_dict = &DefaultSMPTEDict();
g_OP1aHeader = new ASDCP::MXF::OP1aHeader(sg_dict);
g_OPAtomIndexFooter = new ASDCP::MXF::OPAtomIndexFooter(sg_dict);
g_RIP = new ASDCP::MXF::RIP(sg_dict);
sg_DefaultMDTypesInit = true;
}
}
}
//------------------------------------------------------------------------------------------
//
//
ASDCP::h__ASDCPReader::h__ASDCPReader(const Dictionary *d, const Kumu::IFileReaderFactory& fileReaderFactory) : MXF::TrackFileReader<OP1aHeader, OPAtomIndexFooter>(d, fileReaderFactory), m_BodyPart(m_Dict) {}
ASDCP::h__ASDCPReader::~h__ASDCPReader() {}
// AS-DCP method of opening an MXF file for read
Result_t
ASDCP::h__ASDCPReader::OpenMXFRead(const std::string& filename)
{
Result_t result = ASDCP::MXF::TrackFileReader<OP1aHeader, OPAtomIndexFooter>::OpenMXFRead(filename);
if ( KM_SUCCESS(result) )
result = ASDCP::MXF::TrackFileReader<OP1aHeader, OPAtomIndexFooter>::InitInfo();
if( KM_SUCCESS(result) )
{
//
m_Info.LabelSetType = LS_MXF_UNKNOWN;
if ( m_HeaderPart.OperationalPattern.MatchExact(MXFInterop_OPAtom_Entry().ul) )
{
m_Info.LabelSetType = LS_MXF_INTEROP;
}
else if ( m_HeaderPart.OperationalPattern.MatchExact(SMPTE_390_OPAtom_Entry().ul) )
{
m_Info.LabelSetType = LS_MXF_SMPTE;
}
else
{
char strbuf[IdentBufferLen];
const MDDEntry* Entry = m_Dict->FindULExact(m_HeaderPart.OperationalPattern.Value());
if ( Entry == 0 )
{
DefaultLogSink().Warn("Operational pattern is not OP-Atom: %s\n",
m_HeaderPart.OperationalPattern.EncodeString(strbuf, IdentBufferLen));
}
else
{
DefaultLogSink().Warn("Operational pattern is not OP-Atom: %s\n", Entry->name);
}
}
if ( !m_RIP.PairArray.empty() && m_RIP.PairArray.front().ByteOffset != 0 )
{
DefaultLogSink().Error("First Partition in RIP is not at offset 0.\n");
result = RESULT_FORMAT;
}
//
if ( m_RIP.PairArray.size() < 2 )
{
// OP-Atom states that there will be either two or three partitions:
// one closed header and one closed footer with an optional body
// SMPTE 429-5 files may have many partitions, see SMPTE ST 410.
DefaultLogSink().Warn("RIP entry count is less than 2: %u\n", m_RIP.PairArray.size());
}
else if ( m_RIP.PairArray.size() > 2 )
{
// if this is a three partition file, go to the body
// partition and read the partition pack
RIP::const_pair_iterator r_i = m_RIP.PairArray.begin();
r_i++;
m_File->Seek((*r_i).ByteOffset);
result = m_BodyPart.InitFromFile(*m_File);
if( ASDCP_FAILURE(result) )
{
DefaultLogSink().Error("ASDCP::h__ASDCPReader::OpenMXFRead, m_BodyPart.InitFromFile failed\n");
}
}
}
if ( KM_SUCCESS(result) )
{
// this position will be at either
// a) the spot in the header partition where essence units appear, or
// b) right after the body partition header (where essence units appear)
m_HeaderPart.BodyOffset = m_File->TellPosition();
result = m_File->Seek(m_HeaderPart.FooterPartition);
if ( ASDCP_SUCCESS(result) )
{
m_IndexAccess.m_Lookup = &m_HeaderPart.m_Primer;
result = m_IndexAccess.InitFromFile(*m_File);
}
}
m_File->Seek(m_HeaderPart.BodyOffset);
return result;
}
// AS-DCP method of reading a plaintext or encrypted frame
Result_t
ASDCP::h__ASDCPReader::ReadEKLVFrame(ui32_t FrameNum, ASDCP::FrameBuffer& FrameBuf,
const byte_t* EssenceUL, AESDecContext* Ctx, HMACContext* HMAC)
{
return ASDCP::MXF::TrackFileReader<OP1aHeader, OPAtomIndexFooter>::ReadEKLVFrame(m_HeaderPart.BodyOffset, FrameNum, FrameBuf,
EssenceUL, Ctx, HMAC);
}
Result_t
ASDCP::h__ASDCPReader::LocateFrame(ui32_t FrameNum, Kumu::fpos_t& streamOffset,
i8_t& temporalOffset, i8_t& keyFrameOffset)
{
return ASDCP::MXF::TrackFileReader<OP1aHeader, OPAtomIndexFooter>::LocateFrame(m_HeaderPart.BodyOffset, FrameNum,
streamOffset, temporalOffset, keyFrameOffset);
}
//------------------------------------------------------------------------------------------
//
//
Result_t
ASDCP::KLReader::ReadKLFromFile(Kumu::IFileReader& Reader)
{
ui32_t read_count;
ui32_t header_length = SMPTE_UL_LENGTH + MXF_BER_LENGTH;
Result_t result = Reader.Read(m_KeyBuf, header_length, &read_count);
if ( ASDCP_FAILURE(result) )
return result;
if ( read_count != header_length )
return RESULT_READFAIL;
const byte_t* ber_start = m_KeyBuf + SMPTE_UL_LENGTH;
if ( ( *ber_start & 0x80 ) == 0 )
{
DefaultLogSink().Error("BER encoding error.\n");
return RESULT_FORMAT;
}
ui8_t ber_size = ( *ber_start & 0x0f ) + 1;
if ( ber_size > 9 )
{
DefaultLogSink().Error("BER size encoding error.\n");
return RESULT_FORMAT;
}
if ( ber_size < MXF_BER_LENGTH )
{
DefaultLogSink().Error("BER size %d shorter than AS-DCP/AS-02 minimum %d.\n",
ber_size, MXF_BER_LENGTH);
return RESULT_FORMAT;
}
if ( ber_size > MXF_BER_LENGTH )
{
ui32_t diff = ber_size - MXF_BER_LENGTH;
assert((SMPTE_UL_LENGTH + MXF_BER_LENGTH + diff) <= (SMPTE_UL_LENGTH * 2));
result = Reader.Read(m_KeyBuf + SMPTE_UL_LENGTH + MXF_BER_LENGTH, diff, &read_count);
if ( ASDCP_FAILURE(result) )
return result;
if ( read_count != diff )
return RESULT_READFAIL;
header_length += diff;
}
return InitFromBuffer(m_KeyBuf, header_length);
}
//------------------------------------------------------------------------------------------
//
// base subroutine for reading a KLV packet, assumes file position is at the first byte of the packet
Result_t
ASDCP::Read_EKLV_Packet(Kumu::IFileReader& File, const ASDCP::Dictionary& Dict,
const ASDCP::WriterInfo& Info, Kumu::fpos_t& LastPosition, ASDCP::FrameBuffer& CtFrameBuf,
ui32_t FrameNum, ui32_t SequenceNum, ASDCP::FrameBuffer& FrameBuf,
const byte_t* EssenceUL, AESDecContext* Ctx, HMACContext* HMAC)
{
KLReader Reader;
Result_t result = Reader.ReadKLFromFile(File);
if ( KM_FAILURE(result) )
return result;
UL Key(Reader.Key());
ui64_t PacketLength = Reader.Length();
LastPosition = LastPosition + Reader.KLLength() + PacketLength;
if ( Key.MatchIgnoreStream(Dict.ul(MDD_CryptEssence)) ) // ignore the stream numbers
{
if ( ! Info.EncryptedEssence )
{
DefaultLogSink().Error("EKLV packet found, no Cryptographic Context in header.\n");
return RESULT_FORMAT;
}
// read encrypted triplet value into internal buffer
assert(PacketLength <= 0xFFFFFFFFL);
CtFrameBuf.Capacity((ui32_t) PacketLength);
ui32_t read_count;
result = File.Read(CtFrameBuf.Data(), (ui32_t) PacketLength, &read_count);
if ( ASDCP_FAILURE(result) )
return result;
if ( read_count != PacketLength )
{
DefaultLogSink().Error("read length is smaller than EKLV packet length.\n");
return RESULT_FORMAT;
}
CtFrameBuf.Size((ui32_t) PacketLength);
// should be const but mxflib::ReadBER is not
byte_t* ess_p = CtFrameBuf.Data();
// read context ID length
if ( ! Kumu::read_test_BER(&ess_p, UUIDlen) )
return RESULT_FORMAT;
// test the context ID
if ( memcmp(ess_p, Info.ContextID, UUIDlen) != 0 )
{
DefaultLogSink().Error("Packet's Cryptographic Context ID does not match the header.\n");
return RESULT_FORMAT;
}
ess_p += UUIDlen;
// read PlaintextOffset length
if ( ! Kumu::read_test_BER(&ess_p, sizeof(ui64_t)) )
return RESULT_FORMAT;
ui32_t PlaintextOffset = (ui32_t)KM_i64_BE(Kumu::cp2i<ui64_t>(ess_p));
ess_p += sizeof(ui64_t);
// read essence UL length
if ( ! Kumu::read_test_BER(&ess_p, SMPTE_UL_LENGTH) )
return RESULT_FORMAT;
// test essence UL
if ( ! UL(ess_p).MatchIgnoreStream(EssenceUL) ) // ignore the stream number
{
char strbuf[IntBufferLen];
const MDDEntry* Entry = Dict.FindULAnyVersion(Key.Value());
if ( Entry == 0 )
{
DefaultLogSink().Warn("Unexpected Essence UL found: %s.\n", Key.EncodeString(strbuf, IntBufferLen));
}
else
{
DefaultLogSink().Warn("Unexpected Essence UL found: %s.\n", Entry->name);
}
return RESULT_FORMAT;
}
ess_p += SMPTE_UL_LENGTH;
// read SourceLength length
if ( ! Kumu::read_test_BER(&ess_p, sizeof(ui64_t)) )
return RESULT_FORMAT;
ui32_t SourceLength = (ui32_t)KM_i64_BE(Kumu::cp2i<ui64_t>(ess_p));
ess_p += sizeof(ui64_t);
assert(SourceLength);
if ( FrameBuf.Capacity() < SourceLength )
{
DefaultLogSink().Error("FrameBuf.Capacity: %u SourceLength: %u\n", FrameBuf.Capacity(), SourceLength);
return RESULT_SMALLBUF;
}
ui32_t esv_length = calc_esv_length(SourceLength, PlaintextOffset);
// read ESV length
if ( ! Kumu::read_test_BER(&ess_p, esv_length) )
{
DefaultLogSink().Error("read_test_BER did not return %u\n", esv_length);
return RESULT_FORMAT;
}
ui32_t tmp_len = esv_length + (Info.UsesHMAC ? klv_intpack_size : 0);
if ( PacketLength < tmp_len )
{
DefaultLogSink().Error("Frame length is larger than EKLV packet length.\n");
return RESULT_FORMAT;
}
#ifdef HAVE_OPENSSL
if ( Ctx )
{
// wrap the pointer and length as a FrameBuffer for use by
// DecryptFrameBuffer() and TestValues()
FrameBuffer TmpWrapper;
TmpWrapper.SetData(ess_p, tmp_len);
TmpWrapper.Size(tmp_len);
TmpWrapper.SourceLength(SourceLength);
TmpWrapper.PlaintextOffset(PlaintextOffset);
result = DecryptFrameBuffer(TmpWrapper, FrameBuf, Ctx);
FrameBuf.FrameNumber(FrameNum);
// detect and test integrity pack
if ( ASDCP_SUCCESS(result) && Info.UsesHMAC && HMAC )
{
IntegrityPack IntPack;
result = IntPack.TestValues(TmpWrapper, Info.AssetUUID, SequenceNum, HMAC);
}
}
else // return ciphertext to caller
#endif //HAVE_OPENSSL
{
if ( FrameBuf.Capacity() < tmp_len )
{
char intbuf[IntBufferLen];
DefaultLogSink().Error("FrameBuf.Capacity: %u FrameLength: %s\n",
FrameBuf.Capacity(), ui64sz(PacketLength, intbuf));
return RESULT_SMALLBUF;
}
memcpy(FrameBuf.Data(), ess_p, tmp_len);
FrameBuf.Size(tmp_len);
FrameBuf.FrameNumber(FrameNum);
FrameBuf.SourceLength(SourceLength);
FrameBuf.PlaintextOffset(PlaintextOffset);
}
}
else if ( Key.MatchIgnoreStream(EssenceUL) ) // ignore the stream number
{ // read plaintext frame
if ( FrameBuf.Capacity() < PacketLength )
{
char intbuf[IntBufferLen];
DefaultLogSink().Error("FrameBuf.Capacity: %u FrameLength: %s\n",
FrameBuf.Capacity(), ui64sz(PacketLength, intbuf));
return RESULT_SMALLBUF;
}
// read the data into the supplied buffer
ui32_t read_count;
assert(PacketLength <= 0xFFFFFFFFL);
result = File.Read(FrameBuf.Data(), (ui32_t) PacketLength, &read_count);
if ( ASDCP_FAILURE(result) )
return result;
if ( read_count != PacketLength )
{
char intbuf1[IntBufferLen];
char intbuf2[IntBufferLen];
DefaultLogSink().Error("read_count: %s != FrameLength: %s\n",
ui64sz(read_count, intbuf1),
ui64sz(PacketLength, intbuf2) );
return RESULT_READFAIL;
}
FrameBuf.FrameNumber(FrameNum);
FrameBuf.Size(read_count);
}
else
{
char strbuf[IntBufferLen];
const MDDEntry* Entry = Dict.FindULAnyVersion(Key.Value());
if ( Entry == 0 )
{
DefaultLogSink().Warn("Unexpected Essence UL found: %s.\n", Key.EncodeString(strbuf, IntBufferLen));
}
else
{
DefaultLogSink().Warn("Unexpected Essence UL found: %s.\n", Entry->name);
}
return RESULT_FORMAT;
}
return result;
}
//
// end h__Reader.cpp
//
|