2 Copyright (c) 2004-2014, John Hurst
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
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.
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.
29 \brief AS-DCP library, misc classes and subroutines
32 #include "AS_DCP_internal.h"
38 return PACKAGE_VERSION;
43 //------------------------------------------------------------------------------------------
46 // Encodes a rational number as a string having a single delimiter character between
47 // numerator and denominator. Retuns the buffer pointer to allow convenient in-line use.
49 ASDCP::EncodeRational(const Rational& rational, char* str_buf, ui32_t buf_len, char delimiter)
52 snprintf(str_buf, buf_len, "%u%c%u", rational.Numerator, delimiter, rational.Denominator);
56 // Decodes a rational number havng a single non-digit delimiter character between
57 // the numerator and denominator. Returns false if the string does not contain
58 // the expected syntax.
60 ASDCP::DecodeRational(const char* str_rational, Rational& rational)
63 rational.Numerator = strtol(str_rational, 0, 10);
65 const char* p = str_rational;
66 while ( *p && isdigit(*p) )
71 if ( p[0] == 0 || p[1] == 0 )
77 rational.Denominator = strtol(p, 0, 10);
82 //------------------------------------------------------------------------------------------
84 // frame buffer base class implementation
86 ASDCP::FrameBuffer::FrameBuffer() :
87 m_Data(0), m_Capacity(0), m_OwnMem(false), m_Size(0),
88 m_FrameNumber(0), m_SourceLength(0), m_PlaintextOffset(0)
92 ASDCP::FrameBuffer::~FrameBuffer()
94 if ( m_OwnMem && m_Data != 0 )
98 // Instructs the object to use an externally allocated buffer. The external
99 // buffer will not be cleaned up by the frame buffer when it is destroyed.
100 // Call with (0,0) to revert to internally allocated buffer.
101 // Returns error if the buf_addr argument is NULL and either buf_size is
102 // non-zero or internally allocated memory is in use.
104 ASDCP::FrameBuffer::SetData(byte_t* buf_addr, ui32_t buf_size)
106 // if buf_addr is null and we have an external memory reference,
107 // drop the reference and place the object in the initialized-
108 // but-no-buffer-allocated state
111 if ( buf_size > 0 || m_OwnMem )
115 m_Capacity = m_Size = 0;
120 if ( m_OwnMem && m_Data != 0 )
124 m_Capacity = buf_size;
131 // Sets the size of the internally allocate buffer. Returns RESULT_CAPEXTMEM
132 // if the object is using an externally allocated buffer via SetData();
133 // Resets content size to zero.
135 ASDCP::FrameBuffer::Capacity(ui32_t cap_size)
137 if ( ! m_OwnMem && m_Data != 0 )
138 return RESULT_CAPEXTMEM; // cannot resize external memory
140 if ( m_Capacity < cap_size )
148 m_Data = (byte_t*)malloc(cap_size);
153 m_Capacity = cap_size;