/* libsswf_tag_binarydata.c++ -- written by Alexis WILKE for Made to Order Software Corp. (c) 2002-2008 */

/*

Copyright (c) 2002-2008 Made to Order Software Corp.

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and
associated documentation files (the "Software"), to
deal in the Software without restriction, including
without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

/** \file
 *
 * \brief The implementation of the sswf::TagBinaryData class
 *
 * This file declares the body of the functions which are not
 * inline. It is part of the SSWF library.
 */

#include	"sswf/libsswf.h"

using namespace sswf;



/////////////////////////////////////////// TagBinaryData



/** \class sswf::TagBinaryData
 *
 * \brief Binary data reusable by the ActionScripts
 *
 * This tag supports any type of binary data that the ActionScripts
 * may need at some point.
 *
 * The data is identifier with an ID like other objects, for later
 * reference.
 *
 * \sa <a href="../SWFalexref.html#tag_definebinarydata">SWF Alexis' Reference&mdash;Binary Data</a>
 * \sa <a href="../SWFalexref.html#swf_tag">SWF Alexis' Reference&mdash;swf_tag</a>
 */


/** \brief Initializes the TagBinaryData
 *
 * By default, the TagBinaryData is empty.
 *
 * \param[in] parent a TagHeader tag
 */
TagBinaryData::TagBinaryData(TagBase *parent)
	: TagBaseID("binary_data", parent)
{
	f_data = 0;
	f_size = 0;
}



/** \brief Cleanup the TagBinaryData
 *
 * This function cleans up the Binary Data tag objects.
 */
TagBinaryData::~TagBinaryData()
{
}



/** \brief The type of this tag
 *
 * The TagProductInfo is a DEFINE type.
 *
 * \return SWF_TYPE_DEFINE
 *
 * \sa TagBase::swf_type_t sswf::TagBase::TypeFlags(void) const
 */
TagBase::swf_type_t TagBinaryData::TypeFlags(void) const
{
	return SWF_TYPE_DEFINE;
}


/** \brief Retrieve the current data
 *
 * This function retreives a direct reference pointer to the internal buffer
 * of the Binary Data tag and its size.
 *
 * \warning
 * If the size is zero, the pointer will be NULL. This is a valid case that you
 * must be prepared to manage.
 *
 * \param[out] data A reference to a pointer where the tag buffer pointer is saved
 * \param[out] size A reference to a size_t variable to receive the size
 *
 * \bug
 * Do NOT free the memory buffer returned to you. The TagBinaryData is
 * responsible for that task. If you want to free the buffer, call the
 * SetData() function with a size of zero and any data pointer.
 *
 * \bug
 * Also, do NOT use the pointer and size returned by this function after
 * calling the SetData() function since the buffer is likely to move.
 *
 * \sa void sswf::TagBinaryData::SetData(const char *data, size_t size)
 */
void TagBinaryData::GetData(const char *& data, size_t& size) const
{
	data = f_data;
	size = f_size;
}



/** \brief Save the specified data in the tag
 *
 * This function makes a copy of the specified data in this tag.
 * Later, it is saved in the SWF animation with the Save() function.
 *
 * A size of zero is valid.
 *
 * \param[in] data A valid pointer to the data to save in the tag
 * \param[in] size The number of bytes of data to save in the tag
 *
 * \sa void sswf::TagBinaryData::GetData(const char *& data, size_t& size)
 */
void TagBinaryData::SetData(const char *data, size_t size)
{
	MemFree(f_data);
	f_size = size;
	if(f_size == 0) {
		f_data = 0;
	}
	else {
		f_data = reinterpret_cast<char *>(MemAlloc(f_size, "Binary Data Buffer"));
		memcpy(f_data, data, f_size);
	}
}



/** \brief This tag appeared in version 9 of SWF
 *
 * Tells the TagHeader that a version 9+ movie is required.
 *
 * \return ErrorManager::ERROR_CODE_NONE or some other ErrorManager error
 */
ErrorManager::error_code_t TagBinaryData::PreSave(void)
{
	MinimumVersion(9);

	return ErrorManager::ERROR_CODE_NONE;
}


/** \brief Save the binary data tag in the Data buffer
 *
 * This function saves the Binary Data tag in the specified
 * Data buffer. The tag must have an identifier for later
 * reference.
 *
 * \param[in,out] data The Data buffer where the result is saved
 *
 * \return And sswf::ErrorManager error (ErrorManager::ERROR_CODE_NONE when no errors)
 */
ErrorManager::error_code_t TagBinaryData::Save(Data& data)
{
	SaveTag(data, SWF_TAG_DEFINE_BINARY_DATA, 6 + f_size);

	SaveID(data);
	data.PutLong(0);		// reserved

	// The binary data...
	data.Write(f_data, f_size);

	return ErrorManager::ERROR_CODE_NONE;
}







/* The following options fold the documentation; use 'zi' to turn on and off
 *
 * vim: foldexpr=getline(v\:lnum)!~'^/\\*\\*'&&getline(v\:lnum)!~'^\ \\*'?0\:1 foldcolumn=2 foldmethod=expr
 */
