Mercurial > hg > th-libs
view th_endian.h @ 134:4094fcfd4783
Rename functions.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 24 Sep 2014 20:01:13 +0300 |
parents | aa2d608fb3f3 |
children | 51eec969b07a |
line wrap: on
line source
/* * Endianess handling * Programmed and designed by Matti 'ccr' Hamalainen * (C) Copyright 2002-2012 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 "th_types.h" #include <stdio.h> #ifdef __cplusplus extern "C" { #endif /* Check endianess */ #ifndef TH_BYTEORDER # error Undefined byteorder (TH_BYTEORDER not set.) #endif #define TH_BIG_ENDIAN 1234 #define TH_LITTLE_ENDIAN 4321 /* 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 ... */ #if (TH_BYTEORDER == TH_BIG_ENDIAN) #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 // !TH_BIG_ENDIAN #elif (TH_BYTEORDER == TH_LITTLE_ENDIAN) #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 #else # error Unsupported byte order! #endif /* Endian-handling file read/write routines */ BOOL th_fread_str(FILE *f, uint8_t *s, size_t l); BOOL th_fwrite_str(FILE *f, uint8_t *s, size_t l); BOOL th_fread_byte(FILE *f, uint8_t *b); BOOL th_fwrite_byte(FILE *f, uint8_t b); #define TH_DEFINE_HEADER(xname, xtype) \ BOOL th_fread_ ## xname (FILE *f, xtype *v); \ BOOL th_fwrite_ ## 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