summaryrefslogtreecommitdiff
path: root/src/AS_DCP.cpp
blob: a5187d67f0126885308c3d61dcbcaafe32738e0e (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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
Copyright (c) 2004-2005, 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    AS_DCP.cpp
    \version $Id$       
    \brief   AS-DCP library, misc classes and subroutines
*/

#include <AS_DCP_system.h>
#include "hex_utils.h"
#include <assert.h>


static const ui32_t s_MessageCount = 27;
static const char* s_ErrorMessages[] =
{
  "An undefined error was detected.",
  "An unexpected NULL pointer was given.",
  "An unexpected empty string was given.",
  "The given frame buffer is too small.",
  "The object is not yet initialized.",

  "The requested file does not exist on the system.",
  "Insufficient privilege exists to perform the operation.",
  "File open error.",
  "The file contains errors or is not OP-Atom/AS-DCP.",
  "An invalid file location was requested.",

  "File read error.",
  "File write error.",
  "Unknown raw essence file type.",
  "Raw essence format invalid.",
  "Object state error.",

  "Attempt to read past end of file.",
  "Invalid configuration option detected.",
  "Frame number out of range.",
  "AESEncContext required when writing to encrypted file",
  "Plaintext offset exceeds frame buffer size",

  "Error allocating memory",
  "Cannot resize externally allocated memory",
  "The check value did not decrypt correctly",
  "HMAC authentication failure",
  "HMAC context required",

  "Error initializing block cipher context",
  "Attempted to write an empty frame buffer"
};


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

//
class StderrLogSink : public ASDCP::ILogSink
{
public:
  bool show_info;
  bool show_debug;

  StderrLogSink() : show_info(false), show_debug(false) {}
  ~StderrLogSink() {}

  void Error(const char* fmt, ...) {
    va_list args;
    va_start(args, fmt);
    vLogf(LOG_ERROR, fmt, &args);
    va_end(args);
  }

  void Warn(const char* fmt, ...) {
    va_list args;
    va_start(args, fmt);
    vLogf(LOG_WARN, fmt, &args);
    va_end(args);
  }

  void Info(const char* fmt, ...) {
    va_list args;
    va_start(args, fmt);
    vLogf(LOG_INFO, fmt, &args);
    va_end(args);
  }

  void Debug(const char* fmt, ...) {
    va_list args;
    va_start(args, fmt);
    vLogf(LOG_DEBUG, fmt, &args);
    va_end(args);
  }

  void Logf(ASDCP::ILogSink::LogType_t type, const char* fmt, ...) {
    va_list args;
    va_start(args, fmt);
    vLogf(type, fmt, &args);
    va_end(args);
  }

  void vLogf(ASDCP::ILogSink::LogType_t type, const char* fmt, va_list* list) {
    FILE* stream = stderr;

    switch ( type )
      {
      case LOG_ERROR: fputs("Error: ", stream); break;
      case LOG_WARN:  fputs("Warning: ", stream); break;
      case LOG_INFO:
	if ( ! show_info ) return;
	fputs("Info: ", stream);
	break;
      case LOG_DEBUG:
	if ( ! show_debug ) return;
	fputs("Debug: ", stream);
	break;
      }
    
    vfprintf(stream, fmt, *list);
  }

} s_StderrLogSink;

//
static ASDCP::ILogSink* s_DefaultLogSink = 0;

//
void
ASDCP::SetDefaultLogSink(ILogSink* Sink)
{
    s_DefaultLogSink = Sink;
}

// bootleg entry for debug enthusiasts
void
set_debug_mode(bool info_mode, bool debug_mode)
{
  s_StderrLogSink.show_info = info_mode;
  s_StderrLogSink.show_debug = debug_mode;
}

// Returns the internal default sink.
ASDCP::ILogSink&
ASDCP::DefaultLogSink()
{
  if ( s_DefaultLogSink == 0 )
    s_DefaultLogSink = &s_StderrLogSink;

  return *s_DefaultLogSink;
}

const char*
ASDCP::Version()
{
  static char ver[16];
  snprintf(ver, 16, "%lu.%lu.%lu", VERSION_MAJOR, VERSION_APIMINOR, VERSION_IMPMINOR);
  return ver;
}


// Returns a pointer to an English language string describing the given result code.
// If the result code is not a valid member of the Result_t enum, the string
// "**UNKNOWN**" will be returned.
const char*
ASDCP::GetResultString(Result_t result)
{
  if ( result >= 0 )
    return "No error.";

  ui32_t idx = (- result);

  if ( idx > s_MessageCount )
    return "**UNKNOWN**";

  return s_ErrorMessages[--idx];
}


// convert utf-8 hext string to bin
i32_t
ASDCP::hex2bin(const char* str, byte_t* buf, ui32_t buf_len, ui32_t* conv_size)
{
  ASDCP_TEST_NULL(str);
  ASDCP_TEST_NULL(buf);
  ASDCP_TEST_NULL(conv_size);

  *conv_size = 0;

  if ( str[0] == 0 ) // nothing to convert
    return 0;

  for ( int j = 0; str[j]; j++ )
    {
      if ( isxdigit(str[j]) )
	(*conv_size)++;
    }

  if ( *conv_size & 0x01 ) (*conv_size)++;
  *conv_size /= 2;

  if ( *conv_size > buf_len )// maximum possible data size
    return -1;

  *conv_size = 0;

  int phase = 0; // track high/low nybble

  // for each character, fill in the high nybble then the low
  for ( int i = 0; str[i]; i++ )
    {
      if ( ! isxdigit(str[i]) )
        continue;

      byte_t val = str[i] - ( isdigit(str[i]) ? 0x30 : ( isupper(str[i]) ? 0x37 : 0x57 ) );

      if ( phase == 0 )
        {
          buf[*conv_size] = val << 4;
          phase++;
        }
      else
        {
          buf[*conv_size] |= val;
          phase = 0;
          (*conv_size)++;
        }
    }

  return 0;
}


// convert a memory region to a NULL-terminated hexadecimal string
//
const char*
ASDCP::bin2hex(const byte_t* bin_buf, ui32_t bin_len, char* str_buf, ui32_t str_len)
{
  if ( bin_buf == 0
       || str_buf == 0
       || ((bin_len * 2) + 1) > str_len )
    return 0;

  char* p = str_buf;

  for ( ui32_t i = 0; i < bin_len; i++ )
    {
      *p = (bin_buf[i] >> 4) & 0x0f;
      *p += *p < 10 ? 0x30 : 0x61 - 10;
      p++;

      *p = bin_buf[i] & 0x0f;
      *p += *p < 10 ? 0x30 : 0x61 - 10;
      p++;
    }

  *p = '\0';
  return str_buf;
}


// spew a range of bin data as hex
void
ASDCP::hexdump(const byte_t* buf, ui32_t dump_len, FILE* stream)
{
  if ( buf == 0 )
    return;

  if ( stream == 0 )
    stream = stderr;

  static ui32_t row_len = 16;
  const byte_t* p = buf;
  const byte_t* end_p = p + dump_len;

  for ( ui32_t line = 0; p < end_p; line++ )
    {
      fprintf(stream, "  %06x: ", line);
      ui32_t i;
      const byte_t* pp;

      for ( pp = p, i = 0; i < row_len && pp < end_p; i++, pp++ )
	fprintf(stream, "%02x ", *pp);

      while ( i++ < row_len )
	fputs("   ", stream);

      for ( pp = p, i = 0; i < row_len && pp < end_p; i++, pp++ )
	fputc((isprint(*pp) ? *pp : '.'), stream);

      fputc('\n', stream);
      p += row_len;
    }
}

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

// read a ber value from the buffer and compare with test value.
// Advances buffer to first character after BER value.
//
bool
ASDCP::read_test_BER(byte_t **buf, ui64_t test_value)
{
  if ( buf == 0 )
    return false;

  if ( ( **buf & 0x80 ) == 0 )
    return false;

  ui64_t val = 0;
  ui8_t ber_size = ( **buf & 0x0f ) + 1;

  if ( ber_size > 9 )
    return false;

  for ( ui8_t i = 1; i < ber_size; i++ )
    {
      if ( (*buf)[i] > 0 )
        val |= (ui64_t)((*buf)[i]) << ( ( ( ber_size - 1 ) - i ) * 8 );
    }

  *buf += ber_size;
  return ( val == test_value );
}


//
bool
ASDCP::read_BER(const byte_t* buf, ui64_t* val)
{
  ui8_t ber_size, i;
  
  if ( buf == 0 || val == 0 )
    return false;

  if ( ( *buf & 0x80 ) == 0 )
    return false;

  *val = 0;
  ber_size = ( *buf & 0x0f ) + 1;

  if ( ber_size > 9 )
    return false;

  for ( i = 1; i < ber_size; i++ )
    {
      if ( buf[i] > 0 )
	*val |= (ui64_t)buf[i] << ( ( ( ber_size - 1 ) - i ) * 8 );
    }

  return true;
}


static const ui64_t ber_masks[9] =
  { ui64_C(0xffffffffffffffff), ui64_C(0xffffffffffffff00), 
    ui64_C(0xffffffffffff0000), ui64_C(0xffffffffff000000),
    ui64_C(0xffffffff00000000), ui64_C(0xffffff0000000000),
    ui64_C(0xffff000000000000), ui64_C(0xff00000000000000),
    0
  };


//
bool
ASDCP::write_BER(byte_t* buf, ui64_t val, ui32_t ber_len)
{
  if ( buf == 0 )
    return false;

  if ( ber_len == 0 )
    { // calculate default length
      if ( val < 0x01000000L )
        ber_len = 4;
      else if ( val < ui64_C(0x0100000000000000) )
        ber_len = 8;
      else
        ber_len = 9;
    }
  else
    { // sanity check BER length
      if ( ber_len > 9 )
        {
          DefaultLogSink().Error("BER size %lu exceeds maximum size of 9\n", ber_len);
          return false;
        }
      
      if ( val & ber_masks[ber_len - 1] )
        {
	  char intbuf[IntBufferLen];
          DefaultLogSink().Error("BER size %lu too small for value %s\n",
				 ber_len, ui64sz(val, intbuf));
          return false;
        }
    }

  buf[0] = 0x80 + ( ber_len - 1 );

  for ( ui32_t i = ber_len - 1; i > 0; i-- )
    {
      buf[i] = (ui8_t)(val & 0xff);
      val >>= 8;
    }

  return true;
}

//------------------------------------------------------------------------------------------
//
// frame buffer base class implementation

ASDCP::FrameBuffer::FrameBuffer() :
  m_Data(0), m_Capacity(0), m_OwnMem(false), m_Size(0),
  m_FrameNumber(0), m_SourceLength(0), m_PlaintextOffset(0)
{
}

ASDCP::FrameBuffer::~FrameBuffer()
{
  if ( m_OwnMem && m_Data != 0 )
    free(m_Data);
}

// Instructs the object to use an externally allocated buffer. The external
// buffer will not be cleaned up by the frame buffer when it is destroyed.
// Call with (0,0) to revert to internally allocated buffer.
// Returns error if the buf_addr argument is NULL and either buf_size is
// non-zero or internally allocated memory is in use.
ASDCP::Result_t
ASDCP::FrameBuffer::SetData(byte_t* buf_addr, ui32_t buf_size)
{
  // if buf_addr is null and we have an external memory reference,
  // drop the reference and place the object in the initialized-
  // but-no-buffer-allocated state
  if ( buf_addr == 0 )
    {
      if ( buf_size > 0 || m_OwnMem )
	return RESULT_PTR;

      m_OwnMem = false;
      m_Capacity = m_Size = 0;
      m_Data = 0;
      return RESULT_OK;
    }

  if ( m_OwnMem && m_Data != 0 )
    free(m_Data);

  m_OwnMem = false;
  m_Capacity = buf_size;
  m_Data = buf_addr;
  m_Size = 0;

  return RESULT_OK;
}

// Sets the size of the internally allocate buffer. Returns RESULT_CAPEXTMEM
// if the object is using an externally allocated buffer via SetData();
// Resets content size to zero.
ASDCP::Result_t
ASDCP::FrameBuffer::Capacity(ui32_t cap_size)
{
  if ( ! m_OwnMem && m_Data != 0 )
    return RESULT_CAPEXTMEM; // cannot resize external memory

  if ( m_Capacity < cap_size )
    {
      if ( m_Data != 0 )
	{
	  assert(m_OwnMem);
	  free(m_Data);
	}

      m_Data = (byte_t*)malloc(cap_size);

      if ( m_Data == 0 )
	return RESULT_ALLOC;

      m_Capacity = cap_size;
      m_OwnMem = true;
      m_Size = 0;
    }

  return RESULT_OK;
}


//
// end AS_DCP.cpp
//