/* * Copyright (c) 2001 by Cliff Green. All rights reserved. Individual files * may be covered by other copyrights (as noted in the file itself). * * Redistribution and use in source and binary forms are permitted * provided that this entire copyright notice is duplicated in all such * copies. * * This software is provided "as is" and without any expressed or implied * warranties, including, without limitation, the implied warranties of * merchantibility and fitness for any particular purpose. */ //---------------------------------------------------------------------- // Source file: align.h // Written by: Cliff Green, 2001 // Compiler: Metrowerks CodeWarrior Pro 6 // History: // Modified: 8/15/2001 // By: Cliff Green // Comments: Original version. //---------------------------------------------------------------------- // This commentheader supports documentation tools such as Doc++ and // Doxygen: http://www.doxygen.org/ //---------------------------------------------------------------------- /// Template functions for swapping bytes and word alignment. /** * Template functions for swapping 2 or 4 bytes, as well as aligning * unsigned char buffers into a 2 or 4 byte word boundary (many * processor architectures do not support casting a char pointer into * an integral value unless the the char address is aligned correctly). * Note that the alignment functions require compilers that support * explicit template function call syntax (since the template parameter * is only used as the return value type). * * Functions: * swap4bytes: takes value, returns value with 4 bytes swapped * swap2bytes: takes value, returns value with 2 bytes swapped * align4bytes: returns 4 byte val, given array of 4 unsigned chars * align2bytes: returns 2 byte val, given array of 2 unsigned chars * * Pre-conditions and post-conditions should be obvious from function * usage. The main requirement on the template type is that default * construction is allowed (these functions are meant to be used for * fundamental types only, so this should not be an issue). * * Note that these functions are inherently platform specific, and contains * behavior that is undefined by language standards. * */ #ifndef ALIGN_H #define ALIGN_H namespace Util { template T swap4bytes(T i) { T o; *(reinterpret_cast(&o)+0) = *(reinterpret_cast(&i)+3); *(reinterpret_cast(&o)+1) = *(reinterpret_cast(&i)+2); *(reinterpret_cast(&o)+2) = *(reinterpret_cast(&i)+1); *(reinterpret_cast(&o)+3) = *(reinterpret_cast(&i)+0); return o; } template T swap2bytes(T i) { T o; *(reinterpret_cast(&o)+0) = *(reinterpret_cast(&i)+1); *(reinterpret_cast(&o)+1) = *(reinterpret_cast(&i)+0); return o; } template T align4bytes(const unsigned char* p) { T o; *(reinterpret_cast(&o) + 0) = *(p + 0); *(reinterpret_cast(&o) + 1) = *(p + 1); *(reinterpret_cast(&o) + 2) = *(p + 2); *(reinterpret_cast(&o) + 3) = *(p + 3); return o; } template T align2bytes(const unsigned char* p) { T o; *(reinterpret_cast(&o) + 0) = *(p + 0); *(reinterpret_cast(&o) + 1) = *(p + 1); return o; } } // end namespace #endif