vfs: check userland buffers before reading them.
[haiku.git] / src / add-ons / media / plugins / matroska / libebml / EbmlFloat.cpp
blobc03f4c6e3a9eed22964c4864ed236df8114157eb
1 /****************************************************************************
2 ** libebml : parse EBML files, see http://embl.sourceforge.net/
3 **
4 ** <file/class description>
5 **
6 ** Copyright (C) 2002-2005 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 ** See http://www.matroska.org/license/lgpl/ for LGPL licensing information.
26 ** Contact license@matroska.org if any conditions of this licensing are
27 ** not clear to you.
29 **********************************************************************/
31 /*!
32 \file
33 \version \$Id: EbmlFloat.cpp 1243 2006-03-30 19:33:22Z mosu $
34 \author Steve Lhomme <robux4 @ users.sf.net>
37 #include <cassert>
39 #include "ebml/EbmlFloat.h"
41 START_LIBEBML_NAMESPACE
43 EbmlFloat::EbmlFloat(const EbmlFloat::Precision prec)
44 :EbmlElement(0, false)
46 SetPrecision(prec);
49 EbmlFloat::EbmlFloat(const double aDefaultValue, const EbmlFloat::Precision prec)
50 :EbmlElement(0, true), Value(aDefaultValue), DefaultValue(aDefaultValue)
52 DefaultIsSet = true;
53 SetPrecision(prec);
56 EbmlFloat::EbmlFloat(const EbmlFloat & ElementToClone)
57 :EbmlElement(ElementToClone)
58 ,Value(ElementToClone.Value)
59 ,DefaultValue(ElementToClone.DefaultValue)
63 /*!
64 \todo handle exception on errors
65 \todo handle 10 bits precision
67 uint32 EbmlFloat::RenderData(IOCallback & output, bool bForceRender, bool bKeepIntact)
69 assert(Size == 4 || Size == 8);
71 if (Size == 4) {
72 float val = Value;
73 int Tmp;
74 memcpy(&Tmp, &val, 4);
75 big_int32 TmpToWrite(Tmp);
76 output.writeFully(&TmpToWrite.endian(), Size);
77 } else if (Size == 8) {
78 double val = Value;
79 int64 Tmp;
80 memcpy(&Tmp, &val, 8);
81 big_int64 TmpToWrite(Tmp);
82 output.writeFully(&TmpToWrite.endian(), Size);
85 return Size;
88 uint64 EbmlFloat::UpdateSize(bool bKeepIntact, bool bForceRender)
90 if (!bKeepIntact && IsDefaultValue())
91 return 0;
92 return Size;
95 /*!
96 \todo remove the hack for possible endianess pb (test on little & big endian)
98 uint64 EbmlFloat::ReadData(IOCallback & input, ScopeMode ReadFully)
100 if (ReadFully != SCOPE_NO_DATA)
102 binary Buffer[20];
103 assert(Size <= 20);
104 input.readFully(Buffer, Size);
106 if (Size == 4) {
107 big_int32 TmpRead;
108 TmpRead.Eval(Buffer);
109 int32 tmpp = int32(TmpRead);
110 float val;
111 memcpy(&val, &tmpp, 4);
112 Value = val;
113 bValueIsSet = true;
114 } else if (Size == 8) {
115 big_int64 TmpRead;
116 TmpRead.Eval(Buffer);
117 int64 tmpp = int64(TmpRead);
118 double val;
119 memcpy(&val, &tmpp, 8);
120 Value = val;
121 bValueIsSet = true;
125 return Size;
128 END_LIBEBML_NAMESPACE