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 21** 22** See http://www.matroska.org/license/lgpl/ for LGPL licensing information. 23** 24** Contact license@matroska.org if any conditions of this licensing are 25** not clear to you. 26** 27**********************************************************************/ 28 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> 34*/ 35#include <cassert> 36 37#include "ebml/EbmlSInteger.h" 38 39START_LIBEBML_NAMESPACE 40 41EbmlSInteger::EbmlSInteger() 42 :EbmlElement(DEFAULT_INT_SIZE, false) 43{} 44 45EbmlSInteger::EbmlSInteger(const int64 aDefaultValue) 46 :EbmlElement(DEFAULT_INT_SIZE, true), Value(aDefaultValue) 47{ 48 DefaultIsSet = true; 49} 50 51EbmlSInteger::EbmlSInteger(const EbmlSInteger & ElementToClone) 52 :EbmlElement(ElementToClone) 53 ,Value(ElementToClone.Value) 54 ,DefaultValue(ElementToClone.DefaultValue) 55{ 56} 57 58/*! 59 \todo handle exception on errors 60*/ 61uint32 EbmlSInteger::RenderData(IOCallback & output, bool bForceRender, bool bKeepIntact) 62{ 63 binary FinalData[8]; // we don't handle more than 64 bits integers 64 unsigned int i; 65 66 if (SizeLength > 8) 67 return 0; // integer bigger coded on more than 64 bits are not supported 68 69 int64 TempValue = Value; 70 for (i=0; i<Size;i++) { 71 FinalData[Size-i-1] = binary(TempValue & 0xFF); 72 TempValue >>= 8; 73 } 74 75 output.writeFully(FinalData,Size); 76 77 return Size; 78} 79 80uint64 EbmlSInteger::UpdateSize(bool bKeepIntact, bool bForceRender) 81{ 82 if (!bKeepIntact && IsDefaultValue()) 83 return 0; 84 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; 104 } 105 106 if (DefaultSize > Size) { 107 Size = DefaultSize; 108 } 109 110 return Size; 111} 112 113uint64 EbmlSInteger::ReadData(IOCallback & input, ScopeMode ReadFully) 114{ 115 if (ReadFully != SCOPE_NO_DATA) 116 { 117 binary Buffer[8]; 118 input.readFully(Buffer, Size); 119 120 if (Buffer[0] & 0x80) 121 Value = -1; // this is a negative value 122 else 123 Value = 0; // this is a positive value 124 125 for (unsigned int i=0; i<Size; i++) 126 { 127 Value <<= 8; 128 Value |= Buffer[i]; 129 } 130 bValueIsSet = true; 131 } 132 133 return Size; 134} 135 136END_LIBEBML_NAMESPACE 137