vfs: check userland buffers before reading them.
[haiku.git] / src / add-ons / media / plugins / matroska / libebml / ebml / EbmlCrc32.h
blob902aa59483449ae2cf437a0780a3274f6bf1d7d1
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: EbmlCrc32.h 1155 2005-05-06 11:43:38Z robux4 $
34 \author Steve Lhomme <robux4 @ users.sf.net>
35 \author Jory Stone <jcsston @ toughguy.net>
37 #ifndef LIBEBML_CRC32_H
38 #define LIBEBML_CRC32_H
40 #include "EbmlTypes.h"
41 #include "EbmlBinary.h"
43 START_LIBEBML_NAMESPACE
45 const uint32 CRC32_NEGL = 0xffffffffL;
47 #ifdef WORDS_BIGENDIAN
48 # define CRC32_INDEX(c) (c >> 24)
49 # define CRC32_SHIFTED(c) (c << 8)
50 #else
51 # define CRC32_INDEX(c) (c & 0xff)
52 # define CRC32_SHIFTED(c) (c >> 8)
53 #endif
55 class EBML_DLL_API EbmlCrc32 : public EbmlBinary {
56 public:
57 EbmlCrc32();
58 EbmlCrc32(const EbmlCrc32 & ElementToClone);
59 static EbmlElement & Create() {return *(new EbmlCrc32);}
60 const EbmlCallbacks & Generic() const {return ClassInfos;}
61 bool ValidateSize() const {return (Size == 4);}
62 uint32 RenderData(IOCallback & output, bool bForceRender, bool bKeepIntact = false);
63 uint64 ReadData(IOCallback & input, ScopeMode ReadFully = SCOPE_ALL_DATA);
64 // uint64 UpdateSize(bool bKeepIntact = false);
66 static const EbmlCallbacks ClassInfos;
67 bool IsDefaultValue() const {
68 return false;
71 operator const EbmlId &() const {return ClassInfos.GlobalId;}
72 bool IsYourId(const EbmlId & TestId) const;
74 void AddElementCRC32(EbmlElement &ElementToCRC);
75 bool CheckElementCRC32(EbmlElement &ElementToCRC);
77 /*!
78 CRC Checksum Calculation
80 enum {DIGESTSIZE = 4};
82 /*!
83 Use this to quickly check a CRC32 with some data
84 \return True if inputCRC matches CRC32 generated from input data
86 static bool CheckCRC(uint32 inputCRC, const binary *input, uint32 length);
87 /*!
88 Calls Update() and Finalize(), use to create a CRC32 in one go
90 void FillCRC32(const binary *input, uint32 length);
91 /*!
92 Add data to the CRC table, in other words process some data bit by bit
94 void Update(const binary *input, uint32 length);
95 /*!
96 Use this with Update() to Finalize() or Complete the CRC32
98 void Finalize();
99 /*!
100 Returns a uint32 that has the value of the CRC32
102 uint32 GetCrc32() const {
103 return m_crc_final;
106 void ForceCrc32(uint32 NewValue) { m_crc_final = NewValue; bValueIsSet = true;}
108 EbmlElement * Clone() const;
110 protected:
111 void ResetCRC() {m_crc = CRC32_NEGL;}
112 void UpdateByte(binary b) {m_crc = m_tab[CRC32_INDEX(m_crc) ^ b] ^ CRC32_SHIFTED(m_crc);}
114 static const uint32 m_tab[256];
115 uint32 m_crc;
116 uint32 m_crc_final;
119 template <class T>
120 inline unsigned int GetAlignment(T *dummy=NULL) // VC60 workaround
122 #if (_MSC_VER >= 1300)
123 return __alignof(T);
124 #elif defined(__GNUC__)
125 return __alignof__(T);
126 #else
127 return sizeof(T);
128 #endif
131 template <class T>
132 inline bool IsPowerOf2(T n)
134 return n > 0 && (n & (n-1)) == 0;
137 template <class T1, class T2>
138 inline T2 ModPowerOf2(T1 a, T2 b)
140 assert(IsPowerOf2(b));
141 return T2(a) & (b-1);
144 inline bool IsAlignedOn(const void *p, unsigned int alignment)
146 return IsPowerOf2(alignment) ? ModPowerOf2((unsigned long)p, alignment) == 0 : (unsigned long)p % alignment == 0;
149 template <class T>
150 inline bool IsAligned(const void *p, T *dummy=NULL) // VC60 workaround
152 return IsAlignedOn(p, GetAlignment<T>());
155 END_LIBEBML_NAMESPACE
157 #endif // LIBEBML_CRC32_H