vfs: check userland buffers before reading them.
[haiku.git] / src / add-ons / media / plugins / matroska / libebml / EbmlSInteger.cpp
blobb53c1eb1364bb303637f18a51eaa94ba42f5fd15
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 library is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU Lesser General Public
10 ** License as published by the Free Software Foundation; either
11 ** version 2.1 of the License, or (at your option) any later version.
12 **
13 ** This library is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** Lesser General Public License for more details.
17 **
18 ** You should have received a copy of the GNU Lesser General Public
19 ** License along with this library; if not, write to the Free Software
20 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 ** See http://www.matroska.org/license/lgpl/ for LGPL licensing information.
24 ** Contact license@matroska.org if any conditions of this licensing are
25 ** not clear to you.
27 **********************************************************************/
29 /*!
30 \file
31 \version \$Id: EbmlSInteger.cpp 1079 2005-03-03 13:18:14Z robux4 $
32 \author Steve Lhomme <robux4 @ users.sf.net>
33 \author Moritz Bunkus <moritz @ bunkus.org>
35 #include <cassert>
37 #include "ebml/EbmlSInteger.h"
39 START_LIBEBML_NAMESPACE
41 EbmlSInteger::EbmlSInteger()
42 :EbmlElement(DEFAULT_INT_SIZE, false)
45 EbmlSInteger::EbmlSInteger(const int64 aDefaultValue)
46 :EbmlElement(DEFAULT_INT_SIZE, true), Value(aDefaultValue)
48 DefaultIsSet = true;
51 EbmlSInteger::EbmlSInteger(const EbmlSInteger & ElementToClone)
52 :EbmlElement(ElementToClone)
53 ,Value(ElementToClone.Value)
54 ,DefaultValue(ElementToClone.DefaultValue)
58 /*!
59 \todo handle exception on errors
61 uint32 EbmlSInteger::RenderData(IOCallback & output, bool bForceRender, bool bKeepIntact)
63 binary FinalData[8]; // we don't handle more than 64 bits integers
64 unsigned int i;
66 if (SizeLength > 8)
67 return 0; // integer bigger coded on more than 64 bits are not supported
69 int64 TempValue = Value;
70 for (i=0; i<Size;i++) {
71 FinalData[Size-i-1] = binary(TempValue & 0xFF);
72 TempValue >>= 8;
75 output.writeFully(FinalData,Size);
77 return Size;
80 uint64 EbmlSInteger::UpdateSize(bool bKeepIntact, bool bForceRender)
82 if (!bKeepIntact && IsDefaultValue())
83 return 0;
85 if (Value <= 0x7F && Value >= (-0x80)) {
86 Size = 1;
87 } else if (Value <= 0x7FFF && Value >= (-0x8000)) {
88 Size = 2;
89 } else if (Value <= 0x7FFFFF && Value >= (-0x800000)) {
90 Size = 3;
91 } else if (Value <= 0x7FFFFFFF && Value >= (-0x80000000)) {
92 Size = 4;
93 } else if (Value <= EBML_PRETTYLONGINT(0x7FFFFFFFFF) &&
94 Value >= EBML_PRETTYLONGINT(-0x8000000000)) {
95 Size = 5;
96 } else if (Value <= EBML_PRETTYLONGINT(0x7FFFFFFFFFFF) &&
97 Value >= EBML_PRETTYLONGINT(-0x800000000000)) {
98 Size = 6;
99 } else if (Value <= EBML_PRETTYLONGINT(0x7FFFFFFFFFFFFF) &&
100 Value >= EBML_PRETTYLONGINT(-0x80000000000000)) {
101 Size = 7;
102 } else {
103 Size = 8;
106 if (DefaultSize > Size) {
107 Size = DefaultSize;
110 return Size;
113 uint64 EbmlSInteger::ReadData(IOCallback & input, ScopeMode ReadFully)
115 if (ReadFully != SCOPE_NO_DATA)
117 binary Buffer[8];
118 input.readFully(Buffer, Size);
120 if (Buffer[0] & 0x80)
121 Value = -1; // this is a negative value
122 else
123 Value = 0; // this is a positive value
125 for (unsigned int i=0; i<Size; i++)
127 Value <<= 8;
128 Value |= Buffer[i];
130 bValueIsSet = true;
133 return Size;
136 END_LIBEBML_NAMESPACE