summaryrefslogtreecommitdiff
path: root/src/KLV.cpp
blob: c29eb8cde28c1b89433eac31bc5e311510898fa9 (plain)
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
/*
Copyright (c) 2005-2021, 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    KLV.cpp
  \version $Id$
  \brief   KLV objects
*/

#include "KLV.h"
#include <KM_log.h>
using Kumu::DefaultLogSink;


// This is how much we read when we're reading from a file and we don't know
// how long the packet is. This gives us the K (16 bytes) and L (4-9 bytes)
// and the remaining bytes for an even read (tmp_read_size % 16 == 0)
const ui32_t kl_length = ASDCP::SMPTE_UL_LENGTH + ASDCP::MXF_BER_LENGTH;
const ui32_t tmp_read_size = 32;

//------------------------------------------------------------------------------------------
//

// 
ASDCP::Result_t
ASDCP::KLVPacket::InitFromBuffer(const byte_t* buf, ui32_t buf_len, const UL& label)
{
  Result_t result = KLVPacket::InitFromBuffer(buf, buf_len);

  if ( ASDCP_SUCCESS(result) )
    result = ( UL(m_KeyStart) == label ) ? RESULT_OK : RESULT_FAIL;

  return result;
}

//
ASDCP::UL
ASDCP::KLVPacket::GetUL()
{
  if ( m_KeyStart != 0 )
    return UL(m_KeyStart);

  return m_UL;
}

//
bool
ASDCP::KLVPacket::SetUL(const UL& new_ul)
{
  if ( m_KeyStart != 0 )
    return false;

  m_UL = new_ul;
  return true;
}

//
ASDCP::Result_t
ASDCP::KLVPacket::InitFromBuffer(const byte_t* buf, ui32_t buf_len)
{
  m_KeyStart = m_ValueStart = 0;
  m_KLLength = m_ValueLength = 0;

  if ( memcmp(buf, SMPTE_UL_START, 4) != 0 )
    {
      DefaultLogSink().Error("Unexpected UL preamble: %02x.%02x.%02x.%02x\n",
			     buf[0], buf[1], buf[2], buf[3]);
      return RESULT_FAIL;
    }

  ui32_t ber_len = Kumu::BER_length(buf + SMPTE_UL_LENGTH);

  if ( ber_len > ( buf_len - SMPTE_UL_LENGTH ) )
    {
      DefaultLogSink().Error("BER encoding length exceeds buffer size.\n");
      return RESULT_FAIL;
    }

  if ( ber_len == 0 )
    {
      DefaultLogSink().Error("KLV format error, zero BER length not allowed.\n");
      return RESULT_FAIL;
    }

  ui64_t tmp_size;
  if ( ! Kumu::read_BER(buf + SMPTE_UL_LENGTH, &tmp_size) )
    {
      DefaultLogSink().Error("KLV format error, BER decode failure.\n");
      return RESULT_FAIL;
    }

  m_ValueLength = tmp_size;
  m_KLLength = SMPTE_UL_LENGTH + Kumu::BER_length(buf + SMPTE_UL_LENGTH);
  m_KeyStart = buf;
  m_ValueStart = buf + m_KLLength;
  return RESULT_OK;
}

//
bool
ASDCP::KLVPacket::HasUL(const byte_t* ul)
{
  if ( m_KeyStart != 0 )
    {
      return UL(ul) == UL(m_KeyStart);
    }

  if ( m_UL.HasValue() )
    {
      return UL(ul) == m_UL;
    }

  return false;
}

//
ASDCP::Result_t
ASDCP::KLVPacket::WriteKLToBuffer(ASDCP::FrameBuffer& Buffer, const UL& label, ui32_t length)
{
  assert(label.HasValue());

  if ( Buffer.Size() + kl_length > Buffer.Capacity() )
    {
      DefaultLogSink().Error("Small write buffer\n");
      return RESULT_FAIL;
    }
  
  memcpy(Buffer.Data() + Buffer.Size(), label.Value(), label.Size());

  if ( ! Kumu::write_BER(Buffer.Data() + Buffer.Size() + SMPTE_UL_LENGTH, length, MXF_BER_LENGTH) )
    return RESULT_FAIL;

  Buffer.Size(Buffer.Size() + kl_length);
  return RESULT_OK;
}

//
void
ASDCP::KLVPacket::Dump(FILE* stream, const Dictionary& Dict, bool show_value)
{
  char buf[64];

  if ( stream == 0 )
    stream = stderr;

  if ( m_KeyStart != 0 )
    {
      UL TmpUL(m_KeyStart);
      fprintf(stream, "%s", TmpUL.EncodeString(buf, 64));

      const MDDEntry* Entry = Dict.FindULAnyVersion(m_KeyStart);
      fprintf(stream, "  len: %7llu (%s)\n", m_ValueLength, (Entry ? Entry->name : "Unknown"));

      if ( m_ValueStart && show_value && m_ValueLength < 1000 )
	Kumu::hexdump(m_ValueStart, Kumu::xmin(m_ValueLength, (ui64_t)128), stream);
    }
  else if ( m_UL.HasValue() )
    {
      fprintf(stream, "%s\n", m_UL.EncodeString(buf, 64));
    }
  else
    {
      fprintf(stream, "*** Malformed KLV packet ***\n");
    }
}

