[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    Re: Byte order conversion functions
From:       Szombathelyi_György <gyurco () freemail ! hu>
Date:       2004-10-28 11:14:43
Message-ID: 4180D4A3.40000 () freemail ! hu
[Download RAW message or body]

Brad Hards wrote:

> On Thu, 28 Oct 2004 17:14 pm, Szombathelyi György wrote:
> 
>>Yesterday I started a thread about it on kde-devel (sorry for the
>>cross-posting):
>>http://lists.kde.org/?l=kde-devel&m=109888101623409&w=2
>>
>>Is this acceptable?
> 
> API documentation?
> 
Ok, here's a new version. The api docs are added to the modules section. 
  I hope that the summary is enough, it's not to easy to add separate 
documentation to several #ifdef encapsulated functions.

Bye,
György


["kswap.h" (text/x-chdr)]

#ifndef KSWAP_H
#define KSWAP_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <qglobal.h>

/** 
 * \defgroup KSWAP Byte-swapping functions and macros
 * kswap.h contains functions that will help converting
 * 16, 32 and 64 bit lenght data between little-endian and
 * big-endian representations.
 *
 * The KSWAP_16, KSWAP_32 and KSWAP_64 functions are always
 * swaps the byte order of the supplied argument (which should be
 * 16, 32 or 64 bit wide). These functions are inline, and tries to
 * use the most optimized function of the underlying system
 * (bswap_xx functions from byteswap.h in GLIBC, or ntohs and ntohl
 * on little-endian machines, and if neither are applicable, some fast
 * custom code).
 *
 * The KBE2ME_xx, KLE2ME_xx, KME2BE_xx and KME2LE macros, where xx can be
 * 16,32 or 64 are for converting big-endian and little-endian data
 * to and from the machine endianness.
 */

#ifdef HAVE_BYTESWAP_H
#include <byteswap.h>

  static inline Q_UINT16 KSWAP_16( Q_UINT16 b ) { return bswap_16( b ); }
  static inline Q_INT16 KSWAP_16( Q_INT16 b ) { return bswap_16( (Q_UINT16)b ); }
  static inline Q_UINT32 KSWAP_32( Q_UINT32 b ) { return bswap_32( b ); }
  static inline Q_INT32 KSWAP_32( Q_INT32 b ) { return bswap_32( (Q_UINT32)b ); }
  static inline Q_UINT64 KSWAP_64( Q_UINT64 b ) { return bswap_64( b ); }
  static inline Q_INT64 KSWAP_64( Q_INT64 b ) { return bswap_64( (Q_UINT64)b ); }

#else /* HAVE_BYTESWAP_H */
#ifdef WORDS_BIGENDIAN
  static inline Q_UINT16 KSWAP_16( Q_UINT16 b ) 
  { 
    return (((b) & 0x00ff) << 8 | ((b) & 0xff00) >> 8); 
  }

  static inline Q_INT16 KSWAP_16( Q_INT16 b ) 
  { 
    return ((((Q_UINT16)b) & 0x00ff) << 8 | (((Q_UINT16)b) & 0xff00) >> 8); 
  }

  static inline Q_UINT32 KSWAP_32( Q_UINT32 b ) 
  {
    return
      ((((b) & 0xff000000) >> 24) | (((b) & 0x00ff0000) >>  8) | \
       (((b) & 0x0000ff00) <<  8) | (((b) & 0x000000ff) << 24)); 
  }

  static inline Q_INT32 KSWAP_32( Q_INT32 b ) 
  {
    return 
      (((((Q_UINT32)b) & 0xff000000) >> 24) | ((((Q_UINT32)b) & 0x00ff0000) >>  8) | \
       ((((Q_UINT32)b) & 0x0000ff00) <<  8) | ((((Q_UINT32)b) & 0x000000ff) << 24)); 
  }
#else /* WORDS_BIGENDIAN */
#include <sys/types.h>
#include <netinet/in.h>

  static inline Q_UINT16 KSWAP_16( Q_UINT16 b ) { return htons(b); }
  static inline Q_INT16 KSWAP_16( Q_INT16 b ) { return htons((Q_UINT16)b); }
  static inline Q_UINT32 KSWAP_32( Q_UINT32 b ) { return htonl(b); }
  static inline Q_INT32 KSWAP_32( Q_INT32 b ) { return htonl((Q_UINT32)b); }
#endif
  static inline Q_UINT64 KSWAP_64( Q_UINT64 b ) 
  {
    union { 
        Q_UINT64 ll;
        Q_UINT32 l[2]; 
    } w, r;
    w.ll = b;
    r.l[0] = KSWAP_32( w.l[1] );
    r.l[1] = KSWAP_32( w.l[0] );
    return r.ll;
  }

  static inline Q_INT64 KSWAP_64( Q_INT64 b ) 
  {
    union { 
        Q_UINT64 ll;
        Q_UINT32 l[2]; 
    } w, r;
    w.ll = (Q_UINT64) b;
    r.l[0] = KSWAP_32( w.l[1] );
    r.l[1] = KSWAP_32( w.l[0] );
    return r.ll;
  }
#endif	/* !HAVE_BYTESWAP_H */

// KBE2ME ... BigEndian to MachineEndian
// KLE2ME ... LittleEndian to MachineEndian

#ifdef WORDS_BIGENDIAN
#define KBE2ME_16(x) (x)
#define KBE2ME_32(x) (x)
#define KBE2ME_64(x) (x)
#define KLE2ME_16(x) KSWAP_16(x)
#define KLE2ME_32(x) KSWAP_32(x)
#define KLE2ME_64(x) KSWAP_64(x)
#else
#define KBE2ME_16(x) KSWAP_16(x)
#define KBE2ME_32(x) KSWAP_32(x)
#define KBE2ME_64(x) KSWAP_64(x)
#define KLE2ME_16(x) (x)
#define KLE2ME_32(x) (x)
#define KLE2ME_64(x) (x)
#endif

// KME2BE ... MachineEndian to BigEndian
// KME2LE ... MachineEndian to LittleEndian

#define KME2BE_16(x) KBE2ME_16(x)
#define KME2BE_32(x) KBE2ME_32(x)
#define KME2BE_64(x) KBE2ME_64(x)
#define KME2LE_16(x) KLE2ME_16(x)
#define KME2LE_32(x) KLE2ME_32(x)
#define KME2LE_64(x) KLE2ME_64(x)

#endif /* KSWAP_H */


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic