home *** CD-ROM | disk | FTP | other *** search
/ Internet Publisher's Toolbox 2.0 / Internet Publisher's Toolbox.iso / internet / ntserver / wtsource / byte_ord.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-14  |  4.0 KB  |  135 lines

  1.  
  2. /* Copyright (c) CNIDR (see ../COPYRIGHT) */
  3.  
  4. /* byte_order:
  5.  *  This file contains some useful routines to handle the differences between
  6.  *  big and little endian machines. The macros may look a little unwieldy, but
  7.  *  any modern optimiser will take care of things quite nicely. 
  8.  *
  9.  * $Log: byte_order.h,v $
  10.  * Revision 1.1  1993/02/16  15:05:35  freewais
  11.  * Initial revision
  12.  *
  13.  * Revision 1.1  92/03/15  08:49:03  jonathan
  14.  * Initial revision
  15.  * 
  16.  */
  17.  
  18. #ifdef WIN32
  19.  
  20. /* Include the correct configuration file for the machine architecture */
  21. #ifdef _X86_
  22. #include "..\config-I386.h"
  23. #endif
  24. #ifdef _MIPS_
  25. #include "..\config-MIPS.h"
  26. #endif
  27. #ifdef _ALPHA_
  28. #include "..\config-ALPHA.h"
  29. #endif
  30.  
  31. #else
  32.  
  33. #include "../config.h"
  34.  
  35. #endif /* WIN32 */
  36.  
  37. /* ASSIGN_HELPER:
  38.     This macro turns a pointer to long into a pointer to a {1,2,3} byte value
  39. */
  40.  
  41. #ifdef WAIS_BIG_ENDIAN
  42. #define ASSIGN_HELPER(var,size) (FOUR_BYTE*)((ONE_BYTE*) &var+(sizeof(FOUR_BYTE)-size))
  43. #else
  44. #define ASSIGN_HELPER(var,size)  (FOUR_BYTE *)&var
  45. #endif 
  46.  
  47. /*
  48.   ASSIGN(var,size,src,unit,offset): 
  49.      var : variable to store into
  50.      size: how big is value?
  51.      src : source address
  52.      unit,offset: 
  53.       These paramaters are used to help optimise the macro when used with 
  54.       structured data. unit should be the size of one record, and offset should
  55.       be the offset of this item within that record. These paramaters are *not*
  56.       used to calculate the source address - they are used purely to let the
  57.       compiler take advantage of possibly aligned data. 
  58.  
  59.       ASSIGN_NATIVE uses native byte ordering
  60.       ASSIGN_CANON  uses canonical (big_endian) byte ordering
  61. */
  62.  
  63. #if defined(NATIVE_ORDER) || defined(WAIS_BIG_ENDIAN)
  64. #define ASSIGN ASSIGN_NATIVE
  65. #else
  66. #define ASSIGN ASSIGN_CANON
  67. #endif
  68.  
  69. #define ASSIGN_NATIVE(var,size,src,unit,offset) {\
  70.   FOUR_BYTE *dst;\
  71.     dst = ASSIGN_HELPER(var,size);\
  72.   switch(size) \
  73.     {\
  74.     case 1:\
  75.       var = 0;\
  76.       *(unsigned ONE_BYTE*)dst= *(unsigned ONE_BYTE*)(src);\
  77.       break;\
  78.     case 2:\
  79.       if (!(unit % TWO_BYTE_ALIGN) && !(offset % TWO_BYTE_ALIGN)) {\
  80.         var = 0;\
  81.     *(unsigned TWO_BYTE *)dst = *(unsigned TWO_BYTE *)(src);\
  82.       } else {\
  83.     var =0;\
  84.     *(unsigned ONE_BYTE *)dst = *(unsigned ONE_BYTE *)(src);\
  85.     *((unsigned ONE_BYTE *)dst+1) = *(unsigned ONE_BYTE*)(src+1);\
  86.       }\
  87.       break;\
  88.     case 3:\
  89.       var = 0;\
  90.       *(unsigned ONE_BYTE *)dst = *(unsigned ONE_BYTE *)(src);\
  91.       *((unsigned ONE_BYTE *)dst+1) = *(unsigned ONE_BYTE*)(src+1);\
  92.       *((unsigned ONE_BYTE *)dst+2) = *(unsigned ONE_BYTE*)(src+2);\
  93.       break;\
  94.     case 4:\
  95.       if (!(unit % FOUR_BYTE_ALIGN) && !(offset % FOUR_BYTE_ALIGN)) { \
  96.     *dst = *(unsigned FOUR_BYTE *)(src);\
  97.       } else {\
  98.     *(unsigned ONE_BYTE *)dst = *(unsigned ONE_BYTE *)(src);\
  99.     *((unsigned ONE_BYTE *)dst+1) = *(unsigned ONE_BYTE*)(src+1);\
  100.     *((unsigned ONE_BYTE *)dst+2) = *(unsigned ONE_BYTE*)(src+2);\
  101.     *((unsigned ONE_BYTE *)dst+3) = *(unsigned ONE_BYTE*)(src+3);\
  102.       }\
  103.       break;\
  104.     }\
  105. }
  106.  
  107. #define ASSIGN_CANON(var,size,src,unit,offset) {\
  108.   FOUR_BYTE *dst;\
  109.     dst = ASSIGN_HELPER(var,size);\
  110.   switch(size) \
  111.     {\
  112.     case 1:\
  113.       var = 0;\
  114.       *(unsigned ONE_BYTE*)dst= *(unsigned ONE_BYTE*)(src);\
  115.       break;\
  116.     case 2:\
  117.       var =0;\
  118.       *(unsigned ONE_BYTE *)dst = *(unsigned ONE_BYTE *)(src+1);\
  119.       *((unsigned ONE_BYTE *)dst+1) = *(unsigned ONE_BYTE*)(src);\
  120.       break;\
  121.     case 3:\
  122.       var = 0;\
  123.       *(unsigned ONE_BYTE *)dst = *(unsigned ONE_BYTE *)(src+2);\
  124.       *((unsigned ONE_BYTE *)dst+1) = *(unsigned ONE_BYTE*)(src+1);\
  125.       *((unsigned ONE_BYTE *)dst+2) = *(unsigned ONE_BYTE*)(src);\
  126.       break;\
  127.     case 4:\
  128.       *(unsigned ONE_BYTE *)dst = *(unsigned ONE_BYTE *)(src+3);\
  129.       *((unsigned ONE_BYTE *)dst+1) = *(unsigned ONE_BYTE*)(src+2);\
  130.       *((unsigned ONE_BYTE *)dst+2) = *(unsigned ONE_BYTE*)(src+1);\
  131.       *((unsigned ONE_BYTE *)dst+3) = *(unsigned ONE_BYTE*)(src);\
  132.       break;\
  133.     }\
  134. }
  135.