// 
ASDCP::Result_t
ASDCP::KLVFilePacket::InitFromFile(const Kumu::IFileReader& Reader, const UL& label)
{
  Result_t result = KLVFilePacket::InitFromFile(Reader);

  if ( ASDCP_SUCCESS(result) )
    result = ( UL(m_KeyStart) == label ) ? RESULT_OK : RESULT_FAIL;

  return result;
}

// TODO: refactor to use InitFromBuffer
ASDCP::Result_t
ASDCP::KLVFilePacket::InitFromFile(const Kumu::IFileReader& Reader)
{
  ui32_t read_count;
  byte_t tmp_data[tmp_read_size];
  ui64_t tmp_size;
  m_KeyStart = m_ValueStart = 0;
  m_KLLength = m_ValueLength = 0;
  m_Buffer.Size(0);

  Result_t result = Reader.Read(tmp_data, tmp_read_size, &read_count);

  if ( ASDCP_FAILURE(result) )
    return result;

  if ( read_count < (SMPTE_UL_LENGTH + 1) )
    {
      DefaultLogSink().Error("Short read of Key and Length got %u\n", read_count);
      return RESULT_READFAIL;
    }

  if ( memcmp(tmp_data, SMPTE_UL_START, 4) != 0 )
    {
      DefaultLogSink().Error("Unexpected UL preamble: %02x.%02x.%02x.%02x\n",
			     tmp_data[0], tmp_data[1], tmp_data[2], tmp_data[3]);
      return RESULT_FAIL;
    }

  if ( ! Kumu::read_BER(tmp_data + SMPTE_UL_LENGTH, &tmp_size) )
    {
      DefaultLogSink().Error("BER Length decoding error\n");
      return RESULT_FAIL;
    }

  ui32_t ber_len = Kumu::BER_length(tmp_data + SMPTE_UL_LENGTH);
  m_KLLength = SMPTE_UL_LENGTH + ber_len;

  if ( tmp_size > MAX_KLV_PACKET_LENGTH )
    {
      result = Reader.Seek(tmp_size - (tmp_read_size - SMPTE_UL_LENGTH - ber_len), Kumu::SP_POS);

      if ( ASDCP_SUCCESS(result) )
        {
          memcpy(m_Buffer.Data(), tmp_data, SMPTE_UL_LENGTH);
          m_KeyStart = m_Buffer.Data();

          Kumu::ui64Printer tmp_size_str(tmp_size);
          DefaultLogSink().Error("Packet length %s exceeds internal limit.\n", tmp_size_str.c_str());
          result = RESULT_ALLOC;
        }

      return result;
    }

  ui32_t remainder = 0;
  assert(tmp_size <= 0xFFFFFFFFL);
  m_ValueLength = (ui32_t) tmp_size;
  ui32_t packet_length = m_ValueLength + m_KLLength;

  result = m_Buffer.Capacity(packet_length);

  if ( ASDCP_FAILURE(result) )
    return result;

  m_KeyStart = m_Buffer.Data();
  m_ValueStart = m_Buffer.Data() + m_KLLength;
  m_Buffer.Size(packet_length);

  // is the whole packet in the tmp buf?
  if ( packet_length <= tmp_read_size )
    {
      assert(packet_length <= read_count);
      memcpy(m_Buffer.Data(), tmp_data, packet_length);

      if ( (remainder = read_count - packet_length) != 0 )
	{
	  DefaultLogSink().Warn("Repositioning pointer for short packet\n");
	  Kumu::fpos_t pos = Reader.TellPosition();
	  assert(pos > remainder);
	  result = Reader.Seek(pos - remainder);
	}
    }
  else
    {
      if ( read_count < tmp_read_size )
	{
	  DefaultLogSink().Error("Short read of packet body, expecting %u, got %u\n",
				 m_Buffer.Size(), read_count);
	  return RESULT_READFAIL;
	}

      memcpy(m_Buffer.Data(), tmp_data, tmp_read_size);
      remainder = m_Buffer.Size() - tmp_read_size;

      if ( remainder > 0 )
	{
	  result = Reader.Read(m_Buffer.Data() + tmp_read_size, remainder, &read_count);
      
	  if ( read_count != remainder )
	    {
	      DefaultLogSink().Error("Short read of packet body, expecting %u, got %u\n",
				     remainder+tmp_read_size, read_count+tmp_read_size);
	      result = RESULT_READFAIL;
	    }
	}
    }

  return result;
}

//
ASDCP::Result_t
ASDCP::KLVFilePacket::WriteKLToFile(Kumu::FileWriter& Writer, const UL& label, ui32_t length)
{
  byte_t buffer[kl_length];
  memcpy(buffer, label.Value(), label.Size());

  if ( ! Kumu::write_BER(buffer+SMPTE_UL_LENGTH, length, MXF_BER_LENGTH) )
    return RESULT_FAIL;

  ui32_t write_count;
  Writer.Write(buffer, kl_length, &write_count);
  assert(write_count == kl_length);
  return RESULT_OK;
}


//
// end KLV.cpp
//