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 #ifndef __SEAL_BYTE_VECTOR_H__ 00030 #define __SEAL_BYTE_VECTOR_H__ 00031 00032 #include "exception.h" 00033 #include "array1d.h" 00034 00035 namespace seal 00036 { // Start of namespace seal 00037 00041 typedef unsigned char Byte; 00042 00047 template <unsigned int _bytecount_> 00048 class ByteVector 00049 { 00050 public: 00053 ByteVector(void) {} 00054 00057 template <unsigned int bytes> 00058 ByteVector<_bytecount_>& operator=(const ByteVector<bytes> &vec); 00059 00062 ByteVector<_bytecount_>& operator=(const ByteVector<_bytecount_> &rhs); 00063 00066 Byte& operator[](int index) throw (OutOfRangeException<int>) 00067 { 00068 if (index < 0 || index >= _bytecount_) 00069 throw OutOfRangeException<int>(0, _bytecount_-1, index); 00070 00071 return data[index]; 00072 } 00073 00076 const Byte& operator[](int index) const throw (OutOfRangeException<int>) 00077 { 00078 if (index < 0 || index >= _bytecount_) 00079 throw OutOfRangeException<int>(0, _bytecount_-1, index); 00080 00081 return data[index]; 00082 } 00083 00084 protected: 00085 Byte data[_bytecount_]; 00086 }; 00087 00088 template <unsigned int _bytecount_> 00089 template <unsigned int bytes> 00090 ByteVector<_bytecount_>& ByteVector<_bytecount_>::operator=(const ByteVector<bytes> &vec) 00091 { 00092 unsigned int min = bytes; 00093 if (min > _bytecount_) 00094 min = _bytecount_; 00095 else 00096 { 00097 for (int i = min; i < _bytecount_; i++) 00098 data[i] = 0; 00099 } 00100 00101 for (int i = 0; i < min; i++) 00102 data[i] = vec[i]; 00103 00104 return *this; 00105 } // ByteVector<_bytecount_>::operator= 00106 00107 template <unsigned int _bytecount_> 00108 ByteVector<_bytecount_>& ByteVector<_bytecount_>::operator=(const ByteVector<_bytecount_> &rhs) 00109 { 00110 for (int i = 0; i < _bytecount_; i++) 00111 data[i] = rhs.data[i]; 00112 00113 return *this; 00114 } // ByteVector<_bytecount_>::operator= 00115 00118 00121 class QuadByte : public ByteVector<4> 00122 { 00123 public: 00126 QuadByte(void) : ByteVector<4>() {} 00127 00130 QuadByte& operator=(unsigned int rhs) 00131 { 00132 for (int i = 0; i < 4; i++) 00133 data[i] = (rhs >> (i << 3)) & 0xff; 00134 00135 return *this; 00136 } // QuadByte::operator= 00137 00140 QuadByte& operator=(QuadByte& rhs) 00141 { 00142 for (int i = 0; i < 4; i++) 00143 { 00144 data[i] = rhs.data[i]; 00145 } 00146 } 00147 00150 inline operator unsigned int() 00151 { 00152 unsigned int val = 0; 00153 for (int i = 0; i < 4; i++) 00154 { 00155 unsigned int byte = data[i]; 00156 val += (byte << (i << 3)); 00157 } 00158 00159 return val; 00160 } // operator unsigned int 00161 00162 }; // class QuadByte 00163 00166 00169 class HexByte : public Array1D< QuadByte > 00170 { 00171 public: 00174 HexByte(void) : Array1D< QuadByte >(4) {} 00175 00178 HexByte& operator=(HexByte& rhs) 00179 { 00180 for (int i = 0; i < 4; i++) 00181 { 00182 (*this)[i] = rhs[i]; 00183 } 00184 } 00185 00186 }; // class HexByte 00187 00188 } // End of namespace seal 00189 00190 #endif // __SEAL_BYTE_VECTOR_H__ 00191