0ae9a1f4e7c817b5617fa453d8c4dbdcc15df597
[libdcp.git] / asdcplib / src / AS_DCP.cpp
1 /*
2 Copyright (c) 2004-2009, 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: AS_DCP.cpp,v 1.6 2009/04/09 19:16:49 msheby Exp $       
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   return PACKAGE_VERSION;
39 }
40
41
42 //------------------------------------------------------------------------------------------
43 //
44 // frame buffer base class implementation
45
46 ASDCP::FrameBuffer::FrameBuffer() :
47   m_Data(0), m_Capacity(0), m_OwnMem(false), m_Size(0),
48   m_FrameNumber(0), m_SourceLength(0), m_PlaintextOffset(0)
49 {
50 }
51
52 ASDCP::FrameBuffer::~FrameBuffer()
53 {
54   if ( m_OwnMem && m_Data != 0 )
55     free(m_Data);
56 }
57
58 // Instructs the object to use an externally allocated buffer. The external
59 // buffer will not be cleaned up by the frame buffer when it is destroyed.
60 // Call with (0,0) to revert to internally allocated buffer.
61 // Returns error if the buf_addr argument is NULL and either buf_size is
62 // non-zero or internally allocated memory is in use.
63 ASDCP::Result_t
64 ASDCP::FrameBuffer::SetData(byte_t* buf_addr, ui32_t buf_size)
65 {
66   // if buf_addr is null and we have an external memory reference,
67   // drop the reference and place the object in the initialized-
68   // but-no-buffer-allocated state
69   if ( buf_addr == 0 )
70     {
71       if ( buf_size > 0 || m_OwnMem )
72         return RESULT_PTR;
73
74       m_OwnMem = false;
75       m_Capacity = m_Size = 0;
76       m_Data = 0;
77       return RESULT_OK;
78     }
79
80   if ( m_OwnMem && m_Data != 0 )
81     free(m_Data);
82
83   m_OwnMem = false;
84   m_Capacity = buf_size;
85   m_Data = buf_addr;
86   m_Size = 0;
87
88   return RESULT_OK;
89 }
90
91 // Sets the size of the internally allocate buffer. Returns RESULT_CAPEXTMEM
92 // if the object is using an externally allocated buffer via SetData();
93 // Resets content size to zero.
94 ASDCP::Result_t
95 ASDCP::FrameBuffer::Capacity(ui32_t cap_size)
96 {
97   if ( ! m_OwnMem && m_Data != 0 )
98     return RESULT_CAPEXTMEM; // cannot resize external memory
99
100   if ( m_Capacity < cap_size )
101     {
102       if ( m_Data != 0 )
103         {
104           assert(m_OwnMem);
105           free(m_Data);
106         }
107
108       m_Data = (byte_t*)malloc(cap_size);
109
110       if ( m_Data == 0 )
111         return RESULT_ALLOC;
112
113       m_Capacity = cap_size;
114       m_OwnMem = true;
115       m_Size = 0;
116     }
117
118   return RESULT_OK;
119 }
120
121
122 //
123 // end AS_DCP.cpp
124 //