xdr.h

00001 /* 
00002    EyeDB Object Database Management System
00003    Copyright (C) 1994-2008 SYSRA
00004    
00005    EyeDB is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009    
00010    EyeDB is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014    
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA 
00018 */
00019 
00020 /*
00021    Author: Eric Viara <viara@sysra.com>
00022 */
00023 
00024 
00025 #ifndef _EYEDBLIB_XDR_H
00026 #define _EYEDBLIB_XDR_H
00027 
00028 #include <netinet/in.h>
00029 #include <eyedblib/machtypes.h>
00030 #include <eyedblib/endian.h>
00031 
00032 #define XDR_MACROS
00033 
00034 /*#define INTERNAL_XDR*/
00035 
00036 #ifdef INTERNAL_XDR
00037 #undef htonl
00038 #undef htons
00039 #undef ntohl
00040 #undef ntohs
00041 #endif
00042 
00043 #ifdef XDR_MACROS
00044 
00045 /* host to external macros (h2x) */
00046 #define h2x_16(x) (eyedblib::int16)htons(x)
00047 #define h2x_u16(x) (eyedblib::uint16)h2x_16((eyedblib::uint16)(x))
00048 #define h2x_32(x) (eyedblib::int32)htonl(x)
00049 #define h2x_u32(x) (eyedblib::uint32)h2x_32((eyedblib::uint32)(x))
00050 
00051 #ifdef EYEDBLIB_BIG_ENDIAN
00052 #define h2x_64(x) (x)
00053 #else
00054 #define h2x_64(x) (((unsigned long long)htonl(x)) << 32 | htonl((x >> 32) & 0xffffffff))
00055 #endif
00056 
00057 /* external to host macros (x2h) */
00058 
00059 #define x2h_u64(x) ((eyedblib::uint64)x2h_64((eyedblib::uint64)(x)))
00060 #define x2h_16(x) (eyedblib::int16)ntohs(x)
00061 #define x2h_u16(x) (eyedblib::uint16)x2h_16((eyedblib::uint16)(x))
00062 #define x2h_32(x) (eyedblib::int32)ntohl(x)
00063 #define x2h_u32(x) (eyedblib::uint32)x2h_32((eyedblib::uint32)(x))
00064 
00065 #ifdef EYEDBLIB_BIG_ENDIAN
00066 #define x2h_64(x) (x)
00067 #else
00068 #define x2h_64(x) (((unsigned long long)ntohl(x)) << 32 | ntohl((x >> 32) & 0xffffffff))
00069 #endif
00070 
00071 #define h2x_u64(x) ((eyedblib::uint64)h2x_64((eyedblib::uint64)(x)))
00072 
00073 /* host to external copy macros (h2x_cpy) */
00074 
00075 #ifdef EYEDBLIB_BIG_ENDIAN
00076 
00077 #define h2x_16_cpy(to, from) \
00078 do { \
00079   if (from) eyedblib_mcp((to), (from), sizeof(eyedblib::int16)); \
00080 } while(0)
00081 
00082 #define h2x_32_cpy(to, from) \
00083 do { \
00084   if (from) eyedblib_mcp((to), (from), sizeof(eyedblib::int32)); \
00085 } while(0)
00086 
00087 #define h2x_64_cpy(to, from) \
00088 do { \
00089   if (from) eyedblib_mcp((to), (from), sizeof(eyedblib::int64)); \
00090 } while(0)
00091 
00092 #else
00093 
00094 #define h2x_16_cpy(to, xfrom) \
00095 do { \
00096   eyedblib::int16 sx; \
00097   const void *from = (xfrom); \
00098   if (!from) from = (to); \
00099   eyedblib_mcp(&sx, (from), sizeof(sx)); \
00100   sx = h2x_16(sx); \
00101   eyedblib_mcp(to, &sx, sizeof(sx)); \
00102 } while(0)
00103 
00104 #define h2x_32_cpy(to, xfrom) \
00105 do { \
00106   eyedblib::int32 sx; \
00107   const void *from = (xfrom); \
00108   if (!from) from = (to); \
00109   eyedblib_mcp(&sx, (from), sizeof(sx)); \
00110   sx = h2x_32(sx); \
00111   eyedblib_mcp(to, &sx, sizeof(sx)); \
00112 } while(0)
00113 
00114 #define h2x_64_cpy(to, xfrom) \
00115 do { \
00116   eyedblib::int64 sx; \
00117   const void *from = (xfrom); \
00118   if (!from) from = (to); \
00119   eyedblib_mcp(&sx, (from), sizeof(sx)); \
00120   sx = h2x_64(sx); \
00121   eyedblib_mcp(to, &sx, sizeof(sx)); \
00122 } while(0)
00123 #endif
00124 
00125 /* external to host copy macros (x2h_cpy) */
00126 
00127 #ifdef EYEDBLIB_BIG_ENDIAN
00128 
00129 #define x2h_16_cpy(to, from) if (from) eyedblib_mcp((to), (from), sizeof(eyedblib::int16))
00130 #define x2h_32_cpy(to, from) if (from) eyedblib_mcp((to), (from), sizeof(eyedblib::int32))
00131 #define x2h_64_cpy(to, from) if (from) eyedblib_mcp((to), (from), sizeof(eyedblib::int64))
00132 
00133 #else
00134 
00135 #define x2h_16_cpy(to, xfrom) \
00136 { \
00137   eyedblib::int16 sx; \
00138   const void *from = (xfrom); \
00139   if (!from) from = (to); \
00140   eyedblib_mcp(&sx, (from), sizeof(sx)); \
00141   sx = x2h_16(sx); \
00142   eyedblib_mcp(to, &sx, sizeof(sx)); \
00143 }
00144 
00145 #define x2h_32_cpy(to, xfrom) \
00146 { \
00147   eyedblib::int32 sx; \
00148   const void *from = (xfrom); \
00149   if (!from) from = (to); \
00150   eyedblib_mcp(&sx, (from), sizeof(sx)); \
00151   sx = x2h_32(sx); \
00152   eyedblib_mcp(to, &sx, sizeof(sx)); \
00153 }
00154 
00155 #define x2h_64_cpy(to, xfrom) \
00156 { \
00157   eyedblib::int64 sx; \
00158   const void *from = (xfrom); \
00159   if (!from) from = (to); \
00160   eyedblib_mcp(&sx, (from), sizeof(sx)); \
00161   sx = x2h_64(sx); \
00162   eyedblib_mcp(to, &sx, sizeof(sx)); \
00163 }
00164 #endif
00165 
00166 #else
00167 
00168 /* host to external functions (h2x) */
00169 extern eyedblib::int16 h2x_16(eyedblib::int16);
00170 extern eyedblib::uint16 h2x_u16(eyedblib::uint16);
00171 extern eyedblib::int32 h2x_32(eyedblib::int32);
00172 extern eyedblib::uint16 h2x_u32(eyedblib::uint32);
00173 extern eyedblib::int64 h2x_64(eyedblib::int64);
00174 extern eyedblib::uint16 h2x_u64(eyedblib::uint64);
00175 
00176 /* external to host functions (x2h) */
00177 extern eyedblib::int16 x2h_16(eyedblib::int16);
00178 extern eyedblib::uint16 x2h_u16(eyedblib::uint16);
00179 extern eyedblib::int32 x2h_32(eyedblib::int32);
00180 extern eyedblib::uint16 x2h_u32(eyedblib::uint32);
00181 extern eyedblib::int64 x2h_64(eyedblib::int64);
00182 extern eyedblib::uint16 x2h_u64(eyedblib::uint64);
00183 
00184 extern void h2x_16_cpy(void *to, const void *from);
00185 extern void h2x_32_cpy(void *to, const void *from);
00186 extern void h2x_64_cpy(void *to, const void *from);
00187 
00188 extern void x2h_16_cpy(void *to, const void *from);
00189 extern void x2h_32_cpy(void *to, const void *from);
00190 extern void x2h_64_cpy(void *to, const void *from);
00191 
00192 #endif
00193 
00194 extern eyedblib::float32 h2x_f32(eyedblib::float32);
00195 extern eyedblib::float64 h2x_f64(eyedblib::float64);
00196 
00197 #define h2x_f h2x_f32
00198 #define h2x_d h2x_f64
00199 
00200 #define h2x_f32_cpy h2x_32_cpy
00201 #define h2x_f64_cpy h2x_64_cpy
00202 
00203 extern eyedblib::float32 x2h_f32(eyedblib::float32);
00204 extern eyedblib::float64 x2h_f64(eyedblib::float64);
00205 
00206 #define x2h_f x2h_f32
00207 #define x2h_d x2h_f64
00208 
00209 #define x2h_f32_cpy x2h_32_cpy
00210 #define x2h_f64_cpy x2h_64_cpy
00211 
00212 #define h2x_16_icp(addr) h2x_16_cpy(addr, 0)
00213 #define h2x_32_icp(addr) h2x_32_cpy(addr, 0)
00214 #define h2x_64_icp(addr) h2x_64_cpy(addr, 0)
00215 
00216 #define x2h_16_icp(addr) x2h_16_cpy(addr, 0)
00217 #define x2h_32_icp(addr) x2h_32_cpy(addr, 0)
00218 #define x2h_64_icp(addr) x2h_64_cpy(addr, 0)
00219 
00220 #define x2h_nop(x) (x)
00221 #define h2x_nop(x) (x)
00222 
00223 #endif

Generated on Mon Dec 22 18:16:15 2008 for eyedb by  doxygen 1.5.3