home *** CD-ROM | disk | FTP | other *** search
/ Oracle Video Server 3.0.3.1 / OVS_3031_NT.iso / win32 / medianet / server / include / sysxcd.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-25  |  8.9 KB  |  288 lines

  1. /* Copyright (c) 1995 by Oracle Corporation.  All Rights Reserved.
  2.  *
  3.  * sysxcd.h - OMN System-Dependent Byte Ordering Manipulation
  4.  *
  5.  * This file provides the following macros for accessing data in various
  6.  * alignments and byte-orderings.
  7.  *
  8.  * SYSX_BYTE_ORDER:            either SYSX_MSB (0) or SYSX_LSB (1).
  9.  *
  10.  * Alignment macro:            sysxAlignPtr( ptr, boundary )
  11.  *
  12.  * Unaligned Access, native order:  sysxGet2, sysxGet4,
  13.  *                    sysxPut2, sysxPut4
  14.  *
  15.  * Unaligned Swap:            sysxGetSwap2, sysxGetSwap4,
  16.  *                    sysxPutSwap2, sysxPutSwap4
  17.  *
  18.  * Aligned Access, net order:        sysxGetB2, sysxGetB4,
  19.  *                    sysxPutB2, sysxPutB4
  20.  *
  21.  * Unaligned Access, net order:     sysxGetUaB2, sysxGetUaB4,
  22.  *                    sysxPutUaB2, sysxPutUaB4
  23.  */
  24.  
  25. #ifndef SYSXCD_ORACLE
  26. #define SYSXCD_ORACLE
  27.  
  28. #ifndef SYSX_ORACLE
  29. #include <sysx.h>
  30. #endif
  31.  
  32. EXTC_START
  33.  
  34. /*
  35.  * Canonical Extraction Macros, unaligned OK.
  36.  */
  37. #define sysxg2(buf, i, j) \
  38.   ((ub2) ((((ub2) ((buf)[i])) << 8) | (((ub2) ((buf)[j])))))
  39. #define sysxg4(buf, i, j, k, l) \
  40.   ((ub4) ((((ub4) ((buf)[i])) << 24) | (((ub4) ((buf)[j])) << 16) | \
  41.       (((ub4) ((buf)[k])) << 8) | ((ub4) (buf)[l])))
  42. #define sysxp2(buf, val, i, j) \
  43.   ((buf)[i] = (ub1) ((val) >> 8), (buf)[j] = (ub1) (val))
  44. #define sysxp4(buf, val, i, j, k, l) \
  45.   ((buf)[i] = (ub1) ((val) >> 24), (buf)[j] = (ub1) ((val) >> 16), \
  46.    (buf)[k] = (ub1) ((val) >> 8), (buf)[l] = (ub1) (val))
  47.  
  48. /*
  49.  * Byte Ordering
  50.  *
  51.  * SYNOPSIS
  52.  * const sword SYSX_BYTE_ORDER = { SYSX_MSB, SYSX_LSB };
  53.  *
  54.  * DESCRIPTION
  55.  * SYSX_BYTE_ORDER should be defined with the appropriate constant
  56.  * indicating the native representation of integers on the platform.
  57.  * Defining this constant correctly will automatically cause the
  58.  * correct definitions to be selected in the rest of this header.
  59.  *
  60.  * We note here, however, that some platforms (notably, Intel x86)
  61.  * offer single instructions that perform byte-swapping.  Also,
  62.  * some platforms have no alignment restrictions.  Thus, many of
  63.  * these macros may be optimized.
  64.  */
  65. #define SYSX_MSB          0             /* most-significant-byte-first order */
  66. #define SYSX_LSB          1            /* least-significant-byte-first order */
  67.  
  68. #define SYSX_BYTE_ORDER      SYSX_LSB
  69.  
  70. /*
  71.  * Pointer Alignment
  72.  *
  73.  * SYNOPSIS
  74.  * dvoid *sysxAlignPtr(dvoid *ptr, size_t boundary);
  75.  *
  76.  * DESCRIPTION
  77.  * sysxAlignPtr() aligns the given pointer up to the given boundary.
  78.  * The returned pointer should always be greater than or equal to
  79.  * that which was passed in.
  80.  */
  81. #ifdef lint
  82. dvoid *sysxAlignPtr(dvoid *ptr, size_t boundary);
  83. #else
  84. #define sysxAlignPtr(p, b)  ((dvoid *) ((((ub4) (p)) + ((b)-1)) & ~((b)-1)))
  85. #endif
  86.  
  87.  
  88. /*
  89.  * Unaligned Access, native order
  90.  *
  91.  * SYNOPSIS
  92.  * ub2  sysxGet2(ub1 *buf);
  93.  * ub4  sysxGet4(ub1 *buf);
  94.  * void sysxPut2(ub1 *buf, ub2 val);
  95.  * void sysxPut4(ub1 *buf, ub4 val);
  96.  *
  97.  * DESCRIPTION
  98.  * These routines provide simple access to integer scalars at unaligned
  99.  * addresses.  The scalar in the buffer is assumed to be in the machine's
  100.  * native representation.
  101.  *
  102.  * For example, given
  103.  *
  104.  *    buf:  0xaa 0xbb
  105.  *
  106.  * Then, on an LSB machine, sysxGet2() returns the value 0xbbaa regardless
  107.  * of the alignment of buf.
  108.  */
  109. #ifdef lint
  110.  
  111. ub2  sysxGetB2(ub1 *buf);
  112. ub4  sysxGetB4(ub1 *buf);
  113. void sysxPutB2(ub1 *buf, ub2 val);
  114. void sysxPutB4(ub1 *buf, ub4 val);
  115.  
  116. #elif SYSX_BYTE_ORDER == SYSX_MSB
  117.  
  118. #define sysxGet2(buf)      sysxg2(buf, 0, 1)
  119. #define sysxGet4(buf)      sysxg4(buf, 0, 1, 2, 3)
  120. #define sysxPut2(buf, val) sysxp2(buf, val, 0, 1)
  121. #define sysxPut4(buf, val) sysxp4(buf, val, 0, 1, 2, 3)
  122.  
  123. #elif SYSX_BYTE_ORDER == SYSX_LSB
  124.  
  125. #define sysxGet2(buf)      sysxg2(buf, 1, 0)
  126. #define sysxGet4(buf)      sysxg4(buf, 3, 2, 1, 0)
  127. #define sysxPut2(buf, val) sysxp2(buf, val, 1, 0)
  128. #define sysxPut4(buf, val) sysxp4(buf, val, 3, 2, 1, 0)
  129.  
  130. #else
  131. #error "ill-defined SYSX_BYTE_ORDER constant"
  132. #endif
  133.  
  134. /*
  135.  * Byte Swapping
  136.  *
  137.  * SYNOPSIS
  138.  *
  139.  * ub2  sysxGetSwap2(ub1 *buf);
  140.  * ub4  sysxGetSwap4(ub1 *buf);
  141.  * void sysxPutSwap2(ub1 *buf, ub2 val);
  142.  * void sysxPutSwap4(ub1 *buf, ub4 val);
  143.  *
  144.  * DESCRIPTION
  145.  * These routines provide data access to integers that are represented
  146.  * with bytes that are swapped with respect to the native representation.
  147.  * The scalar is always in native form, and the buffer points to the non-
  148.  * native form.
  149.  *
  150.  * sysxGetSwap2() and sysxGetSwap4() read a two- or four-byte integer
  151.  * stored in a swapped form from the position pointed to by buf and
  152.  * return the value.  sysxPutSwap2() and sysxPutSwap4() write val to
  153.  * the position pointed to by buf in swapped form.  The buf pointer
  154.  * does not need any particular alignment.
  155.  *
  156.  * For example, given
  157.  *
  158.  *    buf:  0xaa 0xbb
  159.  *
  160.  * Then, on an LSB machine, sysxGetSwap2() returns the value 0xaabb.
  161.  *
  162.  * In conjunction with the SYSX_BYTE_ORDER symbol, one can force
  163.  * a write of network (MSB) order with the following:
  164.  *
  165.  * if (SYSX_BYTE_ORDER == SYSX_MSB)
  166.  *   *((ub2 *) buf) = val;   // this is safe only if buf is aligned
  167.  * else
  168.  *   sysxPutSwap2(buf, val);
  169.  *
  170.  * A normal compiler will eliminate the compile-time condition check
  171.  * and only one generate code for one or the other assignment.
  172.  */
  173. #ifdef lint
  174.  
  175. ub2  sysxGetSwap2(ub1 *buf);
  176. ub4  sysxGetSwap4(ub1 *buf);
  177. void sysxPutSwap2(ub1 *buf, ub2 val);
  178. void sysxPutSwap4(ub1 *buf, ub4 val);
  179.  
  180. #elif SYSX_BYTE_ORDER == SYSX_MSB
  181.  
  182. #define sysxGetSwap2(buf)      sysxg2(buf, 1, 0)
  183. #define sysxGetSwap4(buf)      sysxg4(buf, 3, 2, 1, 0)
  184. #define sysxPutSwap2(buf, val) sysxp2(buf, val, 1, 0)
  185. #define sysxPutSwap4(buf, val) sysxp4(buf, val, 3, 2, 1, 0)
  186.  
  187. #elif SYSX_BYTE_ORDER == SYSX_LSB
  188.  
  189. #define sysxGetSwap2(buf)      sysxg2(buf, 0, 1)
  190. #define sysxGetSwap4(buf)      sysxg4(buf, 0, 1, 2, 3)
  191. #define sysxPutSwap2(buf, val) sysxp2(buf, val, 0, 1)
  192. #define sysxPutSwap4(buf, val) sysxp4(buf, val, 0, 1, 2, 3)
  193.  
  194. #else
  195. #error "ill-defined SYSX_BYTE_ORDER constant"
  196. #endif
  197.  
  198. /*
  199.  * NAME
  200.  * sysxGetB2, sysxGetB4, sysxPutB2, sysxPutB4 - host/network conversion
  201.  *
  202.  * SYNOPSIS
  203.  * ub2   sysxGetB2(ub1 *buf);
  204.  * ub4   sysxGetB4(ub1 *buf);
  205.  * void  sysxPutB2(ub1 *buf, ub2 val);
  206.  * void  sysxPutB4(ub1 *buf, ub4 val);
  207.  *
  208.  * DESCRIPTION
  209.  * These routines convert between host integer data and network integer
  210.  * data.  sysxGetB2() and sysxGetB4() are expected to read a two- or four-
  211.  * byte integer stored in network format from the position pointed to by
  212.  * buf and return the value.  sysxPutB2() and sysxPutB4() are expected to
  213.  * write val to the position pointed to by buf in network format.  In any
  214.  * case, buf must always be "naturally aligned" for the data type.
  215.  *
  216.  * Network format for a two-byte integer consists of exactly two bytes
  217.  * where the first byte is the high 8 bits and the second byte is the
  218.  * low 8 bits.  Network format for a four-byte integer is similar.
  219.  * The definitions for the corresponding unaligned access below may
  220.  * be used for the aligned access as well and will work on any platform,
  221.  * but faster implementations may be discovered.
  222.  */
  223. #ifdef lint
  224.  
  225. ub2  sysxGetB2(ub1 *buf);
  226. ub4  sysxGetB4(ub1 *buf);
  227. void sysxPutB2(ub1 *buf, ub2 val);
  228. void sysxPutB4(ub1 *buf, ub4 val);
  229.  
  230. #elif SYSX_BYTE_ORDER == SYSX_MSB
  231.  
  232. /* only works when aligned */
  233. #define sysxGetB2(buf)       *((ub2 *) (buf))
  234. #define sysxGetB4(buf)       *((ub4 *) (buf))
  235. #define sysxPutB2(buf, val)  (*((ub2 *) (buf)) = (val))
  236. #define sysxPutB4(buf, val)  (*((ub4 *) (buf)) = (val))
  237.  
  238. #elif SYSX_BYTE_ORDER == SYSX_LSB
  239.  
  240. /* swapping, aligned or unaligned */
  241. #define sysxGetB2(buf)      sysxGetSwap2(buf)
  242. #define sysxGetB4(buf)      sysxGetSwap4(buf)
  243. #define sysxPutB2(buf, val) sysxPutSwap2(buf,val)
  244. #define sysxPutB4(buf, val) sysxPutSwap4(buf,val)
  245.  
  246. #else
  247. #error "ill-defined SYSX_BYTE_ORDER constant"
  248. #endif
  249.  
  250. /*
  251.  * NAME
  252.  * sysxGetUaB2, sysxGetUaB4, sysxPutUaB2, sysxPutUaB4 - get/put unaligned data
  253.  *
  254.  * DESCRIPTION
  255.  * These routines perform the same function as above except they handle
  256.  * unaligned pointers.  These may be slower than the above functions, so
  257.  * only use them if your data is unaligned.  Note that these always convert
  258.  * between native form (the scalar value put or returned), and MSB form
  259.  * (the value in the buffer pointer).
  260.  */
  261. #ifdef lint
  262.  
  263. ub2  sysxGetUaB2(ub1 *buf);
  264. ub4  sysxGetUaB4(ub1 *buf);
  265. void sysxPutUaB2(ub1 *buf, ub2 val);
  266. void sysxPutUaB4(ub1 *buf, ub4 val);
  267.  
  268. #elif SYSX_BYTE_ORDER == SYSX_MSB
  269.  
  270. #define sysxGetUaB2(buf)    sysxGet2(buf)
  271. #define sysxGetUaB4(buf)    sysxGet4(buf)
  272. #define sysxPutUaB2(buf, val)   sysxPut2(buf,val)
  273. #define sysxPutUaB4(buf, val)   sysxPut4(buf,val)
  274.  
  275. #elif SYSX_BYTE_ORDER == SYSX_LSB
  276.  
  277. /* because we swap, same as aligned variety. */
  278. #define sysxGetUaB2(buf)    sysxGetSwap2(buf)
  279. #define sysxGetUaB4(buf)    sysxGetSwap4(buf)
  280. #define sysxPutUaB2(buf, val)   sysxPutSwap2(buf,val)
  281. #define sysxPutUaB4(buf, val)   sysxPutSwap4(buf,val)
  282.  
  283. #endif
  284.  
  285. EXTC_END
  286. #endif /* SYSXCD_ORACLE */
  287.  
  288.