00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00037 #ifndef LIBEBML_CRC32_H
00038 #define LIBEBML_CRC32_H
00039
00040 #include <cassert>
00041
00042 #include "EbmlTypes.h"
00043 #include "EbmlBinary.h"
00044
00045 START_LIBEBML_NAMESPACE
00046
00047 const uint32 CRC32_NEGL = 0xffffffffL;
00048
00049 #ifdef WORDS_BIGENDIAN
00050 # define CRC32_INDEX(c) (c >> 24)
00051 # define CRC32_SHIFTED(c) (c << 8)
00052 #else
00053 # define CRC32_INDEX(c) (c & 0xff)
00054 # define CRC32_SHIFTED(c) (c >> 8)
00055 #endif
00056
00057 DECLARE_EBML_BINARY(EbmlCrc32)
00058 public:
00059 EbmlCrc32(const EbmlCrc32 & ElementToClone);
00060 virtual bool ValidateSize() const {return IsFiniteSize() && (GetSize() == 4);}
00061 filepos_t RenderData(IOCallback & output, bool bForceRender, bool bWithDefault = false);
00062 filepos_t ReadData(IOCallback & input, ScopeMode ReadFully = SCOPE_ALL_DATA);
00063
00064
00065 bool IsDefaultValue() const {
00066 return false;
00067 }
00068
00069 void AddElementCRC32(EbmlElement &ElementToCRC);
00070 bool CheckElementCRC32(EbmlElement &ElementToCRC);
00071
00075 enum {DIGESTSIZE = 4};
00076
00081 static bool CheckCRC(uint32 inputCRC, const binary *input, uint32 length);
00085 void FillCRC32(const binary *input, uint32 length);
00089 void Update(const binary *input, uint32 length);
00093 void Finalize();
00097 uint32 GetCrc32() const {
00098 return m_crc_final;
00099 };
00100
00101 void ForceCrc32(uint32 NewValue) { m_crc_final = NewValue; SetValueIsSet();}
00102
00103 #if defined(EBML_STRICT_API)
00104 private:
00105 #else
00106 protected:
00107 #endif
00108 void ResetCRC() {m_crc = CRC32_NEGL;}
00109 void UpdateByte(binary b) {m_crc = m_tab[CRC32_INDEX(m_crc) ^ b] ^ CRC32_SHIFTED(m_crc);}
00110
00111 static const uint32 m_tab[256];
00112 uint32 m_crc;
00113 uint32 m_crc_final;
00114
00115 EBML_CONCRETE_CLASS(EbmlCrc32)
00116 };
00117
00118 template <class T>
00119 inline unsigned int GetAlignment(T *dummy=NULL)
00120 {
00121 #if (_MSC_VER >= 1300)
00122 return __alignof(T);
00123 #elif defined(__GNUC__)
00124 return __alignof__(T);
00125 #else
00126 return sizeof(T);
00127 #endif
00128 }
00129
00130 template <class T>
00131 inline bool IsPowerOf2(T n)
00132 {
00133 return n > 0 && (n & (n-1)) == 0;
00134 }
00135
00136 template <class T1, class T2>
00137 inline T2 ModPowerOf2(T1 a, T2 b)
00138 {
00139 assert(IsPowerOf2(b));
00140 return T2(a) & (b-1);
00141 }
00142
00143 inline bool IsAlignedOn(const void *p, unsigned int alignment)
00144 {
00145 return IsPowerOf2(alignment) ? ModPowerOf2((uintptr_t)p, alignment) == 0 : (uintptr_t)p % alignment == 0;
00146 }
00147
00148 template <class T>
00149 inline bool IsAligned(const void *p, T *dummy=NULL)
00150 {
00151 return IsAlignedOn(p, GetAlignment<T>());
00152 }
00153
00154 END_LIBEBML_NAMESPACE
00155
00156 #endif // LIBEBML_CRC32_H