1 /****************************************************************************
2 ** libebml : parse EBML files, see http://embl.sourceforge.net/
3 **
4 ** <file/class description>
5 **
6 ** Copyright (C) 2002-2010 Steve Lhomme.  All rights reserved.
7 **
8 ** This file is part of libebml.
9 **
10 ** This library is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU Lesser General Public
12 ** License as published by the Free Software Foundation; either
13 ** version 2.1 of the License, or (at your option) any later version.
14 **
15 ** This library is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ** Lesser General Public License for more details.
19 **
20 ** You should have received a copy of the GNU Lesser General Public
21 ** License along with this library; if not, write to the Free Software
22 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 **
24 ** See http://www.gnu.org/licenses/lgpl-2.1.html for LGPL licensing information.
25 **
26 ** Contact license@matroska.org if any conditions of this licensing are
27 ** not clear to you.
28 **
29 **********************************************************************/
30 
31 /*!
32   \file
33   \version \$Id$
34   \author Steve Lhomme     <robux4 @ users.sf.net>
35   \author Julien Coloos  <suiryc @ users.sf.net>
36 */
37 #include <cassert>
38 #include <string>
39 
40 #include "ebml/EbmlBinary.h"
41 #include "ebml/StdIOCallback.h"
42 
43 START_LIBEBML_NAMESPACE
44 
EbmlBinary()45 EbmlBinary::EbmlBinary()
46   :EbmlElement(0, false), Data(nullptr)
47 {}
48 
EbmlBinary(const EbmlBinary & ElementToClone)49 EbmlBinary::EbmlBinary(const EbmlBinary & ElementToClone)
50   :EbmlElement(ElementToClone)
51 {
52   if (ElementToClone.Data == nullptr)
53     Data = nullptr;
54   else {
55     Data = static_cast<binary *>(malloc(GetSize()));
56     if(Data != nullptr)
57       memcpy(Data, ElementToClone.Data, GetSize());
58   }
59 }
60 
~EbmlBinary()61 EbmlBinary::~EbmlBinary() {
62   if(Data)
63     free(Data);
64 }
65 
operator const binary&() const66 EbmlBinary::operator const binary &() const {return *Data;}
67 
68 
RenderData(IOCallback & output,bool,bool)69 filepos_t EbmlBinary::RenderData(IOCallback & output, bool /* bForceRender */, bool /* bWithDefault */)
70 {
71   output.writeFully(Data,GetSize());
72 
73   return GetSize();
74 }
75 
76 /*!
77   \note no Default binary value handled
78 */
UpdateSize(bool,bool)79 uint64 EbmlBinary::UpdateSize(bool /* bWithDefault */, bool /* bForceRender */)
80 {
81   return GetSize();
82 }
83 
ReadData(IOCallback & input,ScopeMode ReadFully)84 filepos_t EbmlBinary::ReadData(IOCallback & input, ScopeMode ReadFully)
85 {
86   if (Data != nullptr) {
87     free(Data);
88     Data = nullptr;
89   }
90 
91   if (ReadFully == SCOPE_NO_DATA) {
92     return GetSize();
93   }
94 
95   if (!GetSize()) {
96     SetValueIsSet();
97     return 0;
98   }
99 
100   Data = (GetSize() < SIZE_MAX) ? static_cast<binary *>(malloc(GetSize())) : nullptr;
101   if (Data == nullptr)
102     throw CRTError(std::string("Error allocating data"));
103   SetValueIsSet();
104   return input.read(Data, GetSize());
105 }
106 
operator ==(const EbmlBinary & ElementToCompare) const107 bool EbmlBinary::operator==(const EbmlBinary & ElementToCompare) const
108 {
109   return ((GetSize() == ElementToCompare.GetSize()) && (GetSize() == 0 || !memcmp(Data, ElementToCompare.Data, GetSize())));
110 }
111 
112 END_LIBEBML_NAMESPACE
113