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
23**
24** See http://www.matroska.org/license/lgpl/ 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: EbmlUInteger.cpp 1079 2005-03-03 13:18:14Z robux4 $
34	\author Steve Lhomme     <robux4 @ users.sf.net>
35	\author Moritz Bunkus <moritz @ bunkus.org>
36*/
37#include <cassert>
38
39#include "ebml/EbmlUInteger.h"
40
41START_LIBEBML_NAMESPACE
42
43EbmlUInteger::EbmlUInteger()
44 :EbmlElement(DEFAULT_UINT_SIZE, false)
45{}
46
47EbmlUInteger::EbmlUInteger(const uint64 aDefaultValue)
48 :EbmlElement(DEFAULT_UINT_SIZE, true), Value(aDefaultValue), DefaultValue(aDefaultValue)
49{
50	DefaultIsSet = true;
51}
52
53EbmlUInteger::EbmlUInteger(const EbmlUInteger & ElementToClone)
54 :EbmlElement(ElementToClone)
55 ,Value(ElementToClone.Value)
56 ,DefaultValue(ElementToClone.DefaultValue)
57{
58}
59
60/*!
61	\todo handle exception on errors
62*/
63uint32 EbmlUInteger::RenderData(IOCallback & output, bool bForceRender, bool bKeepIntact)
64{
65	binary FinalData[8]; // we don't handle more than 64 bits integers
66
67	if (SizeLength > 8)
68		return 0; // integer bigger coded on more than 64 bits are not supported
69
70	uint64 TempValue = Value;
71	for (unsigned int i=0; i<Size;i++) {
72		FinalData[Size-i-1] = TempValue & 0xFF;
73		TempValue >>= 8;
74	}
75
76	output.writeFully(FinalData,Size);
77
78	return Size;
79}
80
81uint64 EbmlUInteger::UpdateSize(bool bKeepIntact, bool bForceRender)
82{
83	if (!bKeepIntact && IsDefaultValue())
84		return 0;
85
86	if (Value <= 0xFF) {
87		Size = 1;
88	} else if (Value <= 0xFFFF) {
89		Size = 2;
90	} else if (Value <= 0xFFFFFF) {
91		Size = 3;
92	} else if (Value <= 0xFFFFFFFF) {
93		Size = 4;
94	} else if (Value <= EBML_PRETTYLONGINT(0xFFFFFFFFFF)) {
95		Size = 5;
96	} else if (Value <= EBML_PRETTYLONGINT(0xFFFFFFFFFFFF)) {
97		Size = 6;
98	} else if (Value <= EBML_PRETTYLONGINT(0xFFFFFFFFFFFFFF)) {
99		Size = 7;
100	} else {
101		Size = 8;
102	}
103
104	if (DefaultSize > Size) {
105		Size = DefaultSize;
106	}
107
108	return Size;
109}
110
111uint64 EbmlUInteger::ReadData(IOCallback & input, ScopeMode ReadFully)
112{
113	if (ReadFully != SCOPE_NO_DATA)
114	{
115		binary Buffer[8];
116		input.readFully(Buffer, Size);
117		Value = 0;
118
119		for (unsigned int i=0; i<Size; i++)
120		{
121			Value <<= 8;
122			Value |= Buffer[i];
123		}
124		bValueIsSet = true;
125	}
126
127	return Size;
128}
129
130END_LIBEBML_NAMESPACE
131