Changeset 94af885


Ignore:
Timestamp:
08/21/06 21:04:59 (14 years ago)
Author:
Perry Lorier <perry@…>
Branches:
4.0.1-hotfixes, cachetimestamps, develop, dpdk-ndag, etsilive, getfragoff, help, libtrace4, master, ndag_format, pfring, rc-4.0.1, rc-4.0.2, rc-4.0.3, rc-4.0.4, ringdecrementfix, ringperformance, ringtimestampfixes
Children:
c0cd256
Parents:
1034a10
Message:

Add new API for dealing with Big endian or Little Endian numbers on the
wrong endian machine.

Location:
lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • lib/format_helper.c

    r306f91c r94af885  
    272272}
    273273
     274uint64_t byteswap64(uint64_t num)
     275{
     276        return (byteswap32((num&0xFFFFFFFF00000000ULL)>>32))
     277              |((uint64_t)byteswap32(num&0x00000000FFFFFFFFULL)<<32);
     278}
     279
     280uint32_t byteswap32(uint32_t num)
     281{
     282        return ((num&0x000000FFU)<<24)
     283                | ((num&0x0000FF00U)<<8)
     284                | ((num&0x00FF0000U)>>8)
     285                | ((num&0xFF000000U)>>24);
     286}
     287
     288uint16_t byteswap16(uint16_t num)
     289{
     290        return ((num<<8)&0xFF00)|((num>>8)&0x00FF);
     291}
     292
  • lib/libtrace_int.h

    re5c2bc4 r94af885  
    361361bool demote_packet(libtrace_packet_t *packet);
    362362
     363
     364uint64_t byteswap64(uint64_t num);
     365uint32_t byteswap32(uint32_t num);
     366uint16_t byteswap16(uint16_t num);
     367
     368/* Because some traces/protocols are defined as
     369 * being "big endian" or "little endian" we have
     370 * this series of macros.
     371 */
     372#if BYTE_ORDER == BIG_ENDIAN
     373#define bswap_host_to_be64(num) ((uint64_t)(num))
     374#define bswap_host_to_le64(num) byteswap64(num)
     375#define bswap_host_to_be32(num) ((uint32_t)(num))
     376#define bswap_host_to_le32(num) byteswap32(num)
     377#define bswap_host_to_be16(num) ((uint16_t)(num))
     378#define bswap_host_to_le16(num) byteswap16(num)
     379
     380#define bswap_be_to_host64(num) ((uint64_t)(num))
     381#define bswap_le_to_host64(num) byteswap64(num)
     382#define bswap_be_to_host32(num) ((uint32_t)(num))
     383#define bswap_le_to_host32(num) byteswap32(num)
     384#define bswap_be_to_host16(num) ((uint16_t)(num))
     385#define bswap_le_to_host16(num) byteswap16(num)
     386
     387/* We use ntoh*() here, because the compiler may
     388 * attempt to optimise it
     389 */
     390#elif BYTE_ORDER == LITTLE_ENDIAN
     391#define bswap_host_to_be64(num) (byteswap64(num))
     392#define bswap_host_to_le64(num) ((uint64_t)(num))
     393#define bswap_host_to_be32(num) (htonl(num))
     394#define bswap_host_to_le32(num) ((uint32_t)(num))
     395#define bswap_host_to_be16(num) (htons(num))
     396#define bswap_host_to_le16(num) ((uint16_t)(num))
     397
     398#define bswap_be_to_host64(num) (byteswap64(num))
     399#define bswap_le_to_host64(num) ((uint64_t)(num))
     400#define bswap_be_to_host32(num) (ntohl(num))
     401#define bswap_le_to_host32(num) ((uint32_t)(num))
     402#define bswap_be_to_host16(num) (ntohs(num))
     403#define bswap_le_to_host16(num) ((uint16_t)(num))
     404
     405#else
     406#error "Unknown byte order"
     407#endif
     408
    363409#ifdef HAVE_BPF
    364410/* A type encapsulating a bpf filter
Note: See TracChangeset for help on using the changeset viewer.