view th_endian.h @ 5:8552edc844a7

Re-wrote endianess handling.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 20 Apr 2008 00:13:49 +0300
parents bd61a80a6c54
children a25f5d22483e
line wrap: on
line source

/*
 * Endianess handling
 * Programmed and designed by Matti 'ccr' Hamalainen
 * (C) Copyright 2002-2007 Tecnic Software productions (TNSP)
 *
 * Please read file 'COPYING' for information on license and distribution.
 */
#ifndef _TH_ENDIAN_H
#define _TH_ENDIAN_H

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif
#include <stdio.h>
#include "th_types.h"

#ifdef __cplusplus
extern "C" {
#endif


/* Endianess swapping macros
 */
#define TH_SWAP_16_LE_BE(value)	((uint16_t) (   \
    (uint16_t) ((uint16_t) (value) >> 8) |      \
    (uint16_t) ((uint16_t) (value) << 8)) )


#define TH_SWAP_32_LE_BE(value) ((uint32_t) (               \
    (((uint32_t) (value) & (uint32_t) 0x000000ffU) << 24) | \
    (((uint32_t) (value) & (uint32_t) 0x0000ff00U) <<  8) | \
    (((uint32_t) (value) & (uint32_t) 0x00ff0000U) >>  8) | \
    (((uint32_t) (value) & (uint32_t) 0xff000000U) >> 24)))

#ifdef TH_HAVE_64BIT
#define TH_SWAP_64_LE_BE(value) ((uint64_t) (                           \
    (((uint64_t) (value) & (uint64_t) 0x00000000000000ffULL) << 56) |   \
    (((uint64_t) (value) & (uint64_t) 0x000000000000ff00ULL) << 40) |   \
    (((uint64_t) (value) & (uint64_t) 0x0000000000ff0000ULL) << 24) |   \
    (((uint64_t) (value) & (uint64_t) 0x00000000ff000000ULL) <<  8) |   \
    (((uint64_t) (value) & (uint64_t) 0x000000ff00000000ULL) >>  8) |   \
    (((uint64_t) (value) & (uint64_t) 0x0000ff0000000000ULL) >> 24) |   \
    (((uint64_t) (value) & (uint64_t) 0x00ff000000000000ULL) >> 40) |   \
    (((uint64_t) (value) & (uint64_t) 0xff00000000000000ULL) >> 56)))
#endif

/* Macros that swap only when needed ...
 */
#ifdef TH_BIGENDIAN

#define TH_LE16_TO_NATIVE(value) TH_SWAP_16_LE_BE(value)
#define TH_LE32_TO_NATIVE(value) TH_SWAP_32_LE_BE(value)
#define TH_NATIVE_TO_LE16(value) TH_SWAP_16_LE_BE(value)
#define TH_NATIVE_TO_LE32(value) TH_SWAP_32_LE_BE(value)

#define TH_BE16_TO_NATIVE(value) ((uint16_t) (value))
#define TH_BE32_TO_NATIVE(value) ((uint32_t) (value))
#define TH_NATIVE_TO_BE16(value) ((uint16_t) (value))
#define TH_NATIVE_TO_BE32(value) ((uint32_t) (value))

#ifdef TH_HAVE_64BIT
#define TH_LE64_TO_NATIVE(value) TH_SWAP_64_LE_BE(value)
#define TH_NATIVE_TO_LE64(value) TH_SWAP_64_LE_BE(value)
#define TH_BE64_TO_NATIVE(value) ((uint64_t) (value))
#define TH_NATIVE_TO_BE64(value) ((uint64_t) (value))
#endif

#else /* !TH_BIGENDIAN */

#define TH_LE16_TO_NATIVE(value) ((uint16_t) (value))
#define TH_LE32_TO_NATIVE(value) ((uint32_t) (value))
#define TH_NATIVE_TO_LE16(value) ((uint16_t) (value))
#define TH_NATIVE_TO_LE32(value) ((uint32_t) (value))

#define TH_BE16_TO_NATIVE(value) TH_SWAP_16_LE_BE(value)
#define TH_BE32_TO_NATIVE(value) TH_SWAP_32_LE_BE(value)
#define TH_NATIVE_TO_BE16(value) TH_SWAP_16_LE_BE(value)
#define TH_NATIVE_TO_BE32(value) TH_SWAP_32_LE_BE(value)

#ifdef TH_HAVE_64BIT
#define TH_LE64_TO_NATIVE(value) ((uint64_t) (value))
#define TH_NATIVE_TO_LE64(value) ((uint64_t) (value))
#define TH_BE64_TO_NATIVE(value) TH_SWAP_64_LE_BE(value)
#define TH_NATIVE_TO_BE64(value) TH_SWAP_64_LE_BE(value)
#endif

#endif


/* Endian-handling file read/write routines
 */
BOOL    th_read_str(FILE *f, uint8_t *s, size_t l);
BOOL    th_write_str(FILE *f, uint8_t *s, size_t l);

#define TH_DEFINE_HEADER(xname, xtype)          \
BOOL    th_read_ ## xname (FILE *f, xtype *v);  \
BOOL    th_write_ ## xname (FILE *f, xtype v);

TH_DEFINE_HEADER(le16, uint16_t)
TH_DEFINE_HEADER(le32, uint32_t)

TH_DEFINE_HEADER(be16, uint16_t)
TH_DEFINE_HEADER(be32, uint32_t)

#ifdef TH_HAVE_64BIT
TH_DEFINE_HEADER(be64, uint64_t)
TH_DEFINE_HEADER(le64, uint64_t)
#endif

#undef TH_DEFINE_HEADER

/* Cause warnings for old functions */
#define TH_READ_LE16    fdksajlkfdsljf lol
#define TH_READ_LE32    fdksajlkfdsljf lol
#define TH_READ_BE16    fdksajlkfdsljf lol
#define TH_READ_BE32    fdksajlkfdsljf lol

#ifdef __cplusplus
}
#endif
#endif /* _TH_ENDIAN_H */