/*****************************************************************************\ * Copyright (c) 2004 Mark Aylett * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * * "Software"), to deal in the Software without restriction, including * * without limitation the rights to use, copy, modify, merge, publish, * * distribute, sublicense, and/or sell copies of the Software, and to permit * * persons to whom the Software is furnished to do so, subject to the * * following conditions: * * * * The above copyright notice and this permission notice shall be included * * in all copies or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN * * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * * USE OR OTHER DEALINGS IN THE SOFTWARE. * \*****************************************************************************/ static const char rcsid[] = "$Id: mar_format_c.c,v 1.2 2004/12/14 13:09:23 marayl Exp $"; #include "mar_format_c.h" #if !defined(WIN32) #include #include #define mar_ntohs ntohs #define mar_ntohl ntohl #define mar_htons htons #define mar_htonl htonl #else /* WIN32 */ /** * The _X86_ macro is defined in the windows header. */ #include "mar_windows_c.h" #if !defined(_X86_) #include #if defined(_MSC_VER) #pragma comment(lib, "ws2_32.lib") #endif /* _MSC_VER */ #define mar_ntohs ntohs #define mar_ntohl ntohl #define mar_htons htons #define mar_htonl htonl #else /* _X86_ */ /** * Avoid linking to the winsock library where little endian is known. */ static mar_uint16_t mar_swap16(mar_uint16_t i) { return (i & 0xff00) >> 8 | (i & 0x00ff) << 8; } static mar_uint32_t mar_swap32(mar_uint32_t i) { return (i & 0xff000000) >> 24 | (i & 0x00ff0000) >> 8 | (i & 0x0000ff00) << 8 | (i & 0x000000ff) << 24; } #define mar_ntohs mar_swap16 #define mar_ntohl mar_swap32 #define mar_htons mar_swap16 #define mar_htonl mar_swap32 #endif /* _X86_ */ #endif /* WIN32 */ MAR_EXTERN mar_uint16_t mar_decode16_(const mar_byte_t* ptr) { mar_uint16_t i; memcpy(&i, ptr, sizeof(i)); return mar_ntohs(i); } MAR_EXTERN mar_uint32_t mar_decode32_(const mar_byte_t* ptr) { mar_uint32_t i; memcpy(&i, ptr, sizeof(i)); return mar_ntohl(i); } MAR_EXTERN void mar_encode16_(mar_byte_t* ptr, mar_uint16_t i) { i = mar_htons(i); memcpy(ptr, &i, sizeof(i)); } MAR_EXTERN void mar_encode32_(mar_byte_t* ptr, mar_uint32_t i) { i = mar_htonl(i); memcpy(ptr, &i, sizeof(i)); }