FreeTDS API: /home/jklowden/releases/freetds-0.91/include/tdsbytes.h Source File

- Main Page
- Related Pages
- Modules
- Classes
- Files

- File List
- File Members

# /home/jklowden/releases/freetds-0.91/include/tdsbytes.h

    00001 /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
    00002  * Copyright (C) 2005-2008 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.5 2010/07/17 20:05:52 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] = (TDS_UCHAR)((val)>>8); ((TDS_UCHAR*)(ptr))[0] = (TDS_UCHAR)(val); } while(0)
    00065 #define TDS_PUT_UA2BE(ptr,val) do {\
    00066  ((TDS_UCHAR*)(ptr))[0] = (TDS_UCHAR)((val)>>8); ((TDS_UCHAR*)(ptr))[1] = (TDS_UCHAR)(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] = (TDS_UCHAR)((val)>>24); ((TDS_UCHAR*)(ptr))[2] = (TDS_UCHAR)((val)>>16);\
    00082  ((TDS_UCHAR*)(ptr))[1] = (TDS_UCHAR)((val)>>8); ((TDS_UCHAR*)(ptr))[0] = (TDS_UCHAR)(val); } while(0)
    00083 #define TDS_PUT_UA4BE(ptr,val) do {\
    00084  ((TDS_UCHAR*)(ptr))[0] = (TDS_UCHAR)((val)>>24); ((TDS_UCHAR*)(ptr))[1] = (TDS_UCHAR)((val)>>16);\
    00085  ((TDS_UCHAR*)(ptr))[2] = (TDS_UCHAR)((val)>>8); ((TDS_UCHAR*)(ptr))[3] = (TDS_UCHAR)(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 # define TDS_HOST2LE(val) TDS_BYTE_SWAP16(val)
    00113 # define TDS_HOST4LE(val) TDS_BYTE_SWAP32(val)
    00114 # define TDS_HOST2BE(val) (val)
    00115 # define TDS_HOST4BE(val) (val)
    00116 #else
    00117 # define TDS_GET_A1(ptr)  TDS_GET_A1LE(ptr)
    00118 # define TDS_GET_UA1(ptr) TDS_GET_UA1LE(ptr)
    00119 # define TDS_GET_A2(ptr)  TDS_GET_A2LE(ptr)
    00120 # define TDS_GET_UA2(ptr) TDS_GET_UA2LE(ptr)
    00121 # define TDS_GET_A4(ptr)  TDS_GET_A4LE(ptr)
    00122 # define TDS_GET_UA4(ptr) TDS_GET_UA4LE(ptr)
    00123 # undef TDS_GET_A2LE
    00124 # undef TDS_GET_A4LE
    00125 # define TDS_GET_A2LE(ptr) (*((TDS_USMALLINT*)(ptr)))
    00126 # define TDS_GET_A4LE(ptr) (*((TDS_UINT*)(ptr)))
    00127
    00128 # define TDS_PUT_A1(ptr,val)  TDS_PUT_A1LE(ptr,val)
    00129 # define TDS_PUT_UA1(ptr,val) TDS_PUT_UA1LE(ptr,val)
    00130 # define TDS_PUT_A2(ptr,val)  TDS_PUT_A2LE(ptr,val)
    00131 # define TDS_PUT_UA2(ptr,val) TDS_PUT_UA2LE(ptr,val)
    00132 # define TDS_PUT_A4(ptr,val)  TDS_PUT_A4LE(ptr,val)
    00133 # define TDS_PUT_UA4(ptr,val) TDS_PUT_UA4LE(ptr,val)
    00134 # undef TDS_PUT_A2LE
    00135 # undef TDS_PUT_A4LE
    00136 # define TDS_PUT_A2LE(ptr,val) (*((TDS_USMALLINT*)(ptr)) = (val))
    00137 # define TDS_PUT_A4LE(ptr,val) (*((TDS_UINT*)(ptr)) = (val))
    00138 # define TDS_HOST2LE(val) (val)
    00139 # define TDS_HOST4LE(val) (val)
    00140 # define TDS_HOST2BE(val) TDS_BYTE_SWAP16(val)
    00141 # define TDS_HOST4BE(val) TDS_BYTE_SWAP32(val)
    00142 #endif
    00143
    00144 /* these platform support unaligned fetch/store */
    00145 #if defined(__i386__) || defined(__amd64__) || defined(__CRIS__) ||\
    00146   defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__) ||\
    00147   defined(__s390__) || defined(__s390x__) || defined(__m68k__)
    00148 # ifdef WORDS_BIGENDIAN
    00149 #  undef TDS_GET_UA2BE
    00150 #  undef TDS_GET_UA4BE
    00151 #  define TDS_GET_UA2BE(ptr) TDS_GET_A2BE(ptr)
    00152 #  define TDS_GET_UA4BE(ptr) TDS_GET_A4BE(ptr)
    00153
    00154 #  undef TDS_PUT_UA2BE
    00155 #  undef TDS_PUT_UA4BE
    00156 #  define TDS_PUT_UA2BE(ptr,val) TDS_PUT_A2BE(ptr,val)
    00157 #  define TDS_PUT_UA4BE(ptr,val) TDS_PUT_A4BE(ptr,val)
    00158 # else
    00159 #  undef TDS_GET_UA2LE
    00160 #  undef TDS_GET_UA4LE
    00161 #  define TDS_GET_UA2LE(ptr) TDS_GET_A2LE(ptr)
    00162 #  define TDS_GET_UA4LE(ptr) TDS_GET_A4LE(ptr)
    00163
    00164 #  undef TDS_PUT_UA2LE
    00165 #  undef TDS_PUT_UA4LE
    00166 #  define TDS_PUT_UA2LE(ptr,val) TDS_PUT_A2LE(ptr,val)
    00167 #  define TDS_PUT_UA4LE(ptr,val) TDS_PUT_A4LE(ptr,val)
    00168 # endif
    00169 #endif
    00170
    00171 #if defined(__linux__) && defined(__GNUC__) && defined(__i386__)
    00172 # include <byteswap.h>
    00173 # undef TDS_GET_UA2BE
    00174 # undef TDS_GET_UA4BE
    00175 # define TDS_GET_UA2BE(ptr) ({ TDS_USMALLINT _tds_si = TDS_GET_UA2LE(ptr); bswap_16(_tds_si); })
    00176 # define TDS_GET_UA4BE(ptr) ({ TDS_UINT _tds_i = TDS_GET_UA4LE(ptr); bswap_32(_tds_i); })
    00177
    00178 # undef TDS_PUT_UA2BE
    00179 # undef TDS_PUT_UA4BE
    00180 # define TDS_PUT_UA2BE(ptr,val) do {\
    00181    TDS_USMALLINT _tds_si = bswap_16(val); TDS_PUT_UA2LE(ptr,_tds_si); } while(0)
    00182 # define TDS_PUT_UA4BE(ptr,val) do {\
    00183    TDS_UINT _tds_i = bswap_32(val); TDS_PUT_UA4LE(ptr,_tds_i); } while(0)
    00184 #endif
    00185
    00186 #endif

All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines