format string fixes
[asdcplib.git] / src / AS_DCP.cpp
1 /*
2 Copyright (c) 2004-2006, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14    derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 /*! \file    AS_DCP.cpp
28     \version $Id$       
29     \brief   AS-DCP library, misc classes and subroutines
30 */
31
32 #include "AS_DCP_internal.h"
33 #include <assert.h>
34
35 const char*
36 ASDCP::Version()
37 {
38   static char ver[16];
39   snprintf(ver, 16, "%u.%u.%u", VERSION_MAJOR, VERSION_APIMINOR, VERSION_IMPMINOR);
40   return ver;
41 }
42
43
44 //------------------------------------------------------------------------------------------
45 //
46 // frame buffer base class implementation
47
48 ASDCP::FrameBuffer::FrameBuffer() :
49   m_Data(0), m_Capacity(0), m_OwnMem(false), m_Size(0),
50   m_FrameNumber(0), m_SourceLength(0), m_PlaintextOffset(0)
51 {
52 }
53
54 ASDCP::FrameBuffer::~FrameBuffer()
55 {
56   if ( m_OwnMem && m_Data != 0 )
57     free(m_Data);
58 }
59
60 // Instructs the object to use an externally allocated buffer. The external
61 // buffer will not be cleaned up by the frame buffer when it is destroyed.
62 // Call with (0,0) to revert to internally allocated buffer.
63 // Returns error if the buf_addr argument is NULL and either buf_size is
64 // non-zero or internally allocated memory is in use.
65 ASDCP::Result_t
66 ASDCP::FrameBuffer::SetData(byte_t* buf_addr, ui32_t buf_size)
67 {
68   // if buf_addr is null and we have an external memory reference,
69   // drop the reference and place the object in the initialized-
70   // but-no-buffer-allocated state
71   if ( buf_addr == 0 )
72     {
73       if ( buf_size > 0 || m_OwnMem )
74         return RESULT_PTR;
75
76       m_OwnMem = false;
77       m_Capacity = m_Size = 0;
78       m_Data = 0;
79       return RESULT_OK;
80     }
81
82   if ( m_OwnMem && m_Data != 0 )
83     free(m_Data);
84
85   m_OwnMem = false;
86   m_Capacity = buf_size;
87   m_Data = buf_addr;
88   m_Size = 0;
89
90   return RESULT_OK;
91 }
92
93 // Sets the size of the internally allocate buffer. Returns RESULT_CAPEXTMEM
94 // if the object is using an externally allocated buffer via SetData();
95 // Resets content size to zero.
96 ASDCP::Result_t
97 ASDCP::FrameBuffer::Capacity(ui32_t cap_size)
98 {
99   if ( ! m_OwnMem && m_Data != 0 )
100     return RESULT_CAPEXTMEM; // cannot resize external memory
101
102   if ( m_Capacity < cap_size )
103     {
104       if ( m_Data != 0 )
105         {
106           assert(m_OwnMem);
107           free(m_Data);
108         }
109
110       m_Data = (byte_t*)malloc(cap_size);
111
112       if ( m_Data == 0 )
113         return RESULT_ALLOC;
114
115       m_Capacity = cap_size;
116       m_OwnMem = true;
117       m_Size = 0;
118     }
119
120   return RESULT_OK;
121 }
122
123
124 //
125 // end AS_DCP.cpp
126 //