Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

# tdsbytes.h

    00001 /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
    00002  * Copyright (C) 2005 Frediano Ziglio
    00003  *
    00004  * This library is free software; you can redistribute it and/or
    00005  * modify it under the terms of the GNU Library General Public
    00006  * License as published by the Free Software Foundation; either
    00007  * version 2 of the License, or (at your option) any later version.
    00008  *
    00009  * This library is distributed in the hope that it will be useful,
    00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00012  * Library General Public License for more details.
    00013  *
    00014  * You should have received a copy of the GNU Library General Public
    00015  * License along with this library; if not, write to the
    00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00017  * Boston, MA 02111-1307, USA.
    00018  */
    00019
    00020 #ifndef _tdsbytes_h_
    00021 #define _tdsbytes_h_
    00022
    00023 /* $Id: tdsbytes.h,v 1.2 2005/08/10 12:06:03 freddy77 Exp $ */
    00024
    00025 #ifndef _tds_h_
    00026 #error tds.h must be included before tdsbytes.h
    00027 #endif
    00028
    00029 /*
    00030  * read a word of n bytes aligned, architecture dependent endian
    00031  *  TDS_GET_An
    00032  * read a word of n bytes aligned, little endian
    00033  *  TDS_GET_AnLE
    00034  * read a word of n bytes aligned, big endian
    00035  *  TDS_GET_AnBE
    00036  * read a word of n bytes unaligned, architecture dependent endian
    00037  *  TDS_GET_UAn
    00038  * read a word of n bytes unaligned, little endian
    00039  *  TDS_GET_UAnLE
    00040  * read a word of n bytes unaligned, big endian
    00041  *  TDS_GET_UAnBE
    00042  */
    00043
    00044 /* TODO optimize (use swap, unaligned platforms) */
    00045
    00046 /* one byte, easy... */
    00047 #define TDS_GET_A1LE(ptr)  (((TDS_UCHAR*)(ptr))[0])
    00048 #define TDS_GET_A1BE(ptr)  TDS_GET_A1LE(ptr)
    00049 #define TDS_GET_UA1LE(ptr) TDS_GET_A1LE(ptr)
    00050 #define TDS_GET_UA1BE(ptr) TDS_GET_A1LE(ptr)
    00051
    00052 #define TDS_PUT_A1LE(ptr,val)  do { ((TDS_UCHAR*)(ptr))[0] = (val); } while(0)
    00053 #define TDS_PUT_A1BE(ptr,val)  TDS_PUT_A1LE(ptr,val)
    00054 #define TDS_PUT_UA1LE(ptr,val) TDS_PUT_A1LE(ptr,val)
    00055 #define TDS_PUT_UA1BE(ptr,val) TDS_PUT_A1LE(ptr,val)
    00056
    00057 /* two bytes */
    00058 #define TDS_GET_UA2LE(ptr) (((TDS_UCHAR*)(ptr))[1] * 0x100u + ((TDS_UCHAR*)(ptr))[0])
    00059 #define TDS_GET_UA2BE(ptr) (((TDS_UCHAR*)(ptr))[0] * 0x100u + ((TDS_UCHAR*)(ptr))[1])
    00060 #define TDS_GET_A2LE(ptr) TDS_GET_UA2LE(ptr)
    00061 #define TDS_GET_A2BE(ptr) TDS_GET_UA2BE(ptr)
    00062
    00063 #define TDS_PUT_UA2LE(ptr,val) do {\
    00064  ((TDS_UCHAR*)(ptr))[1] = (val)>>8; ((TDS_UCHAR*)(ptr))[0] = (val); } while(0)
    00065 #define TDS_PUT_UA2BE(ptr,val) do {\
    00066  ((TDS_UCHAR*)(ptr))[0] = (val)>>8; ((TDS_UCHAR*)(ptr))[1] = (val); } while(0)
    00067 #define TDS_PUT_A2LE(ptr,val) TDS_PUT_UA2LE(ptr,val)
    00068 #define TDS_PUT_A2BE(ptr,val) TDS_PUT_UA2BE(ptr,val)
    00069
    00070 /* four bytes */
    00071 #define TDS_GET_UA4LE(ptr) \
    00072         (((TDS_UCHAR*)(ptr))[3] * 0x1000000u + ((TDS_UCHAR*)(ptr))[2] * 0x10000u +\
    00073          ((TDS_UCHAR*)(ptr))[1] * 0x100u + ((TDS_UCHAR*)(ptr))[0])
    00074 #define TDS_GET_UA4BE(ptr) \
    00075         (((TDS_UCHAR*)(ptr))[0] * 0x1000000u + ((TDS_UCHAR*)(ptr))[1] * 0x10000u +\
    00076          ((TDS_UCHAR*)(ptr))[2] * 0x100u + ((TDS_UCHAR*)(ptr))[3])
    00077 #define TDS_GET_A4LE(ptr) TDS_GET_UA4LE(ptr)
    00078 #define TDS_GET_A4BE(ptr) TDS_GET_UA4BE(ptr)
    00079
    00080 #define TDS_PUT_UA4LE(ptr,val) do {\
    00081  ((TDS_UCHAR*)(ptr))[3] = (val)>>24; ((TDS_UCHAR*)(ptr))[2] = (val)>>16;\
    00082  ((TDS_UCHAR*)(ptr))[1] = (val)>>8; ((TDS_UCHAR*)(ptr))[0] = (val); } while(0)
    00083 #define TDS_PUT_UA4BE(ptr,val) do {\
    00084  ((TDS_UCHAR*)(ptr))[0] = (val)>>24; ((TDS_UCHAR*)(ptr))[1] = (val)>>16;\
    00085  ((TDS_UCHAR*)(ptr))[2] = (val)>>8; ((TDS_UCHAR*)(ptr))[3] = (val); } while(0)
    00086 #define TDS_PUT_A4LE(ptr,val) TDS_PUT_UA4LE(ptr,val)
    00087 #define TDS_PUT_A4BE(ptr,val) TDS_PUT_UA4BE(ptr,val)
    00088
    00089 /* architecture dependent */
    00090 #ifdef WORDS_BIGENDIAN
    00091 # define TDS_GET_A1(ptr)  TDS_GET_A1BE(ptr)
    00092 # define TDS_GET_UA1(ptr) TDS_GET_UA1BE(ptr)
    00093 # define TDS_GET_A2(ptr)  TDS_GET_A2BE(ptr)
    00094 # define TDS_GET_UA2(ptr) TDS_GET_UA2BE(ptr)
    00095 # define TDS_GET_A4(ptr)  TDS_GET_A4BE(ptr)
    00096 # define TDS_GET_UA4(ptr) TDS_GET_UA4BE(ptr)
    00097 # undef TDS_GET_A2BE
    00098 # undef TDS_GET_A4BE
    00099 # define TDS_GET_A2BE(ptr) (*((TDS_USMALLINT*)(ptr)))
    00100 # define TDS_GET_A4BE(ptr) (*((TDS_UINT*)(ptr)))
    00101
    00102 # define TDS_PUT_A1(ptr,val)  TDS_PUT_A1BE(ptr,val)
    00103 # define TDS_PUT_UA1(ptr,val) TDS_PUT_UA1BE(ptr,val)
    00104 # define TDS_PUT_A2(ptr,val)  TDS_PUT_A2BE(ptr,val)
    00105 # define TDS_PUT_UA2(ptr,val) TDS_PUT_UA2BE(ptr,val)
    00106 # define TDS_PUT_A4(ptr,val)  TDS_PUT_A4BE(ptr,val)
    00107 # define TDS_PUT_UA4(ptr,val) TDS_PUT_UA4BE(ptr,val)
    00108 # undef TDS_PUT_A2BE
    00109 # undef TDS_PUT_A4BE
    00110 # define TDS_PUT_A2BE(ptr,val) (*((TDS_USMALLINT*)(ptr)) = (val))
    00111 # define TDS_PUT_A4BE(ptr,val) (*((TDS_UINT*)(ptr)) = (val))
    00112 #else
    00113 # define TDS_GET_A1(ptr)  TDS_GET_A1LE(ptr)
    00114 # define TDS_GET_UA1(ptr) TDS_GET_UA1LE(ptr)
    00115 # define TDS_GET_A2(ptr)  TDS_GET_A2LE(ptr)
    00116 # define TDS_GET_UA2(ptr) TDS_GET_UA2LE(ptr)
    00117 # define TDS_GET_A4(ptr)  TDS_GET_A4LE(ptr)
    00118 # define TDS_GET_UA4(ptr) TDS_GET_UA4LE(ptr)
    00119 # undef TDS_GET_A2LE
    00120 # undef TDS_GET_A4LE
    00121 # define TDS_GET_A2LE(ptr) (*((TDS_USMALLINT*)(ptr)))
    00122 # define TDS_GET_A4LE(ptr) (*((TDS_UINT*)(ptr)))
    00123
    00124 # define TDS_PUT_A1(ptr,val)  TDS_PUT_A1LE(ptr,val)
    00125 # define TDS_PUT_UA1(ptr,val) TDS_PUT_UA1LE(ptr,val)
    00126 # define TDS_PUT_A2(ptr,val)  TDS_PUT_A2LE(ptr,val)
    00127 # define TDS_PUT_UA2(ptr,val) TDS_PUT_UA2LE(ptr,val)
    00128 # define TDS_PUT_A4(ptr,val)  TDS_PUT_A4LE(ptr,val)
    00129 # define TDS_PUT_UA4(ptr,val) TDS_PUT_UA4LE(ptr,val)
    00130 # undef TDS_PUT_A2LE
    00131 # undef TDS_PUT_A4LE
    00132 # define TDS_PUT_A2LE(ptr,val) (*((TDS_USMALLINT*)(ptr)) = (val))
    00133 # define TDS_PUT_A4LE(ptr,val) (*((TDS_UINT*)(ptr)) = (val))
    00134 #endif
    00135
    00136 /* these platform support unaligned fetch/store */
    00137 #if defined(__i386__) || defined(__amd64__) || defined(__CRIS__) ||\
    00138   defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__) ||\
    00139   defined(__s390__) || defined(__s390x__) || defined(__m68k__)
    00140 # ifdef WORDS_BIGENDIAN
    00141 #  undef TDS_GET_UA2BE
    00142 #  undef TDS_GET_UA4BE
    00143 #  define TDS_GET_UA2BE(ptr) TDS_GET_A2BE(ptr)
    00144 #  define TDS_GET_UA4BE(ptr) TDS_GET_A4BE(ptr)
    00145
    00146 #  undef TDS_PUT_UA2BE
    00147 #  undef TDS_PUT_UA4BE
    00148 #  define TDS_PUT_UA2BE(ptr,val) TDS_PUT_A2BE(ptr,val)
    00149 #  define TDS_PUT_UA4BE(ptr,val) TDS_PUT_A4BE(ptr,val)
    00150 # else
    00151 #  undef TDS_GET_UA2LE
    00152 #  undef TDS_GET_UA4LE
    00153 #  define TDS_GET_UA2LE(ptr) TDS_GET_A2LE(ptr)
    00154 #  define TDS_GET_UA4LE(ptr) TDS_GET_A4LE(ptr)
    00155
    00156 #  undef TDS_PUT_UA2LE
    00157 #  undef TDS_PUT_UA4LE
    00158 #  define TDS_PUT_UA2LE(ptr,val) TDS_PUT_A2LE(ptr,val)
    00159 #  define TDS_PUT_UA4LE(ptr,val) TDS_PUT_A4LE(ptr,val)
    00160 # endif
    00161 #endif
    00162
    00163 #if defined(__linux__) && defined(__GNUC__) && defined(__i386__)
    00164 # include <byteswap.h>
    00165 # undef TDS_GET_UA2BE
    00166 # undef TDS_GET_UA4BE
    00167 # define TDS_GET_UA2BE(ptr) ({ TDS_USMALLINT _tds_si = TDS_GET_UA2LE(ptr); bswap_16(_tds_si); })
    00168 # define TDS_GET_UA4BE(ptr) ({ TDS_UINT _tds_i = TDS_GET_UA4LE(ptr); bswap_32(_tds_i); })
    00169
    00170 # undef TDS_PUT_UA2BE
    00171 # undef TDS_PUT_UA4BE
    00172 # define TDS_PUT_UA2BE(ptr,val) do {\
    00173    TDS_USMALLINT _tds_si = bswap_16(val); TDS_PUT_UA2LE(ptr,_tds_si); } while(0)
    00174 # define TDS_PUT_UA4BE(ptr,val) do {\
    00175    TDS_UINT _tds_i = bswap_32(val); TDS_PUT_UA4LE(ptr,_tds_i); } while(0)
    00176 #endif
    00177
    00178 #endif