RZ

001: // RZNetOrder.hh # trivial network byte ordering inlines
002:
003: #ifndef _RZNetOrder_HH
004: #define _RZNetOrder_HH 1
005:
006: namespace RZ {
007:     template< typename ByteIter > // char const *
008:     inline unsigned int net_in(ByteIter const buffer)
009:     {
010:         unsigned int n = (unsigned char)buffer[ 0 ];
011:         n <<= 8; n |= (unsigned char)buffer[ 1 ];
012:         n <<= 8; n |= (unsigned char)buffer[ 2 ];
013:         n <<= 8; n |= (unsigned char)buffer[ 3 ];
014:         return n;
015:     }
016:
017:     template< typename ByteIter > // char *
018:     inline void net_out(ByteIter const buffer, unsigned int n)
019:     {
020:         /*(unsigned char)*/buffer[ 3 ] = n; n >>= 8;
021:         /*(unsigned char)*/buffer[ 2 ] = n; n >>= 8;
022:         /*(unsigned char)*/buffer[ 1 ] = n; n >>= 8;
023:         /*(unsigned char)*/buffer[ 0 ] = n;
024:     }
025:
026:     /****** ******* ******* ******* ******* ******* ******/
027:
028:     template< typename ByteIter > inline void n_in(ByteIter const buf,
029:         unsigned int &a0, unsigned int &a1)
030:     {
031:         a0 = net_in(buf     ); a1 = net_in(buf +  4);
032:     }
033:     template< typename ByteIter > inline void n_in(ByteIter const buf,
034:         unsigned int &a0, unsigned int &a1, unsigned int &a2)
035:     {
036:         a0 = net_in(buf     ); a1 = net_in(buf +  4); a2 = net_in(buf +  8);
037:     }
038:     template< typename ByteIter > inline void n_in(ByteIter const buf,
039:         unsigned int &a0, unsigned int &a1, unsigned int &a2, unsigned int &a3)
040:     {
041:         a0 = net_in(buf     ); a1 = net_in(buf +  4);
042:         a2 = net_in(buf +  8); a3 = net_in(buf + 12);
043:     }
044:     template< typename ByteIter > inline void n_in(ByteIter const buf,
045:         unsigned int &a0, unsigned int &a1, unsigned int &a2, unsigned int &a3,
046:         unsigned int &a4, unsigned int &a5, unsigned int &a6, unsigned int &a7)
047:     {
048:         a0 = net_in(buf     ); a1 = net_in(buf +  4);
049:         a2 = net_in(buf +  8); a3 = net_in(buf + 12);
050:         a4 = net_in(buf + 16); a5 = net_in(buf + 20);
051:         a6 = net_in(buf + 24); a7 = net_in(buf + 28);
052:     }
053:
054:     template< typename ByteIter > inline void n_out(ByteIter const buf,
055:         unsigned int a0, unsigned int a1)
056:     {
057:         net_out(buf     , a0); net_out(buf +  4, a1);
058:     }
059:     template< typename ByteIter > inline void n_out(ByteIter const buf,
060:         unsigned int a0, unsigned int a1, unsigned int a2)
061:     {
062:         net_out(buf     , a0); net_out(buf +  4, a1); net_out(buf +  8, a2);
063:     }
064:     template< typename ByteIter > inline void n_out(ByteIter const buf,
065:         unsigned int a0, unsigned int a1, unsigned int a2, unsigned int a3)
066:     {
067:         net_out(buf     , a0); net_out(buf +  4, a1);
068:         net_out(buf +  8, a2); net_out(buf + 12, a3);
069:     }
070:     template< typename ByteIter > inline void n_out(ByteIter const buf,
071:         unsigned int a0, unsigned int a1, unsigned int a2, unsigned int a3,
072:         unsigned int a4, unsigned int a5, unsigned int a6, unsigned int a7)
073:     {
074:         net_out(buf     , a0); net_out(buf +  4, a1);
075:         net_out(buf +  8, a2); net_out(buf + 12, a3);
076:         net_out(buf + 16, a4); net_out(buf + 20, a5);
077:         net_out(buf + 24, a6); net_out(buf + 28, a7);
078:     }
079:
080:     /****** ******* ******* ******* ******* ******* ******/
081:
082:     template< class T, typename ByteIter > T net2in(ByteIter const b)
083:     {
084:         return T(net_in(b), net_in(b + 4));
085:     }
086:
087:     template< class T, typename ByteIter > T net3in(ByteIter const b)
088:     {
089:         return T(net_in(b), net_in(b + 4), net_in(b + 8));
090:     }
091:
092:     template< class T, typename ByteIter > T net4in(ByteIter const b)
093:     {
094:         return T(net_in(b), net_in(b + 4), net_in(b + 8), net_in(b + 12));
095:     }
096: }
097:
098: // RZNetOrder.hh # trivial network byte ordering inlines
099:
100: #endif